refactor: double-back-to-close

This commit is contained in:
inotia00 2022-12-27 14:45:53 +09:00
parent cbcdb2f719
commit ffd7c99025
9 changed files with 164 additions and 119 deletions

View File

@ -0,0 +1,14 @@
package app.revanced.patches.youtube.misc.doublebacktoclose.fingerprint
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.Opcode
object OnBackPressedFingerprint : MethodFingerprint(
opcodes = listOf(
Opcode.RETURN_VOID
),
customFingerprint = { methodDef ->
methodDef.definingClass.endsWith("WatchWhileActivity;")
&& methodDef.name == "onBackPressed"
}
)

View File

@ -1,4 +1,4 @@
package app.revanced.patches.youtube.misc.doublebacktoexit.fingerprint
package app.revanced.patches.youtube.misc.doublebacktoclose.fingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint

View File

@ -0,0 +1,21 @@
package app.revanced.patches.youtube.misc.doublebacktoclose.fingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
object ScrollTopFingerprint : MethodFingerprint(
returnType = "V",
access = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = listOf(),
opcodes = listOf(
Opcode.CHECK_CAST,
Opcode.CONST_4,
Opcode.INVOKE_VIRTUAL,
Opcode.GOTO,
Opcode.IGET_OBJECT,
Opcode.INVOKE_INTERFACE
)
)

View File

@ -0,0 +1,26 @@
package app.revanced.patches.youtube.misc.doublebacktoclose.fingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
object ScrollTopParentFingerprint : MethodFingerprint(
returnType = "V",
access = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
parameters = listOf("L", "L", "L", "L"),
opcodes = listOf(
Opcode.INVOKE_DIRECT,
Opcode.IPUT_OBJECT,
Opcode.IPUT_OBJECT,
Opcode.IPUT_OBJECT,
Opcode.IPUT_OBJECT,
Opcode.CONST_16,
Opcode.INVOKE_VIRTUAL,
Opcode.NEW_INSTANCE
),
customFingerprint = { methodDef ->
methodDef.name == "<init>"
}
)

View File

@ -0,0 +1,100 @@
package app.revanced.patches.youtube.misc.doublebacktoclose.patch
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.data.toMethodWalker
import app.revanced.patcher.extensions.addInstruction
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve
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.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.youtube.misc.doublebacktoclose.fingerprint.OnBackPressedFingerprint
import app.revanced.patches.youtube.misc.doublebacktoclose.fingerprint.ScrollPositionFingerprint
import app.revanced.patches.youtube.misc.doublebacktoclose.fingerprint.ScrollTopFingerprint
import app.revanced.patches.youtube.misc.doublebacktoclose.fingerprint.ScrollTopParentFingerprint
import app.revanced.shared.annotation.YouTubeCompatibility
import app.revanced.shared.extensions.toErrorResult
import app.revanced.shared.patches.gestures.PredictiveBackGesturePatch
import app.revanced.shared.util.integrations.Constants.UTILS_PATH
@Patch
@Name("double-back-to-close")
@DependsOn([PredictiveBackGesturePatch::class])
@YouTubeCompatibility
@Version("0.0.1")
class DoubleBackToClosePatch : BytecodePatch(
listOf(
OnBackPressedFingerprint,
ScrollPositionFingerprint,
ScrollTopParentFingerprint
)
) {
override fun execute(context: BytecodeContext): PatchResult {
/*
Hook onBackPressed method inside WatchWhileActivity
*/
OnBackPressedFingerprint.result?.let { result ->
val insertIndex = result.scanResult.patternScanResult!!.endIndex
with(result.mutableMethod) {
addInstruction(
insertIndex,
"invoke-static {p0}, $INTEGRATIONS_CLASS_DESCRIPTOR" +
"->" +
"closeActivityOnBackPressed(Lcom/google/android/apps/youtube/app/watchwhile/WatchWhileActivity;)V"
)
}
} ?: return OnBackPressedFingerprint.toErrorResult()
/*
Inject the methods which start of ScrollView
*/
ScrollPositionFingerprint.result?.let { result ->
val insertMethod = context.toMethodWalker(result.method)
.nextMethod(result.scanResult.patternScanResult!!.startIndex + 1, true)
.getMethod() as MutableMethod
val insertIndex = insertMethod.implementation!!.instructions.size - 1 - 1
injectScrollView(insertMethod, insertIndex, "onStartScrollView")
} ?: return ScrollPositionFingerprint.toErrorResult()
/*
Inject the methods which stop of ScrollView
*/
ScrollTopParentFingerprint.result?.let { parentResult ->
ScrollTopFingerprint.also { it.resolve(context, parentResult.classDef) }.result?.let { result ->
val insertMethod = result.mutableMethod
val insertIndex = result.scanResult.patternScanResult!!.endIndex
injectScrollView(insertMethod, insertIndex, "onStopScrollView")
} ?: return ScrollTopFingerprint.toErrorResult()
} ?: return ScrollTopParentFingerprint.toErrorResult()
return PatchResultSuccess()
}
private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR =
"$UTILS_PATH/DoubleBackToClosePatch;"
fun injectScrollView(
method: MutableMethod,
index: Int,
descriptor: String
) {
method.addInstruction(
index,
"invoke-static {}, $INTEGRATIONS_CLASS_DESCRIPTOR->$descriptor()V"
)
}
}
}

