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

org.ioc.commons.impl.android.flowcontrol.taskcontroller.AndroidAlarmManagerTaskController Maven / Gradle / Ivy

package org.ioc.commons.impl.android.flowcontrol.taskcontroller;

import java.util.Timer;
import java.util.TimerTask;

import org.ioc.commons.flowcontrol.taskcontroller.RepeatingTask;
import org.ioc.commons.flowcontrol.taskcontroller.Task;
import org.ioc.commons.flowcontrol.taskcontroller.TaskController;
import org.ioc.commons.flowcontrol.taskcontroller.TaskHandler;

import android.os.AsyncTask;

//TODO: Create AndroidJobSchedulerTaskController for Android 5.0
public class AndroidAlarmManagerTaskController implements TaskController {

	private int pendingTasks = 0;

	private static class NonCancellableTaskWrapper implements TaskHandler {

		@Override
		public boolean cancel() {
			return false;
		}

	}

	private class TimerTaskWrapper implements TaskHandler {
		private Task task;
		private TimerTask timer = new TimerTask() {

			@Override
			public void run() {
				pendingTasks--;
				TimerTaskWrapper.this.task.run();
			}
		};

		public TimerTaskWrapper(Task task) {
			this.task = task;
		}

		@Override
		public boolean cancel() {
			this.timer.cancel();
			return true;
		}
	}

	private class TimerRepeatingTaskWrapper implements TaskHandler {
		private RepeatingTask task;
		private TimerTask timer = new TimerTask() {

			@Override
			public void run() {
				pendingTasks--;
				boolean again = TimerRepeatingTaskWrapper.this.task.run();
				if (!again) {
					this.cancel();
				} else {
					pendingTasks++;
				}
			}
		};

		public TimerRepeatingTaskWrapper(RepeatingTask task) {
			this.task = task;
		}

		@Override
		public boolean cancel() {
			this.timer.cancel();
			return true;
		}
	}

	private class AsyncTaskWrapper implements TaskHandler {
		private AsyncTask asyncTask;

		public AsyncTaskWrapper(AsyncTask asyncTask) {
			this.asyncTask = asyncTask;
		}

		@Override
		public boolean cancel() {
			return this.asyncTask.cancel(true);
		}
	}

	private static final TaskHandler NON_CANCELLABLE_TASK_WRAPPER = new NonCancellableTaskWrapper();

	public AndroidAlarmManagerTaskController() {
		// Nothing
	}

	@Override
	public TaskHandler scheduleDelayed(int delayMilis, Task task) {
		TimerTaskWrapper timerTaskWrapper = new TimerTaskWrapper(task);
		new Timer().schedule(timerTaskWrapper.timer, delayMilis);

		return timerTaskWrapper;
	}

	@Override
	public TaskHandler scheduleDelayed(int periodMilis, RepeatingTask task) {
		this.pendingTasks++;
		TimerRepeatingTaskWrapper timerTaskWrapper = new TimerRepeatingTaskWrapper(task);
		new Timer().scheduleAtFixedRate(timerTaskWrapper.timer, 0, periodMilis);

		return timerTaskWrapper;
	}

	@Override
	public TaskHandler runAsync(final Task task) {
		this.pendingTasks++;
		AsyncTask asyncTask = new AsyncTask() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				pendingTasks--;
			}

			@Override
			protected Void doInBackground(Void... params) {

				task.run();

				return null;
			}

		};

		return new AsyncTaskWrapper(asyncTask);
	}

	@Override
	public TaskHandler scheduleFinally(final Task task) {
		return scheduleDelayed(1, task);
	}

	@Override
	public TaskHandler scheduleFinally(final RepeatingTask task) {
		return scheduleDelayed(1, task);
	}

	@Override
	public TaskHandler scheduleIdle(final Task task) {
		return scheduleDelayed(1, task);
	}

	@Override
	public TaskHandler scheduleIdle(final RepeatingTask task) {
		return scheduleDelayed(1, task);
	}

	@Override
	public int getPendingTasks() {
		return pendingTasks;
	}

	@Override
	public boolean hasPendingTasks() {
		return (this.getPendingTasks() > 0);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy