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());
}
}
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());
}
}
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());
}
}
Aggregations