191 lines
9.1 KiB
Java
191 lines
9.1 KiB
Java
package androidx.media.session;
|
|
|
|
import android.app.PendingIntent;
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.pm.ActivityInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.content.pm.ResolveInfo;
|
|
import android.content.pm.ServiceInfo;
|
|
import android.os.Build;
|
|
import android.os.Messenger;
|
|
import android.os.RemoteException;
|
|
import android.support.v4.media.MediaBrowserCompat;
|
|
import android.support.v4.media.session.MediaControllerCompat$MediaControllerImplApi21;
|
|
import android.support.v4.media.session.MediaSessionCompat;
|
|
import android.util.Log;
|
|
import android.view.KeyEvent;
|
|
import androidx.annotation.RestrictTo;
|
|
import c.d.b.a.a;
|
|
import java.util.List;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
public class MediaButtonReceiver extends BroadcastReceiver {
|
|
private static final String TAG = "MediaButtonReceiver";
|
|
|
|
public static class MediaButtonConnectionCallback extends MediaBrowserCompat.b {
|
|
private final Context mContext;
|
|
private final Intent mIntent;
|
|
private MediaBrowserCompat mMediaBrowser;
|
|
private final BroadcastReceiver.PendingResult mPendingResult;
|
|
|
|
public MediaButtonConnectionCallback(Context context, Intent intent, BroadcastReceiver.PendingResult pendingResult) {
|
|
this.mContext = context;
|
|
this.mIntent = intent;
|
|
this.mPendingResult = pendingResult;
|
|
}
|
|
|
|
private void finish() {
|
|
Messenger messenger;
|
|
MediaBrowserCompat.d dVar = (MediaBrowserCompat.d) this.mMediaBrowser.b;
|
|
MediaBrowserCompat.h hVar = dVar.f;
|
|
if (!(hVar == null || (messenger = dVar.g) == null)) {
|
|
try {
|
|
hVar.a(7, null, messenger);
|
|
} catch (RemoteException unused) {
|
|
Log.i("MediaBrowserCompat", "Remote error unregistering client messenger.");
|
|
}
|
|
}
|
|
dVar.b.disconnect();
|
|
this.mPendingResult.finish();
|
|
}
|
|
|
|
@Override // android.support.v4.media.MediaBrowserCompat.b
|
|
public void onConnected() {
|
|
Context context = this.mContext;
|
|
MediaBrowserCompat.d dVar = (MediaBrowserCompat.d) this.mMediaBrowser.b;
|
|
if (dVar.h == null) {
|
|
dVar.h = MediaSessionCompat.Token.a(dVar.b.getSessionToken(), null);
|
|
}
|
|
MediaSessionCompat.Token token = dVar.h;
|
|
new ConcurrentHashMap();
|
|
if (token != null) {
|
|
MediaControllerCompat$MediaControllerImplApi21 mediaControllerCompat$MediaControllerImplApi21 = new MediaControllerCompat$MediaControllerImplApi21(context, token);
|
|
KeyEvent keyEvent = (KeyEvent) this.mIntent.getParcelableExtra("android.intent.extra.KEY_EVENT");
|
|
if (keyEvent != null) {
|
|
mediaControllerCompat$MediaControllerImplApi21.a.dispatchMediaButtonEvent(keyEvent);
|
|
finish();
|
|
return;
|
|
}
|
|
throw new IllegalArgumentException("KeyEvent may not be null");
|
|
}
|
|
throw new IllegalArgumentException("sessionToken must not be null");
|
|
}
|
|
|
|
@Override // android.support.v4.media.MediaBrowserCompat.b
|
|
public void onConnectionFailed() {
|
|
finish();
|
|
}
|
|
|
|
@Override // android.support.v4.media.MediaBrowserCompat.b
|
|
public void onConnectionSuspended() {
|
|
finish();
|
|
}
|
|
|
|
public void setMediaBrowser(MediaBrowserCompat mediaBrowserCompat) {
|
|
this.mMediaBrowser = mediaBrowserCompat;
|
|
}
|
|
}
|
|
|
|
public static PendingIntent buildMediaButtonPendingIntent(Context context, long j) {
|
|
ComponentName mediaButtonReceiverComponent = getMediaButtonReceiverComponent(context);
|
|
if (mediaButtonReceiverComponent != null) {
|
|
return buildMediaButtonPendingIntent(context, mediaButtonReceiverComponent, j);
|
|
}
|
|
Log.w("MediaButtonReceiver", "A unique media button receiver could not be found in the given context, so couldn't build a pending intent.");
|
|
return null;
|
|
}
|
|
|
|
public static PendingIntent buildMediaButtonPendingIntent(Context context, ComponentName componentName, long j) {
|
|
if (componentName == null) {
|
|
Log.w("MediaButtonReceiver", "The component name of media button receiver should be provided.");
|
|
return null;
|
|
}
|
|
int i = j == 4 ? 126 : j == 2 ? 127 : j == 32 ? 87 : j == 16 ? 88 : j == 1 ? 86 : j == 64 ? 90 : j == 8 ? 89 : j == 512 ? 85 : 0;
|
|
if (i == 0) {
|
|
Log.w("MediaButtonReceiver", "Cannot build a media button pending intent with the given action: " + j);
|
|
return null;
|
|
}
|
|
Intent intent = new Intent("android.intent.action.MEDIA_BUTTON");
|
|
intent.setComponent(componentName);
|
|
intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, i));
|
|
return PendingIntent.getBroadcast(context, i, intent, 0);
|
|
}
|
|
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY})
|
|
public static ComponentName getMediaButtonReceiverComponent(Context context) {
|
|
Intent intent = new Intent("android.intent.action.MEDIA_BUTTON");
|
|
intent.setPackage(context.getPackageName());
|
|
List<ResolveInfo> queryBroadcastReceivers = context.getPackageManager().queryBroadcastReceivers(intent, 0);
|
|
if (queryBroadcastReceivers.size() == 1) {
|
|
ActivityInfo activityInfo = queryBroadcastReceivers.get(0).activityInfo;
|
|
return new ComponentName(activityInfo.packageName, activityInfo.name);
|
|
} else if (queryBroadcastReceivers.size() <= 1) {
|
|
return null;
|
|
} else {
|
|
Log.w("MediaButtonReceiver", "More than one BroadcastReceiver that handles android.intent.action.MEDIA_BUTTON was found, returning null.");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static ComponentName getServiceComponentByAction(Context context, String str) {
|
|
PackageManager packageManager = context.getPackageManager();
|
|
Intent intent = new Intent(str);
|
|
intent.setPackage(context.getPackageName());
|
|
List<ResolveInfo> queryIntentServices = packageManager.queryIntentServices(intent, 0);
|
|
if (queryIntentServices.size() == 1) {
|
|
ServiceInfo serviceInfo = queryIntentServices.get(0).serviceInfo;
|
|
return new ComponentName(serviceInfo.packageName, serviceInfo.name);
|
|
} else if (queryIntentServices.isEmpty()) {
|
|
return null;
|
|
} else {
|
|
StringBuilder Q = a.Q("Expected 1 service that handles ", str, ", found ");
|
|
Q.append(queryIntentServices.size());
|
|
throw new IllegalStateException(Q.toString());
|
|
}
|
|
}
|
|
|
|
public static KeyEvent handleIntent(MediaSessionCompat mediaSessionCompat, Intent intent) {
|
|
if (mediaSessionCompat == null || intent == null || !"android.intent.action.MEDIA_BUTTON".equals(intent.getAction()) || !intent.hasExtra("android.intent.extra.KEY_EVENT")) {
|
|
return null;
|
|
}
|
|
KeyEvent keyEvent = (KeyEvent) intent.getParcelableExtra("android.intent.extra.KEY_EVENT");
|
|
throw null;
|
|
}
|
|
|
|
private static void startForegroundService(Context context, Intent intent) {
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
context.startForegroundService(intent);
|
|
} else {
|
|
context.startService(intent);
|
|
}
|
|
}
|
|
|
|
@Override // android.content.BroadcastReceiver
|
|
public void onReceive(Context context, Intent intent) {
|
|
if (intent == null || !"android.intent.action.MEDIA_BUTTON".equals(intent.getAction()) || !intent.hasExtra("android.intent.extra.KEY_EVENT")) {
|
|
Log.d("MediaButtonReceiver", "Ignore unsupported intent: " + intent);
|
|
return;
|
|
}
|
|
ComponentName serviceComponentByAction = getServiceComponentByAction(context, "android.intent.action.MEDIA_BUTTON");
|
|
if (serviceComponentByAction != null) {
|
|
intent.setComponent(serviceComponentByAction);
|
|
startForegroundService(context, intent);
|
|
return;
|
|
}
|
|
ComponentName serviceComponentByAction2 = getServiceComponentByAction(context, "android.media.browse.MediaBrowserService");
|
|
if (serviceComponentByAction2 != null) {
|
|
BroadcastReceiver.PendingResult goAsync = goAsync();
|
|
Context applicationContext = context.getApplicationContext();
|
|
MediaButtonConnectionCallback mediaButtonConnectionCallback = new MediaButtonConnectionCallback(applicationContext, intent, goAsync);
|
|
MediaBrowserCompat mediaBrowserCompat = new MediaBrowserCompat(applicationContext, serviceComponentByAction2, mediaButtonConnectionCallback, null);
|
|
mediaButtonConnectionCallback.setMediaBrowser(mediaBrowserCompat);
|
|
Log.d("MediaBrowserCompat", "Connecting to a MediaBrowserService.");
|
|
((MediaBrowserCompat.d) mediaBrowserCompat.b).b.connect();
|
|
return;
|
|
}
|
|
throw new IllegalStateException("Could not find any Service that handles android.intent.action.MEDIA_BUTTON or implements a media browser service.");
|
|
}
|
|
}
|