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     // the ZenLog is *very* verbose, so be careful about setting this to true
40     private static final boolean DEBUG = false;
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;
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;
61     private static final int TYPE_NOT_INTERCEPTED = 12;
62     private static final int TYPE_DISABLE_EFFECTS = 13;
63     private static final int TYPE_SUPPRESSOR_CHANGED = 14;
64     private static final int TYPE_LISTENER_HINTS_CHANGED = 15;
65     private static final int TYPE_SET_NOTIFICATION_POLICY = 16;
66     private static final int TYPE_SET_CONSOLIDATED_ZEN_POLICY = 17;
67 
68     private static int sNext;
69     private static int sSize;
70 
traceIntercepted(NotificationRecord record, String reason)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 
traceNotIntercepted(NotificationRecord record, String reason)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 
traceSetRingerModeExternal(int ringerModeOld, int ringerModeNew, String caller, int ringerModeInternalIn, int ringerModeInternalOut)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 
traceSetRingerModeInternal(int ringerModeOld, int ringerModeNew, String caller, int ringerModeExternalIn, int ringerModeExternalOut)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));
97     }
98 
traceDowntimeAutotrigger(String result)99     public static void traceDowntimeAutotrigger(String result) {
100         append(TYPE_DOWNTIME, result);
101     }
102 
traceSetZenMode(int zenMode, String reason)103     public static void traceSetZenMode(int zenMode, String reason) {
104         append(TYPE_SET_ZEN_MODE, zenModeToString(zenMode) + "," + reason);
105     }
106 
107     /**
108      * trace setting the consolidated zen policy
109      */
traceSetConsolidatedZenPolicy(NotificationManager.Policy policy, String reason)110     public static void traceSetConsolidatedZenPolicy(NotificationManager.Policy policy,
111             String reason) {
112         append(TYPE_SET_CONSOLIDATED_ZEN_POLICY, policy.toString() + "," + reason);
113     }
114 
traceUpdateZenMode(int fromMode, int toMode)115     public static void traceUpdateZenMode(int fromMode, int toMode) {
116         append(TYPE_UPDATE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode));
117     }
118 
traceExitCondition(Condition c, ComponentName component, String reason)119     public static void traceExitCondition(Condition c, ComponentName component, String reason) {
120         append(TYPE_EXIT_CONDITION, c + "," + componentToString(component) + "," + reason);
121     }
122 
traceSetNotificationPolicy(String pkg, int targetSdk, NotificationManager.Policy policy)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 
traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e)129     public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
130         append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e));
131     }
132 
traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e)133     public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
134         append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e));
135     }
136 
traceConfig(String reason, ZenModeConfig oldConfig, ZenModeConfig newConfig)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));
142     }
143 
traceDisableEffects(NotificationRecord record, String reason)144     public static void traceDisableEffects(NotificationRecord record, String reason) {
145         append(TYPE_DISABLE_EFFECTS, record.getKey() + "," + reason);
146     }
147 
traceEffectsSuppressorChanged(List<ComponentName> oldSuppressors, List<ComponentName> newSuppressors, long suppressedEffects)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));
153     }
154 
traceListenerHintsChanged(int oldHints, int newHints, int listenerCount)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 
subscribeResult(IConditionProvider provider, RemoteException e)160     private static String subscribeResult(IConditionProvider provider, RemoteException e) {
161         return provider == null ? "no provider" : e != null ? e.getMessage() : "ok";
162     }
163 
typeToString(int type)164     private static String typeToString(int type) {
165         switch (type) {
166             case TYPE_INTERCEPTED: return "intercepted";
167             case TYPE_ALLOW_DISABLE: return "allow_disable";
168             case TYPE_SET_RINGER_MODE_EXTERNAL: return "set_ringer_mode_external";
169             case TYPE_SET_RINGER_MODE_INTERNAL: return "set_ringer_mode_internal";
170             case TYPE_DOWNTIME: return "downtime";
171             case TYPE_SET_ZEN_MODE: return "set_zen_mode";
172             case TYPE_UPDATE_ZEN_MODE: return "update_zen_mode";
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";
177             case TYPE_NOT_INTERCEPTED: return "not_intercepted";
178             case TYPE_DISABLE_EFFECTS: return "disable_effects";
179             case TYPE_SUPPRESSOR_CHANGED: return "suppressor_changed";
180             case TYPE_LISTENER_HINTS_CHANGED: return "listener_hints_changed";
181             case TYPE_SET_NOTIFICATION_POLICY: return "set_notification_policy";
182             case TYPE_SET_CONSOLIDATED_ZEN_POLICY: return "set_consolidated_policy";
183             default: return "unknown";
184         }
185     }
186 
ringerModeToString(int ringerMode)187     private static String ringerModeToString(int ringerMode) {
188         switch (ringerMode) {
189             case AudioManager.RINGER_MODE_SILENT: return "silent";
190             case AudioManager.RINGER_MODE_VIBRATE: return "vibrate";
191             case AudioManager.RINGER_MODE_NORMAL: return "normal";
192             default: return "unknown";
193         }
194     }
195 
zenModeToString(int zenMode)196     private static String zenModeToString(int zenMode) {
197         switch (zenMode) {
198             case Global.ZEN_MODE_OFF: return "off";
199             case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions";
200             case Global.ZEN_MODE_ALARMS: return "alarms";
201             case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions";
202             default: return "unknown";
203         }
204     }
205 
hintsToString(int hints)206     private static String hintsToString(int hints) {
207         switch (hints) {
208             case 0 : return "none";
209             case NotificationListenerService.HINT_HOST_DISABLE_EFFECTS:
210                     return "disable_effects";
211             case NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS:
212                     return "disable_call_effects";
213             case NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS:
214                     return "disable_notification_effects";
215             default: return Integer.toString(hints);
216         }
217     }
218 
componentToString(ComponentName component)219     private static String componentToString(ComponentName component) {
220         return component != null ? component.toShortString() : null;
221     }
222 
componentListToString(List<ComponentName> components)223     private static String componentListToString(List<ComponentName> components) {
224         StringBuilder stringBuilder = new StringBuilder();
225 
226         for (int i = 0; i < components.size(); ++i) {
227             if (i > 0) {
228                 stringBuilder.append(", ");
229             }
230             stringBuilder.append(componentToString(components.get(i)));
231         }
232 
233         return stringBuilder.toString();
234     }
235 
append(int type, String msg)236     private static void append(int type, String msg) {
237         synchronized(MSGS) {
238             TIMES[sNext] = System.currentTimeMillis();
239             TYPES[sNext] = type;
240             MSGS[sNext] = msg;
241             sNext = (sNext + 1) % SIZE;
242             if (sSize < SIZE) {
243                 sSize++;
244             }
245         }
246         if (DEBUG) Slog.d(TAG, typeToString(type) + ": " + msg);
247     }
248 
dump(PrintWriter pw, String prefix)249     public static void dump(PrintWriter pw, String prefix) {
250         synchronized(MSGS) {
251             final int start = (sNext - sSize + SIZE) % SIZE;
252             for (int i = 0; i < sSize; i++) {
253                 final int j = (start + i) % SIZE;
254                 pw.print(prefix);
255                 pw.print(FORMAT.format(new Date(TIMES[j])));
256                 pw.print(' ');
257                 pw.print(typeToString(TYPES[j]));
258                 pw.print(": ");
259                 pw.println(MSGS[j]);
260             }
261         }
262     }
263 }
264