From 4588147e6d9095e378b8406da34520bfc9f0a74f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Fri, 23 Feb 2018 09:00:20 +0100 Subject: [PATCH] Cleanup stale runnable (#304) --- .../locationlayer/LocationLayerPlugin.java | 20 ++++++----- ...teRunnable.java => StaleStateManager.java} | 36 +++++++++---------- 2 files changed, 29 insertions(+), 27 deletions(-) rename plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/{StaleStateRunnable.java => StaleStateManager.java} (64%) diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java index de56b11a1..d917fe8fe 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java @@ -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; @@ -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 onLocationStaleListeners = new CopyOnWriteArrayList<>(); private final CopyOnWriteArrayList onLocationLayerClickListeners @@ -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); @@ -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. * @@ -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()); } /** @@ -299,7 +301,7 @@ public void onStart() { mapboxMap.addOnCameraMoveListener(this); } if (options.enableStaleState()) { - staleStateRunnable.onStart(); + staleStateManager.onStart(); } compassManager.onStart(); } @@ -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) { @@ -455,7 +457,7 @@ private void updateLocation(final Location location) { return; } - staleStateRunnable.updateLatestLocationTime(); + staleStateManager.updateLatestLocationTime(); if (lastLocation != null) { locationLayerAnimator.feedNewLocation(lastLocation, location); } diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/StaleStateRunnable.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/StaleStateManager.java similarity index 64% rename from plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/StaleStateRunnable.java rename to plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/StaleStateManager.java index 0f288a36d..0d7d0af62 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/StaleStateRunnable.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/StaleStateManager.java @@ -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 @@ -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; @@ -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); } }