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.content.Context; 20import android.content.Intent; 21import android.media.browse.MediaBrowser; 22import android.media.session.MediaSession; 23import android.os.Bundle; 24import android.os.IBinder; 25import android.os.Parcel; 26import android.service.media.MediaBrowserService; 27 28import java.util.ArrayList; 29import java.util.List; 30 31class MediaBrowserServiceCompatApi21 { 32 33 public static Object createService(Context context, ServiceCompatProxy serviceProxy) { 34 return new MediaBrowserServiceAdaptor(context, serviceProxy); 35 } 36 37 public static void onCreate(Object serviceObj) { 38 ((MediaBrowserService) serviceObj).onCreate(); 39 } 40 41 public static IBinder onBind(Object serviceObj, Intent intent) { 42 return ((MediaBrowserService) serviceObj).onBind(intent); 43 } 44 45 public static void setSessionToken(Object serviceObj, Object token) { 46 ((MediaBrowserService) serviceObj).setSessionToken((MediaSession.Token) token); 47 } 48 49 public static void notifyChildrenChanged(Object serviceObj, String parentId) { 50 ((MediaBrowserService) serviceObj).notifyChildrenChanged(parentId); 51 } 52 53 public interface ServiceCompatProxy { 54 BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints); 55 void onLoadChildren(String parentId, ResultWrapper<List<Parcel>> result); 56 } 57 58 static class ResultWrapper<T> { 59 MediaBrowserService.Result mResultObj; 60 61 ResultWrapper(MediaBrowserService.Result result) { 62 mResultObj = result; 63 } 64 65 public void sendResult(T result) { 66 if (result instanceof List) { 67 mResultObj.sendResult(parcelListToItemList((List<Parcel>)result)); 68 } else if (result instanceof Parcel) { 69 mResultObj.sendResult( 70 MediaBrowser.MediaItem.CREATOR.createFromParcel((Parcel) result)); 71 } 72 } 73 74 public void detach() { 75 mResultObj.detach(); 76 } 77 78 List<MediaBrowser.MediaItem> parcelListToItemList(List<Parcel> parcelList) { 79 if (parcelList == null) { 80 return null; 81 } 82 List<MediaBrowser.MediaItem> items = new ArrayList<>(); 83 for (Parcel parcel : parcelList) { 84 parcel.setDataPosition(0); 85 items.add(MediaBrowser.MediaItem.CREATOR.createFromParcel(parcel)); 86 parcel.recycle(); 87 } 88 return items; 89 } 90 } 91 92 static class BrowserRoot { 93 final String mRootId; 94 final Bundle mExtras; 95 96 BrowserRoot(String rootId, Bundle extras) { 97 mRootId = rootId; 98 mExtras = extras; 99 } 100 } 101 102 static class MediaBrowserServiceAdaptor extends MediaBrowserService { 103 final ServiceCompatProxy mServiceProxy; 104 105 MediaBrowserServiceAdaptor(Context context, ServiceCompatProxy serviceWrapper) { 106 attachBaseContext(context); 107 mServiceProxy = serviceWrapper; 108 } 109 110 @Override 111 public MediaBrowserService.BrowserRoot onGetRoot(String clientPackageName, int clientUid, 112 Bundle rootHints) { 113 MediaBrowserServiceCompatApi21.BrowserRoot browserRoot = mServiceProxy.onGetRoot( 114 clientPackageName, clientUid, rootHints); 115 return browserRoot == null ? null : new MediaBrowserService.BrowserRoot( 116 browserRoot.mRootId, browserRoot.mExtras); 117 } 118 119 @Override 120 public void onLoadChildren(String parentId, Result<List<MediaBrowser.MediaItem>> result) { 121 mServiceProxy.onLoadChildren(parentId, new ResultWrapper<List<Parcel>>(result)); 122 } 123 } 124} 125