11package com.observer.effect
22
3+ import android.app.ActivityManager
34import android.app.KeyguardManager
45import android.app.Notification
56import android.app.NotificationChannel
@@ -15,12 +16,14 @@ import android.os.IBinder
1516import android.os.PowerManager
1617import android.util.Log
1718import androidx.camera.core.CameraSelector
19+ import androidx.lifecycle.Lifecycle
1820import androidx.lifecycle.LifecycleOwner
21+ import androidx.lifecycle.LifecycleRegistry
1922
2023class DetectionService : Service (), LifecycleOwner {
21- private val serviceLifecycleProvider = ServiceLifecycleProvider ( )
22- override val lifecycle
23- get() = serviceLifecycleProvider.lifecycle
24+ private val lifecycleRegistry = LifecycleRegistry ( this )
25+ override val lifecycle: Lifecycle
26+ get() = lifecycleRegistry
2427 private lateinit var prefs: SharedPreferences
2528 private lateinit var wakeLock: PowerManager .WakeLock
2629 private lateinit var powerManager: PowerManager
@@ -63,7 +66,9 @@ class DetectionService : Service(), LifecycleOwner {
6366
6467 override fun onCreate () {
6568 super .onCreate()
66- serviceLifecycleProvider.onStart()
69+ lifecycleRegistry.currentState = Lifecycle .State .CREATED
70+ lifecycleRegistry.currentState = Lifecycle .State .STARTED
71+ lifecycleRegistry.currentState = Lifecycle .State .RESUMED
6772 Log .i(TAG , " Service created" )
6873
6974 prefs = getSharedPreferences(MainActivity .PREFS_NAME , MODE_PRIVATE )
@@ -141,7 +146,7 @@ class DetectionService : Service(), LifecycleOwner {
141146
142147 override fun onDestroy () {
143148 super .onDestroy()
144- serviceLifecycleProvider.onDestroy()
149+ lifecycleRegistry.currentState = Lifecycle . State . DESTROYED
145150 Log .i(TAG , " Service destroyed" )
146151 prefs.unregisterOnSharedPreferenceChangeListener(prefsListener)
147152
@@ -284,14 +289,26 @@ class DetectionService : Service(), LifecycleOwner {
284289 }
285290 }
286291
287- // Always launch unlock activity to dismiss lock screen and/or launch app
292+ // Launch app if configured
288293 val launchApp = prefs.getString(MainActivity .KEY_LAUNCH_APP , " " ) ? : " "
289- Log .i(TAG , " Launching unlock activity (app=$launchApp , locked=${keyguardManager.isKeyguardLocked} )" )
290- val intent = Intent (this , UnlockActivity ::class .java).apply {
291- addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
292- addFlags(Intent .FLAG_ACTIVITY_NO_ANIMATION )
294+ if (launchApp.isNotEmpty()) {
295+ try {
296+ Log .i(TAG , " Bringing app to foreground: $launchApp (locked=${keyguardManager.isKeyguardLocked} )" )
297+
298+ val launchIntent = packageManager.getLaunchIntentForPackage(launchApp)
299+ if (launchIntent != null ) {
300+ // Use CLEAR_TASK + NEW_TASK to forcefully bring the app to foreground
301+ // This clears the existing task and creates a fresh one on top
302+ launchIntent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TASK )
303+ startActivity(launchIntent)
304+ Log .i(TAG , " Launched app with CLEAR_TASK flags=${launchIntent.flags} " )
305+ } else {
306+ Log .w(TAG , " No launch intent found for package: $launchApp " )
307+ }
308+ } catch (e: Exception ) {
309+ Log .e(TAG , " Error launching app: $launchApp " , e)
310+ }
293311 }
294- startActivity(intent)
295312 }
296313 } catch (e: Exception ) {
297314 Log .e(TAG , " Error waking screen" , e)
0 commit comments