Code revamping, InputMethod is now clear of unnecessary methods
This commit is contained in:
+1
-1
@@ -11,7 +11,7 @@ android {
|
||||
minSdk 19
|
||||
//noinspection OldTargetApi
|
||||
targetSdk 34
|
||||
versionCode 3
|
||||
versionCode 30
|
||||
versionName "0.3"
|
||||
}
|
||||
|
||||
|
||||
@@ -34,25 +34,26 @@ import java.util.ArrayList;
|
||||
// TODO: Implement device recognition and a keymap picking based on it.
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class InputMethod extends InputMethodService {
|
||||
public MetaKeyManager mMetaKeyManager;
|
||||
public KeymapManager mKeymapManager;
|
||||
public String mCurrentKeymap;
|
||||
private SymbolCycler mSymbolCycler;
|
||||
private boolean mRegularKeyPressed = false;
|
||||
public InputView mInputView;
|
||||
public Settings mSettings;
|
||||
private SymbolCycler mSymbolCycler;
|
||||
private long mLastKeyTime;
|
||||
private int mRegularKeyPressed = -1;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
mSettings = new Settings(this);
|
||||
Vibrator mVibrator = (Vibrator) getBaseContext().getSystemService(VIBRATOR_SERVICE);
|
||||
Vibrator mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
|
||||
mMetaKeyManager = new MetaKeyManager(mVibrator);
|
||||
mKeymapManager = new KeymapManager(getResources().getXml(R.xml.keymaps_titan));
|
||||
mKeymapManager = new KeymapManager(this);
|
||||
mSymbolCycler = new SymbolCycler(this);
|
||||
cycleKeymaps();
|
||||
mKeymapManager.cycleKeymaps(mSettings.getSelectedKeymaps());
|
||||
}
|
||||
|
||||
@SuppressLint("InflateParams")
|
||||
@@ -92,50 +93,42 @@ public class InputMethod extends InputMethodService {
|
||||
mMetaKeyManager.adjustMetaStateAfterKeypress();
|
||||
return false;
|
||||
case KeyEvent.KEYCODE_SPACE:
|
||||
conn.commitText(" ", 1);
|
||||
mMetaKeyManager.adjustMetaStateAfterKeypress();
|
||||
if (mSettings.isCyclingWithSpaceEnabled() && mRegularKeyPressed != -1
|
||||
&& event.getEventTime() - mLastKeyTime > 200) {
|
||||
mSymbolCycler.cycle();
|
||||
} else {
|
||||
conn.commitText(" ", 1);
|
||||
mMetaKeyManager.adjustMetaStateAfterKeypress();
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
ArrayList<String> symbols = getKeyValues(keyCode);
|
||||
ArrayList<String> symbols = mKeymapManager.getSymbols(keyCode, mMetaKeyManager);
|
||||
|
||||
if (symbols == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
mRegularKeyPressed = true;
|
||||
|
||||
if (event.getRepeatCount() == 0 && !symbols.isEmpty()) {
|
||||
conn.commitText(symbols.get(0), 1);
|
||||
mSymbolCycler.startAutomaticCycling(symbols, conn);
|
||||
mRegularKeyPressed = keyCode;
|
||||
mLastKeyTime = event.getEventTime();
|
||||
mSymbolCycler.stopCycling();
|
||||
mSymbolCycler.startCycling(symbols, conn);
|
||||
if (!mSettings.isCyclingWithSpaceEnabled()) {
|
||||
mSymbolCycler.startAutomaticCycling();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void cycleKeymaps() {
|
||||
// We get the selected keymaps from the settings, find the index of the current language
|
||||
// there, and then select the next keymap.
|
||||
ArrayList<String> selectedLanguages = mSettings.getSelectedKeymaps();
|
||||
|
||||
if (selectedLanguages.isEmpty() || mCurrentKeymap == null) {
|
||||
mCurrentKeymap = getBaseContext().getResources().getString(R.string.keymap_default);
|
||||
return;
|
||||
}
|
||||
|
||||
int index = selectedLanguages.indexOf(mCurrentKeymap);
|
||||
if (index == selectedLanguages.size() - 1) {
|
||||
index = 0;
|
||||
} else {
|
||||
++index;
|
||||
}
|
||||
|
||||
mCurrentKeymap = selectedLanguages.get(index);
|
||||
}
|
||||
|
||||
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
||||
if (mRegularKeyPressed) {
|
||||
if (mRegularKeyPressed == keyCode) {
|
||||
mMetaKeyManager.adjustMetaStateAfterKeypress();
|
||||
mSymbolCycler.stopCycling();
|
||||
mRegularKeyPressed = -1;
|
||||
mInputView.updateSymbolsView(null, 0);
|
||||
}
|
||||
|
||||
InputConnection conn = getCurrentInputConnection();
|
||||
@@ -145,10 +138,6 @@ public class InputMethod extends InputMethodService {
|
||||
}
|
||||
|
||||
conn.finishComposingText();
|
||||
mSymbolCycler.stopCycling();
|
||||
mRegularKeyPressed = false;
|
||||
mInputView.updateSymbolsView(null, 0);
|
||||
|
||||
mMetaKeyManager.updateMetaStateUp(keyCode, event);
|
||||
mInputView.updateStateText();
|
||||
|
||||
@@ -163,11 +152,7 @@ public class InputMethod extends InputMethodService {
|
||||
@Override
|
||||
public void onStartInput(EditorInfo info, boolean restarting) {
|
||||
super.onStartInput(info, restarting);
|
||||
|
||||
if (!mSettings.getSelectedKeymaps().contains(mCurrentKeymap)) {
|
||||
cycleKeymaps();
|
||||
}
|
||||
|
||||
mKeymapManager.checkCurrentKeymap(mSettings.getSelectedKeymaps());
|
||||
mInputView.updateStateText();
|
||||
}
|
||||
|
||||
@@ -176,16 +161,4 @@ public class InputMethod extends InputMethodService {
|
||||
mMetaKeyManager.resetMetaKeyState();
|
||||
mInputView.updateStateText();
|
||||
}
|
||||
|
||||
private ArrayList<String> getKeyValues(int keyCode) {
|
||||
if (mMetaKeyManager.getAlt().isActive()) {
|
||||
return mKeymapManager.getKeyAlt(mCurrentKeymap, keyCode);
|
||||
}
|
||||
|
||||
if (mMetaKeyManager.getShift().isActive()) {
|
||||
return mKeymapManager.getKeyShift(mCurrentKeymap, keyCode);
|
||||
}
|
||||
|
||||
return mKeymapManager.getKeyBase(mCurrentKeymap, keyCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class InputView extends RelativeLayout {
|
||||
if (metaManager.getAlt().isActive()) {
|
||||
metaManager.getAlt().resetState();
|
||||
} else {
|
||||
mInputMethod.cycleKeymaps();
|
||||
mInputMethod.mKeymapManager.cycleKeymaps(mInputMethod.mSettings.getSelectedKeymaps());
|
||||
}
|
||||
updateStateText();
|
||||
});
|
||||
@@ -67,7 +67,6 @@ public class InputView extends RelativeLayout {
|
||||
public void updateStateText() {
|
||||
MetaKeyManager metaManager = mInputMethod.mMetaKeyManager;
|
||||
KeymapManager keymapManager = mInputMethod.mKeymapManager;
|
||||
String currentLanguage = mInputMethod.mCurrentKeymap;
|
||||
|
||||
mStateButton.setPaintFlags(mStateButton.getPaintFlags() & ~(Paint.UNDERLINE_TEXT_FLAG));
|
||||
|
||||
@@ -88,22 +87,22 @@ public class InputView extends RelativeLayout {
|
||||
}
|
||||
|
||||
if (metaManager.getShift().isPressed()) {
|
||||
mStateButton.setText(keymapManager.getKeymap(currentLanguage).getLockedCode());
|
||||
mStateButton.setText(keymapManager.getCurrentKeymap().getLockedCode());
|
||||
return;
|
||||
}
|
||||
|
||||
if (metaManager.getShift().isLocked()) {
|
||||
mStateButton.setText(keymapManager.getKeymap(currentLanguage).getLockedCode());
|
||||
mStateButton.setText(keymapManager.getCurrentKeymap().getLockedCode());
|
||||
mStateButton.setPaintFlags(mStateButton.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
|
||||
return;
|
||||
}
|
||||
|
||||
if (metaManager.getShift().isLatched()) {
|
||||
mStateButton.setText(keymapManager.getKeymap(currentLanguage).getLatchedCode());
|
||||
mStateButton.setText(keymapManager.getCurrentKeymap().getLatchedCode());
|
||||
return;
|
||||
}
|
||||
|
||||
mStateButton.setText(keymapManager.getKeymap(currentLanguage).getCode());
|
||||
mStateButton.setText(keymapManager.getCurrentKeymap().getCode());
|
||||
}
|
||||
|
||||
public void updateSymbolsView(ArrayList<String> symbols, int index) {
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
package cfpi_fpsi.eu.titanboard;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
@@ -27,9 +29,16 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class KeymapManager {
|
||||
private final Context mContext;
|
||||
private final HashMap<String, Keymap> mKeymaps = new HashMap<>();
|
||||
public Keymap mCurrentKeymap;
|
||||
|
||||
public KeymapManager(XmlPullParser parser) {
|
||||
public KeymapManager(Context context) {
|
||||
mContext = context;
|
||||
loadDeviceKeymaps(mContext.getResources().getXml(R.xml.keymaps_titan));
|
||||
}
|
||||
|
||||
private void loadDeviceKeymaps(XmlPullParser parser) {
|
||||
Keymap keymap = null;
|
||||
int code = 0;
|
||||
String name = null, latchedName = null, lockedName = null;
|
||||
@@ -78,7 +87,7 @@ public class KeymapManager {
|
||||
if (type == XmlPullParser.START_TAG && parser.getName().equals("base")) {
|
||||
for (int i = 0; i < parser.getAttributeCount(); ++i) {
|
||||
if (parser.getAttributeName(i).startsWith("value") && key != null) {
|
||||
key.getBase().add(parser.getAttributeValue(i));
|
||||
key.getSymbolsBase().add(parser.getAttributeValue(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,7 +95,7 @@ public class KeymapManager {
|
||||
if (type == XmlPullParser.START_TAG && parser.getName().equals("shift")) {
|
||||
for (int i = 0; i < parser.getAttributeCount(); ++i) {
|
||||
if (parser.getAttributeName(i).startsWith("value") && key != null) {
|
||||
key.getShift().add(parser.getAttributeValue(i));
|
||||
key.getSymbolsShift().add(parser.getAttributeValue(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,7 +103,7 @@ public class KeymapManager {
|
||||
if (type == XmlPullParser.START_TAG && parser.getName().equals("alt")) {
|
||||
for (int i = 0; i < parser.getAttributeCount(); ++i) {
|
||||
if (parser.getAttributeName(i).startsWith("value") && key != null) {
|
||||
key.getAlt().add(parser.getAttributeValue(i));
|
||||
key.getSymbolsAlt().add(parser.getAttributeValue(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,31 +115,42 @@ public class KeymapManager {
|
||||
}
|
||||
}
|
||||
|
||||
public Keymap getKeymap(String lang) {
|
||||
return mKeymaps.get(lang);
|
||||
public ArrayList<String> getSymbols(int keyCode, MetaKeyManager metaKeyManager) {
|
||||
if (metaKeyManager.getAlt().isActive()) {
|
||||
return getCurrentKeymap().getSymbolsAlt(keyCode);
|
||||
}
|
||||
|
||||
if (metaKeyManager.getShift().isActive()) {
|
||||
return getCurrentKeymap().getSymbolsShift(keyCode);
|
||||
}
|
||||
|
||||
return getCurrentKeymap().getSymbolsBase(keyCode);
|
||||
}
|
||||
|
||||
public ArrayList<String> getKeyBase(String lang, int keyCode) {
|
||||
return getKey(lang, keyCode) != null ? getKey(lang, keyCode).getBase() : null;
|
||||
}
|
||||
|
||||
public ArrayList<String> getKeyShift(String lang, int keyCode) {
|
||||
return getKey(lang, keyCode) != null ? getKey(lang, keyCode).getShift() : null;
|
||||
}
|
||||
|
||||
public ArrayList<String> getKeyAlt(String lang, int keyCode) {
|
||||
return getKey(lang, keyCode) != null ? getKey(lang, keyCode).getAlt() : null;
|
||||
}
|
||||
|
||||
public Key getKey(String lang, int keyCode) {
|
||||
Keymap keymap = mKeymaps.get(lang);
|
||||
if (keymap != null) {
|
||||
return keymap.get(keyCode);
|
||||
} else {
|
||||
return null;
|
||||
public void checkCurrentKeymap(ArrayList<String> selectedKeymaps) {
|
||||
if (!selectedKeymaps.contains(mCurrentKeymap.mCode)) {
|
||||
cycleKeymaps(selectedKeymaps);
|
||||
}
|
||||
}
|
||||
|
||||
public void cycleKeymaps(ArrayList<String> selectedKeymaps) {
|
||||
if (selectedKeymaps.isEmpty() || mCurrentKeymap == null) {
|
||||
mCurrentKeymap = mKeymaps.get(mContext.getResources().getString(R.string.keymap_default));
|
||||
return;
|
||||
}
|
||||
|
||||
int index = selectedKeymaps.indexOf(mCurrentKeymap.mCode);
|
||||
index++;
|
||||
|
||||
if (index == selectedKeymaps.size()) {
|
||||
index = 0;
|
||||
}
|
||||
|
||||
mCurrentKeymap = mKeymaps.get(selectedKeymaps.get(index));
|
||||
}
|
||||
|
||||
public Keymap getCurrentKeymap() { return mCurrentKeymap; }
|
||||
|
||||
public static class Keymap extends HashMap<Integer, Key> {
|
||||
private String mCode; // A two-letter code for when the keyboard is lowercase.
|
||||
private String mLatchedCode; // Above, but when the keyboard is shift latched.
|
||||
@@ -153,23 +173,35 @@ public class KeymapManager {
|
||||
mLatchedCode = latched;
|
||||
mLockedCode = locked;
|
||||
}
|
||||
|
||||
public ArrayList<String> getSymbolsBase(int keyCode) {
|
||||
return get(keyCode) != null ? get(keyCode).getSymbolsBase() : null;
|
||||
}
|
||||
|
||||
public ArrayList<String> getSymbolsAlt(int keyCode) {
|
||||
return get(keyCode) != null ? get(keyCode).getSymbolsAlt() : null;
|
||||
}
|
||||
|
||||
public ArrayList<String> getSymbolsShift(int keyCode) {
|
||||
return get(keyCode) != null ? get(keyCode).getSymbolsShift() : null;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Key {
|
||||
private final ArrayList<String> mBase = new ArrayList<>();
|
||||
private final ArrayList<String> mShift = new ArrayList<>();
|
||||
private final ArrayList<String> mAlt = new ArrayList<>();
|
||||
private final ArrayList<String> mSymbolsBase = new ArrayList<>();
|
||||
private final ArrayList<String> mSymbolsShift = new ArrayList<>();
|
||||
private final ArrayList<String> mSymbolsAlt = new ArrayList<>();
|
||||
|
||||
public ArrayList<String> getBase() {
|
||||
return mBase;
|
||||
public ArrayList<String> getSymbolsBase() {
|
||||
return mSymbolsBase;
|
||||
}
|
||||
|
||||
public ArrayList<String> getShift() {
|
||||
return mShift;
|
||||
public ArrayList<String> getSymbolsShift() {
|
||||
return mSymbolsShift;
|
||||
}
|
||||
|
||||
public ArrayList<String> getAlt() {
|
||||
return mAlt;
|
||||
public ArrayList<String> getSymbolsAlt() {
|
||||
return mSymbolsAlt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,11 @@ public class Settings implements SharedPreferences.OnSharedPreferenceChangeListe
|
||||
mContext.getResources().getBoolean(R.bool.autocorrect_default));
|
||||
}
|
||||
|
||||
public boolean isCyclingWithSpaceEnabled() {
|
||||
return (boolean) getOrDefault("cycle_with_space",
|
||||
mContext.getResources().getBoolean(R.bool.cycle_with_space_default));
|
||||
}
|
||||
|
||||
public int getCycleSpeed() {
|
||||
return Integer.parseInt((String) getOrDefault("cycle_speed",
|
||||
mContext.getResources().getString(R.string.cycle_speed_default)));
|
||||
|
||||
@@ -41,11 +41,13 @@ public class SymbolCycler extends Handler {
|
||||
mRunnable = new SymbolCyclerRunnable();
|
||||
}
|
||||
|
||||
public void startAutomaticCycling(ArrayList<String> symbols, InputConnection conn) {
|
||||
public void startCycling(ArrayList<String> symbols, InputConnection conn) {
|
||||
mSymbols = symbols;
|
||||
mConn = conn;
|
||||
mIndex = 0;
|
||||
stopCycling();
|
||||
}
|
||||
|
||||
public void startAutomaticCycling() {
|
||||
postDelayed(mRunnable, mInputMethod.mSettings.getCycleSpeed());
|
||||
}
|
||||
|
||||
@@ -53,6 +55,7 @@ public class SymbolCycler extends Handler {
|
||||
removeCallbacks(mRunnable);
|
||||
}
|
||||
|
||||
|
||||
public void cycle() {
|
||||
mIndex++;
|
||||
if (mIndex == mSymbols.size()) {
|
||||
|
||||
@@ -21,4 +21,5 @@
|
||||
<bool name="auto_caps_default">true</bool>
|
||||
<bool name="auto_periods_default">true</bool>
|
||||
<bool name="autocorrect_default">true</bool>
|
||||
<bool name="cycle_with_space_default">false</bool>
|
||||
</resources>
|
||||
@@ -7,6 +7,7 @@
|
||||
<string name="preferences_auto_caps">Automatic capitalization</string>
|
||||
<string name="preferences_auto_periods">Automatic periods</string>
|
||||
<string name="preferences_auto_correct">Autocorrect</string>
|
||||
<string name="preferences_cycle_with_space">Cycle symbols with the space key</string>
|
||||
<string name="preferences_keymaps">Keymaps</string>
|
||||
|
||||
<string name="inputview_default_state">\?\?</string>
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
<base value="c" />
|
||||
<base value="ç" /> <!-- Used in loanwords, e.g. façade -->
|
||||
<shift value="C" />
|
||||
<base value="Ç" />
|
||||
<shift value="Ç" />
|
||||
<alt value="," />
|
||||
<alt value="&" />
|
||||
</key>
|
||||
|
||||
@@ -31,6 +31,10 @@
|
||||
android:defaultValue="@bool/autocorrect_default"
|
||||
android:key="autocorrect"
|
||||
android:title="@string/preferences_auto_correct" />
|
||||
<SwitchPreference
|
||||
android:defaultValue="@bool/cycle_with_space_default"
|
||||
android:key="cycle_with_space"
|
||||
android:title="@string/preferences_cycle_with_space" />
|
||||
<ListPreference
|
||||
android:defaultValue="@string/cycle_speed_default"
|
||||
android:entries="@array/cycle_speed_labels"
|
||||
|
||||
Reference in New Issue
Block a user