Skip to content

Commit

Permalink
feat(android): add callback support to Ti.Platform.openURL() (#11668)
Browse files Browse the repository at this point in the history
Co-authored-by: Christopher Williams <chris.a.williams@gmail.com>
Co-authored-by: ssekhri <ssekhri@axway.com>
  • Loading branch information
3 people committed May 13, 2020
1 parent 1d20bab commit 43d287e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion apidoc/Titanium/Platform/Platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 43d287e

Please sign in to comment.