Simplify prefs migration

This commit is contained in:
vvb2060 2023-04-24 16:58:19 +08:00 committed by John Wu
parent 1610092ec4
commit bd2651057d
2 changed files with 10 additions and 63 deletions

View File

@ -1,22 +1,19 @@
package com.topjohnwu.magisk.core package com.topjohnwu.magisk.core
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.SharedPreferences
import android.util.Xml
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.edit import androidx.core.content.edit
import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.core.di.AppContext import com.topjohnwu.magisk.core.di.AppContext
import com.topjohnwu.magisk.core.di.ServiceLocator import com.topjohnwu.magisk.core.di.ServiceLocator
import com.topjohnwu.magisk.core.ktx.writeTo
import com.topjohnwu.magisk.core.repository.BoolDBPropertyNoWrite import com.topjohnwu.magisk.core.repository.BoolDBPropertyNoWrite
import com.topjohnwu.magisk.core.repository.DBConfig import com.topjohnwu.magisk.core.repository.DBConfig
import com.topjohnwu.magisk.core.repository.PreferenceConfig import com.topjohnwu.magisk.core.repository.PreferenceConfig
import com.topjohnwu.magisk.core.utils.refreshLocale import com.topjohnwu.magisk.core.utils.refreshLocale
import com.topjohnwu.magisk.ui.theme.Theme import com.topjohnwu.magisk.ui.theme.Theme
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import org.xmlpull.v1.XmlPullParser
import java.io.File import java.io.File
import java.io.InputStream
object Config : PreferenceConfig, DBConfig { object Config : PreferenceConfig, DBConfig {
@ -25,13 +22,12 @@ object Config : PreferenceConfig, DBConfig {
override val context get() = ServiceLocator.deContext override val context get() = ServiceLocator.deContext
override val coroutineScope get() = GlobalScope override val coroutineScope get() = GlobalScope
@get:SuppressLint("ApplySharedPref") private val prefsFile = File("${context.filesDir.parent}/shared_prefs", "${fileName}.xml")
val prefsFile: File get() {
// Flush prefs to disk @SuppressLint("ApplySharedPref")
prefs.edit().apply { fun getPrefsFile(): File {
remove(Key.ASKED_HOME) prefs.edit().remove(Key.ASKED_HOME).commit()
}.commit() return prefsFile
return File("${context.filesDir.parent}/shared_prefs", "${fileName}.xml")
} }
object Key { object Key {
@ -172,9 +168,8 @@ object Config : PreferenceConfig, DBConfig {
fun load(pkg: String?) { fun load(pkg: String?) {
// Only try to load prefs when fresh install and a previous package name is set // Only try to load prefs when fresh install and a previous package name is set
if (pkg != null && prefs.all.isEmpty()) runCatching { if (pkg != null && prefs.all.isEmpty()) runCatching {
context.contentResolver.openInputStream(Provider.preferencesUri(pkg))?.use { context.contentResolver.openInputStream(Provider.preferencesUri(pkg))?.writeTo(prefsFile)
prefs.edit { parsePrefs(it) } return
}
} }
prefs.edit { prefs.edit {
@ -191,52 +186,4 @@ object Config : PreferenceConfig, DBConfig {
} }
} }
} }
private fun SharedPreferences.Editor.parsePrefs(input: InputStream) {
runCatching {
val parser = Xml.newPullParser()
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false)
parser.setInput(input, "UTF-8")
parser.nextTag()
parser.require(XmlPullParser.START_TAG, null, "map")
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.eventType != XmlPullParser.START_TAG)
continue
val key: String = parser.getAttributeValue(null, "name")
fun value() = parser.getAttributeValue(null, "value")!!
when (parser.name) {
"string" -> {
parser.require(XmlPullParser.START_TAG, null, "string")
putString(key, parser.nextText())
parser.require(XmlPullParser.END_TAG, null, "string")
}
"boolean" -> {
parser.require(XmlPullParser.START_TAG, null, "boolean")
putBoolean(key, value().toBoolean())
parser.nextTag()
parser.require(XmlPullParser.END_TAG, null, "boolean")
}
"int" -> {
parser.require(XmlPullParser.START_TAG, null, "int")
putInt(key, value().toInt())
parser.nextTag()
parser.require(XmlPullParser.END_TAG, null, "int")
}
"long" -> {
parser.require(XmlPullParser.START_TAG, null, "long")
putLong(key, value().toLong())
parser.nextTag()
parser.require(XmlPullParser.END_TAG, null, "long")
}
"float" -> {
parser.require(XmlPullParser.START_TAG, null, "int")
putFloat(key, value().toFloat())
parser.nextTag()
parser.require(XmlPullParser.END_TAG, null, "int")
}
else -> parser.next()
}
}
}
}
} }

View File

@ -16,7 +16,7 @@ class Provider : BaseProvider() {
override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? { override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? {
return when (uri.encodedPath ?: return null) { return when (uri.encodedPath ?: return null) {
"/prefs_file" -> ParcelFileDescriptor.open(Config.prefsFile, MODE_READ_ONLY) "/prefs_file" -> ParcelFileDescriptor.open(Config.getPrefsFile(), MODE_READ_ONLY)
else -> super.openFile(uri, mode) else -> super.openFile(uri, mode)
} }
} }