1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.view.inputmethod;
import android.annotation.IntRange;
import android.annotation.NonNull;
import java.util.Objects;
/**
* An immutable data snapshot of text editing state.
*/
public final class TextSnapshot {
@NonNull
private final SurroundingText mSurroundingText;
@IntRange(from = -1)
private final int mCompositionStart;
@IntRange(from = -1)
private final int mCompositionEnd;
private final int mCursorCapsMode;
/**
* Creates a new instance of {@link TextSnapshot}
*
* @param surroundingText {@link SurroundingText} of the current edit field.
* @param compositionStart The start index of the composing text.
* {@code -1} if there is no composing text.
* @param compositionEnd The end index of the composing text.
* {@code -1} if there is no composing text.
* @param cursorCapsMode The capitalization mode of the first character being edited in the
* text. See {@link EditorInfo#initialCapsMode}.
* @throws NullPointerException if {@code surroundingText} is {@code null}.
* @throws IllegalArgumentException if {@code compositionStart} and/or {@code compositionEnd}
* is less than {@code -1}.
*/
public TextSnapshot(@NonNull SurroundingText surroundingText,
@IntRange(from = -1) int compositionStart, @IntRange(from = -1) int compositionEnd,
int cursorCapsMode) {
Objects.requireNonNull(surroundingText);
mSurroundingText = surroundingText;
if (compositionStart < -1) {
throw new IllegalArgumentException("compositionStart must be -1 or higher but was "
+ compositionStart);
}
if (compositionEnd < -1) {
throw new IllegalArgumentException("compositionEnd must be -1 or higher but was "
+ compositionEnd);
}
if (compositionStart == -1 && compositionEnd != -1) {
throw new IllegalArgumentException("compositionEnd must be -1 if compositionStart is "
+ "-1 but was " + compositionEnd);
}
if (compositionStart != -1 && compositionEnd == -1) {
throw new IllegalArgumentException("compositionStart must be -1 if compositionEnd is "
+ "-1 but was " + compositionStart);
}
if (compositionStart > compositionEnd) {
throw new IllegalArgumentException("compositionStart=" + compositionStart + " must be"
+ " equal to or greater than compositionEnd=" + compositionEnd);
}
mCompositionStart = compositionStart;
mCompositionEnd = compositionEnd;
mCursorCapsMode = cursorCapsMode;
}
/**
* @return {@link SurroundingText} of the current edit field.
*/
@NonNull
public SurroundingText getSurroundingText() {
return mSurroundingText;
}
/**
* @return The start index of the selection range. {@code -1} if it is not available.
*/
@IntRange(from = -1)
public int getSelectionStart() {
if (mSurroundingText.getOffset() < 0) {
return -1;
}
return mSurroundingText.getSelectionStart() + mSurroundingText.getOffset();
}
/**
* @return The end index of the selection range. {@code -1} if it is not available.
*/
@IntRange(from = -1)
public int getSelectionEnd() {
if (mSurroundingText.getOffset() < 0) {
return -1;
}
return mSurroundingText.getSelectionEnd() + mSurroundingText.getOffset();
}
/**
* @return The end index of the composing text. {@code -1} if there is no composing text.
*/
@IntRange(from = -1)
public int getCompositionStart() {
return mCompositionStart;
}
/**
* @return The end index of the composing text. {@code -1} if there is no composing text.
*/
@IntRange(from = -1)
public int getCompositionEnd() {
return mCompositionEnd;
}
/**
* The capitalization mode of the first character being edited in the text.
*
* <p>Values may be any combination of the following values:</p>
* <ul>
* <li>{@link android.text.TextUtils#CAP_MODE_CHARACTERS TextUtils.CAP_MODE_CHARACTERS}</li>
* <li>{@link android.text.TextUtils#CAP_MODE_WORDS TextUtils.CAP_MODE_WORDS}</li>
* <li>{@link android.text.TextUtils#CAP_MODE_SENTENCES TextUtils.CAP_MODE_SENTENCES}</li>
* </ul>
*
* <p>You should generally just take a non-zero value to mean "start out in caps mode" though.
* </p>
* @see EditorInfo#initialCapsMode
*/
public int getCursorCapsMode() {
return mCursorCapsMode;
}
}
|