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