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