package eu.friedcutlet.yokan.services.applock import eu.friedcutlet.yokan.AndroidForegroundActivity import android.app.KeyguardManager import android.content.Context import android.content.Intent actual fun createPlatformAppLockController(): AppLockController = AndroidAppLockController() /** * Bridges the singleton controller to the foreground activity's * `registerForActivityResult` launcher: the launcher must be registered in * `onCreate`, long before any unlock request happens. */ object AndroidAppLockBridge { /** Launches the confirm-credential intent; returns false when no launcher is registered. */ var launch: ((Intent) -> Boolean)? = null /** Invoked by the activity when the credential activity finishes. */ var onResult: ((Boolean) -> Unit)? = null fun deliverResult(success: Boolean) { onResult?.invoke(success) onResult = null } } internal class AndroidAppLockController : BaseAppLockController() { override val isSupported: Boolean get() { val context = AndroidForegroundActivity.activity ?: return false val keyguard = context.getSystemService(Context.KEYGUARD_SERVICE) as? KeyguardManager ?: return false return keyguard.isDeviceSecure } init { _unlocked.value = true } override fun requestUnlock(onResult: (Boolean) -> Unit) { val activity = AndroidForegroundActivity.activity if (activity == null || !isSupported) { // No secure lock screen configured — nothing to authenticate against. _unlocked.value = true onResult(true) return } val keyguard = activity.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager @Suppress("DEPRECATION") val intent: Intent? = keyguard.createConfirmDeviceCredentialIntent( "Unlock Yokan", "Verify it's you to continue", ) if (intent == null) { _unlocked.value = true onResult(true) return } AndroidAppLockBridge.onResult = { success -> _unlocked.value = success onResult(success) } val launched = AndroidAppLockBridge.launch?.invoke(intent) ?: false if (!launched) { // Cannot present the credential UI — stay locked instead of silently passing. AndroidAppLockBridge.onResult = null onResult(false) } } }