2 * Copyright (C) 2014 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.NotificationManager;
20 import android.content.BroadcastReceiver;
21 import android.content.ComponentName;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.pm.PackageManager;
27 import android.content.pm.ServiceInfo;
28 import android.database.ContentObserver;
29 import android.database.Cursor;
30 import android.database.sqlite.SQLiteException;
31 import android.media.AudioManager;
32 import android.media.RingtoneManager;
33 import android.net.Uri;
34 import android.os.AsyncTask;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.os.Looper;
38 import android.os.Message;
39 import android.os.Vibrator;
40 import android.preference.Preference;
41 import android.preference.Preference.OnPreferenceChangeListener;
42 import android.preference.PreferenceCategory;
43 import android.preference.SeekBarVolumizer;
44 import android.preference.TwoStatePreference;
45 import android.provider.MediaStore;
46 import android.provider.OpenableColumns;
47 import android.provider.SearchIndexableResource;
48 import android.provider.Settings;
49 import android.util.Log;
51 import com.android.internal.widget.LockPatternUtils;
52 import com.android.settings.R;
53 import com.android.settings.SettingsPreferenceFragment;
54 import com.android.settings.Utils;
55 import com.android.settings.search.BaseSearchIndexProvider;
56 import com.android.settings.search.Indexable;
58 import java.util.ArrayList;
59 import java.util.Arrays;
60 import java.util.List;
61 import java.util.Objects;
63 public class NotificationSettings extends SettingsPreferenceFragment implements Indexable {
64 private static final String TAG = "NotificationSettings";
66 private static final String KEY_SOUND = "sound";
67 private static final String KEY_MEDIA_VOLUME = "media_volume";
68 private static final String KEY_ALARM_VOLUME = "alarm_volume";
69 private static final String KEY_RING_VOLUME = "ring_volume";
70 private static final String KEY_NOTIFICATION_VOLUME = "notification_volume";
71 private static final String KEY_PHONE_RINGTONE = "ringtone";
72 private static final String KEY_NOTIFICATION_RINGTONE = "notification_ringtone";
73 private static final String KEY_VIBRATE_WHEN_RINGING = "vibrate_when_ringing";
74 private static final String KEY_NOTIFICATION = "notification";
75 private static final String KEY_NOTIFICATION_PULSE = "notification_pulse";
76 private static final String KEY_LOCK_SCREEN_NOTIFICATIONS = "lock_screen_notifications";
77 private static final String KEY_NOTIFICATION_ACCESS = "manage_notification_access";
79 private static final int SAMPLE_CUTOFF = 2000; // manually cap sample playback at 2 seconds
81 private final VolumePreferenceCallback mVolumeCallback = new VolumePreferenceCallback();
82 private final H mHandler = new H();
83 private final SettingsObserver mSettingsObserver = new SettingsObserver();
84 private final Receiver mReceiver = new Receiver();
85 private final ArrayList<VolumeSeekBarPreference> mVolumePrefs = new ArrayList<>();
87 private Context mContext;
88 private PackageManager mPM;
89 private boolean mVoiceCapable;
90 private Vibrator mVibrator;
91 private AudioManager mAudioManager;
92 private VolumeSeekBarPreference mRingOrNotificationPreference;
94 private Preference mPhoneRingtonePreference;
95 private Preference mNotificationRingtonePreference;
96 private TwoStatePreference mVibrateWhenRinging;
97 private TwoStatePreference mNotificationPulse;
98 private DropDownPreference mLockscreen;
99 private Preference mNotificationAccess;
100 private boolean mSecure;
101 private int mLockscreenSelectedValue;
102 private ComponentName mSuppressor;
103 private int mRingerMode = -1;
106 public void onCreate(Bundle savedInstanceState) {
107 super.onCreate(savedInstanceState);
108 mContext = getActivity();
109 mPM = mContext.getPackageManager();
110 mVoiceCapable = Utils.isVoiceCapable(mContext);
111 mSecure = new LockPatternUtils(getActivity()).isSecure();
113 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
114 mVibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
115 if (mVibrator != null && !mVibrator.hasVibrator()) {
119 addPreferencesFromResource(R.xml.notification_settings);
121 final PreferenceCategory sound = (PreferenceCategory) findPreference(KEY_SOUND);
122 initVolumePreference(KEY_MEDIA_VOLUME, AudioManager.STREAM_MUSIC,
123 com.android.internal.R.drawable.ic_audio_vol_mute);
124 initVolumePreference(KEY_ALARM_VOLUME, AudioManager.STREAM_ALARM,
125 com.android.internal.R.drawable.ic_audio_alarm_mute);
127 mRingOrNotificationPreference =
128 initVolumePreference(KEY_RING_VOLUME, AudioManager.STREAM_RING,
129 com.android.internal.R.drawable.ic_audio_ring_notif_mute);
130 sound.removePreference(sound.findPreference(KEY_NOTIFICATION_VOLUME));
132 mRingOrNotificationPreference =
133 initVolumePreference(KEY_NOTIFICATION_VOLUME, AudioManager.STREAM_NOTIFICATION,
134 com.android.internal.R.drawable.ic_audio_ring_notif_mute);
135 sound.removePreference(sound.findPreference(KEY_RING_VOLUME));
137 initRingtones(sound);
138 initVibrateWhenRinging(sound);
140 final PreferenceCategory notification = (PreferenceCategory)
141 findPreference(KEY_NOTIFICATION);
142 initPulse(notification);
143 initLockscreenNotifications(notification);
145 mNotificationAccess = findPreference(KEY_NOTIFICATION_ACCESS);
146 refreshNotificationListeners();
148 updateEffectsSuppressor();
152 public void onResume() {
154 refreshNotificationListeners();
155 lookupRingtoneNames();
156 mSettingsObserver.register(true);
157 mReceiver.register(true);
158 updateRingOrNotificationPreference();
159 updateEffectsSuppressor();
160 for (VolumeSeekBarPreference volumePref : mVolumePrefs) {
161 volumePref.onActivityResume();
166 public void onPause() {
168 mVolumeCallback.stopSample();
169 mSettingsObserver.register(false);
170 mReceiver.register(false);
175 private VolumeSeekBarPreference initVolumePreference(String key, int stream, int muteIcon) {
176 final VolumeSeekBarPreference volumePref = (VolumeSeekBarPreference) findPreference(key);
177 volumePref.setCallback(mVolumeCallback);
178 volumePref.setStream(stream);
179 mVolumePrefs.add(volumePref);
180 volumePref.setMuteIcon(muteIcon);
184 private void updateRingOrNotificationPreference() {
185 mRingOrNotificationPreference.showIcon(mSuppressor != null
186 ? com.android.internal.R.drawable.ic_audio_ring_notif_mute
187 : mRingerMode == AudioManager.RINGER_MODE_VIBRATE
188 ? com.android.internal.R.drawable.ic_audio_ring_notif_vibrate
189 : com.android.internal.R.drawable.ic_audio_ring_notif);
192 private void updateRingerMode() {
193 final int ringerMode = mAudioManager.getRingerModeInternal();
194 if (mRingerMode == ringerMode) return;
195 mRingerMode = ringerMode;
196 updateRingOrNotificationPreference();
199 private void updateEffectsSuppressor() {
200 final ComponentName suppressor = NotificationManager.from(mContext).getEffectsSuppressor();
201 if (Objects.equals(suppressor, mSuppressor)) return;
202 mSuppressor = suppressor;
203 if (mRingOrNotificationPreference != null) {
204 final String text = suppressor != null ?
205 mContext.getString(com.android.internal.R.string.muted_by,
206 getSuppressorCaption(suppressor)) : null;
207 mRingOrNotificationPreference.setSuppressionText(text);
209 updateRingOrNotificationPreference();
212 private String getSuppressorCaption(ComponentName suppressor) {
213 final PackageManager pm = mContext.getPackageManager();
215 final ServiceInfo info = pm.getServiceInfo(suppressor, 0);
217 final CharSequence seq = info.loadLabel(pm);
219 final String str = seq.toString().trim();
220 if (str.length() > 0) {
225 } catch (Throwable e) {
226 Log.w(TAG, "Error loading suppressor caption", e);
228 return suppressor.getPackageName();
231 private final class VolumePreferenceCallback implements VolumeSeekBarPreference.Callback {
232 private SeekBarVolumizer mCurrent;
235 public void onSampleStarting(SeekBarVolumizer sbv) {
236 if (mCurrent != null && mCurrent != sbv) {
237 mCurrent.stopSample();
240 if (mCurrent != null) {
241 mHandler.removeMessages(H.STOP_SAMPLE);
242 mHandler.sendEmptyMessageDelayed(H.STOP_SAMPLE, SAMPLE_CUTOFF);
247 public void onStreamValueChanged(int stream, int progress) {
251 public void stopSample() {
252 if (mCurrent != null) {
253 mCurrent.stopSample();
259 // === Phone & notification ringtone ===
261 private void initRingtones(PreferenceCategory root) {
262 mPhoneRingtonePreference = root.findPreference(KEY_PHONE_RINGTONE);
263 if (mPhoneRingtonePreference != null && !mVoiceCapable) {
264 root.removePreference(mPhoneRingtonePreference);
265 mPhoneRingtonePreference = null;
267 mNotificationRingtonePreference = root.findPreference(KEY_NOTIFICATION_RINGTONE);
270 private void lookupRingtoneNames() {
271 AsyncTask.execute(mLookupRingtoneNames);
274 private final Runnable mLookupRingtoneNames = new Runnable() {
277 if (mPhoneRingtonePreference != null) {
278 final CharSequence summary = updateRingtoneName(
279 mContext, RingtoneManager.TYPE_RINGTONE);
280 if (summary != null) {
281 mHandler.obtainMessage(H.UPDATE_PHONE_RINGTONE, summary).sendToTarget();
284 if (mNotificationRingtonePreference != null) {
285 final CharSequence summary = updateRingtoneName(
286 mContext, RingtoneManager.TYPE_NOTIFICATION);
287 if (summary != null) {
288 mHandler.obtainMessage(H.UPDATE_NOTIFICATION_RINGTONE, summary).sendToTarget();
294 private static CharSequence updateRingtoneName(Context context, int type) {
295 if (context == null) {
296 Log.e(TAG, "Unable to update ringtone name, no context provided");
299 Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
300 CharSequence summary = context.getString(com.android.internal.R.string.ringtone_unknown);
301 // Is it a silent ringtone?
302 if (ringtoneUri == null) {
303 summary = context.getString(com.android.internal.R.string.ringtone_silent);
305 Cursor cursor = null;
307 if (MediaStore.AUTHORITY.equals(ringtoneUri.getAuthority())) {
308 // Fetch the ringtone title from the media provider
309 cursor = context.getContentResolver().query(ringtoneUri,
310 new String[] { MediaStore.Audio.Media.TITLE }, null, null, null);
311 } else if (ContentResolver.SCHEME_CONTENT.equals(ringtoneUri.getScheme())) {
312 cursor = context.getContentResolver().query(ringtoneUri,
313 new String[] { OpenableColumns.DISPLAY_NAME }, null, null, null);
315 if (cursor != null) {
316 if (cursor.moveToFirst()) {
317 summary = cursor.getString(0);
320 } catch (SQLiteException sqle) {
321 // Unknown title for the ringtone
322 } catch (IllegalArgumentException iae) {
323 // Some other error retrieving the column from the provider
325 if (cursor != null) {
333 // === Vibrate when ringing ===
335 private void initVibrateWhenRinging(PreferenceCategory root) {
336 mVibrateWhenRinging = (TwoStatePreference) root.findPreference(KEY_VIBRATE_WHEN_RINGING);
337 if (mVibrateWhenRinging == null) {
338 Log.i(TAG, "Preference not found: " + KEY_VIBRATE_WHEN_RINGING);
341 if (!mVoiceCapable) {
342 root.removePreference(mVibrateWhenRinging);
343 mVibrateWhenRinging = null;
346 mVibrateWhenRinging.setPersistent(false);
347 updateVibrateWhenRinging();
348 mVibrateWhenRinging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
350 public boolean onPreferenceChange(Preference preference, Object newValue) {
351 final boolean val = (Boolean) newValue;
352 return Settings.System.putInt(getContentResolver(),
353 Settings.System.VIBRATE_WHEN_RINGING,
359 private void updateVibrateWhenRinging() {
360 if (mVibrateWhenRinging == null) return;
361 mVibrateWhenRinging.setChecked(Settings.System.getInt(getContentResolver(),
362 Settings.System.VIBRATE_WHEN_RINGING, 0) != 0);
365 // === Pulse notification light ===
367 private void initPulse(PreferenceCategory parent) {
368 mNotificationPulse = (TwoStatePreference) parent.findPreference(KEY_NOTIFICATION_PULSE);
369 if (mNotificationPulse == null) {
370 Log.i(TAG, "Preference not found: " + KEY_NOTIFICATION_PULSE);
374 .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) {
375 parent.removePreference(mNotificationPulse);
378 mNotificationPulse.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
380 public boolean onPreferenceChange(Preference preference, Object newValue) {
381 final boolean val = (Boolean)newValue;
382 return Settings.System.putInt(getContentResolver(),
383 Settings.System.NOTIFICATION_LIGHT_PULSE,
390 private void updatePulse() {
391 if (mNotificationPulse == null) {
395 mNotificationPulse.setChecked(Settings.System.getInt(getContentResolver(),
396 Settings.System.NOTIFICATION_LIGHT_PULSE) == 1);
397 } catch (Settings.SettingNotFoundException snfe) {
398 Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found");
402 // === Lockscreen (public / private) notifications ===
404 private void initLockscreenNotifications(PreferenceCategory parent) {
405 mLockscreen = (DropDownPreference) parent.findPreference(KEY_LOCK_SCREEN_NOTIFICATIONS);
406 if (mLockscreen == null) {
407 Log.i(TAG, "Preference not found: " + KEY_LOCK_SCREEN_NOTIFICATIONS);
411 mLockscreen.addItem(R.string.lock_screen_notifications_summary_show,
412 R.string.lock_screen_notifications_summary_show);
414 mLockscreen.addItem(R.string.lock_screen_notifications_summary_hide,
415 R.string.lock_screen_notifications_summary_hide);
417 mLockscreen.addItem(R.string.lock_screen_notifications_summary_disable,
418 R.string.lock_screen_notifications_summary_disable);
419 updateLockscreenNotifications();
420 mLockscreen.setCallback(new DropDownPreference.Callback() {
422 public boolean onItemSelected(int pos, Object value) {
423 final int val = (Integer) value;
424 if (val == mLockscreenSelectedValue) {
427 final boolean enabled = val != R.string.lock_screen_notifications_summary_disable;
428 final boolean show = val == R.string.lock_screen_notifications_summary_show;
429 Settings.Secure.putInt(getContentResolver(),
430 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, show ? 1 : 0);
431 Settings.Secure.putInt(getContentResolver(),
432 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, enabled ? 1 : 0);
433 mLockscreenSelectedValue = val;
439 private void updateLockscreenNotifications() {
440 if (mLockscreen == null) {
443 final boolean enabled = getLockscreenNotificationsEnabled();
444 final boolean allowPrivate = !mSecure || getLockscreenAllowPrivateNotifications();
445 mLockscreenSelectedValue = !enabled ? R.string.lock_screen_notifications_summary_disable :
446 allowPrivate ? R.string.lock_screen_notifications_summary_show :
447 R.string.lock_screen_notifications_summary_hide;
448 mLockscreen.setSelectedValue(mLockscreenSelectedValue);
451 private boolean getLockscreenNotificationsEnabled() {
452 return Settings.Secure.getInt(getContentResolver(),
453 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) != 0;
456 private boolean getLockscreenAllowPrivateNotifications() {
457 return Settings.Secure.getInt(getContentResolver(),
458 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0) != 0;
461 // === Notification listeners ===
463 private void refreshNotificationListeners() {
464 if (mNotificationAccess != null) {
465 final int total = NotificationAccessSettings.getListenersCount(mPM);
467 getPreferenceScreen().removePreference(mNotificationAccess);
469 final int n = NotificationAccessSettings.getEnabledListenersCount(mContext);
471 mNotificationAccess.setSummary(getResources().getString(
472 R.string.manage_notification_access_summary_zero));
474 mNotificationAccess.setSummary(String.format(getResources().getQuantityString(
475 R.plurals.manage_notification_access_summary_nonzero,
484 private final class SettingsObserver extends ContentObserver {
485 private final Uri VIBRATE_WHEN_RINGING_URI =
486 Settings.System.getUriFor(Settings.System.VIBRATE_WHEN_RINGING);
487 private final Uri NOTIFICATION_LIGHT_PULSE_URI =
488 Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
489 private final Uri LOCK_SCREEN_PRIVATE_URI =
490 Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
491 private final Uri LOCK_SCREEN_SHOW_URI =
492 Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
494 public SettingsObserver() {
498 public void register(boolean register) {
499 final ContentResolver cr = getContentResolver();
501 cr.registerContentObserver(VIBRATE_WHEN_RINGING_URI, false, this);
502 cr.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, false, this);
503 cr.registerContentObserver(LOCK_SCREEN_PRIVATE_URI, false, this);
504 cr.registerContentObserver(LOCK_SCREEN_SHOW_URI, false, this);
506 cr.unregisterContentObserver(this);
511 public void onChange(boolean selfChange, Uri uri) {
512 super.onChange(selfChange, uri);
513 if (VIBRATE_WHEN_RINGING_URI.equals(uri)) {
514 updateVibrateWhenRinging();
516 if (NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) {
519 if (LOCK_SCREEN_PRIVATE_URI.equals(uri) || LOCK_SCREEN_SHOW_URI.equals(uri)) {
520 updateLockscreenNotifications();
525 private final class H extends Handler {
526 private static final int UPDATE_PHONE_RINGTONE = 1;
527 private static final int UPDATE_NOTIFICATION_RINGTONE = 2;
528 private static final int STOP_SAMPLE = 3;
529 private static final int UPDATE_EFFECTS_SUPPRESSOR = 4;
530 private static final int UPDATE_RINGER_MODE = 5;
533 super(Looper.getMainLooper());
537 public void handleMessage(Message msg) {
539 case UPDATE_PHONE_RINGTONE:
540 mPhoneRingtonePreference.setSummary((CharSequence) msg.obj);
542 case UPDATE_NOTIFICATION_RINGTONE:
543 mNotificationRingtonePreference.setSummary((CharSequence) msg.obj);
546 mVolumeCallback.stopSample();
548 case UPDATE_EFFECTS_SUPPRESSOR:
549 updateEffectsSuppressor();
551 case UPDATE_RINGER_MODE:
558 private class Receiver extends BroadcastReceiver {
559 private boolean mRegistered;
561 public void register(boolean register) {
562 if (mRegistered == register) return;
564 final IntentFilter filter = new IntentFilter();
565 filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED);
566 filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);
567 mContext.registerReceiver(this, filter);
569 mContext.unregisterReceiver(this);
571 mRegistered = register;
575 public void onReceive(Context context, Intent intent) {
576 final String action = intent.getAction();
577 if (NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED.equals(action)) {
578 mHandler.sendEmptyMessage(H.UPDATE_EFFECTS_SUPPRESSOR);
579 } else if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) {
580 mHandler.sendEmptyMessage(H.UPDATE_RINGER_MODE);
587 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
588 new BaseSearchIndexProvider() {
590 public List<SearchIndexableResource> getXmlResourcesToIndex(
591 Context context, boolean enabled) {
592 final SearchIndexableResource sir = new SearchIndexableResource(context);
593 sir.xmlResId = R.xml.notification_settings;
594 return Arrays.asList(sir);
597 public List<String> getNonIndexableKeys(Context context) {
598 final ArrayList<String> rt = new ArrayList<String>();
599 if (Utils.isVoiceCapable(context)) {
600 rt.add(KEY_NOTIFICATION_VOLUME);
602 rt.add(KEY_RING_VOLUME);
603 rt.add(KEY_PHONE_RINGTONE);
604 rt.add(KEY_VIBRATE_WHEN_RINGING);