Refactor new tether settings to remove SharedPreferences

Bluetooth, USB and Wifi Disable tether preferences will turn on/off that
type of tethering interface instantly without storing it into
SharedPreferences. They are listening to TetherEnabler to update their
state.
Refactored to remove dependancy on SharedPreferences.

Bug: 151367756
Test: TetherEnablerTest; BluetoothTetherPreferenceControllerTest;
UsbTetherPreferenceControllerTest;
WifiTetherDisablePreferenceControllerTest; AllInOneSettingsTest;
AllInOneTetherPreferenceControllerTest

Change-Id: Ia6cc60bc4de8f08413beb6d41552a18f6fa6a55b
This commit is contained in:
Zhen Zhang
2020-03-15 11:18:01 -07:00
parent f5ff928958
commit 7f2d6ce058
12 changed files with 406 additions and 498 deletions

View File

@@ -17,16 +17,12 @@ package com.android.settings.network;
import static android.os.UserManager.DISALLOW_CONFIG_TETHERING;
import static com.android.settings.network.TetherEnabler.BLUETOOTH_TETHER_KEY;
import static com.android.settings.network.TetherEnabler.KEY_ENABLE_WIFI_TETHERING;
import static com.android.settings.network.TetherEnabler.USB_TETHER_KEY;
import static com.android.settingslib.RestrictedLockUtilsInternal.checkIfRestrictionEnforced;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.UserHandle;
import android.util.FeatureFlagUtils;
import android.util.Log;
@@ -56,19 +52,23 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
LifecycleObserver, TetherEnabler.OnTetherStateUpdateListener {
private static final String TAG = "AllInOneTetherPreferenceController";
private static final byte HOTSPOT_ONLY = 1;
private static final byte USB_ONLY = 1 << 1;
private static final byte BLUETOOTH_ONLY = 1 << 2;
private static final byte HOTSPOT_AND_USB = HOTSPOT_ONLY | USB_ONLY;
private static final byte HOTSPOT_AND_BLUETOOTH = HOTSPOT_ONLY | BLUETOOTH_ONLY;
private static final byte USB_AND_BLUETOOTH = USB_ONLY | BLUETOOTH_ONLY;
private static final byte HOTSPOT_AND_USB_AND_BLUETOOTH =
HOTSPOT_ONLY | USB_ONLY | BLUETOOTH_ONLY;
private static final byte TETHERING_TYPE_HOTSPOT_ONLY = 1;
private static final byte TETHERING_TYPE_USB_ONLY = 1 << 1;
private static final byte TETHERING_TYPE_BLUETOOTH_ONLY = 1 << 2;
private static final byte TETHERING_TYPE_HOTSPOT_AND_USB =
TETHERING_TYPE_HOTSPOT_ONLY | TETHERING_TYPE_USB_ONLY;
private static final byte TETHERING_TYPE_HOTSPOT_AND_BLUETOOTH =
TETHERING_TYPE_HOTSPOT_ONLY | TETHERING_TYPE_BLUETOOTH_ONLY;
private static final byte TETHERING_TYPE_USB_AND_BLUETOOTH =
TETHERING_TYPE_USB_ONLY | TETHERING_TYPE_BLUETOOTH_ONLY;
private static final byte TETHERING_TYPE_HOTSPOT_AND_USB_AND_BLUETOOTH =
TETHERING_TYPE_HOTSPOT_ONLY | TETHERING_TYPE_USB_ONLY | TETHERING_TYPE_BLUETOOTH_ONLY;
// A bitwise value that stands for the current tethering interface type.
private int mTetheringType;
private final boolean mAdminDisallowedTetherConfig;
private final AtomicReference<BluetoothPan> mBluetoothPan;
private final BluetoothAdapter mBluetoothAdapter;
private final SharedPreferences mTetherEnablerSharedPreferences;
@VisibleForTesting
final BluetoothProfile.ServiceListener mBtProfileServiceListener =
new BluetoothProfile.ServiceListener() {
@@ -92,7 +92,6 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
mAdminDisallowedTetherConfig = false;
mBluetoothPan = new AtomicReference<>();
mBluetoothAdapter = null;
mTetherEnablerSharedPreferences = null;
}
public AllInOneTetherPreferenceController(Context context, String key) {
@@ -101,8 +100,6 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
mAdminDisallowedTetherConfig = checkIfRestrictionEnforced(
context, DISALLOW_CONFIG_TETHERING, UserHandle.myUserId()) != null;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mTetherEnablerSharedPreferences =
context.getSharedPreferences(TetherEnabler.SHARED_PREF, Context.MODE_PRIVATE);
}
@Override
@@ -128,29 +125,22 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
@Override
public CharSequence getSummary() {
if (mPreference != null && mPreference.isChecked()) {
int chosenType = 0;
chosenType |= mTetherEnablerSharedPreferences
.getBoolean(KEY_ENABLE_WIFI_TETHERING, true) ? HOTSPOT_ONLY : 0;
chosenType |= mTetherEnablerSharedPreferences.getBoolean(USB_TETHER_KEY, false)
? USB_ONLY : 0;
chosenType |= mTetherEnablerSharedPreferences.getBoolean(BLUETOOTH_TETHER_KEY, false)
? BLUETOOTH_ONLY : 0;
switch (chosenType) {
case HOTSPOT_ONLY:
switch (mTetheringType) {
case TETHERING_TYPE_HOTSPOT_ONLY:
return mContext.getString(R.string.tether_settings_summary_hotspot_only);
case USB_ONLY:
case TETHERING_TYPE_USB_ONLY:
return mContext.getString(R.string.tether_settings_summary_usb_tethering_only);
case BLUETOOTH_ONLY:
case TETHERING_TYPE_BLUETOOTH_ONLY:
return mContext.getString(
R.string.tether_settings_summary_bluetooth_tethering_only);
case HOTSPOT_AND_USB:
case TETHERING_TYPE_HOTSPOT_AND_USB:
return mContext.getString(R.string.tether_settings_summary_hotspot_and_usb);
case HOTSPOT_AND_BLUETOOTH:
case TETHERING_TYPE_HOTSPOT_AND_BLUETOOTH:
return mContext.getString(
R.string.tether_settings_summary_hotspot_and_bluetooth);
case USB_AND_BLUETOOTH:
case TETHERING_TYPE_USB_AND_BLUETOOTH:
return mContext.getString(R.string.tether_settings_summary_usb_and_bluetooth);
case HOTSPOT_AND_USB_AND_BLUETOOTH:
case TETHERING_TYPE_HOTSPOT_AND_USB_AND_BLUETOOTH:
return mContext.getString(
R.string.tether_settings_summary_hotspot_and_usb_and_bluetooth);
default:
@@ -174,14 +164,14 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
@OnLifecycleEvent(Event.ON_RESUME)
public void onResume() {
if (mTetherEnabler != null) {
mTetherEnabler.setListener(this);
mTetherEnabler.addListener(this);
}
}
@OnLifecycleEvent(Event.ON_PAUSE)
public void onPause() {
if (mTetherEnabler != null) {
mTetherEnabler.setListener(null);
mTetherEnabler.removeListener(this);
}
}
@@ -206,7 +196,12 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
}
@Override
public void onTetherStateUpdated(boolean isTethering) {
public void onTetherStateUpdated(@TetherEnabler.TetheringState int state) {
mTetheringType = 0;
mTetheringType |= TetherEnabler.isBluetoothTethering(state) ? TETHERING_TYPE_BLUETOOTH_ONLY
: 0;
mTetheringType |= TetherEnabler.isWifiTethering(state) ? TETHERING_TYPE_HOTSPOT_ONLY : 0;
mTetheringType |= TetherEnabler.isUsbTethering(state) ? TETHERING_TYPE_USB_ONLY : 0;
updateState(mPreference);
}
}