Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): add callback support to Ti.Platform.openURL() #11668

Merged
merged 3 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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