remove unnecessary methods from callback

This commit is contained in:
Chumva 2018-08-31 12:54:40 +03:00
parent 8b6027ac9a
commit 5ce94dc933
6 changed files with 30 additions and 37 deletions

View file

@ -6,8 +6,4 @@ interface IOsmAndAidlCallback {
void onSearchComplete(in List<SearchResult> resultSet); void onSearchComplete(in List<SearchResult> resultSet);
void onUpdate(); void onUpdate();
long getId();
void setId(long id);
} }

View file

@ -131,6 +131,6 @@ interface IOsmAndAidlInterface {
boolean search(in SearchParams params, IOsmAndAidlCallback callback); boolean search(in SearchParams params, IOsmAndAidlCallback callback);
boolean navigateSearch(in NavigateSearchParams params); boolean navigateSearch(in NavigateSearchParams params);
boolean registerForUpdates(in long updateTimeMS, IOsmAndAidlCallback callback); long registerForUpdates(in long updateTimeMS, IOsmAndAidlCallback callback);
boolean unregisterFromUpdates(in IOsmAndAidlCallback callback); boolean unregisterFromUpdates(in long callbackId);
} }

View file

@ -89,13 +89,6 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
mUpdatesListener!!.update() mUpdatesListener!!.update()
} }
} }
override fun getId() = osmandCallbackId
override fun setId(id: Long) {
osmandCallbackId = id
}
} }
fun setSearchCompleteListener(mSearchCompleteListener: SearchCompleteListener) { fun setSearchCompleteListener(mSearchCompleteListener: SearchCompleteListener) {
@ -1046,7 +1039,8 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
fun registerForUpdates(): Boolean { fun registerForUpdates(): Boolean {
if (mIOsmAndAidlInterface != null) { if (mIOsmAndAidlInterface != null) {
try { try {
return mIOsmAndAidlInterface!!.registerForUpdates(UPDATE_TIME_MS, mIOsmAndAidlCallback) osmandCallbackId = mIOsmAndAidlInterface!!.registerForUpdates(UPDATE_TIME_MS, mIOsmAndAidlCallback)
return osmandCallbackId > 0
} catch (e: RemoteException) { } catch (e: RemoteException) {
e.printStackTrace() e.printStackTrace()
} }
@ -1057,7 +1051,7 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
fun unregisterFromUpdates(): Boolean { fun unregisterFromUpdates(): Boolean {
if (mIOsmAndAidlInterface != null) { if (mIOsmAndAidlInterface != null) {
try { try {
return mIOsmAndAidlInterface!!.unregisterFromUpdates(mIOsmAndAidlCallback) return mIOsmAndAidlInterface!!.unregisterFromUpdates(osmandCallbackId)
} catch (e: RemoteException) { } catch (e: RemoteException) {
e.printStackTrace() e.printStackTrace()
} }

View file

@ -6,8 +6,4 @@ interface IOsmAndAidlCallback {
void onSearchComplete(in List<SearchResult> resultSet); void onSearchComplete(in List<SearchResult> resultSet);
void onUpdate(); void onUpdate();
long getId();
void setId(long id);
} }

View file

@ -131,6 +131,6 @@ interface IOsmAndAidlInterface {
boolean search(in SearchParams params, IOsmAndAidlCallback callback); boolean search(in SearchParams params, IOsmAndAidlCallback callback);
boolean navigateSearch(in NavigateSearchParams params); boolean navigateSearch(in NavigateSearchParams params);
boolean registerForUpdates(in long updateTimeMS, IOsmAndAidlCallback callback); long registerForUpdates(in long updateTimeMS, IOsmAndAidlCallback callback);
boolean unregisterFromUpdates(in IOsmAndAidlCallback callback); boolean unregisterFromUpdates(in long callbackId);
} }

View file

@ -71,6 +71,8 @@ public class OsmandAidlService extends Service {
private static final int MIN_UPDATE_TIME_MS = 1000; private static final int MIN_UPDATE_TIME_MS = 1000;
private static final int MIN_UPDATE_TIME_MS_ERROR = -1;
private Map<Long, IOsmAndAidlCallback> callbacks; private Map<Long, IOsmAndAidlCallback> callbacks;
private Handler mHandler = null; private Handler mHandler = null;
HandlerThread mHandlerThread = new HandlerThread("OsmAndAidlServiceThread"); HandlerThread mHandlerThread = new HandlerThread("OsmAndAidlServiceThread");
@ -102,6 +104,13 @@ public class OsmandAidlService extends Service {
callbacks = new HashMap<>(); callbacks = new HashMap<>();
} }
@Override
public void onDestroy() {
super.onDestroy();
callbacks.clear();
mHandlerThread.quit();
}
private final IOsmAndAidlInterface.Stub mBinder = new IOsmAndAidlInterface.Stub() { private final IOsmAndAidlInterface.Stub mBinder = new IOsmAndAidlInterface.Stub() {
private void handleException(Exception e) { private void handleException(Exception e) {
@ -593,39 +602,37 @@ public class OsmandAidlService extends Service {
} }
@Override @Override
public boolean registerForUpdates(long updateTimeMS, IOsmAndAidlCallback callback) throws RemoteException { public long registerForUpdates(long updateTimeMS, IOsmAndAidlCallback callback) throws RemoteException {
if (callback != null && updateTimeMS >= MIN_UPDATE_TIME_MS) { if (updateTimeMS >= MIN_UPDATE_TIME_MS) {
callback.setId(updateCallbackId);
callbacks.put(updateCallbackId, callback);
updateCallbackId++; updateCallbackId++;
startRemoteUpdates(updateTimeMS, callback); callbacks.put(updateCallbackId, callback);
return true; startRemoteUpdates(updateTimeMS, updateCallbackId, callback);
return updateCallbackId;
} else {
return MIN_UPDATE_TIME_MS_ERROR;
} }
return false;
} }
@Override @Override
public boolean unregisterFromUpdates(IOsmAndAidlCallback callback) throws RemoteException { public boolean unregisterFromUpdates(long callbackId) throws RemoteException {
if (callback != null) { callbacks.remove(callbackId);
callbacks.remove(callback.getId()); return true;
}
return false;
} }
}; };
void startRemoteUpdates(final long updateTimeMS, final IOsmAndAidlCallback callback) { void startRemoteUpdates(final long updateTimeMS, final long callbackId, final IOsmAndAidlCallback callback) {
mHandler.postDelayed(new Runnable() { mHandler.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
if (callbacks.containsKey(callback.getId())) { if (callbacks.containsKey(callbackId)) {
if (getApi("isUpdateAllowed").isUpdateAllowed()) { if (getApi("isUpdateAllowed").isUpdateAllowed()) {
callback.onUpdate(); callback.onUpdate();
} }
startRemoteUpdates(updateTimeMS, callback); startRemoteUpdates(updateTimeMS, callbackId, callback);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); LOG.error("AIDL e.getMessage()", e);
} }
} }
}, updateTimeMS); }, updateTimeMS);