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

Make Linking.sendIntent support Intent(action,uri) #623

Open
FrederickEngelhardt opened this issue Mar 27, 2023 · 0 comments
Open

Make Linking.sendIntent support Intent(action,uri) #623

FrederickEngelhardt opened this issue Mar 27, 2023 · 0 comments

Comments

@FrederickEngelhardt
Copy link

FrederickEngelhardt commented Mar 27, 2023

Introduction

Linking.sendIntent() currently only runs Intents with actions. This is a limitation, and prevents intents that require a URI link too. IE

  • ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION added in API 30 docs

My hope is to add support for URI links, so they are added to the intent. This will fix menu issues where a app uri needs to be provided.

Example native code that would navigate the user to the

    val uri: Uri = Uri.parse("package:" + "com.exampleApp")
    val fileManagerIntent = Intent("android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION", uri)
    startActivity(reactApplicationContext, fileManagerIntent, null)

This would make the Intent use the constructor here

    public Intent(String action, Uri uri) {
        setAction(action);
        mData = uri;
    }

Details

I propose adding additional params to the Linking.sendIntent.

  • This would require a breaking change to be mentioned for react-native dependencies if we changed the order of params.
  • It would fix any global permissions navigation issues such as file access intents and allow android to trigger custom intents for other apps.

Proposed changes (please interject if this seems wrong).

Noting that these are not the react-native codebase files but the provided node_module files that I patch-packaged. This fix will not work unless you re-compile the react-native 0.71.4 library with the changes below.

diff --git a/node_modules/react-native/Libraries/Linking/Linking.d.ts b/node_modules/react-native/Libraries/Linking/Linking.d.ts
index 36dbe9f..1ef3b92 100644
--- a/node_modules/react-native/Libraries/Linking/Linking.d.ts
+++ b/node_modules/react-native/Libraries/Linking/Linking.d.ts
@@ -53,6 +53,7 @@ export interface LinkingStatic extends NativeEventEmitter {
    */
   sendIntent(
     action: string,
+    url: string,
     extras?: Array<{key: string; value: string | number | boolean}>,
   ): Promise<void>;
 }
diff --git a/node_modules/react-native/Libraries/Linking/Linking.js b/node_modules/react-native/Libraries/Linking/Linking.js
index a59fa72..952e760 100644
--- a/node_modules/react-native/Libraries/Linking/Linking.js
+++ b/node_modules/react-native/Libraries/Linking/Linking.js
@@ -111,6 +111,7 @@ class Linking extends NativeEventEmitter<LinkingEventDefinitions> {
    */
   sendIntent(
     action: string,
+    url: string = null,
     extras?: Array<{
       key: string,
       value: string | number | boolean,
@@ -118,7 +119,7 @@ class Linking extends NativeEventEmitter<LinkingEventDefinitions> {
     }>,
   ): Promise<void> {
     if (Platform.OS === 'android') {
-      return nullthrows(NativeIntentAndroid).sendIntent(action, extras);
+      return nullthrows(NativeIntentAndroid).sendIntent(action, url, extras);
     } else {
       return new Promise((resolve, reject) => reject(new Error('Unsupported')));
     }
diff --git a/node_modules/react-native/Libraries/Linking/NativeIntentAndroid.js b/node_modules/react-native/Libraries/Linking/NativeIntentAndroid.js
index 7a9f9b7..dd731c5 100644
--- a/node_modules/react-native/Libraries/Linking/NativeIntentAndroid.js
+++ b/node_modules/react-native/Libraries/Linking/NativeIntentAndroid.js
@@ -19,6 +19,7 @@ export interface Spec extends TurboModule {
   +openSettings: () => Promise<void>;
   +sendIntent: (
     action: string,
+    url: ?string,
     extras: ?Array<{
       key: string,
       value: string | number | boolean, // TODO(T67672788): Union types are not type safe
diff --git a/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java b/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java
index 7b532d4..d27eac4 100644
--- a/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java
+++ b/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java
@@ -167,13 +167,19 @@ public class IntentModule extends NativeIntentAndroidSpec {
    * @param extras An array of extras [{ String, String | Number | Boolean }]
    */
   @Override
-  public void sendIntent(String action, @Nullable ReadableArray extras, Promise promise) {
+  public void sendIntent(String action, @Nullable String url, @Nullable ReadableArray extras, Promise promise) {
     if (action == null || action.isEmpty()) {
       promise.reject(new JSApplicationIllegalArgumentException("Invalid Action: " + action + "."));
       return;
     }
 
-    Intent intent = new Intent(action);
+    Intent intent
+
+    if (url != null){
+      intent = new Intent(action, url)
+    } else {
+      intent = new Intent(action)
+    }
 
     PackageManager packageManager = getReactApplicationContext().getPackageManager();
     if (intent.resolveActivity(packageManager) == null) {

Example:

    const uri = "package:com.exampleApp"
    Linking.sendIntent(
      'android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION',
      uri,
    )

Discussion points

  1. Support intents that require app context.
  2. Determine how the refactor should be done (IE breaking change or some other option)

Alternative solutions

  1. Extend openSettings and allow uri there since this issue is related to the android settings action being fired but not being able to navigate to the screen without a package uri.
  2. Install react-native-send-intent with my fork branch changes. (It adds the linking fix only to openSettings(). IE yarn add https://github.com/FrederickEngelhardt/react-native-send-intent/\#feat/add-open-settings-uri-context

Workarounds?

Tried the following:

    Linking.sendIntent(
      'android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION',
      [{ key: 'EXTRA_APP_PACKAGE', value: uri }]
    )

The extra key does not allow the app to navigate to the file permission manager under settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant