package com.google.android.datatransport.cct; import android.content.Context; import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Build; import android.telephony.TelephonyManager; import com.google.android.datatransport.cct.d; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.TimeZone; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import p2.j; import p2.k; import p2.l; import p2.m; import p2.n; import p2.o; import p2.p; import q2.h; import q2.i; import r2.f; import r2.g; import r2.m; /* loaded from: classes.dex */ final class d implements m { /* renamed from: a, reason: collision with root package name */ private final h4.a f4246a; /* renamed from: b, reason: collision with root package name */ private final ConnectivityManager f4247b; /* renamed from: c, reason: collision with root package name */ private final Context f4248c; /* renamed from: d, reason: collision with root package name */ final URL f4249d; /* renamed from: e, reason: collision with root package name */ private final a3.a f4250e; /* renamed from: f, reason: collision with root package name */ private final a3.a f4251f; /* renamed from: g, reason: collision with root package name */ private final int f4252g; static final class a { /* renamed from: a, reason: collision with root package name */ final URL f4253a; /* renamed from: b, reason: collision with root package name */ final j f4254b; /* renamed from: c, reason: collision with root package name */ final String f4255c; a(URL url, j jVar, String str) { this.f4253a = url; this.f4254b = jVar; this.f4255c = str; } a a(URL url) { return new a(url, this.f4254b, this.f4255c); } } static final class b { /* renamed from: a, reason: collision with root package name */ final int f4256a; /* renamed from: b, reason: collision with root package name */ final URL f4257b; /* renamed from: c, reason: collision with root package name */ final long f4258c; b(int i10, URL url, long j10) { this.f4256a = i10; this.f4257b = url; this.f4258c = j10; } } d(Context context, a3.a aVar, a3.a aVar2, int i10) { this.f4246a = j.b(); this.f4248c = context; this.f4247b = (ConnectivityManager) context.getSystemService("connectivity"); this.f4249d = n(com.google.android.datatransport.cct.a.f4237c); this.f4250e = aVar2; this.f4251f = aVar; this.f4252g = i10; } /* JADX INFO: Access modifiers changed from: private */ public b e(a aVar) { u2.a.f("CctTransportBackend", "Making request to: %s", aVar.f4253a); HttpURLConnection httpURLConnection = (HttpURLConnection) aVar.f4253a.openConnection(); httpURLConnection.setConnectTimeout(30000); httpURLConnection.setReadTimeout(this.f4252g); httpURLConnection.setDoOutput(true); httpURLConnection.setInstanceFollowRedirects(false); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("User-Agent", String.format("datatransport/%s android/", "3.1.8")); httpURLConnection.setRequestProperty("Content-Encoding", "gzip"); httpURLConnection.setRequestProperty("Content-Type", "application/json"); httpURLConnection.setRequestProperty("Accept-Encoding", "gzip"); String str = aVar.f4255c; if (str != null) { httpURLConnection.setRequestProperty("X-Goog-Api-Key", str); } try { OutputStream outputStream = httpURLConnection.getOutputStream(); try { GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(outputStream); try { this.f4246a.a(aVar.f4254b, new BufferedWriter(new OutputStreamWriter(gZIPOutputStream))); gZIPOutputStream.close(); if (outputStream != null) { outputStream.close(); } int responseCode = httpURLConnection.getResponseCode(); u2.a.f("CctTransportBackend", "Status Code: %d", Integer.valueOf(responseCode)); u2.a.b("CctTransportBackend", "Content-Type: %s", httpURLConnection.getHeaderField("Content-Type")); u2.a.b("CctTransportBackend", "Content-Encoding: %s", httpURLConnection.getHeaderField("Content-Encoding")); if (responseCode == 302 || responseCode == 301 || responseCode == 307) { return new b(responseCode, new URL(httpURLConnection.getHeaderField("Location")), 0L); } if (responseCode != 200) { return new b(responseCode, null, 0L); } InputStream inputStream = httpURLConnection.getInputStream(); try { InputStream m10 = m(inputStream, httpURLConnection.getHeaderField("Content-Encoding")); try { b bVar = new b(responseCode, null, n.b(new BufferedReader(new InputStreamReader(m10))).c()); if (m10 != null) { m10.close(); } if (inputStream != null) { inputStream.close(); } return bVar; } finally { } } catch (Throwable th) { if (inputStream != null) { try { inputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } } throw th; } } finally { } } catch (Throwable th3) { if (outputStream != null) { try { outputStream.close(); } catch (Throwable th4) { th3.addSuppressed(th4); } } throw th3; } } catch (h4.b e10) { e = e10; u2.a.d("CctTransportBackend", "Couldn't encode request, returning with 400", e); return new b(400, null, 0L); } catch (ConnectException e11) { e = e11; u2.a.d("CctTransportBackend", "Couldn't open connection, returning with 500", e); return new b(500, null, 0L); } catch (UnknownHostException e12) { e = e12; u2.a.d("CctTransportBackend", "Couldn't open connection, returning with 500", e); return new b(500, null, 0L); } catch (IOException e13) { e = e13; u2.a.d("CctTransportBackend", "Couldn't encode request, returning with 400", e); return new b(400, null, 0L); } } private static int f(NetworkInfo networkInfo) { if (networkInfo == null) { return o.b.UNKNOWN_MOBILE_SUBTYPE.b(); } int subtype = networkInfo.getSubtype(); if (subtype == -1) { return o.b.COMBINED.b(); } if (o.b.a(subtype) != null) { return subtype; } return 0; } private static int g(NetworkInfo networkInfo) { return networkInfo == null ? o.c.NONE.b() : networkInfo.getType(); } private static int h(Context context) { try { return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode; } catch (PackageManager.NameNotFoundException e10) { u2.a.d("CctTransportBackend", "Unable to find version code for package", e10); return -1; } } private j i(f fVar) { l.a j10; HashMap hashMap = new HashMap(); for (i iVar : fVar.b()) { String j11 = iVar.j(); if (hashMap.containsKey(j11)) { ((List) hashMap.get(j11)).add(iVar); } else { ArrayList arrayList = new ArrayList(); arrayList.add(iVar); hashMap.put(j11, arrayList); } } ArrayList arrayList2 = new ArrayList(); for (Map.Entry entry : hashMap.entrySet()) { i iVar2 = (i) ((List) entry.getValue()).get(0); m.a b10 = p2.m.a().f(p.DEFAULT).g(this.f4251f.a()).h(this.f4250e.a()).b(k.a().c(k.b.ANDROID_FIREBASE).b(p2.a.a().m(Integer.valueOf(iVar2.g("sdk-version"))).j(iVar2.b("model")).f(iVar2.b("hardware")).d(iVar2.b("device")).l(iVar2.b("product")).k(iVar2.b("os-uild")).h(iVar2.b("manufacturer")).e(iVar2.b("fingerprint")).c(iVar2.b("country")).g(iVar2.b("locale")).i(iVar2.b("mcc_mnc")).b(iVar2.b("application_build")).a()).a()); try { b10.i(Integer.parseInt((String) entry.getKey())); } catch (NumberFormatException unused) { b10.j((String) entry.getKey()); } ArrayList arrayList3 = new ArrayList(); for (i iVar3 : (List) entry.getValue()) { h e10 = iVar3.e(); o2.b b11 = e10.b(); if (b11.equals(o2.b.b("proto"))) { j10 = l.j(e10.a()); } else if (b11.equals(o2.b.b("json"))) { j10 = l.i(new String(e10.a(), Charset.forName("UTF-8"))); } else { u2.a.g("CctTransportBackend", "Received event of unsupported encoding %s. Skipping...", b11); } j10.c(iVar3.f()).d(iVar3.k()).h(iVar3.h("tz-offset")).e(o.a().c(o.c.a(iVar3.g("net-type"))).b(o.b.a(iVar3.g("mobile-subtype"))).a()); if (iVar3.d() != null) { j10.b(iVar3.d()); } arrayList3.add(j10.a()); } b10.c(arrayList3); arrayList2.add(b10.a()); } return j.a(arrayList2); } private static TelephonyManager j(Context context) { return (TelephonyManager) context.getSystemService("phone"); } static long k() { Calendar.getInstance(); return TimeZone.getDefault().getOffset(Calendar.getInstance().getTimeInMillis()) / 1000; } /* JADX INFO: Access modifiers changed from: private */ public static /* synthetic */ a l(a aVar, b bVar) { URL url = bVar.f4257b; if (url == null) { return null; } u2.a.b("CctTransportBackend", "Following redirect to: %s", url); return aVar.a(bVar.f4257b); } private static InputStream m(InputStream inputStream, String str) { return "gzip".equals(str) ? new GZIPInputStream(inputStream) : inputStream; } private static URL n(String str) { try { return new URL(str); } catch (MalformedURLException e10) { throw new IllegalArgumentException("Invalid url: " + str, e10); } } @Override // r2.m public i a(i iVar) { NetworkInfo activeNetworkInfo = this.f4247b.getActiveNetworkInfo(); return iVar.l().a("sdk-version", Build.VERSION.SDK_INT).c("model", Build.MODEL).c("hardware", Build.HARDWARE).c("device", Build.DEVICE).c("product", Build.PRODUCT).c("os-uild", Build.ID).c("manufacturer", Build.MANUFACTURER).c("fingerprint", Build.FINGERPRINT).b("tz-offset", k()).a("net-type", g(activeNetworkInfo)).a("mobile-subtype", f(activeNetworkInfo)).c("country", Locale.getDefault().getCountry()).c("locale", Locale.getDefault().getLanguage()).c("mcc_mnc", j(this.f4248c).getSimOperator()).c("application_build", Integer.toString(h(this.f4248c))).d(); } @Override // r2.m public g b(f fVar) { j i10 = i(fVar); URL url = this.f4249d; if (fVar.c() != null) { try { com.google.android.datatransport.cct.a d10 = com.google.android.datatransport.cct.a.d(fVar.c()); r3 = d10.e() != null ? d10.e() : null; if (d10.f() != null) { url = n(d10.f()); } } catch (IllegalArgumentException unused) { return g.a(); } } try { b bVar = (b) v2.b.a(5, new a(url, i10, r3), new v2.a() { // from class: com.google.android.datatransport.cct.b @Override // v2.a public final Object apply(Object obj) { d.b e10; e10 = d.this.e((d.a) obj); return e10; } }, new v2.c() { // from class: com.google.android.datatransport.cct.c @Override // v2.c public final Object a(Object obj, Object obj2) { d.a l10; l10 = d.l((d.a) obj, (d.b) obj2); return l10; } }); int i11 = bVar.f4256a; if (i11 == 200) { return g.e(bVar.f4258c); } if (i11 < 500 && i11 != 404) { return i11 == 400 ? g.d() : g.a(); } return g.f(); } catch (IOException e10) { u2.a.d("CctTransportBackend", "Could not make request to the backend", e10); return g.f(); } } d(Context context, a3.a aVar, a3.a aVar2) { this(context, aVar, aVar2, 130000); } }