Skip to content

Commit 79f9fb1

Browse files
committed
refactor: use native apis to build intent and start activity
1 parent 95272b0 commit 79f9fb1

File tree

4 files changed

+54
-45
lines changed

4 files changed

+54
-45
lines changed

app/src/main/java/com/osfans/trime/data/AppPrefs.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ class AppPrefs(
202202
private set
203203

204204
enum class SplitOption {
205-
AUTO,
206-
LANDSCAPE,
207205
NEVER,
206+
LANDSCAPE,
207+
AUTO,
208208
ALWAYS,
209209
}
210210

app/src/main/java/com/osfans/trime/ime/core/TrimeInputMethodService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import com.osfans.trime.ime.symbol.TabManager
5757
import com.osfans.trime.ime.text.Candidate
5858
import com.osfans.trime.ime.text.TextInputManager
5959
import com.osfans.trime.util.ShortcutUtils
60+
import com.osfans.trime.util.ShortcutUtils.openCategory
6061
import com.osfans.trime.util.StringUtils
6162
import com.osfans.trime.util.WeakHashSet
6263
import com.osfans.trime.util.isNightMode
@@ -944,7 +945,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
944945
// 处理返回键(隐藏软键盘)和回车键(换行)
945946
// todo 确认是否有必要单独处理回车键?是否需要把back和escape全部占用?
946947
Timber.d("\t<TrimeInput>\thandleKey()\tEnterOrHide, keycode=%d", keyEventCode)
947-
} else if (ShortcutUtils.openCategory(keyEventCode)) {
948+
} else if (openCategory(keyEventCode)) {
948949
// 打开系统默认应用
949950
Timber.d("\t<TrimeInput>\thandleKey()\topenCategory keycode=%d", keyEventCode)
950951
} else {

app/src/main/java/com/osfans/trime/util/ShortcutUtils.kt

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import android.os.Build
1616
import android.text.TextUtils
1717
import android.util.SparseArray
1818
import android.view.KeyEvent
19-
import com.blankj.utilcode.util.ActivityUtils
20-
import com.blankj.utilcode.util.IntentUtils
2119
import com.osfans.trime.core.Rime
2220
import com.osfans.trime.daemon.RimeDaemon
2321
import com.osfans.trime.data.AppPrefs
@@ -50,62 +48,68 @@ object ShortcutUtils {
5048
"clipboard" -> return pasteFromClipboard(context)
5149
"date" -> return getDate(option)
5250
"commit" -> return option
53-
"run" -> startIntent(option)
51+
"run" -> context.startIntent(option)
5452
"share_text" -> TrimeInputMethodService.getService().shareText()
5553
"liquid_keyboard" -> TrimeInputMethodService.getService().selectLiquidKeyboard(option)
56-
else -> startIntent(command, option)
54+
else -> context.startIntent(command, option)
5755
}
5856
return null
5957
}
6058

