OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.content.browser.input; |
| 6 |
| 7 import android.text.TextUtils; |
| 8 |
| 9 import java.util.Locale; |
| 10 |
| 11 /** |
| 12 * An immutable class to contain text, selection range, composition range, and w
hether |
| 13 * it's single line or multiple lines that are being edited. |
| 14 */ |
| 15 public class TextInputState { |
| 16 private final CharSequence mText; |
| 17 private final Range mSelection; |
| 18 private final Range mComposition; |
| 19 private final boolean mSingleLine; |
| 20 private final boolean mFromIme; |
| 21 |
| 22 public TextInputState(CharSequence text, Range selection, Range composition,
boolean singleLine, |
| 23 boolean fromIme) { |
| 24 selection.clamp(0, text.length()); |
| 25 if (composition.start() != -1 || composition.end() != -1) { |
| 26 composition.clamp(0, text.length()); |
| 27 } |
| 28 mText = text; |
| 29 mSelection = selection; |
| 30 mComposition = composition; |
| 31 mSingleLine = singleLine; |
| 32 mFromIme = fromIme; |
| 33 } |
| 34 |
| 35 public CharSequence text() { |
| 36 return mText; |
| 37 } |
| 38 |
| 39 public Range selection() { |
| 40 return mSelection; |
| 41 } |
| 42 |
| 43 public Range composition() { |
| 44 return mComposition; |
| 45 } |
| 46 |
| 47 public boolean singleLine() { |
| 48 return mSingleLine; |
| 49 } |
| 50 |
| 51 public boolean fromIme() { |
| 52 return mFromIme; |
| 53 } |
| 54 |
| 55 public CharSequence getSelectedText() { |
| 56 return TextUtils.substring(mText, mSelection.start(), mSelection.end()); |
| 57 } |
| 58 |
| 59 public CharSequence getTextAfterSelection(int maxChars) { |
| 60 return TextUtils.substring( |
| 61 mText, mSelection.end(), Math.min(mText.length(), mSelection.end
() + maxChars)); |
| 62 } |
| 63 |
| 64 public CharSequence getTextBeforeSelection(int maxChars) { |
| 65 return TextUtils.substring( |
| 66 mText, Math.max(0, mSelection.start() - maxChars), mSelection.st
art()); |
| 67 } |
| 68 |
| 69 @Override |
| 70 public boolean equals(Object o) { |
| 71 if (!(o instanceof TextInputState)) return false; |
| 72 TextInputState t = (TextInputState) o; |
| 73 if (t == this) return true; |
| 74 return TextUtils.equals(mText, t.mText) && mSelection.equals(t.mSelectio
n) |
| 75 && mComposition.equals(t.mComposition) && mSingleLine == t.mSing
leLine |
| 76 && mFromIme == t.mFromIme; |
| 77 } |
| 78 |
| 79 @Override |
| 80 public int hashCode() { |
| 81 return mText.hashCode() * 7 + mSelection.hashCode() * 11 + mComposition.
hashCode() * 13 |
| 82 + (mSingleLine ? 19 : 0) + (mFromIme ? 23 : 0); |
| 83 } |
| 84 |
| 85 @SuppressWarnings("unused") |
| 86 public boolean shouldUnblock() { |
| 87 return false; |
| 88 } |
| 89 |
| 90 @Override |
| 91 public String toString() { |
| 92 return String.format(Locale.US, "TextInputState {[%s] SEL%s COM%s %s %s}
", mText, |
| 93 mSelection, mComposition, mSingleLine ? "SIN" : "MUL", |
| 94 mFromIme ? "fromIME" : "NOTfromIME"); |
| 95 } |
| 96 } |
OLD | NEW |