Skip to content

Commit

Permalink
feat(Hide ads): add Close fullscreen ads settings inotia00/ReVanced…
Browse files Browse the repository at this point in the history
  • Loading branch information
inotia00 committed Jun 10, 2024
1 parent 4aa18cc commit c003ad9
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 103 deletions.
85 changes: 20 additions & 65 deletions src/main/kotlin/app/revanced/patches/music/ads/general/AdsPatch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package app.revanced.patches.music.ads.general
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.music.ads.general.MusicAdsPatch.hookLithoFullscreenAds
import app.revanced.patches.music.ads.general.fingerprints.AccountMenuFooterFingerprint
import app.revanced.patches.music.ads.general.fingerprints.FloatingLayoutFingerprint
import app.revanced.patches.music.ads.general.fingerprints.GetPremiumTextViewFingerprint
Expand All @@ -25,6 +24,7 @@ import app.revanced.patches.music.utils.resourceid.SharedResourceIdPatch.Interst
import app.revanced.patches.music.utils.settings.CategoryType
import app.revanced.patches.music.utils.settings.SettingsPatch
import app.revanced.patches.shared.litho.LithoFilterPatch
import app.revanced.patches.youtube.ads.general.VideoAdsPatch.hookNonLithoFullscreenAds
import app.revanced.util.getTargetIndex
import app.revanced.util.getTargetIndexWithReference
import app.revanced.util.getWalkerMethod
Expand Down Expand Up @@ -58,11 +58,11 @@ object AdsPatch : BaseBytecodePatch(
ShowDialogCommandFingerprint
)
) {
private const val FILTER_CLASS_DESCRIPTOR =
private const val ADS_FILTER_CLASS_DESCRIPTOR =
"$COMPONENTS_PATH/AdsFilter;"

private const val FULLSCREEN_ADS_CLASS_DESCRIPTOR =
"$ADS_PATH/FullscreenAdsPatch;"
private const val FULLSCREEN_ADS_FILTER_CLASS_DESCRIPTOR =
"${app.revanced.patches.shared.integrations.Constants.COMPONENTS_PATH}/FullscreenAdsFilter;"

private const val PREMIUM_PROMOTION_POP_UP_CLASS_DESCRIPTOR =
"$ADS_PATH/PremiumPromotionPatch;"
Expand All @@ -71,72 +71,18 @@ object AdsPatch : BaseBytecodePatch(
"$ADS_PATH/PremiumRenewalPatch;"

override fun execute(context: BytecodeContext) {
LithoFilterPatch.addFilter(FILTER_CLASS_DESCRIPTOR)

// region patch for hide fullscreen ads

// non-litho view, used in some old clients
InterstitialsContainerFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val targetIndex = getWideLiteralInstructionIndex(InterstitialsContainer) + 2
val targetRegister = getInstruction<OneRegisterInstruction>(targetIndex).registerA

addInstruction(
targetIndex + 1,
"invoke-static {v$targetRegister}, $FULLSCREEN_ADS_CLASS_DESCRIPTOR->hideFullscreenAds(Landroid/view/View;)V"
)
}
}
InterstitialsContainerFingerprint
.resultOrThrow()
.hookNonLithoFullscreenAds(InterstitialsContainer)

// litho view, used in 'ShowDialogCommandOuterClass' in innertube
ShowDialogCommandFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
// In this method, custom dialog is created and shown.
// There were no issues despite adding “return-void” to the first index.
addInstructionsWithLabels(
0,
"""
invoke-static/range {p2 .. p2}, $FULLSCREEN_ADS_CLASS_DESCRIPTOR->hideFullscreenAds(Ljava/lang/Object;)Z
move-result v0
if-eqz v0, :show
return-void
""", ExternalLabel("show", getInstruction(0))
)

// If an issue occurs due to patching due to server-side changes in the future,
// Find the instruction whose name is "show" in [MethodReference] and click the 'AlertDialog.BUTTON_POSITIVE' button.
//
// In this case, an instruction for 'getButton' must be added to smali, not in integrations
// (This custom dialog cannot be cast to [AlertDialog] or [Dialog])
//
// See the comments below.

// val dialogIndex = getTargetIndexWithMethodReferenceName("show")
// val dialogReference = getInstruction<ReferenceInstruction>(dialogIndex).reference
// val dialogDefiningClass = (dialogReference as MethodReference).definingClass
// val getButtonMethod = context.findClass(dialogDefiningClass)!!
// .mutableClass.methods.first { method ->
// method.parameters == listOf("I")
// && method.returnType == "Landroid/widget/Button;"
// }
// val getButtonCall = dialogDefiningClass + "->" + getButtonMethod.name + "(I)Landroid/widget/Button;"
// val dialogRegister = getInstruction<FiveRegisterInstruction>(dialogIndex).registerC
// val freeIndex = getTargetIndex(dialogIndex, Opcode.IF_EQZ)
// val freeRegister = getInstruction<OneRegisterInstruction>(freeIndex).registerA

// addInstructions(
// dialogIndex + 1, """
// # Get the 'AlertDialog.BUTTON_POSITIVE' from custom dialog
// # Since this custom dialog cannot be cast to AlertDialog or Dialog,
// # It should come from smali, not integrations.
// const/4 v$freeRegister, -0x1
// invoke-virtual {v$dialogRegister, $freeRegister}, $getButtonCall
// move-result-object $freeRegister
// invoke-static {$freeRegister}, $FULLSCREEN_ADS_CLASS_DESCRIPTOR->confirmDialog(Landroid/widget/Button;)V
// """
// )
}
}
ShowDialogCommandFingerprint
.resultOrThrow()
.hookLithoFullscreenAds(context)

