• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.app.Notification;
20 import android.app.NotificationChannel;
21 import android.app.NotificationManager;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.Context;
24 import android.content.pm.UserInfo;
25 import android.os.UserHandle;
26 import android.provider.Settings;
27 import android.service.notification.NotificationListenerService;
28 
29 import androidx.preference.Preference;
30 
31 import com.android.internal.widget.LockPatternUtils;
32 import com.android.settings.R;
33 import com.android.settings.RestrictedListPreference;
34 import com.android.settings.core.PreferenceControllerMixin;
35 import com.android.settingslib.RestrictedLockUtils;
36 import com.android.settingslib.RestrictedLockUtilsInternal;
37 
38 import java.util.ArrayList;
39 
40 public class VisibilityPreferenceController extends NotificationPreferenceController
41         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
42 
43     private static final String TAG = "ChannelVisPrefContr";
44     private static final String KEY_VISIBILITY_OVERRIDE = "visibility_override";
45     private LockPatternUtils mLockPatternUtils;
46 
VisibilityPreferenceController(Context context, LockPatternUtils utils, NotificationBackend backend)47     public VisibilityPreferenceController(Context context, LockPatternUtils utils,
48             NotificationBackend backend) {
49         super(context, backend);
50         mLockPatternUtils = utils;
51     }
52 
53     @Override
getPreferenceKey()54     public String getPreferenceKey() {
55         return KEY_VISIBILITY_OVERRIDE;
56     }
57 
58     @Override
isAvailable()59     public boolean isAvailable() {
60         if (!super.isAvailable()) {
61             return false;
62         }
63         if (mChannel == null || mAppRow.banned) {
64             return false;
65         }
66         return checkCanBeVisible(NotificationManager.IMPORTANCE_LOW) && isLockScreenSecure();
67     }
68 
updateState(Preference preference)69     public void updateState(Preference preference) {
70         if (mChannel != null && mAppRow != null) {
71             RestrictedListPreference pref = (RestrictedListPreference) preference;
72             ArrayList<CharSequence> entries = new ArrayList<>();
73             ArrayList<CharSequence> values = new ArrayList<>();
74 
75             pref.clearRestrictedItems();
76             if (getLockscreenNotificationsEnabled() && getLockscreenAllowPrivateNotifications()) {
77                 final String summaryShowEntry =
78                         mContext.getString(R.string.lock_screen_notifications_summary_show);
79                 final String summaryShowEntryValue =
80                         Integer.toString(NotificationManager.VISIBILITY_NO_OVERRIDE);
81                 entries.add(summaryShowEntry);
82                 values.add(summaryShowEntryValue);
83                 setRestrictedIfNotificationFeaturesDisabled(pref, summaryShowEntry,
84                         summaryShowEntryValue,
85                         DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS
86                                 | DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
87             }
88 
89             if (getLockscreenNotificationsEnabled()) {
90                 final String summaryHideEntry =
91                         mContext.getString(R.string.lock_screen_notifications_summary_hide);
92                 final String summaryHideEntryValue = Integer.toString(
93                         Notification.VISIBILITY_PRIVATE);
94                 entries.add(summaryHideEntry);
95                 values.add(summaryHideEntryValue);
96                 setRestrictedIfNotificationFeaturesDisabled(pref,
97                         summaryHideEntry, summaryHideEntryValue,
98                         DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
99             }
100             entries.add(mContext.getString(R.string.lock_screen_notifications_summary_disable));
101             values.add(Integer.toString(Notification.VISIBILITY_SECRET));
102             pref.setEntries(entries.toArray(new CharSequence[entries.size()]));
103             pref.setEntryValues(values.toArray(new CharSequence[values.size()]));
104 
105             if (mChannel.getLockscreenVisibility()
106                     == NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) {
107                 pref.setValue(Integer.toString(getGlobalVisibility()));
108             } else {
109                 pref.setValue(Integer.toString(mChannel.getLockscreenVisibility()));
110             }
111             pref.setSummary("%s");
112         }
113     }
114 
115     @Override
onPreferenceChange(Preference preference, Object newValue)116     public boolean onPreferenceChange(Preference preference, Object newValue) {
117         if (mChannel != null) {
118             int sensitive = Integer.parseInt((String) newValue);
119             if (sensitive == getGlobalVisibility()) {
120                 sensitive = NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
121             }
122             mChannel.setLockscreenVisibility(sensitive);
123             mChannel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
124             saveChannel();
125         }
126         return true;
127     }
128 
setRestrictedIfNotificationFeaturesDisabled(RestrictedListPreference pref, CharSequence entry, CharSequence entryValue, int keyguardNotificationFeatures)129     private void setRestrictedIfNotificationFeaturesDisabled(RestrictedListPreference pref,
130             CharSequence entry, CharSequence entryValue, int keyguardNotificationFeatures) {
131         RestrictedLockUtils.EnforcedAdmin admin =
132                 RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled(
133                         mContext, keyguardNotificationFeatures, mAppRow.userId);
134         if (admin != null) {
135             RestrictedListPreference.RestrictedItem item =
136                     new RestrictedListPreference.RestrictedItem(entry, entryValue, admin);
137             pref.addRestrictedItem(item);
138         }
139     }
140 
getGlobalVisibility()141     private int getGlobalVisibility() {
142         int globalVis = NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
143         if (!getLockscreenNotificationsEnabled()) {
144             globalVis = Notification.VISIBILITY_SECRET;
145         } else if (!getLockscreenAllowPrivateNotifications()) {
146             globalVis = Notification.VISIBILITY_PRIVATE;
147         }
148         return globalVis;
149     }
150 
getLockscreenNotificationsEnabled()151     private boolean getLockscreenNotificationsEnabled() {
152         final UserInfo parentUser = mUm.getProfileParent(UserHandle.myUserId());
153         final int primaryUserId = parentUser != null ? parentUser.id : UserHandle.myUserId();
154         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
155                 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0, primaryUserId) != 0;
156     }
157 
getLockscreenAllowPrivateNotifications()158     private boolean getLockscreenAllowPrivateNotifications() {
159         return Settings.Secure.getInt(mContext.getContentResolver(),
160                 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0) != 0;
161     }
162 
isLockScreenSecure()163     protected boolean isLockScreenSecure() {
164         boolean lockscreenSecure = mLockPatternUtils.isSecure(UserHandle.myUserId());
165         UserInfo parentUser = mUm.getProfileParent(UserHandle.myUserId());
166         if (parentUser != null){
167             lockscreenSecure |= mLockPatternUtils.isSecure(parentUser.id);
168         }
169 
170         return lockscreenSecure;
171     }
172 
173 }
174