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.session.MediaSession; 21 22 import java.lang.annotation.Retention; 23 import java.lang.annotation.RetentionPolicy; 24 25 /** 26 * Handles requests to adjust or set the volume on a session. This is also used 27 * to push volume updates back to the session. The provider must call 28 * {@link #setCurrentVolume(int)} each time the volume being provided changes. 29 * <p> 30 * You can set a volume provider on a session by calling 31 * {@link MediaSession#setPlaybackToRemote}. 32 */ 33 public abstract class VolumeProvider { 34 35 /** 36 * @hide 37 */ 38 @IntDef({VOLUME_CONTROL_FIXED, VOLUME_CONTROL_RELATIVE, VOLUME_CONTROL_ABSOLUTE}) 39 @Retention(RetentionPolicy.SOURCE) 40 public @interface ControlType {} 41 42 /** 43 * The volume is fixed and can not be modified. Requests to change volume 44 * should be ignored. 45 */ 46 public static final int VOLUME_CONTROL_FIXED = 0; 47 48 /** 49 * The volume control uses relative adjustment via 50 * {@link #onAdjustVolume(int)}. Attempts to set the volume to a specific 51 * value should be ignored. 52 */ 53 public static final int VOLUME_CONTROL_RELATIVE = 1; 54 55 /** 56 * The volume control uses an absolute value. It may be adjusted using 57 * {@link #onAdjustVolume(int)} or set directly using 58 * {@link #onSetVolumeTo(int)}. 59 */ 60 public static final int VOLUME_CONTROL_ABSOLUTE = 2; 61 62 private final int mControlType; 63 private final int mMaxVolume; 64 private final String mControlId; 65 private int mCurrentVolume; 66 private Callback mCallback; 67 68 /** 69 * Create a new volume provider for handling volume events. You must specify 70 * the type of volume control, the maximum volume that can be used, and the 71 * current volume on the output. 72 * 73 * @param volumeControl The method for controlling volume that is used by 74 * this provider. 75 * @param maxVolume The maximum allowed volume. 76 * @param currentVolume The current volume on the output. 77 */ 78 VolumeProvider(@ontrolType int volumeControl, int maxVolume, int currentVolume)79 public VolumeProvider(@ControlType int volumeControl, int maxVolume, int currentVolume) { 80 this(volumeControl, maxVolume, currentVolume, null); 81 } 82 83 /** 84 * Create a new volume provider for handling volume events. You must specify 85 * the type of volume control, the maximum volume that can be used, and the 86 * current volume on the output. 87 * 88 * @param volumeControl The method for controlling volume that is used by 89 * this provider. 90 * @param maxVolume The maximum allowed volume. 91 * @param currentVolume The current volume on the output. 92 * @param volumeControlId The volume control ID of this provider. 93 */ VolumeProvider(@ontrolType int volumeControl, int maxVolume, int currentVolume, @Nullable String volumeControlId)94 public VolumeProvider(@ControlType int volumeControl, int maxVolume, int currentVolume, 95 @Nullable String volumeControlId) { 96 mControlType = volumeControl; 97 mMaxVolume = maxVolume; 98 mCurrentVolume = currentVolume; 99 mControlId = volumeControlId; 100 } 101 102 /** 103 * Get the volume control type that this volume provider uses. 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 * Get 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 * Notify the system that the current volume has been changed. This must be 133 * called every time 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 volume control ID. It can be used to identify which volume provider is 146 * used by the session. 147 * 148 * @return the volume control ID or {@code null} if it isn't set. 149 */ 150 @Nullable getVolumeControlId()151 public final String getVolumeControlId() { 152 return mControlId; 153 } 154 155 /** 156 * Override to handle requests to set the volume of the current output. 157 * After the volume has been modified {@link #setCurrentVolume} must be 158 * called to notify the system. 159 * 160 * @param volume The volume to set the output to. 161 */ onSetVolumeTo(int volume)162 public void onSetVolumeTo(int volume) { 163 } 164 165 /** 166 * Override to handle requests to adjust the volume of the current output. 167 * Direction will be one of {@link AudioManager#ADJUST_LOWER}, 168 * {@link AudioManager#ADJUST_RAISE}, {@link AudioManager#ADJUST_SAME}. 169 * After the volume has been modified {@link #setCurrentVolume} must be 170 * called to notify the system. 171 * 172 * @param direction The direction to change the volume in. 173 */ onAdjustVolume(int direction)174 public void onAdjustVolume(int direction) { 175 } 176 177 /** 178 * Sets a callback to receive volume changes. 179 * @hide 180 */ setCallback(Callback callback)181 public void setCallback(Callback callback) { 182 mCallback = callback; 183 } 184 185 /** 186 * Listens for changes to the volume. 187 * @hide 188 */ 189 public abstract static class Callback { 190 /** 191 * Called when volume changed. 192 */ onVolumeChanged(VolumeProvider volumeProvider)193 public abstract void onVolumeChanged(VolumeProvider volumeProvider); 194 } 195 } 196