1/*
2 * Copyright (C) 2015 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;
21import android.support.annotation.NonNull;
22
23class MediaBrowserCompatApi23 {
24
25    public static Object createItemCallback(ItemCallback callback) {
26        return new ItemCallbackProxy<>(callback);
27    }
28
29    public static void getItem(Object browserObj, String mediaId, Object itemCallbackObj) {
30        ((MediaBrowser) browserObj).getItem(mediaId, ((MediaBrowser.ItemCallback) itemCallbackObj));
31    }
32
33    interface ItemCallback {
34        void onItemLoaded(Parcel itemParcel);
35        void onError(@NonNull String itemId);
36    }
37
38    static class ItemCallbackProxy<T extends ItemCallback> extends MediaBrowser.ItemCallback {
39        protected final T mItemCallback;
40
41        public ItemCallbackProxy(T callback) {
42            mItemCallback = callback;
43        }
44
45        @Override
46        public void onItemLoaded(MediaBrowser.MediaItem item) {
47            Parcel parcel = Parcel.obtain();
48            item.writeToParcel(parcel, 0);
49            mItemCallback.onItemLoaded(parcel);
50        }
51
52        @Override
53        public void onError(@NonNull String itemId) {
54            mItemCallback.onError(itemId);
55        }
56    }
57}
58