Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 1 | /* |
Insun Kang | 930b199 | 2019-04-19 19:07:12 +0900 | [diff] [blame] | 2 | * Copyright 2019 The Android Open Source Project |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 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 | |
Insun Kang | 930b199 | 2019-04-19 19:07:12 +0900 | [diff] [blame] | 17 | package androidx.media2.session; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 18 | |
Insun Kang | 930b199 | 2019-04-19 19:07:12 +0900 | [diff] [blame] | 19 | import static androidx.media2.session.LibraryResult.RESULT_ERROR_NOT_SUPPORTED; |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 20 | |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 21 | import android.app.PendingIntent; |
Jaewan Kim | 4832954 | 2018-05-14 10:41:10 +0900 | [diff] [blame] | 22 | import android.content.Context; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 23 | import android.content.Intent; |
Jaewan Kim | a25a835 | 2018-06-16 14:03:12 +0900 | [diff] [blame] | 24 | import android.media.browse.MediaBrowser; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.os.IBinder; |
Hyundo Moon | 10daf4c | 2018-10-26 20:18:26 +0900 | [diff] [blame] | 27 | import android.text.TextUtils; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 28 | |
Hyundo Moon | 10daf4c | 2018-10-26 20:18:26 +0900 | [diff] [blame] | 29 | import androidx.annotation.IntRange; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 30 | import androidx.annotation.NonNull; |
| 31 | import androidx.annotation.Nullable; |
Insun Kang | de8fad1 | 2018-08-31 15:28:09 +0900 | [diff] [blame] | 32 | import androidx.core.content.ContextCompat; |
Jaewan Kim | fa9f974 | 2020-04-13 22:05:18 +0900 | [diff] [blame^] | 33 | import androidx.media.MediaSessionManager.RemoteUserInfo; |
Insun Kang | 930b199 | 2019-04-19 19:07:12 +0900 | [diff] [blame] | 34 | import androidx.media2.common.MediaMetadata; |
| 35 | import androidx.media2.common.SessionPlayer; |
| 36 | import androidx.media2.session.LibraryResult.ResultCode; |
| 37 | import androidx.media2.session.MediaSession.ControllerInfo; |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 38 | import androidx.versionedparcelable.ParcelField; |
| 39 | import androidx.versionedparcelable.VersionedParcelable; |
| 40 | import androidx.versionedparcelable.VersionedParcelize; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 41 | |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 42 | import java.util.concurrent.Executor; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 43 | |
| 44 | /** |
Jaewan Kim | 9ca6bb1 | 2018-08-07 16:33:57 +0900 | [diff] [blame] | 45 | * Base class for media library services, which is the service containing |
| 46 | * {@link MediaLibrarySession}. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 47 | * <p> |
| 48 | * Media library services enable applications to browse media content provided by an application |
| 49 | * and ask the application to start playing it. They may also be used to control content that |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 50 | * is already playing by way of a {@link MediaSession}. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 51 | * <p> |
| 52 | * When extending this class, also add the following to your {@code AndroidManifest.xml}. |
| 53 | * <pre> |
| 54 | * <service android:name="component_name_of_your_implementation" > |
| 55 | * <intent-filter> |
Insun Kang | 930b199 | 2019-04-19 19:07:12 +0900 | [diff] [blame] | 56 | * <action android:name="androidx.media2.session.MediaLibraryService" /> |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 57 | * </intent-filter> |
| 58 | * </service></pre> |
Jaewan Kim | d917b8d | 2018-08-08 14:51:48 +0900 | [diff] [blame] | 59 | * <p> |
| 60 | * You may also declare <pre>android.media.browse.MediaBrowserService</pre> for compatibility with |
| 61 | * {@link android.support.v4.media.MediaBrowserCompat}. This service can handle it automatically. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 62 | * |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 63 | * @see MediaSessionService |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 64 | */ |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 65 | public abstract class MediaLibraryService extends MediaSessionService { |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 66 | /** |
Sungsoo Lim | 6bb9310 | 2018-07-17 13:02:23 +0900 | [diff] [blame] | 67 | * The {@link Intent} that must be declared as handled by the service. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 68 | */ |
Insun Kang | 930b199 | 2019-04-19 19:07:12 +0900 | [diff] [blame] | 69 | public static final String SERVICE_INTERFACE = "androidx.media2.session.MediaLibraryService"; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 70 | |
| 71 | /** |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 72 | * Session for the {@link MediaLibraryService}. Build this object with |
Hyundo Moon | 17552c7 | 2019-04-19 00:51:02 +0900 | [diff] [blame] | 73 | * {@link Builder} and return in {@link MediaSessionService#onGetSession(ControllerInfo)}. |
Jaewan Kim | fa9f974 | 2020-04-13 22:05:18 +0900 | [diff] [blame^] | 74 | * |
| 75 | * <h3 id="BackwardCompatibility">Backward compatibility with legacy media browser APIs</h3> |
| 76 | * Media library session supports connection from both {@link MediaBrowser} and |
| 77 | * {@link android.support.v4.media.MediaBrowserCompat}, but {@link ControllerInfo} may not be |
| 78 | * precise. Here are current limitations with details. |
| 79 | * |
| 80 | * <table> |
| 81 | * <tr><th>SDK version</th> |
| 82 | * <th>{@link ControllerInfo#getPackageName()}<br>for legacy browser</th> |
| 83 | * <th>{@link ControllerInfo#getUid()}<br>for legacy browser</th></tr> |
| 84 | * <tr><td>{@code SDK_VERSION} < 21</td> |
| 85 | * <td>Actual package name via {@link Context#getPackageName()}</td> |
| 86 | * <td>Actual UID</td></tr> |
| 87 | * <tr><td>21 ≥ {@code SDK_VERSION} < 28,<br> |
| 88 | * {@code MediaLibrarySessionCallback#onConnect} and<br> |
| 89 | * {@code MediaLibrarySessionCallback#onGetLibraryRoot}</td> |
| 90 | * <td>Actual package name via {@link Context#getPackageName()}</td> |
| 91 | * <td>Actual UID</td></tr> |
| 92 | * <tr><td>21 ≥ {@code SDK_VERSION} < 28, for other callbacks</td> |
| 93 | * <td>{@link RemoteUserInfo#LEGACY_CONTROLLER}</td> |
| 94 | * <td>Negative value</td></tr> |
| 95 | * <tr><td>28 ≥ {@code SDK_VERSION}</td> |
| 96 | * <td>Actual package name via {@link Context#getPackageName()}</td> |
| 97 | * <td>Actual UID</td></tr> |
| 98 | * </table> |
| 99 | **/ |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 100 | public static final class MediaLibrarySession extends MediaSession { |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 101 | /** |
| 102 | * Callback for the {@link MediaLibrarySession}. |
Kyunglyul Hyun | a27fe4e | 2018-11-02 14:55:54 +0900 | [diff] [blame] | 103 | * <p> |
| 104 | * When you return {@link LibraryResult} with media items, |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 105 | * items must have valid {@link MediaMetadata#METADATA_KEY_MEDIA_ID} and |
| 106 | * specify {@link MediaMetadata#METADATA_KEY_BROWSABLE} and |
| 107 | * {@link MediaMetadata#METADATA_KEY_PLAYABLE}. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 108 | */ |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 109 | public static class MediaLibrarySessionCallback extends MediaSession.SessionCallback { |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 110 | /** |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 111 | * Called to get the root information for browsing by a {@link MediaBrowser}. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 112 | * <p> |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 113 | * To allow browsing media information, return the {@link LibraryResult} with the |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 114 | * {@link LibraryResult#RESULT_SUCCESS} and the root media item with the valid |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 115 | * {@link MediaMetadata#METADATA_KEY_MEDIA_ID media id}. The media id must be included |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 116 | * for the browser to get the children under it. |
Jaewan Kim | e57a84c | 2018-04-10 07:19:52 +0000 | [diff] [blame] | 117 | * <p> |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 118 | * Interoperability: this callback may be called on the main thread, regardless of the |
| 119 | * callback executor. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 120 | * |
| 121 | * @param session the session for this event |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 122 | * @param controller information of the controller requesting access to browse media. |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 123 | * @param params An optional library params of service-specific arguments to send |
| 124 | * to the media library service when connecting and retrieving the |
| 125 | * root id for browsing, or {@code null} if none. |
| 126 | * @return a library result with the root media item with the id. A runtime exception |
| 127 | * will be thrown if an invalid result is returned. |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 128 | * @see SessionCommand#COMMAND_CODE_LIBRARY_GET_LIBRARY_ROOT |
| 129 | * @see MediaMetadata#METADATA_KEY_MEDIA_ID |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 130 | * @see LibraryParams |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 131 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 132 | @NonNull |
| 133 | public LibraryResult onGetLibraryRoot(@NonNull MediaLibrarySession session, |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 134 | @NonNull ControllerInfo controller, @Nullable LibraryParams params) { |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 135 | return new LibraryResult(RESULT_ERROR_NOT_SUPPORTED); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 139 | * Called to get an item. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 140 | * <p> |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 141 | * To allow getting the item, return the {@link LibraryResult} with the |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 142 | * {@link LibraryResult#RESULT_SUCCESS} and the media item. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 143 | * |
| 144 | * @param session the session for this event |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 145 | * @param controller controller |
Hyundo Moon | 184ac0a | 2018-11-06 16:52:50 +0900 | [diff] [blame] | 146 | * @param mediaId non-empty media id of the requested item |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 147 | * @return a library result with a media item with the id. A runtime exception |
| 148 | * will be thrown if an invalid result is returned. |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 149 | * @see SessionCommand#COMMAND_CODE_LIBRARY_GET_ITEM |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 150 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 151 | @NonNull |
| 152 | public LibraryResult onGetItem(@NonNull MediaLibrarySession session, |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 153 | @NonNull ControllerInfo controller, @NonNull String mediaId) { |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 154 | return new LibraryResult(RESULT_ERROR_NOT_SUPPORTED); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Called to get children of given parent id. Return the children here for the browser. |
| 159 | * <p> |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 160 | * To allow getting the children, return the {@link LibraryResult} with the |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 161 | * {@link LibraryResult#RESULT_SUCCESS} and the list of media item. Return an empty |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 162 | * list for no children rather than using result code for error. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 163 | * |
| 164 | * @param session the session for this event |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 165 | * @param controller controller |
Hyundo Moon | 184ac0a | 2018-11-06 16:52:50 +0900 | [diff] [blame] | 166 | * @param parentId non-empty parent id to get children |
Jaewan Kim | a878641 | 2018-07-09 21:12:36 +0900 | [diff] [blame] | 167 | * @param page page number. Starts from {@code 0}. |
| 168 | * @param pageSize page size. Should be greater or equal to {@code 1}. |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 169 | * @param params library params |
| 170 | * @return a library result with a list of media item with the id. A runtime exception |
| 171 | * will be thrown if an invalid result is returned. |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 172 | * @see SessionCommand#COMMAND_CODE_LIBRARY_GET_CHILDREN |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 173 | * @see LibraryParams |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 174 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 175 | @NonNull |
| 176 | public LibraryResult onGetChildren(@NonNull MediaLibrarySession session, |
Hyundo Moon | 3e2a8b3 | 2018-11-05 15:35:52 +0900 | [diff] [blame] | 177 | @NonNull ControllerInfo controller, @NonNull String parentId, |
| 178 | @IntRange(from = 0) int page, @IntRange(from = 1) int pageSize, |
| 179 | @Nullable LibraryParams params) { |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 180 | return new LibraryResult(RESULT_ERROR_NOT_SUPPORTED); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Called when a controller subscribes to the parent. |
| 185 | * <p> |
| 186 | * It's your responsibility to keep subscriptions by your own and call |
Hyundo Moon | 999d714 | 2018-06-19 14:30:19 +0900 | [diff] [blame] | 187 | * {@link MediaLibrarySession#notifyChildrenChanged( |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 188 | * ControllerInfo, String, int, LibraryParams)} when the parent is changed until it's |
| 189 | * unsubscribed. |
Jaewan Kim | a25a835 | 2018-06-16 14:03:12 +0900 | [diff] [blame] | 190 | * <p> |
| 191 | * Interoperability: This will be called when |
| 192 | * {@link android.support.v4.media.MediaBrowserCompat#subscribe} is called. |
| 193 | * However, this won't be called when {@link MediaBrowser#subscribe} is called. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 194 | * |
| 195 | * @param session the session for this event |
| 196 | * @param controller controller |
Hyundo Moon | 184ac0a | 2018-11-06 16:52:50 +0900 | [diff] [blame] | 197 | * @param parentId non-empty parent id |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 198 | * @param params library params |
| 199 | * @return result code |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 200 | * @see SessionCommand#COMMAND_CODE_LIBRARY_SUBSCRIBE |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 201 | * @see LibraryParams |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 202 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 203 | @ResultCode |
| 204 | public int onSubscribe(@NonNull MediaLibrarySession session, |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 205 | @NonNull ControllerInfo controller, @NonNull String parentId, |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 206 | @Nullable LibraryParams params) { |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 207 | return RESULT_ERROR_NOT_SUPPORTED; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Called when a controller unsubscribes to the parent. |
Jaewan Kim | a25a835 | 2018-06-16 14:03:12 +0900 | [diff] [blame] | 212 | * <p> |
| 213 | * Interoperability: This wouldn't be called if {@link MediaBrowser#unsubscribe} is |
| 214 | * called while works well with |
| 215 | * {@link android.support.v4.media.MediaBrowserCompat#unsubscribe}. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 216 | * |
| 217 | * @param session the session for this event |
| 218 | * @param controller controller |
Hyundo Moon | 184ac0a | 2018-11-06 16:52:50 +0900 | [diff] [blame] | 219 | * @param parentId non-empty parent id |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 220 | * @return result code |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 221 | * @see SessionCommand#COMMAND_CODE_LIBRARY_UNSUBSCRIBE |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 222 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 223 | @ResultCode |
| 224 | public int onUnsubscribe(@NonNull MediaLibrarySession session, |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 225 | @NonNull ControllerInfo controller, @NonNull String parentId) { |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 226 | return RESULT_ERROR_NOT_SUPPORTED; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Called when a controller requests search. |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 231 | * <p> |
| 232 | * Return immediately with the result of the attempt to search with the query. Notify |
| 233 | * the number of search result through |
| 234 | * {@link #notifySearchResultChanged(ControllerInfo, String, int, LibraryParams)}. |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 235 | * {@link MediaBrowser} will ask the search result with the pagination later. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 236 | * |
| 237 | * @param session the session for this event |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 238 | * @param controller controller |
Hyundo Moon | 184ac0a | 2018-11-06 16:52:50 +0900 | [diff] [blame] | 239 | * @param query The non-empty search query sent from the media browser. |
| 240 | * It contains keywords separated by space. |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 241 | * @param params library params |
| 242 | * @return result code |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 243 | * @see SessionCommand#COMMAND_CODE_LIBRARY_SEARCH |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 244 | * @see #notifySearchResultChanged(ControllerInfo, String, int, LibraryParams) |
| 245 | * @see LibraryParams |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 246 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 247 | @ResultCode |
| 248 | public int onSearch(@NonNull MediaLibrarySession session, |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 249 | @NonNull ControllerInfo controller, @NonNull String query, |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 250 | @Nullable LibraryParams params) { |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 251 | return RESULT_ERROR_NOT_SUPPORTED; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 255 | * Called to get the search result. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 256 | * <p> |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 257 | * To allow getting the search result, return the {@link LibraryResult} with the |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 258 | * {@link LibraryResult#RESULT_SUCCESS} and the list of media item. Return an empty |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 259 | * list for no search result rather than using result code for error. |
Jaewan Kim | a25a835 | 2018-06-16 14:03:12 +0900 | [diff] [blame] | 260 | * <p> |
| 261 | * This may be called with a query that hasn't called with {@link #onSearch}, especially |
| 262 | * when {@link android.support.v4.media.MediaBrowserCompat#search} is used. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 263 | * |
| 264 | * @param session the session for this event |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 265 | * @param controller controller |
Hyundo Moon | 184ac0a | 2018-11-06 16:52:50 +0900 | [diff] [blame] | 266 | * @param query The non-empty search query which was previously sent through |
| 267 | * {@link #onSearch}. |
Jaewan Kim | a878641 | 2018-07-09 21:12:36 +0900 | [diff] [blame] | 268 | * @param page page number. Starts from {@code 0}. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 269 | * @param pageSize page size. Should be greater or equal to {@code 1}. |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 270 | * @param params library params |
| 271 | * @return a library result with a list of media item with the id. A runtime exception |
| 272 | * will be thrown if an invalid result is returned. |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 273 | * @see SessionCommand#COMMAND_CODE_LIBRARY_GET_SEARCH_RESULT |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 274 | * @see LibraryParams |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 275 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 276 | @NonNull |
| 277 | public LibraryResult onGetSearchResult( |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 278 | @NonNull MediaLibrarySession session, @NonNull ControllerInfo controller, |
Hyundo Moon | 3e2a8b3 | 2018-11-05 15:35:52 +0900 | [diff] [blame] | 279 | @NonNull String query, @IntRange(from = 0) int page, |
| 280 | @IntRange(from = 1) int pageSize, @Nullable LibraryParams params) { |
Jaewan Kim | 0c5c4d6 | 2018-12-10 19:53:05 +0900 | [diff] [blame] | 281 | return new LibraryResult(RESULT_ERROR_NOT_SUPPORTED); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Builder for {@link MediaLibrarySession}. |
Jaewan Kim | 77bbd4b | 2018-12-26 11:15:48 +0900 | [diff] [blame] | 287 | * <p> |
| 288 | * Any incoming event from the {@link MediaController} will be handled on the callback |
| 289 | * executor. If it's not set, {@link ContextCompat#getMainExecutor(Context)} will be used by |
| 290 | * default. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 291 | */ |
| 292 | // Override all methods just to show them with the type instead of generics in Javadoc. |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 293 | // This workarounds javadoc issue described in the MediaSession.BuilderBase. |
Jaewan Kim | 917d3a3 | 2018-04-16 23:05:18 +0900 | [diff] [blame] | 294 | // Note: Don't override #setSessionCallback() because the callback can be set by the |
| 295 | // constructor. |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 296 | public static final class Builder extends MediaSession.BuilderBase<MediaLibrarySession, |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 297 | Builder, MediaLibrarySessionCallback> { |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 298 | // Builder requires MediaLibraryService instead of Context just to ensure that the |
| 299 | // builder can be only instantiated within the MediaLibraryService. |
Jaewan Kim | 4832954 | 2018-05-14 10:41:10 +0900 | [diff] [blame] | 300 | // Ideally it's better to make it inner class of service to enforce, but it violates API |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 301 | // guideline that Builders should be the inner class of the building target. |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 302 | public Builder(@NonNull MediaLibraryService service, |
| 303 | @NonNull SessionPlayer player, |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 304 | @NonNull Executor callbackExecutor, |
| 305 | @NonNull MediaLibrarySessionCallback callback) { |
Jaewan Kim | 85388bf | 2018-09-17 19:47:24 +0900 | [diff] [blame] | 306 | super(service, player); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 307 | setSessionCallback(callbackExecutor, callback); |
| 308 | } |
| 309 | |
| 310 | @Override |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 311 | @NonNull |
| 312 | public Builder setSessionActivity(@Nullable PendingIntent pi) { |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 313 | return super.setSessionActivity(pi); |
| 314 | } |
| 315 | |
| 316 | @Override |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 317 | @NonNull |
| 318 | public Builder setId(@NonNull String id) { |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 319 | return super.setId(id); |
| 320 | } |
| 321 | |
Hyundo Moon | e3b3034 | 2019-04-10 23:04:28 +0900 | [diff] [blame] | 322 | @Override |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 323 | @NonNull |
Hyundo Moon | e3b3034 | 2019-04-10 23:04:28 +0900 | [diff] [blame] | 324 | public Builder setExtras(@NonNull Bundle extras) { |
| 325 | return super.setExtras(extras); |
| 326 | } |
| 327 | |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 328 | @Override |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 329 | @NonNull |
| 330 | public MediaLibrarySession build() { |
Jaewan Kim | 4832954 | 2018-05-14 10:41:10 +0900 | [diff] [blame] | 331 | if (mCallbackExecutor == null) { |
Insun Kang | de8fad1 | 2018-08-31 15:28:09 +0900 | [diff] [blame] | 332 | mCallbackExecutor = ContextCompat.getMainExecutor(mContext); |
Jaewan Kim | 4832954 | 2018-05-14 10:41:10 +0900 | [diff] [blame] | 333 | } |
| 334 | if (mCallback == null) { |
| 335 | mCallback = new MediaLibrarySession.MediaLibrarySessionCallback() {}; |
| 336 | } |
Jaewan Kim | 85388bf | 2018-09-17 19:47:24 +0900 | [diff] [blame] | 337 | return new MediaLibrarySession(mContext, mId, mPlayer, mSessionActivity, |
Hyundo Moon | e3b3034 | 2019-04-10 23:04:28 +0900 | [diff] [blame] | 338 | mCallbackExecutor, mCallback, mExtras); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 342 | MediaLibrarySession(Context context, String id, SessionPlayer player, |
Jaewan Kim | 1f27281 | 2018-09-11 20:12:12 +0900 | [diff] [blame] | 343 | PendingIntent sessionActivity, Executor callbackExecutor, |
Hyundo Moon | e3b3034 | 2019-04-10 23:04:28 +0900 | [diff] [blame] | 344 | MediaSession.SessionCallback callback, Bundle tokenExtras) { |
| 345 | super(context, id, player, sessionActivity, callbackExecutor, callback, tokenExtras); |
Jaewan Kim | 4832954 | 2018-05-14 10:41:10 +0900 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 349 | MediaLibrarySessionImpl createImpl(Context context, String id, SessionPlayer player, |
Jaewan Kim | 1f27281 | 2018-09-11 20:12:12 +0900 | [diff] [blame] | 350 | PendingIntent sessionActivity, Executor callbackExecutor, |
Hyundo Moon | e3b3034 | 2019-04-10 23:04:28 +0900 | [diff] [blame] | 351 | MediaSession.SessionCallback callback, Bundle tokenExtras) { |
Jaewan Kim | 1f27281 | 2018-09-11 20:12:12 +0900 | [diff] [blame] | 352 | return new MediaLibrarySessionImplBase(this, context, id, player, sessionActivity, |
Hyundo Moon | e3b3034 | 2019-04-10 23:04:28 +0900 | [diff] [blame] | 353 | callbackExecutor, callback, tokenExtras); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 354 | } |
| 355 | |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 356 | @Override |
Sungsoo Lim | f2a4ed8 | 2018-07-16 13:31:25 +0900 | [diff] [blame] | 357 | MediaLibrarySessionImpl getImpl() { |
| 358 | return (MediaLibrarySessionImpl) super.getImpl(); |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 359 | } |
| 360 | |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 361 | /** |
Kyunglyul Hyun | bc6e6c7 | 2019-07-31 02:50:00 -0700 | [diff] [blame] | 362 | * Notifies the controller of the change in a parent's children. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 363 | * <p> |
| 364 | * If the controller hasn't subscribed to the parent, the API will do nothing. |
| 365 | * <p> |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 366 | * Controllers will use {@link MediaBrowser#getChildren(String, int, int, LibraryParams)} |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 367 | * to get the list of children. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 368 | * |
| 369 | * @param controller controller to notify |
Hyundo Moon | 184ac0a | 2018-11-06 16:52:50 +0900 | [diff] [blame] | 370 | * @param parentId non-empty parent id with changes in its children |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 371 | * @param itemCount number of children. |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 372 | * @param params library params |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 373 | */ |
| 374 | public void notifyChildrenChanged(@NonNull ControllerInfo controller, |
Hyundo Moon | 10daf4c | 2018-10-26 20:18:26 +0900 | [diff] [blame] | 375 | @NonNull String parentId, @IntRange(from = 0) int itemCount, |
| 376 | @Nullable LibraryParams params) { |
| 377 | if (controller == null) { |
Sungsoo Lim | 590422b | 2019-04-25 10:55:23 +0900 | [diff] [blame] | 378 | throw new NullPointerException("controller shouldn't be null"); |
Hyundo Moon | 10daf4c | 2018-10-26 20:18:26 +0900 | [diff] [blame] | 379 | } |
Sungsoo Lim | 590422b | 2019-04-25 10:55:23 +0900 | [diff] [blame] | 380 | if (parentId == null) { |
| 381 | throw new NullPointerException("parentId shouldn't be null"); |
| 382 | } else if (TextUtils.isEmpty(parentId)) { |
Hyundo Moon | 10daf4c | 2018-10-26 20:18:26 +0900 | [diff] [blame] | 383 | throw new IllegalArgumentException("parentId shouldn't be empty"); |
| 384 | } |
| 385 | if (itemCount < 0) { |
| 386 | throw new IllegalArgumentException("itemCount shouldn't be negative"); |
| 387 | } |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 388 | getImpl().notifyChildrenChanged(controller, parentId, itemCount, params); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
Kyunglyul Hyun | bc6e6c7 | 2019-07-31 02:50:00 -0700 | [diff] [blame] | 392 | * Notifies all controllers that subscribed to the parent about change in the parent's |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 393 | * children, regardless of the library params supplied by |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 394 | * {@link MediaBrowser#subscribe(String, LibraryParams)}. |
Hyundo Moon | 184ac0a | 2018-11-06 16:52:50 +0900 | [diff] [blame] | 395 | * @param parentId non-empty parent id |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 396 | * @param itemCount number of children |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 397 | * @param params library params |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 398 | */ |
| 399 | // This is for the backward compatibility. |
| 400 | public void notifyChildrenChanged(@NonNull String parentId, int itemCount, |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 401 | @Nullable LibraryParams params) { |
Hyundo Moon | 10daf4c | 2018-10-26 20:18:26 +0900 | [diff] [blame] | 402 | if (TextUtils.isEmpty(parentId)) { |
| 403 | throw new IllegalArgumentException("parentId shouldn't be empty"); |
| 404 | } |
| 405 | if (itemCount < 0) { |
| 406 | throw new IllegalArgumentException("itemCount shouldn't be negative"); |
| 407 | } |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 408 | getImpl().notifyChildrenChanged(parentId, itemCount, params); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
Kyunglyul Hyun | bc6e6c7 | 2019-07-31 02:50:00 -0700 | [diff] [blame] | 412 | * Notifies controller about change in the search result. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 413 | * |
| 414 | * @param controller controller to notify |
Hyundo Moon | 184ac0a | 2018-11-06 16:52:50 +0900 | [diff] [blame] | 415 | * @param query previously sent non-empty search query from the controller. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 416 | * @param itemCount the number of items that have been found in the search. |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 417 | * @param params library params |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 418 | */ |
| 419 | public void notifySearchResultChanged(@NonNull ControllerInfo controller, |
Hyundo Moon | 10daf4c | 2018-10-26 20:18:26 +0900 | [diff] [blame] | 420 | @NonNull String query, @IntRange(from = 0) int itemCount, |
| 421 | @Nullable LibraryParams params) { |
| 422 | if (controller == null) { |
Sungsoo Lim | 590422b | 2019-04-25 10:55:23 +0900 | [diff] [blame] | 423 | throw new NullPointerException("controller shouldn't be null"); |
Hyundo Moon | 10daf4c | 2018-10-26 20:18:26 +0900 | [diff] [blame] | 424 | } |
Sungsoo Lim | 590422b | 2019-04-25 10:55:23 +0900 | [diff] [blame] | 425 | if (query == null) { |
| 426 | throw new NullPointerException("query shouldn't be null"); |
| 427 | } else if (TextUtils.isEmpty(query)) { |
Hyundo Moon | 10daf4c | 2018-10-26 20:18:26 +0900 | [diff] [blame] | 428 | throw new IllegalArgumentException("query shouldn't be empty"); |
| 429 | } |
| 430 | if (itemCount < 0) { |
| 431 | throw new IllegalArgumentException("itemCount shouldn't be negative"); |
| 432 | } |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 433 | getImpl().notifySearchResultChanged(controller, query, itemCount, params); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 434 | } |
| 435 | |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 436 | @Override |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 437 | @NonNull |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 438 | MediaLibrarySessionCallback getCallback() { |
| 439 | return (MediaLibrarySessionCallback) super.getCallback(); |
| 440 | } |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 441 | |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 442 | interface MediaLibrarySessionImpl extends MediaSessionImpl { |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 443 | // LibrarySession methods |
| 444 | void notifyChildrenChanged( |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 445 | @NonNull String parentId, int itemCount, @Nullable LibraryParams params); |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 446 | void notifyChildrenChanged(@NonNull ControllerInfo controller, |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 447 | @NonNull String parentId, int itemCount, @Nullable LibraryParams params); |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 448 | void notifySearchResultChanged(@NonNull ControllerInfo controller, |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 449 | @NonNull String query, int itemCount, @Nullable LibraryParams params); |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 450 | |
| 451 | // LibrarySession callback implementations called on the executors |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 452 | LibraryResult onGetLibraryRootOnExecutor(@NonNull ControllerInfo controller, |
| 453 | @Nullable LibraryParams params); |
| 454 | LibraryResult onGetItemOnExecutor(@NonNull ControllerInfo controller, |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 455 | @NonNull String mediaId); |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 456 | LibraryResult onGetChildrenOnExecutor(@NonNull ControllerInfo controller, |
| 457 | @NonNull String parentId, int page, int pageSize, |
| 458 | @Nullable LibraryParams params); |
| 459 | int onSubscribeOnExecutor(@NonNull ControllerInfo controller, |
| 460 | @NonNull String parentId, @Nullable LibraryParams params); |
| 461 | int onUnsubscribeOnExecutor(@NonNull ControllerInfo controller, |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 462 | @NonNull String parentId); |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 463 | int onSearchOnExecutor(@NonNull ControllerInfo controller, @NonNull String query, |
| 464 | @Nullable LibraryParams params); |
| 465 | LibraryResult onGetSearchResultOnExecutor(@NonNull ControllerInfo controller, |
| 466 | @NonNull String query, int page, int pageSize, @Nullable LibraryParams params); |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 467 | |
| 468 | // Internally used methods - only changing return type |
| 469 | @Override |
| 470 | MediaLibrarySession getInstance(); |
| 471 | |
| 472 | @Override |
| 473 | MediaLibrarySessionCallback getCallback(); |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 474 | } |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 475 | } |
| 476 | |
Jaewan Kim | 6967076 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 477 | @Override |
Sungsoo Lim | 9e1e3d5 | 2018-11-08 14:46:16 +0900 | [diff] [blame] | 478 | MediaSessionServiceImpl createImpl() { |
| 479 | return new MediaLibraryServiceImplBase(); |
Jaewan Kim | e57a84c | 2018-04-10 07:19:52 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | @Override |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 483 | public IBinder onBind(@NonNull Intent intent) { |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 484 | return super.onBind(intent); |
| 485 | } |
| 486 | |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 487 | @Override |
Hyundo Moon | 17552c7 | 2019-04-19 00:51:02 +0900 | [diff] [blame] | 488 | @Nullable |
| 489 | public abstract MediaLibrarySession onGetSession(@NonNull ControllerInfo controllerInfo); |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 490 | |
| 491 | /** |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 492 | * Contains information that the library service needs to send to the client. |
| 493 | * <p> |
| 494 | * When the browser supplies {@link LibraryParams}, it's optional field when getting the media |
| 495 | * item(s). The library session is recommended to do the best effort to provide such result. |
| 496 | * It's not an error even when the library session didn't return such items. |
| 497 | * <p> |
| 498 | * The library params returned in the library session callback must include the information |
| 499 | * about the returned media item(s). |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 500 | */ |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 501 | @VersionedParcelize |
| 502 | public static final class LibraryParams implements VersionedParcelable { |
| 503 | @ParcelField(1) |
| 504 | Bundle mBundle; |
| 505 | |
| 506 | // Types are intentionally Integer for future extension of the value with less effort. |
| 507 | @ParcelField(2) |
| 508 | int mRecent; |
| 509 | @ParcelField(3) |
| 510 | int mOffline; |
| 511 | @ParcelField(4) |
| 512 | int mSuggested; |
| 513 | |
Gyumin Sim | 7bdf293 | 2020-04-03 13:54:44 +0900 | [diff] [blame] | 514 | // WARNING: Adding a new ParcelField may break old library users (b/152830728) |
| 515 | |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 516 | // For versioned parcelable. |
| 517 | LibraryParams() { |
| 518 | // no-op |
| 519 | } |
| 520 | |
| 521 | @SuppressWarnings("WeakerAccess") /* synthetic access */ |
| 522 | LibraryParams(Bundle bundle, boolean recent, boolean offline, boolean suggested) { |
| 523 | // Keeps the booleans in Integer type. |
| 524 | // Types are intentionally Integer for future extension of the value with less effort. |
| 525 | this(bundle, |
| 526 | convertToInteger(recent), |
| 527 | convertToInteger(offline), |
| 528 | convertToInteger(suggested)); |
| 529 | } |
| 530 | |
| 531 | private LibraryParams(Bundle bundle, int recent, int offline, int suggested) { |
| 532 | mBundle = bundle; |
| 533 | mRecent = recent; |
| 534 | mOffline = offline; |
| 535 | mSuggested = suggested; |
| 536 | } |
| 537 | |
| 538 | private static int convertToInteger(boolean a) { |
| 539 | return a ? 1 : 0; |
| 540 | } |
| 541 | |
| 542 | private static boolean convertToBoolean(int a) { |
| 543 | return a == 0 ? false : true; |
| 544 | } |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 545 | |
| 546 | /** |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 547 | * Returns {@code true} for recent media items. |
| 548 | * <p> |
| 549 | * When the browser supplies {@link LibraryParams} with the {@code true}, library |
| 550 | * session is recommended to provide such media items. If so, the library session |
| 551 | * implementation must return the params with the {@code true} as well. The list of |
| 552 | * media items is considered ordered by relevance, first being the top suggestion. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 553 | * |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 554 | * @return {@code true} for recent items. {@code false} otherwise. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 555 | */ |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 556 | public boolean isRecent() { |
| 557 | return convertToBoolean(mRecent); |
| 558 | } |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 559 | |
| 560 | /** |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 561 | * Returns {@code true} for offline media items, which can be played without an internet |
| 562 | * connection. |
| 563 | * <p> |
| 564 | * When the browser supplies {@link LibraryParams} with the {@code true}, library |
| 565 | * session is recommended to provide such media items. If so, the library session |
| 566 | * implementation must return the params with the {@code true} as well. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 567 | * |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 568 | * @return {@code true} for offline items. {@code false} otherwise. |
| 569 | **/ |
| 570 | public boolean isOffline() { |
| 571 | return convertToBoolean(mOffline); |
| 572 | } |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 573 | |
| 574 | /** |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 575 | * Returns {@code true} for suggested media items. |
| 576 | * <p> |
| 577 | * When the browser supplies {@link LibraryParams} with the {@code true}, library |
| 578 | * session is recommended to provide such media items. If so, the library session |
| 579 | * implementation must return the params with the {@code true} as well. The list of |
| 580 | * media items is considered ordered by relevance, first being the top suggestion. |
| 581 | * |
| 582 | * @return {@code true} for suggested items. {@code false} otherwise |
| 583 | **/ |
| 584 | public boolean isSuggested() { |
| 585 | return convertToBoolean(mSuggested); |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Gets the extras. |
| 590 | * <p> |
| 591 | * Extras are the private contract between browser and library session. |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 592 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 593 | @Nullable |
| 594 | public Bundle getExtras() { |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 595 | return mBundle; |
| 596 | } |
| 597 | |
| 598 | /** |
Kyunglyul Hyun | bc6e6c7 | 2019-07-31 02:50:00 -0700 | [diff] [blame] | 599 | * Builds a {@link LibraryParams}. |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 600 | */ |
| 601 | public static final class Builder { |
| 602 | private boolean mRecent; |
| 603 | private boolean mOffline; |
| 604 | private boolean mSuggested; |
| 605 | |
| 606 | private Bundle mBundle; |
| 607 | |
| 608 | /** |
| 609 | * Sets whether recently played media item. |
| 610 | * <p> |
| 611 | * When the browser supplies the {@link LibraryParams} with the {@code true}, library |
| 612 | * session is recommended to provide such media items. If so, the library session |
| 613 | * implementation must return the params with the {@code true} as well. |
| 614 | * |
| 615 | * @param recent {@code true} for recent items. {@code false} otherwise. |
| 616 | * @return this builder |
| 617 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 618 | @NonNull |
| 619 | public Builder setRecent(boolean recent) { |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 620 | mRecent = recent; |
| 621 | return this; |
Insun Kang | fbbf807 | 2018-04-09 14:51:51 +0900 | [diff] [blame] | 622 | } |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 623 | |
| 624 | /** |
| 625 | * Sets whether offline media items, which can be played without an internet connection. |
| 626 | * <p> |
| 627 | * When the browser supplies {@link LibraryParams} with the {@code true}, library |
| 628 | * session is recommended to provide such media items. If so, the library session |
| 629 | * implementation must return the params with the {@code true} as well. |
| 630 | * |
| 631 | * @param offline {@code true} for offline items. {@code false} otherwise. |
| 632 | * @return this builder |
| 633 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 634 | @NonNull |
| 635 | public Builder setOffline(boolean offline) { |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 636 | mOffline = offline; |
| 637 | return this; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Sets whether suggested media items. |
| 642 | * <p> |
| 643 | * When the browser supplies {@link LibraryParams} with the {@code true}, library |
| 644 | * session is recommended to provide such media items. If so, the library session |
| 645 | * implementation must return the params with the {@code true} as well. The list of |
| 646 | * media items is considered ordered by relevance, first being the top suggestion. |
| 647 | * |
| 648 | * @param suggested {@code true} for suggested items. {@code false} otherwise |
| 649 | * @return this builder |
| 650 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 651 | @NonNull |
| 652 | public Builder setSuggested(boolean suggested) { |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 653 | mSuggested = suggested; |
| 654 | return this; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Set a bundle of extras, that browser and library session can understand each other. |
| 659 | * |
| 660 | * @param extras The extras or null. |
| 661 | * @return this builder |
| 662 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 663 | @NonNull |
| 664 | public Builder setExtras(@Nullable Bundle extras) { |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 665 | mBundle = extras; |
| 666 | return this; |
| 667 | } |
| 668 | |
| 669 | /** |
Kyunglyul Hyun | bc6e6c7 | 2019-07-31 02:50:00 -0700 | [diff] [blame] | 670 | * Builds a {@link LibraryParams}. |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 671 | * |
| 672 | * @return new LibraryParams |
| 673 | */ |
Sungsoo Lim | fdb7e08 | 2019-06-20 07:54:42 +0900 | [diff] [blame] | 674 | @NonNull |
| 675 | public LibraryParams build() { |
Jaewan Kim | 1cf1ac5 | 2018-10-24 14:44:03 +0900 | [diff] [blame] | 676 | return new LibraryParams(mBundle, mRecent, mOffline, mSuggested); |
| 677 | } |
| 678 | } |
| 679 | } |
Jaewan Kim | 96464f1 | 2018-05-11 15:57:22 +0900 | [diff] [blame] | 680 | } |