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