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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * 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.view.KeyEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
 
/** {@link Key 按鍵}的各種事件(單擊、長按、滑動等) */
public class Event {
  private String TAG = "Event";
  private Keyboard mKeyboard;
  private int code = 0;
  private int mask = 0;
  private String text;
  private String label;
  private String preview;
  private List<String> states;
  private String command;
  private String option;
  private String select;
  private String toggle;
  private String commit;
 
  private String shiftLock;
  private boolean functional;
  private boolean repeatable;
  private boolean sticky;
 
  public Event(Keyboard keyboard, String s) {
    mKeyboard = keyboard;
    if (s.matches("\\{[^\\{\\}]+\\}")) { //{send|key}
      label = s.substring(1, s.length() -1);
      int[] sends = parseSend(label); //send
      code = sends[0];
      mask = sends[1];
      if (code >= 0) return;
      s = label; //key
      label = null;
    }
    if (Key.presetKeys.containsKey(s)) {
      Map m = Key.presetKeys.get(s);
      command = Config.getString(m, "command");
      option = Config.getString(m, "option");
      select = Config.getString(m, "select");
      toggle = Config.getString(m, "toggle");
      label = Config.getString(m, "label");
      preview = Config.getString(m, "preview");
      shiftLock = Config.getString(m, "shift_lock");
      commit = Config.getString(m, "commit");
      String send = Config.getString(m, "send");
      if (Function.isEmpty(send) && !Function.isEmpty(command)) send = "function"; //command默認發function
      int[] sends = parseSend(send);
      code = sends[0];
      mask = sends[1];
      parseLabel();
      text = Config.getString(m, "text");
      if (code < 0 && Function.isEmpty(text)) text = s;
      if (m.containsKey("states")) states = (List<String>) m.get("states");
      sticky = Config.getBoolean(m, "sticky", false);
      repeatable = Config.getBoolean(m, "repeatable", false);
      functional = Config.getBoolean(m, "functional", true);
    } else if ((code = getClickCode(s)) >= 0) {
      parseLabel();
    } else {
      code = 0;
      text = s;
      label = s.replaceAll("\\{[^\\{\\}]+?\\}", "");
    }
  }
 
  public Event(String s) {
    this(null, s);
  }
 
  public int getCode() {
    return code;
  }
 
  public int getMask() {
    return mask;
  }
 
  public String getCommand() {
    return command;
  }
 
  public String getOption() {
    return option;
  }
 
  public String getSelect() {
    return select;
  }
 
  public boolean isFunctional() {
    return functional;
  }
 
  public boolean isRepeatable() {
    return repeatable;
  }
 
  public boolean isSticky() {
    return sticky;
  }
 
  public String getShiftLock() {
    return shiftLock;
  }
 
  public static int[] parseSend(String s) {
    int[] sends = new int[2];
    if (Function.isEmpty(s)) return sends;
    String codes;
    if (!s.contains("+")) codes = s;
    else {
      String[] ss = s.split("\\+");
      int n = ss.length;
      for (int i = 0; i < n - 1; i++) if (masks.containsKey(ss[i])) sends[1] |= masks.get(ss[i]);
      codes = ss[n - 1];
    }
    sends[0] = Key.androidKeys.indexOf(codes);
    return sends;
  }
 
  private String adjustCase(String s) {
    if (Function.isEmpty(s)) return "";
    if (s.length() == 1 && mKeyboard != null && mKeyboard.isShifted())
      s = s.toUpperCase(Locale.getDefault());
    else if (s.length() == 1
        && mKeyboard != null
        && !Rime.isAsciiMode()
        && mKeyboard.isLabelUppercase()) s = s.toUpperCase(Locale.getDefault());
    return s;
  }
 
  public String getLabel() {
    if (!Function.isEmpty(toggle)) return states.get(Rime.getOption(toggle) ? 1 : 0);
    return adjustCase(label);
  }
 
  public String getText() {
    String s = "";
    if (!Function.isEmpty(text)) s = text;
    else if (mKeyboard != null
        && mKeyboard.isShifted()
        && mask == 0
        && code >= KeyEvent.KEYCODE_A
        && code <= KeyEvent.KEYCODE_Z) s = label;
    return adjustCase(s);
  }
 
