Config to json

This commit is contained in:
CherretGit
2025-10-30 21:42:43 +07:00
parent c5b60b1ee9
commit c4dcf934aa
2 changed files with 208 additions and 513 deletions

View File

@@ -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<String> = emptyList(),
@SerialName("active_ipsets")
val activeIpsets: List<String> = emptyList(),
@SerialName("active_exclude_lists")
val activeExcludeLists: List<String> = emptyList(),
@SerialName("active_exclude_ipsets")
val activeExcludeIpsets: List<String> = 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<String> = emptyList(),
@SerialName("blacklist")
val blacklist: List<String> = emptyList()
)

View File

@@ -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<ZaprettConfig>(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<String> {
?: emptyArray()
}
fun getAllStrategies(sharedPreferences : SharedPreferences) : Array<String> {
fun getAllStrategies(sharedPreferences: SharedPreferences): Array<String> {
return if (sharedPreferences.getBoolean("use_module", false)) getAllNfqwsStrategies()
else getAllByeDPIStrategies()
}
fun getActiveLists(sharedPreferences: SharedPreferences): Array<String> {
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<String> {
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<String> {
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<String> {
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<String> {
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<String> {
@@ -276,351 +220,172 @@ fun getActiveByeDPIStrategyContent(sharedPreferences: SharedPreferences): List<S
fun getActiveStrategy(sharedPreferences: SharedPreferences): Array<String> {
return if (sharedPreferences.getBoolean("use_module", false)) getActiveNfqwsStrategy()
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)
}
writeConfig(newConfig)
}
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)
}
}
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)
}
writeConfig(newConfig)
}
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)
}
}
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 config = readConfig()
if (config.strategy != path) {
writeConfig(config.copy(strategy = path))
}
}
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)
}
}
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)
}
writeConfig(newConfig)
}
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)
}
}
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)
}
writeConfig(newConfig)
}
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)
}
}
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 config = readConfig()
if (config.strategy == path) {
writeConfig(config.copy(strategy = ""))
}
}
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)
}
}
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) {
val whitelist = props.getProperty("whitelist", "")
.split(",")
.filter { it.isNotBlank() }
.toMutableList()
if (packageName !in whitelist) {
whitelist.add(packageName)
if (packageName !in config.whitelist) {
writeConfig(config.copy(whitelist = config.whitelist + packageName))
}
props.setProperty("whitelist", whitelist.joinToString(","))
}
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 if (listType == AppListType.Blacklist) {
if (packageName !in config.blacklist) {
writeConfig(config.copy(blacklist = config.blacklist + packageName))
}
}
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 }))
}
} else if (listType == AppListType.Blacklist) {
if (packageName in config.blacklist) {
writeConfig(config.copy(blacklist = config.blacklist.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(","))
}
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,70 +400,35 @@ 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)
val config = readConfig()
return if (listType == AppListType.Whitelist) {
packageName in config.whitelist
} else {
packageName in config.blacklist
}
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)
}
}
}
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<String> {
fun getAppList(listType: AppListType, sharedPreferences: SharedPreferences, context: Context): Set<String> {
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)
val config = readConfig()
return if (listType == AppListType.Whitelist) {
config.whitelist.toSet()
} else {
config.blacklist.toSet()
}
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)
}
}
return emptySet()
}
else {
} else {
return if (listType == AppListType.Whitelist) context.getSharedPreferences("settings", MODE_PRIVATE)
.getStringSet("whitelist", emptySet()) ?: emptySet()
else context.getSharedPreferences("settings", MODE_PRIVATE)
@@ -706,104 +436,43 @@ fun getAppList(listType: AppListType, sharedPreferences : SharedPreferences, con
}
}
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", "")!!
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"
} catch (e: IOException) {
throw RuntimeException(e)
}
}
}
else {
} 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")!!
fun getHostListMode(prefs: SharedPreferences): String {
if (prefs.getBoolean("use_module", false)) {
val hostlist = readConfig().listType
return if (hostlist == "whitelist" || hostlist == "blacklist") hostlist
else "whitelist"
} catch (e: IOException) {
throw RuntimeException(e)
}
}
}
else {
} else {
return prefs.getString("list_type", "whitelist")!!
}
return "whitelist"
}