aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/RichInputConnection.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/RichInputConnection.java')
-rw-r--r--java/src/com/android/inputmethod/latin/RichInputConnection.java66
1 files changed, 57 insertions, 9 deletions
diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java
index 035557610..744b0321a 100644
--- a/java/src/com/android/inputmethod/latin/RichInputConnection.java
+++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java
@@ -30,7 +30,9 @@ import android.view.inputmethod.CorrectionInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
+import android.view.inputmethod.InputMethodManager;
+import com.android.inputmethod.compat.InputConnectionCompatUtils;
import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
import com.android.inputmethod.latin.utils.CapsModeUtils;
import com.android.inputmethod.latin.utils.DebugLogUtils;
@@ -250,7 +252,7 @@ public final class RichInputConnection {
* See {@link InputConnection#commitText(CharSequence, int)}.
*/
public void commitText(final CharSequence text, final int newCursorPosition) {
- commitTextWithBackgroundColor(text, newCursorPosition, Color.TRANSPARENT);
+ commitTextWithBackgroundColor(text, newCursorPosition, Color.TRANSPARENT, text.length());
}
/**
@@ -263,9 +265,11 @@ public final class RichInputConnection {
* the background color. Note that this method specifies {@link BackgroundColorSpan} with
* {@link Spanned#SPAN_COMPOSING} flag, meaning that the background color persists until
* {@link #finishComposingText()} is called.
+ * @param coloredTextLength the length of text, in Java chars, which should be rendered with
+ * the given background color.
*/
public void commitTextWithBackgroundColor(final CharSequence text, final int newCursorPosition,
- final int color) {
+ final int color, final int coloredTextLength) {
if (DEBUG_BATCH_NESTING) checkBatchEdit();
if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
mCommittedTextBeforeComposingText.append(text);
@@ -283,7 +287,8 @@ public final class RichInputConnection {
mTempObjectForCommitText.clear();
mTempObjectForCommitText.append(text);
final BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(color);
- mTempObjectForCommitText.setSpan(backgroundColorSpan, 0, text.length(),
+ final int spanLength = Math.min(coloredTextLength, text.length());
+ mTempObjectForCommitText.setSpan(backgroundColorSpan, 0, spanLength,
Spanned.SPAN_COMPOSING | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mIC.commitText(mTempObjectForCommitText, newCursorPosition);
mLastCommittedTextHasBackgroundColor = true;
@@ -621,14 +626,24 @@ public final class RichInputConnection {
return Arrays.binarySearch(sortedSeparators, code) >= 0;
}
+ private static boolean isPartOfCompositionForScript(final int codePoint,
+ final SpacingAndPunctuations spacingAndPunctuations, final int scriptId) {
+ // We always consider word connectors part of compositions.
+ return spacingAndPunctuations.isWordConnector(codePoint)
+ // Otherwise, it's part of composition if it's part of script and not a separator.
+ || (!spacingAndPunctuations.isWordSeparator(codePoint)
+ && ScriptUtils.isLetterPartOfScript(codePoint, scriptId));
+ }
+
/**
* Returns the text surrounding the cursor.
*
- * @param sortedSeparators a sorted array of code points that split words.
+ * @param spacingAndPunctuations the rules for spacing and punctuation
* @param scriptId the script we consider to be writing words, as one of ScriptUtils.SCRIPT_*
* @return a range containing the text surrounding the cursor
*/
- public TextRange getWordRangeAtCursor(final int[] sortedSeparators, final int scriptId) {
+ public TextRange getWordRangeAtCursor(final SpacingAndPunctuations spacingAndPunctuations,
+ final int scriptId) {
mIC = mParent.getCurrentInputConnection();
if (mIC == null) {
return null;
@@ -645,8 +660,7 @@ public final class RichInputConnection {
int startIndexInBefore = before.length();
while (startIndexInBefore > 0) {
final int codePoint = Character.codePointBefore(before, startIndexInBefore);
- if (isSeparator(codePoint, sortedSeparators)
- || !ScriptUtils.isLetterPartOfScript(codePoint, scriptId)) {
+ if (!isPartOfCompositionForScript(codePoint, spacingAndPunctuations, scriptId)) {
break;
}
--startIndexInBefore;
@@ -659,8 +673,7 @@ public final class RichInputConnection {
int endIndexInAfter = -1;
while (++endIndexInAfter < after.length()) {
final int codePoint = Character.codePointAt(after, endIndexInAfter);
- if (isSeparator(codePoint, sortedSeparators)
- || !ScriptUtils.isLetterPartOfScript(codePoint, scriptId)) {
+ if (!isPartOfCompositionForScript(codePoint, spacingAndPunctuations, scriptId)) {
break;
}
if (Character.isSupplementaryCodePoint(codePoint)) {
@@ -906,4 +919,39 @@ public final class RichInputConnection {
mIC.setSelection(mExpectedSelStart, mExpectedSelEnd);
}
}
+
+ private boolean mCursorAnchorInfoMonitorEnabled = false;
+
+ /**
+ * Requests the editor to call back {@link InputMethodManager#updateCursorAnchorInfo}.
+ * @param enableMonitor {@code true} to request the editor to call back the method whenever the
+ * cursor/anchor position is changed.
+ * @param requestImmediateCallback {@code true} to request the editor to call back the method
+ * as soon as possible to notify the current cursor/anchor position to the input method.
+ * @return {@code true} if the request is accepted. Returns {@code false} otherwise, which
+ * includes "not implemented" or "rejected" or "temporarily unavailable" or whatever which
+ * prevents the application from fulfilling the request. (TODO: Improve the API when it turns
+ * out that we actually need more detailed error codes)
+ */
+ public boolean requestCursorUpdates(final boolean enableMonitor,
+ final boolean requestImmediateCallback) {
+ mIC = mParent.getCurrentInputConnection();
+ final boolean scheduled;
+ if (null != mIC) {
+ scheduled = InputConnectionCompatUtils.requestCursorUpdates(mIC, enableMonitor,
+ requestImmediateCallback);
+ } else {
+ scheduled = false;
+ }
+ mCursorAnchorInfoMonitorEnabled = (scheduled && enableMonitor);
+ return scheduled;
+ }
+
+ /**
+ * @return {@code true} if the application reported that the monitor mode of
+ * {@link InputMethodService#onUpdateCursorAnchorInfo(CursorAnchorInfo)} is currently enabled.
+ */
+ public boolean isCursorAnchorInfoMonitorEnabled() {
+ return mCursorAnchorInfoMonitorEnabled;
+ }
}