• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.notification;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.database.ContentObserver;
22 import android.net.Uri;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.provider.Settings;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.lifecycle.Lifecycle;
30 import androidx.lifecycle.LifecycleEventObserver;
31 import androidx.lifecycle.LifecycleOwner;
32 import androidx.preference.Preference;
33 import androidx.preference.PreferenceScreen;
34 
35 import com.android.server.notification.Flags;
36 import com.android.settings.core.TogglePreferenceController;
37 
38 public class LockScreenNotificationsGlobalPreferenceController
39         extends TogglePreferenceController
40         implements LifecycleEventObserver {
41 
42     public static final int ON = 1;
43     public static final int OFF = 0;
44     private final ContentResolver mContentResolver;
45     private final ContentObserver mContentObserver;
46     @Nullable private Preference mPreference;
47 
48 
LockScreenNotificationsGlobalPreferenceController( @onNull Context context, @NonNull String preferenceKey )49     public LockScreenNotificationsGlobalPreferenceController(
50             @NonNull Context context,
51             @NonNull String preferenceKey
52     ) {
53         super(context, preferenceKey);
54         mContentResolver = context.getContentResolver();
55         mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
56             @Override
57             public void onChange(boolean selfChange, @Nullable Uri uri) {
58                 if (mPreference == null) return;
59                 updateState(mPreference);
60             }
61         };
62     }
63 
64     @Override
displayPreference(@onNull PreferenceScreen screen)65     public void displayPreference(@NonNull PreferenceScreen screen) {
66         super.displayPreference(screen);
67         mPreference = screen.findPreference(getPreferenceKey());
68     }
69 
70     @Override
updateState(@onNull Preference preference)71     public void updateState(@NonNull Preference preference) {
72         super.updateState(preference);
73         setChecked(lockScreenShowNotifications());
74     }
75 
76     @Override
getAvailabilityStatus()77     public int getAvailabilityStatus() {
78         // TODO: b/367455695 - remove this when the feature flag is removed!
79         return Flags.notificationLockScreenSettings() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
80     }
81 
82     @Override
isChecked()83     public boolean isChecked() {
84         return lockScreenShowNotifications();
85     }
86 
lockScreenShowNotifications()87     private boolean lockScreenShowNotifications() {
88         return Settings.Secure.getInt(mContext.getContentResolver(),
89                 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, OFF) == ON;
90     }
91 
92     @Override
setChecked(boolean isChecked)93     public boolean setChecked(boolean isChecked) {
94         return Settings.Secure.putInt(mContext.getContentResolver(),
95                 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, (isChecked ? ON : OFF));
96     }
97 
98     @Override
getSliceHighlightMenuRes()99     public int getSliceHighlightMenuRes() {
100         // not needed since it's not sliceable
101         return NO_RES;
102     }
103 
104     @Override
onStateChanged(@onNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event)105     public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner,
106             @NonNull Lifecycle.Event event) {
107         if (event == Lifecycle.Event.ON_RESUME) {
108             mContentResolver.registerContentObserver(
109                     Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS),
110                     /* notifyForDescendants= */ false,
111                     mContentObserver
112             );
113         } else {
114             mContentResolver.unregisterContentObserver(mContentObserver);
115         }
116     }
117 }
118