1 /*
2  * Copyright (C) 2015 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.systemui.volume;
18 
19 import android.content.Context;
20 import android.media.AudioManager;
21 import android.media.AudioSystem;
22 import android.provider.Settings.Global;
23 import android.util.Log;
24 
25 import com.android.internal.logging.MetricsLogger;
26 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
27 import com.android.systemui.plugins.VolumeDialogController.State;
28 
29 import java.util.Arrays;
30 
31 /**
32  *  Interesting events related to the volume.
33  */
34 public class Events {
35     private static final String TAG = Util.logTag(Events.class);
36 
37     public static final int EVENT_SHOW_DIALOG = 0;  // (reason|int) (keyguard|bool)
38     public static final int EVENT_DISMISS_DIALOG = 1; // (reason|int)
39     public static final int EVENT_ACTIVE_STREAM_CHANGED = 2; // (stream|int)
40     public static final int EVENT_EXPAND = 3; // (expand|bool)
41     public static final int EVENT_KEY = 4;
42     public static final int EVENT_COLLECTION_STARTED = 5;
43     public static final int EVENT_COLLECTION_STOPPED = 6;
44     public static final int EVENT_ICON_CLICK = 7; // (stream|int) (icon_state|int)
45     public static final int EVENT_SETTINGS_CLICK = 8;
46     public static final int EVENT_TOUCH_LEVEL_CHANGED = 9; // (stream|int) (level|int)
47     public static final int EVENT_LEVEL_CHANGED = 10; // (stream|int) (level|int)
48     public static final int EVENT_INTERNAL_RINGER_MODE_CHANGED = 11; // (mode|int)
49     public static final int EVENT_EXTERNAL_RINGER_MODE_CHANGED = 12; // (mode|int)
50     public static final int EVENT_ZEN_MODE_CHANGED = 13; // (mode|int)
51     public static final int EVENT_SUPPRESSOR_CHANGED = 14;  // (component|string) (name|string)
52     public static final int EVENT_MUTE_CHANGED = 15;  // (stream|int) (muted|bool)
53     public static final int EVENT_TOUCH_LEVEL_DONE = 16;  // (stream|int) (level|bool)
54     public static final int EVENT_ZEN_CONFIG_CHANGED = 17; // (allow/disallow|string)
55     public static final int EVENT_RINGER_TOGGLE = 18; // (ringer_mode)
56     public static final int EVENT_SHOW_USB_OVERHEAT_ALARM = 19; // (reason|int) (keyguard|bool)
57     public static final int EVENT_DISMISS_USB_OVERHEAT_ALARM = 20; // (reason|int) (keyguard|bool)
58     public static final int EVENT_ODI_CAPTIONS_CLICK = 21;
59     public static final int EVENT_ODI_CAPTIONS_TOOLTIP_CLICK = 22;
60 
61     private static final String[] EVENT_TAGS = {
62             "show_dialog",
63             "dismiss_dialog",
64             "active_stream_changed",
65             "expand",
66             "key",
67             "collection_started",
68             "collection_stopped",
69             "icon_click",
70             "settings_click",
71             "touch_level_changed",
72             "level_changed",
73             "internal_ringer_mode_changed",
74             "external_ringer_mode_changed",
75             "zen_mode_changed",
76             "suppressor_changed",
77             "mute_changed",
78             "touch_level_done",
79             "zen_mode_config_changed",
80             "ringer_toggle",
81             "show_usb_overheat_alarm",
82             "dismiss_usb_overheat_alarm",
83             "odi_captions_click",
84             "odi_captions_tooltip_click"
85     };
86 
87     public static final int DISMISS_REASON_UNKNOWN = 0;
88     public static final int DISMISS_REASON_TOUCH_OUTSIDE = 1;
89     public static final int DISMISS_REASON_VOLUME_CONTROLLER = 2;
90     public static final int DISMISS_REASON_TIMEOUT = 3;
91     public static final int DISMISS_REASON_SCREEN_OFF = 4;
92     public static final int DISMISS_REASON_SETTINGS_CLICKED = 5;
93     public static final int DISMISS_REASON_DONE_CLICKED = 6;
94     public static final int DISMISS_STREAM_GONE = 7;
95     public static final int DISMISS_REASON_OUTPUT_CHOOSER = 8;
96     public static final int DISMISS_REASON_USB_OVERHEAD_ALARM_CHANGED = 9;
97     public static final String[] DISMISS_REASONS = {
98             "unknown",
99             "touch_outside",
100             "volume_controller",
101             "timeout",
102             "screen_off",
103             "settings_clicked",
104             "done_clicked",
105             "a11y_stream_changed",
106             "output_chooser",
107             "usb_temperature_below_threshold"
108     };
109 
110     public static final int SHOW_REASON_UNKNOWN = 0;
111     public static final int SHOW_REASON_VOLUME_CHANGED = 1;
112     public static final int SHOW_REASON_REMOTE_VOLUME_CHANGED = 2;
113     public static final int SHOW_REASON_USB_OVERHEAD_ALARM_CHANGED = 3;
114     public static final String[] SHOW_REASONS = {
115         "unknown",
116         "volume_changed",
117         "remote_volume_changed",
118         "usb_temperature_above_threshold"
119     };
120 
121     public static final int ICON_STATE_UNKNOWN = 0;
122     public static final int ICON_STATE_UNMUTE = 1;
123     public static final int ICON_STATE_MUTE = 2;
124     public static final int ICON_STATE_VIBRATE = 3;
125 
126     public static Callback sCallback;
127 
writeEvent(Context context, int tag, Object... list)128     public static void writeEvent(Context context, int tag, Object... list) {
129         MetricsLogger logger = new MetricsLogger();
130         final long time = System.currentTimeMillis();
131         final StringBuilder sb = new StringBuilder("writeEvent ").append(EVENT_TAGS[tag]);
132         if (list != null && list.length > 0) {
133             sb.append(" ");
134             switch (tag) {
135                 case EVENT_SHOW_DIALOG:
136                     MetricsLogger.visible(context, MetricsEvent.VOLUME_DIALOG);
137                     MetricsLogger.histogram(context, "volume_from_keyguard",
138                             (Boolean) list[1] ? 1 : 0);
139                     sb.append(SHOW_REASONS[(Integer) list[0]]).append(" keyguard=").append(list[1]);
140                     break;
141                 case EVENT_EXPAND:
142                     MetricsLogger.visibility(context, MetricsEvent.VOLUME_DIALOG_DETAILS,
143                             (Boolean) list[0]);
144                     sb.append(list[0]);
145                     break;
146                 case EVENT_DISMISS_DIALOG:
147                     MetricsLogger.hidden(context, MetricsEvent.VOLUME_DIALOG);
148                     sb.append(DISMISS_REASONS[(Integer) list[0]]);
149                     break;
150                 case EVENT_ACTIVE_STREAM_CHANGED:
151                     MetricsLogger.action(context, MetricsEvent.ACTION_VOLUME_STREAM,
152                             (Integer) list[0]);
153                     sb.append(AudioSystem.streamToString((Integer) list[0]));
154                     break;
155                 case EVENT_ICON_CLICK:
156                     MetricsLogger.action(context, MetricsEvent.ACTION_VOLUME_ICON,
157                             (Integer) list[0]);
158                     sb.append(AudioSystem.streamToString((Integer) list[0])).append(' ')
159                             .append(iconStateToString((Integer) list[1]));
160                     break;
161                 case EVENT_TOUCH_LEVEL_DONE:
162                     MetricsLogger.action(context, MetricsEvent.ACTION_VOLUME_SLIDER,
163                             (Integer) list[1]);
164                     // fall through
165                 case EVENT_TOUCH_LEVEL_CHANGED:
166                 case EVENT_LEVEL_CHANGED:
167                 case EVENT_MUTE_CHANGED:
168                     sb.append(AudioSystem.streamToString((Integer) list[0])).append(' ')
169                             .append(list[1]);
170                     break;
171                 case EVENT_KEY:
172                     MetricsLogger.action(context, MetricsEvent.ACTION_VOLUME_KEY,
173                             (Integer) list[0]);
174                     sb.append(AudioSystem.streamToString((Integer) list[0])).append(' ')
175                             .append(list[1]);
176                     break;
177                 case EVENT_RINGER_TOGGLE:
178                     logger.action(MetricsEvent.ACTION_VOLUME_RINGER_TOGGLE, (Integer) list[0]);
179                     break;
180                 case EVENT_SETTINGS_CLICK:
181                     logger.action(MetricsEvent.ACTION_VOLUME_SETTINGS);
182                     break;
183                 case EVENT_EXTERNAL_RINGER_MODE_CHANGED:
184                     MetricsLogger.action(context, MetricsEvent.ACTION_RINGER_MODE,
185                             (Integer) list[0]);
186                     // fall through
187                 case EVENT_INTERNAL_RINGER_MODE_CHANGED:
188                     sb.append(ringerModeToString((Integer) list[0]));
189                     break;
190                 case EVENT_ZEN_MODE_CHANGED:
191                     sb.append(zenModeToString((Integer) list[0]));
192                     break;
193                 case EVENT_SUPPRESSOR_CHANGED:
194                     sb.append(list[0]).append(' ').append(list[1]);
195                     break;
196                 case EVENT_SHOW_USB_OVERHEAT_ALARM:
197                     MetricsLogger.visible(context, MetricsEvent.POWER_OVERHEAT_ALARM);
198                     MetricsLogger.histogram(context, "show_usb_overheat_alarm",
199                             (Boolean) list[1] ? 1 : 0);
200                     sb.append(SHOW_REASONS[(Integer) list[0]]).append(" keyguard=").append(list[1]);
201                     break;
202                 case EVENT_DISMISS_USB_OVERHEAT_ALARM:
203                     MetricsLogger.hidden(context, MetricsEvent.POWER_OVERHEAT_ALARM);
204                     MetricsLogger.histogram(context, "dismiss_usb_overheat_alarm",
205                             (Boolean) list[1] ? 1 : 0);
206                     sb.append(DISMISS_REASONS[(Integer) list[0]])
207                         .append(" keyguard=").append(list[1]);
208                     break;
209                 default:
210                     sb.append(Arrays.asList(list));
211                     break;
212             }
213         }
214         Log.i(TAG, sb.toString());
215         if (sCallback != null) {
216             sCallback.writeEvent(time, tag, list);
217         }
218     }
219 
writeState(long time, State state)220     public static void writeState(long time, State state) {
221         if (sCallback != null) {
222             sCallback.writeState(time, state);
223         }
224     }
225 
iconStateToString(int iconState)226     private static String iconStateToString(int iconState) {
227         switch (iconState) {
228             case ICON_STATE_UNMUTE: return "unmute";
229             case ICON_STATE_MUTE: return "mute";
230             case ICON_STATE_VIBRATE: return "vibrate";
231             default: return "unknown_state_" + iconState;
232         }
233     }
234 
ringerModeToString(int ringerMode)235     private static String ringerModeToString(int ringerMode) {
236         switch (ringerMode) {
237             case AudioManager.RINGER_MODE_SILENT: return "silent";
238             case AudioManager.RINGER_MODE_VIBRATE: return "vibrate";
239             case AudioManager.RINGER_MODE_NORMAL: return "normal";
240             default: return "unknown";
241         }
242     }
243 
zenModeToString(int zenMode)244     private static String zenModeToString(int zenMode) {
245         switch (zenMode) {
246             case Global.ZEN_MODE_OFF: return "off";
247             case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions";
248             case Global.ZEN_MODE_ALARMS: return "alarms";
249             case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions";
250             default: return "unknown";
251         }
252     }
253 
254     public interface Callback {
writeEvent(long time, int tag, Object[] list)255         void writeEvent(long time, int tag, Object[] list);
writeState(long time, State state)256         void writeState(long time, State state);
257     }
258 }
259