fix(YouTube Music - SponsorBlock): Change segment behavior and About sections are hidden in the settings https://github.com/inotia00/ReVanced_Extended/issues/2575

This commit is contained in:
inotia00 2024-12-21 13:44:01 +09:00
parent aa3abf57d1
commit 32467937db

View File

@ -3,7 +3,6 @@ package app.revanced.patches.music.utils.sponsorblock
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.ResourcePatchContext
import app.revanced.patcher.patch.bytecodePatch import app.revanced.patcher.patch.bytecodePatch
import app.revanced.patcher.patch.resourcePatch import app.revanced.patcher.patch.resourcePatch
import app.revanced.patches.music.utils.compatibility.Constants.COMPATIBLE_PACKAGE import app.revanced.patches.music.utils.compatibility.Constants.COMPATIBLE_PACKAGE
@ -170,6 +169,134 @@ val sponsorBlockPatch = resourcePatch(
) )
execute { execute {
fun addSwitchPreference(
category: String,
key: String,
defaultValue: String,
dependencyKey: String
) {
document(SETTINGS_HEADER_PATH).use { document ->
val tags = document.getElementsByTagName(PREFERENCE_SCREEN_TAG_NAME)
List(tags.length) { tags.item(it) as Element }
.filter {
it.getAttribute("android:key").contains("revanced_preference_screen_$category")
}
.forEach {
it.adoptChild(SWITCH_PREFERENCE_TAG_NAME) {
setAttribute("android:title", "@string/revanced_$key")
setAttribute("android:summary", "@string/revanced_$key" + "_sum")
setAttribute("android:key", key)
setAttribute("android:defaultValue", defaultValue)
if (dependencyKey != "") {
setAttribute("android:dependency", dependencyKey)
}
}
}
}
}
fun addSwitchPreference(
category: String,
key: String,
defaultValue: String
) = addSwitchPreference(category, key, defaultValue, "")
fun addPreferenceWithIntent(
category: String,
key: String,
dependencyKey: String
) {
document(SETTINGS_HEADER_PATH).use { document ->
val tags = document.getElementsByTagName(PREFERENCE_SCREEN_TAG_NAME)
List(tags.length) { tags.item(it) as Element }
.filter {
it.getAttribute("android:key").contains("revanced_preference_screen_$category")
}
.forEach {
it.adoptChild("Preference") {
setAttribute("android:title", "@string/revanced_$key")
setAttribute("android:summary", "@string/revanced_$key" + "_sum")
setAttribute("android:key", key)
setAttribute("android:dependency", dependencyKey)
this.adoptChild("intent") {
setAttribute("android:targetPackage", musicPackageName)
setAttribute("android:data", key)
setAttribute(
"android:targetClass",
ACTIVITY_HOOK_TARGET_CLASS
)
}
}
}
}
}
fun addPreferenceCategoryUnderPreferenceScreen(
preferenceScreenKey: String,
category: String
) {
document(SETTINGS_HEADER_PATH).use { document ->
val tags = document.getElementsByTagName(PREFERENCE_SCREEN_TAG_NAME)
List(tags.length) { tags.item(it) as Element }
.filter { it.getAttribute("android:key").contains(preferenceScreenKey) }
.forEach {
it.adoptChild(PREFERENCE_CATEGORY_TAG_NAME) {
setAttribute("android:title", "@string/revanced_$category")
setAttribute("android:key", category)
}
}
}
}
fun addSegmentsPreference(
key: String,
dependencyKey: String
) {
document(SETTINGS_HEADER_PATH).use { document ->
val tags = document.getElementsByTagName(PREFERENCE_CATEGORY_TAG_NAME)
List(tags.length) { tags.item(it) as Element }
.filter { it.getAttribute("android:key") == SEGMENTS_CATEGORY_KEY }
.forEach {
it.adoptChild("Preference") {
setAttribute("android:title", "@string/revanced_$key")
setAttribute("android:summary", "@string/revanced_$key" + "_sum")
setAttribute("android:key", key)
setAttribute("android:dependency", dependencyKey)
this.adoptChild("intent") {
setAttribute("android:targetPackage", musicPackageName)
setAttribute("android:data", key)
setAttribute(
"android:targetClass",
ACTIVITY_HOOK_TARGET_CLASS
)
}
}
}
}
}
fun addAboutPreference(
key: String,
data: String
) {
document(SETTINGS_HEADER_PATH).use { document ->
val tags = document.getElementsByTagName(PREFERENCE_CATEGORY_TAG_NAME)
List(tags.length) { tags.item(it) as Element }
.filter { it.getAttribute("android:key") == ABOUT_CATEGORY_KEY }
.forEach {
it.adoptChild("Preference") {
setAttribute("android:title", "@string/revanced_$key")
setAttribute("android:summary", "@string/revanced_$key" + "_sum")
setAttribute("android:key", key)
this.adoptChild("intent") {
setAttribute("android:action", "android.intent.action.VIEW")
setAttribute("android:data", data)
}
}
}
}
}
addPreferenceCategory(SPONSOR_BLOCK_CATEGORY) addPreferenceCategory(SPONSOR_BLOCK_CATEGORY)
addSwitchPreference( addSwitchPreference(
@ -201,42 +328,34 @@ val sponsorBlockPatch = resourcePatch(
) )
addSegmentsPreference( addSegmentsPreference(
SEGMENTS_CATEGORY_KEY,
"sb_segments_sponsor", "sb_segments_sponsor",
"sb_enabled" "sb_enabled"
) )
addSegmentsPreference( addSegmentsPreference(
SEGMENTS_CATEGORY_KEY,
"sb_segments_selfpromo", "sb_segments_selfpromo",
"sb_enabled" "sb_enabled"
) )
addSegmentsPreference( addSegmentsPreference(
SEGMENTS_CATEGORY_KEY,
"sb_segments_interaction", "sb_segments_interaction",
"sb_enabled" "sb_enabled"
) )
addSegmentsPreference( addSegmentsPreference(
SEGMENTS_CATEGORY_KEY,
"sb_segments_intro", "sb_segments_intro",
"sb_enabled" "sb_enabled"
) )
addSegmentsPreference( addSegmentsPreference(
SEGMENTS_CATEGORY_KEY,
"sb_segments_outro", "sb_segments_outro",
"sb_enabled" "sb_enabled"
) )
addSegmentsPreference( addSegmentsPreference(
SEGMENTS_CATEGORY_KEY,
"sb_segments_preview", "sb_segments_preview",
"sb_enabled" "sb_enabled"
) )
addSegmentsPreference( addSegmentsPreference(
SEGMENTS_CATEGORY_KEY,
"sb_segments_filler", "sb_segments_filler",
"sb_enabled" "sb_enabled"
) )
addSegmentsPreference( addSegmentsPreference(
SEGMENTS_CATEGORY_KEY,
"sb_segments_nomusic", "sb_segments_nomusic",
"sb_enabled" "sb_enabled"
) )
@ -247,7 +366,6 @@ val sponsorBlockPatch = resourcePatch(
) )
addAboutPreference( addAboutPreference(
ABOUT_CATEGORY_KEY,
"sb_about_api", "sb_about_api",
"https://sponsor.ajay.app" "https://sponsor.ajay.app"
) )
@ -267,133 +385,3 @@ val sponsorBlockPatch = resourcePatch(
} }
} }
private fun ResourcePatchContext.addSwitchPreference(
category: String,
key: String,
defaultValue: String
) = addSwitchPreference(category, key, defaultValue, "")
private fun ResourcePatchContext.addSwitchPreference(
category: String,
key: String,
defaultValue: String,
dependencyKey: String
) {
document(SETTINGS_HEADER_PATH).use { document ->
val tags = document.getElementsByTagName(PREFERENCE_SCREEN_TAG_NAME)
List(tags.length) { tags.item(it) as Element }
.filter {
it.getAttribute("android:key").contains("revanced_preference_screen_$category")
}
.forEach {
it.adoptChild(SWITCH_PREFERENCE_TAG_NAME) {
setAttribute("android:title", "@string/revanced_$key")
setAttribute("android:summary", "@string/revanced_$key" + "_sum")
setAttribute("android:key", key)
setAttribute("android:defaultValue", defaultValue)
if (dependencyKey != "") {
setAttribute("android:dependency", dependencyKey)
}
}
}
}
}
private fun ResourcePatchContext.addPreferenceWithIntent(
category: String,
key: String,
dependencyKey: String
) {
document(SETTINGS_HEADER_PATH).use { document ->
val tags = document.getElementsByTagName(PREFERENCE_SCREEN_TAG_NAME)
List(tags.length) { tags.item(it) as Element }
.filter {
it.getAttribute("android:key").contains("revanced_preference_screen_$category")
}
.forEach {
it.adoptChild("Preference") {
setAttribute("android:title", "@string/revanced_$key")
setAttribute("android:summary", "@string/revanced_$key" + "_sum")
setAttribute("android:key", key)
setAttribute("android:dependency", dependencyKey)
this.adoptChild("intent") {
setAttribute("android:targetPackage", musicPackageName)
setAttribute("android:data", key)
setAttribute(
"android:targetClass",
ACTIVITY_HOOK_TARGET_CLASS
)
}
}
}
}
}
private fun ResourcePatchContext.addPreferenceCategoryUnderPreferenceScreen(
preferenceScreenKey: String,
category: String
) {
document(SETTINGS_HEADER_PATH).use { document ->
val tags = document.getElementsByTagName(PREFERENCE_SCREEN_TAG_NAME)
List(tags.length) { tags.item(it) as Element }
.filter { it.getAttribute("android:key").contains(preferenceScreenKey) }
.forEach {
it.adoptChild(PREFERENCE_CATEGORY_TAG_NAME) {
setAttribute("android:title", "@string/revanced_$category")
setAttribute("android:key", category)
}
}
}
}
private fun ResourcePatchContext.addSegmentsPreference(
preferenceCategoryKey: String,
key: String,
dependencyKey: String
) {
document(SETTINGS_HEADER_PATH).use { document ->
val tags = document.getElementsByTagName(PREFERENCE_SCREEN_TAG_NAME)
List(tags.length) { tags.item(it) as Element }
.filter { it.getAttribute("android:key").contains(preferenceCategoryKey) }
.forEach {
it.adoptChild("Preference") {
setAttribute("android:title", "@string/revanced_$key")
setAttribute("android:summary", "@string/revanced_$key" + "_sum")
setAttribute("android:key", key)
setAttribute("android:dependency", dependencyKey)
this.adoptChild("intent") {
setAttribute("android:targetPackage", musicPackageName)
setAttribute("android:data", key)
setAttribute(
"android:targetClass",
ACTIVITY_HOOK_TARGET_CLASS
)
}
}
}
}
}
private fun ResourcePatchContext.addAboutPreference(
preferenceCategoryKey: String,
key: String,
data: String
) {
document(SETTINGS_HEADER_PATH).use { document ->
val tags = document.getElementsByTagName(PREFERENCE_SCREEN_TAG_NAME)
List(tags.length) { tags.item(it) as Element }
.filter { it.getAttribute("android:key").contains(preferenceCategoryKey) }
.forEach {
it.adoptChild("Preference") {
setAttribute("android:title", "@string/revanced_$key")
setAttribute("android:summary", "@string/revanced_$key" + "_sum")
setAttribute("android:key", key)
this.adoptChild("intent") {
setAttribute("android:action", "android.intent.action.VIEW")
setAttribute("android:data", data)
}
}
}
}
}