Skip to content

Commit 7de5fdd

Browse files
Move Termux permission gate to send action and revert regression
1 parent c632912 commit 7de5fdd

3 files changed

Lines changed: 28 additions & 39 deletions

File tree

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

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
145145
private val handler = Handler(Looper.getMainLooper()) // Instance handler
146146

147147
private var pendingScreenshotDelayMillis: Long = 0L
148-
private var sawNonTermuxCommandSinceLastScreenshot: Boolean = false
149148
private var pendingDelayedScreenshotRunnable: Runnable? = null
150149

151150
// App name to package mapper
@@ -419,35 +418,13 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
419418
}
420419
}
421420
}
422-
.also { _ ->
423-
if (command !is Command.TakeScreenshot && command !is Command.TermuxCommand) {
424-
sawNonTermuxCommandSinceLastScreenshot = true
425-
}
426-
}
427421
}
428422

429423
private fun executeTakeScreenshotCommand(): Boolean {
430424
val delayMillis = pendingScreenshotDelayMillis
431425
pendingScreenshotDelayMillis = 0L
432-
val onlyTermuxContext = !sawNonTermuxCommandSinceLastScreenshot
433-
434-
if (!isTermuxRunCommandPermissionGranted()) {
435-
val denialCount = TermuxFeedbackPreferences.incrementPermissionDenialCount(applicationContext)
436-
if (denialCount >= 2) {
437-
showToast("Enable Termux permissions in the Android settings", true)
438-
}
439-
Log.w(TAG, "Blocking screenshot/AI handoff because Termux RUN_COMMAND permission is not granted.")
440-
return false
441-
} else {
442-
TermuxFeedbackPreferences.resetPermissionDenialCount(applicationContext)
443-
}
444-
445426
fun buildScreenInfoPayload(rawScreenInfo: String?): String? {
446-
val termuxOutput = if (onlyTermuxContext) {
447-
TermuxOutputPreferences.peekOutput(applicationContext)?.trim().orEmpty()
448-
} else {
449-
TermuxOutputPreferences.consumeOutput(applicationContext)?.trim().orEmpty()
450-
}
427+
val termuxOutput = TermuxOutputPreferences.consumeOutput(applicationContext)?.trim().orEmpty()
451428
if (termuxOutput.isBlank()) {
452429
return rawScreenInfo
453430
}
@@ -457,7 +434,7 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
457434

458435
val captureAndRequestScreenshot = {
459436
val currentModel = GenerativeAiViewModelFactory.getCurrentModel()
460-
if (!currentModel.supportsScreenshot || onlyTermuxContext) {
437+
if (!currentModel.supportsScreenshot) {
461438
Log.d(TAG, "Command.TakeScreenshot: Model has no screenshot support, capturing screen info only.")
462439
showToast("Capturing screen info...", false)
463440
val screenInfo = buildScreenInfoPayload(captureScreenInformation())
@@ -467,7 +444,6 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
467444
applicationContext,
468445
screenInfo
469446
)
470-
sawNonTermuxCommandSinceLastScreenshot = false
471447
} else {
472448
Log.d(TAG, "Command.TakeScreenshot: Capturing screen info and sending request broadcast to MainActivity.")
473449
showToast("Preparing screenshot...", false)
@@ -480,7 +456,6 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
480456
}
481457
applicationContext.sendBroadcast(intent)
482458
Log.d(TAG, "Sent broadcast ACTION_REQUEST_MEDIAPROJECTION_SCREENSHOT to MainActivity with screenInfo.")
483-
sawNonTermuxCommandSinceLastScreenshot = false
484459
}
485460
}
486461

@@ -501,10 +476,6 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
501476
return true
502477
}
503478

