Skip to content

Commit

Permalink
Don't require reload config if config version is the same
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-TSNG committed Dec 30, 2022
1 parent 99c3e1f commit abb67b9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/icu/nullptr/hidemyapplist/MyApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package icu.nullptr.hidemyapplist
import android.annotation.SuppressLint
import android.app.Application
import com.tsng.hidemyapplist.R
import icu.nullptr.hidemyapplist.service.ConfigManager
import icu.nullptr.hidemyapplist.service.PrefManager
import icu.nullptr.hidemyapplist.ui.receiver.AppChangeReceiver
import icu.nullptr.hidemyapplist.ui.util.makeToast
Expand Down Expand Up @@ -37,6 +38,7 @@ class MyApp : Application() {
}
hmaApp = this
AppChangeReceiver.register(this)
ConfigManager.init()

DayNightDelegate.setApplicationContext(this)
DayNightDelegate.setDefaultNightMode(PrefManager.darkTheme)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ object ConfigManager {
private lateinit var config: JsonConfig
val configFile = File("${hmaApp.filesDir.absolutePath}/config.json")

init {
if (!configFile.exists())
fun init() {
if (!configFile.exists()) {
configFile.writeText(JsonConfig().toString())
}
runCatching {
config = JsonConfig.parse(configFile.readText())
val configVersion = config.configVersion
if (configVersion < 65) throw RuntimeException("Config version too old")
config.configVersion = BuildConfig.SERVICE_VERSION
config.configVersion = BuildConfig.CONFIG_VERSION
saveConfig()
}.onFailure {
makeToast(R.string.config_damaged)
Expand Down Expand Up @@ -63,7 +64,7 @@ object ConfigManager {

fun importConfig(json: String) {
config = JsonConfig.parse(json)
config.configVersion = BuildConfig.SERVICE_VERSION
config.configVersion = BuildConfig.CONFIG_VERSION
saveConfig()
}

Expand Down
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ val targetSdkVer by extra(33)
val buildToolsVer by extra("33.0.1")

val appVerName by extra("3.0.6")
val serviceVerCode by extra(93)
val configVerCode by extra(90)
val serviceVerCode by extra(94)
val minBackupVerCode by extra(65)

val androidSourceCompatibility = JavaVersion.VERSION_11
Expand Down
2 changes: 2 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ plugins {
kotlin("plugin.serialization")
}

val configVerCode: Int by rootProject.extra
val serviceVerCode: Int by rootProject.extra
val minBackupVerCode: Int by rootProject.extra

android {
namespace = "icu.nullptr.hidemyapplist.common"

defaultConfig {
buildConfigField("int", "CONFIG_VERSION", configVerCode.toString())
buildConfigField("int", "SERVICE_VERSION", serviceVerCode.toString())
buildConfigField("int", "MIN_BACKUP_VERSION", minBackupVerCode.toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private val encoder = Json {

@Serializable
data class JsonConfig(
var configVersion: Int = BuildConfig.SERVICE_VERSION,
var configVersion: Int = BuildConfig.CONFIG_VERSION,
var detailLog: Boolean = false,
var maxLogSize: Int = 512,
var forceMountData: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HMAService(val pms: IPackageManager) : IHMAService.Stub() {
private val systemApps = mutableSetOf<String>()
private val frameworkHooks = mutableSetOf<IFrameworkHook>()

var config = JsonConfig()
var config = JsonConfig().apply { detailLog = true }
private set

var filterCount = 0
Expand Down Expand Up @@ -92,7 +92,7 @@ class HMAService(val pms: IPackageManager) : IHMAService.Stub() {
logE(TAG, "Failed to parse config.json", it)
return
}
if (loading.configVersion != BuildConfig.SERVICE_VERSION) {
if (loading.configVersion != BuildConfig.CONFIG_VERSION) {
logW(TAG, "Config version mismatch, need to reload")
return
}
Expand Down Expand Up @@ -171,7 +171,7 @@ class HMAService(val pms: IPackageManager) : IHMAService.Stub() {
synchronized(configLock) {
configFile.writeText(json)
val newConfig = JsonConfig.parse(json)
if (newConfig.configVersion != BuildConfig.SERVICE_VERSION) {
if (newConfig.configVersion != BuildConfig.CONFIG_VERSION) {
logW(TAG, "Sync config: version mismatch, need reboot")
return
}
Expand All @@ -185,7 +185,9 @@ class HMAService(val pms: IPackageManager) : IHMAService.Stub() {

override fun getFilterCount() = filterCount

override fun getLogs() = synchronized(loggerLock) { logFile.readText() }
override fun getLogs() = synchronized(loggerLock) {
logFile.readText()
}

override fun clearLogs() {
synchronized(loggerLock) {
Expand Down

0 comments on commit abb67b9

Please sign in to comment.