From c4dcf934aa909d5c143a55cbaef8661dde45a406 Mon Sep 17 00:00:00 2001 From: CherretGit Date: Thu, 30 Oct 2025 21:42:43 +0700 Subject: [PATCH] Config to json --- .../com/cherret/zaprett/data/ZaprettConfig.kt | 26 + .../cherret/zaprett/utils/ZaprettManager.kt | 695 +++++------------- 2 files changed, 208 insertions(+), 513 deletions(-) create mode 100644 app/src/main/java/com/cherret/zaprett/data/ZaprettConfig.kt diff --git a/app/src/main/java/com/cherret/zaprett/data/ZaprettConfig.kt b/app/src/main/java/com/cherret/zaprett/data/ZaprettConfig.kt new file mode 100644 index 0000000..b20da14 --- /dev/null +++ b/app/src/main/java/com/cherret/zaprett/data/ZaprettConfig.kt @@ -0,0 +1,26 @@ +package com.cherret.zaprett.data + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class ZaprettConfig( + @SerialName("active_lists") + val activeLists: List = emptyList(), + @SerialName("active_ipsets") + val activeIpsets: List = emptyList(), + @SerialName("active_exclude_lists") + val activeExcludeLists: List = emptyList(), + @SerialName("active_exclude_ipsets") + val activeExcludeIpsets: List = emptyList(), + @SerialName("list_type") + val listType: String = "whitelist", + @SerialName("strategy") + val strategy: String = "", + @SerialName("app_list") + val appList: String = "none", + @SerialName("whitelist") + val whitelist: List = emptyList(), + @SerialName("blacklist") + val blacklist: List = emptyList() +) diff --git a/app/src/main/java/com/cherret/zaprett/utils/ZaprettManager.kt b/app/src/main/java/com/cherret/zaprett/utils/ZaprettManager.kt index edbc0db..0789915 100644 --- a/app/src/main/java/com/cherret/zaprett/utils/ZaprettManager.kt +++ b/app/src/main/java/com/cherret/zaprett/utils/ZaprettManager.kt @@ -5,14 +5,43 @@ import android.content.Context.MODE_PRIVATE import android.content.SharedPreferences import android.os.Environment import android.util.Log -import com.topjohnwu.superuser.Shell -import java.io.File -import java.io.FileInputStream -import java.io.FileOutputStream -import java.io.IOException -import java.util.Properties import androidx.core.content.edit import com.cherret.zaprett.data.AppListType +import com.cherret.zaprett.data.ZaprettConfig +import com.topjohnwu.superuser.Shell +import kotlinx.serialization.json.Json +import java.io.File + +private val json = Json { + prettyPrint = true + ignoreUnknownKeys = true + coerceInputValues = true +} + +private fun readConfig(): ZaprettConfig { + val configFile = getConfigFile() + if (!configFile.exists()) { + return ZaprettConfig() + } + return try { + val content = configFile.readText() + if (content.isBlank()) ZaprettConfig() else json.decodeFromString(content) + } catch (e: Exception) { + Log.e("ZaprettManager", "Error reading config, returning defaults", e) + ZaprettConfig() + } +} + +private fun writeConfig(config: ZaprettConfig) { + val configFile = getConfigFile() + try { + configFile.parentFile?.mkdirs() + val content = json.encodeToString(config) + configFile.writeText(content) + } catch (e: Exception) { + Log.e("ZaprettManager", "Error writing config", e) + } +} fun checkRoot(callback: (Boolean) -> Unit) { Shell.getShell().isRoot.let { callback(it) } @@ -61,10 +90,10 @@ fun getBinVersion(callback: (String) -> Unit) { } fun getConfigFile(): File { - return File(Environment.getExternalStorageDirectory(), "zaprett/config") + return File(Environment.getExternalStorageDirectory(), "zaprett/config.json") } -fun setStartOnBoot(prefs : SharedPreferences, callback: (Boolean) -> Unit) { +fun setStartOnBoot(prefs: SharedPreferences, callback: (Boolean) -> Unit) { if (prefs.getBoolean("use_module", false)) { Shell.cmd("zaprett autostart").submit { result -> if (result.out.isNotEmpty() && result.out.toString().contains("true")) callback(true) else callback(false) @@ -72,27 +101,17 @@ fun setStartOnBoot(prefs : SharedPreferences, callback: (Boolean) -> Unit) { } } -fun getStartOnBoot(prefs : SharedPreferences, callback: (Boolean) -> Unit) { +fun getStartOnBoot(prefs: SharedPreferences, callback: (Boolean) -> Unit) { if (prefs.getBoolean("use_module", false)) { Shell.cmd("zaprett get-autostart").submit { result -> if (result.out.isNotEmpty() && result.out.toString().contains("true")) callback(true) else callback(false) } - } else { callback(false) } + } else { + callback(false) + } } fun getZaprettPath(): String { - val props = Properties() - val configFile = getConfigFile() - if (configFile.exists()) { - return try { - FileInputStream(configFile).use { input -> - props.load(input) - } - props.getProperty("zaprettdir", Environment.getExternalStorageDirectory().path + "/zaprett") - } catch (e: IOException) { - throw RuntimeException(e) - } - } return Environment.getExternalStorageDirectory().path + "/zaprett" } @@ -144,117 +163,42 @@ fun getAllByeDPIStrategies(): Array { ?: emptyArray() } -fun getAllStrategies(sharedPreferences : SharedPreferences) : Array { +fun getAllStrategies(sharedPreferences: SharedPreferences): Array { return if (sharedPreferences.getBoolean("use_module", false)) getAllNfqwsStrategies() - else getAllByeDPIStrategies() + else getAllByeDPIStrategies() } - fun getActiveLists(sharedPreferences: SharedPreferences): Array { if (sharedPreferences.getBoolean("use_module", false)) { - val configFile = getConfigFile() - if (configFile.exists()) { - val props = Properties() - return try { - FileInputStream(configFile).use { input -> - props.load(input) - } - val activeLists = props.getProperty("active_lists", "") - Log.d("Active lists", activeLists) - if (activeLists.isNotEmpty()) activeLists.split(",") - .toTypedArray() else emptyArray() - } catch (e: IOException) { - throw RuntimeException(e) - } - } - return emptyArray() - } - else { + return readConfig().activeLists.toTypedArray() + } else { return sharedPreferences.getStringSet("lists", emptySet())?.toTypedArray() ?: emptyArray() } } + fun getActiveIpsets(sharedPreferences: SharedPreferences): Array { if (sharedPreferences.getBoolean("use_module", false)) { - val configFile = getConfigFile() - if (configFile.exists()) { - val props = Properties() - return try { - FileInputStream(configFile).use { input -> - props.load(input) - } - val activeLists = props.getProperty("active_ipsets", "") - Log.d("Active ipsets", activeLists) - if (activeLists.isNotEmpty()) activeLists.split(",") - .toTypedArray() else emptyArray() - } catch (e: IOException) { - throw RuntimeException(e) - } - } - return emptyArray() - } - else return sharedPreferences.getStringSet("ipsets", emptySet())?.toTypedArray() ?: emptyArray() + return readConfig().activeIpsets.toTypedArray() + } else return sharedPreferences.getStringSet("ipsets", emptySet())?.toTypedArray() ?: emptyArray() } + fun getActiveExcludeLists(sharedPreferences: SharedPreferences): Array { if (sharedPreferences.getBoolean("use_module", false)) { - val configFile = getConfigFile() - if (configFile.exists()) { - val props = Properties() - return try { - FileInputStream(configFile).use { input -> - props.load(input) - } - val activeLists = props.getProperty("active_exclude_lists", "") - if (activeLists.isNotEmpty()) activeLists.split(",") - .toTypedArray() else emptyArray() - } catch (e: IOException) { - throw RuntimeException(e) - } - } - return emptyArray() - } - else { + return readConfig().activeExcludeLists.toTypedArray() + } else { return sharedPreferences.getStringSet("exclude_lists", emptySet())?.toTypedArray() ?: emptyArray() } } fun getActiveExcludeIpsets(sharedPreferences: SharedPreferences): Array { if (sharedPreferences.getBoolean("use_module", false)) { - val configFile = getConfigFile() - if (configFile.exists()) { - val props = Properties() - return try { - FileInputStream(configFile).use { input -> - props.load(input) - } - val activeLists = props.getProperty("active_exclude_ipsets", "") - Log.d("Active ipsets", activeLists) - if (activeLists.isNotEmpty()) activeLists.split(",") - .toTypedArray() else emptyArray() - } catch (e: IOException) { - throw RuntimeException(e) - } - } - return emptyArray() - } - else return sharedPreferences.getStringSet("exclude_ipsets", emptySet())?.toTypedArray() ?: emptyArray() + return readConfig().activeExcludeIpsets.toTypedArray() + } else return sharedPreferences.getStringSet("exclude_ipsets", emptySet())?.toTypedArray() ?: emptyArray() } fun getActiveNfqwsStrategy(): Array { - val configFile = File("${getZaprettPath()}/config") - if (configFile.exists()) { - val props = Properties() - return try { - FileInputStream(configFile).use { input -> - props.load(input) - } - val activeStrategies = props.getProperty("strategy", "") - Log.d("Active strategies", activeStrategies) - if (activeStrategies.isNotEmpty()) activeStrategies.split(",").toTypedArray() else emptyArray() - } catch (e: IOException) { - throw RuntimeException(e) - } - } - return emptyArray() + val strategy = readConfig().strategy + return if (strategy.isNotBlank()) arrayOf(strategy) else emptyArray() } fun getActiveByeDPIStrategy(sharedPreferences: SharedPreferences): Array { @@ -275,352 +219,173 @@ fun getActiveByeDPIStrategyContent(sharedPreferences: SharedPreferences): List { return if (sharedPreferences.getBoolean("use_module", false)) getActiveNfqwsStrategy() - else getActiveByeDPIStrategy(sharedPreferences) - + else getActiveByeDPIStrategy(sharedPreferences) } fun enableList(path: String, sharedPreferences: SharedPreferences) { if (sharedPreferences.getBoolean("use_module", false)) { - val configFile = getConfigFile() - try { - val props = Properties() - if (configFile.exists()) { - FileInputStream(configFile).use { input -> - props.load(input) - } + val config = readConfig() + val isWhitelist = getHostListMode(sharedPreferences) == "whitelist" + val currentLists = if (isWhitelist) config.activeLists else config.activeExcludeLists + if (path !in currentLists) { + val updatedLists = currentLists + path + val newConfig = if (isWhitelist) { + config.copy(activeLists = updatedLists) + } else { + config.copy(activeExcludeLists = updatedLists) } - val activeLists = props.getProperty( - if (getHostListMode(sharedPreferences) == "whitelist") "active_lists" - else "active_exclude_lists", - "") - .split(",") - .filter { it.isNotBlank() } - .toMutableList() - if (path !in activeLists) { - activeLists.add(path) - } - props.setProperty( - if (getHostListMode(sharedPreferences) == "whitelist") "active_lists" - else "active_exclude_lists", - activeLists.joinToString(",") - ) - FileOutputStream(configFile).use { output -> - props.store(output, "Don't place '/' in end of directory! Example: /sdcard") - } - } catch (e: IOException) { - throw RuntimeException(e) + writeConfig(newConfig) } - } - else { - val currentSet = sharedPreferences.getStringSet( - if (getHostListMode(sharedPreferences) == "whitelist") "lists" - else "exclude_lists", emptySet())?.toMutableSet() ?: mutableSetOf() + } else { + val key = if (getHostListMode(sharedPreferences) == "whitelist") "lists" else "exclude_lists" + val currentSet = sharedPreferences.getStringSet(key, emptySet())?.toMutableSet() ?: mutableSetOf() if (path !in currentSet) { currentSet.add(path) - sharedPreferences.edit { putStringSet( - if (getHostListMode(sharedPreferences) == "whitelist") "lists" - else "exclude_lists", currentSet) } + sharedPreferences.edit { putStringSet(key, currentSet) } } } } + fun enableIpset(path: String, sharedPreferences: SharedPreferences) { if (sharedPreferences.getBoolean("use_module", false)) { - val configFile = getConfigFile() - try { - val props = Properties() - if (configFile.exists()) { - FileInputStream(configFile).use { input -> - props.load(input) - } + val config = readConfig() + val isWhitelist = getHostListMode(sharedPreferences) == "whitelist" + val currentIpsets = if (isWhitelist) config.activeIpsets else config.activeExcludeIpsets + if (path !in currentIpsets) { + val updatedIpsets = currentIpsets + path + val newConfig = if (isWhitelist) { + config.copy(activeIpsets = updatedIpsets) + } else { + config.copy(activeExcludeIpsets = updatedIpsets) } - val activeLists = props.getProperty( - if (getHostListMode(sharedPreferences) == "whitelist") "active_ipsets" - else "active_exclude_ipsets", - "") - .split(",") - .filter { it.isNotBlank() } - .toMutableList() - if (path !in activeLists) { - activeLists.add(path) - } - props.setProperty( - if (getHostListMode(sharedPreferences) == "whitelist") "active_ipsets" - else "active_exclude_ipsets", - activeLists.joinToString(",") - ) - FileOutputStream(configFile).use { output -> - props.store(output, "Don't place '/' in end of directory! Example: /sdcard") - } - } catch (e: IOException) { - throw RuntimeException(e) + writeConfig(newConfig) } - } - else { - val currentSet = sharedPreferences.getStringSet( - if (getHostListMode(sharedPreferences) == "whitelist") "ipsets" - else "exclude_ipsets", emptySet())?.toMutableSet() ?: mutableSetOf() + } else { + val key = if (getHostListMode(sharedPreferences) == "whitelist") "ipsets" else "exclude_ipsets" + val currentSet = sharedPreferences.getStringSet(key, emptySet())?.toMutableSet() ?: mutableSetOf() if (path !in currentSet) { currentSet.add(path) - sharedPreferences.edit { putStringSet( - if (getHostListMode(sharedPreferences) == "whitelist") "ipsets" - else "exclude_ipsets", currentSet) } + sharedPreferences.edit { putStringSet(key, currentSet) } } } } + fun enableStrategy(path: String, sharedPreferences: SharedPreferences) { if (sharedPreferences.getBoolean("use_module", false)) { - val props = Properties() - val configFile = getConfigFile() - try { - if (configFile.exists()) { - FileInputStream(configFile).use { input -> - props.load(input) - } - } - val activeStrategies = props.getProperty("strategy", "") - .split(",") - .filter { it.isNotBlank() } - .toMutableList() - if (path !in activeStrategies) { - activeStrategies.add(path) - } - props.setProperty("strategy", activeStrategies.joinToString(",")) - FileOutputStream(configFile).use { output -> - props.store(output, "Don't place '/' in end of directory! Example: /sdcard") - } - } catch (e: IOException) { - throw RuntimeException(e) + val config = readConfig() + if (config.strategy != path) { + writeConfig(config.copy(strategy = path)) } - } - else { + } else { sharedPreferences.edit { putString("active_strategy", path) } } } fun disableList(path: String, sharedPreferences: SharedPreferences) { if (sharedPreferences.getBoolean("use_module", false)) { - val props = Properties() - val configFile = getConfigFile() - try { - if (configFile.exists()) { - FileInputStream(configFile).use { input -> - props.load(input) - } + val config = readConfig() + val isWhitelist = getHostListMode(sharedPreferences) == "whitelist" + val currentLists = if (isWhitelist) config.activeLists else config.activeExcludeLists + if (path in currentLists) { + val updatedLists = currentLists.filter { it != path } + val newConfig = if (isWhitelist) { + config.copy(activeLists = updatedLists) + } else { + config.copy(activeExcludeLists = updatedLists) } - val activeLists = props.getProperty( - if (getHostListMode(sharedPreferences) == "whitelist") "active_lists" - else "active_exclude_lists", - "") - .split(",") - .filter { it.isNotBlank() } - .toMutableList() - if (path in activeLists) { - activeLists.remove(path) - } - props.setProperty( - if (getHostListMode(sharedPreferences) == "whitelist") "active_lists" - else "active_exclude_lists", - activeLists.joinToString(",") - ) - FileOutputStream(configFile).use { output -> - props.store(output, "Don't place '/' in end of directory! Example: /sdcard") - } - } catch (e: IOException) { - throw RuntimeException(e) + writeConfig(newConfig) } - } - else { - val currentSet = sharedPreferences.getStringSet( - if (getHostListMode(sharedPreferences) == "whitelist") "lists" - else "exclude_lists", emptySet())?.toMutableSet() ?: mutableSetOf() + } else { + val key = if (getHostListMode(sharedPreferences) == "whitelist") "lists" else "exclude_lists" + val currentSet = sharedPreferences.getStringSet(key, emptySet())?.toMutableSet() ?: mutableSetOf() if (path in currentSet) { currentSet.remove(path) - sharedPreferences.edit { putStringSet( - if (getHostListMode(sharedPreferences) == "whitelist") "lists" - else "exclude_lists", currentSet) } + sharedPreferences.edit { putStringSet(key, currentSet) } } if (currentSet.isEmpty()) { - sharedPreferences.edit { remove( - if (getHostListMode(sharedPreferences) == "whitelist") "lists" - else "exclude_lists" - ) } + sharedPreferences.edit { remove(key) } } } } fun disableIpset(path: String, sharedPreferences: SharedPreferences) { if (sharedPreferences.getBoolean("use_module", false)) { - val props = Properties() - val configFile = getConfigFile() - try { - if (configFile.exists()) { - FileInputStream(configFile).use { input -> - props.load(input) - } + val config = readConfig() + val isWhitelist = getHostListMode(sharedPreferences) == "whitelist" + val currentIpsets = if (isWhitelist) config.activeIpsets else config.activeExcludeIpsets + if (path in currentIpsets) { + val updatedIpsets = currentIpsets.filter { it != path } + val newConfig = if (isWhitelist) { + config.copy(activeIpsets = updatedIpsets) + } else { + config.copy(activeExcludeIpsets = updatedIpsets) } - val activeLists = props.getProperty( - if (getHostListMode(sharedPreferences) == "whitelist") "active_ipsets" - else "active_exclude_ipsets", - "") - .split(",") - .filter { it.isNotBlank() } - .toMutableList() - if (path in activeLists) { - activeLists.remove(path) - } - props.setProperty( - if (getHostListMode(sharedPreferences) == "whitelist") "active_ipsets" - else "active_exclude_ipsets", - activeLists.joinToString(",") - ) - FileOutputStream(configFile).use { output -> - props.store(output, "Don't place '/' in end of directory! Example: /sdcard") - } - } catch (e: IOException) { - throw RuntimeException(e) + writeConfig(newConfig) } - } - else { - val currentSet = sharedPreferences.getStringSet( - if (getHostListMode(sharedPreferences) == "whitelist") "ipsets" - else "exclude_ipsets", emptySet())?.toMutableSet() ?: mutableSetOf() + } else { + val key = if (getHostListMode(sharedPreferences) == "whitelist") "ipsets" else "exclude_ipsets" + val currentSet = sharedPreferences.getStringSet(key, emptySet())?.toMutableSet() ?: mutableSetOf() if (path in currentSet) { currentSet.remove(path) - sharedPreferences.edit { putStringSet( - if (getHostListMode(sharedPreferences) == "whitelist") "ipsets" - else "exclude_ipsets", currentSet) } + sharedPreferences.edit { putStringSet(key, currentSet) } } if (currentSet.isEmpty()) { - sharedPreferences.edit { remove( - if (getHostListMode(sharedPreferences) == "whitelist") "ipsets" - else "exclude_ipsets" - ) } + sharedPreferences.edit { remove(key) } } } } fun disableStrategy(path: String, sharedPreferences: SharedPreferences) { if (sharedPreferences.getBoolean("use_module", false)) { - val props = Properties() - val configFile = getConfigFile() - try { - if (configFile.exists()) { - FileInputStream(configFile).use { input -> - props.load(input) - } - } - val activeStrategies = props.getProperty("strategy", "") - .split(",") - .filter { it.isNotBlank() } - .toMutableList() - if (path in activeStrategies) { - activeStrategies.remove(path) - } - props.setProperty("strategy", activeStrategies.joinToString(",")) - FileOutputStream(configFile).use { output -> - props.store(output, "Don't place '/' in end of directory! Example: /sdcard") - } - } catch (e: IOException) { - throw RuntimeException(e) + val config = readConfig() + if (config.strategy == path) { + writeConfig(config.copy(strategy = "")) } - } - else { + } else { sharedPreferences.edit { remove("active_strategy") } } } -fun addPackageToList(listType: AppListType, packageName: String, prefs : SharedPreferences, context : Context) { - if (prefs.getBoolean("use_module", false)){ - val configFile = getConfigFile() - try { - val props = Properties() - if (configFile.exists()) { - FileInputStream(configFile).use { input -> - props.load(input) - } +fun addPackageToList(listType: AppListType, packageName: String, prefs: SharedPreferences, context: Context) { + if (prefs.getBoolean("use_module", false)) { + val config = readConfig() + if (listType == AppListType.Whitelist) { + if (packageName !in config.whitelist) { + writeConfig(config.copy(whitelist = config.whitelist + packageName)) } - if (listType == AppListType.Whitelist) { - val whitelist = props.getProperty("whitelist", "") - .split(",") - .filter { it.isNotBlank() } - .toMutableList() - if (packageName !in whitelist) { - whitelist.add(packageName) - } - props.setProperty("whitelist", whitelist.joinToString(",")) + } else if (listType == AppListType.Blacklist) { + if (packageName !in config.blacklist) { + writeConfig(config.copy(blacklist = config.blacklist + packageName)) } - if (listType == AppListType.Blacklist) { - val blacklist = props.getProperty("blacklist", "") - .split(",") - .filter { it.isNotBlank() } - .toMutableList() - if (packageName !in blacklist) { - blacklist.add(packageName) - } - props.setProperty("blacklist", blacklist.joinToString(",")) - } - FileOutputStream(configFile).use { output -> - props.store(output, "Don't place '/' in end of directory! Example: /sdcard") - } - } catch (e: IOException) { - throw RuntimeException(e) } - } - else { + } else { val prefs = context.getSharedPreferences("settings", MODE_PRIVATE) - if (listType == AppListType.Whitelist){ + if (listType == AppListType.Whitelist) { val set = prefs.getStringSet("whitelist", emptySet())?.toMutableSet() ?: mutableSetOf() set.add(packageName) prefs.edit().putStringSet("whitelist", set).apply() } - if (listType == AppListType.Blacklist){ + if (listType == AppListType.Blacklist) { val set = prefs.getStringSet("blacklist", emptySet())?.toMutableSet() ?: mutableSetOf() set.add(packageName) prefs.edit().putStringSet("blacklist", set).apply() } - } } fun removePackageFromList(listType: AppListType, packageName: String, prefs: SharedPreferences, context: Context) { - if (prefs.getBoolean("use_module", false)){ - val props = Properties() - val configFile = getConfigFile() - try { - if (configFile.exists()) { - FileInputStream(configFile).use { input -> - props.load(input) - } + if (prefs.getBoolean("use_module", false)) { + val config = readConfig() + if (listType == AppListType.Whitelist) { + if (packageName in config.whitelist) { + writeConfig(config.copy(whitelist = config.whitelist.filter { it != packageName })) } - if (listType == AppListType.Whitelist){ - val whitelist = props.getProperty("whitelist", "") - .split(",") - .filter { it.isNotBlank() } - .toMutableList() - if (packageName in whitelist) { - whitelist.remove(packageName) - } - props.setProperty("whitelist", whitelist.joinToString(",")) + } else if (listType == AppListType.Blacklist) { + if (packageName in config.blacklist) { + writeConfig(config.copy(blacklist = config.blacklist.filter { it != packageName })) } - if (listType == AppListType.Blacklist) { - val blacklist = props.getProperty("blacklist", "") - .split(",") - .filter { it.isNotBlank() } - .toMutableList() - if (packageName in blacklist) { - blacklist.remove(packageName) - } - props.setProperty("blacklist", blacklist.joinToString(",")) - } - - FileOutputStream(configFile).use { output -> - props.store(output, "Don't place '/' in end of directory! Example: /sdcard") - } - } catch (e: IOException) { - throw RuntimeException(e) } - } - else { + } else { val prefs = context.getSharedPreferences("settings", MODE_PRIVATE) if (listType == AppListType.Whitelist) { val set = prefs.getStringSet("whitelist", emptySet())?.toMutableSet() ?: mutableSetOf() @@ -635,175 +400,79 @@ fun removePackageFromList(listType: AppListType, packageName: String, prefs: Sha } } -fun isInList(listType: AppListType, packageName: String, prefs: SharedPreferences, context: Context) : Boolean { +fun isInList(listType: AppListType, packageName: String, prefs: SharedPreferences, context: Context): Boolean { if (prefs.getBoolean("use_module", false)) { - val configFile = getConfigFile() - if (configFile.exists()) { - val props = Properties() - try { - FileInputStream(configFile).use { input -> - props.load(input) - } - if (listType == AppListType.Whitelist) { - val whitelist = props.getProperty("whitelist", "") - return if (whitelist.isNotEmpty()) whitelist.split(",") - .toTypedArray().contains(packageName) else false - } - if (listType == AppListType.Blacklist) { - val blacklist = props.getProperty("blacklist", "") - return if (blacklist.isNotEmpty()) blacklist.split(",") - .toTypedArray().contains(packageName) else false - } - } catch (e: IOException) { - throw RuntimeException(e) - } + val config = readConfig() + return if (listType == AppListType.Whitelist) { + packageName in config.whitelist + } else { + packageName in config.blacklist } - } - else { + } else { val prefs = context.getSharedPreferences("settings", MODE_PRIVATE) - if(listType == AppListType.Whitelist){ + if (listType == AppListType.Whitelist) { val whitelist = prefs.getStringSet("whitelist", emptySet()) ?: emptySet() return packageName in whitelist - } - else { + } else { val blacklist = prefs.getStringSet("blacklist", emptySet()) ?: emptySet() return packageName in blacklist } } - return false } -fun getAppList(listType: AppListType, sharedPreferences : SharedPreferences, context : Context) : Set { +fun getAppList(listType: AppListType, sharedPreferences: SharedPreferences, context: Context): Set { if (sharedPreferences.getBoolean("use_module", false)) { - val configFile = File("${getZaprettPath()}/config") - if (configFile.exists()) { - val props = Properties() - try { - FileInputStream(configFile).use { input -> - props.load(input) - } - if (listType == AppListType.Whitelist) { - val whitelist = props.getProperty("whitelist", "") - return if (whitelist.isNotEmpty()) whitelist.split(",") - .toSet() else emptySet() - } - if (listType == AppListType.Blacklist) { - val blacklist = props.getProperty("blacklist", "") - return if (blacklist.isNotEmpty()) blacklist.split(",") - .toSet() else emptySet() - } - } catch (e: IOException) { - throw RuntimeException(e) - } + val config = readConfig() + return if (listType == AppListType.Whitelist) { + config.whitelist.toSet() + } else { + config.blacklist.toSet() } - return emptySet() - } - else { + } else { return if (listType == AppListType.Whitelist) context.getSharedPreferences("settings", MODE_PRIVATE) .getStringSet("whitelist", emptySet()) ?: emptySet() - else context.getSharedPreferences("settings", MODE_PRIVATE) + else context.getSharedPreferences("settings", MODE_PRIVATE) .getStringSet("blacklist", emptySet()) ?: emptySet() } } -fun getAppsListMode(prefs : SharedPreferences) : String { - if(prefs.getBoolean("use_module", false)) { - val configFile = getConfigFile() - if (configFile.exists()) { - val props = Properties() - try { - FileInputStream(configFile).use { input -> - props.load(input) - } - val applist = props.getProperty("app_list", "")!! - Log.d("App list", "Equals to $applist") - return if (applist == "whitelist" || applist == "blacklist" || applist == "none") applist - else "none" - } catch (e: IOException) { - throw RuntimeException(e) - } - } - } - else { +fun getAppsListMode(prefs: SharedPreferences): String { + if (prefs.getBoolean("use_module", false)) { + val applist = readConfig().appList + Log.d("App list", "Equals to $applist") + return if (applist == "whitelist" || applist == "blacklist" || applist == "none") applist + else "none" + } else { return prefs.getString("app_list", "none")!! } - return "none" } fun setAppsListMode(prefs: SharedPreferences, mode: String) { if (prefs.getBoolean("use_module", false)) { - val configFile = getConfigFile() - val props = Properties() - if (configFile.exists()) { - try { - FileInputStream(configFile).use { input -> - props.load(input) - } - } catch (e: IOException) { - throw RuntimeException(e) - } - } - props.setProperty("app_list", mode) - try { - FileOutputStream(configFile).use { output -> - props.store(output, "Don't place '/' in end of directory! Example: /sdcard") - } - } catch (e: IOException) { - throw RuntimeException(e) - } - } - else { + val config = readConfig() + writeConfig(config.copy(appList = mode)) + } else { prefs.edit { putString("app_list", mode) } } Log.d("App List", "Changed to $mode") } + fun setHostListMode(prefs: SharedPreferences, mode: String) { if (prefs.getBoolean("use_module", false)) { - val configFile = getConfigFile() - val props = Properties() - if (configFile.exists()) { - try { - FileInputStream(configFile).use { input -> - props.load(input) - } - } catch (e: IOException) { - throw RuntimeException(e) - } - } - props.setProperty("list_type", mode) - try { - FileOutputStream(configFile).use { output -> - props.store(output, "Don't place '/' in end of directory! Example: /sdcard") - } - } catch (e: IOException) { - throw RuntimeException(e) - } - } - else { + val config = readConfig() + writeConfig(config.copy(listType = mode)) + } else { prefs.edit { putString("list_type", mode) } } Log.d("App List", "Changed to $mode") } -fun getHostListMode(prefs : SharedPreferences) : String { - if(prefs.getBoolean("use_module", false)) { - val configFile = getConfigFile() - if (configFile.exists()) { - val props = Properties() - try { - FileInputStream(configFile).use { input -> - props.load(input) - } - val hostlist = props.getProperty("list_type", "whitelist")!! - return if (hostlist == "whitelist" || hostlist == "blacklist") hostlist - else "whitelist" - } catch (e: IOException) { - throw RuntimeException(e) - } - } - } - else { +fun getHostListMode(prefs: SharedPreferences): String { + if (prefs.getBoolean("use_module", false)) { + val hostlist = readConfig().listType + return if (hostlist == "whitelist" || hostlist == "blacklist") hostlist + else "whitelist" + } else { return prefs.getString("list_type", "whitelist")!! } - return "whitelist" } \ No newline at end of file