package androidx.media3.session;

import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import androidx.core.graphics.drawable.IconCompat;
import androidx.media3.common.C;
import androidx.media3.session.MediaNotification;
import defpackage.if7;
import defpackage.l18;
import defpackage.rq6;
import defpackage.wp6;

/* JADX INFO: loaded from: classes.dex */
final class DefaultActionFactory implements MediaNotification.ActionFactory {
    private static final String ACTION_CUSTOM = "androidx.media3.session.CUSTOM_NOTIFICATION_ACTION";
    private static final String EXTRAS_KEY_ACTION_CUSTOM = "androidx.media3.session.EXTRAS_KEY_CUSTOM_NOTIFICATION_ACTION";
    public static final String EXTRAS_KEY_ACTION_CUSTOM_EXTRAS = "androidx.media3.session.EXTRAS_KEY_CUSTOM_NOTIFICATION_ACTION_EXTRAS";
    private int customActionPendingIntentRequestCode = 0;
    private final Service service;

    @l18
    public static final class Api26 {
        private Api26() {
        }

        public static PendingIntent createForegroundServicePendingIntent(Service service, int i, Intent intent) {
            return PendingIntent.getForegroundService(service, i, intent, C.BUFFER_FLAG_NOT_DEPENDED_ON);
        }
    }

    public DefaultActionFactory(Service service) {
        this.service = service;
    }

    private PendingIntent createCustomActionPendingIntent(MediaSession mediaSession, String str, Bundle bundle) {
        Intent intent = new Intent(ACTION_CUSTOM);
        intent.setData(mediaSession.getImpl().getUri());
        Service service = this.service;
        intent.setComponent(new ComponentName(service, service.getClass()));
        intent.putExtra(EXTRAS_KEY_ACTION_CUSTOM, str);
        intent.putExtra(EXTRAS_KEY_ACTION_CUSTOM_EXTRAS, bundle);
        Service service2 = this.service;
        int i = this.customActionPendingIntentRequestCode + 1;
        this.customActionPendingIntentRequestCode = i;
        return PendingIntent.getService(service2, i, intent, 201326592);
    }

    @rq6
    public static KeyEvent getKeyEvent(Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras == null || !extras.containsKey("android.intent.extra.KEY_EVENT")) {
            return null;
        }
        return (KeyEvent) extras.getParcelable("android.intent.extra.KEY_EVENT");
    }

    private Intent getMediaButtonIntent(MediaSession mediaSession, int i) {
        Intent intent = new Intent("android.intent.action.MEDIA_BUTTON");
        intent.setData(mediaSession.getImpl().getUri());
        Service service = this.service;
        intent.setComponent(new ComponentName(service, service.getClass()));
        intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, i));
        return intent;
    }

    private int toKeyCode(long j) {
        if (j == 8 || j == 9) {
            return 87;
        }
        if (j == 6 || j == 7) {
            return 88;
        }
        if (j == 3) {
            return 86;
        }
        if (j == 12) {
            return 90;
        }
        if (j == 11) {
            return 89;
        }
        return j == 1 ? 85 : 0;
    }

    @Override // androidx.media3.session.MediaNotification.ActionFactory
    public wp6.b createCustomAction(MediaSession mediaSession, IconCompat iconCompat, CharSequence charSequence, String str, Bundle bundle) {
        return new wp6.b(iconCompat, charSequence, createCustomActionPendingIntent(mediaSession, str, bundle));
    }

    @Override // androidx.media3.session.MediaNotification.ActionFactory
    public wp6.b createCustomActionFromCustomCommandButton(MediaSession mediaSession, CommandButton commandButton) {
        SessionCommand sessionCommand = commandButton.sessionCommand;
        if7.e(sessionCommand != null && sessionCommand.commandCode == 0);
        SessionCommand sessionCommand2 = commandButton.sessionCommand;
        sessionCommand2.getClass();
        return new wp6.b(IconCompat.c(this.service, commandButton.iconResId), commandButton.displayName, createCustomActionPendingIntent(mediaSession, sessionCommand2.customAction, sessionCommand2.customExtras));
    }

    @Override // androidx.media3.session.MediaNotification.ActionFactory
    public wp6.b createMediaAction(MediaSession mediaSession, IconCompat iconCompat, CharSequence charSequence, int i) {
        return new wp6.b(iconCompat, charSequence, createMediaActionPendingIntent(mediaSession, i));
    }

    @Override // androidx.media3.session.MediaNotification.ActionFactory
    public PendingIntent createMediaActionPendingIntent(MediaSession mediaSession, long j) {
        int keyCode = toKeyCode(j);
        Intent mediaButtonIntent = getMediaButtonIntent(mediaSession, keyCode);
        return (j != 1 || mediaSession.getPlayer().getPlayWhenReady()) ? PendingIntent.getService(this.service, keyCode, mediaButtonIntent, C.BUFFER_FLAG_NOT_DEPENDED_ON) : Api26.createForegroundServicePendingIntent(this.service, keyCode, mediaButtonIntent);
    }

    @Override // androidx.media3.session.MediaNotification.ActionFactory
    public PendingIntent createNotificationDismissalIntent(MediaSession mediaSession) {
        return PendingIntent.getService(this.service, 86, getMediaButtonIntent(mediaSession, 86).putExtra(MediaNotification.NOTIFICATION_DISMISSED_EVENT_KEY, true), C.BUFFER_FLAG_NOT_DEPENDED_ON);
    }

    @rq6
    public String getCustomAction(Intent intent) {
        Bundle extras = intent.getExtras();
        Object obj = extras != null ? extras.get(EXTRAS_KEY_ACTION_CUSTOM) : null;
        if (obj instanceof String) {
            return (String) obj;
        }
        return null;
    }

    public Bundle getCustomActionExtras(Intent intent) {
        Bundle extras = intent.getExtras();
        Object obj = extras != null ? extras.get(EXTRAS_KEY_ACTION_CUSTOM_EXTRAS) : null;
        return obj instanceof Bundle ? (Bundle) obj : Bundle.EMPTY;
    }

    public boolean isCustomAction(Intent intent) {
        return ACTION_CUSTOM.equals(intent.getAction());
    }

    public boolean isMediaAction(Intent intent) {
        return "android.intent.action.MEDIA_BUTTON".equals(intent.getAction());
    }
}
