package androidx.work.impl.background.systemalarm;

import android.content.Intent;
import android.os.PowerManager;
import androidx.annotation.MainThread;
import androidx.annotation.RestrictTo;
import androidx.lifecycle.LifecycleService;
import androidx.work.u;
import i4.g;
import i4.h;
import java.util.HashMap;
import java.util.WeakHashMap;
import o4.m;

/* JADX INFO: loaded from: classes.dex */
@RestrictTo({RestrictTo.Scope.f556b})
public class SystemAlarmService extends LifecycleService implements g {
    private static final String TAG = u.e("SystemAlarmService");
    private h mDispatcher;
    private boolean mIsShutdown;

    @MainThread
    private void initializeDispatcher() {
        h hVar = new h(this, null, null);
        this.mDispatcher = hVar;
        hVar.g(this);
    }

    @Override // i4.g
    @MainThread
    public void onAllCommandsCompleted() {
        this.mIsShutdown = true;
        u.c().a(new Throwable[0]);
        WeakHashMap weakHashMap = m.f31399a;
        HashMap map = new HashMap();
        WeakHashMap weakHashMap2 = m.f31399a;
        synchronized (weakHashMap2) {
            map.putAll(weakHashMap2);
        }
        for (PowerManager.WakeLock wakeLock : map.keySet()) {
            if (wakeLock != null && wakeLock.isHeld()) {
                String.format("WakeLock held for %s", map.get(wakeLock));
                u uVarC = u.c();
                WeakHashMap weakHashMap3 = m.f31399a;
                uVarC.f(new Throwable[0]);
            }
        }
        stopSelf();
    }

    @Override // androidx.lifecycle.LifecycleService, android.app.Service
    public void onCreate() {
        super.onCreate();
        initializeDispatcher();
        this.mIsShutdown = false;
    }

    @Override // androidx.lifecycle.LifecycleService, android.app.Service
    public void onDestroy() {
        super.onDestroy();
        this.mIsShutdown = true;
        this.mDispatcher.d();
    }

    @Override // androidx.lifecycle.LifecycleService, android.app.Service
    public int onStartCommand(Intent intent, int i10, int i11) {
        super.onStartCommand(intent, i10, i11);
        if (this.mIsShutdown) {
            u.c().d(new Throwable[0]);
            this.mDispatcher.d();
            initializeDispatcher();
            this.mIsShutdown = false;
        }
        if (intent == null) {
            return 3;
        }
        this.mDispatcher.a(i11, intent);
        return 3;
    }
}
