From 43d287e685fe9da5efedbda9ed0921bf32fff573 Mon Sep 17 00:00:00 2001 From: Joshua Quick Date: Tue, 12 May 2020 17:50:18 -0700 Subject: [PATCH] feat(android): add callback support to Ti.Platform.openURL() (#11668) Co-authored-by: Christopher Williams Co-authored-by: ssekhri --- .../titanium/platform/PlatformModule.java | 33 +++++++++++++++++-- apidoc/Titanium/Platform/Platform.yml | 4 ++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/android/modules/platform/src/java/ti/modules/titanium/platform/PlatformModule.java b/android/modules/platform/src/java/ti/modules/titanium/platform/PlatformModule.java index 73585d8c209..49bbde02e66 100644 --- a/android/modules/platform/src/java/ti/modules/titanium/platform/PlatformModule.java +++ b/android/modules/platform/src/java/ti/modules/titanium/platform/PlatformModule.java @@ -7,6 +7,7 @@ package ti.modules.titanium.platform; import org.appcelerator.kroll.KrollDict; +import org.appcelerator.kroll.KrollFunction; import org.appcelerator.kroll.KrollInvocation; import org.appcelerator.kroll.KrollModule; import org.appcelerator.kroll.KrollProxy; @@ -295,8 +296,36 @@ private boolean canOpen(Intent intent) } @Kroll.method - public boolean openURL(KrollInvocation invocation, String url) - { + public boolean openURL(KrollInvocation invocation, String url, + @Kroll.argument(optional = true) Object arg2, @Kroll.argument(optional = true) Object arg3) + { + // If given an optional callback, then call this method recursively without the callback. + // Note: We might also receieve an optional KrollDict argument. This is iOS only and should be ignored. + KrollFunction callback = null; + if (arg2 instanceof KrollFunction) { + callback = (KrollFunction) arg2; + } else if (arg3 instanceof KrollFunction) { + callback = (KrollFunction) arg3; + } + if (callback != null) { + // Attempt to open the URL. + boolean wasOpened = false; + String errorMessage = null; + try { + wasOpened = openURL(invocation, url, null, null); + } catch (Exception ex) { + errorMessage = ex.getMessage(); + } + + // Invoke callback with the result on the next message pump. (Mimics iOS' behavior.) + KrollDict event = new KrollDict(); + event.putCodeAndMessage(wasOpened ? 0 : 1, errorMessage); + callback.callAsync(getKrollObject(), event); + + // Return the result immediately for backward compatibility. + return wasOpened; + } + Log.d(TAG, "Launching viewer for: " + url, Log.DEBUG_MODE); // Validate argument. diff --git a/apidoc/Titanium/Platform/Platform.yml b/apidoc/Titanium/Platform/Platform.yml index c6731ce0bad..8e978cb3724 100644 --- a/apidoc/Titanium/Platform/Platform.yml +++ b/apidoc/Titanium/Platform/Platform.yml @@ -98,7 +98,9 @@ methods: type: OpenURLOptions optional: true - name: callback - summary: The optional callback that is called once the URL is opened (iOS 10+). + summary: | + The optional callback that is called once the URL is opened (iOS 10+). + Also supported on Android as of Titanium 9.1.0. type: Callback optional: true