Skip to content

Commit becb985

Browse files
Refactor: Adjust permission requests
- Comment out notification permission request from 'try' button. - Delete photo/video file access permission request logic. The 'try' button in the menu no longer requests notification permissions. The application no longer requests photo/video permissions on startup. Associated dialogs and logic for photo/video permissions have been removed. MediaProjection functionality remains unaffected.
1 parent 4124038 commit becb985

2 files changed

Lines changed: 12 additions & 110 deletions

File tree

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

Lines changed: 10 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class MainActivity : ComponentActivity() {
111111
private var showTrialInfoDialog by mutableStateOf(false)
112112
private var trialInfoMessage by mutableStateOf("")
113113

114-
private var showPermissionRationaleDialog by mutableStateOf(false)
114+
// private var showPermissionRationaleDialog by mutableStateOf(false) // Deleted
115115
private var permissionRequestCount by mutableStateOf(0)
116116

117117
// MediaProjection
@@ -166,32 +166,7 @@ class MainActivity : ComponentActivity() {
166166

167167
// Permission Launchers
168168
private lateinit var requestNotificationPermissionLauncher: ActivityResultLauncher<String>
169-
private val requestPermissionLauncher = registerForActivityResult(
170-
ActivityResultContracts.RequestMultiplePermissions()
171-
) { permissions ->
172-
Log.d(TAG, "requestPermissionLauncher callback received. Permissions: $permissions")
173-
val allGranted = permissions.entries.all { it.value }
174-
if (allGranted) {
175-
Log.i(TAG, "All required permissions granted by user.")
176-
updateStatusMessage("All required permissions granted")
177-
} else {
178-
val deniedPermissions = permissions.entries.filter { !it.value }.map { it.key }
179-
Log.w(TAG, "Permissions denied. Current request count: $permissionRequestCount. Denied permissions: $deniedPermissions")
180-
181-
if (permissionRequestCount == 1) {
182-
Log.i(TAG, "Permissions denied once. Showing rationale dialog again for a second attempt.")
183-
showPermissionRationaleDialog = true
184-
} else if (permissionRequestCount >= 2) {
185-
Log.w(TAG, "Permissions denied after second formal request (request count: $permissionRequestCount). App will exit.")
186-
Toast.makeText(this, "Without this authorization, operation is not possible.", Toast.LENGTH_LONG).show()
187-
finish()
188-
} else {
189-
Log.e(TAG, "Permissions denied with unexpected permissionRequestCount: $permissionRequestCount. Exiting.")
190-
Toast.makeText(this, "Permissions repeatedly denied. Operation not possible.", Toast.LENGTH_LONG).show()
191-
finish()
192-
}
193-
}
194-
}
169+
// private val requestPermissionLauncher = registerForActivityResult(...) // Deleted
195170

196171
private fun requestMediaProjectionPermission() {
197172
Log.d(TAG, "Requesting MediaProjection permission")
@@ -377,17 +352,7 @@ class MainActivity : ComponentActivity() {
377352
}
378353
}
379354

380-
private val requiredPermissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
381-
arrayOf(
382-
Manifest.permission.READ_MEDIA_IMAGES,
383-
Manifest.permission.READ_MEDIA_VIDEO
384-
)
385-
} else {
386-
arrayOf(
387-
Manifest.permission.READ_EXTERNAL_STORAGE,
388-
Manifest.permission.WRITE_EXTERNAL_STORAGE
389-
)
390-
}
355+
// private val requiredPermissions = ... // Deleted
391356

392357
override fun onCreate(savedInstanceState: Bundle?) {
393358
Log.d(TAG, "onCreate: Activity creating.")
@@ -404,8 +369,8 @@ class MainActivity : ComponentActivity() {
404369
Log.d(TAG, "onCreate: API key found.")
405370
}
406371

407-
Log.d(TAG, "onCreate: Calling checkAndRequestPermissions.")
408-
checkAndRequestPermissions()
372+
// Log.d(TAG, "onCreate: Calling checkAndRequestPermissions.") // Deleted
373+
// checkAndRequestPermissions() // Deleted
409374
Log.d(TAG, "onCreate: Calling setupBillingClient.")
410375
setupBillingClient()
411376

@@ -523,19 +488,8 @@ class MainActivity : ComponentActivity() {
523488
Log.d(TAG, "setContent: Rendering AppNavigation.")
524489
AppNavigation(navController)
525490

526-
if (showPermissionRationaleDialog) {
527-
Log.d(TAG, "setContent: Rendering PermissionRationaleDialog. Request count before dialog action: $permissionRequestCount")
528-
PermissionRationaleDialog(
529-
onDismiss = {
530-
Log.i(TAG, "PermissionRationaleDialog OK clicked. Current request count: $permissionRequestCount")
531-
permissionRequestCount++
532-
Log.i(TAG, "Permission request count incremented to: $permissionRequestCount")
533-
showPermissionRationaleDialog = false
534-
Log.i(TAG, "Requesting permissions now. Required: ${requiredPermissions.joinToString()}")
535-
requestPermissionLauncher.launch(requiredPermissions)
536-
}
537-
)
538-
} else if (showFirstLaunchInfoDialog) {
491+
// if (showPermissionRationaleDialog) { ... } // Deleted block
492+
if (showFirstLaunchInfoDialog) {
539493
Log.d(TAG, "setContent: Rendering FirstLaunchInfoDialog.")
540494
FirstLaunchInfoDialog(
541495
onDismiss = {
@@ -1086,18 +1040,7 @@ class MainActivity : ComponentActivity() {
10861040
Log.d(TAG, "onDestroy: Finished.")
10871041
}
10881042

1089-
private fun checkAndRequestPermissions() {
1090-
Log.d(TAG, "checkAndRequestPermissions called.")
1091-
val permissionsToRequest = requiredPermissions.filter {
1092-
ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED
1093-
}.toTypedArray()
1094-
if (permissionsToRequest.isNotEmpty()) {
1095-
Log.i(TAG, "Required permissions not granted. Showing rationale dialog. Permissions: ${permissionsToRequest.joinToString()}")
1096-
showPermissionRationaleDialog = true
1097-
} else {
1098-
Log.i(TAG, "All required permissions already granted.")
1099-
}
1100-
}
1043+
// private fun checkAndRequestPermissions() { ... } // Deleted
11011044

11021045
companion object {
11031046
private const val TAG = "MainActivity"
@@ -1173,49 +1116,8 @@ fun FirstLaunchInfoDialog(onDismiss: () -> Unit) {
11731116
}
11741117
}
11751118

1176-
@Composable
1177-
fun PermissionRationaleDialog(onDismiss: () -> Unit) {
1178-
Log.d("PermissionRationaleDialog", "Composing PermissionRationaleDialog")
1179-
Dialog(onDismissRequest = {
1180-
Log.d("PermissionRationaleDialog", "onDismissRequest called")
1181-
onDismiss()
1182-
}) {
1183-
Card(
1184-
modifier = Modifier
1185-
.fillMaxWidth()
1186-
.padding(16.dp),
1187-
) {
1188-
Column(
1189-
modifier = Modifier
1190-
.padding(16.dp)
1191-
.fillMaxWidth(),
1192-
verticalArrangement = Arrangement.Center,
1193-
horizontalAlignment = Alignment.CenterHorizontally
1194-
) {
1195-
Text(
1196-
text = "Permission Required",
1197-
style = MaterialTheme.typography.titleLarge
1198-
)
1199-
Spacer(modifier = Modifier.height(16.dp))
1200-
Text(
1201-
text = "You'll immediately be asked for photo and video permissions. These is necessary so the AI ​​can see the screenshots and thus also the screen taken by the Screen Operator. It will never access media that the Screen Operator didn't create themselves.",
1202-
style = MaterialTheme.typography.bodyMedium,
1203-
modifier = Modifier.align(Alignment.CenterHorizontally)
1204-
)
1205-
Spacer(modifier = Modifier.height(24.dp))
1206-
TextButton(
1207-
onClick = {
1208-
Log.d("PermissionRationaleDialog", "OK button clicked")
1209-
onDismiss()
1210-
},
1211-
modifier = Modifier.fillMaxWidth()
1212-
) {
1213-
Text("OK")
1214-
}
1215-
}
1216-
}
1217-
}
1218-
}
1119+
// @Composable // Deleted
1120+
// fun PermissionRationaleDialog(onDismiss: () -> Unit) { ... } // Deleted
12191121

12201122

12211123
@Composable

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ fun MenuScreen(
202202
// onItemClicked will be called from dialog
203203
} else {
204204
Log.d("MenuScreen", "Rationale not needed or already handled. Requesting permission directly.")
205-
mainActivity.requestNotificationPermission()
205+
// mainActivity.requestNotificationPermission()
206206
onItemClicked(menuItem.routeId) // Proceed to navigate
207207
}
208208
} else {
@@ -322,7 +322,7 @@ fun MenuScreen(
322322
showRationaleDialogForPhotoReasoning = false
323323
mainActivity?.setNotificationRationaleShown(true)
324324
Log.d("MenuScreen", "Requesting notification permission from rationale dialog.")
325-
mainActivity?.requestNotificationPermission()
325+
// mainActivity?.requestNotificationPermission()
326326
// Log after to see if it's called immediately or if requestNotificationPermission is suspending (it's not)
327327
Log.d("MenuScreen", "Navigating to photo_reasoning after rationale OK.")
328328
mainActivity?.let { onItemClicked("photo_reasoning") }

0 commit comments

Comments
 (0)