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

[image_picker] fix camera on Android 11 #3194

Merged

Conversation

Bubu
Copy link
Contributor

@Bubu Bubu commented Oct 23, 2020

Description

resolveActivity(intent) might return false on android 11+ even though startActivity(intent) would succeed.
Instead of introducing a entry we can just try to start the
activity and catch the exception if that doesn't work.

See here for a longer form explanation: https://cketti.de/2020/09/03/avoid-intent-resolveactivity/

Related Issues

Fixes flutter/flutter#85599
flutter/flutter#62669

Some more info on the android 11 behaviour change: flutter/flutter#63727 (comment)

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

@Bubu Bubu requested a review from cyanglaz as a code owner October 23, 2020 16:53
@google-cla
Copy link

google-cla bot commented Oct 23, 2020

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label Oct 23, 2020
@Bubu
Copy link
Contributor Author

Bubu commented Oct 23, 2020

@googlebot I signed it!

@google-cla google-cla bot added cla: yes and removed cla: no labels Oct 23, 2020
@Bubu
Copy link
Contributor Author

Bubu commented Nov 17, 2020

Hi, is there anything else I can do here?

@Bubu Bubu force-pushed the image_picker_camera_on_android11 branch from 5970e8b to 0d9c4b9 Compare November 25, 2020 00:24
@zigapovhe
Copy link

@cyanglaz Is this ready to merge?
@Bubu can you fix conflict with the CHANGELOG.md?

@Bubu Bubu force-pushed the image_picker_camera_on_android11 branch from 6e416e9 to d29990f Compare January 3, 2021 12:53
@Bubu
Copy link
Contributor Author

Bubu commented Jan 3, 2021

Rebased on master and fixed conflicts.

@Bubu
Copy link
Contributor Author

Bubu commented Feb 24, 2021

This branch has conflicts that must be resolved

sigh Any chance that someone might look at this? 🤔

@Bubu Bubu force-pushed the image_picker_camera_on_android11 branch from d29990f to 2fa3b12 Compare February 24, 2021 23:20
@Bubu Bubu force-pushed the image_picker_camera_on_android11 branch from 2fa3b12 to 7f54426 Compare April 26, 2021 13:44
@Bubu
Copy link
Contributor Author

Bubu commented Apr 26, 2021

Any chance to get this looked at?

@Bubu Bubu force-pushed the image_picker_camera_on_android11 branch from 7f54426 to 010f3cf Compare May 19, 2021 10:47
@stuartmorgan
Copy link
Contributor

Thanks for the contribution! We apologize for the long delay in triaging this PR. We’re in the process of overhauling our PR triage system to respond much more quickly, as well as working through the backlog.

@mvanbeusekom Could you add this review to your list?

Copy link
Contributor

@renefloor renefloor left a comment

Choose a reason for hiding this comment

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

Looks like a good change to me, especially because Google also recommends catch (e: ActivityNotFoundException) in their blog post.

However, I think it would be better to clean the empty file.

@Bubu Bubu force-pushed the image_picker_camera_on_android11 branch from 010f3cf to c9cdd66 Compare June 24, 2021 13:08
Copy link
Contributor

@renefloor renefloor left a comment

Choose a reason for hiding this comment

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

LGTM

@renefloor
Copy link
Contributor

@Bubu can you bring your branch up to date with master once more and update the changelog/pubspec? I'm afraid 0.8.1+1
is already taken :)

I think there is also 1 broken test (maybe that is new since you fixed the tests).

/tmp/cirrus-ci-build/packages/image_picker/image_picker/example/android/app/src/test/java/io/flutter/plugins/imagepicker/ImagePickerDelegateTest.java:197: error: cannot find symbol
    when(mockIntentResolver.resolveActivity(any(Intent.class))).thenReturn(true);

@Bubu
Copy link
Contributor Author

Bubu commented Jun 25, 2021

I think there is also 1 broken test (maybe that is new since you fixed the tests).

Ugh, there is... It's been so long since I wrote the changes to the tests here... 🙈 . Let me see if I can make it work again :D.

@Bubu Bubu force-pushed the image_picker_camera_on_android11 branch from c9cdd66 to a2509ed Compare June 25, 2021 15:18
@Bubu
Copy link
Contributor Author

Bubu commented Jun 25, 2021

@renefloor I can't figure out how to run the tests successfully locally (any pointers for that?). I still pushed a commit that should fix it and also (hopefully) resolved the conflicts. 🎉

@renefloor
Copy link
Contributor

@Bubu Currently takeImageWithCamera_WhenHasCameraPermission_AndNoActivityToHandleCameraIntent_FinishesWithNoCamerasAvailableError in ImagePickerDelegateTest is failing with java.io.IOException: No such file or directory.

