Created by wyouflf on 15/6/5. 异步任务基类 @param 任务返回值类型
| 14 | * @param <ResultType> 任务返回值类型 |
| 15 | */ |
| 16 | public abstract class AbsTask<ResultType> implements Callback.Cancelable { |
| 17 | |
| 18 | private TaskProxy taskProxy = null; |
| 19 | private final Callback.Cancelable cancelHandler; |
| 20 | |
| 21 | private volatile boolean isCancelled = false; |
| 22 | private volatile State state = State.IDLE; |
| 23 | private ResultType result; |
| 24 | |
| 25 | public AbsTask() { |
| 26 | this(null); |
| 27 | } |
| 28 | |
| 29 | public AbsTask(Callback.Cancelable cancelHandler) { |
| 30 | this.cancelHandler = cancelHandler; |
| 31 | } |
| 32 | |
| 33 | protected abstract ResultType doBackground() throws Throwable; |
| 34 | |
| 35 | protected abstract void onSuccess(ResultType result); |
| 36 | |
| 37 | protected abstract void onError(Throwable ex, boolean isCallbackError); |
| 38 | |
| 39 | protected void onWaiting() { |
| 40 | } |
| 41 | |
| 42 | protected void onStarted() { |
| 43 | } |
| 44 | |
| 45 | protected void onUpdate(int flag, Object... args) { |
| 46 | } |
| 47 | |
| 48 | protected void onCancelled(Callback.CancelledException cex) { |
| 49 | } |
| 50 | |
| 51 | protected void onFinished() { |
| 52 | } |
| 53 | |
| 54 | public Priority getPriority() { |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | public Executor getExecutor() { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | public Looper customLooper() { |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | protected final void update(int flag, Object... args) { |
| 67 | if (taskProxy != null) { |
| 68 | taskProxy.onUpdate(flag, args); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * invoked via cancel() |
nothing calls this directly
no outgoing calls
no test coverage detected