Skip to content

Commit eb188c8

Browse files
satya164facebook-github-bot-5
authored andcommitted
Add deep linking support to IntentAndroid
Summary: Add a method to handle URLs registered to the app, ```js IntentAndroid.getInitialURL(url => { if (url) { // do stuff } }); ``` Refer - http://developer.android.com/training/app-indexing/deep-linking.html#adding-filters The API cannot be same as the iOS API (i.e. as a constant), as the activity is not availble at the time of module initialization. Moreover, multiple activties can share the same bridge instance, and the activity itself is not a constant. Hence the initialURL can change. Closes #4320 Reviewed By: svcscm Differential Revision: D2759667 Pulled By: foghina fb-gh-sync-id: b725231ae1401fa5565d444eee5a30d303e263ae
1 parent cf94a9e commit eb188c8

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

Libraries/Components/Intent/IntentAndroid.android.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ var invariant = require('invariant');
1616
/**
1717
* `IntentAndroid` gives you a general interface to handle external links.
1818
*
19+
* ### Basic Usage
20+
*
21+
* #### Handling deep links
22+
*
23+
* If your app was launched from an external url registered to your app you can
24+
* access and handle it from any component you want with
25+
*
26+
* ```
27+
* componentDidMount() {
28+
* var url = IntentAndroid.getInitialURL(url => {
29+
* if (url) {
30+
* console.log('Initial url is: ' + url);
31+
* }
32+
* });
33+
* }
34+
* ```
35+
*
36+
* NOTE: For instructions on how to add support for deep linking,
37+
* refer [Enabling Deep Links for App Content - Add Intent Filters for Your Deep Links](http://developer.android.com/training/app-indexing/deep-linking.html#adding-filters).
38+
*
1939
* #### Opening external links
2040
*
2141
* To start the corresponding activity for a link (web URL, email, contact etc.), call
@@ -75,6 +95,20 @@ class IntentAndroid {
7595
IntentAndroidModule.canOpenURL(url, callback);
7696
}
7797

98+
/**
99+
* If the app launch was triggered by an app link with {@code Intent.ACTION_VIEW},
100+
* it will give the link url, otherwise it will give `null`
101+
*
102+
* Refer http://developer.android.com/training/app-indexing/deep-linking.html#handling-intents
103+
*/
104+
static getInitialURL(callback: Function) {
105+
invariant(
106+
typeof callback === 'function',
107+
'A valid callback function is required'
108+
);
109+
IntentAndroidModule.getInitialURL(callback);
110+
}
111+
78112
static _validateURL(url: string) {
79113
invariant(
80114
typeof url === 'string',

ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
package com.facebook.react.modules.intent;
1111

12+
import android.app.Activity;
1213
import android.content.Intent;
1314
import android.net.Uri;
1415

1516
import com.facebook.react.bridge.Callback;
17+
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
1618
import com.facebook.react.bridge.ReactApplicationContext;
17-
import com.facebook.react.bridge.ReactContext;
1819
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1920
import com.facebook.react.bridge.ReactMethod;
20-
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
2121

2222
/**
2323
* Intent module. Launch other activities or open URLs.
@@ -33,25 +33,58 @@ public String getName() {
3333
return "IntentAndroid";
3434
}
3535

36+
/**
37+
* Return the URL the activity was started with
38+
*
39+
* @param callback a callback which is called with the initial URL
40+
*/
41+
@ReactMethod
42+
public void getInitialURL(Callback callback) {
43+
try {
44+
Activity currentActivity = getCurrentActivity();
45+
String initialURL = null;
46+
47+
if (currentActivity != null) {
48+
Intent intent = currentActivity.getIntent();
49+
String action = intent.getAction();
50+
Uri uri = intent.getData();
51+
52+
if (Intent.ACTION_VIEW.equals(action) && uri != null) {
53+
initialURL = uri.toString();
54+
}
55+
}
56+
57+
callback.invoke(initialURL);
58+
} catch (Exception e) {
59+
throw new JSApplicationIllegalArgumentException(
60+
"Could not get the initial URL : " + e.getMessage());
61+
}
62+
}
63+
3664
/**
3765
* Starts a corresponding external activity for the given URL.
3866
*
3967
* For example, if the URL is "https://www.facebook.com", the system browser will be opened,
4068
* or the "choose application" dialog will be shown.
4169
*
42-
* @param URL the URL to open
70+
* @param url the URL to open
4371
*/
4472
@ReactMethod
4573
public void openURL(String url) {
4674
if (url == null || url.isEmpty()) {
4775
throw new JSApplicationIllegalArgumentException("Invalid URL: " + url);
4876
}
77+
4978
try {
79+
Activity currentActivity = getCurrentActivity();
5080
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
51-
// We need Intent.FLAG_ACTIVITY_NEW_TASK since getReactApplicationContext() returns
52-
// the ApplicationContext instead of the Activity context.
53-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
54-
getReactApplicationContext().startActivity(intent);
81+
82+
if (currentActivity != null) {
83+
currentActivity.startActivity(intent);
84+
} else {
85+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
86+
getReactApplicationContext().startActivity(intent);
87+
}
5588
} catch (Exception e) {
5689
throw new JSApplicationIllegalArgumentException(
5790
"Could not open URL '" + url + "': " + e.getMessage());
@@ -61,21 +94,22 @@ public void openURL(String url) {
6194
/**
6295
* Determine whether or not an installed app can handle a given URL.
6396
*
64-
* @param URL the URL to open
65-
* @param promise a promise that is always resolved with a boolean argument
97+
* @param url the URL to open
98+
* @param callback a callback that is always called with a boolean argument
6699
*/
67100
@ReactMethod
68101
public void canOpenURL(String url, Callback callback) {
69102
if (url == null || url.isEmpty()) {
70103
throw new JSApplicationIllegalArgumentException("Invalid URL: " + url);
71104
}
105+
72106
try {
73107
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
74108
// We need Intent.FLAG_ACTIVITY_NEW_TASK since getReactApplicationContext() returns
75109
// the ApplicationContext instead of the Activity context.
76110
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
77111
boolean canOpen =
78-
intent.resolveActivity(this.getReactApplicationContext().getPackageManager()) != null;
112+
intent.resolveActivity(getReactApplicationContext().getPackageManager()) != null;
79113
callback.invoke(canOpen);
80114
} catch (Exception e) {
81115
throw new JSApplicationIllegalArgumentException(

0 commit comments

Comments
 (0)