1
0
Fork 0

added Change Keyboard hotkey

This commit is contained in:
Dimo Karaivanov 2023-08-24 16:16:51 +03:00
parent bd675be3a2
commit b7a0db29df
28 changed files with 101 additions and 34 deletions

View file

@ -231,6 +231,10 @@ abstract class KeyPadHandler extends InputMethodService {
return onKeyAddWord(validateOnly);
}
if (keyCode == settings.getKeyChangeKeyboard() * (hold ? -1 : 1)) {
return onKeyChangeKeyboard(validateOnly);
}
if (keyCode == settings.getKeyFilterClear() * (hold ? -1 : 1)) {
return onKeyFilterClear(validateOnly);
}
@ -280,6 +284,7 @@ abstract class KeyPadHandler extends InputMethodService {
// hotkey handlers
abstract protected boolean onKeyAddWord(boolean validateOnly);
abstract protected boolean onKeyChangeKeyboard(boolean validateOnly);
abstract protected boolean onKeyFilterClear(boolean validateOnly);
abstract protected boolean onKeyFilterSuggestions(boolean validateOnly, boolean repeat);
abstract protected boolean onKeyNextLanguage(boolean validateOnly);

View file

@ -373,6 +373,19 @@ public class TraditionalT9 extends KeyPadHandler {
}
public boolean onKeyChangeKeyboard(boolean validateOnly) {
if (!isInputViewShown()) {
return false;
}
if (!validateOnly) {
UI.showChangeKeyboardDialog(this);
}
return true;
}
public boolean onKeyFilterClear(boolean validateOnly) {
if (isSuggestionViewHidden()) {
return false;

View file

@ -23,6 +23,7 @@ public class Key {
public static boolean isHotkey(SettingsStore settings, int keyCode) {
return keyCode == settings.getKeyAddWord()
|| keyCode == settings.getKeyBackspace()
|| keyCode == settings.getKeyChangeKeyboard()
|| keyCode == settings.getKeyFilterClear()
|| keyCode == settings.getKeyFilterSuggestions()
|| keyCode == settings.getKeyPreviousSuggestion()

View file

@ -167,6 +167,7 @@ public class SettingsStore {
public void setDefaultKeys(
int addWord,
int backspace,
int changeKeyboard,
int filterClear,
int filterSuggestions,
int previousSuggestion,
@ -178,6 +179,7 @@ public class SettingsStore {
prefsEditor
.putString(SectionKeymap.ITEM_ADD_WORD, String.valueOf(addWord))
.putString(SectionKeymap.ITEM_BACKSPACE, String.valueOf(backspace))
.putString(SectionKeymap.ITEM_CHANGE_KEYBOARD, String.valueOf(changeKeyboard))
.putString(SectionKeymap.ITEM_FILTER_CLEAR, String.valueOf(filterClear))
.putString(SectionKeymap.ITEM_FILTER_SUGGESTIONS, String.valueOf(filterSuggestions))
.putString(SectionKeymap.ITEM_PREVIOUS_SUGGESTION, String.valueOf(previousSuggestion))
@ -203,6 +205,9 @@ public class SettingsStore {
public int getKeyBackspace() {
return getFunctionKey(SectionKeymap.ITEM_BACKSPACE);
}
public int getKeyChangeKeyboard() {
return getFunctionKey(SectionKeymap.ITEM_CHANGE_KEYBOARD);
}
public int getKeyFilterClear() {
return getFunctionKey(SectionKeymap.ITEM_FILTER_CLEAR);
}

View file

@ -69,6 +69,7 @@ public class Hotkeys {
settings.setDefaultKeys(
KeyEvent.KEYCODE_STAR,
backspace,
0, // "change keyboard" is unassigned by default
clearFilter,
filter,
previousSuggestion,

View file

@ -1,11 +1,9 @@
package io.github.sspanak.tt9.preferences.items;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import androidx.preference.Preference;
import io.github.sspanak.tt9.preferences.PreferencesActivity;
import io.github.sspanak.tt9.ui.UI;
public class ItemSetDefaultGlobalKeyboard extends ItemClickable {
private final PreferencesActivity activity;
@ -17,7 +15,7 @@ public class ItemSetDefaultGlobalKeyboard extends ItemClickable {
@Override
protected boolean onClick(Preference p) {
((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).showInputMethodPicker();
UI.showChangeKeyboardDialog(activity);
return false;
}
}

View file

@ -15,6 +15,7 @@ import io.github.sspanak.tt9.preferences.helpers.Hotkeys;
public class SectionKeymap {
public static final String ITEM_ADD_WORD = "key_add_word";
public static final String ITEM_BACKSPACE = "key_backspace";
public static final String ITEM_CHANGE_KEYBOARD = "key_change_keyboard";
public static final String ITEM_FILTER_CLEAR = "key_filter_clear";
public static final String ITEM_FILTER_SUGGESTIONS = "key_filter_suggestions";
public static final String ITEM_PREVIOUS_SUGGESTION = "key_previous_suggestion";

View file

@ -21,6 +21,7 @@ public class HotkeysScreen extends BaseScreenFragment {
DropDownPreference[] dropDowns = {
findPreference(SectionKeymap.ITEM_ADD_WORD),
findPreference(SectionKeymap.ITEM_BACKSPACE),
findPreference(SectionKeymap.ITEM_CHANGE_KEYBOARD),
findPreference(SectionKeymap.ITEM_FILTER_CLEAR),
findPreference(SectionKeymap.ITEM_FILTER_SUGGESTIONS),
findPreference(SectionKeymap.ITEM_PREVIOUS_SUGGESTION),

View file

@ -4,6 +4,7 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import io.github.sspanak.tt9.ime.TraditionalT9;
@ -20,6 +21,11 @@ public class UI {
}
public static void showChangeKeyboardDialog(Context context) {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showInputMethodPicker();
}
public static void showSettingsScreen(TraditionalT9 tt9) {
Intent prefIntent = new Intent(tt9, PreferencesActivity.class);
prefIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@ -63,17 +69,11 @@ public class UI {
Toast.makeText(context, resourceId, Toast.LENGTH_LONG).show();
}
public static void toastLongFromAsync(Context context, int resourceId) {
if (Looper.myLooper() == null) {
Looper.prepare();
}
toast(context, resourceId);
}
public static void toastLong(Context context, CharSequence msg) {
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
@Deprecated
public static void toastLongFromAsync(Context context, CharSequence msg) {
if (Looper.myLooper() == null) {
Looper.prepare();

View file

@ -3,8 +3,6 @@ package io.github.sspanak.tt9.ui.main.keys;
import android.content.Context;
import android.util.AttributeSet;
import io.github.sspanak.tt9.Logger;
public class SoftBackspaceKey extends SoftKey {
public SoftBackspaceKey(Context context) {
@ -26,12 +24,7 @@ public class SoftBackspaceKey extends SoftKey {
@Override
final protected boolean handleHold() {
if (tt9 == null) {
Logger.w(getClass().getCanonicalName(), "Traditional T9 handler is not set. Ignoring key press.");
return false;
}
return tt9.onBackspace();
return validateTT9Handler() && tt9.onBackspace();
}
@Override

View file

@ -99,8 +99,7 @@ public class SoftKey extends androidx.appcompat.widget.AppCompatButton implement
* Repeatedly calls "handleHold()" upon holding the respective SoftKey, to simulate physical keyboard behavior.
*/
private void repeatOnLongPress() {
if (tt9 == null) {
Logger.w(getClass().getCanonicalName(), "Traditional T9 handler is not set. Ignoring key press.");
if (!validateTT9Handler()) {
hold = false;
return;
}
@ -132,8 +131,7 @@ public class SoftKey extends androidx.appcompat.widget.AppCompatButton implement
}
protected boolean handleRelease() {
if (tt9 == null) {
Logger.w(getClass().getCanonicalName(), "Traditional T9 handler is not set. Ignoring key press.");
if (!validateTT9Handler()) {
return false;
}
@ -145,7 +143,6 @@ public class SoftKey extends androidx.appcompat.widget.AppCompatButton implement
if (keyId == R.id.soft_key_clear_filter) return tt9.onKeyFilterClear(false);
if (keyId == R.id.soft_key_left_arrow) return tt9.onKeyScrollSuggestion(false, true);
if (keyId == R.id.soft_key_right_arrow) return tt9.onKeyScrollSuggestion(false, false);
if (keyId == R.id.soft_key_input_mode) return tt9.onKeyNextInputMode(false);
if (keyId == R.id.soft_key_language) return tt9.onKeyNextLanguage(false);
if (keyId == R.id.soft_key_ok) return tt9.onOK();
if (keyId == R.id.soft_key_settings) return tt9.onKeyShowSettings(false);
@ -153,6 +150,15 @@ public class SoftKey extends androidx.appcompat.widget.AppCompatButton implement
return false;
}
protected boolean validateTT9Handler() {
if (tt9 == null) {
Logger.w(getClass().getCanonicalName(), "Traditional T9 handler is not set. Ignoring key press.");
return false;
}
return true;
}
/**
* getTitle
* Generates the name of the key, for example: "OK", "Backspace", "1", etc...

View file

@ -0,0 +1,30 @@
package io.github.sspanak.tt9.ui.main.keys;
import android.content.Context;
import android.util.AttributeSet;
public class SoftKeyInputMode extends SoftKey {
public SoftKeyInputMode(Context context) {
super(context);
}
public SoftKeyInputMode(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SoftKeyInputMode(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected boolean handleHold() {
preventRepeat();
return validateTT9Handler() && tt9.onKeyChangeKeyboard(false);
}
@Override
protected boolean handleRelease() {
return validateTT9Handler() && tt9.onKeyNextInputMode(false);
}
}

View file

@ -40,13 +40,8 @@ public class SoftNumberKey extends SoftKey {
@Override
protected boolean handleRelease() {
if (tt9 == null) {
Logger.w(getClass().getCanonicalName(), "Traditional T9 handler is not set. Ignoring key press.");
return false;
}
int keyCode = Key.numberToCode(getNumber(getId()));
if (keyCode < 0) {
if (keyCode < 0 || !validateTT9Handler()) {
return false;
}

View file

@ -3,7 +3,6 @@ package io.github.sspanak.tt9.ui.main.keys;
import android.content.Context;
import android.util.AttributeSet;
import io.github.sspanak.tt9.Logger;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.ime.modes.InputMode;
@ -36,8 +35,7 @@ public class SoftPunctuationKey extends SoftKey {
@Override
protected boolean handleRelease() {
if (tt9 == null) {
Logger.w(getClass().getCanonicalName(), "Traditional T9 handler is not set. Ignoring key press.");
if (!validateTT9Handler()) {
return false;
}