109 lines
4.3 KiB
Java
109 lines
4.3 KiB
Java
package androidx.media.session;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
import android.content.pm.ResolveInfo;
|
|
import android.os.Build;
|
|
import android.os.RemoteException;
|
|
import android.support.v4.media.MediaBrowserCompat;
|
|
import android.support.v4.media.session.MediaControllerCompat;
|
|
import android.util.Log;
|
|
import android.view.KeyEvent;
|
|
import java.util.List;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class MediaButtonReceiver extends BroadcastReceiver {
|
|
|
|
private static class MediaButtonConnectionCallback extends MediaBrowserCompat.ConnectionCallback {
|
|
private final Context c;
|
|
private final Intent d;
|
|
private final BroadcastReceiver.PendingResult e;
|
|
private MediaBrowserCompat f;
|
|
|
|
MediaButtonConnectionCallback(Context context, Intent intent, BroadcastReceiver.PendingResult pendingResult) {
|
|
this.c = context;
|
|
this.d = intent;
|
|
this.e = pendingResult;
|
|
}
|
|
|
|
private void d() {
|
|
this.f.b();
|
|
this.e.finish();
|
|
}
|
|
|
|
void a(MediaBrowserCompat mediaBrowserCompat) {
|
|
this.f = mediaBrowserCompat;
|
|
}
|
|
|
|
@Override // android.support.v4.media.MediaBrowserCompat.ConnectionCallback
|
|
public void b() {
|
|
d();
|
|
}
|
|
|
|
@Override // android.support.v4.media.MediaBrowserCompat.ConnectionCallback
|
|
public void c() {
|
|
d();
|
|
}
|
|
|
|
@Override // android.support.v4.media.MediaBrowserCompat.ConnectionCallback
|
|
public void a() {
|
|
try {
|
|
new MediaControllerCompat(this.c, this.f.c()).a((KeyEvent) this.d.getParcelableExtra("android.intent.extra.KEY_EVENT"));
|
|
} catch (RemoteException e) {
|
|
Log.e("MediaButtonReceiver", "Failed to create a media controller", e);
|
|
}
|
|
d();
|
|
}
|
|
}
|
|
|
|
private static void a(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 a = a(context, "android.intent.action.MEDIA_BUTTON");
|
|
if (a != null) {
|
|
intent.setComponent(a);
|
|
a(context, intent);
|
|
return;
|
|
}
|
|
ComponentName a2 = a(context, "android.media.browse.MediaBrowserService");
|
|
if (a2 == null) {
|
|
throw new IllegalStateException("Could not find any Service that handles android.intent.action.MEDIA_BUTTON or implements a media browser service.");
|
|
}
|
|
BroadcastReceiver.PendingResult goAsync = goAsync();
|
|
Context applicationContext = context.getApplicationContext();
|
|
MediaButtonConnectionCallback mediaButtonConnectionCallback = new MediaButtonConnectionCallback(applicationContext, intent, goAsync);
|
|
MediaBrowserCompat mediaBrowserCompat = new MediaBrowserCompat(applicationContext, a2, mediaButtonConnectionCallback, null);
|
|
mediaButtonConnectionCallback.a(mediaBrowserCompat);
|
|
mediaBrowserCompat.a();
|
|
}
|
|
|
|
private static ComponentName a(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) {
|
|
ResolveInfo resolveInfo = queryIntentServices.get(0);
|
|
return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
|
|
}
|
|
if (queryIntentServices.isEmpty()) {
|
|
return null;
|
|
}
|
|
throw new IllegalStateException("Expected 1 service that handles " + str + ", found " + queryIntentServices.size());
|
|
}
|
|
}
|