diff --git a/src/main/kotlin/app/revanced/patches/reddit/misc/tracking/url/patch/SanitizeUrlQueryPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/misc/tracking/url/patch/SanitizeUrlQueryPatch.kt index ad613e89f..1c3a23f5e 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/misc/tracking/url/patch/SanitizeUrlQueryPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/misc/tracking/url/patch/SanitizeUrlQueryPatch.kt @@ -10,18 +10,20 @@ import app.revanced.patcher.extensions.InstructionExtensions.getInstruction 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.patch.annotations.RequiresIntegrations import app.revanced.patches.reddit.misc.tracking.url.fingerprints.ShareLinkFactoryFingerprint import app.revanced.patches.reddit.utils.annotations.RedditCompatibility +import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsPatch +import app.revanced.patches.reddit.utils.settings.bytecode.patch.SettingsPatch.Companion.updateSettingsStatus import org.jf.dexlib2.iface.instruction.OneRegisterInstruction @Patch @Name("sanitize-sharing-links") @Description("Removes (tracking) query parameters from the URLs when sharing links.") +@DependsOn([SettingsPatch::class]) @RedditCompatibility @Version("0.0.1") -@RequiresIntegrations class SanitizeUrlQueryPatch : BytecodePatch( listOf(ShareLinkFactoryFingerprint) ) { @@ -41,6 +43,8 @@ class SanitizeUrlQueryPatch : BytecodePatch( } } ?: return ShareLinkFactoryFingerprint.toErrorResult() + updateSettingsStatus("SanitizeUrlQuery") + return PatchResultSuccess() } diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/integrations/fingerprints/InitFingerprint.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/integrations/fingerprints/InitFingerprint.kt new file mode 100644 index 000000000..860b6b026 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/integrations/fingerprints/InitFingerprint.kt @@ -0,0 +1,10 @@ +package app.revanced.patches.reddit.utils.integrations.fingerprints + +import app.revanced.patches.shared.patch.integrations.AbstractIntegrationsPatch.IntegrationsFingerprint + +object InitFingerprint : IntegrationsFingerprint( + customFingerprint = { methodDef, _ -> + methodDef.definingClass.endsWith("/FrontpageApplication;") && + methodDef.name == "onCreate" + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/integrations/patch/IntegrationsPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/integrations/patch/IntegrationsPatch.kt new file mode 100644 index 000000000..0715e2919 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/integrations/patch/IntegrationsPatch.kt @@ -0,0 +1,15 @@ +package app.revanced.patches.reddit.utils.integrations.patch + +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.patch.annotations.RequiresIntegrations +import app.revanced.patches.reddit.utils.annotations.RedditCompatibility +import app.revanced.patches.reddit.utils.integrations.fingerprints.InitFingerprint +import app.revanced.patches.shared.patch.integrations.AbstractIntegrationsPatch + +@Name("integrations") +@RedditCompatibility +@RequiresIntegrations +class IntegrationsPatch : AbstractIntegrationsPatch( + "Lapp/revanced/reddit/utils/ReVancedUtils;", + listOf(InitFingerprint), +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/fingerprints/OssLicensesMenuActivityOnCreateFingerprint.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/fingerprints/OssLicensesMenuActivityOnCreateFingerprint.kt new file mode 100644 index 000000000..9bbbb300f --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/fingerprints/OssLicensesMenuActivityOnCreateFingerprint.kt @@ -0,0 +1,17 @@ +package app.revanced.patches.reddit.utils.settings.bytecode.fingerprints + +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import org.jf.dexlib2.Opcode + +object OssLicensesMenuActivityOnCreateFingerprint : MethodFingerprint( + returnType = "V", + opcodes = listOf( + Opcode.IGET_BOOLEAN, + Opcode.IF_EQZ, + Opcode.INVOKE_STATIC + ), + customFingerprint = { methodDef, _ -> + methodDef.definingClass.endsWith("/OssLicensesMenuActivity;") && + methodDef.name == "onCreate" + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/fingerprints/SettingsStatusLoadFingerprint.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/fingerprints/SettingsStatusLoadFingerprint.kt new file mode 100644 index 000000000..b0d2b2249 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/fingerprints/SettingsStatusLoadFingerprint.kt @@ -0,0 +1,10 @@ +package app.revanced.patches.reddit.utils.settings.bytecode.fingerprints + +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint + +object SettingsStatusLoadFingerprint : MethodFingerprint( + customFingerprint = { methodDef, _ -> + methodDef.definingClass.endsWith("Lapp/revanced/reddit/settingsmenu/SettingsStatus;") && + methodDef.name == "load" + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsPatch.kt new file mode 100644 index 000000000..439c623ef --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/bytecode/patch/SettingsPatch.kt @@ -0,0 +1,75 @@ +package app.revanced.patches.reddit.utils.settings.bytecode.patch + +import app.revanced.extensions.toErrorResult +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.extensions.InstructionExtensions.addInstruction +import app.revanced.patcher.extensions.InstructionExtensions.addInstructions +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.reddit.utils.annotations.RedditCompatibility +import app.revanced.patches.reddit.utils.integrations.patch.IntegrationsPatch +import app.revanced.patches.reddit.utils.settings.bytecode.fingerprints.OssLicensesMenuActivityOnCreateFingerprint +import app.revanced.patches.reddit.utils.settings.bytecode.fingerprints.SettingsStatusLoadFingerprint +import app.revanced.patches.reddit.utils.settings.resource.patch.SettingsResourcePatch + +@Patch +@Name("settings") +@Description("Adds ReVanced settings to Reddit.") +@DependsOn( + [ + IntegrationsPatch::class, + SettingsResourcePatch::class + ] +) +@RedditCompatibility +@Version("0.0.1") +class SettingsPatch : BytecodePatch( + listOf( + OssLicensesMenuActivityOnCreateFingerprint, + SettingsStatusLoadFingerprint + ) +) { + override fun execute(context: BytecodeContext): PatchResult { + + OssLicensesMenuActivityOnCreateFingerprint.result?.let { + it.mutableMethod.apply { + val insertIndex = it.scanResult.patternScanResult!!.startIndex + 1 + + addInstructions( + insertIndex, """ + invoke-static {p0}, $INTEGRATIONS_METHOD_DESCRIPTOR + return-void + """ + ) + } + } ?: return OssLicensesMenuActivityOnCreateFingerprint.toErrorResult() + + targetMethod = SettingsStatusLoadFingerprint.result?.mutableMethod + ?: return SettingsStatusLoadFingerprint.toErrorResult() + + return PatchResultSuccess() + } + + internal companion object { + private const val INTEGRATIONS_METHOD_DESCRIPTOR = + "Lapp/revanced/reddit/settingsmenu/ReVancedSettingActivity;->initializeSettings(Landroid/app/Activity;)V" + + private lateinit var targetMethod: MutableMethod + + fun updateSettingsStatus(description: String) { + targetMethod.apply { + addInstruction( + 0, + "invoke-static {}, Lapp/revanced/reddit/settingsmenu/SettingsStatus;->$description()V" + ) + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsResourcePatch.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsResourcePatch.kt new file mode 100644 index 000000000..577675dc6 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/resource/patch/SettingsResourcePatch.kt @@ -0,0 +1,72 @@ +package app.revanced.patches.reddit.utils.settings.resource.patch + +import app.revanced.extensions.doRecursively +import app.revanced.extensions.startsWithAny +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.ResourceContext +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patches.shared.patch.settings.AbstractSettingsResourcePatch +import org.w3c.dom.Element +import kotlin.io.path.exists + +@Name("settings-resource-patch") +@Version("0.0.1") +class SettingsResourcePatch : AbstractSettingsResourcePatch( + "reddit/settings", + "reddit/settings/host", + false +) { + override fun execute(context: ResourceContext): PatchResult { + super.execute(context) + + fun setIcon(targetXML: String) { + context.xmlEditor["res/xml/$targetXML.xml"].use { editor -> + editor.file.doRecursively loop@{ + if (it !is Element) return@loop + + it.getAttributeNode("android:key")?.let { attribute -> + if (attribute.textContent.endsWith("key_pref_acknowledgements")) { + it.getAttributeNode("android:icon").textContent = + "@drawable/icon_beta_planet" + } + } + } + } + } + + arrayOf("preferences", "preferences_logged_in").forEach { targetXML -> + val resDirectory = context["res"] + val targetXml = resDirectory.resolve("xml").resolve("$targetXML.xml").toPath() + + if (targetXml.exists()) + setIcon(targetXML) + } + + // App name + val resourceFileNames = arrayOf("strings.xml") + + context.forEach { + if (!it.name.startsWithAny(*resourceFileNames)) return@forEach + + // for each file in the "layouts" directory replace all necessary attributes content + context.xmlEditor[it.absolutePath].use { editor -> + val resourcesNode = editor.file.getElementsByTagName("resources").item(0) as Element + + for (i in 0 until resourcesNode.childNodes.length) { + val node = resourcesNode.childNodes.item(i) + if (node !is Element) continue + + val element = resourcesNode.childNodes.item(i) as Element + element.textContent = when (element.getAttribute("name")) { + "label_acknowledgements" -> "@string/revanced_extended_settings_title" + else -> continue + } + } + } + } + + return PatchResultSuccess() + } +} \ No newline at end of file diff --git a/src/main/resources/reddit/settings/host/values/strings.xml b/src/main/resources/reddit/settings/host/values/strings.xml new file mode 100644 index 000000000..726a9c55b --- /dev/null +++ b/src/main/resources/reddit/settings/host/values/strings.xml @@ -0,0 +1,6 @@ + + + Removes (tracking) query parameters from the URLs when sharing links + Sanitize sharing links + ReVanced Extended +