mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Android: Adding sendIntent on Linking module (#22302)
Summary: This PR implements "Add a standardized way to send intents on Android" discussed in https://github.com/react-native-community/discussions-and-proposals/issues/34. Pull Request resolved: https://github.com/facebook/react-native/pull/22302 Differential Revision: D13374186 Pulled By: cpojer fbshipit-source-id: 2f0b9b9f46e99f382b6c35b1914e75df23a7fd74
This commit is contained in:
committed by
Facebook Github Bot
parent
cc4211c72f
commit
e8a6cb5e18
@@ -10,6 +10,7 @@ package com.facebook.react.modules.intent;
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
|
||||
@@ -17,8 +18,13 @@ import com.facebook.react.bridge.Promise;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.ReadableArray;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.ReadableType;
|
||||
import com.facebook.react.module.annotations.ReactModule;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Intent module. Launch other activities or open URLs.
|
||||
*/
|
||||
@@ -133,4 +139,67 @@ public class IntentModule extends ReactContextBaseJavaModule {
|
||||
"Could not check if URL '" + url + "' can be opened: " + e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows to send intents on Android
|
||||
*
|
||||
* For example, you can open the Notification Category screen for a specific application
|
||||
* passing action = 'android.settings.CHANNEL_NOTIFICATION_SETTINGS'
|
||||
* and extras = [
|
||||
* { 'android.provider.extra.APP_PACKAGE': 'your.package.name.here' },
|
||||
* { 'android.provider.extra.CHANNEL_ID': 'your.channel.id.here }
|
||||
* ]
|
||||
*
|
||||
* @param action The general action to be performed
|
||||
* @param extras An array of extras [{ String, String | Number | Boolean }]
|
||||
*/
|
||||
@ReactMethod
|
||||
public void sendIntent(String action, @Nullable ReadableArray extras, Promise promise) {
|
||||
if (action == null || action.isEmpty()) {
|
||||
promise.reject(new JSApplicationIllegalArgumentException("Invalid Action: " + action + "."));
|
||||
return;
|
||||
}
|
||||
|
||||
Intent intent = new Intent(action);
|
||||
|
||||
PackageManager packageManager = getReactApplicationContext().getPackageManager();
|
||||
if (intent.resolveActivity(packageManager) == null) {
|
||||
promise.reject(new JSApplicationIllegalArgumentException("Could not launch Intent with action " + action + "."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (extras != null) {
|
||||
for (int i = 0; i < extras.size(); i++) {
|
||||
ReadableMap map = extras.getMap(i);
|
||||
String name = map.keySetIterator().nextKey();
|
||||
ReadableType type = map.getType(name);
|
||||
|
||||
switch (type) {
|
||||
case String: {
|
||||
intent.putExtra(name, map.getString(name));
|
||||
break;
|
||||
}
|
||||
case Number: {
|
||||
// We cannot know from JS if is an Integer or Double
|
||||
// See: https://github.com/facebook/react-native/issues/4141
|
||||
// We might need to find a workaround if this is really an issue
|
||||
Double number = map.getDouble(name);
|
||||
intent.putExtra(name, number);
|
||||
break;
|
||||
}
|
||||
case Boolean: {
|
||||
intent.putExtra(name, map.getBoolean(name));
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
promise.reject(new JSApplicationIllegalArgumentException(
|
||||
"Extra type for " + name + " not supported."));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getReactApplicationContext().startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user