Skip to content

Commit 6b38a09

Browse files
committed
implement OnBackPressedDispatcherOwner
1 parent 3aee36d commit 6b38a09

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

app/src/main/java/dev/pranav/applock/features/lockscreen/ui/LockScreenOverlayManager.kt

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import android.annotation.SuppressLint
44
import android.content.Context
55
import android.content.Intent
66
import android.graphics.PixelFormat
7-
import android.view.KeyEvent
87
import android.view.WindowManager
8+
import androidx.activity.OnBackPressedDispatcher
9+
import androidx.activity.OnBackPressedDispatcherOwner
910
import androidx.activity.compose.BackHandler
1011
import androidx.compose.ui.platform.ComposeView
1112
import androidx.lifecycle.*
@@ -19,7 +20,7 @@ import dev.pranav.applock.ui.theme.AppLockTheme
1920

2021
@SuppressLint("ViewConstructor")
2122
class LockScreenOverlayManager(private val context: Context):
22-
LifecycleOwner, ViewModelStoreOwner, SavedStateRegistryOwner {
23+
LifecycleOwner, ViewModelStoreOwner, SavedStateRegistryOwner, OnBackPressedDispatcherOwner {
2324

2425
private val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
2526
private var composeView: ComposeView? = null
@@ -34,6 +35,10 @@ class LockScreenOverlayManager(private val context: Context):
3435
override val savedStateRegistry: SavedStateRegistry get() = savedStateRegistryController.savedStateRegistry
3536
override val viewModelStore: ViewModelStore get() = store
3637

38+
override val onBackPressedDispatcher = OnBackPressedDispatcher {
39+
removeOverlay()
40+
}
41+
3742
fun showOverlay(
3843
lockedPackageName: String,
3944
triggeringPackageName: String,
@@ -144,20 +149,20 @@ class LockScreenOverlayManager(private val context: Context):
144149
composeView?.isFocusableInTouchMode = true
145150
composeView?.requestFocus()
146151

147-
// Block Back Button
148-
composeView?.setOnKeyListener { _, keyCode, event ->
149-
if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) {
150-
val intent = Intent(Intent.ACTION_MAIN).apply {
151-
addCategory(Intent.CATEGORY_HOME)
152-
flags = Intent.FLAG_ACTIVITY_NEW_TASK
153-
}
154-
context.startActivity(intent)
155-
156-
removeOverlay()
157-
return@setOnKeyListener true
158-
}
159-
false
160-
}
152+
//// Block Back Button
153+
//composeView?.setOnKeyListener { _, keyCode, event ->
154+
// if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) {
155+
// val intent = Intent(Intent.ACTION_MAIN).apply {
156+
// addCategory(Intent.CATEGORY_HOME)
157+
// flags = Intent.FLAG_ACTIVITY_NEW_TASK
158+
// }
159+
// context.startActivity(intent)
160+
//
161+
// removeOverlay()
162+
// return@setOnKeyListener true
163+
// }
164+
// false
165+
//}
161166

162167
try {
163168
windowManager.addView(composeView, params)

0 commit comments

Comments
 (0)