Skip to content

Commit 7e61023

Browse files
authored
perf(Bring back twitter): Optimize string replacement (#440)
* perf(Bring back twitter): Optimize string replacement * Don't create new xml or append string when it is missing * Minor fix * Move pt_rBR to custom implementations to add strings * Beautify log * Remove the missing strings log When user removed some languages from split apk and SettingsPatch was executed prior to this patch, this log always show warnings because SettingsPatch adds other languages. * improve comment sentence
1 parent 4d59c41 commit 7e61023

File tree

3 files changed

+60
-39
lines changed

3 files changed

+60
-39
lines changed

src/main/kotlin/crimera/patches/twitter/misc/bringbacktwitter/BringBackTwitterResourcePatch.kt

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ import app.revanced.patcher.patch.annotation.Patch
77
import app.revanced.util.ResourceGroup
88
import app.revanced.util.asSequence
99
import app.revanced.util.copyResources
10-
import app.util.measureExecutionTime
1110
import crimera.patches.twitter.misc.bringbacktwitter.custromstringsupdater.ja
11+
import crimera.patches.twitter.misc.bringbacktwitter.custromstringsupdater.pt_rBR
1212
import crimera.patches.twitter.misc.bringbacktwitter.strings.StringsMap
1313
import org.w3c.dom.Element
1414
import java.io.File
15-
import java.io.FileWriter
1615

1716
@Patch(
1817
name = "Bring back twitter",
@@ -97,20 +96,16 @@ object BringBackTwitterResourcePatch : ResourcePatch() {
9796
// update strings to old ones
9897
updateStrings(context)
9998
ja.updateStrings(context)
99+
pt_rBR.updateStrings(context)
100100
}
101101

102102
private fun updateStrings(context: ResourceContext) {
103103
val langs = StringsMap.replacementMap
104104
for ((key, value) in langs) {
105105
val stringsFile = context["res/$key/strings.xml"]
106-
if (!stringsFile.isFile) {
107-
108-
context["res/$key"].mkdirs()
109-
FileWriter(stringsFile).use {
110-
it.write("<?xml version=\"1.0\" encoding=\"utf-8\"?><resources></resources>")
111-
}
106+
if (stringsFile.exists()) {
107+
updateStringsFile(stringsFile, value, context)
112108
}
113-
updateStringsFile(stringsFile, value, context)
114109
}
115110
}
116111

@@ -121,39 +116,27 @@ object BringBackTwitterResourcePatch : ResourcePatch() {
121116
context.xmlEditor[stringsFile.toString()].use { editor ->
122117
val document = editor.file
123118

124-
for ((key, value) in stringsMap) {
125-
val nodes = document.getElementsByTagName("string")
126-
var keyReplaced = false
127-
for (i in 0 until nodes.length) {
128-
val node = nodes.item(i)
129-
val name = node.attributes.getNamedItem("name")?.nodeValue
130-
if (name == key) {
131-
node.textContent = value
132-
keyReplaced = true
133-
break
134-
} else if (name == "conference_default_title") {/*
135-
* Parsing causes the default value which contains the
136-
* character 𝕏 to be "corrupted" so we change it to a normal X
137-
*/
119+
val nodes = document.getElementsByTagName("string")
120+
for (i in 0 until nodes.length) {
121+
val node = nodes.item(i)
122+
val name = node.attributes.getNamedItem("name")?.nodeValue
123+
124+
if (name == "conference_default_title") {
125+
/*
126+
* Parsing XML causes the string which contains the
127+
* character "𝕏" to be corrupted, so we change it to "Twitter"
128+
*/
129+
node.textContent = stringsMap[name] ?: run {
138130
val content = node.textContent
139-
node.textContent = stringsMap[name] ?: run {
140-
val delimiter = if (content.contains("-")) '-' else ' '
141-
content.split(delimiter).joinToString(delimiter.toString()) {
142-
if (it.startsWithSpecialByte()) "Twitter" else it
143-
}
131+
val delimiter = if (content.contains("-")) '-' else ' '
132+
content.split(delimiter).joinToString(delimiter.toString()) {
133+
if (it.startsWithSpecialByte()) "Twitter" else it
144134
}
145135
}
136+
continue
146137
}
147138

148-
// log which keys were not found or failed
149-
if (!keyReplaced) {
150-
val colorElement = document.createElement("string")
151-
152-
colorElement.setAttribute("name", key)
153-
colorElement.textContent = value
154-
155-
document.getElementsByTagName("resources").item(0).appendChild(colorElement)
156-
}
139+
node.textContent = stringsMap[name] ?: continue
157140
}
158141
}
159142
}

src/main/kotlin/crimera/patches/twitter/misc/bringbacktwitter/strings/pt_rBR.kt renamed to src/main/kotlin/crimera/patches/twitter/misc/bringbacktwitter/custromstringsupdater/pt_rBR.kt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
package crimera.patches.twitter.misc.bringbacktwitter.strings
1+
package crimera.patches.twitter.misc.bringbacktwitter.custromstringsupdater
2+
3+
import app.revanced.patcher.data.ResourceContext
4+
import java.io.FileWriter
25

36
object pt_rBR {
47
val values = mapOf(
@@ -63,4 +66,40 @@ object pt_rBR {
6366
"ps__retweeted_on_twitter" to "*%s* retweetou em",
6467
"tweet_removed_from_your_bookmarks" to "Tweet removido dos seus Itens salvos"
6568
)
69+
70+
/*
71+
* The official X app doesn't have Brazilian Portuguese string resources.
72+
* So, add the strings instead of trying to replace them.
73+
*/
74+
fun updateStrings(context: ResourceContext) {
75+
if (!context["res/values-pt"].exists()) {
76+
// User likely intentionally excluded the Portuguese split when merging split APKs.
77+
// In this case, there is no need to add the pt-rBR strings.
78+
return
79+
}
80+
81+
val stringsFile = context["res/values-pt-rBR/strings.xml"]
82+
83+
// Note: If SettingsPatch was executed prior to this patch, stringsFile will exist.
84+
if (!stringsFile.exists()) {
85+
context["res/values-pt-rBR"].mkdirs()
86+
FileWriter(stringsFile).use {
87+
it.write("<?xml version=\"1.0\" encoding=\"utf-8\"?><resources></resources>")
88+
}
89+
}
90+
91+
// Append strings from the map.
92+
context.xmlEditor[stringsFile.toString()].use {
93+
val document = it.file
94+
val parentNode = document.getElementsByTagName("resources").item(0)
95+
96+
for ((key, value) in values) {
97+
val newElement = document.createElement("string")
98+
newElement.setAttribute("name", key)
99+
newElement.textContent = value
100+
101+
parentNode.appendChild(newElement)
102+
}
103+
}
104+
}
66105
}

src/main/kotlin/crimera/patches/twitter/misc/bringbacktwitter/strings/StringsMap.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ object StringsMap {
66
"values-en-rGB" to en_rGB.values,
77
"values-ru" to ru.values,
88
"values-hi" to hi.values,
9-
"values-pt-rBR" to pt_rBR.values,
109
"values-tr" to tr.values,
1110
"values-zh-rCN" to zh_rCN.values,
1211
"values-zh-rTW" to zh_rTW.values,

0 commit comments

Comments
 (0)