Index: content/public/android/java/src/org/chromium/content/browser/input/TextInputState.java |
diff --git a/content/public/android/java/src/org/chromium/content/browser/input/TextInputState.java b/content/public/android/java/src/org/chromium/content/browser/input/TextInputState.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9d702a3a7ca743d0aa8be95e83d6f699d0389a8e |
--- /dev/null |
+++ b/content/public/android/java/src/org/chromium/content/browser/input/TextInputState.java |
@@ -0,0 +1,95 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.content.browser.input; |
+ |
+import android.text.TextUtils; |
+ |
+import org.chromium.base.Log; |
+ |
+/** |
+ * A class to track and interact with the latest text input state which |
+ * consists of text, selection range, and composition range. |
+ */ |
+public class TextInputState { |
+ private final CharSequence mText; |
+ private final Range mSelection; |
+ private final Range mComposition; |
+ |
+ public TextInputState(CharSequence text, Range selection, Range composition) { |
+ if (text == null) text = ""; |
+ if (selection == null) selection = getDefaultSelection(); |
+ if (composition == null) composition = getDefaultComposition(); |
+ Log.d("cr.Ime", "onUpdate"); |
+ |
+ selection.forceWithin(Range.fromText(text)); |
+ composition.forceWithin(Range.fromText(text)); |
+ mText = text; |
+ mSelection = selection; |
+ mComposition = composition; |
+ } |
+ |
+ public CharSequence text() { |
+ return mText; |
+ } |
+ |
+ public Range selection() { |
+ return mSelection; |
+ } |
+ |
+ public Range composition() { |
+ return mComposition; |
+ } |
+ |
+ public CharSequence getSelectedText() { |
+ return TextUtils.substring(mText, mSelection.start(), mSelection.end()); |
+ } |
+ |
+ public CharSequence getTextAfterSelection(int maxChars) { |
+ return TextUtils.substring( |
+ mText, mSelection.end(), Math.min(mText.length(), mSelection.end() + maxChars)); |
+ } |
+ |
+ public CharSequence getTextBeforeSelection(int maxChars) { |
+ return TextUtils.substring( |
+ mText, Math.max(0, mSelection.start() - maxChars), mSelection.start()); |
+ } |
+ |
+ private static Range getDefaultSelection() { |
+ return new Range(0, 0); |
+ } |
+ |
+ private static Range getDefaultComposition() { |
+ return new Range(-1, -1); |
+ } |
+ |
+ @Override |
+ public boolean equals(Object o) { |
+ if (!(o instanceof Range)) return false; |
+ if (o == this) return true; |
+ TextInputState t = (TextInputState) o; |
+ return TextUtils.equals(mText, t.mText) && mSelection == t.mSelection |
+ && mComposition == t.mComposition; |
+ } |
+ |
+ @Override |
+ public int hashCode() { |
+ return mText.hashCode() * 7 + mSelection.hashCode() * 11 + mComposition.hashCode() * 13; |
+ } |
+ |
+ public boolean isDummy() { |
+ return false; |
+ } |
+ |
+ static class Dummy extends TextInputState { |
+ public Dummy() { |
+ super("", null, null); |
+ } |
+ |
+ @Override |
+ public boolean isDummy() { |
+ return true; |
+ } |
+ } |
+} |