Merge changes I3b1d1c4e,I11bdad27 into rvc-dev

* changes:
  Let tether preference controllers extend TogglePreferenceController
  Make tether option preferences listen to own SharedPreferences change
This commit is contained in:
Zhen Zhang
2020-03-04 01:53:07 +00:00
committed by Android (Google) Code Review
5 changed files with 162 additions and 64 deletions

View File

@@ -31,9 +31,8 @@ import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.TogglePreferenceController;
import com.google.common.annotations.VisibleForTesting;
@@ -41,8 +40,8 @@ import com.google.common.annotations.VisibleForTesting;
* This controller helps to manage the switch state and visibility of bluetooth tether switch
* preference. It stores preference value when preference changed.
*/
public final class BluetoothTetherPreferenceController extends BasePreferenceController
implements LifecycleObserver, Preference.OnPreferenceChangeListener {
public final class BluetoothTetherPreferenceController extends TogglePreferenceController
implements LifecycleObserver, SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "BluetoothTetherPreferenceController";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
@@ -58,6 +57,22 @@ public final class BluetoothTetherPreferenceController extends BasePreferenceCon
context.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE);
}
@Override
public boolean isChecked() {
return mSharedPreferences.getBoolean(mPreferenceKey, false);
}
@Override
public boolean setChecked(boolean isChecked) {
if (DEBUG) {
Log.d(TAG, "preference changing to " + isChecked);
}
final SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(mPreferenceKey, isChecked);
editor.apply();
return true;
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mBluetoothState = BluetoothAdapter.getDefaultAdapter().getState();
@@ -65,6 +80,16 @@ public final class BluetoothTetherPreferenceController extends BasePreferenceCon
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
mSharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
mSharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
mContext.unregisterReceiver(mBluetoothChangeReceiver);
@@ -74,15 +99,15 @@ public final class BluetoothTetherPreferenceController extends BasePreferenceCon
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(mPreferenceKey);
if (mPreference != null && mPreference instanceof SwitchPreference) {
((SwitchPreference) mPreference)
.setChecked(mSharedPreferences.getBoolean(mPreferenceKey, false));
}
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (preference == null) {
return;
}
switch (mBluetoothState) {
case BluetoothAdapter.STATE_ON:
case BluetoothAdapter.STATE_OFF:
@@ -120,15 +145,10 @@ public final class BluetoothTetherPreferenceController extends BasePreferenceCon
}
};
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
if (DEBUG) {
Log.d(TAG, "preference changing to " + o);
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (TextUtils.equals(mPreferenceKey, key)) {
updateState(mPreference);
}
final SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(mPreferenceKey, (Boolean) o);
editor.apply();
return true;
}
}

View File

@@ -32,19 +32,18 @@ import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.TogglePreferenceController;
/**
* This controller helps to manage the switch state and visibility of USB tether switch
* preference. It stores preference values when preference changed.
*
*/
public final class UsbTetherPreferenceController extends BasePreferenceController implements
LifecycleObserver, Preference.OnPreferenceChangeListener {
public final class UsbTetherPreferenceController extends TogglePreferenceController implements
LifecycleObserver, SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "UsbTetherPrefController";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
@@ -62,6 +61,22 @@ public final class UsbTetherPreferenceController extends BasePreferenceControlle
context.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE);
}
@Override
public boolean isChecked() {
return mSharedPreferences.getBoolean(mPreferenceKey, false);
}
@Override
public boolean setChecked(boolean isChecked) {
if (DEBUG) {
Log.d(TAG, "preference changing to " + isChecked);
}
final SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(mPreferenceKey, isChecked);
editor.apply();
return true;
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
@@ -71,6 +86,17 @@ public final class UsbTetherPreferenceController extends BasePreferenceControlle
mContext.registerReceiver(mUsbChangeReceiver, filter);
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
mSharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
mSharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
mContext.unregisterReceiver(mUsbChangeReceiver);
@@ -90,21 +116,13 @@ public final class UsbTetherPreferenceController extends BasePreferenceControlle
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(mPreferenceKey);
if (mPreference != null && mPreference instanceof SwitchPreference) {
((SwitchPreference) mPreference)
.setChecked(mSharedPreferences.getBoolean(mPreferenceKey, false));
}
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (preference != null) {
if (mUsbConnected && !mMassStorageActive) {
preference.setEnabled(true);
} else {
preference.setEnabled(false);
}
preference.setEnabled(mUsbConnected && !mMassStorageActive);
}
}
@@ -125,13 +143,9 @@ public final class UsbTetherPreferenceController extends BasePreferenceControlle
};
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
if (DEBUG) {
Log.d(TAG, "preference changing to " + o);
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (TextUtils.equals(mPreferenceKey, key)) {
updateState(mPreference);
}
final SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(mPreferenceKey, (Boolean) o);
editor.apply();
return true;
}
}

View File

