@@ -16,8 +16,6 @@ import android.os.Build
16
16
import android.text.TextUtils
17
17
import android.util.SparseArray
18
18
import android.view.KeyEvent
19
- import com.blankj.utilcode.util.ActivityUtils
20
- import com.blankj.utilcode.util.IntentUtils
21
19
import com.osfans.trime.core.Rime
22
20
import com.osfans.trime.daemon.RimeDaemon
23
21
import com.osfans.trime.data.AppPrefs
@@ -50,62 +48,68 @@ object ShortcutUtils {
50
48
" clipboard" -> return pasteFromClipboard(context)
51
49
" date" -> return getDate(option)
52
50
" commit" -> return option
53
- " run" -> startIntent(option)
51
+ " run" -> context. startIntent(option)
54
52
" share_text" -> TrimeInputMethodService .getService().shareText()
55
53
" liquid_keyboard" -> TrimeInputMethodService .getService().selectLiquidKeyboard(option)
56
- else -> startIntent(command, option)
54
+ else -> context. startIntent(command, option)
57
55
}
58
56
return null
59
57
}
60
58
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)
72
68
}
73
- else -> IntentUtils .getLaunchAppIntent(arg)
74
69
}
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
+ }
80
79
}
81
80
82
- private fun startIntent (
81
+ private fun Context. startIntent (
83
82
action : String ,
84
83
arg : String ,
85
84
) {
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 ) {
89
88
// Search or open link
90
89
// Note that web_search cannot directly open link
91
90
Intent .ACTION_WEB_SEARCH , Intent .ACTION_SEARCH -> {
92
91
if (arg.startsWith(" http" )) {
93
92
startIntent(arg)
94
- ActivityUtils .startLauncherActivity()
95
93
return
96
94
} else {
97
95
intent.putExtra(SearchManager .QUERY , arg)
98
96
}
99
97
}
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)
105
106
}
106
107
}
107
108
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" ) }
109
113
}
110
114
111
115
private fun getDate (string : String ): CharSequence {
@@ -148,13 +152,17 @@ object ShortcutUtils {
148
152
}
149
153
}
150
154
151
- fun openCategory (keyCode : Int ): Boolean {
155
+ fun Context. openCategory (keyCode : Int ): Boolean {
152
156
val category = applicationLaunchKeyCategories[keyCode]
153
- return if (category != null ) {
154
- Timber .d(" \t <TrimeInput>\t openCategory()\t keycode=%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" ) }
158
166
true
159
167
} else {
160
168
false
0 commit comments