• 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.Log;
31 import android.util.Slog;
32 
33 import java.io.PrintWriter;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.List;
37 
38 public class ZenLog {
39     private static final String TAG = "ZenLog";
40     // the ZenLog is *very* verbose, so be careful about setting this to true
41     private static final boolean DEBUG = false;
42 
43     private static final int SIZE = Build.IS_DEBUGGABLE ? 200 : 100;
44 
45     private static final long[] TIMES = new long[SIZE];
46     private static final int[] TYPES = new int[SIZE];
47     private static final String[] MSGS = new String[SIZE];
48 
49     private static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
50 
51     private static final int TYPE_INTERCEPTED = 1;
52     private static final int TYPE_ALLOW_DISABLE = 2;
53     private static final int TYPE_SET_RINGER_MODE_EXTERNAL = 3;
54     private static final int TYPE_SET_RINGER_MODE_INTERNAL = 4;
55     private static final int TYPE_DOWNTIME = 5;
56     private static final int TYPE_SET_ZEN_MODE = 6;
57     private static final int TYPE_UPDATE_ZEN_MODE = 7;
58     private static final int TYPE_EXIT_CONDITION = 8;
59     private static final int TYPE_SUBSCRIBE = 9;
60     private static final int TYPE_UNSUBSCRIBE = 10;
61     private static final int TYPE_CONFIG = 11;
62     private static final int TYPE_NOT_INTERCEPTED = 12;
63     private static final int TYPE_DISABLE_EFFECTS = 13;
64     private static final int TYPE_SUPPRESSOR_CHANGED = 14;
65     private static final int TYPE_LISTENER_HINTS_CHANGED = 15;
66     private static final int TYPE_SET_NOTIFICATION_POLICY = 16;
67     private static final int TYPE_SET_CONSOLIDATED_ZEN_POLICY = 17;
68     private static final int TYPE_MATCHES_CALL_FILTER = 18;
69     private static final int TYPE_RECORD_CALLER = 19;
70     private static final int TYPE_CHECK_REPEAT_CALLER = 20;
71     private static final int TYPE_ALERT_ON_UPDATED_INTERCEPT = 21;
72 
73     private static int sNext;
74     private static int sSize;
75 
traceIntercepted(NotificationRecord record, String reason)76     public static void traceIntercepted(NotificationRecord record, String reason) {
77         append(TYPE_INTERCEPTED, record.getKey() + "," + reason);
78     }
79 
traceNotIntercepted(NotificationRecord record, String reason)80     public static void traceNotIntercepted(NotificationRecord record, String reason) {
81         append(TYPE_NOT_INTERCEPTED, record.getKey() + "," + reason);
82     }
83 
traceAlertOnUpdatedIntercept(NotificationRecord record)84     public static void traceAlertOnUpdatedIntercept(NotificationRecord record) {
85         append(TYPE_ALERT_ON_UPDATED_INTERCEPT, record.getKey());
86     }
87 
traceSetRingerModeExternal(int ringerModeOld, int ringerModeNew, String caller, int ringerModeInternalIn, int ringerModeInternalOut)88     public static void traceSetRingerModeExternal(int ringerModeOld, int ringerModeNew,
89             String caller, int ringerModeInternalIn, int ringerModeInternalOut) {
90         append(TYPE_SET_RINGER_MODE_EXTERNAL, caller + ",e:" +
91                 ringerModeToString(ringerModeOld) + "->" +
92                 ringerModeToString(ringerModeNew)  + ",i:" +
93                 ringerModeToString(ringerModeInternalIn) + "->" +
94                 ringerModeToString(ringerModeInternalOut));
95     }
96 
traceSetRingerModeInternal(int ringerModeOld, int ringerModeNew, String caller, int ringerModeExternalIn, int ringerModeExternalOut)97     public static void traceSetRingerModeInternal(int ringerModeOld, int ringerModeNew,
98             String caller, int ringerModeExternalIn, int ringerModeExternalOut) {
99         append(TYPE_SET_RINGER_MODE_INTERNAL, caller + ",i:" +
100                 ringerModeToString(ringerModeOld) + "->" +
101                 ringerModeToString(ringerModeNew)  + ",e:" +
102                 ringerModeToString(ringerModeExternalIn) + "->" +
103                 ringerModeToString(ringerModeExternalOut));
104     }
105 
traceDowntimeAutotrigger(String result)106     public static void traceDowntimeAutotrigger(String result) {
107         append(TYPE_DOWNTIME, result);
108     }
109 
traceSetZenMode(int zenMode, String reason)110     public static void traceSetZenMode(int zenMode, String reason) {
111         append(TYPE_SET_ZEN_MODE, zenModeToString(zenMode) + "," + reason);
112     }
113 
114     /**
115      * trace setting the consolidated zen policy
116      */
traceSetConsolidatedZenPolicy(NotificationManager.Policy policy, String reason)117     public static void traceSetConsolidatedZenPolicy(NotificationManager.Policy policy,
118             String reason) {
119         append(TYPE_SET_CONSOLIDATED_ZEN_POLICY, policy.toString() + "," + reason);
120     }
121 
traceUpdateZenMode(int fromMode, int toMode)122     public static void traceUpdateZenMode(int fromMode, int toMode) {
123         append(TYPE_UPDATE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode));
124     }
125 
traceExitCondition(Condition c, ComponentName component, String reason)126     public static void traceExitCondition(Condition c, ComponentName component, String reason) {
127         append(TYPE_EXIT_CONDITION, c + "," + componentToString(component) + "," + reason);
128     }
129 
traceSetNotificationPolicy(String pkg, int targetSdk, NotificationManager.Policy policy)130     public static void traceSetNotificationPolicy(String pkg, int targetSdk,
131             NotificationManager.Policy policy) {
132         String policyLog = "pkg=" + pkg + " targetSdk=" + targetSdk
133                 + " NotificationPolicy=" + policy.toString();
134         append(TYPE_SET_NOTIFICATION_POLICY, policyLog);
135         // TODO(b/180205791): remove when we can better surface apps that are changing policy
136         Log.d(TAG, "Zen Policy Changed: " + policyLog);
137     }
138 
traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e)139     public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
140         append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e));
141     }
142 
traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e)143     public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
144         append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e));
145     }
146 
traceConfig(String reason, ZenModeConfig oldConfig, ZenModeConfig newConfig)147     public static void traceConfig(String reason, ZenModeConfig oldConfig,
148             ZenModeConfig newConfig) {
149         ZenModeConfig.Diff diff = ZenModeConfig.diff(oldConfig, newConfig);
150         if (diff.isEmpty()) {
151             append(TYPE_CONFIG, reason + " no changes");
152         } else {
153             append(TYPE_CONFIG, reason
154                     + ",\n" + (newConfig != null ? newConfig.toString() : null)
155                     + ",\n" + ZenModeConfig.diff(oldConfig, newConfig));
156         }
157     }
158 
traceDisableEffects(NotificationRecord record, String reason)159     public static void traceDisableEffects(NotificationRecord record, String reason) {
160         append(TYPE_DISABLE_EFFECTS, record.getKey() + "," + reason);
161     }
162 
traceEffectsSuppressorChanged(List<ComponentName> oldSuppressors, List<ComponentName> newSuppressors, long suppressedEffects)163     public static void traceEffectsSuppressorChanged(List<ComponentName> oldSuppressors,
164             List<ComponentName> newSuppressors, long suppressedEffects) {
165         append(TYPE_SUPPRESSOR_CHANGED, "suppressed effects:" + suppressedEffects + ","
166                 + componentListToString(oldSuppressors) + "->"
167                 + componentListToString(newSuppressors));
168     }
169 
traceListenerHintsChanged(int oldHints, int newHints, int listenerCount)170     public static void traceListenerHintsChanged(int oldHints, int newHints, int listenerCount) {
171         append(TYPE_LISTENER_HINTS_CHANGED, hintsToString(oldHints) + "->"
172             + hintsToString(newHints) + ",listeners=" + listenerCount);
173     }
174 
175     /**
176      * Trace calls to matchesCallFilter with the result of the call and the reason for the result.
177      */
traceMatchesCallFilter(boolean result, String reason, int callingUid)178     public static void traceMatchesCallFilter(boolean result, String reason, int callingUid) {
179         append(TYPE_MATCHES_CALL_FILTER, "result=" + result + ", reason=" + reason
180                 + ", calling uid=" + callingUid);
181     }
182 
183     /**
184      * Trace what information is available about an incoming call when it's recorded
185      */
traceRecordCaller(boolean hasPhone, boolean hasUri)186     public static void traceRecordCaller(boolean hasPhone, boolean hasUri) {
187         append(TYPE_RECORD_CALLER, "has phone number=" + hasPhone + ", has uri=" + hasUri);
188     }
189 
190     /**
191      * Trace what information was provided about a caller when checking whether it is from a repeat
192      * caller
193      */
traceCheckRepeatCaller(boolean found, boolean hasPhone, boolean hasUri)194     public static void traceCheckRepeatCaller(boolean found, boolean hasPhone, boolean hasUri) {
195         append(TYPE_CHECK_REPEAT_CALLER, "res=" + found + ", given phone number=" + hasPhone
196                 + ", given uri=" + hasUri);
197     }
198 
subscribeResult(IConditionProvider provider, RemoteException e)199     private static String subscribeResult(IConditionProvider provider, RemoteException e) {
200         return provider == null ? "no provider" : e != null ? e.getMessage() : "ok";
201     }
202 
typeToString(int type)203     private static String typeToString(int type) {
204         switch (type) {
205             case TYPE_INTERCEPTED: return "intercepted";
206             case TYPE_ALLOW_DISABLE: return "allow_disable";
207             case TYPE_SET_RINGER_MODE_EXTERNAL: return "set_ringer_mode_external";
208             case TYPE_SET_RINGER_MODE_INTERNAL: return "set_ringer_mode_internal";
209             case TYPE_DOWNTIME: return "downtime";
210             case TYPE_SET_ZEN_MODE: return "set_zen_mode";
211             case TYPE_UPDATE_ZEN_MODE: return "update_zen_mode";
212             case TYPE_EXIT_CONDITION: return "exit_condition";
213             case TYPE_SUBSCRIBE: return "subscribe";
214             case TYPE_UNSUBSCRIBE: return "unsubscribe";
215             case TYPE_CONFIG: return "config";
216             case TYPE_NOT_INTERCEPTED: return "not_intercepted";
217             case TYPE_DISABLE_EFFECTS: return "disable_effects";
218             case TYPE_SUPPRESSOR_CHANGED: return "suppressor_changed";
219             case TYPE_LISTENER_HINTS_CHANGED: return "listener_hints_changed";
220             case TYPE_SET_NOTIFICATION_POLICY: return "set_notification_policy";
221             case TYPE_SET_CONSOLIDATED_ZEN_POLICY: return "set_consolidated_policy";
222             case TYPE_MATCHES_CALL_FILTER: return "matches_call_filter";
223             case TYPE_RECORD_CALLER: return "record_caller";
224             case TYPE_CHECK_REPEAT_CALLER: return "check_repeat_caller";
225             case TYPE_ALERT_ON_UPDATED_INTERCEPT: return "alert_on_updated_intercept";
226             default: return "unknown";
227         }
228     }
229 
ringerModeToString(int ringerMode)230     private static String ringerModeToString(int ringerMode) {
231         switch (ringerMode) {
232             case AudioManager.RINGER_MODE_SILENT: return "silent";
233             case AudioManager.RINGER_MODE_VIBRATE: return "vibrate";
234             case AudioManager.RINGER_MODE_NORMAL: return "normal";
235             default: return "unknown";
236         }
237     }
238 
zenModeToString(int zenMode)239     private static String zenModeToString(int zenMode) {
240         switch (zenMode) {
241             case Global.ZEN_MODE_OFF: return "off";
242             case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions";
243             case Global.ZEN_MODE_ALARMS: return "alarms";
244             case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions";
245             default: return "unknown";
246         }
247     }
248 
hintsToString(int hints)249     private static String hintsToString(int hints) {
250         switch (hints) {
251             case 0 : return "none";
252             case NotificationListenerService.HINT_HOST_DISABLE_EFFECTS:
253                     return "disable_effects";
254             case NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS:
255                     return "disable_call_effects";
256             case NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS:
257                     return "disable_notification_effects";
258             default: return Integer.toString(hints);
259         }
260     }
261 
componentToString(ComponentName component)262     private static String componentToString(ComponentName component) {
263         return component != null ? component.toShortString() : null;
264     }
265 
componentListToString(List<ComponentName> components)266     private static String componentListToString(List<ComponentName> components) {
267         StringBuilder stringBuilder = new StringBuilder();
268 
269         for (int i = 0; i < components.size(); ++i) {
270             if (i > 0) {
271                 stringBuilder.append(", ");
272             }
273             stringBuilder.append(componentToString(components.get(i)));
274         }
275 
276         return stringBuilder.toString();
277     }
278 
append(int type, String msg)279     private static void append(int type, String msg) {
280         synchronized(MSGS) {
281             TIMES[sNext] = System.currentTimeMillis();
282             TYPES[sNext] = type;
283             MSGS[sNext] = msg;
284             sNext = (sNext + 1) % SIZE;
285             if (sSize < SIZE) {
286                 sSize++;
287             }
288         }
289         if (DEBUG) Slog.d(TAG, typeToString(type) + ": " + msg);
290     }
291 
dump(PrintWriter pw, String prefix)292     public static void dump(PrintWriter pw, String prefix) {
293         synchronized(MSGS) {
294             final int start = (sNext - sSize + SIZE) % SIZE;
295             for (int i = 0; i < sSize; i++) {
296                 final int j = (start + i) % SIZE;
297                 pw.print(prefix);
298                 pw.print(FORMAT.format(new Date(TIMES[j])));
299                 pw.print(' ');
300                 pw.print(typeToString(TYPES[j]));
301                 pw.print(": ");
302                 pw.println(MSGS[j]);
303             }
304         }
305     }
306 }
307