LastViewedEntryStore.kt

1.7 KB · raw

1package com.peachpasswords.vault23import android.content.Context4import androidx.security.crypto.EncryptedSharedPreferences5import androidx.security.crypto.MasterKey6import dagger.hilt.android.qualifiers.ApplicationContext7import javax.inject.Inject8import javax.inject.Singleton910/**11 * Remembers the id of the entry the user last opened so a fresh unlock can land12 * straight back on it (matching the extension, which opens the last entry rather13 * than the list). Stored in EncryptedSharedPreferences — an entry id is a weak14 * pointer, but it should not be trivially readable at rest, and it sits in the15 * same encrypted-prefs family as the duress/license stores.16 *17 * The id is only a hint: the post-unlock resolver re-validates it against the18 * live vault, so a stale id (entry since deleted) silently falls back to the19 * first entry.20 */21@Singleton22class LastViewedEntryStore @Inject constructor(23    @ApplicationContext context: Context,24) {25    private val masterKey = MasterKey.Builder(context)26        .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)27        .build()2829    private val prefs = EncryptedSharedPreferences.create(30        context,31        PREFS_FILE,32        masterKey,33        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,34        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,35    )3637    fun get(): String? = prefs.getString(KEY_LAST_VIEWED, null)3839    fun set(id: String) { prefs.edit().putString(KEY_LAST_VIEWED, id).apply() }4041    fun clear() { prefs.edit().remove(KEY_LAST_VIEWED).apply() }4243    companion object {44        private const val PREFS_FILE = "peach_nav_prefs"45        private const val KEY_LAST_VIEWED = "last_viewed_entry_id"46    }47}