• 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.app.admin.DevicePolicyManager;
25 import android.content.Context;
26 import android.os.UserHandle;
27 import android.os.UserManager;
28 import android.provider.Settings;
29 
30 import com.android.internal.widget.LockPatternUtils;
31 import com.android.settings.Utils;
32 import com.android.settings.core.TogglePreferenceController;
33 import com.android.settings.overlay.FeatureFactory;
34 
35 public class RedactNotificationPreferenceController extends TogglePreferenceController {
36 
37     private static final String TAG = "LockScreenNotifPref";
38 
39     static final String KEY_LOCKSCREEN_REDACT = "lock_screen_redact";
40     static final String KEY_LOCKSCREEN_WORK_PROFILE_REDACT = "lock_screen_work_redact";
41 
42     private DevicePolicyManager mDpm;
43     private UserManager mUm;
44     private KeyguardManager mKm;
45     private final int mProfileUserId;
46 
RedactNotificationPreferenceController(Context context, String settingKey)47     public RedactNotificationPreferenceController(Context context, String settingKey) {
48         super(context, settingKey);
49 
50         mUm = context.getSystemService(UserManager.class);
51         mDpm = context.getSystemService(DevicePolicyManager.class);
52         mKm = context.getSystemService(KeyguardManager.class);
53 
54         mProfileUserId = Utils.getManagedProfileId(mUm, UserHandle.myUserId());
55     }
56 
57     @Override
isChecked()58     public boolean isChecked() {
59         int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey())
60                 ? UserHandle.myUserId() : mProfileUserId;
61 
62         return getAllowPrivateNotifications(userId);
63     }
64 
65     @Override
setChecked(boolean isChecked)66     public boolean setChecked(boolean isChecked) {
67         int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey())
68                 ? UserHandle.myUserId() : mProfileUserId;
69 
70         Settings.Secure.putIntForUser(mContext.getContentResolver(),
71                 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, isChecked ? 1 : 0, userId);
72         return true;
73     }
74 
75     @Override
getAvailabilityStatus()76     public int getAvailabilityStatus() {
77         // hide work profile setting if no work profile
78         if (KEY_LOCKSCREEN_WORK_PROFILE_REDACT.equals(getPreferenceKey())
79                 && mProfileUserId == UserHandle.USER_NULL) {
80             return CONDITIONALLY_UNAVAILABLE;
81         }
82 
83         int userId = KEY_LOCKSCREEN_REDACT.equals(getPreferenceKey())
84                 ? UserHandle.myUserId() : mProfileUserId;
85 
86         // hide if lockscreen isn't secure for this user
87         final LockPatternUtils utils = FeatureFactory.getFactory(mContext)
88                 .getSecurityFeatureProvider()
89                 .getLockPatternUtils(mContext);
90         if (!utils.isSecure(userId)) {
91             return CONDITIONALLY_UNAVAILABLE;
92         }
93 
94         // all notifs hidden? admin doesn't allow notifs or redacted notifs? disabled
95         if (!getLockscreenNotificationsEnabled(userId)
96                 || !adminAllowsNotifications(userId)
97                 || !adminAllowsUnredactedNotifications(userId)) {
98             return DISABLED_DEPENDENT_SETTING;
99         }
100 
101         // specifically the work profile setting requires the work profile to be unlocked
102         if (KEY_LOCKSCREEN_WORK_PROFILE_REDACT.equals(getPreferenceKey())) {
103             if (mKm.isDeviceLocked(mProfileUserId)) {
104                 return DISABLED_DEPENDENT_SETTING;
105             }
106         }
107 
108         return AVAILABLE;
109     }
110 
adminAllowsNotifications(int userId)111     private boolean adminAllowsNotifications(int userId) {
112         final int dpmFlags = mDpm.getKeyguardDisabledFeatures(null/* admin */, userId);
113         return (dpmFlags & KEYGUARD_DISABLE_SECURE_NOTIFICATIONS) == 0;
114     }
115 
adminAllowsUnredactedNotifications(int userId)116     private boolean adminAllowsUnredactedNotifications(int userId) {
117         final int dpmFlags = mDpm.getKeyguardDisabledFeatures(null/* admin */, userId);
118         return (dpmFlags & KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS) == 0;
119     }
120 
getAllowPrivateNotifications(int userId)121     private boolean getAllowPrivateNotifications(int userId) {
122         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
123                 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, userId) != 0;
124     }
125 
getLockscreenNotificationsEnabled(int userId)126    private boolean getLockscreenNotificationsEnabled(int userId) {
127         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
128                 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1, userId) != 0;
129     }
130 }
131