Fix crash in AllInOneTetherSettings when user is not admin

When user is not admin, we should skip the setup and tear down
procedures in AllInOneTetherSettings' lifecycle. Also, the switch for
"Hotspot & tethering" on "Network & internet" should be disabled in such
case.

Bug: 151648198
Test: Build and flashed to crosshatch device. Using guest user, the
settings won't crash when using AllInOneTetherSetting and the switch is
disabled.

Change-Id: Ibd2f4111c53b8b9fa6a18504a681a27b966a3c46
This commit is contained in:
Zhen Zhang
2020-03-23 12:18:14 -07:00
parent 7f2d6ce058
commit 3b53b80715
2 changed files with 27 additions and 6 deletions

View File

@@ -249,18 +249,31 @@ public class AllInOneTetherSettings extends RestrictedDashboardFragment
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
if (mUnavailable) {
return;
}
if (mTetherEnabler != null) {
mTetherEnabler.addListener(mStateUpdateListener); mTetherEnabler.addListener(mStateUpdateListener);
} }
}
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
if (mUnavailable) {
return;
}
if (mTetherEnabler != null) {
mTetherEnabler.removeListener(mStateUpdateListener); mTetherEnabler.removeListener(mStateUpdateListener);
} }
}
@Override @Override
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
if (mUnavailable) {
return;
}
final Context context = getContext(); final Context context = getContext();
if (context != null) { if (context != null) {
context.unregisterReceiver(mTetherChangeReceiver); context.unregisterReceiver(mTetherChangeReceiver);

View File

@@ -35,6 +35,7 @@ import android.net.ConnectivityManager;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.UserManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@@ -106,6 +107,7 @@ public class TetherEnabler implements SwitchWidgetController.OnSwitchChangeListe
private final SwitchWidgetController mSwitchWidgetController; private final SwitchWidgetController mSwitchWidgetController;
private final WifiManager mWifiManager; private final WifiManager mWifiManager;
private final ConnectivityManager mConnectivityManager; private final ConnectivityManager mConnectivityManager;
private final UserManager mUserManager;
private final DataSaverBackend mDataSaverBackend; private final DataSaverBackend mDataSaverBackend;
private boolean mDataSaverEnabled; private boolean mDataSaverEnabled;
@@ -128,6 +130,7 @@ public class TetherEnabler implements SwitchWidgetController.OnSwitchChangeListe
mConnectivityManager = mConnectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothPan = bluetoothPan; mBluetoothPan = bluetoothPan;
mDataSaverEnabled = mDataSaverBackend.isDataSaverEnabled(); mDataSaverEnabled = mDataSaverBackend.isDataSaverEnabled();
@@ -171,6 +174,11 @@ public class TetherEnabler implements SwitchWidgetController.OnSwitchChangeListe
} }
} }
private void setSwitchEnabled(boolean enabled) {
mSwitchWidgetController.setEnabled(
enabled && !mDataSaverEnabled && mUserManager.isAdminUser());
}
@VisibleForTesting @VisibleForTesting
void updateState(@Nullable String[] tethered) { void updateState(@Nullable String[] tethered) {
int state = getTetheringState(tethered); int state = getTetheringState(tethered);
@@ -178,7 +186,7 @@ public class TetherEnabler implements SwitchWidgetController.OnSwitchChangeListe
Log.d(TAG, "updateState: " + state); Log.d(TAG, "updateState: " + state);
} }
setSwitchCheckedInternal(state != TETHERING_OFF); setSwitchCheckedInternal(state != TETHERING_OFF);
mSwitchWidgetController.setEnabled(!mDataSaverEnabled); setSwitchEnabled(true);
for (int i = 0, size = mListeners.size(); i < size; ++i) { for (int i = 0, size = mListeners.size(); i < size; ++i) {
mListeners.get(i).onTetherStateUpdated(state); mListeners.get(i).onTetherStateUpdated(state);
} }
@@ -251,7 +259,7 @@ public class TetherEnabler implements SwitchWidgetController.OnSwitchChangeListe
if ((choice == TETHERING_WIFI && isWifiTethering(state)) if ((choice == TETHERING_WIFI && isWifiTethering(state))
|| (choice == TETHERING_USB && isUsbTethering(state)) || (choice == TETHERING_USB && isUsbTethering(state))
|| (choice == TETHERING_BLUETOOTH && isBluetoothTethering(state))) { || (choice == TETHERING_BLUETOOTH && isBluetoothTethering(state))) {
mSwitchWidgetController.setEnabled(false); setSwitchEnabled(false);
mConnectivityManager.stopTethering(choice); mConnectivityManager.stopTethering(choice);
if (choice == TETHERING_BLUETOOTH) { if (choice == TETHERING_BLUETOOTH) {
// Stop bluetooth tether won't invoke tether state changed callback, so we need this // Stop bluetooth tether won't invoke tether state changed callback, so we need this
@@ -283,7 +291,7 @@ public class TetherEnabler implements SwitchWidgetController.OnSwitchChangeListe
} }
} }
mSwitchWidgetController.setEnabled(false); setSwitchEnabled(false);
mConnectivityManager.startTethering(choice, true /* showProvisioningUi */, mConnectivityManager.startTethering(choice, true /* showProvisioningUi */,
mOnStartTetheringCallback, new Handler(Looper.getMainLooper())); mOnStartTetheringCallback, new Handler(Looper.getMainLooper()));
} }
@@ -351,7 +359,7 @@ public class TetherEnabler implements SwitchWidgetController.OnSwitchChangeListe
@Override @Override
public void onDataSaverChanged(boolean isDataSaving) { public void onDataSaverChanged(boolean isDataSaving) {
mDataSaverEnabled = isDataSaving; mDataSaverEnabled = isDataSaving;
mSwitchWidgetController.setEnabled(!isDataSaving); setSwitchEnabled(true);
} }
@Override @Override