1/*
2 * Copyright (C) 2016 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.content.Context;
20import android.media.browse.MediaBrowser;
21import android.os.Bundle;
22import android.os.Parcel;
23import android.service.media.MediaBrowserService;
24import android.util.Log;
25
26import java.lang.reflect.Field;
27import java.util.ArrayList;
28import java.util.List;
29
30class MediaBrowserServiceCompatApi24 {
31    private static final String TAG = "MBSCompatApi24";
32
33    private static Field sResultFlags;
34    static {
35        try {
36            sResultFlags = MediaBrowserService.Result.class.getDeclaredField("mFlags");
37            sResultFlags.setAccessible(true);
38        } catch (NoSuchFieldException e) {
39            Log.w(TAG, e);
40        }
41    }
42
43    public static Object createService(Context context, ServiceCompatProxy serviceProxy) {
44        return new MediaBrowserServiceAdaptor(context, serviceProxy);
45    }
46
47    public static void notifyChildrenChanged(Object serviceObj, String parentId, Bundle options) {
48        ((MediaBrowserService) serviceObj).notifyChildrenChanged(parentId, options);
49    }
50
51    public static Bundle getBrowserRootHints(Object serviceObj) {
52        return ((MediaBrowserService) serviceObj).getBrowserRootHints();
53    }
54
55    public interface ServiceCompatProxy extends MediaBrowserServiceCompatApi23.ServiceCompatProxy {
56        void onLoadChildren(String parentId, ResultWrapper result, Bundle options);
57    }
58
59    static class ResultWrapper {
60        MediaBrowserService.Result mResultObj;
61
62        ResultWrapper(MediaBrowserService.Result result) {
63            mResultObj = result;
64        }
65
66        public void sendResult(List<Parcel> result, int flags) {
67            try {
68                sResultFlags.setInt(mResultObj, flags);
69            } catch (IllegalAccessException e) {
70                Log.w(TAG, e);
71            }
72            mResultObj.sendResult(parcelListToItemList(result));
73        }
74
75        public void detach() {
76            mResultObj.detach();
77        }
78
79        List<MediaBrowser.MediaItem> parcelListToItemList(List<Parcel> parcelList) {
80            if (parcelList == null) {
81                return null;
82            }
83            List<MediaBrowser.MediaItem> items = new ArrayList<>();
84            for (Parcel parcel : parcelList) {
85                parcel.setDataPosition(0);
86                items.add(MediaBrowser.MediaItem.CREATOR.createFromParcel(parcel));
87                parcel.recycle();
88            }
89            return items;
90        }
91    }
92
93    static class MediaBrowserServiceAdaptor extends
94            MediaBrowserServiceCompatApi23.MediaBrowserServiceAdaptor {
95        MediaBrowserServiceAdaptor(Context context, ServiceCompatProxy serviceWrapper) {
96            super(context, serviceWrapper);
97        }
98
99        @Override
100        public void onLoadChildren(String parentId, Result<List<MediaBrowser.MediaItem>> result,
101                Bundle options) {
102            ((ServiceCompatProxy) mServiceProxy).onLoadChildren(
103                    parentId, new ResultWrapper(result), options);
104        }
105    }
106}
107