1 /* 2 * Copyright 2018 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 android.media; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.media.update.ApiLoader; 22 import android.media.update.VolumeProvider2Provider; 23 24 import java.lang.annotation.Retention; 25 import java.lang.annotation.RetentionPolicy; 26 27 /** 28 * @hide 29 * Handles requests to adjust or set the volume on a session. This is also used 30 * to push volume updates back to the session. The provider must call 31 * {@link #setCurrentVolume(int)} each time the volume being provided changes. 32 * <p> 33 * You can set a volume provider on a session by calling 34 * {@link MediaSession2#updatePlayer}. 35 */ 36 // New version of VolumeProvider with following changes 37 // - Don't implement Parcelable for updatable support. 38 public abstract class VolumeProvider2 { 39 /** 40 * @hide 41 */ 42 @IntDef({VOLUME_CONTROL_FIXED, VOLUME_CONTROL_RELATIVE, VOLUME_CONTROL_ABSOLUTE}) 43 @Retention(RetentionPolicy.SOURCE) 44 public @interface ControlType {} 45 46 /** 47 * The volume is fixed and can not be modified. Requests to change volume 48 * should be ignored. 49 */ 50 public static final int VOLUME_CONTROL_FIXED = 0; 51 52 /** 53 * The volume control uses relative adjustment via 54 * {@link #onAdjustVolume(int)}. Attempts to set the volume to a specific 55 * value should be ignored. 56 */ 57 public static final int VOLUME_CONTROL_RELATIVE = 1; 58 59 /** 60 * The volume control uses an absolute value. It may be adjusted using 61 * {@link #onAdjustVolume(int)} or set directly using 62 * {@link #onSetVolumeTo(int)}. 63 */ 64 public static final int VOLUME_CONTROL_ABSOLUTE = 2; 65 66 private final VolumeProvider2Provider mProvider; 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 controlType The method for controlling volume that is used by this provider. 74 * @param maxVolume The maximum allowed volume. 75 * @param currentVolume The current volume on the output. 76 */ 77 public VolumeProvider2(@ControlType int controlType, int maxVolume, int currentVolume) { 78 mProvider = ApiLoader.getProvider().createVolumeProvider2( 79 this, controlType, maxVolume, currentVolume); 80 } 81 82 /** 83 * @hide 84 */ 85 public VolumeProvider2Provider getProvider() { 86 return mProvider; 87 } 88 89 /** 90 * Get the volume control type that this volume provider uses. 91 * 92 * @return The volume control type for this volume provider 93 */ 94 @ControlType 95 public final int getControlType() { 96 return mProvider.getControlType_impl(); 97 } 98 99 /** 100 * Get the maximum volume this provider allows. 101 * 102 * @return The max allowed volume. 103 */ 104 public final int getMaxVolume() { 105 return mProvider.getMaxVolume_impl(); 106 } 107 108 /** 109 * Gets the current volume. This will be the last value set by 110 * {@link #setCurrentVolume(int)}. 111 * 112 * @return The current volume. 113 */ 114 public final int getCurrentVolume() { 115 return mProvider.getCurrentVolume_impl(); 116 } 117 118 /** 119 * Notify the system that the current volume has been changed. This must be 120 * called every time the volume changes to ensure it is displayed properly. 121 * 122 * @param currentVolume The current volume on the output. 123 */ 124 public final void setCurrentVolume(int currentVolume) { 125 mProvider.setCurrentVolume_impl(currentVolume); 126 } 127 128 /** 129 * Override to handle requests to set the volume of the current output. 130 * After the volume has been modified {@link #setCurrentVolume} must be 131 * called to notify the system. 132 * 133 * @param volume The volume to set the output to. 134 */ 135 public void onSetVolumeTo(int volume) { } 136 137 /** 138 * Override to handle requests to adjust the volume of the current output. 139 * Direction will be one of {@link AudioManager#ADJUST_LOWER}, 140 * {@link AudioManager#ADJUST_RAISE}, {@link AudioManager#ADJUST_SAME}. 141 * After the volume has been modified {@link #setCurrentVolume} must be 142 * called to notify the system. 143 * 144 * @param direction The direction to change the volume in. 145 */ 146 public void onAdjustVolume(int direction) { } 147 } 148