Fix NPE with registering content listener

1) Fix the condition to check either subscribed or null.
2) Don't register listeners unless the feature flag is enabled

Test: make RunSettingsRoboTest
Bug: 267357231
Change-Id: I0134812cbac60c394d96c5a5621a7c16d781b05d
This commit is contained in:
Derek Jedral
2023-01-31 14:23:15 -08:00
parent 957a35bd26
commit 999372caf4
3 changed files with 44 additions and 13 deletions

View File

@@ -87,10 +87,10 @@ public class ActiveUnlockContentListener {
}
/** Starts listening for updates from the ContentProvider, and fetches the current value. */
public synchronized void subscribe() {
if (mSubscribed && mUri != null) {
return;
/** Returns true if start listening for updates from the ContentProvider, false otherwise. */
public synchronized boolean subscribe() {
if (mSubscribed || mUri == null) {
return false;
}
mSubscribed = true;
mContext.getContentResolver().registerContentObserver(
@@ -99,15 +99,17 @@ public class ActiveUnlockContentListener {
() -> {
getContentFromUri();
});
return true;
}
/** Stops listening for updates from the ContentProvider. */
public synchronized void unsubscribe() {
if (!mSubscribed && mUri != null) {
return;
/** Returns true if stops listening for updates from the ContentProvider, false otherewise. */
public synchronized boolean unsubscribe() {
if (!mSubscribed || mUri == null) {
return false;
}
mSubscribed = false;
mContext.getContentResolver().unregisterContentObserver(mContentObserver);
return true;
}
/** Retrieves the most recently fetched value from the ContentProvider. */

View File

@@ -50,6 +50,7 @@ public class ActiveUnlockStatusPreferenceController
private final CombinedBiometricStatusUtils mCombinedBiometricStatusUtils;
private final ActiveUnlockSummaryListener mActiveUnlockSummaryListener;
private final ActiveUnlockDeviceNameListener mActiveUnlockDeviceNameListener;
private final boolean mIsAvailable;
public ActiveUnlockStatusPreferenceController(@NonNull Context context) {
this(context, KEY_ACTIVE_UNLOCK_SETTINGS);
@@ -59,6 +60,7 @@ public class ActiveUnlockStatusPreferenceController
@NonNull Context context, @NonNull String key) {
super(context, key);
mActiveUnlockStatusUtils = new ActiveUnlockStatusUtils(context);
mIsAvailable = mActiveUnlockStatusUtils.isAvailable();
mCombinedBiometricStatusUtils = new CombinedBiometricStatusUtils(context, getUserId());
OnContentChangedListener onSummaryChangedListener = new OnContentChangedListener() {
@Override
@@ -90,8 +92,10 @@ public class ActiveUnlockStatusPreferenceController
/** Subscribes to update preference summary dynamically. */
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mActiveUnlockSummaryListener.subscribe();
mActiveUnlockDeviceNameListener.subscribe();
if (mIsAvailable) {
mActiveUnlockSummaryListener.subscribe();
mActiveUnlockDeviceNameListener.subscribe();
}
}
/** Resets the preference reference on resume. */
@@ -105,8 +109,10 @@ public class ActiveUnlockStatusPreferenceController
/** Unsubscribes to prevent leaked listener. */
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
mActiveUnlockSummaryListener.unsubscribe();
mActiveUnlockDeviceNameListener.unsubscribe();
if (mIsAvailable) {
mActiveUnlockSummaryListener.unsubscribe();
mActiveUnlockDeviceNameListener.unsubscribe();
}
}
@Override
@@ -127,7 +133,7 @@ public class ActiveUnlockStatusPreferenceController
// This should never be called, as getAvailabilityStatus() will return the exact value.
// However, this is an abstract method in BiometricStatusPreferenceController, and so
// needs to be overridden.
return mActiveUnlockStatusUtils.isAvailable();
return mIsAvailable;
}
@Override