Skip to content

Commit

Permalink
fix(Reddit - Hide ads): View all comments button hidden if comment …
Browse files Browse the repository at this point in the history
…ads are hidden
  • Loading branch information
inotia00 authored and anddea committed Feb 8, 2025
1 parent 2d30ae1 commit f73d19f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

import app.revanced.extension.reddit.settings.preference.ReVancedPreferenceFragment;

/**
* @noinspection ALL
*/
@SuppressWarnings("all")
public class ActivityHook {
public static void initialize(Activity activity) {
SettingsStatus.load();
Expand Down
97 changes: 34 additions & 63 deletions patches/src/main/kotlin/app/revanced/patches/reddit/ad/AdsPatch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,42 @@ import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWith
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
import app.revanced.patcher.patch.bytecodePatch
import app.revanced.patcher.patch.resourcePatch
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.reddit.utils.compatibility.Constants.COMPATIBLE_PACKAGE
import app.revanced.patches.reddit.utils.extension.Constants.PATCHES_PATH
import app.revanced.patches.reddit.utils.patch.PatchList.HIDE_ADS
import app.revanced.patches.reddit.utils.settings.settingsPatch
import app.revanced.patches.reddit.utils.settings.updatePatchStatus
import app.revanced.util.fingerprint.matchOrThrow
import app.revanced.util.findMutableMethodOf
import app.revanced.util.fingerprint.methodOrThrow
import app.revanced.util.getReference
import app.revanced.util.getWalkerMethod
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.indexOfFirstStringInstruction
import app.revanced.util.or
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.iface.Method
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.FieldReference

private const val RESOURCE_FILE_PATH = "res/layout/merge_listheader_link_detail.xml"

private val bannerAdsPatch = resourcePatch(
description = "bannerAdsPatch",
) {
execute {
document(RESOURCE_FILE_PATH).use { document ->
document.getElementsByTagName("merge").item(0).childNodes.apply {
val attributes = arrayOf("height", "width")

for (i in 1 until length) {
val view = item(i)
if (
view.hasAttributes() &&
view.attributes.getNamedItem("android:id").nodeValue.endsWith("ad_view_stub")
) {
attributes.forEach { attribute ->
view.attributes.getNamedItem("android:layout_$attribute").nodeValue =
"0.0dip"
}

break
}
}
}
}
}
}

private const val EXTENSION_METHOD_DESCRIPTOR =
"$PATCHES_PATH/GeneralAdsPatch;->hideCommentAds()Z"

private val commentAdsPatch = bytecodePatch(
description = "commentAdsPatch",
) {
execute {
commentAdsFingerprint.matchOrThrow().let {
val walkerMethod = it.getWalkerMethod(it.patternMatch!!.startIndex)
walkerMethod.apply {
addInstructionsWithLabels(
0, """
invoke-static {}, $EXTENSION_METHOD_DESCRIPTOR
move-result v0
if-eqz v0, :show
new-instance v0, Ljava/lang/Object;
invoke-direct {v0}, Ljava/lang/Object;-><init>()V
return-object v0
""", ExternalLabel("show", getInstruction(0))
)
}
}
}
}

private const val EXTENSION_CLASS_DESCRIPTOR =
"$PATCHES_PATH/GeneralAdsPatch;"

private val isCommentAdsMethod: Method.() -> Boolean = {
parameterTypes.size == 1 &&
parameterTypes.first().startsWith("Lcom/reddit/ads/conversation/") &&
accessFlags == AccessFlags.PUBLIC or AccessFlags.FINAL &&
returnType == "V" &&
indexOfFirstStringInstruction("ad") >= 0
}

@Suppress("unused")
val adsPatch = bytecodePatch(
HIDE_ADS.title,
HIDE_ADS.summary,
) {
compatibleWith(COMPATIBLE_PACKAGE)

dependsOn(
settingsPatch,
bannerAdsPatch,
commentAdsPatch,
)
dependsOn(settingsPatch)

execute {
// region Filter promoted ads (does not work in popular or latest feed)
Expand Down Expand Up @@ -127,6 +77,27 @@ val adsPatch = bytecodePatch(
)
}

// region Filter comment ads
classes.forEach { classDef ->
classDef.methods.forEach { method ->
if (method.isCommentAdsMethod()) {
proxy(classDef)
.mutableClass
.findMutableMethodOf(method)
.addInstructionsWithLabels(
0, """
invoke-static {}, $EXTENSION_CLASS_DESCRIPTOR->hideCommentAds()Z
move-result v0
if-eqz v0, :show
return-void
:show
nop
"""
)
}
}
}

updatePatchStatus(
"enableGeneralAds",
HIDE_ADS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ 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 val commentAdsFingerprint = legacyFingerprint(
name = "commentAdsFingerprint",
returnType = "L",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = listOf("L"),
opcodes = listOf(
Opcode.INVOKE_STATIC,
Opcode.MOVE_RESULT_OBJECT,
Opcode.RETURN_OBJECT
),
customFingerprint = { method, _ ->
method.definingClass.endsWith("/PostDetailPresenter\$loadAd\$1;") &&
method.name == "invokeSuspend"
},
)

internal val adPostFingerprint = legacyFingerprint(
name = "adPostFingerprint",
returnType = "V",
Expand Down

0 comments on commit f73d19f

Please sign in to comment.