<NME> GeoDirHandler.java
<BEF> package cgeo.geocaching.sensors;

import androidx.annotation.NonNull;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.BackpressureStrategy;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import org.apache.commons.lang3.tuple.ImmutablePair;
import rx.functions.Func2;
import rx.subscriptions.CompositeSubscription;

/**
 * GeoData and Direction handler.
 * <p>
 * The direction is always relative to the top of the device (natural direction), and that it must
 * be fixed using {@link cgeo.geocaching.utils.AngleUtils#getDirectionNow(float)}. When the direction is derived from the GPS,
 * it is altered so that the fix can still be applied as if the information came from the compass.
 */
public abstract class GeoDirHandler {

    public static final int UPDATE_GEODATA = 1 << 0;
    public static final int UPDATE_DIRECTION = 1 << 1;
    public static final int UPDATE_GEODIR = 1 << 2;
    public static final int LOW_POWER = 1 << 3;

    /**
     * Update method called when new geodata is available. This method is called on the UI thread.
     * {@link #start(int)} must be called with the {@link #UPDATE_GEODATA} flag set.
     *
     * @param geoData the new geographical data
     */
    public void updateGeoData(final GeoData geoData) {
    }

    /**
     * Update method called when new direction is available. This method is called on the UI thread.
     * {@link #start(int)} must be called with the {@link #UPDATE_DIRECTION} flag set.
     *
     * @param direction the new direction
     */
    public void updateDirection(final float direction) {
    }

    public void updateDirectionData(final DirectionData directionData) {
        updateDirection(directionData.getDirection());
    }


    /**
     * Update method called when new data is available. This method is called on the UI thread.
     * {@link #start(int)} must be called with the {@link #UPDATE_GEODIR} flag set.
     *
     * @param geoData   the new geographical data
     * @param direction the new direction
     *
     *                  If the device goes fast enough, or if the compass use is not enabled in the settings,
     *                  the GPS direction information will be used instead of the compass one.
     */
    public void updateGeoDir(@NonNull final GeoData geoData, final float direction) {
    }

    public void updateGeoDirData(@NonNull final GeoData geoData, final DirectionData directionData) {
        updateGeoDir(geoData, directionData.getDirection());
    }


    private static <T> Flowable<T> throttleIfNeeded(final Observable<T> observable, final long windowDuration, final TimeUnit unit) {
        return (windowDuration > 0 ? observable.throttleFirst(windowDuration, unit) : observable).toFlowable(BackpressureStrategy.LATEST);
    }

