package com.google.firebase.messaging;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/* JADX INFO: loaded from: classes2.dex */
public final class q implements Closeable {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final URL f24243a;

    @Nullable
    private volatile Future<?> future;

    @Nullable
    private w8.h task;

    public q(URL url) {
        this.f24243a = url;
    }

    @Nullable
    public static q create(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            return new q(new URL(str));
        } catch (MalformedURLException unused) {
            return null;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        this.future.cancel(true);
    }

    public final Bitmap e() throws IOException {
        boolean zIsLoggable = Log.isLoggable("FirebaseMessaging", 4);
        URL url = this.f24243a;
        if (zIsLoggable) {
            Objects.toString(url);
        }
        URLConnection uRLConnectionOpenConnection = url.openConnection();
        if (uRLConnectionOpenConnection.getContentLength() > 1048576) {
            throw new IOException("Content-Length exceeds max size of 1048576");
        }
        InputStream inputStream = uRLConnectionOpenConnection.getInputStream();
        try {
            byte[] bArrJ = ua.b.J(new e(inputStream));
            if (inputStream != null) {
                inputStream.close();
            }
            if (Log.isLoggable("FirebaseMessaging", 2)) {
                int length = bArrJ.length;
                Objects.toString(url);
            }
            if (bArrJ.length > 1048576) {
                throw new IOException("Image exceeds max size of 1048576");
            }
            Bitmap bitmapDecodeByteArray = BitmapFactory.decodeByteArray(bArrJ, 0, bArrJ.length);
            if (bitmapDecodeByteArray != null) {
                if (Log.isLoggable("FirebaseMessaging", 3)) {
                    Objects.toString(url);
                }
                return bitmapDecodeByteArray;
            }
            throw new IOException("Failed to decode image: " + url);
        } catch (Throwable th) {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
            }
            throw th;
        }
    }

    public final w8.h g() {
        return (w8.h) com.google.android.gms.common.internal.w.checkNotNull(this.task);
    }

    public final void k(ExecutorService executorService) {
        w8.i iVar = new w8.i();
        this.future = executorService.submit(new e.t(this, iVar, 27));
        this.task = iVar.f34224a;
    }
}
