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 
17 package com.android.bluetooth.audio_util;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.media.session.MediaSession;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.util.Log;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 
28 /**
29  * Provide a mockable interface in order to test classes that use MediaBrowser.
30  * We need this class due to the fact that the MediaController class is marked as final and
31  * there is no way to currently mock final classes in Android. Once this is possible this class
32  * can be deleted.
33  */
34 public class MediaBrowser {
35     android.media.browse.MediaBrowser mDelegate;
36 
37     /**
38      * Wrap a real MediaBrowser object
39      */
MediaBrowser(android.media.browse.MediaBrowser delegate)40     public MediaBrowser(android.media.browse.MediaBrowser delegate) {
41         mDelegate = delegate;
42     }
43 
44     /**
45      * Create a real MediaBrowser object and wrap it
46      */
MediaBrowser(Context context, ComponentName serviceComponent, ConnectionCallback callback, Bundle rootHints)47     public MediaBrowser(Context context, ComponentName serviceComponent,
48             ConnectionCallback callback, Bundle rootHints) {
49         mDelegate = new android.media.browse.MediaBrowser(context, serviceComponent, callback,
50                 rootHints);
51     }
52 
53     /**
54      * Wrapper for MediaBrowser.ConnectionCallback
55      */
56     public abstract static class ConnectionCallback extends
57             android.media.browse.MediaBrowser.ConnectionCallback {}
58 
59     /**
60      * Wrapper for MediaBrowser.ItemCallback
61      */
62     public abstract static class ItemCallback extends
63             android.media.browse.MediaBrowser.ItemCallback {}
64 
65     /**
66      * Wrapper for MediaBrowser.SubscriptionCallback
67      */
68     public abstract static class SubscriptionCallback extends
69             android.media.browse.MediaBrowser.SubscriptionCallback {
70         /**
71          * Wrapper for MediaBrowser.SubscriptionCallback.getTimeoutHandler()
72          */
getTimeoutHandler()73         public abstract Handler getTimeoutHandler();
74     }
75 
76     /**
77      * Wrapper for MediaBrowser.connect()
78      */
connect()79     public void connect() {
80         mDelegate.connect();
81     }
82 
83     /**
84      * Wrapper for MediaBrowser.disconnect()
85      */
disconnect()86     public void disconnect() {
87         mDelegate.disconnect();
88     }
89 
90     /**
91      * Wrapper for MediaBrowser.getExtras()
92      */
getExtras()93     public Bundle getExtras() {
94         return mDelegate.getExtras();
95     }
96 
97     /**
98      * Wrapper for MediaBrowser.getItem(String mediaId, ItemCallback callback)
99      */
getItem(String mediaId, ItemCallback callback)100     public void getItem(String mediaId, ItemCallback callback) {
101         mDelegate.getItem(mediaId, callback);
102     }
103 
104     /**
105      * Wrapper for MediaBrowser.getRoot()
106      */
getRoot()107     public String getRoot() {
108         return mDelegate.getRoot();
109     }
110 
111     /**
112      * Wrapper for MediaBrowser.getServiceComponent()
113      */
getServiceComponent()114     public ComponentName getServiceComponent() {
115         return mDelegate.getServiceComponent();
116     }
117 
118     /**
119      * Wrapper for MediaBrowser.getSessionToken()
120      */
getSessionToken()121     public MediaSession.Token getSessionToken() {
122         return mDelegate.getSessionToken();
123     }
124     /**
125      * Wrapper for MediaBrowser.isConnected()
126      */
isConnected()127     public boolean isConnected() {
128         return mDelegate.isConnected();
129     }
130 
131     /**
132      * Wrapper for MediaBrowser.subscribe(String parentId, Bundle options,
133      * SubscriptionCallback callback)
134      */
subscribe(String parentId, Bundle options, SubscriptionCallback callback)135     public void subscribe(String parentId, Bundle options, SubscriptionCallback callback) {
136         mDelegate.subscribe(parentId, options, callback);
137     }
138 
139     /**
140      * Wrapper for MediaBrowser.subscribe(String parentId, SubscriptionCallback callback)
141      */
subscribe(String parentId, SubscriptionCallback callback)142     public void subscribe(String parentId, SubscriptionCallback callback) {
143         mDelegate.subscribe(parentId, callback);
144     }
145 
146     /**
147      * Wrapper for MediaBrowser.unsubscribe(String parentId)
148      */
unsubscribe(String parentId)149     public void unsubscribe(String parentId) {
150         mDelegate.unsubscribe(parentId);
151     }
152 
153 
154     /**
155      * Wrapper for MediaBrowser.unsubscribe(String parentId, SubscriptionCallback callback)
156      */
unsubscribe(String parentId, SubscriptionCallback callback)157     public void unsubscribe(String parentId, SubscriptionCallback callback) {
158         mDelegate.unsubscribe(parentId, callback);
159     }
160 
161     /**
162      * A function that allows Mockito to capture the constructor arguments when using
163      * MediaBrowserFactory.make()
164      */
165     @VisibleForTesting
testInit(Context context, ComponentName serviceComponent, ConnectionCallback callback, Bundle rootHints)166     public void testInit(Context context, ComponentName serviceComponent,
167             ConnectionCallback callback, Bundle rootHints) {
168         // This is only used by Mockito to capture the constructor arguments on creation
169         Log.wtf("AvrcpMockMediaBrowser", "This function should never be called");
170     }
171 }
172