package com.peachpasswords.vault import android.content.Context import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKey import dagger.hilt.android.qualifiers.ApplicationContext import javax.inject.Inject import javax.inject.Singleton /** * Remembers the id of the entry the user last opened so a fresh unlock can land * straight back on it (matching the extension, which opens the last entry rather * than the list). Stored in EncryptedSharedPreferences — an entry id is a weak * pointer, but it should not be trivially readable at rest, and it sits in the * same encrypted-prefs family as the duress/license stores. * * The id is only a hint: the post-unlock resolver re-validates it against the * live vault, so a stale id (entry since deleted) silently falls back to the * first entry. */ @Singleton class LastViewedEntryStore @Inject constructor( @ApplicationContext context: Context, ) { private val masterKey = MasterKey.Builder(context) .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) .build() private val prefs = EncryptedSharedPreferences.create( context, PREFS_FILE, masterKey, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM, ) fun get(): String? = prefs.getString(KEY_LAST_VIEWED, null) fun set(id: String) { prefs.edit().putString(KEY_LAST_VIEWED, id).apply() } fun clear() { prefs.edit().remove(KEY_LAST_VIEWED).apply() } companion object { private const val PREFS_FILE = "peach_nav_prefs" private const val KEY_LAST_VIEWED = "last_viewed_entry_id" } }