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