Make tether preference controllers extend BasePreferenceController

These controllers extended AbstractPreferenceController, which is not
quite compatible with some of the present framework. Especially for
indexable keys of settings search.

TetherEnabler and AllInOneTetherSettings are refactored based on the
change. Tests are updated.

Bug: 147272749
Test: CodeInspectionTest and other tests of classes touched by this CL
Change-Id: Ic3ad13e735d133c0959a0a309319a6fd5165b015
This commit is contained in:
Zhen Zhang
2020-02-05 10:09:27 -08:00
parent c240b27862
commit 258fb7f672
9 changed files with 77 additions and 102 deletions

View File

@@ -20,7 +20,7 @@ import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED;
import static android.net.ConnectivityManager.TETHERING_WIFI;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_CHANGED_ACTION;
import static com.android.settings.network.WifiTetherDisablePreferenceController.KEY_ENABLE_WIFI_TETHERING;
import static com.android.settings.network.TetherEnabler.KEY_ENABLE_WIFI_TETHERING;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;

View File

@@ -33,34 +33,29 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.core.BasePreferenceController;
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.
* TODO(b/147272749): Extend BasePreferenceController.java instead.
*/
public final class BluetoothTetherPreferenceController extends AbstractPreferenceController
public final class BluetoothTetherPreferenceController extends BasePreferenceController
implements LifecycleObserver, Preference.OnPreferenceChangeListener {
private static final String TAG = "BluetoothTetherPreferenceController";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
public static final String PREF_KEY = "enable_bluetooth_tethering";
private final ConnectivityManager mCm;
private int mBluetoothState;
private Preference mPreference;
private final SharedPreferences mSharedPreferences;
public BluetoothTetherPreferenceController(Context context, Lifecycle lifecycle) {
super(context);
public BluetoothTetherPreferenceController(Context context, String prefKey) {
super(context, prefKey);
mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
mSharedPreferences =
context.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE);
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
@@ -75,19 +70,13 @@ public final class BluetoothTetherPreferenceController extends AbstractPreferenc
mContext.unregisterReceiver(mBluetoothChangeReceiver);
}
@Override
public boolean isAvailable() {
final String[] bluetoothRegexs = mCm.getTetherableBluetoothRegexs();
return bluetoothRegexs != null && bluetoothRegexs.length > 0;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(PREF_KEY);
mPreference = screen.findPreference(mPreferenceKey);
if (mPreference != null && mPreference instanceof SwitchPreference) {
((SwitchPreference) mPreference)
.setChecked(mSharedPreferences.getBoolean(PREF_KEY, false));
.setChecked(mSharedPreferences.getBoolean(mPreferenceKey, false));
}
}
@@ -110,8 +99,13 @@ public final class BluetoothTetherPreferenceController extends AbstractPreferenc
}
@Override
public String getPreferenceKey() {
return PREF_KEY;
public int getAvailabilityStatus() {
final String[] bluetoothRegexs = mCm.getTetherableBluetoothRegexs();
if (bluetoothRegexs == null || bluetoothRegexs.length == 0) {
return CONDITIONALLY_UNAVAILABLE;
} else {
return AVAILABLE;
}
}
@VisibleForTesting
@@ -133,7 +127,7 @@ public final class BluetoothTetherPreferenceController extends AbstractPreferenc
Log.d(TAG, "preference changing to " + o);
}
final SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(PREF_KEY, (Boolean) o);
editor.putBoolean(mPreferenceKey, (Boolean) o);
editor.apply();
return true;
}

View File

