Add ContentObserver to RedactNotificationPreferenceController

- Monitor the LOCK_SCREEN_SHOW_NOTIFICATIONS to decide the enabled/disabled status.

Fixes: 134055112
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.notification
Change-Id: I7c7edbe13f47638df69f31d15d863c2088a5774f
diff --git a/src/com/android/settings/notification/RedactNotificationPreferenceController.java b/src/com/android/settings/notification/RedactNotificationPreferenceController.java
index 94d7fc1..2814e1b 100644
--- a/src/com/android/settings/notification/RedactNotificationPreferenceController.java
+++ b/src/com/android/settings/notification/RedactNotificationPreferenceController.java
@@ -23,17 +23,29 @@
 import android.app.KeyguardManager;
 import android.app.admin.DevicePolicyManager;
 import android.content.Context;
+import android.database.ContentObserver;
+import android.os.Handler;
+import android.os.Looper;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.provider.Settings;
 
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
 import com.android.internal.widget.LockPatternUtils;
 import com.android.settings.Utils;
 import com.android.settings.core.TogglePreferenceController;
 import com.android.settings.overlay.FeatureFactory;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnStart;
+import com.android.settingslib.core.lifecycle.events.OnStop;
 
-public class RedactNotificationPreferenceController extends TogglePreferenceController {
-
+/**
+ * The controller of the sensitive notifications.
+ */
+public class RedactNotificationPreferenceController extends TogglePreferenceController implements
+        LifecycleObserver, OnStart, OnStop {
     private static final String TAG = "LockScreenNotifPref";
 
     static final String KEY_LOCKSCREEN_REDACT = "lock_screen_redact";
@@ -43,6 +55,17 @@
     private UserManager mUm;
     private KeyguardManager mKm;
     private final int mProfileUserId;
+    private Preference mPreference;
+    private ContentObserver mContentObserver =
+            new ContentObserver(new Handler(Looper.getMainLooper())) {
+                @Override
+                public void onChange(boolean selfChange) {
+                    if (mPreference != null) {
+                        mPreference.setEnabled(
+                                getAvailabilityStatus() != DISABLED_DEPENDENT_SETTING);
+                    }
+                }
+            };
 
     public RedactNotificationPreferenceController(Context context, String settingKey) {
         super(context, settingKey);
@@ -55,6 +78,12 @@
     }
 
     @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mPreference = screen.findPreference(getPreferenceKey());
+    }
+
+    @Override
     public boolean isChecked() {
         int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey())
                 ? UserHandle.myUserId() : mProfileUserId;
@@ -108,6 +137,18 @@
         return AVAILABLE;
     }
 
+    @Override
+    public void onStart() {
+        mContext.getContentResolver().registerContentObserver(
+                Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS),
+                false /* notifyForDescendants */, mContentObserver);
+    }
+
+    @Override
+    public void onStop() {
+        mContext.getContentResolver().unregisterContentObserver(mContentObserver);
+    }
+
     private boolean adminAllowsNotifications(int userId) {
         final int dpmFlags = mDpm.getKeyguardDisabledFeatures(null/* admin */, userId);
         return (dpmFlags & KEYGUARD_DISABLE_SECURE_NOTIFICATIONS) == 0;
@@ -123,7 +164,7 @@
                 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, userId) != 0;
     }
 
-   private boolean getLockscreenNotificationsEnabled(int userId) {
+    private boolean getLockscreenNotificationsEnabled(int userId) {
         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
                 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1, userId) != 0;
     }