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-
171package com.google.ai.sample
182
193import android.app.Activity
@@ -27,9 +11,6 @@ import android.os.IBinder
2711import android.util.Log
2812import java.io.File
2913
30- /* *
31- * Manager class for handling screenshot functionality using MediaProjection via a foreground service
32- */
3314class 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