Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2795)

Unified Diff: content/public/android/java/src/org/chromium/content/browser/input/TextInputState.java

Issue 1278593004: Introduce ThreadedInputConnection behind a switch (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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;
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698