Use an actual semaphore to manage the cleanup and always release the lock
MODIFY
src/android/plugin/google/maps/PluginMarker.java
src/android/plugin/google/maps/PluginMarker.java
@@ -34,6 +34,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Set;
+import java.util.concurrent.Semaphore;
public class PluginMarker extends MyPlugin implements MyPluginInterface {
@@ -46,11 +47,11 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
protected HashMap<String, Bitmap> icons = new HashMap<String, Bitmap>();
protected final HashMap<String, Integer> iconCacheKeys = new HashMap<String, Integer>();
private static final Paint paint = new Paint();
- private final HashMap<String, Integer> semaphoreAsync = new HashMap<String, Integer>();
private boolean _clearDone = false;
protected interface ICreateMarkerCallback {
void onSuccess(Marker marker);
+
void onError(String message);
}
@@ -95,7 +96,9 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
@Override
protected void clear() {
- synchronized (semaphoreAsync) {
+
+ Semaphore semaphore = new Semaphore(1);
+
_clearDone = false;
cordova.getThreadPool().submit(new Runnable() {
@@ -143,12 +146,13 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
icons.clear();
}
+ try {
+
+ semaphore.acquire();
//--------------------------------------
// clean up properties as much as possible
//--------------------------------------
- cordova.getActivity().runOnUiThread(new Runnable() {
- @Override
- public void run() {
+ cordova.getActivity().runOnUiThread(() -> {
Set<String> keySet = pluginMap.objects.keys;
if (keySet.size() > 0) {
String[] objectIdArray = keySet.toArray(new String[keySet.size()]);
@@ -169,30 +173,23 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
}
}
}
-
- synchronized (semaphoreAsync) {
- _clearDone = true;
- semaphoreAsync.notify();
- }
}
- }
+ _clearDone = true;
+ semaphore.release();
});
- try {
if (!_clearDone) {
- semaphoreAsync.wait(1000);
- }
- } catch (InterruptedException e) {
- // ignore
- //e.printStackTrace();
- }
+ semaphore.acquire();
}
+ } catch (InterruptedException ignore) {
}
+ }
/**
* Create a marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -598,8 +595,8 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
}
/**
- *
* http://android-er.blogspot.com/2013/01/implement-bouncing-marker-for-google.html
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -620,6 +617,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
public void onPostExecute(Object object) {
callbackContext.success();
}
+
@Override
public void onError(String errorMsg) {
callbackContext.error(errorMsg);
@@ -630,6 +628,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Show the InfoWindow bound with the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -650,6 +649,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Set rotation for the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -662,6 +662,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Set opacity for the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -674,6 +675,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Set zIndex for the marker (dummy code, not available on Android V2)
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -687,6 +689,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* set position
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -708,6 +711,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Set flat for the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -720,6 +724,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Set visibility for the object
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -745,6 +750,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
this.setBoolean("setVisible", id, isVisible, callbackContext);
}
+
/**
* @param args
* @param callbackContext
@@ -769,8 +775,10 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
self.pluginMap.objects.put(propertyId, properties);
callbackContext.success();
}
+
/**
* Set title for the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -783,6 +791,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Set the snippet for the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -795,6 +804,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Hide the InfoWindow binded with the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -815,6 +825,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Remove the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -848,6 +859,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
}
});
}
+
protected void _removeMarker(Marker marker) {
if (marker == null || marker.getTag() == null) {
return;
@@ -881,6 +893,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Set anchor for the icon of the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -901,6 +914,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Set anchor for the InfoWindow of the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -920,6 +934,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Set draggable for the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -932,6 +947,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
/**
* Set icon of the marker
+ *
* @param args
* @param callbackContext
* @throws JSONException
@@ -1173,6 +1189,7 @@ public class PluginMarker extends MyPlugin implements MyPluginInterface {
}
});
}
+
private void _setInfoWindowAnchor(final Marker marker, double anchorX, double anchorY, final int imageWidth, final int imageHeight) {
// The `anchor` of the `icon` property
anchorX = anchorX * density;

Use an actual semaphore to manage the cleanup and always release the lock