2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
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 import android.support.v7.preference.Preference;
30 import com.android.internal.widget.LockPatternUtils;
31 import com.android.settings.R;
32 import com.android.settings.RestrictedListPreference;
33 import com.android.settings.core.PreferenceControllerMixin;
34 import com.android.settingslib.RestrictedLockUtils;
36 import java.util.ArrayList;
38 public class VisibilityPreferenceController extends NotificationPreferenceController
39 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
41 private static final String TAG = "ChannelVisPrefContr";
42 private static final String KEY_VISIBILITY_OVERRIDE = "visibility_override";
43 private LockPatternUtils mLockPatternUtils;
45 public VisibilityPreferenceController(Context context, LockPatternUtils utils,
46 NotificationBackend backend) {
47 super(context, backend);
48 mLockPatternUtils = utils;
52 public String getPreferenceKey() {
53 return KEY_VISIBILITY_OVERRIDE;
57 public boolean isAvailable() {
58 if (!super.isAvailable()) {
61 if (mChannel == null || mAppRow.banned) {
64 return checkCanBeVisible(NotificationManager.IMPORTANCE_LOW) && isLockScreenSecure();
67 public void updateState(Preference preference) {
68 if (mChannel != null && mAppRow != null) {
69 RestrictedListPreference pref = (RestrictedListPreference) preference;
70 ArrayList<CharSequence> entries = new ArrayList<>();
71 ArrayList<CharSequence> values = new ArrayList<>();
73 pref.clearRestrictedItems();
74 if (getLockscreenNotificationsEnabled() && getLockscreenAllowPrivateNotifications()) {
75 final String summaryShowEntry =
76 mContext.getString(R.string.lock_screen_notifications_summary_show);
77 final String summaryShowEntryValue =
78 Integer.toString(NotificationManager.VISIBILITY_NO_OVERRIDE);
79 entries.add(summaryShowEntry);
80 values.add(summaryShowEntryValue);
81 setRestrictedIfNotificationFeaturesDisabled(pref, summaryShowEntry,
82 summaryShowEntryValue,
83 DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS
84 | DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
87 if (getLockscreenNotificationsEnabled()) {
88 final String summaryHideEntry =
89 mContext.getString(R.string.lock_screen_notifications_summary_hide);
90 final String summaryHideEntryValue = Integer.toString(
91 Notification.VISIBILITY_PRIVATE);
92 entries.add(summaryHideEntry);
93 values.add(summaryHideEntryValue);
94 setRestrictedIfNotificationFeaturesDisabled(pref,
95 summaryHideEntry, summaryHideEntryValue,
96 DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
98 entries.add(mContext.getString(R.string.lock_screen_notifications_summary_disable));
99 values.add(Integer.toString(Notification.VISIBILITY_SECRET));
100 pref.setEntries(entries.toArray(new CharSequence[entries.size()]));
101 pref.setEntryValues(values.toArray(new CharSequence[values.size()]));
103 if (mChannel.getLockscreenVisibility()
104 == NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) {
105 pref.setValue(Integer.toString(getGlobalVisibility()));
107 pref.setValue(Integer.toString(mChannel.getLockscreenVisibility()));
109 pref.setSummary("%s");
114 public boolean onPreferenceChange(Preference preference, Object newValue) {
115 if (mChannel != null) {
116 int sensitive = Integer.parseInt((String) newValue);
117 if (sensitive == getGlobalVisibility()) {
118 sensitive = NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
120 mChannel.setLockscreenVisibility(sensitive);
121 mChannel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
127 private void setRestrictedIfNotificationFeaturesDisabled(RestrictedListPreference pref,
128 CharSequence entry, CharSequence entryValue, int keyguardNotificationFeatures) {
129 RestrictedLockUtils.EnforcedAdmin admin =
130 RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(
131 mContext, keyguardNotificationFeatures, mAppRow.userId);
133 RestrictedListPreference.RestrictedItem item =
134 new RestrictedListPreference.RestrictedItem(entry, entryValue, admin);
135 pref.addRestrictedItem(item);
139 private int getGlobalVisibility() {
140 int globalVis = NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
141 if (!getLockscreenNotificationsEnabled()) {
142 globalVis = Notification.VISIBILITY_SECRET;
143 } else if (!getLockscreenAllowPrivateNotifications()) {
144 globalVis = Notification.VISIBILITY_PRIVATE;
149 private boolean getLockscreenNotificationsEnabled() {
150 final UserInfo parentUser = mUm.getProfileParent(UserHandle.myUserId());
151 final int primaryUserId = parentUser != null ? parentUser.id : UserHandle.myUserId();
152 return Settings.Secure.getIntForUser(mContext.getContentResolver(),
153 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0, primaryUserId) != 0;
156 private boolean getLockscreenAllowPrivateNotifications() {
157 return Settings.Secure.getInt(mContext.getContentResolver(),
158 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0) != 0;
161 protected boolean isLockScreenSecure() {
162 boolean lockscreenSecure = mLockPatternUtils.isSecure(UserHandle.myUserId());
163 UserInfo parentUser = mUm.getProfileParent(UserHandle.myUserId());
164 if (parentUser != null){
165 lockscreenSecure |= mLockPatternUtils.isSecure(parentUser.id);
168 return lockscreenSecure;