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