Add backward compatible for MainSwitchPreference

- Before Android S, it will apply SwitchBar layout
- Fix TypedArrayUtils can't build pass in another path.

Bug: 182179573
Test: robotest & manual
Change-Id: I33ca1991cef359f8e95650c7616bb76cee3bb7ff
This commit is contained in:
Edgar Wang
2021-03-09 02:35:57 +08:00
parent 0d4e510467
commit 43e006b69b
11 changed files with 217 additions and 57 deletions

View File

@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2021 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?android:attr/colorBackground"
android:orientation="vertical">
<LinearLayout
android:id="@+id/frame"
android:minHeight="@dimen/min_switch_bar_height"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?android:attr/selectableItemBackground"
android:paddingLeft="@dimen/switchbar_margin_start"
android:paddingRight="@dimen/switchbar_margin_end">
<TextView
android:id="@+id/switch_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginRight="16dp"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textAlignment="viewStart"
style="@style/MainSwitchText" />
<ImageView
android:id="@+id/restricted_icon"
android:layout_width="@dimen/restricted_icon_size"
android:layout_height="@dimen/restricted_icon_size"
android:tint="?android:attr/colorAccent"
android:theme="@android:style/Theme.Material"
android:layout_gravity="center_vertical"
android:layout_marginEnd="@dimen/restricted_icon_margin_end"
android:src="@*android:drawable/ic_info"
android:visibility="gone" />
<Switch
android:id="@android:id/switch_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:track="@drawable/track_selector"
android:thumb="@drawable/thumb_selector"
android:theme="@style/Settings.MainSwitch"/>
</LinearLayout>
<View
android:id="@+id/below_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
</LinearLayout>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2020 The Android Open Source Project
Copyright (C) 2021 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -18,58 +18,40 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?android:attr/colorBackground"
android:orientation="vertical">
android:layout_width="match_parent">
<LinearLayout
android:id="@+id/frame"
android:minHeight="@dimen/min_switch_bar_height"
<TextView
android:id="@+id/switch_text"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?android:attr/selectableItemBackground"
android:paddingLeft="@dimen/switchbar_margin_start"
android:paddingRight="@dimen/switchbar_margin_end">
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:ellipsize="end"
android:textAppearance="@*android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title"
android:textSize="16sp"
android:textColor="?android:attr/textColorPrimaryInverse"
android:layout_marginStart="@dimen/switchbar_subsettings_margin_start"
android:textAlignment="viewStart"/>
<TextView
android:id="@+id/switch_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginRight="16dp"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textAlignment="viewStart"
style="@style/MainSwitchText" />
<ImageView
android:id="@+id/restricted_icon"
android:layout_width="@dimen/restricted_icon_size"
android:layout_height="@dimen/restricted_icon_size"
android:tint="?android:attr/colorAccent"
android:theme="@android:style/Theme.Material"
android:layout_gravity="center_vertical"
android:layout_marginEnd="@dimen/restricted_icon_margin_end"
android:src="@*android:drawable/ic_info"
android:visibility="gone"/>
<ImageView
android:id="@+id/restricted_icon"
android:layout_width="@dimen/restricted_icon_size"
android:layout_height="@dimen/restricted_icon_size"
android:tint="?android:attr/colorAccent"
android:theme="@android:style/Theme.Material"
android:layout_gravity="center_vertical"
android:layout_marginEnd="@dimen/restricted_icon_margin_end"
android:src="@*android:drawable/ic_info"
android:visibility="gone" />
<Switch
android:id="@android:id/switch_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:track="@drawable/track_selector"
android:thumb="@drawable/thumb_selector"
android:theme="@style/Settings.MainSwitch"/>
</LinearLayout>
<View
android:id="@+id/below_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
<Switch
android:id="@android:id/switch_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="@dimen/switchbar_subsettings_margin_end"
android:theme="@style/Widget.SwitchBar.Switch"/>
</LinearLayout>

View File

@@ -20,4 +20,6 @@
<color name="thumb_off">#BFFFFFFF</color>
<color name="track_off">@*android:color/material_grey_600</color>
<color name="switchbar_switch_track_tint">#82000000</color>
<color name="switchbar_switch_thumb_tint">@android:color/black</color>
</resources>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2021 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<!-- SwitchBar sub settings margin start / end -->
<dimen name="switchbar_subsettings_margin_start">80dp</dimen>
</resources>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2021 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<!-- SwitchBar sub settings margin start / end -->
<dimen name="switchbar_subsettings_margin_start">128dp</dimen>
<dimen name="switchbar_subsettings_margin_end">128dp</dimen>
</resources>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2021 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<!-- SwitchBar sub settings margin start / end -->
<dimen name="switchbar_subsettings_margin_start">80dp</dimen>
<dimen name="switchbar_subsettings_margin_end">80dp</dimen>
</resources>

