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