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 17 package android.support.v4.media.session; 18 19 import android.media.session.MediaSession; 20 import android.net.Uri; 21 import android.os.Bundle; 22 import android.support.annotation.RequiresApi; 23 import android.util.Log; 24 25 import java.lang.reflect.InvocationTargetException; 26 import java.lang.reflect.Method; 27 28 @RequiresApi(24) 29 class MediaSessionCompatApi24 { 30 private static final String TAG = "MediaSessionCompatApi24"; 31 createCallback(Callback callback)32 public static Object createCallback(Callback callback) { 33 return new CallbackProxy<Callback>(callback); 34 } 35 getCallingPackage(Object sessionObj)36 public static String getCallingPackage(Object sessionObj) { 37 MediaSession session = (MediaSession) sessionObj; 38 try { 39 Method getCallingPackageMethod = session.getClass().getMethod("getCallingPackage"); 40 return (String) getCallingPackageMethod.invoke(session); 41 } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { 42 Log.e(TAG, "Cannot execute MediaSession.getCallingPackage()", e); 43 } 44 return null; 45 } 46 47 public interface Callback extends MediaSessionCompatApi23.Callback { onPrepare()48 public void onPrepare(); onPrepareFromMediaId(String mediaId, Bundle extras)49 public void onPrepareFromMediaId(String mediaId, Bundle extras); onPrepareFromSearch(String query, Bundle extras)50 public void onPrepareFromSearch(String query, Bundle extras); onPrepareFromUri(Uri uri, Bundle extras)51 public void onPrepareFromUri(Uri uri, Bundle extras); 52 } 53 54 static class CallbackProxy<T extends Callback> 55 extends MediaSessionCompatApi23.CallbackProxy<T> { CallbackProxy(T callback)56 public CallbackProxy(T callback) { 57 super(callback); 58 } 59 60 @Override onPrepare()61 public void onPrepare() { 62 mCallback.onPrepare(); 63 } 64 65 @Override onPrepareFromMediaId(String mediaId, Bundle extras)66 public void onPrepareFromMediaId(String mediaId, Bundle extras) { 67 mCallback.onPrepareFromMediaId(mediaId, extras); 68 } 69 70 @Override onPrepareFromSearch(String query, Bundle extras)71 public void onPrepareFromSearch(String query, Bundle extras) { 72 mCallback.onPrepareFromSearch(query, extras); 73 } 74 75 @Override onPrepareFromUri(Uri uri, Bundle extras)76 public void onPrepareFromUri(Uri uri, Bundle extras) { 77 mCallback.onPrepareFromUri(uri, extras); 78 } 79 } 80 } 81