Add check for active mapActivity and add parameter updateTimeMS

This commit is contained in:
Chumva 2018-08-30 17:07:54 +03:00
parent 50c7e5e63e
commit 396d56ba28
5 changed files with 29 additions and 30 deletions

View file

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

View file

@ -51,6 +51,8 @@ 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 UPDATE_TIME_MS = 5000L
}
private var mIOsmAndAidlInterface: IOsmAndAidlInterface? = null
@ -1032,7 +1034,7 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
fun registerForUpdates(): Boolean {
if (mIOsmAndAidlInterface != null) {
try {
return mIOsmAndAidlInterface!!.registerForUpdates(mIOsmAndAidlCallback)
return mIOsmAndAidlInterface!!.registerForUpdates(UPDATE_TIME_MS, mIOsmAndAidlCallback)
} catch (e: RemoteException) {
e.printStackTrace()
}

View file

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

View file

@ -160,12 +160,14 @@ public class OsmandAidlApi {
private Map<String, OsmandMapLayer> mapLayers = new ConcurrentHashMap<>();
private Map<String, BroadcastReceiver> receivers = new TreeMap<>();
private boolean mapActivityActive = false;
public OsmandAidlApi(OsmandApplication app) {
this.app = app;
}
public void onCreateMapActivity(final MapActivity mapActivity) {
mapActivityActive = true;
registerRefreshMapReceiver(mapActivity);
registerSetMapLocationReceiver(mapActivity);
registerAddMapWidgetReceiver(mapActivity);
@ -186,6 +188,7 @@ public class OsmandAidlApi {
}
public void onDestroyMapActivity(final MapActivity mapActivity) {
mapActivityActive = false;
for (BroadcastReceiver b : receivers.values()) {
if(b == null) {
continue;
@ -199,6 +202,10 @@ public class OsmandAidlApi {
receivers = new TreeMap<>();
}
public boolean isUpdateAllowed() {
return mapActivityActive;
}
private void registerRefreshMapReceiver(final MapActivity mapActivity) {
BroadcastReceiver refreshMapReceiver = new BroadcastReceiver() {
@Override
@ -1552,8 +1559,4 @@ public class OsmandAidlApi {
public interface SearchCompleteCallback {
void onSearchComplete(List<SearchResult> resultSet);
}
public interface UpdateCompleteCallback {
void onUpdateComplete();
}
}

View file

@ -69,7 +69,7 @@ public class OsmandAidlService extends Service {
private static final Log LOG = PlatformUtil.getLog(OsmandAidlService.class);
private static final int UPDATE_TIME_MS = 5000;
private static final int MIN_UPDATE_TIME_MS = 1000;
private static final int MSG_RUN_SEARCH = 53;
private static final String DATA_KEY_RESULT_SET = "resultSet";
@ -79,8 +79,7 @@ public class OsmandAidlService extends Service {
private ServiceHandler mHandler = null;
HandlerThread mHandlerThread = new HandlerThread("OsmAndAidlServiceThread");
private boolean updatesStarted = false;
private long callbackId = 0;
private long updateCallbackId = 0;
OsmandApplication getApp() {
return (OsmandApplication) getApplication();
@ -581,15 +580,12 @@ public class OsmandAidlService extends Service {
}
@Override
public boolean registerForUpdates(IOsmAndAidlCallback callback) throws RemoteException {
if (callback != null) {
callback.setId(callbackId);
callbacks.put(callbackId, callback);
callbackId++;
if (!updatesStarted) {
startRemoteUpdates();
updatesStarted = true;
}
public boolean registerForUpdates(long updateTimeMS, IOsmAndAidlCallback callback) throws RemoteException {
if (callback != null && updateTimeMS >= MIN_UPDATE_TIME_MS) {
callback.setId(updateCallbackId);
callbacks.put(updateCallbackId, callback);
updateCallbackId++;
startRemoteUpdates(updateTimeMS, callback);
return true;
}
return false;
@ -598,31 +594,29 @@ public class OsmandAidlService extends Service {
@Override
public boolean unregisterFromUpdates(IOsmAndAidlCallback callback) throws RemoteException {
if (callback != null) {
callbacks.remove(callback.getId());
long id = callback.getId();
callbacks.remove(id);
}
return false;
}
};
void startRemoteUpdates() {
void startRemoteUpdates(final long updateTimeMS, final IOsmAndAidlCallback callback) {
mHandler.postDelayed((new Runnable() {
@Override
public void run() {
try {
for (Map.Entry entry : callbacks.entrySet()) {
IOsmAndAidlCallback callback = (IOsmAndAidlCallback) entry.getValue();
callback.onUpdate();
if (callbacks.containsKey(callback.getId())) {
if (getApi("isUpdateAllowed").isUpdateAllowed()) {
callback.onUpdate();
}
startRemoteUpdates(updateTimeMS, callback);
}
} catch (RemoteException e) {
e.printStackTrace();
}
if (callbacks.isEmpty()) {
updatesStarted = false;
} else {
startRemoteUpdates();
}
}
}), UPDATE_TIME_MS);
}), updateTimeMS);
}
/**