@@ -61,13 +61,14 @@ public final class TetherEnabler implements SwitchWidgetController.OnSwitchChang
public static final String SHARED_PREF = "tether_options";
// This KEY is used for a shared preference value, not for any displayed preferences.
public static final String KEY_ENABLE_WIFI_TETHERING = "enable_wifi_tethering";
@VisibleForTesting
static final String WIFI_TETHER_KEY =
WifiTetherDisablePreferenceController.KEY_ENABLE_WIFI_TETHERING;
static final String WIFI_TETHER_DISABLE_KEY = "disable_wifi_tethering";
@VisibleForTesting
static final String USB_TETHER_KEY = UsbTetherPreferenceController.PREF_KEY;
static final String USB_TETHER_KEY = "enable_usb_tethering";
@VisibleForTesting
static final String BLUETOOTH_TETHER_KEY = BluetoothTetherPreferenceController.PREF_KEY;
static final String BLUETOOTH_TETHER_KEY = "enable_bluetooth_tethering";
private final SwitchWidgetController mSwitchWidgetController;
private final WifiManager mWifiManager;
@@ -176,7 +177,7 @@ public final class TetherEnabler implements SwitchWidgetController.OnSwitchChang
private void stopTether() {
// Wi-Fi tether is selected by default.
if (mSharedPreferences.getBoolean(WIFI_TETHER_KEY, true)) {
if (mSharedPreferences.getBoolean(KEY_ENABLE_WIFI_TETHERING, true)) {
stopTethering(TETHERING_WIFI);
}
@@ -202,7 +203,7 @@ public final class TetherEnabler implements SwitchWidgetController.OnSwitchChang
void startTether() {
// Wi-Fi tether is selected by default.
if (mSharedPreferences.getBoolean(WIFI_TETHER_KEY, true)) {
if (mSharedPreferences.getBoolean(KEY_ENABLE_WIFI_TETHERING, true)) {
startTethering(TETHERING_WIFI);
}
@@ -328,7 +329,7 @@ public final class TetherEnabler implements SwitchWidgetController.OnSwitchChang
if (!mSwitchWidgetController.isChecked()) {
return;
}
if (TextUtils.equals(WIFI_TETHER_KEY, key)) {
if (TextUtils.equals(KEY_ENABLE_WIFI_TETHERING, key)) {
if (sharedPreferences.getBoolean(key, true)) {
startTethering(TETHERING_WIFI);
} else {

View File

@@ -36,20 +36,18 @@ import androidx.preference.SwitchPreference;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.Utils;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.core.BasePreferenceController;
/**
* This controller helps to manage the switch state and visibility of USB tether switch
* preference. It stores preference values when preference changed.
* TODO(b/147272749): Extend BasePreferenceController.java instead.
*
*/
public final class UsbTetherPreferenceController extends AbstractPreferenceController implements
public final class UsbTetherPreferenceController extends BasePreferenceController implements
LifecycleObserver, Preference.OnPreferenceChangeListener {
private static final String TAG = "UsbTetherPrefController";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
public static final String PREF_KEY = "enable_usb_tethering";
private final ConnectivityManager mCm;
private boolean mUsbConnected;
@@ -57,14 +55,11 @@ public final class UsbTetherPreferenceController extends AbstractPreferenceContr
private Preference mPreference;
private final SharedPreferences mSharedPreferences;
public UsbTetherPreferenceController(Context context, Lifecycle lifecycle) {
super(context);
public UsbTetherPreferenceController(Context context, String prefKey) {
super(context, prefKey);
mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
mSharedPreferences =
context.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE);
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
@@ -82,23 +77,22 @@ public final class UsbTetherPreferenceController extends AbstractPreferenceContr
}
@Override
public boolean isAvailable() {
public int getAvailabilityStatus() {
String[] usbRegexs = mCm.getTetherableUsbRegexs();
return usbRegexs != null && usbRegexs.length > 0 && !Utils.isMonkeyRunning();
}
@Override
public String getPreferenceKey() {
return PREF_KEY;
if (usbRegexs == null || usbRegexs.length == 0 || Utils.isMonkeyRunning()) {
return CONDITIONALLY_UNAVAILABLE;
} else {
return AVAILABLE;
}
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(PREF_KEY);
mPreference = screen.findPreference(mPreferenceKey);
if (mPreference != null && mPreference instanceof SwitchPreference) {
((SwitchPreference) mPreference)
.setChecked(mSharedPreferences.getBoolean(PREF_KEY, false));
.setChecked(mSharedPreferences.getBoolean(mPreferenceKey, false));
}
}
@@ -136,7 +130,7 @@ public final class UsbTetherPreferenceController extends AbstractPreferenceContr
Log.d(TAG, "preference changing to " + o);
}
final SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(PREF_KEY, (Boolean) o);
editor.putBoolean(mPreferenceKey, (Boolean) o);
editor.apply();
return true;
}

View File

@@ -30,7 +30,7 @@ import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.core.BasePreferenceController;
/**
* This controller helps to manage the switch state and visibility of wifi tether disable switch
@@ -40,19 +40,13 @@ import com.android.settingslib.core.AbstractPreferenceController;
*
* @see BluetoothTetherPreferenceController
* @see UsbTetherPreferenceController
* TODO(b/147272749): Extend BasePreferenceController.java instead.
*
*/
public final class WifiTetherDisablePreferenceController extends AbstractPreferenceController
public final class WifiTetherDisablePreferenceController extends BasePreferenceController
implements LifecycleObserver, Preference.OnPreferenceChangeListener,
SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "WifiTetherDisablePreferenceController";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
public static final String PREF_KEY = "disable_wifi_tethering";
// This KEY is used for a shared preference value, not for any displayed preferences.
public static final String KEY_ENABLE_WIFI_TETHERING = "enable_wifi_tethering";
private final ConnectivityManager mCm;
private boolean mBluetoothTetherEnabled;
@@ -61,8 +55,8 @@ public final class WifiTetherDisablePreferenceController extends AbstractPrefere
private Preference mPreference;
private final SharedPreferences mSharedPreferences;
public WifiTetherDisablePreferenceController(Context context, Lifecycle lifecycle) {
super(context);
public WifiTetherDisablePreferenceController(Context context, String prefKey) {
super(context, prefKey);
mSharedPreferences =
context.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE);
mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -70,9 +64,6 @@ public final class WifiTetherDisablePreferenceController extends AbstractPrefere
TetherEnabler.USB_TETHER_KEY, false);
mBluetoothTetherEnabled = mSharedPreferences.getBoolean(
TetherEnabler.BLUETOOTH_TETHER_KEY, false);
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
@@ -85,20 +76,19 @@ public final class WifiTetherDisablePreferenceController extends AbstractPrefere
mSharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public boolean isAvailable() {
final String[] wifiRegexs = mCm.getTetherableWifiRegexs();
return wifiRegexs != null && wifiRegexs.length > 0 && shouldShow();
}
@VisibleForTesting
boolean shouldShow() {
return mBluetoothTetherEnabled || mUSBTetherEnabled;
}
@Override
public String getPreferenceKey() {
return PREF_KEY;
public int getAvailabilityStatus() {
final String[] wifiRegexs = mCm.getTetherableWifiRegexs();
if (wifiRegexs == null || wifiRegexs.length == 0 || !shouldShow()) {
return CONDITIONALLY_UNAVAILABLE;
} else {
return AVAILABLE;
}
}
@Override
@@ -111,10 +101,11 @@ public final class WifiTetherDisablePreferenceController extends AbstractPrefere
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mScreen = screen;
mPreference = screen.findPreference(PREF_KEY);
mPreference = screen.findPreference(mPreferenceKey);
if (mPreference != null && mPreference instanceof SwitchPreference) {
((SwitchPreference) mPreference)
.setChecked(!mSharedPreferences.getBoolean(KEY_ENABLE_WIFI_TETHERING, true));
.setChecked(!mSharedPreferences.getBoolean(
TetherEnabler.KEY_ENABLE_WIFI_TETHERING, true));
mPreference.setOnPreferenceChangeListener(this);
}
updateState(mPreference);
@@ -123,7 +114,7 @@ public final class WifiTetherDisablePreferenceController extends AbstractPrefere
@Override
public void updateState(Preference preference) {
super.updateState(preference);
setVisible(mScreen, PREF_KEY, shouldShow());
setVisible(mScreen, mPreferenceKey, shouldShow());
}
@Override
@@ -162,7 +153,7 @@ public final class WifiTetherDisablePreferenceController extends AbstractPrefere
Log.d(TAG, "check state changing to " + o);
}
final SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(KEY_ENABLE_WIFI_TETHERING, enableWifi);
editor.putBoolean(TetherEnabler.KEY_ENABLE_WIFI_TETHERING, enableWifi);
editor.apply();
return true;
}

View File

@@ -16,11 +16,12 @@
package com.android.settings.network;
import static com.android.settings.network.TetherEnabler.BLUETOOTH_TETHER_KEY;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -28,7 +29,6 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
@@ -55,7 +55,7 @@ public class BluetoothTetherPreferenceControllerTest {
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[] {""});
mController = new BluetoothTetherPreferenceController(mContext, mock(Lifecycle.class));
mController = new BluetoothTetherPreferenceController(mContext, BLUETOOTH_TETHER_KEY);
}
@Test

View File

@@ -136,7 +136,8 @@ public class TetherEnablerTest {
@Test
public void onSwitchToggled_onlyStartsWifiTetherWhenNeeded() {
when(mSharedPreferences.getBoolean(TetherEnabler.WIFI_TETHER_KEY, true)).thenReturn(true);
when(mSharedPreferences.getBoolean(TetherEnabler.KEY_ENABLE_WIFI_TETHERING, true))
.thenReturn(true);
when(mWifiManager.isWifiApEnabled()).thenReturn(true);
mEnabler.onSwitchToggled(true);
verify(mConnectivityManager, never()).startTethering(anyInt(), anyBoolean(), any(), any());
@@ -150,7 +151,8 @@ public class TetherEnablerTest {
public void onSwitchToggled_shouldStartUSBTetherWhenSelected() {
SharedPreferences preference = mock(SharedPreferences.class);
ReflectionHelpers.setField(mEnabler, "mSharedPreferences", preference);
when(preference.getBoolean(TetherEnabler.WIFI_TETHER_KEY, true)).thenReturn(false);
when(preference.getBoolean(TetherEnabler.KEY_ENABLE_WIFI_TETHERING, true))
.thenReturn(false);
when(preference.getBoolean(TetherEnabler.USB_TETHER_KEY, false)).thenReturn(true);
when(preference.getBoolean(TetherEnabler.BLUETOOTH_TETHER_KEY, true)).thenReturn(false);

View File

@@ -16,11 +16,12 @@
package com.android.settings.network;
import static com.android.settings.network.TetherEnabler.USB_TETHER_KEY;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -28,7 +29,6 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
@@ -54,8 +54,8 @@ public class UsbTetherPreferenceControllerTest {
mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[] {""});
mController = new UsbTetherPreferenceController(mContext, mock(Lifecycle.class));
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[]{""});
mController = new UsbTetherPreferenceController(mContext, USB_TETHER_KEY);
}
@Test
@@ -75,7 +75,7 @@ public class UsbTetherPreferenceControllerTest {
@Test
public void display_availableChangedCorrectly() {
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[] {""});
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[]{""});
assertThat(mController.isAvailable()).isTrue();
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]);

View File

@@ -16,7 +16,9 @@
package com.android.settings.network;
import static com.android.settings.network.WifiTetherDisablePreferenceController.PREF_KEY;
import static com.android.settings.network.TetherEnabler.BLUETOOTH_TETHER_KEY;
import static com.android.settings.network.TetherEnabler.USB_TETHER_KEY;
import static com.android.settings.network.TetherEnabler.WIFI_TETHER_DISABLE_KEY;
import static com.google.common.truth.Truth.assertThat;
@@ -30,7 +32,6 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import androidx.lifecycle.Lifecycle;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
@@ -68,10 +69,10 @@ public class WifiTetherDisablePreferenceControllerTest {
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{""});
when(mContext.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE))
.thenReturn(mSharedPreferences);
mController = new WifiTetherDisablePreferenceController(mContext, mock(Lifecycle.class));
mController = new WifiTetherDisablePreferenceController(mContext, WIFI_TETHER_DISABLE_KEY);
ReflectionHelpers.setField(mController, "mScreen", mPreferenceScreen);
ReflectionHelpers.setField(mController, "mPreference", mPreference);
when(mPreferenceScreen.findPreference(PREF_KEY)).thenReturn(mPreference);
when(mPreferenceScreen.findPreference(WIFI_TETHER_DISABLE_KEY)).thenReturn(mPreference);
}
@Test
@@ -103,30 +104,22 @@ public class WifiTetherDisablePreferenceControllerTest {
@Test
public void switch_shouldListenToUsbAndBluetooth() {
when(mSharedPreferences.getBoolean(
BluetoothTetherPreferenceController.PREF_KEY, false)).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences,
BluetoothTetherPreferenceController.PREF_KEY);
when(mSharedPreferences.getBoolean(BLUETOOTH_TETHER_KEY, false)).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences, BLUETOOTH_TETHER_KEY);
verify(mPreference).setVisible(eq(true));
when(mSharedPreferences.getBoolean(
UsbTetherPreferenceController.PREF_KEY, false)).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences,
UsbTetherPreferenceController.PREF_KEY);
when(mSharedPreferences.getBoolean(USB_TETHER_KEY, false)).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences, USB_TETHER_KEY);
assertThat(mController.shouldShow()).isTrue();
when(mSharedPreferences.getBoolean(
UsbTetherPreferenceController.PREF_KEY, false)).thenReturn(false);
mController.onSharedPreferenceChanged(mSharedPreferences,
UsbTetherPreferenceController.PREF_KEY);
when(mSharedPreferences.getBoolean(USB_TETHER_KEY, false)).thenReturn(false);
mController.onSharedPreferenceChanged(mSharedPreferences, USB_TETHER_KEY);
assertThat(mController.shouldShow()).isTrue();
when(mSharedPreferences.getBoolean(
BluetoothTetherPreferenceController.PREF_KEY, false)).thenReturn(false);
when(mSharedPreferences.getBoolean(BLUETOOTH_TETHER_KEY, false)).thenReturn(false);
when(mSharedPreferences.edit()).thenReturn(mock(SharedPreferences.Editor.class));
when(mPreference.isChecked()).thenReturn(true);
mController.onSharedPreferenceChanged(mSharedPreferences,
BluetoothTetherPreferenceController.PREF_KEY);
mController.onSharedPreferenceChanged(mSharedPreferences, BLUETOOTH_TETHER_KEY);
verify(mPreference).setChecked(eq(false));
verify(mPreference).setVisible(eq(false));
}