61-
private fun startIntent(arg: String) {
62-
val intent =
63-
when {
64-
arg.indexOf(':') >= 0 -> {
65-
Intent.parseUri(arg, Intent.URI_INTENT_SCHEME)
66-
}
67-
arg.indexOf('/') >= 0 -> {
68-
Intent(Intent.ACTION_MAIN).apply {
69-
addCategory(Intent.CATEGORY_LAUNCHER)
70-
component = ComponentName.unflattenFromString(arg)
71-
}
59+
private fun Context.startIntent(arg: String) {
60+
when {
61+
arg.contains(':') -> { // URI
62+
Intent.parseUri(arg, Intent.URI_INTENT_SCHEME)
63+
}
64+
arg.contains('/') -> { // Component name
65+
Intent(Intent.ACTION_MAIN).apply {
66+
addCategory(Intent.CATEGORY_LAUNCHER)
67+
component = ComponentName.unflattenFromString(arg)
7268
}
73-
else -> IntentUtils.getLaunchAppIntent(arg)
7469
}
75-
intent.flags = (
76-
Intent.FLAG_ACTIVITY_NEW_TASK
77-
or Intent.FLAG_ACTIVITY_NO_HISTORY
78-
)
79-
ActivityUtils.startActivity(intent)
70+
else -> packageManager.getLaunchIntentForPackage(arg) // Package name
71+
}?.apply {
72+
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
73+
}?.let {
74+
runCatching {
75+
Timber.d("startIntent: arg=$arg")
76+
startActivity(it)
77+
}.getOrElse { Timber.e(it, "Error on starting activity with intent") }
78+
}
8079
}
8180

82-
private fun startIntent(
81+
private fun Context.startIntent(
8382
action: String,
8483
arg: String,
8584
) {
86-
val act = "android.intent.action.${action.uppercase()}"
87-
var intent = Intent(act)
88-
when (act) {
85+
val longAction = "android.intent.action.${action.uppercase()}"
86+
val intent = Intent(longAction)
87+
when (longAction) {
8988
// Search or open link
9089
// Note that web_search cannot directly open link
9190
Intent.ACTION_WEB_SEARCH, Intent.ACTION_SEARCH -> {
9291
if (arg.startsWith("http")) {
9392
startIntent(arg)
94-
ActivityUtils.startLauncherActivity()
9593
return
9694
} else {
9795
intent.putExtra(SearchManager.QUERY, arg)
9896
}
9997
}
100-
// Share text
101-
Intent.ACTION_SEND -> intent = IntentUtils.getShareTextIntent(arg)
102-
// Stage the data
103-
else -> {
104-
if (arg.isNotEmpty()) Intent(act).data = Uri.parse(arg) else Intent(act)
98+
Intent.ACTION_SEND -> { // Share text
99+
intent.apply {
100+
type = "text/plain"
101+
putExtra(Intent.EXTRA_TEXT, arg)
102+
}
103+
}
104+
else -> { // Stage the data
105+
if (arg.isNotEmpty()) intent.data = Uri.parse(arg)
105106
}
106107
}
107108
intent.flags = (Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY)
108-
ActivityUtils.startActivity(intent)
109+
runCatching {
110+
Timber.d("startIntent: action=$longAction, arg=$arg")
111+
startActivity(intent)
112+
}.getOrElse { Timber.e(it, "Error on starting activity with intent") }
109113
}
110114

111115
private fun getDate(string: String): CharSequence {
@@ -148,13 +152,17 @@ object ShortcutUtils {
148152
}
149153
}
150154

151-
fun openCategory(keyCode: Int): Boolean {
155+
fun Context.openCategory(keyCode: Int): Boolean {
152156
val category = applicationLaunchKeyCategories[keyCode]
153-
return if (category != null) {
154-
Timber.d("\t<TrimeInput>\topenCategory()\tkeycode=%d, app=%s", keyCode, category)
155-
val intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, category)
156-
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
157-
ActivityUtils.startActivity(intent)
157+
return if (!category.isNullOrEmpty()) {
158+
Timber.d("openCategory: keyEvent=${KeyEvent.keyCodeToString(keyCode)}, category=$category")
159+
val intent =
160+
Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, category).apply {
161+
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
162+
}
163+
runCatching {
164+
startActivity(intent)
165+
}.getOrElse { Timber.e(it, "Error on starting activity with category") }
158166
true
159167
} else {
160168
false

app/src/main/res/values/donottranslate.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ SPDX-License-Identifier: GPL-3.0-or-later
1414
<item>none</item>
1515
</string-array>
1616
<string-array name="keyboard__split_values">
17-
<item>never</item>
18-
<item>landscape</item>
19-
<item>auto</item>
20-
<item>always</item>
17+
<item>NEVER</item>
18+
<item>LANDSCAPE</item>
19+
<item>AUTO</item>
20+
<item>ALWAYS</item>
2121
</string-array>
2222
<string-array name="other__ui_mode_values">
2323
<item>auto</item>

0 commit comments

Comments
 (0)