• 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 
21 import android.app.admin.DevicePolicyManager;
22 import android.content.Context;
23 import android.os.UserHandle;
24 import android.provider.Settings;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.server.notification.Flags;
30 import com.android.settings.R;
31 import com.android.settings.RestrictedListPreference;
32 import com.android.settings.core.PreferenceControllerMixin;
33 import com.android.settingslib.RestrictedLockUtils;
34 import com.android.settingslib.RestrictedLockUtilsInternal;
35 import com.android.settingslib.core.AbstractPreferenceController;
36 
37 import com.google.common.annotations.VisibleForTesting;
38 
39 import java.util.ArrayList;
40 
41 public class ShowOnLockScreenNotificationPreferenceController extends AbstractPreferenceController
42         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
43 
44     private static final String TAG = "LockScreenNotifPref";
45 
46     private final String mSettingKey;
47     private DevicePolicyManager mDpm;
48 
ShowOnLockScreenNotificationPreferenceController(Context context, String settingKey)49     public ShowOnLockScreenNotificationPreferenceController(Context context, String settingKey) {
50         super(context);
51         mSettingKey = settingKey;
52         mDpm = context.getSystemService(DevicePolicyManager.class);
53     }
54 
55     @VisibleForTesting
setDpm(DevicePolicyManager dpm)56     void setDpm(DevicePolicyManager dpm) {
57         mDpm = dpm;
58     }
59 
60     @Override
getPreferenceKey()61     public String getPreferenceKey() {
62         return mSettingKey;
63     }
64 
65     @Override
isAvailable()66     public boolean isAvailable() {
67         // When notificationLockScreenSettings is enabled, show the lock screen notif settings page
68         return !Flags.notificationLockScreenSettings();
69     }
70 
71     @Override
displayPreference(PreferenceScreen screen)72     public void displayPreference(PreferenceScreen screen) {
73         super.displayPreference(screen);
74         RestrictedListPreference pref = screen.findPreference(mSettingKey);
75         pref.clearRestrictedItems();
76         ArrayList<CharSequence> entries = new ArrayList<>();
77         ArrayList<CharSequence> values = new ArrayList<>();
78 
79         String showAllEntry =
80                 mContext.getString(R.string.lock_screen_notifs_show_all);
81         String showAllEntryValue =
82                 Integer.toString(R.string.lock_screen_notifs_show_all);
83         entries.add(showAllEntry);
84         values.add(showAllEntryValue);
85         setRestrictedIfNotificationFeaturesDisabled(pref, showAllEntry, showAllEntryValue,
86                 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
87 
88         String alertingEntry = mContext.getString(R.string.lock_screen_notifs_show_alerting);
89         String alertingEntryValue = Integer.toString(R.string.lock_screen_notifs_show_alerting);
90         entries.add(alertingEntry);
91         values.add(alertingEntryValue);
92         setRestrictedIfNotificationFeaturesDisabled(pref, alertingEntry, alertingEntryValue,
93                 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
94 
95         entries.add(mContext.getString(R.string.lock_screen_notifs_show_none));
96         values.add(Integer.toString(R.string.lock_screen_notifs_show_none));
97 
98         pref.setEntries(entries.toArray(new CharSequence[entries.size()]));
99         pref.setEntryValues(values.toArray(new CharSequence[values.size()]));
100 
101         if (!adminAllowsNotifications() || !getLockscreenNotificationsEnabled()) {
102             pref.setValue(Integer.toString(R.string.lock_screen_notifs_show_none));
103         } else if (!getLockscreenSilentNotificationsEnabled()) {
104             pref.setValue(Integer.toString(R.string.lock_screen_notifs_show_alerting));
105         } else {
106             pref.setValue(Integer.toString(R.string.lock_screen_notifs_show_all));
107         }
108 
109         pref.setOnPreferenceChangeListener(this);
110 
111         refreshSummary(pref);
112     }
113 
114     @Override
getSummary()115     public CharSequence getSummary() {
116         if (!adminAllowsNotifications() || !getLockscreenNotificationsEnabled()) {
117             return mContext.getString(R.string.lock_screen_notifs_show_none);
118         } else if (!getLockscreenSilentNotificationsEnabled()) {
119             return mContext.getString(R.string.lock_screen_notifs_show_alerting);
120         } else {
121             return mContext.getString(R.string.lock_screen_notifs_show_all_summary);
122         }
123     }
124 
125     @Override
onPreferenceChange(Preference preference, Object newValue)126     public boolean onPreferenceChange(Preference preference, Object newValue) {
127         final int val = Integer.parseInt((String) newValue);
128         final boolean enabled = val != R.string.lock_screen_notifs_show_none;
129         final boolean show = val == R.string.lock_screen_notifs_show_all;
130         Settings.Secure.putInt(mContext.getContentResolver(),
131                 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, show ? 1 : 0);
132         Settings.Secure.putInt(mContext.getContentResolver(),
133                 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, enabled ? 1 : 0);
134         refreshSummary(preference);
135         return true;
136     }
137 
setRestrictedIfNotificationFeaturesDisabled(RestrictedListPreference pref, CharSequence entry, CharSequence entryValue, int keyguardNotificationFeatures)138     private void setRestrictedIfNotificationFeaturesDisabled(RestrictedListPreference pref,
139             CharSequence entry, CharSequence entryValue, int keyguardNotificationFeatures) {
140         RestrictedLockUtils.EnforcedAdmin admin =
141                 RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled(
142                         mContext, keyguardNotificationFeatures, UserHandle.myUserId());
143         if (admin != null && pref != null) {
144             RestrictedListPreference.RestrictedItem item =
145                     new RestrictedListPreference.RestrictedItem(entry, entryValue, admin);
146             pref.addRestrictedItem(item);
147         }
148     }
149 
adminAllowsNotifications()150     private boolean adminAllowsNotifications() {
151         final int dpmFlags = mDpm.getKeyguardDisabledFeatures(null/* admin */);
152         return (dpmFlags & KEYGUARD_DISABLE_SECURE_NOTIFICATIONS) == 0;
153     }
154 
getLockscreenNotificationsEnabled()155     private boolean getLockscreenNotificationsEnabled() {
156         return Settings.Secure.getInt(mContext.getContentResolver(),
157                 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1) != 0;
158     }
159 
getLockscreenSilentNotificationsEnabled()160     private boolean getLockscreenSilentNotificationsEnabled() {
161         return Settings.Secure.getInt(mContext.getContentResolver(),
162                 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0) != 0;
163     }
164 }
165