keyboard: support lock-screen (Direct Boot) input via device-protected settings
- Commit
941bd5057c3b5a933ba10a55e8d16a651508ee1a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-09 18:17 UTC (3 months ago)
- Parents
656a9d6
Diff
keyboard/src/main/AndroidManifest.xml
diff --git a/keyboard/src/main/AndroidManifest.xml b/keyboard/src/main/AndroidManifest.xmlindex bfa89d98b5..83a5154207 100644--- a/keyboard/src/main/AndroidManifest.xml+++ b/keyboard/src/main/AndroidManifest.xml@@ -24,10 +24,15 @@ </intent-filter> </activity> - <!-- The input method (IME) service the system binds to. -->+ <!-- The input method (IME) service the system binds to.+ directBootAware: required so the keyboard is available on the lock screen after+ a reboot (Direct Boot, before the first unlock) — otherwise you couldn't type+ your password/PIN password. Any data touched before unlock must come from+ device-protected storage (see KeyboardService). --> <service android:name=".ime.KeyboardService" android:label="@string/app_name"+ android:directBootAware="true" android:permission="android.permission.BIND_INPUT_METHOD" android:exported="true"> <intent-filter>
keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardService.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardService.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardService.ktindex 1b30587e6f..f800ac3511 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardService.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardService.kt@@ -133,7 +133,7 @@ class KeyboardService : InputMethodService(), savedStateRegistryController.performRestore(null) lifecycleRegistry.currentState = Lifecycle.State.CREATED - ds = DataStoreUtils.getInstance(this)+ ds = DataStoreUtils.getInstance(this, deviceProtected = true) kbState.settings = KeyboardSettings.load(ds) vibrator = getSystemService(VibratorManager::class.java)?.defaultVibrator
keyboard/src/main/java/com/vayunmathur/keyboard/ui/SetupScreen.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/SetupScreen.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/SetupScreen.ktindex 7940bf33e4..c51764e4f1 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/SetupScreen.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/SetupScreen.kt@@ -62,7 +62,7 @@ import kotlinx.coroutines.launch @Composable fun SetupScreen() { val context = LocalContext.current- val ds = remember { DataStoreUtils.getInstance(context) }+ val ds = remember { DataStoreUtils.getInstance(context, deviceProtected = true) } val scope = rememberCoroutineScope() val imm = remember { context.getSystemService(InputMethodManager::class.java) }
library/src/main/java/com/vayunmathur/library/util/DataStoreUtils.kt
diff --git a/library/src/main/java/com/vayunmathur/library/util/DataStoreUtils.kt b/library/src/main/java/com/vayunmathur/library/util/DataStoreUtils.ktindex 010ed4e735..12cd8113c0 100644--- a/library/src/main/java/com/vayunmathur/library/util/DataStoreUtils.kt+++ b/library/src/main/java/com/vayunmathur/library/util/DataStoreUtils.kt@@ -202,8 +202,23 @@ class DataStoreUtils private constructor(context: Context) { companion object { @Volatile private var instance: DataStoreUtils? = null-- fun getInstance(context: Context): DataStoreUtils {+ @Volatile+ private var deviceProtectedInstance: DataStoreUtils? = null+ /**+ * @param deviceProtected when true, the store is backed by device-protected storage+ * (available in Direct Boot, i.e. before the first unlock after a reboot). Use this+ * for components that must run on the lock screen — e.g. an IME used to type the+ * unlock password. Data in device-protected storage is not credential-encrypted, so+ * only put non-sensitive settings there. The two modes are separate singletons/files.+ */+ fun getInstance(context: Context, deviceProtected: Boolean = false): DataStoreUtils {+ if (deviceProtected) {+ return deviceProtectedInstance ?: synchronized(this) {+ deviceProtectedInstance ?: DataStoreUtils(+ context.applicationContext.createDeviceProtectedStorageContext()+ ).also { deviceProtectedInstance = it }+ }+ } // First check (no locking for performance) return instance ?: synchronized(this) { // Second check (inside lock to ensure only one thread initializes)