That's because the test doesn't expect a file to be created (because the camera is not available), so the file system was not mocked.

I added this after line 183 in that test:

    MockedStatic<File> mockStaticFile = Mockito.mockStatic(File.class);
    mockStaticFile
            .when(() -> File.createTempFile(any(), any(), any()))
            .thenReturn(new File("/tmpfile"));

and I added the following line at the end of the test:

    mockStaticFile.close();

I cannot really comment this as a suggestion on your PR as these are not lines that changed, but I hope it makes sense. The complete test becomes:

  @Test
  public void
      takeImageWithCamera_WhenHasCameraPermission_AndNoActivityToHandleCameraIntent_FinishesWithNoCamerasAvailableError() {
    when(mockPermissionManager.isPermissionGranted(Manifest.permission.CAMERA)).thenReturn(true);

    MockedStatic<File> mockStaticFile = Mockito.mockStatic(File.class);
    mockStaticFile
            .when(() -> File.createTempFile(any(), any(), any()))
            .thenReturn(new File("/tmpfile"));

    doThrow(ActivityNotFoundException.class)
        .when(mockActivity)
        .startActivityForResult(any(Intent.class), anyInt());
    ImagePickerDelegate delegate = createDelegate();
    delegate.takeImageWithCamera(mockMethodCall, mockResult);

    verify(mockResult)
        .error("no_available_camera", "No cameras available for taking pictures.", null);
    verifyNoMoreInteractions(mockResult);
    mockStaticFile.close();
  }

It would me nice to also add mockStaticFile.close(); in takeImageWithCamera_WritesImageToCacheDirectory.

@Bubu
Copy link
Contributor Author

Bubu commented Jun 25, 2021

🤞

(I'm flying blind here, when I try to run the tests I get a huge amount of unrelated build errors instead. Let's just hope it works this time :))

@Bubu
Copy link
Contributor Author

Bubu commented Jun 25, 2021

We are now failing at a different test. I don't quite understand why my MR would be changing the behavior for that test. takeImageWithCamera_WhenHasCameraPermission_AndAnActivityCanHandleCameraIntent_LaunchesTakeWithCameraIntent should have been creating files even before this change?

Did some merge go wrong here?

@renefloor
Copy link
Contributor

I'm at my phone at the moment, but make sure to open image_picker/example/android and not image_picker/android. You might need to run the flutter example once or maybe you are missing parts of the SDK.

@Bubu
Copy link
Contributor Author

Bubu commented Jul 8, 2021

Rebased. Let's see how it goes :).

@stuartmorgan
Copy link
Contributor

@blasten Do you want to do a final review on this?

@Bubu
Copy link
Contributor Author

Bubu commented Jul 26, 2021

Hey everyone, any progress here? :-)

Copy link

@blasten blasten left a comment

Choose a reason for hiding this comment

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

requires to catch the exception

@Bubu Bubu requested a review from blasten July 26, 2021 20:53
@ManuLpz4
Copy link

ManuLpz4 commented Aug 2, 2021

Please ♥️

UPDATE: You can temporally fix this issue targeting your app to sdk 29 :)

@stuartmorgan
Copy link
Contributor

@Bubu Could you resolve the conflicts so we move forward with landing this?

resolveActivity(intent) might return false on android 11+_even though startActivity(intent) would succeed.
Instead of introducing a <queries> entry we can just try to start the
activity and catch the exception if that doesn't work.

Properly fixes flutter/flutter#62669.

Ref: https://cketti.de/2020/09/03/avoid-intent-resolveactivity/
@Bubu Bubu force-pushed the image_picker_camera_on_android11 branch from fdb4c24 to fe5cd5d Compare August 2, 2021 21:20
@Bubu
Copy link
Contributor Author

Bubu commented Aug 2, 2021

@stuartmorgan hopefully all done correctly.

@Bubu
Copy link
Contributor Author

Bubu commented Aug 9, 2021

There's now conflicts again here now... 🤔

@stuartmorgan
Copy link
Contributor

That's my mistake for not following up after merging in master to fix the infra issues.

@stuartmorgan stuartmorgan added the waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land. label Aug 9, 2021
@fluttergithubbot fluttergithubbot merged commit 256d37d into flutter:master Aug 9, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Aug 13, 2021
fotiDim pushed a commit to fotiDim/plugins that referenced this pull request Sep 13, 2021
amantoux pushed a commit to amantoux/plugins that referenced this pull request Sep 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes p: image_picker platform-android waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land.
Projects
None yet
7 participants