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.app.NotificationManager;
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;
26 import android.service.notification.Condition;
27 import android.service.notification.IConditionProvider;
28 import android.service.notification.NotificationListenerService;
29 import android.service.notification.ZenModeConfig;
30 import android.util.Slog;
31 
32 import java.io.PrintWriter;
33 import java.text.SimpleDateFormat;
34 import java.util.Date;
35 import java.util.List;
36 
37 public class ZenLog {
38     private static final String TAG = "ZenLog";
39     private static final boolean DEBUG = Build.IS_DEBUGGABLE;
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;
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;
60     private static final int TYPE_NOT_INTERCEPTED = 12;
61     private static final int TYPE_DISABLE_EFFECTS = 13;
62     private static final int TYPE_SUPPRESSOR_CHANGED = 14;
63     private static final int TYPE_LISTENER_HINTS_CHANGED = 15;
64     private static final int TYPE_SET_NOTIFICATION_POLICY = 16;
65 
66     private static int sNext;
67     private static int sSize;
68 
traceIntercepted(NotificationRecord record, String reason)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 
traceNotIntercepted(NotificationRecord record, String reason)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 
traceSetRingerModeExternal(int ringerModeOld, int ringerModeNew, String caller, int ringerModeInternalIn, int ringerModeInternalOut)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 
traceSetRingerModeInternal(int ringerModeOld, int ringerModeNew, String caller, int ringerModeExternalIn, int ringerModeExternalOut)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));
95     }
96 
traceDowntimeAutotrigger(String result)97     public static void traceDowntimeAutotrigger(String result) {
98         append(TYPE_DOWNTIME, result);
99     }
100 
traceSetZenMode(int zenMode, String reason)101     public static void traceSetZenMode(int zenMode, String reason) {
102         append(TYPE_SET_ZEN_MODE, zenModeToString(zenMode) + "," + reason);
103     }
104 
traceUpdateZenMode(int fromMode, int toMode)105     public static void traceUpdateZenMode(int fromMode, int toMode) {
106         append(TYPE_UPDATE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode));
107     }
108 
traceExitCondition(Condition c, ComponentName component, String reason)109     public static void traceExitCondition(Condition c, ComponentName component, String reason) {
110         append(TYPE_EXIT_CONDITION, c + "," + componentToString(component) + "," + reason);
111     }
112 
traceSetNotificationPolicy(String pkg, int targetSdk, NotificationManager.Policy policy)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 
traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e)119     public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
120         append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e));
121     }
122 
traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e)123     public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
124         append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e));
125     }
126 
traceConfig(String reason, ZenModeConfig oldConfig, ZenModeConfig newConfig)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));
132     }
133 
traceDisableEffects(NotificationRecord record, String reason)134     public static void traceDisableEffects(NotificationRecord record, String reason) {
135         append(TYPE_DISABLE_EFFECTS, record.getKey() + "," + reason);
136     }
137 
traceEffectsSuppressorChanged(List<ComponentName> oldSuppressors, List<ComponentName> newSuppressors, long suppressedEffects)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));
143     }
144 
traceListenerHintsChanged(int oldHints, int newHints, int listenerCount)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 
subscribeResult(IConditionProvider provider, RemoteException e)150     private static String subscribeResult(IConditionProvider provider, RemoteException e) {
151         return provider == null ? "no provider" : e != null ? e.getMessage() : "ok";
152     }
153 
typeToString(int type)154     private static String typeToString(int type) {
155         switch (type) {
156             case TYPE_INTERCEPTED: return "intercepted";
157             case TYPE_ALLOW_DISABLE: return "allow_disable";
158             case TYPE_SET_RINGER_MODE_EXTERNAL: return "set_ringer_mode_external";
159             case TYPE_SET_RINGER_MODE_INTERNAL: return "set_ringer_mode_internal";
160             case TYPE_DOWNTIME: return "downtime";
161             case TYPE_SET_ZEN_MODE: return "set_zen_mode";
162             case TYPE_UPDATE_ZEN_MODE: return "update_zen_mode";
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";
167             case TYPE_NOT_INTERCEPTED: return "not_intercepted";
168             case TYPE_DISABLE_EFFECTS: return "disable_effects";
169             case TYPE_SUPPRESSOR_CHANGED: return "suppressor_changed";
170             case TYPE_LISTENER_HINTS_CHANGED: return "listener_hints_changed";
171             case TYPE_SET_NOTIFICATION_POLICY: return "set_notification_policy";
172             default: return "unknown";
173         }
174     }
175 
ringerModeToString(int ringerMode)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 
zenModeToString(int zenMode)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";
189             case Global.ZEN_MODE_ALARMS: return "alarms";
190             case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions";
191             default: return "unknown";
192         }
193     }
194 
hintsToString(int hints)195     private static String hintsToString(int hints) {
196         switch (hints) {
197             case 0 : return "none";
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";
204             default: return Integer.toString(hints);
205         }
206     }
207 
componentToString(ComponentName component)208     private static String componentToString(ComponentName component) {
209         return component != null ? component.toShortString() : null;
210     }
211 
componentListToString(List<ComponentName> components)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 
append(int type, String msg)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         }
235         if (DEBUG) Slog.d(TAG, typeToString(type) + ": " + msg);
236     }
237 
dump(PrintWriter pw, String prefix)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 }
253