Skip to content

Commit 329f83c

Browse files
[image_picker] Fix Android lints (#3887)
Removes the lint-baseline.xml and fixes the resulting issues. Also does some Andoid-Studio-suggested auto-conversions of anonymous classes to lambdas. Part of flutter/flutter#88011
1 parent 7319ca8 commit 329f83c

File tree

9 files changed

+79
-486
lines changed

9 files changed

+79
-486
lines changed

packages/image_picker/image_picker_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.6+14
2+
3+
* Fixes Java warnings.
4+
15
## 0.8.6+13
26

37
* Fixes `BuildContext` handling in example.

packages/image_picker/image_picker_android/android/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ android {
3636
checkAllWarnings true
3737
warningsAsErrors true
3838
disable 'AndroidGradlePluginVersion', 'InvalidPackage', 'GradleDependency'
39-
baseline file("lint-baseline.xml")
4039
}
4140
dependencies {
4241
implementation 'androidx.core:core:1.9.0'

packages/image_picker/image_picker_android/android/lint-baseline.xml

Lines changed: 0 additions & 400 deletions
This file was deleted.

packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerDelegate.java

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static class PendingCallState {
9595
public final @Nullable VideoSelectionOptions videoOptions;
9696
public final @NonNull Messages.Result<List<String>> result;
9797

98-
private PendingCallState(
98+
PendingCallState(
9999
@Nullable ImageSelectionOptions imageOptions,
100100
@Nullable VideoSelectionOptions videoOptions,
101101
@NonNull Messages.Result<List<String>> result) {
@@ -107,10 +107,10 @@ private PendingCallState(
107107

108108
@VisibleForTesting final String fileProviderName;
109109

110-
private final Activity activity;
111-
@VisibleForTesting final File externalFilesDirectory;
112-
private final ImageResizer imageResizer;
113-
private final ImagePickerCache cache;
110+
private final @NonNull Activity activity;
111+
@VisibleForTesting final @NonNull File externalFilesDirectory;
112+
private final @NonNull ImageResizer imageResizer;
113+
private final @NonNull ImagePickerCache cache;
114114
private final PermissionManager permissionManager;
115115
private final FileUriResolver fileUriResolver;
116116
private final FileUtils fileUtils;
@@ -140,10 +140,10 @@ interface OnPathReadyListener {
140140
private final Object pendingCallStateLock = new Object();
141141

142142
public ImagePickerDelegate(
143-
final Activity activity,
144-
final File externalFilesDirectory,
145-
final ImageResizer imageResizer,
146-
final ImagePickerCache cache) {
143+
final @NonNull Activity activity,
144+
final @NonNull File externalFilesDirectory,
145+
final @NonNull ImageResizer imageResizer,
146+
final @NonNull ImagePickerCache cache) {
147147
this(
148148
activity,
149149
externalFilesDirectory,
@@ -181,12 +181,7 @@ public void getFullImagePath(final Uri imageUri, final OnPathReadyListener liste
181181
activity,
182182
new String[] {(imageUri != null) ? imageUri.getPath() : ""},
183183
null,
184-
new MediaScannerConnection.OnScanCompletedListener() {
185-
@Override
186-
public void onScanCompleted(String path, Uri uri) {
187-
listener.onPathReady(path);
188-
}
189-
});
184+
(path, uri) -> listener.onPathReady(path));
190185
}
191186
},
192187
new FileUtils(),
@@ -199,13 +194,13 @@ public void onScanCompleted(String path, Uri uri) {
199194
*/
200195
@VisibleForTesting
201196
ImagePickerDelegate(
202-
final Activity activity,
203-
final File externalFilesDirectory,
204-
final ImageResizer imageResizer,
197+
final @NonNull Activity activity,
198+
final @NonNull File externalFilesDirectory,
199+
final @NonNull ImageResizer imageResizer,
205200
final @Nullable ImageSelectionOptions pendingImageOptions,
206201
final @Nullable VideoSelectionOptions pendingVideoOptions,
207202
final @Nullable Messages.Result<List<String>> result,
208-
final ImagePickerCache cache,
203+
final @NonNull ImagePickerCache cache,
209204
final PermissionManager permissionManager,
210205
final FileUriResolver fileUriResolver,
211206
final FileUtils fileUtils,
@@ -290,7 +285,9 @@ Messages.CacheRetrievalResult retrieveLostImage() {
290285
}
291286

292287
public void chooseVideoFromGallery(
293-
VideoSelectionOptions options, boolean usePhotoPicker, Messages.Result<List<String>> result) {
288+
@NonNull VideoSelectionOptions options,
289+
boolean usePhotoPicker,
290+
@NonNull Messages.Result<List<String>> result) {
294291
if (!setPendingOptionsAndResult(null, options, result)) {
295292
finishWithAlreadyActiveError(result);
296293
return;
@@ -318,7 +315,7 @@ private void launchPickVideoFromGalleryIntent(Boolean useAndroidPhotoPicker) {
318315
}
319316

320317
public void takeVideoWithCamera(
321-
VideoSelectionOptions options, Messages.Result<List<String>> result) {
318+
@NonNull VideoSelectionOptions options, @NonNull Messages.Result<List<String>> result) {
322319
if (!setPendingOptionsAndResult(null, options, result)) {
323320
finishWithAlreadyActiveError(result);
324321
return;
@@ -376,7 +373,7 @@ private void launchTakeVideoWithCameraIntent() {
376373
public void chooseImageFromGallery(
377374
@NonNull ImageSelectionOptions options,
378375
boolean usePhotoPicker,
379-
Messages.Result<List<String>> result) {
376+
@NonNull Messages.Result<List<String>> result) {
380377
if (!setPendingOptionsAndResult(options, null, result)) {
381378
finishWithAlreadyActiveError(result);
382379
return;
@@ -388,7 +385,7 @@ public void chooseImageFromGallery(
388385
public void chooseMultiImageFromGallery(
389386
@NonNull ImageSelectionOptions options,
390387
boolean usePhotoPicker,
391-
Messages.Result<List<String>> result) {
388+
@NonNull Messages.Result<List<String>> result) {
392389
if (!setPendingOptionsAndResult(options, null, result)) {
393390
finishWithAlreadyActiveError(result);
394391
return;
@@ -436,7 +433,7 @@ private void launchMultiPickImageFromGalleryIntent(Boolean useAndroidPhotoPicker
436433
}
437434

438435
public void takeImageWithCamera(
439-
@NonNull ImageSelectionOptions options, Messages.Result<List<String>> result) {
436+
@NonNull ImageSelectionOptions options, @NonNull Messages.Result<List<String>> result) {
440437
if (!setPendingOptionsAndResult(options, null, result)) {
441438
finishWithAlreadyActiveError(result);
442439
return;
@@ -556,7 +553,8 @@ public boolean onRequestPermissionsResult(
556553
}
557554

558555
@Override
559-
public boolean onActivityResult(final int requestCode, final int resultCode, final Intent data) {
556+
public boolean onActivityResult(
557+
final int requestCode, final int resultCode, final @Nullable Intent data) {
560558
Runnable handlerRunnable;
561559

562560
switch (requestCode) {
@@ -605,7 +603,7 @@ private void handleChooseMultiImageResult(int resultCode, Intent intent) {
605603
} else {
606604
paths.add(fileUtils.getPathFromUri(activity, intent.getData()));
607605
}
608-
handleMultiImageResult(paths, false);
606+
handleMultiImageResult(paths);
609607
return;
610608
}
611609

@@ -632,12 +630,7 @@ private void handleCaptureImageResult(int resultCode) {
632630
localPendingCameraMediaUri != null
633631
? localPendingCameraMediaUri
634632
: Uri.parse(cache.retrievePendingCameraMediaUriPath()),
635-
new OnPathReadyListener() {
636-
@Override
637-
public void onPathReady(String path) {
638-
handleImageResult(path, true);
639-
}
640-
});
633+
path -> handleImageResult(path, true));
641634
return;
642635
}
643636

@@ -652,21 +645,15 @@ private void handleCaptureVideoResult(int resultCode) {
652645
localPendingCameraMediaUrl != null
653646
? localPendingCameraMediaUrl
654647
: Uri.parse(cache.retrievePendingCameraMediaUriPath()),
655-
new OnPathReadyListener() {
656-
@Override
657-
public void onPathReady(String path) {
658-
handleVideoResult(path);
659-
}
660-
});
648+
this::handleVideoResult);
661649
return;
662650
}
663651

664652
// User cancelled taking a picture.
665653
finishWithSuccess(null);
666654
}
667655

668-
private void handleMultiImageResult(
669-
ArrayList<String> paths, boolean shouldDeleteOriginalIfScaled) {
656+
private void handleMultiImageResult(ArrayList<String> paths) {
670657
ImageSelectionOptions localImageOptions = null;
671658
synchronized (pendingCallStateLock) {
672659
if (pendingCallState != null) {
@@ -678,13 +665,6 @@ private void handleMultiImageResult(
678665
ArrayList<String> finalPath = new ArrayList<>();
679666
for (int i = 0; i < paths.size(); i++) {
680667
String finalImagePath = getResizedImagePath(paths.get(i), localImageOptions);
681-
682-
//delete original file if scaled
683-
if (finalImagePath != null
684-
&& !finalImagePath.equals(paths.get(i))
685-
&& shouldDeleteOriginalIfScaled) {
686-
new File(paths.get(i)).delete();
687-
}
688668
finalPath.add(i, finalImagePath);
689669
}
690670
finishWithListSuccess(finalPath);
@@ -693,7 +673,7 @@ private void handleMultiImageResult(
693673
}
694674
}
695675

696-
private void handleImageResult(String path, boolean shouldDeleteOriginalIfScaled) {
676+
void handleImageResult(String path, boolean shouldDeleteOriginalIfScaled) {
697677
ImageSelectionOptions localImageOptions = null;
698678
synchronized (pendingCallStateLock) {
699679
if (pendingCallState != null) {
@@ -721,7 +701,7 @@ private String getResizedImagePath(String path, @NonNull ImageSelectionOptions o
721701
outputOptions.getQuality().intValue());
722702
}
723703

724-
private void handleVideoResult(String path) {
704+
void handleVideoResult(String path) {
725705
finishWithSuccess(path);
726706
}
727707

packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerPlugin.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,18 @@ ImagePickerDelegate getDelegate() {
176176
}
177177

178178
private FlutterPluginBinding pluginBinding;
179-
private ActivityState activityState;
179+
ActivityState activityState;
180180

181181
@SuppressWarnings("deprecation")
182-
public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registrar registrar) {
182+
public static void registerWith(
183+
@NonNull io.flutter.plugin.common.PluginRegistry.Registrar registrar) {
183184
if (registrar.activity() == null) {
184185
// If a background flutter view tries to register the plugin, there will be no activity from the registrar,
185186
// we stop the registering process immediately because the ImagePicker requires an activity.
186187
return;
187188
}
188189
Activity activity = registrar.activity();
189-
Application application = null;
190-
if (registrar.context() != null) {
191-
application = (Application) (registrar.context().getApplicationContext());
192-
}
190+
Application application = (Application) (registrar.context().getApplicationContext());
193191
ImagePickerPlugin plugin = new ImagePickerPlugin();
194192
plugin.setup(registrar.messenger(), application, activity, registrar, null);
195193
}
@@ -305,7 +303,7 @@ public void pickImages(
305303
@NonNull Messages.ImageSelectionOptions options,
306304
@NonNull Boolean allowMultiple,
307305
@NonNull Boolean usePhotoPicker,
308-
Result<List<String>> result) {
306+
@NonNull Result<List<String>> result) {
309307
ImagePickerDelegate delegate = getImagePickerDelegate();
310308
if (delegate == null) {
311309
result.error(
@@ -335,7 +333,7 @@ public void pickVideos(
335333
@NonNull Messages.VideoSelectionOptions options,
336334
@NonNull Boolean allowMultiple,
337335
@NonNull Boolean usePhotoPicker,
338-
Result<List<String>> result) {
336+
@NonNull Result<List<String>> result) {
339337
ImagePickerDelegate delegate = getImagePickerDelegate();
340338
if (delegate == null) {
341339
result.error(

packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker/Messages.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v9.1.0), do not edit directly.
4+
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66

77
package io.flutter.plugins.imagepicker;
@@ -39,7 +39,7 @@ public FlutterError(@NonNull String code, @Nullable String message, @Nullable Ob
3939
}
4040

4141
@NonNull
42-
private static ArrayList<Object> wrapError(@NonNull Throwable exception) {
42+
protected static ArrayList<Object> wrapError(@NonNull Throwable exception) {
4343
ArrayList<Object> errorList = new ArrayList<Object>(3);
4444
if (exception instanceof FlutterError) {
4545
FlutterError error = (FlutterError) exception;
@@ -59,7 +59,7 @@ public enum SourceCamera {
5959
REAR(0),
6060
FRONT(1);
6161

62-
private final int index;
62+
final int index;
6363

6464
private SourceCamera(final int index) {
6565
this.index = index;
@@ -70,7 +70,7 @@ public enum SourceType {
7070
CAMERA(0),
7171
GALLERY(1);
7272

73-
private final int index;
73+
final int index;
7474

7575
private SourceType(final int index) {
7676
this.index = index;
@@ -81,7 +81,7 @@ public enum CacheRetrievalType {
8181
IMAGE(0),
8282
VIDEO(1);
8383

84-
private final int index;
84+
final int index;
8585

8686
private CacheRetrievalType(final int index) {
8787
this.index = index;
@@ -134,8 +134,8 @@ public void setQuality(@NonNull Long setterArg) {
134134
this.quality = setterArg;
135135
}
136136

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

140140
public static final class Builder {
141141

@@ -275,8 +275,8 @@ public void setCamera(@Nullable SourceCamera setterArg) {
275275
this.camera = setterArg;
276276
}
277277

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

281281
public static final class Builder {
282282

@@ -351,8 +351,8 @@ public void setMessage(@Nullable String setterArg) {
351351
this.message = setterArg;
352352
}
353353

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

357357
public static final class Builder {
358358

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

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

452452
public static final class Builder {
453453

@@ -504,9 +504,10 @@ ArrayList<Object> toList() {
504504
}
505505

506506
public interface Result<T> {
507+
@SuppressWarnings("UnknownNullness")
507508
void success(T result);
508509

509-
void error(Throwable error);
510+
void error(@NonNull Throwable error);
510511
}
511512

512513
private static class ImagePickerApiCodec extends StandardMessageCodec {
@@ -568,7 +569,7 @@ void pickImages(
568569
@NonNull ImageSelectionOptions options,
569570
@NonNull Boolean allowMultiple,
570571
@NonNull Boolean usePhotoPicker,
571-
Result<List<String>> result);
572+
@NonNull Result<List<String>> result);
572573
/**
573574
* Selects video and returns their paths.
574575
*
@@ -580,17 +581,17 @@ void pickVideos(
580581
@NonNull VideoSelectionOptions options,
581582
@NonNull Boolean allowMultiple,
582583
@NonNull Boolean usePhotoPicker,
583-
Result<List<String>> result);
584+
@NonNull Result<List<String>> result);
584585
/** Returns results from a previous app session, if any. */
585586
@Nullable
586587
CacheRetrievalResult retrieveLostResults();
587588

588589
/** The codec used by ImagePickerApi. */
589-
static MessageCodec<Object> getCodec() {
590+
static @NonNull MessageCodec<Object> getCodec() {
590591
return ImagePickerApiCodec.INSTANCE;
591592
}
592593
/** Sets up an instance of `ImagePickerApi` to handle messages through the `binaryMessenger`. */
593-
static void setup(BinaryMessenger binaryMessenger, ImagePickerApi api) {
594+
static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable ImagePickerApi api) {
594595
{
595596
BasicMessageChannel<Object> channel =
596597
new BasicMessageChannel<>(

0 commit comments

Comments
 (0)