Skip to content

Commit 28667f2

Browse files
Add files via upload
1 parent e6799aa commit 28667f2

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

app/src/main/kotlin/com/google/ai/sample/feature/chat/ChatViewModel.kt

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.lifecycle.viewModelScope
2222
import com.google.ai.client.generativeai.GenerativeModel
2323
import com.google.ai.client.generativeai.type.asTextOrNull
2424
import com.google.ai.client.generativeai.type.content
25+
import com.google.ai.sample.MainActivity
2526
import com.google.ai.sample.ScreenOperatorAccessibilityService
2627
import com.google.ai.sample.util.CommandParser
2728
import kotlinx.coroutines.flow.MutableStateFlow
@@ -32,6 +33,8 @@ import kotlinx.coroutines.launch
3233
class ChatViewModel(
3334
generativeModel: GenerativeModel
3435
) : ViewModel() {
36+
private val TAG = "ChatViewModel"
37+
3538
private val chat = generativeModel.startChat(
3639
history = listOf(
3740
content(role = "user") { text("Hello, I have 2 dogs in my house.") },
@@ -58,12 +61,46 @@ class ChatViewModel(
5861

5962
// If commands were found, execute them
6063
if (commands.isNotEmpty()) {
61-
Log.d("ChatViewModel", "Found ${commands.size} commands in response")
64+
Log.d(TAG, "Found ${commands.size} commands in response")
65+
66+
// Check if accessibility service is enabled
67+
val context = MainActivity.getInstance()?.applicationContext
68+
if (context != null) {
69+
val isServiceEnabled = ScreenOperatorAccessibilityService.isAccessibilityServiceEnabled(context)
70+
if (!isServiceEnabled) {
71+
Log.e(TAG, "Accessibility service is not enabled. Commands will not work.")
72+
MainActivity.getInstance()?.updateStatusMessage(
73+
"Accessibility service is not enabled. Please enable it in settings to use click commands.",
74+
true
75+
)
76+
return
77+
}
78+
}
6279

6380
// Execute each command
6481
for (command in commands) {
82+
Log.d(TAG, "Executing command: $command")
83+
84+
// Notify user about command execution
85+
val commandText = when (command) {
86+
is com.google.ai.sample.util.Command.ClickButton ->
87+
"clickOnButton(\"${command.buttonText}\")"
88+
is com.google.ai.sample.util.Command.TapCoordinates ->
89+
"tapAtCoordinates(${command.x}, ${command.y})"
90+
is com.google.ai.sample.util.Command.TakeScreenshot ->
91+
"takeScreenshot()"
92+
}
93+
94+
MainActivity.getInstance()?.updateStatusMessage(
95+
"AI is attempting to execute: $commandText",
96+
false
97+
)
98+
99+
// Execute the command
65100
ScreenOperatorAccessibilityService.executeCommand(command)
66101
}
102+
} else {
103+
Log.d(TAG, "No commands found in response")
67104
}
68105
}
69106

@@ -96,13 +133,20 @@ class ChatViewModel(
96133
processCommandsInResponse(modelResponse)
97134
}
98135
} catch (e: Exception) {
136+
Log.e(TAG, "Error sending message: ${e.message}", e)
99137
_uiState.value.replaceLastPendingMessage()
100138
_uiState.value.addMessage(
101139
ChatMessage(
102140
text = e.localizedMessage ?: "An error occurred",
103141
participant = Participant.ERROR
104142
)
105143
)
144+
145+
// Notify user about the error
146+
MainActivity.getInstance()?.updateStatusMessage(
147+
"Error communicating with AI: ${e.message}",
148+
true
149+
)
106150
}
107151
}
108152
}

0 commit comments

Comments
 (0)