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