Make tether option preferences listen to own SharedPreferences change

The SharedPreferences value could be changed by other classes. This can
help make UI consistent with shared preferences value.

Bug: 148968321
Test: UsbTetherPreferenceControllerTest;
BluetoothTetherPreferenceControllerTest;
WifiTetherDisablePreferenceControllerTest;
CodeInspectionTest.

Change-Id: I11bdad2729e88c07ea1f046d9e95a6a63b398931
This commit is contained in:
Zhen Zhang
2020-02-26 12:54:20 -08:00
parent 4d59bce21b
commit 084875a104
5 changed files with 134 additions and 28 deletions

View File

@@ -111,33 +111,45 @@ public final class WifiTetherDisablePreferenceController extends BasePreferenceC
super.displayPreference(screen);
mScreen = screen;
mPreference = screen.findPreference(mPreferenceKey);
if (mPreference != null && mPreference instanceof SwitchPreference) {
((SwitchPreference) mPreference)
.setChecked(!mSharedPreferences.getBoolean(
TetherEnabler.KEY_ENABLE_WIFI_TETHERING, true));
if (mPreference != null) {
mPreference.setOnPreferenceChangeListener(this);
}
updateState(mPreference);
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (preference instanceof SwitchPreference) {
((SwitchPreference) preference)
.setChecked(!mSharedPreferences.getBoolean(
TetherEnabler.KEY_ENABLE_WIFI_TETHERING, true));
}
setVisible(mScreen, mPreferenceKey, shouldShow());
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (TextUtils.equals(TetherEnabler.USB_TETHER_KEY, key)) {
mUSBTetherEnabled = sharedPreferences.getBoolean(key, false);
} else if (TextUtils.equals(TetherEnabler.BLUETOOTH_TETHER_KEY, key)) {
mBluetoothTetherEnabled = sharedPreferences.getBoolean(key, false);
final boolean shouldShowBefore = shouldShow();
if (TextUtils.equals(TetherEnabler.KEY_ENABLE_WIFI_TETHERING, key) && shouldShowBefore) {
updateState(mPreference);
return;
}
// Check if we are hiding this preference. If so, make sure the preference is set to
boolean shouldUpdateState = false;
if (TextUtils.equals(TetherEnabler.USB_TETHER_KEY, key)) {
mUSBTetherEnabled = sharedPreferences.getBoolean(key, false);
shouldUpdateState = true;
} else if (TextUtils.equals(TetherEnabler.BLUETOOTH_TETHER_KEY, key)) {
mBluetoothTetherEnabled = sharedPreferences.getBoolean(key, false);
shouldUpdateState = true;
}
// Check if we are hiding this preference. If so, make sure the preference is set to
// unchecked to enable wifi tether.
if (mPreference != null && mPreference instanceof SwitchPreference && !shouldShow()) {
if (mPreference != null && mPreference instanceof SwitchPreference
&& shouldShowBefore && !shouldShow()) {
final SwitchPreference switchPreference = (SwitchPreference) mPreference;
if (switchPreference.isChecked()) {
if (DEBUG) {
@@ -151,7 +163,9 @@ public final class WifiTetherDisablePreferenceController extends BasePreferenceC
}
}
updateState(mPreference);
if (shouldUpdateState) {
updateState(mPreference);
}
}
@Override