1 /*
2  * Copyright (C) 2014 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 package android.media;
17 
18 import android.annotation.IntDef;
19 import android.annotation.Nullable;
20 import android.media.MediaRouter2.RoutingController;
21 import android.media.session.MediaSession;
22 
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25 
26 /**
27  * Handles requests to adjust or set the volume on a session. This is also used
28  * to push volume updates back to the session. The provider must call
29  * {@link #setCurrentVolume(int)} each time the volume being provided changes.
30  * <p>
31  * You can set a volume provider on a session by calling
32  * {@link MediaSession#setPlaybackToRemote}.
33  */
34 public abstract class VolumeProvider {
35 
36     /**
37      * @hide
38      */
39     @IntDef({VOLUME_CONTROL_FIXED, VOLUME_CONTROL_RELATIVE, VOLUME_CONTROL_ABSOLUTE})
40     @Retention(RetentionPolicy.SOURCE)
41     public @interface ControlType {}
42 
43     /**
44      * The volume is fixed and can not be modified. Requests to change volume
45      * should be ignored.
46      */
47     public static final int VOLUME_CONTROL_FIXED = 0;
48 
49     /**
50      * The volume control uses relative adjustment via
51      * {@link #onAdjustVolume(int)}. Attempts to set the volume to a specific
52      * value should be ignored.
53      */
54     public static final int VOLUME_CONTROL_RELATIVE = 1;
55 
56     /**
57      * The volume control uses an absolute value. It may be adjusted using
58      * {@link #onAdjustVolume(int)} or set directly using
59      * {@link #onSetVolumeTo(int)}.
60      */
61     public static final int VOLUME_CONTROL_ABSOLUTE = 2;
62 
63     private final int mControlType;
64     private final int mMaxVolume;
65     private final String mControlId;
66     private int mCurrentVolume;
67     private Callback mCallback;
68 
69     /**
70      * Creates a new volume provider for handling volume events.
71      *
72      * @param volumeControl See {@link #getVolumeControl()}.
73      * @param maxVolume The maximum allowed volume.
74      * @param currentVolume The current volume on the output.
75      */
VolumeProvider(@ontrolType int volumeControl, int maxVolume, int currentVolume)76     public VolumeProvider(@ControlType int volumeControl, int maxVolume, int currentVolume) {
77         this(volumeControl, maxVolume, currentVolume, null);
78     }
79 
80     /**
81      * Creates a new volume provider for handling volume events.
82      *
83      * @param volumeControl See {@link #getVolumeControl()}.
84      * @param maxVolume The maximum allowed volume.
85      * @param currentVolume The current volume on the output.
86      * @param volumeControlId See {@link #getVolumeControlId()}.
87      */
VolumeProvider( @ontrolType int volumeControl, int maxVolume, int currentVolume, @Nullable String volumeControlId)88     public VolumeProvider(
89             @ControlType int volumeControl,
90             int maxVolume,
91             int currentVolume,
92             @Nullable String volumeControlId) {
93         mControlType = volumeControl;
94         mMaxVolume = maxVolume;
95         mCurrentVolume = currentVolume;
96         mControlId = volumeControlId;
97     }
98 
99     /**
100      * Gets the volume control type that this volume provider uses.
101      *
102      * <p>One of {@link #VOLUME_CONTROL_FIXED}, {@link #VOLUME_CONTROL_ABSOLUTE}, or {@link
103      * #VOLUME_CONTROL_RELATIVE}.
104      *
105      * @return The volume control type for this volume provider
106      */
107     @ControlType
getVolumeControl()108     public final int getVolumeControl() {
109         return mControlType;
110     }
111 
112     /**
113      * Gets the maximum volume this provider allows.
114      *
115      * @return The max allowed volume.
116      */
getMaxVolume()117     public final int getMaxVolume() {
118         return mMaxVolume;
119     }
120 
121     /**
122      * Gets the current volume. This will be the last value set by
123      * {@link #setCurrentVolume(int)}.
124      *
125      * @return The current volume.
126      */
getCurrentVolume()127     public final int getCurrentVolume() {
128         return mCurrentVolume;
129     }
130 
131     /**
132      * Notifies the system that the current volume has been changed. This must be called every time
133      * the volume changes to ensure it is displayed properly.
134      *
135      * @param currentVolume The current volume on the output.
136      */
setCurrentVolume(int currentVolume)137     public final void setCurrentVolume(int currentVolume) {
138         mCurrentVolume = currentVolume;
139         if (mCallback != null) {
140             mCallback.onVolumeChanged(this);
141         }
142     }
143 
144     /**
145      * Gets the {@link RoutingController#getId() routing controller id} of the {@link
146      * RoutingController} associated with this volume provider, or null if unset.
147      *
148      * <p>This id allows mapping this volume provider to a routing controller, which provides
149      * information about the media route and allows controlling its volume.
150      */
151     @Nullable
getVolumeControlId()152     public final String getVolumeControlId() {
153         return mControlId;
154     }
155 
156     /**
157      * Override to handle requests to set the volume of the current output.
158      * After the volume has been modified {@link #setCurrentVolume} must be
159      * called to notify the system.
160      *
161      * @param volume The volume to set the output to.
162      */
onSetVolumeTo(int volume)163     public void onSetVolumeTo(int volume) {
164     }
165 
166     /**
167      * Override to handle requests to adjust the volume of the current output.
168      * Direction will be one of {@link AudioManager#ADJUST_LOWER},
169      * {@link AudioManager#ADJUST_RAISE}, {@link AudioManager#ADJUST_SAME}.
170      * After the volume has been modified {@link #setCurrentVolume} must be
171      * called to notify the system.
172      *
173      * @param direction The direction to change the volume in.
174      */
onAdjustVolume(int direction)175     public void onAdjustVolume(int direction) {
176     }
177 
178     /**
179      * Sets a callback to receive volume changes.
180      * @hide
181      */
setCallback(Callback callback)182     public void setCallback(Callback callback) {
183         mCallback = callback;
184     }
185 
186     /**
187      * Listens for changes to the volume.
188      * @hide
189      */
190     public abstract static class Callback {
191         /**
192          * Called when volume changed.
193          */
onVolumeChanged(VolumeProvider volumeProvider)194         public abstract void onVolumeChanged(VolumeProvider volumeProvider);
195     }
196 }
197