package androidx.core.app;

import android.app.Service;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobServiceEngine;
import android.app.job.JobWorkItem;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import java.util.ArrayList;
import java.util.HashMap;

/* loaded from: classes.dex */
public abstract class h extends Service {

    /* renamed from: w, reason: collision with root package name */
    static final Object f3549w = new Object();

    /* renamed from: x, reason: collision with root package name */
    static final HashMap f3550x = new HashMap();

    /* renamed from: p, reason: collision with root package name */
    b f3551p;

    /* renamed from: q, reason: collision with root package name */
    g f3552q;

    /* renamed from: r, reason: collision with root package name */
    a f3553r;

    /* renamed from: s, reason: collision with root package name */
    boolean f3554s = false;

    /* renamed from: t, reason: collision with root package name */
    boolean f3555t = false;

    /* renamed from: u, reason: collision with root package name */
    boolean f3556u = false;

    /* renamed from: v, reason: collision with root package name */
    final ArrayList f3557v = null;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public final class a extends AsyncTask {
        a() {
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public Void doInBackground(Void... voidArr) {
            while (true) {
                d a10 = h.this.a();
                if (a10 == null) {
                    return null;
                }
                h.this.g(a10.getIntent());
                a10.a();
            }
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        /* renamed from: b, reason: merged with bridge method [inline-methods] */
        public void onCancelled(Void r12) {
            h.this.i();
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        /* renamed from: c, reason: merged with bridge method [inline-methods] */
        public void onPostExecute(Void r12) {
            h.this.i();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public interface b {
        IBinder a();

        d b();
    }

    /* loaded from: classes.dex */
    final class c implements d {

        /* renamed from: a, reason: collision with root package name */
        final Intent f3559a;

        /* renamed from: b, reason: collision with root package name */
        final int f3560b;

        c(Intent intent, int i10) {
            this.f3559a = intent;
            this.f3560b = i10;
        }

        @Override // androidx.core.app.h.d
        public void a() {
            h.this.stopSelf(this.f3560b);
        }

        @Override // androidx.core.app.h.d
        public Intent getIntent() {
            return this.f3559a;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public interface d {
        void a();

        Intent getIntent();
    }

    /* loaded from: classes.dex */
    static final class e extends JobServiceEngine implements b {

        /* renamed from: a, reason: collision with root package name */
        final h f3562a;

        /* renamed from: b, reason: collision with root package name */
        final Object f3563b;

        /* renamed from: c, reason: collision with root package name */
        JobParameters f3564c;

        /* loaded from: classes.dex */
        final class a implements d {

            /* renamed from: a, reason: collision with root package name */
            final JobWorkItem f3565a;

            a(JobWorkItem jobWorkItem) {
                this.f3565a = jobWorkItem;
            }

            @Override // androidx.core.app.h.d
            public void a() {
                synchronized (e.this.f3563b) {
                    JobParameters jobParameters = e.this.f3564c;
                    if (jobParameters != null) {
                        jobParameters.completeWork(this.f3565a);
                    }
                }
            }

            @Override // androidx.core.app.h.d
            public Intent getIntent() {
                return this.f3565a.getIntent();
            }
        }

        e(h hVar) {
            super(hVar);
            this.f3563b = new Object();
            this.f3562a = hVar;
        }

        @Override // androidx.core.app.h.b
        public IBinder a() {
            return getBinder();
        }

        @Override // androidx.core.app.h.b
        public d b() {
            synchronized (this.f3563b) {
                JobParameters jobParameters = this.f3564c;
                if (jobParameters == null) {
                    return null;
                }
                JobWorkItem dequeueWork = jobParameters.dequeueWork();
                if (dequeueWork == null) {
                    return null;
                }
                dequeueWork.getIntent().setExtrasClassLoader(this.f3562a.getClassLoader());
                return new a(dequeueWork);
            }
        }

        @Override // android.app.job.JobServiceEngine
        public boolean onStartJob(JobParameters jobParameters) {
            this.f3564c = jobParameters;
            this.f3562a.e(false);
            return true;
        }

        @Override // android.app.job.JobServiceEngine
        public boolean onStopJob(JobParameters jobParameters) {
            boolean b10 = this.f3562a.b();
            synchronized (this.f3563b) {
                this.f3564c = null;
            }
            return b10;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static final class f extends g {

        /* renamed from: d, reason: collision with root package name */
        private final JobInfo f3567d;

        /* renamed from: e, reason: collision with root package name */
        private final JobScheduler f3568e;

        f(Context context, ComponentName componentName, int i10) {
            super(componentName);
            b(i10);
            this.f3567d = new JobInfo.Builder(i10, this.f3569a).setOverrideDeadline(0L).build();
            this.f3568e = (JobScheduler) context.getApplicationContext().getSystemService("jobscheduler");
        }

        @Override // androidx.core.app.h.g
        void a(Intent intent) {
            this.f3568e.enqueue(this.f3567d, new JobWorkItem(intent));
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static abstract class g {

        /* renamed from: a, reason: collision with root package name */
        final ComponentName f3569a;

        /* renamed from: b, reason: collision with root package name */
        boolean f3570b;

        /* renamed from: c, reason: collision with root package name */
        int f3571c;

        g(ComponentName componentName) {
            this.f3569a = componentName;
        }

        abstract void a(Intent intent);

        void b(int i10) {
            if (!this.f3570b) {
                this.f3570b = true;
                this.f3571c = i10;
            } else {
                if (this.f3571c == i10) {
                    return;
                }
                throw new IllegalArgumentException("Given job ID " + i10 + " is different than previous " + this.f3571c);
            }
        }

        public void c() {
        }

        public void d() {
        }

        public void e() {
        }
    }

    public static void c(Context context, ComponentName componentName, int i10, Intent intent) {
        if (intent == null) {
            throw new IllegalArgumentException("work must not be null");
        }
        synchronized (f3549w) {
            g f10 = f(context, componentName, true, i10);
            f10.b(i10);
            f10.a(intent);
        }
    }

    public static void d(Context context, Class cls, int i10, Intent intent) {
        c(context, new ComponentName(context, (Class<?>) cls), i10, intent);
    }

    static g f(Context context, ComponentName componentName, boolean z10, int i10) {
        HashMap hashMap = f3550x;
        g gVar = (g) hashMap.get(componentName);
        if (gVar != null) {
            return gVar;
        }
        if (!z10) {
            throw new IllegalArgumentException("Can't be here without a job id");
        }
        f fVar = new f(context, componentName, i10);
        hashMap.put(componentName, fVar);
        return fVar;
    }

    d a() {
        b bVar = this.f3551p;
        if (bVar != null) {
            return bVar.b();
        }
        synchronized (this.f3557v) {
            if (this.f3557v.size() <= 0) {
                return null;
            }
            return (d) this.f3557v.remove(0);
        }
    }

    boolean b() {
        a aVar = this.f3553r;
        if (aVar != null) {
            aVar.cancel(this.f3554s);
        }
        this.f3555t = true;
        return h();
    }

    void e(boolean z10) {
        if (this.f3553r == null) {
            this.f3553r = new a();
            g gVar = this.f3552q;
            if (gVar != null && z10) {
                gVar.d();
            }
            this.f3553r.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
        }
    }

    protected abstract void g(Intent intent);

    public boolean h() {
        return true;
    }

    void i() {
        ArrayList arrayList = this.f3557v;
        if (arrayList != null) {
            synchronized (arrayList) {
                this.f3553r = null;
                ArrayList arrayList2 = this.f3557v;
                if (arrayList2 != null && arrayList2.size() > 0) {
                    e(false);
                } else if (!this.f3556u) {
                    this.f3552q.c();
                }
            }
        }
    }

    @Override // android.app.Service
    public IBinder onBind(Intent intent) {
        b bVar = this.f3551p;
        if (bVar != null) {
            return bVar.a();
        }
        return null;
    }

    @Override // android.app.Service
    public void onCreate() {
        super.onCreate();
        this.f3551p = new e(this);
        this.f3552q = null;
    }

    @Override // android.app.Service
    public void onDestroy() {
        super.onDestroy();
        ArrayList arrayList = this.f3557v;
        if (arrayList != null) {
            synchronized (arrayList) {
                this.f3556u = true;
                this.f3552q.c();
            }
        }
    }

    @Override // android.app.Service
    public int onStartCommand(Intent intent, int i10, int i11) {
        if (this.f3557v == null) {
            return 2;
        }
        this.f3552q.e();
        synchronized (this.f3557v) {
            ArrayList arrayList = this.f3557v;
            if (intent == null) {
                intent = new Intent();
            }
            arrayList.add(new c(intent, i11));
            e(true);
        }
        return 3;
    }
}
