John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.server.notification; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.media.AudioManager; |
| 21 | import android.net.Uri; |
| 22 | import android.os.Build; |
| 23 | import android.os.RemoteException; |
| 24 | import android.provider.Settings.Global; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 25 | import android.service.notification.Condition; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 26 | import android.service.notification.IConditionProvider; |
| 27 | import android.service.notification.ZenModeConfig; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 28 | import android.util.ArraySet; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 29 | import android.util.Slog; |
| 30 | |
| 31 | import java.io.PrintWriter; |
| 32 | import java.text.SimpleDateFormat; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 33 | import java.util.Date; |
| 34 | |
| 35 | public class ZenLog { |
| 36 | private static final String TAG = "ZenLog"; |
Griff Hazen | 1aad144 | 2014-10-20 14:02:59 -0700 | [diff] [blame] | 37 | private static final boolean DEBUG = Build.IS_DEBUGGABLE; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 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; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame^] | 49 | private static final int TYPE_SET_RINGER_MODE_EXTERNAL = 3; |
| 50 | private static final int TYPE_SET_RINGER_MODE_INTERNAL = 4; |
| 51 | private static final int TYPE_DOWNTIME = 5; |
| 52 | private static final int TYPE_SET_ZEN_MODE = 6; |
| 53 | private static final int TYPE_UPDATE_ZEN_MODE = 7; |
| 54 | private static final int TYPE_EXIT_CONDITION = 8; |
| 55 | private static final int TYPE_SUBSCRIBE = 9; |
| 56 | private static final int TYPE_UNSUBSCRIBE = 10; |
| 57 | private static final int TYPE_CONFIG = 11; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 58 | private static final int TYPE_NOT_INTERCEPTED = 12; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 59 | private static final int TYPE_DISABLE_EFFECTS = 13; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 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 | |
John Spurlock | 6ac5f8d | 2014-07-18 11:27:54 -0400 | [diff] [blame] | 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 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame^] | 74 | public static void traceSetRingerModeExternal(int ringerModeOld, int ringerModeNew, |
| 75 | String caller, int ringerModeInternalIn, int ringerModeInternalOut) { |
| 76 | append(TYPE_SET_RINGER_MODE_EXTERNAL, caller + ",e:" + |
| 77 | ringerModeToString(ringerModeOld) + "->" + |
| 78 | ringerModeToString(ringerModeNew) + ",i:" + |
| 79 | ringerModeToString(ringerModeInternalIn) + "->" + |
| 80 | ringerModeToString(ringerModeInternalOut)); |
| 81 | } |
| 82 | |
| 83 | public static void traceSetRingerModeInternal(int ringerModeOld, int ringerModeNew, |
| 84 | String caller, int ringerModeExternalIn, int ringerModeExternalOut) { |
| 85 | append(TYPE_SET_RINGER_MODE_INTERNAL, caller + ",i:" + |
| 86 | ringerModeToString(ringerModeOld) + "->" + |
| 87 | ringerModeToString(ringerModeNew) + ",e:" + |
| 88 | ringerModeToString(ringerModeExternalIn) + "->" + |
| 89 | ringerModeToString(ringerModeExternalOut)); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 90 | } |
| 91 | |
John Spurlock | cc30c8d | 2014-11-05 09:13:30 -0500 | [diff] [blame] | 92 | public static void traceDowntime(int downtimeMode, int day, ArraySet<Integer> days) { |
| 93 | append(TYPE_DOWNTIME, zenModeToString(downtimeMode) + ",day=" + day + ",days=" + days); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | public static void traceSetZenMode(int mode, String reason) { |
| 97 | append(TYPE_SET_ZEN_MODE, zenModeToString(mode) + "," + reason); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public static void traceUpdateZenMode(int fromMode, int toMode) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 101 | append(TYPE_UPDATE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode)); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 102 | } |
| 103 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 104 | public static void traceExitCondition(Condition c, ComponentName component, String reason) { |
| 105 | append(TYPE_EXIT_CONDITION, c + "," + componentToString(component) + "," + reason); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public static void traceSubscribe(Uri uri, IConditionProvider provider, RemoteException e) { |
| 109 | append(TYPE_SUBSCRIBE, uri + "," + subscribeResult(provider, e)); |
| 110 | } |
| 111 | |
| 112 | public static void traceUnsubscribe(Uri uri, IConditionProvider provider, RemoteException e) { |
| 113 | append(TYPE_UNSUBSCRIBE, uri + "," + subscribeResult(provider, e)); |
| 114 | } |
| 115 | |
| 116 | public static void traceConfig(ZenModeConfig oldConfig, ZenModeConfig newConfig) { |
| 117 | append(TYPE_CONFIG, newConfig != null ? newConfig.toString() : null); |
| 118 | } |
| 119 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 120 | public static void traceDisableEffects(NotificationRecord record, String reason) { |
| 121 | append(TYPE_DISABLE_EFFECTS, record.getKey() + "," + reason); |
| 122 | } |
| 123 | |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 124 | private static String subscribeResult(IConditionProvider provider, RemoteException e) { |
| 125 | return provider == null ? "no provider" : e != null ? e.getMessage() : "ok"; |
| 126 | } |
| 127 | |
| 128 | private static String typeToString(int type) { |
| 129 | switch (type) { |
| 130 | case TYPE_INTERCEPTED: return "intercepted"; |
| 131 | case TYPE_ALLOW_DISABLE: return "allow_disable"; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame^] | 132 | case TYPE_SET_RINGER_MODE_EXTERNAL: return "set_ringer_mode_external"; |
| 133 | case TYPE_SET_RINGER_MODE_INTERNAL: return "set_ringer_mode_internal"; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 134 | case TYPE_DOWNTIME: return "downtime"; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 135 | case TYPE_SET_ZEN_MODE: return "set_zen_mode"; |
| 136 | case TYPE_UPDATE_ZEN_MODE: return "update_zen_mode"; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 137 | case TYPE_EXIT_CONDITION: return "exit_condition"; |
| 138 | case TYPE_SUBSCRIBE: return "subscribe"; |
| 139 | case TYPE_UNSUBSCRIBE: return "unsubscribe"; |
| 140 | case TYPE_CONFIG: return "config"; |
John Spurlock | 6ac5f8d | 2014-07-18 11:27:54 -0400 | [diff] [blame] | 141 | case TYPE_NOT_INTERCEPTED: return "not_intercepted"; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 142 | case TYPE_DISABLE_EFFECTS: return "disable_effects"; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 143 | default: return "unknown"; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private static String ringerModeToString(int ringerMode) { |
| 148 | switch (ringerMode) { |
| 149 | case AudioManager.RINGER_MODE_SILENT: return "silent"; |
| 150 | case AudioManager.RINGER_MODE_VIBRATE: return "vibrate"; |
| 151 | case AudioManager.RINGER_MODE_NORMAL: return "normal"; |
| 152 | default: return "unknown"; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private static String zenModeToString(int zenMode) { |
| 157 | switch (zenMode) { |
| 158 | case Global.ZEN_MODE_OFF: return "off"; |
| 159 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions"; |
| 160 | case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions"; |
| 161 | default: return "unknown"; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private static String componentToString(ComponentName component) { |
| 166 | return component != null ? component.toShortString() : null; |
| 167 | } |
| 168 | |
| 169 | private static void append(int type, String msg) { |
| 170 | synchronized(MSGS) { |
| 171 | TIMES[sNext] = System.currentTimeMillis(); |
| 172 | TYPES[sNext] = type; |
| 173 | MSGS[sNext] = msg; |
| 174 | sNext = (sNext + 1) % SIZE; |
| 175 | if (sSize < SIZE) { |
| 176 | sSize++; |
| 177 | } |
| 178 | } |
Griff Hazen | 1aad144 | 2014-10-20 14:02:59 -0700 | [diff] [blame] | 179 | if (DEBUG) Slog.d(TAG, typeToString(type) + ": " + msg); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | public static void dump(PrintWriter pw, String prefix) { |
| 183 | synchronized(MSGS) { |
| 184 | final int start = (sNext - sSize + SIZE) % SIZE; |
| 185 | for (int i = 0; i < sSize; i++) { |
| 186 | final int j = (start + i) % SIZE; |
| 187 | pw.print(prefix); |
| 188 | pw.print(FORMAT.format(new Date(TIMES[j]))); |
| 189 | pw.print(' '); |
| 190 | pw.print(typeToString(TYPES[j])); |
| 191 | pw.print(": "); |
| 192 | pw.println(MSGS[j]); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |