blob: 207bdba47ff62061f1caeaff13837eaba32e7384 [file] [log] [blame]
John Spurlock6ae82a72014-07-16 16:23:01 -04001/**
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 android.content.ComponentName;
20import android.media.AudioManager;
21import android.net.Uri;
22import android.os.Build;
23import android.os.RemoteException;
24import android.provider.Settings.Global;
John Spurlock4db0d982014-08-13 09:19:03 -040025import android.service.notification.Condition;
John Spurlock6ae82a72014-07-16 16:23:01 -040026import android.service.notification.IConditionProvider;
John Spurlocka7082992015-03-09 12:19:23 -040027import android.service.notification.NotificationListenerService;
John Spurlock6ae82a72014-07-16 16:23:01 -040028import android.service.notification.ZenModeConfig;
29import android.util.Slog;
30
31import java.io.PrintWriter;
32import java.text.SimpleDateFormat;
John Spurlock6ae82a72014-07-16 16:23:01 -040033import java.util.Date;
Bryce Lee7219ada2016-04-08 10:54:23 -070034import java.util.List;
John Spurlock6ae82a72014-07-16 16:23:01 -040035
36public class ZenLog {
37 private static final String TAG = "ZenLog";
Griff Hazen1aad1442014-10-20 14:02:59 -070038 private static final boolean DEBUG = Build.IS_DEBUGGABLE;
John Spurlock6ae82a72014-07-16 16:23:01 -040039
40 private static final int SIZE = Build.IS_DEBUGGABLE ? 100 : 20;
41
42 private static final long[] TIMES = new long[SIZE];
43 private static final int[] TYPES = new int[SIZE];
44 private static final String[] MSGS = new String[SIZE];
45
46 private static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
47
48 private static final int TYPE_INTERCEPTED = 1;
49 private static final int TYPE_ALLOW_DISABLE = 2;
John Spurlock661f2cf2014-11-17 10:29:10 -050050 private static final int TYPE_SET_RINGER_MODE_EXTERNAL = 3;
51 private static final int TYPE_SET_RINGER_MODE_INTERNAL = 4;
52 private static final int TYPE_DOWNTIME = 5;
53 private static final int TYPE_SET_ZEN_MODE = 6;
54 private static final int TYPE_UPDATE_ZEN_MODE = 7;
55 private static final int TYPE_EXIT_CONDITION = 8;
56 private static final int TYPE_SUBSCRIBE = 9;
57 private static final int TYPE_UNSUBSCRIBE = 10;
58 private static final int TYPE_CONFIG = 11;
John Spurlock4db0d982014-08-13 09:19:03 -040059 private static final int TYPE_NOT_INTERCEPTED = 12;
John Spurlock32fe4c62014-10-02 12:16:02 -040060 private static final int TYPE_DISABLE_EFFECTS = 13;
John Spurlocka7082992015-03-09 12:19:23 -040061 private static final int TYPE_SUPPRESSOR_CHANGED = 14;
62 private static final int TYPE_LISTENER_HINTS_CHANGED = 15;
John Spurlock6ae82a72014-07-16 16:23:01 -040063
64 private static int sNext;
65 private static int sSize;
66
67 public static void traceIntercepted(NotificationRecord record, String reason) {
68 if (record != null && record.isIntercepted()) return; // already logged
69 append(TYPE_INTERCEPTED, record.getKey() + "," + reason);
70 }
71
John Spurlock6ac5f8d2014-07-18 11:27:54 -040072 public static void traceNotIntercepted(NotificationRecord record, String reason) {
73 if (record != null && record.isUpdate) return; // already logged
74 append(TYPE_NOT_INTERCEPTED, record.getKey() + "," + reason);
75 }
76
John Spurlock661f2cf2014-11-17 10:29:10 -050077 public static void traceSetRingerModeExternal(int ringerModeOld, int ringerModeNew,
78 String caller, int ringerModeInternalIn, int ringerModeInternalOut) {
79 append(TYPE_SET_RINGER_MODE_EXTERNAL, caller + ",e:" +
80 ringerModeToString(ringerModeOld) + "->" +
81 ringerModeToString(ringerModeNew) + ",i:" +
82 ringerModeToString(ringerModeInternalIn) + "->" +
83 ringerModeToString(ringerModeInternalOut));
84 }
85
86 public static void traceSetRingerModeInternal(int ringerModeOld, int ringerModeNew,
87 String caller, int ringerModeExternalIn, int ringerModeExternalOut) {
88 append(TYPE_SET_RINGER_MODE_INTERNAL, caller + ",i:" +
89 ringerModeToString(ringerModeOld) + "->" +
90 ringerModeToString(ringerModeNew) + ",e:" +
91 ringerModeToString(ringerModeExternalIn) + "->" +
92 ringerModeToString(ringerModeExternalOut));
John Spurlock6ae82a72014-07-16 16:23:01 -040093 }
94
John Spurlock530052a2014-11-30 16:26:19 -050095 public static void traceDowntimeAutotrigger(String result) {
96 append(TYPE_DOWNTIME, result);
John Spurlock4db0d982014-08-13 09:19:03 -040097 }
98
John Spurlock530052a2014-11-30 16:26:19 -050099 public static void traceSetZenMode(int zenMode, String reason) {
100 append(TYPE_SET_ZEN_MODE, zenModeToString(zenMode) + "," + reason);
John Spurlock6ae82a72014-07-16 16:23:01 -0400101 }
102
103 public static void traceUpdateZenMode(int fromMode, int toMode) {
John Spurlock4db0d982014-08-13 09:19:03 -0400104 append(TYPE_UPDATE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode));
John Spurlock6ae82a72014-07-16 16:23:01 -0400105 }
106
John Spurlock4db0d982014-08-13 09:19:03 -0400107 public static void traceExitCondition(Condition c, ComponentName component, String reason) {
108 append(TYPE_EXIT_CONDITION, c + "," + componentToString(component) + "," + reason);
John Spurlock6ae82a72014-07-16 16:23:01 -0400109 }
110
111 public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
112 append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e));
113 }
114
115 public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
116 append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e));
117 }
118
John Spurlock21258a32015-05-27 18:22:55 -0400119 public static void traceConfig(String reason, ZenModeConfig oldConfig,
120 ZenModeConfig newConfig) {
121 append(TYPE_CONFIG, reason
122 + "," + (newConfig != null ? newConfig.toString() : null)
123 + "," + ZenModeConfig.diff(oldConfig, newConfig));
John Spurlock6ae82a72014-07-16 16:23:01 -0400124 }
125
John Spurlock32fe4c62014-10-02 12:16:02 -0400126 public static void traceDisableEffects(NotificationRecord record, String reason) {
127 append(TYPE_DISABLE_EFFECTS, record.getKey() + "," + reason);
128 }
129
Bryce Lee7219ada2016-04-08 10:54:23 -0700130 public static void traceEffectsSuppressorChanged(List<ComponentName> oldSuppressors,
131 List<ComponentName> newSuppressors, long suppressedEffects) {
132 append(TYPE_SUPPRESSOR_CHANGED, "suppressed effects:" + suppressedEffects + ","
133 + componentListToString(oldSuppressors) + "->"
134 + componentListToString(newSuppressors));
John Spurlocka7082992015-03-09 12:19:23 -0400135 }
136
137 public static void traceListenerHintsChanged(int oldHints, int newHints, int listenerCount) {
138 append(TYPE_LISTENER_HINTS_CHANGED, hintsToString(oldHints) + "->"
139 + hintsToString(newHints) + ",listeners=" + listenerCount);
140 }
141
John Spurlock6ae82a72014-07-16 16:23:01 -0400142 private static String subscribeResult(IConditionProvider provider, RemoteException e) {
143 return provider == null ? "no provider" : e != null ? e.getMessage() : "ok";
144 }
145
146 private static String typeToString(int type) {
147 switch (type) {
148 case TYPE_INTERCEPTED: return "intercepted";
149 case TYPE_ALLOW_DISABLE: return "allow_disable";
John Spurlock661f2cf2014-11-17 10:29:10 -0500150 case TYPE_SET_RINGER_MODE_EXTERNAL: return "set_ringer_mode_external";
151 case TYPE_SET_RINGER_MODE_INTERNAL: return "set_ringer_mode_internal";
John Spurlock6ae82a72014-07-16 16:23:01 -0400152 case TYPE_DOWNTIME: return "downtime";
John Spurlock4db0d982014-08-13 09:19:03 -0400153 case TYPE_SET_ZEN_MODE: return "set_zen_mode";
154 case TYPE_UPDATE_ZEN_MODE: return "update_zen_mode";
John Spurlock6ae82a72014-07-16 16:23:01 -0400155 case TYPE_EXIT_CONDITION: return "exit_condition";
156 case TYPE_SUBSCRIBE: return "subscribe";
157 case TYPE_UNSUBSCRIBE: return "unsubscribe";
158 case TYPE_CONFIG: return "config";
John Spurlock6ac5f8d2014-07-18 11:27:54 -0400159 case TYPE_NOT_INTERCEPTED: return "not_intercepted";
John Spurlock32fe4c62014-10-02 12:16:02 -0400160 case TYPE_DISABLE_EFFECTS: return "disable_effects";
John Spurlocka7082992015-03-09 12:19:23 -0400161 case TYPE_SUPPRESSOR_CHANGED: return "suppressor_changed";
162 case TYPE_LISTENER_HINTS_CHANGED: return "listener_hints_changed";
John Spurlock6ae82a72014-07-16 16:23:01 -0400163 default: return "unknown";
164 }
165 }
166
167 private static String ringerModeToString(int ringerMode) {
168 switch (ringerMode) {
169 case AudioManager.RINGER_MODE_SILENT: return "silent";
170 case AudioManager.RINGER_MODE_VIBRATE: return "vibrate";
171 case AudioManager.RINGER_MODE_NORMAL: return "normal";
172 default: return "unknown";
173 }
174 }
175
176 private static String zenModeToString(int zenMode) {
177 switch (zenMode) {
178 case Global.ZEN_MODE_OFF: return "off";
179 case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions";
John Spurlock4f1163c2015-04-02 17:41:21 -0400180 case Global.ZEN_MODE_ALARMS: return "alarms";
John Spurlock6ae82a72014-07-16 16:23:01 -0400181 case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions";
182 default: return "unknown";
183 }
184 }
185
John Spurlocka7082992015-03-09 12:19:23 -0400186 private static String hintsToString(int hints) {
187 switch (hints) {
188 case 0 : return "none";
189 case NotificationListenerService.HINT_HOST_DISABLE_EFFECTS : return "disable_effects";
190 default: return Integer.toString(hints);
191 }
192 }
193
John Spurlock6ae82a72014-07-16 16:23:01 -0400194 private static String componentToString(ComponentName component) {
195 return component != null ? component.toShortString() : null;
196 }
197
Bryce Lee7219ada2016-04-08 10:54:23 -0700198 private static String componentListToString(List<ComponentName> components) {
199 StringBuilder stringBuilder = new StringBuilder();
200
201 for (int i = 0; i < components.size(); ++i) {
202 if (i > 0) {
203 stringBuilder.append(", ");
204 }
205 stringBuilder.append(componentToString(components.get(i)));
206 }
207
208 return stringBuilder.toString();
209 }
210
John Spurlock6ae82a72014-07-16 16:23:01 -0400211 private static void append(int type, String msg) {
212 synchronized(MSGS) {
213 TIMES[sNext] = System.currentTimeMillis();
214 TYPES[sNext] = type;
215 MSGS[sNext] = msg;
216 sNext = (sNext + 1) % SIZE;
217 if (sSize < SIZE) {
218 sSize++;
219 }
220 }
Griff Hazen1aad1442014-10-20 14:02:59 -0700221 if (DEBUG) Slog.d(TAG, typeToString(type) + ": " + msg);
John Spurlock6ae82a72014-07-16 16:23:01 -0400222 }
223
224 public static void dump(PrintWriter pw, String prefix) {
225 synchronized(MSGS) {
226 final int start = (sNext - sSize + SIZE) % SIZE;
227 for (int i = 0; i < sSize; i++) {
228 final int j = (start + i) % SIZE;
229 pw.print(prefix);
230 pw.print(FORMAT.format(new Date(TIMES[j])));
231 pw.print(' ');
232 pw.print(typeToString(TYPES[j]));
233 pw.print(": ");
234 pw.println(MSGS[j]);
235 }
236 }
237 }
238}