Merge "Update MainSwitchBar to Material Next style." into sc-dev

This commit is contained in:
Edgar Wang
2021-05-08 07:30:06 +00:00
committed by Android (Google) Code Review
21 changed files with 187 additions and 153 deletions

View File

@@ -14,7 +14,7 @@ android_library {
resource_dirs: ["res"],
static_libs: [
"androidx.preference_preference",
"androidx.preference_preference",
],
sdk_version: "system_current",

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.
@@ -15,12 +15,12 @@
limitations under the License.
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/track_on_background" />
<item
android:width="13.19dp"
android:height="10.06dp"
android:gravity="center"
android:right="21dp"
android:drawable="@drawable/track_on_indicator" />
</layer-list>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/state_off_color"/>
<corners android:radius="@dimen/switch_bar_radius"/>
</shape>
</item>
</ripple>

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.
@@ -15,20 +15,12 @@
limitations under the License.
-->
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="13.33dp"
android:height="1.67dp"
android:viewportWidth="13.33"
android:viewportHeight="1.67">
<group>
<clip-path
android:pathData="M0 0H13.3333V1.66667H0V0Z" />
<path
android:pathData="M0 0V1.66667H13.3333V0"
android:fillColor="@android:color/white" />
</group>
</vector>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/state_off_color"/>
<corners android:radius="@dimen/switch_bar_radius"/>
</shape>
</item>
</ripple>

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.
@@ -15,12 +15,12 @@
limitations under the License.
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/track_off_background" />
<item
android:width="13.33dp"
android:height="1.67dp"
android:left="21dp"
android:gravity="center"
android:drawable="@drawable/track_off_indicator" />
</layer-list>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/state_on_color"/>
<corners android:radius="@dimen/switch_bar_radius"/>
</shape>
</item>
</ripple>

View File

@@ -0,0 +1,33 @@
<?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.
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="@dimen/switch_thumb_margin"
android:left="@dimen/switch_thumb_margin"
android:right="@dimen/switch_thumb_margin"
android:bottom="@dimen/switch_thumb_margin">
<shape android:shape="oval">
<size
android:height="@dimen/switch_thumb_size"
android:width="@dimen/switch_thumb_size"/>
<solid
android:color="@color/state_off_color"
android:alpha="?android:attr/disabledAlpha"/>
</shape>
</item>
</layer-list>

View File

@@ -16,16 +16,16 @@
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="4dp"
android:left="4dp"
android:right="4dp"
android:bottom="4dp">
<shape android:shape="oval" >
<size android:height="20dp" android:width="20dp" />
<solid android:color="@color/thumb_off" />
</shape>
</item>
<item
android:top="@dimen/switch_thumb_margin"
android:left="@dimen/switch_thumb_margin"
android:right="@dimen/switch_thumb_margin"
android:bottom="@dimen/switch_thumb_margin">
<shape android:shape="oval">
<size
android:height="@dimen/switch_thumb_size"
android:width="@dimen/switch_thumb_size"/>
<solid android:color="@color/state_off_color"/>
</shape>
</item>
</layer-list>

View File

@@ -16,16 +16,16 @@
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="4dp"
android:left="4dp"
android:right="4dp"
android:bottom="4dp">
<shape android:shape="oval" >
<size android:height="20dp" android:width="20dp" />
<solid android:color="?android:attr/colorAccent" />
</shape>
</item>
<item
android:top="@dimen/switch_thumb_margin"
android:left="@dimen/switch_thumb_margin"
android:right="@dimen/switch_thumb_margin"
android:bottom="@dimen/switch_thumb_margin">
<shape android:shape="oval">
<size
android:height="@dimen/switch_thumb_size"
android:width="@dimen/switch_thumb_size"/>
<solid android:color="@color/state_on_color"/>
</shape>
</item>
</layer-list>

View File

@@ -16,6 +16,7 @@
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/thumb_on" android:state_checked="true" />
<item android:drawable="@drawable/thumb_off" android:state_checked="false" />
<item android:drawable="@drawable/thumb_on" android:state_checked="true"/>
<item android:drawable="@drawable/thumb_off" android:state_checked="false"/>
<item android:drawable="@drawable/thumb_disabled" android:state_enabled="false"/>
</selector>

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.
@@ -15,17 +15,12 @@
limitations under the License.
-->
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="13.19dp"
android:height="10.06dp"
android:viewportWidth="13.19"
android:viewportHeight="10.06">
<group>
<path
android:pathData="M4.75012 8.12738L1.62262 4.99988L0.557617 6.05738L4.75012 10.2499L13.7501 1.24988L12.6926 0.192383L4.75012 8.12738Z"
android:fillColor="@android:color/white" />
</group>
</vector>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:width="@dimen/switch_track_width"
android:height="@dimen/switch_track_height">
<solid
android:color="@color/track_off_color"
android:alpha="?android:attr/disabledAlpha"/>
<corners android:radius="@dimen/switch_track_radius"/>
</shape>

View File

@@ -15,20 +15,10 @@
limitations under the License.
-->
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="52dp"
android:height="28dp"
android:viewportWidth="52"
android:viewportHeight="28">
<group>
<clip-path
android:pathData="M14 0H38C45.732 0 52 6.26801 52 14C52 21.732 45.732 28 38 28H14C6.26801 28 0 21.732 0 14C0 6.26801 6.26801 0 14 0Z" />
<path
android:pathData="M0 0V28H52V0"
android:fillColor="@color/track_off" />
</group>
</vector>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:width="@dimen/switch_track_width"
android:height="@dimen/switch_track_height">
<solid android:color="@color/track_off_color"/>
<corners android:radius="@dimen/switch_track_radius"/>
</shape>

View File

@@ -15,22 +15,10 @@
limitations under the License.
-->
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="52dp"
android:height="28dp"
android:viewportWidth="52"
android:viewportHeight="28"
android:tint="@*android:color/switch_track_material">
<group>
<clip-path
android:pathData="M14 0H38C45.732 0 52 6.26801 52 14C52 21.732 45.732 28 38 28H14C6.26801 28 0 21.732 0 14C0 6.26801 6.26801 0 14 0Z" />
<path
android:pathData="M0 0V28H52V0"
android:fillColor="@*android:color/white_disabled_material" />
</group>
</vector>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:width="@dimen/switch_track_width"
android:height="@dimen/switch_track_height">
<solid android:color="@color/track_on_color"/>
<corners android:radius="@dimen/switch_track_radius"/>
</shape>

View File

@@ -16,6 +16,7 @@
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/track_on" android:state_checked="true" />
<item android:drawable="@drawable/track_off" android:state_checked="false" />
<item android:drawable="@drawable/track_on_background" android:state_checked="true"/>
<item android:drawable="@drawable/track_off_background" android:state_checked="false"/>
<item android:drawable="@drawable/track_disabled_background" android:state_enabled="false"/>
</selector>

View File

@@ -19,6 +19,10 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:background="?android:attr/colorBackground"
android:orientation="vertical">
@@ -27,7 +31,6 @@
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">
@@ -41,7 +44,6 @@
android:maxLines="2"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textAlignment="viewStart"
style="@style/MainSwitchText" />
<ImageView
@@ -58,18 +60,13 @@
<Switch
android:id="@android:id/switch_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="48dp"
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

@@ -54,4 +54,3 @@
android:theme="@style/Widget.SwitchBar.Switch"/>
</LinearLayout>

View File

@@ -15,11 +15,10 @@
limitations under the License.
-->
<resources>
<color name="thumb_off">#BFFFFFFF</color>
<color name="track_off">@*android:color/material_grey_600</color>
<resources xmlns:androidprv="http://schemas.android.com/apk/prv/res/android">
<color name="switchbar_switch_track_tint">#82000000</color>
<color name="switchbar_switch_thumb_tint">@android:color/black</color>
<!-- Material next track on color-->
<color name="track_on_color">?androidprv:attr/colorSurfaceHighlight</color>
</resources>

View File

@@ -15,12 +15,20 @@
limitations under the License.
-->
<resources>
<color name="thumb_off">#BFFFFFFF</color>
<color name="track_off">@*android:color/material_grey_600</color>
<resources xmlns:androidprv="http://schemas.android.com/apk/prv/res/android">
<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>
<!-- Material next state on color-->
<color name="state_on_color">?androidprv:attr/colorAccentPrimary</color>
<!-- Material next state off color-->
<color name="state_off_color">?androidprv:attr/colorAccentSecondary</color>
<!-- Material next track on color-->
<color name="track_on_color">?androidprv:attr/colorAccentPrimaryVariant</color>
<!-- Material next track off color-->
<color name="track_off_color">?androidprv:attr/colorAccentSecondaryVariant</color>
</resources>

View File

@@ -18,7 +18,7 @@
<resources>
<!-- Size of layout margin left -->
<dimen name="switchbar_margin_start">22dp</dimen>
<dimen name="switchbar_margin_start">24dp</dimen>
<!-- Size of layout margin right -->
<dimen name="switchbar_margin_end">16dp</dimen>
@@ -35,6 +35,24 @@
<!-- Restricted icon in switch bar -->
<dimen name="restricted_icon_margin_end">16dp</dimen>
<!-- Radius of switch bar -->
<dimen name="switch_bar_radius">28dp</dimen>
<!-- Margin of switch thumb -->
<dimen name="switch_thumb_margin">4dp</dimen>
<!-- Size of switch thumb -->
<dimen name="switch_thumb_size">20dp</dimen>
<!-- Width of switch track -->
<dimen name="switch_track_width">52dp</dimen>
<!-- Height of switch track -->
<dimen name="switch_track_height">28dp</dimen>
<!-- Radius of switch track -->
<dimen name="switch_track_radius">35dp</dimen>
<!-- SwitchBar sub settings margin start / end -->
<dimen name="switchbar_subsettings_margin_start">72dp</dimen>
<dimen name="switchbar_subsettings_margin_end">16dp</dimen>

View File

@@ -19,6 +19,8 @@
<style name="MainSwitchText">
<item name="android:textSize">20sp</item>
<item name="android:fontFamily">@*android:string/config_headlineFontFamily</item>
<item name="android:textColor">@android:color/black</item>
</style>
<style name="Settings.MainSwitch" parent="@android:style/Widget.Material.CompoundButton.Switch">

View File

@@ -18,6 +18,7 @@ package com.android.settingslib.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
@@ -50,6 +51,10 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
protected TextView mTextView;
protected Switch mSwitch;
private Drawable mBackgroundOn;
private Drawable mBackgroundOff;
private Drawable mBackgroundDisabled;
private View mFrameView;
public MainSwitchBar(Context context) {
this(context, null);
@@ -81,8 +86,12 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
setFocusable(true);
setClickable(true);
mFrameView = findViewById(R.id.frame);
mTextView = (TextView) findViewById(R.id.switch_text);
mSwitch = (Switch) findViewById(android.R.id.switch_widget);
mBackgroundOn = getContext().getDrawable(R.drawable.switch_bar_bg_on);
mBackgroundOff = getContext().getDrawable(R.drawable.switch_bar_bg_off);
mBackgroundDisabled = getContext().getDrawable(R.drawable.switch_bar_bg_disabled);
addOnSwitchChangeListener((switchView, isChecked) -> setChecked(isChecked));
@@ -194,21 +203,31 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
super.setEnabled(enabled);
mTextView.setEnabled(enabled);
mSwitch.setEnabled(enabled);
if (BuildCompat.isAtLeastS()) {
if (enabled) {
mFrameView.setBackground(isChecked() ? mBackgroundOn : mBackgroundOff);
} else {
mFrameView.setBackground(mBackgroundDisabled);
}
}
}
private void propagateChecked(boolean isChecked) {
setBackground(isChecked);
final int count = mSwitchChangeListeners.size();
for (int n = 0; n < count; n++) {
mSwitchChangeListeners.get(n).onSwitchChanged(mSwitch, isChecked);
}
}
private void setBackground(boolean checked) {
if (BuildCompat.isAtLeastS()) {
return;
private void setBackground(boolean isChecked) {
if (!BuildCompat.isAtLeastS()) {
setBackgroundColor(isChecked ? mBackgroundActivatedColor : mBackgroundColor);
} else {
mFrameView.setBackground(isChecked ? mBackgroundOn : mBackgroundOff);
}
setBackgroundColor(checked ? mBackgroundActivatedColor : mBackgroundColor);
}
static class SavedState extends BaseSavedState {
@@ -273,6 +292,7 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
mSwitch.setChecked(ss.mChecked);
setChecked(ss.mChecked);
setBackground(ss.mChecked);
setVisibility(ss.mVisible ? View.VISIBLE : View.GONE);
mSwitch.setOnCheckedChangeListener(ss.mVisible ? this : null);

View File

@@ -63,7 +63,7 @@ public class MainSwitchPreference extends TwoStatePreference {
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
holder.setDividerAllowedAbove(true);
holder.setDividerAllowedAbove(false);
holder.setDividerAllowedBelow(false);
mMainSwitchBar = (MainSwitchBar) holder.findViewById(R.id.main_switch_bar);

View File

@@ -58,15 +58,6 @@ public class MainSwitchPreferenceTest {
.isEqualTo(defaultOnText);
}
@Test
public void shouldAllowDividerBelow() {
mPreference.onBindViewHolder(mHolder);
View divider = mRootView.findViewById(R.id.below_divider);
assertThat(divider.getVisibility()).isEqualTo(View.VISIBLE);
}
@Test
public void updateStatus_shouldMatchTheStatus() {
mPreference.onBindViewHolder(mHolder);