Fix an NPE related to absent InputConnection
Bug: 5035577 Change-Id: I1a11fc475d4a0f692636000d0b0f40bc35427867
This commit is contained in:
parent
44861474fb
commit
40f7efc172
2 changed files with 25 additions and 8 deletions
|
@ -33,6 +33,7 @@ public class EditingUtils {
|
|||
* Number of characters we want to look back in order to identify the previous word
|
||||
*/
|
||||
private static final int LOOKBACK_CHARACTER_NUM = 15;
|
||||
private static final int INVALID_CURSOR_POSITION = -1;
|
||||
|
||||
private EditingUtils() {
|
||||
// Unintentional empty constructor for singleton.
|
||||
|
@ -63,10 +64,11 @@ public class EditingUtils {
|
|||
}
|
||||
|
||||
private static int getCursorPosition(InputConnection connection) {
|
||||
if (null == connection) return INVALID_CURSOR_POSITION;
|
||||
ExtractedText extracted = connection.getExtractedText(
|
||||
new ExtractedTextRequest(), 0);
|
||||
if (extracted == null) {
|
||||
return -1;
|
||||
return INVALID_CURSOR_POSITION;
|
||||
}
|
||||
return extracted.startOffset + extracted.selectionStart;
|
||||
}
|
||||
|
@ -79,6 +81,7 @@ public class EditingUtils {
|
|||
* represents the cursor, then "hello " will be returned.
|
||||
*/
|
||||
public static String getWordAtCursor(InputConnection connection, String separators) {
|
||||
// getWordRangeAtCursor returns null if the connection is null
|
||||
Range r = getWordRangeAtCursor(connection, separators);
|
||||
return (r == null) ? null : r.mWord;
|
||||
}
|
||||
|
@ -88,6 +91,7 @@ public class EditingUtils {
|
|||
* getWordAtCursor.
|
||||
*/
|
||||
public static void deleteWordAtCursor(InputConnection connection, String separators) {
|
||||
// getWordRangeAtCursor returns null if the connection is null
|
||||
Range range = getWordRangeAtCursor(connection, separators);
|
||||
if (range == null) return;
|
||||
|
||||
|
@ -165,6 +169,7 @@ public class EditingUtils {
|
|||
public static CharSequence getPreviousWord(InputConnection connection,
|
||||
String sentenceSeperators) {
|
||||
//TODO: Should fix this. This could be slow!
|
||||
if (null == connection) return null;
|
||||
CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
|
||||
return getPreviousWord(prev, sentenceSeperators);
|
||||
}
|
||||
|
@ -194,6 +199,7 @@ public class EditingUtils {
|
|||
}
|
||||
|
||||
public static CharSequence getThisWord(InputConnection connection, String sentenceSeperators) {
|
||||
if (null == connection) return null;
|
||||
final CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
|
||||
return getThisWord(prev, sentenceSeperators);
|
||||
}
|
||||
|
@ -256,12 +262,14 @@ public class EditingUtils {
|
|||
int selStart, int selEnd, String wordSeparators) {
|
||||
if (selStart == selEnd) {
|
||||
// There is just a cursor, so get the word at the cursor
|
||||
// getWordRangeAtCursor returns null if the connection is null
|
||||
EditingUtils.Range range = getWordRangeAtCursor(ic, wordSeparators);
|
||||
if (range != null && !TextUtils.isEmpty(range.mWord)) {
|
||||
return new SelectedWord(selStart - range.mCharsBefore, selEnd + range.mCharsAfter,
|
||||
range.mWord);
|
||||
}
|
||||
} else {
|
||||
if (null == ic) return null;
|
||||
// Is the previous character empty or a word separator? If not, return null.
|
||||
CharSequence charsBefore = ic.getTextBeforeCursor(1, 0);
|
||||
if (!isWordBoundary(charsBefore, wordSeparators)) {
|
||||
|
|
|
@ -1514,9 +1514,15 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
|
|||
|
||||
final WordComposer wordComposer = mWordComposer;
|
||||
// TODO: May need a better way of retrieving previous word
|
||||
CharSequence prevWord = EditingUtils.getPreviousWord(getCurrentInputConnection(),
|
||||
mSettingsValues.mWordSeparators);
|
||||
SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(
|
||||
final InputConnection ic = getCurrentInputConnection();
|
||||
final CharSequence prevWord;
|
||||
if (null == ic) {
|
||||
prevWord = null;
|
||||
} else {
|
||||
prevWord = EditingUtils.getPreviousWord(ic, mSettingsValues.mWordSeparators);
|
||||
}
|
||||
// getSuggestedWordBuilder handles gracefully a null value of prevWord
|
||||
final SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(
|
||||
mKeyboardSwitcher.getKeyboardView(), wordComposer, prevWord);
|
||||
|
||||
boolean autoCorrectionAvailable = !mInputTypeNoAutoCorrect && mSuggest.hasAutoCorrection();
|
||||
|
@ -1788,10 +1794,13 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
|
|||
// We don't want to register as bigrams words separated by a separator.
|
||||
// For example "I will, and you too" : we don't want the pair ("will" "and") to be
|
||||
// a bigram.
|
||||
CharSequence prevWord = EditingUtils.getPreviousWord(getCurrentInputConnection(),
|
||||
mSettingsValues.mWordSeparators);
|
||||
if (!TextUtils.isEmpty(prevWord)) {
|
||||
mUserBigramDictionary.addBigrams(prevWord.toString(), suggestion.toString());
|
||||
final InputConnection ic = getCurrentInputConnection();
|
||||
if (null != ic) {
|
||||
final CharSequence prevWord =
|
||||
EditingUtils.getPreviousWord(ic, mSettingsValues.mWordSeparators);
|
||||
if (!TextUtils.isEmpty(prevWord)) {
|
||||
mUserBigramDictionary.addBigrams(prevWord.toString(), suggestion.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue