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

org.solovyev.android.tasks.Tasks Maven / Gradle / Ivy

There is a newer version: 1.1.18
Show newest version
package org.solovyev.android.tasks;

import android.app.Activity;
import android.content.Context;
import com.google.common.util.concurrent.FutureCallback;
import org.solovyev.android.Threads;
import org.solovyev.tasks.NamedTask;
import org.solovyev.tasks.Task;

import javax.annotation.Nonnull;

/**
 * User: serso
 * Date: 4/8/13
 * Time: 9:50 PM
 */

/**
 * Android tasks
 */
public final class Tasks extends org.solovyev.tasks.Tasks {

	private Tasks() {
		super();
	}

	/**
	 * The only difference from {@link Tasks#toFutureCallback(A, ContextCallback )} is that all {@link ContextCallback}
	 * method calls will be done on UI thread (main application thread)
	 *
	 * @see Tasks#toFutureCallback(A, ContextCallback )
	 */
	@Nonnull
	public static  FutureCallback toUiThreadFutureCallback(@Nonnull A activity, @Nonnull ContextCallback callback) {
		return FutureCallbackAdapter.newUiThreadAdapter(activity, callback);
	}

	/**
	 * Method convert specified context callback to {@link com.google.common.util.concurrent.FutureCallback}.
	 *
	 * @param context  context to be used in {@link ContextCallback} methods
	 * @param callback context callback
	 * @param       type of context
	 * @param       type of result
	 * @return {@link com.google.common.util.concurrent.FutureCallback} wrapper for specified callback
	 */
	@Nonnull
	public static  FutureCallback toFutureCallback(@Nonnull C context, @Nonnull ContextCallback callback) {
		return FutureCallbackAdapter.newAdapter(context, callback);
	}

	/**
	 * Method converts specified context task to {@link Task}
	 *
	 * @param context context to be used in {@link ContextCallback} methods
	 * @param task    context task
	 * @param      type of context
	 * @param      type of result
	 * @return {@link Task} wrapper for specified task
	 */
	@Nonnull
	public static  Task toTask(@Nonnull C context, @Nonnull ContextTask task) {
		return TaskAdapter.newAdapter(context, task);
	}

	@Nonnull
	public static  NamedTask toTask(@Nonnull C context, @Nonnull NamedContextTask task) {
		return NamedTaskAdapter.newAdapter(context, task);
	}

	/**
	 * The only difference from {@link Tasks#toTask(C, ContextTask)} is that all {@link ContextCallback}
	 * method calls will be done on UI thread (main application thread)
	 *
	 * @see Tasks#toTask(C, ContextTask)
	 */
	@Nonnull
	public static  Task toUiThreadTask(@Nonnull A activity, @Nonnull ContextTask task) {
		return TaskAdapter.newUiThreadTaskAdapter(activity, task);
	}

	@Nonnull
	public static  NamedTask toUiThreadTask(@Nonnull A activity, @Nonnull NamedContextTask task) {
		return NamedTaskAdapter.newUiThreadAdapter(activity, task);
	}

    /*
	**********************************************************************
    *
    *                           STATIC
    *
    **********************************************************************
    */

	/**
	 * {@link ContextCallback} to {@link com.google.common.util.concurrent.FutureCallback} adapter
	 */
	private static final class FutureCallbackAdapter extends ContextAwareFutureCallback {

		@Nonnull
		private final ContextCallback callback;

		private final boolean onUiThread;

		private FutureCallbackAdapter(@Nonnull C context, @Nonnull ContextCallback callback, boolean onUiThread) {
			super(context);
			this.callback = callback;
			this.onUiThread = onUiThread;
		}

		private static  FutureCallbackAdapter newUiThreadAdapter(@Nonnull A activity, @Nonnull ContextCallback callback) {
			return new FutureCallbackAdapter(activity, callback, true);
		}

		private static  FutureCallbackAdapter newAdapter(@Nonnull C context, @Nonnull ContextCallback callback) {
			return new FutureCallbackAdapter(context, callback, false);
		}

		@Override
		public void onSuccess(final V result) {
			final C context = getContext();
			if (context != null) {
				if (onUiThread) {
					final Activity activity = (Activity) context;
					Threads.tryRunOnUiThread(activity, new Runnable() {
						@Override
						public void run() {
							callback.onSuccess(context, result);
						}
					});
				} else {
					callback.onSuccess(context, result);
				}
			}
		}

		@Override
		public void onFailure(final Throwable e) {
			final C context = getContext();
			if (context != null) {
				if (onUiThread) {
					Threads.tryRunOnUiThread((Activity) context, new Runnable() {
						@Override
						public void run() {
							callback.onFailure(context, e);
						}
					});
				} else {
					callback.onFailure(context, e);
				}
			}
		}
	}

	/**
	 * {@link ContextTask} to {@link Task} adapter
	 */
	private static final class TaskAdapter implements Task {

		@Nonnull
		private final ContextTask task;

		@Nonnull
		private final FutureCallback callback;

		private TaskAdapter(@Nonnull ContextTask task, @Nonnull FutureCallback callback) {
			this.task = task;
			this.callback = callback;
		}

		@Nonnull
		private static  Task newUiThreadTaskAdapter(@Nonnull A activity, @Nonnull ContextTask task) {
			return new TaskAdapter(task, toUiThreadFutureCallback(activity, task));
		}

		@Nonnull
		private static  Task newAdapter(@Nonnull C context, @Nonnull ContextTask task) {
			return new TaskAdapter(task, toFutureCallback(context, task));
		}

		@Override
		public V call() throws Exception {
			return task.call();
		}

		@Override
		public void onSuccess(V result) {
			callback.onSuccess(result);
		}

		@Override
		public void onFailure(Throwable t) {
			callback.onFailure(t);
		}
	}

	private static final class NamedTaskAdapter implements NamedTask {

		@Nonnull
		private final NamedContextTask namedTask;

		@Nonnull
		private final Task task;

		private NamedTaskAdapter(@Nonnull NamedContextTask namedTask, @Nonnull Task task) {
			this.task = task;
			this.namedTask = namedTask;
		}

		@Nonnull
		private static  NamedTaskAdapter newAdapter(@Nonnull C context, @Nonnull NamedContextTask namedTask) {
			return new NamedTaskAdapter(namedTask, TaskAdapter.newAdapter(context, namedTask));
		}

		@Nonnull
		private static  NamedTaskAdapter newUiThreadAdapter(@Nonnull A activity, @Nonnull NamedContextTask namedTask) {
			return new NamedTaskAdapter(namedTask, TaskAdapter.newUiThreadTaskAdapter(activity, namedTask));
		}

		@Nonnull
		@Override
		public String getName() {
			return namedTask.getName();
		}

		@Override
		public V call() throws Exception {
			return namedTask.call();
		}

		@Override
		public void onSuccess(V result) {
			task.onSuccess(result);
		}

		@Override
		public void onFailure(Throwable t) {
			task.onFailure(t);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy