what-the-bank/sources/io/flutter/plugins/imagepicker/ImagePickerDelegate.java

551 lines
23 KiB
Java

package io.flutter.plugins.imagepicker;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageItemInfo;
import android.content.pm.ResolveInfo;
import android.media.MediaScannerConnection;
import android.net.Uri;
import androidx.core.app.ActivityCompat;
import com.huawei.hms.support.api.entity.common.CommonConstant;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import o.C9257dgM;
/* loaded from: classes6.dex */
public class ImagePickerDelegate implements PluginRegistry.ActivityResultListener, PluginRegistry.RequestPermissionsResultListener {
static final int REQUEST_CAMERA_IMAGE_PERMISSION = 2345;
static final int REQUEST_CAMERA_VIDEO_PERMISSION = 2355;
static final int REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY = 2342;
static final int REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY = 2346;
static final int REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY = 2352;
static final int REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA = 2343;
static final int REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA = 2353;
private final Activity activity;
private final ImagePickerCache cache;
private CameraDevice cameraDevice;
final File externalFilesDirectory;
final String fileProviderName;
private final FileUriResolver fileUriResolver;
private final FileUtils fileUtils;
private final ImageResizer imageResizer;
private MethodCall methodCall;
private Uri pendingCameraMediaUri;
private MethodChannel.Result pendingResult;
private final PermissionManager permissionManager;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public interface FileUriResolver {
void getFullImagePath(Uri uri, OnPathReadyListener onPathReadyListener);
Uri resolveFileProviderUriForFile(String str, File file);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public interface OnPathReadyListener {
void onPathReady(String str);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public interface PermissionManager {
void askForPermission(String str, int i);
boolean isPermissionGranted(String str);
boolean needRequestCameraPermission();
}
public ImagePickerDelegate(Activity activity, File file, ImageResizer imageResizer, ImagePickerCache imagePickerCache) {
this(activity, file, imageResizer, null, null, imagePickerCache, new PermissionManager(activity) { // from class: io.flutter.plugins.imagepicker.ImagePickerDelegate.1
final Activity val$activity;
{
this.val$activity = activity;
}
@Override // io.flutter.plugins.imagepicker.ImagePickerDelegate.PermissionManager
public boolean isPermissionGranted(String str) {
return ActivityCompat.checkSelfPermission(this.val$activity, str) == 0;
}
@Override // io.flutter.plugins.imagepicker.ImagePickerDelegate.PermissionManager
public void askForPermission(String str, int i) {
ActivityCompat.lM_(this.val$activity, new String[]{str}, i);
}
@Override // io.flutter.plugins.imagepicker.ImagePickerDelegate.PermissionManager
public boolean needRequestCameraPermission() {
return ImagePickerUtils.needRequestCameraPermission(this.val$activity);
}
}, new FileUriResolver(activity) { // from class: io.flutter.plugins.imagepicker.ImagePickerDelegate.2
final Activity val$activity;
{
this.val$activity = activity;
}
@Override // io.flutter.plugins.imagepicker.ImagePickerDelegate.FileUriResolver
public Uri resolveFileProviderUriForFile(String str, File file2) {
return C9257dgM.getUriForFile(this.val$activity, str, file2);
}
@Override // io.flutter.plugins.imagepicker.ImagePickerDelegate.FileUriResolver
public void getFullImagePath(Uri uri, OnPathReadyListener onPathReadyListener) {
Activity activity2 = this.val$activity;
String[] strArr = new String[1];
strArr[0] = uri != null ? uri.getPath() : "";
MediaScannerConnection.scanFile(activity2, strArr, null, new MediaScannerConnection.OnScanCompletedListener(this, onPathReadyListener) { // from class: io.flutter.plugins.imagepicker.ImagePickerDelegate.2.1
final AnonymousClass2 this$1;
final OnPathReadyListener val$listener;
{
this.this$1 = this;
this.val$listener = onPathReadyListener;
}
@Override // android.media.MediaScannerConnection.OnScanCompletedListener
public void onScanCompleted(String str, Uri uri2) {
this.val$listener.onPathReady(str);
}
});
}
}, new FileUtils());
}
ImagePickerDelegate(Activity activity, File file, ImageResizer imageResizer, MethodChannel.Result result, MethodCall methodCall, ImagePickerCache imagePickerCache, PermissionManager permissionManager, FileUriResolver fileUriResolver, FileUtils fileUtils) {
this.activity = activity;
this.externalFilesDirectory = file;
this.imageResizer = imageResizer;
StringBuilder sb = new StringBuilder();
sb.append(activity.getPackageName());
sb.append(".flutter.image_provider");
this.fileProviderName = sb.toString();
this.pendingResult = result;
this.methodCall = methodCall;
this.permissionManager = permissionManager;
this.fileUriResolver = fileUriResolver;
this.fileUtils = fileUtils;
this.cache = imagePickerCache;
}
/* JADX INFO: Access modifiers changed from: package-private */
public void saveStateBeforeResult() {
MethodCall methodCall = this.methodCall;
if (methodCall == null) {
return;
}
this.cache.saveTypeWithMethodCallName(methodCall.method);
this.cache.saveDimensionWithMethodCall(this.methodCall);
Uri uri = this.pendingCameraMediaUri;
if (uri != null) {
this.cache.savePendingCameraMediaUriPath(uri);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public void retrieveLostImage(MethodChannel.Result result) {
Map<String, Object> cacheMap = this.cache.getCacheMap();
ArrayList arrayList = (ArrayList) cacheMap.get("pathList");
ArrayList arrayList2 = new ArrayList();
if (arrayList != null) {
Iterator it = arrayList.iterator();
while (it.hasNext()) {
arrayList2.add(this.imageResizer.resizeImageIfNeeded((String) it.next(), (Double) cacheMap.get("maxWidth"), (Double) cacheMap.get("maxHeight"), Integer.valueOf(cacheMap.get("imageQuality") == null ? 100 : ((Integer) cacheMap.get("imageQuality")).intValue())));
}
cacheMap.put("pathList", arrayList2);
cacheMap.put("path", arrayList2.get(arrayList2.size() - 1));
}
if (cacheMap.isEmpty()) {
result.success(null);
} else {
result.success(cacheMap);
}
this.cache.clear();
}
public void chooseVideoFromGallery(MethodCall methodCall, MethodChannel.Result result) {
if (!setPendingMethodCallAndResult(methodCall, result)) {
finishWithAlreadyActiveError(result);
} else {
launchPickVideoFromGalleryIntent();
}
}
private void launchPickVideoFromGalleryIntent() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("video/*");
this.activity.startActivityForResult(intent, REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY);
}
public void takeVideoWithCamera(MethodCall methodCall, MethodChannel.Result result) {
if (!setPendingMethodCallAndResult(methodCall, result)) {
finishWithAlreadyActiveError(result);
} else if (needRequestCameraPermission() && !this.permissionManager.isPermissionGranted("android.permission.CAMERA")) {
this.permissionManager.askForPermission("android.permission.CAMERA", REQUEST_CAMERA_VIDEO_PERMISSION);
} else {
launchTakeVideoWithCameraIntent();
}
}
private void launchTakeVideoWithCameraIntent() {
Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
MethodCall methodCall = this.methodCall;
if (methodCall != null && methodCall.argument("maxDuration") != null) {
intent.putExtra("android.intent.extra.durationLimit", ((Integer) this.methodCall.argument("maxDuration")).intValue());
}
if (this.cameraDevice == CameraDevice.FRONT) {
useFrontCamera(intent);
}
File createTemporaryWritableVideoFile = createTemporaryWritableVideoFile();
StringBuilder sb = new StringBuilder("file:");
sb.append(createTemporaryWritableVideoFile.getAbsolutePath());
this.pendingCameraMediaUri = Uri.parse(sb.toString());
Uri resolveFileProviderUriForFile = this.fileUriResolver.resolveFileProviderUriForFile(this.fileProviderName, createTemporaryWritableVideoFile);
intent.putExtra("output", resolveFileProviderUriForFile);
grantUriPermissions(intent, resolveFileProviderUriForFile);
try {
try {
this.activity.startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA);
} catch (SecurityException unused) {
finishWithError("no_available_camera", "No cameras available for taking pictures.");
}
} catch (ActivityNotFoundException unused2) {
createTemporaryWritableVideoFile.delete();
finishWithError("no_available_camera", "No cameras available for taking pictures.");
}
}
public void chooseImageFromGallery(MethodCall methodCall, MethodChannel.Result result) {
if (!setPendingMethodCallAndResult(methodCall, result)) {
finishWithAlreadyActiveError(result);
} else {
launchPickImageFromGalleryIntent();
}
}
public void chooseMultiImageFromGallery(MethodCall methodCall, MethodChannel.Result result) {
if (!setPendingMethodCallAndResult(methodCall, result)) {
finishWithAlreadyActiveError(result);
} else {
launchMultiPickImageFromGalleryIntent();
}
}
private void launchPickImageFromGalleryIntent() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
this.activity.startActivityForResult(intent, REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY);
}
private void launchMultiPickImageFromGalleryIntent() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.putExtra("android.intent.extra.ALLOW_MULTIPLE", true);
intent.setType("image/*");
this.activity.startActivityForResult(intent, REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY);
}
public void takeImageWithCamera(MethodCall methodCall, MethodChannel.Result result) {
if (!setPendingMethodCallAndResult(methodCall, result)) {
finishWithAlreadyActiveError(result);
} else if (needRequestCameraPermission() && !this.permissionManager.isPermissionGranted("android.permission.CAMERA")) {
this.permissionManager.askForPermission("android.permission.CAMERA", REQUEST_CAMERA_IMAGE_PERMISSION);
} else {
launchTakeImageWithCameraIntent();
}
}
private boolean needRequestCameraPermission() {
PermissionManager permissionManager = this.permissionManager;
if (permissionManager == null) {
return false;
}
return permissionManager.needRequestCameraPermission();
}
private void launchTakeImageWithCameraIntent() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
if (this.cameraDevice == CameraDevice.FRONT) {
useFrontCamera(intent);
}
File createTemporaryWritableImageFile = createTemporaryWritableImageFile();
StringBuilder sb = new StringBuilder("file:");
sb.append(createTemporaryWritableImageFile.getAbsolutePath());
this.pendingCameraMediaUri = Uri.parse(sb.toString());
Uri resolveFileProviderUriForFile = this.fileUriResolver.resolveFileProviderUriForFile(this.fileProviderName, createTemporaryWritableImageFile);
intent.putExtra("output", resolveFileProviderUriForFile);
grantUriPermissions(intent, resolveFileProviderUriForFile);
try {
try {
this.activity.startActivityForResult(intent, REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA);
} catch (ActivityNotFoundException unused) {
createTemporaryWritableImageFile.delete();
finishWithError("no_available_camera", "No cameras available for taking pictures.");
}
} catch (SecurityException unused2) {
finishWithError("no_available_camera", "No cameras available for taking pictures.");
}
}
private File createTemporaryWritableImageFile() {
return createTemporaryWritableFile(".jpg");
}
private File createTemporaryWritableVideoFile() {
return createTemporaryWritableFile(".mp4");
}
private File createTemporaryWritableFile(String str) {
String obj = UUID.randomUUID().toString();
try {
this.externalFilesDirectory.mkdirs();
return File.createTempFile(obj, str, this.externalFilesDirectory);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void grantUriPermissions(Intent intent, Uri uri) {
for (ResolveInfo resolveInfo : this.activity.getPackageManager().queryIntentActivities(intent, 65536)) {
this.activity.grantUriPermission((String) PackageItemInfo.class.getField(CommonConstant.ReqAccessTokenParam.PACKAGE_NAME).get(resolveInfo.activityInfo), uri, 3);
}
}
@Override // io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
public boolean onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
boolean z = iArr.length > 0 && iArr[0] == 0;
if (i != REQUEST_CAMERA_IMAGE_PERMISSION) {
if (i != REQUEST_CAMERA_VIDEO_PERMISSION) {
return false;
}
if (z) {
launchTakeVideoWithCameraIntent();
}
} else if (z) {
launchTakeImageWithCameraIntent();
}
if (!z && (i == REQUEST_CAMERA_IMAGE_PERMISSION || i == REQUEST_CAMERA_VIDEO_PERMISSION)) {
finishWithError("camera_access_denied", "The user did not allow camera access.");
}
return true;
}
@Override // io.flutter.plugin.common.PluginRegistry.ActivityResultListener
public boolean onActivityResult(int i, int i2, Intent intent) {
if (i == REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY) {
handleChooseImageResult(i2, intent);
return true;
}
if (i == REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA) {
handleCaptureImageResult(i2);
return true;
}
if (i == REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY) {
handleChooseMultiImageResult(i2, intent);
return true;
}
if (i == REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY) {
handleChooseVideoResult(i2, intent);
return true;
}
if (i != REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA) {
return false;
}
handleCaptureVideoResult(i2);
return true;
}
private void handleChooseImageResult(int i, Intent intent) {
if (i == -1 && intent != null) {
handleImageResult(this.fileUtils.getPathFromUri(this.activity, intent.getData()), false);
} else {
finishWithSuccess(null);
}
}
private void handleChooseMultiImageResult(int i, Intent intent) {
if (i == -1 && intent != null) {
ArrayList<String> arrayList = new ArrayList<>();
if (intent.getClipData() != null) {
for (int i2 = 0; i2 < intent.getClipData().getItemCount(); i2++) {
arrayList.add(this.fileUtils.getPathFromUri(this.activity, intent.getClipData().getItemAt(i2).getUri()));
}
} else {
arrayList.add(this.fileUtils.getPathFromUri(this.activity, intent.getData()));
}
handleMultiImageResult(arrayList, false);
return;
}
finishWithSuccess(null);
}
private void handleChooseVideoResult(int i, Intent intent) {
if (i == -1 && intent != null) {
handleVideoResult(this.fileUtils.getPathFromUri(this.activity, intent.getData()));
} else {
finishWithSuccess(null);
}
}
private void handleCaptureImageResult(int i) {
if (i == -1) {
FileUriResolver fileUriResolver = this.fileUriResolver;
Uri uri = this.pendingCameraMediaUri;
if (uri == null) {
uri = Uri.parse(this.cache.retrievePendingCameraMediaUriPath());
}
fileUriResolver.getFullImagePath(uri, new OnPathReadyListener(this) { // from class: io.flutter.plugins.imagepicker.ImagePickerDelegate.3
final ImagePickerDelegate this$0;
{
this.this$0 = this;
}
@Override // io.flutter.plugins.imagepicker.ImagePickerDelegate.OnPathReadyListener
public void onPathReady(String str) {
this.this$0.handleImageResult(str, true);
}
});
return;
}
finishWithSuccess(null);
}
private void handleCaptureVideoResult(int i) {
if (i == -1) {
FileUriResolver fileUriResolver = this.fileUriResolver;
Uri uri = this.pendingCameraMediaUri;
if (uri == null) {
uri = Uri.parse(this.cache.retrievePendingCameraMediaUriPath());
}
fileUriResolver.getFullImagePath(uri, new OnPathReadyListener(this) { // from class: io.flutter.plugins.imagepicker.ImagePickerDelegate.4
final ImagePickerDelegate this$0;
{
this.this$0 = this;
}
@Override // io.flutter.plugins.imagepicker.ImagePickerDelegate.OnPathReadyListener
public void onPathReady(String str) {
this.this$0.handleVideoResult(str);
}
});
return;
}
finishWithSuccess(null);
}
private void handleMultiImageResult(ArrayList<String> arrayList, boolean z) {
if (this.methodCall != null) {
ArrayList<String> arrayList2 = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
String resizedImagePath = getResizedImagePath(arrayList.get(i));
if (resizedImagePath != null && !resizedImagePath.equals(arrayList.get(i)) && z) {
new File(arrayList.get(i)).delete();
}
arrayList2.add(i, resizedImagePath);
}
finishWithListSuccess(arrayList2);
return;
}
finishWithListSuccess(arrayList);
}
/* JADX INFO: Access modifiers changed from: private */
public void handleImageResult(String str, boolean z) {
if (this.methodCall != null) {
String resizedImagePath = getResizedImagePath(str);
if (resizedImagePath != null && !resizedImagePath.equals(str) && z) {
new File(str).delete();
}
finishWithSuccess(resizedImagePath);
return;
}
finishWithSuccess(str);
}
private String getResizedImagePath(String str) {
return this.imageResizer.resizeImageIfNeeded(str, (Double) this.methodCall.argument("maxWidth"), (Double) this.methodCall.argument("maxHeight"), (Integer) this.methodCall.argument("imageQuality"));
}
/* JADX INFO: Access modifiers changed from: private */
public void handleVideoResult(String str) {
finishWithSuccess(str);
}
private boolean setPendingMethodCallAndResult(MethodCall methodCall, MethodChannel.Result result) {
if (this.pendingResult != null) {
return false;
}
this.methodCall = methodCall;
this.pendingResult = result;
this.cache.clear();
return true;
}
private void finishWithSuccess(String str) {
MethodChannel.Result result = this.pendingResult;
if (result != null) {
result.success(str);
clearMethodCallAndResult();
} else if (str != null) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add(str);
this.cache.saveResult(arrayList, null, null);
}
}
private void finishWithListSuccess(ArrayList<String> arrayList) {
MethodChannel.Result result = this.pendingResult;
if (result == null) {
this.cache.saveResult(arrayList, null, null);
} else {
result.success(arrayList);
clearMethodCallAndResult();
}
}
private void finishWithAlreadyActiveError(MethodChannel.Result result) {
result.error("already_active", "Image picker is already active", null);
}
private void finishWithError(String str, String str2) {
MethodChannel.Result result = this.pendingResult;
if (result == null) {
this.cache.saveResult(null, str, str2);
} else {
result.error(str, str2, null);
clearMethodCallAndResult();
}
}
private void useFrontCamera(Intent intent) {
intent.putExtra("android.intent.extras.CAMERA_FACING", 0);
intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
}
/* JADX INFO: Access modifiers changed from: package-private */
public void setCameraDevice(CameraDevice cameraDevice) {
this.cameraDevice = cameraDevice;
}
CameraDevice getCameraDevice() {
return this.cameraDevice;
}
private void clearMethodCallAndResult() {
this.methodCall = null;
this.pendingResult = null;
}
}