504-
private fun isTermuxRunCommandPermissionGranted(): Boolean {
505-
return checkSelfPermission("com.termux.permission.RUN_COMMAND") == PackageManager.PERMISSION_GRANTED
506-
}
507-
508479
private fun cancelPendingDelayedScreenshot() {
509480
pendingScreenshotDelayMillis = 0L
510481
pendingDelayedScreenshotRunnable?.let { runnable ->
@@ -622,8 +593,8 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
622593
putExtra("com.termux.RUN_COMMAND_PATH", "/data/data/com.termux/files/usr/bin/bash")
623594
putExtra("com.termux.RUN_COMMAND_ARGUMENTS", arrayOf("-lc", trimmedCommand))
624595
putExtra("com.termux.RUN_COMMAND_WORKDIR", "/data/data/com.termux/files/home")
625-
putExtra("com.termux.RUN_COMMAND_BACKGROUND", false)
626-
putExtra("com.termux.RUN_COMMAND_SESSION_ACTION", 1)
596+
putExtra("com.termux.RUN_COMMAND_BACKGROUND", true)
597+
putExtra("com.termux.RUN_COMMAND_SESSION_ACTION", 0)
627598
putExtra("com.termux.RUN_COMMAND_RUNNER", "app-shell")
628599
putExtra("com.termux.RUN_COMMAND_PENDING_INTENT", pendingResultIntent)
629600
putExtra("com.termux.RUN_COMMAND_BACKGROUND_CUSTOM_LOG_LEVEL", 0)

app/src/main/kotlin/com/google/ai/sample/feature/multimodal/PhotoReasoningScreen.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.graphics.drawable.BitmapDrawable
66
import android.net.Uri
77
import android.provider.Settings
88
import android.widget.Toast
9+
import androidx.core.content.ContextCompat
910
import androidx.activity.compose.BackHandler
1011
import androidx.activity.compose.rememberLauncherForActivityResult
1112
import androidx.activity.result.PickVisualMediaRequest
@@ -110,6 +111,7 @@ import com.google.ai.sample.ScreenOperatorAccessibilityService
110111
import com.google.ai.sample.util.Command
111112
import com.google.ai.sample.util.SystemMessageEntry
112113
import com.google.ai.sample.util.SystemMessageEntryPreferences
114+
import com.google.ai.sample.util.TermuxFeedbackPreferences
113115
import com.google.ai.sample.util.UriSaver
114116
import com.google.ai.sample.util.shareTextFile
115117
import kotlinx.coroutines.Dispatchers
@@ -425,6 +427,28 @@ fun PhotoReasoningScreen(
425427
}
426428

427429
if (userQuestion.isNotBlank()) {
430+
val hasTermuxRunCommandPermission = ContextCompat.checkSelfPermission(
431+
context,
432+
"com.termux.permission.RUN_COMMAND"
433+
) == android.content.pm.PackageManager.PERMISSION_GRANTED
434+
if (!hasTermuxRunCommandPermission) {
435+
val denialCount = TermuxFeedbackPreferences.incrementPermissionDenialCount(context)
436+
if (denialCount >= 2) {
437+
Toast.makeText(
438+
context,
439+
"Enable Termux permissions in the Android settings",
440+
Toast.LENGTH_LONG
441+
).show()
442+
} else {
443+
Toast.makeText(
444+
context,
445+
"Please enable Termux run command permissions",
446+
Toast.LENGTH_LONG
447+
).show()
448+
}
449+
return@IconButton
450+
}
451+
TermuxFeedbackPreferences.resetPermissionDenialCount(context)
428452
onReasonClicked(userQuestion, imageUris.toList())
429453
onUserQuestionChanged("")
430454
imageUris.clear()

app/src/main/kotlin/com/google/ai/sample/util/TermuxOutputPreferences.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,4 @@ object TermuxOutputPreferences {
2727
}
2828
return value
2929
}
30-
31-
fun peekOutput(context: Context): String? {
32-
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
33-
val value = prefs.getString(KEY_PENDING_OUTPUT, "").orEmpty().trim()
34-
return value.ifBlank { null }
35-
}
3630
}

0 commit comments

Comments
 (0)