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