Skip to content

Commit

Permalink
* proper handling of POUND and STAR, when no hotkey function is assi…
Browse files Browse the repository at this point in the history
…gned to them

  * Mode123 types numbers as text again, instead of using key codes

  * removed the InputMode.onOtherKey() functionality, all "other" stuff will be typed as text from now on

  * slightly optimized TraditionalT9.getComposingText() to return faster when there is no text
  • Loading branch information
sspanak committed Jul 28, 2023
1 parent 005683b commit 1f2bc8a
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 102 deletions.
12 changes: 3 additions & 9 deletions src/io/github/sspanak/tt9/ime/KeyPadHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
return Key.isNumber(keyCode)
|| Key.isOK(keyCode)
|| Key.isHotkey(settings, keyCode) || Key.isHotkey(settings, -keyCode)
|| keyCode == KeyEvent.KEYCODE_STAR
|| keyCode == KeyEvent.KEYCODE_POUND
|| (keyCode == KeyEvent.KEYCODE_POUND && onText("#"))
|| (keyCode == KeyEvent.KEYCODE_STAR && onText("*"))
|| ((keyCode == KeyEvent.KEYCODE_DPAD_UP || keyCode == KeyEvent.KEYCODE_DPAD_DOWN) && shouldTrackUpDown())
|| ((keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) && shouldTrackLeftRight());
}
Expand Down Expand Up @@ -170,10 +170,6 @@ public boolean onKeyLongPress(int keyCode, KeyEvent event) {
return onNumber(Key.codeToNumber(settings, keyCode), true, 0);
}

if (Key.isPoundOrStar(keyCode) && onOtherKey(keyCode)) {
return true;
}

ignoreNextKeyUp = 0;
return false;
}
Expand Down Expand Up @@ -229,8 +225,6 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT: return onArrow(keyCode, keyRepeatCounter > 0);
case KeyEvent.KEYCODE_STAR:
case KeyEvent.KEYCODE_POUND: return onOtherKey(keyCode);
}

return false;
Expand Down Expand Up @@ -275,7 +269,7 @@ protected void resetKeyRepeat() {
abstract public boolean onBackspace();
abstract protected boolean onNumber(int key, boolean hold, int repeat);
abstract public boolean onOK();
abstract protected boolean onOtherKey(int key);
abstract public boolean onText(String text);

// customized key handlers
abstract protected boolean onKeyAddWord();
Expand Down
29 changes: 4 additions & 25 deletions src/io/github/sspanak/tt9/ime/TraditionalT9.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,8 @@ public boolean onOK() {
}


public boolean onOtherKey(int keyCode) {
cancelAutoAccept();

String acceptedWord = acceptIncompleteSuggestion();
if (mInputMode.onOtherKey(keyCode)) {
autoCorrectSpace(acceptedWord, false, keyCode);
getSuggestions();
resetKeyRepeat();
return true;
}

return acceptedWord.length() > 0;
}


public boolean onText(String text) {
if (mInputMode.isNumeric() || text.length() == 0) {
if (mInputMode.shouldIgnoreText(text)) {
return false;
}

Expand All @@ -331,10 +316,11 @@ public boolean onText(String text) {
// accept the previously typed word (if any)
autoCorrectSpace(acceptIncompleteSuggestion(), false, -1);

// "type" and accept the text
// "type" and accept the new word
mInputMode.onAcceptSuggestion(text);
textField.setText(text);
autoCorrectSpace(text, true, -1);

return true;
}

Expand Down Expand Up @@ -586,13 +572,6 @@ private void handleSuggestions() {
mInputMode.determineNextWordTextCase(textField.isThereText(), textField.getTextBeforeCursor());
}

// key code "suggestions" take priority over words
if (mInputMode.getKeyCode() > 0) {
sendDownUpKeyEvents(mInputMode.getKeyCode());
mInputMode.reset();
return;
}

// display the word suggestions
setSuggestions(mInputMode.getSuggestions());

Expand Down Expand Up @@ -622,7 +601,7 @@ private void setSuggestions(List<String> suggestions, int selectedIndex) {


private String getComposingText(int maxLength) {
if (maxLength == 0) {
if (maxLength == 0 || !suggestionBar.hasElements()) {
return "";
}

Expand Down
11 changes: 0 additions & 11 deletions src/io/github/sspanak/tt9/ime/helpers/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,6 @@ public static boolean isHotkey(SettingsStore settings, int keyCode) {
}


public static boolean isPoundOrStar(int keyCode) {
return keyCode == KeyEvent.KEYCODE_POUND || keyCode == KeyEvent.KEYCODE_STAR;
}

public static boolean isDecimalSeparator(int keyCode) {
return
keyCode == KeyEvent.KEYCODE_COMMA
|| keyCode == KeyEvent.KEYCODE_NUMPAD_DOT
|| keyCode == KeyEvent.KEYCODE_PERIOD;
}

public static boolean isOK(int keyCode) {
return
keyCode == KeyEvent.KEYCODE_DPAD_CENTER
Expand Down
5 changes: 1 addition & 4 deletions src/io/github/sspanak/tt9/ime/modes/InputMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ abstract public class InputMode {
protected int autoAcceptTimeout = -1;
protected Language language;
protected final ArrayList<String> suggestions = new ArrayList<>();
protected int keyCode = 0;


public static InputMode getInstance(SettingsStore settings, Language language, int mode) {
Expand All @@ -53,7 +52,6 @@ public static InputMode getInstance(SettingsStore settings, Language language, i
// Key handlers. Return "true" when handling the key or "false", when is nothing to do.
public boolean onBackspace() { return false; }
abstract public boolean onNumber(int number, boolean hold, int repeat);
abstract public boolean onOtherKey(int key);

// Suggestions
public void onAcceptSuggestion(@NonNull String word) { onAcceptSuggestion(word, false); }
Expand Down Expand Up @@ -90,7 +88,6 @@ public ArrayList<String> getSuggestions() {
public int getAutoAcceptTimeout() {
return autoAcceptTimeout;
}
public int getKeyCode() { return keyCode; }
public void changeLanguage(Language newLanguage) {
if (newLanguage != null) {
language = newLanguage;
Expand All @@ -102,14 +99,14 @@ public void changeLanguage(Language newLanguage) {
public boolean shouldAcceptPreviousSuggestion(int nextKey) { return false; }
public boolean shouldAddAutoSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually, int nextKey) { return false; }
public boolean shouldDeletePrecedingSpace(InputType inputType) { return false; }
public boolean shouldIgnoreText(String text) { return text == null || text.isEmpty(); }
public boolean shouldSelectNextSuggestion() { return false; }

public boolean shouldTrackUpDown() { return false; }
public boolean shouldTrackLeftRight() { return false; }

public void reset() {
autoAcceptTimeout = -1;
keyCode = 0;
suggestions.clear();
}

Expand Down
40 changes: 24 additions & 16 deletions src/io/github/sspanak/tt9/ime/modes/Mode123.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
package io.github.sspanak.tt9.ime.modes;

import android.view.KeyEvent;

import androidx.annotation.NonNull;

import io.github.sspanak.tt9.ime.helpers.Key;

public class Mode123 extends ModePassthrough {
@Override public int getId() { return MODE_123; }
@Override @NonNull public String toString() { return "123"; }

@Override public final boolean is123() { return true; }
@Override public boolean isPassthrough() { return false; }

@Override
public boolean onNumber(int number, boolean hold, int repeat) {
reset();
keyCode = (number == 0 && hold) ? KeyEvent.KEYCODE_PLUS : Key.numberToCode(number);
return true;
@Override public void reset() {
super.reset();
autoAcceptTimeout = 0;
}

@Override
public boolean onOtherKey(int key) {
@Override public boolean onNumber(int number, boolean hold, int repeat) {
reset();
if (Key.isDecimalSeparator(key) || Key.isPoundOrStar(key)) {
keyCode = key;
return true;
}
suggestions.add((number == 0 && hold) ? "+" : String.valueOf(number));
return true;
}

return false;
/**
* shouldIgnoreText
* Since this is a numeric mode, we allow typing only numbers and:
* 1. In numeric fields, we must allow math chars
* 2. In dialer fields, we must allow various punctuation chars, because they are used as dialing shortcuts
* at least in Japan.
* More info and discussion: <a href="https://github.com/sspanak/tt9/issues/241">issue 241 on Github</a>.
*/
@Override public boolean shouldIgnoreText(String text) {
return
text == null
|| text.length() != 1
|| !(
(text.charAt(0) > 31 && text.charAt(0) < 65)
|| (text.charAt(0) > 90 && text.charAt(0) < 97)
|| (text.charAt(0) > 122 && text.charAt(0) < 127)
);
}
}
16 changes: 0 additions & 16 deletions src/io/github/sspanak/tt9/ime/modes/ModeABC.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class ModeABC extends InputMode {
changeLanguage(lang);
}


@Override
public boolean onNumber(int number, boolean hold, int repeat) {
if (hold) {
Expand All @@ -36,26 +35,11 @@ public boolean onNumber(int number, boolean hold, int repeat) {
return true;
}


@Override
public boolean onOtherKey(int key) {
reset();

if (key > 0) {
keyCode = key;
return true;
}

return false;
}


@Override
protected String adjustSuggestionTextCase(String word, int newTextCase) {
return newTextCase == CASE_UPPER ? word.toUpperCase(language.getLocale()) : word.toLowerCase(language.getLocale());
}


@Override
public void changeLanguage(Language language) {
super.changeLanguage(language);
Expand Down
4 changes: 2 additions & 2 deletions src/io/github/sspanak/tt9/ime/modes/ModePassthrough.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class ModePassthrough extends InputMode {
@Override public boolean isNumeric() { return true; }
@Override public boolean isPassthrough() { return true; }

public boolean onNumber(int number, boolean hold, int repeat) { return false; }
public boolean onOtherKey(int key) { return false; }
@Override public boolean onNumber(int number, boolean hold, int repeat) { return false; }
@Override public boolean shouldIgnoreText(String text) { return true; }
}
14 changes: 0 additions & 14 deletions src/io/github/sspanak/tt9/ime/modes/ModePredictive.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,6 @@ public boolean onNumber(int number, boolean hold, int repeat) {
}


@Override
public boolean onOtherKey(int key) {
reset();

if (key > 0) {
disablePredictions = true;
keyCode = key;
return true;
}

return false;
}


@Override
public void changeLanguage(Language language) {
super.changeLanguage(language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;

import io.github.sspanak.tt9.Logger;
import io.github.sspanak.tt9.R;
Expand All @@ -29,8 +28,8 @@ protected boolean handleHold() {

preventRepeat();
int keyId = getId();
if (keyId == R.id.soft_key_punctuation_1) return tt9.onOtherKey(KeyEvent.KEYCODE_COMMA);
if (keyId == R.id.soft_key_punctuation_2) return tt9.onOtherKey(KeyEvent.KEYCODE_PERIOD);
if (keyId == R.id.soft_key_punctuation_1) return tt9.onText(",");
if (keyId == R.id.soft_key_punctuation_2) return tt9.onText(".");

return false;
}
Expand All @@ -44,8 +43,8 @@ protected boolean handleRelease() {

int keyId = getId();
if (tt9.getInputMode() == InputMode.MODE_123) {
if (keyId == R.id.soft_key_punctuation_1) return tt9.onOtherKey(KeyEvent.KEYCODE_STAR);
if (keyId == R.id.soft_key_punctuation_2) return tt9.onOtherKey(KeyEvent.KEYCODE_POUND);
if (keyId == R.id.soft_key_punctuation_1) return tt9.onText("*");
if (keyId == R.id.soft_key_punctuation_2) return tt9.onText("#");
} else {
if (keyId == R.id.soft_key_punctuation_1) return tt9.onText("!");
if (keyId == R.id.soft_key_punctuation_2) return tt9.onText("?");
Expand Down

0 comments on commit 1f2bc8a

Please sign in to comment.