Skip to content

Commit

Permalink
refactor: Update navigation patch
Browse files Browse the repository at this point in the history
  • Loading branch information
anddea committed Apr 12, 2024
1 parent 18f0ae1 commit c58911b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.youtube.navigation.navigationbuttons.fingerprints.MainActivityOnBackPressedFingerprint
import app.revanced.patches.youtube.navigation.navigationbuttons.fingerprints.PivotBarButtonsViewSetSelectedFingerprint
import app.revanced.patches.youtube.navigation.navigationbuttons.fingerprints.*
import app.revanced.util.exception
import app.revanced.util.getReference
Expand All @@ -33,7 +35,9 @@ object NavigationBarHookPatch : BytecodePatch(
NavigationEnumFingerprint,
PivotBarButtonsCreateDrawableViewFingerprint,
PivotBarButtonsCreateResourceViewFingerprint,
PivotBarButtonsViewSetSelectedFingerprint,
NavigationBarHookCallbackFingerprint,
MainActivityOnBackPressedFingerprint,
ActionBarSearchResultsFingerprint,
),
) {
Expand Down Expand Up @@ -90,6 +94,29 @@ object NavigationBarHookPatch : BytecodePatch(
}
}

PivotBarButtonsViewSetSelectedFingerprint.getResultOrThrow().mutableMethod.apply {
val index = PivotBarButtonsViewSetSelectedFingerprint.indexOfSetViewSelectedInstruction(this)
val instruction = getInstruction<FiveRegisterInstruction>(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.getResultOrThrow().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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package app.revanced.patches.youtube.navigation.navigationbuttons.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"
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app.revanced.patches.youtube.navigation.navigationbuttons.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.patches.youtube.navigation.navigationbuttons.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<MethodReference>()?.name == "setSelected"
}
}

0 comments on commit c58911b

Please sign in to comment.