• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             default: return "unknown";
183         }
184     }
185 
ringerModeToString(int ringerMode)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 
zenModeToString(int zenMode)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";
199             case Global.ZEN_MODE_ALARMS: return "alarms";
200             case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions";
201             default: return "unknown";
202         }
203     }
204 
hintsToString(int hints)205     private static String hintsToString(int hints) {
206         switch (hints) {
207             case 0 : return "none";
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";
214             default: return Integer.toString(hints);
215         }
216     }
217 
componentToString(ComponentName component)218     private static String componentToString(ComponentName component) {
219         return component != null ? component.toShortString() : null;
220     }
221 
componentListToString(List<ComponentName> components)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 
append(int type, String msg)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         }
245         if (DEBUG) Slog.d(TAG, typeToString(type) + ": " + msg);
246     }
247 
dump(PrintWriter pw, String prefix)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 }
263