Skip to content

Commit

Permalink
Don't request for write storage permissions for android 11 and above. (
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored Feb 29, 2024
1 parent f8b05d0 commit a2d69a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PermissionsManager
@Inject
constructor(
@ApplicationContext private val context: Context,
private val activityStreams: ActivityStreams
private val activityStreams: ActivityStreams,
) {
/**
* Asynchronously requests the app be granted the specified permission. If the permission has
Expand Down Expand Up @@ -68,7 +68,7 @@ constructor(
private suspend fun getPermissionsResult(permission: String) {
val result = activityStreams.getNextRequestPermissionsResult(PERMISSIONS_REQUEST_CODE)
if (!result.isGranted(permission)) {
throw PermissionDeniedException()
throw PermissionDeniedException("Permission denied: $permission")
}
}

Expand All @@ -81,4 +81,4 @@ constructor(
* Indicates a request to grant the app permissions was denied. More specifically, it indicates the
* requested permission was not in the list of granted permissions in the system's response.
*/
class PermissionDeniedException : Exception()
class PermissionDeniedException(message: String) : Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.google.android.ground.ui.datacollection.tasks.photo

import android.Manifest
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -121,7 +122,16 @@ class PhotoTaskFragment : AbstractTaskFragment<PhotoTaskViewModel>() {
private fun obtainCapturePhotoPermissions(onPermissionsGranted: () -> Unit = {}) {
externalScope.launch {
try {
permissionsManager.obtainPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)

// From Android 11 onwards (api level 30), requesting WRITE_EXTERNAL_STORAGE permission
// always returns denied. By default, the app has read/write access to shared data.
//
// For more details please refer to:
// https://developer.android.com/about/versions/11/privacy/storage#permissions-target-11
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
permissionsManager.obtainPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}

permissionsManager.obtainPermission(Manifest.permission.CAMERA)

onPermissionsGranted()
Expand Down

0 comments on commit a2d69a2

Please sign in to comment.