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 final String summaryHideEntry =
88 mContext.getString(R.string.lock_screen_notifications_summary_hide);
89 final String summaryHideEntryValue = Integer.toString(Notification.VISIBILITY_PRIVATE);
90 entries.add(summaryHideEntry);
91 values.add(summaryHideEntryValue);
92 setRestrictedIfNotificationFeaturesDisabled(pref,
93 summaryHideEntry, summaryHideEntryValue,
94 DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
95 entries.add(mContext.getString(R.string.lock_screen_notifications_summary_disable));
96 values.add(Integer.toString(Notification.VISIBILITY_SECRET));
97 pref.setEntries(entries.toArray(new CharSequence[entries.size()]));
98 pref.setEntryValues(values.toArray(new CharSequence[values.size()]));
100 if (mChannel.getLockscreenVisibility()
101 == NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) {
102 pref.setValue(Integer.toString(getGlobalVisibility()));
104 pref.setValue(Integer.toString(mChannel.getLockscreenVisibility()));
106 pref.setSummary("%s");
111 public boolean onPreferenceChange(Preference preference, Object newValue) {
112 if (mChannel != null) {
113 int sensitive = Integer.parseInt((String) newValue);
114 if (sensitive == getGlobalVisibility()) {
115 sensitive = NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
117 mChannel.setLockscreenVisibility(sensitive);
118 mChannel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
124 private void setRestrictedIfNotificationFeaturesDisabled(RestrictedListPreference pref,
125 CharSequence entry, CharSequence entryValue, int keyguardNotificationFeatures) {
126 RestrictedLockUtils.EnforcedAdmin admin =
127 RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(
128 mContext, keyguardNotificationFeatures, mAppRow.userId);
130 RestrictedListPreference.RestrictedItem item =
131 new RestrictedListPreference.RestrictedItem(entry, entryValue, admin);
132 pref.addRestrictedItem(item);
136 private int getGlobalVisibility() {
137 int globalVis = NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
138 if (!getLockscreenNotificationsEnabled()) {
139 globalVis = Notification.VISIBILITY_SECRET;
140 } else if (!getLockscreenAllowPrivateNotifications()) {
141 globalVis = Notification.VISIBILITY_PRIVATE;
146 private boolean getLockscreenNotificationsEnabled() {
147 return Settings.Secure.getInt(mContext.getContentResolver(),
148 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) != 0;
151 private boolean getLockscreenAllowPrivateNotifications() {
152 return Settings.Secure.getInt(mContext.getContentResolver(),
153 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0) != 0;
156 protected boolean isLockScreenSecure() {
157 boolean lockscreenSecure = mLockPatternUtils.isSecure(UserHandle.myUserId());
158 UserInfo parentUser = mUm.getProfileParent(UserHandle.myUserId());
159 if (parentUser != null){
160 lockscreenSecure |= mLockPatternUtils.isSecure(parentUser.id);
163 return lockscreenSecure;