Fix NPE crash in UwbPreferenceController and optimize the lifecycle observer adding condition in page

This patch contains two fixes:

- Make sure mUwbManager is non-null by calling isUwbSupportedOnDevice to avoid a NPE.
- Modify AdvancedConnectedDeviceDashboardFragment, add lifecycle observer only if device supports UWB.

Bug: 244871579

Test: manual test and Robotest.
Change-Id: I78f97794a66f3fb487f067c4570899e21c254acf
This commit is contained in:
Shen Lin
2022-09-04 22:45:10 +08:00
parent 36a80cd8e6
commit 0b9a6304aa
2 changed files with 10 additions and 4 deletions

View File

@@ -69,10 +69,14 @@ public class AdvancedConnectedDeviceDashboardFragment extends DashboardFragment
public void onAttach(Context context) {
super.onAttach(context);
UwbPreferenceController uwbPreferenceController = use(UwbPreferenceController.class);
if (uwbPreferenceController != null && getSettingsLifecycle() != null) {
// We only need the observer listen to the broadcast in the background for refreshing
// UI if the device supports UWB.
if (uwbPreferenceController != null && uwbPreferenceController.isUwbSupportedOnDevice()) {
if (getSettingsLifecycle() != null) {
getSettingsLifecycle().addObserver(uwbPreferenceController);
}
}
}
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {

View File

@@ -75,8 +75,7 @@ public class UwbPreferenceController extends TogglePreferenceController implemen
};
}
@VisibleForTesting
boolean isUwbSupportedOnDevice() {
public boolean isUwbSupportedOnDevice() {
return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_UWB);
}
@@ -99,6 +98,9 @@ public class UwbPreferenceController extends TogglePreferenceController implemen
@Override
public boolean isChecked() {
if (!isUwbSupportedOnDevice()) {
return false;
}
int state = mUwbManager.getAdapterState();
return state == STATE_ENABLED_ACTIVE || state == STATE_ENABLED_INACTIVE;
}