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
17package com.android.server.notification;
18
19import android.content.ComponentName;
20import android.media.AudioManager;
21import android.net.Uri;
22import android.os.Build;
23import android.os.RemoteException;
24import android.provider.Settings.Global;
25import android.service.notification.Condition;
26import android.service.notification.IConditionProvider;
27import android.service.notification.NotificationListenerService;
28import android.service.notification.ZenModeConfig;
29import android.util.Slog;
30
31import java.io.PrintWriter;
32import java.text.SimpleDateFormat;
33import java.util.Date;
34import java.util.List;
35
36public class ZenLog {
37    private static final String TAG = "ZenLog";
38    private static final boolean DEBUG = Build.IS_DEBUGGABLE;
39
40    private static final int SIZE = Build.IS_DEBUGGABLE ? 100 : 20;
41
42    private static final long[] TIMES = new long[SIZE];
43    private static final int[] TYPES = new int[SIZE];
44    private static final String[] MSGS = new String[SIZE];
45
46    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
47
48    private static final int TYPE_INTERCEPTED = 1;
49    private static final int TYPE_ALLOW_DISABLE = 2;
50    private static final int TYPE_SET_RINGER_MODE_EXTERNAL = 3;
51    private static final int TYPE_SET_RINGER_MODE_INTERNAL = 4;
52    private static final int TYPE_DOWNTIME = 5;
53    private static final int TYPE_SET_ZEN_MODE = 6;
54    private static final int TYPE_UPDATE_ZEN_MODE = 7;
55    private static final int TYPE_EXIT_CONDITION = 8;
56    private static final int TYPE_SUBSCRIBE = 9;
57    private static final int TYPE_UNSUBSCRIBE = 10;
58    private static final int TYPE_CONFIG = 11;
59    private static final int TYPE_NOT_INTERCEPTED = 12;
60    private static final int TYPE_DISABLE_EFFECTS = 13;
61    private static final int TYPE_SUPPRESSOR_CHANGED = 14;
62    private static final int TYPE_LISTENER_HINTS_CHANGED = 15;
63
64    private static int sNext;
65    private static int sSize;
66
67    public static void traceIntercepted(NotificationRecord record, String reason) {
68        if (record != null && record.isIntercepted()) return;  // already logged
69        append(TYPE_INTERCEPTED, record.getKey() + "," + reason);
70    }
71
72    public static void traceNotIntercepted(NotificationRecord record, String reason) {
73        if (record != null && record.isUpdate) return;  // already logged
74        append(TYPE_NOT_INTERCEPTED, record.getKey() + "," + reason);
75    }
76
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
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
95    public static void traceDowntimeAutotrigger(String result) {
96        append(TYPE_DOWNTIME, result);
97    }
98
99    public static void traceSetZenMode(int zenMode, String reason) {
100        append(TYPE_SET_ZEN_MODE, zenModeToString(zenMode) + "," + reason);
101    }
102
103    public static void traceUpdateZenMode(int fromMode, int toMode) {
104        append(TYPE_UPDATE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode));
105    }
106
107    public static void traceExitCondition(Condition c, ComponentName component, String reason) {
108        append(TYPE_EXIT_CONDITION, c + "," + componentToString(component) + "," + reason);
109    }
110
111    public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
112        append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e));
113    }
114
115    public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
116        append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e));
117    }
118
119    public static void traceConfig(String reason, ZenModeConfig oldConfig,
120            ZenModeConfig newConfig) {
121        append(TYPE_CONFIG, reason
122                + "," + (newConfig != null ? newConfig.toString() : null)
123                + "," + ZenModeConfig.diff(oldConfig, newConfig));
124    }
125
126    public static void traceDisableEffects(NotificationRecord record, String reason) {
127        append(TYPE_DISABLE_EFFECTS, record.getKey() + "," + reason);
128    }
129
130    public static void traceEffectsSuppressorChanged(List<ComponentName> oldSuppressors,
131            List<ComponentName> newSuppressors, long suppressedEffects) {
132        append(TYPE_SUPPRESSOR_CHANGED, "suppressed effects:" + suppressedEffects + ","
133                + componentListToString(oldSuppressors) + "->"
134                + componentListToString(newSuppressors));
135    }
136
137    public static void traceListenerHintsChanged(int oldHints, int newHints, int listenerCount) {
138        append(TYPE_LISTENER_HINTS_CHANGED, hintsToString(oldHints) + "->"
139            + hintsToString(newHints) + ",listeners=" + listenerCount);
140    }
141
142    private static String subscribeResult(IConditionProvider provider, RemoteException e) {
143        return provider == null ? "no provider" : e != null ? e.getMessage() : "ok";
144    }
145
146    private static String typeToString(int type) {
147        switch (type) {
148            case TYPE_INTERCEPTED: return "intercepted";
149            case TYPE_ALLOW_DISABLE: return "allow_disable";
150            case TYPE_SET_RINGER_MODE_EXTERNAL: return "set_ringer_mode_external";
151            case TYPE_SET_RINGER_MODE_INTERNAL: return "set_ringer_mode_internal";
152            case TYPE_DOWNTIME: return "downtime";
153            case TYPE_SET_ZEN_MODE: return "set_zen_mode";
154            case TYPE_UPDATE_ZEN_MODE: return "update_zen_mode";
155            case TYPE_EXIT_CONDITION: return "exit_condition";
156            case TYPE_SUBSCRIBE: return "subscribe";
157            case TYPE_UNSUBSCRIBE: return "unsubscribe";
158            case TYPE_CONFIG: return "config";
159            case TYPE_NOT_INTERCEPTED: return "not_intercepted";
160            case TYPE_DISABLE_EFFECTS: return "disable_effects";
161            case TYPE_SUPPRESSOR_CHANGED: return "suppressor_changed";
162            case TYPE_LISTENER_HINTS_CHANGED: return "listener_hints_changed";
163            default: return "unknown";
164        }
165    }
166
167    private static String ringerModeToString(int ringerMode) {
168        switch (ringerMode) {
169            case AudioManager.RINGER_MODE_SILENT: return "silent";
170            case AudioManager.RINGER_MODE_VIBRATE: return "vibrate";
171            case AudioManager.RINGER_MODE_NORMAL: return "normal";
172            default: return "unknown";
173        }
174    }
175
176    private static String zenModeToString(int zenMode) {
177        switch (zenMode) {
178            case Global.ZEN_MODE_OFF: return "off";
179            case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions";
180            case Global.ZEN_MODE_ALARMS: return "alarms";
181            case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions";
182            default: return "unknown";
183        }
184    }
185
186    private static String hintsToString(int hints) {
187        switch (hints) {
188            case 0 : return "none";
189            case NotificationListenerService.HINT_HOST_DISABLE_EFFECTS : return "disable_effects";
190            default: return Integer.toString(hints);
191        }
192    }
193
194    private static String componentToString(ComponentName component) {
195        return component != null ? component.toShortString() : null;
196    }
197
198    private static String componentListToString(List<ComponentName> components) {
199        StringBuilder stringBuilder = new StringBuilder();
200
201        for (int i = 0; i < components.size(); ++i) {
202            if (i > 0) {
203                stringBuilder.append(", ");
204            }
205            stringBuilder.append(componentToString(components.get(i)));
206        }
207
208        return stringBuilder.toString();
209    }
210
211    private static void append(int type, String msg) {
212        synchronized(MSGS) {
213            TIMES[sNext] = System.currentTimeMillis();
214            TYPES[sNext] = type;
215            MSGS[sNext] = msg;
216            sNext = (sNext + 1) % SIZE;
217            if (sSize < SIZE) {
218                sSize++;
219            }
220        }
221        if (DEBUG) Slog.d(TAG, typeToString(type) + ": " + msg);
222    }
223
224    public static void dump(PrintWriter pw, String prefix) {
225        synchronized(MSGS) {
226            final int start = (sNext - sSize + SIZE) % SIZE;
227            for (int i = 0; i < sSize; i++) {
228                final int j = (start + i) % SIZE;
229                pw.print(prefix);
230                pw.print(FORMAT.format(new Date(TIMES[j])));
231                pw.print(' ');
232                pw.print(typeToString(TYPES[j]));
233                pw.print(": ");
234                pw.println(MSGS[j]);
235            }
236        }
237    }
238}
239