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

Grant/Revoke Uri permissions #4

Open
sam-maverick opened this issue Apr 9, 2024 · 0 comments
Open

Grant/Revoke Uri permissions #4

sam-maverick opened this issue Apr 9, 2024 · 0 comments

Comments

@sam-maverick
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] 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.

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