Skip to content

Commit

Permalink
Cleanup stale runnable (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz Paczos authored Feb 23, 2018
1 parent b66d653 commit 4588147
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

import java.util.concurrent.CopyOnWriteArrayList;

import timber.log.Timber;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;

Expand Down Expand Up @@ -67,7 +65,7 @@ public final class LocationLayerPlugin implements LocationEngineListener, Compas
private Location lastLocation;

private boolean isEnabled;
private StaleStateRunnable staleStateRunnable;
private StaleStateManager staleStateManager;
private final CopyOnWriteArrayList<OnLocationStaleListener> onLocationStaleListeners
= new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<OnLocationLayerClickListener> onLocationLayerClickListeners
Expand Down Expand Up @@ -120,7 +118,7 @@ private void initialize() {

compassManager = new CompassManager(mapView.getContext());
compassManager.addCompassListener(this);
staleStateRunnable = new StaleStateRunnable(this, options.staleStateDelay());
staleStateManager = new StaleStateManager(this, options.staleStateDelay());

locationLayer = new LocationLayer(mapView, mapboxMap, options);
locationLayerCamera = new LocationLayerCamera(mapboxMap);
Expand Down Expand Up @@ -221,6 +219,10 @@ public void onStaleStateChange(boolean isStale) {
}
}

public boolean isLocationStale() {
return staleStateManager.isStale();
}

/**
* Apply a new Location Layer style after the {@link LocationLayerPlugin} has been constructed.
*
Expand All @@ -234,9 +236,9 @@ public void applyStyle(@StyleRes int styleRes) {
public void applyStyle(LocationLayerOptions options) {
locationLayer.applyStyle(options);
if (!options.enableStaleState()) {
staleStateRunnable.onStop();
staleStateManager.onStop();
}
staleStateRunnable.setDelayTime(options.staleStateDelay());
staleStateManager.setDelayTime(options.staleStateDelay());
}

/**
Expand Down Expand Up @@ -299,7 +301,7 @@ public void onStart() {
mapboxMap.addOnCameraMoveListener(this);
}
if (options.enableStaleState()) {
staleStateRunnable.onStart();
staleStateManager.onStart();
}
compassManager.onStart();
}
Expand All @@ -311,7 +313,7 @@ public void onStart() {
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
staleStateRunnable.onStop();
staleStateManager.onStop();
compassManager.onStop();
locationLayerAnimator.cancelAllAnimations();
if (locationEngine != null) {
Expand Down Expand Up @@ -455,7 +457,7 @@ private void updateLocation(final Location location) {
return;
}

staleStateRunnable.updateLatestLocationTime();
staleStateManager.updateLatestLocationTime();
if (lastLocation != null) {
locationLayerAnimator.feedNewLocation(lastLocation, location);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.mapbox.mapboxsdk.plugins.locationlayer;

import android.os.Handler;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

/**
* Class controls the location layer stale state when the {@link android.location.Location} hasn't
Expand All @@ -14,24 +10,26 @@
*
* @since 0.4.0
*/
class StaleStateRunnable implements Runnable {
class StaleStateManager {

private final OnLocationStaleListener innerOnLocationStaleListeners;
private final Handler handler;
private boolean isStale;
private long delayTime;

StaleStateRunnable(OnLocationStaleListener innerListener, long delayTime) {
StaleStateManager(OnLocationStaleListener innerListener, long delayTime) {
innerOnLocationStaleListeners = innerListener;
this.delayTime = delayTime;
handler = new Handler();
}

@Override
public void run() {
isStale = true;
innerOnLocationStaleListeners.onStaleStateChange(true);
}
private Runnable staleStateRunnable = new Runnable() {
@Override
public void run() {
isStale = true;
innerOnLocationStaleListeners.onStaleStateChange(true);
}
};

boolean isStale() {
return isStale;
Expand All @@ -42,24 +40,26 @@ void updateLatestLocationTime() {
isStale = false;
innerOnLocationStaleListeners.onStaleStateChange(false);
}

handler.removeCallbacksAndMessages(this);
handler.postDelayed(this, delayTime);
postTheCallback();
}

void setDelayTime(long delayTime) {
this.delayTime = delayTime;
handler.removeCallbacksAndMessages(this);
handler.postDelayed(this, delayTime);
postTheCallback();
}

void onStart() {
if (!isStale) {
handler.postDelayed(this, delayTime);
postTheCallback();
}
}

void onStop() {
handler.removeCallbacksAndMessages(this);
handler.removeCallbacksAndMessages(null);
}

private void postTheCallback() {
handler.removeCallbacksAndMessages(null);
handler.postDelayed(staleStateRunnable, delayTime);
}
}

0 comments on commit 4588147

Please sign in to comment.