Skip to content

Commit

Permalink
feat: Improve package change patch
Browse files Browse the repository at this point in the history
  • Loading branch information
1fexd committed Nov 7, 2024
1 parent 4b818e4 commit 3e02292
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
6 changes: 6 additions & 0 deletions patches/api/patches.api
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ public final class app/revanced/patches/all/misc/network/OverrideCertificatePinn

public final class app/revanced/patches/all/misc/packagename/ChangePackageNamePatchKt {
public static field packageNameOption Lapp/revanced/patcher/patch/Option;
public static field updatePermissions Lapp/revanced/patcher/patch/Option;
public static field updateProviders Lapp/revanced/patcher/patch/Option;
public static final fun getChangePackageNamePatch ()Lapp/revanced/patcher/patch/ResourcePatch;
public static final fun getPackageNameOption ()Lapp/revanced/patcher/patch/Option;
public static final fun getUpdatePermissions ()Lapp/revanced/patcher/patch/Option;
public static final fun getUpdateProviders ()Lapp/revanced/patcher/patch/Option;
public static final fun setOrGetFallbackPackageName (Ljava/lang/String;)Ljava/lang/String;
public static final fun setPackageNameOption (Lapp/revanced/patcher/patch/Option;)V
public static final fun setUpdatePermissions (Lapp/revanced/patcher/patch/Option;)V
public static final fun setUpdateProviders (Lapp/revanced/patcher/patch/Option;)V
}

public final class app/revanced/patches/all/misc/resources/AddResourcesPatchKt {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package app.revanced.patches.all.misc.packagename

import app.revanced.patcher.patch.Option
import app.revanced.patcher.patch.booleanOption
import app.revanced.patcher.patch.resourcePatch
import app.revanced.patcher.patch.stringOption
import app.revanced.util.asSequence
import org.w3c.dom.Element

lateinit var packageNameOption: Option<String>
lateinit var updatePermissions: Option<Boolean>
lateinit var updateProviders: Option<Boolean>

/**
* Set the package name to use.
Expand All @@ -25,6 +29,8 @@ fun setOrGetFallbackPackageName(fallbackPackageName: String): String {
}
}

private const val RECEIVER_NOT_EXPORTED = "DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"

val changePackageNamePatch = resourcePatch(
name = "Change package name",
description = "Appends \".revanced\" to the package name by default. Changing the package name of the app can lead to unexpected issues.",
Expand All @@ -41,20 +47,61 @@ val changePackageNamePatch = resourcePatch(
it == "Default" || it!!.matches(Regex("^[a-z]\\w*(\\.[a-z]\\w*)+\$"))
}

updatePermissions = booleanOption(
key = "updatePermissions",
default = false,
title = "Update permissions",
description = "Update compatibility receiver permissions.",
required = false
)

updateProviders = booleanOption(
key = "updateProviders",
default = false,
title = "Update providers",
description = "Update provider names declared by the app.",
required = false
)

finalize {
document("AndroidManifest.xml").use { document ->

val replacementPackageName = packageNameOption.value

val manifest = document.getElementsByTagName("manifest").item(0) as Element
manifest.setAttribute(
"package",
if (replacementPackageName != packageNameOption.default) {
replacementPackageName
} else {
"${manifest.getAttribute("package")}.revanced"
},
)
val packageName = manifest.getAttribute("package")
val newPackageName = if (replacementPackageName != packageNameOption.default) {
replacementPackageName!!
} else {
"${packageName}.revanced"
}

manifest.setAttribute("package", newPackageName)


if (updatePermissions.value == true) {
val permissions = manifest.getElementsByTagName("permission").asSequence()
val usesPermissions = manifest.getElementsByTagName("uses-permission").asSequence()

(permissions + usesPermissions)
.map { it as Element }
.filter { it.getAttribute("android:name") == "$packageName.$RECEIVER_NOT_EXPORTED" }
.forEach { it.setAttribute("android:name", "$newPackageName.$RECEIVER_NOT_EXPORTED") }
}

if(updateProviders.value == true){
val providers = manifest.getElementsByTagName("provider").asSequence()

for (node in providers) {
val provider = node as Element

val authorities = provider.getAttribute("android:authorities")
if (!authorities.startsWith("$packageName.")) continue

provider.setAttribute("android:authorities", authorities.replace(packageName, newPackageName))
}
}
}
}
}

0 comments on commit 3e02292

Please sign in to comment.