Skip to content

Commit

Permalink
Fix null safety in filepickerdelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilian Cadiou committed Oct 18, 2024
1 parent 8bbe066 commit 0bcc22d
Showing 1 changed file with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class FilePickerDelegate implements PluginRegistry.ActivityResultListener
private boolean isMultipleSelection = false;
private boolean loadDataToMemory = false;
private String type;
private int compressionQuality=20;
private int compressionQuality = 20;
private String[] allowedExtensions;
private EventChannel.EventSink eventSink;

Expand Down Expand Up @@ -85,14 +85,16 @@ public boolean onActivityResult(final int requestCode, final int resultCode, fin
// Save file
if (requestCode == SAVE_FILE_CODE) {
if (resultCode == Activity.RESULT_OK) {
if (data == null)
return false;
this.dispatchEventStatus(true);
final Uri uri = data.getData();
if (uri != null) {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.getAbsolutePath() + File.separator + FileUtils.getFileName(uri, this.activity);
try {
OutputStream outputStream = this.activity.getContentResolver().openOutputStream(uri);
if(outputStream != null){
if (outputStream != null) {
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
Expand All @@ -104,7 +106,6 @@ public boolean onActivityResult(final int requestCode, final int resultCode, fin
finishWithError("Error while saving file", e.getMessage());
}
}

}
if (resultCode == Activity.RESULT_CANCELED) {
Log.i(TAG, "User cancelled the save request");
Expand All @@ -131,13 +132,13 @@ public void run() {
final int count = data.getClipData().getItemCount();
int currentItem = 0;
while (currentItem < count) {
Uri currentUri = data.getClipData().getItemAt(currentItem).getUri();
Uri currentUri = data.getClipData().getItemAt(currentItem).getUri();

if (Objects.equals(type, "image/*") && compressionQuality > 0) {
currentUri = FileUtils.compressImage(currentUri, compressionQuality, activity.getApplicationContext());
}
final FileInfo file = FileUtils.openFileStream(FilePickerDelegate.this.activity, currentUri, loadDataToMemory);
if(file != null) {
if (file != null) {
files.add(file);
Log.d(FilePickerDelegate.TAG, "[MultiFilePick] File #" + currentItem + " - URI: " + currentUri.getPath());
}
Expand All @@ -158,7 +159,7 @@ public void run() {
Log.d(FilePickerDelegate.TAG, "[SingleFilePick] File URI:" + uri.toString());
final String dirPath = FileUtils.getFullPathFromTreeUri(uri, activity);

if(dirPath != null) {
if (dirPath != null) {
finishWithSuccess(dirPath);
} else {
finishWithError("unknown_path", "Failed to retrieve directory path.");
Expand All @@ -168,7 +169,7 @@ public void run() {

final FileInfo file = FileUtils.openFileStream(FilePickerDelegate.this.activity, uri, loadDataToMemory);

if(file != null) {
if (file != null) {
files.add(file);
}

Expand All @@ -179,7 +180,7 @@ public void run() {
finishWithError("unknown_path", "Failed to retrieve path.");
}

} else if (data.getExtras() != null){
} else if (data.getExtras() != null) {
Bundle bundle = data.getExtras();
if (bundle.keySet().contains("selectedItems")) {
ArrayList<Parcelable> fileUris = getSelectedItems(bundle);
Expand Down Expand Up @@ -255,8 +256,8 @@ private static void finishWithAlreadyActiveError(final MethodChannel.Result resu
}

@SuppressWarnings("deprecation")
private ArrayList<Parcelable> getSelectedItems(Bundle bundle){
if(Build.VERSION.SDK_INT >= 33){
private ArrayList<Parcelable> getSelectedItems(Bundle bundle) {
if (Build.VERSION.SDK_INT >= 33) {
return bundle.getParcelableArrayList("selectedItems", Parcelable.class);
}

Expand Down Expand Up @@ -315,7 +316,7 @@ public void startFileExplorer(final String type, final boolean isMultipleSelecti
this.isMultipleSelection = isMultipleSelection;
this.loadDataToMemory = withData;
this.allowedExtensions = allowedExtensions;
this.compressionQuality=compressionQuality;
this.compressionQuality = compressionQuality;
// `READ_EXTERNAL_STORAGE` permission is not needed since SDK 33 (Android 13 or higher).
// `READ_EXTERNAL_STORAGE` & `WRITE_EXTERNAL_STORAGE` are no longer meant to be used, but classified into granular types.
// Reference: https://developer.android.com/about/versions/13/behavior-changes-13
Expand Down Expand Up @@ -370,7 +371,7 @@ private void finishWithSuccess(Object data) {
if (data != null && !(data instanceof String)) {
final ArrayList<HashMap<String, Object>> files = new ArrayList<>();

for (FileInfo file : (ArrayList<FileInfo>)data) {
for (FileInfo file : (ArrayList<FileInfo>) data) {
files.add(file.toMap());
}
data = files;
Expand All @@ -393,7 +394,7 @@ private void finishWithError(final String errorCode, final String errorMessage)

private void dispatchEventStatus(final boolean status) {

if(eventSink == null || type.equals("dir")) {
if (eventSink == null || type.equals("dir")) {
return;
}

Expand Down

0 comments on commit 0bcc22d

Please sign in to comment.