Skip to content

Commit e81eb67

Browse files
feat: Stabilize conditional toast with ActivityManager
This change stabilizes the logic for showing the 'The AI stopped Screen Operator' toast. The previous implementation, which relied on lifecycle callbacks in `MainActivity`, was prone to timing issues. This commit refactors the implementation to use `ActivityManager` directly within the `BroadcastReceiver` in `PhotoReasoningViewModel`. This ensures that the foreground check is performed at the exact moment the AI response is received, providing a reliable and stable solution. The changes in `MainActivity.kt` have been reverted.
1 parent af044fb commit e81eb67

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,6 @@ class MainActivity : ComponentActivity() {
10441044
override fun onResume() {
10451045
Log.d(TAG, "onResume: Activity resuming.")
10461046
super.onResume()
1047-
isInForeground = true // Setze Flag: App ist sichtbar
10481047
instance = this
10491048
Log.d(TAG, "onResume: MainActivity instance set.")
10501049
Log.d(TAG, "onResume: Calling refreshAccessibilityServiceStatus.")
@@ -1069,12 +1068,6 @@ class MainActivity : ComponentActivity() {
10691068
Log.d(TAG, "onResume: Finished.")
10701069
}
10711070

1072-
override fun onPause() {
1073-
super.onPause()
1074-
isInForeground = false // Setze Flag: App geht in den Hintergrund
1075-
Log.d(TAG, "onPause: App is no longer in foreground.")
1076-
}
1077-
10781071
override fun onDestroy() {
10791072
Log.d(TAG, "onDestroy: Activity destroying.")
10801073
super.onDestroy()
@@ -1141,7 +1134,6 @@ class MainActivity : ComponentActivity() {
11411134
const val EXTRA_SCREEN_INFO = "com.google.ai.sample.EXTRA_SCREEN_INFO"
11421135
}
11431136

1144-
var isInForeground: Boolean = false // Neue public Flag für Vordergrund-Status
11451137

11461138
override fun onNewIntent(intent: Intent?) {
11471139
super.onNewIntent(intent)

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.google.ai.sample.feature.multimodal
22

33
import android.app.ActivityManager
4-
import android.app.ActivityManager.RunningAppProcessInfo // Für IMPORTANCE_FOREGROUND
4+
import android.app.ActivityManager.RunningAppProcessInfo
55
import android.content.Context
66
import android.graphics.Bitmap
77
import android.content.BroadcastReceiver
@@ -144,8 +144,14 @@ class PhotoReasoningViewModel(
144144
updateAiMessage(responseText) // Existing method to update chat history
145145
processCommands(responseText) // Existing method
146146
if (!responseText.contains("takeScreenshot()", ignoreCase = true)) {
147-
val mainActivity = MainActivity.getInstance()
148-
if (mainActivity != null && !mainActivity.isInForeground) {
147+
// Stabile Prüfung, ob die App im Vordergrund ist
148+
val activityManager = receiverContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
149+
val runningProcesses = activityManager.runningAppProcesses ?: emptyList<RunningAppProcessInfo>()
150+
val isInForeground = runningProcesses.any { process ->
151+
process.processName == receiverContext.packageName && process.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
152+
}
153+
Log.d(TAG, "Foreground check: isInForeground = $isInForeground") // Debugging-Log (kann später entfernt werden)
154+
if (!isInForeground) {
149155
Toast.makeText(receiverContext, "The AI stopped Screen Operator", Toast.LENGTH_SHORT).show()
150156
}
151157
}

0 commit comments

Comments
 (0)