From c89b2aa26125d302079b5cf522f3b6c9fbe1257a Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sat, 26 Aug 2023 17:10:21 +0200 Subject: [PATCH] feat(YouTube): Add `Enable tablet layout` patch --- .../EnableTabletLayoutCompatibility.kt | 8 +++ .../fingerprints/GetFormFactorFingerprint.kt | 25 +++++++++ .../tablet/patch/EnableTabletLayoutPatch.kt | 56 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/tablet/annotations/EnableTabletLayoutCompatibility.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/tablet/fingerprints/GetFormFactorFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/tablet/patch/EnableTabletLayoutPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/annotations/EnableTabletLayoutCompatibility.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/annotations/EnableTabletLayoutCompatibility.kt new file mode 100644 index 00000000..430281f6 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/annotations/EnableTabletLayoutCompatibility.kt @@ -0,0 +1,8 @@ +package app.revanced.patches.youtube.layout.tablet.annotations + +import app.revanced.patcher.annotation.Compatibility +import app.revanced.patcher.annotation.Package + +@Compatibility([Package("com.google.android.youtube")]) +@Target(AnnotationTarget.CLASS) +internal annotation class EnableTabletLayoutCompatibility \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/fingerprints/GetFormFactorFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/fingerprints/GetFormFactorFingerprint.kt new file mode 100644 index 00000000..da576067 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/fingerprints/GetFormFactorFingerprint.kt @@ -0,0 +1,25 @@ +package app.revanced.patches.youtube.layout.tablet.fingerprints + +import app.revanced.patcher.extensions.or +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import com.android.tools.smali.dexlib2.AccessFlags +import com.android.tools.smali.dexlib2.Opcode + +object GetFormFactorFingerprint : MethodFingerprint( + accessFlags = AccessFlags.PUBLIC or AccessFlags.STATIC, + returnType = "L", + parameters = listOf("Landroid/content/Context;", "Ljava/util/List;"), + opcodes = listOf( + Opcode.SGET_OBJECT, + Opcode.INVOKE_VIRTUAL, + Opcode.MOVE_RESULT_OBJECT, + Opcode.INVOKE_VIRTUAL, + Opcode.MOVE_RESULT, + Opcode.IF_EQZ, + Opcode.SGET_OBJECT, + Opcode.RETURN_OBJECT, + Opcode.INVOKE_STATIC, + Opcode.MOVE_RESULT_OBJECT, + Opcode.RETURN_OBJECT + ) +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/patch/EnableTabletLayoutPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/patch/EnableTabletLayoutPatch.kt new file mode 100644 index 00000000..95bbc1c4 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/tablet/patch/EnableTabletLayoutPatch.kt @@ -0,0 +1,56 @@ +package app.revanced.patches.youtube.layout.tablet.patch + +import app.revanced.extensions.exception +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction +import app.revanced.patcher.patch.BytecodePatch +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patcher.util.smali.ExternalLabel +import app.revanced.patches.shared.settings.preference.impl.StringResource +import app.revanced.patches.shared.settings.preference.impl.SwitchPreference +import app.revanced.patches.youtube.layout.tablet.annotations.EnableTabletLayoutCompatibility +import app.revanced.patches.youtube.layout.tablet.fingerprints.GetFormFactorFingerprint +import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch +import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch + +@Patch +@DependsOn([IntegrationsPatch::class, SettingsPatch::class]) +@Name("Enable tablet layout") +@Description("Spoofs the device form factor to a tablet which enables the tablet layout.") +@EnableTabletLayoutCompatibility +class EnableTabletLayoutPatch : BytecodePatch(listOf(GetFormFactorFingerprint)) { + override fun execute(context: BytecodeContext) { + SettingsPatch.PreferenceScreen.LAYOUT.addPreferences( + SwitchPreference( + "revanced_tablet_layout", + StringResource("revanced_tablet_layout_enabled_title", "Enable tablet layout"), + StringResource("revanced_tablet_layout_summary_on", "Tablet layout is enabled"), + StringResource("revanced_tablet_layout_summary_off", "Tablet layout is disabled"), + StringResource("revanced_tablet_layout_user_dialog_message", "Community posts do not show up on tablet layouts") + ) + ) + + GetFormFactorFingerprint.result?.let { + it.mutableMethod.apply { + val returnLargeFormFactorIndex = it.scanResult.patternScanResult!!.endIndex - 4 + + addInstructionsWithLabels( + 0, + """ + invoke-static {}, Lapp/revanced/integrations/patches/EnableTabletLayoutPatch;->enableTabletLayout()Z + move-result v0 + if-nez v0, :is_large_form_factor + """, + ExternalLabel( + "is_large_form_factor", + getInstruction(returnLargeFormFactorIndex) + ) + ) + } + } ?: GetFormFactorFingerprint.exception + } +}