package com.google.firebase.messaging; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.TextUtils; import android.util.Log; import com.google.android.gms.common.internal.Preconditions; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.TaskCompletionSource; 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.concurrent.ExecutorService; import java.util.concurrent.Future; /* loaded from: classes3.dex */ public class ImageDownload implements Closeable { public volatile Future future; public Task task; public final URL url; public static ImageDownload create(String str) { if (TextUtils.isEmpty(str)) { return null; } try { return new ImageDownload(new URL(str)); } catch (MalformedURLException unused) { Log.w("FirebaseMessaging", "Not downloading image, bad URL: " + str); return null; } } public ImageDownload(URL url) { this.url = url; } public void start(ExecutorService executorService) { final TaskCompletionSource taskCompletionSource = new TaskCompletionSource(); this.future = executorService.submit(new Runnable() { // from class: com.google.firebase.messaging.ImageDownload$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { ImageDownload.this.lambda$start$0(taskCompletionSource); } }); this.task = taskCompletionSource.getTask(); } public final /* synthetic */ void lambda$start$0(TaskCompletionSource taskCompletionSource) { try { taskCompletionSource.setResult(blockingDownload()); } catch (Exception e) { taskCompletionSource.setException(e); } } public Task getTask() { return (Task) Preconditions.checkNotNull(this.task); } public Bitmap blockingDownload() { if (Log.isLoggable("FirebaseMessaging", 4)) { StringBuilder sb = new StringBuilder(); sb.append("Starting download of: "); sb.append(this.url); } byte[] blockingDownloadBytes = blockingDownloadBytes(); Bitmap decodeByteArray = BitmapFactory.decodeByteArray(blockingDownloadBytes, 0, blockingDownloadBytes.length); if (decodeByteArray == null) { throw new IOException("Failed to decode image: " + this.url); } if (Log.isLoggable("FirebaseMessaging", 3)) { StringBuilder sb2 = new StringBuilder(); sb2.append("Successfully downloaded image: "); sb2.append(this.url); } return decodeByteArray; } public final byte[] blockingDownloadBytes() { URLConnection openConnection = this.url.openConnection(); if (openConnection.getContentLength() > 1048576) { throw new IOException("Content-Length exceeds max size of 1048576"); } InputStream inputStream = openConnection.getInputStream(); try { byte[] byteArray = ByteStreams.toByteArray(ByteStreams.limit(inputStream, 1048577L)); if (inputStream != null) { inputStream.close(); } if (Log.isLoggable("FirebaseMessaging", 2)) { StringBuilder sb = new StringBuilder(); sb.append("Downloaded "); sb.append(byteArray.length); sb.append(" bytes from "); sb.append(this.url); } if (byteArray.length <= 1048576) { return byteArray; } throw new IOException("Image exceeds max size of 1048576"); } catch (Throwable th) { if (inputStream != null) { try { inputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } } throw th; } } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() { this.future.cancel(true); } }