Merge "Synchronize the states of MainSwitchBar and MainSwitchPreference." into sc-dev
This commit is contained in:
@@ -18,6 +18,8 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.android.settingslib.widget">
|
package="com.android.settingslib.widget">
|
||||||
|
|
||||||
<uses-sdk android:minSdkVersion="21" />
|
<uses-sdk
|
||||||
|
android:minSdkVersion="21"
|
||||||
|
android:targetSdkVersion="31"/>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ package com.android.settingslib.widget;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.TypedArray;
|
import android.content.res.TypedArray;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.widget.Switch;
|
||||||
|
|
||||||
import androidx.preference.PreferenceViewHolder;
|
import androidx.preference.PreferenceViewHolder;
|
||||||
import androidx.preference.TwoStatePreference;
|
import androidx.preference.TwoStatePreference;
|
||||||
@@ -31,7 +32,7 @@ import java.util.List;
|
|||||||
* This component is used as the main switch of the page
|
* This component is used as the main switch of the page
|
||||||
* to enable or disable the prefereces on the page.
|
* to enable or disable the prefereces on the page.
|
||||||
*/
|
*/
|
||||||
public class MainSwitchPreference extends TwoStatePreference {
|
public class MainSwitchPreference extends TwoStatePreference implements OnMainSwitchChangeListener {
|
||||||
|
|
||||||
private final List<OnMainSwitchChangeListener> mSwitchChangeListeners = new ArrayList<>();
|
private final List<OnMainSwitchChangeListener> mSwitchChangeListeners = new ArrayList<>();
|
||||||
|
|
||||||
@@ -73,7 +74,7 @@ public class MainSwitchPreference extends TwoStatePreference {
|
|||||||
|
|
||||||
private void init(Context context, AttributeSet attrs) {
|
private void init(Context context, AttributeSet attrs) {
|
||||||
setLayoutResource(R.layout.settingslib_main_switch_layout);
|
setLayoutResource(R.layout.settingslib_main_switch_layout);
|
||||||
|
mSwitchChangeListeners.add(this);
|
||||||
if (attrs != null) {
|
if (attrs != null) {
|
||||||
final TypedArray a = context.obtainStyledAttributes(attrs,
|
final TypedArray a = context.obtainStyledAttributes(attrs,
|
||||||
androidx.preference.R.styleable.Preference, 0 /*defStyleAttr*/,
|
androidx.preference.R.styleable.Preference, 0 /*defStyleAttr*/,
|
||||||
@@ -88,7 +89,7 @@ public class MainSwitchPreference extends TwoStatePreference {
|
|||||||
@Override
|
@Override
|
||||||
public void setChecked(boolean checked) {
|
public void setChecked(boolean checked) {
|
||||||
super.setChecked(checked);
|
super.setChecked(checked);
|
||||||
if (mMainSwitchBar != null) {
|
if (mMainSwitchBar != null && mMainSwitchBar.isChecked() != checked) {
|
||||||
mMainSwitchBar.setChecked(checked);
|
mMainSwitchBar.setChecked(checked);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -101,13 +102,17 @@ public class MainSwitchPreference extends TwoStatePreference {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
||||||
|
super.setChecked(isChecked);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the switch status of preference
|
* Update the switch status of preference
|
||||||
*/
|
*/
|
||||||
public void updateStatus(boolean checked) {
|
public void updateStatus(boolean checked) {
|
||||||
setChecked(checked);
|
setChecked(checked);
|
||||||
if (mMainSwitchBar != null) {
|
if (mMainSwitchBar != null) {
|
||||||
mMainSwitchBar.setChecked(checked);
|
|
||||||
mMainSwitchBar.setTitle(mTitle);
|
mMainSwitchBar.setTitle(mTitle);
|
||||||
mMainSwitchBar.show();
|
mMainSwitchBar.show();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user