diff options
Diffstat (limited to 'main/src/cgeo/geocaching/utils')
| -rw-r--r-- | main/src/cgeo/geocaching/utils/GeoDirHandler.java | 107 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/IObserver.java | 22 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/ISubject.java | 57 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/MemorySubject.java | 45 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/Subject.java | 76 |
5 files changed, 52 insertions, 255 deletions
diff --git a/main/src/cgeo/geocaching/utils/GeoDirHandler.java b/main/src/cgeo/geocaching/utils/GeoDirHandler.java index c85648b..b9c605b 100644 --- a/main/src/cgeo/geocaching/utils/GeoDirHandler.java +++ b/main/src/cgeo/geocaching/utils/GeoDirHandler.java @@ -4,12 +4,15 @@ import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.IGeoData; import cgeo.geocaching.settings.Settings; -import android.os.Handler; -import android.os.Message; +import rx.Observable; +import rx.Subscription; +import rx.android.concurrency.AndroidSchedulers; +import rx.util.functions.Action1; + +import java.util.concurrent.TimeUnit; /** - * GeoData and Direction handler. Manipulating geodata and direction information - * through a GeoDirHandler ensures that all listeners are registered from a {@link android.os.Looper} thread. + * GeoData and Direction handler. * <p> * To use this class, override at least one of {@link #updateDirection(float)} or {@link #updateGeoData(IGeoData)}. You * need to start the handler using one of @@ -21,47 +24,11 @@ import android.os.Message; * A good place might be the {@code onResume} method of the Activity. Stop the Handler accordingly in {@code onPause}. * </p> */ -public abstract class GeoDirHandler extends Handler implements IObserver<Object> { - - private static final int OBSERVABLE = 1 << 1; - private static final int START_GEO = 1 << 2; - private static final int START_DIR = 1 << 3; - private static final int STOP_GEO = 1 << 4; - private static final int STOP_DIR = 1 << 5; - +public abstract class GeoDirHandler { private static final CgeoApplication app = CgeoApplication.getInstance(); - @Override - final public void handleMessage(final Message message) { - if ((message.what & START_GEO) != 0) { - app.addGeoObserver(this); - } - - if ((message.what & START_DIR) != 0) { - app.addDirectionObserver(this); - } - - if ((message.what & STOP_GEO) != 0) { - app.deleteGeoObserver(this); - } - - if ((message.what & STOP_DIR) != 0) { - app.deleteDirectionObserver(this); - } - - if ((message.what & OBSERVABLE) != 0) { - if (message.obj instanceof IGeoData) { - updateGeoData((IGeoData) message.obj); - } else { - updateDirection((Float) message.obj); - } - } - } - - @Override - final public void update(final Object o) { - obtainMessage(OBSERVABLE, o).sendToTarget(); - } + private Subscription dirSubscription = null; + private Subscription geoSubscription = null; /** * Update method called when new IGeoData is available. @@ -69,7 +36,7 @@ public abstract class GeoDirHandler extends Handler implements IObserver<Object> * @param data * the new data */ - protected void updateGeoData(final IGeoData data) { + public void updateGeoData(final IGeoData data) { // Override this in children } @@ -79,24 +46,38 @@ public abstract class GeoDirHandler extends Handler implements IObserver<Object> * @param direction * the new direction */ - protected void updateDirection(final float direction) { + public void updateDirection(final float direction) { // Override this in children } /** * Register the current GeoDirHandler for GeoData information. */ - public void startGeo() { - sendEmptyMessage(START_GEO); + public synchronized void startGeo() { + geoSubscription = app.currentGeoObject() + .subscribeOn(AndroidSchedulers.mainThread()) + .subscribe(new Action1<IGeoData>() { + @Override + public void call(final IGeoData geoData) { + updateGeoData(geoData); + } + }); } /** * Register the current GeoDirHandler for direction information if the preferences * allow it. */ - public void startDir() { + public synchronized void startDir() { if (Settings.isUseCompass()) { - sendEmptyMessage(START_DIR); + dirSubscription = app.currentDirObject() + .subscribeOn(AndroidSchedulers.mainThread()) + .subscribe(new Action1<Float>() { + @Override + public void call(final Float direction) { + updateDirection(direction); + } + }); } } @@ -105,27 +86,43 @@ public abstract class GeoDirHandler extends Handler implements IObserver<Object> * preferences allow it). */ public void startGeoAndDir() { - sendEmptyMessage(START_GEO | (Settings.isUseCompass() ? START_DIR : 0)); + startGeo(); + startDir(); } /** * Unregister the current GeoDirHandler for GeoData information. */ - public void stopGeo() { - sendEmptyMessage(STOP_GEO); + public synchronized void stopGeo() { + // Delay the unsubscription by 2.5 seconds, so that another activity has + // the time to subscribe and the GPS receiver will not be turned down. + if (geoSubscription != null) { + final Subscription subscription = geoSubscription; + geoSubscription = null; + Observable.interval(2500, TimeUnit.MILLISECONDS).take(1).subscribe(new Action1<Long>() { + @Override + public void call(final Long aLong) { + subscription.unsubscribe(); + } + }); + } } /** * Unregister the current GeoDirHandler for direction information. */ - public void stopDir() { - sendEmptyMessage(STOP_DIR); + public synchronized void stopDir() { + if (dirSubscription != null) { + dirSubscription.unsubscribe(); + dirSubscription = null; + } } /** * Unregister the current GeoDirHandler for GeoData and direction information. */ public void stopGeoAndDir() { - sendEmptyMessage(STOP_GEO | STOP_DIR); + stopGeo(); + stopDir(); } } diff --git a/main/src/cgeo/geocaching/utils/IObserver.java b/main/src/cgeo/geocaching/utils/IObserver.java deleted file mode 100644 index bfcc798..0000000 --- a/main/src/cgeo/geocaching/utils/IObserver.java +++ /dev/null @@ -1,22 +0,0 @@ -package cgeo.geocaching.utils; - -/** - * Observer interface. - * <p/> - * An observer will receive updates about the observed object (implementing the {@link ISubject} interface) through its - * {@link #update(Object)} method. - * - * @param <T> - * the kind of data to observe - */ -public interface IObserver<T> { - - /** - * Called when an observed object has updated its data. - * - * @param data - * the updated data - */ - void update(final T data); - -} diff --git a/main/src/cgeo/geocaching/utils/ISubject.java b/main/src/cgeo/geocaching/utils/ISubject.java deleted file mode 100644 index c325db0..0000000 --- a/main/src/cgeo/geocaching/utils/ISubject.java +++ /dev/null @@ -1,57 +0,0 @@ -package cgeo.geocaching.utils; - -/** - * Interface for subjects objects. Those can be observed by objects implementing the {@link IObserver} interface. - * - * @param <T> - * the kind of data to observe - */ - -public interface ISubject<T> { - - /** - * Add an observer to the observers list. - * <p/> - * Observers will be notified with no particular order. - * - * @param observer - * the observer to add - * @return true if the observer has been added, false if it was present already - */ - public boolean addObserver(final IObserver<? super T> observer); - - /** - * Delete an observer from the observers list. - * - * @param observer - * the observer to remove - * @return true if the observer has been removed, false if it was not in the list of observers - */ - public boolean deleteObserver(final IObserver<? super T> observer); - - /** - * Number of observers currently observing the object. - * - * @return the number of observers - */ - public int sizeObservers(); - - /** - * Notify all the observers that new data is available. - * <p/> - * The {@link IObserver#update(Object)} method of each observer will be called with no particular order. - * - * @param data - * the updated data - * @return true if at least one observer was notified, false if there were no observers - */ - public boolean notifyObservers(final T data); - - /** - * Clear the observers list. - * - * @return true if there were observers before calling this method, false if the observers list was empty - */ - public boolean clearObservers(); - -} diff --git a/main/src/cgeo/geocaching/utils/MemorySubject.java b/main/src/cgeo/geocaching/utils/MemorySubject.java deleted file mode 100644 index c424528..0000000 --- a/main/src/cgeo/geocaching/utils/MemorySubject.java +++ /dev/null @@ -1,45 +0,0 @@ -package cgeo.geocaching.utils; - -/** - * Synchronized implementation of the {@link ISubject} interface with an added pull interface. - * - * @param <T> - * the kind of data to observe - */ -public class MemorySubject<T> extends Subject<T> { - - /** - * The latest version of the observed data. - * <p/> - * A child class implementation may want to set this field from its constructors, in case early observers request - * the data before it got a chance to get updated. Otherwise, <code>null</code> will be returned until updated - * data is available. - */ - private T memory; - - @Override - public synchronized boolean addObserver(final IObserver<? super T> observer) { - final boolean added = super.addObserver(observer); - if (added && memory != null) { - observer.update(memory); - } - return added; - } - - @Override - public synchronized boolean notifyObservers(final T data) { - memory = data; - return super.notifyObservers(data); - } - - /** - * Get the memorized version of the data. - * - * @return the initial data set by the subject (which may be <code>null</code>), - * or the updated data if it is available - */ - public synchronized T getMemory() { - return memory; - } - -} diff --git a/main/src/cgeo/geocaching/utils/Subject.java b/main/src/cgeo/geocaching/utils/Subject.java deleted file mode 100644 index b1754cc..0000000 --- a/main/src/cgeo/geocaching/utils/Subject.java +++ /dev/null @@ -1,76 +0,0 @@ -package cgeo.geocaching.utils; - -import java.util.LinkedHashSet; -import java.util.Set; - -/** - * Synchronized implementation of the {@link ISubject} interface. - * - * @param <T> - * the kind of data to observe - */ -public class Subject<T> implements ISubject<T> { - - /** - * Collection of observers. - */ - protected final Set<IObserver<? super T>> observers = new LinkedHashSet<IObserver<? super T>>(); - - @Override - public synchronized boolean addObserver(final IObserver<? super T> observer) { - final boolean added = observers.add(observer); - if (added && observers.size() == 1) { - onFirstObserver(); - } - return added; - } - - @Override - public synchronized boolean deleteObserver(final IObserver<? super T> observer) { - final boolean removed = observers.remove(observer); - if (removed && observers.isEmpty()) { - onLastObserver(); - } - return removed; - } - - @Override - public synchronized boolean notifyObservers(final T arg) { - final boolean nonEmpty = !observers.isEmpty(); - for (final IObserver<? super T> observer : observers) { - observer.update(arg); - } - return nonEmpty; - } - - @Override - public synchronized int sizeObservers() { - return observers.size(); - } - - @Override - public synchronized boolean clearObservers() { - final boolean nonEmpty = !observers.isEmpty(); - for (final IObserver<? super T> observer : observers) { - deleteObserver(observer); - } - return nonEmpty; - } - - /** - * Method called when the collection of observers goes from empty to non-empty. - * <p/> - * The default implementation does nothing and may be overwritten by child classes. - */ - protected void onFirstObserver() { - } - - /** - * Method called when the collection of observers goes from non-empty to empty. - * <p/> - * The default implementation does nothing and may be overwritten by child classes. - */ - protected void onLastObserver() { - } - -} |
