From 33a893df0cd26aeba8f2398aa9a64bd01799eaa7 Mon Sep 17 00:00:00 2001 From: WhiredPlanck Date: Tue, 7 Sep 2021 23:26:48 +0800 Subject: [PATCH] utils: `ShortcutUtils`: mark new line; remove `Function` --- .../java/com/osfans/trime/util/Function.java | 207 ------------------ .../com/osfans/trime/util/ShortcutUtils.kt | 7 +- 2 files changed, 4 insertions(+), 210 deletions(-) delete mode 100644 app/src/main/java/com/osfans/trime/util/Function.java diff --git a/app/src/main/java/com/osfans/trime/util/Function.java b/app/src/main/java/com/osfans/trime/util/Function.java deleted file mode 100644 index 0d1a1599a4..0000000000 --- a/app/src/main/java/com/osfans/trime/util/Function.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (C) 2015-present, osfans - * waxaca@163.com https://github.com/osfans - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.osfans.trime.util; - -import android.annotation.SuppressLint; -import android.app.SearchManager; -import android.content.ClipData; -import android.content.ClipboardManager; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.icu.util.Calendar; -import android.icu.util.ULocale; -import android.net.Uri; -import android.os.Build.VERSION; -import android.os.Build.VERSION_CODES; -import android.text.TextUtils; -import android.util.SparseArray; -import android.view.KeyEvent; -import androidx.annotation.NonNull; -import com.osfans.trime.Rime; -import com.osfans.trime.ime.core.Preferences; -import com.osfans.trime.settings.PrefMainActivity; -import java.text.FieldPosition; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; -import timber.log.Timber; - -/** 實現打開指定程序、打開{@link PrefMainActivity 輸入法全局設置}對話框等功能 */ -public class Function { - private static final SparseArray sApplicationLaunchKeyCategories; - - static { - sApplicationLaunchKeyCategories = new SparseArray<>(); - sApplicationLaunchKeyCategories.append( - KeyEvent.KEYCODE_EXPLORER, "android.intent.category.APP_BROWSER"); - sApplicationLaunchKeyCategories.append( - KeyEvent.KEYCODE_ENVELOPE, "android.intent.category.APP_EMAIL"); - sApplicationLaunchKeyCategories.append(207, "android.intent.category.APP_CONTACTS"); - sApplicationLaunchKeyCategories.append(208, "android.intent.category.APP_CALENDAR"); - sApplicationLaunchKeyCategories.append(209, "android.intent.category.APP_EMAIL"); - sApplicationLaunchKeyCategories.append(210, "android.intent.category.APP_CALCULATOR"); - } - - public static boolean openCategory(Context context, int keyCode) { - String category = sApplicationLaunchKeyCategories.get(keyCode); - if (category != null) { - Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, category); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY); - try { - context.startActivity(intent); - } catch (Exception ex) { - Timber.e(ex, "Start Activity Exception"); - } - return true; - } - return false; - } - - private static void startIntent(Context context, String arg) { - Intent intent; - try { - if (arg.indexOf(':') >= 0) { - // The argument is a URI. Fully parse it, and use that result - // to fill in any data not specified so far. - intent = Intent.parseUri(arg, Intent.URI_INTENT_SCHEME); - } else if (arg.indexOf('/') >= 0) { - // The argument is a component name. Build an Intent to launch - // it. - intent = new Intent(Intent.ACTION_MAIN); - intent.addCategory(Intent.CATEGORY_LAUNCHER); - intent.setComponent(ComponentName.unflattenFromString(arg)); - } else { - // Assume the argument is a package name. - intent = context.getPackageManager().getLaunchIntentForPackage(arg); - } - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY); - context.startActivity(intent); - } catch (Exception ex) { - Timber.e(ex, "Start Activity Exception"); - } - } - - private static void startIntent(Context context, String action, String arg) { - action = "android.intent.action." + action.toUpperCase(Locale.getDefault()); - try { - Intent intent = new Intent(action); - switch (action) { - case Intent.ACTION_WEB_SEARCH: - case Intent.ACTION_SEARCH: - if (arg.startsWith("http")) { // web_search無法直接打開網址 - startIntent(context, arg); - return; - } - intent.putExtra(SearchManager.QUERY, arg); - break; - case Intent.ACTION_SEND: // 分享文本 - intent.setType("text/plain"); - intent.putExtra(Intent.EXTRA_TEXT, arg); - break; - default: - if (!TextUtils.isEmpty(arg)) intent.setData(Uri.parse(arg)); - break; - } - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY); - context.startActivity(intent); - } catch (Exception ex) { - Timber.e(ex, "Start Activity Exception"); - } - } - - @SuppressLint("SimpleDateFormat") - private static String getDate(String option) { - String s; - String locale = ""; - if (option.contains("@")) { - String[] ss = option.split(" ", 2); - if (ss.length == 2 && ss[0].contains("@")) { - locale = ss[0]; - option = ss[1]; - } else if (ss.length == 1) { - locale = ss[0]; - option = ""; - } - } - if (VERSION.SDK_INT >= VERSION_CODES.N && !TextUtils.isEmpty(locale)) { - ULocale ul = new ULocale(locale); - Calendar cc = Calendar.getInstance(ul); - android.icu.text.DateFormat df; - if (TextUtils.isEmpty(option)) { - df = android.icu.text.DateFormat.getDateInstance(android.icu.text.DateFormat.LONG, ul); - } else { - df = new android.icu.text.SimpleDateFormat(option, ul); - } - s = df.format(cc, new StringBuffer(256), new FieldPosition(0)).toString(); - } else { - s = new SimpleDateFormat(option, Locale.getDefault()).format(new Date()); // 時間 - } - return s; - } - - @NonNull - private static String getClipboard(@NonNull Context context) { - final ClipboardManager clipboard = - (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); - final ClipData primaryClip = clipboard.getPrimaryClip(); - if (primaryClip == null) { - return ""; - } - final ClipData.Item item = primaryClip.getItemAt(0); - if (item == null) { - return ""; - } - final CharSequence pasteData = item.getText(); - if (pasteData != null) { - return pasteData.toString(); - } else { - return ""; - } - } - - public static String handle(Context context, String command, String option) { - String s = null; - if (command == null) return null; - switch (command) { - case "date": - s = getDate(option); - break; - case "run": - startIntent(context, option); // 啓動程序 - break; - case "broadcast": - context.sendBroadcast(new Intent(option)); // 廣播 - break; - case "clipboard": - s = getClipboard(context); - break; - default: - startIntent(context, command, option); // 其他intent - break; - } - return s; - } - - public static void syncBackground(Context ctx) { - final Preferences prefs = Preferences.Companion.defaultInstance(); - prefs.getConf().setLastSyncTime(new Date().getTime()); - prefs.getConf().setLastSyncStatus(Rime.syncUserData(ctx)); - } -} diff --git a/app/src/main/java/com/osfans/trime/util/ShortcutUtils.kt b/app/src/main/java/com/osfans/trime/util/ShortcutUtils.kt index 92427b8ec0..5bd12007f4 100644 --- a/app/src/main/java/com/osfans/trime/util/ShortcutUtils.kt +++ b/app/src/main/java/com/osfans/trime/util/ShortcutUtils.kt @@ -18,7 +18,8 @@ import com.osfans.trime.Rime import com.osfans.trime.ime.core.Preferences import java.text.FieldPosition import java.text.SimpleDateFormat -import java.util.* +import java.util.Date +import java.util.Locale /** * Implementation to open/call specified application/function @@ -97,7 +98,7 @@ object ShortcutUtils { val df = if (option.isEmpty()) { DateFormat.getDateInstance(DateFormat.LONG, ul) } else { - android.icu.text.SimpleDateFormat(option, Locale(locale)) + android.icu.text.SimpleDateFormat(option, ul.toLocale()) } df.format(cc, StringBuffer(256), FieldPosition(0)).toString() } else { @@ -136,4 +137,4 @@ object ShortcutUtils { append(KeyEvent.KEYCODE_MUSIC, "android.intent.category.APP_MUSIC") append(KeyEvent.KEYCODE_CALCULATOR, "android.intent.category.APP_CALCULATOR") } -} \ No newline at end of file +}