[android] Add bindings for onRemoveUnusedStyleImages API
This commit is contained in:
@@ -33,6 +33,8 @@ class MapChangeReceiver implements NativeMapView.StateCallback {
|
||||
private final List<MapView.OnSourceChangedListener> onSourceChangedListenerList = new CopyOnWriteArrayList<>();
|
||||
private final List<MapView.OnStyleImageMissingListener> onStyleImageMissingListenerList
|
||||
= new CopyOnWriteArrayList<>();
|
||||
private final List<MapView.OnCanRemoveUnusedStyleImageListener> onCanRemoveUnusedStyleImageListenerList
|
||||
= new CopyOnWriteArrayList<>();
|
||||
|
||||
@Override
|
||||
public void onCameraWillChange(boolean animated) {
|
||||
@@ -230,6 +232,29 @@ class MapChangeReceiver implements NativeMapView.StateCallback {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCanRemoveUnusedStyleImage(String imageId) {
|
||||
if (onCanRemoveUnusedStyleImageListenerList.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
if (!onCanRemoveUnusedStyleImageListenerList.isEmpty()) {
|
||||
boolean canRemove = true;
|
||||
for (MapView.OnCanRemoveUnusedStyleImageListener listener : onCanRemoveUnusedStyleImageListenerList) {
|
||||
canRemove &= listener.onCanRemoveUnusedStyleImage(imageId);
|
||||
}
|
||||
|
||||
return canRemove;
|
||||
}
|
||||
} catch (Throwable err) {
|
||||
Logger.e(TAG, "Exception in onCanRemoveUnusedStyleImage", err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void addOnCameraWillChangeListener(MapView.OnCameraWillChangeListener listener) {
|
||||
onCameraWillChangeListenerList.add(listener);
|
||||
}
|
||||
@@ -342,6 +367,14 @@ class MapChangeReceiver implements NativeMapView.StateCallback {
|
||||
onStyleImageMissingListenerList.remove(listener);
|
||||
}
|
||||
|
||||
void addOnCanRemoveUnusedStyleImageListener(MapView.OnCanRemoveUnusedStyleImageListener listener) {
|
||||
onCanRemoveUnusedStyleImageListenerList.add(listener);
|
||||
}
|
||||
|
||||
void removeOnCanRemoveUnusedStyleImageListener(MapView.OnCanRemoveUnusedStyleImageListener listener) {
|
||||
onCanRemoveUnusedStyleImageListenerList.remove(listener);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
onCameraWillChangeListenerList.clear();
|
||||
onCameraIsChangingListenerList.clear();
|
||||
@@ -357,5 +390,6 @@ class MapChangeReceiver implements NativeMapView.StateCallback {
|
||||
onDidFinishLoadingStyleListenerList.clear();
|
||||
onSourceChangedListenerList.clear();
|
||||
onStyleImageMissingListenerList.clear();
|
||||
onCanRemoveUnusedStyleImageListenerList.clear();
|
||||
}
|
||||
}
|
||||
@@ -797,6 +797,33 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
|
||||
mapChangeReceiver.removeOnStyleImageMissingListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a callback that's invoked when map needs to release unused image resources.
|
||||
*
|
||||
* A callback will be called only for unused images that were provided by the client via
|
||||
* {@link OnStyleImageMissingListener#onStyleImageMissing(String)} listener interface.
|
||||
*
|
||||
* By default, platform will remove unused images from the style. By adding listener, default
|
||||
* behavior can be overridden and client can control whether to release unused resources.
|
||||
*
|
||||
* @param listener The callback that's invoked when map needs to release unused image resources
|
||||
*/
|
||||
public void addOnCanRemoveUnusedStyleImageListener(@NonNull OnCanRemoveUnusedStyleImageListener listener) {
|
||||
mapChangeReceiver.addOnCanRemoveUnusedStyleImageListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a callback that's invoked when map needs to release unused image resources.
|
||||
*
|
||||
* When all listeners are removed, platform will fallback to default behavior, which is to remove
|
||||
* unused images from the style.
|
||||
*
|
||||
* @param listener The callback that's invoked when map needs to release unused image resources
|
||||
*/
|
||||
public void removeOnCanRemoveUnusedStyleImageListener(@NonNull OnCanRemoveUnusedStyleImageListener listener) {
|
||||
mapChangeReceiver.removeOnCanRemoveUnusedStyleImageListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface definition for a callback to be invoked when the camera will change.
|
||||
* <p>
|
||||
@@ -994,6 +1021,22 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
|
||||
void onStyleImageMissing(@NonNull String id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface definition for a callback to be invoked with an unused image identifier.
|
||||
* <p>
|
||||
* {@link MapView#addOnCanRemoveUnusedStyleImageListener(OnCanRemoveUnusedStyleImageListener)}
|
||||
* </p>
|
||||
*/
|
||||
public interface OnCanRemoveUnusedStyleImageListener {
|
||||
/**
|
||||
* Called when the map needs to release unused image resources.
|
||||
*
|
||||
* @param id of an image that is not used by the map and can be removed from the style.
|
||||
* @return true if image can be removed, false otherwise.
|
||||
*/
|
||||
boolean onCanRemoveUnusedStyleImage(@NonNull String id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a callback object which will be triggered when the {@link MapboxMap} instance is ready to be used.
|
||||
*
|
||||
|
||||
@@ -1050,6 +1050,15 @@ final class NativeMapView implements NativeMap {
|
||||
}
|
||||
}
|
||||
|
||||
@Keep
|
||||
private boolean onCanRemoveUnusedStyleImage(String imageId) {
|
||||
if (stateCallback != null) {
|
||||
return stateCallback.onCanRemoveUnusedStyleImage(imageId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Keep
|
||||
protected void onSnapshotReady(@Nullable Bitmap mapContent) {
|
||||
if (checkState("OnSnapshotReady")) {
|
||||
@@ -1463,5 +1472,7 @@ final class NativeMapView implements NativeMap {
|
||||
void onSourceChanged(String sourceId);
|
||||
|
||||
void onStyleImageMissing(String imageId);
|
||||
|
||||
boolean onCanRemoveUnusedStyleImage(String imageId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user