Yokan / rebuild
Course 1 8624561

AppLockController.android.kt

composeApp/src/androidMain/kotlin/eu/friedcutlet/yokan/services/applock/AppLockController.android.kt

implementation71 lines
1package eu.friedcutlet.yokan.services.applock23import eu.friedcutlet.yokan.AndroidForegroundActivity4import android.app.KeyguardManager5import android.content.Context6import android.content.Intent78actual fun createPlatformAppLockController(): AppLockController = AndroidAppLockController()910/**11 * Bridges the singleton controller to the foreground activity's12 * `registerForActivityResult` launcher: the launcher must be registered in13 * `onCreate`, long before any unlock request happens.14 */15object AndroidAppLockBridge {16    /** Launches the confirm-credential intent; returns false when no launcher is registered. */17    var launch: ((Intent) -> Boolean)? = null1819    /** Invoked by the activity when the credential activity finishes. */20    var onResult: ((Boolean) -> Unit)? = null2122    fun deliverResult(success: Boolean) {23        onResult?.invoke(success)24        onResult = null25    }26}2728internal class AndroidAppLockController : BaseAppLockController() {29    override val isSupported: Boolean30        get() {31            val context = AndroidForegroundActivity.activity ?: return false32            val keyguard = context.getSystemService(Context.KEYGUARD_SERVICE) as? KeyguardManager33                ?: return false34            return keyguard.isDeviceSecure35        }3637    init {38        _unlocked.value = true39    }4041    override fun requestUnlock(onResult: (Boolean) -> Unit) {42        val activity = AndroidForegroundActivity.activity43        if (activity == null || !isSupported) {44            // No secure lock screen configured — nothing to authenticate against.45            _unlocked.value = true46            onResult(true)47            return48        }49        val keyguard = activity.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager50        @Suppress("DEPRECATION")51        val intent: Intent? = keyguard.createConfirmDeviceCredentialIntent(52            "Unlock Yokan",53            "Verify it's you to continue",54        )55        if (intent == null) {56            _unlocked.value = true57            onResult(true)58            return59        }60        AndroidAppLockBridge.onResult = { success ->61            _unlocked.value = success62            onResult(success)63        }64        val launched = AndroidAppLockBridge.launch?.invoke(intent) ?: false65        if (!launched) {66            // Cannot present the credential UI — stay locked instead of silently passing.67            AndroidAppLockBridge.onResult = null68            onResult(false)69        }70    }71}72

Search chapters, reference and source filenames.