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

Android 13 media permissions #18183

Conversation

irfano
Copy link
Member

@irfano irfano commented Mar 27, 2023

This updates media permissions for Android 13 compatibility.

Granular media permissions
READ_EXTERNAL_STORAGE is used for accessing photos, videos, and audio files. After upgrading targetSdk to 33, we need to use specific permissions, READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, and READ_MEDIA_AUDIO.

  • I've used granular permissions for >=API 33. READ_EXTERNAL_STORAGE is still used for older APIs. READ_MEDIA_IMAGES and READ_MEDIA_VIDEO are different permissions on the code, but they appear as single permission, "Photos and videos". So, I requested both READ_MEDIA_IMAGES and READ_MEDIA_VIDEO even though just photos are needed.
  • I've also updated [WRITE_EXTERNAL_STORAGE] (https://developer.android.com/reference/android/Manifest.permission#WRITE_EXTERNAL_STORAGE) usage, which has no effect for devices higher than API 29.
  • pdf files are not listed on the media picker on devices that use API 30 or newer. It's not introduced with API 33. Opened an issue.
  • Some strings were wrong. They're also updated in this PR. (Storage and StoragePhotos and videos)
before denied after denied - all media after denied - images after denied - audio

To test:
Please perform all tests using a device with API 33 or higher and a device with an API version lower than 33.

Media Picker - Image - Choose from device

  1. Launch the JP app.
  2. Create a blog post.
  3. Add an Image block.
  4. Tap on the block and tap "Choose from device".
  5. Allow permission and add an image.
  6. Ensure the image is added without any issues.
  7. Disable the permission from the system settings.
  8. Repeat 1-6, but deny the permission this time. Ensure the message direct you to the system settings to allow the permission.
  9. Enable the permission on system settings
  10. Add an image and ensure it's added as expected.

Media Picker - Image - Other options

  1. Repeat "Media Picker - Image - Choose from device", but this time, try selecting other options instead of "Choose from device".

Media Picker - Video
Repeat Media Picker - Image tests with the video.

Media Picker - Audio
Repeat Media Picker - Image tests with the audio.

Media Browser

  1. Launch the JP app.
  2. Ensure you have all types of files on your media browser. (image, video, audio, and a file such as pdf)
  3. Navigate to "My Site → Media".
  4. Ensure all files on your device are listed.
  5. Tap an image.
  6. On the media detail screen, tap the save button at top of the screen.
  7. Ensure the file is downloaded to your device.
  8. Repeat 1-7 for other types of files.

Story

Warning
Story feature can be tested once Automattic/stories-android#731 is ready.

  1. Launch the JP app.
  2. Tap the green floating button at the bottom of the screen.
  3. Tap "Story post". If you see a popup, tap Create Post button to continue.
  4. Allow permissions and ensure you can see your media on the device.
  5. Select a media and post the story.
  6. Create another post and take a photo this time.
  7. Ensure you can post the story by taking a photo or video.
  8. Put the app in the background.
  9. Go to the system settings and disable the permissions.
  10. Go back to the create story screen. Ensure denied permission message is shown.
  11. To ensure it works as expected for all cases, try to allow and disable some permissions and test different variations (like allow audio, deny camera, deny images).

Regression Notes

  1. Potential unintended areas of impact
    All screens that need storage permission.

  2. What I did to test those areas of impact (or what existing automated tests I relied on)
    Tested all cases manually.

  3. What automated tests I added (or what prevented me from doing so)
    Updated existing tests.

PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

irfano added 4 commits March 27, 2023 17:59
WRITE_EXTERNAL_STORAGE is not required for SDK higher than 29.
For devices with API 33 and higher, we must request one or more of the granular media permissions instead of the READ_EXTERNAL_STORAGE permission.
For devices with API 33 and higher, we must request one or more of the granular media permissions instead of the READ_EXTERNAL_STORAGE permission.
@irfano irfano added this to the 22.1 milestone Mar 27, 2023
@irfano irfano requested a review from ravishanker March 27, 2023 22:06
@irfano irfano requested a review from a team as a code owner March 27, 2023 22:06
@wpmobilebot
Copy link
Contributor

wpmobilebot commented Mar 27, 2023

