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