1
0
Fork 0

More code cleanup (#37)

* fixed the UI not appearing when a text field is re-focused

* cleaned up TraditionalT9.onStartInput() (removed the dependence on the deprecated KeyboardView.OnKeyboardActionListener)
This commit is contained in:
Dimo Karaivanov 2022-08-06 09:31:10 +03:00 committed by GitHub
parent 5d3894e403
commit c19c29912b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,6 @@ package io.github.sspanak.tt9;
import android.content.Intent; import android.content.Intent;
import android.inputmethodservice.InputMethodService; import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.KeyboardView;
import android.os.Handler; import android.os.Handler;
import android.os.SystemClock; import android.os.SystemClock;
import android.text.InputType; import android.text.InputType;
@ -26,7 +25,7 @@ import java.util.AbstractList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TraditionalT9 extends InputMethodService implements KeyboardView.OnKeyboardActionListener { public class TraditionalT9 extends InputMethodService {
private CandidateView mCandidateView; private CandidateView mCandidateView;
private InterfaceHandler interfacehandler = null; private InterfaceHandler interfacehandler = null;
@ -125,7 +124,6 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
*/ */
@Override @Override
public View onCreateInputView() { public View onCreateInputView() {
//updateKeyMode();
View v = getLayoutInflater().inflate(R.layout.mainview, null); View v = getLayoutInflater().inflate(R.layout.mainview, null);
interfacehandler.changeView(v); interfacehandler.changeView(v);
if (mKeyMode == T9Preferences.MODE_PREDICTIVE) { if (mKeyMode == T9Preferences.MODE_PREDICTIVE) {
@ -239,8 +237,6 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
*/ */
@Override @Override
public void onStartInput(EditorInfo inputField, boolean restarting) { public void onStartInput(EditorInfo inputField, boolean restarting) {
super.onStartInput(inputField, restarting);
currentInputConnection = getCurrentInputConnection(); currentInputConnection = getCurrentInputConnection();
// Log.d("T9.onStartInput", "INPUTTYPE: " + inputField.inputType + " FIELDID: " + inputField.fieldId + // Log.d("T9.onStartInput", "INPUTTYPE: " + inputField.inputType + " FIELDID: " + inputField.fieldId +
// " FIELDNAME: " + inputField.fieldName + " PACKAGE NAME: " + inputField.packageName); // " FIELDNAME: " + inputField.fieldName + " PACKAGE NAME: " + inputField.packageName);
@ -254,132 +250,46 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
mEditing = NON_EDIT; mEditing = NON_EDIT;
requestHideSelf(0); requestHideSelf(0);
hideStatusIcon(); hideStatusIcon();
// TODO: verify if need this
// if (interfacehandler != null) {
// interfacehandler.hideView();
// }
return; return;
} }
mFirstPress = true;
mEditing = EDITING;
// Reset our state. We want to do this even if restarting, because // Reset our state. We want to do this even if restarting, because
// the underlying state of the text editor could have changed in any // the underlying state of the text editor could have changed in any
// way. // way.
clearState(); clearState();
// get relevant settings
mLangsAvailable = LangHelper.buildLangs(prefs.getEnabledLanguages()); mLangsAvailable = LangHelper.buildLangs(prefs.getEnabledLanguages());
mLang = sanitizeLang(LANGUAGE.get(prefs.getInputLanguage())); mLang = sanitizeLang(LANGUAGE.get(prefs.getInputLanguage()));
// initialize typing mode
mFirstPress = true;
mEditing = isFilterTextField(inputField) ? EDITING_NOSHOW : EDITING;
mKeyMode = determineInputMode(inputField);
// show or hide UI elements
requestShowSelf(1);
updateCandidates(); updateCandidates();
setSuggestions(null, -1);
//TODO: Check if "restarting" variable will make things faster/more effecient setCandidatesViewShown(false);
mKeyMode = T9Preferences.MODE_ABC;
// We are now going to initialize our state based on the type of
// text being edited.
switch (inputField.inputType & InputType.TYPE_MASK_CLASS) {
case InputType.TYPE_CLASS_NUMBER:
case InputType.TYPE_CLASS_DATETIME:
// Numbers and dates default to the symbols keyboard, with
// no extra features.
mKeyMode = T9Preferences.MODE_123;
break;
case InputType.TYPE_CLASS_PHONE:
// Phones will also default to the symbols keyboard, though
// often you will want to have a dedicated phone keyboard.
mKeyMode = T9Preferences.MODE_123;
break;
case 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).
mKeyMode = prefs.getInputMode();
// We now look for a few special variations of text that will
// modify our behavior.
int variation = inputField.inputType & InputType.TYPE_MASK_VARIATION;
if (variation == InputType.TYPE_TEXT_VARIATION_PASSWORD
|| variation == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
// Do not display predictions / what the user is typing
// when they are entering a password.
mKeyMode = T9Preferences.MODE_ABC;
}
if (variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|| variation == InputType.TYPE_TEXT_VARIATION_URI
|| variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
// Our predictions are not useful for e-mail addresses
// or URIs.
mKeyMode = T9Preferences.MODE_ABC;
}
if ((inputField.inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) {
// If this is an auto-complete text view, then our predictions
// will not be shown and instead we will allow the editor
// to supply their own. We only show the editor's
// candidates when in fullscreen mode, otherwise relying
// own it displaying its own UI.
// ????
mKeyMode = prefs.getInputMode();
}
// handle filter list cases... do not hijack DPAD center and make
// sure back's go through proper
if (variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
mEditing = EDITING_NOSHOW;
}
// We also want to look at the current state of the editor // We also want to look at the current state of the editor
// to decide whether our alphabetic keyboard should start out // to decide whether our alphabetic keyboard should start out
// shifted. // shifted.
updateShiftKeyState(inputField); if (mKeyMode != T9Preferences.MODE_123) {
break; updateTextCase(inputField);
}
default: updateStatusIcon();
Log.d("onStartInput", "defaulting");
// For all unknown input types, default to the alphabetic // handle word adding
// keyboard with no special features. if (inputField.privateImeOptions != null && inputField.privateImeOptions.equals("io.github.sspanak.tt9.addword=true")) {
updateShiftKeyState(inputField);
}
if (inputField.inputType == SpecialInputType.TYPE_SHARP_007H_PHONE_BOOK) {
mKeyMode = T9Preferences.MODE_ABC;
}
if (
inputField.privateImeOptions != null
&& inputField.privateImeOptions.equals("io.github.sspanak.tt9.addword=true")
) {
mAddingWord = true; mAddingWord = true;
// mAddingSkipInput = true;
// Log.d("onStartInput", "ADDING WORD");
mKeyMode = T9Preferences.MODE_ABC;
} else { } else {
mAddingWord = false; restoreLastWordIfAny();
// Log.d("onStartInput", "not adding word");
String prevword = prefs.getLastWord();
if (prevword != "") {
onText(prevword);
prefs.setLastWord("");
} }
} }
// Update the label on the enter key, depending on what the application
// says it will do.
// mCurKeyboard.setImeOptions(getResources(), inputField.imeOptions);
setSuggestions(null, -1);
setCandidatesViewShown(false);
mSuggestionStrings.clear();
mSuggestionInts.clear();
mSuggestionSym.clear();
updateKeyMode();
// show Window()?
}
/** /**
* This is called when the user is done editing a field. We can use this to * This is called when the user is done editing a field. We can use this to
* reset our state. * reset our state.
@ -738,12 +648,12 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
* Helper to update the shift state of our keyboard based on the initial * Helper to update the shift state of our keyboard based on the initial
* editor state. * editor state.
*/ */
private void updateShiftKeyState(EditorInfo attr) { private void updateTextCase(EditorInfo inputField) {
// Log.d("updateShift", "CM start: " + mCapsMode); // Log.d("updateShift", "CM start: " + mCapsMode);
if (attr != null && mCapsMode != T9Preferences.CASE_UPPER) { if (inputField != null && mCapsMode != T9Preferences.CASE_UPPER) {
int caps = 0; int caps = 0;
if (attr.inputType != InputType.TYPE_NULL) { if (inputField.inputType != InputType.TYPE_NULL) {
caps = currentInputConnection.getCursorCapsMode(attr.inputType); caps = currentInputConnection.getCursorCapsMode(inputField.inputType);
} }
// mInputView.setShifted(mCapsLock || caps != 0); // mInputView.setShifted(mCapsLock || caps != 0);
// Log.d("updateShift", "caps: " + caps); // Log.d("updateShift", "caps: " + caps);
@ -756,7 +666,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
} else { } else {
mCapsMode = T9Preferences.CASE_LOWER; mCapsMode = T9Preferences.CASE_LOWER;
} }
updateKeyMode(); updateStatusIcon();
} }
// Log.d("updateShift", "CM end: " + mCapsMode); // Log.d("updateShift", "CM end: " + mCapsMode);
} }
@ -774,8 +684,6 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
currentInputConnection.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), keys, 0, 0)); currentInputConnection.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), keys, 0, 0));
} }
// Implementation of KeyboardViewListener
@Override
public void onKey(int keyCode, int[] keyCodes) { public void onKey(int keyCode, int[] keyCodes) {
// Log.d("OnKey", "pri: " + keyCode); // Log.d("OnKey", "pri: " + keyCode);
// Log.d("onKey", "START Cm: " + mCapsMode); // Log.d("onKey", "START Cm: " + mCapsMode);
@ -814,7 +722,6 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
// Log.d("onKey", "END Cm: " + mCapsMode); // Log.d("onKey", "END Cm: " + mCapsMode);
} }
@Override
public void onText(CharSequence text) { public void onText(CharSequence text) {
if (currentInputConnection == null) if (currentInputConnection == null)
return; return;
@ -824,7 +731,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
} }
currentInputConnection.commitText(text, 1); currentInputConnection.commitText(text, 1);
currentInputConnection.endBatchEdit(); currentInputConnection.endBatchEdit();
updateShiftKeyState(getCurrentInputEditorInfo()); updateTextCase(getCurrentInputEditorInfo());
} }
/** /**
@ -854,6 +761,82 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
} }
} }
/**
* determineInputMode
* Determine the typing mode based on the input field being edited.
*
* @param inputField
* @return T9Preferences.MODE_ABC | T9Preferences.MODE_123 | T9Preferences.MODE_PREDICTIVE
*/
private int determineInputMode(EditorInfo inputField) {
if (inputField.inputType == SpecialInputType.TYPE_SHARP_007H_PHONE_BOOK) {
return T9Preferences.MODE_ABC;
}
if (inputField.privateImeOptions != null && inputField.privateImeOptions.equals("io.github.sspanak.tt9.addword=true")) {
return T9Preferences.MODE_ABC;
}
switch (inputField.inputType & InputType.TYPE_MASK_CLASS) {
case InputType.TYPE_CLASS_NUMBER:
case InputType.TYPE_CLASS_DATETIME:
// Numbers and dates default to the symbols keyboard, with
// no extra features.
case InputType.TYPE_CLASS_PHONE:
// Phones will also default to the symbols keyboard, though
// often you will want to have a dedicated phone keyboard.
return T9Preferences.MODE_123;
case 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).
return isSpecializedTextField(inputField) ? T9Preferences.MODE_ABC : prefs.getInputMode();
default:
// For all unknown input types, default to the alphabetic
// keyboard with no special features.
return T9Preferences.MODE_ABC;
}
}
private boolean isSpecializedTextField(EditorInfo inputField) {
int variation = inputField.inputType & InputType.TYPE_MASK_VARIATION;
return (
variation == InputType.TYPE_TEXT_VARIATION_PASSWORD
|| variation == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
|| variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|| variation == InputType.TYPE_TEXT_VARIATION_URI
|| variation == InputType.TYPE_TEXT_VARIATION_FILTER
);
}
/**
* isFilterTextField
* handle filter list cases... do not hijack DPAD center and make sure back's go through proper
*
* @param inputField
* @return boolean
*/
private boolean isFilterTextField(EditorInfo inputField) {
int inputType = inputField.inputType & InputType.TYPE_MASK_CLASS;
int inputVariation = inputField.inputType & InputType.TYPE_MASK_VARIATION;
return inputType == InputType.TYPE_CLASS_TEXT && inputVariation == InputType.TYPE_TEXT_VARIATION_FILTER;
}
private void restoreLastWordIfAny() {
mAddingWord = false;
String prevword = prefs.getLastWord();
if (prevword != "") {
onText(prevword);
prefs.setLastWord("");
}
}
/** /**
* Update the list of available candidates from the current composing text. * Update the list of available candidates from the current composing text.
* Do a lot of complicated stuffs. * Do a lot of complicated stuffs.
@ -990,10 +973,10 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
mPreviousWord = ""; mPreviousWord = "";
keyDownUp(prefs.getKeyBackspace()); keyDownUp(prefs.getKeyBackspace());
} }
updateShiftKeyState(getCurrentInputEditorInfo()); updateTextCase(getCurrentInputEditorInfo());
// Log.d("handleBS", "Cm: " + mCapsMode); // Log.d("handleBS", "Cm: " + mCapsMode);
// Why do I need to call this twice, android... // Why do I need to call this twice, android...
updateShiftKeyState(getCurrentInputEditorInfo()); updateTextCase(getCurrentInputEditorInfo());
} }
private void handleShift() { private void handleShift() {
@ -1008,7 +991,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
updateCandidates(); updateCandidates();
currentInputConnection.setComposingText(mComposing, 1); currentInputConnection.setComposingText(mComposing, 1);
} }
updateKeyMode(); updateStatusIcon();
} }
/** /**
@ -1054,7 +1037,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
} else { } else {
//Log.d("handleChar", "COMMITING:" + mComposing.toString()); //Log.d("handleChar", "COMMITING:" + mComposing.toString());
commitTyped(); commitTyped();
// updateShiftKeyState(getCurrentInputEditorInfo()); // updateTextCase(getCurrentInputEditorInfo());
newChar = true; newChar = true;
mCharIndex = 0; mCharIndex = 0;
mPrevious = keyCode; mPrevious = keyCode;
@ -1085,7 +1068,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
} }
} }
updateCandidates(); updateCandidates();
updateShiftKeyState(getCurrentInputEditorInfo()); updateTextCase(getCurrentInputEditorInfo());
break; break;
case T9Preferences.MODE_123: case T9Preferences.MODE_123:
@ -1183,7 +1166,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
mCapsMode = T9Preferences.CASE_LOWER; mCapsMode = T9Preferences.CASE_LOWER;
} }
// Log.d("commitReset", "CM pre: " + mCapsMode); // Log.d("commitReset", "CM pre: " + mCapsMode);
updateShiftKeyState(getCurrentInputEditorInfo()); updateTextCase(getCurrentInputEditorInfo());
// Log.d("commitReset", "CM post: " + mCapsMode); // Log.d("commitReset", "CM post: " + mCapsMode);
} }
@ -1205,7 +1188,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
else { else {
mKeyMode++; mKeyMode++;
} }
updateKeyMode(); updateStatusIcon();
resetKeyMode(); resetKeyMode();
} }
@ -1215,7 +1198,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
mLangIndex = 0; mLangIndex = 0;
} }
mLang = mLangsAvailable[mLangIndex]; mLang = mLangsAvailable[mLangIndex];
updateKeyMode(); updateStatusIcon();
} }
private void resetKeyMode() { private void resetKeyMode() {
@ -1232,7 +1215,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
* Set the status icon that is appropriate in current mode (based on * Set the status icon that is appropriate in current mode (based on
* openwmm-legacy) * openwmm-legacy)
*/ */
private void updateKeyMode() { private void updateStatusIcon() {
int icon = 0; int icon = 0;
switch (mKeyMode) { switch (mKeyMode) {
@ -1258,7 +1241,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
} else { } else {
interfacehandler.showHold(true); interfacehandler.showHold(true);
} }
//Log.d("T9.updateKeyMode", "lang: " + mLang + " mKeyMode: " + mKeyMode + " mCapsMode" //Log.d("T9.updateStatusIcon", "lang: " + mLang + " mKeyMode: " + mKeyMode + " mCapsMode"
// + mCapsMode); // + mCapsMode);
icon = LangHelper.ICONMAP[mLang.index][mKeyMode][mCapsMode]; icon = LangHelper.ICONMAP[mLang.index][mKeyMode][mCapsMode];
break; break;
@ -1267,7 +1250,7 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
icon = R.drawable.ime_number; icon = R.drawable.ime_number;
break; break;
default: default:
Log.e("updateKeyMode", "How."); Log.e("updateStatusIcon", "How.");
break; break;
} }
showStatusIcon(icon); showStatusIcon(icon);
@ -1304,36 +1287,4 @@ public class TraditionalT9 extends InputMethodService implements KeyboardView.On
} }
} }
/**
* Ignore this for now.
*/
@Override
public void swipeRight() {
// if (mPredictionOn) {
// pickDefaultCandidate();
// }
}
@Override
public void swipeLeft() {
handleBackspace();
}
@Override
public void swipeDown() {
handleClose();
}
@Override
public void swipeUp() {
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onRelease(int primaryCode) {
}
} }