package androidx.work.impl.background.systemalarm;

import a6.i;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.text.TextUtils;
import i6.h;
import i6.l;
import i6.p;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import z5.k;

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

    /* renamed from: z, reason: collision with root package name */
    static final String f5948z = k.f("SystemAlarmDispatcher");

    /* renamed from: p, reason: collision with root package name */
    final Context f5949p;

    /* renamed from: q, reason: collision with root package name */
    private final j6.a f5950q;

    /* renamed from: r, reason: collision with root package name */
    private final p f5951r;

    /* renamed from: s, reason: collision with root package name */
    private final a6.d f5952s;

    /* renamed from: t, reason: collision with root package name */
    private final i f5953t;

    /* renamed from: u, reason: collision with root package name */
    final androidx.work.impl.background.systemalarm.b f5954u;

    /* renamed from: v, reason: collision with root package name */
    private final Handler f5955v;

    /* renamed from: w, reason: collision with root package name */
    final List f5956w;

    /* renamed from: x, reason: collision with root package name */
    Intent f5957x;

    /* renamed from: y, reason: collision with root package name */
    private c f5958y;

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

        @Override // java.lang.Runnable
        public void run() {
            e eVar;
            d dVar;
            synchronized (e.this.f5956w) {
                e eVar2 = e.this;
                eVar2.f5957x = (Intent) eVar2.f5956w.get(0);
            }
            Intent intent = e.this.f5957x;
            if (intent != null) {
                String action = intent.getAction();
                int intExtra = e.this.f5957x.getIntExtra("KEY_START_ID", 0);
                k c10 = k.c();
                String str = e.f5948z;
                c10.a(str, String.format("Processing command %s, %s", e.this.f5957x, Integer.valueOf(intExtra)), new Throwable[0]);
                PowerManager.WakeLock b10 = l.b(e.this.f5949p, String.format("%s (%s)", action, Integer.valueOf(intExtra)));
                try {
                    k.c().a(str, String.format("Acquiring operation wake lock (%s) %s", action, b10), new Throwable[0]);
                    b10.acquire();
                    e eVar3 = e.this;
                    eVar3.f5954u.n(eVar3.f5957x, intExtra, eVar3);
                    k.c().a(str, String.format("Releasing operation wake lock (%s) %s", action, b10), new Throwable[0]);
                    b10.release();
                    eVar = e.this;
                    dVar = new d(eVar);
                } catch (Throwable th2) {
                    try {
                        k c11 = k.c();
                        String str2 = e.f5948z;
                        c11.b(str2, "Unexpected error in onHandleIntent", th2);
                        k.c().a(str2, String.format("Releasing operation wake lock (%s) %s", action, b10), new Throwable[0]);
                        b10.release();
                        eVar = e.this;
                        dVar = new d(eVar);
                    } catch (Throwable th3) {
                        k.c().a(e.f5948z, String.format("Releasing operation wake lock (%s) %s", action, b10), new Throwable[0]);
                        b10.release();
                        e eVar4 = e.this;
                        eVar4.j(new d(eVar4));
                        throw th3;
                    }
                }
                eVar.j(dVar);
            }
        }
    }

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

        /* renamed from: p, reason: collision with root package name */
        private final e f5960p;

        /* renamed from: q, reason: collision with root package name */
        private final Intent f5961q;

        /* renamed from: r, reason: collision with root package name */
        private final int f5962r;

        /* JADX INFO: Access modifiers changed from: package-private */
        public b(e eVar, Intent intent, int i10) {
            this.f5960p = eVar;
            this.f5961q = intent;
            this.f5962r = i10;
        }

        @Override // java.lang.Runnable
        public void run() {
            this.f5960p.a(this.f5961q, this.f5962r);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public interface c {
        void a();
    }

    /* loaded from: classes.dex */
    static class d implements Runnable {

        /* renamed from: p, reason: collision with root package name */
        private final e f5963p;

        d(e eVar) {
            this.f5963p = eVar;
        }

        @Override // java.lang.Runnable
        public void run() {
            this.f5963p.c();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public e(Context context) {
        this(context, null, null);
    }

    private void b() {
        if (this.f5955v.getLooper().getThread() != Thread.currentThread()) {
            throw new IllegalStateException("Needs to be invoked on the main thread.");
        }
    }

    private boolean h(String str) {
        b();
        synchronized (this.f5956w) {
            Iterator it = this.f5956w.iterator();
            while (it.hasNext()) {
                if (str.equals(((Intent) it.next()).getAction())) {
                    return true;
                }
            }
            return false;
        }
    }

    private void k() {
        b();
        PowerManager.WakeLock b10 = l.b(this.f5949p, "ProcessCommand");
        try {
            b10.acquire();
            this.f5953t.u().b(new a());
        } finally {
            b10.release();
        }
    }

    public boolean a(Intent intent, int i10) {
        k c10 = k.c();
        String str = f5948z;
        c10.a(str, String.format("Adding command %s (%s)", intent, Integer.valueOf(i10)), new Throwable[0]);
        b();
        String action = intent.getAction();
        if (TextUtils.isEmpty(action)) {
            k.c().h(str, "Unknown command. Ignoring", new Throwable[0]);
            return false;
        }
        if ("ACTION_CONSTRAINTS_CHANGED".equals(action) && h("ACTION_CONSTRAINTS_CHANGED")) {
            return false;
        }
        intent.putExtra("KEY_START_ID", i10);
        synchronized (this.f5956w) {
            boolean z10 = this.f5956w.isEmpty() ? false : true;
            this.f5956w.add(intent);
            if (!z10) {
                k();
            }
        }
        return true;
    }

    void c() {
        k c10 = k.c();
        String str = f5948z;
        c10.a(str, "Checking if commands are complete.", new Throwable[0]);
        b();
        synchronized (this.f5956w) {
            if (this.f5957x != null) {
                k.c().a(str, String.format("Removing command %s", this.f5957x), new Throwable[0]);
                if (!((Intent) this.f5956w.remove(0)).equals(this.f5957x)) {
                    throw new IllegalStateException("Dequeue-d command is not the first.");
                }
                this.f5957x = null;
            }
            h c11 = this.f5950q.c();
            if (!this.f5954u.m() && this.f5956w.isEmpty() && !c11.a()) {
                k.c().a(str, "No more commands & intents.", new Throwable[0]);
                c cVar = this.f5958y;
                if (cVar != null) {
                    cVar.a();
                }
            } else if (!this.f5956w.isEmpty()) {
                k();
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public a6.d d() {
        return this.f5952s;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public j6.a e() {
        return this.f5950q;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public i f() {
        return this.f5953t;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public p g() {
        return this.f5951r;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void i() {
        k.c().a(f5948z, "Destroying SystemAlarmDispatcher", new Throwable[0]);
        this.f5952s.h(this);
        this.f5951r.a();
        this.f5958y = null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void j(Runnable runnable) {
        this.f5955v.post(runnable);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void l(c cVar) {
        if (this.f5958y != null) {
            k.c().b(f5948z, "A completion listener for SystemAlarmDispatcher already exists.", new Throwable[0]);
        } else {
            this.f5958y = cVar;
        }
    }

    @Override // a6.b
    public void onExecuted(String str, boolean z10) {
        j(new b(this, androidx.work.impl.background.systemalarm.b.c(this.f5949p, str, z10), 0));
    }

    e(Context context, a6.d dVar, i iVar) {
        Context applicationContext = context.getApplicationContext();
        this.f5949p = applicationContext;
        this.f5954u = new androidx.work.impl.background.systemalarm.b(applicationContext);
        this.f5951r = new p();
        iVar = iVar == null ? i.o(context) : iVar;
        this.f5953t = iVar;
        dVar = dVar == null ? iVar.q() : dVar;
        this.f5952s = dVar;
        this.f5950q = iVar.u();
        dVar.c(this);
        this.f5956w = new ArrayList();
        this.f5957x = null;
        this.f5955v = new Handler(Looper.getMainLooper());
    }
}
