|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. All rights reserved. |
| 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 | +package com.example.android.mediacontroller; |
| 17 | + |
| 18 | +import android.content.ComponentName; |
| 19 | +import android.content.Context; |
| 20 | +import android.content.pm.PackageManager; |
| 21 | +import android.content.pm.ServiceInfo; |
| 22 | +import android.graphics.drawable.Drawable; |
| 23 | +import android.media.session.MediaSession; |
| 24 | +import android.support.v4.media.MediaBrowserCompat; |
| 25 | +import android.support.v4.media.session.MediaSessionCompat; |
| 26 | + |
| 27 | +/** |
| 28 | + * Represents a media app, used to populate entries in LaunchActivity. |
| 29 | + */ |
| 30 | +public abstract class MediaAppEntry { |
| 31 | + |
| 32 | + public final String appName; |
| 33 | + public final String packageName; |
| 34 | + public final Drawable icon; |
| 35 | + |
| 36 | + private MediaAppEntry(String name, String pkg, Drawable appIcon) { |
| 37 | + appName = name; |
| 38 | + packageName = pkg; |
| 39 | + icon = appIcon; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Callback for session token, since connecting to a browse service and obtaining a session |
| 44 | + * token is an asynchronous operation. |
| 45 | + */ |
| 46 | + public interface SessionTokenAvailableCallback { |
| 47 | + |
| 48 | + void onSuccess(MediaSessionCompat.Token sessionToken); |
| 49 | + |
| 50 | + void onFailure(); |
| 51 | + } |
| 52 | + |
| 53 | + public abstract void getSessionToken(Context context, SessionTokenAvailableCallback callback); |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean equals(Object o) { |
| 57 | + if (this == o) return true; |
| 58 | + if (o == null || getClass() != o.getClass()) return false; |
| 59 | + MediaAppEntry that = (MediaAppEntry) o; |
| 60 | + return appName.equals(that.appName) && packageName.equals(that.packageName); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public int hashCode() { |
| 65 | + int result = appName.hashCode(); |
| 66 | + result = 31 * result + packageName.hashCode(); |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + public static MediaAppEntry fromSessionToken(MediaSession.Token sessionToken, |
| 71 | + String appName, |
| 72 | + String packageName, |
| 73 | + Drawable appIcon) { |
| 74 | + return new MediaAppEntry(appName, packageName, appIcon) { |
| 75 | + @Override |
| 76 | + public void getSessionToken(Context context, SessionTokenAvailableCallback callback) { |
| 77 | + callback.onSuccess(MediaSessionCompat.Token.fromToken(sessionToken)); |
| 78 | + } |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + public static MediaAppEntry fromBrowseService(ServiceInfo serviceInfo, PackageManager pm) { |
| 83 | + String appName = serviceInfo.loadLabel(pm).toString(); |
| 84 | + Drawable appIcon = serviceInfo.loadIcon(pm); |
| 85 | + ComponentName browseService = new ComponentName(serviceInfo.packageName, serviceInfo.name); |
| 86 | + |
| 87 | + return new MediaAppEntry(appName, serviceInfo.packageName, appIcon) { |
| 88 | + private MediaBrowserCompat browser; |
| 89 | + |
| 90 | + @Override |
| 91 | + public void getSessionToken(Context context, SessionTokenAvailableCallback callback) { |
| 92 | + browser = new MediaBrowserCompat(context, browseService, |
| 93 | + new MediaBrowserCompat.ConnectionCallback() { |
| 94 | + @Override |
| 95 | + public void onConnected() { |
| 96 | + callback.onSuccess(browser.getSessionToken()); |
| 97 | + // Once we've obtained the session token, we no longer need to keep |
| 98 | + // an open connection to the browse service. |
| 99 | + browser.disconnect(); |
| 100 | + browser = null; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onConnectionFailed() { |
| 105 | + callback.onFailure(); |
| 106 | + } |
| 107 | + }, null); |
| 108 | + browser.connect(); |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | +} |
0 commit comments