From 50c7e5e63e4d3818ec733feba670f49aad0630c3 Mon Sep 17 00:00:00 2001 From: Chumva Date: Thu, 30 Aug 2018 15:05:30 +0300 Subject: [PATCH] Add setId to callback --- .../net/osmand/aidl/IOsmAndAidlCallback.aidl | 4 +- .../telegram/helpers/OsmandAidlHelper.kt | 11 ++++-- .../net/osmand/aidl/IOsmAndAidlCallback.aidl | 4 +- .../net/osmand/aidl/OsmandAidlService.java | 37 ++++++++----------- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlCallback.aidl b/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlCallback.aidl index c83c19d362..306fed480d 100644 --- a/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlCallback.aidl +++ b/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlCallback.aidl @@ -7,5 +7,7 @@ interface IOsmAndAidlCallback { void onUpdate(); - int getId(); + long getId(); + + void setId(long id); } \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt index 37f0078fcf..6c8ee6f381 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt @@ -51,8 +51,6 @@ class OsmandAidlHelper(private val app: TelegramApplication) { companion object { const val OSMAND_FREE_PACKAGE_NAME = "net.osmand" const val OSMAND_PLUS_PACKAGE_NAME = "net.osmand.plus" - - const val OSMAND_CALLBACK_ID = 17 } private var mIOsmAndAidlInterface: IOsmAndAidlInterface? = null @@ -60,6 +58,8 @@ class OsmandAidlHelper(private val app: TelegramApplication) { private var initialized: Boolean = false private var bound: Boolean = false + private var osmandCallbackId: Long = 0 + var listener: OsmandHelperListener? = null interface OsmandHelperListener { @@ -88,7 +88,12 @@ class OsmandAidlHelper(private val app: TelegramApplication) { } } - override fun getId(): Int = OSMAND_CALLBACK_ID + override fun getId() = osmandCallbackId + + + override fun setId(id: Long) { + osmandCallbackId = id + } } fun setSearchCompleteListener(mSearchCompleteListener: SearchCompleteListener) { diff --git a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlCallback.aidl b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlCallback.aidl index c83c19d362..9cefdc2475 100644 --- a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlCallback.aidl +++ b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlCallback.aidl @@ -7,5 +7,7 @@ interface IOsmAndAidlCallback { void onUpdate(); - int getId(); + long getId(); + + void setId(long id); } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java index c5cac6166d..f5d0e882a5 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java @@ -8,7 +8,6 @@ import android.os.HandlerThread; import android.os.IBinder; import android.os.Looper; import android.os.Message; -import android.os.Parcelable; import android.os.RemoteException; import net.osmand.PlatformUtil; @@ -62,26 +61,26 @@ import org.apache.commons.logging.Log; import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; +import java.util.HashMap; import java.util.List; -import java.util.Set; +import java.util.Map; public class OsmandAidlService extends Service { private static final Log LOG = PlatformUtil.getLog(OsmandAidlService.class); - private static final int UPDATE_TIME_MIL = 5000; + private static final int UPDATE_TIME_MS = 5000; private static final int MSG_RUN_SEARCH = 53; private static final String DATA_KEY_RESULT_SET = "resultSet"; private ArrayList mRemoteCallbacks; - private Set callbacks; + private Map callbacks; private ServiceHandler mHandler = null; HandlerThread mHandlerThread = new HandlerThread("OsmAndAidlServiceThread"); private boolean updatesStarted = false; + private long callbackId = 0; OsmandApplication getApp() { return (OsmandApplication) getApplication(); @@ -107,7 +106,7 @@ public class OsmandAidlService extends Service { super.onCreate(); mRemoteCallbacks = new ArrayList<>(); - callbacks = new HashSet<>(); + callbacks = new HashMap<>(); } private final IOsmAndAidlInterface.Stub mBinder = new IOsmAndAidlInterface.Stub() { @@ -584,7 +583,9 @@ public class OsmandAidlService extends Service { @Override public boolean registerForUpdates(IOsmAndAidlCallback callback) throws RemoteException { if (callback != null) { - callbacks.add(callback); + callback.setId(callbackId); + callbacks.put(callbackId, callback); + callbackId++; if (!updatesStarted) { startRemoteUpdates(); updatesStarted = true; @@ -597,14 +598,7 @@ public class OsmandAidlService extends Service { @Override public boolean unregisterFromUpdates(IOsmAndAidlCallback callback) throws RemoteException { if (callback != null) { - Iterator i = callbacks.iterator(); - while (i.hasNext()) { - IOsmAndAidlCallback aidlCallback = i.next(); - if (aidlCallback.getId() == callback.getId()) { - i.remove(); - } - } - return true; + callbacks.remove(callback.getId()); } return false; } @@ -614,12 +608,13 @@ public class OsmandAidlService extends Service { mHandler.postDelayed((new Runnable() { @Override public void run() { - for (IOsmAndAidlCallback callback : callbacks) { - try { + try { + for (Map.Entry entry : callbacks.entrySet()) { + IOsmAndAidlCallback callback = (IOsmAndAidlCallback) entry.getValue(); callback.onUpdate(); - } catch (RemoteException e) { - e.printStackTrace(); } + } catch (RemoteException e) { + e.printStackTrace(); } if (callbacks.isEmpty()) { updatesStarted = false; @@ -627,7 +622,7 @@ public class OsmandAidlService extends Service { startRemoteUpdates(); } } - }), UPDATE_TIME_MIL); + }), UPDATE_TIME_MS); } /**