Skip to content

Commit 32042e0

Browse files
committed
Upload file
1 parent 3b77304 commit 32042e0

1 file changed

Lines changed: 54 additions & 15 deletions

File tree

common/src/main/java/com/omarea/common/shell/ShellTranslation.kt

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ class ShellTranslation(val context: Context) {
1919
private val resRegex =
2020
Regex("@(string|dimen)[:/][_a-z][_0-9a-z]*", RegexOption.IGNORE_CASE)
2121

22+
private fun getAmHelp(): String = """
23+
am:[command] syntax:
24+
25+
am:[start -a ACTION -d URI -n PACKAGE/CLASS]
26+
am:[startservice -n PACKAGE/CLASS]
27+
am:[broadcast -a ACTION]
28+
29+
Extras:
30+
--es key value String
31+
--ei key value Int
32+
--ez key value Boolean
33+
-f flags Intent flags (decimal or 0x)
34+
35+
Example:
36+
am:[start -a android.intent.action.VIEW -d https://google.com]
37+
""".trimIndent()
38+
2239
private val appResources: Resources by lazy {
2340
runCatching {
2441
val langFile = File(context.filesDir, "home/log/language")
@@ -258,15 +275,27 @@ class ShellTranslation(val context: Context) {
258275
val cur = StringBuilder()
259276
var inQuote = false
260277
var quoteChar = '\u0000'
278+
var i = 0
261279

262-
for (i in args.indices) {
280+
while (i < args.length) {
263281
val c = args[i]
264282

265283
if (inQuote) {
266-
if (c == quoteChar) {
267-
inQuote = false
268-
} else {
269-
cur.append(c)
284+
when {
285+
c == quoteChar -> {
286+
inQuote = false
287+
}
288+
c == '\\' && i + 1 < args.length -> {
289+
when (val n = args[++i]) {
290+
'n' -> cur.append('\n')
291+
't' -> cur.append('\t')
292+
'\\' -> cur.append('\\')
293+
'"' -> cur.append('"')
294+
'\'' -> cur.append('\'')
295+
else -> cur.append(n)
296+
}
297+
}
298+
else -> cur.append(c)
270299
}
271300
} else {
272301
when {
@@ -283,23 +312,33 @@ class ShellTranslation(val context: Context) {
283312
else -> cur.append(c)
284313
}
285314
}
315+
i++
316+
}
317+
318+
if (cur.isNotEmpty()) {
319+
out.add(cur.toString())
286320
}
287321

288-
if (cur.isNotEmpty()) out.add(cur.toString())
289322
return out
290323
}
291324

292-
fun resolveRow(originRow: String): String {
325+
fun resolveRow(originRow: String): String? {
326+
293327
val trimmed = originRow.trim()
294328
if (trimmed.startsWith("am:[", true) && trimmed.endsWith("]")) {
295329
val args = trimmed
296330
.removePrefix("am:[")
297331
.removeSuffix("]")
298332
.trim()
299333

300-
if (args.isNotEmpty()) onAm(args)
301-
return ""
334+
if (args.equals("help", true)) {
335+
return getAmHelp()
336+
} else if (args.isNotEmpty()) {
337+
onAm(args)
338+
}
339+
return null
302340
}
341+
303342
var result = originRow
304343
val res = appResources
305344
resRegex.findAll(originRow).forEach { match ->
@@ -325,13 +364,13 @@ class ShellTranslation(val context: Context) {
325364

326365
fun resolveRows(rows: List<String>): String {
327366
val builder = StringBuilder()
328-
var rowIndex = 0
367+
var first = true
368+
329369
for (row in rows) {
330-
if (rowIndex > 0) {
331-
builder.append("\n")
332-
}
333-
builder.append(resolveRow(row))
334-
rowIndex ++
370+
val resolved = resolveRow(row) ?: continue
371+
if (!first) builder.append('\n')
372+
builder.append(resolved)
373+
first = false
335374
}
336375
return builder.toString()
337376
}

0 commit comments

Comments
 (0)