243 lines
7.7 KiB
Java
243 lines
7.7 KiB
Java
package io.fabric.sdk.android.services.concurrency;
|
|
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.os.Message;
|
|
import android.os.Process;
|
|
import android.util.Log;
|
|
import com.ubtrobot.jimu.robotapi.PeripheralType;
|
|
import java.util.LinkedList;
|
|
import java.util.concurrent.BlockingQueue;
|
|
import java.util.concurrent.Callable;
|
|
import java.util.concurrent.CancellationException;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.Executor;
|
|
import java.util.concurrent.FutureTask;
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
import java.util.concurrent.ThreadFactory;
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public abstract class AsyncTask<Params, Progress, Result> {
|
|
private static final int f = Runtime.getRuntime().availableProcessors();
|
|
private static final int g;
|
|
private static final int h;
|
|
private static final ThreadFactory i;
|
|
private static final BlockingQueue<Runnable> j;
|
|
public static final Executor k;
|
|
public static final Executor l;
|
|
private static final InternalHandler m;
|
|
private volatile Status c = Status.PENDING;
|
|
private final AtomicBoolean d = new AtomicBoolean();
|
|
private final AtomicBoolean e = new AtomicBoolean();
|
|
private final WorkerRunnable<Params, Result> a = new WorkerRunnable<Params, Result>() { // from class: io.fabric.sdk.android.services.concurrency.AsyncTask.2
|
|
@Override // java.util.concurrent.Callable
|
|
public Result call() throws Exception {
|
|
AsyncTask.this.e.set(true);
|
|
Process.setThreadPriority(10);
|
|
AsyncTask asyncTask = AsyncTask.this;
|
|
Result result = (Result) asyncTask.a(this.a);
|
|
AsyncTask.a(asyncTask, result);
|
|
return result;
|
|
}
|
|
};
|
|
private final FutureTask<Result> b = new FutureTask<Result>(this.a) { // from class: io.fabric.sdk.android.services.concurrency.AsyncTask.3
|
|
@Override // java.util.concurrent.FutureTask
|
|
protected void done() {
|
|
try {
|
|
AsyncTask.this.f(get());
|
|
} catch (InterruptedException e) {
|
|
Log.w("AsyncTask", e);
|
|
} catch (CancellationException unused) {
|
|
AsyncTask.this.f(null);
|
|
} catch (ExecutionException e2) {
|
|
throw new RuntimeException("An error occured while executing doInBackground()", e2.getCause());
|
|
}
|
|
}
|
|
};
|
|
|
|
/* renamed from: io.fabric.sdk.android.services.concurrency.AsyncTask$4, reason: invalid class name */
|
|
static /* synthetic */ class AnonymousClass4 {
|
|
static final /* synthetic */ int[] a = new int[Status.values().length];
|
|
|
|
static {
|
|
try {
|
|
a[Status.RUNNING.ordinal()] = 1;
|
|
} catch (NoSuchFieldError unused) {
|
|
}
|
|
try {
|
|
a[Status.FINISHED.ordinal()] = 2;
|
|
} catch (NoSuchFieldError unused2) {
|
|
}
|
|
}
|
|
}
|
|
|
|
private static class AsyncTaskResult<Data> {
|
|
final AsyncTask a;
|
|
final Data[] b;
|
|
|
|
AsyncTaskResult(AsyncTask asyncTask, Data... dataArr) {
|
|
this.a = asyncTask;
|
|
this.b = dataArr;
|
|
}
|
|
}
|
|
|
|
private static class InternalHandler extends Handler {
|
|
public InternalHandler() {
|
|
super(Looper.getMainLooper());
|
|
}
|
|
|
|
@Override // android.os.Handler
|
|
public void handleMessage(Message message) {
|
|
AsyncTaskResult asyncTaskResult = (AsyncTaskResult) message.obj;
|
|
int i = message.what;
|
|
if (i == 1) {
|
|
asyncTaskResult.a.d(asyncTaskResult.b[0]);
|
|
} else {
|
|
if (i != 2) {
|
|
return;
|
|
}
|
|
asyncTaskResult.a.b((Object[]) asyncTaskResult.b);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static class SerialExecutor implements Executor {
|
|
final LinkedList<Runnable> a;
|
|
Runnable b;
|
|
|
|
private SerialExecutor() {
|
|
this.a = new LinkedList<>();
|
|
}
|
|
|
|
protected synchronized void a() {
|
|
Runnable poll = this.a.poll();
|
|
this.b = poll;
|
|
if (poll != null) {
|
|
AsyncTask.k.execute(this.b);
|
|
}
|
|
}
|
|
|
|
@Override // java.util.concurrent.Executor
|
|
public synchronized void execute(final Runnable runnable) {
|
|
this.a.offer(new Runnable() { // from class: io.fabric.sdk.android.services.concurrency.AsyncTask.SerialExecutor.1
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
try {
|
|
runnable.run();
|
|
} finally {
|
|
SerialExecutor.this.a();
|
|
}
|
|
}
|
|
});
|
|
if (this.b == null) {
|
|
a();
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum Status {
|
|
PENDING,
|
|
RUNNING,
|
|
FINISHED
|
|
}
|
|
|
|
private static abstract class WorkerRunnable<Params, Result> implements Callable<Result> {
|
|
Params[] a;
|
|
|
|
private WorkerRunnable() {
|
|
}
|
|
}
|
|
|
|
static {
|
|
int i2 = f;
|
|
g = i2 + 1;
|
|
h = (i2 * 2) + 1;
|
|
i = new ThreadFactory() { // from class: io.fabric.sdk.android.services.concurrency.AsyncTask.1
|
|
private final AtomicInteger a = new AtomicInteger(1);
|
|
|
|
@Override // java.util.concurrent.ThreadFactory
|
|
public Thread newThread(Runnable runnable) {
|
|
return new Thread(runnable, "AsyncTask #" + this.a.getAndIncrement());
|
|
}
|
|
};
|
|
j = new LinkedBlockingQueue(PeripheralType.SERVO);
|
|
k = new ThreadPoolExecutor(g, h, 1L, TimeUnit.SECONDS, j, i);
|
|
l = new SerialExecutor();
|
|
m = new InternalHandler();
|
|
}
|
|
|
|
private Result e(Result result) {
|
|
m.obtainMessage(1, new AsyncTaskResult(this, result)).sendToTarget();
|
|
return result;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void f(Result result) {
|
|
if (this.e.get()) {
|
|
return;
|
|
}
|
|
e(result);
|
|
}
|
|
|
|
protected abstract Result a(Params... paramsArr);
|
|
|
|
protected abstract void b(Result result);
|
|
|
|
protected void b(Progress... progressArr) {
|
|
}
|
|
|
|
protected abstract void c(Result result);
|
|
|
|
public final Status d() {
|
|
return this.c;
|
|
}
|
|
|
|
protected void f() {
|
|
}
|
|
|
|
static /* synthetic */ Object a(AsyncTask asyncTask, Object obj) {
|
|
asyncTask.e(obj);
|
|
return obj;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void d(Result result) {
|
|
if (e()) {
|
|
b((AsyncTask<Params, Progress, Result>) result);
|
|
} else {
|
|
c(result);
|
|
}
|
|
this.c = Status.FINISHED;
|
|
}
|
|
|
|
public final boolean b(boolean z) {
|
|
this.d.set(true);
|
|
return this.b.cancel(z);
|
|
}
|
|
|
|
public final AsyncTask<Params, Progress, Result> a(Executor executor, Params... paramsArr) {
|
|
if (this.c != Status.PENDING) {
|
|
int i2 = AnonymousClass4.a[this.c.ordinal()];
|
|
if (i2 == 1) {
|
|
throw new IllegalStateException("Cannot execute task: the task is already running.");
|
|
}
|
|
if (i2 == 2) {
|
|
throw new IllegalStateException("Cannot execute task: the task has already been executed (a task can be executed only once)");
|
|
}
|
|
}
|
|
this.c = Status.RUNNING;
|
|
f();
|
|
this.a.a = paramsArr;
|
|
executor.execute(this.b);
|
|
return this;
|
|
}
|
|
|
|
public final boolean e() {
|
|
return this.d.get();
|
|
}
|
|
}
|