package androidx.media3.session;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import androidx.core.graphics.drawable.IconCompat;
import androidx.media3.common.C;
import androidx.media3.common.MediaMetadata;
import androidx.media3.common.Player;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.session.CommandButton;
import androidx.media3.session.DefaultMediaNotificationProvider;
import androidx.media3.session.MediaNotification;
import androidx.media3.session.MediaSession;
import androidx.media3.session.MediaStyleNotificationHelper;
import defpackage.ae9;
import defpackage.ar1;
import defpackage.bj4;
import defpackage.c02;
import defpackage.ce9;
import defpackage.ds3;
import defpackage.hs0;
import defpackage.if7;
import defpackage.ks3;
import defpackage.l18;
import defpackage.of2;
import defpackage.q12;
import defpackage.rq6;
import defpackage.t79;
import defpackage.wp6;
import defpackage.zb5;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public class DefaultMediaNotificationProvider implements MediaNotification.Provider {
    public static final String COMMAND_KEY_COMPACT_VIEW_INDEX = "androidx.media3.session.command.COMPACT_VIEW_INDEX";
    public static final String DEFAULT_CHANNEL_ID = "default_channel_id";
    public static final int DEFAULT_NOTIFICATION_ID = 1001;
    public static final String GROUP_KEY = "media3_group_key";
    private static final String TAG = "NotificationProvider";
    private final String channelId;

    @t79
    private final int channelNameResourceId;
    private final Context context;

    @rq6
    private androidx.media3.common.util.BitmapLoader mediaSessionBitmapLoader;

    @rq6
    private androidx.media3.common.util.BitmapLoader notificationIconBitmapLoader;
    private final NotificationIdProvider notificationIdProvider;
    private final NotificationManager notificationManager;
    private OnBitmapLoadedFutureCallback pendingOnBitmapLoadedFutureCallback;

    @of2
    private int smallIconResourceId;

    @t79
    public static final int DEFAULT_CHANNEL_NAME_RESOURCE_ID = R.string.default_notification_channel_name;
    private static final ae9<Integer> maxNotificationIconSize = ce9.a(new ar1(1));

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

        public static void createNotificationChannel(NotificationManager notificationManager, String str, String str2) {
            notificationManager.createNotificationChannel(new NotificationChannel(str, str2, 2));
        }
    }

    @l18
    public static class Api31 {
        private Api31() {
        }

        public static void setForegroundServiceBehavior(wp6.o oVar) {
            oVar.aa = 1;
        }
    }

    public static final class Builder {
        private boolean built;
        private final Context context;
        private NotificationIdProvider notificationIdProvider = new c02(4);
        private String channelId = DefaultMediaNotificationProvider.DEFAULT_CHANNEL_ID;

        @t79
        private int channelNameResourceId = DefaultMediaNotificationProvider.DEFAULT_CHANNEL_NAME_RESOURCE_ID;

        public Builder(Context context) {
            this.context = context;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static /* synthetic */ int lambda$new$0(MediaSession mediaSession) {
            return 1001;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static /* synthetic */ int lambda$setNotificationId$1(int i, MediaSession mediaSession) {
            return i;
        }

        public DefaultMediaNotificationProvider build() {
            if7.o(!this.built);
            DefaultMediaNotificationProvider defaultMediaNotificationProvider = new DefaultMediaNotificationProvider(this);
            this.built = true;
            return defaultMediaNotificationProvider;
        }

        @hs0
        public Builder setChannelId(String str) {
            this.channelId = str;
            return this;
        }

        @hs0
        public Builder setChannelName(@t79 int i) {
            this.channelNameResourceId = i;
            return this;
        }

        @hs0
        public Builder setNotificationId(final int i) {
            this.notificationIdProvider = new NotificationIdProvider() { // from class: s02
                @Override // androidx.media3.session.DefaultMediaNotificationProvider.NotificationIdProvider
                public final int getNotificationId(MediaSession mediaSession) {
                    return DefaultMediaNotificationProvider.Builder.lambda$setNotificationId$1(i, mediaSession);
                }
            };
            return this;
        }

        @hs0
        public Builder setNotificationIdProvider(NotificationIdProvider notificationIdProvider) {
            this.notificationIdProvider = notificationIdProvider;
            return this;
        }
    }

    public interface NotificationIdProvider {
        int getNotificationId(MediaSession mediaSession);
    }

    public static class OnBitmapLoadedFutureCallback implements ds3<Bitmap> {
        private final wp6.o builder;
        private boolean discarded;
        private final int notificationId;
        private final MediaNotification.Provider.Callback onNotificationChangedCallback;

        public OnBitmapLoadedFutureCallback(int i, wp6.o oVar, MediaNotification.Provider.Callback callback) {
            this.notificationId = i;
            this.builder = oVar;
            this.onNotificationChangedCallback = callback;
        }

        public void discardIfPending() {
            this.discarded = true;
        }

        @Override // defpackage.ds3
        public void onFailure(Throwable th) {
            if (this.discarded) {
                return;
            }
            Log.w(DefaultMediaNotificationProvider.TAG, DefaultMediaNotificationProvider.getBitmapLoadErrorMessage(th));
        }

        @Override // defpackage.ds3
        public void onSuccess(Bitmap bitmap) {
            IconCompat iconCompat;
            if (this.discarded) {
                return;
            }
            wp6.o oVar = this.builder;
            if (bitmap == null) {
                iconCompat = null;
            } else {
                oVar.getClass();
                IconCompat iconCompat2 = new IconCompat(1);
                iconCompat2.b = bitmap;
                iconCompat = iconCompat2;
            }
            oVar.h = iconCompat;
            this.onNotificationChangedCallback.onNotificationChanged(new MediaNotification(this.notificationId, this.builder.a()));
        }
    }

    private void ensureNotificationChannel() {
        if (this.notificationManager.getNotificationChannel(this.channelId) != null) {
            return;
        }
        Api26.createNotificationChannel(this.notificationManager, this.channelId, this.context.getString(this.channelNameResourceId));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static String getBitmapLoadErrorMessage(Throwable th) {
        return "Failed to load bitmap: " + th.getMessage();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static int getMaxNotificationIconSize() {
        Resources system = Resources.getSystem();
        try {
            return system.getDimensionPixelSize(system.getIdentifier("notification_right_icon_size", "dimen", "android"));
        } catch (Resources.NotFoundException unused) {
            return (int) (system.getDisplayMetrics().density * 48.0f);
        }
    }

    private static long getPlaybackStartTimeEpochMs(Player player) {
        return (!player.isPlaying() || player.isPlayingAd() || player.isCurrentMediaItemDynamic() || player.getPlaybackParameters().speed != 1.0f) ? C.TIME_UNSET : System.currentTimeMillis() - player.getContentPosition();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ int lambda$new$0(MediaSession mediaSession) {
        return 1001;
    }

    public int[] addNotificationActions(MediaSession mediaSession, bj4<CommandButton> bj4Var, wp6.o oVar, MediaNotification.ActionFactory actionFactory) {
        int[] iArr = new int[3];
        int[] iArr2 = new int[3];
        Arrays.fill(iArr, -1);
        Arrays.fill(iArr2, -1);
        boolean z = false;
        for (int i = 0; i < bj4Var.size(); i++) {
            CommandButton commandButton = bj4Var.get(i);
            if (commandButton.sessionCommand != null) {
                wp6.b bVarCreateCustomActionFromCustomCommandButton = actionFactory.createCustomActionFromCustomCommandButton(mediaSession, commandButton);
                if (bVarCreateCustomActionFromCustomCommandButton != null) {
                    oVar.b.add(bVarCreateCustomActionFromCustomCommandButton);
                } else {
                    oVar.getClass();
                }
            } else {
                if7.o(commandButton.playerCommand != -1);
                wp6.b bVarCreateMediaAction = actionFactory.createMediaAction(mediaSession, IconCompat.c(this.context, commandButton.iconResId), commandButton.displayName, commandButton.playerCommand);
                if (bVarCreateMediaAction != null) {
                    oVar.b.add(bVarCreateMediaAction);
                } else {
                    oVar.getClass();
                }
            }
            int i2 = commandButton.extras.getInt(COMMAND_KEY_COMPACT_VIEW_INDEX, -1);
            if (i2 >= 0 && i2 < 3) {
                iArr[i2] = i;
                z = true;
            } else if (commandButton.slots.a(0) == 2) {
                iArr2[0] = i;
            } else if (commandButton.slots.a(0) == 1) {
                iArr2[1] = i;
            } else if (commandButton.slots.a(0) == 3) {
                iArr2[2] = i;
            }
        }
        if (!z) {
            int i3 = 0;
            for (int i4 = 0; i4 < 3; i4++) {
                int i5 = iArr2[i4];
                if (i5 != -1) {
                    iArr[i3] = i5;
                    i3++;
                }
            }
        }
        for (int i6 = 0; i6 < 3; i6++) {
            if (iArr[i6] == -1) {
                return Arrays.copyOf(iArr, i6);
            }
        }
        return iArr;
    }

    @Override // androidx.media3.session.MediaNotification.Provider
    public final MediaNotification createNotification(MediaSession mediaSession, bj4<CommandButton> bj4Var, MediaNotification.ActionFactory actionFactory, MediaNotification.Provider.Callback callback) {
        IconCompat iconCompat;
        ensureNotificationChannel();
        Player player = mediaSession.getPlayer();
        wp6.o oVar = new wp6.o(this.context, this.channelId);
        int notificationId = this.notificationIdProvider.getNotificationId(mediaSession);
        MediaStyleNotificationHelper.MediaStyle mediaStyle = new MediaStyleNotificationHelper.MediaStyle(mediaSession);
        mediaStyle.setShowActionsInCompactView(addNotificationActions(mediaSession, getMediaButtons(mediaSession, player.getAvailableCommands(), bj4Var, !Util.shouldShowPlayButton(player, mediaSession.getShowPlayButtonIfPlaybackIsSuppressed())), oVar, actionFactory));
        if (player.isCommandAvailable(18)) {
            MediaMetadata mediaMetadata = player.getMediaMetadata();
            oVar.e = wp6.o.b(getNotificationContentTitle(mediaMetadata));
            oVar.f = wp6.o.b(getNotificationContentText(mediaMetadata));
            androidx.media3.common.util.BitmapLoader bitmapLoader = mediaSession.getBitmapLoader();
            if (this.notificationIconBitmapLoader == null || !bitmapLoader.equals(this.mediaSessionBitmapLoader)) {
                this.mediaSessionBitmapLoader = bitmapLoader;
                this.notificationIconBitmapLoader = new CacheBitmapLoader(new SizeLimitedBitmapLoader(bitmapLoader, ((Integer) maxNotificationIconSize.get()).intValue(), true));
            }
            zb5<Bitmap> zb5VarLoadBitmapFromMetadata = this.notificationIconBitmapLoader.loadBitmapFromMetadata(mediaMetadata);
            if (zb5VarLoadBitmapFromMetadata != null) {
                OnBitmapLoadedFutureCallback onBitmapLoadedFutureCallback = this.pendingOnBitmapLoadedFutureCallback;
                if (onBitmapLoadedFutureCallback != null) {
                    onBitmapLoadedFutureCallback.discardIfPending();
                }
                if (zb5VarLoadBitmapFromMetadata.isDone()) {
                    try {
                        Bitmap bitmap = (Bitmap) ks3.b(zb5VarLoadBitmapFromMetadata);
                        if (bitmap == null) {
                            iconCompat = null;
                        } else {
                            IconCompat iconCompat2 = new IconCompat(1);
                            iconCompat2.b = bitmap;
                            iconCompat = iconCompat2;
                        }
                        oVar.h = iconCompat;
                    } catch (CancellationException | ExecutionException e) {
                        Log.w(TAG, getBitmapLoadErrorMessage(e));
                    }
                } else {
                    OnBitmapLoadedFutureCallback onBitmapLoadedFutureCallback2 = new OnBitmapLoadedFutureCallback(notificationId, oVar, callback);
                    this.pendingOnBitmapLoadedFutureCallback = onBitmapLoadedFutureCallback2;
                    Handler applicationHandler = mediaSession.getImpl().getApplicationHandler();
                    Objects.requireNonNull(applicationHandler);
                    ks3.a(zb5VarLoadBitmapFromMetadata, onBitmapLoadedFutureCallback2, new q12(applicationHandler, 2));
                }
            }
        }
        long playbackStartTimeEpochMs = getPlaybackStartTimeEpochMs(player);
        boolean z = playbackStartTimeEpochMs != C.TIME_UNSET;
        if (!z) {
            playbackStartTimeEpochMs = 0;
        }
        oVar.ac.when = playbackStartTimeEpochMs;
        oVar.j = z;
        oVar.k = z;
        if (Build.VERSION.SDK_INT >= 31) {
            Api31.setForegroundServiceBehavior(oVar);
        }
        oVar.g = mediaSession.getSessionActivity();
        oVar.ac.deleteIntent = actionFactory.createNotificationDismissalIntent(mediaSession);
        oVar.c(8, true);
        oVar.ac.icon = this.smallIconResourceId;
        oVar.d(mediaStyle);
        oVar.x = 1;
        oVar.c(2, false);
        oVar.q = GROUP_KEY;
        return new MediaNotification(notificationId, oVar.a());
    }

    public bj4<CommandButton> getMediaButtons(MediaSession mediaSession, Player.Commands commands, bj4<CommandButton> bj4Var, boolean z) {
        bj4<CommandButton> customLayoutFromMediaButtonPreferences = CommandButton.getCustomLayoutFromMediaButtonPreferences(bj4Var, true, true);
        boolean zContainsButtonForSlot = CommandButton.containsButtonForSlot(customLayoutFromMediaButtonPreferences, 2);
        boolean zContainsButtonForSlot2 = CommandButton.containsButtonForSlot(customLayoutFromMediaButtonPreferences, 3);
        bj4.a aVar = new bj4.a();
        int i = 0;
        if (zContainsButtonForSlot) {
            aVar.h(customLayoutFromMediaButtonPreferences.get(0));
            i = 1;
        } else if (commands.containsAny(7, 6)) {
            aVar.h(new CommandButton.Builder(CommandButton.ICON_PREVIOUS).setPlayerCommand(6).setDisplayName(this.context.getString(R.string.media3_controls_seek_to_previous_description)).build());
        }
        if (commands.contains(1)) {
            if (z) {
                aVar.h(new CommandButton.Builder(CommandButton.ICON_PAUSE).setPlayerCommand(1).setDisplayName(this.context.getString(R.string.media3_controls_pause_description)).build());
            } else {
                aVar.h(new CommandButton.Builder(CommandButton.ICON_PLAY).setPlayerCommand(1).setDisplayName(this.context.getString(R.string.media3_controls_play_description)).build());
            }
        }
        if (zContainsButtonForSlot2) {
            aVar.h(customLayoutFromMediaButtonPreferences.get(i));
            i++;
        } else if (commands.containsAny(9, 8)) {
            aVar.h(new CommandButton.Builder(CommandButton.ICON_NEXT).setPlayerCommand(8).setDisplayName(this.context.getString(R.string.media3_controls_seek_to_next_description)).build());
        }
        while (i < customLayoutFromMediaButtonPreferences.size()) {
            aVar.h(customLayoutFromMediaButtonPreferences.get(i));
            i++;
        }
        return aVar.j();
    }

    @rq6
    public CharSequence getNotificationContentText(MediaMetadata mediaMetadata) {
        return mediaMetadata.artist;
    }

    @rq6
    public CharSequence getNotificationContentTitle(MediaMetadata mediaMetadata) {
        return mediaMetadata.title;
    }

    @Override // androidx.media3.session.MediaNotification.Provider
    public final boolean handleCustomCommand(MediaSession mediaSession, String str, Bundle bundle) {
        return false;
    }

    public final void setSmallIcon(@of2 int i) {
        this.smallIconResourceId = i;
    }

    public DefaultMediaNotificationProvider(Context context) {
        this(context, new c02(3), DEFAULT_CHANNEL_ID, DEFAULT_CHANNEL_NAME_RESOURCE_ID);
    }

    public DefaultMediaNotificationProvider(Context context, NotificationIdProvider notificationIdProvider, String str, int i) {
        this.context = context;
        this.notificationIdProvider = notificationIdProvider;
        this.channelId = str;
        this.channelNameResourceId = i;
        NotificationManager notificationManager = (NotificationManager) context.getSystemService("notification");
        notificationManager.getClass();
        this.notificationManager = notificationManager;
        this.smallIconResourceId = R.drawable.media3_notification_small_icon;
    }

    private DefaultMediaNotificationProvider(Builder builder) {
        this(builder.context, builder.notificationIdProvider, builder.channelId, builder.channelNameResourceId);
    }
}
