Fix TetherSettings crash issue

- When the user is a guest user, the UI will remove all preferences to restrict setting changes. If the ViewModel updates the UI in this situation, it will cause Settings to crash.
  - Avoid to setup ViewModel when UI is restricted

Bug: 284435378
Test: Manual test
atest -c TetherSettingsTest

Change-Id: I52d4ea717c34eacc9cc2321e3950dc89408049f8
This commit is contained in:
Weng Su
2023-05-31 20:34:30 +08:00
parent bbf55cafb9
commit 086d062cb2
2 changed files with 51 additions and 18 deletions

View File

@@ -145,19 +145,13 @@ public class TetherSettings extends RestrictedSettingsFragment
super(UserManager.DISALLOW_CONFIG_TETHERING);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
TetheringManagerModel model = new ViewModelProvider(this).get(TetheringManagerModel.class);
mWifiTetherPreferenceController =
new WifiTetherPreferenceController(context, getSettingsLifecycle(), model);
mTm = model.getTetheringManager();
model.getTetheredInterfaces().observe(this, this::onTetheredInterfacesChanged);
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setIfOnlyAvailableForAdmins(true);
if (isUiRestricted()) {
return;
}
addPreferencesFromResource(R.xml.tether_prefs);
mContext = getContext();
@@ -165,13 +159,8 @@ public class TetherSettings extends RestrictedSettingsFragment
mDataSaverEnabled = mDataSaverBackend.isDataSaverEnabled();
mDataSaverFooter = findPreference(KEY_DATA_SAVER_FOOTER);
setIfOnlyAvailableForAdmins(true);
if (isUiRestricted()) {
getPreferenceScreen().removeAll();
return;
}
setupTetherPreference();
setupViewModel();
final Activity activity = getActivity();
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
@@ -223,8 +212,22 @@ public class TetherSettings extends RestrictedSettingsFragment
onDataSaverChanged(mDataSaverBackend.isDataSaverEnabled());
}
@VisibleForTesting
void setupViewModel() {
TetheringManagerModel model = new ViewModelProvider(this).get(TetheringManagerModel.class);
mWifiTetherPreferenceController =
new WifiTetherPreferenceController(getContext(), getSettingsLifecycle(), model);
mTm = model.getTetheringManager();
model.getTetheredInterfaces().observe(this, this::onTetheredInterfacesChanged);
}
@Override
public void onDestroy() {
if (isUiRestricted()) {
super.onDestroy();
return;
}
mDataSaverBackend.remListener(this);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();