From a648c51588627404b5ad30c35943fed18af4a546 Mon Sep 17 00:00:00 2001 From: Carl Poole Date: Wed, 3 Mar 2021 09:33:32 -0600 Subject: [PATCH] feat(android): Unifying saving plugin calls (#4254) --- .../main/java/com/getcapacitor/Bridge.java | 14 ++++++-- .../java/com/getcapacitor/MessageHandler.java | 2 +- .../main/java/com/getcapacitor/Plugin.java | 10 +++--- .../java/com/getcapacitor/PluginCall.java | 36 ++++++++++++++++--- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 41cadfed54..261e7b16ee 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -545,7 +545,7 @@ public void callPluginMethod(String pluginId, final String methodName, final Plu try { plugin.invoke(methodName, call); - if (call.isSaved()) { + if (call.isKeptAlive()) { saveCall(call); } } catch (PluginLoadException | InvalidPluginMethodException ex) { @@ -646,10 +646,18 @@ PluginCall getPluginCallForLastActivity() { /** * Release a retained call - * @param call + * @param call a call to release */ public void releaseCall(PluginCall call) { - this.savedCalls.remove(call.getCallbackId()); + releaseCall(call.getCallbackId()); + } + + /** + * Release a retained call by its ID + * @param callbackId an ID of a callback to release + */ + public void releaseCall(String callbackId) { + this.savedCalls.remove(callbackId); } /** diff --git a/android/capacitor/src/main/java/com/getcapacitor/MessageHandler.java b/android/capacitor/src/main/java/com/getcapacitor/MessageHandler.java index 50f83897d9..8b83e1236b 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/MessageHandler.java +++ b/android/capacitor/src/main/java/com/getcapacitor/MessageHandler.java @@ -81,7 +81,7 @@ public void postMessage(String jsonStr) { public void sendResponseMessage(PluginCall call, PluginResult successResult, PluginResult errorResult) { try { PluginResult data = new PluginResult(); - data.put("save", call.isSaved()); + data.put("save", call.isKeptAlive()); data.put("callbackId", call.getCallbackId()); data.put("pluginId", call.getPluginId()); data.put("methodName", call.getMethodName()); diff --git a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java index 6cb0f12e26..cba5998090 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java @@ -147,7 +147,7 @@ private void triggerPermissionCallback(Method method, Map permi e.printStackTrace(); } - if (!savedCall.isReleased() && !savedCall.isSaved()) { + if (!savedCall.isKeptAlive()) { savedCall.release(bridge); } } @@ -167,7 +167,7 @@ private void triggerActivityCallback(Method method, ActivityResult result) { e.printStackTrace(); } - if (!savedCall.isReleased() && !savedCall.isSaved()) { + if (!savedCall.isKeptAlive()) { savedCall.release(bridge); } } @@ -285,9 +285,7 @@ public void saveCall(PluginCall lastCall) { */ @Deprecated public void freeSavedCall() { - if (!this.savedLastCall.isReleased()) { - this.savedLastCall.release(bridge); - } + this.savedLastCall.release(bridge); this.savedLastCall = null; } @@ -735,7 +733,7 @@ private void sendRetainedArgumentsForEvent(String eventName) { @PluginMethod(returnType = PluginMethod.RETURN_NONE) public void addListener(PluginCall call) { String eventName = call.getString("eventName"); - call.save(); + call.setKeepAlive(true); addEventListener(eventName, call); } diff --git a/android/capacitor/src/main/java/com/getcapacitor/PluginCall.java b/android/capacitor/src/main/java/com/getcapacitor/PluginCall.java index 50ea603f32..f4ca7c9367 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/PluginCall.java +++ b/android/capacitor/src/main/java/com/getcapacitor/PluginCall.java @@ -25,11 +25,12 @@ public class PluginCall { private final String methodName; private final JSObject data; - private boolean shouldSave = false; + private boolean keepAlive = false; /** * Indicates that this PluginCall was released, and should no longer be used */ + @Deprecated private boolean isReleased = false; public PluginCall(MessageHandler msgHandler, String pluginId, String callbackId, String methodName, JSObject data) { @@ -340,21 +341,48 @@ public boolean hasOption(String name) { * Indicate that the Bridge should cache this call in order to call * it again later. For example, the addListener system uses this to * continuously call the call's callback (😆). + * @deprecated use {@link #setKeepAlive(Boolean)} instead */ + @Deprecated public void save() { - this.shouldSave = true; + setKeepAlive(true); + } + + /** + * Indicate that the Bridge should cache this call in order to call + * it again later. For example, the addListener system uses this to + * continuously call the call's callback. + * + * @param keepAlive whether to keep the callback saved + */ + public void setKeepAlive(Boolean keepAlive) { + this.keepAlive = keepAlive; } public void release(Bridge bridge) { - this.shouldSave = false; + this.keepAlive = false; bridge.releaseCall(this); this.isReleased = true; } + /** + * @deprecated use {@link #isKeptAlive()} + * @return true if the plugin call is kept alive + */ + @Deprecated public boolean isSaved() { - return shouldSave; + return isKeptAlive(); + } + + /** + * Gets the keepAlive value of the plugin call + * @return true if the plugin call is kept alive + */ + public boolean isKeptAlive() { + return keepAlive; } + @Deprecated public boolean isReleased() { return isReleased; }