1
1
package app.revanced.patches.all.misc.packagename
2
2
3
- import app.revanced.patcher.patch.Option
4
- import app.revanced.patcher.patch.resourcePatch
5
- import app.revanced.patcher.patch.stringOption
3
+ import app.revanced.patcher.patch.*
4
+ import app.revanced.util.asSequence
5
+ import app.revanced.util.getNode
6
6
import org.w3c.dom.Element
7
7
import java.util.logging.Logger
8
8
@@ -28,7 +28,8 @@ fun setOrGetFallbackPackageName(fallbackPackageName: String): String {
28
28
29
29
val changePackageNamePatch = resourcePatch(
30
30
name = " Change package name" ,
31
- description = " Appends \" .revanced\" to the package name by default. Changing the package name of the app can lead to unexpected issues." ,
31
+ description = " Appends \" .revanced\" to the package name by default. " +
32
+ " Changing the package name of the app can lead to unexpected issues." ,
32
33
use = false ,
33
34
) {
34
35
packageNameOption = stringOption(
@@ -42,40 +43,81 @@ val changePackageNamePatch = resourcePatch(
42
43
it == " Default" || it!! .matches(Regex (" ^[a-z]\\ w*(\\ .[a-z]\\ w*)+\$ " ))
43
44
}
44
45
45
- /* *
46
- * Apps that are confirmed to not work correctly with this patch.
47
- * This is not an exhaustive list, and is only the apps with
48
- * ReVanced specific patches and are confirmed incompatible with this patch.
49
- */
50
- val incompatibleAppPackages = setOf (
51
- // Cannot login, settings menu is broken.
52
- " com.reddit.frontpage" ,
53
-
54
- // Patches and installs but crashes on launch.
55
- " com.duolingo" ,
56
- " com.twitter.android" ,
57
- " tv.twitch.android.app" ,
46
+ val updatePermissions by booleanOption(
47
+ key = " updatePermissions" ,
48
+ default = false ,
49
+ title = " Update permissions" ,
50
+ description = " Update compatibility receiver permissions. " +
51
+ " Enabling this can fix installation errors, but this can also break features in certain apps." ,
52
+ )
53
+
54
+ val updateProviders by booleanOption(
55
+ key = " updateProviders" ,
56
+ default = false ,
57
+ title = " Update providers" ,
58
+ description = " Update provider names declared by the app. " +
59
+ " Enabling this can fix installation errors, but this can also break features in certain apps." ,
58
60
)
59
61
60
62
finalize {
63
+ /* *
64
+ * Apps that are confirmed to not work correctly with this patch.
65
+ * This is not an exhaustive list, and is only the apps with
66
+ * ReVanced specific patches and are confirmed incompatible with this patch.
67
+ */
68
+ val incompatibleAppPackages = setOf (
69
+ // Cannot log in, settings menu is broken.
70
+ " com.reddit.frontpage" ,
71
+
72
+ // Patches and installs but crashes on launch.
73
+ " com.duolingo" ,
74
+ " com.twitter.android" ,
75
+ " tv.twitch.android.app" ,
76
+ )
77
+
61
78
document(" AndroidManifest.xml" ).use { document ->
62
- val manifest = document.getElementsByTagName (" manifest" ).item( 0 ) as Element
63
- val originalPackageName = manifest.getAttribute(" package" )
79
+ val manifest = document.getNode (" manifest" ) as Element
80
+ val packageName = manifest.getAttribute(" package" )
64
81
65
- if (incompatibleAppPackages.contains(originalPackageName )) {
82
+ if (incompatibleAppPackages.contains(packageName )) {
66
83
return @finalize Logger .getLogger(this ::class .java.name).severe(
67
- " '$originalPackageName ' does not work correctly with \" Change package name\" " )
84
+ " '$packageName ' does not work correctly with \" Change package name\" " ,
85
+ )
68
86
}
69
87
70
88
val replacementPackageName = packageNameOption.value
71
- manifest.setAttribute(
72
- " package" ,
73
- if (replacementPackageName != packageNameOption.default) {
74
- replacementPackageName
75
- } else {
76
- " ${originalPackageName} .revanced"
77
- },
78
- )
89
+ val newPackageName = if (replacementPackageName != packageNameOption.default) {
90
+ replacementPackageName!!
91
+ } else {
92
+ " $packageName .revanced"
93
+ }
94
+
95
+ manifest.setAttribute(" package" , newPackageName)
96
+
97
+ if (updatePermissions == true ) {
98
+ val permissions = manifest.getElementsByTagName(" permission" ).asSequence()
99
+ val usesPermissions = manifest.getElementsByTagName(" uses-permission" ).asSequence()
100
+
101
+ val receiverNotExported = " DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
102
+
103
+ (permissions + usesPermissions)
104
+ .map { it as Element }
105
+ .filter { it.getAttribute(" android:name" ) == " $packageName .$receiverNotExported " }
106
+ .forEach { it.setAttribute(" android:name" , " $newPackageName .$receiverNotExported " ) }
107
+ }
108
+
109
+ if (updateProviders == true ) {
110
+ val providers = manifest.getElementsByTagName(" provider" ).asSequence()
111
+
112
+ for (node in providers) {
113
+ val provider = node as Element
114
+
115
+ val authorities = provider.getAttribute(" android:authorities" )
116
+ if (! authorities.startsWith(" $packageName ." )) continue
117
+
118
+ provider.setAttribute(" android:authorities" , authorities.replace(packageName, newPackageName))
119
+ }
120
+ }
79
121
}
80
122
}
81
123
}
0 commit comments