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

[image_picker] Fix Android lints #3887

Merged
merged 4 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.6+12

* Fixes Java warnings.

## 0.8.6+11

* Updates gradle to 7.6.1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ android {
checkAllWarnings true
warningsAsErrors true
disable 'AndroidGradlePluginVersion', 'InvalidPackage', 'GradleDependency'
baseline file("lint-baseline.xml")
}
dependencies {
implementation 'androidx.core:core:1.9.0'
Expand Down
400 changes: 0 additions & 400 deletions packages/image_picker/image_picker_android/android/lint-baseline.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static class PendingCallState {
public final @Nullable VideoSelectionOptions videoOptions;
public final @NonNull Messages.Result<List<String>> result;

private PendingCallState(
PendingCallState(
@Nullable ImageSelectionOptions imageOptions,
@Nullable VideoSelectionOptions videoOptions,
@NonNull Messages.Result<List<String>> result) {
Expand All @@ -107,10 +107,10 @@ private PendingCallState(

@VisibleForTesting final String fileProviderName;

private final Activity activity;
@VisibleForTesting final File externalFilesDirectory;
private final ImageResizer imageResizer;
private final ImagePickerCache cache;
private final @NonNull Activity activity;
@VisibleForTesting final @NonNull File externalFilesDirectory;
private final @NonNull ImageResizer imageResizer;
private final @NonNull ImagePickerCache cache;
private final PermissionManager permissionManager;
private final FileUriResolver fileUriResolver;
private final FileUtils fileUtils;
Expand Down Expand Up @@ -140,10 +140,10 @@ interface OnPathReadyListener {
private final Object pendingCallStateLock = new Object();

public ImagePickerDelegate(
final Activity activity,
final File externalFilesDirectory,
final ImageResizer imageResizer,
final ImagePickerCache cache) {
final @NonNull Activity activity,
final @NonNull File externalFilesDirectory,
final @NonNull ImageResizer imageResizer,
final @NonNull ImagePickerCache cache) {
this(
activity,
externalFilesDirectory,
Expand Down Expand Up @@ -181,12 +181,7 @@ public void getFullImagePath(final Uri imageUri, final OnPathReadyListener liste
activity,
new String[] {(imageUri != null) ? imageUri.getPath() : ""},
null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
listener.onPathReady(path);
}
});
(path, uri) -> listener.onPathReady(path));
}
},
new FileUtils(),
Expand All @@ -199,13 +194,13 @@ public void onScanCompleted(String path, Uri uri) {
*/
@VisibleForTesting
ImagePickerDelegate(
final Activity activity,
final File externalFilesDirectory,
final ImageResizer imageResizer,
final @NonNull Activity activity,
final @NonNull File externalFilesDirectory,
final @NonNull ImageResizer imageResizer,
final @Nullable ImageSelectionOptions pendingImageOptions,
final @Nullable VideoSelectionOptions pendingVideoOptions,
final @Nullable Messages.Result<List<String>> result,
final ImagePickerCache cache,
final @NonNull ImagePickerCache cache,
final PermissionManager permissionManager,
final FileUriResolver fileUriResolver,
final FileUtils fileUtils,
Expand Down Expand Up @@ -290,7 +285,9 @@ Messages.CacheRetrievalResult retrieveLostImage() {
}

public void chooseVideoFromGallery(
VideoSelectionOptions options, boolean usePhotoPicker, Messages.Result<List<String>> result) {
@NonNull VideoSelectionOptions options,
boolean usePhotoPicker,
@NonNull Messages.Result<List<String>> result) {
if (!setPendingOptionsAndResult(null, options, result)) {
finishWithAlreadyActiveError(result);
return;
Expand Down Expand Up @@ -318,7 +315,7 @@ private void launchPickVideoFromGalleryIntent(Boolean useAndroidPhotoPicker) {
}

public void takeVideoWithCamera(
VideoSelectionOptions options, Messages.Result<List<String>> result) {
@NonNull VideoSelectionOptions options, @NonNull Messages.Result<List<String>> result) {
if (!setPendingOptionsAndResult(null, options, result)) {
finishWithAlreadyActiveError(result);
return;
Expand Down Expand Up @@ -376,7 +373,7 @@ private void launchTakeVideoWithCameraIntent() {
public void chooseImageFromGallery(
@NonNull ImageSelectionOptions options,
boolean usePhotoPicker,
Messages.Result<List<String>> result) {
@NonNull Messages.Result<List<String>> result) {
if (!setPendingOptionsAndResult(options, null, result)) {
finishWithAlreadyActiveError(result);
return;
Expand All @@ -388,7 +385,7 @@ public void chooseImageFromGallery(
public void chooseMultiImageFromGallery(
@NonNull ImageSelectionOptions options,
boolean usePhotoPicker,
Messages.Result<List<String>> result) {
@NonNull Messages.Result<List<String>> result) {
if (!setPendingOptionsAndResult(options, null, result)) {
finishWithAlreadyActiveError(result);
return;
Expand Down Expand Up @@ -436,7 +433,7 @@ private void launchMultiPickImageFromGalleryIntent(Boolean useAndroidPhotoPicker
}

public void takeImageWithCamera(
@NonNull ImageSelectionOptions options, Messages.Result<List<String>> result) {
@NonNull ImageSelectionOptions options, @NonNull Messages.Result<List<String>> result) {
if (!setPendingOptionsAndResult(options, null, result)) {
finishWithAlreadyActiveError(result);
return;
Expand Down Expand Up @@ -556,7 +553,8 @@ public boolean onRequestPermissionsResult(
}

@Override
public boolean onActivityResult(final int requestCode, final int resultCode, final Intent data) {
public boolean onActivityResult(
final int requestCode, final int resultCode, final @Nullable Intent data) {
Runnable handlerRunnable;

switch (requestCode) {
Expand Down Expand Up @@ -605,7 +603,7 @@ private void handleChooseMultiImageResult(int resultCode, Intent intent) {
} else {
paths.add(fileUtils.getPathFromUri(activity, intent.getData()));
}
handleMultiImageResult(paths, false);
handleMultiImageResult(paths);
return;
}

Expand All @@ -632,12 +630,7 @@ private void handleCaptureImageResult(int resultCode) {
localPendingCameraMediaUri != null
? localPendingCameraMediaUri
: Uri.parse(cache.retrievePendingCameraMediaUriPath()),
new OnPathReadyListener() {
@Override
public void onPathReady(String path) {
handleImageResult(path, true);
}
});
path -> handleImageResult(path, true));
return;
}

Expand All @@ -652,21 +645,15 @@ private void handleCaptureVideoResult(int resultCode) {
localPendingCameraMediaUrl != null
? localPendingCameraMediaUrl
: Uri.parse(cache.retrievePendingCameraMediaUriPath()),
new OnPathReadyListener() {
@Override
public void onPathReady(String path) {
handleVideoResult(path);
}
});
this::handleVideoResult);
return;
}

// User cancelled taking a picture.
finishWithSuccess(null);
}

private void handleMultiImageResult(
ArrayList<String> paths, boolean shouldDeleteOriginalIfScaled) {
private void handleMultiImageResult(ArrayList<String> paths) {
ImageSelectionOptions localImageOptions = null;
synchronized (pendingCallStateLock) {
if (pendingCallState != null) {
Expand All @@ -678,13 +665,6 @@ private void handleMultiImageResult(
ArrayList<String> finalPath = new ArrayList<>();
for (int i = 0; i < paths.size(); i++) {
String finalImagePath = getResizedImagePath(paths.get(i), localImageOptions);

//delete original file if scaled
if (finalImagePath != null
&& !finalImagePath.equals(paths.get(i))
&& shouldDeleteOriginalIfScaled) {
new File(paths.get(i)).delete();
}
finalPath.add(i, finalImagePath);
}
finishWithListSuccess(finalPath);
Expand All @@ -693,7 +673,7 @@ private void handleMultiImageResult(
}
}

private void handleImageResult(String path, boolean shouldDeleteOriginalIfScaled) {
void handleImageResult(String path, boolean shouldDeleteOriginalIfScaled) {
ImageSelectionOptions localImageOptions = null;
synchronized (pendingCallStateLock) {
if (pendingCallState != null) {
Expand Down Expand Up @@ -721,7 +701,7 @@ private String getResizedImagePath(String path, @NonNull ImageSelectionOptions o
outputOptions.getQuality().intValue());
}

private void handleVideoResult(String path) {
void handleVideoResult(String path) {
finishWithSuccess(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,18 @@ ImagePickerDelegate getDelegate() {
}

private FlutterPluginBinding pluginBinding;
private ActivityState activityState;
ActivityState activityState;

@SuppressWarnings("deprecation")
public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registrar registrar) {
public static void registerWith(
@NonNull io.flutter.plugin.common.PluginRegistry.Registrar registrar) {
if (registrar.activity() == null) {
// If a background flutter view tries to register the plugin, there will be no activity from the registrar,
// we stop the registering process immediately because the ImagePicker requires an activity.
return;
}
Activity activity = registrar.activity();
Application application = null;
if (registrar.context() != null) {
application = (Application) (registrar.context().getApplicationContext());
}
Application application = (Application) (registrar.context().getApplicationContext());
ImagePickerPlugin plugin = new ImagePickerPlugin();
plugin.setup(registrar.messenger(), application, activity, registrar, null);
}
Expand Down Expand Up @@ -305,7 +303,7 @@ public void pickImages(
@NonNull Messages.ImageSelectionOptions options,
@NonNull Boolean allowMultiple,
@NonNull Boolean usePhotoPicker,
Result<List<String>> result) {
@NonNull Result<List<String>> result) {
ImagePickerDelegate delegate = getImagePickerDelegate();
if (delegate == null) {
result.error(
Expand Down Expand Up @@ -335,7 +333,7 @@ public void pickVideos(
@NonNull Messages.VideoSelectionOptions options,
@NonNull Boolean allowMultiple,
@NonNull Boolean usePhotoPicker,
Result<List<String>> result) {
@NonNull Result<List<String>> result) {
ImagePickerDelegate delegate = getImagePickerDelegate();
if (delegate == null) {
result.error(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Autogenerated from Pigeon (v9.1.0), do not edit directly.
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package io.flutter.plugins.imagepicker;
Expand Down Expand Up @@ -39,7 +39,7 @@ public FlutterError(@NonNull String code, @Nullable String message, @Nullable Ob
}

@NonNull
private static ArrayList<Object> wrapError(@NonNull Throwable exception) {
protected static ArrayList<Object> wrapError(@NonNull Throwable exception) {
ArrayList<Object> errorList = new ArrayList<Object>(3);
if (exception instanceof FlutterError) {
FlutterError error = (FlutterError) exception;
Expand All @@ -59,7 +59,7 @@ public enum SourceCamera {
REAR(0),
FRONT(1);

private final int index;
final int index;

private SourceCamera(final int index) {
this.index = index;
Expand All @@ -70,7 +70,7 @@ public enum SourceType {
CAMERA(0),
GALLERY(1);

private final int index;
final int index;

private SourceType(final int index) {
this.index = index;
Expand All @@ -81,7 +81,7 @@ public enum CacheRetrievalType {
IMAGE(0),
VIDEO(1);

private final int index;
final int index;

private CacheRetrievalType(final int index) {
this.index = index;
Expand Down Expand Up @@ -134,8 +134,8 @@ public void setQuality(@NonNull Long setterArg) {
this.quality = setterArg;
}

/** Constructor is private to enforce null safety; use Builder. */
private ImageSelectionOptions() {}
/** Constructor is non-public to enforce null safety; use Builder. */
ImageSelectionOptions() {}

public static final class Builder {

Expand Down Expand Up @@ -275,8 +275,8 @@ public void setCamera(@Nullable SourceCamera setterArg) {
this.camera = setterArg;
}

/** Constructor is private to enforce null safety; use Builder. */
private SourceSpecification() {}
/** Constructor is non-public to enforce null safety; use Builder. */
SourceSpecification() {}

public static final class Builder {

Expand Down Expand Up @@ -351,8 +351,8 @@ public void setMessage(@Nullable String setterArg) {
this.message = setterArg;
}

/** Constructor is private to enforce null safety; use Builder. */
private CacheRetrievalError() {}
/** Constructor is non-public to enforce null safety; use Builder. */
CacheRetrievalError() {}

public static final class Builder {

Expand Down Expand Up @@ -446,8 +446,8 @@ public void setPaths(@NonNull List<String> setterArg) {
this.paths = setterArg;
}

/** Constructor is private to enforce null safety; use Builder. */
private CacheRetrievalResult() {}
/** Constructor is non-public to enforce null safety; use Builder. */
CacheRetrievalResult() {}

public static final class Builder {

Expand Down Expand Up @@ -504,9 +504,10 @@ ArrayList<Object> toList() {
}

public interface Result<T> {
@SuppressWarnings("UnknownNullness")
void success(T result);

void error(Throwable error);
void error(@NonNull Throwable error);
}

private static class ImagePickerApiCodec extends StandardMessageCodec {
Expand Down Expand Up @@ -568,7 +569,7 @@ void pickImages(
@NonNull ImageSelectionOptions options,
@NonNull Boolean allowMultiple,
@NonNull Boolean usePhotoPicker,
Result<List<String>> result);
@NonNull Result<List<String>> result);
/**
* Selects video and returns their paths.
*
Expand All @@ -580,17 +581,17 @@ void pickVideos(
@NonNull VideoSelectionOptions options,
@NonNull Boolean allowMultiple,
@NonNull Boolean usePhotoPicker,
Result<List<String>> result);
@NonNull Result<List<String>> result);
/** Returns results from a previous app session, if any. */
@Nullable
CacheRetrievalResult retrieveLostResults();

/** The codec used by ImagePickerApi. */
static MessageCodec<Object> getCodec() {
static @NonNull MessageCodec<Object> getCodec() {
return ImagePickerApiCodec.INSTANCE;
}
/** Sets up an instance of `ImagePickerApi` to handle messages through the `binaryMessenger`. */
static void setup(BinaryMessenger binaryMessenger, ImagePickerApi api) {
static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable ImagePickerApi api) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
Expand Down
Loading