Search in sources :

Example 1 with ActivityEventListener

use of com.facebook.react.bridge.ActivityEventListener in project react-native-scoped-storage by ammarahm-ed.

the class RNScopedStorageModule method openDocument.

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@ReactMethod
public void openDocument(final boolean readData, final String encoding, final Promise promise) {
    try {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        if (activityEventListener != null) {
            reactContext.removeActivityEventListener(activityEventListener);
            activityEventListener = null;
        }
        activityEventListener = new ActivityEventListener() {

            @Override
            public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
                if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
                    if (data != null) {
                        Uri uri = data.getData();
                        DocumentFile dir = DocumentFile.fromSingleUri(reactContext, uri);
                        WritableMap params = resolveWithDocument(dir, null);
                        if (readData) {
                            try {
                                if (encoding != null) {
                                    if (encoding == "ascii") {
                                        WritableArray arr = (WritableArray) readFromUri(uri, encoding);
                                        params.putArray("data", arr);
                                    } else {
                                        params.putString("data", (String) readFromUri(uri, encoding));
                                    }
                                } else {
                                    params.putString("data", (String) readFromUri(uri, "utf8"));
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        promise.resolve(params);
                    } else {
                        promise.resolve(null);
                    }
                } else {
                    promise.resolve(null);
                }
                reactContext.removeActivityEventListener(activityEventListener);
                activityEventListener = null;
            }

            @Override
            public void onNewIntent(Intent intent) {
            }
        };
        reactContext.addActivityEventListener(activityEventListener);
        reactContext.getCurrentActivity().startActivityForResult(intent, REQUEST_CODE);
    } catch (Exception e) {
        promise.reject("ERROR", e.getMessage());
    }
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) ActivityEventListener(com.facebook.react.bridge.ActivityEventListener) WritableMap(com.facebook.react.bridge.WritableMap) WritableArray(com.facebook.react.bridge.WritableArray) Activity(android.app.Activity) Intent(android.content.Intent) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) RequiresApi(androidx.annotation.RequiresApi) ReactMethod(com.facebook.react.bridge.ReactMethod)

Example 2 with ActivityEventListener

use of com.facebook.react.bridge.ActivityEventListener in project react-native-scoped-storage by ammarahm-ed.

the class RNScopedStorageModule method openDocumentTree.

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@ReactMethod
public void openDocumentTree(final boolean persist, final Promise promise) {
    try {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_OPEN_DOCUMENT_TREE);
        if (activityEventListener != null) {
            reactContext.removeActivityEventListener(activityEventListener);
            activityEventListener = null;
        }
        activityEventListener = new ActivityEventListener() {

            @Override
            public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
                if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
                    if (data != null) {
                        Uri uri = data.getData();
                        if (persist) {
                            final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                            reactContext.getContentResolver().takePersistableUriPermission(uri, takeFlags);
                        }
                        DocumentFile dir = DocumentFile.fromTreeUri(reactContext, uri);
                        resolveWithDocument(dir, promise);
                    } else {
                        promise.resolve(null);
                    }
                } else {
                    promise.resolve(null);
                }
                reactContext.removeActivityEventListener(activityEventListener);
                activityEventListener = null;
            }

            @Override
            public void onNewIntent(Intent intent) {
            }
        };
        reactContext.addActivityEventListener(activityEventListener);
        reactContext.getCurrentActivity().startActivityForResult(intent, REQUEST_CODE);
    } catch (Exception e) {
        promise.reject("ERROR", e.getMessage());
    }
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) ActivityEventListener(com.facebook.react.bridge.ActivityEventListener) Activity(android.app.Activity) Intent(android.content.Intent) Uri(android.net.Uri) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) RequiresApi(androidx.annotation.RequiresApi) ReactMethod(com.facebook.react.bridge.ReactMethod)

Example 3 with ActivityEventListener

use of com.facebook.react.bridge.ActivityEventListener in project react-native-scoped-storage by ammarahm-ed.

the class RNScopedStorageModule method createDocument.

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@ReactMethod
public void createDocument(final String fileName, final String mimeType, final String data, final String encoding, final Promise promise) {
    try {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.putExtra(Intent.EXTRA_TITLE, fileName);
        intent.setType(mimeType);
        if (activityEventListener != null) {
            reactContext.removeActivityEventListener(activityEventListener);
            activityEventListener = null;
        }
        activityEventListener = new ActivityEventListener() {

            @Override
            public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {
                if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
                    if (intent != null) {
                        Uri uri = intent.getData();
                        DocumentFile dir = DocumentFile.fromSingleUri(reactContext, uri);
                        try {
                            byte[] bytes = stringToBytes(data, encoding);
                            OutputStream os = reactContext.getContentResolver().openOutputStream(uri);
                            try {
                                os.write(bytes);
                            } finally {
                                os.close();
                            }
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        resolveWithDocument(dir, promise);
                    } else {
                        promise.resolve(null);
                    }
                } else {
                    promise.resolve(null);
                }
                reactContext.removeActivityEventListener(activityEventListener);
                activityEventListener = null;
            }

            @Override
            public void onNewIntent(Intent intent) {
            }
        };
        reactContext.addActivityEventListener(activityEventListener);
        reactContext.getCurrentActivity().startActivityForResult(intent, REQUEST_CODE);
    } catch (Exception e) {
        promise.reject("ERROR", e.getMessage());
    }
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) ActivityEventListener(com.facebook.react.bridge.ActivityEventListener) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) Activity(android.app.Activity) Intent(android.content.Intent) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) RequiresApi(androidx.annotation.RequiresApi) ReactMethod(com.facebook.react.bridge.ReactMethod)

Aggregations

Activity (android.app.Activity)3 Intent (android.content.Intent)3 Uri (android.net.Uri)3 RequiresApi (androidx.annotation.RequiresApi)3 DocumentFile (androidx.documentfile.provider.DocumentFile)3 ActivityEventListener (com.facebook.react.bridge.ActivityEventListener)3 ReactMethod (com.facebook.react.bridge.ReactMethod)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 WritableArray (com.facebook.react.bridge.WritableArray)1 WritableMap (com.facebook.react.bridge.WritableMap)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1