package com.google.android.gms.internal.ads;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Process;
import android.os.RemoteException;
import android.os.ext.SdkExtensions;
import androidx.annotation.Nullable;
import java.util.List;

/* JADX INFO: loaded from: classes2.dex */
public final class tb extends rb {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f16835b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Context f16836c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final String f16837d;

    @Nullable
    private dj zzd;

    @Nullable
    private ServiceConnection zze;

    public tb(Context context) {
        super(0);
        this.f16835b = 0;
        this.f16836c = context.getApplicationContext();
        String packageName = (m0.a.c() && SdkExtensions.getExtensionVersion(34) >= 8 && Process.isSdkSandbox()) ? androidx.media3.exoplayer.audio.n.l(context.getSystemService(androidx.media3.exoplayer.audio.n.C())).getClientPackageName() : context.getPackageName();
        this.f16837d = packageName;
    }

    public final void A(dh0 dh0Var) {
        ServiceInfo serviceInfo;
        if (z()) {
            dh0Var.e(0);
            return;
        }
        int i10 = this.f16835b;
        if (i10 == 1) {
            dh0Var.e(3);
            return;
        }
        if (i10 == 3) {
            dh0Var.e(3);
            return;
        }
        Intent intent = new Intent("com.google.android.finsky.BIND_GET_INSTALL_REFERRER_SERVICE");
        intent.setComponent(new ComponentName("com.android.vending", "com.google.android.finsky.externalreferrer.GetInstallReferrerService"));
        Context context = this.f16836c;
        List<ResolveInfo> listQueryIntentServices = context.getPackageManager().queryIntentServices(intent, 0);
        if (listQueryIntentServices == null || listQueryIntentServices.isEmpty() || (serviceInfo = listQueryIntentServices.get(0).serviceInfo) == null) {
            this.f16835b = 0;
            dh0Var.e(2);
            return;
        }
        String str = serviceInfo.packageName;
        String str2 = serviceInfo.name;
        if ("com.android.vending".equals(str) && str2 != null) {
            try {
                if (context.getPackageManager().getPackageInfo("com.android.vending", 128).versionCode >= 80837300) {
                    Intent intent2 = new Intent(intent);
                    sb sbVar = new sb(this, dh0Var);
                    this.zze = sbVar;
                    try {
                        if (context.bindService(intent2, sbVar, 1)) {
                            return;
                        }
                        this.f16835b = 0;
                        dh0Var.e(1);
                        return;
                    } catch (SecurityException unused) {
                        this.f16835b = 0;
                        dh0Var.e(4);
                        return;
                    }
                }
            } catch (PackageManager.NameNotFoundException unused2) {
            }
        }
        this.f16835b = 0;
        dh0Var.e(2);
    }

    public final void B() {
        this.f16835b = 3;
        ServiceConnection serviceConnection = this.zze;
        if (serviceConnection != null) {
            this.f16836c.unbindService(serviceConnection);
            this.zze = null;
        }
        this.zzd = null;
    }

    public final ub C() throws RemoteException {
        if (!z()) {
            throw new IllegalStateException("Service not connected. Please start a connection before using the service.");
        }
        Bundle bundle = new Bundle();
        bundle.putString("package_name", this.f16837d);
        try {
            bj bjVar = (bj) this.zzd;
            Parcel parcelO = bjVar.O();
            aj.b(parcelO, bundle);
            Parcel parcelB0 = bjVar.B0(1, parcelO);
            Bundle bundle2 = (Bundle) aj.a(parcelB0, Bundle.CREATOR);
            parcelB0.recycle();
            return new ub(bundle2);
        } catch (RemoteException e10) {
            this.f16835b = 0;
            throw e10;
        }
    }

    public final /* synthetic */ void D(dj djVar) {
        this.zzd = djVar;
    }

    public final boolean z() {
        return (this.f16835b != 2 || this.zzd == null || this.zze == null) ? false : true;
    }
}
