From 055c403f14a2a3c676555b99f2d0e355875d26fc Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Fri, 19 Apr 2019 10:24:53 +0300 Subject: [PATCH] add pr fixes, add AidlCallbackListener documentation --- .../net/osmand/aidl/AidlCallbackListener.java | 30 +++++++++++++- OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 6 +-- .../osmand/plus/routing/RoutingHelper.java | 40 ++++++++----------- 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/OsmAnd/src/net/osmand/aidl/AidlCallbackListener.java b/OsmAnd/src/net/osmand/aidl/AidlCallbackListener.java index 95d958fb39..71fe1f1b8f 100644 --- a/OsmAnd/src/net/osmand/aidl/AidlCallbackListener.java +++ b/OsmAnd/src/net/osmand/aidl/AidlCallbackListener.java @@ -2,8 +2,36 @@ package net.osmand.aidl; import java.util.Map; -public interface AidlCallbackListener{ +public interface AidlCallbackListener { + + /** + * Add AidlCallbackListener to OsmandAidlService's map of listeners. Key is unique to each AIDL + * method that wants to register callback and used to access only "own" callbacks. + * + * @param callback + * @param key - every AIDL method which uses that register callbacks in service need to use its own bit key + * 1 - key for registerForUpdates(...) + * 2 - key for registerForNavigationUpdates(...) + * 4 - key for onContextMenuButtonClicked(...) + * 8 - key for... future use + * 16 - key for... future use + * @return long - unique id of callback. Could be used for unregistering callback + */ long addAidlCallback(IOsmAndAidlCallback callback, int key); + + + /** + * Unregister AidlCallbackListener from OsmandAidlService's map + * + * @param id - unique id of callback + * @return - true if callback successfully unregistered + */ boolean removeAidlCallback(long id); + + /** + * + * @return map of all callbacks. AidlCallbackParams contains method key and callback. + */ Map getAidlCallbacks(); + } diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index f51e292677..a8cd1acfcc 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -25,8 +25,6 @@ import android.text.TextUtils; 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; @@ -1961,11 +1959,11 @@ public class OsmandAidlApi { } }; navUpdateCallbacks.put(id, listener); - app.getRoutingHelper().addDataUpdateListener(listener); + app.getRoutingHelper().addRouteDataListener(listener); } public void unregisterFromUpdates(long id) { - app.getRoutingHelper().removeDataUpdateListener(navUpdateCallbacks.get(id)); + app.getRoutingHelper().removeRouteDataListener(navUpdateCallbacks.get(id)); navUpdateCallbacks.remove(id); } diff --git a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java index c3117224f3..86ceb4f8e6 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java +++ b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java @@ -30,7 +30,6 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; import static net.osmand.plus.notifications.OsmandNotification.NotificationType.NAVIGATION; @@ -267,31 +266,29 @@ public class RoutingHelper { transportRoutingHelper.addListener(l); } - public void addDataUpdateListener(IRoutingDataUpdateListener listener) { - List> copyList = new ArrayList<>(updateListeners); - Iterator> it = copyList.iterator(); - while (it.hasNext()) { - WeakReference ref = it.next(); - IRoutingDataUpdateListener l = ref.get(); - if (l == null) { - it.remove(); - } - } - copyList.add(new WeakReference<>(listener)); - updateListeners = copyList; + public void addRouteDataListener(IRoutingDataUpdateListener listener) { + updateListeners = updateListenersList(new ArrayList<>(updateListeners), listener, true); } - public void removeDataUpdateListener(IRoutingDataUpdateListener listener) { - List> copyList = new ArrayList<>(updateListeners); + public void removeRouteDataListener(IRoutingDataUpdateListener listener) { + updateListeners = updateListenersList(new ArrayList<>(updateListeners), listener, false); + } + + private List> updateListenersList( + List> copyList, + IRoutingDataUpdateListener listener, boolean isNewListener) { Iterator> it = copyList.iterator(); while (it.hasNext()) { WeakReference ref = it.next(); IRoutingDataUpdateListener l = ref.get(); - if (l == null || l == listener) { + if (l == null || (l == listener)) { it.remove(); } } - updateListeners = copyList; + if (isNewListener) { + copyList.add(new WeakReference<>(listener)); + } + return copyList; } public boolean removeListener(IRouteInformationListener lt){ @@ -533,16 +530,11 @@ public class RoutingHelper { app.getNotificationHelper().refreshNotification(NotificationType.NAVIGATION); if (!updateListeners.isEmpty()) { ArrayList> tmp = new ArrayList<>(updateListeners); - Iterator> it = tmp.iterator(); - while (it.hasNext()) { - WeakReference ref = it.next(); + for (WeakReference ref : tmp) { IRoutingDataUpdateListener l = ref.get(); - if (l == null) { - it.remove(); - } else { + if (l != null) { l.onRoutingDataUpdate(); } - updateListeners = tmp; } } } else {