use of android.text.Editable in project platform_frameworks_base by android.
the class BaseInputConnection method replaceText.
private void replaceText(CharSequence text, int newCursorPosition, boolean composing) {
final Editable content = getEditable();
if (content == null) {
return;
}
beginBatchEdit();
// delete composing text set previously.
int a = getComposingSpanStart(content);
int b = getComposingSpanEnd(content);
if (DEBUG)
Log.v(TAG, "Composing span: " + a + " to " + b);
if (b < a) {
int tmp = a;
a = b;
b = tmp;
}
if (a != -1 && b != -1) {
removeComposingSpans(content);
} else {
a = Selection.getSelectionStart(content);
b = Selection.getSelectionEnd(content);
if (a < 0)
a = 0;
if (b < 0)
b = 0;
if (b < a) {
int tmp = a;
a = b;
b = tmp;
}
}
if (composing) {
Spannable sp = null;
if (!(text instanceof Spannable)) {
sp = new SpannableStringBuilder(text);
text = sp;
ensureDefaultComposingSpans();
if (mDefaultComposingSpans != null) {
for (int i = 0; i < mDefaultComposingSpans.length; ++i) {
sp.setSpan(mDefaultComposingSpans[i], 0, sp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
}
}
} else {
sp = (Spannable) text;
}
setComposingSpans(sp);
}
if (DEBUG)
Log.v(TAG, "Replacing from " + a + " to " + b + " with \"" + text + "\", composing=" + composing + ", type=" + text.getClass().getCanonicalName());
if (DEBUG) {
LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG);
lp.println("Current text:");
TextUtils.dumpSpans(content, lp, " ");
lp.println("Composing text:");
TextUtils.dumpSpans(text, lp, " ");
}
// we are providing here.
if (newCursorPosition > 0) {
newCursorPosition += b - 1;
} else {
newCursorPosition += a;
}
if (newCursorPosition < 0)
newCursorPosition = 0;
if (newCursorPosition > content.length())
newCursorPosition = content.length();
Selection.setSelection(content, newCursorPosition);
content.replace(a, b, text);
if (DEBUG) {
LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG);
lp.println("Final text:");
TextUtils.dumpSpans(content, lp, " ");
}
endBatchEdit();
}
use of android.text.Editable in project platform_frameworks_base by android.
the class BaseInputConnection method getSelectedText.
/**
* The default implementation returns the text currently selected, or null if none is
* selected.
*/
public CharSequence getSelectedText(int flags) {
final Editable content = getEditable();
if (content == null)
return null;
int a = Selection.getSelectionStart(content);
int b = Selection.getSelectionEnd(content);
if (a > b) {
int tmp = a;
a = b;
b = tmp;
}
if (a == b)
return null;
if ((flags & GET_TEXT_WITH_STYLES) != 0) {
return content.subSequence(a, b);
}
return TextUtils.substring(content, a, b);
}
use of android.text.Editable in project platform_frameworks_base by android.
the class BaseInputConnection method clearMetaKeyStates.
/**
* Default implementation uses
* {@link MetaKeyKeyListener#clearMetaKeyState(long, int)
* MetaKeyKeyListener.clearMetaKeyState(long, int)} to clear the state.
*/
public boolean clearMetaKeyStates(int states) {
final Editable content = getEditable();
if (content == null)
return false;
MetaKeyKeyListener.clearMetaKeyState(content, states);
return true;
}
use of android.text.Editable in project platform_frameworks_base by android.
the class BaseInputConnection method getTextBeforeCursor.
/**
* The default implementation returns the given amount of text from the
* current cursor position in the buffer.
*/
public CharSequence getTextBeforeCursor(int length, int flags) {
final Editable content = getEditable();
if (content == null)
return null;
int a = Selection.getSelectionStart(content);
int b = Selection.getSelectionEnd(content);
if (a > b) {
int tmp = a;
a = b;
b = tmp;
}
if (a <= 0) {
return "";
}
if (length > a) {
length = a;
}
if ((flags & GET_TEXT_WITH_STYLES) != 0) {
return content.subSequence(a - length, a);
}
return TextUtils.substring(content, a - length, a);
}
use of android.text.Editable in project platform_frameworks_base by android.
the class BaseInputConnection method setComposingRegion.
public boolean setComposingRegion(int start, int end) {
final Editable content = getEditable();
if (content != null) {
beginBatchEdit();
removeComposingSpans(content);
int a = start;
int b = end;
if (a > b) {
int tmp = a;
a = b;
b = tmp;
}
// Clip the end points to be within the content bounds.
final int length = content.length();
if (a < 0)
a = 0;
if (b < 0)
b = 0;
if (a > length)
a = length;
if (b > length)
b = length;
ensureDefaultComposingSpans();
if (mDefaultComposingSpans != null) {
for (int i = 0; i < mDefaultComposingSpans.length; ++i) {
content.setSpan(mDefaultComposingSpans[i], a, b, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
}
}
content.setSpan(COMPOSING, a, b, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
// Note: sendCurrentText does nothing unless mDummyMode is set
sendCurrentText();
endBatchEdit();
}
return true;
}
Aggregations