package androidx.work.multiprocess;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import androidx.work.multiprocess.a;
import androidx.work.multiprocess.d;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import z5.k;

/* loaded from: classes.dex */
public class e {

    /* renamed from: e, reason: collision with root package name */
    static final String f6073e = k.f("ListenableWorkerImplClient");

    /* renamed from: a, reason: collision with root package name */
    final Context f6074a;

    /* renamed from: b, reason: collision with root package name */
    final Executor f6075b;

    /* renamed from: c, reason: collision with root package name */
    private final Object f6076c = new Object();

    /* renamed from: d, reason: collision with root package name */
    private b f6077d;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class a implements Runnable {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ ek.d f6078p;

        /* renamed from: q, reason: collision with root package name */
        final /* synthetic */ f f6079q;

        /* renamed from: r, reason: collision with root package name */
        final /* synthetic */ k6.b f6080r;

        /* renamed from: androidx.work.multiprocess.e$a$a, reason: collision with other inner class name */
        /* loaded from: classes.dex */
        class RunnableC0156a implements Runnable {

            /* renamed from: p, reason: collision with root package name */
            final /* synthetic */ androidx.work.multiprocess.a f6082p;

            RunnableC0156a(androidx.work.multiprocess.a aVar) {
                this.f6082p = aVar;
            }

            @Override // java.lang.Runnable
            public void run() {
                try {
                    a aVar = a.this;
                    aVar.f6080r.a(this.f6082p, aVar.f6079q);
                } catch (Throwable th2) {
                    k.c().b(e.f6073e, "Unable to execute", th2);
                    d.a.a(a.this.f6079q, th2);
                }
            }
        }

        a(ek.d dVar, f fVar, k6.b bVar) {
            this.f6078p = dVar;
            this.f6079q = fVar;
            this.f6080r = bVar;
        }

        @Override // java.lang.Runnable
        public void run() {
            try {
                androidx.work.multiprocess.a aVar = (androidx.work.multiprocess.a) this.f6078p.get();
                this.f6079q.Z(aVar.asBinder());
                e.this.f6075b.execute(new RunnableC0156a(aVar));
            } catch (InterruptedException | ExecutionException e10) {
                k.c().b(e.f6073e, "Unable to bind to service", e10);
                d.a.a(this.f6079q, e10);
            }
        }
    }

    /* loaded from: classes.dex */
    public static class b implements ServiceConnection {

        /* renamed from: b, reason: collision with root package name */
        private static final String f6084b = k.f("ListenableWorkerImplSession");

        /* renamed from: a, reason: collision with root package name */
        final androidx.work.impl.utils.futures.c f6085a = androidx.work.impl.utils.futures.c.s();

        @Override // android.content.ServiceConnection
        public void onBindingDied(ComponentName componentName) {
            k.c().h(f6084b, "Binding died", new Throwable[0]);
            this.f6085a.p(new RuntimeException("Binding died"));
        }

        @Override // android.content.ServiceConnection
        public void onNullBinding(ComponentName componentName) {
            k.c().b(f6084b, "Unable to bind to service", new Throwable[0]);
            this.f6085a.p(new RuntimeException(String.format("Cannot bind to service %s", componentName)));
        }

        @Override // android.content.ServiceConnection
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            k.c().a(f6084b, "Service connected", new Throwable[0]);
            this.f6085a.o(a.AbstractBinderC0153a.U(iBinder));
        }

        @Override // android.content.ServiceConnection
        public void onServiceDisconnected(ComponentName componentName) {
            k.c().h(f6084b, "Service disconnected", new Throwable[0]);
            this.f6085a.p(new RuntimeException("Service disconnected"));
        }
    }

    public e(Context context, Executor executor) {
        this.f6074a = context;
        this.f6075b = executor;
    }

    private static void d(b bVar, Throwable th2) {
        k.c().b(f6073e, "Unable to bind to service", th2);
        bVar.f6085a.p(th2);
    }

    public ek.d a(ComponentName componentName, k6.b bVar) {
        return b(c(componentName), bVar, new f());
    }

    public ek.d b(ek.d dVar, k6.b bVar, f fVar) {
        dVar.addListener(new a(dVar, fVar, bVar), this.f6075b);
        return fVar.W();
    }

    public ek.d c(ComponentName componentName) {
        androidx.work.impl.utils.futures.c cVar;
        synchronized (this.f6076c) {
            if (this.f6077d == null) {
                k.c().a(f6073e, String.format("Binding to %s, %s", componentName.getPackageName(), componentName.getClassName()), new Throwable[0]);
                this.f6077d = new b();
                try {
                    Intent intent = new Intent();
                    intent.setComponent(componentName);
                    if (!this.f6074a.bindService(intent, this.f6077d, 1)) {
                        d(this.f6077d, new RuntimeException("Unable to bind to service"));
                    }
                } catch (Throwable th2) {
                    d(this.f6077d, th2);
                }
            }
            cVar = this.f6077d.f6085a;
        }
        return cVar;
    }

    public void e() {
        synchronized (this.f6076c) {
            b bVar = this.f6077d;
            if (bVar != null) {
                this.f6074a.unbindService(bVar);
                this.f6077d = null;
            }
        }
    }
}
