Prefs refactoring

This commit is contained in:
albexk
2023-11-23 14:19:51 +03:00
parent c34c3e51ea
commit 6d6710db4a
+12 -23
View File
@@ -1,35 +1,24 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.amnezia.vpn package org.amnezia.vpn
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.util.Log
import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey import androidx.security.crypto.MasterKey
object Prefs { private const val TAG = "Prefs"
// Opens and returns an instance of EncryptedSharedPreferences private const val PREFS_FILE = "org.amnezia.vpn.prefs"
fun get(context: Context): SharedPreferences { private const val SECURE_PREFS_FILE = PREFS_FILE + ".secure"
try {
val mainKey = MasterKey.Builder(context.applicationContext)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
val sharedPrefsFile = "com.amnezia.vpn.secure.prefs" object Prefs {
val sharedPreferences: SharedPreferences = EncryptedSharedPreferences.create( fun get(context: Context, appContext: Context = context.applicationContext): SharedPreferences =
context.applicationContext, try {
sharedPrefsFile, EncryptedSharedPreferences(
mainKey, appContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, SECURE_PREFS_FILE,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM MasterKey(appContext)
) )
return sharedPreferences
} catch (e: Exception) { } catch (e: Exception) {
Log.e("Android-Prefs", "Getting Encryption Storage failed, plaintext fallback") Log.e(TAG, "Getting Encryption Storage failed, plaintext fallback")
return context.getSharedPreferences("com.amnezia.vpn.preferences", Context.MODE_PRIVATE) appContext.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE)
} }
}
} }