package androidx.work.impl.background.gcm;

import a6.b;
import a6.d;
import a6.f;
import a6.i;
import android.content.Context;
import android.os.PowerManager;
import androidx.work.impl.WorkDatabase;
import com.google.android.gms.gcm.TaskParams;
import h6.r;
import i6.l;
import i6.p;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import z5.k;
import z5.t;

/* loaded from: classes.dex */
public class WorkManagerGcmDispatcher {
    private static final long AWAIT_TIME_IN_MILLISECONDS = 600000;
    private static final long AWAIT_TIME_IN_MINUTES = 10;
    static final String TAG = k.f("WrkMgrGcmDispatcher");
    private final Context mContext;
    i mWorkManagerImpl;
    private final p mWorkTimer;

    /* renamed from: androidx.work.impl.background.gcm.WorkManagerGcmDispatcher$3, reason: invalid class name */
    /* loaded from: classes.dex */
    static /* synthetic */ class AnonymousClass3 {
        static final /* synthetic */ int[] $SwitchMap$androidx$work$WorkInfo$State;

        static {
            int[] iArr = new int[t.a.values().length];
            $SwitchMap$androidx$work$WorkInfo$State = iArr;
            try {
                iArr[t.a.SUCCEEDED.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$androidx$work$WorkInfo$State[t.a.CANCELLED.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$androidx$work$WorkInfo$State[t.a.FAILED.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
        }
    }

    /* loaded from: classes.dex */
    static class WorkSpecExecutionListener implements b {
        private static final String TAG = k.f("WorkSpecExecutionListener");
        private final CountDownLatch mLatch = new CountDownLatch(1);
        private boolean mNeedsReschedule = false;
        private final String mWorkSpecId;

        WorkSpecExecutionListener(String str) {
            this.mWorkSpecId = str;
        }

        CountDownLatch getLatch() {
            return this.mLatch;
        }

        boolean needsReschedule() {
            return this.mNeedsReschedule;
        }

        @Override // a6.b
        public void onExecuted(String str, boolean z10) {
            if (!this.mWorkSpecId.equals(str)) {
                k.c().h(TAG, String.format("Notified for %s, but was looking for %s", str, this.mWorkSpecId), new Throwable[0]);
            } else {
                this.mNeedsReschedule = z10;
                this.mLatch.countDown();
            }
        }
    }

    /* loaded from: classes.dex */
    static class WorkSpecTimeLimitExceededListener implements p.b {
        private static final String TAG = k.f("WrkTimeLimitExceededLstnr");
        private final i mWorkManager;

        WorkSpecTimeLimitExceededListener(i iVar) {
            this.mWorkManager = iVar;
        }

        @Override // i6.p.b
        public void onTimeLimitExceeded(String str) {
            k.c().a(TAG, String.format("WorkSpec time limit exceeded %s", str), new Throwable[0]);
            this.mWorkManager.C(str);
        }
    }

    public WorkManagerGcmDispatcher(Context context, p pVar) {
        this.mContext = context.getApplicationContext();
        this.mWorkTimer = pVar;
        this.mWorkManagerImpl = i.o(context);
    }

    private int reschedule(final String str) {
        final WorkDatabase s10 = this.mWorkManagerImpl.s();
        s10.B(new Runnable() { // from class: androidx.work.impl.background.gcm.WorkManagerGcmDispatcher.2
            @Override // java.lang.Runnable
            public void run() {
                s10.O().b(str, -1L);
                f.b(WorkManagerGcmDispatcher.this.mWorkManagerImpl.m(), WorkManagerGcmDispatcher.this.mWorkManagerImpl.s(), WorkManagerGcmDispatcher.this.mWorkManagerImpl.r());
            }
        });
        k.c().a(TAG, String.format("Returning RESULT_SUCCESS for WorkSpec %s", str), new Throwable[0]);
        return 0;
    }

    public void onDestroy() {
        this.mWorkTimer.a();
    }

    public void onInitializeTasks() {
        this.mWorkManagerImpl.u().b(new Runnable() { // from class: androidx.work.impl.background.gcm.WorkManagerGcmDispatcher.1
            @Override // java.lang.Runnable
            public void run() {
                k.c().a(WorkManagerGcmDispatcher.TAG, "onInitializeTasks(): Rescheduling work", new Throwable[0]);
                WorkManagerGcmDispatcher.this.mWorkManagerImpl.x();
            }
        });
    }

    public int onRunTask(TaskParams taskParams) {
        k c10 = k.c();
        String str = TAG;
        c10.a(str, String.format("Handling task %s", taskParams), new Throwable[0]);
        String tag = taskParams.getTag();
        if (tag == null || tag.isEmpty()) {
            k.c().a(str, "Bad request. No workSpecId.", new Throwable[0]);
            return 2;
        }
        WorkSpecExecutionListener workSpecExecutionListener = new WorkSpecExecutionListener(tag);
        WorkSpecTimeLimitExceededListener workSpecTimeLimitExceededListener = new WorkSpecTimeLimitExceededListener(this.mWorkManagerImpl);
        d q10 = this.mWorkManagerImpl.q();
        q10.c(workSpecExecutionListener);
        PowerManager.WakeLock b10 = l.b(this.mContext, String.format("WorkGcm-onRunTask (%s)", tag));
        this.mWorkManagerImpl.z(tag);
        this.mWorkTimer.b(tag, AWAIT_TIME_IN_MILLISECONDS, workSpecTimeLimitExceededListener);
        try {
            try {
                b10.acquire();
                workSpecExecutionListener.getLatch().await(AWAIT_TIME_IN_MINUTES, TimeUnit.MINUTES);
                q10.h(workSpecExecutionListener);
                this.mWorkTimer.c(tag);
                b10.release();
                if (workSpecExecutionListener.needsReschedule()) {
                    k.c().a(str, String.format("Rescheduling WorkSpec %s", tag), new Throwable[0]);
                    return reschedule(tag);
                }
                r o10 = this.mWorkManagerImpl.s().O().o(tag);
                t.a aVar = o10 != null ? o10.f20891b : null;
                if (aVar == null) {
                    k.c().a(str, String.format("WorkSpec %s does not exist", tag), new Throwable[0]);
                    return 2;
                }
                int i10 = AnonymousClass3.$SwitchMap$androidx$work$WorkInfo$State[aVar.ordinal()];
                if (i10 == 1 || i10 == 2) {
                    k.c().a(str, String.format("Returning RESULT_SUCCESS for WorkSpec %s", tag), new Throwable[0]);
                    return 0;
                }
                if (i10 != 3) {
                    k.c().a(str, "Rescheduling eligible work.", new Throwable[0]);
                    return reschedule(tag);
                }
                k.c().a(str, String.format("Returning RESULT_FAILURE for WorkSpec %s", tag), new Throwable[0]);
                return 2;
            } catch (InterruptedException unused) {
                k.c().a(TAG, String.format("Rescheduling WorkSpec %s", tag), new Throwable[0]);
                int reschedule = reschedule(tag);
                q10.h(workSpecExecutionListener);
                this.mWorkTimer.c(tag);
                b10.release();
                return reschedule;
            }
        } catch (Throwable th2) {
            q10.h(workSpecExecutionListener);
            this.mWorkTimer.c(tag);
            b10.release();
            throw th2;
        }
    }
}
