Skip to content

Commit

Permalink
feat(YouTube): Add Hide settings preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
anddea committed Mar 31, 2024
1 parent d297a88 commit d8b1da2
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/revanced-patches.api
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,12 @@ public final class app/revanced/patches/youtube/layout/pipnotification/fingerpri
public static final field INSTANCE Lapp/revanced/patches/youtube/layout/pipnotification/fingerprints/PiPNotificationFingerprint;
}

public final class app/revanced/patches/youtube/layout/settings/HideSettingsPrefsPatch : app/revanced/patcher/patch/ResourcePatch {
public static final field INSTANCE Lapp/revanced/patches/youtube/layout/settings/HideSettingsPrefsPatch;
public synthetic fun execute (Lapp/revanced/patcher/data/Context;)V
public fun execute (Lapp/revanced/patcher/data/ResourceContext;)V
}

public final class app/revanced/patches/youtube/layout/splashanimation/AddSplashAnimationPatch : app/revanced/patcher/patch/ResourcePatch {
public static final field INSTANCE Lapp/revanced/patches/youtube/layout/splashanimation/AddSplashAnimationPatch;
public synthetic fun execute (Lapp/revanced/patcher/data/Context;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package app.revanced.patches.youtube.layout.settings

import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotation.CompatiblePackage
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import org.w3c.dom.Element

@Patch(
name = "Hide settings preferences",
description = "Force to hide settings menu elements. Prefs \"About\" (it won't add ReVanced/SB/RYD settings) and \"Account\" (it will crash the app) will be ignored if you add them.",
compatiblePackages = [CompatiblePackage("com.google.android.youtube")]
)
@Suppress("unused")
object HideSettingsPrefsPatch : ResourcePatch() {

private const val DEFAULT_ELEMENTS = "General, Data saving, Autoplay, " +
"Video quality preferences, Background, Watch on TV, History & privacy, " +
"Try experimental new features, Notifications, Captions, Accessibility"

private val SettingElements by stringPatchOption(
key = "SettingElements",
default = DEFAULT_ELEMENTS,
title = "Main settings elements",
description = "Hide main settings elements."
)

private val DEFAULT_ELEMENTS_MAP = mapOf(
"General" to "general_key",
"Account" to "account_switcher_key",
"Data saving" to "data_saving_settings_key",
"Autoplay" to "auto_play_key",
"Video quality preferences" to "video_quality_settings_key",
"Background" to "offline_key", // no
"Watch on TV" to "pair_with_tv_key",
"History & privacy" to "privacy_key",
"Try experimental new features" to "premium_early_access_browse_page_key",
"Notifications" to "notification_key",
"Captions" to "captions_key",
"Accessibility" to "accessibility_settings_key"
)

// Function to parse comma-separated string into a list of strings
private fun parseElementsFromString(elementsString: String?): List<String> {
return elementsString?.split(",")!!.map { it.trim() }
}

override fun execute(context: ResourceContext) {

val elementsToHide = mutableListOf<String>()

parseElementsFromString(SettingElements).forEach { element ->
val key = DEFAULT_ELEMENTS_MAP[element]
if (key != null) {
elementsToHide.add("@string/$key")
} else {
println("WARNING: Unknown element '$element' will be skipped")
}
}

context.xmlEditor["res/xml/settings_fragment.xml"].use { editor ->
val preferenceElements = editor.file.getElementsByTagName("Preference")

for (i in preferenceElements.length - 1 downTo 0) {
val item = preferenceElements.item(i) as? Element
val titleAttribute = item?.getAttribute("android:key")
if (titleAttribute in elementsToHide) {
item?.parentNode?.removeChild(item)
}
}
}

SettingsPatch.updatePatchStatus("Hide settings preferences")
}
}

0 comments on commit d8b1da2

Please sign in to comment.