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