    /**
     * Register the current GeoDirHandler for GeoData and direction information (if the preferences allow it).
     *
     * @param flags a combination of UPDATE_GEODATA, UPDATE_DIRECTION, UPDATE_GEODIR, and LOW_POWER
     * @return a disposable which can be used to stop the handler
        return useGPSBearing ? AngleUtils.reverseDirectionNow(geoData.getBearing()) : direction;
    }

    /**
     * Register the current GeoDirHandler for GeoData and direction information (if the
     * preferences allow it).
     */
    public Subscription start(final int flags) {
        final CompositeSubscription subscriptions = new CompositeSubscription();
        final boolean lowPower = (flags & LOW_POWER) != 0;
        if ((flags & UPDATE_GEODATA) != 0) {
            subscriptions.add(app.geoDataObservable(lowPower).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<GeoData>() {
                @Override
                public void call(final GeoData geoData) {
                    updateGeoData(geoData);
            }));
        }
        if ((flags & UPDATE_DIRECTION) != 0) {
            subscriptions.add(fixedDirection().observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Float>() {
                @Override
                public void call(final Float direction) {
                    updateDirection(direction);
            }));
        }
        if ((flags & UPDATE_GEODIR) != 0) {
            subscriptions.add(Observable.combineLatest(app.geoDataObservable(lowPower), app.directionObservable(), new Func2<GeoData, Float, ImmutablePair<GeoData, Float>>() {
                @Override
                public ImmutablePair<GeoData, Float> call(final GeoData geoData, final Float direction) {
                    return ImmutablePair.of(geoData, fixDirection(geoData, direction));
                }
            }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ImmutablePair<GeoData, Float>>() {
                @Override
                public void call(final ImmutablePair<GeoData, Float> geoDir) {
                    updateGeoDir(geoDir.left, geoDir.right);

<MSG> Throttle the rotation display in lists to 4 Hz

There is no need to update the arrow display in list more than 4 times
per second, as this is not an instrument for precise cache hunting.

Part of work on #4491.

<DFF> @@ -14,6 +14,8 @@ import rx.functions.Func1;
 import rx.functions.Func2;
 import rx.subscriptions.CompositeSubscription;
 
+import java.util.concurrent.TimeUnit;
+
 /**
  * GeoData and Direction handler.
  * <p>
@@ -81,15 +83,33 @@ public abstract class GeoDirHandler {
         return useGPSBearing ? AngleUtils.reverseDirectionNow(geoData.getBearing()) : direction;
     }
 
+    private static <T> Observable<T> throttleIfNeeded(final Observable<T> observable, final long windowDuration, final TimeUnit unit) {
+        return windowDuration > 0 ? observable.throttleFirst(windowDuration, unit) : observable;
+    }
+
     /**
-     * Register the current GeoDirHandler for GeoData and direction information (if the
-     * preferences allow it).
+     * Register the current GeoDirHandler for GeoData and direction information (if the preferences allow it).
+     *
+     * @param flags a combination of UPDATE_GEODATA, UPDATE_DIRECTION, UPDATE_GEODIR, and LOW_POWER
+     * @return a subscription which can be used to stop the handler
      */
     public Subscription start(final int flags) {
+        return start(flags, 0, TimeUnit.SECONDS);
+    }
+
+    /**
+     * Register the current GeoDirHandler for GeoData and direction information (if the preferences allow it).
+     *
+     * @param flags a combination of UPDATE_GEODATA, UPDATE_DIRECTION, UPDATE_GEODIR, and LOW_POWER
+     * @param windowDuration if greater than 0, the size of the window duration during which no new value will be presented
+     * @param unit the unit for the windowDuration
+     * @return a subscription which can be used to stop the handler
+     */
+    public Subscription start(final int flags, final long windowDuration, final TimeUnit unit) {
         final CompositeSubscription subscriptions = new CompositeSubscription();
         final boolean lowPower = (flags & LOW_POWER) != 0;
         if ((flags & UPDATE_GEODATA) != 0) {
-            subscriptions.add(app.geoDataObservable(lowPower).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<GeoData>() {
+            subscriptions.add(throttleIfNeeded(app.geoDataObservable(lowPower), windowDuration, unit).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<GeoData>() {
                 @Override
                 public void call(final GeoData geoData) {
                     updateGeoData(geoData);
@@ -97,7 +117,7 @@ public abstract class GeoDirHandler {
             }));
         }
         if ((flags & UPDATE_DIRECTION) != 0) {
-            subscriptions.add(fixedDirection().observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Float>() {
+            subscriptions.add(throttleIfNeeded(fixedDirection(), windowDuration, unit).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Float>() {
                 @Override
                 public void call(final Float direction) {
                     updateDirection(direction);
@@ -105,12 +125,12 @@ public abstract class GeoDirHandler {
             }));
         }
         if ((flags & UPDATE_GEODIR) != 0) {
-            subscriptions.add(Observable.combineLatest(app.geoDataObservable(lowPower), app.directionObservable(), new Func2<GeoData, Float, ImmutablePair<GeoData, Float>>() {
+            subscriptions.add(throttleIfNeeded(Observable.combineLatest(app.geoDataObservable(lowPower), app.directionObservable(), new Func2<GeoData, Float, ImmutablePair<GeoData, Float>>() {
                 @Override
                 public ImmutablePair<GeoData, Float> call(final GeoData geoData, final Float direction) {
                     return ImmutablePair.of(geoData, fixDirection(geoData, direction));
                 }
-            }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ImmutablePair<GeoData, Float>>() {
+            }), windowDuration, unit).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ImmutablePair<GeoData, Float>>() {
                 @Override
                 public void call(final ImmutablePair<GeoData, Float> geoDir) {
                     updateGeoDir(geoDir.left, geoDir.right);

Throttle the rotation display in lists to 4 Hz