@@ -31,7 +31,7 @@ import androidx.preference.SwitchPreference;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.TetherUtil;
/**
@@ -43,9 +43,8 @@ import com.android.settingslib.TetherUtil;
* @see BluetoothTetherPreferenceController
* @see UsbTetherPreferenceController
*/
public final class WifiTetherDisablePreferenceController extends BasePreferenceController
implements LifecycleObserver, Preference.OnPreferenceChangeListener,
SharedPreferences.OnSharedPreferenceChangeListener {
public final class WifiTetherDisablePreferenceController extends TogglePreferenceController
implements LifecycleObserver, SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "WifiTetherDisablePreferenceController";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
@@ -68,6 +67,24 @@ public final class WifiTetherDisablePreferenceController extends BasePreferenceC
TetherEnabler.BLUETOOTH_TETHER_KEY, false);
}
@Override
public boolean isChecked() {
return !mSharedPreferences.getBoolean(TetherEnabler.KEY_ENABLE_WIFI_TETHERING, true);
}
@Override
public boolean setChecked(boolean isChecked) {
// The shared preference's value is in the opposite of this preference's value.
final boolean enableWifi = !isChecked;
if (DEBUG) {
Log.d(TAG, "check state changing to " + isChecked);
}
final SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(TetherEnabler.KEY_ENABLE_WIFI_TETHERING, enableWifi);
editor.apply();
return true;
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
mSharedPreferences.registerOnSharedPreferenceChangeListener(this);
@@ -111,13 +128,9 @@ 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
@@ -129,15 +142,25 @@ public final class WifiTetherDisablePreferenceController extends BasePreferenceC
@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,19 +174,8 @@ public final class WifiTetherDisablePreferenceController extends BasePreferenceC
}
}
updateState(mPreference);
}
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
// The shared preference's value is in the opposite of this preference's value.
final boolean enableWifi = !(boolean) o;
if (DEBUG) {
Log.d(TAG, "check state changing to " + o);
if (shouldUpdateState) {
updateState(mPreference);
}
final SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(TetherEnabler.KEY_ENABLE_WIFI_TETHERING, enableWifi);
editor.apply();
return true;
}
}

View File

@@ -27,8 +27,10 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
@@ -37,13 +39,17 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class BluetoothTetherPreferenceControllerTest {
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
private SharedPreferences mSharedPreferences;
private SwitchPreference mSwitchPreference;
private BluetoothTetherPreferenceController mController;
private Context mContext;
@@ -52,10 +58,14 @@ public class BluetoothTetherPreferenceControllerTest {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
mSwitchPreference = spy(SwitchPreference.class);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mContext.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE))
.thenReturn(mSharedPreferences);
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[] {""});
mController = new BluetoothTetherPreferenceController(mContext, BLUETOOTH_TETHER_KEY);
ReflectionHelpers.setField(mController, "mPreference", mSwitchPreference);
}
@Test
@@ -84,4 +94,20 @@ public class BluetoothTetherPreferenceControllerTest {
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void switch_shouldCheckedWhenSharedPreferenceIsTrue() {
when(mSharedPreferences.getBoolean(BLUETOOTH_TETHER_KEY, false)).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences, BLUETOOTH_TETHER_KEY);
verify(mSwitchPreference).setChecked(true);
}
@Test
public void switch_shouldUnCheckedWhenSharedPreferenceIsFalse() {
when(mSharedPreferences.getBoolean(BLUETOOTH_TETHER_KEY, false)).thenReturn(false);
mController.onSharedPreferenceChanged(mSharedPreferences, BLUETOOTH_TETHER_KEY);
verify(mSwitchPreference).setChecked(false);
}
}

View File

@@ -27,8 +27,10 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
@@ -37,15 +39,19 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class UsbTetherPreferenceControllerTest {
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
private SharedPreferences mSharedPreferences;
private Context mContext;
private UsbTetherPreferenceController mController;
private SwitchPreference mSwitchPreference;
@Before
public void setUp() {
@@ -55,7 +61,11 @@ public class UsbTetherPreferenceControllerTest {
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[]{""});
when(mContext.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE))
.thenReturn(mSharedPreferences);
mController = new UsbTetherPreferenceController(mContext, USB_TETHER_KEY);
mSwitchPreference = spy(SwitchPreference.class);
ReflectionHelpers.setField(mController, "mPreference", mSwitchPreference);
}
@Test
@@ -81,4 +91,20 @@ public class UsbTetherPreferenceControllerTest {
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void switch_shouldCheckedWhenSharedPreferencesIsTrue() {
when(mSharedPreferences.getBoolean(USB_TETHER_KEY, false)).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences, USB_TETHER_KEY);
verify(mSwitchPreference).setChecked(true);
}
@Test
public void switch_shouldUnCheckedWhenSharedPreferencesIsFalse() {
when(mSharedPreferences.getBoolean(USB_TETHER_KEY, false)).thenReturn(false);
mController.onSharedPreferenceChanged(mSharedPreferences, USB_TETHER_KEY);
verify(mSwitchPreference).setChecked(false);
}
}