Skip to content

Commit

Permalink
Revert "Fix: Preserve selection order in Android image and video picker"
Browse files Browse the repository at this point in the history
This reverts commit 1c2d5d0.
  • Loading branch information
sean5940 committed Oct 21, 2024
1 parent e35a88b commit f14f6d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import android.Manifest;
import android.app.Activity;
import android.content.ClipData;
import android.content.ContentResolver;
import android.content.Context;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
Expand All @@ -13,15 +13,12 @@
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.webkit.MimeTypeMap;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;

Expand All @@ -43,8 +40,8 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -56,6 +53,7 @@
import java.util.concurrent.Callable;



class PickerModule extends ReactContextBaseJavaModule implements ActivityEventListener {

private static final int IMAGE_PICKER_REQUEST = 61110;
Expand Down Expand Up @@ -236,7 +234,7 @@ private void permissionsCheck(final Activity activity, final Promise promise, fi
List<String> supportedPermissions = new ArrayList<>(requiredPermissions);

// android 11 introduced scoped storage, and WRITE_EXTERNAL_STORAGE no longer works there
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
supportedPermissions.remove(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}

Expand Down Expand Up @@ -494,7 +492,7 @@ private WritableMap getSelection(Activity activity, Uri uri, boolean isCamera) t
return getImage(activity, path);
}

private void getAsyncSelection(final Activity activity, Uri uri, boolean isCamera, int index) throws Exception {
private void getAsyncSelection(final Activity activity, Uri uri, boolean isCamera) throws Exception {
String path = resolveRealPath(activity, uri, isCamera);
if (path == null || path.isEmpty()) {
resultCollector.notifyProblem(E_NO_IMAGE_DATA_FOUND, "Cannot resolve asset path.");
Expand All @@ -503,15 +501,11 @@ private void getAsyncSelection(final Activity activity, Uri uri, boolean isCamer

String mime = getMimeType(path);
if (mime != null && mime.startsWith("video/")) {
getVideo(activity, path, mime, index);
getVideo(activity, path, mime);
return;
}

resultCollector.notifySuccess(getImage(activity, path), index);
}

private void getAsyncSelection(final Activity activity, Uri uri, boolean isCamera) throws Exception {
this.getAsyncSelection(activity, uri, isCamera, 0);
resultCollector.notifySuccess(getImage(activity, path));
}

private Bitmap validateVideo(String path) throws Exception {
Expand All @@ -538,7 +532,7 @@ private static Long getVideoDuration(String path) {
}
}

private void getVideo(final Activity activity, final String path, final String mime, int index) throws Exception {
private void getVideo(final Activity activity, final String path, final String mime) throws Exception {
validateVideo(path);
final String compressedVideoPath = getTmpDir(activity) + "/" + UUID.randomUUID().toString() + ".mp4";

Expand All @@ -564,7 +558,7 @@ public void invoke(Object... args) {
video.putString("path", "file://" + videoPath);
video.putString("modificationDate", String.valueOf(modificationDate));

resultCollector.notifySuccess(video, index);
resultCollector.notifySuccess(video);
} catch (Exception e) {
resultCollector.notifyProblem(E_NO_IMAGE_DATA_FOUND, e);
}
Expand All @@ -580,10 +574,6 @@ public void invoke(Object... args) {
}).run();
}

private void getVideo(final Activity activity, final String path, final String mime) throws Exception {
this.getVideo(activity, path, mime, 0);
}

private String resolveRealPath(Activity activity, Uri uri, boolean isCamera) throws IOException {
String path;

Expand Down Expand Up @@ -790,7 +780,7 @@ private void imagePickerResult(Activity activity, final int requestCode, final i
} else {
resultCollector.setWaitCount(clipData.getItemCount());
for (int i = 0; i < clipData.getItemCount(); i++) {
getAsyncSelection(activity, clipData.getItemAt(i).getUri(), false, i);
getAsyncSelection(activity, clipData.getItemAt(i).getUri(), false);
}
}
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
Expand All @@ -24,8 +21,6 @@ class ResultCollector {
private WritableArray arrayResult;
private boolean resultSent;

private List<WritableMap> tempList;

synchronized void setup(Promise promise, boolean multiple) {
this.promise = promise;
this.multiple = multiple;
Expand All @@ -36,7 +31,6 @@ synchronized void setup(Promise promise, boolean multiple) {

if (multiple) {
this.arrayResult = new WritableNativeArray();
this.tempList = new ArrayList<>();
}
}

Expand All @@ -61,19 +55,16 @@ synchronized private boolean isRequestValid() {
return true;
}

synchronized void notifySuccess(WritableMap result, int index) {
synchronized void notifySuccess(WritableMap result) {
if (!isRequestValid()) {
return;
}

if (multiple) {
tempList.add(index, result);
arrayResult.pushMap(result);
int currentCount = waitCounter.addAndGet(1);

if (currentCount == waitCount) {
for(WritableMap item : tempList) {
arrayResult.pushMap(item);
}
promise.resolve(arrayResult);
resultSent = true;
}
Expand All @@ -83,10 +74,6 @@ synchronized void notifySuccess(WritableMap result, int index) {
}
}

synchronized void notifySuccess(WritableMap result) {
notifySuccess(result,0);
}

synchronized void notifyProblem(String code, String message) {
if (!isRequestValid()) {
return;
Expand Down

0 comments on commit f14f6d9

Please sign in to comment.