Skip to content

Commit

Permalink
Fix image picker crash when used with android alarm manager (flutter#…
Browse files Browse the repository at this point in the history
…1125)

Fixing [flutter/flutter#21972](flutter/flutter#21972)
Android Alarm Manager will create a background flutter view to register plugins. When that happens, image_picker will be registered without an activity, which then caused crash. 
So we escape the registration process when there is no activity in the registrar.
  • Loading branch information
Chris Yang authored and Altercode IT Solutions committed Feb 19, 2019
1 parent 4a7e24f commit 03d2c79
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.0+3

* Fixed am Android crash when Image Picker is registered without an activity.

## 0.5.0+2

* Log a more detailed warning at build time about the previous AndroidX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class ImagePickerPlugin implements MethodChannel.MethodCallHandler {
private final ImagePickerDelegate delegate;

public static void registerWith(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;
}
final MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL);

final File externalFilesDirectory =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.flutter.plugins.imagepicker;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -95,6 +96,14 @@ public void onMethodCall_WhenSourceIsCamera_InvokesTakeImageWithCamera() {
verifyZeroInteractions(mockResult);
}

@Test
public void onResiter_WhenAcitivityIsNull_ShouldNotCrash() {
when(mockRegistrar.activity()).thenReturn(null);
ImagePickerPlugin.registerWith((mockRegistrar));
assertTrue(
"No exception thrown when ImagePickerPlugin.registerWith ran with activity = null", true);
}

private MethodCall buildMethodCall(final int source) {
final Map<String, Object> arguments = new HashMap<>();
arguments.put("source", source);
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors:
- Flutter Team <flutter-dev@googlegroups.com>
- Rhodes Davis Jr. <rody.davis.jr@gmail.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker
version: 0.5.0+2
version: 0.5.0+3

flutter:
plugin:
Expand Down

0 comments on commit 03d2c79

Please sign in to comment.