ZenLog.java revision cc30c8d729c9d846012ac6dd03c1e51262936e8f
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 private static final boolean DEBUG = Build.IS_DEBUGGABLE; 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 47 private static final int TYPE_INTERCEPTED = 1; 48 private static final int TYPE_ALLOW_DISABLE = 2; 49 private static final int TYPE_SET_RINGER_MODE = 3; 50 private static final int TYPE_DOWNTIME = 4; 51 private static final int TYPE_SET_ZEN_MODE = 5; 52 private static final int TYPE_UPDATE_ZEN_MODE = 6; 53 private static final int TYPE_EXIT_CONDITION = 7; 54 private static final int TYPE_SUBSCRIBE = 8; 55 private static final int TYPE_UNSUBSCRIBE = 9; 56 private static final int TYPE_CONFIG = 10; 57 private static final int TYPE_FOLLOW_RINGER_MODE = 11; 58 private static final int TYPE_NOT_INTERCEPTED = 12; 59 private static final int TYPE_DISABLE_EFFECTS = 13; 60 61 private static int sNext; 62 private static int sSize; 63 64 public static void traceIntercepted(NotificationRecord record, String reason) { 65 if (record != null && record.isIntercepted()) return; // already logged 66 append(TYPE_INTERCEPTED, record.getKey() + "," + reason); 67 } 68 69 public static void traceNotIntercepted(NotificationRecord record, String reason) { 70 if (record != null && record.isUpdate) return; // already logged 71 append(TYPE_NOT_INTERCEPTED, record.getKey() + "," + reason); 72 } 73 74 public static void traceSetRingerMode(int ringerMode) { 75 append(TYPE_SET_RINGER_MODE, ringerModeToString(ringerMode)); 76 } 77 78 public static void traceDowntime(int downtimeMode, int day, ArraySet<Integer> days) { 79 append(TYPE_DOWNTIME, zenModeToString(downtimeMode) + ",day=" + day + ",days=" + days); 80 } 81 82 public static void traceSetZenMode(int mode, String reason) { 83 append(TYPE_SET_ZEN_MODE, zenModeToString(mode) + "," + reason); 84 } 85 86 public static void traceUpdateZenMode(int fromMode, int toMode) { 87 append(TYPE_UPDATE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode)); 88 } 89 90 public static void traceExitCondition(Condition c, ComponentName component, String reason) { 91 append(TYPE_EXIT_CONDITION, c + "," + componentToString(component) + "," + reason); 92 } 93 94 public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) { 95 append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e)); 96 } 97 98 public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) { 99 append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e)); 100 } 101 102 public static void traceConfig(ZenModeConfig oldConfig, ZenModeConfig newConfig) { 103 append(TYPE_CONFIG, newConfig != null ? newConfig.toString() : null); 104 } 105 106 public static void traceFollowRingerMode(int ringerMode, int oldZen, int newZen) { 107 append(TYPE_FOLLOW_RINGER_MODE, ringerModeToString(ringerMode) + ", " 108 + zenModeToString(oldZen) + " -> " + zenModeToString(newZen)); 109 } 110 111 public static void traceDisableEffects(NotificationRecord record, String reason) { 112 append(TYPE_DISABLE_EFFECTS, record.getKey() + "," + reason); 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_SET_ZEN_MODE: return "set_zen_mode"; 126 case TYPE_UPDATE_ZEN_MODE: return "update_zen_mode"; 127 case TYPE_EXIT_CONDITION: return "exit_condition"; 128 case TYPE_SUBSCRIBE: return "subscribe"; 129 case TYPE_UNSUBSCRIBE: return "unsubscribe"; 130 case TYPE_CONFIG: return "config"; 131 case TYPE_FOLLOW_RINGER_MODE: return "follow_ringer_mode"; 132 case TYPE_NOT_INTERCEPTED: return "not_intercepted"; 133 case TYPE_DISABLE_EFFECTS: return "disable_effects"; 134 default: return "unknown"; 135 } 136 } 137 138 private static String ringerModeToString(int ringerMode) { 139 switch (ringerMode) { 140 case AudioManager.RINGER_MODE_SILENT: return "silent"; 141 case AudioManager.RINGER_MODE_VIBRATE: return "vibrate"; 142 case AudioManager.RINGER_MODE_NORMAL: return "normal"; 143 default: return "unknown"; 144 } 145 } 146 147 private static String zenModeToString(int zenMode) { 148 switch (zenMode) { 149 case Global.ZEN_MODE_OFF: return "off"; 150 case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions"; 151 case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions"; 152 default: return "unknown"; 153 } 154 } 155 156 private static String componentToString(ComponentName component) { 157 return component != null ? component.toShortString() : null; 158 } 159 160 private static void append(int type, String msg) { 161 synchronized(MSGS) { 162 TIMES[sNext] = System.currentTimeMillis(); 163 TYPES[sNext] = type; 164 MSGS[sNext] = msg; 165 sNext = (sNext + 1) % SIZE; 166 if (sSize < SIZE) { 167 sSize++; 168 } 169 } 170 if (DEBUG) Slog.d(TAG, typeToString(type) + ": " + msg); 171 } 172 173 public static void dump(PrintWriter pw, String prefix) { 174 synchronized(MSGS) { 175 final int start = (sNext - sSize + SIZE) % SIZE; 176 for (int i = 0; i < sSize; i++) { 177 final int j = (start + i) % SIZE; 178 pw.print(prefix); 179 pw.print(FORMAT.format(new Date(TIMES[j]))); 180 pw.print(' '); 181 pw.print(typeToString(TYPES[j])); 182 pw.print(": "); 183 pw.println(MSGS[j]); 184 } 185 } 186 } 187} 188