1c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik/* 2c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * Copyright (C) 2014 The Android Open Source Project 3c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * 4c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * Licensed under the Apache License, Version 2.0 (the "License"); 5c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * you may not use this file except in compliance with the License. 6c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * You may obtain a copy of the License at 7c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * 8c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * http://www.apache.org/licenses/LICENSE-2.0 9c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * 10c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * Unless required by applicable law or agreed to in writing, software 11c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * distributed under the License is distributed on an "AS IS" BASIS, 12c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * See the License for the specific language governing permissions and 14c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * limitations under the License. 15c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik */ 16ef3c9e9b057a5aac2d0d012e8e6385660478e203RoboErikpackage android.media; 1733983a901176adcc16c820444b667a37e6472243RoboErik 187c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kangimport android.annotation.IntDef; 19ef3c9e9b057a5aac2d0d012e8e6385660478e203RoboErikimport android.media.session.MediaSession; 205d3114b64a88ac1f72becd8d46f148c666f64aa3RoboErik 217c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kangimport java.lang.annotation.Retention; 227c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kangimport java.lang.annotation.RetentionPolicy; 237c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang 2433983a901176adcc16c820444b667a37e6472243RoboErik/** 2533983a901176adcc16c820444b667a37e6472243RoboErik * Handles requests to adjust or set the volume on a session. This is also used 260d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * to push volume updates back to the session. The provider must call 270d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * {@link #setCurrentVolume(int)} each time the volume being provided changes. 280d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * <p> 2933983a901176adcc16c820444b667a37e6472243RoboErik * You can set a volume provider on a session by calling 3079fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik * {@link MediaSession#setPlaybackToRemote}. 3133983a901176adcc16c820444b667a37e6472243RoboErik */ 32ef3c9e9b057a5aac2d0d012e8e6385660478e203RoboErikpublic abstract class VolumeProvider { 337c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang 347c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang /** 357c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang * @hide 367c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang */ 377c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang @IntDef({VOLUME_CONTROL_FIXED, VOLUME_CONTROL_RELATIVE, VOLUME_CONTROL_ABSOLUTE}) 387c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang @Retention(RetentionPolicy.SOURCE) 397c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang public @interface ControlType {} 407c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang 4133983a901176adcc16c820444b667a37e6472243RoboErik /** 4279fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik * The volume is fixed and can not be modified. Requests to change volume 4379fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik * should be ignored. 4433983a901176adcc16c820444b667a37e6472243RoboErik */ 45c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik public static final int VOLUME_CONTROL_FIXED = 0; 4633983a901176adcc16c820444b667a37e6472243RoboErik 4733983a901176adcc16c820444b667a37e6472243RoboErik /** 4879fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik * The volume control uses relative adjustment via 491ff5b1648a051e9650614f0c0f1b3f449777db81RoboErik * {@link #onAdjustVolume(int)}. Attempts to set the volume to a specific 5079fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik * value should be ignored. 5133983a901176adcc16c820444b667a37e6472243RoboErik */ 52c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik public static final int VOLUME_CONTROL_RELATIVE = 1; 5379fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik 5479fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik /** 5579fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik * The volume control uses an absolute value. It may be adjusted using 561ff5b1648a051e9650614f0c0f1b3f449777db81RoboErik * {@link #onAdjustVolume(int)} or set directly using 5779fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik * {@link #onSetVolumeTo(int)}. 5879fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik */ 59c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik public static final int VOLUME_CONTROL_ABSOLUTE = 2; 6033983a901176adcc16c820444b667a37e6472243RoboErik 61c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik private final int mControlType; 6233983a901176adcc16c820444b667a37e6472243RoboErik private final int mMaxVolume; 630d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik private int mCurrentVolume; 64bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown private Callback mCallback; 655d3114b64a88ac1f72becd8d46f148c666f64aa3RoboErik 6633983a901176adcc16c820444b667a37e6472243RoboErik /** 6733983a901176adcc16c820444b667a37e6472243RoboErik * Create a new volume provider for handling volume events. You must specify 680d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * the type of volume control, the maximum volume that can be used, and the 690d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * current volume on the output. 7033983a901176adcc16c820444b667a37e6472243RoboErik * 7179fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik * @param volumeControl The method for controlling volume that is used by 7279fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik * this provider. 7333983a901176adcc16c820444b667a37e6472243RoboErik * @param maxVolume The maximum allowed volume. 740d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * @param currentVolume The current volume on the output. 7533983a901176adcc16c820444b667a37e6472243RoboErik */ 767c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang public VolumeProvider(@ControlType int volumeControl, int maxVolume, int currentVolume) { 77c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik mControlType = volumeControl; 7833983a901176adcc16c820444b667a37e6472243RoboErik mMaxVolume = maxVolume; 790d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik mCurrentVolume = currentVolume; 8033983a901176adcc16c820444b667a37e6472243RoboErik } 8133983a901176adcc16c820444b667a37e6472243RoboErik 8233983a901176adcc16c820444b667a37e6472243RoboErik /** 83c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * Get the volume control type that this volume provider uses. 8433983a901176adcc16c820444b667a37e6472243RoboErik * 85c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik * @return The volume control type for this volume provider 8633983a901176adcc16c820444b667a37e6472243RoboErik */ 877c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang @ControlType 88c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik public final int getVolumeControl() { 89c47fa84b0a6bda48c38ba8822481ce613bafd019RoboErik return mControlType; 9033983a901176adcc16c820444b667a37e6472243RoboErik } 9133983a901176adcc16c820444b667a37e6472243RoboErik 9233983a901176adcc16c820444b667a37e6472243RoboErik /** 9333983a901176adcc16c820444b667a37e6472243RoboErik * Get the maximum volume this provider allows. 9433983a901176adcc16c820444b667a37e6472243RoboErik * 9533983a901176adcc16c820444b667a37e6472243RoboErik * @return The max allowed volume. 9633983a901176adcc16c820444b667a37e6472243RoboErik */ 9733983a901176adcc16c820444b667a37e6472243RoboErik public final int getMaxVolume() { 9833983a901176adcc16c820444b667a37e6472243RoboErik return mMaxVolume; 9933983a901176adcc16c820444b667a37e6472243RoboErik } 10033983a901176adcc16c820444b667a37e6472243RoboErik 10133983a901176adcc16c820444b667a37e6472243RoboErik /** 1020d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * Gets the current volume. This will be the last value set by 1030d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * {@link #setCurrentVolume(int)}. 1040d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * 1050d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * @return The current volume. 10633983a901176adcc16c820444b667a37e6472243RoboErik */ 1070d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik public final int getCurrentVolume() { 1080d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik return mCurrentVolume; 1090d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik } 1100d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik 1110d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik /** 1120d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * Notify the system that the current volume has been changed. This must be 1130d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * called every time the volume changes to ensure it is displayed properly. 1140d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * 1150d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * @param currentVolume The current volume on the output. 1160d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik */ 1170d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik public final void setCurrentVolume(int currentVolume) { 1180d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik mCurrentVolume = currentVolume; 119bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown if (mCallback != null) { 120bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown mCallback.onVolumeChanged(this); 121bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown } 12233983a901176adcc16c820444b667a37e6472243RoboErik } 12333983a901176adcc16c820444b667a37e6472243RoboErik 12433983a901176adcc16c820444b667a37e6472243RoboErik /** 12533983a901176adcc16c820444b667a37e6472243RoboErik * Override to handle requests to set the volume of the current output. 1260d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * After the volume has been modified {@link #setCurrentVolume} must be 1270d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * called to notify the system. 12833983a901176adcc16c820444b667a37e6472243RoboErik * 12933983a901176adcc16c820444b667a37e6472243RoboErik * @param volume The volume to set the output to. 13033983a901176adcc16c820444b667a37e6472243RoboErik */ 13179fa4630bbca7c6c251eea99fe8997e4b45beceeRoboErik public void onSetVolumeTo(int volume) { 13233983a901176adcc16c820444b667a37e6472243RoboErik } 13333983a901176adcc16c820444b667a37e6472243RoboErik 13433983a901176adcc16c820444b667a37e6472243RoboErik /** 1351ff5b1648a051e9650614f0c0f1b3f449777db81RoboErik * Override to handle requests to adjust the volume of the current output. 1361ff5b1648a051e9650614f0c0f1b3f449777db81RoboErik * Direction will be one of {@link AudioManager#ADJUST_LOWER}, 1371ff5b1648a051e9650614f0c0f1b3f449777db81RoboErik * {@link AudioManager#ADJUST_RAISE}, {@link AudioManager#ADJUST_SAME}. 1380d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * After the volume has been modified {@link #setCurrentVolume} must be 1390d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * called to notify the system. 1400d0f67f5ee5f939a1b611bc4583212707afd9beeRoboErik * 1411ff5b1648a051e9650614f0c0f1b3f449777db81RoboErik * @param direction The direction to change the volume in. 14233983a901176adcc16c820444b667a37e6472243RoboErik */ 1431ff5b1648a051e9650614f0c0f1b3f449777db81RoboErik public void onAdjustVolume(int direction) { 14433983a901176adcc16c820444b667a37e6472243RoboErik } 1455d3114b64a88ac1f72becd8d46f148c666f64aa3RoboErik 1465d3114b64a88ac1f72becd8d46f148c666f64aa3RoboErik /** 147bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown * Sets a callback to receive volume changes. 148bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown * @hide 149bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown */ 150bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown public void setCallback(Callback callback) { 151bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown mCallback = callback; 152bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown } 153bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown 154bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown /** 155bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown * Listens for changes to the volume. 1565d3114b64a88ac1f72becd8d46f148c666f64aa3RoboErik * @hide 1575d3114b64a88ac1f72becd8d46f148c666f64aa3RoboErik */ 158bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown public static abstract class Callback { 159bf58d9b727f1007c7c620f622ac1d8003b1b211bJeff Brown public abstract void onVolumeChanged(VolumeProvider volumeProvider); 1605d3114b64a88ac1f72becd8d46f148c666f64aa3RoboErik } 1617c090d54e2c0eb5309d3f7dc131e137d9c986793Insun Kang} 162