package androidx.work.impl.foreground;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.browser.customtabs.i;
import androidx.lifecycle.LifecycleService;
import androidx.work.u;

/* JADX INFO: loaded from: classes.dex */
@RestrictTo({RestrictTo.Scope.f556b})
public class SystemForegroundService extends LifecycleService implements b {
    private static final String TAG = u.e("SystemFgService");

    @Nullable
    private static SystemForegroundService sForegroundService = null;
    c mDispatcher;
    private Handler mHandler;
    private boolean mIsShutdown;
    NotificationManager mNotificationManager;

    @Nullable
    public static SystemForegroundService getInstance() {
        return sForegroundService;
    }

    @MainThread
    private void initializeDispatcher() {
        this.mHandler = new Handler(Looper.getMainLooper());
        this.mNotificationManager = (NotificationManager) getApplicationContext().getSystemService("notification");
        c cVar = new c(getApplicationContext());
        this.mDispatcher = cVar;
        cVar.h(this);
    }

    @Override // androidx.work.impl.foreground.b
    public void cancelNotification(int i10) {
        this.mHandler.post(new e(this, i10));
    }

    @Override // androidx.work.impl.foreground.b
    public void notify(int i10, @NonNull Notification notification) {
        this.mHandler.post(new d(this, i10, notification));
    }

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

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

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

    @Override // androidx.work.impl.foreground.b
    public void startForeground(int i10, int i11, @NonNull Notification notification) {
        this.mHandler.post(new i(this, i10, notification, i11));
    }

    @Override // androidx.work.impl.foreground.b
    @MainThread
    public void stop() {
        this.mIsShutdown = true;
        u.c().a(new Throwable[0]);
        if (Build.VERSION.SDK_INT >= 26) {
            stopForeground(true);
        }
        sForegroundService = null;
        stopSelf();
    }
}
