ZenModeHelper.java revision 2b122f4c2e691f0319e4f9ea5873989792bb56a6
1/** 2 * Copyright (c) 2014, 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.server.notification; 18 19import static android.media.AudioAttributes.USAGE_ALARM; 20import static android.media.AudioAttributes.USAGE_NOTIFICATION_RINGTONE; 21import static android.media.AudioAttributes.USAGE_UNKNOWN; 22 23import android.app.AppOpsManager; 24import android.app.Notification; 25import android.content.BroadcastReceiver; 26import android.content.ComponentName; 27import android.content.ContentResolver; 28import android.content.Context; 29import android.content.Intent; 30import android.content.IntentFilter; 31import android.content.res.Resources; 32import android.content.res.XmlResourceParser; 33import android.database.ContentObserver; 34import android.media.AudioAttributes; 35import android.media.AudioManager; 36import android.net.Uri; 37import android.os.Bundle; 38import android.os.Handler; 39import android.os.UserHandle; 40import android.provider.Settings.Global; 41import android.provider.Settings.Secure; 42import android.service.notification.NotificationListenerService; 43import android.service.notification.ZenModeConfig; 44import android.telecomm.TelecommManager; 45import android.util.Log; 46import android.util.Slog; 47 48import com.android.internal.R; 49 50import libcore.io.IoUtils; 51 52import org.xmlpull.v1.XmlPullParser; 53import org.xmlpull.v1.XmlPullParserException; 54import org.xmlpull.v1.XmlSerializer; 55 56import java.io.IOException; 57import java.io.PrintWriter; 58import java.util.ArrayList; 59import java.util.Objects; 60 61/** 62 * NotificationManagerService helper for functionality related to zen mode. 63 */ 64public class ZenModeHelper { 65 private static final String TAG = "ZenModeHelper"; 66 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 67 68 private final Context mContext; 69 private final Handler mHandler; 70 private final SettingsObserver mSettingsObserver; 71 private final AppOpsManager mAppOps; 72 private final ZenModeConfig mDefaultConfig; 73 private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); 74 75 private ComponentName mDefaultPhoneApp; 76 private int mZenMode; 77 private ZenModeConfig mConfig; 78 private AudioManager mAudioManager; 79 private int mPreviousRingerMode = -1; 80 81 public ZenModeHelper(Context context, Handler handler) { 82 mContext = context; 83 mHandler = handler; 84 mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 85 mDefaultConfig = readDefaultConfig(context.getResources()); 86 mConfig = mDefaultConfig; 87 mSettingsObserver = new SettingsObserver(mHandler); 88 mSettingsObserver.observe(); 89 90 final IntentFilter filter = new IntentFilter(); 91 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION); 92 mContext.registerReceiver(mReceiver, filter); 93 } 94 95 public static ZenModeConfig readDefaultConfig(Resources resources) { 96 XmlResourceParser parser = null; 97 try { 98 parser = resources.getXml(R.xml.default_zen_mode_config); 99 while (parser.next() != XmlPullParser.END_DOCUMENT) { 100 final ZenModeConfig config = ZenModeConfig.readXml(parser); 101 if (config != null) return config; 102 } 103 } catch (Exception e) { 104 Slog.w(TAG, "Error reading default zen mode config from resource", e); 105 } finally { 106 IoUtils.closeQuietly(parser); 107 } 108 return new ZenModeConfig(); 109 } 110 111 public void addCallback(Callback callback) { 112 mCallbacks.add(callback); 113 } 114 115 public void setAudioManager(AudioManager audioManager) { 116 mAudioManager = audioManager; 117 } 118 119 public int getZenModeListenerInterruptionFilter() { 120 switch (mZenMode) { 121 case Global.ZEN_MODE_OFF: 122 return NotificationListenerService.INTERRUPTION_FILTER_ALL; 123 case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: 124 return NotificationListenerService.INTERRUPTION_FILTER_PRIORITY; 125 case Global.ZEN_MODE_NO_INTERRUPTIONS: 126 return NotificationListenerService.INTERRUPTION_FILTER_NONE; 127 default: 128 return 0; 129 } 130 } 131 132 private static int zenModeFromListenerInterruptionFilter(int listenerInterruptionFilter, 133 int defValue) { 134 switch (listenerInterruptionFilter) { 135 case NotificationListenerService.INTERRUPTION_FILTER_ALL: 136 return Global.ZEN_MODE_OFF; 137 case NotificationListenerService.INTERRUPTION_FILTER_PRIORITY: 138 return Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; 139 case NotificationListenerService.INTERRUPTION_FILTER_NONE: 140 return Global.ZEN_MODE_NO_INTERRUPTIONS; 141 default: 142 return defValue; 143 } 144 } 145 146 public void requestFromListener(int interruptionFilter) { 147 final int newZen = zenModeFromListenerInterruptionFilter(interruptionFilter, -1); 148 if (newZen != -1) { 149 setZenMode(newZen, "listener"); 150 } 151 } 152 153 public boolean shouldIntercept(NotificationRecord record) { 154 if (mZenMode != Global.ZEN_MODE_OFF) { 155 if (isSystem(record)) { 156 return false; 157 } 158 if (isAlarm(record)) { 159 if (mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) { 160 ZenLog.traceIntercepted(record, "alarm"); 161 return true; 162 } 163 return false; 164 } 165 // allow user-prioritized packages through in priority mode 166 if (mZenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) { 167 if (record.getPackagePriority() == Notification.PRIORITY_MAX) { 168 ZenLog.traceNotIntercepted(record, "priorityApp"); 169 return false; 170 } 171 } 172 if (isCall(record)) { 173 if (!mConfig.allowCalls) { 174 ZenLog.traceIntercepted(record, "!allowCalls"); 175 return true; 176 } 177 return shouldInterceptAudience(record); 178 } 179 if (isMessage(record)) { 180 if (!mConfig.allowMessages) { 181 ZenLog.traceIntercepted(record, "!allowMessages"); 182 return true; 183 } 184 return shouldInterceptAudience(record); 185 } 186 ZenLog.traceIntercepted(record, "!allowed"); 187 return true; 188 } 189 return false; 190 } 191 192 private boolean shouldInterceptAudience(NotificationRecord record) { 193 if (!audienceMatches(record.getContactAffinity())) { 194 ZenLog.traceIntercepted(record, "!audienceMatches"); 195 return true; 196 } 197 return false; 198 } 199 200 public int getZenMode() { 201 return mZenMode; 202 } 203 204 public void setZenMode(int zenModeValue, String reason) { 205 ZenLog.traceSetZenMode(zenModeValue, reason); 206 Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, zenModeValue); 207 } 208 209 public void updateZenMode() { 210 final int mode = Global.getInt(mContext.getContentResolver(), 211 Global.ZEN_MODE, Global.ZEN_MODE_OFF); 212 if (mode != mZenMode) { 213 ZenLog.traceUpdateZenMode(mZenMode, mode); 214 } 215 mZenMode = mode; 216 final boolean zen = mZenMode != Global.ZEN_MODE_OFF; 217 final String[] exceptionPackages = null; // none (for now) 218 219 // call restrictions 220 final boolean muteCalls = zen && !mConfig.allowCalls; 221 mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, USAGE_NOTIFICATION_RINGTONE, 222 muteCalls ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED, 223 exceptionPackages); 224 mAppOps.setRestriction(AppOpsManager.OP_PLAY_AUDIO, USAGE_NOTIFICATION_RINGTONE, 225 muteCalls ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED, 226 exceptionPackages); 227 228 // restrict vibrations with no hints 229 mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, USAGE_UNKNOWN, 230 zen ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED, 231 exceptionPackages); 232 233 // alarm restrictions 234 final boolean muteAlarms = mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS; 235 mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, USAGE_ALARM, 236 muteAlarms ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED, 237 exceptionPackages); 238 mAppOps.setRestriction(AppOpsManager.OP_PLAY_AUDIO, USAGE_ALARM, 239 muteAlarms ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED, 240 exceptionPackages); 241 242 // force ringer mode into compliance 243 if (mAudioManager != null) { 244 int ringerMode = mAudioManager.getRingerMode(); 245 int forcedRingerMode = -1; 246 if (mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) { 247 if (ringerMode != AudioManager.RINGER_MODE_SILENT) { 248 mPreviousRingerMode = ringerMode; 249 if (DEBUG) Slog.d(TAG, "Silencing ringer"); 250 forcedRingerMode = AudioManager.RINGER_MODE_SILENT; 251 } 252 } else { 253 if (ringerMode == AudioManager.RINGER_MODE_SILENT) { 254 if (DEBUG) Slog.d(TAG, "Unsilencing ringer"); 255 forcedRingerMode = mPreviousRingerMode != -1 ? mPreviousRingerMode 256 : AudioManager.RINGER_MODE_NORMAL; 257 mPreviousRingerMode = -1; 258 } 259 } 260 if (forcedRingerMode != -1) { 261 mAudioManager.setRingerMode(forcedRingerMode); 262 ZenLog.traceSetRingerMode(forcedRingerMode); 263 } 264 } 265 dispatchOnZenModeChanged(); 266 } 267 268 public void dump(PrintWriter pw, String prefix) { 269 pw.print(prefix); pw.print("mZenMode="); 270 pw.println(Global.zenModeToString(mZenMode)); 271 pw.print(prefix); pw.print("mConfig="); pw.println(mConfig); 272 pw.print(prefix); pw.print("mDefaultConfig="); pw.println(mDefaultConfig); 273 pw.print(prefix); pw.print("mPreviousRingerMode="); pw.println(mPreviousRingerMode); 274 pw.print(prefix); pw.print("mDefaultPhoneApp="); pw.println(mDefaultPhoneApp); 275 } 276 277 public void readXml(XmlPullParser parser) throws XmlPullParserException, IOException { 278 final ZenModeConfig config = ZenModeConfig.readXml(parser); 279 if (config != null) { 280 setConfig(config); 281 } 282 } 283 284 public void writeXml(XmlSerializer out) throws IOException { 285 mConfig.writeXml(out); 286 } 287 288 public ZenModeConfig getConfig() { 289 return mConfig; 290 } 291 292 public boolean setConfig(ZenModeConfig config) { 293 if (config == null || !config.isValid()) return false; 294 if (config.equals(mConfig)) return true; 295 ZenLog.traceConfig(mConfig, config); 296 mConfig = config; 297 dispatchOnConfigChanged(); 298 final String val = Integer.toString(mConfig.hashCode()); 299 Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_CONFIG_ETAG, val); 300 updateZenMode(); 301 return true; 302 } 303 304 private void handleRingerModeChanged() { 305 if (mAudioManager != null) { 306 // follow ringer mode if necessary 307 final int ringerMode = mAudioManager.getRingerMode(); 308 int newZen = -1; 309 if (ringerMode == AudioManager.RINGER_MODE_SILENT) { 310 if (mZenMode != Global.ZEN_MODE_NO_INTERRUPTIONS) { 311 newZen = Global.ZEN_MODE_NO_INTERRUPTIONS; 312 } 313 } else if ((ringerMode == AudioManager.RINGER_MODE_NORMAL 314 || ringerMode == AudioManager.RINGER_MODE_VIBRATE) 315 && mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) { 316 newZen = Global.ZEN_MODE_OFF; 317 } 318 if (newZen != -1) { 319 ZenLog.traceFollowRingerMode(ringerMode, mZenMode, newZen); 320 setZenMode(newZen, "ringerMode"); 321 } 322 } 323 } 324 325 private void dispatchOnConfigChanged() { 326 for (Callback callback : mCallbacks) { 327 callback.onConfigChanged(); 328 } 329 } 330 331 private void dispatchOnZenModeChanged() { 332 for (Callback callback : mCallbacks) { 333 callback.onZenModeChanged(); 334 } 335 } 336 337 private boolean isSystem(NotificationRecord record) { 338 return record.isCategory(Notification.CATEGORY_SYSTEM); 339 } 340 341 private boolean isAlarm(NotificationRecord record) { 342 return record.isCategory(Notification.CATEGORY_ALARM) 343 || record.isCategory(Notification.CATEGORY_EVENT) 344 || record.isAudioStream(AudioManager.STREAM_ALARM) 345 || record.isAudioAttributesUsage(AudioAttributes.USAGE_ALARM); 346 } 347 348 private boolean isCall(NotificationRecord record) { 349 return isDefaultPhoneApp(record.sbn.getPackageName()) 350 || record.isCategory(Notification.CATEGORY_CALL); 351 } 352 353 private boolean isDefaultPhoneApp(String pkg) { 354 if (mDefaultPhoneApp == null) { 355 final TelecommManager telecomm = 356 (TelecommManager) mContext.getSystemService(Context.TELECOMM_SERVICE); 357 mDefaultPhoneApp = telecomm != null ? telecomm.getDefaultPhoneApp() : null; 358 if (DEBUG) Slog.d(TAG, "Default phone app: " + mDefaultPhoneApp); 359 } 360 return pkg != null && mDefaultPhoneApp != null 361 && pkg.equals(mDefaultPhoneApp.getPackageName()); 362 } 363 364 private boolean isDefaultMessagingApp(NotificationRecord record) { 365 final int userId = record.getUserId(); 366 if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false; 367 final String defaultApp = Secure.getStringForUser(mContext.getContentResolver(), 368 Secure.SMS_DEFAULT_APPLICATION, userId); 369 return Objects.equals(defaultApp, record.sbn.getPackageName()); 370 } 371 372 private boolean isMessage(NotificationRecord record) { 373 return record.isCategory(Notification.CATEGORY_MESSAGE) || isDefaultMessagingApp(record); 374 } 375 376 public boolean matchesCallFilter(Bundle extras, ValidateNotificationPeople validator) { 377 final int zen = mZenMode; 378 if (zen == Global.ZEN_MODE_NO_INTERRUPTIONS) return false; // nothing gets through 379 if (zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) { 380 if (!mConfig.allowCalls) return false; // no calls get through 381 if (validator != null) { 382 final float contactAffinity = validator.getContactAffinity(extras); 383 return audienceMatches(contactAffinity); 384 } 385 } 386 return true; 387 } 388 389 private boolean audienceMatches(float contactAffinity) { 390 switch (mConfig.allowFrom) { 391 case ZenModeConfig.SOURCE_ANYONE: 392 return true; 393 case ZenModeConfig.SOURCE_CONTACT: 394 return contactAffinity >= ValidateNotificationPeople.VALID_CONTACT; 395 case ZenModeConfig.SOURCE_STAR: 396 return contactAffinity >= ValidateNotificationPeople.STARRED_CONTACT; 397 default: 398 Slog.w(TAG, "Encountered unknown source: " + mConfig.allowFrom); 399 return true; 400 } 401 } 402 403 private final Runnable mRingerModeChanged = new Runnable() { 404 @Override 405 public void run() { 406 handleRingerModeChanged(); 407 } 408 }; 409 410 private class SettingsObserver extends ContentObserver { 411 private final Uri ZEN_MODE = Global.getUriFor(Global.ZEN_MODE); 412 413 public SettingsObserver(Handler handler) { 414 super(handler); 415 } 416 417 public void observe() { 418 final ContentResolver resolver = mContext.getContentResolver(); 419 resolver.registerContentObserver(ZEN_MODE, false /*notifyForDescendents*/, this); 420 update(null); 421 } 422 423 @Override 424 public void onChange(boolean selfChange, Uri uri) { 425 update(uri); 426 } 427 428 public void update(Uri uri) { 429 if (ZEN_MODE.equals(uri)) { 430 updateZenMode(); 431 } 432 } 433 } 434 435 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 436 @Override 437 public void onReceive(Context context, Intent intent) { 438 mHandler.post(mRingerModeChanged); 439 } 440 }; 441 442 public static class Callback { 443 void onConfigChanged() {} 444 void onZenModeChanged() {} 445 } 446} 447