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

fix: ignore foreign startActivityForResult #652

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ New architecture is supported.

- [react-native-document-picker](#react-native-document-picker)
- [Installation](#installation)
- [RN >= 0.69](#rn--063)
- [RN >= 0.69](#rn--069)
- [API](#api)
- [pickSingle(options) / pick(options)](#picksingleoptions--pickoptions)
- [pickDirectory()](#pickdirectory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,22 @@ public RNDocumentPickerModule(ReactApplicationContext reactContext) {
private final ActivityEventListener activityEventListener = new BaseActivityEventListener() {
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
boolean isForeignResult = requestCode != READ_REQUEST_CODE && requestCode != PICK_DIR_REQUEST_CODE;
if (isForeignResult) {
return;
}
final Promise storedPromise = promise;
if (storedPromise == null) {
Log.e(NAME, "promise was null in onActivityResult");
return;
}
if (resultCode == Activity.RESULT_CANCELED) {
sendError(E_DOCUMENT_PICKER_CANCELED, "User canceled directory picker");
return;
}
if (requestCode == READ_REQUEST_CODE) {
onShowActivityResult(resultCode, data, storedPromise);
} else if (requestCode == PICK_DIR_REQUEST_CODE) {
} else {
onPickDirectoryResult(resultCode, data);
}
}
Expand All @@ -96,8 +104,8 @@ private String[] readableArrayToStringArray(ReadableArray readableArray) {

@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
getReactApplicationContext().removeActivityEventListener(activityEventListener);
super.onCatalystInstanceDestroy();
}

@NonNull
Expand Down Expand Up @@ -170,10 +178,7 @@ public void releaseSecureAccess(ReadableArray uris, Promise promise) {
}

private void onPickDirectoryResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_CANCELED) {
sendError(E_DOCUMENT_PICKER_CANCELED, "User canceled directory picker");
return;
} else if (resultCode != Activity.RESULT_OK) {
if (resultCode != Activity.RESULT_OK) {
sendError(E_UNKNOWN_ACTIVITY_RESULT, "Unknown activity result: " + resultCode);
return;
}
Expand All @@ -190,39 +195,37 @@ private void onPickDirectoryResult(int resultCode, Intent data) {
}

public void onShowActivityResult(int resultCode, Intent data, Promise promise) {
if (resultCode == Activity.RESULT_CANCELED) {
sendError(E_DOCUMENT_PICKER_CANCELED, "User canceled document picker");
} else if (resultCode == Activity.RESULT_OK) {
Uri uri = null;
ClipData clipData = null;

if (data != null) {
uri = data.getData();
clipData = data.getClipData();
}
if (resultCode != Activity.RESULT_OK) {
sendError(E_UNKNOWN_ACTIVITY_RESULT, "Unknown activity result: " + resultCode);
return;
}
Uri uri = null;
ClipData clipData = null;

try {
List<Uri> uris = new ArrayList<>();
// condition order seems to matter: https://github.com/rnmods/react-native-document-picker/issues/317#issuecomment-645222635
if (clipData != null && clipData.getItemCount() > 0) {
final int length = clipData.getItemCount();
for (int i = 0; i < length; ++i) {
ClipData.Item item = clipData.getItemAt(i);
uris.add(item.getUri());
}
} else if (uri != null) {
uris.add(uri);
} else {
sendError(E_INVALID_DATA_RETURNED, "Invalid data returned by intent");
return;
}
if (data != null) {
uri = data.getData();
clipData = data.getClipData();
}

new ProcessDataTask(getReactApplicationContext(), uris, copyTo, promise).execute();
} catch (Exception e) {
sendError(E_UNEXPECTED_EXCEPTION, e.getLocalizedMessage(), e);
try {
List<Uri> uris = new ArrayList<>();
// condition order seems to matter: https://github.com/rnmods/react-native-document-picker/issues/317#issuecomment-645222635
if (clipData != null && clipData.getItemCount() > 0) {
final int length = clipData.getItemCount();
for (int i = 0; i < length; ++i) {
ClipData.Item item = clipData.getItemAt(i);
uris.add(item.getUri());
}
} else if (uri != null) {
uris.add(uri);
} else {
sendError(E_INVALID_DATA_RETURNED, "Invalid data returned by intent");
return;
}
} else {
sendError(E_UNKNOWN_ACTIVITY_RESULT, "Unknown activity result: " + resultCode);

new ProcessDataTask(getReactApplicationContext(), uris, copyTo, promise).execute();
} catch (Exception e) {
sendError(E_UNEXPECTED_EXCEPTION, e.getLocalizedMessage(), e);
}
}

Expand Down