diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2011-10-22 15:41:30 +0200 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2011-10-24 23:52:44 +0200 |
commit | c165e77c4975ca58a211f29b09c209f6eeea1a1b (patch) | |
tree | c794fd1c282218b8cfb15d69de4bbdef972586b6 /main/src/cgeo/geocaching/utils | |
parent | 3bec4991410eabe8d8c80c214e30c149bb6bee3d (diff) | |
download | cgeo-c165e77c4975ca58a211f29b09c209f6eeea1a1b.zip cgeo-c165e77c4975ca58a211f29b09c209f6eeea1a1b.tar.gz cgeo-c165e77c4975ca58a211f29b09c209f6eeea1a1b.tar.bz2 |
Use a cancellable handler when loading, storing and refreshing caches
This closes #685.
Diffstat (limited to 'main/src/cgeo/geocaching/utils')
-rw-r--r-- | main/src/cgeo/geocaching/utils/CancellableHandler.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/utils/CancellableHandler.java b/main/src/cgeo/geocaching/utils/CancellableHandler.java new file mode 100644 index 0000000..0b490d8 --- /dev/null +++ b/main/src/cgeo/geocaching/utils/CancellableHandler.java @@ -0,0 +1,112 @@ +package cgeo.geocaching.utils; + +import android.os.Handler; +import android.os.Message; + +/** + * Handler with a cancel policy. Once cancelled, the handler will not handle + * any more cancel or regular message. + */ +public abstract class CancellableHandler extends Handler { + + private boolean cancelled = false; + + private static class CancelHolder { + final Object payload; + + CancelHolder(final Object payload) { + this.payload = payload; + } + } + + @Override + final public void handleMessage(final Message message) { + if (cancelled) { + return; + } + + if (message.obj instanceof CancelHolder) { + cancelled = true; + handleCancel(((CancelHolder) message.obj).payload); + } else { + handleRegularMessage(message); + } + } + + /** + * Handle a non-cancel message.<br> + * Subclasses must implement this to handle messages. + * + * @param message + * the message to handle + */ + abstract protected void handleRegularMessage(final Message message); + + /** + * Handle a cancel message. + * + * @param extra + * the additional payload given by the canceller + */ + protected void handleCancel(final Object extra) { + } + + /** + * Get a cancel message that can later be sent to this handler to cancel it. + * + * @return a cancel message + */ + public Message cancelMessage() { + return cancelMessage(null); + } + + /** + * Get a cancel message with an additional parameter that can later be sent to + * this handler to cancel it. + * + * @param extra + * the extra parameter to give to the cancel handler + * @return a cancel message + */ + public Message cancelMessage(final Object extra) { + return this.obtainMessage(0, new CancelHolder(extra)); + } + + /** + * Cancel the current handler. This can be called from any thread. + */ + public void cancel() { + cancel(null); + } + + /** + * Cancel the current handler. This can be called from any thread. + * + * @param extra + * the extra parameter to give to the cancel handler + */ + public void cancel(final Object extra) { + cancelMessage(extra).sendToTarget(); + } + + /** + * Check if the current handler has been cancelled. + * + * @return true if the handler has been cancelled + */ + public boolean isCancelled() { + return cancelled; + } + + /** + * Check if a handler has been cancelled. + * + * @param handler + * a handler, or null + * @return true if the handler is not null and has been cancelled + */ + public static boolean isCancelled(final CancellableHandler handler) { + return handler != null && handler.isCancelled(); + } + +} |