use of android.view.inputmethod.ExtractedText in project speechutils by Kaljurand.
the class InputConnectionCommandEditor method moveAbs.
@Override
public Op moveAbs(final int pos) {
return new Op("moveAbs " + pos) {
@Override
public Op run() {
Op undo = null;
mInputConnection.beginBatchEdit();
ExtractedText et = getExtractedText();
if (et != null) {
int charPos = pos;
if (pos < 0) {
//-1 == end of text
charPos = et.text.length() + pos + 1;
}
undo = getOpSetSelection(charPos, charPos, et.selectionStart, et.selectionEnd).run();
}
mInputConnection.endBatchEdit();
return undo;
}
};
}
use of android.view.inputmethod.ExtractedText in project speechutils by Kaljurand.
the class InputConnectionCommandEditor method replace.
@Override
public Op replace(final String query, final String replacement) {
return new Op("replace") {
@Override
public Op run() {
Op undo = null;
mInputConnection.beginBatchEdit();
final ExtractedText et = getExtractedText();
if (et != null) {
Pair<Integer, CharSequence> queryResult = lastIndexOf(query, et);
final CharSequence match = queryResult.second;
if (queryResult.first >= 0) {
boolean success = mInputConnection.setSelection(queryResult.first, queryResult.first);
if (success) {
// Delete existing text
success = mInputConnection.deleteSurroundingText(0, match.length());
if (replacement.isEmpty()) {
if (success) {
undo = new Op("undo replace1") {
@Override
public Op run() {
mInputConnection.beginBatchEdit();
boolean success2 = mInputConnection.commitText(match, 1) && mInputConnection.setSelection(et.selectionStart, et.selectionEnd);
mInputConnection.endBatchEdit();
if (success2) {
return NO_OP;
}
return null;
}
};
}
} else {
success = mInputConnection.commitText(replacement, 1);
if (success) {
undo = new Op("undo replace2") {
@Override
public Op run() {
mInputConnection.beginBatchEdit();
boolean success2 = mInputConnection.deleteSurroundingText(replacement.length(), 0) && mInputConnection.commitText(match, 1) && mInputConnection.setSelection(et.selectionStart, et.selectionEnd);
mInputConnection.endBatchEdit();
if (success2) {
return NO_OP;
}
return null;
}
};
}
}
}
}
}
mInputConnection.endBatchEdit();
return undo;
}
};
}
use of android.view.inputmethod.ExtractedText in project speechutils by Kaljurand.
the class InputConnectionCommandEditor method commitFinalResult.
@Override
public CommandEditorResult commitFinalResult(final String text) {
CommandEditorResult result = null;
if (mRewriters == null || mRewriters.isEmpty()) {
// If rewrites/commands are not defined (default), then selection can be dictated over.
commitWithOverwrite(text);
} else {
final ExtractedText et = getExtractedText();
final String selectedText = getSelectedText();
// Try to interpret the text as a command and if it is, then apply it.
// Otherwise write out the text as usual.
UtteranceRewriter.Rewrite rewrite = applyCommand(text);
String textRewritten = rewrite.mStr;
final int len = commitWithOverwrite(textRewritten);
// TODO: add undo for setSelection even if len==0
if (len > 0) {
pushOpUndo(new Op("delete " + len) {
@Override
public Op run() {
mInputConnection.beginBatchEdit();
boolean success = mInputConnection.deleteSurroundingText(len, 0);
if (et != null && selectedText.length() > 0) {
success = mInputConnection.commitText(selectedText, 1) && mInputConnection.setSelection(et.selectionStart, et.selectionEnd);
}
mInputConnection.endBatchEdit();
if (success) {
return NO_OP;
}
return null;
}
});
}
boolean success = false;
if (rewrite.isCommand()) {
CommandEditorManager.EditorCommand ec = CommandEditorManager.get(rewrite.mId);
if (ec != null) {
// TODO: dont call runOp from here
success = runOp(ec.getOp(this, rewrite.mArgs));
}
} else {
mCommandPrefix.add(textRewritten);
}
result = new CommandEditorResult(success, rewrite);
}
mPrevText = "";
mAddedLength = 0;
return result;
}
use of android.view.inputmethod.ExtractedText in project speechutils by Kaljurand.
the class InputConnectionCommandEditor method selectReBefore.
@Override
public Op selectReBefore(final String regex) {
return new Op("selectReBefore") {
@Override
public Op run() {
Op undo = null;
mInputConnection.beginBatchEdit();
final ExtractedText et = getExtractedText();
if (et != null) {
CharSequence input = et.text.subSequence(0, et.selectionStart);
// 0 == last match
Pair<Integer, Integer> pos = matchNth(Pattern.compile(regex), input, 0);
if (pos != null) {
undo = getOpSetSelection(pos.first, pos.second, et.selectionStart, et.selectionEnd).run();
}
}
mInputConnection.endBatchEdit();
return undo;
}
};
}
use of android.view.inputmethod.ExtractedText in project android_frameworks_base by DirtyUnicorns.
the class InputConnectionWrapper method getExtractedText.
public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
ExtractedText value = null;
try {
InputContextCallback callback = InputContextCallback.getInstance();
mIInputContext.getExtractedText(request, flags, callback.mSeq, callback);
synchronized (callback) {
callback.waitForResultLocked();
if (callback.mHaveValue) {
value = callback.mExtractedText;
}
}
callback.dispose();
} catch (RemoteException e) {
return null;
}
return value;
}
Aggregations