Skip to content

Commit

Permalink
update PriorityAsyncTask
Browse files Browse the repository at this point in the history
  • Loading branch information
wyouflf committed Jun 7, 2014
1 parent 94d7406 commit 8038c63
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 34 deletions.
6 changes: 2 additions & 4 deletions library/src/com/lidroid/xutils/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,9 @@ private <T> HttpHandler<T> sendRequest(HttpRequest request, RequestParams params
Priority priority = null;
if (params != null) {
priority = params.getPriority();
handler.setPriority(priority);
}
if (priority == null) {
priority = Priority.DEFAULT;
}
handler.executeOnExecutor(EXECUTOR, priority, request);
handler.executeOnExecutor(EXECUTOR, request);
return handler;
}

Expand Down
42 changes: 13 additions & 29 deletions library/src/com/lidroid/xutils/task/PriorityAsyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import com.lidroid.xutils.util.LogUtils;

import java.util.concurrent.*;
Expand All @@ -45,6 +44,16 @@ public abstract class PriorityAsyncTask<Params, Progress, Result> implements Tas
private final AtomicBoolean mCancelled = new AtomicBoolean();
private final AtomicBoolean mTaskInvoked = new AtomicBoolean();

private Priority priority;

public Priority getPriority() {
return priority;
}

public void setPriority(Priority priority) {
this.priority = priority;
}

/**
* Creates a new asynchronous task. This constructor must be invoked on the UI thread.
*/
Expand All @@ -53,7 +62,7 @@ public PriorityAsyncTask() {
public Result call() throws Exception {
mTaskInvoked.set(true);

android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
//noinspection unchecked
return postResult(doInBackground(mParams));
}
Expand All @@ -65,7 +74,7 @@ protected void done() {
try {
postResultIfNotInvoked(get());
} catch (InterruptedException e) {
LogUtils.w(e);
LogUtils.d(e.getMessage());
} catch (ExecutionException e) {
throw new RuntimeException("An error occured while executing doInBackground()",
e.getCause());
Expand Down Expand Up @@ -185,6 +194,7 @@ protected void onCancelled() {
* @return <tt>true</tt> if task was cancelled before it completed
* @see #cancel(boolean)
*/
@Override
public final boolean isCancelled() {
return mCancelled.get();
}
Expand Down Expand Up @@ -280,18 +290,6 @@ public final PriorityAsyncTask<Params, Progress, Result> execute(Params... param
return executeOnExecutor(sDefaultExecutor, params);
}

/**
* @param priority
* @param params The parameters of the task.
* @return This instance of AsyncTask.
* @throws IllegalStateException If execute has invoked.
* @see #executeOnExecutor(java.util.concurrent.Executor, Object[])
* @see #execute(Runnable)
*/
public final PriorityAsyncTask<Params, Progress, Result> execute(Priority priority, Params... params) {
return executeOnExecutor(sDefaultExecutor, priority, params);
}

/**
* @param exec The executor to use.
* @param params The parameters of the task.
Expand All @@ -301,20 +299,6 @@ public final PriorityAsyncTask<Params, Progress, Result> execute(Priority priori
*/
public final PriorityAsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Params... params) {
return executeOnExecutor(exec, Priority.DEFAULT, params);
}

/**
* @param exec The executor to use.
* @param priority
* @param params The parameters of the task.
* @return This instance of AsyncTask.
* @throws IllegalStateException If execute has invoked.
* @see #execute(Object[])
*/
public final PriorityAsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Priority priority,
Params... params) {
if (mExecuteInvoked) {
throw new IllegalStateException("Cannot execute task:"
+ " the task is already executed.");
Expand Down
2 changes: 1 addition & 1 deletion library/src/com/lidroid/xutils/task/PriorityObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PriorityObject<E> {
public final E obj;

public PriorityObject(Priority priority, E obj) {
this.priority = priority;
this.priority = priority == null ? Priority.DEFAULT : priority;
this.obj = obj;
}
}

0 comments on commit 8038c63

Please sign in to comment.