package o;

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 com.google.android.gms.common.internal.Preconditions;
import com.google.android.gms.common.stats.ConnectionTracker;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class Tu0 implements ServiceConnection {
    public int a = 0;
    public final Messenger b;
    public C3031ym0 c;
    public final ArrayDeque d;
    public final SparseArray e;
    public final /* synthetic */ Ew0 f;

    public Tu0(Ew0 ew0) {
        this.f = ew0;
        Gn0 gn0 = new Gn0(Looper.getMainLooper(), new C1935n90(1, this));
        Looper.getMainLooper();
        this.b = new Messenger(gn0);
        this.d = new ArrayDeque();
        this.e = 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)) {
                "Disconnected: ".concat(String.valueOf(str));
            }
            int i = this.a;
            if (i == 0) {
                throw new IllegalStateException();
            }
            if (i != 1 && i != 2) {
                if (i != 3) {
                    return;
                }
                this.a = 4;
                return;
            }
            Log.isLoggable("MessengerIpcClient", 2);
            this.a = 4;
            ConnectionTracker.getInstance().unbindService((Context) this.f.b, this);
            C1549j60 c1549j60 = new C1549j60(str, securityException);
            Iterator it = this.d.iterator();
            while (it.hasNext()) {
                ((jw0) it.next()).b(c1549j60);
            }
            this.d.clear();
            for (int i2 = 0; i2 < this.e.size(); i2++) {
                ((jw0) this.e.valueAt(i2)).b(c1549j60);
            }
            this.e.clear();
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void c() {
        if (this.a == 2 && this.d.isEmpty() && this.e.size() == 0) {
            Log.isLoggable("MessengerIpcClient", 2);
            this.a = 3;
            ConnectionTracker.getInstance().unbindService((Context) this.f.b, this);
        }
    }

    public final synchronized boolean d(jw0 jw0Var) {
        int i = this.a;
        if (i != 0) {
            if (i == 1) {
                this.d.add(jw0Var);
                return true;
            }
            if (i != 2) {
                return false;
            }
            this.d.add(jw0Var);
            ((ScheduledExecutorService) this.f.c).execute(new RunnableC1045ds0(this, 0));
            return true;
        }
        this.d.add(jw0Var);
        Preconditions.checkState(this.a == 0);
        Log.isLoggable("MessengerIpcClient", 2);
        this.a = 1;
        Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
        intent.setPackage("com.google.android.gms");
        try {
            if (ConnectionTracker.getInstance().bindService((Context) this.f.b, intent, this, 1)) {
                ((ScheduledExecutorService) this.f.c).schedule(new RunnableC1045ds0(this, 1), 30L, TimeUnit.SECONDS);
            } else {
                a("Unable to bind to service");
            }
        } catch (SecurityException e) {
            b("Unable to bind to service", e);
        }
        return true;
    }

    @Override // android.content.ServiceConnection
    public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        Log.isLoggable("MessengerIpcClient", 2);
        ((ScheduledExecutorService) this.f.c).execute(new RunnableC0890cD(26, this, iBinder));
    }

    @Override // android.content.ServiceConnection
    public final void onServiceDisconnected(ComponentName componentName) {
        Log.isLoggable("MessengerIpcClient", 2);
        ((ScheduledExecutorService) this.f.c).execute(new RunnableC1045ds0(this, 2));
    }
}
