1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.plugins;
16 
17 import android.annotation.IntegerRes;
18 import android.content.ComponentName;
19 import android.media.AudioManager;
20 import android.media.AudioSystem;
21 import android.os.Handler;
22 import android.os.VibrationEffect;
23 import android.util.SparseArray;
24 
25 import com.android.systemui.plugins.VolumeDialogController.Callbacks;
26 import com.android.systemui.plugins.VolumeDialogController.State;
27 import com.android.systemui.plugins.VolumeDialogController.StreamState;
28 import com.android.systemui.plugins.annotations.DependsOn;
29 import com.android.systemui.plugins.annotations.ProvidesInterface;
30 
31 /**
32  * Manages the VolumeDialog.
33  *
34  * Accessible through {@link PluginDependency}
35  */
36 @ProvidesInterface(version = VolumeDialogController.VERSION)
37 @DependsOn(target = StreamState.class)
38 @DependsOn(target = State.class)
39 @DependsOn(target = Callbacks.class)
40 public interface VolumeDialogController {
41     int VERSION = 1;
42 
setActiveStream(int stream)43     void setActiveStream(int stream);
setStreamVolume(int stream, int userLevel)44     void setStreamVolume(int stream, int userLevel);
setRingerMode(int ringerModeNormal, boolean external)45     void setRingerMode(int ringerModeNormal, boolean external);
46 
hasVibrator()47     boolean hasVibrator();
vibrate(VibrationEffect effect)48     void vibrate(VibrationEffect effect);
scheduleTouchFeedback()49     void scheduleTouchFeedback();
50 
getAudioManager()51     AudioManager getAudioManager();
52 
notifyVisible(boolean visible)53     void notifyVisible(boolean visible);
54 
addCallback(Callbacks callbacks, Handler handler)55     void addCallback(Callbacks callbacks, Handler handler);
removeCallback(Callbacks callbacks)56     void removeCallback(Callbacks callbacks);
57 
userActivity()58     void userActivity();
getState()59     void getState();
60 
areCaptionsEnabled()61     boolean areCaptionsEnabled();
setCaptionsEnabled(boolean isEnabled)62     void setCaptionsEnabled(boolean isEnabled);
isCaptionStreamOptedOut()63     boolean isCaptionStreamOptedOut();
64 
getCaptionsComponentState(boolean fromTooltip)65     void getCaptionsComponentState(boolean fromTooltip);
66 
67     @ProvidesInterface(version = StreamState.VERSION)
68     public static final class StreamState {
69         public static final int VERSION = 1;
70 
71         public boolean dynamic;
72         public int level;
73         public int levelMin;
74         public int levelMax;
75         public boolean muted;
76         public boolean muteSupported;
77         public @IntegerRes int name;
78         public String remoteLabel;
79         public boolean routedToBluetooth;
80 
copy()81         public StreamState copy() {
82             final StreamState rt = new StreamState();
83             rt.dynamic = dynamic;
84             rt.level = level;
85             rt.levelMin = levelMin;
86             rt.levelMax = levelMax;
87             rt.muted = muted;
88             rt.muteSupported = muteSupported;
89             rt.name = name;
90             rt.remoteLabel = remoteLabel;
91             rt.routedToBluetooth = routedToBluetooth;
92             return rt;
93         }
94     }
95 
96     @ProvidesInterface(version = State.VERSION)
97     public static final class State {
98         public static final int VERSION = 1;
99 
100         public static int NO_ACTIVE_STREAM = -1;
101 
102         public final SparseArray<StreamState> states = new SparseArray<>();
103 
104         public int ringerModeInternal;
105         public int ringerModeExternal;
106         public int zenMode;
107         public ComponentName effectsSuppressor;
108         public String effectsSuppressorName;
109         public int activeStream = NO_ACTIVE_STREAM;
110         public boolean disallowAlarms;
111         public boolean disallowMedia;
112         public boolean disallowSystem;
113         public boolean disallowRinger;
114 
copy()115         public State copy() {
116             final State rt = new State();
117             for (int i = 0; i < states.size(); i++) {
118                 rt.states.put(states.keyAt(i), states.valueAt(i).copy());
119             }
120             rt.ringerModeExternal = ringerModeExternal;
121             rt.ringerModeInternal = ringerModeInternal;
122             rt.zenMode = zenMode;
123             if (effectsSuppressor != null) {
124                 rt.effectsSuppressor = effectsSuppressor.clone();
125             }
126             rt.effectsSuppressorName = effectsSuppressorName;
127             rt.activeStream = activeStream;
128             rt.disallowAlarms = disallowAlarms;
129             rt.disallowMedia = disallowMedia;
130             rt.disallowSystem = disallowSystem;
131             rt.disallowRinger = disallowRinger;
132             return rt;
133         }
134 
135         @Override
toString()136         public String toString() {
137             return toString(0);
138         }
139 
toString(int indent)140         public String toString(int indent) {
141             final StringBuilder sb = new StringBuilder("{");
142             if (indent > 0) sep(sb, indent);
143             for (int i = 0; i < states.size(); i++) {
144                 if (i > 0) {
145                     sep(sb, indent);
146                 }
147                 final int stream = states.keyAt(i);
148                 final StreamState ss = states.valueAt(i);
149                 sb.append(AudioSystem.streamToString(stream)).append(":").append(ss.level)
150                         .append('[').append(ss.levelMin).append("..").append(ss.levelMax)
151                         .append(']');
152                 if (ss.muted) sb.append(" [MUTED]");
153                 if (ss.dynamic) sb.append(" [DYNAMIC]");
154             }
155             sep(sb, indent); sb.append("ringerModeExternal:").append(ringerModeExternal);
156             sep(sb, indent); sb.append("ringerModeInternal:").append(ringerModeInternal);
157             sep(sb, indent); sb.append("zenMode:").append(zenMode);
158             sep(sb, indent); sb.append("effectsSuppressor:").append(effectsSuppressor);
159             sep(sb, indent); sb.append("effectsSuppressorName:").append(effectsSuppressorName);
160             sep(sb, indent); sb.append("activeStream:").append(activeStream);
161             sep(sb, indent); sb.append("disallowAlarms:").append(disallowAlarms);
162             sep(sb, indent); sb.append("disallowMedia:").append(disallowMedia);
163             sep(sb, indent); sb.append("disallowSystem:").append(disallowSystem);
164             sep(sb, indent); sb.append("disallowRinger:").append(disallowRinger);
165             if (indent > 0) sep(sb, indent);
166             return sb.append('}').toString();
167         }
168 
sep(StringBuilder sb, int indent)169         private static void sep(StringBuilder sb, int indent) {
170             if (indent > 0) {
171                 sb.append('\n');
172                 for (int i = 0; i < indent; i++) {
173                     sb.append(' ');
174                 }
175             } else {
176                 sb.append(',');
177             }
178         }
179     }
180 
181     @ProvidesInterface(version = Callbacks.VERSION)
182     public interface Callbacks {
183         int VERSION = 1;
184 
onShowRequested(int reason)185         void onShowRequested(int reason);
onDismissRequested(int reason)186         void onDismissRequested(int reason);
onStateChanged(State state)187         void onStateChanged(State state);
onLayoutDirectionChanged(int layoutDirection)188         void onLayoutDirectionChanged(int layoutDirection);
onConfigurationChanged()189         void onConfigurationChanged();
onShowVibrateHint()190         void onShowVibrateHint();
onShowSilentHint()191         void onShowSilentHint();
onScreenOff()192         void onScreenOff();
onShowSafetyWarning(int flags)193         void onShowSafetyWarning(int flags);
onAccessibilityModeChanged(Boolean showA11yStream)194         void onAccessibilityModeChanged(Boolean showA11yStream);
onCaptionComponentStateChanged(Boolean isComponentEnabled, Boolean fromTooltip)195         void onCaptionComponentStateChanged(Boolean isComponentEnabled, Boolean fromTooltip);
196     }
197 }
198