OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 org.chromium.base.Log; |
| 10 |
| 11 /** |
| 12 * A class to track and interact with the latest text input state which |
| 13 * consists of text, selection range, and composition range. |
| 14 */ |
| 15 public class TextInputState { |
| 16 private final CharSequence mText; |
| 17 private final Range mSelection; |
| 18 private final Range mComposition; |
| 19 |
| 20 public TextInputState(CharSequence text, Range selection, Range composition)
{ |
| 21 if (text == null) text = ""; |
| 22 if (selection == null) selection = getDefaultSelection(); |
| 23 if (composition == null) composition = getDefaultComposition(); |
| 24 Log.d("cr.Ime", "onUpdate"); |
| 25 |
| 26 selection.forceWithin(Range.fromText(text)); |
| 27 composition.forceWithin(Range.fromText(text)); |
| 28 mText = text; |
| 29 mSelection = selection; |
| 30 mComposition = composition; |
| 31 } |
| 32 |
| 33 public CharSequence text() { |
| 34 return mText; |
| 35 } |
| 36 |
| 37 public Range selection() { |
| 38 return mSelection; |
| 39 } |
| 40 |
| 41 public Range composition() { |
| 42 return mComposition; |
| 43 } |
| 44 |
| 45 public CharSequence getSelectedText() { |
| 46 return TextUtils.substring(mText, mSelection.start(), mSelection.end()); |
| 47 } |
| 48 |
| 49 public CharSequence getTextAfterSelection(int maxChars) { |
| 50 return TextUtils.substring( |
| 51 mText, mSelection.end(), Math.min(mText.length(), mSelection.end
() + maxChars)); |
| 52 } |
| 53 |
| 54 public CharSequence getTextBeforeSelection(int maxChars) { |
| 55 return TextUtils.substring( |
| 56 mText, Math.max(0, mSelection.start() - maxChars), mSelection.st
art()); |
| 57 } |
| 58 |
| 59 private static Range getDefaultSelection() { |
| 60 return new Range(0, 0); |
| 61 } |
| 62 |
| 63 private static Range getDefaultComposition() { |
| 64 return new Range(-1, -1); |
| 65 } |
| 66 |
| 67 @Override |
| 68 public boolean equals(Object o) { |
| 69 if (!(o instanceof Range)) return false; |
| 70 if (o == this) return true; |
| 71 TextInputState t = (TextInputState) o; |
| 72 return TextUtils.equals(mText, t.mText) && mSelection == t.mSelection |
| 73 && mComposition == t.mComposition; |
| 74 } |
| 75 |
| 76 @Override |
| 77 public int hashCode() { |
| 78 return mText.hashCode() * 7 + mSelection.hashCode() * 11 + mComposition.
hashCode() * 13; |
| 79 } |
| 80 |
| 81 public boolean isDummy() { |
| 82 return false; |
| 83 } |
| 84 |
| 85 static class Dummy extends TextInputState { |
| 86 public Dummy() { |
| 87 super("", null, null); |
| 88 } |
| 89 |
| 90 @Override |
| 91 public boolean isDummy() { |
| 92 return true; |
| 93 } |
| 94 } |
| 95 } |
OLD | NEW |