Skip to content

Commit

Permalink
utils: ShortcutUtils: new, to replace Function
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck authored and Bambooin committed Sep 8, 2021
1 parent a4c31cb commit 559e43f
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 4 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
import com.osfans.trime.settings.components.ThemePickerDialog;
import com.osfans.trime.setup.Config;
import com.osfans.trime.setup.IntentReceiver;
import com.osfans.trime.util.Function;
import com.osfans.trime.util.LocaleUtils;
import com.osfans.trime.util.ShortcutUtils;
import com.osfans.trime.util.StringUtils;
import java.util.Locale;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -152,7 +152,7 @@ private Preferences getPrefs() {
new Handler(
msg -> {
if (!((Trime) msg.obj).isShowInputRequested()) { // 若当前没有输入面板,则后台同步。防止面板关闭后5秒内再次打开
Function.syncBackground((Trime) msg.obj);
ShortcutUtils.INSTANCE.syncInBackground((Trime) msg.obj);
((Trime) msg.obj).loadConfig();
}
return false;
Expand Down Expand Up @@ -1040,7 +1040,7 @@ public void onEvent(@NonNull Event event) {
getActiveText(2),
getActiveText(3),
getActiveText(4));
s = Function.handle(this, event.getCommand(), arg);
s = (String) ShortcutUtils.INSTANCE.call(this, event.getCommand(), arg);
if (s != null) {
commitText(s);
updateComposing();
Expand Down Expand Up @@ -1080,7 +1080,7 @@ private boolean handleKey(int keyCode, int mask) { // 軟鍵盤
|| handleEnter(keyCode)
|| handleBack(keyCode)) {
Timber.i("Trime onKey");
} else if (Function.openCategory(this, keyCode)) {
} else if (ShortcutUtils.INSTANCE.openCategory(keyCode)) {
Timber.i("Open category");
} else {
keyUpNeeded = true;
Expand Down
139 changes: 139 additions & 0 deletions app/src/main/java/com/osfans/trime/util/ShortcutUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.osfans.trime.util

import android.app.SearchManager
import android.content.ClipboardManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.icu.text.DateFormat
import android.icu.util.Calendar
import android.icu.util.ULocale
import android.net.Uri
import android.os.Build
import android.util.SparseArray
import android.view.KeyEvent
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.IntentUtils
import com.osfans.trime.Rime
import com.osfans.trime.ime.core.Preferences
import java.text.FieldPosition
import java.text.SimpleDateFormat
import java.util.*

/**
* Implementation to open/call specified application/function
*/
object ShortcutUtils {
fun call(context: Context, command: String, option: String): Any? {
when (command) {
"broadcast" -> context.sendBroadcast(Intent(option))
"clipboard" -> return pasteFromClipboard(context)
"date" -> return getDate(option)
"run" -> startIntent(option)
else -> 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)
}
}
else -> IntentUtils.getLaunchAppIntent(arg)
}
intent.flags = (Intent.FLAG_ACTIVITY_NEW_TASK
or Intent.FLAG_ACTIVITY_NO_HISTORY)
ActivityUtils.startActivity(intent)
}

private fun startIntent(action: String, arg: String) {
val act = "android.intent.action.${action.uppercase()}"
var intent = Intent(act)
when (act) {
// 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.flags = (Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY)
ActivityUtils.startActivity(intent)
}

private fun getDate(string: String): CharSequence {
var option = ""
var locale = ""
if (string.contains("@")) {
val opt = string.split(" ".toRegex(), 2)
if (opt.size == 2 && opt[0].contains("@")) {
locale = opt[0]
option = opt[1]
} else {
locale = opt[0]
}
}
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val ul = ULocale(locale)
val cc = Calendar.getInstance(ul)
val df = if (option.isEmpty()) {
DateFormat.getDateInstance(DateFormat.LONG, ul)
} else {
android.icu.text.SimpleDateFormat(option, Locale(locale))
}
df.format(cc, StringBuffer(256), FieldPosition(0)).toString()
} else {
SimpleDateFormat(option, Locale.getDefault()).format(Date()) // Time
}
}

private fun pasteFromClipboard(context: Context): CharSequence {
val systemClipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val systemPrimaryClip = systemClipboardManager.primaryClip
return if (systemPrimaryClip?.getItemAt(0)?.text == null) {""}
else systemPrimaryClip.getItemAt(0)?.text!!
}

fun syncInBackground(context: Context) {
val prefs = Preferences.defaultInstance()
prefs.conf.lastSyncTime = Date().time
prefs.conf.lastSyncStatus = Rime.syncUserData(context)
}

fun openCategory(keyCode: Int): Boolean {
val category = applicationLaunchKeyCategories[keyCode]
return if (category != null) {
val intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, category)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
ActivityUtils.startActivity(intent)
true
} else false
}

private val applicationLaunchKeyCategories = SparseArray<String>().apply {
append(KeyEvent.KEYCODE_EXPLORER, "android.intent.category.APP_BROWSER")
append(KeyEvent.KEYCODE_ENVELOPE, "android.intent.category.APP_EMAIL")
append(KeyEvent.KEYCODE_CONTACTS, "android.intent.category.APP_CONTACTS")
append(KeyEvent.KEYCODE_CALENDAR, "android.intent.category.APP_CALENDAR")
append(KeyEvent.KEYCODE_MUSIC, "android.intent.category.APP_MUSIC")
append(KeyEvent.KEYCODE_CALCULATOR, "android.intent.category.APP_CALCULATOR")
}
}

0 comments on commit 559e43f

Please sign in to comment.