// endregion

Expand Down Expand Up @@ -227,11 +173,20 @@ object AdsPatch : BaseBytecodePatch(

// endregion

LithoFilterPatch.addFilter(ADS_FILTER_CLASS_DESCRIPTOR)
LithoFilterPatch.addFilter(FULLSCREEN_ADS_FILTER_CLASS_DESCRIPTOR)

SettingsPatch.addSwitchPreference(
CategoryType.ADS,
"revanced_hide_fullscreen_ads",
"true"
)
SettingsPatch.addSwitchPreference(
CategoryType.ADS,
"revanced_hide_fullscreen_ads_type",
"true",
"revanced_hide_fullscreen_ads"
)
SettingsPatch.addSwitchPreference(
CategoryType.ADS,
"revanced_hide_general_ads",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ package app.revanced.patches.music.ads.general.fingerprints

import app.revanced.patches.music.utils.resourceid.SharedResourceIdPatch.SlidingDialogAnimation
import app.revanced.util.fingerprint.LiteralValueFingerprint
import com.android.tools.smali.dexlib2.Opcode

internal object ShowDialogCommandFingerprint : LiteralValueFingerprint(
returnType = "V",
parameters = listOf("[B", "L"),
opcodes = listOf(
Opcode.IF_EQ,
Opcode.IGET_OBJECT,
Opcode.INVOKE_VIRTUAL,
Opcode.IGET, // get dialog code
),
literalSupplier = { SlidingDialogAnimation }
)
83 changes: 83 additions & 0 deletions src/main/kotlin/app/revanced/patches/shared/ads/BaseAdsPatch.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package app.revanced.patches.shared.ads

import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.fingerprint.MethodFingerprintResult
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.shared.ads.fingerprints.MusicAdsFingerprint
import app.revanced.patches.shared.ads.fingerprints.VideoAdsFingerprint
import app.revanced.patches.shared.integrations.Constants.PATCHES_PATH
import app.revanced.util.getTargetIndex
import app.revanced.util.getTargetIndexWithMethodReferenceName
import app.revanced.util.getWalkerMethod
import app.revanced.util.getWideLiteralInstructionIndex
import app.revanced.util.indexOfFirstInstruction
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
import com.android.tools.smali.dexlib2.iface.reference.FieldReference
import com.android.tools.smali.dexlib2.iface.reference.MethodReference

abstract class BaseAdsPatch(
Expand Down Expand Up @@ -59,4 +69,77 @@ abstract class BaseAdsPatch(
}
}
}

internal fun MethodFingerprintResult.hookNonLithoFullscreenAds(literal: Long) {
mutableMethod.apply {
val targetIndex = getWideLiteralInstructionIndex(literal) + 2
val targetRegister = getInstruction<OneRegisterInstruction>(targetIndex).registerA

addInstruction(
targetIndex + 1,
"invoke-static {v$targetRegister}, $INTEGRATIONS_CLASS_DESCRIPTOR->hideFullscreenAds(Landroid/view/View;)V"
)
}
}

internal fun MethodFingerprintResult.hookLithoFullscreenAds(context: BytecodeContext) {
mutableMethod.apply {
val dialogCodeIndex = scanResult.patternScanResult!!.endIndex
val dialogCodeField = getInstruction<ReferenceInstruction>(dialogCodeIndex).reference as FieldReference
if (dialogCodeField.type != "I")
throw PatchException("Invalid dialogCodeField: $dialogCodeField")

// Disable fullscreen ads
addInstructionsWithLabels(
0,
"""
move-object v0, p2
# In the latest version of YouTube and YouTube Music, it is used after being cast
check-cast v0, ${dialogCodeField.definingClass}
iget v0, v0, $dialogCodeField
invoke-static {v0}, $INTEGRATIONS_CLASS_DESCRIPTOR->disableFullscreenAds(I)Z
move-result v0
if-eqz v0, :show
return-void
""", ExternalLabel("show", getInstruction(0))
)

// Close fullscreen ads

// Find the instruction whose name is "show" in [MethodReference] and click the 'AlertDialog.BUTTON_POSITIVE' button.
// In this case, an instruction for 'getButton' must be added to smali, not in integrations
// (This custom dialog cannot be cast to [AlertDialog] or [Dialog])
val dialogIndex = getTargetIndexWithMethodReferenceName("show")
val dialogReference = getInstruction<ReferenceInstruction>(dialogIndex).reference
val dialogDefiningClass = (dialogReference as MethodReference).definingClass
val getButtonMethod = context.findClass(dialogDefiningClass)!!
.mutableClass.methods.first { method ->
method.parameters == listOf("I")
&& method.returnType == "Landroid/widget/Button;"
}
val getButtonCall = dialogDefiningClass + "->" + getButtonMethod.name + "(I)Landroid/widget/Button;"
val dialogRegister = getInstruction<FiveRegisterInstruction>(dialogIndex).registerC
val freeIndex = getTargetIndex(dialogIndex, Opcode.IF_EQZ)
val freeRegister = getInstruction<OneRegisterInstruction>(freeIndex).registerA

addInstructions(
dialogIndex + 1, """
# Get the 'AlertDialog.BUTTON_POSITIVE' from custom dialog
# Since this custom dialog cannot be cast to AlertDialog or Dialog,
# It should come from smali, not integrations.
const/4 v$freeRegister, -0x1
invoke-virtual {v$dialogRegister, v$freeRegister}, $getButtonCall
move-result-object v$freeRegister
invoke-static {v$freeRegister}, $INTEGRATIONS_CLASS_DESCRIPTOR->setCloseButton(Landroid/widget/Button;)V
"""
)
}
}

private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR =
"$PATCHES_PATH/FullscreenAdsPatch;"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import app.revanced.patches.shared.gms.fingerprints.GmsCoreSupportFingerprint.GE
import app.revanced.patches.shared.gms.fingerprints.GooglePlayUtilityFingerprint
import app.revanced.patches.shared.gms.fingerprints.PrimeMethodFingerprint
import app.revanced.patches.shared.gms.fingerprints.ServiceCheckFingerprint
import app.revanced.patches.shared.integrations.Constants.INTEGRATIONS_PATH
import app.revanced.patches.shared.integrations.Constants.PATCHES_PATH
import app.revanced.patches.shared.packagename.PackageNamePatch
import app.revanced.util.getReference
import app.revanced.util.resultOrThrow
Expand Down Expand Up @@ -106,7 +106,7 @@ abstract class BaseGmsCoreSupportPatch(
// Verify GmsCore is installed and whitelisted for power optimizations and background usage.
mainActivityOnCreateFingerprint.resultOrThrow().mutableMethod.addInstructions(
1, // Hack to not disturb other patches (such as the YTMusic integrations patch).
"invoke-static/range { p0 .. p0 }, $INTEGRATIONS_PATH/patches/GmsCoreSupport;->" +
"invoke-static/range { p0 .. p0 }, $PATCHES_PATH/GmsCoreSupport;->" +
"checkGmsCore(Landroid/app/Activity;)V",
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package app.revanced.patches.shared.integrations

@Suppress("MemberVisibilityCanBePrivate", "SpellCheckingInspection")
@Suppress("MemberVisibilityCanBePrivate")
object Constants {
const val INTEGRATIONS_PATH = "Lapp/revanced/integrations/shared"
const val COMPONENTS_PATH = "$INTEGRATIONS_PATH/patches/components"
const val PATCHES_PATH = "$INTEGRATIONS_PATH/patches"
const val COMPONENTS_PATH = "$PATCHES_PATH/components"

const val INTEGRATIONS_SETTING_CLASS_DESCRIPTOR = "$INTEGRATIONS_PATH/settings/Setting;"
const val INTEGRATIONS_UTILS_CLASS_DESCRIPTOR = "$INTEGRATIONS_PATH/utils/Utils;"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package app.revanced.patches.youtube.ads.general

import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
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.annotation.Patch
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.music.ads.general.MusicAdsPatch.hookLithoFullscreenAds
import app.revanced.patches.youtube.ads.general.VideoAdsPatch.hookNonLithoFullscreenAds
import app.revanced.patches.youtube.ads.general.fingerprints.CompactYpcOfferModuleViewFingerprint
import app.revanced.patches.youtube.ads.general.fingerprints.InterstitialsContainerFingerprint
import app.revanced.patches.youtube.ads.general.fingerprints.ShowDialogCommandFingerprint
Expand All @@ -15,11 +16,9 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.AdAttribution
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.InterstitialsContainer
import app.revanced.util.findMutableMethodOf
import app.revanced.util.getWideLiteralInstructionIndex
import app.revanced.util.injectHideViewCall
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.formats.Instruction31i
import com.android.tools.smali.dexlib2.iface.instruction.formats.Instruction35c
Expand All @@ -37,34 +36,14 @@ object AdsBytecodePatch : BytecodePatch(
// region patch for hide fullscreen ads

// non-litho view, used in some old clients.
InterstitialsContainerFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val targetIndex = getWideLiteralInstructionIndex(InterstitialsContainer) + 2
val targetRegister = getInstruction<OneRegisterInstruction>(targetIndex).registerA

addInstruction(
targetIndex + 1,
"invoke-static {v$targetRegister}, $ADS_CLASS_DESCRIPTOR->hideFullscreenAds(Landroid/view/View;)V"
)
}
}
InterstitialsContainerFingerprint
.resultOrThrow()
.hookNonLithoFullscreenAds(InterstitialsContainer)

// litho view, used in 'ShowDialogCommandOuterClass' in innertube
ShowDialogCommandFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
// In this method, custom dialog is created and shown.
// There were no issues despite adding “return-void” to the first index.
addInstructionsWithLabels(
0,
"""
invoke-static/range {p2 .. p2}, $ADS_CLASS_DESCRIPTOR->hideFullscreenAds(Ljava/lang/Object;)Z
move-result v0
if-eqz v0, :show
return-void
""", ExternalLabel("show", getInstruction(0))
)
}
}
ShowDialogCommandFingerprint
.resultOrThrow()
.hookLithoFullscreenAds(context)

// endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ object AdsPatch : BaseResourcePatch(
"Top"
)

private const val FILTER_CLASS_DESCRIPTOR =
private const val ADS_FILTER_CLASS_DESCRIPTOR =
"$COMPONENTS_PATH/AdsFilter;"

private const val FULLSCREEN_ADS_FILTER_CLASS_DESCRIPTOR =
"${app.revanced.patches.shared.integrations.Constants.COMPONENTS_PATH}/FullscreenAdsFilter;"

override fun execute(context: ResourceContext) {
LithoFilterPatch.addFilter(FILTER_CLASS_DESCRIPTOR)
LithoFilterPatch.addFilter(ADS_FILTER_CLASS_DESCRIPTOR)
LithoFilterPatch.addFilter(FULLSCREEN_ADS_FILTER_CLASS_DESCRIPTOR)

context.forEach {

Expand Down
Loading

0 comments on commit c003ad9

Please sign in to comment.