280 lines
9.8 KiB
Java
280 lines
9.8 KiB
Java
package androidx.loader.content;
|
|
|
|
import android.os.Binder;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.os.Message;
|
|
import android.os.Process;
|
|
import android.util.Log;
|
|
import androidx.annotation.RestrictTo;
|
|
import c.d.b.a.a;
|
|
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.TimeoutException;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
public abstract class ModernAsyncTask<Params, Progress, Result> {
|
|
private static final int CORE_POOL_SIZE = 5;
|
|
private static final int KEEP_ALIVE = 1;
|
|
private static final String LOG_TAG = "AsyncTask";
|
|
private static final int MAXIMUM_POOL_SIZE = 128;
|
|
private static final int MESSAGE_POST_PROGRESS = 2;
|
|
private static final int MESSAGE_POST_RESULT = 1;
|
|
public static final Executor THREAD_POOL_EXECUTOR;
|
|
private static volatile Executor sDefaultExecutor;
|
|
private static InternalHandler sHandler;
|
|
private static final BlockingQueue<Runnable> sPoolWorkQueue;
|
|
private static final ThreadFactory sThreadFactory;
|
|
public final AtomicBoolean mCancelled = new AtomicBoolean();
|
|
private final FutureTask<Result> mFuture;
|
|
private volatile Status mStatus = Status.PENDING;
|
|
public final AtomicBoolean mTaskInvoked = new AtomicBoolean();
|
|
private final WorkerRunnable<Params, Result> mWorker;
|
|
|
|
/* renamed from: androidx.loader.content.ModernAsyncTask$1 reason: invalid class name */
|
|
public static class AnonymousClass1 implements ThreadFactory {
|
|
private final AtomicInteger mCount = new AtomicInteger(1);
|
|
|
|
@Override // java.util.concurrent.ThreadFactory
|
|
public Thread newThread(Runnable runnable) {
|
|
StringBuilder L = a.L("ModernAsyncTask #");
|
|
L.append(this.mCount.getAndIncrement());
|
|
return new Thread(runnable, L.toString());
|
|
}
|
|
}
|
|
|
|
/* renamed from: androidx.loader.content.ModernAsyncTask$2 reason: invalid class name */
|
|
public class AnonymousClass2 extends WorkerRunnable<Params, Result> {
|
|
public AnonymousClass2() {
|
|
}
|
|
|
|
@Override // java.util.concurrent.Callable
|
|
public Result call() throws Exception {
|
|
ModernAsyncTask.this.mTaskInvoked.set(true);
|
|
Result result = null;
|
|
try {
|
|
Process.setThreadPriority(10);
|
|
result = (Result) ModernAsyncTask.this.doInBackground(this.mParams);
|
|
Binder.flushPendingCommands();
|
|
ModernAsyncTask.this.postResult(result);
|
|
return result;
|
|
} catch (Throwable th) {
|
|
ModernAsyncTask.this.postResult(result);
|
|
throw th;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* renamed from: androidx.loader.content.ModernAsyncTask$3 reason: invalid class name */
|
|
public class AnonymousClass3 extends FutureTask<Result> {
|
|
public AnonymousClass3(Callable callable) {
|
|
super(callable);
|
|
}
|
|
|
|
@Override // java.util.concurrent.FutureTask
|
|
public void done() {
|
|
try {
|
|
ModernAsyncTask.this.postResultIfNotInvoked(get());
|
|
} catch (InterruptedException e) {
|
|
Log.w("AsyncTask", e);
|
|
} catch (ExecutionException e2) {
|
|
throw new RuntimeException("An error occurred while executing doInBackground()", e2.getCause());
|
|
} catch (CancellationException unused) {
|
|
ModernAsyncTask.this.postResultIfNotInvoked(null);
|
|
} catch (Throwable th) {
|
|
throw new RuntimeException("An error occurred while executing doInBackground()", th);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* renamed from: androidx.loader.content.ModernAsyncTask$4 reason: invalid class name */
|
|
public static /* synthetic */ class AnonymousClass4 {
|
|
public static final /* synthetic */ int[] $SwitchMap$androidx$loader$content$ModernAsyncTask$Status;
|
|
|
|
static {
|
|
Status.values();
|
|
int[] iArr = new int[3];
|
|
$SwitchMap$androidx$loader$content$ModernAsyncTask$Status = iArr;
|
|
try {
|
|
iArr[Status.RUNNING.ordinal()] = 1;
|
|
} catch (NoSuchFieldError unused) {
|
|
}
|
|
try {
|
|
$SwitchMap$androidx$loader$content$ModernAsyncTask$Status[Status.FINISHED.ordinal()] = 2;
|
|
} catch (NoSuchFieldError unused2) {
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class AsyncTaskResult<Data> {
|
|
public final Data[] mData;
|
|
public final ModernAsyncTask mTask;
|
|
|
|
public AsyncTaskResult(ModernAsyncTask modernAsyncTask, Data... dataArr) {
|
|
this.mTask = modernAsyncTask;
|
|
this.mData = dataArr;
|
|
}
|
|
}
|
|
|
|
public static class InternalHandler extends Handler {
|
|
public InternalHandler() {
|
|
super(Looper.getMainLooper());
|
|
}
|
|
|
|
/* JADX DEBUG: Multi-variable search result rejected for r3v2, resolved type: androidx.loader.content.ModernAsyncTask */
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
@Override // android.os.Handler
|
|
public void handleMessage(Message message) {
|
|
AsyncTaskResult asyncTaskResult = (AsyncTaskResult) message.obj;
|
|
int i = message.what;
|
|
if (i == 1) {
|
|
asyncTaskResult.mTask.finish(asyncTaskResult.mData[0]);
|
|
} else if (i == 2) {
|
|
asyncTaskResult.mTask.onProgressUpdate(asyncTaskResult.mData);
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum Status {
|
|
PENDING,
|
|
RUNNING,
|
|
FINISHED
|
|
}
|
|
|
|
public static abstract class WorkerRunnable<Params, Result> implements Callable<Result> {
|
|
public Params[] mParams;
|
|
}
|
|
|
|
static {
|
|
AnonymousClass1 r7 = new AnonymousClass1();
|
|
sThreadFactory = r7;
|
|
LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(10);
|
|
sPoolWorkQueue = linkedBlockingQueue;
|
|
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 128, 1, TimeUnit.SECONDS, linkedBlockingQueue, r7);
|
|
THREAD_POOL_EXECUTOR = threadPoolExecutor;
|
|
sDefaultExecutor = threadPoolExecutor;
|
|
}
|
|
|
|
public ModernAsyncTask() {
|
|
AnonymousClass2 r0 = new AnonymousClass2();
|
|
this.mWorker = r0;
|
|
this.mFuture = new AnonymousClass3(r0);
|
|
}
|
|
|
|
public static void execute(Runnable runnable) {
|
|
sDefaultExecutor.execute(runnable);
|
|
}
|
|
|
|
private static Handler getHandler() {
|
|
InternalHandler internalHandler;
|
|
synchronized (ModernAsyncTask.class) {
|
|
if (sHandler == null) {
|
|
sHandler = new InternalHandler();
|
|
}
|
|
internalHandler = sHandler;
|
|
}
|
|
return internalHandler;
|
|
}
|
|
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
public static void setDefaultExecutor(Executor executor) {
|
|
sDefaultExecutor = executor;
|
|
}
|
|
|
|
public final boolean cancel(boolean z2) {
|
|
this.mCancelled.set(true);
|
|
return this.mFuture.cancel(z2);
|
|
}
|
|
|
|
public abstract Result doInBackground(Params... paramsArr);
|
|
|
|
public final ModernAsyncTask<Params, Progress, Result> execute(Params... paramsArr) {
|
|
return executeOnExecutor(sDefaultExecutor, paramsArr);
|
|
}
|
|
|
|
public final ModernAsyncTask<Params, Progress, Result> executeOnExecutor(Executor executor, Params... paramsArr) {
|
|
if (this.mStatus != Status.PENDING) {
|
|
int ordinal = this.mStatus.ordinal();
|
|
if (ordinal == 1) {
|
|
throw new IllegalStateException("Cannot execute task: the task is already running.");
|
|
} else if (ordinal != 2) {
|
|
throw new IllegalStateException("We should never reach this state");
|
|
} else {
|
|
throw new IllegalStateException("Cannot execute task: the task has already been executed (a task can be executed only once)");
|
|
}
|
|
} else {
|
|
this.mStatus = Status.RUNNING;
|
|
onPreExecute();
|
|
this.mWorker.mParams = paramsArr;
|
|
executor.execute(this.mFuture);
|
|
return this;
|
|
}
|
|
}
|
|
|
|
public void finish(Result result) {
|
|
if (isCancelled()) {
|
|
onCancelled(result);
|
|
} else {
|
|
onPostExecute(result);
|
|
}
|
|
this.mStatus = Status.FINISHED;
|
|
}
|
|
|
|
public final Result get() throws InterruptedException, ExecutionException {
|
|
return this.mFuture.get();
|
|
}
|
|
|
|
public final Result get(long j, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
|
|
return this.mFuture.get(j, timeUnit);
|
|
}
|
|
|
|
public final Status getStatus() {
|
|
return this.mStatus;
|
|
}
|
|
|
|
public final boolean isCancelled() {
|
|
return this.mCancelled.get();
|
|
}
|
|
|
|
public void onCancelled() {
|
|
}
|
|
|
|
public void onCancelled(Result result) {
|
|
onCancelled();
|
|
}
|
|
|
|
public void onPostExecute(Result result) {
|
|
}
|
|
|
|
public void onPreExecute() {
|
|
}
|
|
|
|
public void onProgressUpdate(Progress... progressArr) {
|
|
}
|
|
|
|
public Result postResult(Result result) {
|
|
getHandler().obtainMessage(1, new AsyncTaskResult(this, result)).sendToTarget();
|
|
return result;
|
|
}
|
|
|
|
public void postResultIfNotInvoked(Result result) {
|
|
if (!this.mTaskInvoked.get()) {
|
|
postResult(result);
|
|
}
|
|
}
|
|
|
|
public final void publishProgress(Progress... progressArr) {
|
|
if (!isCancelled()) {
|
|
getHandler().obtainMessage(2, new AsyncTaskResult(this, progressArr)).sendToTarget();
|
|
}
|
|
}
|
|
}
|