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
/*
 * 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.content.Context;
import java.util.List;
 
/** 管理多個{@link Keyboard 鍵盤} */
class KeyboardSwitch {
 
  private final Context context;
 
  private Keyboard[] mKeyboards;
  private List<String> mKeyboardNames;
  private int currentId, lastId, lastLockId;
  private int currentDisplayWidth;
 
  public KeyboardSwitch(Context context) {
    this.context = context;
    currentId = -1;
    lastId = 0;
    lastLockId = 0;
    reset();
  }
 
  public void reset() {
    mKeyboardNames = Config.get().getKeyboardNames();
    for (int i = 0; i < mKeyboardNames.size(); i++) {
      if (mKeyboardNames.get(i).equals("qwerty")) {
        mKeyboardNames.set(i, "default");
      }
    }
    int n = mKeyboardNames.size();
    mKeyboards = new Keyboard[n];
    for (int i = 0; i < n; i++) {
      mKeyboards[i] = new Keyboard(context, mKeyboardNames.get(i));
    }
    setKeyboard(0);
  }
 
  public void setKeyboard(String name) {
    int i = 0;
    if (isValidId(currentId)) i = currentId;
    if (Function.isEmpty(name)) {
      if (!mKeyboards[i].isLock()) i = lastLockId; //不記憶鍵盤時使用默認鍵盤
    } else if (name.contentEquals(".default")) {
      i = 0;
    } else if (name.contentEquals(".prior")) { //前一個
      i = currentId - 1;
    } else if (name.contentEquals(".next")) { //下一個
      i = currentId + 1;
    } else if (name.contentEquals(".last")) { //最近一個
      i = lastId;
    } else if (name.contentEquals(".last_lock")) { //最近一個Lock鍵盤
      i = lastLockId;
    } else if (name.contentEquals(".ascii")) { //英文鍵盤
      String asciiKeyboard = mKeyboards[i].getAsciiKeyboard();
      if (!Function.isEmpty(asciiKeyboard)) i = mKeyboardNames.indexOf(asciiKeyboard);
    } else {
      i = mKeyboardNames.indexOf(name); //指定鍵盤
    }
    setKeyboard(i);
  }
 
  private boolean isValidId(int i) {
    return i >= 0 && i < mKeyboards.length;
  }
 
  private void setKeyboard(int i) {
    if (!isValidId(i)) i = 0;
    lastId = currentId;
    if (isValidId(lastId)) {
      if (mKeyboards[lastId].isLock()) lastLockId = lastId;
    }
    currentId = i;
  }
 
  public void init(int displayWidth) {
    if ((currentId >= 0) && (displayWidth == currentDisplayWidth)) {
      return;
    }
 
    currentDisplayWidth = displayWidth;
    reset();
  }
 
  public Keyboard getCurrentKeyboard() {
    return mKeyboards[currentId];
  }
 
  public boolean getAsciiMode() {
    return getCurrentKeyboard().getAsciiMode();
  }
}