View File

@ -1,22 +0,0 @@
package app.revanced.patches.youtube.misc.doublebacktoexit.fingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
object RecyclerViewFingerprint : MethodFingerprint(
returnType = "V",
access = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = listOf("I"),
opcodes = listOf(
Opcode.INVOKE_VIRTUAL,
Opcode.IGET_OBJECT,
Opcode.IF_NEZ,
),
strings = listOf("Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument."),
customFingerprint = { methodDef ->
methodDef.definingClass == "Landroid/support/v7/widget/RecyclerView;"
}
)

View File

@ -1,68 +0,0 @@
package app.revanced.patches.youtube.misc.doublebacktoexit.patch
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.data.toMethodWalker
import app.revanced.patcher.extensions.addInstruction
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.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.youtube.misc.doublebacktoexit.fingerprint.RecyclerViewFingerprint
import app.revanced.patches.youtube.misc.doublebacktoexit.fingerprint.ScrollPositionFingerprint
import app.revanced.shared.annotation.YouTubeCompatibility
import app.revanced.shared.extensions.toErrorResult
import app.revanced.shared.patches.gestures.PredictiveBackGesturePatch
import app.revanced.shared.util.bytecode.BytecodeHelper
import app.revanced.shared.util.integrations.Constants.UTILS_PATH
import org.jf.dexlib2.Opcode
@Name("enable-double-back-to-exit")
@Description("Enable double back to exit.")
@YouTubeCompatibility
@Version("0.0.1")
@DependsOn([PredictiveBackGesturePatch::class])
class DoubleBackToExitPatch : BytecodePatch(
listOf(
RecyclerViewFingerprint,
ScrollPositionFingerprint
)
) {
override fun execute(context: BytecodeContext): PatchResult {
val DTE_CLASS_DESCRIPTOR = "$UTILS_PATH/DoubleBackToExitPatch;"
val scrollPositionResult = ScrollPositionFingerprint.result!!
val scrollPositionMethod =
context.toMethodWalker(scrollPositionResult.method)
.nextMethod(scrollPositionResult.scanResult.patternScanResult!!.startIndex + 1, true)
.getMethod() as MutableMethod
val backToExitInstructions = scrollPositionMethod.implementation!!.instructions
for ((index, instruction) in backToExitInstructions.withIndex()) {
if (instruction.opcode != Opcode.CONST_4) continue
scrollPositionMethod.addInstruction(
index + 2,
"invoke-static {}, $DTE_CLASS_DESCRIPTOR->onCreate()V"
)
break
}
val recyclerViewResult = RecyclerViewFingerprint.result ?: return RecyclerViewFingerprint.toErrorResult()
val recyclerViewMethod = recyclerViewResult.mutableMethod
val recyclerViewEndIndex = recyclerViewResult.scanResult.patternScanResult!!.endIndex
recyclerViewMethod.addInstruction(
recyclerViewEndIndex,
"invoke-static {}, $DTE_CLASS_DESCRIPTOR->onDestroy()V"
)
BytecodeHelper.injectBackPressed(context)
return PatchResultSuccess()
}
}

View File

@ -9,7 +9,7 @@ import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.youtube.misc.doublebacktoexit.patch.DoubleBackToExitPatch
import app.revanced.patches.youtube.misc.doublebacktoclose.patch.DoubleBackToClosePatch
import app.revanced.patches.youtube.misc.litho.filter.fingerprints.LithoFingerprint
import app.revanced.patches.youtube.misc.swiperefresh.patch.SwipeRefreshPatch
import app.revanced.shared.annotation.YouTubeCompatibility
@ -22,7 +22,7 @@ import org.jf.dexlib2.Opcode
@DependsOn(
[
DoubleBackToExitPatch::class,
DoubleBackToClosePatch::class,
SwipeRefreshPatch::class
]
)

View File

@ -28,32 +28,6 @@ internal object BytecodeHelper {
}
}
fun injectBackPressed(
context: BytecodeContext
) {
context.classes.forEach { classDef ->
classDef.methods.forEach { method ->
if (classDef.type.endsWith("WatchWhileActivity;") && method.name == "onBackPressed") {
val onBackPressedMethod =
context.proxy(classDef).mutableClass.methods.first { it.name == "onBackPressed" }
val onBackPressedMethodInstructions =
onBackPressedMethod.implementation!!.instructions
for ((index, instruction) in onBackPressedMethodInstructions.withIndex()) {
if (instruction.opcode != Opcode.RETURN_VOID) continue
onBackPressedMethod.addInstruction(
index,
"invoke-static {p0}, $UTILS_PATH/DoubleBackToExitPatch;->doubleBackToExit(Lcom/google/android/apps/youtube/app/watchwhile/WatchWhileActivity;)V"
)
break
}
}
}
}
}
fun patchStatus(
context: BytecodeContext,
name: String