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
17package android.support.v4.media;
18
19import android.media.browse.MediaBrowser;
20import android.os.Parcel;
21
22import androidx.annotation.NonNull;
23import androidx.annotation.RequiresApi;
24
25@RequiresApi(23)
26class MediaBrowserCompatApi23 {
27
28    public static Object createItemCallback(ItemCallback callback) {
29        return new ItemCallbackProxy<>(callback);
30    }
31
32    public static void getItem(Object browserObj, String mediaId, Object itemCallbackObj) {
33        ((MediaBrowser) browserObj).getItem(mediaId, ((MediaBrowser.ItemCallback) itemCallbackObj));
34    }
35
36    interface ItemCallback {
37        void onItemLoaded(Parcel itemParcel);
38        void onError(@NonNull String itemId);
39    }
40
41    static class ItemCallbackProxy<T extends ItemCallback> extends MediaBrowser.ItemCallback {
42        protected final T mItemCallback;
43
44        public ItemCallbackProxy(T callback) {
45            mItemCallback = callback;
46        }
47
48        @Override
49        public void onItemLoaded(MediaBrowser.MediaItem item) {
50            if (item == null) {
51                mItemCallback.onItemLoaded(null);
52            } else {
53                Parcel parcel = Parcel.obtain();
54                item.writeToParcel(parcel, 0);
55                mItemCallback.onItemLoaded(parcel);
56            }
57        }
58
59        @Override
60        public void onError(@NonNull String itemId) {
61            mItemCallback.onError(itemId);
62        }
63    }
64
65    private MediaBrowserCompatApi23() {
66    }
67}
68