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
17package com.android.systemui.volume;
18
19import android.content.Context;
20import android.media.AudioManager;
21import android.media.AudioSystem;
22import android.provider.Settings.Global;
23import android.util.Log;
24
25import com.android.internal.logging.MetricsLogger;
26import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
27import com.android.systemui.plugins.VolumeDialogController.State;
28
29import java.util.Arrays;
30
31/**
32 *  Interesting events related to the volume.
33 */
34public 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 int DISMISS_STREAM_GONE = 7;
83    public static final String[] DISMISS_REASONS = {
84            "unknown",
85            "touch_outside",
86            "volume_controller",
87            "timeout",
88            "screen_off",
89            "settings_clicked",
90            "done_clicked",
91            "a11y_stream_changed"
92    };
93
94    public static final int SHOW_REASON_UNKNOWN = 0;
95    public static final int SHOW_REASON_VOLUME_CHANGED = 1;
96    public static final int SHOW_REASON_REMOTE_VOLUME_CHANGED = 2;
97    public static final String[] SHOW_REASONS = {
98        "unknown",
99        "volume_changed",
100        "remote_volume_changed"
101    };
102
103    public static final int ICON_STATE_UNKNOWN = 0;
104    public static final int ICON_STATE_UNMUTE = 1;
105    public static final int ICON_STATE_MUTE = 2;
106    public static final int ICON_STATE_VIBRATE = 3;
107
108    public static Callback sCallback;
109
110    public static void writeEvent(Context context, int tag, Object... list) {
111        final long time = System.currentTimeMillis();
112        final StringBuilder sb = new StringBuilder("writeEvent ").append(EVENT_TAGS[tag]);
113        if (list != null && list.length > 0) {
114            sb.append(" ");
115            switch (tag) {
116                case EVENT_SHOW_DIALOG:
117                    MetricsLogger.visible(context, MetricsEvent.VOLUME_DIALOG);
118                    MetricsLogger.histogram(context, "volume_from_keyguard",
119                            (Boolean) list[1] ? 1 : 0);
120                    sb.append(SHOW_REASONS[(Integer) list[0]]).append(" keyguard=").append(list[1]);
121                    break;
122                case EVENT_EXPAND:
123                    MetricsLogger.visibility(context, MetricsEvent.VOLUME_DIALOG_DETAILS,
124                            (Boolean) list[0]);
125                    sb.append(list[0]);
126                    break;
127                case EVENT_DISMISS_DIALOG:
128                    MetricsLogger.hidden(context, MetricsEvent.VOLUME_DIALOG);
129                    sb.append(DISMISS_REASONS[(Integer) list[0]]);
130                    break;
131                case EVENT_ACTIVE_STREAM_CHANGED:
132                    MetricsLogger.action(context, MetricsEvent.ACTION_VOLUME_STREAM,
133                            (Integer) list[0]);
134                    sb.append(AudioSystem.streamToString((Integer) list[0]));
135                    break;
136                case EVENT_ICON_CLICK:
137                    MetricsLogger.action(context, MetricsEvent.ACTION_VOLUME_ICON,
138                            (Integer) list[1]);
139                    sb.append(AudioSystem.streamToString((Integer) list[0])).append(' ')
140                            .append(iconStateToString((Integer) list[1]));
141                    break;
142                case EVENT_TOUCH_LEVEL_DONE:
143                    MetricsLogger.action(context, MetricsEvent.ACTION_VOLUME_SLIDER,
144                            (Integer) list[1]);
145                    // fall through
146                case EVENT_TOUCH_LEVEL_CHANGED:
147                case EVENT_LEVEL_CHANGED:
148                case EVENT_MUTE_CHANGED:
149                    sb.append(AudioSystem.streamToString((Integer) list[0])).append(' ')
150                            .append(list[1]);
151                    break;
152                case EVENT_KEY:
153                    MetricsLogger.action(context, MetricsEvent.ACTION_VOLUME_KEY,
154                            (Integer) list[1]);
155                    sb.append(AudioSystem.streamToString((Integer) list[0])).append(' ')
156                            .append(list[1]);
157                    break;
158                case EVENT_EXTERNAL_RINGER_MODE_CHANGED:
159                    MetricsLogger.action(context, MetricsEvent.ACTION_RINGER_MODE,
160                            (Integer) list[0]);
161                    // fall through
162                case EVENT_INTERNAL_RINGER_MODE_CHANGED:
163                    sb.append(ringerModeToString((Integer) list[0]));
164                    break;
165                case EVENT_ZEN_MODE_CHANGED:
166                    sb.append(zenModeToString((Integer) list[0]));
167                    break;
168                case EVENT_SUPPRESSOR_CHANGED:
169                    sb.append(list[0]).append(' ').append(list[1]);
170                    break;
171                default:
172                    sb.append(Arrays.asList(list));
173                    break;
174            }
175        }
176        Log.i(TAG, sb.toString());
177        if (sCallback != null) {
178            sCallback.writeEvent(time, tag, list);
179        }
180    }
181
182    public static void writeState(long time, State state) {
183        if (sCallback != null) {
184            sCallback.writeState(time, state);
185        }
186    }
187
188    private static String iconStateToString(int iconState) {
189        switch (iconState) {
190            case ICON_STATE_UNMUTE: return "unmute";
191            case ICON_STATE_MUTE: return "mute";
192            case ICON_STATE_VIBRATE: return "vibrate";
193            default: return "unknown_state_" + iconState;
194        }
195    }
196
197    private static String ringerModeToString(int ringerMode) {
198        switch (ringerMode) {
199            case AudioManager.RINGER_MODE_SILENT: return "silent";
200            case AudioManager.RINGER_MODE_VIBRATE: return "vibrate";
201            case AudioManager.RINGER_MODE_NORMAL: return "normal";
202            default: return "unknown";
203        }
204    }
205
206    private static String zenModeToString(int zenMode) {
207        switch (zenMode) {
208            case Global.ZEN_MODE_OFF: return "off";
209            case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return "important_interruptions";
210            case Global.ZEN_MODE_ALARMS: return "alarms";
211            case Global.ZEN_MODE_NO_INTERRUPTIONS: return "no_interruptions";
212            default: return "unknown";
213        }
214    }
215
216    public interface Callback {
217        void writeEvent(long time, int tag, Object[] list);
218        void writeState(long time, State state);
219    }
220}
221