2010-01-16 20:21:23 +00:00
|
|
|
/*
|
2011-05-20 03:09:57 +00:00
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
2010-01-16 20:21:23 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
package com.android.inputmethod.latin;
|
2010-01-16 20:21:23 +00:00
|
|
|
|
2011-03-29 01:45:49 +00:00
|
|
|
import com.android.inputmethod.compat.InputConnectionCompatUtils;
|
|
|
|
|
2010-08-26 19:22:58 +00:00
|
|
|
import android.text.TextUtils;
|
2010-01-16 20:21:23 +00:00
|
|
|
import android.view.inputmethod.ExtractedText;
|
|
|
|
import android.view.inputmethod.ExtractedTextRequest;
|
|
|
|
import android.view.inputmethod.InputConnection;
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2010-01-16 20:21:23 +00:00
|
|
|
/**
|
|
|
|
* Utility methods to deal with editing text through an InputConnection.
|
|
|
|
*/
|
2010-12-10 06:24:28 +00:00
|
|
|
public class EditingUtils {
|
2010-08-20 05:35:02 +00:00
|
|
|
/**
|
|
|
|
* Number of characters we want to look back in order to identify the previous word
|
|
|
|
*/
|
|
|
|
private static final int LOOKBACK_CHARACTER_NUM = 15;
|
|
|
|
|
2010-12-10 06:24:28 +00:00
|
|
|
private EditingUtils() {
|
|
|
|
// Unintentional empty constructor for singleton.
|
|
|
|
}
|
2010-01-16 20:21:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append newText to the text field represented by connection.
|
|
|
|
* The new text becomes selected.
|
|
|
|
*/
|
|
|
|
public static void appendText(InputConnection connection, String newText) {
|
|
|
|
if (connection == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the composing text
|
|
|
|
connection.finishComposingText();
|
|
|
|
|
|
|
|
// Add a space if the field already has text.
|
2010-12-10 06:24:28 +00:00
|
|
|
String text = newText;
|
2010-01-16 20:21:23 +00:00
|
|
|
CharSequence charBeforeCursor = connection.getTextBeforeCursor(1, 0);
|
|
|
|
if (charBeforeCursor != null
|
|
|
|
&& !charBeforeCursor.equals(" ")
|
|
|
|
&& (charBeforeCursor.length() > 0)) {
|
2010-12-10 06:24:28 +00:00
|
|
|
text = " " + text;
|
2010-01-16 20:21:23 +00:00
|
|
|
}
|
|
|
|
|
2010-12-10 06:24:28 +00:00
|
|
|
connection.setComposingText(text, 1);
|
2010-01-16 20:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int getCursorPosition(InputConnection connection) {
|
|
|
|
ExtractedText extracted = connection.getExtractedText(
|
|
|
|
new ExtractedTextRequest(), 0);
|
|
|
|
if (extracted == null) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return extracted.startOffset + extracted.selectionStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param connection connection to the current text field.
|
2011-04-26 07:28:56 +00:00
|
|
|
* @param separators characters which may separate words
|
2010-01-16 20:21:23 +00:00
|
|
|
* @return the word that surrounds the cursor, including up to one trailing
|
|
|
|
* separator. For example, if the field contains "he|llo world", where |
|
|
|
|
* represents the cursor, then "hello " will be returned.
|
|
|
|
*/
|
2010-12-10 06:24:28 +00:00
|
|
|
public static String getWordAtCursor(InputConnection connection, String separators) {
|
|
|
|
Range r = getWordRangeAtCursor(connection, separators);
|
|
|
|
return (r == null) ? null : r.mWord;
|
2010-01-16 20:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the word surrounding the cursor. Parameters are identical to
|
|
|
|
* getWordAtCursor.
|
|
|
|
*/
|
2010-12-10 06:24:28 +00:00
|
|
|
public static void deleteWordAtCursor(InputConnection connection, String separators) {
|
|
|
|
Range range = getWordRangeAtCursor(connection, separators);
|
2010-01-16 20:21:23 +00:00
|
|
|
if (range == null) return;
|
|
|
|
|
|
|
|
connection.finishComposingText();
|
|
|
|
// Move cursor to beginning of word, to avoid crash when cursor is outside
|
|
|
|
// of valid range after deleting text.
|
2010-12-10 06:24:28 +00:00
|
|
|
int newCursor = getCursorPosition(connection) - range.mCharsBefore;
|
2010-01-16 20:21:23 +00:00
|
|
|
connection.setSelection(newCursor, newCursor);
|
2010-12-10 06:24:28 +00:00
|
|
|
connection.deleteSurroundingText(0, range.mCharsBefore + range.mCharsAfter);
|
2010-01-16 20:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a range of text, relative to the current cursor position.
|
|
|
|
*/
|
2010-08-20 05:35:02 +00:00
|
|
|
public static class Range {
|
2010-01-16 20:21:23 +00:00
|
|
|
/** Characters before selection start */
|
2010-12-10 06:24:28 +00:00
|
|
|
public final int mCharsBefore;
|
2010-01-16 20:21:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Characters after selection start, including one trailing word
|
|
|
|
* separator.
|
|
|
|
*/
|
2010-12-10 06:24:28 +00:00
|
|
|
public final int mCharsAfter;
|
2010-01-16 20:21:23 +00:00
|
|
|
|
|
|
|
/** The actual characters that make up a word */
|
2010-12-10 06:24:28 +00:00
|
|
|
public final String mWord;
|
2010-01-16 20:21:23 +00:00
|
|
|
|
|
|
|
public Range(int charsBefore, int charsAfter, String word) {
|
|
|
|
if (charsBefore < 0 || charsAfter < 0) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
2010-12-10 06:24:28 +00:00
|
|
|
this.mCharsBefore = charsBefore;
|
|
|
|
this.mCharsAfter = charsAfter;
|
|
|
|
this.mWord = word;
|
2010-01-16 20:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-10 06:24:28 +00:00
|
|
|
private static Range getWordRangeAtCursor(InputConnection connection, String sep) {
|
2010-01-16 20:21:23 +00:00
|
|
|
if (connection == null || sep == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
CharSequence before = connection.getTextBeforeCursor(1000, 0);
|
|
|
|
CharSequence after = connection.getTextAfterCursor(1000, 0);
|
|
|
|
if (before == null || after == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find first word separator before the cursor
|
|
|
|
int start = before.length();
|
2010-08-20 05:35:02 +00:00
|
|
|
while (start > 0 && !isWhitespace(before.charAt(start - 1), sep)) start--;
|
2010-01-16 20:21:23 +00:00
|
|
|
|
|
|
|
// Find last word separator after the cursor
|
|
|
|
int end = -1;
|
2010-12-10 06:24:28 +00:00
|
|
|
while (++end < after.length() && !isWhitespace(after.charAt(end), sep)) {
|
|
|
|
// Nothing to do here.
|
|
|
|
}
|
2010-01-16 20:21:23 +00:00
|
|
|
|
|
|
|
int cursor = getCursorPosition(connection);
|
|
|
|
if (start >= 0 && cursor + end <= after.length() + before.length()) {
|
|
|
|
String word = before.toString().substring(start, before.length())
|
2010-08-20 05:35:02 +00:00
|
|
|
+ after.toString().substring(0, end);
|
2010-12-10 06:24:28 +00:00
|
|
|
return new Range(before.length() - start, end, word);
|
2010-01-16 20:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isWhitespace(int code, String whitespace) {
|
|
|
|
return whitespace.contains(String.valueOf((char) code));
|
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
|
|
|
|
private static final Pattern spaceRegex = Pattern.compile("\\s+");
|
|
|
|
|
2011-04-20 02:50:05 +00:00
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
public static CharSequence getPreviousWord(InputConnection connection,
|
|
|
|
String sentenceSeperators) {
|
|
|
|
//TODO: Should fix this. This could be slow!
|
|
|
|
CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
|
2011-04-20 02:50:05 +00:00
|
|
|
return getPreviousWord(prev, sentenceSeperators);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the word before the whitespace preceding the non-whitespace preceding the cursor.
|
|
|
|
// Also, it won't return words that end in a separator.
|
|
|
|
// Example :
|
|
|
|
// "abc def|" -> abc
|
|
|
|
// "abc def |" -> abc
|
|
|
|
// "abc def. |" -> abc
|
|
|
|
// "abc def . |" -> def
|
|
|
|
// "abc|" -> null
|
|
|
|
// "abc |" -> null
|
|
|
|
// "abc. def|" -> null
|
|
|
|
public static CharSequence getPreviousWord(CharSequence prev, String sentenceSeperators) {
|
|
|
|
if (prev == null) return null;
|
2010-08-20 05:35:02 +00:00
|
|
|
String[] w = spaceRegex.split(prev);
|
2011-04-20 02:50:05 +00:00
|
|
|
|
|
|
|
// If we can't find two words, or we found an empty word, return null.
|
|
|
|
if (w.length < 2 || w[w.length - 2].length() <= 0) return null;
|
|
|
|
|
|
|
|
// If ends in a separator, return null
|
|
|
|
char lastChar = w[w.length - 2].charAt(w[w.length - 2].length() - 1);
|
|
|
|
if (sentenceSeperators.contains(String.valueOf(lastChar))) return null;
|
|
|
|
|
|
|
|
return w[w.length - 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static CharSequence getThisWord(InputConnection connection, String sentenceSeperators) {
|
|
|
|
final CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
|
|
|
|
return getThisWord(prev, sentenceSeperators);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the word immediately before the cursor, even if there is whitespace between it and
|
|
|
|
// the cursor - but not if there is punctuation.
|
|
|
|
// Example :
|
|
|
|
// "abc def|" -> def
|
|
|
|
// "abc def |" -> def
|
|
|
|
// "abc def. |" -> null
|
|
|
|
// "abc def . |" -> null
|
|
|
|
public static CharSequence getThisWord(CharSequence prev, String sentenceSeperators) {
|
|
|
|
if (prev == null) return null;
|
|
|
|
String[] w = spaceRegex.split(prev);
|
|
|
|
|
|
|
|
// No word : return null
|
|
|
|
if (w.length < 1 || w[w.length - 1].length() <= 0) return null;
|
|
|
|
|
|
|
|
// If ends in a separator, return null
|
|
|
|
char lastChar = w[w.length - 1].charAt(w[w.length - 1].length() - 1);
|
|
|
|
if (sentenceSeperators.contains(String.valueOf(lastChar))) return null;
|
|
|
|
|
|
|
|
return w[w.length - 1];
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2010-08-26 19:22:58 +00:00
|
|
|
public static class SelectedWord {
|
2010-12-10 06:24:28 +00:00
|
|
|
public final int mStart;
|
|
|
|
public final int mEnd;
|
|
|
|
public final CharSequence mWord;
|
|
|
|
|
|
|
|
public SelectedWord(int start, int end, CharSequence word) {
|
|
|
|
mStart = start;
|
|
|
|
mEnd = end;
|
|
|
|
mWord = word;
|
|
|
|
}
|
2010-08-26 19:22:58 +00:00
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
/**
|
2010-08-26 19:22:58 +00:00
|
|
|
* Takes a character sequence with a single character and checks if the character occurs
|
|
|
|
* in a list of word separators or is empty.
|
|
|
|
* @param singleChar A CharSequence with null, zero or one character
|
|
|
|
* @param wordSeparators A String containing the word separators
|
|
|
|
* @return true if the character is at a word boundary, false otherwise
|
2010-08-20 05:35:02 +00:00
|
|
|
*/
|
2010-08-26 19:22:58 +00:00
|
|
|
private static boolean isWordBoundary(CharSequence singleChar, String wordSeparators) {
|
|
|
|
return TextUtils.isEmpty(singleChar) || wordSeparators.contains(singleChar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the cursor is inside a word or the current selection is a whole word.
|
|
|
|
* @param ic the InputConnection for accessing the text field
|
|
|
|
* @param selStart the start position of the selection within the text field
|
|
|
|
* @param selEnd the end position of the selection within the text field. This could be
|
|
|
|
* the same as selStart, if there's no selection.
|
|
|
|
* @param wordSeparators the word separator characters for the current language
|
|
|
|
* @return an object containing the text and coordinates of the selected/touching word,
|
|
|
|
* null if the selection/cursor is not marking a whole word.
|
|
|
|
*/
|
|
|
|
public static SelectedWord getWordAtCursorOrSelection(final InputConnection ic,
|
|
|
|
int selStart, int selEnd, String wordSeparators) {
|
|
|
|
if (selStart == selEnd) {
|
|
|
|
// There is just a cursor, so get the word at the cursor
|
2010-12-10 06:24:28 +00:00
|
|
|
EditingUtils.Range range = getWordRangeAtCursor(ic, wordSeparators);
|
|
|
|
if (range != null && !TextUtils.isEmpty(range.mWord)) {
|
|
|
|
return new SelectedWord(selStart - range.mCharsBefore, selEnd + range.mCharsAfter,
|
|
|
|
range.mWord);
|
2010-08-26 19:22:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Is the previous character empty or a word separator? If not, return null.
|
|
|
|
CharSequence charsBefore = ic.getTextBeforeCursor(1, 0);
|
|
|
|
if (!isWordBoundary(charsBefore, wordSeparators)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the next character empty or a word separator? If not, return null.
|
|
|
|
CharSequence charsAfter = ic.getTextAfterCursor(1, 0);
|
|
|
|
if (!isWordBoundary(charsAfter, wordSeparators)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the selection alone
|
2011-03-29 01:45:49 +00:00
|
|
|
CharSequence touching = InputConnectionCompatUtils.getSelectedText(
|
|
|
|
ic, selStart, selEnd);
|
2010-08-26 19:22:58 +00:00
|
|
|
if (TextUtils.isEmpty(touching)) return null;
|
|
|
|
// Is any part of the selection a separator? If so, return null.
|
|
|
|
final int length = touching.length();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (wordSeparators.contains(touching.subSequence(i, i + 1))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Prepare the selected word
|
2010-12-10 06:24:28 +00:00
|
|
|
return new SelectedWord(selStart, selEnd, touching);
|
2010-08-26 19:22:58 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2010-01-16 20:21:23 +00:00
|
|
|
}
|