Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Fixed image picker crash when used with android alarm manager #1125

Merged
merged 7 commits into from
Feb 8, 2019

Conversation

cyanglaz
Copy link
Contributor

@cyanglaz cyanglaz commented Jan 28, 2019

@Hixie
Copy link
Contributor

Hixie commented Jan 29, 2019

test?

@cyanglaz cyanglaz requested a review from amirh January 29, 2019 22:10
@Hixie
Copy link
Contributor

Hixie commented Feb 7, 2019

LGTM

@@ -21,6 +21,9 @@
private final ImagePickerDelegate delegate;

public static void registerWith(PluginRegistry.Registrar registrar) {
if (registrar.activity() == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will prevent the crash but IIUC leaves us with an unregistered plugin, as even if the activity asks the GeneratedPluginRegistrant to register again it will short circuit.

Unless I'm missing something this will trade the startup crash with a crash the first time the plugin is used (as the method channel isn't registered).

I believe the only eager use of activity is getExternalFilesDir and this can be done with the application context which is guaranteed to be available.
It looks like ImagePickerDelegate is caching the activity that's being passed to it from here, I believe instead of doing that we can make ImagePickerDelegate always ask the activity from the registrar when it needs an activity.

@cyanglaz @Hixie am I getting this wrong? is the current fix actually leaving the picker in a working state? are you interested in merging this for now just to prevent the early crash or doing a little more refactoring here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to test if the picker was in a working state with this fix -- the problem I was seeing was that when the app was starting in the background (for the alarm manager), nothing was working, because the plugin would crash and prevent anything else from happening. In that case, I don't actually care about the image picker.

I've no idea if it's possible for the app to then get re-used in the foreground. If so, then I agree that this wouldn't work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point for the scenario that the app is executed in the background and then quits (and the Android APP is destroyed) it's ok to live with the plugin unregistered. So this fix at least improves some cases, we can land it now. We should probably follow-up and figure what happens if e.g the app's background code is calling startActivity and starts an activity of the same (Android) application with a FlutterView.

Copy link
Contributor

@amirh amirh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM to land this to fix the crash for the background only scenario.
We should followup to better understand the background->foreground scenario (e.g the code executed by alarm manager starts a Flutter Activity in the same Android app)

@cyanglaz
Copy link
Contributor Author

cyanglaz commented Feb 8, 2019

LGTM to land this to fix the crash for the background only scenario.
We should followup to better understand the background->foreground scenario (e.g the code executed by alarm manager starts a Flutter Activity in the same Android app)

After an offline discussion, we verified that the registering process that crashed the image picker came from a background flutter view that is used by the Android Alarm Manager. And a foreground registration will happen with a different registry. Image Picker only needs the foreground registry so the fix should not cause any issue with the Image Picker.

@cyanglaz cyanglaz merged commit f8923c9 into flutter:master Feb 8, 2019
andreidiaconu pushed a commit to andreidiaconu/plugins that referenced this pull request Feb 17, 2019
…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.
andreidiaconu added a commit to andreidiaconu/plugins that referenced this pull request Feb 17, 2019
karantanwar pushed a commit to karantanwar/plugins that referenced this pull request Feb 19, 2019
…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.
karantanwar pushed a commit to karantanwar/plugins that referenced this pull request Feb 19, 2019
…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.
amirh pushed a commit to amirh/plugins that referenced this pull request Feb 22, 2019
Background FlutterViews do not have an activity, the Maps and Camera
plugins are foreground only and assumed and activity is available which
can result in a crash when the plugin is registered by a background
FlutterView.

We workaround by just not registering the plugins if there is no
activity.

Similar to flutter#1125
amirh added a commit that referenced this pull request Feb 22, 2019
#1255)

Background FlutterViews do not have an activity, the Maps and Camera
plugins are foreground only and assumed and activity is available which
can result in a crash when the plugin is registered by a background
FlutterView.

We workaround by just not registering the plugins if there is no
activity.

Similar to #1125
@cyanglaz cyanglaz deleted the image_picker branch March 4, 2019 21:38
curt-weber pushed a commit to KWRI/google_maps_flutter that referenced this pull request Nov 3, 2022
…. (#1255)

Background FlutterViews do not have an activity, the Maps and Camera
plugins are foreground only and assumed and activity is available which
can result in a crash when the plugin is registered by a background
FlutterView.

We workaround by just not registering the plugins if there is no
activity.

Similar to flutter/plugins#1125
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants