Skip to content

Commit

Permalink
Migrate AsyncTask into @IntDef
Browse files Browse the repository at this point in the history
@IntDef/@stringdef annotation are preferred way for declaring
set of String/int values.

1. they need less space in APK than enum, see
https://developer.android.com/topic/performance/reduce-apk-size#remove-enums
2. they give more control over allowed values than "static final" values

Main goal of patches from this series is writing "static final" values, enum and some classes in one common @IntDef/@stringdef form:

1. with @IntDef/@stringdef first, @retention second
   and related @interface third
2. with values inside @interface
3. with NUM_ENTRIES declaring number of entries if necessary
4. with comment about numbering from 0 without gaps when necessary
5. with @retention(RetentionPolicy.SOURCE)
6. without "static final" in the @interface

Change-Id: Ic1bf8f93ac2d697571272870e03d6d96b0534ea6
Reviewed-on: https://chromium-review.googlesource.com/c/1370183
Commit-Queue: Marcin Wiącek <marcin@mwiacek.com>
Commit-Queue: agrieve <agrieve@chromium.org>
Reviewed-by: agrieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#616001}
  • Loading branch information
marcinwiacek authored and Commit Bot committed Dec 12, 2018
1 parent 292508f commit 0727fec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 13 additions & 8 deletions base/android/java/src/org/chromium/base/task/AsyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

import android.os.Binder;
import android.os.Process;
import android.support.annotation.IntDef;
import android.support.annotation.MainThread;
import android.support.annotation.WorkerThread;

import org.chromium.base.ThreadUtils;
import org.chromium.base.TraceEvent;
import org.chromium.base.annotations.DoNotInline;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -47,7 +50,7 @@ public abstract class AsyncTask<Result> {
private final Callable<Result> mWorker;
private final FutureTask<Result> mFuture;

private volatile Status mStatus = Status.PENDING;
private volatile @Status int mStatus = Status.PENDING;

private final AtomicBoolean mCancelled = new AtomicBoolean();
private final AtomicBoolean mTaskInvoked = new AtomicBoolean();
Expand All @@ -63,19 +66,21 @@ public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
* Indicates the current status of the task. Each status will be set only once
* during the lifetime of a task.
*/
public enum Status {
@IntDef({Status.PENDING, Status.RUNNING, Status.FINISHED})
@Retention(RetentionPolicy.SOURCE)
public @interface Status {
/**
* Indicates that the task has not been executed yet.
*/
PENDING,
int PENDING = 0;
/**
* Indicates that the task is running.
*/
RUNNING,
int RUNNING = 1;
/**
* Indicates that {@link AsyncTask#onPostExecute} has finished.
*/
FINISHED,
int FINISHED = 2;
}

@SuppressWarnings("NoAndroidAsyncTaskCheck")
Expand Down Expand Up @@ -127,7 +132,7 @@ private void postResult(Result result) {
*
* @return The current status.
*/
public final Status getStatus() {
public final @Status int getStatus() {
return mStatus;
}

Expand Down Expand Up @@ -283,10 +288,10 @@ public final Result get() throws InterruptedException, ExecutionException {
private void executionPreamble() {
if (mStatus != Status.PENDING) {
switch (mStatus) {
case RUNNING:
case Status.RUNNING:
throw new IllegalStateException("Cannot execute task:"
+ " the task is already running.");
case FINISHED:
case Status.FINISHED:
throw new IllegalStateException("Cannot execute task:"
+ " the task has already been executed "
+ "(a task can be executed only once)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ShadowAsyncTask<Result> {

private final FutureTask<Result> future;
private final Callable<Result> worker;
private AsyncTask.Status status = AsyncTask.Status.PENDING;
private @AsyncTask.Status int status = AsyncTask.Status.PENDING;

public ShadowAsyncTask() {
worker = new Callable<Result>() {
Expand Down Expand Up @@ -119,7 +119,7 @@ public void run() {
}

@Implementation
public AsyncTask.Status getStatus() {
public @AsyncTask.Status int getStatus() {
return status;
}

Expand Down

0 comments on commit 0727fec

Please sign in to comment.