diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 661f2bb..85ee460 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,7 +26,7 @@ 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 714dc16..758b0b1 100644 --- a/app/src/main/java/cfpi_fpsi/eu/titanboard/InputMethod.java +++ b/app/src/main/java/cfpi_fpsi/eu/titanboard/InputMethod.java @@ -22,6 +22,7 @@ package cfpi_fpsi.eu.titanboard; import android.annotation.SuppressLint; import android.inputmethodservice.InputMethodService; import android.os.Vibrator; +import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.EditorInfo; @@ -29,32 +30,33 @@ import android.view.inputmethod.ExtractedText; import android.view.inputmethod.ExtractedTextRequest; import android.view.inputmethod.InputConnection; -import java.util.ArrayDeque; import java.util.ArrayList; -import java.util.Collections; -import java.util.Deque; +import java.util.Set; // TODO: Implement auto-correct. // TODO: Implement auto-caps. // TODO: Implement device recognition and a keymap picking based on it. +/** + * @noinspection deprecation + */ public class InputMethod extends InputMethodService { - private final Deque mLanguageQueue = new ArrayDeque<>(); public MetaKeyManager mMetaKeyManager; public KeymapManager mKeymapManager; public String mCurrentLanguage; private int mKeyCounter = 0; private boolean mRegularKeyPressed = false; private InputView mInputView; + private Settings mSettings; @Override public void onCreate() { super.onCreate(); + mSettings = new Settings(this); Vibrator mVibrator = (Vibrator) getBaseContext().getSystemService(VIBRATOR_SERVICE); mMetaKeyManager = new MetaKeyManager(mVibrator); mKeymapManager = new KeymapManager(getResources().getXml(R.xml.keymaps_titan)); - Collections.addAll(mLanguageQueue, mKeymapManager.getAvailableKeymaps()); - cycleLanguages(); + cycleKeymaps(); } @SuppressLint("InflateParams") @@ -128,9 +130,24 @@ public class InputMethod extends InputMethodService { } } - public void cycleLanguages() { - mCurrentLanguage = mLanguageQueue.pop(); - mLanguageQueue.addLast(mCurrentLanguage); + 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() || mCurrentLanguage == null) { + mCurrentLanguage = "eu"; + return; + } + + int index = selectedLanguages.indexOf(mCurrentLanguage); + if (index == selectedLanguages.size() - 1) { + index = 0; + } else { + ++index; + } + + mCurrentLanguage = selectedLanguages.get(index); } public boolean onKeyUp(int keyCode, KeyEvent event) { @@ -162,6 +179,9 @@ public class InputMethod extends InputMethodService { @Override public void onStartInput(EditorInfo info, boolean restarting) { super.onStartInput(info, restarting); + if (!mSettings.getSelectedKeymaps().contains(mCurrentLanguage)) { + cycleKeymaps(); + } mInputView.updateStateText(); } 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 4fa36f8..95f1c29 100644 --- a/app/src/main/java/cfpi_fpsi/eu/titanboard/InputView.java +++ b/app/src/main/java/cfpi_fpsi/eu/titanboard/InputView.java @@ -20,6 +20,8 @@ package cfpi_fpsi.eu.titanboard; import android.content.Context; +import android.content.Intent; +import android.graphics.Paint; import android.util.AttributeSet; import android.widget.Button; import android.widget.LinearLayout; @@ -43,10 +45,23 @@ public class InputView extends RelativeLayout { super.onFinishInflate(); mMultiList = findViewById(R.id.multi_list); mStateButton = findViewById(R.id.input_state); + mStateButton.setOnClickListener(button -> { - mInputMethod.cycleLanguages(); + MetaKeyManager metaManager = mInputMethod.mMetaKeyManager; + if (metaManager.getAlt().isActive()) { + metaManager.getAlt().resetState(); + } else { + mInputMethod.cycleKeymaps(); + } updateStateText(); }); + + mStateButton.setOnLongClickListener(button -> { + Intent intent = new Intent(getContext(), SettingsActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + getContext().startActivity(intent); + return true; + }); } public void updateStateText() { @@ -54,21 +69,35 @@ public class InputView extends RelativeLayout { KeymapManager keymapManager = mInputMethod.mKeymapManager; String currentLanguage = mInputMethod.mCurrentLanguage; - if (metaManager.getAlt().isPressed() || metaManager.getAlt().isLocked()) { - mStateButton.setText("#!"); + mStateButton.setPaintFlags(mStateButton.getPaintFlags() & ~(Paint.UNDERLINE_TEXT_FLAG)); + + if (metaManager.getAlt().isPressed()) { + mStateButton.setText(R.string.inputview_symbols_locked_state); + return; + } + + if (metaManager.getAlt().isLocked()) { + mStateButton.setText(R.string.inputview_symbols_locked_state); + mStateButton.setPaintFlags(mStateButton.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); return; } if (metaManager.getAlt().isLatched()) { - mStateButton.setText("#?"); + mStateButton.setText(R.string.inputview_symbols_latched_state); return; } - if (metaManager.getShift().isPressed() || metaManager.getShift().isLocked()) { + if (metaManager.getShift().isPressed()) { mStateButton.setText(keymapManager.getKeymap(currentLanguage).getLockedCode()); return; } + if (metaManager.getShift().isLocked()) { + mStateButton.setText(keymapManager.getKeymap(currentLanguage).getLockedCode()); + mStateButton.setPaintFlags(mStateButton.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + return; + } + if (metaManager.getShift().isLatched()) { mStateButton.setText(keymapManager.getKeymap(currentLanguage).getLatchedCode()); return; 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 63a5e98..0763f35 100644 --- a/app/src/main/java/cfpi_fpsi/eu/titanboard/KeymapManager.java +++ b/app/src/main/java/cfpi_fpsi/eu/titanboard/KeymapManager.java @@ -32,7 +32,7 @@ public class KeymapManager { public KeymapManager(XmlPullParser parser) { Keymap keymap = null; int code = 0; - String label = null, name = null, latchedName = null, lockedName = null; + String name = null, latchedName = null, lockedName = null; Key key = null; try { @@ -44,9 +44,6 @@ public class KeymapManager { keymap = new Keymap(); for (int i = 0; i < parser.getAttributeCount(); ++i) { switch (parser.getAttributeName(i)) { - case "label": - label = parser.getAttributeValue(i); - break; case "name": name = parser.getAttributeValue(i); break; @@ -60,8 +57,8 @@ public class KeymapManager { } } else if (type == XmlPullParser.END_TAG && parser.getName().equals("keymap") && keymap != null) { - keymap.setStrings(label, name, latchedName, lockedName); - mKeymaps.put(label, keymap); + keymap.setStrings(name, latchedName, lockedName); + mKeymaps.put(name, keymap); } // We got a new key. @@ -109,10 +106,6 @@ public class KeymapManager { } } - public String[] getAvailableKeymaps() { - return mKeymaps.keySet().toArray(new String[0]); - } - public Keymap getKeymap(String lang) { return mKeymaps.get(lang); } @@ -139,7 +132,6 @@ public class KeymapManager { } public static class Keymap extends HashMap { - private String mLabel; // The full name of the keymap. private String mCode; // A two-letter code for when the keyboard is lowercase. private String mLatchedCode; // Above, but when the keyboard is shift latched. private String mLockedCode; // Above, but when the shift is locked/pressed. @@ -156,12 +148,7 @@ public class KeymapManager { return mLockedCode; } - public String getLabel() { - return mLabel; - } - - public void setStrings(String label, String code, String latched, String locked) { - mLabel = label; + public void setStrings(String code, String latched, String locked) { mCode = code; mLatchedCode = latched; mLockedCode = locked; diff --git a/app/src/main/java/cfpi_fpsi/eu/titanboard/Settings.java b/app/src/main/java/cfpi_fpsi/eu/titanboard/Settings.java new file mode 100644 index 0000000..3c9a3d3 --- /dev/null +++ b/app/src/main/java/cfpi_fpsi/eu/titanboard/Settings.java @@ -0,0 +1,69 @@ +/* + * An input method for the Unihertz Titan/physical keyboard phones + * Copyright (C) 2024 Emil Kosz + * + * 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 . + * + */ + +package cfpi_fpsi.eu.titanboard; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class Settings implements SharedPreferences.OnSharedPreferenceChangeListener { + private final SharedPreferences mPreferences; + private final Map mPreferenceValues; + + public Settings(Context context) { + mPreferences = PreferenceManager.getDefaultSharedPreferences(context); + mPreferences.registerOnSharedPreferenceChangeListener(this); + mPreferenceValues = (Map) mPreferences.getAll(); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + Object value = sharedPreferences.getAll().get(key); + if (value != null) { + mPreferenceValues.put(key, value); + } + } + + public boolean isAutoCapsEnabled() { + return (boolean) mPreferenceValues.get("auto_caps"); + } + + public boolean isAutoPeriodsEnabled() { + return (boolean) mPreferenceValues.get("auto_periods"); + } + + public boolean isAutocorrectEnabled() { + return (boolean) mPreferenceValues.get("auto_correct"); + } + + public int getLongClickSpeed() { + return Integer.parseInt((String) mPreferenceValues.get("long_click_speed")); + } + + public ArrayList getSelectedKeymaps() { + Set selectedKeymaps = (Set) mPreferenceValues.get("selected_keymaps"); + return new ArrayList<>(selectedKeymaps); + } +} diff --git a/app/src/main/java/cfpi_fpsi/eu/titanboard/PreferencesActivity.java b/app/src/main/java/cfpi_fpsi/eu/titanboard/SettingsActivity.java similarity index 71% rename from app/src/main/java/cfpi_fpsi/eu/titanboard/PreferencesActivity.java rename to app/src/main/java/cfpi_fpsi/eu/titanboard/SettingsActivity.java index a3c82c5..d6950c6 100644 --- a/app/src/main/java/cfpi_fpsi/eu/titanboard/PreferencesActivity.java +++ b/app/src/main/java/cfpi_fpsi/eu/titanboard/SettingsActivity.java @@ -19,20 +19,19 @@ package cfpi_fpsi.eu.titanboard; -import android.app.Activity; -import android.content.Context; -import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceActivity; -public class PreferencesActivity extends Activity { - SharedPreferences mPreferences; +import java.util.Objects; +/** + * @noinspection deprecation + */ +public class SettingsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.preferences); - - mPreferences = getBaseContext().getSharedPreferences(getString(R.string.preferences_key), - Context.MODE_PRIVATE); + addPreferencesFromResource(R.xml.preferences); + Objects.requireNonNull(getActionBar()).setDisplayHomeAsUpEnabled(true); } } diff --git a/app/src/main/res/layout/preferences.xml b/app/src/main/res/layout/preferences.xml deleted file mode 100644 index 2dc1aa9..0000000 --- a/app/src/main/res/layout/preferences.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 0000000..425cf8b --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,52 @@ + + + + + European + Phonetic Cyrillic + + + + eu + цp + + + + eu + + + + 200 ms + 300 ms + 400 ms + 500 ms + 750 ms + 1000 ms + + + + 200 + 300 + 400 + 500 + 750 + 1000 + + \ No newline at end of file diff --git a/app/src/main/res/values/bools.xml b/app/src/main/res/values/bools.xml new file mode 100644 index 0000000..2666ce9 --- /dev/null +++ b/app/src/main/res/values/bools.xml @@ -0,0 +1,24 @@ + + + + true + true + true + \ No newline at end of file diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/strings.xml similarity index 59% rename from app/src/main/res/values/values.xml rename to app/src/main/res/values/strings.xml index a2a93d2..dfebda9 100644 --- a/app/src/main/res/values/values.xml +++ b/app/src/main/res/values/strings.xml @@ -2,14 +2,16 @@ Titanboard Titanboard - \?\? - Titanboard preferences - Functions - Languages Long click speed Automatic capitalization - Automatic periods + Automatic periods Autocorrect - cfpi_fpsi.eu.titanboard.prefs + Keymaps + + \?\? + #? + #! + + 400 \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 273bdf6..b736ea3 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -32,30 +32,4 @@ #DDD bold - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/xml/keymaps_titan.xml b/app/src/main/res/xml/keymaps_titan.xml index 22937db..b14d1f1 100644 --- a/app/src/main/res/xml/keymaps_titan.xml +++ b/app/src/main/res/xml/keymaps_titan.xml @@ -23,7 +23,6 @@ Turkish, Hungarian, Swedish, Czech, Portuguese, Catalan, Serbian, Albanian. --> @@ -329,7 +328,6 @@ diff --git a/app/src/main/res/xml/method.xml b/app/src/main/res/xml/method.xml index d178e74..ea034b6 100644 --- a/app/src/main/res/xml/method.xml +++ b/app/src/main/res/xml/method.xml @@ -1,6 +1,6 @@ + + + + + + + + + \ No newline at end of file