a
554325746@qq.com
2019-12-31 fa104829ecef68865e5e3bce174515009c6d687b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * 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 <http://www.gnu.org/licenses/>.
 */
 
package com.osfans.trime;
 
import android.annotation.TargetApi;
import android.app.SearchManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.icu.util.Calendar;
import android.icu.util.ULocale;
import android.net.Uri;
import android.os.Build.*;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.SparseArray;
import android.view.KeyEvent;
import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
 
/** 實現打開指定程序、打開{@link Pref 輸入法全局設置}對話框等功能 */
class Function {
  private static String TAG = Function.class.getSimpleName();
  private static SparseArray<String> sApplicationLaunchKeyCategories;
 
  static {
    sApplicationLaunchKeyCategories = new SparseArray<String>();
    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");
  }
 
  @TargetApi(VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
  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) {
        Log.e(TAG, "Start Activity Exception" + ex);
      }
      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) {
      Log.e(TAG, "Start Activity Exception" + ex);
    }
  }
 
  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 (!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) {
      Log.e(TAG, "Start Activity Exception" + ex);
    }
  }
 
  public static void showPrefDialog(Context context) {
    Intent intent = new Intent(context, Pref.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
    context.startActivity(intent);
  }
 
  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 && !isEmpty(locale)) {
      ULocale ul = new ULocale(locale);
      Calendar cc = Calendar.getInstance(ul);
      android.icu.text.DateFormat df;
      if (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;
  }
 
  public static String handle(Context context, String command, String option) {
    String s = null;
    if (command == null) return s;
    switch (command) {
      case "date":
        s = getDate(option);
        break;
      case "run":
        startIntent(context, option); //啓動程序
        break;
      case "broadcast":
        context.sendBroadcast(new Intent(option)); //廣播
        break;
      default:
        startIntent(context, command, option); //其他intent
        break;
    }
    return s;
  }
 
  public static boolean isEmpty(CharSequence s) {
    return (s == null) || (s.length() == 0);
  }
 
  public static void check() {
    Rime.check(true);
    System.exit(0); //清理內存
  }
 
  public static void deploy() {
    Rime.destroy();
    Rime.get(true);
    //Trime trime = Trime.getService();
    //if (trime != null) trime.invalidate();
  }
 
  public static void sync() {
    Rime.syncUserData();
  }
 
  public static String getVersion(Context context) {
    try {
      return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (Exception e) {
      return null;
    }
  }
 
  public static boolean isAppAvailable(Context context, String app) {
    final PackageManager packageManager = context.getPackageManager();
    List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
    if (pinfo != null) {
      for (int i = 0; i < pinfo.size(); i++) {
        String pn = pinfo.get(i).packageName;
        if (pn.equals(app)) {
          return true;
        }
      }
    }
    return false;
  }
 
  public static SharedPreferences getPref(Context context) {
    return PreferenceManager.getDefaultSharedPreferences(context);
  }
 
  public static boolean isDiffVer(Context context) {
    String version = getVersion(context);
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
    String pref_ver = pref.getString("version_name", "");
    boolean isDiff = !version.contentEquals(pref_ver);
    if (isDiff) {
      SharedPreferences.Editor edit = pref.edit();
      edit.putString("version_name", version);
      edit.apply();
    }
    return isDiff;
  }
}