callable, int priority) {
276 | super(callable);
277 | this.priority = priority;
278 | }
279 |
280 | @Override
281 | protected void done() {
282 | try {
283 | postResultIfNotInvoked(get());
284 | } catch (InterruptedException e) {
285 | android.util.Log.w(LOG_TAG, e);
286 | } catch (ExecutionException e) {
287 | throw new RuntimeException("An error occured while executing doInBackground()", e.getCause());
288 | } catch (CancellationException e) {
289 | postResultIfNotInvoked(null);
290 | }
291 | }
292 |
293 | @Override
294 | public int compareTo(PriorityFutureTask another) {
295 | return priority < another.priority ? 1 : (priority == another.priority ? 0 : -1);
296 | }
297 | }
298 |
299 | private void postResultIfNotInvoked(Result result) {
300 | final boolean wasTaskInvoked = mTaskInvoked.get();
301 | if (!wasTaskInvoked) {
302 | postResult(result);
303 | }
304 | }
305 |
306 | private Handler getHandler() {
307 | return mHandler;
308 | }
309 |
310 | private Result postResult(Result result) {
311 | @SuppressWarnings("unchecked")
312 | Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT, new AdvancedAsyncTaskResult<>(this, result));
313 | message.sendToTarget();
314 | return result;
315 | }
316 |
317 | /**
318 | * Returns the current status of this task.
319 | *
320 | * @return The current status.
321 | */
322 | public final AdvancedAsyncTaskStatus getStatus() {
323 | return mStatus;
324 | }
325 |
326 | /**
327 | * Override this method to perform a computation on a background thread. The
328 | * specified parameters are the parameters passed to {@link #execute}
329 | * by the caller of this task.
330 | *
331 | * This method can call {@link #publishProgress} to publish updates
332 | * on the UI thread.
333 | *
334 | * @param params The parameters of the task.
335 | *
336 | * @return A result, defined by the subclass of this task.
337 | *
338 | * @see #onPreExecute()
339 | * @see #onPostExecute
340 | * @see #publishProgress
341 | */
342 | protected abstract Result doInBackground(Params... params);
343 |
344 | /**
345 | * Runs on the UI thread before {@link #doInBackground}.
346 | *
347 | * @see #onPostExecute
348 | * @see #doInBackground
349 | */
350 | protected void onPreExecute() {
351 |
352 | }
353 |
354 | /**
355 | * Runs on the UI thread after {@link #doInBackground}. The
356 | * specified result is the value returned by {@link #doInBackground}.
357 | *
358 | * This method won't be invoked if the task was cancelled.
359 | *
360 | * @param result The result of the operation computed by {@link #doInBackground}.
361 | *
362 | * @see #onPreExecute
363 | * @see #doInBackground
364 | * @see #onCancelled(Object)
365 | */
366 | @SuppressWarnings({"UnusedDeclaration"})
367 | protected void onPostExecute(Result result) {
368 |
369 | }
370 |
371 | /**
372 | * Runs on the UI thread after {@link #publishProgress} is invoked.
373 | * The specified values are the values passed to {@link #publishProgress}.
374 | *
375 | * @param values The values indicating progress.
376 | *
377 | * @see #publishProgress
378 | * @see #doInBackground
379 | */
380 | @SuppressWarnings({"UnusedDeclaration"})
381 | protected void onProgressUpdate(Progress... values) {
382 |
383 | }
384 |
385 | /**
386 | * Runs on the UI thread after {@link #cancel(boolean)} is invoked and
387 | * {@link #doInBackground(Object[])} has finished.
388 | *
389 | * The default implementation simply invokes {@link #onCancelled()} and
390 | * ignores the result. If you write your own implementation, do not call
391 | * super.onCancelled(result)
.
392 | *
393 | * @param result The result, if any, computed in
394 | * {@link #doInBackground(Object[])}, can be null
395 | *
396 | * @see #cancel(boolean)
397 | * @see #isCancelled()
398 | */
399 | @SuppressWarnings({"UnusedParameters"})
400 | protected void onCancelled(Result result) {
401 | onCancelled();
402 | }
403 |
404 | /**
405 | * Applications should preferably override {@link #onCancelled(Object)}.
406 | * This method is invoked by the default implementation of
407 | * {@link #onCancelled(Object)}.
408 | *
409 | * Runs on the UI thread after {@link #cancel(boolean)} is invoked and
410 | * {@link #doInBackground(Object[])} has finished.
411 | *
412 | * @see #onCancelled(Object)
413 | * @see #cancel(boolean)
414 | * @see #isCancelled()
415 | */
416 | protected void onCancelled() {
417 |
418 | }
419 |
420 | /**
421 | * Returns true if this task was cancelled before it completed
422 | * normally. If you are calling {@link #cancel(boolean)} on the task,
423 | * the value returned by this method should be checked periodically from
424 | * {@link #doInBackground(Object[])} to end the task as soon as possible.
425 | *
426 | * @return true if task was cancelled before it completed
427 | *
428 | * @see #cancel(boolean)
429 | */
430 | public final boolean isCancelled() {
431 | return mCancelled.get();
432 | }
433 |
434 | /**
435 | * Attempts to cancel execution of this task. This attempt will
436 | * fail if the task has already completed, already been cancelled,
437 | * or could not be cancelled for some other reason. If successful,
438 | * and this task has not started when cancel is called,
439 | * this task should never run. If the task has already started,
440 | * then the mayInterruptIfRunning parameter determines
441 | * whether the thread executing this task should be interrupted in
442 | * an attempt to stop the task.
443 | *
444 | * Calling this method will result in {@link #onCancelled(Object)} being
445 | * invoked on the UI thread after {@link #doInBackground(Object[])}
446 | * returns. Calling this method guarantees that {@link #onPostExecute(Object)}
447 | * is never invoked. After invoking this method, you should check the
448 | * value returned by {@link #isCancelled()} periodically from
449 | * {@link #doInBackground(Object[])} to finish the task as early as
450 | * possible.
451 | *
452 | * @param mayInterruptIfRunning true if the thread executing this
453 | * task should be interrupted; otherwise, in-progress tasks are allowed
454 | * to complete.
455 | *
456 | * @return false if the task could not be cancelled,
457 | * typically because it has already completed normally;
458 | * true otherwise
459 | *
460 | * @see #isCancelled()
461 | * @see #onCancelled(Object)
462 | */
463 | public final boolean cancel(boolean mayInterruptIfRunning) {
464 | mCancelled.set(true);
465 | return mFuture.cancel(mayInterruptIfRunning);
466 | }
467 |
468 | /**
469 | * Waits if necessary for the computation to complete, and then
470 | * retrieves its result.
471 | *
472 | * @return The computed result.
473 | *
474 | * @throws CancellationException If the computation was cancelled.
475 | * @throws ExecutionException If the computation threw an exception.
476 | * @throws InterruptedException If the current thread was interrupted
477 | * while waiting.
478 | */
479 | public final Result get() throws InterruptedException, ExecutionException {
480 | return mFuture.get();
481 | }
482 |
483 | /**
484 | * Waits if necessary for at most the given time for the computation
485 | * to complete, and then retrieves its result.
486 | *
487 | * @param timeout Time to wait before cancelling the operation.
488 | * @param unit The time unit for the timeout.
489 | *
490 | * @return The computed result.
491 | *
492 | * @throws CancellationException If the computation was cancelled.
493 | * @throws ExecutionException If the computation threw an exception.
494 | * @throws InterruptedException If the current thread was interrupted
495 | * while waiting.
496 | * @throws TimeoutException If the wait timed out.
497 | */
498 | public final Result get(long timeout, TimeUnit unit) throws InterruptedException,
499 | ExecutionException, TimeoutException {
500 | return mFuture.get(timeout, unit);
501 | }
502 |
503 | /**
504 | * Executes the task with the specified parameters. The task returns
505 | * itself (this) so that the caller can keep a reference to it.
506 | *
507 | * Note: this function schedules the task on a queue for a single background
508 | * thread or pool of threads depending on the platform version. When first
509 | * introduced, AsyncTasks were executed serially on a single background thread.
510 | * Starting with {@link Build.VERSION_CODES#DONUT}, this was changed
511 | * to a pool of threads allowing multiple tasks to operate in parallel. Starting
512 | * {@link Build.VERSION_CODES#HONEYCOMB}, tasks are back to being
513 | * executed on a single thread to avoid common application errors caused
514 | * by parallel execution. If you truly want parallel execution, you can use
515 | * the {@link #executeOnExecutor} version of this method
516 | * with {@link #THREAD_POOL_EXECUTOR}; however, see commentary there for warnings
517 | * on its use.
518 | *
519 | *
This method must be invoked on the UI thread.
520 | *
521 | * @param params The parameters of the task.
522 | *
523 | * @return This instance of AsyncTask.
524 | *
525 | * @throws IllegalStateException If {@link #getStatus()} returns either
526 | * {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}.
527 | *
528 | * @see #executeOnExecutor(Executor, Object[])
529 | * @see #execute(Runnable)
530 | */
531 | public final AdvancedAsyncTask execute(Params... params) {
532 | return executeOnExecutor(sDefaultExecutor, params);
533 | }
534 |
535 | /**
536 | * Executes the task with the specified parameters. The task returns
537 | * itself (this) so that the caller can keep a reference to it.
538 | *
539 | * This method is typically used with {@link #THREAD_POOL_EXECUTOR} to
540 | * allow multiple tasks to run in parallel on a pool of threads managed by
541 | * AsyncTask, however you can also use your own {@link Executor} for custom
542 | * behavior.
543 | *
544 | *
Warning: Allowing multiple tasks to run in parallel from
545 | * a thread pool is generally not what one wants, because the order
546 | * of their operation is not defined. For example, if these tasks are used
547 | * to modify any state in common (such as writing a file due to a button click),
548 | * there are no guarantees on the order of the modifications.
549 | * Without careful work it is possible in rare cases for the newer version
550 | * of the data to be over-written by an older one, leading to obscure data
551 | * loss and stability issues. Such changes are best
552 | * executed in serial; to guarantee such work is serialized regardless of
553 | * platform version you can use this function with {@link #SERIAL_EXECUTOR}.
554 | *
555 | *
This method must be invoked on the UI thread.
556 | *
557 | * @param exec The executor to use. {@link #THREAD_POOL_EXECUTOR} is available as a
558 | * convenient process-wide thread pool for tasks that are loosely coupled.
559 | * @param params The parameters of the task.
560 | *
561 | * @return This instance of AsyncTask.
562 | *
563 | * @throws IllegalStateException If {@link #getStatus()} returns either
564 | * {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}.
565 | *
566 | * @see #execute(Object[])
567 | */
568 | public final AdvancedAsyncTask executeOnExecutor(Executor exec, Params... params) {
569 | if (mStatus != AdvancedAsyncTaskStatus.PENDING) {
570 | switch (mStatus) {
571 | case RUNNING:
572 | throw new IllegalStateException("Cannot execute task:"
573 | + " the task is already running.");
574 | case FINISHED:
575 | throw new IllegalStateException("Cannot execute task:"
576 | + " the task has already been executed "
577 | + "(a task can be executed only once)");
578 | }
579 | }
580 |
581 | mStatus = AdvancedAsyncTaskStatus.RUNNING;
582 |
583 | onPreExecute();
584 |
585 | mWorker.mParams = params;
586 |
587 | exec.execute(mFuture);
588 |
589 | return this;
590 | }
591 |
592 | /**
593 | * Convenience version of {@link #execute(Object...)} for use with
594 | * a simple Runnable object. See {@link #execute(Object[])} for more
595 | * information on the order of execution.
596 | *
597 | * @see #execute(Object[])
598 | * @see #executeOnExecutor(Executor, Object[])
599 | */
600 | public static void execute(Runnable runnable) {
601 | sDefaultExecutor.execute(runnable);
602 | }
603 |
604 | /**
605 | * This method can be invoked from {@link #doInBackground} to
606 | * publish updates on the UI thread while the background computation is
607 | * still running. Each call to this method will trigger the execution of
608 | * {@link #onProgressUpdate} on the UI thread.
609 | *
610 | * {@link #onProgressUpdate} will note be called if the task has been
611 | * canceled.
612 | *
613 | * @param values The progress values to update the UI with.
614 | *
615 | * @see #onProgressUpdate
616 | * @see #doInBackground
617 | */
618 | protected final void publishProgress(Progress... values) {
619 | if (isCancelled()) {
620 | return;
621 | }
622 |
623 | getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
624 | new AdvancedAsyncTaskResult<>(this, values)).sendToTarget();
625 | }
626 |
627 | void finish(Result result) {
628 | if (isCancelled()) {
629 | onCancelled(result);
630 | } else {
631 | onPostExecute(result);
632 | }
633 |
634 | mStatus = AdvancedAsyncTaskStatus.FINISHED;
635 | }
636 |
637 | }
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/AdvancedAsyncTaskCancelTimer.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | import android.os.CountDownTimer;
4 |
5 | /**
6 | *
7 | * Timer for cancelling registered AdvancedAsyncTask instance.
8 | *
9 | * Created by mcsong@gmail.com on 2015-09-08.
10 | */
11 | @SuppressWarnings("rawtypes")
12 | public class AdvancedAsyncTaskCancelTimer extends CountDownTimer {
13 | private AdvancedAsyncTask advancedAsyncTask;
14 | private boolean interrupt;
15 |
16 | public AdvancedAsyncTaskCancelTimer(long millisInFuture, long countDownInterval) {
17 | super(millisInFuture, countDownInterval);
18 | }
19 |
20 | public void setAdvancedAsyncTask(AdvancedAsyncTask asyncTask) {
21 | this.advancedAsyncTask = asyncTask;
22 | }
23 |
24 | public void setInterrupt(boolean interrupt) {
25 | this.interrupt = interrupt;
26 | }
27 |
28 | public AdvancedAsyncTask getAdvancedAsyncTask() {
29 | return advancedAsyncTask;
30 | }
31 |
32 | public boolean isInterrupt() {
33 | return interrupt;
34 | }
35 |
36 | @Override
37 | public void onTick(long millisUntilFinished) {
38 | if(advancedAsyncTask == null) {
39 | this.cancel();
40 | return;
41 | }
42 |
43 | if(advancedAsyncTask.isCancelled()) {
44 | this.cancel();
45 | }
46 |
47 | if(advancedAsyncTask.getStatus() == AdvancedAsyncTaskStatus.FINISHED) {
48 | this.cancel();
49 | }
50 | }
51 |
52 | @Override
53 | public void onFinish() {
54 | if(advancedAsyncTask == null || advancedAsyncTask.isCancelled() ) {
55 | return;
56 | }
57 |
58 | if(advancedAsyncTask.getStatus() == AdvancedAsyncTaskStatus.FINISHED) {
59 | return;
60 | }
61 |
62 | advancedAsyncTask.cancel(interrupt);
63 | }
64 |
65 | }
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/AdvancedAsyncTaskCompat.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | /**
4 | *
5 | * Created by mcsong@gmail.com on 2015-11-10.
6 | *
7 | */
8 | public class AdvancedAsyncTaskCompat {
9 |
10 | public static AdvancedAsyncTask executeParallel(
11 | AdvancedAsyncTask task) {
12 | if (task == null) {
13 | throw new IllegalArgumentException("task can not be null");
14 | }
15 |
16 | return executeParallel(task, null);
17 | }
18 |
19 |
20 | public static AdvancedAsyncTask executeParallel(
21 | AdvancedAsyncTask task, Params... params) {
22 | if (task == null) {
23 | throw new IllegalArgumentException("task can not be null");
24 | }
25 |
26 | task.executeOnExecutor(AdvancedAsyncTask.getThreadPoolExecutor(), params);
27 | return task;
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/AdvancedAsyncTaskResult.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | /**
4 | *
5 | * Created by mcsong@gmail.com on 2015-09-08.
6 | *
7 | */
8 | @SuppressWarnings({"RawUseOfParameterizedType"})
9 | class AdvancedAsyncTaskResult {
10 | final AdvancedAsyncTask mTask;
11 | final Data[] mData;
12 |
13 | AdvancedAsyncTaskResult(AdvancedAsyncTask task, Data... data) {
14 | mTask = task;
15 | mData = data;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/AdvancedAsyncTaskStatus.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | /**
4 | * Indicates the current status of the task. Each status will be set only once
5 | * during the lifetime of a task.
6 | */
7 | public enum AdvancedAsyncTaskStatus {
8 | /**
9 | * Indicates that the task has not been executed yet.
10 | */
11 | PENDING,
12 | /**
13 | * Indicates that the task is running.
14 | */
15 | RUNNING,
16 | /**
17 | * Indicates that {@link AdvancedAsyncTask#onPostExecute} has finished.
18 | */
19 | FINISHED,
20 | }
21 |
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/AdvancedSerialExecutor.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | import java.util.ArrayDeque;
4 | import java.util.concurrent.Executor;
5 |
6 | /**
7 | *
8 | *
9 | * Created by mcsong@gmail.com on 2015-09-09.
10 | */
11 | class AdvancedSerialExecutor implements Executor {
12 |
13 | final ArrayDeque mTasks = new ArrayDeque();
14 | Runnable mActive;
15 |
16 | public synchronized void execute(final Runnable r) {
17 | mTasks.offer(new Runnable() {
18 | public void run() {
19 | try {
20 | r.run();
21 | } finally {
22 | scheduleNext();
23 | }
24 | }
25 | });
26 |
27 | if (mActive == null) {
28 | scheduleNext();
29 | }
30 | }
31 |
32 | protected synchronized void scheduleNext() {
33 | if((mActive = mTasks.poll()) == null)
34 | return;
35 |
36 | AdvancedAsyncTask.getThreadPoolExecutor().execute(mActive);
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/AdvancedThreadPoolExecutorFactory.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | import java.util.concurrent.*;
4 | import java.util.concurrent.atomic.AtomicInteger;
5 | import java.util.concurrent.locks.ReentrantLock;
6 |
7 | /**
8 | *
9 | * Created by mcsong@gmail.com on 2015-09-09.
10 | */
11 | public class AdvancedThreadPoolExecutorFactory {
12 | private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
13 |
14 | private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
15 | private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
16 | private static final int KEEP_ALIVE_SECONDS = 30;
17 |
18 | private int DEFAULT_QUEUE_SIZE = 64;
19 | private final ReentrantLock lock = new ReentrantLock();
20 |
21 | private static final ThreadFactory sThreadFactory = new ThreadFactory() {
22 | private final AtomicInteger mCount = new AtomicInteger(1);
23 | public Thread newThread(Runnable r) {
24 | return new Thread(r, "AdvacnedAsyncTask #" + mCount.getAndIncrement());
25 | }
26 | };
27 |
28 | private BlockingQueue sTaskQueue;
29 |
30 | /**
31 | * An {@link Executor} that can be used to execute tasks in parallel.
32 | */
33 | private ThreadPoolExecutor THREAD_POOL_EXECUTOR;
34 |
35 | Executor getThreadPoolExecutor() {
36 | if(THREAD_POOL_EXECUTOR != null) {
37 | return THREAD_POOL_EXECUTOR;
38 | }
39 |
40 | return getThreadPoolExecutor(0);
41 | }
42 |
43 | Executor getThreadPoolExecutor(int queueSize) {
44 | return getThreadPoolExecutor(queueSize, null);
45 | }
46 |
47 | Executor getThreadPoolExecutor(int queueSize, RejectedExecutionHandler rejectedExecutionHandler) {
48 | return getThreadPoolExecutor(queueSize, null, rejectedExecutionHandler);
49 | }
50 |
51 | Executor getThreadPoolExecutor(int queueSize, BlockingQueue queue, RejectedExecutionHandler rejectedExecutionHandler) {
52 | if(queueSize == 0) {
53 | if(CPU_COUNT >= 8) {
54 | DEFAULT_QUEUE_SIZE = 256;
55 | } else if(CPU_COUNT >= 4) {
56 | DEFAULT_QUEUE_SIZE = 128;
57 | } else {
58 | DEFAULT_QUEUE_SIZE = 64;
59 | }
60 | } else if(queueSize > 0 && queueSize < 64) {
61 | DEFAULT_QUEUE_SIZE = 64;
62 | } else {
63 | DEFAULT_QUEUE_SIZE = queueSize;
64 | }
65 |
66 | if(queue == null) {
67 | sTaskQueue = new PriorityBlockingQueue<>(DEFAULT_QUEUE_SIZE);
68 | } else {
69 | sTaskQueue = queue;
70 | }
71 |
72 | RejectedExecutionHandler rejectHandler = rejectedExecutionHandler;
73 | if(rejectHandler == null) {
74 | rejectHandler = new ThreadPoolExecutor.DiscardOldestPolicy();
75 | }
76 |
77 | THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS,
78 | TimeUnit.SECONDS, sTaskQueue, sThreadFactory, rejectHandler);
79 |
80 | THREAD_POOL_EXECUTOR.allowCoreThreadTimeOut(true);
81 | return THREAD_POOL_EXECUTOR;
82 | }
83 |
84 | private static AdvancedThreadPoolExecutorFactory instance = new AdvancedThreadPoolExecutorFactory();
85 | public static AdvancedThreadPoolExecutorFactory getInstance() {
86 | if(instance == null) {
87 | instance = new AdvancedThreadPoolExecutorFactory();
88 | }
89 |
90 | return instance;
91 | }
92 |
93 | private AdvancedThreadPoolExecutorFactory() { }
94 |
95 | public int getQueueCount() {
96 | if(sTaskQueue == null) {
97 | return 0;
98 | }
99 |
100 | return sTaskQueue.size();
101 | }
102 |
103 | public boolean setExecutorToAdvancedAsyncTask(int queueSize)
104 | throws InterruptedException {
105 |
106 | return setExecutorToAdvancedAsyncTask(queueSize, null);
107 | }
108 |
109 | public boolean setExecutorToAdvancedAsyncTask(int queueSize, BlockingQueue queue)
110 | throws InterruptedException {
111 |
112 | return setExecutorToAdvancedAsyncTask(queueSize, queue, null);
113 | }
114 |
115 | public boolean setExecutorToAdvancedAsyncTask(int queueSize, BlockingQueue queue,
116 | RejectedExecutionHandler rejectedExecutionHandler) throws InterruptedException {
117 | if (lock.tryLock(500, TimeUnit.MILLISECONDS)) {
118 | try {
119 | return AdvancedAsyncTask.setThreadPoolExecutor(getThreadPoolExecutor(queueSize, queue, rejectedExecutionHandler));
120 | } finally {
121 | lock.unlock();
122 | }
123 | }
124 |
125 | return false;
126 | }
127 |
128 | }
129 |
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/InternalHandler.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | import android.os.Handler;
4 | import android.os.Looper;
5 | import android.os.Message;
6 |
7 | /**
8 | *
9 | * Created by mcsong@gmail.com on 2015-09-08.
10 | */
11 | class InternalHandler extends Handler implements Messages {
12 | public InternalHandler(Looper looper) {
13 | super(looper);
14 | }
15 |
16 | @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
17 | @Override
18 | public void handleMessage(Message msg) {
19 | AdvancedAsyncTaskResult result = (AdvancedAsyncTaskResult) msg.obj;
20 |
21 | switch (msg.what) {
22 | case MESSAGE_POST_RESULT:
23 | result.mTask.finish(result.mData[0]);
24 |
25 | break;
26 | case MESSAGE_POST_PROGRESS:
27 | result.mTask.onProgressUpdate(result.mData);
28 |
29 | break;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/Messages.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | /**
4 | *
5 | * Created by mcsong@gmail.com on 2015-09-09.
6 | */
7 | interface Messages {
8 | static final int MESSAGE_POST_RESULT = 0x1;
9 | static final int MESSAGE_POST_PROGRESS = 0x2;
10 | }
11 |
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/QueuePriority.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | /**
4 | *
5 | * Created by mcsong@gmail.com on 2015-09-09.
6 | */
7 | public enum QueuePriority {
8 | LOWEST(0), LOW(1), MEDIUM(2), HIGH(3), HIGHEST(4);
9 |
10 | private final int code;
11 |
12 | private QueuePriority(int code) {
13 | this.code = code;
14 | }
15 |
16 | public int toInt() {
17 | return code;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/ThreadPriority.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | /**
4 | *
5 | *
6 | * Created by mcsong@gmail.com on 2015-09-09.
7 | */
8 | public enum ThreadPriority {
9 | //Process.THREAD_PRIORITY_LOWEST
10 | //Process.THREAD_PRIORITY_BACKGROUND
11 | //Process.THREAD_PRIORITY_DEFAULT
12 | //Process.THREAD_PRIORITY_MORE_FAVORABLE
13 | LOW(19), MEDIUM(10), HIGH(0), HIGHEST(-1);
14 |
15 | private final int code;
16 |
17 | private ThreadPriority(int code) {
18 | this.code = code;
19 | }
20 |
21 | public int toInt() {
22 | return code;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/library/src/main/java/net/sjava/advancedasynctask/WorkerRunnable.java:
--------------------------------------------------------------------------------
1 | package net.sjava.advancedasynctask;
2 |
3 | import java.util.concurrent.Callable;
4 |
5 | /**
6 | *
7 | * Created by mcsong@gmail.com on 2015-09-08.
8 | */
9 | abstract class WorkerRunnable implements Callable {
10 | Params[] mParams;
11 | }
12 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AdvancedAsyncTask
3 |
4 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':demo', ':library'
2 |
--------------------------------------------------------------------------------