blob: 525f5f858631b40b3d29b768432ad42eda636f20 [file] [log] [blame]
John Spurlock6ae82a72014-07-16 16:23:01 -04001/**
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;
John Spurlock4db0d982014-08-13 09:19:03 -040025import android.service.notification.Condition;
John Spurlock6ae82a72014-07-16 16:23:01 -040026import android.service.notification.IConditionProvider;
27import android.service.notification.ZenModeConfig;
John Spurlock4db0d982014-08-13 09:19:03 -040028import android.util.ArraySet;
John Spurlock6ae82a72014-07-16 16:23:01 -040029import android.util.Slog;
30
31import java.io.PrintWriter;
32import java.text.SimpleDateFormat;
33import java.util.Arrays;
34import java.util.Date;
John Spurlockc54459fb2014-07-27 11:19:38 -040035import java.util.HashSet;
36import java.util.Set;
John Spurlock6ae82a72014-07-16 16:23:01 -040037
38public class ZenLog {
39 private static final String TAG = "ZenLog";
40
41 private static final int SIZE = Build.IS_DEBUGGABLE ? 100 : 20;
42
43 private static final long[] TIMES = new long[SIZE];
44 private static final int[] TYPES = new int[SIZE];
45 private static final String[] MSGS = new String[SIZE];
46
47 private static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
John Spurlockc54459fb2014-07-27 11:19:38 -040048 private static final Set<String> SYSTEM_PACKAGES = new HashSet<String>(Arrays.asList(
49 "android",
50 "com.android.systemui"
51 ));
John Spurlock6ae82a72014-07-16 16:23:01 -040052
53 private static final int TYPE_INTERCEPTED = 1;
54 private static final int TYPE_ALLOW_DISABLE = 2;
55 private static final int TYPE_SET_RINGER_MODE = 3;
56 private static final int TYPE_DOWNTIME = 4;
John Spurlock4db0d982014-08-13 09:19:03 -040057 private static final int TYPE_SET_ZEN_MODE = 5;
58 private static final int TYPE_UPDATE_ZEN_MODE = 6;
59 private static final int TYPE_EXIT_CONDITION = 7;
60 private static final int TYPE_SUBSCRIBE = 8;
61 private static final int TYPE_UNSUBSCRIBE = 9;
62 private static final int TYPE_CONFIG = 10;
63 private static final int TYPE_FOLLOW_RINGER_MODE = 11;
64 private static final int TYPE_NOT_INTERCEPTED = 12;
John Spurlock6ae82a72014-07-16 16:23:01 -040065
66 private static int sNext;
67 private static int sSize;
68
69 public static void traceIntercepted(NotificationRecord record, String reason) {
70 if (record != null && record.isIntercepted()) return; // already logged
71 append(TYPE_INTERCEPTED, record.getKey() + "," + reason);
72 }
73
John Spurlock6ac5f8d2014-07-18 11:27:54 -040074 public static void traceNotIntercepted(NotificationRecord record, String reason) {
75 if (record != null && record.isUpdate) return; // already logged
76 append(TYPE_NOT_INTERCEPTED, record.getKey() + "," + reason);
77 }
78
John Spurlock6ae82a72014-07-16 16:23:01 -040079 public static void traceAllowDisable(String pkg, boolean allowDisable, String reason) {
John Spurlockc54459fb2014-07-27 11:19:38 -040080 if (SYSTEM_PACKAGES.contains(pkg)) return;
John Spurlock6ae82a72014-07-16 16:23:01 -040081 append(TYPE_ALLOW_DISABLE, allowDisable + "," + pkg + "," + reason);
82 }
83
84 public static void traceSetRingerMode(int ringerMode) {
85 append(TYPE_SET_RINGER_MODE, ringerModeToString(ringerMode));
86 }
87
John Spurlock4db0d982014-08-13 09:19:03 -040088 public static void traceDowntime(boolean inDowntime, int day, ArraySet<Integer> days) {
89 append(TYPE_DOWNTIME, inDowntime + ",day=" + day + ",days=" + days);
90 }
91
92 public static void traceSetZenMode(int mode, String reason) {
93 append(TYPE_SET_ZEN_MODE, zenModeToString(mode) + "," + reason);
John Spurlock6ae82a72014-07-16 16:23:01 -040094 }
95
96 public static void traceUpdateZenMode(int fromMode, int toMode) {
John Spurlock4db0d982014-08-13 09:19:03 -040097 append(TYPE_UPDATE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode));
John Spurlock6ae82a72014-07-16 16:23:01 -040098 }
99
John Spurlock4db0d982014-08-13 09:19:03 -0400100 public static void traceExitCondition(Condition c, ComponentName component, String reason) {
101 append(TYPE_EXIT_CONDITION, c + "," + componentToString(component) + "," + reason);
John Spurlock6ae82a72014-07-16 16:23:01 -0400102 }
103
104 public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
105 append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e));
106 }
107
108 public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) {
109 append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e));
110 }
111
112 public static void traceConfig(ZenModeConfig oldConfig, ZenModeConfig newConfig) {
113 append(TYPE_CONFIG, newConfig != null ? newConfig.toString() : null);
114 }
115
John Spurlock8c01d882014-07-28 13:37:13 -0400116 public static void traceFollowRingerMode(int ringerMode, int oldZen, int newZen) {
117 append(TYPE_FOLLOW_RINGER_MODE, ringerModeToString(ringerMode) + ", "
118 + zenModeToString(oldZen) + " -> " + zenModeToString(newZen));
119 }
120
John Spurlock6ae82a72014-07-16 16:23:01 -0400121 private static String subscribeResult(IConditionProvider provider, RemoteException e) {
122 return provider == null ? "no provider" : e != null ? e.getMessage() : "ok";
123 }
124
125 private static String typeToString(int type) {
126 switch (type) {
127 case TYPE_INTERCEPTED: return "intercepted";
128 case TYPE_ALLOW_DISABLE: return "allow_disable";
129 case TYPE_SET_RINGER_MODE: return "set_ringer_mode";
130 case TYPE_DOWNTIME: return "downtime";
John Spurlock4db0d982014-08-13 09:19:03 -0400131 case TYPE_SET_ZEN_MODE: return "set_zen_mode";
132 case TYPE_UPDATE_ZEN_MODE: return "update_zen_mode";
John Spurlock6ae82a72014-07-16 16:23:01 -0400133 case TYPE_EXIT_CONDITION: return "exit_condition";
134 case TYPE_SUBSCRIBE: return "subscribe";
135 case TYPE_UNSUBSCRIBE: return "unsubscribe";
136 case TYPE_CONFIG: return "config";
John Spurlock8c01d882014-07-28 13:37:13 -0400137 case TYPE_FOLLOW_RINGER_MODE: return "follow_ringer_mode";
John Spurlock6ac5f8d2014-07-18 11:27:54 -0400138 case TYPE_NOT_INTERCEPTED: return "not_intercepted";
John Spurlock6ae82a72014-07-16 16:23:01 -0400139 default: return "unknown";
140 }
141 }
142
143 private static String ringerModeToString(int ringerMode) {
144 switch (ringerMode) {
145 case AudioManager.RINGER_MODE_SILENT: return "silent";
146 case AudioManager.RINGER_MODE_VIBRATE: return "vibrate";
147 case AudioManager.RINGER_MODE_NORMAL: return "normal";
148 default: return "unknown";
149 }
150 }
151
152 private static String zenModeToString(int zenMode) {
153 switch (zenMode) {
154 case Global.ZEN_MODE_OFF: return "off";
155 case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions";
156 case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions";
157 default: return "unknown";
158 }
159 }
160
161 private static String componentToString(ComponentName component) {
162 return component != null ? component.toShortString() : null;
163 }
164
165 private static void append(int type, String msg) {
166 synchronized(MSGS) {
167 TIMES[sNext] = System.currentTimeMillis();
168 TYPES[sNext] = type;
169 MSGS[sNext] = msg;
170 sNext = (sNext + 1) % SIZE;
171 if (sSize < SIZE) {
172 sSize++;
173 }
174 }
175 Slog.d(TAG, typeToString(type) + ": " + msg);
176 }
177
178 public static void dump(PrintWriter pw, String prefix) {
179 synchronized(MSGS) {
180 final int start = (sNext - sSize + SIZE) % SIZE;
181 for (int i = 0; i < sSize; i++) {
182 final int j = (start + i) % SIZE;
183 pw.print(prefix);
184 pw.print(FORMAT.format(new Date(TIMES[j])));
185 pw.print(' ');
186 pw.print(typeToString(TYPES[j]));
187 pw.print(": ");
188 pw.println(MSGS[j]);
189 }
190 }
191 }
192}