[android] do not post stale state message when new option provided but the component is stopped
This commit is contained in:
committed by
Łukasz Paczos
parent
f5a0e96a5f
commit
9750921dac
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user