forked from GitHub/gf-core
the UI for the Android API now has stable word prediction
This commit is contained in:
@@ -18,7 +18,8 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
private CompletionsView mCandidateView;
|
private CompletionsView mCandidateView;
|
||||||
private CompletionInfo[] mCompletions;
|
private CompletionInfo[] mCompletions;
|
||||||
|
|
||||||
private StringBuilder mComposing = new StringBuilder();
|
private StringBuilder mComposingText = new StringBuilder();
|
||||||
|
private StringBuilder mComposingWord = new StringBuilder();
|
||||||
private boolean mPredictionOn;
|
private boolean mPredictionOn;
|
||||||
private boolean mCompletionOn;
|
private boolean mCompletionOn;
|
||||||
private boolean mCapsLock;
|
private boolean mCapsLock;
|
||||||
@@ -78,7 +79,8 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
|
|
||||||
// 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 way.
|
// the underlying state of the text editor could have changed in any way.
|
||||||
mComposing.setLength(0);
|
mComposingText.setLength(0);
|
||||||
|
mComposingWord.setLength(0);
|
||||||
updateCandidates();
|
updateCandidates();
|
||||||
|
|
||||||
if (!restarting) {
|
if (!restarting) {
|
||||||
@@ -182,7 +184,8 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
super.onFinishInput();
|
super.onFinishInput();
|
||||||
|
|
||||||
// Clear current composing text and candidates.
|
// Clear current composing text and candidates.
|
||||||
mComposing.setLength(0);
|
mComposingText.setLength(0);
|
||||||
|
mComposingWord.setLength(0);
|
||||||
updateCandidates();
|
updateCandidates();
|
||||||
|
|
||||||
// We only hide the candidates window when finishing input on
|
// We only hide the candidates window when finishing input on
|
||||||
@@ -217,9 +220,10 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
|
|
||||||
// If the current selection in the text view changes, we should
|
// If the current selection in the text view changes, we should
|
||||||
// clear whatever candidate text we have.
|
// clear whatever candidate text we have.
|
||||||
if (mComposing.length() > 0 && (newSelStart != candidatesEnd
|
if (mComposingText.length() + mComposingWord.length() > 0 &&
|
||||||
|| newSelEnd != candidatesEnd)) {
|
(newSelStart != candidatesEnd || newSelEnd != candidatesEnd)) {
|
||||||
mComposing.setLength(0);
|
mComposingText.setLength(0);
|
||||||
|
mComposingWord.setLength(0);
|
||||||
updateCandidates();
|
updateCandidates();
|
||||||
InputConnection ic = getCurrentInputConnection();
|
InputConnection ic = getCurrentInputConnection();
|
||||||
if (ic != null) {
|
if (ic != null) {
|
||||||
@@ -260,13 +264,13 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
c = c & KeyCharacterMap.COMBINING_ACCENT_MASK;
|
c = c & KeyCharacterMap.COMBINING_ACCENT_MASK;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mComposing.length() > 0) {
|
if (mComposingWord.length() > 0) {
|
||||||
char accent = mComposing.charAt(mComposing.length() -1 );
|
char accent = mComposingWord.charAt(mComposingWord.length()-1);
|
||||||
int composed = KeyEvent.getDeadChar(accent, c);
|
int composed = KeyEvent.getDeadChar(accent, c);
|
||||||
|
|
||||||
if (composed != 0) {
|
if (composed != 0) {
|
||||||
c = composed;
|
c = composed;
|
||||||
mComposing.setLength(mComposing.length()-1);
|
mComposingWord.setLength(mComposingWord.length()-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,7 +298,7 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
// Special handling of the delete key: if we currently are
|
// Special handling of the delete key: if we currently are
|
||||||
// composing text for the user, we want to modify that instead
|
// composing text for the user, we want to modify that instead
|
||||||
// of let the application to the delete itself.
|
// of let the application to the delete itself.
|
||||||
if (mComposing.length() > 0) {
|
if (mComposingText.length() + mComposingWord.length() > 0) {
|
||||||
onKey(TranslatorKeyboard.KEYCODE_DELETE, null);
|
onKey(TranslatorKeyboard.KEYCODE_DELETE, null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -317,9 +321,11 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
* Helper function to commit any text being composed in to the editor.
|
* Helper function to commit any text being composed in to the editor.
|
||||||
*/
|
*/
|
||||||
private void commitTyped(InputConnection inputConnection) {
|
private void commitTyped(InputConnection inputConnection) {
|
||||||
if (mComposing.length() > 0) {
|
if (mComposingText.length() + mComposingWord.length() > 0) {
|
||||||
inputConnection.commitText(mComposing, mComposing.length());
|
String s = getComposingString();
|
||||||
mComposing.setLength(0);
|
inputConnection.commitText(s, s.length());
|
||||||
|
mComposingText.setLength(0);
|
||||||
|
mComposingWord.setLength(0);
|
||||||
updateCandidates();
|
updateCandidates();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -368,7 +374,7 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
mTranslator.setSourceLanguage(newSource);
|
mTranslator.setSourceLanguage(newSource);
|
||||||
handleChangeSourceLanguage(newSource);
|
handleChangeSourceLanguage(newSource);
|
||||||
} else if (primaryCode == TranslatorKeyboard.KEYCODE_TARGET_LANGUAGE) {
|
} else if (primaryCode == TranslatorKeyboard.KEYCODE_TARGET_LANGUAGE) {
|
||||||
String translation = mTranslator.translate(mComposing.toString());
|
String translation = mTranslator.translate(mComposingText.toString());
|
||||||
getCurrentInputConnection().commitText(translation, 1);
|
getCurrentInputConnection().commitText(translation, 1);
|
||||||
return;
|
return;
|
||||||
} else if (primaryCode < TranslatorKeyboard.KEYCODE_TARGET_LANGUAGE &&
|
} else if (primaryCode < TranslatorKeyboard.KEYCODE_TARGET_LANGUAGE &&
|
||||||
@@ -403,8 +409,18 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
getCurrentInputConnection().performEditorAction(mActionId & EditorInfo.IME_MASK_ACTION);
|
getCurrentInputConnection().performEditorAction(mActionId & EditorInfo.IME_MASK_ACTION);
|
||||||
else
|
else
|
||||||
handleCharacter(primaryCode, keyCodes);
|
handleCharacter(primaryCode, keyCodes);
|
||||||
} else if (primaryCode == ' ' && mComposing.length() == 0) {
|
} else if (primaryCode == ' ') {
|
||||||
getCurrentInputConnection().commitText(" ", 1);
|
if (mComposingText.length() + mComposingWord.length() == 0)
|
||||||
|
getCurrentInputConnection().commitText(" ", 1);
|
||||||
|
else if (mComposingWord.length() > 0) {
|
||||||
|
mComposingText.append(mComposingWord);
|
||||||
|
mComposingText.append(' ');
|
||||||
|
mComposingWord.setLength(0);
|
||||||
|
getCurrentInputConnection().setComposingText(getComposingString(), 1);
|
||||||
|
} else {
|
||||||
|
mComposingText.append(' ');
|
||||||
|
getCurrentInputConnection().setComposingText(getComposingString(), 1);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
handleCharacter(primaryCode, keyCodes);
|
handleCharacter(primaryCode, keyCodes);
|
||||||
}
|
}
|
||||||
@@ -414,7 +430,7 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
InputConnection ic = getCurrentInputConnection();
|
InputConnection ic = getCurrentInputConnection();
|
||||||
if (ic == null) return;
|
if (ic == null) return;
|
||||||
ic.beginBatchEdit();
|
ic.beginBatchEdit();
|
||||||
if (mComposing.length() > 0) {
|
if (mComposingText.length() > 0) {
|
||||||
commitTyped(ic);
|
commitTyped(ic);
|
||||||
}
|
}
|
||||||
ic.commitText(text, 0);
|
ic.commitText(text, 0);
|
||||||
@@ -429,9 +445,9 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
*/
|
*/
|
||||||
private void updateCandidates() {
|
private void updateCandidates() {
|
||||||
if (!mCompletionOn) {
|
if (!mCompletionOn) {
|
||||||
if (mComposing.length() > 1) {
|
if (mComposingWord.length() > 1) {
|
||||||
mCompletions =
|
mCompletions =
|
||||||
mTranslator.lookupWordPrefix(mComposing.toString());
|
mTranslator.lookupWordPrefix(mComposingWord.toString());
|
||||||
setSuggestions(mCompletions, true, true);
|
setSuggestions(mCompletions, true, true);
|
||||||
} else {
|
} else {
|
||||||
setSuggestions(null, false, false);
|
setSuggestions(null, false, false);
|
||||||
@@ -452,14 +468,33 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleBackspace() {
|
private void handleBackspace() {
|
||||||
final int length = mComposing.length();
|
int wordLength = mComposingWord.length();
|
||||||
if (length > 1) {
|
int textLength = mComposingText.length();
|
||||||
mComposing.delete(length - 1, length);
|
if (wordLength > 1) {
|
||||||
getCurrentInputConnection().setComposingText(mComposing, 1);
|
mComposingWord.delete(wordLength - 1, wordLength);
|
||||||
|
getCurrentInputConnection().setComposingText(getComposingString(), 1);
|
||||||
updateCandidates();
|
updateCandidates();
|
||||||
} else if (length > 0) {
|
} else if (wordLength > 0) {
|
||||||
mComposing.setLength(0);
|
mComposingWord.setLength(0);
|
||||||
getCurrentInputConnection().commitText("", 0);
|
getCurrentInputConnection().setComposingText(getComposingString(), 1);
|
||||||
|
updateCandidates();
|
||||||
|
} else if (textLength > 0) {
|
||||||
|
if (mComposingText.charAt(textLength - 1) == ' ') {
|
||||||
|
mComposingText.delete(textLength - 1, textLength);
|
||||||
|
getCurrentInputConnection().setComposingText(getComposingString(), 1);
|
||||||
|
} else {
|
||||||
|
mComposingText.delete(textLength - 1, textLength);
|
||||||
|
textLength--;
|
||||||
|
int index = mComposingText.lastIndexOf(" ");
|
||||||
|
if (index == -1) {
|
||||||
|
mComposingWord.append(mComposingText.toString());
|
||||||
|
mComposingText.setLength(0);
|
||||||
|
} else {
|
||||||
|
mComposingWord.append(mComposingText.substring(index+1, textLength));
|
||||||
|
mComposingText.delete(index+1, textLength);
|
||||||
|
}
|
||||||
|
getCurrentInputConnection().setComposingText(getComposingString(), 1);
|
||||||
|
}
|
||||||
updateCandidates();
|
updateCandidates();
|
||||||
} else {
|
} else {
|
||||||
keyDownUp(KeyEvent.KEYCODE_DEL);
|
keyDownUp(KeyEvent.KEYCODE_DEL);
|
||||||
@@ -488,25 +523,29 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
mSymbolsPage1Keyboard.setShifted(false);
|
mSymbolsPage1Keyboard.setShifted(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getComposingString() {
|
||||||
|
return mComposingText.toString() + mComposingWord.toString();
|
||||||
|
}
|
||||||
|
|
||||||
private void handleCharacter(int primaryCode, int[] keyCodes) {
|
private void handleCharacter(int primaryCode, int[] keyCodes) {
|
||||||
if (keyCodes.length > 0 && keyCodes[0] > 0) {
|
if (keyCodes.length > 0 && keyCodes[0] > 0) {
|
||||||
for (int i = 0; i < keyCodes.length && keyCodes[i] > 0; i++) {
|
for (int i = 0; i < keyCodes.length && keyCodes[i] > 0; i++) {
|
||||||
int code = keyCodes[i];
|
int code = keyCodes[i];
|
||||||
if (mInputView.isShifted())
|
if (mInputView.isShifted())
|
||||||
code = Character.toUpperCase(code);
|
code = Character.toUpperCase(code);
|
||||||
mComposing.append((char) code);
|
mComposingWord.append((char) code);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (mInputView.isShifted())
|
if (mInputView.isShifted())
|
||||||
primaryCode = Character.toUpperCase(primaryCode);
|
primaryCode = Character.toUpperCase(primaryCode);
|
||||||
mComposing.append((char) primaryCode);
|
mComposingWord.append((char) primaryCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (primaryCode == 10)
|
if (primaryCode == 10)
|
||||||
commitTyped(getCurrentInputConnection());
|
commitTyped(getCurrentInputConnection());
|
||||||
else
|
else
|
||||||
getCurrentInputConnection().setComposingText(mComposing, 1);
|
getCurrentInputConnection().setComposingText(getComposingString(), 1);
|
||||||
updateShiftKeyState(getCurrentInputEditorInfo());
|
updateShiftKeyState(getCurrentInputEditorInfo());
|
||||||
|
|
||||||
if (mPredictionOn) {
|
if (mPredictionOn) {
|
||||||
@@ -586,8 +625,8 @@ public class TranslatorInputMethodService extends InputMethodService
|
|||||||
if (mCompletionOn)
|
if (mCompletionOn)
|
||||||
getCurrentInputConnection().commitCompletion(ci);
|
getCurrentInputConnection().commitCompletion(ci);
|
||||||
else {
|
else {
|
||||||
mComposing.setLength(0);
|
mComposingWord.setLength(0);
|
||||||
mComposing.append(ci.getText());
|
mComposingWord.append(ci.getText());
|
||||||
commitTyped(getCurrentInputConnection());
|
commitTyped(getCurrentInputConnection());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user