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 /** |
| 10 * An immutable class to contain text, selection range, composition range, and w
hether |
| 11 * it's single line or multiple lines that are being edited. |
| 12 */ |
| 13 public class TextInputState { |
| 14 private final CharSequence mText; |
| 15 private final Range mSelection; |
| 16 private final Range mComposition; |
| 17 private final boolean mSingleLine; |
| 18 |
| 19 public TextInputState(CharSequence text, Range selection, Range composition, |
| 20 boolean singleLine) { |
| 21 selection.clamp(0, text.length()); |
| 22 if (composition.start() != -1 || composition.end() != -1) { |
| 23 composition.clamp(0, text.length()); |
| 24 } |
| 25 mText = text; |
| 26 mSelection = selection; |
| 27 mComposition = composition; |
| 28 mSingleLine = singleLine; |
| 29 } |
| 30 |
| 31 public CharSequence text() { |
| 32 return mText; |
| 33 } |
| 34 |
| 35 public Range selection() { |
| 36 return mSelection; |
| 37 } |
| 38 |
| 39 public Range composition() { |
| 40 return mComposition; |
| 41 } |
| 42 |
| 43 public boolean singleLine() { |
| 44 return mSingleLine; |
| 45 } |
| 46 |
| 47 public CharSequence getSelectedText() { |
| 48 return TextUtils.substring(mText, mSelection.start(), mSelection.end()); |
| 49 } |
| 50 |
| 51 public CharSequence getTextAfterSelection(int maxChars) { |
| 52 return TextUtils.substring( |
| 53 mText, mSelection.end(), Math.min(mText.length(), mSelection.end
() + maxChars)); |
| 54 } |
| 55 |
| 56 public CharSequence getTextBeforeSelection(int maxChars) { |
| 57 return TextUtils.substring( |
| 58 mText, Math.max(0, mSelection.start() - maxChars), mSelection.st
art()); |
| 59 } |
| 60 |
| 61 @Override |
| 62 public boolean equals(Object o) { |
| 63 if (!(o instanceof TextInputState)) return false; |
| 64 TextInputState t = (TextInputState) o; |
| 65 if (t == this) return true; |
| 66 return TextUtils.equals(mText, t.mText) && mSelection.equals(t.mSelectio
n) |
| 67 && mComposition.equals(t.mComposition) && mSingleLine == t.mSing
leLine; |
| 68 } |
| 69 |
| 70 @Override |
| 71 public int hashCode() { |
| 72 return mText.hashCode() * 7 + mSelection.hashCode() * 11 + mComposition.
hashCode() * 13 |
| 73 + (mSingleLine ? 19 : 0); |
| 74 } |
| 75 |
| 76 public boolean shouldUnblock() { |
| 77 return false; |
| 78 } |
| 79 |
| 80 static class Unblocker extends TextInputState { |
| 81 public Unblocker() { |
| 82 super("", new Range(0, 0), new Range(-1, -1), false); |
| 83 } |
| 84 |
| 85 @Override |
| 86 public boolean shouldUnblock() { |
| 87 return true; |
| 88 } |
| 89 } |
| 90 } |
OLD | NEW |