package androidx.work.impl.background.systemalarm;

import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.os.PowerManager;
import android.text.TextUtils;
import defpackage.ava;
import defpackage.eh5;
import defpackage.fna;
import defpackage.gsa;
import defpackage.jsa;
import defpackage.ksa;
import defpackage.li8;
import defpackage.lr2;
import defpackage.m38;
import defpackage.osa;
import defpackage.ti7;
import defpackage.tj9;
import defpackage.x19;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.Executor;

/* JADX INFO: loaded from: classes.dex */
@m38
public class g implements lr2 {
    public static final String m = eh5.d("SystemAlarmDispatcher");
    public final Context a;
    public final tj9 b;
    public final ava c;
    public final ti7 d;
    public final osa e;
    public final androidx.work.impl.background.systemalarm.b f;
    public final ArrayList i;
    public Intent j;
    public SystemAlarmService k;
    public final jsa l;

    public static class a implements Runnable {
        public final g a;
        public final Intent b;
        public final int c;

        public a(int i, Intent intent, g gVar) {
            this.a = gVar;
            this.b = intent;
            this.c = i;
        }

        @Override // java.lang.Runnable
        public final void run() {
            Intent intent = this.b;
            this.a.b(this.c, intent);
        }
    }

    public interface b {
    }

    public static class c implements Runnable {
        public final g a;

        public c(g gVar) {
            this.a = gVar;
        }

        /* JADX WARN: Removed duplicated region for block: B:32:0x0087 A[Catch: all -> 0x0036, TryCatch #2 {all -> 0x0036, blocks: (B:4:0x0011, B:6:0x0015, B:8:0x0032, B:11:0x0038, B:12:0x003f, B:13:0x0040, B:14:0x004a, B:18:0x0054, B:20:0x005c, B:21:0x005e, B:25:0x0068, B:27:0x0073, B:35:0x0092, B:31:0x0086, B:32:0x0087, B:34:0x008f, B:39:0x0096, B:22:0x005f, B:23:0x0065, B:15:0x004b, B:16:0x0051), top: B:46:0x0011, inners: #0, #1 }] */
        @Override // java.lang.Runnable
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final void run() {
            boolean zIsEmpty;
            boolean zIsEmpty2;
            g gVar = this.a;
            eh5 eh5VarC = eh5.c();
            String str = g.m;
            eh5VarC.getClass();
            g.c();
            synchronized (gVar.i) {
                try {
                    if (gVar.j != null) {
                        eh5 eh5VarC2 = eh5.c();
                        Objects.toString(gVar.j);
                        eh5VarC2.getClass();
                        if (!((Intent) gVar.i.remove(0)).equals(gVar.j)) {
                            throw new IllegalStateException("Dequeue-d command is not the first.");
                        }
                        gVar.j = null;
                    }
                    li8 li8VarC = gVar.b.c();
                    androidx.work.impl.background.systemalarm.b bVar = gVar.f;
                    synchronized (bVar.c) {
                        zIsEmpty = bVar.b.isEmpty();
                    }
                    if (zIsEmpty && gVar.i.isEmpty()) {
                        synchronized (li8VarC.d) {
                            zIsEmpty2 = li8VarC.a.isEmpty();
                        }
                        if (zIsEmpty2) {
                            eh5.c().getClass();
                            SystemAlarmService systemAlarmService = gVar.k;
                            if (systemAlarmService != null) {
                                systemAlarmService.c = true;
                                eh5.c().getClass();
                                fna.a();
                                systemAlarmService.stopSelf();
                            }
                        }
                    } else if (!gVar.i.isEmpty()) {
                        gVar.d();
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    public g(SystemAlarmService systemAlarmService) {
        Context applicationContext = systemAlarmService.getApplicationContext();
        this.a = applicationContext;
        x19 x19Var = new x19();
        osa osaVarD = osa.d(systemAlarmService);
        this.e = osaVarD;
        this.f = new androidx.work.impl.background.systemalarm.b(applicationContext, osaVarD.b.c, x19Var);
        this.c = new ava(osaVarD.b.f);
        ti7 ti7Var = osaVarD.f;
        this.d = ti7Var;
        tj9 tj9Var = osaVarD.d;
        this.b = tj9Var;
        this.l = new ksa(ti7Var, tj9Var);
        ti7Var.b(this);
        this.i = new ArrayList();
        this.j = null;
    }

    public static void c() {
        if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
            throw new IllegalStateException("Needs to be invoked on the main thread.");
        }
    }

    @Override // defpackage.lr2
    public final void a(gsa gsaVar, boolean z) {
        Executor executorA = this.b.a();
        String str = androidx.work.impl.background.systemalarm.b.f;
        Intent intent = new Intent(this.a, (Class<?>) SystemAlarmService.class);
        intent.setAction("ACTION_EXECUTION_COMPLETED");
        intent.putExtra("KEY_NEEDS_RESCHEDULE", z);
        androidx.work.impl.background.systemalarm.b.d(intent, gsaVar);
        executorA.execute(new a(0, intent, this));
    }

    public final void b(int i, Intent intent) {
        eh5 eh5VarC = eh5.c();
        String str = m;
        intent.toString();
        eh5VarC.getClass();
        c();
        String action = intent.getAction();
        if (TextUtils.isEmpty(action)) {
            eh5.c().e(str, "Unknown command. Ignoring");
            return;
        }
        if ("ACTION_CONSTRAINTS_CHANGED".equals(action)) {
            c();
            synchronized (this.i) {
                try {
                    Iterator it = this.i.iterator();
                    while (it.hasNext()) {
                        if ("ACTION_CONSTRAINTS_CHANGED".equals(((Intent) it.next()).getAction())) {
                            return;
                        }
                    }
                } finally {
                }
            }
        }
        intent.putExtra("KEY_START_ID", i);
        synchronized (this.i) {
            try {
                boolean zIsEmpty = this.i.isEmpty();
                this.i.add(intent);
                if (zIsEmpty) {
                    d();
                }
            } finally {
            }
        }
    }

    public final void d() {
        c();
        PowerManager.WakeLock wakeLockB = fna.b(this.a, "ProcessCommand");
        try {
            wakeLockB.acquire();
            this.e.d.d(new f(this));
        } finally {
            wakeLockB.release();
        }
    }
}
