Skip to content

Commit

Permalink
fix: ignore foreign onActivityResult (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak authored May 15, 2023
1 parent 2d6571d commit 6c4ca11
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
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

0 comments on commit 6c4ca11

Please sign in to comment.