/**
*
*/
package eagle.android.util;
import java.util.ArrayList;
import java.util.List;
import eagle.util.EagleUtil;
import android.os.AsyncTask;
/**
*
*/
public class AsyncActionQueue {
private List<Action> queue = new ArrayList<Action>();
private ActionTask current = null;
/**
* true
*/
private boolean isStarted = false;
public AsyncActionQueue() {
}
/**
*
*
* @param action
*/
public void pushBack(Action action) {
synchronized (queue) {
queue.add(action);
}
}
/**
*
*
* @param action
*/
public void pushFront(Action action) {
synchronized (queue) {
queue.add(0, action);
}
}
public void push(Action action, int location) {
synchronized (queue) {
queue.add(location, action);
}
}
/**
*
*
* @param action
*/
public void cancel(Action action) {
synchronized (queue) {
queue.remove(action);
if (current != null && current.action == action) {
synchronized (current) {
try {
current.cancel(true);
current = null;
} catch (Exception e) {
EagleUtil.log(e);
}
}
}
}
}
/**
*
*/
public void cancelAll() {
synchronized (queue) {
queue.clear();
if (current != null) {
try {
current.cancel(true);
synchronized (current.action) {
current = null;
}
} catch (Exception e) {
EagleUtil.log(e);
}
}
}
isStarted = false;
}
/**
* equals
*
* @param action
*/
public void cancelEquals(Action action) {
synchronized (queue) {
for (Action act : queue) {
if (act.equals(action)) {
queue.remove(act);
}
}
if (current != null && current.action.equals(action)) {
try {
synchronized (current) {
current.cancel(true);
current = null;
}
} catch (Exception e) {
EagleUtil.log(e);
}
}
}
}
private void _startAction() {
if (queue.size() == 0) {
return;
}
synchronized (queue) {
Action action = queue.get(0);
queue.remove(0);
current = (ActionTask) (new ActionTask(action)).execute();
}
}
/**
*
*
* @return
*/
public boolean isStartActions() {
return isStarted;
}
/**
*
*/
public void startActions() {
if (isStarted || queue.size() == 0) {
return;
}
_startAction();
isStarted = true;
}
/**
*
*/
private void onActionExit(Action action) {
if (queue.size() > 0) {
// !
_startAction();
} else {
current = null;
isStarted = false;
}
}
/**
*
*/
public void onPause() {
try {
if (isStarted) {
synchronized (current) {
current.cancel(true);
// !
pushFront(current.action);
current = null;
}
}
} catch (Exception e) {
EagleUtil.log(e);
}
}
/**
*
*/
public void onResume() {
try {
if (isStarted) {
_startAction();
}
} catch (Exception e) {
EagleUtil.log(e);
}
}
/**
*
*/
private class ActionTask extends AsyncTask<Object, Object, Object> {
private Action action = null;
/**
*
* @param action
*/
public ActionTask(Action action) {
this.action = action;
}
@Override
protected void onPreExecute() {
try {
super.onPreExecute();
} catch (Exception e) {
EagleUtil.log(e);
}
}
@Override
protected Object doInBackground(Object... params) {
try {
synchronized (action) {
return action.onBackgroundAction();
}
} catch (Exception e) {
EagleUtil.log(e);
return null;
}
}
/**
*
*/
@Override
protected void onCancelled() {
try {
super.onCancelled();
synchronized (action) {
action.onCancel();
}
} catch (Exception e) {
EagleUtil.log(e);
}
}
@Override
protected void onPostExecute(Object result) {
try {
super.onPostExecute(result);
// !
action.onPost(result);
onActionExit(action);
} catch (Exception e) {
EagleUtil.log(e);
}
}
}
/**
*
*/
public interface Action {
/**
*
*/
public Object onBackgroundAction();
/**
*
*/
public void onCancel();
/**
*
*/
public void onPost(Object obj);
};
}
|