Skip to content

Grant/Revoke Uri permissions #4

@sam-maverick

Description

@sam-maverick

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch react-native-file-provider@1.1.0 for the project I'm working on.

I am using this library for creating Uri links that other apps can use to access shared files in Android. However, in my case I need to grant and revoke permissions for that Uri, so that the other app can access it. As I have not found other libraries implementing this, I am attaching the suggested modifications to implement the grantUriPermission() and revokeUriPermission() methods.

More info here:

https://developer.android.com/reference/android/content/Context#grantUriPermission(java.lang.String,%20android.net.Uri,%20int)

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-file-provider/android/src/main/java/com/artirigo/fileprovider/RNFileProviderModule.java b/node_modules/react-native-file-provider/android/src/main/java/com/artirigo/fileprovider/RNFileProviderModule.java
index a9bc7d9..d73af12 100644
--- a/node_modules/react-native-file-provider/android/src/main/java/com/artirigo/fileprovider/RNFileProviderModule.java
+++ b/node_modules/react-native-file-provider/android/src/main/java/com/artirigo/fileprovider/RNFileProviderModule.java
@@ -9,6 +9,7 @@ import com.facebook.react.bridge.Callback;
 
 import java.io.File;
 import android.net.Uri;
+import android.content.Intent;
 
 import android.support.v4.content.FileProvider;
 
@@ -42,6 +43,29 @@ public class RNFileProviderModule extends ReactContextBaseJavaModule {
     }
   }
 
+  @ReactMethod
+  public void grantUriPermissionRW(String packageName, String uri, Promise promise) {
+    try {
+      //grant permision specific app, e.g. before starting other app via intent
+      reactContext.getApplicationContext().grantUriPermission(packageName, Uri.parse(uri), Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
+      promise.resolve(null);
+    } catch (Exception ex) {
+      ex.printStackTrace();
+      reject(promise, uri, ex);
+    }
+  }
+
+  @ReactMethod
+  public void revokeUriPermissionRW(String uri, Promise promise) {
+    try {
+      reactContext.getApplicationContext().revokeUriPermission(Uri.parse(uri), Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
+      promise.resolve(null);
+    } catch (Exception ex) {
+      ex.printStackTrace();
+      reject(promise, uri, ex);
+    }
+  }
+
   private void reject(Promise promise, String filepath, Exception ex) {
     promise.reject(null, ex.getMessage());
   }

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions