1
0
Fork 0

code cleanup

This commit is contained in:
sspanak 2024-04-05 18:46:24 +03:00 committed by Dimo Karaivanov
parent 0f0439d0d9
commit dce95c2888
4 changed files with 88 additions and 86 deletions

View file

@ -290,7 +290,7 @@ public abstract class HotkeyHandler extends TypingHandler {
} else {
int nextModeIndex = (allowedInputModes.indexOf(mInputMode.getId()) + 1) % allowedInputModes.size();
mInputMode = InputMode.getInstance(settings, mLanguage, inputType, allowedInputModes.get(nextModeIndex));
mInputMode.setTextFieldCase(textField.determineTextCase(inputType));
mInputMode.setTextFieldCase(inputType.determineTextCase());
mInputMode.determineNextWordTextCase(textField.getStringBeforeCursor());
resetKeyRepeat();

View file

@ -212,7 +212,7 @@ public abstract class TypingHandler extends KeyPadHandler {
*/
private void determineTextCase() {
mInputMode.defaultTextCase();
mInputMode.setTextFieldCase(textField.determineTextCase(inputType));
mInputMode.setTextFieldCase(inputType.determineTextCase());
mInputMode.determineNextWordTextCase(textField.getStringBeforeCursor());
InputModeValidator.validateTextCase(mInputMode, settings.getTextCase());
}
@ -230,7 +230,7 @@ public abstract class TypingHandler extends KeyPadHandler {
return InputMode.MODE_PASSTHROUGH;
}
allowedInputModes = textField.determineInputModes(inputType);
allowedInputModes = inputType.determineInputModes();
return InputModeValidator.validateMode(settings.getInputMode(), allowedInputModes);
}

View file

@ -3,6 +3,10 @@ package io.github.sspanak.tt9.ime.helpers;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import java.util.ArrayList;
import io.github.sspanak.tt9.ime.modes.InputMode;
public class InputType {
private final InputConnection connection;
@ -117,4 +121,85 @@ public class InputType {
private boolean isUri() {
return field != null && (field.inputType & android.text.InputType.TYPE_MASK_VARIATION) == android.text.InputType.TYPE_TEXT_VARIATION_URI;
}
/**
* determineInputModes
* Determine the typing mode based on the input field being edited. Returns an ArrayList of the allowed modes.
*
* @return ArrayList<SettingsStore.MODE_ABC | SettingsStore.MODE_123 | SettingsStore.MODE_PREDICTIVE>
*/
public ArrayList<Integer> determineInputModes() {
ArrayList<Integer> allowedModes = new ArrayList<>();
if (field == null) {
allowedModes.add(InputMode.MODE_123);
return allowedModes;
}
// Calculators (only 0-9 and math) and Dialer (0-9, "#" and "*") fields
// handle all input themselves, so we are supposed to pass through all key presses.
// Note: A Dialer field is not a Phone number field.
if (isSpecialNumeric()) {
allowedModes.add(InputMode.MODE_PASSTHROUGH);
return allowedModes;
}
switch (field.inputType & android.text.InputType.TYPE_MASK_CLASS) {
case android.text.InputType.TYPE_CLASS_NUMBER:
case android.text.InputType.TYPE_CLASS_DATETIME:
case android.text.InputType.TYPE_CLASS_PHONE:
// Numbers, dates and phone numbers default to the numeric keyboard,
// with no extra features.
allowedModes.add(InputMode.MODE_123);
return allowedModes;
case android.text.InputType.TYPE_CLASS_TEXT:
// This is general text editing. We will default to the
// normal alphabetic keyboard, and assume that we should
// be doing predictive text (showing candidates as the
// user types).
if (!isPassword()) {
allowedModes.add(InputMode.MODE_PREDICTIVE);
}
// fallthrough to add ABC and 123 modes
default:
// For all unknown input types, default to the alphabetic
// keyboard with no special features.
allowedModes.add(InputMode.MODE_123);
allowedModes.add(InputMode.MODE_ABC);
return allowedModes;
}
}
/**
* Helper to update the shift state of our keyboard based on the initial
* editor state.
*/
public int determineTextCase() {
if (connection == null || field == null || field.inputType == android.text.InputType.TYPE_NULL) {
return InputMode.CASE_UNDEFINED;
}
if (isSpecialized()) {
return InputMode.CASE_LOWER;
}
if (isPersonName()) {
return InputMode.CASE_CAPITALIZE;
}
switch (field.inputType & android.text.InputType.TYPE_MASK_FLAGS) {
case android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS:
return InputMode.CASE_UPPER;
case android.text.InputType.TYPE_TEXT_FLAG_CAP_WORDS:
return InputMode.CASE_CAPITALIZE;
}
return InputMode.CASE_UNDEFINED;
}
}

View file

@ -12,8 +12,6 @@ import android.view.inputmethod.InputConnection;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import io.github.sspanak.tt9.ime.modes.InputMode;
import io.github.sspanak.tt9.languages.Language;
import io.github.sspanak.tt9.languages.LanguageKind;
@ -51,87 +49,6 @@ public class TextField {
}
/**
* determineInputModes
* Determine the typing mode based on the input field being edited. Returns an ArrayList of the allowed modes.
*
* @return ArrayList<SettingsStore.MODE_ABC | SettingsStore.MODE_123 | SettingsStore.MODE_PREDICTIVE>
*/
public ArrayList<Integer> determineInputModes(InputType inputType) {
ArrayList<Integer> allowedModes = new ArrayList<>();
if (field == null) {
allowedModes.add(InputMode.MODE_123);
return allowedModes;
}
// Calculators (only 0-9 and math) and Dialer (0-9, "#" and "*") fields
// handle all input themselves, so we are supposed to pass through all key presses.
// Note: A Dialer field is not a Phone number field.
if (inputType.isSpecialNumeric()) {
allowedModes.add(InputMode.MODE_PASSTHROUGH);
return allowedModes;
}
switch (field.inputType & android.text.InputType.TYPE_MASK_CLASS) {
case android.text.InputType.TYPE_CLASS_NUMBER:
case android.text.InputType.TYPE_CLASS_DATETIME:
case android.text.InputType.TYPE_CLASS_PHONE:
// Numbers, dates and phone numbers default to the numeric keyboard,
// with no extra features.
allowedModes.add(InputMode.MODE_123);
return allowedModes;
case android.text.InputType.TYPE_CLASS_TEXT:
// This is general text editing. We will default to the
// normal alphabetic keyboard, and assume that we should
// be doing predictive text (showing candidates as the
// user types).
if (!inputType.isPassword()) {
allowedModes.add(InputMode.MODE_PREDICTIVE);
}
// fallthrough to add ABC and 123 modes
default:
// For all unknown input types, default to the alphabetic
// keyboard with no special features.
allowedModes.add(InputMode.MODE_123);
allowedModes.add(InputMode.MODE_ABC);
return allowedModes;
}
}
/**
* Helper to update the shift state of our keyboard based on the initial
* editor state.
*/
public int determineTextCase(InputType inputType) {
if (connection == null || field == null || field.inputType == android.text.InputType.TYPE_NULL) {
return InputMode.CASE_UNDEFINED;
}
if (inputType.isSpecialized()) {
return InputMode.CASE_LOWER;
}
if (inputType.isPersonName()) {
return InputMode.CASE_CAPITALIZE;
}
switch (field.inputType & android.text.InputType.TYPE_MASK_FLAGS) {
case android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS:
return InputMode.CASE_UPPER;
case android.text.InputType.TYPE_TEXT_FLAG_CAP_WORDS:
return InputMode.CASE_CAPITALIZE;
}
return InputMode.CASE_UNDEFINED;
}
public String getStringAfterCursor(int numberOfChars) {
CharSequence character = connection != null ? connection.getTextAfterCursor(numberOfChars, 0) : null;
return character != null ? character.toString() : "";