package androidx.work.impl.foreground;

import android.app.ForegroundServiceStartNotAllowedException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import androidx.work.impl.foreground.b;
import defpackage.eh5;
import defpackage.l18;
import defpackage.m38;
import defpackage.n85;
import defpackage.osa;
import defpackage.ps0;
import defpackage.wd2;
import java.util.UUID;

/* JADX INFO: loaded from: classes.dex */
@m38
public class SystemForegroundService extends n85 implements b.a {
    public static final String f = eh5.d("SystemFgService");
    public Handler b;
    public boolean c;
    public androidx.work.impl.foreground.b d;
    public NotificationManager e;

    @l18
    public static class a {
        @wd2
        public static void a(Service service, int i, Notification notification, int i2) {
            service.startForeground(i, notification, i2);
        }
    }

    @l18
    public static class b {
        @wd2
        public static void a(Service service, int i, Notification notification, int i2) {
            try {
                service.startForeground(i, notification, i2);
            } catch (ForegroundServiceStartNotAllowedException e) {
                eh5.c().f(SystemForegroundService.f, "Unable to start foreground service", e);
            }
        }
    }

    public final void a(int i) {
        this.b.post(new e(this, i));
    }

    public final void b() {
        this.b = new Handler(Looper.getMainLooper());
        this.e = (NotificationManager) getApplicationContext().getSystemService("notification");
        androidx.work.impl.foreground.b bVar = new androidx.work.impl.foreground.b(getApplicationContext());
        this.d = bVar;
        if (bVar.k != null) {
            eh5.c().a(androidx.work.impl.foreground.b.l, "A callback already exists.");
        } else {
            bVar.k = this;
        }
    }

    public final void c(int i, int i2, Notification notification) {
        this.b.post(new c(this, i, notification, i2));
    }

    @Override // defpackage.n85, android.app.Service
    public final void onCreate() {
        super.onCreate();
        b();
    }

    @Override // defpackage.n85, android.app.Service
    public final void onDestroy() {
        super.onDestroy();
        this.d.f();
    }

    @Override // android.app.Service
    public final int onStartCommand(Intent intent, int i, int i2) {
        super.onStartCommand(intent, i, i2);
        if (this.c) {
            eh5.c().getClass();
            this.d.f();
            b();
            this.c = false;
        }
        if (intent == null) {
            return 3;
        }
        androidx.work.impl.foreground.b bVar = this.d;
        bVar.getClass();
        String str = androidx.work.impl.foreground.b.l;
        String action = intent.getAction();
        if ("ACTION_START_FOREGROUND".equals(action)) {
            eh5 eh5VarC = eh5.c();
            intent.toString();
            eh5VarC.getClass();
            bVar.b.d(new androidx.work.impl.foreground.a(bVar, intent.getStringExtra("KEY_WORKSPEC_ID")));
            bVar.e(intent);
            return 3;
        }
        if ("ACTION_NOTIFY".equals(action)) {
            bVar.e(intent);
            return 3;
        }
        if (!"ACTION_CANCEL_WORK".equals(action)) {
            if (!"ACTION_STOP_FOREGROUND".equals(action)) {
                return 3;
            }
            eh5.c().getClass();
            SystemForegroundService systemForegroundService = bVar.k;
            if (systemForegroundService == null) {
                return 3;
            }
            systemForegroundService.c = true;
            eh5.c().getClass();
            systemForegroundService.stopForeground(true);
            systemForegroundService.stopSelf();
            return 3;
        }
        eh5 eh5VarC2 = eh5.c();
        intent.toString();
        eh5VarC2.getClass();
        String stringExtra = intent.getStringExtra("KEY_WORKSPEC_ID");
        if (stringExtra == null || TextUtils.isEmpty(stringExtra)) {
            return 3;
        }
        osa osaVar = bVar.a;
        UUID uuidFromString = UUID.fromString(stringExtra);
        osaVar.getClass();
        osaVar.d.d(ps0.b(osaVar, uuidFromString));
        return 3;
    }
}
