package androidx.media3.session;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import defpackage.ae9;
import defpackage.ar1;
import defpackage.ce9;
import defpackage.gc5;
import defpackage.if7;
import defpackage.n41;
import defpackage.o96;
import defpackage.ql1;
import defpackage.to0;
import defpackage.zb5;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
@Deprecated
public final class SimpleBitmapLoader implements androidx.media3.common.util.BitmapLoader {
    private static final ae9<gc5> DEFAULT_EXECUTOR_SERVICE = ce9.a(new ar1(4));
    private static final String FILE_URI_EXCEPTION_MESSAGE = "Could not read image from file";
    private final gc5 executorService;

    /* JADX WARN: Illegal instructions before constructor call */
    public SimpleBitmapLoader() {
        gc5 gc5Var = (gc5) DEFAULT_EXECUTOR_SERVICE.get();
        gc5Var.getClass();
        this(gc5Var);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static Bitmap decode(byte[] bArr) {
        Bitmap bitmapDecodeByteArray = BitmapFactory.decodeByteArray(bArr, 0, bArr.length);
        if7.f(bitmapDecodeByteArray != null, "Could not decode image data");
        return bitmapDecodeByteArray;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ gc5 lambda$static$0() {
        return o96.b(Executors.newSingleThreadExecutor());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static Bitmap load(Uri uri) throws IOException {
        if ("file".equals(uri.getScheme())) {
            String path = uri.getPath();
            if (path == null) {
                throw new IllegalArgumentException(FILE_URI_EXCEPTION_MESSAGE);
            }
            Bitmap bitmapDecodeFile = BitmapFactory.decodeFile(path);
            if (bitmapDecodeFile != null) {
                return bitmapDecodeFile;
            }
            throw new IllegalArgumentException(FILE_URI_EXCEPTION_MESSAGE);
        }
        URLConnection uRLConnectionOpenConnection = new URL(uri.toString()).openConnection();
        if (!(uRLConnectionOpenConnection instanceof HttpURLConnection)) {
            throw new UnsupportedOperationException("Unsupported scheme: " + uri.getScheme());
        }
        HttpURLConnection httpURLConnection = (HttpURLConnection) uRLConnectionOpenConnection;
        httpURLConnection.connect();
        int responseCode = httpURLConnection.getResponseCode();
        if (responseCode != 200) {
            throw new IOException(n41.k("Invalid response status code: ", responseCode));
        }
        InputStream inputStream = httpURLConnection.getInputStream();
        try {
            Bitmap bitmapDecode = decode(to0.c(inputStream));
            inputStream.close();
            return bitmapDecode;
        } catch (Throwable th) {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
            }
            throw th;
        }
    }

    @Override // androidx.media3.common.util.BitmapLoader
    public zb5<Bitmap> decodeBitmap(byte[] bArr) {
        return this.executorService.submit((Callable) new ql1(bArr, 5));
    }

    @Override // androidx.media3.common.util.BitmapLoader
    public zb5<Bitmap> loadBitmap(Uri uri) {
        return this.executorService.submit((Callable) new ql1(uri, 4));
    }

    @Override // androidx.media3.common.util.BitmapLoader
    public boolean supportsMimeType(String str) {
        return Util.isBitmapFactorySupportedMimeType(str);
    }

    public SimpleBitmapLoader(ExecutorService executorService) {
        this.executorService = o96.b(executorService);
    }
}
