From a481c922cfd95bc22c5815ab512290229ebb130b Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Thu, 27 Jun 2024 23:23:44 +0900 Subject: [PATCH] feat(YouTube/Toolbar components): add `Hide image search button` settings https://github.com/inotia00/ReVanced_Extended/issues/2188 --- .../components/LayoutComponentsPatch.kt | 61 +++------ .../general/toolbar/ToolBarComponentsPatch.kt | 127 ++++++++---------- .../ImageSearchButtonConfigFingerprint.kt | 14 ++ .../youtube/settings/host/values/strings.xml | 3 + .../youtube/settings/xml/revanced_prefs.xml | 5 +- 5 files changed, 93 insertions(+), 117 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/general/toolbar/fingerprints/ImageSearchButtonConfigFingerprint.kt diff --git a/src/main/kotlin/app/revanced/patches/music/general/components/LayoutComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/music/general/components/LayoutComponentsPatch.kt index aa229a630..6c332458f 100644 --- a/src/main/kotlin/app/revanced/patches/music/general/components/LayoutComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/general/components/LayoutComponentsPatch.kt @@ -7,7 +7,6 @@ import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWith import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction import app.revanced.patcher.patch.PatchException -import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.booleanPatchOption import app.revanced.patcher.util.smali.ExternalLabel import app.revanced.patches.music.general.components.fingerprints.ChipCloudFingerprint import app.revanced.patches.music.general.components.fingerprints.ContentPillInFingerprint @@ -32,7 +31,6 @@ import app.revanced.patches.music.utils.resourceid.SharedResourceIdPatch.TopBarM import app.revanced.patches.music.utils.settings.CategoryType import app.revanced.patches.music.utils.settings.SettingsPatch import app.revanced.patches.shared.litho.LithoFilterPatch -import app.revanced.patches.shared.voicesearch.VoiceSearchUtils.patchXml import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.getWideLiteralInstructionIndex @@ -74,18 +72,9 @@ object LayoutComponentsPatch : BaseBytecodePatch( private const val LAYOUT_COMPONENTS_FILTER_CLASS_DESCRIPTOR = "$COMPONENTS_PATH/LayoutComponentsFilter;" - private val ForceHideVoiceSearchButton by booleanPatchOption( - key = "ForceHideVoiceSearchButton", - default = false, - title = "Force hide voice search button", - description = "Permanently hide the voice search button with the legacy method.", - required = true - ) - override fun execute(context: BytecodeContext) { var notificationButtonIncluded = false var soundSearchButtonIncluded = false - var voiceSearchButtonIncluded = false // region patch for hide cast button @@ -272,31 +261,23 @@ object LayoutComponentsPatch : BaseBytecodePatch( // region patch for hide voice search button - if (ForceHideVoiceSearchButton == true) { - SettingsPatch.contexts.patchXml( - arrayOf("search_toolbar_view.xml"), - arrayOf("height", "width") - ) - } else { - SearchBarFingerprint.resolve( - context, - SearchBarParentFingerprint.resultOrThrow().classDef - ) - SearchBarFingerprint.resultOrThrow().let { - it.mutableMethod.apply { - val setVisibilityIndex = - getTargetIndexWithMethodReferenceNameOrThrow("setVisibility") - val setVisibilityInstruction = - getInstruction(setVisibilityIndex) + SearchBarFingerprint.resolve( + context, + SearchBarParentFingerprint.resultOrThrow().classDef + ) + SearchBarFingerprint.resultOrThrow().let { + it.mutableMethod.apply { + val setVisibilityIndex = + getTargetIndexWithMethodReferenceNameOrThrow("setVisibility") + val setVisibilityInstruction = + getInstruction(setVisibilityIndex) - replaceInstruction( - setVisibilityIndex, - "invoke-static {v${setVisibilityInstruction.registerC}, v${setVisibilityInstruction.registerD}}, " + - "$GENERAL_CLASS_DESCRIPTOR->hideVoiceSearchButton(Landroid/widget/ImageView;I)V" - ) - } + replaceInstruction( + setVisibilityIndex, + "invoke-static {v${setVisibilityInstruction.registerC}, v${setVisibilityInstruction.registerD}}, " + + "$GENERAL_CLASS_DESCRIPTOR->hideVoiceSearchButton(Landroid/widget/ImageView;I)V" + ) } - voiceSearchButtonIncluded = true } // endregion @@ -373,12 +354,10 @@ object LayoutComponentsPatch : BaseBytecodePatch( "false" ) } - if (voiceSearchButtonIncluded) { - SettingsPatch.addSwitchPreference( - CategoryType.GENERAL, - "revanced_hide_voice_search_button", - "false" - ) - } + SettingsPatch.addSwitchPreference( + CategoryType.GENERAL, + "revanced_hide_voice_search_button", + "false" + ) } } diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/ToolBarComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/ToolBarComponentsPatch.kt index 072ca16c8..cbf337831 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/ToolBarComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/ToolBarComponentsPatch.kt @@ -8,10 +8,8 @@ import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.extensions.InstructionExtensions.removeInstruction import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction import app.revanced.patcher.patch.PatchException -import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.booleanPatchOption import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patcher.util.smali.ExternalLabel -import app.revanced.patches.shared.voicesearch.VoiceSearchUtils.patchXml import app.revanced.patches.youtube.general.toolbar.fingerprints.ActionBarRingoBackgroundFingerprint import app.revanced.patches.youtube.general.toolbar.fingerprints.ActionBarRingoTextFingerprint import app.revanced.patches.youtube.general.toolbar.fingerprints.AttributeResolverFingerprint @@ -19,6 +17,7 @@ import app.revanced.patches.youtube.general.toolbar.fingerprints.CreateButtonDra import app.revanced.patches.youtube.general.toolbar.fingerprints.CreateSearchSuggestionsFingerprint import app.revanced.patches.youtube.general.toolbar.fingerprints.DrawerContentViewConstructorFingerprint import app.revanced.patches.youtube.general.toolbar.fingerprints.DrawerContentViewFingerprint +import app.revanced.patches.youtube.general.toolbar.fingerprints.ImageSearchButtonConfigFingerprint import app.revanced.patches.youtube.general.toolbar.fingerprints.SearchBarFingerprint import app.revanced.patches.youtube.general.toolbar.fingerprints.SearchBarParentFingerprint import app.revanced.patches.youtube.general.toolbar.fingerprints.SearchResultFingerprint @@ -45,6 +44,7 @@ import app.revanced.util.getTargetIndexWithReferenceOrThrow import app.revanced.util.getTargetIndexWithReferenceReversedOrThrow import app.revanced.util.getWalkerMethod import app.revanced.util.getWideLiteralInstructionIndex +import app.revanced.util.literalInstructionBooleanHook import app.revanced.util.literalInstructionHook import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow @@ -77,21 +77,19 @@ object ToolBarComponentsPatch : BaseBytecodePatch( SearchBarParentFingerprint, SearchResultFingerprint, SetActionBarRingoFingerprint, - SetWordMarkHeaderFingerprint + SetWordMarkHeaderFingerprint, + ImageSearchButtonConfigFingerprint, ) ) { private const val TARGET_RESOURCE_PATH = "res/layout/action_bar_ringo_background.xml" - private val ForceHideVoiceSearchButton by booleanPatchOption( - key = "ForceHideVoiceSearchButton", - default = false, - title = "Force hide voice search button", - description = "Permanently hide the voice search button with the legacy method.", - required = true - ) - override fun execute(context: BytecodeContext) { + var settingArray = arrayOf( + "PREFERENCE_SCREEN: GENERAL", + "SETTINGS: TOOLBAR_COMPONENTS" + ) + // region patch for change YouTube header // Invoke YouTube's header attribute into integrations. @@ -308,69 +306,55 @@ object ToolBarComponentsPatch : BaseBytecodePatch( // region patch for hide voice search button - if (ForceHideVoiceSearchButton == true) { - contexts.patchXml( - arrayOf( - "action_bar_search_results_view_mic.xml", - "action_bar_search_view.xml", - "action_bar_search_view_grey.xml", - "action_bar_search_view_mic_out.xml" - ), - arrayOf( - "height", - "marginEnd", - "marginStart", - "width" + if (SettingsPatch.upward1923) { + ImageSearchButtonConfigFingerprint.literalInstructionBooleanHook( + 45617544, + "$GENERAL_CLASS_DESCRIPTOR->hideImageSearchButton(Z)Z" + ) + + settingArray += "SETTINGS: HIDE_IMAGE_SEARCH_BUTTON" + } + + // endregion + + // region patch for hide voice search button + + SearchBarFingerprint.resolve( + context, + SearchBarParentFingerprint.resultOrThrow().classDef + ) + SearchBarFingerprint.resultOrThrow().let { + it.mutableMethod.apply { + val startIndex = it.scanResult.patternScanResult!!.startIndex + val setVisibilityIndex = + getTargetIndexWithMethodReferenceNameOrThrow(startIndex, "setVisibility") + val setVisibilityInstruction = + getInstruction(setVisibilityIndex) + + replaceInstruction( + setVisibilityIndex, + "invoke-static {v${setVisibilityInstruction.registerC}, v${setVisibilityInstruction.registerD}}, " + + "$GENERAL_CLASS_DESCRIPTOR->hideVoiceSearchButton(Landroid/view/View;I)V" ) - ) - } else { - SearchBarFingerprint.resolve( - context, - SearchBarParentFingerprint.resultOrThrow().classDef - ) - - SearchBarFingerprint.resultOrThrow().let { - it.mutableMethod.apply { - val startIndex = it.scanResult.patternScanResult!!.startIndex - val setVisibilityIndex = - getTargetIndexWithMethodReferenceNameOrThrow(startIndex, "setVisibility") - val setVisibilityInstruction = - getInstruction(setVisibilityIndex) - - replaceInstruction( - setVisibilityIndex, - "invoke-static {v${setVisibilityInstruction.registerC}, v${setVisibilityInstruction.registerD}}, " + - "$GENERAL_CLASS_DESCRIPTOR->hideVoiceSearchButton(Landroid/view/View;I)V" - ) - } } + } - SearchResultFingerprint.resultOrThrow().let { - it.mutableMethod.apply { - val startIndex = getWideLiteralInstructionIndex(VoiceSearch) - val setOnClickListenerIndex = - getTargetIndexWithMethodReferenceNameOrThrow( - startIndex, - "setOnClickListener" - ) - val viewRegister = - getInstruction(setOnClickListenerIndex).registerC - - addInstruction( - setOnClickListenerIndex + 1, - "invoke-static {v$viewRegister}, $GENERAL_CLASS_DESCRIPTOR->hideVoiceSearchButton(Landroid/view/View;)V" + SearchResultFingerprint.resultOrThrow().let { + it.mutableMethod.apply { + val startIndex = getWideLiteralInstructionIndex(VoiceSearch) + val setOnClickListenerIndex = + getTargetIndexWithMethodReferenceNameOrThrow( + startIndex, + "setOnClickListener" ) - } - } + val viewRegister = + getInstruction(setOnClickListenerIndex).registerC - /** - * Add settings - */ - SettingsPatch.addPreference( - arrayOf( - "SETTINGS: HIDE_VOICE_SEARCH_BUTTON" + addInstruction( + setOnClickListenerIndex + 1, + "invoke-static {v$viewRegister}, $GENERAL_CLASS_DESCRIPTOR->hideVoiceSearchButton(Landroid/view/View;)V" ) - ) + } } // endregion @@ -406,12 +390,7 @@ object ToolBarComponentsPatch : BaseBytecodePatch( /** * Add settings */ - SettingsPatch.addPreference( - arrayOf( - "PREFERENCE_SCREEN: GENERAL", - "SETTINGS: TOOLBAR_COMPONENTS" - ) - ) + SettingsPatch.addPreference(settingArray) SettingsPatch.updatePatchStatus(this) } diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/fingerprints/ImageSearchButtonConfigFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/fingerprints/ImageSearchButtonConfigFingerprint.kt new file mode 100644 index 000000000..1a139ee62 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/fingerprints/ImageSearchButtonConfigFingerprint.kt @@ -0,0 +1,14 @@ +package app.revanced.patches.youtube.general.toolbar.fingerprints + +import app.revanced.patcher.extensions.or +import app.revanced.util.fingerprint.LiteralValueFingerprint +import com.android.tools.smali.dexlib2.AccessFlags + +/** + * This fingerprint is compatible with YouTube v19.07.40+ + */ +internal object ImageSearchButtonConfigFingerprint : LiteralValueFingerprint( + returnType = "Z", + accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, + literalSupplier = { 45617544 } +) \ No newline at end of file diff --git a/src/main/resources/youtube/settings/host/values/strings.xml b/src/main/resources/youtube/settings/host/values/strings.xml index b15c76765..edca5626e 100644 --- a/src/main/resources/youtube/settings/host/values/strings.xml +++ b/src/main/resources/youtube/settings/host/values/strings.xml @@ -477,6 +477,9 @@ You tab → View channel → Menu → Settings" Hide search term thumbnails Thumbnails in the search term history are hidden. Thumbnails in the search term history are shown. + Hide image search button + Image search button is hidden. + Image search button is shown. Hide voice search button Voice search button is hidden. Voice search button is shown. diff --git a/src/main/resources/youtube/settings/xml/revanced_prefs.xml b/src/main/resources/youtube/settings/xml/revanced_prefs.xml index 0f52f0d23..f3471000d 100644 --- a/src/main/resources/youtube/settings/xml/revanced_prefs.xml +++ b/src/main/resources/youtube/settings/xml/revanced_prefs.xml @@ -174,10 +174,11 @@ SETTINGS: TOOLBAR_COMPONENTS --> - +