package androidx.media3.session;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.C0215d;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
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;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
@Deprecated
public final class SimpleBitmapLoader implements androidx.media3.common.util.BitmapLoader {
    private static final Supplier<ListeningExecutorService> DEFAULT_EXECUTOR_SERVICE = Suppliers.memoize(new C0215d());
    private static final String FILE_URI_EXCEPTION_MESSAGE = "Could not read image from file";
    private final ListeningExecutorService executorService;

    public SimpleBitmapLoader() {
        this((ExecutorService) Assertions.checkStateNotNull((ListeningExecutorService) DEFAULT_EXECUTOR_SERVICE.get()));
    }

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

    /* 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("Invalid response status code: " + responseCode);
        }
        InputStream inputStream = httpURLConnection.getInputStream();
        try {
            Bitmap bitmapDecode = decode(ByteStreams.toByteArray(inputStream));
            if (inputStream != null) {
                inputStream.close();
            }
            return bitmapDecode;
        } catch (Throwable th) {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
            }
            throw th;
        }
    }

    public ListenableFuture decodeBitmap(final byte[] bArr) {
        return this.executorService.submit(new Callable() { // from class: androidx.media3.session.S5
            @Override // java.util.concurrent.Callable
            public final Object call() {
                return SimpleBitmapLoader.decode(bArr);
            }
        });
    }

    public ListenableFuture loadBitmap(final Uri uri) {
        return this.executorService.submit(new Callable() { // from class: androidx.media3.session.R5
            @Override // java.util.concurrent.Callable
            public final Object call() {
                return SimpleBitmapLoader.load(uri);
            }
        });
    }

    public boolean supportsMimeType(String str) {
        return Util.isBitmapFactorySupportedMimeType(str);
    }

    public SimpleBitmapLoader(ExecutorService executorService) {
        this.executorService = MoreExecutors.listeningDecorator(executorService);
    }
}
