diff --git a/app/build.gradle b/app/build.gradle index a5686dd..8aaba36 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { minSdk 19 //noinspection OldTargetApi targetSdk 34 - versionCode 3 + versionCode 30 versionName "0.3" } diff --git a/app/src/main/java/cfpi_fpsi/eu/titanboard/InputMethod.java b/app/src/main/java/cfpi_fpsi/eu/titanboard/InputMethod.java index d44b8aa..28eef4c 100644 --- a/app/src/main/java/cfpi_fpsi/eu/titanboard/InputMethod.java +++ b/app/src/main/java/cfpi_fpsi/eu/titanboard/InputMethod.java @@ -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 symbols = getKeyValues(keyCode); + ArrayList 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 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 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); - } } diff --git a/app/src/main/java/cfpi_fpsi/eu/titanboard/InputView.java b/app/src/main/java/cfpi_fpsi/eu/titanboard/InputView.java index 0752efc..3f73e67 100644 --- a/app/src/main/java/cfpi_fpsi/eu/titanboard/InputView.java +++ b/app/src/main/java/cfpi_fpsi/eu/titanboard/InputView.java @@ -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 symbols, int index) { diff --git a/app/src/main/java/cfpi_fpsi/eu/titanboard/KeymapManager.java b/app/src/main/java/cfpi_fpsi/eu/titanboard/KeymapManager.java index 0763f35..40a1512 100644 --- a/app/src/main/java/cfpi_fpsi/eu/titanboard/KeymapManager.java +++ b/app/src/main/java/cfpi_fpsi/eu/titanboard/KeymapManager.java @@ -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 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 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 getKeyBase(String lang, int keyCode) { - return getKey(lang, keyCode) != null ? getKey(lang, keyCode).getBase() : null; - } - - public ArrayList getKeyShift(String lang, int keyCode) { - return getKey(lang, keyCode) != null ? getKey(lang, keyCode).getShift() : null; - } - - public ArrayList 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 selectedKeymaps) { + if (!selectedKeymaps.contains(mCurrentKeymap.mCode)) { + cycleKeymaps(selectedKeymaps); } } + public void cycleKeymaps(ArrayList 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 { 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 getSymbolsBase(int keyCode) { + return get(keyCode) != null ? get(keyCode).getSymbolsBase() : null; + } + + public ArrayList getSymbolsAlt(int keyCode) { + return get(keyCode) != null ? get(keyCode).getSymbolsAlt() : null; + } + + public ArrayList getSymbolsShift(int keyCode) { + return get(keyCode) != null ? get(keyCode).getSymbolsShift() : null; + } } public static class Key { - private final ArrayList mBase = new ArrayList<>(); - private final ArrayList mShift = new ArrayList<>(); - private final ArrayList mAlt = new ArrayList<>(); + private final ArrayList mSymbolsBase = new ArrayList<>(); + private final ArrayList mSymbolsShift = new ArrayList<>(); + private final ArrayList mSymbolsAlt = new ArrayList<>(); - public ArrayList getBase() { - return mBase; + public ArrayList getSymbolsBase() { + return mSymbolsBase; } - public ArrayList getShift() { - return mShift; + public ArrayList getSymbolsShift() { + return mSymbolsShift; } - public ArrayList getAlt() { - return mAlt; + public ArrayList getSymbolsAlt() { + return mSymbolsAlt; } } } diff --git a/app/src/main/java/cfpi_fpsi/eu/titanboard/Settings.java b/app/src/main/java/cfpi_fpsi/eu/titanboard/Settings.java index 4898aca..0f9f4bd 100644 --- a/app/src/main/java/cfpi_fpsi/eu/titanboard/Settings.java +++ b/app/src/main/java/cfpi_fpsi/eu/titanboard/Settings.java @@ -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))); diff --git a/app/src/main/java/cfpi_fpsi/eu/titanboard/SymbolCycler.java b/app/src/main/java/cfpi_fpsi/eu/titanboard/SymbolCycler.java index 3bab644..1c262a7 100644 --- a/app/src/main/java/cfpi_fpsi/eu/titanboard/SymbolCycler.java +++ b/app/src/main/java/cfpi_fpsi/eu/titanboard/SymbolCycler.java @@ -41,11 +41,13 @@ public class SymbolCycler extends Handler { mRunnable = new SymbolCyclerRunnable(); } - public void startAutomaticCycling(ArrayList symbols, InputConnection conn) { + public void startCycling(ArrayList 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()) { diff --git a/app/src/main/res/values/bools.xml b/app/src/main/res/values/bools.xml index 2666ce9..cdc9cb6 100644 --- a/app/src/main/res/values/bools.xml +++ b/app/src/main/res/values/bools.xml @@ -21,4 +21,5 @@ true true true + false \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2016e2c..c47d3b3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,6 +7,7 @@ Automatic capitalization Automatic periods Autocorrect + Cycle symbols with the space key Keymaps \?\? diff --git a/app/src/main/res/xml/keymaps_titan.xml b/app/src/main/res/xml/keymaps_titan.xml index 24e0cd2..ecdbb53 100644 --- a/app/src/main/res/xml/keymaps_titan.xml +++ b/app/src/main/res/xml/keymaps_titan.xml @@ -43,7 +43,7 @@ - + diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index cce4a1a..dfaf5e7 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -31,6 +31,10 @@ android:defaultValue="@bool/autocorrect_default" android:key="autocorrect" android:title="@string/preferences_auto_correct" /> +