From 8f33b110facb8f8a04416459f3d715bc77002231 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 26 Feb 2023 22:59:57 +0100 Subject: [PATCH] feat(youtube): `hide-floating-microphone-button` patch Signed-off-by: oSumAtrIX --- ...deFloatingMicrophoneButtonCompatibility.kt | 13 +++++ ...ShowFloatingMicrophoneButtonFingerprint.kt | 19 +++++++ .../HideFloatingMicrophoneButtonPatch.kt | 51 +++++++++++++++++++ ...deFloatingMicrophoneButtonResourcePatch.kt | 42 +++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/annotations/HideFloatingMicrophoneButtonCompatibility.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/fingerprints/ShowFloatingMicrophoneButtonFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/patch/HideFloatingMicrophoneButtonPatch.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/patch/HideFloatingMicrophoneButtonResourcePatch.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/annotations/HideFloatingMicrophoneButtonCompatibility.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/annotations/HideFloatingMicrophoneButtonCompatibility.kt new file mode 100644 index 000000000..767d673e7 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/annotations/HideFloatingMicrophoneButtonCompatibility.kt @@ -0,0 +1,13 @@ +package app.revanced.patches.youtube.layout.hide.floatingmicrophone.annotations + +import app.revanced.patcher.annotation.Compatibility +import app.revanced.patcher.annotation.Package + +@Compatibility( + [Package( + "com.google.android.youtube", arrayOf("17.49.37", "18.03.36") + )] +) +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +internal annotation class HideFloatingMicrophoneButtonCompatibility diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/fingerprints/ShowFloatingMicrophoneButtonFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/fingerprints/ShowFloatingMicrophoneButtonFingerprint.kt new file mode 100644 index 000000000..ed72bd968 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/fingerprints/ShowFloatingMicrophoneButtonFingerprint.kt @@ -0,0 +1,19 @@ +package app.revanced.patches.youtube.layout.hide.floatingmicrophone.fingerprints + +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.layout.hide.floatingmicrophone.patch.HideFloatingMicrophoneButtonResourcePatch +import org.jf.dexlib2.Opcode +import org.jf.dexlib2.iface.instruction.WideLiteralInstruction + +object ShowFloatingMicrophoneButtonFingerprint : MethodFingerprint( + opcodes = listOf( + Opcode.IGET_BOOLEAN, + Opcode.IF_EQZ, + Opcode.RETURN_VOID + ), + customFingerprint = { methodDef -> + methodDef.implementation?.instructions?.any { + (it as? WideLiteralInstruction)?.wideLiteral == HideFloatingMicrophoneButtonResourcePatch.fabButtonId + } == true + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/patch/HideFloatingMicrophoneButtonPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/patch/HideFloatingMicrophoneButtonPatch.kt new file mode 100644 index 000000000..30f02c143 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/patch/HideFloatingMicrophoneButtonPatch.kt @@ -0,0 +1,51 @@ +package app.revanced.patches.youtube.layout.hide.floatingmicrophone.patch + +import app.revanced.extensions.toErrorResult +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.addInstructions +import app.revanced.patcher.extensions.instruction +import app.revanced.patcher.patch.BytecodePatch +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patches.youtube.layout.hide.floatingmicrophone.annotations.HideFloatingMicrophoneButtonCompatibility +import app.revanced.patches.youtube.layout.hide.floatingmicrophone.fingerprints.ShowFloatingMicrophoneButtonFingerprint +import org.jf.dexlib2.iface.instruction.TwoRegisterInstruction + +@Patch +@Name("hide-floating-microphone-button") +@Description("Hides the floating microphone button which appears in search.") +@DependsOn([HideFloatingMicrophoneButtonResourcePatch::class]) +@HideFloatingMicrophoneButtonCompatibility +@Version("0.0.1") +class HideFloatingMicrophoneButtonPatch : BytecodePatch( + listOf(ShowFloatingMicrophoneButtonFingerprint) +) { + override fun execute(context: BytecodeContext): PatchResult { + ShowFloatingMicrophoneButtonFingerprint.result?.let { result -> + with(result.mutableMethod) { + val insertIndex = result.scanResult.patternScanResult!!.startIndex + 1 + val showButtonRegister = (instruction(insertIndex - 1) as TwoRegisterInstruction).registerA + + addInstructions( + insertIndex, + """ + invoke-static {v$showButtonRegister}, $INTEGRATIONS_CLASS_DESCRIPTOR->hideFloatingMicrophoneButton(Z)Z + move-result v$showButtonRegister + """ + ) + } + } ?: return ShowFloatingMicrophoneButtonFingerprint.toErrorResult() + + return PatchResultSuccess() + } + + private companion object { + const val INTEGRATIONS_CLASS_DESCRIPTOR = + "Lapp/revanced/integrations/patches/HideFloatingMicrophoneButtonPatch;" + } +} diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/patch/HideFloatingMicrophoneButtonResourcePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/patch/HideFloatingMicrophoneButtonResourcePatch.kt new file mode 100644 index 000000000..5bc6cc043 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/hide/floatingmicrophone/patch/HideFloatingMicrophoneButtonResourcePatch.kt @@ -0,0 +1,42 @@ +package app.revanced.patches.youtube.layout.hide.floatingmicrophone.patch + +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.ResourceContext +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultError +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.ResourcePatch +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patches.shared.mapping.misc.patch.ResourceMappingPatch +import app.revanced.patches.shared.settings.preference.impl.StringResource +import app.revanced.patches.shared.settings.preference.impl.SwitchPreference +import app.revanced.patches.youtube.layout.hide.floatingmicrophone.annotations.HideFloatingMicrophoneButtonCompatibility +import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch + +@DependsOn([SettingsPatch::class, ResourceMappingPatch::class]) +@HideFloatingMicrophoneButtonCompatibility +@Version("0.0.1") +class HideFloatingMicrophoneButtonResourcePatch : ResourcePatch { + override fun execute(context: ResourceContext): PatchResult { + SettingsPatch.PreferenceScreen.LAYOUT.addPreferences( + SwitchPreference( + "revanced_hide_floating_microphone_button", + StringResource( + "revanced_hide_floating_microphone_button_enabled_title", + "Hide floating microphone button" + ), + true, + StringResource("revanced_hide_floating_microphone_button_summary_on", "Microphone button hidden"), + StringResource("revanced_hide_floating_microphone_button_summary_off", "Microphone button shown") + ) + ) + + fabButtonId = ResourceMappingPatch.resourceMappings.find { it.type == "id" && it.name == "fab" }?.id + ?: return PatchResultError("Can not find required fab button resource id") + return PatchResultSuccess() + } + + internal companion object { + var fabButtonId: Long = -1 + } +}