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.NonNull; 20 import android.annotation.Nullable; 21 import android.content.Context; 22 import android.media.update.ApiLoader; 23 import android.media.update.MediaSession2Provider; 24 import android.media.MediaSession2.ControllerInfo; 25 import android.media.MediaSession2.SessionCallback; 26 import android.net.Uri; 27 import android.os.Bundle; 28 29 import java.util.List; 30 31 /** 32 * @hide 33 * Define a command that a {@link MediaController2} can send to a {@link MediaSession2}. 34 * <p> 35 * If {@link #getCommandCode()} isn't {@link #COMMAND_CODE_CUSTOM}), it's predefined command. 36 * If {@link #getCommandCode()} is {@link #COMMAND_CODE_CUSTOM}), it's custom command and 37 * {@link #getCustomCommand()} shouldn't be {@code null}. 38 */ 39 public final class SessionCommand2 { 40 /** 41 * Command code for the custom command which can be defined by string action in the 42 * {@link SessionCommand2}. 43 */ 44 public static final int COMMAND_CODE_CUSTOM = 0; 45 46 /** 47 * Command code for {@link MediaController2#play()}. 48 * <p> 49 * Command would be sent directly to the player if the session doesn't reject the request 50 * through the {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, 51 * SessionCommand2)}. 52 */ 53 public static final int COMMAND_CODE_PLAYBACK_PLAY = 1; 54 55 /** 56 * Command code for {@link MediaController2#pause()}. 57 * <p> 58 * Command would be sent directly to the player if the session doesn't reject the request 59 * through the {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, 60 * SessionCommand2)}. 61 */ 62 public static final int COMMAND_CODE_PLAYBACK_PAUSE = 2; 63 64 /** 65 * Command code for {@link MediaController2#stop()}. 66 * <p> 67 * Command would be sent directly to the player if the session doesn't reject the request 68 * through the {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, 69 * SessionCommand2)}. 70 */ 71 public static final int COMMAND_CODE_PLAYBACK_STOP = 3; 72 73 /** 74 * Command code for {@link MediaController2#skipToNextItem()}. 75 * <p> 76 * Command would be sent directly to the playlist agent if the session doesn't reject the 77 * request through the {@link SessionCallback#onCommandRequest( 78 * MediaSession2, ControllerInfo, SessionCommand2)}. 79 */ 80 public static final int COMMAND_CODE_PLAYLIST_SKIP_NEXT_ITEM = 4; 81 82 /** 83 * Command code for {@link MediaController2#skipToPreviousItem()}. 84 * <p> 85 * Command would be sent directly to the playlist agent if the session doesn't reject the 86 * request through the {@link SessionCallback#onCommandRequest( 87 * MediaSession2, ControllerInfo, SessionCommand2)}. 88 */ 89 public static final int COMMAND_CODE_PLAYLIST_SKIP_PREV_ITEM = 5; 90 91 /** 92 * Command code for {@link MediaController2#prepare()}. 93 * <p> 94 * Command would be sent directly to the player if the session doesn't reject the request 95 * through the {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, 96 * SessionCommand2)}. 97 */ 98 public static final int COMMAND_CODE_PLAYBACK_PREPARE = 6; 99 100 /** 101 * Command code for {@link MediaController2#fastForward()}. 102 */ 103 public static final int COMMAND_CODE_SESSION_FAST_FORWARD = 7; 104 105 /** 106 * Command code for {@link MediaController2#rewind()}. 107 */ 108 public static final int COMMAND_CODE_SESSION_REWIND = 8; 109 110 /** 111 * Command code for {@link MediaController2#seekTo(long)}. 112 * <p> 113 * Command would be sent directly to the player if the session doesn't reject the request 114 * through the {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, 115 * SessionCommand2)}. 116 */ 117 public static final int COMMAND_CODE_PLAYBACK_SEEK_TO = 9; 118 119 /** 120 * Command code for both {@link MediaController2#setVolumeTo(int, int)}. 121 * <p> 122 * Command would set the device volume or send to the volume provider directly if the session 123 * doesn't reject the request through the 124 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 125 */ 126 public static final int COMMAND_CODE_SET_VOLUME = 10; 127 128 /** 129 * Command code for both {@link MediaController2#adjustVolume(int, int)}. 130 * <p> 131 * Command would adjust the device volume or send to the volume provider directly if the session 132 * doesn't reject the request through the 133 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 134 */ 135 public static final int COMMAND_CODE_ADJUST_VOLUME = 11; 136 137 /** 138 * Command code for {@link MediaController2#skipToPlaylistItem(MediaItem2)}. 139 * <p> 140 * Command would be sent directly to the playlist agent if the session doesn't reject the 141 * request through the 142 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 143 */ 144 public static final int COMMAND_CODE_PLAYLIST_SKIP_TO_PLAYLIST_ITEM = 12; 145 146 /** 147 * Command code for {@link MediaController2#setShuffleMode(int)}. 148 * <p> 149 * Command would be sent directly to the playlist agent if the session doesn't reject the 150 * request through the 151 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 152 */ 153 public static final int COMMAND_CODE_PLAYLIST_SET_SHUFFLE_MODE = 13; 154 155 /** 156 * Command code for {@link MediaController2#setRepeatMode(int)}. 157 * <p> 158 * Command would be sent directly to the playlist agent if the session doesn't reject the 159 * request through the 160 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 161 */ 162 public static final int COMMAND_CODE_PLAYLIST_SET_REPEAT_MODE = 14; 163 164 /** 165 * Command code for {@link MediaController2#addPlaylistItem(int, MediaItem2)}. 166 * <p> 167 * Command would be sent directly to the playlist agent if the session doesn't reject the 168 * request through the 169 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 170 */ 171 public static final int COMMAND_CODE_PLAYLIST_ADD_ITEM = 15; 172 173 /** 174 * Command code for {@link MediaController2#addPlaylistItem(int, MediaItem2)}. 175 * <p> 176 * Command would be sent directly to the playlist agent if the session doesn't reject the 177 * request through the 178 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 179 */ 180 public static final int COMMAND_CODE_PLAYLIST_REMOVE_ITEM = 16; 181 182 /** 183 * Command code for {@link MediaController2#replacePlaylistItem(int, MediaItem2)}. 184 * <p> 185 * Command would be sent directly to the playlist agent if the session doesn't reject the 186 * request through the 187 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 188 */ 189 public static final int COMMAND_CODE_PLAYLIST_REPLACE_ITEM = 17; 190 191 /** 192 * Command code for {@link MediaController2#getPlaylist()}. This will expose metadata 193 * information to the controller. 194 * <p> 195 * Command would be sent directly to the playlist agent if the session doesn't reject the 196 * request through the 197 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 198 */ 199 public static final int COMMAND_CODE_PLAYLIST_GET_LIST = 18; 200 201 /** 202 * Command code for {@link MediaController2#setPlaylist(List, MediaMetadata2)}. 203 * <p> 204 * Command would be sent directly to the playlist agent if the session doesn't reject the 205 * request through the 206 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 207 */ 208 public static final int COMMAND_CODE_PLAYLIST_SET_LIST = 19; 209 210 /** 211 * Command code for {@link MediaController2#getPlaylistMetadata()}. This will expose 212 * metadata information to the controller. 213 * <p> 214 * Command would be sent directly to the playlist agent if the session doesn't reject the 215 * request through the 216 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 217 */ 218 public static final int COMMAND_CODE_PLAYLIST_GET_LIST_METADATA = 20; 219 220 /** 221 * Command code for {@link MediaController2#updatePlaylistMetadata(MediaMetadata2)}. 222 * <p> 223 * Command would be sent directly to the playlist agent if the session doesn't reject the 224 * request through the 225 * {@link SessionCallback#onCommandRequest(MediaSession2, ControllerInfo, SessionCommand2)}. 226 */ 227 public static final int COMMAND_CODE_PLAYLIST_SET_LIST_METADATA = 21; 228 229 /** 230 * Command code for {@link MediaController2#playFromMediaId(String, Bundle)}. 231 */ 232 public static final int COMMAND_CODE_SESSION_PLAY_FROM_MEDIA_ID = 22; 233 234 /** 235 * Command code for {@link MediaController2#playFromUri(Uri, Bundle)}. 236 */ 237 public static final int COMMAND_CODE_SESSION_PLAY_FROM_URI = 23; 238 239 /** 240 * Command code for {@link MediaController2#playFromSearch(String, Bundle)}. 241 */ 242 public static final int COMMAND_CODE_SESSION_PLAY_FROM_SEARCH = 24; 243 244 /** 245 * Command code for {@link MediaController2#prepareFromMediaId(String, Bundle)}. 246 */ 247 public static final int COMMAND_CODE_SESSION_PREPARE_FROM_MEDIA_ID = 25; 248 249 /** 250 * Command code for {@link MediaController2#prepareFromUri(Uri, Bundle)}. 251 */ 252 public static final int COMMAND_CODE_SESSION_PREPARE_FROM_URI = 26; 253 254 /** 255 * Command code for {@link MediaController2#prepareFromSearch(String, Bundle)}. 256 */ 257 public static final int COMMAND_CODE_SESSION_PREPARE_FROM_SEARCH = 27; 258 259 /** 260 * Command code for {@link MediaController2#setRating(String, Rating2)}. 261 */ 262 public static final int COMMAND_CODE_SESSION_SET_RATING = 28; 263 264 // TODO(jaewan): Add javadoc 265 public static final int COMMAND_CODE_LIBRARY_GET_CHILDREN = 29; 266 public static final int COMMAND_CODE_LIBRARY_GET_ITEM = 30; 267 public static final int COMMAND_CODE_LIBRARY_GET_LIBRARY_ROOT = 31; 268 public static final int COMMAND_CODE_LIBRARY_GET_SEARCH_RESULT = 32; 269 public static final int COMMAND_CODE_LIBRARY_SEARCH = 33; 270 public static final int COMMAND_CODE_LIBRARY_SUBSCRIBE = 34; 271 public static final int COMMAND_CODE_LIBRARY_UNSUBSCRIBE = 35; 272 273 // TODO(jaewan): Rename and move provider 274 private final MediaSession2Provider.CommandProvider mProvider; 275 276 public SessionCommand2(int commandCode) { 277 mProvider = ApiLoader.getProvider().createMediaSession2Command( 278 this, commandCode, null, null); 279 } 280 281 public SessionCommand2(@NonNull String action, @Nullable Bundle extras) { 282 if (action == null) { 283 throw new IllegalArgumentException("action shouldn't be null"); 284 } 285 mProvider = ApiLoader.getProvider().createMediaSession2Command( 286 this, COMMAND_CODE_CUSTOM, action, extras); 287 } 288 289 /** 290 * @hide 291 */ 292 public MediaSession2Provider.CommandProvider getProvider() { 293 return mProvider; 294 } 295 296 public int getCommandCode() { 297 return mProvider.getCommandCode_impl(); 298 } 299 300 public @Nullable String getCustomCommand() { 301 return mProvider.getCustomCommand_impl(); 302 } 303 304 public @Nullable Bundle getExtras() { 305 return mProvider.getExtras_impl(); 306 } 307 308 /** 309 * @return a new Bundle instance from the Command 310 * @hide 311 */ 312 public Bundle toBundle() { 313 return mProvider.toBundle_impl(); 314 } 315 316 @Override 317 public boolean equals(Object obj) { 318 if (!(obj instanceof SessionCommand2)) { 319 return false; 320 } 321 return mProvider.equals_impl(((SessionCommand2) obj).mProvider); 322 } 323 324 @Override 325 public int hashCode() { 326 return mProvider.hashCode_impl(); 327 } 328 329 /** 330 * @return a new Command instance from the Bundle 331 * @hide 332 */ 333 public static SessionCommand2 fromBundle(@NonNull Bundle command) { 334 return ApiLoader.getProvider().fromBundle_MediaSession2Command(command); 335 } 336 } 337