Skip to content

Commit d55cb2e

Browse files
Add files via upload
1 parent 64c6f00 commit d55cb2e

File tree

3 files changed

+179
-106
lines changed

3 files changed

+179
-106
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.google.ai.sample
2+
13
import android.accessibilityservice.AccessibilityService
24
import android.view.accessibility.AccessibilityEvent
35
import android.util.Log
@@ -20,4 +22,4 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
2022
// Service is connected, perform any initial setup here
2123
Log.d(TAG, "Accessibility service connected")
2224
}
23-
}
25+
}

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
/*
2-
* Copyright 2023 Google LLC
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
171
package com.google.ai.sample
182

193
import android.app.Activity
@@ -27,9 +11,6 @@ import android.os.IBinder
2711
import android.util.Log
2812
import java.io.File
2913

30-
/**
31-
* Manager class for handling screenshot functionality using MediaProjection via a foreground service
32-
*/
3314
class ScreenshotManager(private val context: Context) {
3415
companion object {
3516
private const val TAG = "ScreenshotManager"
@@ -58,9 +39,11 @@ class ScreenshotManager(private val context: Context) {
5839
val binder = service as ScreenshotService.LocalBinder
5940
screenshotService = binder.getService()
6041
isBound = true
42+
Log.d(TAG, "Service connected")
6143

6244
// If there's a pending screenshot request, execute it now
6345
pendingScreenshotCallback?.let { callback ->
46+
Log.d(TAG, "Executing pending screenshot request")
6447
takeScreenshot(callback)
6548
pendingScreenshotCallback = null
6649
}
@@ -69,6 +52,7 @@ class ScreenshotManager(private val context: Context) {
6952
override fun onServiceDisconnected(name: ComponentName?) {
7053
screenshotService = null
7154
isBound = false
55+
Log.d(TAG, "Service disconnected")
7256
}
7357
}
7458

@@ -118,8 +102,12 @@ class ScreenshotManager(private val context: Context) {
118102
*/
119103
private fun bindService() {
120104
if (!isBound) {
105+
Log.d(TAG, "Binding to service")
121106
val intent = Intent(context, ScreenshotService::class.java)
122-
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
107+
val result = context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
108+
Log.d(TAG, "Bind service result: $result")
109+
} else {
110+
Log.d(TAG, "Service already bound")
123111
}
124112
}
125113

@@ -128,8 +116,13 @@ class ScreenshotManager(private val context: Context) {
128116
*/
129117
private fun unbindService() {
130118
if (isBound) {
131-
context.unbindService(serviceConnection)
132-
isBound = false
119+
try {
120+
context.unbindService(serviceConnection)
121+
isBound = false
122+
Log.d(TAG, "Service unbound")
123+
} catch (e: Exception) {
124+
Log.e(TAG, "Error unbinding service: ${e.message}")
125+
}
133126
}
134127
}
135128

@@ -138,10 +131,13 @@ class ScreenshotManager(private val context: Context) {
138131
* @param callback Callback function that will be called with the screenshot bitmap
139132
*/
140133
fun takeScreenshot(callback: (Bitmap?) -> Unit) {
134+
Log.d(TAG, "takeScreenshot called, isBound=$isBound")
141135
if (isBound && screenshotService != null) {
136+
Log.d(TAG, "Taking screenshot via service")
142137
screenshotService?.takeScreenshot(callback)
143138
} else {
144139
// Store the callback and execute it when the service is connected
140+
Log.d(TAG, "Service not bound, storing callback and binding")
145141
pendingScreenshotCallback = callback
146142

147143
// Try to bind to the service if not already bound
@@ -170,10 +166,15 @@ class ScreenshotManager(private val context: Context) {
170166
*/
171167
fun release() {
172168
// Stop the service
173-
val serviceIntent = Intent(context, ScreenshotService::class.java).apply {
174-
action = ScreenshotService.ACTION_STOP
169+
try {
170+
val serviceIntent = Intent(context, ScreenshotService::class.java).apply {
171+
action = ScreenshotService.ACTION_STOP
172+
}
173+
context.startService(serviceIntent)
174+
Log.d(TAG, "Stop service request sent")
175+
} catch (e: Exception) {
176+
Log.e(TAG, "Error stopping service: ${e.message}")
175177
}
176-
context.startService(serviceIntent)
177178

178179
// Unbind from the service
179180
unbindService()

0 commit comments

Comments
 (0)