John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame^] | 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 | |
| 17 | package com.android.server.notification; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.media.AudioAttributes; |
| 23 | import android.media.AudioManager; |
| 24 | import android.os.Bundle; |
| 25 | import android.os.UserHandle; |
| 26 | import android.provider.Settings.Global; |
| 27 | import android.provider.Settings.Secure; |
| 28 | import android.service.notification.ZenModeConfig; |
| 29 | import android.telecom.TelecomManager; |
| 30 | import android.util.Slog; |
| 31 | |
| 32 | import java.util.Objects; |
| 33 | |
| 34 | public class ZenModeFiltering { |
| 35 | private static final String TAG = ZenModeHelper.TAG; |
| 36 | private static final boolean DEBUG = ZenModeHelper.DEBUG; |
| 37 | |
| 38 | private final Context mContext; |
| 39 | |
| 40 | private ComponentName mDefaultPhoneApp; |
| 41 | |
| 42 | public ZenModeFiltering(Context context) { |
| 43 | mContext = context; |
| 44 | } |
| 45 | |
| 46 | public ComponentName getDefaultPhoneApp() { |
| 47 | return mDefaultPhoneApp; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param extras extras of the notification with EXTRA_PEOPLE populated |
| 52 | * @param contactsTimeoutMs timeout in milliseconds to wait for contacts response |
| 53 | * @param timeoutAffinity affinity to return when the timeout specified via |
| 54 | * <code>contactsTimeoutMs</code> is hit |
| 55 | */ |
| 56 | public static boolean matchesCallFilter(int zen, ZenModeConfig config, UserHandle userHandle, |
| 57 | Bundle extras, ValidateNotificationPeople validator, int contactsTimeoutMs, |
| 58 | float timeoutAffinity) { |
| 59 | if (zen == Global.ZEN_MODE_NO_INTERRUPTIONS) return false; // nothing gets through |
| 60 | if (zen == Global.ZEN_MODE_ALARMS) return false; // not an alarm |
| 61 | if (zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) { |
| 62 | if (!config.allowCalls) return false; // no calls get through |
| 63 | if (validator != null) { |
| 64 | final float contactAffinity = validator.getContactAffinity(userHandle, extras, |
| 65 | contactsTimeoutMs, timeoutAffinity); |
| 66 | return audienceMatches(config, contactAffinity); |
| 67 | } |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | public boolean shouldIntercept(int zen, ZenModeConfig config, NotificationRecord record) { |
| 73 | if (isSystem(record)) { |
| 74 | return false; |
| 75 | } |
| 76 | switch (zen) { |
| 77 | case Global.ZEN_MODE_NO_INTERRUPTIONS: |
| 78 | // #notevenalarms |
| 79 | ZenLog.traceIntercepted(record, "none"); |
| 80 | return true; |
| 81 | case Global.ZEN_MODE_ALARMS: |
| 82 | if (isAlarm(record)) { |
| 83 | // Alarms only |
| 84 | return false; |
| 85 | } |
| 86 | ZenLog.traceIntercepted(record, "alarmsOnly"); |
| 87 | return true; |
| 88 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
| 89 | if (isAlarm(record)) { |
| 90 | // Alarms are always priority |
| 91 | return false; |
| 92 | } |
| 93 | // allow user-prioritized packages through in priority mode |
| 94 | if (record.getPackagePriority() == Notification.PRIORITY_MAX) { |
| 95 | ZenLog.traceNotIntercepted(record, "priorityApp"); |
| 96 | return false; |
| 97 | } |
| 98 | if (isCall(record)) { |
| 99 | if (!config.allowCalls) { |
| 100 | ZenLog.traceIntercepted(record, "!allowCalls"); |
| 101 | return true; |
| 102 | } |
| 103 | return shouldInterceptAudience(config, record); |
| 104 | } |
| 105 | if (isMessage(record)) { |
| 106 | if (!config.allowMessages) { |
| 107 | ZenLog.traceIntercepted(record, "!allowMessages"); |
| 108 | return true; |
| 109 | } |
| 110 | return shouldInterceptAudience(config, record); |
| 111 | } |
| 112 | if (isEvent(record)) { |
| 113 | if (!config.allowEvents) { |
| 114 | ZenLog.traceIntercepted(record, "!allowEvents"); |
| 115 | return true; |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | if (isReminder(record)) { |
| 120 | if (!config.allowReminders) { |
| 121 | ZenLog.traceIntercepted(record, "!allowReminders"); |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | ZenLog.traceIntercepted(record, "!priority"); |
| 127 | return true; |
| 128 | default: |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private static boolean shouldInterceptAudience(ZenModeConfig config, |
| 134 | NotificationRecord record) { |
| 135 | if (!audienceMatches(config, record.getContactAffinity())) { |
| 136 | ZenLog.traceIntercepted(record, "!audienceMatches"); |
| 137 | return true; |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | private static boolean isSystem(NotificationRecord record) { |
| 143 | return record.isCategory(Notification.CATEGORY_SYSTEM); |
| 144 | } |
| 145 | |
| 146 | private static boolean isAlarm(NotificationRecord record) { |
| 147 | return record.isCategory(Notification.CATEGORY_ALARM) |
| 148 | || record.isAudioStream(AudioManager.STREAM_ALARM) |
| 149 | || record.isAudioAttributesUsage(AudioAttributes.USAGE_ALARM); |
| 150 | } |
| 151 | |
| 152 | private static boolean isEvent(NotificationRecord record) { |
| 153 | return record.isCategory(Notification.CATEGORY_EVENT); |
| 154 | } |
| 155 | |
| 156 | private static boolean isReminder(NotificationRecord record) { |
| 157 | return record.isCategory(Notification.CATEGORY_REMINDER); |
| 158 | } |
| 159 | |
| 160 | public boolean isCall(NotificationRecord record) { |
| 161 | return record != null && (isDefaultPhoneApp(record.sbn.getPackageName()) |
| 162 | || record.isCategory(Notification.CATEGORY_CALL)); |
| 163 | } |
| 164 | |
| 165 | private boolean isDefaultPhoneApp(String pkg) { |
| 166 | if (mDefaultPhoneApp == null) { |
| 167 | final TelecomManager telecomm = |
| 168 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
| 169 | mDefaultPhoneApp = telecomm != null ? telecomm.getDefaultPhoneApp() : null; |
| 170 | if (DEBUG) Slog.d(TAG, "Default phone app: " + mDefaultPhoneApp); |
| 171 | } |
| 172 | return pkg != null && mDefaultPhoneApp != null |
| 173 | && pkg.equals(mDefaultPhoneApp.getPackageName()); |
| 174 | } |
| 175 | |
| 176 | @SuppressWarnings("deprecation") |
| 177 | private boolean isDefaultMessagingApp(NotificationRecord record) { |
| 178 | final int userId = record.getUserId(); |
| 179 | if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false; |
| 180 | final String defaultApp = Secure.getStringForUser(mContext.getContentResolver(), |
| 181 | Secure.SMS_DEFAULT_APPLICATION, userId); |
| 182 | return Objects.equals(defaultApp, record.sbn.getPackageName()); |
| 183 | } |
| 184 | |
| 185 | private boolean isMessage(NotificationRecord record) { |
| 186 | return record.isCategory(Notification.CATEGORY_MESSAGE) || isDefaultMessagingApp(record); |
| 187 | } |
| 188 | |
| 189 | private static boolean audienceMatches(ZenModeConfig config, float contactAffinity) { |
| 190 | switch (config.allowFrom) { |
| 191 | case ZenModeConfig.SOURCE_ANYONE: |
| 192 | return true; |
| 193 | case ZenModeConfig.SOURCE_CONTACT: |
| 194 | return contactAffinity >= ValidateNotificationPeople.VALID_CONTACT; |
| 195 | case ZenModeConfig.SOURCE_STAR: |
| 196 | return contactAffinity >= ValidateNotificationPeople.STARRED_CONTACT; |
| 197 | default: |
| 198 | Slog.w(TAG, "Encountered unknown source: " + config.allowFrom); |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | } |