summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/media/controls/resume/ResumeMediaBrowser.java
blob: d460b5b5d7823c44c1f08cddec50c0c962162f2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.media.controls.resume;

import android.annotation.Nullable;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaDescription;
import android.media.browse.MediaBrowser;
import android.media.session.MediaController;
import android.media.session.MediaSession;
import android.os.Bundle;
import android.service.media.MediaBrowserService;
import android.text.TextUtils;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;

import java.util.List;

/**
 * Media browser for managing resumption in media controls
 */
public class ResumeMediaBrowser {

    /** Maximum number of controls to show on boot */
    public static final int MAX_RESUMPTION_CONTROLS = 5;

    /** Delimiter for saved component names */
    public static final String DELIMITER = ":";

    private static final String TAG = "ResumeMediaBrowser";
    private final Context mContext;
    @Nullable private final Callback mCallback;
    private final MediaBrowserFactory mBrowserFactory;
    private final ResumeMediaBrowserLogger mLogger;
    private final ComponentName mComponentName;
    private final MediaController.Callback mMediaControllerCallback = new SessionDestroyCallback();

    private MediaBrowser mMediaBrowser;
    @Nullable private MediaController mMediaController;

    /**
     * Initialize a new media browser
     * @param context the context
     * @param callback used to report media items found
     * @param componentName Component name of the MediaBrowserService this browser will connect to
     */
    public ResumeMediaBrowser(
            Context context,
            @Nullable Callback callback,
            ComponentName componentName,
            MediaBrowserFactory browserFactory,
            ResumeMediaBrowserLogger logger) {
        mContext = context;
        mCallback = callback;
        mComponentName = componentName;
        mBrowserFactory = browserFactory;
        mLogger = logger;
    }

    /**
     * Connects to the MediaBrowserService and looks for valid media. If a media item is returned,
     * ResumeMediaBrowser.Callback#addTrack will be called with the MediaDescription.
     * ResumeMediaBrowser.Callback#onConnected and ResumeMediaBrowser.Callback#onError will also be
     * called when the initial connection is successful, or an error occurs.
     * Note that it is possible for the service to connect but for no playable tracks to be found.
     * ResumeMediaBrowser#disconnect will be called automatically with this function.
     */
    public void findRecentMedia() {
        Bundle rootHints = new Bundle();
        rootHints.putBoolean(MediaBrowserService.BrowserRoot.EXTRA_RECENT, true);
        MediaBrowser browser = mBrowserFactory.create(
                mComponentName,
                mConnectionCallback,
                rootHints);
        connectBrowser(browser, "findRecentMedia");
    }

    private final MediaBrowser.SubscriptionCallback mSubscriptionCallback =
            new MediaBrowser.SubscriptionCallback() {
        @Override
        public void onChildrenLoaded(String parentId,
                List<MediaBrowser.MediaItem> children) {
            if (children.size() == 0) {
                Log.d(TAG, "No children found for " + mComponentName);
                if (mCallback != null) {
                    mCallback.onError();
                }
            } else {
                // We ask apps to return a playable item as the first child when sending
                // a request with EXTRA_RECENT; if they don't, no resume controls
                MediaBrowser.MediaItem child = children.get(0);
                MediaDescription desc = child.getDescription();
                if (child.isPlayable() && mMediaBrowser != null) {
                    if (mCallback != null) {
                        mCallback.addTrack(desc, mMediaBrowser.getServiceComponent(),
                                ResumeMediaBrowser.this);
                    }
                } else {
                    Log.d(TAG, "Child found but not playable for " + mComponentName);
                    if (mCallback != null) {
                        mCallback.onError();
                    }
                }
            }
            disconnect();
        }

        @Override
        public void onError(String parentId) {
            Log.d(TAG, "Subscribe error for " + mComponentName + ": " + parentId);
            if (mCallback != null) {
                mCallback.onError();
            }
            disconnect();
        }

        @Override
        public void onError(String parentId, Bundle options) {
            Log.d(TAG, "Subscribe error for " + mComponentName + ": " + parentId
                    + ", options: " + options);
            if (mCallback != null) {
                mCallback.onError();
            }
            disconnect();
        }
    };

    private final MediaBrowser.ConnectionCallback mConnectionCallback =
            new MediaBrowser.ConnectionCallback() {
        /**
         * Invoked after {@link MediaBrowser#connect()} when the request has successfully completed.
         * For resumption controls, apps are expected to return a playable media item as the first
         * child. If there are no children or it isn't playable it will be ignored.
         */
        @Override
        public void onConnected() {
            Log.d(TAG, "Service connected for " + mComponentName);
            updateMediaController();
            if (isBrowserConnected()) {
                String root = mMediaBrowser.getRoot();
                if (!TextUtils.isEmpty(root)) {
                    if (mCallback != null) {
                        mCallback.onConnected();
                    }
                    if (mMediaBrowser != null) {
                        mMediaBrowser.subscribe(root, mSubscriptionCallback);
                    }
                    return;
                }
            }
            if (mCallback != null) {
                mCallback.onError();
            }
            disconnect();
        }

        /**
         * Invoked when the client is disconnected from the media browser.
         */
        @Override
        public void onConnectionSuspended() {
            Log.d(TAG, "Connection suspended for " + mComponentName);
            if (mCallback != null) {
                mCallback.onError();
            }
            disconnect();
        }

        /**
         * Invoked when the connection to the media browser failed.
         */
        @Override
        public void onConnectionFailed() {
            Log.d(TAG, "Connection failed for " + mComponentName);
            if (mCallback != null) {
                mCallback.onError();
            }
            disconnect();
        }
    };

    /**
     * Connect using a new media browser. Disconnects the existing browser first, if it exists.
     * @param browser media browser to connect
     * @param reason Reason to log for connection
     */
    private void connectBrowser(MediaBrowser browser, String reason) {
        mLogger.logConnection(mComponentName, reason);
        disconnect();
        mMediaBrowser = browser;
        if (browser != null) {
            browser.connect();
        }
        updateMediaController();
    }

    /**
     * Disconnect the media browser. This should be done after callbacks have completed to
     * disconnect from the media browser service.
     */
    protected void disconnect() {
        if (mMediaBrowser != null) {
            mLogger.logDisconnect(mComponentName);
            mMediaBrowser.disconnect();
        }
        mMediaBrowser = null;
        updateMediaController();
    }

    /**
     * Connects to the MediaBrowserService and starts playback.
     * ResumeMediaBrowser.Callback#onError or ResumeMediaBrowser.Callback#onConnected will be called
     * depending on whether it was successful.
     * If the connection is successful, the listener should call ResumeMediaBrowser#disconnect after
     * getting a media update from the app
     */
    public void restart() {
        Bundle rootHints = new Bundle();
        rootHints.putBoolean(MediaBrowserService.BrowserRoot.EXTRA_RECENT, true);
        MediaBrowser browser = mBrowserFactory.create(mComponentName,
                new MediaBrowser.ConnectionCallback() {
                    @Override
                    public void onConnected() {
                        Log.d(TAG, "Connected for restart " + mMediaBrowser.isConnected());
                        updateMediaController();
                        if (!isBrowserConnected()) {
                            if (mCallback != null) {
                                mCallback.onError();
                            }
                            disconnect();
                            return;
                        }
                        MediaSession.Token token = mMediaBrowser.getSessionToken();
                        MediaController controller = createMediaController(token);
                        controller.getTransportControls();
                        controller.getTransportControls().prepare();
                        controller.getTransportControls().play();
                        if (mCallback != null) {
                            mCallback.onConnected();
                        }
                        // listener should disconnect after media player update
                    }

                    @Override
                    public void onConnectionFailed() {
                        if (mCallback != null) {
                            mCallback.onError();
                        }
                        disconnect();
                    }

                    @Override
                    public void onConnectionSuspended() {
                        if (mCallback != null) {
                            mCallback.onError();
                        }
                        disconnect();
                    }
                }, rootHints);
        connectBrowser(browser, "restart");
    }

    @VisibleForTesting
    protected MediaController createMediaController(MediaSession.Token token) {
        return new MediaController(mContext, token);
    }

    /**
     * Get the media session token
     * @return the token, or null if the MediaBrowser is null or disconnected
     */
    public MediaSession.Token getToken() {
        if (!isBrowserConnected()) {
            return null;
        }
        return mMediaBrowser.getSessionToken();
    }

    /**
     * Get an intent to launch the app associated with this browser service
     * @return
     */
    public PendingIntent getAppIntent() {
        PackageManager pm = mContext.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage(mComponentName.getPackageName());
        return PendingIntent.getActivity(mContext, 0, launchIntent, PendingIntent.FLAG_IMMUTABLE);
    }

    /**
     * Used to test if SystemUI is allowed to connect to the given component as a MediaBrowser.
     * If it can connect, ResumeMediaBrowser.Callback#onConnected will be called. If valid media is
     * found, then ResumeMediaBrowser.Callback#addTrack will also be called. This allows for more
     * detailed logging if the service has issues. If it cannot connect, or cannot find valid media,
     * then ResumeMediaBrowser.Callback#onError will be called.
     * ResumeMediaBrowser#disconnect should be called after this to ensure the connection is closed.
     */
    public void testConnection() {
        Bundle rootHints = new Bundle();
        rootHints.putBoolean(MediaBrowserService.BrowserRoot.EXTRA_RECENT, true);
        MediaBrowser browser = mBrowserFactory.create(
                mComponentName,
                mConnectionCallback,
                rootHints);
        connectBrowser(browser, "testConnection");
    }

    /** Updates mMediaController based on our current browser values. */
    private void updateMediaController() {
        MediaSession.Token controllerToken =
                mMediaController != null ? mMediaController.getSessionToken() : null;
        MediaSession.Token currentToken = getToken();
        boolean areEqual = (controllerToken == null && currentToken == null)
                || (controllerToken != null && controllerToken.equals(currentToken));
        if (areEqual) {
            return;
        }

        // Whenever the token changes, un-register the callback on the old controller (if we have
        // one) and create a new controller with the callback attached.
        if (mMediaController != null) {
            mMediaController.unregisterCallback(mMediaControllerCallback);
        }
        if (currentToken != null) {
            mMediaController = createMediaController(currentToken);
            mMediaController.registerCallback(mMediaControllerCallback);
        } else {
            mMediaController = null;
        }
    }

    private boolean isBrowserConnected() {
        return mMediaBrowser != null && mMediaBrowser.isConnected();
    }

    /**
     * Interface to handle results from ResumeMediaBrowser
     */
    public static class Callback {
        /**
         * Called when the browser has successfully connected to the service
         */
        public void onConnected() {
        }

        /**
         * Called when the browser encountered an error connecting to the service
         */
        public void onError() {
        }

        /**
         * Called when the browser finds a suitable track to add to the media carousel
         * @param track media info for the item
         * @param component component of the MediaBrowserService which returned this
         * @param browser reference to the browser
         */
        public void addTrack(MediaDescription track, ComponentName component,
                ResumeMediaBrowser browser) {
        }
    }

    private class SessionDestroyCallback extends MediaController.Callback {
        @Override
        public void onSessionDestroyed() {
            mLogger.logSessionDestroyed(isBrowserConnected(), mComponentName);
            disconnect();
        }
    }
}