Add cross-platform Linking module

Summary:
A promise based API for handling Link for Android and iOS. Refer #4971

The iOS part doesn't handle errors. Will need someone with iOS knowledge to do that.

cc skevy ide brentvatne mkonicek vjeux nicklockwood
Closes https://github.com/facebook/react-native/pull/5336

Reviewed By: svcscm

Differential Revision: D2866664

Pulled By: androidtrunkagent

fb-gh-sync-id: 67e68a827e6b85886bfa84e79b897f079e78b1b5
This commit is contained in:
Satyajit Sahoo
2016-01-26 14:34:00 -08:00
committed by facebook-github-bot-5
parent affd6230fe
commit e33e6ab1f0
7 changed files with 281 additions and 73 deletions

View File

@@ -13,8 +13,8 @@ import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
@@ -36,10 +36,10 @@ public class IntentModule extends ReactContextBaseJavaModule {
/**
* Return the URL the activity was started with
*
* @param callback a callback which is called with the initial URL
* @param promise a promise which is resolved with the initial URL
*/
@ReactMethod
public void getInitialURL(Callback callback) {
public void getInitialURL(Promise promise) {
try {
Activity currentActivity = getCurrentActivity();
String initialURL = null;
@@ -54,10 +54,10 @@ public class IntentModule extends ReactContextBaseJavaModule {
}
}
callback.invoke(initialURL);
promise.resolve(initialURL);
} catch (Exception e) {
throw new JSApplicationIllegalArgumentException(
"Could not get the initial URL : " + e.getMessage());
promise.reject(new JSApplicationIllegalArgumentException(
"Could not get the initial URL : " + e.getMessage()));
}
}
@@ -70,9 +70,10 @@ public class IntentModule extends ReactContextBaseJavaModule {
* @param url the URL to open
*/
@ReactMethod
public void openURL(String url) {
public void openURL(String url, Promise promise) {
if (url == null || url.isEmpty()) {
throw new JSApplicationIllegalArgumentException("Invalid URL: " + url);
promise.reject(new JSApplicationIllegalArgumentException("Invalid URL: " + url));
return;
}
try {
@@ -85,9 +86,11 @@ public class IntentModule extends ReactContextBaseJavaModule {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getReactApplicationContext().startActivity(intent);
}
promise.resolve(true);
} catch (Exception e) {
throw new JSApplicationIllegalArgumentException(
"Could not open URL '" + url + "': " + e.getMessage());
promise.reject(new JSApplicationIllegalArgumentException(
"Could not open URL '" + url + "': " + e.getMessage()));
}
}
@@ -95,12 +98,13 @@ public class IntentModule extends ReactContextBaseJavaModule {
* Determine whether or not an installed app can handle a given URL.
*
* @param url the URL to open
* @param callback a callback that is always called with a boolean argument
* @param promise a promise that is always resolved with a boolean argument
*/
@ReactMethod
public void canOpenURL(String url, Callback callback) {
public void canOpenURL(String url, Promise promise) {
if (url == null || url.isEmpty()) {
throw new JSApplicationIllegalArgumentException("Invalid URL: " + url);
promise.reject(new JSApplicationIllegalArgumentException("Invalid URL: " + url));
return;
}
try {
@@ -110,10 +114,10 @@ public class IntentModule extends ReactContextBaseJavaModule {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
boolean canOpen =
intent.resolveActivity(getReactApplicationContext().getPackageManager()) != null;
callback.invoke(canOpen);
promise.resolve(canOpen);
} catch (Exception e) {
throw new JSApplicationIllegalArgumentException(
"Could not check if URL '" + url + "' can be opened: " + e.getMessage());
promise.reject(new JSApplicationIllegalArgumentException(
"Could not check if URL '" + url + "' can be opened: " + e.getMessage()));
}
}
}