From 5728b5be2f1523628cc19baae5b73baf8e128983 Mon Sep 17 00:00:00 2001 From: Stanley Wang Date: Wed, 9 Dec 2020 17:12:21 +0800 Subject: [PATCH] Create a main switch preference widget. Based on our ux spec, we create this widget to let everyone can follow up our spec easily. Bug: 175181773 Test: Run robotest and apply this widget in Settings and see the ui Change-Id: I4ea5f38c44aacfeb2c8dbe4a7d31159712f95441 --- packages/SettingsLib/Android.bp | 1 + .../MainSwitchPreference/Android.bp | 14 + .../MainSwitchPreference/AndroidManifest.xml | 23 ++ .../res/drawable/thumb_off.xml | 25 ++ .../res/drawable/thumb_on.xml | 25 ++ .../res/drawable/thumb_selector.xml | 21 ++ .../res/drawable/track_off.xml | 26 ++ .../res/drawable/track_off_background.xml | 34 +++ .../res/drawable/track_off_indicator.xml | 34 +++ .../res/drawable/track_on.xml | 26 ++ .../res/drawable/track_on_background.xml | 34 +++ .../res/drawable/track_on_indicator.xml | 31 ++ .../res/drawable/track_selector.xml | 21 ++ .../res/layout/main_switch_bar.xml | 73 +++++ .../res/layout/main_switch_layout.xml | 30 ++ .../res/values-night/colors.xml | 25 ++ .../res/values/colors.xml | 26 ++ .../res/values/dimens.xml | 37 +++ .../res/values/styles.xml | 29 ++ .../settingslib/widget/MainSwitchBar.java | 283 ++++++++++++++++++ .../widget/MainSwitchPreference.java | 146 +++++++++ .../widget/OnMainSwitchChangeListener.java | 30 ++ .../widget/MainSwitchPreferenceTest.java | 78 +++++ 23 files changed, 1072 insertions(+) create mode 100644 packages/SettingsLib/MainSwitchPreference/Android.bp create mode 100644 packages/SettingsLib/MainSwitchPreference/AndroidManifest.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_off.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_on.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_selector.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/drawable/track_off.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/drawable/track_off_background.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/drawable/track_off_indicator.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/drawable/track_on.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/drawable/track_on_background.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/drawable/track_on_indicator.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/drawable/track_selector.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_bar.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_layout.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/values-night/colors.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/values/colors.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/values/dimens.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/res/values/styles.xml create mode 100644 packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java create mode 100644 packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java create mode 100644 packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/OnMainSwitchChangeListener.java create mode 100644 packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/MainSwitchPreferenceTest.java diff --git a/packages/SettingsLib/Android.bp b/packages/SettingsLib/Android.bp index 23cb36fc2a90c..3834162219011 100644 --- a/packages/SettingsLib/Android.bp +++ b/packages/SettingsLib/Android.bp @@ -43,6 +43,7 @@ java_defaults { "SettingsLibSearchWidget", "SettingsLibSettingsSpinner", "SettingsLibLayoutPreference", + "SettingsLibMainSwitchPreference", "SettingsLibActionButtonsPreference", "SettingsLibEntityHeaderWidgets", "SettingsLibBarChartPreference", diff --git a/packages/SettingsLib/MainSwitchPreference/Android.bp b/packages/SettingsLib/MainSwitchPreference/Android.bp new file mode 100644 index 0000000000000..1dc18f5cc4d22 --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/Android.bp @@ -0,0 +1,14 @@ +android_library { + name: "SettingsLibMainSwitchPreference", + + srcs: ["src/**/*.java"], + resource_dirs: ["res"], + + static_libs: [ + "androidx.preference_preference", + "SettingsLibRestrictedLockUtils", + ], + + sdk_version: "system_current", + min_sdk_version: "21", +} diff --git a/packages/SettingsLib/MainSwitchPreference/AndroidManifest.xml b/packages/SettingsLib/MainSwitchPreference/AndroidManifest.xml new file mode 100644 index 0000000000000..96d9e518663f9 --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_off.xml b/packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_off.xml new file mode 100644 index 0000000000000..8a73fb52de757 --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_off.xml @@ -0,0 +1,25 @@ + + + + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_on.xml b/packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_on.xml new file mode 100644 index 0000000000000..8a0af00b0d619 --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_on.xml @@ -0,0 +1,25 @@ + + + + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_selector.xml b/packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_selector.xml new file mode 100644 index 0000000000000..8cc9bb3d5198a --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/drawable/thumb_selector.xml @@ -0,0 +1,21 @@ + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/drawable/track_off.xml b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_off.xml new file mode 100644 index 0000000000000..1be3a8eda19ee --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_off.xml @@ -0,0 +1,26 @@ + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/drawable/track_off_background.xml b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_off_background.xml new file mode 100644 index 0000000000000..3cc490f9815cb --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_off_background.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/drawable/track_off_indicator.xml b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_off_indicator.xml new file mode 100644 index 0000000000000..6cc6224ae6c5d --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_off_indicator.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/drawable/track_on.xml b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_on.xml new file mode 100644 index 0000000000000..2553891437822 --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_on.xml @@ -0,0 +1,26 @@ + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/drawable/track_on_background.xml b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_on_background.xml new file mode 100644 index 0000000000000..02e3a84957bcd --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_on_background.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/drawable/track_on_indicator.xml b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_on_indicator.xml new file mode 100644 index 0000000000000..2281d045e1010 --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_on_indicator.xml @@ -0,0 +1,31 @@ + + + + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/drawable/track_selector.xml b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_selector.xml new file mode 100644 index 0000000000000..5c699be4939af --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/drawable/track_selector.xml @@ -0,0 +1,21 @@ + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_bar.xml b/packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_bar.xml new file mode 100644 index 0000000000000..66bb2f376ec5b --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_bar.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_layout.xml b/packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_layout.xml new file mode 100644 index 0000000000000..efc047cbd69e5 --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/layout/main_switch_layout.xml @@ -0,0 +1,30 @@ + + + + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/values-night/colors.xml b/packages/SettingsLib/MainSwitchPreference/res/values-night/colors.xml new file mode 100644 index 0000000000000..ecbb84acac25d --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/values-night/colors.xml @@ -0,0 +1,25 @@ + + + + + + @*android:color/primary_text_dark + #BFFFFFFF + @*android:color/material_grey_600 + @*android:color/material_grey_600 + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/values/colors.xml b/packages/SettingsLib/MainSwitchPreference/res/values/colors.xml new file mode 100644 index 0000000000000..b4013988fc849 --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/values/colors.xml @@ -0,0 +1,26 @@ + + + + + + @android:color/transparent + @*android:color/primary_text_light + #BFFFFFFF + @*android:color/accent_device_default_dark + @*android:color/material_grey_600 + + diff --git a/packages/SettingsLib/MainSwitchPreference/res/values/dimens.xml b/packages/SettingsLib/MainSwitchPreference/res/values/dimens.xml new file mode 100644 index 0000000000000..dd443deeb8695 --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/values/dimens.xml @@ -0,0 +1,37 @@ + + + + + + + 26dp + + + 24dp + + + 48dp + + + 72dp + + + @*android:dimen/config_restrictedIconSize + + + 16dp + diff --git a/packages/SettingsLib/MainSwitchPreference/res/values/styles.xml b/packages/SettingsLib/MainSwitchPreference/res/values/styles.xml new file mode 100644 index 0000000000000..fbb896c76295a --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/res/values/styles.xml @@ -0,0 +1,29 @@ + + + + + + + + + + diff --git a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java new file mode 100644 index 0000000000000..1db64e3b5c450 --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java @@ -0,0 +1,283 @@ +/* + * Copyright (C) 2020 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. + */ + +package com.android.settingslib.widget; + +import android.content.Context; +import android.os.Parcel; +import android.os.Parcelable; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.CompoundButton; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.Switch; +import android.widget.TextView; + +import com.android.settingslib.RestrictedLockUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * MainSwitchBar is a View with a customized Switch. + * This component is used as the main switch of the page + * to enable or disable the prefereces on the page. + */ +public class MainSwitchBar extends LinearLayout implements CompoundButton.OnCheckedChangeListener { + + private final List mSwitchChangeListeners = new ArrayList<>(); + + private View mAboveDivider; + private View mBelowDivider; + private TextView mTextView; + private ImageView mRestrictedIcon; + private Switch mSwitch; + + private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin; + private boolean mDisabledByAdmin; + + public MainSwitchBar(Context context) { + this(context, null); + } + + public MainSwitchBar(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public MainSwitchBar(Context context, AttributeSet attrs, int defStyleAttr) { + this(context, attrs, defStyleAttr, 0); + } + + public MainSwitchBar(Context context, AttributeSet attrs, int defStyleAttr, + int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + + LayoutInflater.from(context).inflate(R.layout.main_switch_bar, this); + + setFocusable(true); + setClickable(true); + + mTextView = (TextView) findViewById(R.id.switch_text); + mSwitch = (Switch) findViewById(android.R.id.switch_widget); + + addOnSwitchChangeListener((switchView, isChecked) -> setChecked(isChecked)); + + mRestrictedIcon = findViewById(R.id.restricted_icon); + mRestrictedIcon.setOnClickListener((View v) -> { + if (mDisabledByAdmin) { + RestrictedLockUtils.sendShowAdminSupportDetailsIntent(context, mEnforcedAdmin); + onRestrictedIconClick(); + } + }); + + setChecked(mSwitch.isChecked()); + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + propagateChecked(isChecked); + } + + @Override + public boolean performClick() { + return getDelegatingView().performClick(); + } + + /** + * Update the switch status + */ + public void setChecked(boolean checked) { + if (mSwitch != null) { + mSwitch.setChecked(checked); + } + } + + /** + * Set the title text + */ + public void setTitle(String text) { + if (mTextView != null) { + mTextView.setText(text); + } + } + + /** + * Show the MainSwitchBar + */ + public void show() { + if (!isShowing()) { + setVisibility(View.VISIBLE); + mSwitch.setOnCheckedChangeListener(this); + } + } + + /** + * Hide the MainSwitchBar + */ + public void hide() { + if (isShowing()) { + setVisibility(View.GONE); + mSwitch.setOnCheckedChangeListener(null); + } + } + + /** + * Return the displaying status of MainSwitchBar + */ + public boolean isShowing() { + return (getVisibility() == View.VISIBLE); + } + + /** + * Adds a listener for switch changes + */ + public void addOnSwitchChangeListener(OnMainSwitchChangeListener listener) { + if (!mSwitchChangeListeners.contains(listener)) { + mSwitchChangeListeners.add(listener); + } + } + + /** + * Remove a listener for switch changes + */ + public void removeOnSwitchChangeListener(OnMainSwitchChangeListener listener) { + if (mSwitchChangeListeners.contains(listener)) { + mSwitchChangeListeners.remove(listener); + } + } + + /** + * If admin is not null, disables the text and switch but keeps the view clickable. + * Otherwise, calls setEnabled which will enables the entire view including + * the text and switch. + */ + public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) { + mEnforcedAdmin = admin; + if (admin != null) { + super.setEnabled(true); + mDisabledByAdmin = true; + mTextView.setEnabled(false); + mSwitch.setEnabled(false); + mSwitch.setVisibility(View.GONE); + mRestrictedIcon.setVisibility(View.VISIBLE); + } else { + mDisabledByAdmin = false; + mSwitch.setVisibility(View.VISIBLE); + mRestrictedIcon.setVisibility(View.GONE); + setEnabled(true); + } + } + + /** + * Enable or disable the text and switch. + */ + public void setEnabled(boolean enabled) { + if (enabled && mDisabledByAdmin) { + setDisabledByAdmin(null); + return; + } + super.setEnabled(enabled); + mTextView.setEnabled(enabled); + mSwitch.setEnabled(enabled); + } + + /** + * Called by the restricted icon clicked. + */ + protected void onRestrictedIconClick() { + } + + private View getDelegatingView() { + return mDisabledByAdmin ? mRestrictedIcon : mSwitch; + } + + private void propagateChecked(boolean isChecked) { + final int count = mSwitchChangeListeners.size(); + for (int n = 0; n < count; n++) { + mSwitchChangeListeners.get(n).onSwitchChanged(mSwitch, isChecked); + } + } + + static class SavedState extends BaseSavedState { + boolean mChecked; + boolean mVisible; + + SavedState(Parcelable superState) { + super(superState); + } + + /** + * Constructor called from {@link #CREATOR} + */ + private SavedState(Parcel in) { + super(in); + mChecked = (Boolean) in.readValue(null); + mVisible = (Boolean) in.readValue(null); + } + + @Override + public void writeToParcel(Parcel out, int flags) { + super.writeToParcel(out, flags); + out.writeValue(mChecked); + out.writeValue(mVisible); + } + + @Override + public String toString() { + return "MainSwitchBar.SavedState{" + + Integer.toHexString(System.identityHashCode(this)) + + " checked=" + mChecked + + " visible=" + mVisible + "}"; + } + + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + public SavedState createFromParcel(Parcel in) { + return new SavedState(in); + } + + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; + } + + @Override + public Parcelable onSaveInstanceState() { + Parcelable superState = super.onSaveInstanceState(); + + SavedState ss = new SavedState(superState); + ss.mChecked = mSwitch.isChecked(); + ss.mVisible = isShowing(); + return ss; + } + + @Override + public void onRestoreInstanceState(Parcelable state) { + SavedState ss = (SavedState) state; + + super.onRestoreInstanceState(ss.getSuperState()); + + mSwitch.setChecked(ss.mChecked); + setChecked(ss.mChecked); + setVisibility(ss.mVisible ? View.VISIBLE : View.GONE); + mSwitch.setOnCheckedChangeListener(ss.mVisible ? this : null); + + requestLayout(); + } +} diff --git a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java new file mode 100644 index 0000000000000..dae0e705794db --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchPreference.java @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2020 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. + */ + +package com.android.settingslib.widget; + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.Switch; + +import androidx.preference.PreferenceViewHolder; +import androidx.preference.TwoStatePreference; + +import com.android.settingslib.RestrictedLockUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * MainSwitchPreference is a Preference with a customized Switch. + * This component is used as the main switch of the page + * to enable or disable the prefereces on the page. + */ +public class MainSwitchPreference extends TwoStatePreference { + + private final List mSwitchChangeListeners = new ArrayList<>(); + + private MainSwitchBar mMainSwitchBar; + private Switch mSwitch; + private String mTitle; + + private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin; + + public MainSwitchPreference(Context context) { + super(context); + init(); + } + + public MainSwitchPreference(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + public MainSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(); + } + + public MainSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, + int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + init(); + } + + @Override + public void onBindViewHolder(PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + + holder.setDividerAllowedAbove(true); + holder.setDividerAllowedBelow(false); + + mMainSwitchBar = (MainSwitchBar) holder.findViewById(R.id.main_switch_bar); + updateStatus(isChecked()); + registerListenerToSwitchBar(); + } + + private void init() { + setLayoutResource(R.layout.main_switch_layout); + } + + /** + * Set the preference title text + */ + public void setTitle(String text) { + mTitle = text; + if (mMainSwitchBar != null) { + mMainSwitchBar.setTitle(mTitle); + } + } + + /** + * Update the switch status of preference + */ + public void updateStatus(boolean checked) { + setChecked(checked); + if (mMainSwitchBar != null) { + mMainSwitchBar.setChecked(checked); + mMainSwitchBar.setTitle(mTitle); + mMainSwitchBar.setDisabledByAdmin(mEnforcedAdmin); + mMainSwitchBar.show(); + } + } + + /** + * Adds a listener for switch changes + */ + public void addOnSwitchChangeListener(OnMainSwitchChangeListener listener) { + if (mMainSwitchBar == null) { + mSwitchChangeListeners.add(listener); + } else { + mMainSwitchBar.addOnSwitchChangeListener(listener); + } + } + + /** + * Remove a listener for switch changes + */ + public void removeOnSwitchChangeListener(OnMainSwitchChangeListener listener) { + if (mMainSwitchBar == null) { + mSwitchChangeListeners.remove(listener); + } else { + mMainSwitchBar.removeOnSwitchChangeListener(listener); + } + } + + /** + * If admin is not null, disables the text and switch but keeps the view clickable. + * Otherwise, calls setEnabled which will enables the entire view including + * the text and switch. + */ + public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) { + mEnforcedAdmin = admin; + if (mMainSwitchBar != null) { + mMainSwitchBar.setDisabledByAdmin(mEnforcedAdmin); + } + } + + private void registerListenerToSwitchBar() { + for (OnMainSwitchChangeListener listener : mSwitchChangeListeners) { + mMainSwitchBar.addOnSwitchChangeListener(listener); + } + mSwitchChangeListeners.clear(); + } +} diff --git a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/OnMainSwitchChangeListener.java b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/OnMainSwitchChangeListener.java new file mode 100644 index 0000000000000..1c610d9995f6b --- /dev/null +++ b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/OnMainSwitchChangeListener.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2020 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. + */ + +package com.android.settingslib.widget; + +import android.widget.Switch; + +/** + * Called when the checked state of the Switch has changed. + */ +public interface OnMainSwitchChangeListener { + /** + * @param switchView The Switch view whose state has changed. + * @param isChecked The new checked state of switchView. + */ + void onSwitchChanged(Switch switchView, boolean isChecked); +} diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/MainSwitchPreferenceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/MainSwitchPreferenceTest.java new file mode 100644 index 0000000000000..635e3bd7bf0b0 --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/MainSwitchPreferenceTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2020 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. + */ + +package com.android.settingslib.widget; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.Context; +import android.view.View; +import android.widget.TextView; + +import androidx.preference.PreferenceViewHolder; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class MainSwitchPreferenceTest { + + private Context mContext; + private View mRootView; + private PreferenceViewHolder mHolder; + private MainSwitchPreference mPreference; + + @Before + public void setUp() { + mContext = RuntimeEnvironment.application; + mRootView = View.inflate(mContext, R.layout.main_switch_layout, null /* parent */); + mHolder = PreferenceViewHolder.createInstanceForTests(mRootView); + mPreference = new MainSwitchPreference(mContext); + } + + @Test + public void setTitleText_shouldUpdateTitle() { + final String defaultOnText = "Test title"; + + mPreference.onBindViewHolder(mHolder); + mPreference.setTitle(defaultOnText); + mPreference.updateStatus(true /* checked */); + + assertThat(((TextView) mRootView.findViewById(R.id.switch_text)).getText()) + .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); + mPreference.updateStatus(true); + + assertThat(mPreference.isChecked()).isTrue(); + } + +}