package o1;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Looper;
import android.os.Messenger;
import android.util.Log;
import android.util.SparseArray;
import io.sentry.C0732k1;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import v1.C1074a;

/* JADX INFO: loaded from: classes.dex */
public final class k implements ServiceConnection {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f9404a = 0;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Messenger f9405b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public C0732k1 f9406c;

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final SparseArray f9408e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final /* synthetic */ m f9409f;

    public k(m mVar) {
        this.f9409f = mVar;
        C1.g gVar = new C1.g(Looper.getMainLooper(), new k2.e(1, this));
        Looper.getMainLooper();
        this.f9405b = new Messenger(gVar);
        this.f9407d = new ArrayDeque();
        this.f9408e = new SparseArray();
    }

    public final synchronized void a(String str) {
        b(str, null);
    }

    public final synchronized void b(String str, SecurityException securityException) {
        try {
            if (Log.isLoggable("MessengerIpcClient", 3)) {
                Log.d("MessengerIpcClient", "Disconnected: ".concat(String.valueOf(str)));
            }
            int i = this.f9404a;
            if (i == 0) {
                throw new IllegalStateException();
            }
            if (i != 1 && i != 2) {
                if (i != 3) {
                    return;
                }
                this.f9404a = 4;
                return;
            }
            if (Log.isLoggable("MessengerIpcClient", 2)) {
                Log.v("MessengerIpcClient", "Unbinding service");
            }
            this.f9404a = 4;
            C1074a.b().c((Context) this.f9409f.f9417l, this);
            F0.j jVar = new F0.j(str, securityException);
            Iterator it = this.f9407d.iterator();
            while (it.hasNext()) {
                ((l) it.next()).b(jVar);
            }
            this.f9407d.clear();
            for (int i5 = 0; i5 < this.f9408e.size(); i5++) {
                ((l) this.f9408e.valueAt(i5)).b(jVar);
            }
            this.f9408e.clear();
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void c() {
        try {
            if (this.f9404a == 2 && this.f9407d.isEmpty() && this.f9408e.size() == 0) {
                if (Log.isLoggable("MessengerIpcClient", 2)) {
                    Log.v("MessengerIpcClient", "Finished handling requests, unbinding");
                }
                this.f9404a = 3;
                C1074a.b().c((Context) this.f9409f.f9417l, this);
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized boolean d(l lVar) {
        int i = this.f9404a;
        if (i != 0) {
            if (i == 1) {
                this.f9407d.add(lVar);
                return true;
            }
            if (i != 2) {
                return false;
            }
            this.f9407d.add(lVar);
            ((ScheduledExecutorService) this.f9409f.f9418m).execute(new j(this, 0));
            return true;
        }
        this.f9407d.add(lVar);
        if (this.f9404a != 0) {
            throw new IllegalStateException();
        }
        if (Log.isLoggable("MessengerIpcClient", 2)) {
            Log.v("MessengerIpcClient", "Starting bind to GmsCore");
        }
        this.f9404a = 1;
        Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
        intent.setPackage("com.google.android.gms");
        try {
            if (C1074a.b().a((Context) this.f9409f.f9417l, intent, this, 1)) {
                ((ScheduledExecutorService) this.f9409f.f9418m).schedule(new j(this, 1), 30L, TimeUnit.SECONDS);
            } else {
                a("Unable to bind to service");
            }
        } catch (SecurityException e2) {
            b("Unable to bind to service", e2);
        }
        return true;
    }

    @Override // android.content.ServiceConnection
    public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        if (Log.isLoggable("MessengerIpcClient", 2)) {
            Log.v("MessengerIpcClient", "Service connected");
        }
        ((ScheduledExecutorService) this.f9409f.f9418m).execute(new i(0, this, iBinder));
    }

    @Override // android.content.ServiceConnection
    public final void onServiceDisconnected(ComponentName componentName) {
        if (Log.isLoggable("MessengerIpcClient", 2)) {
            Log.v("MessengerIpcClient", "Service disconnected");
        }
        ((ScheduledExecutorService) this.f9409f.f9418m).execute(new j(this, 2));
    }
}
