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"; |
| 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; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 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; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame^] | 58 | private static final int TYPE_DISABLE_EFFECTS = 13; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 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 | |
John Spurlock | 6ac5f8d | 2014-07-18 11:27:54 -0400 | [diff] [blame] | 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 | |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 73 | public static void traceSetRingerMode(int ringerMode) { |
| 74 | append(TYPE_SET_RINGER_MODE, ringerModeToString(ringerMode)); |
| 75 | } |
| 76 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 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); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | public static void traceUpdateZenMode(int fromMode, int toMode) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 86 | append(TYPE_UPDATE_ZEN_MODE, zenModeToString(fromMode) + " -> " + zenModeToString(toMode)); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 87 | } |
| 88 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 89 | public static void traceExitCondition(Condition c, ComponentName component, String reason) { |
| 90 | append(TYPE_EXIT_CONDITION, c + "," + componentToString(component) + "," + reason); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 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 | |
John Spurlock | 8c01d88 | 2014-07-28 13:37:13 -0400 | [diff] [blame] | 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 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame^] | 110 | public static void traceDisableEffects(NotificationRecord record, String reason) { |
| 111 | append(TYPE_DISABLE_EFFECTS, record.getKey() + "," + reason); |
| 112 | } |
| 113 | |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 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"; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 124 | case TYPE_SET_ZEN_MODE: return "set_zen_mode"; |
| 125 | case TYPE_UPDATE_ZEN_MODE: return "update_zen_mode"; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 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"; |
John Spurlock | 8c01d88 | 2014-07-28 13:37:13 -0400 | [diff] [blame] | 130 | case TYPE_FOLLOW_RINGER_MODE: return "follow_ringer_mode"; |
John Spurlock | 6ac5f8d | 2014-07-18 11:27:54 -0400 | [diff] [blame] | 131 | case TYPE_NOT_INTERCEPTED: return "not_intercepted"; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame^] | 132 | case TYPE_DISABLE_EFFECTS: return "disable_effects"; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 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 | } |