Skip to content

Commit

Permalink
refactor: use native apis to build intent and start activity
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jun 14, 2024
1 parent 95272b0 commit 79f9fb1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/osfans/trime/data/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ class AppPrefs(
private set

enum class SplitOption {
AUTO,
LANDSCAPE,
NEVER,
LANDSCAPE,
AUTO,
ALWAYS,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.osfans.trime.ime.symbol.TabManager
import com.osfans.trime.ime.text.Candidate
import com.osfans.trime.ime.text.TextInputManager
import com.osfans.trime.util.ShortcutUtils
import com.osfans.trime.util.ShortcutUtils.openCategory
import com.osfans.trime.util.StringUtils
import com.osfans.trime.util.WeakHashSet
import com.osfans.trime.util.isNightMode
Expand Down Expand Up @@ -944,7 +945,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
// 处理返回键(隐藏软键盘)和回车键(换行)
// todo 确认是否有必要单独处理回车键?是否需要把back和escape全部占用?
Timber.d("\t<TrimeInput>\thandleKey()\tEnterOrHide, keycode=%d", keyEventCode)
} else if (ShortcutUtils.openCategory(keyEventCode)) {
} else if (openCategory(keyEventCode)) {
// 打开系统默认应用
Timber.d("\t<TrimeInput>\thandleKey()\topenCategory keycode=%d", keyEventCode)
} else {
Expand Down
84 changes: 46 additions & 38 deletions app/src/main/java/com/osfans/trime/util/ShortcutUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import android.os.Build
import android.text.TextUtils
import android.util.SparseArray
import android.view.KeyEvent
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.IntentUtils
import com.osfans.trime.core.Rime
import com.osfans.trime.daemon.RimeDaemon
import com.osfans.trime.data.AppPrefs
Expand Down Expand Up @@ -50,62 +48,68 @@ object ShortcutUtils {
"clipboard" -> return pasteFromClipboard(context)
"date" -> return getDate(option)
"commit" -> return option
"run" -> startIntent(option)
"run" -> context.startIntent(option)
"share_text" -> TrimeInputMethodService.getService().shareText()
"liquid_keyboard" -> TrimeInputMethodService.getService().selectLiquidKeyboard(option)
else -> startIntent(command, option)
else -> context.startIntent(command, option)
}
return null
}

private fun startIntent(arg: String) {
val intent =
when {
arg.indexOf(':') >= 0 -> {
Intent.parseUri(arg, Intent.URI_INTENT_SCHEME)
}
arg.indexOf('/') >= 0 -> {
Intent(Intent.ACTION_MAIN).apply {
addCategory(Intent.CATEGORY_LAUNCHER)
component = ComponentName.unflattenFromString(arg)
}
private fun Context.startIntent(arg: String) {
when {
arg.contains(':') -> { // URI
Intent.parseUri(arg, Intent.URI_INTENT_SCHEME)
}
arg.contains('/') -> { // Component name
Intent(Intent.ACTION_MAIN).apply {
addCategory(Intent.CATEGORY_LAUNCHER)
component = ComponentName.unflattenFromString(arg)
}
else -> IntentUtils.getLaunchAppIntent(arg)
}
intent.flags = (
Intent.FLAG_ACTIVITY_NEW_TASK
or Intent.FLAG_ACTIVITY_NO_HISTORY
)
ActivityUtils.startActivity(intent)
else -> packageManager.getLaunchIntentForPackage(arg) // Package name
}?.apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
}?.let {
runCatching {
Timber.d("startIntent: arg=$arg")
startActivity(it)
}.getOrElse { Timber.e(it, "Error on starting activity with intent") }
}
}

private fun startIntent(
private fun Context.startIntent(
action: String,
arg: String,
) {
val act = "android.intent.action.${action.uppercase()}"
var intent = Intent(act)
when (act) {
val longAction = "android.intent.action.${action.uppercase()}"
val intent = Intent(longAction)
when (longAction) {
// Search or open link
// Note that web_search cannot directly open link
Intent.ACTION_WEB_SEARCH, Intent.ACTION_SEARCH -> {
if (arg.startsWith("http")) {
startIntent(arg)
ActivityUtils.startLauncherActivity()
return
} else {
intent.putExtra(SearchManager.QUERY, arg)
}
}
// Share text
Intent.ACTION_SEND -> intent = IntentUtils.getShareTextIntent(arg)
// Stage the data
else -> {
if (arg.isNotEmpty()) Intent(act).data = Uri.parse(arg) else Intent(act)
Intent.ACTION_SEND -> { // Share text
intent.apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, arg)
}
}
else -> { // Stage the data
if (arg.isNotEmpty()) intent.data = Uri.parse(arg)
}
}
intent.flags = (Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY)
ActivityUtils.startActivity(intent)
runCatching {
Timber.d("startIntent: action=$longAction, arg=$arg")
startActivity(intent)
}.getOrElse { Timber.e(it, "Error on starting activity with intent") }
}

private fun getDate(string: String): CharSequence {
Expand Down Expand Up @@ -148,13 +152,17 @@ object ShortcutUtils {
}
}

fun openCategory(keyCode: Int): Boolean {
fun Context.openCategory(keyCode: Int): Boolean {
val category = applicationLaunchKeyCategories[keyCode]
return if (category != null) {
Timber.d("\t<TrimeInput>\topenCategory()\tkeycode=%d, app=%s", keyCode, category)
val intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, category)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
ActivityUtils.startActivity(intent)
return if (!category.isNullOrEmpty()) {
Timber.d("openCategory: keyEvent=${KeyEvent.keyCodeToString(keyCode)}, category=$category")
val intent =
Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, category).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
}
runCatching {
startActivity(intent)
}.getOrElse { Timber.e(it, "Error on starting activity with category") }
true
} else {
false
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ SPDX-License-Identifier: GPL-3.0-or-later
<item>none</item>
</string-array>
<string-array name="keyboard__split_values">
<item>never</item>
<item>landscape</item>
<item>auto</item>
<item>always</item>
<item>NEVER</item>
<item>LANDSCAPE</item>
<item>AUTO</item>
<item>ALWAYS</item>
</string-array>
<string-array name="other__ui_mode_values">
<item>auto</item>
Expand Down

0 comments on commit 79f9fb1

Please sign in to comment.