Skip to content

Commit

Permalink
feat(Hide ads): remove Close fullscreen ads setting inotia00/ReVanc…
Browse files Browse the repository at this point in the history
  • Loading branch information
inotia00 committed Sep 15, 2024
1 parent bd6690c commit 2d6142b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ object AdsPatch : BaseBytecodePatch(
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;"

private const val PREMIUM_PROMOTION_POP_UP_CLASS_DESCRIPTOR =
"$ADS_PATH/PremiumPromotionPatch;"

Expand All @@ -82,7 +79,7 @@ object AdsPatch : BaseBytecodePatch(
// litho view, used in 'ShowDialogCommandOuterClass' in innertube
ShowDialogCommandFingerprint
.resultOrThrow()
.hookLithoFullscreenAds(context)
.hookLithoFullscreenAds()

// endregion

Expand Down Expand Up @@ -174,19 +171,12 @@ 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
72 changes: 26 additions & 46 deletions src/main/kotlin/app/revanced/patches/shared/ads/BaseAdsPatch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ 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.getTargetIndexOrThrow
import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow
import app.revanced.util.getReference
import app.revanced.util.getWalkerMethod
import app.revanced.util.getWideLiteralInstructionIndex
import app.revanced.util.indexOfFirstInstructionOrThrow
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
Expand Down Expand Up @@ -82,60 +80,42 @@ abstract class BaseAdsPatch(
}
}

internal fun MethodFingerprintResult.hookLithoFullscreenAds(context: BytecodeContext) {
internal fun MethodFingerprintResult.hookLithoFullscreenAds() {
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,
var prependInstructions = """
move-object/from16 v0, p1
move-object/from16 v1, p2
"""
move-object/from16 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
if (parameterTypes.firstOrNull() != "[B") {
val toByteArrayReference = getInstruction<ReferenceInstruction>(
indexOfFirstInstructionOrThrow {
getReference<MethodReference>()?.name == "toByteArray"
}
).reference

// 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 = getTargetIndexWithMethodReferenceNameOrThrow("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 = getTargetIndexOrThrow(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
prependInstructions += """
invoke-virtual {v0}, $toByteArrayReference
move-result-object v0
"""
}

// Disable fullscreen ads
addInstructionsWithLabels(
0, prependInstructions + """
check-cast v1, ${dialogCodeField.definingClass}
iget v1, v1, $dialogCodeField
invoke-static {v0, v1}, $INTEGRATIONS_CLASS_DESCRIPTOR->disableFullscreenAds([BI)Z
move-result v1
if-eqz v1, :show
return-void
""", ExternalLabel("show", getInstruction(0))
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object AdsBytecodePatch : BytecodePatch(
// litho view, used in 'ShowDialogCommandOuterClass' in innertube
ShowDialogCommandFingerprint
.resultOrThrow()
.hookLithoFullscreenAds(context)
.hookLithoFullscreenAds()

// endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ object AdsPatch : BaseResourcePatch(
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(ADS_FILTER_CLASS_DESCRIPTOR)
LithoFilterPatch.addFilter(FULLSCREEN_ADS_FILTER_CLASS_DESCRIPTOR)

context.forEach {

Expand Down
5 changes: 0 additions & 5 deletions src/main/resources/music/settings/host/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ Please download %2$s from the website."</string>

<string name="revanced_hide_fullscreen_ads_title">Hide fullscreen ads</string>
<string name="revanced_hide_fullscreen_ads_summary">Hides fullscreen ads.</string>
<string name="revanced_hide_fullscreen_ads_type_title">Close fullscreen ads</string>
<string name="revanced_hide_fullscreen_ads_type_summary">"If it is enabled, fullscreen ads are closed through the Close button.
If it is disabled, fullscreen ads are blocked. (there may be side effects)"</string>
<string name="revanced_hide_fullscreen_ads_blocked_success">Fullscreen ads have been blocked. (DialogType: %s)</string>
<string name="revanced_hide_fullscreen_ads_closed_success">Fullscreen ads have been closed.</string>
<string name="revanced_hide_general_ads_title">Hide general ads</string>
<string name="revanced_hide_general_ads_summary">Hides general ads.</string>
<string name="revanced_hide_music_ads_title">Hide media ads</string>
Expand Down
7 changes: 0 additions & 7 deletions src/main/resources/youtube/settings/host/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ Please download %2$s from the website."</string>
<string name="revanced_hide_fullscreen_ads_title">Hide fullscreen ads</string>
<string name="revanced_hide_fullscreen_ads_summary_on">Fullscreen ads are hidden.</string>
<string name="revanced_hide_fullscreen_ads_summary_off">Fullscreen ads are shown.</string>
<string name="revanced_hide_fullscreen_ads_type_title">Close fullscreen ads</string>
<string name="revanced_hide_fullscreen_ads_type_summary_on">Fullscreen ads are closed through the Close button.</string>
<string name="revanced_hide_fullscreen_ads_type_summary_off">"Fullscreen ads are blocked.

Side effect: Community post images may be blocked in fullscreen."</string>
<string name="revanced_hide_fullscreen_ads_blocked_success">Fullscreen ads have been blocked. (DialogType: %s)</string>
<string name="revanced_hide_fullscreen_ads_closed_success">Fullscreen ads have been closed.</string>
<string name="revanced_hide_general_ads_title">Hide general ads</string>
<string name="revanced_hide_general_ads_summary_on">General ads are hidden.</string>
<string name="revanced_hide_general_ads_summary_off">General ads are shown.</string>
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/youtube/settings/xml/revanced_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<!-- PREFERENCE_SCREEN: ADS
<PreferenceScreen android:title="@string/revanced_preference_screen_ads_title" android:key="revanced_preference_screen_ads">
<SwitchPreference android:title="@string/revanced_hide_fullscreen_ads_title" android:key="revanced_hide_fullscreen_ads" android:summaryOn="@string/revanced_hide_fullscreen_ads_summary_on" android:summaryOff="@string/revanced_hide_fullscreen_ads_summary_off" />
<SwitchPreference android:title="@string/revanced_hide_fullscreen_ads_type_title" android:key="revanced_hide_fullscreen_ads_type" android:summaryOn="@string/revanced_hide_fullscreen_ads_type_summary_on" android:summaryOff="@string/revanced_hide_fullscreen_ads_type_summary_off" android:dependency="revanced_hide_fullscreen_ads" />
<SwitchPreference android:title="@string/revanced_hide_general_ads_title" android:key="revanced_hide_general_ads" android:summaryOn="@string/revanced_hide_general_ads_summary_on" android:summaryOff="@string/revanced_hide_general_ads_summary_off" />
<SwitchPreference android:title="@string/revanced_hide_merchandise_shelf_title" android:key="revanced_hide_merchandise_shelf" android:summaryOn="@string/revanced_hide_merchandise_shelf_summary_on" android:summaryOff="@string/revanced_hide_merchandise_shelf_summary_off" />
<SwitchPreference android:title="@string/revanced_hide_paid_promotion_label_title" android:key="revanced_hide_paid_promotion_label" android:summaryOn="@string/revanced_hide_paid_promotion_label_summary_on" android:summaryOff="@string/revanced_hide_paid_promotion_label_summary_off" />
Expand Down

0 comments on commit 2d6142b

Please sign in to comment.