View File

@@ -20,4 +20,7 @@
<color name="thumb_off">#BFFFFFFF</color>
<color name="track_off">@*android:color/material_grey_600</color>
<color name="switchbar_background_color">@*android:color/material_grey_600</color>
<color name="switchbar_switch_track_tint">#BFFFFFFF</color>
<color name="switchbar_switch_thumb_tint">@android:color/white</color>
</resources>

View File

@@ -34,4 +34,8 @@
<!-- Restricted icon in switch bar -->
<dimen name="restricted_icon_margin_end">16dp</dimen>
<!-- SwitchBar sub settings margin start / end -->
<dimen name="switchbar_subsettings_margin_start">72dp</dimen>
<dimen name="switchbar_subsettings_margin_end">16dp</dimen>
</resources>

View File

@@ -25,4 +25,10 @@
<item name="android:switchMinWidth">@dimen/min_switch_width</item>
</style>
<style name="Widget.SwitchBar.Switch" parent="@android:style/Widget.Material.CompoundButton.Switch">
<item name="android:trackTint">@color/switchbar_switch_track_tint</item>
<item name="android:thumbTint">@color/switchbar_switch_thumb_tint</item>
<item name="android:minHeight">48dp</item>
<item name="android:minWidth">48dp</item>
</style>
</resources>

View File

@@ -28,7 +28,8 @@ import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import androidx.core.content.res.TypedArrayUtils;
import androidx.annotation.ColorInt;
import androidx.core.os.BuildCompat;
import java.util.ArrayList;
import java.util.List;
@@ -42,8 +43,11 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
private final List<OnMainSwitchChangeListener> mSwitchChangeListeners = new ArrayList<>();
private View mAboveDivider;
private View mBelowDivider;
@ColorInt
private int mBackgroundColor;
@ColorInt
private int mBackgroundActivatedColor;
protected TextView mTextView;
protected Switch mSwitch;
@@ -66,6 +70,14 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
LayoutInflater.from(context).inflate(resourceId(context, "layout", "main_switch_bar"),
this);
if (!BuildCompat.isAtLeastS()) {
final TypedArray a = context.obtainStyledAttributes(
new int[]{android.R.attr.colorAccent});
mBackgroundActivatedColor = a.getColor(0, 0);
mBackgroundColor = context.getColor(R.color.switchbar_background_color);
a.recycle();
}
setFocusable(true);
setClickable(true);
@@ -80,12 +92,13 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
final TypedArray a = context.obtainStyledAttributes(attrs,
androidx.preference.R.styleable.Preference, 0 /*defStyleAttr*/,
0 /*defStyleRes*/);
final CharSequence title = TypedArrayUtils.getText(a,
androidx.preference.R.styleable.Preference_title,
final CharSequence title = a.getText(
androidx.preference.R.styleable.Preference_android_title);
setTitle(title);
a.recycle();
}
setBackground(true);
}
@Override
@@ -105,6 +118,7 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
if (mSwitch != null) {
mSwitch.setChecked(checked);
}
setBackground(checked);
}
/**
@@ -189,6 +203,14 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
}
}
private void setBackground(boolean checked) {
if (BuildCompat.isAtLeastS()) {
return;
}
setBackgroundColor(checked ? mBackgroundActivatedColor : mBackgroundColor);
}
static class SavedState extends BaseSavedState {
boolean mChecked;
boolean mVisible;

View File

@@ -20,7 +20,6 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import androidx.core.content.res.TypedArrayUtils;
import androidx.preference.PreferenceViewHolder;
import androidx.preference.TwoStatePreference;
@@ -79,8 +78,7 @@ public class MainSwitchPreference extends TwoStatePreference {
final TypedArray a = context.obtainStyledAttributes(attrs,
androidx.preference.R.styleable.Preference, 0 /*defStyleAttr*/,
0 /*defStyleRes*/);
final CharSequence title = TypedArrayUtils.getText(a,
androidx.preference.R.styleable.Preference_title,
final CharSequence title = a.getText(
androidx.preference.R.styleable.Preference_android_title);
setTitle(title);
a.recycle();