@@ -10,6 +10,7 @@ import android.content.pm.ServiceInfo
1010import android.graphics.PixelFormat
1111import android.os.Build
1212import android.os.IBinder
13+ import android.util.Log
1314import android.view.Gravity
1415import android.view.MotionEvent
1516import android.view.View
@@ -41,6 +42,7 @@ class OverlayService : Service() {
4142 const val ACTION_STOP = " com.joebuildsthings.gamedeck.STOP"
4243 private const val CHANNEL_ID = " gamedeck_overlay"
4344 private const val NOTIFICATION_ID = 1
45+ private const val TAG = " GameDeck"
4446 }
4547
4648 private var windowManager: WindowManager ? = null
@@ -50,23 +52,31 @@ class OverlayService : Service() {
5052 override fun onBind (intent : Intent ? ): IBinder ? = null
5153
5254 override fun onStartCommand (intent : Intent ? , flags : Int , startId : Int ): Int {
55+ // 1. Start foreground immediately to satisfy Android's 5-second rule
56+ try {
57+ val notification = buildNotification()
58+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
59+ startForeground(NOTIFICATION_ID , notification, ServiceInfo .FOREGROUND_SERVICE_TYPE_SPECIAL_USE )
60+ } else {
61+ startForeground(NOTIFICATION_ID , notification)
62+ }
63+ Log .i(TAG , " Foreground service started successfully." )
64+ } catch (t: Throwable ) {
65+ Log .e(TAG , " Failed to start foreground service." , t)
66+ stopSelf()
67+ return START_NOT_STICKY
68+ }
69+
70+ // 2. Now handle the intent action
5371 when (intent?.action) {
5472 ACTION_START -> {
55- try {
56- val notification = buildNotification()
57- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
58- startForeground(
59- NOTIFICATION_ID ,
60- notification,
61- ServiceInfo .FOREGROUND_SERVICE_TYPE_SPECIAL_USE
62- )
63- } else {
64- startForeground(NOTIFICATION_ID , notification)
73+ if (overlayView == null ) {
74+ try {
75+ showOverlay()
76+ Log .i(TAG , " Overlay view added successfully." )
77+ } catch (t: Throwable ) {
78+ Log .e(TAG , " Failed to add overlay view." , t)
6579 }
66- if (overlayView == null ) showOverlay()
67- } catch (t: Throwable ) {
68- android.util.Log .e(" GameDeck" , " Overlay start failed" , t)
69- stopSelf()
7080 }
7181 }
7282 ACTION_STOP -> {
@@ -114,26 +124,22 @@ class OverlayService : Service() {
114124
115125 private fun showOverlay () {
116126 windowManager = getSystemService(Context .WINDOW_SERVICE ) as WindowManager
117-
118127 val params = WindowManager .LayoutParams (
119128 WindowManager .LayoutParams .WRAP_CONTENT ,
120129 WindowManager .LayoutParams .WRAP_CONTENT ,
121130 WindowManager .LayoutParams .TYPE_APPLICATION_OVERLAY ,
122- WindowManager .LayoutParams .FLAG_NOT_FOCUSABLE or
123- WindowManager .LayoutParams .FLAG_LAYOUT_IN_SCREEN ,
131+ WindowManager .LayoutParams .FLAG_NOT_FOCUSABLE or WindowManager .LayoutParams .FLAG_LAYOUT_IN_SCREEN ,
124132 PixelFormat .TRANSLUCENT
125133 ).apply {
126134 gravity = Gravity .TOP or Gravity .START
127135 x = 40
128136 y = 200
129137 }
130138 layoutParams = params
131-
132139 val view = ComposeView (this ).apply {
133140 setViewCompositionStrategy(ViewCompositionStrategy .DisposeOnDetachedFromWindow )
134141 setContent { GameDeckTheme { Pill () } }
135142 }
136-
137143 var initialX = 0
138144 var initialY = 0
139145 var touchX = 0f
@@ -156,7 +162,6 @@ class OverlayService : Service() {
156162 else -> false
157163 }
158164 }
159-
160165 windowManager?.addView(view, params)
161166 overlayView = view
162167 }
0 commit comments