diff options
author | Łukasz Paczos <lukas.paczos@gmail.com> | 2019-02-19 18:49:03 +0100 |
---|---|---|
committer | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2019-02-19 19:15:59 +0100 |
commit | 9750921dacf42cffa3c8b187d8fdc38e6ae1e5b4 (patch) | |
tree | 53c1dc4f8cb708f387acc18f993e3944fc88971b | |
parent | f5a0e96a5f30a87331529aa6c16d800c911c97aa (diff) | |
download | qtlocation-mapboxgl-9750921dacf42cffa3c8b187d8fdc38e6ae1e5b4.tar.gz |
[android] do not post stale state message when new option provided but the component is stopped
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/StaleStateManager.java | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/StaleStateManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/StaleStateManager.java index c7447796e0..74c2e3a731 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/StaleStateManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/StaleStateManager.java @@ -1,8 +1,11 @@ package com.mapbox.mapboxsdk.location; import android.os.Handler; +import android.os.Message; import android.support.annotation.NonNull; +import java.lang.ref.WeakReference; + /** * Class controls the location stale state when the {@link android.location.Location} hasn't * been updated in 'x' amount of time. {@link LocationComponentOptions#staleStateTimeout()} can be used to @@ -14,25 +17,19 @@ class StaleStateManager { private boolean isEnabled; private final OnLocationStaleListener innerOnLocationStaleListeners; @NonNull - private final Handler handler; + private final StaleMessageHandler handler; private boolean isStale = true; private long delayTime; + private final int staleStateMessage = 1; + StaleStateManager(OnLocationStaleListener innerListener, LocationComponentOptions options) { innerOnLocationStaleListeners = innerListener; - handler = new Handler(); + handler = new StaleMessageHandler(this); isEnabled = options.enableStaleState(); delayTime = options.staleStateTimeout(); } - @NonNull - private Runnable staleStateRunnable = new Runnable() { - @Override - public void run() { - setState(true); - } - }; - void setEnabled(boolean enabled) { if (enabled) { setState(isStale); @@ -54,7 +51,9 @@ class StaleStateManager { void setDelayTime(long delayTime) { this.delayTime = delayTime; - postTheCallback(); + if (handler.hasMessages(staleStateMessage)) { + postTheCallback(); + } } void onStart() { @@ -69,7 +68,7 @@ class StaleStateManager { private void postTheCallback() { handler.removeCallbacksAndMessages(null); - handler.postDelayed(staleStateRunnable, delayTime); + handler.sendEmptyMessageDelayed(staleStateMessage, delayTime); } private void setState(boolean stale) { @@ -80,4 +79,21 @@ class StaleStateManager { } } } + + private static class StaleMessageHandler extends Handler { + + private final WeakReference<StaleStateManager> managerWeakReference; + + private StaleMessageHandler(StaleStateManager staleStateManager) { + this.managerWeakReference = new WeakReference<>(staleStateManager); + } + + @Override + public void handleMessage(Message msg) { + StaleStateManager manager = managerWeakReference.get(); + if (manager != null) { + manager.setState(true); + } + } + } } |