379 lines
11 KiB
Java
379 lines
11 KiB
Java
package androidx.core.app;
|
|
|
|
import android.app.Service;
|
|
import android.app.job.JobInfo;
|
|
import android.app.job.JobParameters;
|
|
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.Build;
|
|
import android.os.IBinder;
|
|
import android.os.PowerManager;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
|
|
/* loaded from: classes.dex */
|
|
public abstract class JobIntentService extends Service {
|
|
static final HashMap<ComponentName, WorkEnqueuer> g = new HashMap<>();
|
|
CompatJobEngine a;
|
|
WorkEnqueuer b;
|
|
CommandProcessor c;
|
|
boolean d = false;
|
|
boolean e = false;
|
|
final ArrayList<CompatWorkItem> f;
|
|
|
|
interface CompatJobEngine {
|
|
IBinder a();
|
|
|
|
GenericWorkItem b();
|
|
}
|
|
|
|
static final class CompatWorkEnqueuer extends WorkEnqueuer {
|
|
private final PowerManager.WakeLock d;
|
|
private final PowerManager.WakeLock e;
|
|
boolean f;
|
|
boolean g;
|
|
|
|
CompatWorkEnqueuer(Context context, ComponentName componentName) {
|
|
super(context, componentName);
|
|
context.getApplicationContext();
|
|
PowerManager powerManager = (PowerManager) context.getSystemService("power");
|
|
this.d = powerManager.newWakeLock(1, componentName.getClassName() + ":launch");
|
|
this.d.setReferenceCounted(false);
|
|
this.e = powerManager.newWakeLock(1, componentName.getClassName() + ":run");
|
|
this.e.setReferenceCounted(false);
|
|
}
|
|
|
|
@Override // androidx.core.app.JobIntentService.WorkEnqueuer
|
|
public void a() {
|
|
synchronized (this) {
|
|
if (this.g) {
|
|
if (this.f) {
|
|
this.d.acquire(60000L);
|
|
}
|
|
this.g = false;
|
|
this.e.release();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // androidx.core.app.JobIntentService.WorkEnqueuer
|
|
public void b() {
|
|
synchronized (this) {
|
|
if (!this.g) {
|
|
this.g = true;
|
|
this.e.acquire(600000L);
|
|
this.d.release();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // androidx.core.app.JobIntentService.WorkEnqueuer
|
|
public void c() {
|
|
synchronized (this) {
|
|
this.f = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
final class CompatWorkItem implements GenericWorkItem {
|
|
final Intent a;
|
|
final int b;
|
|
|
|
CompatWorkItem(Intent intent, int i) {
|
|
this.a = intent;
|
|
this.b = i;
|
|
}
|
|
|
|
@Override // androidx.core.app.JobIntentService.GenericWorkItem
|
|
public void a() {
|
|
JobIntentService.this.stopSelf(this.b);
|
|
}
|
|
|
|
@Override // androidx.core.app.JobIntentService.GenericWorkItem
|
|
public Intent getIntent() {
|
|
return this.a;
|
|
}
|
|
}
|
|
|
|
interface GenericWorkItem {
|
|
void a();
|
|
|
|
Intent getIntent();
|
|
}
|
|
|
|
static final class JobServiceEngineImpl extends JobServiceEngine implements CompatJobEngine {
|
|
final JobIntentService a;
|
|
final Object b;
|
|
JobParameters c;
|
|
|
|
final class WrapperWorkItem implements GenericWorkItem {
|
|
final JobWorkItem a;
|
|
|
|
WrapperWorkItem(JobWorkItem jobWorkItem) {
|
|
this.a = jobWorkItem;
|
|
}
|
|
|
|
@Override // androidx.core.app.JobIntentService.GenericWorkItem
|
|
public void a() {
|
|
synchronized (JobServiceEngineImpl.this.b) {
|
|
if (JobServiceEngineImpl.this.c != null) {
|
|
JobServiceEngineImpl.this.c.completeWork(this.a);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // androidx.core.app.JobIntentService.GenericWorkItem
|
|
public Intent getIntent() {
|
|
return this.a.getIntent();
|
|
}
|
|
}
|
|
|
|
JobServiceEngineImpl(JobIntentService jobIntentService) {
|
|
super(jobIntentService);
|
|
this.b = new Object();
|
|
this.a = jobIntentService;
|
|
}
|
|
|
|
@Override // androidx.core.app.JobIntentService.CompatJobEngine
|
|
public IBinder a() {
|
|
return getBinder();
|
|
}
|
|
|
|
@Override // androidx.core.app.JobIntentService.CompatJobEngine
|
|
public GenericWorkItem b() {
|
|
synchronized (this.b) {
|
|
if (this.c == null) {
|
|
return null;
|
|
}
|
|
JobWorkItem dequeueWork = this.c.dequeueWork();
|
|
if (dequeueWork == null) {
|
|
return null;
|
|
}
|
|
dequeueWork.getIntent().setExtrasClassLoader(this.a.getClassLoader());
|
|
return new WrapperWorkItem(dequeueWork);
|
|
}
|
|
}
|
|
|
|
@Override // android.app.job.JobServiceEngine
|
|
public boolean onStartJob(JobParameters jobParameters) {
|
|
this.c = jobParameters;
|
|
this.a.a(false);
|
|
return true;
|
|
}
|
|
|
|
@Override // android.app.job.JobServiceEngine
|
|
public boolean onStopJob(JobParameters jobParameters) {
|
|
boolean b = this.a.b();
|
|
synchronized (this.b) {
|
|
this.c = null;
|
|
}
|
|
return b;
|
|
}
|
|
}
|
|
|
|
static final class JobWorkEnqueuer extends WorkEnqueuer {
|
|
JobWorkEnqueuer(Context context, ComponentName componentName, int i) {
|
|
super(context, componentName);
|
|
a(i);
|
|
new JobInfo.Builder(i, this.a).setOverrideDeadline(0L).build();
|
|
}
|
|
}
|
|
|
|
static abstract class WorkEnqueuer {
|
|
final ComponentName a;
|
|
boolean b;
|
|
int c;
|
|
|
|
WorkEnqueuer(Context context, ComponentName componentName) {
|
|
this.a = componentName;
|
|
}
|
|
|
|
public void a() {
|
|
}
|
|
|
|
void a(int i) {
|
|
if (!this.b) {
|
|
this.b = true;
|
|
this.c = i;
|
|
} else {
|
|
if (this.c == i) {
|
|
return;
|
|
}
|
|
throw new IllegalArgumentException("Given job ID " + i + " is different than previous " + this.c);
|
|
}
|
|
}
|
|
|
|
public void b() {
|
|
}
|
|
|
|
public void c() {
|
|
}
|
|
}
|
|
|
|
public JobIntentService() {
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
this.f = null;
|
|
} else {
|
|
this.f = new ArrayList<>();
|
|
}
|
|
}
|
|
|
|
static WorkEnqueuer a(Context context, ComponentName componentName, boolean z, int i) {
|
|
WorkEnqueuer compatWorkEnqueuer;
|
|
WorkEnqueuer workEnqueuer = g.get(componentName);
|
|
if (workEnqueuer != null) {
|
|
return workEnqueuer;
|
|
}
|
|
if (Build.VERSION.SDK_INT < 26) {
|
|
compatWorkEnqueuer = new CompatWorkEnqueuer(context, componentName);
|
|
} else {
|
|
if (!z) {
|
|
throw new IllegalArgumentException("Can't be here without a job id");
|
|
}
|
|
compatWorkEnqueuer = new JobWorkEnqueuer(context, componentName, i);
|
|
}
|
|
WorkEnqueuer workEnqueuer2 = compatWorkEnqueuer;
|
|
g.put(componentName, workEnqueuer2);
|
|
return workEnqueuer2;
|
|
}
|
|
|
|
protected abstract void a(Intent intent);
|
|
|
|
boolean b() {
|
|
CommandProcessor commandProcessor = this.c;
|
|
if (commandProcessor != null) {
|
|
commandProcessor.cancel(this.d);
|
|
}
|
|
return c();
|
|
}
|
|
|
|
public boolean c() {
|
|
return true;
|
|
}
|
|
|
|
void d() {
|
|
ArrayList<CompatWorkItem> arrayList = this.f;
|
|
if (arrayList != null) {
|
|
synchronized (arrayList) {
|
|
this.c = null;
|
|
if (this.f != null && this.f.size() > 0) {
|
|
a(false);
|
|
} else if (!this.e) {
|
|
this.b.a();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // android.app.Service
|
|
public IBinder onBind(Intent intent) {
|
|
CompatJobEngine compatJobEngine = this.a;
|
|
if (compatJobEngine != null) {
|
|
return compatJobEngine.a();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override // android.app.Service
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
this.a = new JobServiceEngineImpl(this);
|
|
this.b = null;
|
|
} else {
|
|
this.a = null;
|
|
this.b = a(this, new ComponentName(this, (Class<?>) JobIntentService.class), false, 0);
|
|
}
|
|
}
|
|
|
|
@Override // android.app.Service
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
ArrayList<CompatWorkItem> arrayList = this.f;
|
|
if (arrayList != null) {
|
|
synchronized (arrayList) {
|
|
this.e = true;
|
|
this.b.a();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // android.app.Service
|
|
public int onStartCommand(Intent intent, int i, int i2) {
|
|
if (this.f == null) {
|
|
return 2;
|
|
}
|
|
this.b.c();
|
|
synchronized (this.f) {
|
|
ArrayList<CompatWorkItem> arrayList = this.f;
|
|
if (intent == null) {
|
|
intent = new Intent();
|
|
}
|
|
arrayList.add(new CompatWorkItem(intent, i2));
|
|
a(true);
|
|
}
|
|
return 3;
|
|
}
|
|
|
|
final class CommandProcessor extends AsyncTask<Void, Void, Void> {
|
|
CommandProcessor() {
|
|
}
|
|
|
|
/* 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) {
|
|
GenericWorkItem a = JobIntentService.this.a();
|
|
if (a == null) {
|
|
return null;
|
|
}
|
|
JobIntentService.this.a(a.getIntent());
|
|
a.a();
|
|
}
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: protected */
|
|
@Override // android.os.AsyncTask
|
|
/* renamed from: b, reason: merged with bridge method [inline-methods] */
|
|
public void onPostExecute(Void r1) {
|
|
JobIntentService.this.d();
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: protected */
|
|
@Override // android.os.AsyncTask
|
|
/* renamed from: a, reason: merged with bridge method [inline-methods] */
|
|
public void onCancelled(Void r1) {
|
|
JobIntentService.this.d();
|
|
}
|
|
}
|
|
|
|
void a(boolean z) {
|
|
if (this.c == null) {
|
|
this.c = new CommandProcessor();
|
|
WorkEnqueuer workEnqueuer = this.b;
|
|
if (workEnqueuer != null && z) {
|
|
workEnqueuer.b();
|
|
}
|
|
this.c.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
|
|
}
|
|
}
|
|
|
|
GenericWorkItem a() {
|
|
CompatJobEngine compatJobEngine = this.a;
|
|
if (compatJobEngine != null) {
|
|
return compatJobEngine.b();
|
|
}
|
|
synchronized (this.f) {
|
|
if (this.f.size() <= 0) {
|
|
return null;
|
|
}
|
|
return this.f.remove(0);
|
|
}
|
|
}
|
|
}
|