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