package com.google.android.play.core.assetpacks; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.ParcelFileDescriptor; import android.os.Parcelable; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.TaskCompletionSource; import com.google.android.gms.tasks.Tasks; import java.io.File; import java.io.FileNotFoundException; import java.io.FilenameFilter; import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicInteger; /* loaded from: classes2.dex */ final class d2 implements t3 { /* renamed from: i, reason: collision with root package name */ private static final r3.h0 f4817i = new r3.h0("FakeAssetPackService"); /* renamed from: j, reason: collision with root package name */ private static final AtomicInteger f4818j = new AtomicInteger(1); /* renamed from: a, reason: collision with root package name */ private final String f4819a; /* renamed from: b, reason: collision with root package name */ private final y f4820b; /* renamed from: c, reason: collision with root package name */ private final h1 f4821c; /* renamed from: d, reason: collision with root package name */ private final Context f4822d; /* renamed from: e, reason: collision with root package name */ private final r2 f4823e; /* renamed from: f, reason: collision with root package name */ private final p2 f4824f; /* renamed from: g, reason: collision with root package name */ private final Handler f4825g = new Handler(Looper.getMainLooper()); /* renamed from: h, reason: collision with root package name */ private final r3.o f4826h; d2(File file, y yVar, h1 h1Var, Context context, r2 r2Var, r3.o oVar, p2 p2Var) { this.f4819a = file.getAbsolutePath(); this.f4820b = yVar; this.f4821c = h1Var; this.f4822d = context; this.f4823e = r2Var; this.f4826h = oVar; this.f4824f = p2Var; } static long g(int i10, long j10) { if (i10 == 2) { return j10 / 2; } if (i10 == 3 || i10 == 4) { return j10; } return 0L; } private final Bundle j(int i10, String str, int i11) { Bundle bundle = new Bundle(); bundle.putInt("app_version_code", this.f4823e.a()); bundle.putInt("session_id", i10); File[] l10 = l(str); ArrayList arrayList = new ArrayList<>(); long j10 = 0; for (File file : l10) { j10 += file.length(); ArrayList arrayList2 = new ArrayList<>(); arrayList2.add(i11 == 3 ? new Intent().setData(Uri.EMPTY) : null); String a10 = r3.i.a(file); bundle.putParcelableArrayList(s3.b.b("chunk_intents", str, a10), arrayList2); bundle.putString(s3.b.b("uncompressed_hash_sha256", str, a10), k(file)); bundle.putLong(s3.b.b("uncompressed_size", str, a10), file.length()); arrayList.add(a10); } bundle.putStringArrayList(s3.b.a("slice_ids", str), arrayList); bundle.putLong(s3.b.a("pack_version", str), this.f4823e.a()); bundle.putInt(s3.b.a("status", str), i11); bundle.putInt(s3.b.a("error_code", str), 0); bundle.putLong(s3.b.a("bytes_downloaded", str), g(i11, j10)); bundle.putLong(s3.b.a("total_bytes_to_download", str), j10); bundle.putStringArrayList("pack_names", new ArrayList<>(Arrays.asList(str))); bundle.putLong("bytes_downloaded", g(i11, j10)); bundle.putLong("total_bytes_to_download", j10); final Intent putExtra = new Intent("com.google.android.play.core.assetpacks.receiver.ACTION_SESSION_UPDATE").putExtra("com.google.android.play.core.assetpacks.receiver.EXTRA_SESSION_STATE", bundle); this.f4825g.post(new Runnable() { // from class: com.google.android.play.core.assetpacks.c2 @Override // java.lang.Runnable public final void run() { d2.this.h(putExtra); } }); return bundle; } private static String k(File file) { try { return f2.a(Arrays.asList(file)); } catch (IOException e10) { throw new t3.a(String.format("Could not digest file: %s.", file), e10); } catch (NoSuchAlgorithmException e11) { throw new t3.a("SHA256 algorithm not supported.", e11); } } private final File[] l(final String str) { File file = new File(this.f4819a); if (!file.isDirectory()) { throw new t3.a(String.format("Local testing directory '%s' not found.", file)); } File[] listFiles = file.listFiles(new FilenameFilter() { // from class: q3.f @Override // java.io.FilenameFilter public final boolean accept(File file2, String str2) { return str2.startsWith(String.valueOf(str).concat("-")) && str2.endsWith(".apk"); } }); if (listFiles == null) { throw new t3.a(String.format("Failed fetching APKs for pack '%s'.", str)); } if (listFiles.length == 0) { throw new t3.a(String.format("No APKs available for pack '%s'.", str)); } for (File file2 : listFiles) { if (r3.i.a(file2).equals(str)) { return listFiles; } } throw new t3.a(String.format("No main slice available for pack '%s'.", str)); } @Override // com.google.android.play.core.assetpacks.t3 public final void a(int i10) { f4817i.d("notifySessionFailed", new Object[0]); } @Override // com.google.android.play.core.assetpacks.t3 public final void b(final int i10, final String str) { f4817i.d("notifyModuleCompleted", new Object[0]); ((Executor) this.f4826h.a()).execute(new Runnable() { // from class: com.google.android.play.core.assetpacks.b2 @Override // java.lang.Runnable public final void run() { d2.this.i(i10, str); } }); } @Override // com.google.android.play.core.assetpacks.t3 public final void c(List list) { f4817i.d("cancelDownload(%s)", list); } @Override // com.google.android.play.core.assetpacks.t3 public final Task d(Map map) { f4817i.d("syncPacks()", new Object[0]); return Tasks.forResult(new ArrayList()); } @Override // com.google.android.play.core.assetpacks.t3 public final void e(int i10, String str, String str2, int i11) { f4817i.d("notifyChunkTransferred", new Object[0]); } @Override // com.google.android.play.core.assetpacks.t3 public final Task f(int i10, String str, String str2, int i11) { int i12; f4817i.d("getChunkFileDescriptor(session=%d, %s, %s, %d)", Integer.valueOf(i10), str, str2, Integer.valueOf(i11)); TaskCompletionSource taskCompletionSource = new TaskCompletionSource(); try { } catch (FileNotFoundException e10) { f4817i.e("getChunkFileDescriptor failed", e10); taskCompletionSource.setException(new t3.a("Asset Slice file not found.", e10)); } catch (t3.a e11) { f4817i.e("getChunkFileDescriptor failed", e11); taskCompletionSource.setException(e11); } for (File file : l(str)) { if (r3.i.a(file).equals(str2)) { taskCompletionSource.setResult(ParcelFileDescriptor.open(file, 268435456)); return taskCompletionSource.getTask(); } } throw new t3.a(String.format("Local testing slice for '%s' not found.", str2)); } final /* synthetic */ void h(Intent intent) { this.f4820b.b(this.f4822d, intent); } final /* synthetic */ void i(int i10, String str) { try { j(i10, str, 4); } catch (t3.a e10) { f4817i.e("notifyModuleCompleted failed", e10); } } @Override // com.google.android.play.core.assetpacks.t3 public final void f() { f4817i.d("keepAlive", new Object[0]); } }