0.0.1版本
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package cn.airnan.a2fair.data
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import cn.airnan.a2fair.crypto.CryptoManager
|
||||
import cn.airnan.a2fair.data.dao.GroupDao
|
||||
import cn.airnan.a2fair.data.dao.TokenDao
|
||||
import cn.airnan.a2fair.data.entity.Group
|
||||
import cn.airnan.a2fair.data.entity.Token
|
||||
|
||||
@Database(entities = [Group::class, Token::class], version = 1, exportSchema = false)
|
||||
@TypeConverters(SecretTypeConverter::class, EnumConverters::class)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun groupDao(): GroupDao
|
||||
abstract fun tokenDao(): TokenDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var INSTANCE: AppDatabase? = null
|
||||
|
||||
fun getDatabase(context: Context, cryptoManager: CryptoManager): AppDatabase {
|
||||
return INSTANCE ?: synchronized(this) {
|
||||
val instance = Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
AppDatabase::class.java,
|
||||
"a2fair_database"
|
||||
)
|
||||
.addTypeConverter(SecretTypeConverter(cryptoManager))
|
||||
.build()
|
||||
INSTANCE = instance
|
||||
instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package cn.airnan.a2fair.data
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import cn.airnan.a2fair.data.entity.HashAlgorithm
|
||||
import cn.airnan.a2fair.data.entity.TokenType
|
||||
|
||||
class EnumConverters {
|
||||
|
||||
@TypeConverter
|
||||
fun fromTokenType(value: TokenType): String {
|
||||
return value.name
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toTokenType(value: String): TokenType {
|
||||
return try {
|
||||
TokenType.valueOf(value)
|
||||
} catch (e: Exception) {
|
||||
TokenType.TOTP
|
||||
}
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun fromHashAlgorithm(value: HashAlgorithm): String {
|
||||
return value.name
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toHashAlgorithm(value: String): HashAlgorithm {
|
||||
return try {
|
||||
HashAlgorithm.valueOf(value)
|
||||
} catch (e: Exception) {
|
||||
HashAlgorithm.SHA1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package cn.airnan.a2fair.data
|
||||
|
||||
data class SecretString(val value: String) {
|
||||
override fun toString(): String {
|
||||
return "***"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.airnan.a2fair.data
|
||||
|
||||
import androidx.room.ProvidedTypeConverter
|
||||
import androidx.room.TypeConverter
|
||||
import cn.airnan.a2fair.crypto.CryptoManager
|
||||
|
||||
@ProvidedTypeConverter
|
||||
class SecretTypeConverter(private val cryptoManager: CryptoManager) {
|
||||
|
||||
@TypeConverter
|
||||
fun fromSecretString(secretString: SecretString?): String? {
|
||||
if (secretString == null) return null
|
||||
return cryptoManager.encrypt(secretString.value)
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toSecretString(encryptedString: String?): SecretString? {
|
||||
if (encryptedString == null) return null
|
||||
return SecretString(cryptoManager.decrypt(encryptedString))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cn.airnan.a2fair.data.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import cn.airnan.a2fair.data.entity.Group
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface GroupDao {
|
||||
@Query("SELECT * FROM groups ORDER BY sortOrder ASC")
|
||||
fun getAllGroups(): Flow<List<Group>>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertGroup(group: Group): Long
|
||||
|
||||
@Update
|
||||
fun updateGroup(group: Group): Int
|
||||
|
||||
@Delete
|
||||
fun deleteGroup(group: Group): Int
|
||||
|
||||
@Query("SELECT * FROM groups WHERE id = :id LIMIT 1")
|
||||
fun getGroupById(id: Long): Group?
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package cn.airnan.a2fair.data.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import cn.airnan.a2fair.data.entity.Token
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface TokenDao {
|
||||
@Query("SELECT * FROM tokens ORDER BY sortOrder ASC")
|
||||
fun getAllTokens(): Flow<List<Token>>
|
||||
|
||||
@Query("SELECT * FROM tokens WHERE groupId = :groupId ORDER BY sortOrder ASC")
|
||||
fun getTokensByGroupId(groupId: Long): Flow<List<Token>>
|
||||
|
||||
@Query("SELECT * FROM tokens WHERE groupId IS NULL ORDER BY sortOrder ASC")
|
||||
fun getUncategorizedTokens(): Flow<List<Token>>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertToken(token: Token): Long
|
||||
|
||||
@Update
|
||||
fun updateToken(token: Token): Int
|
||||
|
||||
@Delete
|
||||
fun deleteToken(token: Token): Int
|
||||
|
||||
@Query("SELECT * FROM tokens WHERE id = :id LIMIT 1")
|
||||
fun getTokenById(id: Long): Token?
|
||||
|
||||
@Query("UPDATE tokens SET counter = :counter WHERE id = :id")
|
||||
fun updateCounter(id: Long, counter: Long): Int
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.airnan.a2fair.data.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "groups")
|
||||
data class Group(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val id: Long = 0,
|
||||
val name: String,
|
||||
val sortOrder: Int = 0
|
||||
)
|
||||
@@ -0,0 +1,42 @@
|
||||
package cn.airnan.a2fair.data.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
enum class TokenType {
|
||||
TOTP, HOTP, STEAM
|
||||
}
|
||||
|
||||
enum class HashAlgorithm {
|
||||
SHA1, SHA256, SHA512
|
||||
}
|
||||
|
||||
@Entity(
|
||||
tableName = "tokens",
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = Group::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["groupId"],
|
||||
onDelete = ForeignKey.SET_NULL
|
||||
)
|
||||
],
|
||||
indices = [Index("groupId")]
|
||||
)
|
||||
data class Token(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val id: Long = 0,
|
||||
val issuer: String = "",
|
||||
val serviceName: String,
|
||||
val secret: cn.airnan.a2fair.data.SecretString, // will be encrypted
|
||||
val tokenType: TokenType = TokenType.TOTP,
|
||||
val algorithm: HashAlgorithm = HashAlgorithm.SHA1,
|
||||
val digits: Int = 6,
|
||||
val period: Int = 30,
|
||||
val counter: Long = 0,
|
||||
val groupId: Long? = null,
|
||||
val icon: String? = null,
|
||||
val sortOrder: Int = 0
|
||||
)
|
||||
@@ -0,0 +1,153 @@
|
||||
package cn.airnan.a2fair.data.repository
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.intPreferencesKey
|
||||
import androidx.datastore.preferences.core.longPreferencesKey
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import cn.airnan.a2fair.crypto.CryptoManager
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
|
||||
|
||||
class SettingsRepository(private val context: Context, private val cryptoManager: CryptoManager) {
|
||||
|
||||
private val dataStore = context.dataStore
|
||||
|
||||
val isPinEnabled: Flow<Boolean> = dataStore.data.map { preferences ->
|
||||
preferences[PreferencesKeys.PIN_ENABLED] ?: false
|
||||
}
|
||||
|
||||
suspend fun setPinEnabled(enabled: Boolean) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PreferencesKeys.PIN_ENABLED] = enabled
|
||||
}
|
||||
}
|
||||
|
||||
val pinCode: Flow<String?> = dataStore.data.map { preferences ->
|
||||
val encryptedPin = preferences[PreferencesKeys.PIN_CODE]
|
||||
if (encryptedPin != null) {
|
||||
try {
|
||||
cryptoManager.decrypt(encryptedPin)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setPinCode(pin: String?) {
|
||||
dataStore.edit { preferences ->
|
||||
if (pin == null) {
|
||||
preferences.remove(PreferencesKeys.PIN_CODE)
|
||||
} else {
|
||||
preferences[PreferencesKeys.PIN_CODE] = cryptoManager.encrypt(pin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val isBiometricEnabled: Flow<Boolean> = dataStore.data.map { preferences ->
|
||||
preferences[PreferencesKeys.BIOMETRIC_ENABLED] ?: false
|
||||
}
|
||||
|
||||
suspend fun setBiometricEnabled(enabled: Boolean) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PreferencesKeys.BIOMETRIC_ENABLED] = enabled
|
||||
}
|
||||
}
|
||||
|
||||
val isFlagSecureEnabled: Flow<Boolean> = dataStore.data.map { preferences ->
|
||||
preferences[PreferencesKeys.FLAG_SECURE_ENABLED] ?: false
|
||||
}
|
||||
|
||||
suspend fun setFlagSecureEnabled(enabled: Boolean) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PreferencesKeys.FLAG_SECURE_ENABLED] = enabled
|
||||
}
|
||||
}
|
||||
|
||||
val failedAttempts: Flow<Int> = dataStore.data.map { preferences ->
|
||||
preferences[PreferencesKeys.FAILED_ATTEMPTS] ?: 0
|
||||
}
|
||||
|
||||
suspend fun setFailedAttempts(attempts: Int) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PreferencesKeys.FAILED_ATTEMPTS] = attempts
|
||||
}
|
||||
}
|
||||
|
||||
val lockoutEndTime: Flow<Long> = dataStore.data.map { preferences ->
|
||||
preferences[PreferencesKeys.LOCKOUT_END_TIME] ?: 0L
|
||||
}
|
||||
|
||||
suspend fun setLockoutEndTime(time: Long) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PreferencesKeys.LOCKOUT_END_TIME] = time
|
||||
}
|
||||
}
|
||||
|
||||
val themeMode: Flow<Int> = dataStore.data.map { preferences ->
|
||||
preferences[PreferencesKeys.THEME_MODE] ?: 0 // 0: System, 1: Light, 2: Dark
|
||||
}
|
||||
|
||||
suspend fun setThemeMode(mode: Int) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PreferencesKeys.THEME_MODE] = mode
|
||||
}
|
||||
}
|
||||
|
||||
val dynamicColor: Flow<Boolean> = dataStore.data.map { preferences ->
|
||||
preferences[PreferencesKeys.DYNAMIC_COLOR] ?: true
|
||||
}
|
||||
|
||||
suspend fun setDynamicColor(enabled: Boolean) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PreferencesKeys.DYNAMIC_COLOR] = enabled
|
||||
}
|
||||
}
|
||||
|
||||
val seedColor: Flow<Int> = dataStore.data.map { preferences ->
|
||||
preferences[PreferencesKeys.SEED_COLOR] ?: 0xFF6750A4.toInt() // Default to Material You purple
|
||||
}
|
||||
|
||||
suspend fun setSeedColor(color: Int) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PreferencesKeys.SEED_COLOR] = color
|
||||
}
|
||||
}
|
||||
|
||||
val language: Flow<Int> = dataStore.data.map { preferences ->
|
||||
preferences[PreferencesKeys.LANGUAGE] ?: 0 // 0: System, 1: zh-CN, 2: zh-TW, 3: en
|
||||
}
|
||||
|
||||
private val _languageChangeEvent = MutableSharedFlow<Unit>(extraBufferCapacity = 1)
|
||||
val languageChangeEvent: SharedFlow<Unit> = _languageChangeEvent
|
||||
|
||||
suspend fun setLanguage(code: Int) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PreferencesKeys.LANGUAGE] = code
|
||||
}
|
||||
_languageChangeEvent.emit(Unit)
|
||||
}
|
||||
|
||||
private object PreferencesKeys {
|
||||
val PIN_ENABLED = booleanPreferencesKey("pin_enabled")
|
||||
val PIN_CODE = stringPreferencesKey("pin_code")
|
||||
val BIOMETRIC_ENABLED = booleanPreferencesKey("biometric_enabled")
|
||||
val FLAG_SECURE_ENABLED = booleanPreferencesKey("flag_secure_enabled")
|
||||
val FAILED_ATTEMPTS = intPreferencesKey("failed_attempts")
|
||||
val LOCKOUT_END_TIME = longPreferencesKey("lockout_end_time")
|
||||
val THEME_MODE = intPreferencesKey("theme_mode")
|
||||
val DYNAMIC_COLOR = booleanPreferencesKey("dynamic_color")
|
||||
val SEED_COLOR = intPreferencesKey("seed_color")
|
||||
val LANGUAGE = intPreferencesKey("language")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.airnan.a2fair.data.repository
|
||||
|
||||
import cn.airnan.a2fair.data.dao.GroupDao
|
||||
import cn.airnan.a2fair.data.dao.TokenDao
|
||||
import cn.airnan.a2fair.data.entity.Group
|
||||
import cn.airnan.a2fair.data.entity.Token
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class TokenRepository(
|
||||
private val tokenDao: TokenDao,
|
||||
private val groupDao: GroupDao
|
||||
) {
|
||||
// Group
|
||||
fun getAllGroups(): Flow<List<Group>> = groupDao.getAllGroups()
|
||||
suspend fun getGroupById(id: Long): Group? = withContext(Dispatchers.IO) { groupDao.getGroupById(id) }
|
||||
suspend fun insertGroup(group: Group): Long = withContext(Dispatchers.IO) { groupDao.insertGroup(group) }
|
||||
suspend fun updateGroup(group: Group) { withContext(Dispatchers.IO) { groupDao.updateGroup(group) } }
|
||||
suspend fun deleteGroup(group: Group) { withContext(Dispatchers.IO) { groupDao.deleteGroup(group) } }
|
||||
|
||||
// Token
|
||||
fun getAllTokens(): Flow<List<Token>> = tokenDao.getAllTokens()
|
||||
fun getTokensByGroupId(groupId: Long): Flow<List<Token>> = tokenDao.getTokensByGroupId(groupId)
|
||||
fun getUncategorizedTokens(): Flow<List<Token>> = tokenDao.getUncategorizedTokens()
|
||||
suspend fun getTokenById(id: Long): Token? = withContext(Dispatchers.IO) { tokenDao.getTokenById(id) }
|
||||
suspend fun insertToken(token: Token): Long = withContext(Dispatchers.IO) { tokenDao.insertToken(token) }
|
||||
suspend fun updateToken(token: Token) { withContext(Dispatchers.IO) { tokenDao.updateToken(token) } }
|
||||
suspend fun deleteToken(token: Token) { withContext(Dispatchers.IO) { tokenDao.deleteToken(token) } }
|
||||
suspend fun updateCounter(id: Long, counter: Long) { withContext(Dispatchers.IO) { tokenDao.updateCounter(id, counter) } }
|
||||
}
|
||||
Reference in New Issue
Block a user