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..31b2bcbe59354d3f219cce9cf571a5af62d83dad |
--- /dev/null |
+++ b/content/public/android/java/src/org/chromium/content/browser/input/TextInputState.java |
@@ -0,0 +1,92 @@ |
+// 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; |
+ |
+/** |
+ * An immutable class to contain text, selection range, and composition range. |
+ */ |
+public class TextInputState { |
+ private final CharSequence mText; |
+ private final Range mSelection; |
+ private final Range mComposition; |
+ private final boolean mSingleLine; |
+ |
+ public TextInputState(CharSequence text, Range selection, Range composition, |
+ boolean singleLine) { |
+ if (text == null) text = ""; |
aelias_OOO_until_Jul13
2016/01/21 07:43:05
Unblocker seems to be the only use of these == nul
Changwan Ryu
2016/01/22 10:22:16
Done.
|
+ if (selection == null) selection = new Range(0, 0); |
+ if (composition == null) composition = new Range(-1, -1); |
+ |
+ selection.clamp(0, text.length()); |
+ if (composition.start() != -1 || composition.end() != -1) { |
+ composition.clamp(0, text.length()); |
+ } |
+ mText = text; |
+ mSelection = selection; |
+ mComposition = composition; |
+ mSingleLine = singleLine; |
+ } |
+ |
+ public CharSequence text() { |
+ return mText; |
+ } |
+ |
+ public Range selection() { |
+ return mSelection; |
+ } |
+ |
+ public Range composition() { |
+ return mComposition; |
+ } |
+ |
+ public boolean singleLine() { |
+ return mSingleLine; |
+ } |
+ |
+ 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()); |
+ } |
+ |
+ @Override |
+ public boolean equals(Object o) { |
+ if (!(o instanceof TextInputState)) return false; |
+ TextInputState t = (TextInputState) o; |
+ if (t == this) return true; |
+ return TextUtils.equals(mText, t.mText) && mSelection.equals(t.mSelection) |
aelias_OOO_until_Jul13
2016/01/21 07:43:05
Missing mSingleLine.equals(t.mSingleLine)
Changwan Ryu
2016/01/22 10:22:16
Done.
|
+ && mComposition.equals(t.mComposition); |
+ } |
+ |
+ @Override |
+ public int hashCode() { |
+ return mText.hashCode() * 7 + mSelection.hashCode() * 11 + mComposition.hashCode() * 13; |
aelias_OOO_until_Jul13
2016/01/21 07:43:05
Missing mSingleLine hashcode
Changwan Ryu
2016/01/22 10:22:17
Done.
|
+ } |
+ |
+ public boolean shouldUnblock() { |
+ return false; |
+ } |
+ |
+ static class Unblocker extends TextInputState { |
+ public Unblocker() { |
+ super("", null, null, false); |
+ } |
+ |
+ @Override |
+ public boolean shouldUnblock() { |
+ return true; |
+ } |
+ } |
+} |