  public String getPreviewText() {
    if (!Function.isEmpty(preview)) return preview;
    return getLabel();
  }
 
  public String getToggle() {
    if (!Function.isEmpty(toggle)) return toggle;
    return "ascii_mode";
  }
 
  public String getCommit() {
    return commit;
  }
 
  private void parseLabel() {
    if (!Function.isEmpty(label)) return;
    int c = code;
    if (c == KeyEvent.KEYCODE_SPACE) {
      label = Rime.getSchemaName();
    } else {
      if (c > 0) label = getDisplayLabel(c);
    }
  }
 
  public static String getDisplayLabel(int keyCode) {
    String s = "";
    if (keyCode < Key.getSymbolStart()) { //字母數字
      if (Key.getKcm().isPrintingKey(keyCode)) {
        char c = Key.getKcm().getDisplayLabel(keyCode);
        if (Character.isUpperCase(c)) c = Character.toLowerCase(c);
        s = String.valueOf(c);
      } else {
        s = Key.androidKeys.get(keyCode);
      }
    } else if (keyCode < Key.androidKeys.size()) { //可見符號
      keyCode -= Key.getSymbolStart();
      s = Key.getSymbols().substring(keyCode, keyCode + 1);
    }
    return s;
  }
 
  public static int getClickCode(String s) {
    int keyCode = -1;
    if (Function.isEmpty(s)) { //空鍵
      keyCode = 0;
    } else if (Key.androidKeys.contains(s)) { //字母數字
      keyCode = Key.androidKeys.indexOf(s);
    } else if (Key.getSymbols().contains(s)) { //可見符號
      keyCode = Key.getSymbolStart() + Key.getSymbols().indexOf(s);
    } else if (symbolAliases.containsKey(s)) {
      keyCode = symbolAliases.get(s);
    }
    return keyCode;
  }
 
  private static int getRimeCode(int code) {
    int i = 0;
    if (code >= 0 && code < Key.androidKeys.size()) {
      String s = Key.androidKeys.get(code);
      i = Rime.get_keycode_by_name(s);
    }
    return i;
  }
 
  public static boolean hasModifier(int mask, int modifier) {
    return (mask & modifier) > 0;
  }
 
  public static int[] getRimeEvent(int code, int mask) {
    int i = getRimeCode(code);
    int m = 0;
    if (hasModifier(mask, KeyEvent.META_SHIFT_ON)) m |= Rime.META_SHIFT_ON;
    if (hasModifier(mask, KeyEvent.META_CTRL_ON)) m |= Rime.META_CTRL_ON;
    if (hasModifier(mask, KeyEvent.META_ALT_ON)) m |= Rime.META_ALT_ON;
    if (mask == Rime.META_RELEASE_ON) m |= Rime.META_RELEASE_ON;
    return new int[] {i, m};
  }
 
  private static Map<String, Integer> masks =
      new HashMap<String, Integer>() {
        {
          put("Shift", KeyEvent.META_SHIFT_ON);
          put("Control", KeyEvent.META_CTRL_ON);
          put("Alt", KeyEvent.META_ALT_ON);
        }
      };
 
  private static Map<String, Integer> symbolAliases =
      new HashMap<String, Integer>() {
        {
          put("#", KeyEvent.KEYCODE_POUND);
          put("'", KeyEvent.KEYCODE_APOSTROPHE);
          put("(", KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN);
          put(")", KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN);
          put("*", KeyEvent.KEYCODE_STAR);
          put("+", KeyEvent.KEYCODE_PLUS);
          put(",", KeyEvent.KEYCODE_COMMA);
          put("-", KeyEvent.KEYCODE_MINUS);
          put(".", KeyEvent.KEYCODE_PERIOD);
          put("/", KeyEvent.KEYCODE_SLASH);
          put(";", KeyEvent.KEYCODE_SEMICOLON);
          put("=", KeyEvent.KEYCODE_EQUALS);
          put("@", KeyEvent.KEYCODE_AT);
          put("\\", KeyEvent.KEYCODE_BACKSLASH);
          put("[", KeyEvent.KEYCODE_LEFT_BRACKET);
          put("`", KeyEvent.KEYCODE_GRAVE);
          put("]", KeyEvent.KEYCODE_RIGHT_BRACKET);
        }
      };
}