package `in`.caresoft.sahi import android.content.Context import android.content.SharedPreferences import android.util.Log import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKey /** * Server address and access key. * * Encrypted at rest, because this is a keyboard: anything it stores sits on a * device that gets lost, shared and rooted. If the keystore is unavailable — * it happens on some older or heavily modified builds — we fall back to plain * preferences rather than leaving the app unusable, and say so in the log. */ object Prefs { private const val FILE = "sahi" private const val KEY_SERVER = "server" private const val KEY_ACCESS = "key" private const val KEY_VARIETY = "variety" private lateinit var prefs: SharedPreferences fun init(context: Context) { prefs = try { val masterKey = MasterKey.Builder(context) .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) .build() EncryptedSharedPreferences.create( context, FILE, masterKey, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM ) } catch (e: Exception) { Log.w("Sahi", "Encrypted storage unavailable, falling back to plain preferences", e) context.getSharedPreferences(FILE, Context.MODE_PRIVATE) } } var server: String get() = prefs.getString(KEY_SERVER, "").orEmpty() set(value) = prefs.edit().putString(KEY_SERVER, value.trimEnd('/')).apply() var accessKey: String get() = prefs.getString(KEY_ACCESS, "").orEmpty() set(value) = prefs.edit().putString(KEY_ACCESS, value.trim()).apply() var variety: String get() = prefs.getString(KEY_VARIETY, "en-IN").orEmpty() set(value) = prefs.edit().putString(KEY_VARIETY, value).apply() val isConfigured: Boolean get() = server.isNotEmpty() && accessKey.isNotEmpty() fun clear() = prefs.edit().clear().apply() }