ConfigureNotificationSettings.java revision d553f4f9a94a16bb19463fb42294212dbc21c09a
1/** 2 * Copyright (C) 2015 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 17package com.android.settings.notification; 18 19import android.content.ContentResolver; 20import android.content.Context; 21import android.database.ContentObserver; 22import android.net.Uri; 23import android.os.Bundle; 24import android.os.Handler; 25import android.os.UserHandle; 26import android.provider.Settings; 27import android.support.v7.preference.Preference; 28import android.support.v7.preference.Preference.OnPreferenceChangeListener; 29import android.support.v7.preference.TwoStatePreference; 30import android.util.Log; 31 32import com.android.internal.widget.LockPatternUtils; 33import com.android.settings.InstrumentedFragment; 34import com.android.settings.R; 35import com.android.settings.RestrictedListPreference.RestrictedItem; 36import com.android.settings.SettingsPreferenceFragment; 37import com.android.settingslib.RestrictedLockUtils; 38 39import java.util.ArrayList; 40 41import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS; 42import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS; 43 44import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 45 46public class ConfigureNotificationSettings extends SettingsPreferenceFragment { 47 private static final String TAG = "ConfigNotiSettings"; 48 49 private static final String KEY_NOTIFICATION_PULSE = "notification_pulse"; 50 private static final String KEY_LOCK_SCREEN_NOTIFICATIONS = "lock_screen_notifications"; 51 52 private final SettingsObserver mSettingsObserver = new SettingsObserver(); 53 54 private Context mContext; 55 56 private TwoStatePreference mNotificationPulse; 57 private NotificationLockscreenPreference mLockscreen; 58 private boolean mSecure; 59 private int mLockscreenSelectedValue; 60 61 @Override 62 protected int getMetricsCategory() { 63 return InstrumentedFragment.CONFIGURE_NOTIFICATION; 64 } 65 66 @Override 67 public void onCreate(Bundle savedInstanceState) { 68 super.onCreate(savedInstanceState); 69 mContext = getActivity(); 70 mSecure = new LockPatternUtils(getActivity()).isSecure(UserHandle.myUserId()); 71 72 addPreferencesFromResource(R.xml.configure_notification_settings); 73 74 initPulse(); 75 initLockscreenNotifications(); 76 77 } 78 79 @Override 80 public void onResume() { 81 super.onResume(); 82 mSettingsObserver.register(true); 83 } 84 85 @Override 86 public void onPause() { 87 super.onPause(); 88 mSettingsObserver.register(false); 89 } 90 91 // === Pulse notification light === 92 93 private void initPulse() { 94 mNotificationPulse = 95 (TwoStatePreference) getPreferenceScreen().findPreference(KEY_NOTIFICATION_PULSE); 96 if (mNotificationPulse == null) { 97 Log.i(TAG, "Preference not found: " + KEY_NOTIFICATION_PULSE); 98 return; 99 } 100 if (!getResources() 101 .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) { 102 getPreferenceScreen().removePreference(mNotificationPulse); 103 } else { 104 updatePulse(); 105 mNotificationPulse.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 106 @Override 107 public boolean onPreferenceChange(Preference preference, Object newValue) { 108 final boolean val = (Boolean)newValue; 109 return Settings.System.putInt(getContentResolver(), 110 Settings.System.NOTIFICATION_LIGHT_PULSE, 111 val ? 1 : 0); 112 } 113 }); 114 } 115 } 116 117 private void updatePulse() { 118 if (mNotificationPulse == null) { 119 return; 120 } 121 try { 122 mNotificationPulse.setChecked(Settings.System.getInt(getContentResolver(), 123 Settings.System.NOTIFICATION_LIGHT_PULSE) == 1); 124 } catch (Settings.SettingNotFoundException snfe) { 125 Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found"); 126 } 127 } 128 129 // === Lockscreen (public / private) notifications === 130 131 private void initLockscreenNotifications() { 132 mLockscreen = (NotificationLockscreenPreference) getPreferenceScreen().findPreference( 133 KEY_LOCK_SCREEN_NOTIFICATIONS); 134 if (mLockscreen == null) { 135 Log.i(TAG, "Preference not found: " + KEY_LOCK_SCREEN_NOTIFICATIONS); 136 return; 137 } 138 139 ArrayList<CharSequence> entries = new ArrayList<>(); 140 ArrayList<CharSequence> values = new ArrayList<>(); 141 entries.add(getString(R.string.lock_screen_notifications_summary_disable)); 142 values.add(Integer.toString(R.string.lock_screen_notifications_summary_disable)); 143 144 String summaryShowEntry = getString(R.string.lock_screen_notifications_summary_show); 145 String summaryShowEntryValue = Integer.toString( 146 R.string.lock_screen_notifications_summary_show); 147 entries.add(summaryShowEntry); 148 values.add(summaryShowEntryValue); 149 setRestrictedIfNotificationFeaturesDisabled(summaryShowEntry, summaryShowEntryValue, 150 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS | KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS); 151 152 if (mSecure) { 153 String summaryHideEntry = getString(R.string.lock_screen_notifications_summary_hide); 154 String summaryHideEntryValue = Integer.toString( 155 R.string.lock_screen_notifications_summary_hide); 156 entries.add(summaryHideEntry); 157 values.add(summaryHideEntryValue); 158 setRestrictedIfNotificationFeaturesDisabled(summaryHideEntry, summaryHideEntryValue, 159 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS); 160 } 161 162 mLockscreen.setEntries(entries.toArray(new CharSequence[entries.size()])); 163 mLockscreen.setEntryValues(values.toArray(new CharSequence[values.size()])); 164 updateLockscreenNotifications(); 165 if (mLockscreen.getEntries().length > 1) { 166 mLockscreen.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 167 @Override 168 public boolean onPreferenceChange(Preference preference, Object newValue) { 169 final int val = Integer.parseInt((String) newValue); 170 if (val == mLockscreenSelectedValue) { 171 return false; 172 } 173 final boolean enabled = 174 val != R.string.lock_screen_notifications_summary_disable; 175 final boolean show = val == R.string.lock_screen_notifications_summary_show; 176 Settings.Secure.putInt(getContentResolver(), 177 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, show ? 1 : 0); 178 Settings.Secure.putInt(getContentResolver(), 179 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, enabled ? 1 : 0); 180 mLockscreenSelectedValue = val; 181 return true; 182 } 183 }); 184 } else { 185 // There is one or less option for the user, disable the drop down. 186 mLockscreen.setEnabled(false); 187 } 188 } 189 190 private void setRestrictedIfNotificationFeaturesDisabled(CharSequence entry, 191 CharSequence entryValue, int keyguardNotificationFeatures) { 192 EnforcedAdmin admin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled( 193 mContext, keyguardNotificationFeatures, UserHandle.myUserId()); 194 if (admin != null) { 195 RestrictedItem item = new RestrictedItem(entry, entryValue, admin); 196 mLockscreen.addRestrictedItem(item); 197 } 198 } 199 200 private void updateLockscreenNotifications() { 201 if (mLockscreen == null) { 202 return; 203 } 204 final boolean enabled = getLockscreenNotificationsEnabled(); 205 final boolean allowPrivate = !mSecure || getLockscreenAllowPrivateNotifications(); 206 mLockscreenSelectedValue = !enabled ? R.string.lock_screen_notifications_summary_disable : 207 allowPrivate ? R.string.lock_screen_notifications_summary_show : 208 R.string.lock_screen_notifications_summary_hide; 209 mLockscreen.setValue(Integer.toString(mLockscreenSelectedValue)); 210 } 211 212 private boolean getLockscreenNotificationsEnabled() { 213 return Settings.Secure.getInt(getContentResolver(), 214 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) != 0; 215 } 216 217 private boolean getLockscreenAllowPrivateNotifications() { 218 return Settings.Secure.getInt(getContentResolver(), 219 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0) != 0; 220 } 221 222 223 // === Callbacks === 224 225 private final class SettingsObserver extends ContentObserver { 226 private final Uri NOTIFICATION_LIGHT_PULSE_URI = 227 Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); 228 private final Uri LOCK_SCREEN_PRIVATE_URI = 229 Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS); 230 private final Uri LOCK_SCREEN_SHOW_URI = 231 Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS); 232 233 public SettingsObserver() { 234 super(new Handler()); 235 } 236 237 public void register(boolean register) { 238 final ContentResolver cr = getContentResolver(); 239 if (register) { 240 cr.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, false, this); 241 cr.registerContentObserver(LOCK_SCREEN_PRIVATE_URI, false, this); 242 cr.registerContentObserver(LOCK_SCREEN_SHOW_URI, false, this); 243 } else { 244 cr.unregisterContentObserver(this); 245 } 246 } 247 248 @Override 249 public void onChange(boolean selfChange, Uri uri) { 250 super.onChange(selfChange, uri); 251 if (NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { 252 updatePulse(); 253 } 254 if (LOCK_SCREEN_PRIVATE_URI.equals(uri) || LOCK_SCREEN_SHOW_URI.equals(uri)) { 255 updateLockscreenNotifications(); 256 } 257 } 258 } 259} 260