All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdeferred2.impl.DefaultAndroidDeferredManager Maven / Gradle / Ivy

Go to download

Use JDeferred in Android while dispatching callbacks in UI thread and/or Background thread.

There is a newer version: 2.0.0
Show newest version
package org.jdeferred2.impl;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Build;
import org.jdeferred2.DeferredFutureTask;
import org.jdeferred2.DeferredManager;
import org.jdeferred2.Promise;
import org.jdeferred2.android.AndroidDeferredObject;
import org.jdeferred2.android.AndroidExecutionScope;
import org.jdeferred2.android.DeferredAsyncTask;
import org.jdeferred2.multiple.MasterProgress;
import org.jdeferred2.multiple.MultipleResults;
import org.jdeferred2.multiple.MultipleResults2;
import org.jdeferred2.multiple.MultipleResults3;
import org.jdeferred2.multiple.MultipleResults4;
import org.jdeferred2.multiple.MultipleResults5;
import org.jdeferred2.multiple.MultipleResultsN;
import org.jdeferred2.multiple.OneReject;

import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;

/**
 * This DeferredManager is designed to execute deferred tasks in the background,
 * but also executes callbacks (e.g., done, fail, progress, and always) in the UI thread.
 * This is important because only UI thread executions can update UI elements!
 * 

* You can use {@link DeferredAsyncTask} to write in the more familiar Android {@link AsyncTask} API * and still being able to take advantage of {@link Promise} chaining. *

* Even more powerful, you can also use {@link Promise}, {@link Runnable}, {@link Callable}, * and any other types supported by {@link DeferredManager}. This implementation will hand off * callbacks to UI thread automatically. * * IMPLEMENTATION NOTE: relies on package protected classes. * * @author Ray Tsang * @author Andres Almiray */ public abstract class DefaultAndroidDeferredManager extends DefaultDeferredManager { private static Void[] EMPTY_PARAMS = new Void[]{}; public DefaultAndroidDeferredManager() { super(); } public DefaultAndroidDeferredManager(ExecutorService executorService) { super(executorService); } public Promise, MasterProgress> when( DeferredAsyncTask taskV1, DeferredAsyncTask taskV2) { assertNotNull(taskV1, TASK_V1); assertNotNull(taskV2, TASK_V2); return new MasterDeferredObject2(when(taskV1), when(taskV2)); } public Promise, MasterProgress> when( DeferredAsyncTask taskV1, DeferredAsyncTask taskV2, DeferredAsyncTask taskV3) { assertNotNull(taskV1, TASK_V1); assertNotNull(taskV2, TASK_V2); assertNotNull(taskV3, TASK_V3); return new MasterDeferredObject3(when(taskV1), when(taskV2), when(taskV3)); } public Promise, MasterProgress> when( DeferredAsyncTask taskV1, DeferredAsyncTask taskV2, DeferredAsyncTask taskV3, DeferredAsyncTask taskV4) { assertNotNull(taskV1, TASK_V1); assertNotNull(taskV2, TASK_V2); assertNotNull(taskV3, TASK_V3); assertNotNull(taskV4, TASK_V4); return new MasterDeferredObject4(when(taskV1), when(taskV2), when(taskV3), when(taskV4)); } public Promise, MasterProgress> when( DeferredAsyncTask taskV1, DeferredAsyncTask taskV2, DeferredAsyncTask taskV3, DeferredAsyncTask taskV4, DeferredAsyncTask taskV5) { assertNotNull(taskV1, TASK_V1); assertNotNull(taskV2, TASK_V2); assertNotNull(taskV3, TASK_V3); assertNotNull(taskV4, TASK_V4); assertNotNull(taskV5, TASK_V5); return new MasterDeferredObject5(when(taskV1), when(taskV2), when(taskV3), when(taskV4), when(taskV5)); } public Promise, MasterProgress> when( DeferredAsyncTask taskV1, DeferredAsyncTask taskV2, DeferredAsyncTask taskV3, DeferredAsyncTask taskV4, DeferredAsyncTask taskV5, DeferredAsyncTask task6, DeferredAsyncTask... tasks) { assertNotNull(taskV1, TASK_V1); assertNotNull(taskV2, TASK_V2); assertNotNull(taskV3, TASK_V3); assertNotNull(taskV4, TASK_V4); assertNotNull(taskV5, TASK_V5); assertNotNull(task6, "task6"); Promise promise1 = when(taskV1); Promise promise2 = when(taskV2); Promise promise3 = when(taskV3); Promise promise4 = when(taskV4); Promise promise5 = when(taskV5); Promise promise6 = when(task6); Promise[] promiseN = new Promise[tasks.length]; for (int i = 0; i < tasks.length; i++) { promiseN[i] = when(tasks[i]); } return new MasterDeferredObjectN(promise1, promise2, promise3, promise4, promise5, promise6, promiseN); } public Promise, MasterProgress> when( AndroidExecutionScope scope, DeferredAsyncTask taskV1, DeferredAsyncTask taskV2) { assertNotNull(taskV1, TASK_V1); assertNotNull(taskV2, TASK_V2); return new MasterDeferredObject2(when(taskV1, scope), when(taskV2, scope)); } public Promise, MasterProgress> when( AndroidExecutionScope scope, DeferredAsyncTask taskV1, DeferredAsyncTask taskV2, DeferredAsyncTask taskV3) { assertNotNull(taskV1, TASK_V1); assertNotNull(taskV2, TASK_V2); assertNotNull(taskV3, TASK_V3); return new MasterDeferredObject3(when(taskV1, scope), when(taskV2, scope), when(taskV3, scope)); } public Promise, MasterProgress> when( AndroidExecutionScope scope, DeferredAsyncTask taskV1, DeferredAsyncTask taskV2, DeferredAsyncTask taskV3, DeferredAsyncTask taskV4) { assertNotNull(taskV1, TASK_V1); assertNotNull(taskV2, TASK_V2); assertNotNull(taskV3, TASK_V3); assertNotNull(taskV4, TASK_V4); return new MasterDeferredObject4(when(taskV1, scope), when(taskV2, scope), when(taskV3, scope), when(taskV4, scope)); } public Promise, MasterProgress> when( AndroidExecutionScope scope, DeferredAsyncTask taskV1, DeferredAsyncTask taskV2, DeferredAsyncTask taskV3, DeferredAsyncTask taskV4, DeferredAsyncTask taskV5) { assertNotNull(taskV1, TASK_V1); assertNotNull(taskV2, TASK_V2); assertNotNull(taskV3, TASK_V3); assertNotNull(taskV4, TASK_V4); assertNotNull(taskV5, TASK_V5); return new MasterDeferredObject5(when(taskV1, scope), when(taskV2, scope), when(taskV3, scope), when(taskV4, scope), when(taskV5, scope)); } public Promise, MasterProgress> when( AndroidExecutionScope scope, DeferredAsyncTask taskV1, DeferredAsyncTask taskV2, DeferredAsyncTask taskV3, DeferredAsyncTask taskV4, DeferredAsyncTask taskV5, DeferredAsyncTask task6, DeferredAsyncTask... tasks) { assertNotNull(taskV1, TASK_V1); assertNotNull(taskV2, TASK_V2); assertNotNull(taskV3, TASK_V3); assertNotNull(taskV4, TASK_V4); assertNotNull(taskV5, TASK_V5); assertNotNull(task6, "task6"); Promise promise1 = when(taskV1, scope); Promise promise2 = when(taskV2, scope); Promise promise3 = when(taskV3, scope); Promise promise4 = when(taskV4, scope); Promise promise5 = when(taskV5, scope); Promise promise6 = when(task6, scope); Promise[] promiseN = new Promise[tasks.length]; for (int i = 0; i < tasks.length; i++) { promiseN[i] = when(tasks[i], scope); } return new MasterDeferredObjectN(promise1, promise2, promise3, promise4, promise5, promise6, promiseN); } /** * Return a {@link Promise} for the {@link DeferredAsyncTask}. * This can also automatically execute the task in background depending on * {@link DeferredAsyncTask#getStartPolicy()} and/or {@link DefaultDeferredManager#isAutoSubmit()}. *

* Prior to Android Honeycomb (API 11), {@link AsyncTask#execute(Object...)} would be * executed in the background concurrently in a thread pool, but starting with Honeycomb, * {@link AsyncTask#execute(Object...)} will execute the background task serially. To achieve * older behavior, developer need to use {@link AsyncTask#executeOnExecutor(java.util.concurrent.Executor, Object...)}. *

* This method will always execute task in background concurrently if the task should be executed/submitted automatically. * Hence, when using this method on Android API prior to Honeycomb, the task will be executed * using {@link AsyncTask#execute(Object...)}. On Android API version starting from Honeycomb, * this method will execute with @see {@link AsyncTask#executeOnExecutor(java.util.concurrent.Executor, Object...)} * using {@link Executor} from {@link #getExecutorService()} * * @param task {@link DeferredAsyncTask} to run in the background * * @return {@link DeferredAsyncTask#promise()} * * @see {@link AsyncTask#execute(Object...)} * @see {@link AsyncTask#executeOnExecutor(java.util.concurrent.Executor, Object...)} */ @SuppressLint("NewApi") public Promise when( DeferredAsyncTask task) { if (task.getStartPolicy() == StartPolicy.AUTO || (task.getStartPolicy() == StartPolicy.DEFAULT && isAutoSubmit())) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { task.executeOnExecutor(getExecutorService(), EMPTY_PARAMS); } else { task.execute(EMPTY_PARAMS); } } return task.promise(); } public Promise when( DeferredAsyncTask task, AndroidExecutionScope scope) { return when(when(task.promise()), scope); } /** * Wrap with {@link AndroidDeferredObject} so that callbacks can be executed in UI thread. */ @Override public Promise when(DeferredFutureTask task) { return new AndroidDeferredObject(super.when(task)).promise(); } /** * If a non-Android friendly promise is passed in, wrap it with {@link AndroidDeferredObject} * so that callbacks can be executed in the UI thread. */ @Override public Promise when(Promise promise) { if (promise instanceof AndroidDeferredObject) { return promise; } return new AndroidDeferredObject(promise).promise(); } /** * If a non-Android friendly promise is passed in, wrap it with {@link AndroidDeferredObject} * so that callbacks can be executed in the corresponding execution scope. * * @param scope Whether to execute in UI thread or Background thread * @param promise A promise * * @return A promise wrapped in @{link AndroidDeferredObject} */ public Promise when(Promise promise, AndroidExecutionScope scope) { if (promise instanceof AndroidDeferredObject) { return promise; } return new AndroidDeferredObject(promise, scope).promise(); } /** * Wraps {@link org.jdeferred2.impl.MasterDeferredObject2} with {@link AndroidDeferredObject} so that callbacks can * be executed in UI thread. */ @Override public Promise, OneReject, MasterProgress> when(Promise promiseV1, Promise promiseV2) { Promise, OneReject, MasterProgress> p = super.when(promiseV1, promiseV2); return new AndroidDeferredObject, OneReject, MasterProgress>(p).promise(); } /** * Wraps {@link org.jdeferred2.impl.MasterDeferredObject3} with {@link AndroidDeferredObject} so that callbacks can * be executed in UI thread. */ @Override public Promise, OneReject, MasterProgress> when(Promise promiseV1, Promise promiseV2, Promise promiseV3) { Promise, OneReject, MasterProgress> p = super.when(promiseV1, promiseV2, promiseV3); return new AndroidDeferredObject, OneReject, MasterProgress>(p).promise(); } /** * Wraps {@link org.jdeferred2.impl.MasterDeferredObject4} with {@link AndroidDeferredObject} so that callbacks can * be executed in UI thread. */ @Override public Promise, OneReject, MasterProgress> when(Promise promiseV1, Promise promiseV2, Promise promiseV3, Promise promiseV4) { Promise, OneReject, MasterProgress> p = super.when(promiseV1, promiseV2, promiseV3, promiseV4); return new AndroidDeferredObject, OneReject, MasterProgress>(p).promise(); } /** * Wraps {@link org.jdeferred2.impl.MasterDeferredObject5} with {@link AndroidDeferredObject} so that callbacks can * be executed in UI thread. */ @Override public Promise, OneReject, MasterProgress> when(Promise promiseV1, Promise promiseV2, Promise promiseV3, Promise promiseV4, Promise promiseV5) { Promise, OneReject, MasterProgress> p = super.when(promiseV1, promiseV2, promiseV3, promiseV4, promiseV5); return new AndroidDeferredObject, OneReject, MasterProgress>(p).promise(); } /** * Wraps {@link org.jdeferred2.impl.MasterDeferredObjectN} with {@link AndroidDeferredObject} so that callbacks can * be executed in UI thread. */ @Override public Promise, OneReject, MasterProgress> when(Promise promiseV1, Promise promiseV2, Promise promiseV3, Promise promiseV4, Promise promiseV5, Promise promise6, Promise... promises) { Promise, OneReject, MasterProgress> p = super.when(promiseV1, promiseV2, promiseV3, promiseV4, promiseV5, promise6, promises); return new AndroidDeferredObject, OneReject, MasterProgress>(p).promise(); } /** * Wraps {@link org.jdeferred2.impl.MasterDeferredObject2} with {@link AndroidDeferredObject} * so that callbacks can be executed in the corresponding execution scope. * * @return A promise wrapped in @{link AndroidDeferredObject} */ public Promise, OneReject, MasterProgress> when(AndroidExecutionScope scope, Promise promiseV1, Promise promiseV2) { Promise, OneReject, MasterProgress> p = super.when(promiseV1, promiseV2); return new AndroidDeferredObject, OneReject, MasterProgress>(p, scope).promise(); } /** * Wraps {@link org.jdeferred2.impl.MasterDeferredObject3} with {@link AndroidDeferredObject} * so that callbacks can be executed in the corresponding execution scope. * * @return A promise wrapped in @{link AndroidDeferredObject} */ public Promise, OneReject, MasterProgress> when(AndroidExecutionScope scope, Promise promiseV1, Promise promiseV2, Promise promiseV3) { Promise, OneReject, MasterProgress> p = super.when(promiseV1, promiseV2, promiseV3); return new AndroidDeferredObject, OneReject, MasterProgress>(p, scope).promise(); } /** * Wraps {@link org.jdeferred2.impl.MasterDeferredObject4} with {@link AndroidDeferredObject} * so that callbacks can be executed in the corresponding execution scope. * * @return A promise wrapped in @{link AndroidDeferredObject} */ public Promise, OneReject, MasterProgress> when(AndroidExecutionScope scope, Promise promiseV1, Promise promiseV2, Promise promiseV3, Promise promiseV4) { Promise, OneReject, MasterProgress> p = super.when(promiseV1, promiseV2, promiseV3, promiseV4); return new AndroidDeferredObject, OneReject, MasterProgress>(p, scope).promise(); } /** * Wraps {@link org.jdeferred2.impl.MasterDeferredObject5} with {@link AndroidDeferredObject} * so that callbacks can be executed in the corresponding execution scope. * * @return A promise wrapped in @{link AndroidDeferredObject} */ public Promise, OneReject, MasterProgress> when(AndroidExecutionScope scope, Promise promiseV1, Promise promiseV2, Promise promiseV3, Promise promiseV4, Promise promiseV5) { Promise, OneReject, MasterProgress> p = super.when(promiseV1, promiseV2, promiseV3, promiseV4, promiseV5); return new AndroidDeferredObject, OneReject, MasterProgress>(p, scope).promise(); } /** * Wraps {@link org.jdeferred2.impl.MasterDeferredObjectN} with {@link AndroidDeferredObject} * so that callbacks can be executed in the corresponding execution scope. * * @return A promise wrapped in @{link AndroidDeferredObject} */ public Promise, OneReject, MasterProgress> when(AndroidExecutionScope scope, Promise promiseV1, Promise promiseV2, Promise promiseV3, Promise promiseV4, Promise promiseV5, Promise promise6, Promise... promises) { Promise, OneReject, MasterProgress> p = super.when(promiseV1, promiseV2, promiseV3, promiseV4, promiseV5, promise6, promises); return new AndroidDeferredObject, OneReject, MasterProgress>(p, scope).promise(); } @Override protected boolean canPromise(Object o) { if (o instanceof DeferredAsyncTask) { return true; } return super.canPromise(o); } @Override protected Promise toPromise(Object o) { if (o instanceof DeferredAsyncTask) { return when((DeferredAsyncTask) o); } return super.toPromise(o); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy