diff --git a/patches/src/main/kotlin/app/revanced/patches/all/misc/packagename/ChangePackageNamePatch.kt b/patches/src/main/kotlin/app/revanced/patches/all/misc/packagename/ChangePackageNamePatch.kt index 4c0bd71b2..4a6443a8c 100644 --- a/patches/src/main/kotlin/app/revanced/patches/all/misc/packagename/ChangePackageNamePatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/all/misc/packagename/ChangePackageNamePatch.kt @@ -1,8 +1,8 @@ package app.revanced.patches.all.misc.packagename -import app.revanced.patcher.patch.Option -import app.revanced.patcher.patch.resourcePatch -import app.revanced.patcher.patch.stringOption +import app.revanced.patcher.patch.* +import app.revanced.util.asSequence +import app.revanced.util.getNode import org.w3c.dom.Element import java.util.logging.Logger @@ -28,7 +28,8 @@ fun setOrGetFallbackPackageName(fallbackPackageName: String): String { 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.", + description = "Appends \".revanced\" to the package name by default. " + + "Changing the package name of the app can lead to unexpected issues.", use = false, ) { packageNameOption = stringOption( @@ -42,40 +43,81 @@ val changePackageNamePatch = resourcePatch( it == "Default" || it!!.matches(Regex("^[a-z]\\w*(\\.[a-z]\\w*)+\$")) } - /** - * Apps that are confirmed to not work correctly with this patch. - * This is not an exhaustive list, and is only the apps with - * ReVanced specific patches and are confirmed incompatible with this patch. - */ - val incompatibleAppPackages = setOf( - // Cannot login, settings menu is broken. - "com.reddit.frontpage", + val updatePermissions by booleanOption( + key = "updatePermissions", + default = false, + title = "Update permissions", + description = "Update compatibility receiver permissions. " + + "Enabling this can fix installation errors, but this can also break features in certain apps.", + ) - // Patches and installs but crashes on launch. - "com.duolingo", - "com.twitter.android", - "tv.twitch.android.app", + val updateProviders by booleanOption( + key = "updateProviders", + default = false, + title = "Update providers", + description = "Update provider names declared by the app. " + + "Enabling this can fix installation errors, but this can also break features in certain apps.", ) finalize { - document("AndroidManifest.xml").use { document -> - val manifest = document.getElementsByTagName("manifest").item(0) as Element - val originalPackageName = manifest.getAttribute("package") + /** + * Apps that are confirmed to not work correctly with this patch. + * This is not an exhaustive list, and is only the apps with + * ReVanced specific patches and are confirmed incompatible with this patch. + */ + val incompatibleAppPackages = setOf( + // Cannot log in, settings menu is broken. + "com.reddit.frontpage", - if (incompatibleAppPackages.contains(originalPackageName)) { + // Patches and installs but crashes on launch. + "com.duolingo", + "com.twitter.android", + "tv.twitch.android.app", + ) + + document("AndroidManifest.xml").use { document -> + val manifest = document.getNode("manifest") as Element + val packageName = manifest.getAttribute("package") + + if (incompatibleAppPackages.contains(packageName)) { return@finalize Logger.getLogger(this::class.java.name).severe( - "'$originalPackageName' does not work correctly with \"Change package name\"") + "'$packageName' does not work correctly with \"Change package name\"", + ) } val replacementPackageName = packageNameOption.value - manifest.setAttribute( - "package", - if (replacementPackageName != packageNameOption.default) { - replacementPackageName - } else { - "${originalPackageName}.revanced" - }, - ) + val newPackageName = if (replacementPackageName != packageNameOption.default) { + replacementPackageName!! + } else { + "$packageName.revanced" + } + + manifest.setAttribute("package", newPackageName) + + if (updatePermissions == true) { + val permissions = manifest.getElementsByTagName("permission").asSequence() + val usesPermissions = manifest.getElementsByTagName("uses-permission").asSequence() + + val receiverNotExported = "DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION" + + (permissions + usesPermissions) + .map { it as Element } + .filter { it.getAttribute("android:name") == "$packageName.$receiverNotExported" } + .forEach { it.setAttribute("android:name", "$newPackageName.$receiverNotExported") } + } + + if (updateProviders == 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)) + } + } } } }