-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature|fix|build] Support remove WebDAV server and account info; fix …
…#40; update dependencies
- Loading branch information
Showing
5 changed files
with
132 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/src/main/java/com/skyd/rays/model/AniVuEncryptedSharedPreferences.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.skyd.rays.model | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme | ||
import androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme | ||
import androidx.security.crypto.MasterKey | ||
import javax.crypto.AEADBadTagException | ||
|
||
class AniVuEncryptedSharedPreferences( | ||
private val encryptedSharedPreferences: EncryptedSharedPreferences, | ||
) : SharedPreferences { | ||
|
||
companion object { | ||
fun create( | ||
context: Context, | ||
fileName: String, | ||
masterKey: MasterKey, | ||
prefKeyEncryptionScheme: PrefKeyEncryptionScheme, | ||
prefValueEncryptionScheme: PrefValueEncryptionScheme, | ||
): AniVuEncryptedSharedPreferences { | ||
val creator: () -> EncryptedSharedPreferences = { | ||
EncryptedSharedPreferences.create( | ||
context, | ||
fileName, | ||
masterKey, | ||
prefKeyEncryptionScheme, | ||
prefValueEncryptionScheme | ||
) as EncryptedSharedPreferences | ||
} | ||
return AniVuEncryptedSharedPreferences( | ||
safeBlock(creator) | ||
?: context.deleteSharedPreferences(fileName).run { creator() } | ||
) | ||
} | ||
|
||
private fun <T> safeBlock(block: () -> T): T? { | ||
return try { | ||
block() | ||
} catch (e: AEADBadTagException) { | ||
e.printStackTrace() | ||
null | ||
} | ||
} | ||
} | ||
|
||
override fun getAll() = safeBlock { encryptedSharedPreferences.all }.orEmpty().toMutableMap() | ||
|
||
override fun getString(key: String?, defValue: String?): String? = safeBlock { | ||
encryptedSharedPreferences.getString(key, defValue) | ||
} ?: defValue | ||
|
||
override fun getStringSet( | ||
key: String?, | ||
defValues: MutableSet<String>?, | ||
): MutableSet<String>? = safeBlock { | ||
encryptedSharedPreferences.getStringSet(key, defValues) | ||
} ?: defValues | ||
|
||
override fun getInt(key: String?, defValue: Int): Int = safeBlock { | ||
encryptedSharedPreferences.getInt(key, defValue) | ||
} ?: defValue | ||
|
||
override fun getLong(key: String?, defValue: Long): Long = safeBlock { | ||
encryptedSharedPreferences.getLong(key, defValue) | ||
} ?: defValue | ||
|
||
override fun getFloat(key: String?, defValue: Float): Float = safeBlock { | ||
encryptedSharedPreferences.getFloat(key, defValue) | ||
} ?: defValue | ||
|
||
override fun getBoolean(key: String?, defValue: Boolean): Boolean = safeBlock { | ||
encryptedSharedPreferences.getBoolean(key, defValue) | ||
} ?: defValue | ||
|
||
override fun contains(key: String?): Boolean = safeBlock { | ||
encryptedSharedPreferences.contains(key) | ||
} ?: false | ||
|
||
override fun edit(): SharedPreferences.Editor = encryptedSharedPreferences.edit() | ||
|
||
override fun registerOnSharedPreferenceChangeListener( | ||
listener: SharedPreferences.OnSharedPreferenceChangeListener, | ||
) = encryptedSharedPreferences.registerOnSharedPreferenceChangeListener(listener) | ||
|
||
override fun unregisterOnSharedPreferenceChangeListener( | ||
listener: SharedPreferences.OnSharedPreferenceChangeListener, | ||
) = encryptedSharedPreferences.unregisterOnSharedPreferenceChangeListener(listener) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters