Skip to content

Commit 559e43f

Browse files
WhiredPlanckBambooin
authored andcommitted
utils: ShortcutUtils: new, to replace Function
1 parent a4c31cb commit 559e43f

File tree

2 files changed

+143
-4
lines changed

2 files changed

+143
-4
lines changed

app/src/main/java/com/osfans/trime/ime/core/Trime.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@
8383
import com.osfans.trime.settings.components.ThemePickerDialog;
8484
import com.osfans.trime.setup.Config;
8585
import com.osfans.trime.setup.IntentReceiver;
86-
import com.osfans.trime.util.Function;
8786
import com.osfans.trime.util.LocaleUtils;
87+
import com.osfans.trime.util.ShortcutUtils;
8888
import com.osfans.trime.util.StringUtils;
8989
import java.util.Locale;
9090
import java.util.regex.Matcher;
@@ -152,7 +152,7 @@ private Preferences getPrefs() {
152152
new Handler(
153153
msg -> {
154154
if (!((Trime) msg.obj).isShowInputRequested()) { // 若当前没有输入面板,则后台同步。防止面板关闭后5秒内再次打开
155-
Function.syncBackground((Trime) msg.obj);
155+
ShortcutUtils.INSTANCE.syncInBackground((Trime) msg.obj);
156156
((Trime) msg.obj).loadConfig();
157157
}
158158
return false;
@@ -1040,7 +1040,7 @@ public void onEvent(@NonNull Event event) {
10401040
getActiveText(2),
10411041
getActiveText(3),
10421042
getActiveText(4));
1043-
s = Function.handle(this, event.getCommand(), arg);
1043+
s = (String) ShortcutUtils.INSTANCE.call(this, event.getCommand(), arg);
10441044
if (s != null) {
10451045
commitText(s);
10461046
updateComposing();
@@ -1080,7 +1080,7 @@ private boolean handleKey(int keyCode, int mask) { // 軟鍵盤
10801080
|| handleEnter(keyCode)
10811081
|| handleBack(keyCode)) {
10821082
Timber.i("Trime onKey");
1083-
} else if (Function.openCategory(this, keyCode)) {
1083+
} else if (ShortcutUtils.INSTANCE.openCategory(keyCode)) {
10841084
Timber.i("Open category");
10851085
} else {
10861086
keyUpNeeded = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.osfans.trime.util
2+
3+
import android.app.SearchManager
4+
import android.content.ClipboardManager
5+
import android.content.ComponentName
6+
import android.content.Context
7+
import android.content.Intent
8+
import android.icu.text.DateFormat
9+
import android.icu.util.Calendar
10+
import android.icu.util.ULocale
11+
import android.net.Uri
12+
import android.os.Build
13+
import android.util.SparseArray
14+
import android.view.KeyEvent
15+
import com.blankj.utilcode.util.ActivityUtils
16+
import com.blankj.utilcode.util.IntentUtils
17+
import com.osfans.trime.Rime
18+
import com.osfans.trime.ime.core.Preferences
19+
import java.text.FieldPosition
20+
import java.text.SimpleDateFormat
21+
import java.util.*
22+
23+
/**
24+
* Implementation to open/call specified application/function
25+
*/
26+
object ShortcutUtils {
27+
fun call(context: Context, command: String, option: String): Any? {
28+
when (command) {
29+
"broadcast" -> context.sendBroadcast(Intent(option))
30+
"clipboard" -> return pasteFromClipboard(context)
31+
"date" -> return getDate(option)
32+
"run" -> startIntent(option)
33+
else -> startIntent(command, option)
34+
}
35+
return null
36+
}
37+
38+
private fun startIntent(arg: String) {
39+
val intent = when {
40+
arg.indexOf(':') >= 0 -> {
41+
Intent.parseUri(arg, Intent.URI_INTENT_SCHEME)
42+
}
43+
arg.indexOf('/') >= 0 -> {
44+
Intent(Intent.ACTION_MAIN).apply {
45+
addCategory(Intent.CATEGORY_LAUNCHER)
46+
component = ComponentName.unflattenFromString(arg)
47+
}
48+
}
49+
else -> IntentUtils.getLaunchAppIntent(arg)
50+
}
51+
intent.flags = (Intent.FLAG_ACTIVITY_NEW_TASK
52+
or Intent.FLAG_ACTIVITY_NO_HISTORY)
53+
ActivityUtils.startActivity(intent)
54+
}
55+
56+
private fun startIntent(action: String, arg: String) {
57+
val act = "android.intent.action.${action.uppercase()}"
58+
var intent = Intent(act)
59+
when (act) {
60+
// Search or open link
61+
// Note that web_search cannot directly open link
62+
Intent.ACTION_WEB_SEARCH, Intent.ACTION_SEARCH -> {
63+
if (arg.startsWith("http")) {
64+
startIntent(arg)
65+
ActivityUtils.startLauncherActivity()
66+
return
67+
} else {
68+
intent.putExtra(SearchManager.QUERY, arg)
69+
}
70+
}
71+
// Share text
72+
Intent.ACTION_SEND -> intent = IntentUtils.getShareTextIntent(arg)
73+
// Stage the data
74+
else -> {
75+
if (arg.isNotEmpty()) Intent(act).data = Uri.parse(arg) else Intent(act)
76+
}
77+
}
78+
intent.flags = (Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY)
79+
ActivityUtils.startActivity(intent)
80+
}
81+
82+
private fun getDate(string: String): CharSequence {
83+
var option = ""
84+
var locale = ""
85+
if (string.contains("@")) {
86+
val opt = string.split(" ".toRegex(), 2)
87+
if (opt.size == 2 && opt[0].contains("@")) {
88+
locale = opt[0]
89+
option = opt[1]
90+
} else {
91+
locale = opt[0]
92+
}
93+
}
94+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
95+
val ul = ULocale(locale)
96+
val cc = Calendar.getInstance(ul)
97+
val df = if (option.isEmpty()) {
98+
DateFormat.getDateInstance(DateFormat.LONG, ul)
99+
} else {
100+
android.icu.text.SimpleDateFormat(option, Locale(locale))
101+
}
102+
df.format(cc, StringBuffer(256), FieldPosition(0)).toString()
103+
} else {
104+
SimpleDateFormat(option, Locale.getDefault()).format(Date()) // Time
105+
}
106+
}
107+
108+
private fun pasteFromClipboard(context: Context): CharSequence {
109+
val systemClipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
110+
val systemPrimaryClip = systemClipboardManager.primaryClip
111+
return if (systemPrimaryClip?.getItemAt(0)?.text == null) {""}
112+
else systemPrimaryClip.getItemAt(0)?.text!!
113+
}
114+
115+
fun syncInBackground(context: Context) {
116+
val prefs = Preferences.defaultInstance()
117+
prefs.conf.lastSyncTime = Date().time
118+
prefs.conf.lastSyncStatus = Rime.syncUserData(context)
119+
}
120+
121+
fun openCategory(keyCode: Int): Boolean {
122+
val category = applicationLaunchKeyCategories[keyCode]
123+
return if (category != null) {
124+
val intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, category)
125+
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
126+
ActivityUtils.startActivity(intent)
127+
true
128+
} else false
129+
}
130+
131+
private val applicationLaunchKeyCategories = SparseArray<String>().apply {
132+
append(KeyEvent.KEYCODE_EXPLORER, "android.intent.category.APP_BROWSER")
133+
append(KeyEvent.KEYCODE_ENVELOPE, "android.intent.category.APP_EMAIL")
134+
append(KeyEvent.KEYCODE_CONTACTS, "android.intent.category.APP_CONTACTS")
135+
append(KeyEvent.KEYCODE_CALENDAR, "android.intent.category.APP_CALENDAR")
136+
append(KeyEvent.KEYCODE_MUSIC, "android.intent.category.APP_MUSIC")
137+
append(KeyEvent.KEYCODE_CALCULATOR, "android.intent.category.APP_CALCULATOR")
138+
}
139+
}

0 commit comments

Comments
 (0)