package i4;

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 androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.work.impl.background.systemalarm.SystemAlarmService;
import androidx.work.impl.o;
import androidx.work.u;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ScheduledExecutorService;
import o4.j;
import o4.m;
import o4.t;

/* JADX INFO: loaded from: classes.dex */
public final class h implements androidx.work.impl.b {

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final /* synthetic */ int f26558j = 0;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f26559a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final p4.a f26560b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final t f26561c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final androidx.work.impl.d f26562d;

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Handler f26565g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final ArrayList f26566h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public Intent f26567i;

    @Nullable
    private g mCompletedListener;

    static {
        u.e("SystemAlarmDispatcher");
    }

    @VisibleForTesting
    public h(@NonNull Context context, @Nullable androidx.work.impl.d dVar, @Nullable o oVar) {
        Context applicationContext = context.getApplicationContext();
        this.f26559a = applicationContext;
        this.f26564f = new b(applicationContext);
        this.f26561c = new t();
        oVar = oVar == null ? o.b(context) : oVar;
        this.f26563e = oVar;
        dVar = dVar == null ? oVar.f4628f : dVar;
        this.f26562d = dVar;
        this.f26560b = oVar.f4626d;
        dVar.a(this);
        this.f26566h = new ArrayList();
        this.f26567i = null;
        this.f26565g = new Handler(Looper.getMainLooper());
    }

    public final void a(int i10, Intent intent) {
        u uVarC = u.c();
        String.format("Adding command %s (%s)", intent, Integer.valueOf(i10));
        uVarC.a(new Throwable[0]);
        b();
        String action = intent.getAction();
        if (TextUtils.isEmpty(action)) {
            u.c().f(new Throwable[0]);
            return;
        }
        if ("ACTION_CONSTRAINTS_CHANGED".equals(action)) {
            b();
            synchronized (this.f26566h) {
                try {
                    Iterator it = this.f26566h.iterator();
                    while (it.hasNext()) {
                        if ("ACTION_CONSTRAINTS_CHANGED".equals(((Intent) it.next()).getAction())) {
                            return;
                        }
                    }
                } finally {
                }
            }
        }
        intent.putExtra("KEY_START_ID", i10);
        synchronized (this.f26566h) {
            try {
                boolean z10 = !this.f26566h.isEmpty();
                this.f26566h.add(intent);
                if (!z10) {
                    f();
                }
            } finally {
            }
        }
    }

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

    /* JADX WARN: Removed duplicated region for block: B:32:0x0085 A[Catch: all -> 0x003d, TryCatch #2 {all -> 0x003d, blocks: (B:4:0x0010, B:6:0x0015, B:8:0x0039, B:11:0x003f, B:12:0x0046, B:13:0x0047, B:14:0x0051, B:18:0x005c, B:20:0x0064, B:21:0x0066, B:25:0x0071, B:27:0x007e, B:35:0x0090, B:31:0x0084, B:32:0x0085, B:34:0x008d, B:39:0x0094, B:22:0x0067, B:23:0x006e, B:15:0x0052, B:16:0x0059), top: B:46:0x0010, inners: #0, #1 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c() {
        boolean z10;
        boolean z11;
        u.c().a(new Throwable[0]);
        b();
        synchronized (this.f26566h) {
            try {
                if (this.f26567i != null) {
                    u uVarC = u.c();
                    String.format("Removing command %s", this.f26567i);
                    uVarC.a(new Throwable[0]);
                    if (!((Intent) this.f26566h.remove(0)).equals(this.f26567i)) {
                        throw new IllegalStateException("Dequeue-d command is not the first.");
                    }
                    this.f26567i = null;
                }
                j jVar = ((p4.c) this.f26560b).f31623a;
                b bVar = this.f26564f;
                synchronized (bVar.f26541c) {
                    z10 = !bVar.f26540b.isEmpty();
                }
                if (!z10 && this.f26566h.isEmpty()) {
                    synchronized (jVar.f31391c) {
                        z11 = !jVar.f31389a.isEmpty();
                    }
                    if (!z11) {
                        u.c().a(new Throwable[0]);
                        g gVar = this.mCompletedListener;
                        if (gVar != null) {
                            gVar.onAllCommandsCompleted();
                        }
                    }
                } else if (!this.f26566h.isEmpty()) {
                    f();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void d() {
        u.c().a(new Throwable[0]);
        this.f26562d.c(this);
        ScheduledExecutorService scheduledExecutorService = this.f26561c.f31418a;
        if (!scheduledExecutorService.isShutdown()) {
            scheduledExecutorService.shutdownNow();
        }
        this.mCompletedListener = null;
    }

    public final void e(Runnable runnable) {
        this.f26565g.post(runnable);
    }

    public final void f() {
        b();
        PowerManager.WakeLock wakeLockA = m.a(this.f26559a, "ProcessCommand");
        try {
            wakeLockA.acquire();
            ((p4.c) this.f26563e.f4626d).a(new f(this, 0));
        } finally {
            wakeLockA.release();
        }
    }

    public final void g(g gVar) {
        if (this.mCompletedListener != null) {
            u.c().b(new Throwable[0]);
        } else {
            this.mCompletedListener = gVar;
        }
    }

    @Override // androidx.work.impl.b
    public final void onExecuted(String str, boolean z10) {
        int i10 = b.f26538d;
        Intent intent = new Intent(this.f26559a, (Class<?>) SystemAlarmService.class);
        intent.setAction("ACTION_EXECUTION_COMPLETED");
        intent.putExtra("KEY_WORKSPEC_ID", str);
        intent.putExtra("KEY_NEEDS_RESCHEDULE", z10);
        e(new androidx.browser.customtabs.f(this, intent, 0));
    }
}