Jetpack📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack Jetpack
FlavorJalapeno
Build TypeDebug
Versionpr18183-8b50e67
Commit8b50e67
Direct Downloadjetpack-prototype-build-pr18183-8b50e67.apk
Note: Google Login is not supported on these builds.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Mar 27, 2023

WordPress📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress WordPress
FlavorJalapeno
Build TypeDebug
Versionpr18183-8b50e67
Commit8b50e67
Direct Downloadwordpress-prototype-build-pr18183-8b50e67.apk
Note: Google Login is not supported on these builds.

@AjeshRPai AjeshRPai self-requested a review March 28, 2023 04:32
@ravishanker
Copy link
Contributor

ravishanker commented Mar 28, 2023

👋 @irfano - Nice work! All the steps are working, except Take a Photo or Video is asking for Storage permission, and that leads to nowhere in system settings where it can be enabled! Looked through System Settings -> Apps and through Privacy -> Permissions Manager. Happening on both JP and WP.

@fluiddot
Copy link
Contributor

👋 @irfano - Nice work! All the steps are working, except Take a Photo or Video is asking for Storage permission, and that leads to nowhere in system settings where it can be enabled! Looked through System Settings -> Apps and through Privacy -> Permissions Manager. Happening on both JP and WP.

Seems the Storage permission is asked due to the logic when taking a photo from the editor:

@Override
public void onCapturePhotoButtonClicked() {
checkAndRequestCameraAndStoragePermissions(CAPTURE_PHOTO_PERMISSION_REQUEST_CODE);
}

private void checkAndRequestCameraAndStoragePermissions(int permissionRequestCode) {
if (PermissionUtils.checkAndRequestCameraAndStoragePermissions(this,

It relies on PermissionUtils.checkAndRequestCameraAndStoragePermissions , which as far as I checked, it tries to access android.permission.WRITE_EXTERNAL_STORAGE permission.

@irfano @ravishanker Maybe a potential workaround would be to apply a similar approach like this one on the PermissionUtils class, WDYT?

String[] permissions = null;
if (item == AddMenuItem.ITEM_CAPTURE_PHOTO || item == AddMenuItem.ITEM_CAPTURE_VIDEO) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
permissions = new String[]{Manifest.permission.CAMERA};
} else {
permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
}

@irfano
Copy link
Member Author

irfano commented Mar 29, 2023

Thank you so much for the help @fluiddot! I thought the change was required on gutenberg-mobile but it was on Utils library as you said.
I opened a PR on Utils library, and it fixed the camera issue on this PR.

@irfano
Copy link
Member Author

irfano commented Mar 29, 2023

@ravishanker, I updated the Utils library, and the camera permission issue should be fixed now.

@ravishanker
Copy link
Contributor

@ravishanker, I updated the Utils library, and the camera permission issue should be fixed now.

Yes, working now.

Published a new version of Utils 3.5.0, please update it.

Copy link
Contributor

@pachlava pachlava left a comment

Choose a reason for hiding this comment

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

@irfano 👋

Just leaving a note that mobile-ui-testing-squad was pinged for a review because the PR removes the write permission from two UI tests-related file. This does not seem to affect the execution negatively, so from our side, it's good to go as well 🙂

@AjeshRPai
Copy link
Contributor

AjeshRPai commented Mar 29, 2023

Hey @irfano

Nice work. 🙌🏼 . I have tested the app on Android 12, Pixel, and Android 13, Samsung.

While the positive scenario(all permissions granted) works in all the cases on both the devices, I found a behavior in Android 13, Samsung. I am not sure if it's right. More details below

Permissions Media → + icon → Choose from device Screen

Audio and Music Permission - Allowed ✅

Photos and Videos Permission - Dont Allow ❌
Screenshot 2023-03-29 at 9 42 13 PM

Audio and Music Permission - Don't allow ❌

Photos and Videos Permission - Allow ✅
WhatsApp Image 2023-03-29 at 9 47 45 PM

I haven't worked on the permissions for some time, so I am not sure what the issue is or whether this is to be expected.

@irfano
Copy link
Member Author

irfano commented Mar 30, 2023

While the positive scenario(all permissions granted) works in all the cases on both the devices, I found a behavior in Android 13, Samsung. I am not sure if it's right. More details below

🤔 Thank you for testing, @AjeshRPai! Can you see photos even though permission for the "Photos & videos" is not allowed? I tested it on both the emulator and the Android 13 Samsung device but couldn't reproduce your case. Can you write me testing instructions to reproduce it?

@AjeshRPai
Copy link
Contributor

Hey Irfan 👋🏼

I can see some photos even when the permission Photos and Videos is not allowed and Audio and Music is allowed. When I am not allowing the permission Audio and Music, I cannot see anything even when Photos and Video permission is allowed

Steps to reproduce

  • Deny the permissions Photos and Videos and Audio and Music
  • Go to Dashboard → Media → + icon → Choose from device
  • Verify that the permission dialog is open → Deny the permission
  • Put the app in background
  • Go to the System settings → Allow Photos and Videos and don't allow Audio and Music
  • Go to the app → Notice that no photos or images are shown on Screen
  • Go to the System settings → Change Photos and Videosto Don't allow and allow Audio and Music
  • Go to the app → Notice that some photos are shown

Here the Audio and Music seems to be the breaker in showing the Photos, My instinct is that in Android 13 either,

  • We need the Audios and Music permission to access photos and video.
    • In that case, we need to update the messaging to include Audio and Music on screen from the current implementation in this PR to the one similar in this PR raised by @0nko .
  • We don't need the Audios and Music permission, but we are incorrectly checking the status of Audio and Music permission before showing the photos and videos on media picker.
Current permission message Permission message shown in this PR
WhatsApp Image 2023-03-29 at 9 47 45 PM 228355568-2ba9f8e5-cebc-4e1a-9f28-3497bf616b3b

@irfano I will spend some additional time checking whether I can reproduce the scenario I have described here . I will make sure to let you know with a comment.

@@ -192,7 +187,8 @@ class MediaPickerLauncher @Inject constructor(
primaryDataSource = DEVICE,
availableDataSources = setOf(),
canMultiselect = canMultiselect,
requiresStoragePermissions = true,
requiresPhotosVideosPermissions = false,
Copy link
Contributor

Choose a reason for hiding this comment

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

❓I believe this is the function that's being called when you navigate from Media → + Icon → Choose from Device. Is the requiresPhotosVideosPermissions having correct value here?.

I did a quick run and test after changing this requiresPhotosVideosPermissions to true. I was able to see the photos in the Scenario

  • Allowed Photos and Video & Dont allow Audio and Music - I can see the photos and videos
  • Dont allow Photos and Video & Allow Audio and Music - I can't see any photos and video, Permission message is shown
  • Dont allow both permissions - Permission message is shown

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you so much for testing and finding a critical problem with the implementation, @AjeshRPai!
I added the support for "Image picker" or "Audio picker" but not for "Image + video + audio picker".
Now, I have added all media support to the media picker: ac21e3c.

@AjeshRPai
Copy link
Contributor

AjeshRPai commented Mar 31, 2023

@irfano

I have tested the app again, I can reproduce this issue. ✅ I have ensured that I am on the correct branch, and the Android version is 13.

📹 Please find below the video capture of this issue.

WhatsApp.Video.2023-03-31.at.12.53.58.PM.mp4

I looked into this issue, and I think I found the cause of the issue and left a suggestion here. I didn't commit the change. You can verify and make the change yourself. Feel free to let me know if you need anything else from me.

@irfano irfano modified the milestones: 22.1, 22.2 Apr 3, 2023
ParaskP7 added a commit that referenced this pull request Apr 3, 2023
Warning Message: "WRITE_EXTERNAL_STORAGE no longer provides write access
when targeting Android 11+, even when using
'requestLegacyExternalStorage'"

Explanation: "Scoped storage is enforced on Android 10+
(or Android 11+ if using requestLegacyExternalStorage). In particular,
WRITE_EXTERNAL_STORAGE will no longer provide write access to all files;
it will provide the equivalent of READ_EXTERNAL_STORAGE instead."

------------------------------------------------------------------------

This warning is already addressed and will be fixed as part of the
ongoing Android 13 media permissions work (see PR below):

PR: #18183
@irfano irfano requested a review from AjeshRPai April 3, 2023 22:29
@irfano
Copy link
Member Author

irfano commented Apr 3, 2023

I discovered another issue: the 'Story post' screen was not functioning properly.
I fixed it with the following commit: 898b117.
Additionally, I included it in the testing instructions. However, it can only be tested once Automattic/stories-android#731 is ready.

@0nko
Copy link
Contributor

0nko commented Apr 5, 2023

Hey there! 👋 Looking at this PR, I've been wondering why WPAndroid has its own version of the Media Picker instead of the using the library, which was designed to be customizable and reusable across different apps.

Is there anything specific that the library doesn't have or why it can't be integrated?

@irfano
Copy link
Member Author

irfano commented Apr 5, 2023

Hey there! 👋 Looking at this PR, I've been wondering why WPAndroid has its own version of the Media Picker instead of the using the library, which was designed to be customizable and reusable across different apps.

Is there anything specific that the library doesn't have or why it can't be integrated?

The reason for not using the MediaPicker library previously was that it required Hilt, which was not available when I attempted to add the Media Picker library to WPAndroid.

Now we have Hilt on WPAndroid, and we can add the library. I'll create an issue. Thanks for bringing it up, @0nko.

Btw, I recall we discussed this exact topic before. 😅 (Internal ref: p1637924023002400-slack-C02814L5EGP)

@0nko
Copy link
Contributor

0nko commented Apr 6, 2023

Btw, I recall we discussed this exact topic before. 😅 (Internal ref: p1637924023002400-slack-C02814L5EGP)

Good memory, it was 2.5 years ago. I completely forgot 😆

Copy link
Contributor

@AjeshRPai AjeshRPai left a comment

Choose a reason for hiding this comment

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

Hey @irfano

I have tested the app for the Media permissions, everything looks good to me 👍🏼

I have tested all the features by enabling/disabling permissions, and everything works as expected. I was able to select and upload photos and video when given permission and I was shown the message to allow permissions when I disabled the permissions.

The following features works as expected.

  • Media picker ✅
  • Media browser ✅
  • Story ✅

Thanks for working on this. I have left a Nitpick for you to address. Feel free to merge it once you had a look at it. I am approving but not merging so that you can have a look at the Nitpick I have left.

@@ -2850,6 +2854,10 @@
<string name="permissions_denied_title">Permissions</string>
<string name="permissions_denied_message">It looks like you turned off permissions required for this feature.&lt;br/&gt;&lt;br/&gt;To change this, edit your permissions and make sure &lt;strong&gt;%s&lt;/strong&gt; is enabled.</string>
<string name="permission_storage">Storage</string>
<string name="permission_images">Photos and videos</string>
<string name="permission_video">Photos and videos</string>
Copy link
Contributor

Choose a reason for hiding this comment

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

nip 🔍 : cant we reference the pemission_images string here?. Like

<string name="permission_video" translatable="false">@string/permission_images </string>

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the idea. I've done it: 21f9975

@peril-wordpress-mobile
Copy link

peril-wordpress-mobile bot commented Apr 6, 2023

Warnings
⚠️ PR has more than 300 lines of code changing. Consider splitting into smaller PRs if possible.

Generated by 🚫 dangerJS

@irfano
Copy link
Member Author

irfano commented Apr 6, 2023

I have also updated the RELEASE-NOTES.
Thank you all for reviewing. I am now merging.

@irfano irfano merged commit a76ef47 into Parent-PR-for-updating-targetSdkVersion-to-33-(Android-13) Apr 6, 2023
@irfano irfano deleted the android-13-media-permissions branch April 6, 2023 20:33
ParaskP7 added a commit that referenced this pull request Apr 7, 2023
Warning Message: "WRITE_EXTERNAL_STORAGE no longer provides write access
when targeting Android 11+, even when using
'requestLegacyExternalStorage'"

Explanation: "Scoped storage is enforced on Android 10+
(or Android 11+ if using requestLegacyExternalStorage). In particular,
WRITE_EXTERNAL_STORAGE will no longer provide write access to all files;
it will provide the equivalent of READ_EXTERNAL_STORAGE instead."

------------------------------------------------------------------------

This warning is already addressed and will be fixed as part of the
ongoing Android 13 media permissions work (see PR below):

PR: #18183
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants