From 5ce94dc933836cbed2f713e36f418c6d54628a00 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 31 Aug 2018 12:54:40 +0300 Subject: [PATCH] remove unnecessary methods from callback --- .../net/osmand/aidl/IOsmAndAidlCallback.aidl | 4 -- .../net/osmand/aidl/IOsmAndAidlInterface.aidl | 4 +- .../telegram/helpers/OsmandAidlHelper.kt | 12 ++---- .../net/osmand/aidl/IOsmAndAidlCallback.aidl | 4 -- .../net/osmand/aidl/IOsmAndAidlInterface.aidl | 4 +- .../net/osmand/aidl/OsmandAidlService.java | 39 +++++++++++-------- 6 files changed, 30 insertions(+), 37 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlCallback.aidl b/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlCallback.aidl index 306fed480d..be010bb062 100644 --- a/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlCallback.aidl +++ b/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlCallback.aidl @@ -6,8 +6,4 @@ interface IOsmAndAidlCallback { void onSearchComplete(in List resultSet); void onUpdate(); - - long getId(); - - void setId(long id); } \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl b/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl index 3982e83a34..d7ebb4492d 100644 --- a/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl +++ b/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl @@ -131,6 +131,6 @@ interface IOsmAndAidlInterface { boolean search(in SearchParams params, IOsmAndAidlCallback callback); boolean navigateSearch(in NavigateSearchParams params); - boolean registerForUpdates(in long updateTimeMS, IOsmAndAidlCallback callback); - boolean unregisterFromUpdates(in IOsmAndAidlCallback callback); + long registerForUpdates(in long updateTimeMS, IOsmAndAidlCallback callback); + boolean unregisterFromUpdates(in long callbackId); } \ 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 113b337cb9..6252ae4250 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt @@ -89,13 +89,6 @@ class OsmandAidlHelper(private val app: TelegramApplication) { mUpdatesListener!!.update() } } - - override fun getId() = osmandCallbackId - - - override fun setId(id: Long) { - osmandCallbackId = id - } } fun setSearchCompleteListener(mSearchCompleteListener: SearchCompleteListener) { @@ -1046,7 +1039,8 @@ class OsmandAidlHelper(private val app: TelegramApplication) { fun registerForUpdates(): Boolean { if (mIOsmAndAidlInterface != null) { try { - return mIOsmAndAidlInterface!!.registerForUpdates(UPDATE_TIME_MS, mIOsmAndAidlCallback) + osmandCallbackId = mIOsmAndAidlInterface!!.registerForUpdates(UPDATE_TIME_MS, mIOsmAndAidlCallback) + return osmandCallbackId > 0 } catch (e: RemoteException) { e.printStackTrace() } @@ -1057,7 +1051,7 @@ class OsmandAidlHelper(private val app: TelegramApplication) { fun unregisterFromUpdates(): Boolean { if (mIOsmAndAidlInterface != null) { try { - return mIOsmAndAidlInterface!!.unregisterFromUpdates(mIOsmAndAidlCallback) + return mIOsmAndAidlInterface!!.unregisterFromUpdates(osmandCallbackId) } catch (e: RemoteException) { e.printStackTrace() } diff --git a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlCallback.aidl b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlCallback.aidl index 9cefdc2475..be010bb062 100644 --- a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlCallback.aidl +++ b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlCallback.aidl @@ -6,8 +6,4 @@ interface IOsmAndAidlCallback { void onSearchComplete(in List resultSet); void onUpdate(); - - long getId(); - - void setId(long id); } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl index 3982e83a34..d7ebb4492d 100644 --- a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl +++ b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl @@ -131,6 +131,6 @@ interface IOsmAndAidlInterface { boolean search(in SearchParams params, IOsmAndAidlCallback callback); boolean navigateSearch(in NavigateSearchParams params); - boolean registerForUpdates(in long updateTimeMS, IOsmAndAidlCallback callback); - boolean unregisterFromUpdates(in IOsmAndAidlCallback callback); + long registerForUpdates(in long updateTimeMS, IOsmAndAidlCallback callback); + boolean unregisterFromUpdates(in long callbackId); } \ 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 edfde0c07d..424cab7479 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java @@ -70,6 +70,8 @@ public class OsmandAidlService extends Service { private static final String DATA_KEY_RESULT_SET = "resultSet"; private static final int MIN_UPDATE_TIME_MS = 1000; + + private static final int MIN_UPDATE_TIME_MS_ERROR = -1; private Map callbacks; private Handler mHandler = null; @@ -102,6 +104,13 @@ public class OsmandAidlService extends Service { callbacks = new HashMap<>(); } + @Override + public void onDestroy() { + super.onDestroy(); + callbacks.clear(); + mHandlerThread.quit(); + } + private final IOsmAndAidlInterface.Stub mBinder = new IOsmAndAidlInterface.Stub() { private void handleException(Exception e) { @@ -593,39 +602,37 @@ public class OsmandAidlService extends Service { } @Override - public boolean registerForUpdates(long updateTimeMS, IOsmAndAidlCallback callback) throws RemoteException { - if (callback != null && updateTimeMS >= MIN_UPDATE_TIME_MS) { - callback.setId(updateCallbackId); - callbacks.put(updateCallbackId, callback); + public long registerForUpdates(long updateTimeMS, IOsmAndAidlCallback callback) throws RemoteException { + if (updateTimeMS >= MIN_UPDATE_TIME_MS) { updateCallbackId++; - startRemoteUpdates(updateTimeMS, callback); - return true; + callbacks.put(updateCallbackId, callback); + startRemoteUpdates(updateTimeMS, updateCallbackId, callback); + return updateCallbackId; + } else { + return MIN_UPDATE_TIME_MS_ERROR; } - return false; } @Override - public boolean unregisterFromUpdates(IOsmAndAidlCallback callback) throws RemoteException { - if (callback != null) { - callbacks.remove(callback.getId()); - } - return false; + public boolean unregisterFromUpdates(long callbackId) throws RemoteException { + callbacks.remove(callbackId); + return true; } }; - void startRemoteUpdates(final long updateTimeMS, final IOsmAndAidlCallback callback) { + void startRemoteUpdates(final long updateTimeMS, final long callbackId, final IOsmAndAidlCallback callback) { mHandler.postDelayed(new Runnable() { @Override public void run() { try { - if (callbacks.containsKey(callback.getId())) { + if (callbacks.containsKey(callbackId)) { if (getApi("isUpdateAllowed").isUpdateAllowed()) { callback.onUpdate(); } - startRemoteUpdates(updateTimeMS, callback); + startRemoteUpdates(updateTimeMS, callbackId, callback); } } catch (RemoteException e) { - e.printStackTrace(); + LOG.error("AIDL e.getMessage()", e); } } }, updateTimeMS);