some refactoring + creating config on every change

This commit is contained in:
white
2025-08-21 12:10:14 +03:00
parent baec0f3838
commit ef04471a05

View File

@@ -66,34 +66,29 @@ fun getConfigFile(): File {
fun setStartOnBoot(startOnBoot: Boolean) {
val configFile = getConfigFile()
if (configFile.exists()) {
val props = Properties()
try {
FileInputStream(configFile).use { input ->
props.load(input)
}
props.setProperty("start-on-boot", startOnBoot.toString())
FileOutputStream(configFile).use { output ->
props.store(output, "Don't place '/' in end of directory! Example: /sdcard")
}
} catch (e: IOException) {
throw RuntimeException(e)
val props = Properties()
try {
FileInputStream(configFile).use { input ->
props.load(input)
}
props.setProperty("start-on-boot", startOnBoot.toString())
FileOutputStream(configFile).use { output ->
props.store(output, "Don't place '/' in end of directory! Example: /sdcard")
}
} catch (e: IOException) {
throw RuntimeException(e)
}
}
fun getStartOnBoot(): Boolean {
val configFile = getConfigFile()
val props = Properties()
return try {
if (configFile.exists()) {
FileInputStream(configFile).use { input ->
props.load(input)
}
props.getProperty("start-on-boot", "false").toBoolean()
} else {
false
FileInputStream(configFile).use { input ->
props.load(input)
}
props.getProperty("start-on-boot", "false").toBoolean()
} catch (_: IOException) {
false
}
@@ -102,17 +97,18 @@ fun getStartOnBoot(): Boolean {
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 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"
}
fun getAllLists(): Array<String> {
@@ -151,65 +147,59 @@ fun getAllByeDPIStrategies(): Array<String> {
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 sharedPreferences.getStringSet("lists", 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 sharedPreferences.getStringSet("exclude_lists", emptySet())?.toTypedArray() ?: emptyArray()
}
}
fun getActiveNfqwsStrategies(): 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()
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)
}
} else {
return sharedPreferences.getStringSet("lists", emptySet())?.toTypedArray() ?: emptyArray()
}
return emptyArray()
}
fun getActiveExcludeLists(sharedPreferences: SharedPreferences): Array<String> {
if (sharedPreferences.getBoolean("use_module", false)) {
val configFile = getConfigFile()
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)
}
} else {
return sharedPreferences.getStringSet("exclude_lists", emptySet())?.toTypedArray()
?: emptyArray()
}
}
fun getActiveNfqwsStrategies(): Array<String> {
val configFile = File("${getZaprettPath()}/config")
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)
}
}
fun getActiveByeDPIStrategies(sharedPreferences: SharedPreferences): Array<String> {
@@ -233,18 +223,18 @@ fun enableList(path: String, sharedPreferences: SharedPreferences) {
val configFile = getConfigFile()
try {
val props = Properties()
if (configFile.exists()) {
FileInputStream(configFile).use { input ->
props.load(input)
}
FileInputStream(configFile).use { input ->
props.load(input)
}
val activeLists = props.getProperty(
if (getHostListMode(sharedPreferences) == "whitelist") "active-lists"
else "active-exclude-lists",
"")
.split(",")
.filter { it.isNotBlank() }
.toMutableList()
if (getHostListMode(sharedPreferences) == "whitelist") "active-lists"
else "active-exclude-lists",
""
)
.split(",")
.filter { it.isNotBlank() }
.toMutableList()
if (path !in activeLists) {
activeLists.add(path)
}
@@ -259,16 +249,19 @@ fun enableList(path: String, sharedPreferences: SharedPreferences) {
} catch (e: IOException) {
throw RuntimeException(e)
}
}
else {
} else {
val currentSet = sharedPreferences.getStringSet(
if (getHostListMode(sharedPreferences) == "whitelist") "lists"
else "exclude_lists", emptySet())?.toMutableSet() ?: mutableSetOf()
else "exclude_lists", 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(
if (getHostListMode(sharedPreferences) == "whitelist") "lists"
else "exclude_lists", currentSet
)
}
}
}
}
@@ -278,10 +271,8 @@ fun enableStrategy(path: String, sharedPreferences: SharedPreferences) {
val props = Properties()
val configFile = getConfigFile()
try {
if (configFile.exists()) {
FileInputStream(configFile).use { input ->
props.load(input)
}
FileInputStream(configFile).use { input ->
props.load(input)
}
val activeStrategies = props.getProperty("strategy", "")
.split(",")
@@ -297,8 +288,7 @@ fun enableStrategy(path: String, sharedPreferences: SharedPreferences) {
} catch (e: IOException) {
throw RuntimeException(e)
}
}
else {
} else {
sharedPreferences.edit { putString("active_strategy", path) }
}
}
@@ -308,24 +298,25 @@ fun disableList(path: String, sharedPreferences: SharedPreferences) {
val props = Properties()
val configFile = getConfigFile()
try {
if (configFile.exists()) {
FileInputStream(configFile).use { input ->
props.load(input)
}
FileInputStream(configFile).use { input ->
props.load(input)
}
val activeLists = props.getProperty(
if (getHostListMode(sharedPreferences) == "whitelist") "active-lists"
else "active-exclude-lists",
"")
.split(",")
.filter { it.isNotBlank() }
.toMutableList()
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",
else "active-exclude-lists",
activeLists.joinToString(",")
)
FileOutputStream(configFile).use { output ->
@@ -334,22 +325,27 @@ fun disableList(path: String, sharedPreferences: SharedPreferences) {
} catch (e: IOException) {
throw RuntimeException(e)
}
}
else {
} else {
val currentSet = sharedPreferences.getStringSet(
if (getHostListMode(sharedPreferences) == "whitelist") "lists"
else "exclude_lists", emptySet())?.toMutableSet() ?: mutableSetOf()
else "exclude_lists", 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(
if (getHostListMode(sharedPreferences) == "whitelist") "lists"
else "exclude_lists", currentSet
)
}
}
if (currentSet.isEmpty()) {
sharedPreferences.edit { remove(
if (getHostListMode(sharedPreferences) == "whitelist") "lists"
else "exclude_lists"
) }
sharedPreferences.edit {
remove(
if (getHostListMode(sharedPreferences) == "whitelist") "lists"
else "exclude_lists"
)
}
}
}
}
@@ -359,11 +355,10 @@ fun disableStrategy(path: String, sharedPreferences: SharedPreferences) {
val props = Properties()
val configFile = getConfigFile()
try {
if (configFile.exists()) {
FileInputStream(configFile).use { input ->
props.load(input)
}
FileInputStream(configFile).use { input ->
props.load(input)
}
val activeStrategies = props.getProperty("strategy", "")
.split(",")
.filter { it.isNotBlank() }
@@ -378,22 +373,25 @@ fun disableStrategy(path: String, sharedPreferences: SharedPreferences) {
} 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)){
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)
}
FileInputStream(configFile).use { input ->
props.load(input)
}
if (listType == AppListType.Whitelist) {
val whitelist = props.getProperty("whitelist", "")
.split(",")
@@ -420,34 +418,36 @@ fun addPackageToList(listType: AppListType, packageName: String, prefs : SharedP
} 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()
prefs.edit { putStringSet("whitelist", set) }
}
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()
prefs.edit { putStringSet("blacklist", set) }
}
}
}
fun removePackageFromList(listType: AppListType, packageName: String, prefs: SharedPreferences, context: Context) {
if (prefs.getBoolean("use_module", false)){
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)
}
FileInputStream(configFile).use { input ->
props.load(input)
}
if (listType == AppListType.Whitelist){
if (listType == AppListType.Whitelist) {
val whitelist = props.getProperty("whitelist", "")
.split(",")
.filter { it.isNotBlank() }
@@ -474,53 +474,55 @@ fun removePackageFromList(listType: AppListType, packageName: String, prefs: Sha
} 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()
set.remove(packageName)
prefs.edit().putStringSet("whitelist", set).apply()
prefs.edit { putStringSet("whitelist", set) }
}
if (listType == AppListType.Blacklist) {
val set = prefs.getStringSet("blacklist", emptySet())?.toMutableSet() ?: mutableSetOf()
set.remove(packageName)
prefs.edit().putStringSet("blacklist", set).apply()
prefs.edit { putStringSet("blacklist", set) }
}
}
}
fun isInList(listType: AppListType, packageName: String, prefs: SharedPreferences, context: Context) : Boolean {
// Never used?
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 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)
}
}
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
}
@@ -528,130 +530,121 @@ fun isInList(listType: AppListType, packageName: String, prefs: SharedPreference
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)
}
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 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)
}
return emptySet()
}
else {
return if (listType == AppListType.Whitelist) context.getSharedPreferences("settings", MODE_PRIVATE)
} 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()
}
return emptySet()
}
fun getAppsListMode(prefs : SharedPreferences) : String {
if(prefs.getBoolean("use_module", false)) {
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("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)
val props = Properties()
try {
FileInputStream(configFile).use { input ->
props.load(input)
}
val applist = props.getProperty("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("applist", "")!!
}
return "none"
}
fun setAppsListMode(prefs: SharedPreferences, mode: String) {
if (prefs.getBoolean("use_module", false)) {
val configFile = getConfigFile()
if (configFile.exists()) {
val props = Properties()
try {
FileInputStream(configFile).use { input ->
props.load(input)
}
props.setProperty("app-list", mode)
FileOutputStream(configFile).use { output ->
props.store(output, "Don't place '/' in end of directory! Example: /sdcard")
}
} catch (e: IOException) {
throw RuntimeException(e)
val props = Properties()
try {
FileInputStream(configFile).use { input ->
props.load(input)
}
props.setProperty("app-list", mode)
FileOutputStream(configFile).use { output ->
props.store(output, "Don't place '/' in end of directory! Example: /sdcard")
}
} catch (e: IOException) {
throw RuntimeException(e)
}
}
else {
} 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()
if (configFile.exists()) {
val props = Properties()
try {
FileInputStream(configFile).use { input ->
props.load(input)
}
props.setProperty("list-type", mode)
FileOutputStream(configFile).use { output ->
props.store(output, "Don't place '/' in end of directory! Example: /sdcard")
}
} catch (e: IOException) {
throw RuntimeException(e)
val props = Properties()
try {
FileInputStream(configFile).use { input ->
props.load(input)
}
props.setProperty("list-type", mode)
FileOutputStream(configFile).use { output ->
props.store(output, "Don't place '/' in end of directory! Example: /sdcard")
}
} catch (e: IOException) {
throw RuntimeException(e)
}
}
else {
} 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)) {
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)
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 {
} else {
return prefs.getString("list_type", "whitelist")!!
}
return "none"
}