Last active
December 16, 2015 12:29
-
-
Save venator85/5434932 to your computer and use it in GitHub Desktop.
SemaphoreHandler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cx.ath.venator.semaphorehandler; | |
import java.util.Vector; | |
import android.os.Handler; | |
import android.os.Message; | |
/** | |
* Message Handler class that supports buffering up of messages when the | |
* activity is paused i.e. in the background. | |
*/ | |
public abstract class PausableHandler extends Handler { | |
/** | |
* Message Queue Buffer | |
*/ | |
final Vector<Message> messageQueueBuffer = new Vector<Message>(); | |
/** | |
* Flag indicating the pause state | |
*/ | |
private boolean paused; | |
/** | |
* Resume the handler | |
*/ | |
final public void resume() { | |
paused = false; | |
while (messageQueueBuffer.size() > 0) { | |
final Message msg = messageQueueBuffer.elementAt(0); | |
messageQueueBuffer.removeElementAt(0); | |
sendMessage(msg); | |
} | |
} | |
/** | |
* Pause the handler | |
*/ | |
final public void pause() { | |
paused = true; | |
} | |
/** | |
* Notification that the message is about to be stored as the activity is | |
* paused. If not handled the message will be saved and replayed when the | |
* activity resumes. | |
* | |
* @param message | |
* the message which optional can be handled | |
* @return true if the message is to be stored | |
*/ | |
protected boolean storeMessage(Message message) { | |
return true; | |
} | |
/** | |
* Notification message to be processed. This will either be directly from | |
* handleMessage or played back from a saved message when the activity was | |
* paused. | |
* | |
* @param message | |
* the message to be handled | |
*/ | |
protected abstract void processMessage(Message message); | |
/** {@inheritDoc} */ | |
@Override | |
final public void handleMessage(Message msg) { | |
if (paused) { | |
if (storeMessage(msg)) { | |
Message msgCopy = new Message(); | |
msgCopy.copyFrom(msg); | |
messageQueueBuffer.add(msgCopy); | |
} | |
} else { | |
processMessage(msg); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cx.ath.venator.semaphorehandler; | |
import java.util.BitSet; | |
import android.os.Message; | |
public class SemaphoreHandler<E extends Enum<E>> extends PausableHandler { | |
// public static class ExampleSemaphore extends SemaphoreHandler<Prova> { | |
// public static enum Prova { | |
// LOCATION_RECEIVED, SETTINGS_RECEIVED | |
// } | |
// | |
// ExampleSemaphore() { | |
// super(Prova.values()); | |
// } | |
// | |
//// Set condition usage: | |
//// setCondition(Prova.SETTINGS_RECEIVED, true); | |
// | |
//// Post message usage: | |
//// Message.obtain(semaphoreHandler, 0, new Runnable() { | |
//// @Override | |
//// public void run() { | |
//// // TODO Auto-generated method stub | |
//// } | |
//// }).sendToTarget(); | |
// } | |
private int howManyConditions; | |
private BitSet conditions; | |
private Object lock = new Object(); | |
public SemaphoreHandler(E[] values) { | |
this.conditions = new BitSet(values.length); | |
this.howManyConditions = values.length; | |
pause(); | |
} | |
public void setCondition(Enum<E> item, boolean value) { | |
synchronized (lock) { | |
conditions.set(item.ordinal(), value); | |
if (conditions.cardinality() == howManyConditions) { | |
resume(); | |
} else { | |
pause(); | |
} | |
} | |
} | |
@Override | |
protected void processMessage(Message message) { | |
Runnable runnable = (Runnable) message.obj; | |
runnable.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment