@ -199,7 +199,8 @@ class ScreenCaptureService : Service() {
context = this ,
onDetectionRequested = { triggerDetection ( ) } ,
onToggleOverlay = { toggleOverlay ( ) } ,
onReturnToApp = { returnToMainApp ( ) }
onReturnToApp = { returnToMainApp ( ) } ,
onShowLastResult = { showLastDetectionResult ( ) }
)
// Initialize detection result handler
@ -219,41 +220,6 @@ class ScreenCaptureService : Service() {
triggerManualDetection ( )
}
/ * *
* Toggle overlay visibility from UI
* /
fun toggleOverlay ( ) {
overlayEnabled = ! overlayEnabled
if ( ! overlayEnabled ) {
hideDetectionOverlay ( )
PGHLog . i ( TAG , " 🔇 Detection overlay disabled and hidden " )
} else {
// Show cached detections immediately if available
if ( lastDetections . isNotEmpty ( ) ) {
showYOLODetectionOverlay ( lastDetections )
PGHLog . i ( TAG , " 🔊 Detection overlay enabled and showing ${lastDetections.size} cached detections " )
} else {
PGHLog . i ( TAG , " 🔊 Detection overlay enabled (no cached detections to show) " )
}
}
}
/ * *
* Return to main app from UI
* /
fun returnToMainApp ( ) {
try {
val intent = Intent ( this , MainActivity :: class . java ) . apply {
flags = Intent . FLAG_ACTIVITY_NEW_TASK or
Intent . FLAG_ACTIVITY_CLEAR_TOP or
Intent . FLAG_ACTIVITY_SINGLE_TOP
}
startActivity ( intent )
PGHLog . i ( TAG , " 🏠 Returning to main app " )
} catch ( e : Exception ) {
PGHLog . e ( TAG , " Failed to return to main app " , e )
}
}
override fun onStartCommand ( intent : Intent ? , flags : Int , startId : Int ) : Int {
when ( intent ?. action ) {
@ -1293,4 +1259,77 @@ class ScreenCaptureService : Service() {
stopScreenCapture ( )
}
/ * *
* Show the last detection result by re - displaying the drawer
* /
private fun showLastDetectionResult ( ) {
// Run in background to avoid blocking UI
ocrExecutor . execute {
try {
// Use a proper coroutine scope for suspend function
val results = runBlocking {
val storageService = ServiceLocator . getStorageService ( )
storageService . getDetectionResults (
filter = null ,
sortBy = com . quillstudios . pokegoalshelper . models . DetectionSortBy . TIMESTAMP_DESC
)
}
val latestResult = results . firstOrNull ( )
if ( latestResult != null ) {
// Show the drawer on the main thread
handler . post {
detectionResultHandler ?. let { handler ->
// Use reflection to access the private bottomDrawer field
try {
val bottomDrawer = handler . javaClass . getDeclaredField ( " bottomDrawer " )
bottomDrawer . isAccessible = true
val drawer = bottomDrawer . get ( handler )
drawer . javaClass . getMethod ( " show " , com . quillstudios . pokegoalshelper . models . DetectionResult :: class . java )
. invoke ( drawer , latestResult )
PGHLog . d ( TAG , " ✅ Re-showed last detection result: ${latestResult.id} " )
} catch ( e : Exception ) {
PGHLog . e ( TAG , " ❌ Error accessing drawer via reflection " , e )
}
}
}
} else {
PGHLog . i ( TAG , " ℹ️ No previous detection results to show " )
}
} catch ( e : Exception ) {
PGHLog . e ( TAG , " ❌ Error showing last detection result " , e )
}
}
}
/ * *
* Toggle the detection overlay on / off
* /
private fun toggleOverlay ( ) {
overlayEnabled = ! overlayEnabled
if ( overlayEnabled && lastDetections . isNotEmpty ( ) ) {
showYOLODetectionOverlay ( lastDetections )
PGHLog . d ( TAG , " ✅ Detection overlay enabled " )
} else {
hideDetectionOverlay ( )
PGHLog . d ( TAG , " ✅ Detection overlay disabled " )
}
}
/ * *
* Return to the main app
* /
private fun returnToMainApp ( ) {
try {
val intent = packageManager . getLaunchIntentForPackage ( packageName )
intent ?. addFlags ( Intent . FLAG_ACTIVITY_NEW_TASK or Intent . FLAG_ACTIVITY_CLEAR_TOP )
startActivity ( intent )
PGHLog . d ( TAG , " ✅ Returned to main app " )
} catch ( e : Exception ) {
PGHLog . e ( TAG , " ❌ Error returning to main app " , e )
}
}
}