ZenLog.java revision 6ac5f8df62a4b6d87cf32797d2886efab8e28226
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.IConditionProvider; 26import android.service.notification.ZenModeConfig; 27import android.util.Slog; 28 29import java.io.PrintWriter; 30import java.text.SimpleDateFormat; 31import java.util.Arrays; 32import java.util.Date; 33import java.util.HashSet; 34import java.util.Set; 35 36public class ZenLog { 37 private static final String TAG = "ZenLog"; 38 39 private static final int SIZE = Build.IS_DEBUGGABLE ? 100 : 20; 40 41 private static final long[] TIMES = new long[SIZE]; 42 private static final int[] TYPES = new int[SIZE]; 43 private static final String[] MSGS = new String[SIZE]; 44 45 private static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS"); 46 private static final Set<String> SYSTEM_PACKAGES = new HashSet<String>(Arrays.asList( 47 "android", 48 "com.android.systemui" 49 )); 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 = 3; 54 private static final int TYPE_DOWNTIME = 4; 55 private static final int TYPE_ZEN_MODE = 5; 56 private static final int TYPE_EXIT_CONDITION = 6; 57 private static final int TYPE_SUBSCRIBE = 7; 58 private static final int TYPE_UNSUBSCRIBE = 8; 59 private static final int TYPE_CONFIG = 9; 60 private static final int TYPE_FOLLOW_RINGER_MODE = 10; 61 private static final int TYPE_NOT_INTERCEPTED = 11; 62 63 private static int sNext; 64 private static int sSize; 65 66 public static void traceIntercepted(NotificationRecord record, String reason) { 67 if (record != null && record.isIntercepted()) return; // already logged 68 append(TYPE_INTERCEPTED, record.getKey() + "," + reason); 69 } 70 71 public static void traceNotIntercepted(NotificationRecord record, String reason) { 72 if (record != null && record.isUpdate) return; // already logged 73 append(TYPE_NOT_INTERCEPTED, record.getKey() + "," + reason); 74 } 75 76 public static void traceAllowDisable(String pkg, boolean allowDisable, String reason) { 77 if (SYSTEM_PACKAGES.contains(pkg)) return; 78 append(TYPE_ALLOW_DISABLE, allowDisable + "," + pkg + "," + reason); 79 } 80 81 public static void traceSetRingerMode(int ringerMode) { 82 append(TYPE_SET_RINGER_MODE, ringerModeToString(ringerMode)); 83 } 84 85 public static void traceDowntime(boolean enter, int day, int[] days) { 86 append(TYPE_DOWNTIME, enter + ",day=" + day + ",days=" + (days != null ? Arrays.asList(days) 87 : null)); 88 } 89 90 public static void traceUpdateZenMode(int fromMode, int toMode) { 91 append(TYPE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode)); 92 } 93 94 public static void traceExitCondition(Uri id, ComponentName component, String reason) { 95 append(TYPE_EXIT_CONDITION, id + "," + componentToString(component) + "," + reason); 96 } 97 98 public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) { 99 append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e)); 100 } 101 102 public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) { 103 append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e)); 104 } 105 106 public static void traceConfig(ZenModeConfig oldConfig, ZenModeConfig newConfig) { 107 append(TYPE_CONFIG, newConfig != null ? newConfig.toString() : null); 108 } 109 110 public static void traceFollowRingerMode(int ringerMode, int oldZen, int newZen) { 111 append(TYPE_FOLLOW_RINGER_MODE, ringerModeToString(ringerMode) + ", " 112 + zenModeToString(oldZen) + " -> " + zenModeToString(newZen)); 113 } 114 115 private static String subscribeResult(IConditionProvider provider, RemoteException e) { 116 return provider == null ? "no provider" : e != null ? e.getMessage() : "ok"; 117 } 118 119 private static String typeToString(int type) { 120 switch (type) { 121 case TYPE_INTERCEPTED: return "intercepted"; 122 case TYPE_ALLOW_DISABLE: return "allow_disable"; 123 case TYPE_SET_RINGER_MODE: return "set_ringer_mode"; 124 case TYPE_DOWNTIME: return "downtime"; 125 case TYPE_ZEN_MODE: return "zen_mode"; 126 case TYPE_EXIT_CONDITION: return "exit_condition"; 127 case TYPE_SUBSCRIBE: return "subscribe"; 128 case TYPE_UNSUBSCRIBE: return "unsubscribe"; 129 case TYPE_CONFIG: return "config"; 130 case TYPE_FOLLOW_RINGER_MODE: return "follow_ringer_mode"; 131 case TYPE_NOT_INTERCEPTED: return "not_intercepted"; 132 default: return "unknown"; 133 } 134 } 135 136 private static String ringerModeToString(int ringerMode) { 137 switch (ringerMode) { 138 case AudioManager.RINGER_MODE_SILENT: return "silent"; 139 case AudioManager.RINGER_MODE_VIBRATE: return "vibrate"; 140 case AudioManager.RINGER_MODE_NORMAL: return "normal"; 141 default: return "unknown"; 142 } 143 } 144 145 private static String zenModeToString(int zenMode) { 146 switch (zenMode) { 147 case Global.ZEN_MODE_OFF: return "off"; 148 case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions"; 149 case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions"; 150 default: return "unknown"; 151 } 152 } 153 154 private static String componentToString(ComponentName component) { 155 return component != null ? component.toShortString() : null; 156 } 157 158 private static void append(int type, String msg) { 159 synchronized(MSGS) { 160 TIMES[sNext] = System.currentTimeMillis(); 161 TYPES[sNext] = type; 162 MSGS[sNext] = msg; 163 sNext = (sNext + 1) % SIZE; 164 if (sSize < SIZE) { 165 sSize++; 166 } 167 } 168 Slog.d(TAG, typeToString(type) + ": " + msg); 169 } 170 171 public static void dump(PrintWriter pw, String prefix) { 172 synchronized(MSGS) { 173 final int start = (sNext - sSize + SIZE) % SIZE; 174 for (int i = 0; i < sSize; i++) { 175 final int j = (start + i) % SIZE; 176 pw.print(prefix); 177 pw.print(FORMAT.format(new Date(TIMES[j]))); 178 pw.print(' '); 179 pw.print(typeToString(TYPES[j])); 180 pw.print(": "); 181 pw.println(MSGS[j]); 182 } 183 } 184 } 185} 186