refactor: better method is used for settings-framework

This commit is contained in:
inotia00
2023-02-18 01:57:06 +09:00
parent fb45389c5e
commit ef370c82d1
3 changed files with 10 additions and 25 deletions

View File

@ -18,6 +18,7 @@ import app.revanced.patches.youtube.misc.microg.shared.Constants.SPOOFED_PACKAGE
import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsPatch import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsPatch
import app.revanced.util.microg.MicroGManifestHelper.addSpoofingMetadata import app.revanced.util.microg.MicroGManifestHelper.addSpoofingMetadata
import app.revanced.util.microg.MicroGResourceHelper.patchManifest import app.revanced.util.microg.MicroGResourceHelper.patchManifest
import app.revanced.util.microg.MicroGResourceHelper.patchSetting
import app.revanced.util.resources.ResourceHelper.setMicroG import app.revanced.util.resources.ResourceHelper.setMicroG
@Patch @Patch
@ -49,12 +50,10 @@ class MicroGPatch : ResourcePatch {
) )
SettingsPatch.updatePatchStatus("microg-support") SettingsPatch.updatePatchStatus("microg-support")
val settingsFragment = context["res/xml/settings_fragment.xml"] // update settings fragment
settingsFragment.writeText( context.patchSetting(
settingsFragment.readText().replace( PACKAGE_NAME,
"android:targetPackage=\"com.google.android.youtube", packageName
"android:targetPackage=\"$packageName"
)
) )
// update manifest // update manifest

View File

@ -16,7 +16,6 @@ import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsPatch
import app.revanced.patches.youtube.video.customspeed.bytecode.patch.CustomVideoSpeedBytecodePatch import app.revanced.patches.youtube.video.customspeed.bytecode.patch.CustomVideoSpeedBytecodePatch
import app.revanced.util.resources.ResourceHelper.addSpeedEntries import app.revanced.util.resources.ResourceHelper.addSpeedEntries
import app.revanced.util.resources.ResourceHelper.addSpeedEntryValues import app.revanced.util.resources.ResourceHelper.addSpeedEntryValues
import app.revanced.util.resources.ResourceHelper.replaceCustomSpeed
import app.revanced.util.resources.ResourceUtils.copyXmlNode import app.revanced.util.resources.ResourceUtils.copyXmlNode
@Patch @Patch
@ -50,8 +49,6 @@ class CustomVideoSpeedPatch : ResourcePatch {
context.addSpeedEntryValues(speedElements[index]) context.addSpeedEntryValues(speedElements[index])
} }
context.replaceCustomSpeed()
/* /*
add settings add settings
*/ */

View File

@ -7,7 +7,7 @@ import org.w3c.dom.Node
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.StandardCopyOption import java.nio.file.StandardCopyOption
private fun Node.adoptChild(tagName: String, targetNode: Node, block: Element.() -> Unit) { private fun Node.insertNode(tagName: String, targetNode: Node, block: Element.() -> Unit) {
val child = ownerDocument.createElement(tagName) val child = ownerDocument.createElement(tagName)
child.block() child.block()
parentNode.insertBefore(child, targetNode) parentNode.insertBefore(child, targetNode)
@ -20,11 +20,11 @@ internal object ResourceHelper {
private const val TARGET_PREFERENCE_PATH = "res/xml/revanced_prefs.xml" private const val TARGET_PREFERENCE_PATH = "res/xml/revanced_prefs.xml"
private const val TARGET_SETTINGS_PATH = "res/xml/settings_fragment.xml" private const val YOUTUBE_SETTINGS_PATH = "res/xml/settings_fragment.xml"
private var targetPackage = "com.google.android.youtube" private var targetPackage = "com.google.android.youtube"
fun setMicroG (newPackage: String) { internal fun setMicroG (newPackage: String) {
targetPackage = newPackage targetPackage = newPackage
} }
@ -71,17 +71,6 @@ internal object ResourceHelper {
) )
} }
internal fun ResourceContext.replaceCustomSpeed() {
val prefs = this[TARGET_PREFERENCE_PATH]
prefs.writeText(
prefs.readText()
.replace(
"revanced_default_video_speed_entry",
"revanced_custom_video_speed_entry"
)
)
}
internal fun ResourceContext.addPreference(settingArray: Array<String>) { internal fun ResourceContext.addPreference(settingArray: Array<String>) {
val prefs = this[TARGET_PREFERENCE_PATH] val prefs = this[TARGET_PREFERENCE_PATH]
@ -130,13 +119,13 @@ internal object ResourceHelper {
internal fun ResourceContext.addReVancedPreference(key: String) { internal fun ResourceContext.addReVancedPreference(key: String) {
val targetClass = "com.google.android.apps.youtube.app.settings.videoquality.VideoQualitySettingsActivity" val targetClass = "com.google.android.apps.youtube.app.settings.videoquality.VideoQualitySettingsActivity"
this.xmlEditor[TARGET_SETTINGS_PATH].use { editor -> this.xmlEditor[YOUTUBE_SETTINGS_PATH].use { editor ->
with (editor.file) { with (editor.file) {
doRecursively loop@{ doRecursively loop@{
if (it !is Element) return@loop if (it !is Element) return@loop
it.getAttributeNode("android:key")?.let { attribute -> it.getAttributeNode("android:key")?.let { attribute ->
if (attribute.textContent == "@string/about_key" && it.getAttributeNode("app:iconSpaceReserved").textContent == "false") { if (attribute.textContent == "@string/about_key" && it.getAttributeNode("app:iconSpaceReserved").textContent == "false") {
it.adoptChild("Preference", it) { it.insertNode("Preference", it) {
setAttribute("android:title", "@string/revanced_" + key + "_title") setAttribute("android:title", "@string/revanced_" + key + "_title")
setAttribute("android:summary", "@string/revanced_" + key + "_summary") setAttribute("android:summary", "@string/revanced_" + key + "_summary")
this.appendChild(ownerDocument.createElement("intent").also { intentNode -> this.appendChild(ownerDocument.createElement("intent").also { intentNode ->