From e5848e99c4cc838595164ef673a77fe60d28086b Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Wed, 10 Apr 2024 12:29:39 +0400 Subject: [PATCH] fix(YouTube - Hide Shorts components): Correctly hide Shorts if navigation tab is changed using device back button (#3007) --- .../misc/navigation/NavigationBarHookPatch.kt | 25 +++++++++++++++++ .../MainActivityOnBackPressedFingerprint.kt | 17 ++++++++++++ ...votBarButtonsViewSetSelectedFingerprint.kt | 27 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/misc/navigation/fingerprints/MainActivityOnBackPressedFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/misc/navigation/fingerprints/PivotBarButtonsViewSetSelectedFingerprint.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/navigation/NavigationBarHookPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/navigation/NavigationBarHookPatch.kt index 514ec929..eb9f31e8 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/navigation/NavigationBarHookPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/navigation/NavigationBarHookPatch.kt @@ -36,7 +36,9 @@ object NavigationBarHookPatch : BytecodePatch( NavigationEnumFingerprint, PivotBarButtonsCreateDrawableViewFingerprint, PivotBarButtonsCreateResourceViewFingerprint, + PivotBarButtonsViewSetSelectedFingerprint, NavigationBarHookCallbackFingerprint, + MainActivityOnBackPressedFingerprint, ActionBarSearchResultsFingerprint, ), ) { @@ -90,6 +92,29 @@ object NavigationBarHookPatch : BytecodePatch( } } + PivotBarButtonsViewSetSelectedFingerprint.resultOrThrow().mutableMethod.apply { + val index = PivotBarButtonsViewSetSelectedFingerprint.indexOfSetViewSelectedInstruction(this) + val instruction = getInstruction(index) + val viewRegister = instruction.registerC + val isSelectedRegister = instruction.registerD + + addInstruction( + index + 1, + "invoke-static { v$viewRegister, v$isSelectedRegister }, " + + "$INTEGRATIONS_CLASS_DESCRIPTOR->navigationTabSelected(Landroid/view/View;Z)V", + ) + } + + // Hook onto back button pressed. Needed to fix race problem with + // litho filtering based on navigation tab before the tab is updated. + MainActivityOnBackPressedFingerprint.resultOrThrow().mutableMethod.apply { + addInstruction( + 0, + "invoke-static { p0 }, " + + "$INTEGRATIONS_CLASS_DESCRIPTOR->onBackPressed(Landroid/app/Activity;)V", + ) + } + // Hook the search bar. // Two different layouts are used at the hooked code. diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/navigation/fingerprints/MainActivityOnBackPressedFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/navigation/fingerprints/MainActivityOnBackPressedFingerprint.kt new file mode 100644 index 00000000..23ee7488 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/navigation/fingerprints/MainActivityOnBackPressedFingerprint.kt @@ -0,0 +1,17 @@ +package app.revanced.patches.youtube.misc.navigation.fingerprints + +import app.revanced.patcher.extensions.or +import app.revanced.patcher.fingerprint.MethodFingerprint +import com.android.tools.smali.dexlib2.AccessFlags + +internal object MainActivityOnBackPressedFingerprint : MethodFingerprint( + accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, + returnType = "V", + parameters = listOf(), + customFingerprint = { methodDef, _ -> + (methodDef.definingClass.endsWith("MainActivity;") || + // Old versions of YouTube called this class "WatchWhileActivity" instead. + methodDef.definingClass.endsWith("WatchWhileActivity;")) + && methodDef.name == "onBackPressed" + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/navigation/fingerprints/PivotBarButtonsViewSetSelectedFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/navigation/fingerprints/PivotBarButtonsViewSetSelectedFingerprint.kt new file mode 100644 index 00000000..8e1c3621 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/navigation/fingerprints/PivotBarButtonsViewSetSelectedFingerprint.kt @@ -0,0 +1,27 @@ +package app.revanced.patches.youtube.misc.navigation.fingerprints + +import app.revanced.patcher.extensions.or +import app.revanced.patcher.fingerprint.MethodFingerprint +import app.revanced.patches.youtube.misc.navigation.fingerprints.PivotBarButtonsViewSetSelectedFingerprint.indexOfSetViewSelectedInstruction +import app.revanced.util.getReference +import app.revanced.util.indexOfFirstInstruction +import com.android.tools.smali.dexlib2.AccessFlags +import com.android.tools.smali.dexlib2.Opcode +import com.android.tools.smali.dexlib2.iface.Method +import com.android.tools.smali.dexlib2.iface.reference.MethodReference + +internal object PivotBarButtonsViewSetSelectedFingerprint : MethodFingerprint( + accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL, + parameters = listOf("I", "Z"), + returnType = "V", + customFingerprint = { methodDef, classDef -> + classDef.type == "Lcom/google/android/libraries/youtube/rendering/ui/pivotbar/PivotBar;" && + indexOfSetViewSelectedInstruction(methodDef) >= 0 + } +) { + fun indexOfSetViewSelectedInstruction(methodDef: Method) = + methodDef.indexOfFirstInstruction { + opcode == Opcode.INVOKE_VIRTUAL && getReference()?.name == "setSelected" + } +} +