package d3;

import android.app.BroadcastOptions;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import java.io.IOException;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: renamed from: d3.i0, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0791i0 implements InterfaceC0768a1 {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final /* synthetic */ int f12004q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final C0817r0 f12005r;

    public /* synthetic */ C0791i0(C0817r0 c0817r0, int i) {
        this.f12004q = i;
        this.f12005r = c0817r0;
    }

    public void a(String str, Bundle bundle) {
        String string;
        C0817r0 c0817r0 = this.f12005r;
        C0815q0 c0815q0 = c0817r0.f12151z;
        C0817r0.k(c0815q0);
        c0815q0.E();
        if (c0817r0.f()) {
            return;
        }
        if (bundle.isEmpty()) {
            string = null;
        } else {
            if (true == str.isEmpty()) {
                str = "auto";
            }
            Uri.Builder builder = new Uri.Builder();
            builder.path(str);
            for (String str2 : bundle.keySet()) {
                builder.appendQueryParameter(str2, bundle.getString(str2));
            }
            string = builder.build().toString();
        }
        if (TextUtils.isEmpty(string)) {
            return;
        }
        C0785g0 c0785g0 = c0817r0.f12149x;
        C0817r0.i(c0785g0);
        c0785g0.f11968N.g(string);
        c0817r0.D.getClass();
        c0785g0.f11969O.h(System.currentTimeMillis());
    }

    /* JADX WARN: Multi-variable type inference failed */
    public boolean b() {
        switch (this.f12004q) {
            case n4.u0.f15495q /* 0 */:
                C0817r0 c0817r0 = this.f12005r;
                boolean z8 = false;
                try {
                    I0.j jVarA = U2.b.a(c0817r0.f12142q);
                    if (jVarA == null) {
                        Z z9 = c0817r0.f12150y;
                        C0817r0.k(z9);
                        z9.D.b("Failed to get PackageManager for Install Referrer Play Store compatibility check");
                        c0817r0 = c0817r0;
                    } else {
                        int i = jVarA.c(128, "com.android.vending").versionCode;
                        c0817r0 = i;
                        if (i >= 80837300) {
                            z8 = true;
                            c0817r0 = i;
                        }
                    }
                    break;
                } catch (Exception e8) {
                    Z z10 = c0817r0.f12150y;
                    C0817r0.k(z10);
                    z10.D.c(e8, "Failed to retrieve Play Store version for Install Referrer");
                }
                return z8;
            default:
                C0817r0 c0817r02 = this.f12005r;
                if (TextUtils.isEmpty(c0817r02.f12143r)) {
                    Z z11 = c0817r02.f12150y;
                    C0817r0.k(z11);
                    if (Log.isLoggable(z11.P(), 3)) {
                        return true;
                    }
                }
                return false;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:10:0x0025  */
    @Override // d3.InterfaceC0768a1
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void c(int i, IOException iOException, byte[] bArr) {
        Z z8;
        int i5 = i;
        C0817r0 c0817r0 = this.f12005r;
        if (i5 == 200 || i5 == 204) {
            if (iOException == null) {
                C0785g0 c0785g0 = c0817r0.f12149x;
                C0817r0.i(c0785g0);
                c0785g0.f11965K.a(true);
                Z z9 = c0817r0.f12150y;
                if (bArr == null || bArr.length == 0) {
                    C0817r0.k(z9);
                    z9.f11871C.b("Deferred Deep Link response empty.");
                    return;
                }
                try {
                    JSONObject jSONObject = new JSONObject(new String(bArr));
                    String strOptString = jSONObject.optString("deeplink", "");
                    if (TextUtils.isEmpty(strOptString)) {
                        C0817r0.k(z9);
                        z9.f11871C.b("Deferred Deep Link is empty.");
                        return;
                    }
                    String strOptString2 = jSONObject.optString("gclid", "");
                    String strOptString3 = jSONObject.optString("gbraid", "");
                    String strOptString4 = jSONObject.optString("gad_source", "");
                    double dOptDouble = jSONObject.optDouble("timestamp", 0.0d);
                    Bundle bundle = new Bundle();
                    W1 w12 = c0817r0.f12122B;
                    C0817r0.i(w12);
                    C0817r0 c0817r02 = (C0817r0) w12.f5146q;
                    if (TextUtils.isEmpty(strOptString)) {
                        z8 = z9;
                    } else {
                        Context context = c0817r02.f12142q;
                        z8 = z9;
                        try {
                            List<ResolveInfo> listQueryIntentActivities = context.getPackageManager().queryIntentActivities(new Intent("android.intent.action.VIEW", Uri.parse(strOptString)), 0);
                            if (listQueryIntentActivities != null && !listQueryIntentActivities.isEmpty()) {
                                if (!TextUtils.isEmpty(strOptString3)) {
                                    bundle.putString("gbraid", strOptString3);
                                }
                                if (!TextUtils.isEmpty(strOptString4)) {
                                    bundle.putString("gad_source", strOptString4);
                                }
                                bundle.putString("gclid", strOptString2);
                                bundle.putString("_cis", "ddp");
                                c0817r0.f12125F.P("auto", "_cmp", bundle);
                                if (TextUtils.isEmpty(strOptString)) {
                                    return;
                                }
                                try {
                                    SharedPreferences.Editor editorEdit = context.getSharedPreferences("google.analytics.deferred.deeplink.prefs", 0).edit();
                                    editorEdit.putString("deeplink", strOptString);
                                    editorEdit.putLong("timestamp", Double.doubleToRawLongBits(dOptDouble));
                                    if (editorEdit.commit()) {
                                        Intent intent = new Intent("android.google.analytics.action.DEEPLINK_ACTION");
                                        Context context2 = c0817r02.f12142q;
                                        if (Build.VERSION.SDK_INT < 34) {
                                            context2.sendBroadcast(intent);
                                            return;
                                        } else {
                                            context2.sendBroadcast(intent, null, BroadcastOptions.makeBasic().setShareIdentityEnabled(true).toBundle());
                                            return;
                                        }
                                    }
                                    return;
                                } catch (RuntimeException e8) {
                                    Z z10 = c0817r02.f12150y;
                                    C0817r0.k(z10);
                                    z10.f11875v.c(e8, "Failed to persist Deferred Deep Link. exception");
                                    return;
                                }
                            }
                        } catch (JSONException e9) {
                            e = e9;
                            z9 = z8;
                            C0817r0.k(z9);
                            z9.f11875v.c(e, "Failed to parse the Deferred Deep Link response. exception");
                            return;
                        }
                    }
                    C0817r0.k(z8);
                    z8.f11878y.e("Deferred Deep Link validation failed. gclid, gbraid, deep link", strOptString2, strOptString3, strOptString);
                    return;
                } catch (JSONException e10) {
                    e = e10;
                }
            }
        } else if (i5 == 304) {
            i5 = 304;
            if (iOException == null) {
            }
        }
        Z z11 = c0817r0.f12150y;
        C0817r0.k(z11);
        z11.f11878y.d("Network Request for Deferred Deep Link failed. response, exception", Integer.valueOf(i5), iOException);
    }

    public boolean d() {
        C0785g0 c0785g0 = this.f12005r.f12149x;
        C0817r0.i(c0785g0);
        return c0785g0.f11969O.g() > 0;
    }

    public boolean e() {
        if (!d()) {
            return false;
        }
        C0817r0 c0817r0 = this.f12005r;
        c0817r0.D.getClass();
        long jCurrentTimeMillis = System.currentTimeMillis();
        C0785g0 c0785g0 = c0817r0.f12149x;
        C0817r0.i(c0785g0);
        return jCurrentTimeMillis - c0785g0.f11969O.g() > c0817r0.f12148w.M(null, I.f11560j0);
    }

    public C0791i0(Q1 q12) {
        this.f12004q = 0;
        this.f12005r = q12.f11659B;
    }
}
