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
|
* 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 LOOKBACK_CHARACTER_NUM = 15;
|
||||||
|
private static final int INVALID_CURSOR_POSITION = -1;
|
||||||
|
|
||||||
private EditingUtils() {
|
private EditingUtils() {
|
||||||
// Unintentional empty constructor for singleton.
|
// Unintentional empty constructor for singleton.
|
||||||
|
@ -63,10 +64,11 @@ public class EditingUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getCursorPosition(InputConnection connection) {
|
private static int getCursorPosition(InputConnection connection) {
|
||||||
|
if (null == connection) return INVALID_CURSOR_POSITION;
|
||||||
ExtractedText extracted = connection.getExtractedText(
|
ExtractedText extracted = connection.getExtractedText(
|
||||||
new ExtractedTextRequest(), 0);
|
new ExtractedTextRequest(), 0);
|
||||||
if (extracted == null) {
|
if (extracted == null) {
|
||||||
return -1;
|
return INVALID_CURSOR_POSITION;
|
||||||
}
|
}
|
||||||
return extracted.startOffset + extracted.selectionStart;
|
return extracted.startOffset + extracted.selectionStart;
|
||||||
}
|
}
|
||||||
|
@ -79,6 +81,7 @@ public class EditingUtils {
|
||||||
* represents the cursor, then "hello " will be returned.
|
* represents the cursor, then "hello " will be returned.
|
||||||
*/
|
*/
|
||||||
public static String getWordAtCursor(InputConnection connection, String separators) {
|
public static String getWordAtCursor(InputConnection connection, String separators) {
|
||||||
|
// getWordRangeAtCursor returns null if the connection is null
|
||||||
Range r = getWordRangeAtCursor(connection, separators);
|
Range r = getWordRangeAtCursor(connection, separators);
|
||||||
return (r == null) ? null : r.mWord;
|
return (r == null) ? null : r.mWord;
|
||||||
}
|
}
|
||||||
|
@ -88,6 +91,7 @@ public class EditingUtils {
|
||||||
* getWordAtCursor.
|
* getWordAtCursor.
|
||||||
*/
|
*/
|
||||||
public static void deleteWordAtCursor(InputConnection connection, String separators) {
|
public static void deleteWordAtCursor(InputConnection connection, String separators) {
|
||||||
|
// getWordRangeAtCursor returns null if the connection is null
|
||||||
Range range = getWordRangeAtCursor(connection, separators);
|
Range range = getWordRangeAtCursor(connection, separators);
|
||||||
if (range == null) return;
|
if (range == null) return;
|
||||||
|
|
||||||
|
@ -165,6 +169,7 @@ public class EditingUtils {
|
||||||
public static CharSequence getPreviousWord(InputConnection connection,
|
public static CharSequence getPreviousWord(InputConnection connection,
|
||||||
String sentenceSeperators) {
|
String sentenceSeperators) {
|
||||||
//TODO: Should fix this. This could be slow!
|
//TODO: Should fix this. This could be slow!
|
||||||
|
if (null == connection) return null;
|
||||||
CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
|
CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
|
||||||
return getPreviousWord(prev, sentenceSeperators);
|
return getPreviousWord(prev, sentenceSeperators);
|
||||||
}
|
}
|
||||||
|
@ -194,6 +199,7 @@ public class EditingUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CharSequence getThisWord(InputConnection connection, String sentenceSeperators) {
|
public static CharSequence getThisWord(InputConnection connection, String sentenceSeperators) {
|
||||||
|
if (null == connection) return null;
|
||||||
final CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
|
final CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
|
||||||
return getThisWord(prev, sentenceSeperators);
|
return getThisWord(prev, sentenceSeperators);
|
||||||
}
|
}
|
||||||
|
@ -256,12 +262,14 @@ public class EditingUtils {
|
||||||
int selStart, int selEnd, String wordSeparators) {
|
int selStart, int selEnd, String wordSeparators) {
|
||||||
if (selStart == selEnd) {
|
if (selStart == selEnd) {
|
||||||
// There is just a cursor, so get the word at the cursor
|
// 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);
|
EditingUtils.Range range = getWordRangeAtCursor(ic, wordSeparators);
|
||||||
if (range != null && !TextUtils.isEmpty(range.mWord)) {
|
if (range != null && !TextUtils.isEmpty(range.mWord)) {
|
||||||
return new SelectedWord(selStart - range.mCharsBefore, selEnd + range.mCharsAfter,
|
return new SelectedWord(selStart - range.mCharsBefore, selEnd + range.mCharsAfter,
|
||||||
range.mWord);
|
range.mWord);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (null == ic) return null;
|
||||||
// Is the previous character empty or a word separator? If not, return null.
|
// Is the previous character empty or a word separator? If not, return null.
|
||||||
CharSequence charsBefore = ic.getTextBeforeCursor(1, 0);
|
CharSequence charsBefore = ic.getTextBeforeCursor(1, 0);
|
||||||
if (!isWordBoundary(charsBefore, wordSeparators)) {
|
if (!isWordBoundary(charsBefore, wordSeparators)) {
|
||||||
|
|
|
@ -1514,9 +1514,15 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
|
||||||
|
|
||||||
final WordComposer wordComposer = mWordComposer;
|
final WordComposer wordComposer = mWordComposer;
|
||||||
// TODO: May need a better way of retrieving previous word
|
// TODO: May need a better way of retrieving previous word
|
||||||
CharSequence prevWord = EditingUtils.getPreviousWord(getCurrentInputConnection(),
|
final InputConnection ic = getCurrentInputConnection();
|
||||||
mSettingsValues.mWordSeparators);
|
final CharSequence prevWord;
|
||||||
SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(
|
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);
|
mKeyboardSwitcher.getKeyboardView(), wordComposer, prevWord);
|
||||||
|
|
||||||
boolean autoCorrectionAvailable = !mInputTypeNoAutoCorrect && mSuggest.hasAutoCorrection();
|
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.
|
// 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
|
// For example "I will, and you too" : we don't want the pair ("will" "and") to be
|
||||||
// a bigram.
|
// a bigram.
|
||||||
CharSequence prevWord = EditingUtils.getPreviousWord(getCurrentInputConnection(),
|
final InputConnection ic = getCurrentInputConnection();
|
||||||
mSettingsValues.mWordSeparators);
|
if (null != ic) {
|
||||||
if (!TextUtils.isEmpty(prevWord)) {
|
final CharSequence prevWord =
|
||||||
mUserBigramDictionary.addBigrams(prevWord.toString(), suggestion.toString());
|
EditingUtils.getPreviousWord(ic, mSettingsValues.mWordSeparators);
|
||||||
|
if (!TextUtils.isEmpty(prevWord)) {
|
||||||
|
mUserBigramDictionary.addBigrams(prevWord.toString(), suggestion.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue