John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 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 | |
Beverly | 5e07322 | 2018-03-08 10:36:25 -0500 | [diff] [blame] | 19 | import android.app.NotificationManager; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.media.AudioManager; |
| 22 | import android.net.Uri; |
| 23 | import android.os.Build; |
| 24 | import android.os.RemoteException; |
| 25 | import android.provider.Settings.Global; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 26 | import android.service.notification.Condition; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 27 | import android.service.notification.IConditionProvider; |
John Spurlock | a708299 | 2015-03-09 12:19:23 -0400 | [diff] [blame] | 28 | import android.service.notification.NotificationListenerService; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 29 | import android.service.notification.ZenModeConfig; |
| 30 | import android.util.Slog; |
| 31 | |
| 32 | import java.io.PrintWriter; |
| 33 | import java.text.SimpleDateFormat; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 34 | import java.util.Date; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 35 | import java.util.List; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 36 | |
| 37 | public class ZenLog { |
| 38 | private static final String TAG = "ZenLog"; |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 39 | // the ZenLog is *very* verbose, so be careful about setting this to true |
| 40 | private static final boolean DEBUG = false; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 41 | |
| 42 | private static final int SIZE = Build.IS_DEBUGGABLE ? 100 : 20; |
| 43 | |
| 44 | private static final long[] TIMES = new long[SIZE]; |
| 45 | private static final int[] TYPES = new int[SIZE]; |
| 46 | private static final String[] MSGS = new String[SIZE]; |
| 47 | |
| 48 | private static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS"); |
| 49 | |
| 50 | private static final int TYPE_INTERCEPTED = 1; |
| 51 | private static final int TYPE_ALLOW_DISABLE = 2; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 52 | private static final int TYPE_SET_RINGER_MODE_EXTERNAL = 3; |
| 53 | private static final int TYPE_SET_RINGER_MODE_INTERNAL = 4; |
| 54 | private static final int TYPE_DOWNTIME = 5; |
| 55 | private static final int TYPE_SET_ZEN_MODE = 6; |
| 56 | private static final int TYPE_UPDATE_ZEN_MODE = 7; |
| 57 | private static final int TYPE_EXIT_CONDITION = 8; |
| 58 | private static final int TYPE_SUBSCRIBE = 9; |
| 59 | private static final int TYPE_UNSUBSCRIBE = 10; |
| 60 | private static final int TYPE_CONFIG = 11; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 61 | private static final int TYPE_NOT_INTERCEPTED = 12; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 62 | private static final int TYPE_DISABLE_EFFECTS = 13; |
John Spurlock | a708299 | 2015-03-09 12:19:23 -0400 | [diff] [blame] | 63 | private static final int TYPE_SUPPRESSOR_CHANGED = 14; |
| 64 | private static final int TYPE_LISTENER_HINTS_CHANGED = 15; |
Beverly | 5e07322 | 2018-03-08 10:36:25 -0500 | [diff] [blame] | 65 | private static final int TYPE_SET_NOTIFICATION_POLICY = 16; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 66 | private static final int TYPE_SET_CONSOLIDATED_ZEN_POLICY = 17; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 67 | |
| 68 | private static int sNext; |
| 69 | private static int sSize; |
| 70 | |
| 71 | public static void traceIntercepted(NotificationRecord record, String reason) { |
| 72 | if (record != null && record.isIntercepted()) return; // already logged |
| 73 | append(TYPE_INTERCEPTED, record.getKey() + "," + reason); |
| 74 | } |
| 75 | |
John Spurlock | 6ac5f8d | 2014-07-18 11:27:54 -0400 | [diff] [blame] | 76 | public static void traceNotIntercepted(NotificationRecord record, String reason) { |
| 77 | if (record != null && record.isUpdate) return; // already logged |
| 78 | append(TYPE_NOT_INTERCEPTED, record.getKey() + "," + reason); |
| 79 | } |
| 80 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 81 | public static void traceSetRingerModeExternal(int ringerModeOld, int ringerModeNew, |
| 82 | String caller, int ringerModeInternalIn, int ringerModeInternalOut) { |
| 83 | append(TYPE_SET_RINGER_MODE_EXTERNAL, caller + ",e:" + |
| 84 | ringerModeToString(ringerModeOld) + "->" + |
| 85 | ringerModeToString(ringerModeNew) + ",i:" + |
| 86 | ringerModeToString(ringerModeInternalIn) + "->" + |
| 87 | ringerModeToString(ringerModeInternalOut)); |
| 88 | } |
| 89 | |
| 90 | public static void traceSetRingerModeInternal(int ringerModeOld, int ringerModeNew, |
| 91 | String caller, int ringerModeExternalIn, int ringerModeExternalOut) { |
| 92 | append(TYPE_SET_RINGER_MODE_INTERNAL, caller + ",i:" + |
| 93 | ringerModeToString(ringerModeOld) + "->" + |
| 94 | ringerModeToString(ringerModeNew) + ",e:" + |
| 95 | ringerModeToString(ringerModeExternalIn) + "->" + |
| 96 | ringerModeToString(ringerModeExternalOut)); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 97 | } |
| 98 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 99 | public static void traceDowntimeAutotrigger(String result) { |
| 100 | append(TYPE_DOWNTIME, result); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 101 | } |
| 102 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 103 | public static void traceSetZenMode(int zenMode, String reason) { |
| 104 | append(TYPE_SET_ZEN_MODE, zenModeToString(zenMode) + "," + reason); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 107 | /** |
| 108 | * trace setting the consolidated zen policy |
| 109 | */ |
| 110 | public static void traceSetConsolidatedZenPolicy(NotificationManager.Policy policy, |
| 111 | String reason) { |
| 112 | append(TYPE_SET_CONSOLIDATED_ZEN_POLICY, policy.toString() + "," + reason); |
| 113 | } |
| 114 | |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 115 | public static void traceUpdateZenMode(int fromMode, int toMode) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 116 | append(TYPE_UPDATE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode)); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 117 | } |
| 118 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 119 | public static void traceExitCondition(Condition c, ComponentName component, String reason) { |
| 120 | append(TYPE_EXIT_CONDITION, c + "," + componentToString(component) + "," + reason); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Beverly | 5e07322 | 2018-03-08 10:36:25 -0500 | [diff] [blame] | 123 | public static void traceSetNotificationPolicy(String pkg, int targetSdk, |
| 124 | NotificationManager.Policy policy) { |
| 125 | append(TYPE_SET_NOTIFICATION_POLICY, "pkg=" + pkg + " targetSdk=" + targetSdk |
| 126 | + " NotificationPolicy=" + policy.toString()); |
| 127 | } |
| 128 | |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 129 | public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) { |
| 130 | append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e)); |
| 131 | } |
| 132 | |
| 133 | public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) { |
| 134 | append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e)); |
| 135 | } |
| 136 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 137 | public static void traceConfig(String reason, ZenModeConfig oldConfig, |
| 138 | ZenModeConfig newConfig) { |
| 139 | append(TYPE_CONFIG, reason |
| 140 | + "," + (newConfig != null ? newConfig.toString() : null) |
| 141 | + "," + ZenModeConfig.diff(oldConfig, newConfig)); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 142 | } |
| 143 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 144 | public static void traceDisableEffects(NotificationRecord record, String reason) { |
| 145 | append(TYPE_DISABLE_EFFECTS, record.getKey() + "," + reason); |
| 146 | } |
| 147 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 148 | public static void traceEffectsSuppressorChanged(List<ComponentName> oldSuppressors, |
| 149 | List<ComponentName> newSuppressors, long suppressedEffects) { |
| 150 | append(TYPE_SUPPRESSOR_CHANGED, "suppressed effects:" + suppressedEffects + "," |
| 151 | + componentListToString(oldSuppressors) + "->" |
| 152 | + componentListToString(newSuppressors)); |
John Spurlock | a708299 | 2015-03-09 12:19:23 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public static void traceListenerHintsChanged(int oldHints, int newHints, int listenerCount) { |
| 156 | append(TYPE_LISTENER_HINTS_CHANGED, hintsToString(oldHints) + "->" |
| 157 | + hintsToString(newHints) + ",listeners=" + listenerCount); |
| 158 | } |
| 159 | |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 160 | private static String subscribeResult(IConditionProvider provider, RemoteException e) { |
| 161 | return provider == null ? "no provider" : e != null ? e.getMessage() : "ok"; |
| 162 | } |
| 163 | |
| 164 | private static String typeToString(int type) { |
| 165 | switch (type) { |
| 166 | case TYPE_INTERCEPTED: return "intercepted"; |
| 167 | case TYPE_ALLOW_DISABLE: return "allow_disable"; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 168 | case TYPE_SET_RINGER_MODE_EXTERNAL: return "set_ringer_mode_external"; |
| 169 | case TYPE_SET_RINGER_MODE_INTERNAL: return "set_ringer_mode_internal"; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 170 | case TYPE_DOWNTIME: return "downtime"; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 171 | case TYPE_SET_ZEN_MODE: return "set_zen_mode"; |
| 172 | case TYPE_UPDATE_ZEN_MODE: return "update_zen_mode"; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 173 | case TYPE_EXIT_CONDITION: return "exit_condition"; |
| 174 | case TYPE_SUBSCRIBE: return "subscribe"; |
| 175 | case TYPE_UNSUBSCRIBE: return "unsubscribe"; |
| 176 | case TYPE_CONFIG: return "config"; |
John Spurlock | 6ac5f8d | 2014-07-18 11:27:54 -0400 | [diff] [blame] | 177 | case TYPE_NOT_INTERCEPTED: return "not_intercepted"; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 178 | case TYPE_DISABLE_EFFECTS: return "disable_effects"; |
John Spurlock | a708299 | 2015-03-09 12:19:23 -0400 | [diff] [blame] | 179 | case TYPE_SUPPRESSOR_CHANGED: return "suppressor_changed"; |
| 180 | case TYPE_LISTENER_HINTS_CHANGED: return "listener_hints_changed"; |
Beverly | 5e07322 | 2018-03-08 10:36:25 -0500 | [diff] [blame] | 181 | case TYPE_SET_NOTIFICATION_POLICY: return "set_notification_policy"; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 182 | default: return "unknown"; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private static String ringerModeToString(int ringerMode) { |
| 187 | switch (ringerMode) { |
| 188 | case AudioManager.RINGER_MODE_SILENT: return "silent"; |
| 189 | case AudioManager.RINGER_MODE_VIBRATE: return "vibrate"; |
| 190 | case AudioManager.RINGER_MODE_NORMAL: return "normal"; |
| 191 | default: return "unknown"; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | private static String zenModeToString(int zenMode) { |
| 196 | switch (zenMode) { |
| 197 | case Global.ZEN_MODE_OFF: return "off"; |
| 198 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions"; |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 199 | case Global.ZEN_MODE_ALARMS: return "alarms"; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 200 | case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions"; |
| 201 | default: return "unknown"; |
| 202 | } |
| 203 | } |
| 204 | |
John Spurlock | a708299 | 2015-03-09 12:19:23 -0400 | [diff] [blame] | 205 | private static String hintsToString(int hints) { |
| 206 | switch (hints) { |
| 207 | case 0 : return "none"; |
Zak Cohen | 70c173a | 2017-08-03 10:06:30 -0700 | [diff] [blame] | 208 | case NotificationListenerService.HINT_HOST_DISABLE_EFFECTS: |
| 209 | return "disable_effects"; |
| 210 | case NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS: |
| 211 | return "disable_call_effects"; |
| 212 | case NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS: |
| 213 | return "disable_notification_effects"; |
John Spurlock | a708299 | 2015-03-09 12:19:23 -0400 | [diff] [blame] | 214 | default: return Integer.toString(hints); |
| 215 | } |
| 216 | } |
| 217 | |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 218 | private static String componentToString(ComponentName component) { |
| 219 | return component != null ? component.toShortString() : null; |
| 220 | } |
| 221 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 222 | private static String componentListToString(List<ComponentName> components) { |
| 223 | StringBuilder stringBuilder = new StringBuilder(); |
| 224 | |
| 225 | for (int i = 0; i < components.size(); ++i) { |
| 226 | if (i > 0) { |
| 227 | stringBuilder.append(", "); |
| 228 | } |
| 229 | stringBuilder.append(componentToString(components.get(i))); |
| 230 | } |
| 231 | |
| 232 | return stringBuilder.toString(); |
| 233 | } |
| 234 | |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 235 | private static void append(int type, String msg) { |
| 236 | synchronized(MSGS) { |
| 237 | TIMES[sNext] = System.currentTimeMillis(); |
| 238 | TYPES[sNext] = type; |
| 239 | MSGS[sNext] = msg; |
| 240 | sNext = (sNext + 1) % SIZE; |
| 241 | if (sSize < SIZE) { |
| 242 | sSize++; |
| 243 | } |
| 244 | } |
Griff Hazen | 1aad144 | 2014-10-20 14:02:59 -0700 | [diff] [blame] | 245 | if (DEBUG) Slog.d(TAG, typeToString(type) + ": " + msg); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | public static void dump(PrintWriter pw, String prefix) { |
| 249 | synchronized(MSGS) { |
| 250 | final int start = (sNext - sSize + SIZE) % SIZE; |
| 251 | for (int i = 0; i < sSize; i++) { |
| 252 | final int j = (start + i) % SIZE; |
| 253 | pw.print(prefix); |
| 254 | pw.print(FORMAT.format(new Date(TIMES[j]))); |
| 255 | pw.print(' '); |
| 256 | pw.print(typeToString(TYPES[j])); |
| 257 | pw.print(": "); |
| 258 | pw.println(MSGS[j]); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |