Skip to content

Commit 188ec66

Browse files
Refactor: Use READ_MEDIA_IMAGES for Android 11+
I've switched the permission request logic to use READ_MEDIA_IMAGES for Android 11 (API 30) and newer. This aligns with modern Android permission practices and aims to resolve media access issues on Android 12 by using a more standard permission than MANAGE_EXTERNAL_STORAGE for typical media access. Specifically: - I modified `requiredPermissions` in MainActivity to request READ_MEDIA_IMAGES and READ_MEDIA_VIDEO for API 30+. - For API 29 and older, it continues to request READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE. - I reverted previous attempts to use MANAGE_EXTERNAL_STORAGE for Android 11/12, including removing the associated ActivityResultLauncher and specific dialogs. - The existing permission rationale dialog is now used for all permission requests. This change addresses your feedback to use minimal necessary permissions and prepares for testing on Android 12 and 15.
1 parent 00cf903 commit 188ec66

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/MainActivity.kt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ package com.google.ai.sample
22

33
import android.Manifest
44
import android.app.Activity
5-
import android.app.AlertDialog
5+
// AlertDialog, Uri, and Settings imports are removed as they were specific to the reverted MANAGE_EXTERNAL_STORAGE logic
66
import android.content.BroadcastReceiver
77
import android.content.Context
88
import android.content.Intent
99
import android.content.IntentFilter
1010
import android.content.SharedPreferences
1111
import android.content.pm.PackageManager
12-
import android.net.Uri
1312
import android.os.Build
1413
import android.os.Bundle
15-
import android.provider.Settings
14+
import android.os.Environment // Kept as per instruction for now
1615
import android.util.Log
1716
import android.widget.Toast
1817
import androidx.activity.ComponentActivity
@@ -97,6 +96,8 @@ class MainActivity : ComponentActivity() {
9796
private lateinit var prefs: SharedPreferences
9897
private var showFirstLaunchInfoDialog by mutableStateOf(false)
9998

99+
// manageStoragePermissionLauncher has been removed.
100+
100101
private val trialStatusReceiver = object : BroadcastReceiver() {
101102
override fun onReceive(context: Context?, intent: Intent?) {
102103
Log.i(TAG, "trialStatusReceiver: Received broadcast: ${intent?.action}")
@@ -729,32 +730,33 @@ class MainActivity : ComponentActivity() {
729730

730731
private fun checkAndRequestPermissions() {
731732
Log.d(TAG, "checkAndRequestPermissions called.")
732-
val permissionsToRequest = requiredPermissions.filter {
733+
// The 'requiredPermissions' field will be updated in the next plan step.
734+
// This step focuses on reverting the structure of this method.
735+
val permissionsNeeded = requiredPermissions.filter {
733736
ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED
734737
}.toTypedArray()
735-
if (permissionsToRequest.isNotEmpty()) {
736-
// Original: requestPermissionLauncher.launch(permissionsToRequest)
737-
// New:
738-
Log.i(TAG, "Required permissions not granted. Showing rationale dialog. Permissions: ${permissionsToRequest.joinToString()}")
739-
showPermissionRationaleDialog = true
738+
739+
if (permissionsNeeded.isNotEmpty()) {
740+
Log.i(TAG, "Permissions needed (${permissionsNeeded.joinToString()}). Showing rationale dialog.")
741+
showPermissionRationaleDialog = true // This triggers PermissionRationaleDialog -> requestPermissionLauncher
740742
} else {
741-
Log.i(TAG, "All required permissions already granted.")
742-
Log.d(TAG, "checkAndRequestPermissions: Permissions granted, calling startTrialServiceIfNeeded. Current state: $currentTrialState")
743-
// TODO: It seems the startTrialServiceIfNeeded() call was here in the original code when permissions were granted.
744-
// Confirm if it should remain here or be handled elsewhere. For now, keeping it as per the 'else' block's original apparent structure.
745-
// Based on the prompt, only the 'if (permissionsToRequest.isNotEmpty())' block's direct action changes.
743+
Log.i(TAG, "All necessary permissions already granted.")
744+
// Potentially call startTrialServiceIfNeeded() or other logic that runs when permissions are good.
745+
// Refer to the original state of this method before MANAGE_EXTERNAL_STORAGE changes.
746+
// Based on the provided MainActivity.kt, there wasn't a direct call here,
747+
// but ensure the structure allows for normal app flow if permissions are granted.
746748
}
747749
}
748750

749-
private val requiredPermissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
751+
private val requiredPermissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // R is API Level 30 (Android 11)
750752
arrayOf(
751753
Manifest.permission.READ_MEDIA_IMAGES,
752754
Manifest.permission.READ_MEDIA_VIDEO
753755
)
754-
} else {
756+
} else { // Versions older than Android 11 (i.e., Android 10 and below)
755757
arrayOf(
756758
Manifest.permission.READ_EXTERNAL_STORAGE,
757-
Manifest.permission.WRITE_EXTERNAL_STORAGE
759+
Manifest.permission.WRITE_EXTERNAL_STORAGE
758760
)
759761
}
760762

0 commit comments

Comments
 (0)