• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.settings.widget.PreferenceCategoryController;
36 
37 public class LockScreenWhatToShowController extends PreferenceCategoryController implements
38         LifecycleEventObserver {
39 
40     @Nullable
41     private Preference mPreference;
42     private final ContentResolver mContentResolver;
43 
44     final ContentObserver mContentObserver = new ContentObserver(
45             new Handler(Looper.getMainLooper())) {
46         @Override
47         public void onChange(boolean selfChange, @Nullable Uri uri) {
48             if (mPreference == null) return;
49             updateState(mPreference);
50         }
51     };
52 
53     @Override
updateState(@ullable Preference preference)54     public void updateState(@Nullable Preference preference) {
55         super.updateState(preference);
56         if (preference == null) return;
57         preference.setVisible(isAvailable());
58     }
59 
LockScreenWhatToShowController(@onNull Context context, @NonNull String key)60     public LockScreenWhatToShowController(@NonNull Context context, @NonNull String key) {
61         super(context, key);
62         mContentResolver = context.getContentResolver();
63     }
64 
65     @Override
displayPreference(@onNull PreferenceScreen screen)66     public void displayPreference(@NonNull PreferenceScreen screen) {
67         super.displayPreference(screen);
68         mPreference = screen.findPreference(getPreferenceKey());
69     }
70 
71     @Override
getAvailabilityStatus()72     public int getAvailabilityStatus() {
73         if (!lockScreenShowNotification()) {
74             return CONDITIONALLY_UNAVAILABLE;
75         }
76         return AVAILABLE;
77     }
78 
79     /**
80      * @return Whether showing notifications on the lockscreen is enabled.
81      */
lockScreenShowNotification()82     private boolean lockScreenShowNotification() {
83         return Settings.Secure.getInt(
84                 mContext.getContentResolver(),
85                 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
86                 LockScreenNotificationsGlobalPreferenceController.OFF
87         ) == LockScreenNotificationsGlobalPreferenceController.ON;
88     }
89 
90     @Override
onStateChanged(@onNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event)91     public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner,
92             @NonNull Lifecycle.Event event) {
93         if (event == Lifecycle.Event.ON_RESUME) {
94             mContentResolver.registerContentObserver(
95                     Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS),
96                     /* notifyForDescendants= */ false, mContentObserver);
97         } else if (event == Lifecycle.Event.ON_PAUSE) {
98             mContentResolver.unregisterContentObserver(mContentObserver);
99         }
100     }
101 }
102