Skip to content

Commit da8a1d0

Browse files
committed
Improve reliability of foreground app switching
1 parent c335139 commit da8a1d0

File tree

4 files changed

+28
-110
lines changed

4 files changed

+28
-110
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@
3535
</intent-filter>
3636
</activity>
3737

38-
<activity
39-
android:name=".UnlockActivity"
40-
android:excludeFromRecents="true"
41-
android:exported="false"
42-
android:launchMode="singleInstance"
43-
android:noHistory="true"
44-
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
45-
4638
<service
4739
android:name=".DetectionService"
4840
android:enabled="true"

app/src/main/kotlin/com/observer/effect/DetectionService.kt

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.observer.effect
22

3+
import android.app.ActivityManager
34
import android.app.KeyguardManager
45
import android.app.Notification
56
import android.app.NotificationChannel
@@ -15,12 +16,14 @@ import android.os.IBinder
1516
import android.os.PowerManager
1617
import android.util.Log
1718
import androidx.camera.core.CameraSelector
19+
import androidx.lifecycle.Lifecycle
1820
import androidx.lifecycle.LifecycleOwner
21+
import androidx.lifecycle.LifecycleRegistry
1922

2023
class 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)

app/src/main/kotlin/com/observer/effect/ServiceLifecycleProvider.kt

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/src/main/kotlin/com/observer/effect/UnlockActivity.kt

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)