From cf10d7394b7fed08a4bdba4b77f549354514f18f Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Mon, 8 Apr 2019 12:31:21 +0300 Subject: [PATCH] add option to remove of inner callback from RoutingHelper if no aidl callbacks registered. --- OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 32 +++++++++++-------- .../net/osmand/aidl/OsmandAidlService.java | 1 - 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 2ee46a73eb..34dbc38793 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -26,6 +26,7 @@ import android.view.View; import android.widget.ArrayAdapter; import java.util.HashMap; +import java.util.Map.Entry; import net.osmand.CallbackWithObject; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; @@ -1930,10 +1931,13 @@ public class OsmandAidlApi { if (params.isSubscribeToUpdates()) { updateCallbackId++; callbacks.put(updateCallbackId, callback); - startNavigationalUpdates(updateCallbackId); + startNavigationalUpdates(); return updateCallbackId; } else { callbacks.remove(params.getCallbackId()); + if (callbacks.size() == 0) { + navUpdateListener = null; + } return -1; } } @@ -1944,14 +1948,14 @@ public class OsmandAidlApi { return callbacks.size() > 0; } - private void startNavigationalUpdates(final long updateCallbackId) { + private void startNavigationalUpdates() { final ADirectionInfo directionInfo = new ADirectionInfo(-1, -1, false); final NextDirectionInfo baseNdi = new NextDirectionInfo(); - navUpdateListener = new NavUpdateListener() { - @Override - public void onNavUpdate() { - RoutingHelper rh = app.getRoutingHelper(); - if (callbacks.containsKey(updateCallbackId)) { + if (navUpdateListener == null) { + navUpdateListener = new NavUpdateListener() { + @Override + public void onNavUpdate() { + RoutingHelper rh = app.getRoutingHelper(); if (rh.isDeviatedFromRoute()) { directionInfo.setTurnType(TurnType.OFFR); directionInfo.setDistanceTo((int) rh.getRouteDeviation()); @@ -1962,14 +1966,16 @@ public class OsmandAidlApi { directionInfo.setTurnType(ndi.directionInfo.getTurnType().getValue()); } } - try { - callbacks.get(updateCallbackId).updateNavigationInfo(directionInfo); - } catch (Exception e) { - LOG.debug(e.getMessage(), e); + for (Entry cb : callbacks.entrySet()) { + try { + cb.getValue().updateNavigationInfo(directionInfo); + } catch (Exception e) { + LOG.debug(e.getMessage(), e); + } } } - } - }; + }; + } } public interface NavUpdateListener { diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java index f334494ba5..42f74164b9 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java @@ -11,7 +11,6 @@ import android.os.RemoteException; import android.support.annotation.Nullable; import net.osmand.PlatformUtil; -import net.osmand.aidl.OsmandAidlApi.DirectionsUpdateCallback; import net.osmand.aidl.OsmandAidlApi.GpxBitmapCreatedCallback; import net.osmand.aidl.OsmandAidlApi.OsmandAppInitCallback; import net.osmand.aidl.OsmandAidlApi.SearchCompleteCallback;