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 17 package com.android.server.notification; 18 19 import android.annotation.Nullable; 20 import android.app.NotificationManager; 21 import android.content.ComponentName; 22 import android.media.AudioManager; 23 import android.net.Uri; 24 import android.os.Build; 25 import android.os.RemoteException; 26 import android.provider.Settings.Global; 27 import android.service.notification.IConditionProvider; 28 import android.service.notification.NotificationListenerService; 29 import android.service.notification.ZenModeConfig; 30 import android.service.notification.ZenModeConfig.ConfigOrigin; 31 import android.service.notification.ZenModeDiff; 32 import android.util.LocalLog; 33 34 import com.android.internal.annotations.VisibleForTesting; 35 36 import java.io.PrintWriter; 37 import java.util.List; 38 39 public class ZenLog { 40 41 private static final int SIZE = Build.IS_DEBUGGABLE ? 200 : 100; 42 43 private static final LocalLog STATE_CHANGES = new LocalLog(SIZE); 44 private static final LocalLog INTERCEPTION_EVENTS = new LocalLog(SIZE); 45 46 private static final int TYPE_INTERCEPTED = 1; 47 private static final int TYPE_SET_RINGER_MODE_EXTERNAL = 3; 48 private static final int TYPE_SET_RINGER_MODE_INTERNAL = 4; 49 private static final int TYPE_SET_ZEN_MODE = 6; 50 private static final int TYPE_SUBSCRIBE = 9; 51 private static final int TYPE_UNSUBSCRIBE = 10; 52 private static final int TYPE_CONFIG = 11; 53 private static final int TYPE_NOT_INTERCEPTED = 12; 54 private static final int TYPE_DISABLE_EFFECTS = 13; 55 private static final int TYPE_SUPPRESSOR_CHANGED = 14; 56 private static final int TYPE_LISTENER_HINTS_CHANGED = 15; 57 private static final int TYPE_SET_NOTIFICATION_POLICY = 16; 58 private static final int TYPE_SET_CONSOLIDATED_ZEN_POLICY = 17; 59 private static final int TYPE_MATCHES_CALL_FILTER = 18; 60 private static final int TYPE_RECORD_CALLER = 19; 61 private static final int TYPE_CHECK_REPEAT_CALLER = 20; 62 private static final int TYPE_ALERT_ON_UPDATED_INTERCEPT = 21; 63 private static final int TYPE_APPLY_DEVICE_EFFECT = 22; 64 private static final int TYPE_SCHEDULE_APPLY_DEVICE_EFFECT = 23; 65 traceIntercepted(NotificationRecord record, String reason)66 public static void traceIntercepted(NotificationRecord record, String reason) { 67 append(TYPE_INTERCEPTED, record.getKey() + "," + reason); 68 } 69 traceNotIntercepted(NotificationRecord record, String reason)70 public static void traceNotIntercepted(NotificationRecord record, String reason) { 71 append(TYPE_NOT_INTERCEPTED, record.getKey() + "," + reason); 72 } 73 traceAlertOnUpdatedIntercept(NotificationRecord record)74 public static void traceAlertOnUpdatedIntercept(NotificationRecord record) { 75 append(TYPE_ALERT_ON_UPDATED_INTERCEPT, record.getKey()); 76 } 77 traceSetRingerModeExternal(int ringerModeOld, int ringerModeNew, String caller, int ringerModeInternalIn, int ringerModeInternalOut)78 public static void traceSetRingerModeExternal(int ringerModeOld, int ringerModeNew, 79 String caller, int ringerModeInternalIn, int ringerModeInternalOut) { 80 append(TYPE_SET_RINGER_MODE_EXTERNAL, caller + ",e:" + 81 ringerModeToString(ringerModeOld) + "->" + 82 ringerModeToString(ringerModeNew) + ",i:" + 83 ringerModeToString(ringerModeInternalIn) + "->" + 84 ringerModeToString(ringerModeInternalOut)); 85 } 86 traceSetRingerModeInternal(int ringerModeOld, int ringerModeNew, String caller, int ringerModeExternalIn, int ringerModeExternalOut)87 public static void traceSetRingerModeInternal(int ringerModeOld, int ringerModeNew, 88 String caller, int ringerModeExternalIn, int ringerModeExternalOut) { 89 append(TYPE_SET_RINGER_MODE_INTERNAL, caller + ",i:" + 90 ringerModeToString(ringerModeOld) + "->" + 91 ringerModeToString(ringerModeNew) + ",e:" + 92 ringerModeToString(ringerModeExternalIn) + "->" + 93 ringerModeToString(ringerModeExternalOut)); 94 } 95 traceSetZenMode(int zenMode, String reason)96 public static void traceSetZenMode(int zenMode, String reason) { 97 append(TYPE_SET_ZEN_MODE, zenModeToString(zenMode) + "," + reason); 98 } 99 100 /** 101 * trace setting the consolidated zen policy 102 */ traceSetConsolidatedZenPolicy(NotificationManager.Policy policy, String reason)103 public static void traceSetConsolidatedZenPolicy(NotificationManager.Policy policy, 104 String reason) { 105 append(TYPE_SET_CONSOLIDATED_ZEN_POLICY, policy.toString() + "," + reason); 106 } 107 108 traceSetNotificationPolicy(String pkg, int targetSdk, NotificationManager.Policy policy)109 public static void traceSetNotificationPolicy(String pkg, int targetSdk, 110 NotificationManager.Policy policy) { 111 String policyLog = "pkg=" + pkg + " targetSdk=" + targetSdk 112 + " NotificationPolicy=" + policy.toString(); 113 append(TYPE_SET_NOTIFICATION_POLICY, policyLog); 114 } 115 traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e)116 public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) { 117 append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e)); 118 } 119 traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e)120 public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) { 121 append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e)); 122 } 123 traceConfig(@onfigOrigin int origin, String reason, @Nullable ComponentName triggeringComponent, ZenModeConfig oldConfig, ZenModeConfig newConfig, int callingUid)124 public static void traceConfig(@ConfigOrigin int origin, String reason, 125 @Nullable ComponentName triggeringComponent, ZenModeConfig oldConfig, 126 ZenModeConfig newConfig, int callingUid) { 127 ZenModeDiff.ConfigDiff diff = new ZenModeDiff.ConfigDiff(oldConfig, newConfig); 128 if (!diff.hasDiff()) { 129 append(TYPE_CONFIG, reason + " (" + originToString(origin) + ") no changes"); 130 } else { 131 append(TYPE_CONFIG, reason + " (" + originToString(origin) + ") from uid " + callingUid 132 + (triggeringComponent != null ? " - " + triggeringComponent : "") + ",\n" 133 + (newConfig != null ? newConfig.toString() : null) + ",\n" 134 + diff); 135 } 136 } 137 traceDisableEffects(NotificationRecord record, String reason)138 public static void traceDisableEffects(NotificationRecord record, String reason) { 139 append(TYPE_DISABLE_EFFECTS, record.getKey() + "," + reason); 140 } 141 traceEffectsSuppressorChanged(List<ComponentName> oldSuppressors, List<ComponentName> newSuppressors, long oldSuppressedEffects, long suppressedEffects)142 public static void traceEffectsSuppressorChanged(List<ComponentName> oldSuppressors, 143 List<ComponentName> newSuppressors, long oldSuppressedEffects, long suppressedEffects) { 144 append(TYPE_SUPPRESSOR_CHANGED, "suppressed effects:" 145 + oldSuppressedEffects + "->" + suppressedEffects + "," 146 + componentListToString(oldSuppressors) + "->" 147 + componentListToString(newSuppressors)); 148 } 149 traceListenerHintsChanged(int oldHints, int newHints, int listenerCount)150 public static void traceListenerHintsChanged(int oldHints, int newHints, int listenerCount) { 151 append(TYPE_LISTENER_HINTS_CHANGED, hintsToString(oldHints) + "->" 152 + hintsToString(newHints) + ",listeners=" + listenerCount); 153 } 154 155 /** 156 * Trace calls to matchesCallFilter with the result of the call and the reason for the result. 157 */ traceMatchesCallFilter(boolean result, String reason, int callingUid)158 public static void traceMatchesCallFilter(boolean result, String reason, int callingUid) { 159 append(TYPE_MATCHES_CALL_FILTER, "result=" + result + ", reason=" + reason 160 + ", calling uid=" + callingUid); 161 } 162 163 /** 164 * Trace what information is available about an incoming call when it's recorded 165 */ traceRecordCaller(boolean hasPhone, boolean hasUri)166 public static void traceRecordCaller(boolean hasPhone, boolean hasUri) { 167 append(TYPE_RECORD_CALLER, "has phone number=" + hasPhone + ", has uri=" + hasUri); 168 } 169 170 /** 171 * Trace what information was provided about a caller when checking whether it is from a repeat 172 * caller 173 */ traceCheckRepeatCaller(boolean found, boolean hasPhone, boolean hasUri)174 public static void traceCheckRepeatCaller(boolean found, boolean hasPhone, boolean hasUri) { 175 append(TYPE_CHECK_REPEAT_CALLER, "res=" + found + ", given phone number=" + hasPhone 176 + ", given uri=" + hasUri); 177 } 178 traceApplyDeviceEffect(String effect, boolean newValue)179 public static void traceApplyDeviceEffect(String effect, boolean newValue) { 180 append(TYPE_APPLY_DEVICE_EFFECT, effect + " -> " + newValue); 181 } 182 traceScheduleApplyDeviceEffect(String effect, boolean scheduledValue)183 public static void traceScheduleApplyDeviceEffect(String effect, boolean scheduledValue) { 184 append(TYPE_SCHEDULE_APPLY_DEVICE_EFFECT, effect + " -> " + scheduledValue); 185 } 186 subscribeResult(IConditionProvider provider, RemoteException e)187 private static String subscribeResult(IConditionProvider provider, RemoteException e) { 188 return provider == null ? "no provider" : e != null ? e.getMessage() : "ok"; 189 } 190 typeToString(int type)191 private static String typeToString(int type) { 192 switch (type) { 193 case TYPE_INTERCEPTED: return "intercepted"; 194 case TYPE_SET_RINGER_MODE_EXTERNAL: return "set_ringer_mode_external"; 195 case TYPE_SET_RINGER_MODE_INTERNAL: return "set_ringer_mode_internal"; 196 case TYPE_SET_ZEN_MODE: return "set_zen_mode"; 197 case TYPE_SUBSCRIBE: return "subscribe"; 198 case TYPE_UNSUBSCRIBE: return "unsubscribe"; 199 case TYPE_CONFIG: return "config"; 200 case TYPE_NOT_INTERCEPTED: return "not_intercepted"; 201 case TYPE_DISABLE_EFFECTS: return "disable_effects"; 202 case TYPE_SUPPRESSOR_CHANGED: return "suppressor_changed"; 203 case TYPE_LISTENER_HINTS_CHANGED: return "listener_hints_changed"; 204 case TYPE_SET_NOTIFICATION_POLICY: return "set_notification_policy"; 205 case TYPE_SET_CONSOLIDATED_ZEN_POLICY: return "set_consolidated_policy"; 206 case TYPE_MATCHES_CALL_FILTER: return "matches_call_filter"; 207 case TYPE_RECORD_CALLER: return "record_caller"; 208 case TYPE_CHECK_REPEAT_CALLER: return "check_repeat_caller"; 209 case TYPE_ALERT_ON_UPDATED_INTERCEPT: return "alert_on_updated_intercept"; 210 case TYPE_APPLY_DEVICE_EFFECT: return "apply_device_effect"; 211 case TYPE_SCHEDULE_APPLY_DEVICE_EFFECT: return "schedule_device_effect"; 212 default: return "unknown"; 213 } 214 } 215 ringerModeToString(int ringerMode)216 private static String ringerModeToString(int ringerMode) { 217 switch (ringerMode) { 218 case AudioManager.RINGER_MODE_SILENT: return "silent"; 219 case AudioManager.RINGER_MODE_VIBRATE: return "vibrate"; 220 case AudioManager.RINGER_MODE_NORMAL: return "normal"; 221 default: return "unknown"; 222 } 223 } 224 zenModeToString(int zenMode)225 private static String zenModeToString(int zenMode) { 226 switch (zenMode) { 227 case Global.ZEN_MODE_OFF: return "off"; 228 case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions"; 229 case Global.ZEN_MODE_ALARMS: return "alarms"; 230 case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions"; 231 default: return "unknown"; 232 } 233 } 234 hintsToString(int hints)235 private static String hintsToString(int hints) { 236 switch (hints) { 237 case 0 : return "none"; 238 case NotificationListenerService.HINT_HOST_DISABLE_EFFECTS: 239 return "disable_effects"; 240 case NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS: 241 return "disable_call_effects"; 242 case NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS: 243 return "disable_notification_effects"; 244 default: return Integer.toString(hints); 245 } 246 } 247 originToString(@onfigOrigin int origin)248 private static String originToString(@ConfigOrigin int origin) { 249 return switch (origin) { 250 case ZenModeConfig.ORIGIN_UNKNOWN -> "ORIGIN_UNKNOWN"; 251 case ZenModeConfig.ORIGIN_INIT -> "ORIGIN_INIT"; 252 case ZenModeConfig.ORIGIN_INIT_USER -> "ORIGIN_INIT_USER"; 253 case ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI -> "ORIGIN_USER_IN_SYSTEMUI"; 254 case ZenModeConfig.ORIGIN_APP -> "ORIGIN_APP"; 255 case ZenModeConfig.ORIGIN_SYSTEM -> "ORIGIN_SYSTEM"; 256 case ZenModeConfig.ORIGIN_RESTORE_BACKUP -> "ORIGIN_RESTORE_BACKUP"; 257 case ZenModeConfig.ORIGIN_USER_IN_APP -> "ORIGIN_USER_IN_APP"; 258 default -> origin + "??"; 259 }; 260 } 261 262 @Nullable componentToString(@ullable ComponentName component)263 private static String componentToString(@Nullable ComponentName component) { 264 return component != null ? component.toShortString() : null; 265 } 266 componentListToString(List<ComponentName> components)267 private static String componentListToString(List<ComponentName> components) { 268 StringBuilder stringBuilder = new StringBuilder(); 269 270 for (int i = 0; i < components.size(); ++i) { 271 if (i > 0) { 272 stringBuilder.append(", "); 273 } 274 stringBuilder.append(componentToString(components.get(i))); 275 } 276 277 return stringBuilder.toString(); 278 } 279 append(int type, String msg)280 private static void append(int type, String msg) { 281 if (type == TYPE_INTERCEPTED || type == TYPE_NOT_INTERCEPTED 282 || type == TYPE_CHECK_REPEAT_CALLER || type == TYPE_RECORD_CALLER 283 || type == TYPE_MATCHES_CALL_FILTER || type == TYPE_ALERT_ON_UPDATED_INTERCEPT) { 284 synchronized (INTERCEPTION_EVENTS) { 285 INTERCEPTION_EVENTS.log(typeToString(type) + ": " +msg); 286 } 287 } else { 288 synchronized (STATE_CHANGES) { 289 STATE_CHANGES.log(typeToString(type) + ": " +msg); 290 } 291 } 292 } 293 dump(PrintWriter pw, String prefix)294 public static void dump(PrintWriter pw, String prefix) { 295 synchronized (INTERCEPTION_EVENTS) { 296 pw.printf(prefix + "Interception Events:\n"); 297 INTERCEPTION_EVENTS.dump(prefix, pw); 298 } 299 synchronized (STATE_CHANGES) { 300 pw.printf(prefix + "State Changes:\n"); 301 STATE_CHANGES.dump(prefix, pw); 302 } 303 } 304 305 @VisibleForTesting(/* otherwise = VisibleForTesting.NONE */) clear()306 public static void clear() { 307 synchronized (INTERCEPTION_EVENTS) { 308 INTERCEPTION_EVENTS.clear(); 309 } 310 synchronized (STATE_CHANGES) { 311 STATE_CHANGES.clear(); 312 } 313 } 314 } 315