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