• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS;
20 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS;
21 import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS;
22 
23 import android.app.KeyguardManager;
24 import android.content.Context;
25 import android.database.ContentObserver;
26 import android.os.Handler;
27 import android.os.Looper;
28 import android.os.UserHandle;
29 import android.os.UserManager;
30 import android.provider.Settings;
31 
32 import androidx.preference.PreferenceScreen;
33 
34 import com.android.internal.widget.LockPatternUtils;
35 import com.android.settings.core.TogglePreferenceController;
36 import com.android.settings.overlay.FeatureFactory;
37 import com.android.settingslib.RestrictedLockUtils;
38 import com.android.settingslib.RestrictedLockUtilsInternal;
39 import com.android.settingslib.RestrictedSwitchPreference;
40 import com.android.settingslib.core.lifecycle.LifecycleObserver;
41 import com.android.settingslib.core.lifecycle.events.OnStart;
42 import com.android.settingslib.core.lifecycle.events.OnStop;
43 
44 /**
45  * The controller of the sensitive notifications.
46  */
47 public class RedactNotificationPreferenceController extends TogglePreferenceController implements
48         LifecycleObserver, OnStart, OnStop {
49     private static final String TAG = "LockScreenNotifPref";
50 
51     static final String KEY_LOCKSCREEN_REDACT = "lock_screen_redact";
52     static final String KEY_LOCKSCREEN_WORK_PROFILE_REDACT = "lock_screen_work_redact";
53 
54     private UserManager mUm;
55     private KeyguardManager mKm;
56     int mProfileUserId;
57     private RestrictedSwitchPreference mPreference;
58     private ContentObserver mContentObserver =
59             new ContentObserver(new Handler(Looper.getMainLooper())) {
60                 @Override
61                 public void onChange(boolean selfChange) {
62                     if (mPreference != null) {
63                         mPreference.setEnabled(
64                                 getAvailabilityStatus() != DISABLED_DEPENDENT_SETTING);
65                     }
66                 }
67             };
68 
RedactNotificationPreferenceController(Context context, String settingKey)69     public RedactNotificationPreferenceController(Context context, String settingKey) {
70         super(context, settingKey);
71 
72         mUm = context.getSystemService(UserManager.class);
73         mKm = context.getSystemService(KeyguardManager.class);
74 
75         mProfileUserId = UserHandle.myUserId();
76         final int[] profileIds = mUm.getProfileIdsWithDisabled(UserHandle.myUserId());
77 
78         for (int profileId : profileIds) {
79             if (profileId != UserHandle.myUserId()) {
80                 mProfileUserId = profileId;
81             }
82         }
83     }
84 
85     @Override
displayPreference(PreferenceScreen screen)86     public void displayPreference(PreferenceScreen screen) {
87         super.displayPreference(screen);
88         mPreference = screen.findPreference(getPreferenceKey());
89 
90         int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey())
91                 ? UserHandle.myUserId() : mProfileUserId;
92         if (userId != UserHandle.USER_NULL) {
93             mPreference.setDisabledByAdmin(getEnforcedAdmin(userId));
94         }
95     }
96 
97     @Override
isChecked()98     public boolean isChecked() {
99         int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey())
100                 ? UserHandle.myUserId() : mProfileUserId;
101 
102         return getAllowPrivateNotifications(userId);
103     }
104 
105     @Override
setChecked(boolean isChecked)106     public boolean setChecked(boolean isChecked) {
107         int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey())
108                 ? UserHandle.myUserId() : mProfileUserId;
109 
110         Settings.Secure.putIntForUser(mContext.getContentResolver(),
111                 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, isChecked ? 1 : 0, userId);
112         return true;
113     }
114 
115     @Override
getAvailabilityStatus()116     public int getAvailabilityStatus() {
117         // hide work profile setting if no work profile
118         if (KEY_LOCKSCREEN_WORK_PROFILE_REDACT.equals(getPreferenceKey())
119                 && mProfileUserId == UserHandle.myUserId()) {
120             return CONDITIONALLY_UNAVAILABLE;
121         }
122 
123         int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey())
124                 ? UserHandle.myUserId() : mProfileUserId;
125 
126         // hide if lockscreen isn't secure for this user
127         final LockPatternUtils utils = FeatureFactory.getFactory(mContext)
128                 .getSecurityFeatureProvider()
129                 .getLockPatternUtils(mContext);
130         if (!utils.isSecure(userId)) {
131             return CONDITIONALLY_UNAVAILABLE;
132         }
133 
134         // all notifs hidden? disabled
135         if (!getLockscreenNotificationsEnabled(userId)) {
136             return DISABLED_DEPENDENT_SETTING;
137         }
138 
139         // specifically the work profile setting requires the work profile to be unlocked
140         if (KEY_LOCKSCREEN_WORK_PROFILE_REDACT.equals(getPreferenceKey())) {
141             if (mKm.isDeviceLocked(mProfileUserId)) {
142                 return DISABLED_DEPENDENT_SETTING;
143             }
144         }
145 
146         return AVAILABLE;
147     }
148 
149     @Override
onStart()150     public void onStart() {
151         mContext.getContentResolver().registerContentObserver(
152                 Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS),
153                 false /* notifyForDescendants */, mContentObserver);
154     }
155 
156     @Override
onStop()157     public void onStop() {
158         mContext.getContentResolver().unregisterContentObserver(mContentObserver);
159     }
160 
getEnforcedAdmin(int userId)161     private RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin(int userId) {
162         RestrictedLockUtils.EnforcedAdmin admin =
163                 RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled(
164                         mContext, KEYGUARD_DISABLE_SECURE_NOTIFICATIONS, userId);
165         if (admin != null) {
166             return admin;
167         }
168         admin = RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled(
169                 mContext, KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS, userId);
170         return admin;
171     }
172 
getAllowPrivateNotifications(int userId)173     private boolean getAllowPrivateNotifications(int userId) {
174         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
175                 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, userId) != 0;
176     }
177 
getLockscreenNotificationsEnabled(int userId)178     private boolean getLockscreenNotificationsEnabled(int userId) {
179         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
180                 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1, userId) != 0;
181     }
182 }
183