chore: code refactor

This commit is contained in:
rebelonion 2024-02-06 02:16:10 -06:00
parent 8d7b86a667
commit a2e44da99d
334 changed files with 3550 additions and 3092 deletions

View file

@ -22,12 +22,18 @@ object PrefManager {
private var protectedPreferences: SharedPreferences? = null
fun init(context: Context) { //must be called in Application class or will crash
generalPreferences = context.getSharedPreferences(Location.General.location, Context.MODE_PRIVATE)
playerPreferences = context.getSharedPreferences(Location.Player.location, Context.MODE_PRIVATE)
readerPreferences = context.getSharedPreferences(Location.Reader.location, Context.MODE_PRIVATE)
irrelevantPreferences = context.getSharedPreferences(Location.Irrelevant.location, Context.MODE_PRIVATE)
animeDownloadsPreferences = context.getSharedPreferences(Location.AnimeDownloads.location, Context.MODE_PRIVATE)
protectedPreferences = context.getSharedPreferences(Location.Protected.location, Context.MODE_PRIVATE)
generalPreferences =
context.getSharedPreferences(Location.General.location, Context.MODE_PRIVATE)
playerPreferences =
context.getSharedPreferences(Location.Player.location, Context.MODE_PRIVATE)
readerPreferences =
context.getSharedPreferences(Location.Reader.location, Context.MODE_PRIVATE)
irrelevantPreferences =
context.getSharedPreferences(Location.Irrelevant.location, Context.MODE_PRIVATE)
animeDownloadsPreferences =
context.getSharedPreferences(Location.AnimeDownloads.location, Context.MODE_PRIVATE)
protectedPreferences =
context.getSharedPreferences(Location.Protected.location, Context.MODE_PRIVATE)
Compat.importOldPrefs(context)
}
@ -49,7 +55,7 @@ object PrefManager {
}
@Suppress("UNCHECKED_CAST")
fun <T> getVal(prefName: PrefName, default: T) : T {
fun <T> getVal(prefName: PrefName, default: T): T {
return try {
val pref = getPrefLocation(prefName.data.prefLocation)
when (prefName.data.type) {
@ -58,8 +64,17 @@ object PrefManager {
Float::class -> pref.getFloat(prefName.name, default as Float) as T
Long::class -> pref.getLong(prefName.name, default as Long) as T
String::class -> pref.getString(prefName.name, default as String?) as T
Set::class -> convertFromStringSet(pref.getStringSet(prefName.name, null), default) as T
List::class -> deserializeClass(prefName.name, default, prefName.data.prefLocation) as T
Set::class -> convertFromStringSet(
pref.getStringSet(prefName.name, null),
default
) as T
List::class -> deserializeClass(
prefName.name,
default,
prefName.data.prefLocation
) as T
else -> throw IllegalArgumentException("Type not supported")
}
} catch (e: Exception) {
@ -68,17 +83,34 @@ object PrefManager {
}
@Suppress("UNCHECKED_CAST")
fun <T> getVal(prefName: PrefName) : T {
fun <T> getVal(prefName: PrefName): T {
return try {
val pref = getPrefLocation(prefName.data.prefLocation)
when (prefName.data.type) {
Boolean::class -> pref.getBoolean(prefName.name, prefName.data.default as Boolean) as T
Boolean::class -> pref.getBoolean(
prefName.name,
prefName.data.default as Boolean
) as T
Int::class -> pref.getInt(prefName.name, prefName.data.default as Int) as T
Float::class -> pref.getFloat(prefName.name, prefName.data.default as Float) as T
Long::class -> pref.getLong(prefName.name, prefName.data.default as Long) as T
String::class -> pref.getString(prefName.name, prefName.data.default as String?) as T
Set::class -> convertFromStringSet(pref.getStringSet(prefName.name, null), prefName.data.default) as T
List::class -> deserializeClass(prefName.name, prefName.data.default, prefName.data.prefLocation) as T
String::class -> pref.getString(
prefName.name,
prefName.data.default as String?
) as T
Set::class -> convertFromStringSet(
pref.getStringSet(prefName.name, null),
prefName.data.default
) as T
List::class -> deserializeClass(
prefName.name,
prefName.data.default,
prefName.data.prefLocation
) as T
else -> throw IllegalArgumentException("Type not supported")
}
} catch (e: Exception) {
@ -87,7 +119,10 @@ object PrefManager {
}
@Suppress("UNCHECKED_CAST")
fun <T> getNullableVal(prefName: PrefName, default: T?) : T? { //Strings don't necessarily need to use this one
fun <T> getNullableVal(
prefName: PrefName,
default: T?
): T? { //Strings don't necessarily need to use this one
return try {
val pref = getPrefLocation(prefName.data.prefLocation)
when (prefName.data.type) {
@ -96,7 +131,11 @@ object PrefManager {
Float::class -> pref.getFloat(prefName.name, default as Float) as T?
Long::class -> pref.getLong(prefName.name, default as Long) as T?
String::class -> pref.getString(prefName.name, default as String?) as T?
Set::class -> convertFromStringSet(pref.getStringSet(prefName.name, null), default) as T?
Set::class -> convertFromStringSet(
pref.getStringSet(prefName.name, null),
default
) as T?
else -> deserializeClass(prefName.name, default, prefName.data.prefLocation)
}
} catch (e: Exception) {
@ -113,7 +152,11 @@ object PrefManager {
is Float -> irrelevantPreferences!!.getFloat(key, default) as T
is Long -> irrelevantPreferences!!.getLong(key, default) as T
is String -> irrelevantPreferences!!.getString(key, default) as T
is Set<*> -> convertFromStringSet(irrelevantPreferences!!.getStringSet(key, null), default) as T
is Set<*> -> convertFromStringSet(
irrelevantPreferences!!.getStringSet(key, null),
default
) as T
else -> throw IllegalArgumentException("Type not supported")
}
} catch (e: Exception) {
@ -125,12 +168,36 @@ object PrefManager {
fun <T> getNullableCustomVal(key: String, default: T?, clazz: Class<T>): T? {
return try {
when {
clazz.isAssignableFrom(Boolean::class.java) -> irrelevantPreferences!!.getBoolean(key, default as? Boolean ?: false) as T?
clazz.isAssignableFrom(Int::class.java) -> irrelevantPreferences!!.getInt(key, default as? Int ?: 0) as T?
clazz.isAssignableFrom(Float::class.java) -> irrelevantPreferences!!.getFloat(key, default as? Float ?: 0f) as T?
clazz.isAssignableFrom(Long::class.java) -> irrelevantPreferences!!.getLong(key, default as? Long ?: 0L) as T?
clazz.isAssignableFrom(String::class.java) -> irrelevantPreferences!!.getString(key, default as? String) as T?
clazz.isAssignableFrom(Set::class.java) -> convertFromStringSet(irrelevantPreferences!!.getStringSet(key, null), default) as T?
clazz.isAssignableFrom(Boolean::class.java) -> irrelevantPreferences!!.getBoolean(
key,
default as? Boolean ?: false
) as T?
clazz.isAssignableFrom(Int::class.java) -> irrelevantPreferences!!.getInt(
key,
default as? Int ?: 0
) as T?
clazz.isAssignableFrom(Float::class.java) -> irrelevantPreferences!!.getFloat(
key,
default as? Float ?: 0f
) as T?
clazz.isAssignableFrom(Long::class.java) -> irrelevantPreferences!!.getLong(
key,
default as? Long ?: 0L
) as T?
clazz.isAssignableFrom(String::class.java) -> irrelevantPreferences!!.getString(
key,
default as? String
) as T?
clazz.isAssignableFrom(Set::class.java) -> convertFromStringSet(
irrelevantPreferences!!.getStringSet(key, null),
default
) as T?
else -> deserializeClass(key, default, Location.Irrelevant)
}
} catch (e: Exception) {
@ -173,7 +240,7 @@ object PrefManager {
}
@Suppress("UNCHECKED_CAST")
fun <T> getLiveVal(prefName: PrefName, default: T) : SharedPreferenceLiveData<T> {
fun <T> getLiveVal(prefName: PrefName, default: T): SharedPreferenceLiveData<T> {
val pref = getPrefLocation(prefName.data.prefLocation)
return when (prefName.data.type) {
Boolean::class -> SharedPreferenceBooleanLiveData(
@ -181,31 +248,37 @@ object PrefManager {
prefName.name,
default as Boolean
) as SharedPreferenceLiveData<T>
Int::class -> SharedPreferenceIntLiveData(
pref,
prefName.name,
default as Int
) as SharedPreferenceLiveData<T>
Float::class -> SharedPreferenceFloatLiveData(
pref,
prefName.name,
default as Float
) as SharedPreferenceLiveData<T>
Long::class -> SharedPreferenceLongLiveData(
pref,
prefName.name,
default as Long
) as SharedPreferenceLiveData<T>
String::class -> SharedPreferenceStringLiveData(
pref,
prefName.name,
default as String
) as SharedPreferenceLiveData<T>
Set::class -> SharedPreferenceStringSetLiveData(
pref,
prefName.name,
default as Set<String>
) as SharedPreferenceLiveData<T>
else -> throw IllegalArgumentException("Type not supported")
}
}
@ -234,7 +307,8 @@ object PrefManager {
this as? SharedPreferenceStringSetLiveData
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Set<String>>")
fun getAnimeDownloadPreferences(): SharedPreferences = animeDownloadsPreferences!! //needs to be used externally
fun getAnimeDownloadPreferences(): SharedPreferences =
animeDownloadsPreferences!! //needs to be used externally
fun exportAllPrefs(prefLocation: List<Location>): String {
return PreferencePackager.pack(
@ -272,7 +346,7 @@ object PrefManager {
return if (hadError) {
snackString("Error importing preferences")
false
} else {
} else {
snackString("Preferences imported")
true
}
@ -331,7 +405,7 @@ object PrefManager {
}
private fun <T> serializeClass(key: String, value: T, location: Location){
private fun <T> serializeClass(key: String, value: T, location: Location) {
val pref = getPrefLocation(location)
try {
val bos = ByteArrayOutputStream()

View file

@ -2,8 +2,8 @@ package ani.dantotsu.settings.saving
import android.graphics.Color
import ani.dantotsu.connections.mal.MAL
import ani.dantotsu.settings.saving.internal.Pref
import ani.dantotsu.settings.saving.internal.Location
import ani.dantotsu.settings.saving.internal.Pref
enum class PrefName(val data: Pref) { //TODO: Split this into multiple files
//General
@ -20,7 +20,13 @@ enum class PrefName(val data: Pref) { //TODO: Split this into multiple files
CheckUpdate(Pref(Location.General, Boolean::class, true)),
VerboseLogging(Pref(Location.General, Boolean::class, false)),
DohProvider(Pref(Location.General, Int::class, 0)),
DefaultUserAgent(Pref(Location.General, String::class, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:110.0) Gecko/20100101 Firefox/110.0")),
DefaultUserAgent(
Pref(
Location.General,
String::class,
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:110.0) Gecko/20100101 Firefox/110.0"
)
),
AnimeSourcesOrder(Pref(Location.General, List::class, listOf<String>())),
AnimeSearchHistory(Pref(Location.General, Set::class, setOf<String>())),
MangaSourcesOrder(Pref(Location.General, List::class, listOf<String>())),
@ -42,7 +48,13 @@ enum class PrefName(val data: Pref) { //TODO: Split this into multiple files
ImmersiveMode(Pref(Location.UI, Boolean::class, false)),
SmallView(Pref(Location.UI, Boolean::class, true)),
DefaultStartUpTab(Pref(Location.UI, Int::class, 1)),
HomeLayoutShow(Pref(Location.UI, List::class, listOf(true, false, false, true, false, false, true))),
HomeLayoutShow(
Pref(
Location.UI,
List::class,
listOf(true, false, false, true, false, false, true)
)
),
BannerAnimations(Pref(Location.UI, Boolean::class, true)),
LayoutAnimations(Pref(Location.UI, Boolean::class, true)),
AnimationSpeed(Pref(Location.UI, Float::class, 1f)),

View file

@ -1,8 +1,8 @@
package ani.dantotsu.settings.saving.internal
import android.content.Context
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
class Compat {
companion object {

View file

@ -8,6 +8,7 @@ data class Pref(
val type: KClass<*>,
val default: Any
)
enum class Location(val location: String, val exportable: Boolean) {
General("ani.dantotsu.general", true),
UI("ani.dantotsu.ui", true),

View file

@ -2,7 +2,6 @@ package ani.dantotsu.settings.saving.internal
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import java.security.KeyStore
import java.security.SecureRandom
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
@ -15,7 +14,8 @@ import javax.crypto.spec.PBEKeySpec
class PreferenceKeystore {
companion object {
fun generateKey(alias: String) {
val keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
val keyGenerator =
KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
keyGenerator.init(
KeyGenParameterSpec.Builder(
@ -30,17 +30,31 @@ class PreferenceKeystore {
keyGenerator.generateKey()
}
fun encryptWithPassword(password: CharArray, plaintext: String, salt: ByteArray): ByteArray {
fun encryptWithPassword(
password: CharArray,
plaintext: String,
salt: ByteArray
): ByteArray {
val secretKey = deriveKeyFromPassword(password, salt)
val cipher = Cipher.getInstance("${KeyProperties.KEY_ALGORITHM_AES}/${KeyProperties.BLOCK_MODE_CBC}/${KeyProperties.ENCRYPTION_PADDING_PKCS7}")
val cipher =
Cipher.getInstance("${KeyProperties.KEY_ALGORITHM_AES}/${KeyProperties.BLOCK_MODE_CBC}/${KeyProperties.ENCRYPTION_PADDING_PKCS7}")
cipher.init(Cipher.ENCRYPT_MODE, secretKey, IvParameterSpec(ByteArray(16)))
return cipher.doFinal(plaintext.toByteArray(Charsets.UTF_8))
}
fun decryptWithPassword(password: CharArray, ciphertext: ByteArray, salt: ByteArray): String {
fun decryptWithPassword(
password: CharArray,
ciphertext: ByteArray,
salt: ByteArray
): String {
val secretKey = deriveKeyFromPassword(password, salt)
val cipher = Cipher.getInstance("${KeyProperties.KEY_ALGORITHM_AES}/${KeyProperties.BLOCK_MODE_CBC}/${KeyProperties.ENCRYPTION_PADDING_PKCS7}")
cipher.init(Cipher.DECRYPT_MODE, secretKey, IvParameterSpec(ByteArray(16))) // Use the correct IV
val cipher =
Cipher.getInstance("${KeyProperties.KEY_ALGORITHM_AES}/${KeyProperties.BLOCK_MODE_CBC}/${KeyProperties.ENCRYPTION_PADDING_PKCS7}")
cipher.init(
Cipher.DECRYPT_MODE,
secretKey,
IvParameterSpec(ByteArray(16))
) // Use the correct IV
return cipher.doFinal(ciphertext).toString(Charsets.UTF_8)
}

View file

@ -1,11 +1,7 @@
package ani.dantotsu.settings.saving.internal
import android.content.SharedPreferences
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import ani.dantotsu.connections.discord.serializers.Activity
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.toast
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
@ -28,8 +24,10 @@ class PreferencePackager {
*/
fun unpack(decryptedJson: String): Boolean {
val gson = Gson()
val type = object : TypeToken<Map<String, Map<String, Map<String, Any>>>>() {}.type //oh god...
val rawPrefsMap: Map<String, Map<String, Map<String, Any>>> = gson.fromJson(decryptedJson, type)
val type = object :
TypeToken<Map<String, Map<String, Map<String, Any>>>>() {}.type //oh god...
val rawPrefsMap: Map<String, Map<String, Map<String, Any>>> =
gson.fromJson(decryptedJson, type)
val deserializedMap = mutableMapOf<String, Map<String, Any?>>()