下面列出了android.text.Editable#getChars ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a SafeHelperFactory from an Editable, such as what you get by
* calling getText() on an EditText.
*
* The Editable will be cleared as part of this call.
*
* @param editor the user's supplied passphrase
* @param options options for pre-key, post-key SQL
* @return a SafeHelperFactory
*/
public static SafeHelperFactory fromUser(Editable editor, Options options) {
char[] passphrase=new char[editor.length()];
SafeHelperFactory result;
editor.getChars(0, editor.length(), passphrase, 0);
try {
result=new SafeHelperFactory(passphrase, options);
}
finally {
editor.clear();
}
return(result);
}
private void sendCurrentText() {
if (!mDummyMode) {
return;
}
Editable content = getEditable();
if (content != null) {
final int N = content.length();
if (N == 0) {
return;
}
if (N == 1) {
// If it's 1 character, we have a chance of being
// able to generate normal key events...
if (mKeyCharacterMap == null) {
mKeyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
}
char[] chars = new char[1];
content.getChars(0, 1, chars, 0);
KeyEvent[] events = mKeyCharacterMap.getEvents(chars);
if (events != null) {
for (int i=0; i<events.length; i++) {
if (DEBUG) Log.v(TAG, "Sending: " + events[i]);
sendKeyEvent(events[i]);
}
content.clear();
return;
}
}
// Otherwise, revert to the special key event containing
// the actual characters.
KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(),
content.toString(), KeyCharacterMap.VIRTUAL_KEYBOARD, 0);
sendKeyEvent(event);
content.clear();
}
}
/**
* Changes the passphrase associated with this database. The supplied
* Editable is cleared as part of this operation.
*
* @param editor source of passphrase, presumably from a user
*/
public void rekey(Editable editor) {
char[] passphrase=new char[editor.length()];
editor.getChars(0, editor.length(), passphrase, 0);
try {
rekey(passphrase);
}
finally {
editor.clear();
}
}
public char[] getPassword()
{
if(hasPassword() && _passwordEditText!=null)
{
Editable pwd = _passwordEditText.getText();
char[] res = new char[pwd.length()];
pwd.getChars(0, res.length, res, 0);
return res;
}
return null;
}
/**
* Changes the passphrase associated with this database. The supplied
* Editable is cleared as part of this operation.
*
* @param editor
* source of passphrase, presumably from a user
*/
public void rekey(Editable editor) {
char[] passphrase = new char[editor.length()];
editor.getChars(0, editor.length(), passphrase, 0);
try {
rekey(passphrase);
} finally {
editor.clear();
}
}
/**
* Creates a SafeHelperFactory from an Editable, such as what you get by
* calling getText() on an EditText.
*
* The Editable will be cleared as part of this call.
*
* @param editor
* the user's supplied passphrase
* @param options
* options for pre-key, post-key SQL
* @return a SafeHelperFactory
*/
public static KriptonSQLCipherHelperFactory fromUser(Editable editor, Options options) {
char[] passphrase = new char[editor.length()];
KriptonSQLCipherHelperFactory result;
editor.getChars(0, editor.length(), passphrase, 0);
try {
result = new KriptonSQLCipherHelperFactory(passphrase, options);
} finally {
editor.clear();
}
return (result);
}
private void processSpoiler(boolean opening, Editable output) {
int len = output.length();
if (opening) {
output.setSpan(new Spoiler(), len, len, Spannable.SPAN_MARK_MARK);
} else {
Object obj = getLast(output, Spoiler.class);
int where = output.getSpanStart(obj);
output.removeSpan(obj);
if (where != len) {
char[] str = new char[len - where];
output.getChars(where, len, str, 0);
final String text = String.valueOf(str);
output.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Dialog dialog = new Dialog(widget.getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.spoiler_dialog);
((TextView) dialog.findViewById(R.id.text)).setText(text);
dialog.show();
}
}, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
output.setSpan(new ForegroundColorSpan(0xff666666), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
output.setSpan(new BackgroundColorSpan(0xff666666), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
@Override
public boolean onKeyDown(View view, Editable content, int keyCode, KeyEvent event) {
boolean result = super.onKeyDown(view, content, keyCode, event);
// auto indent
if ( sAutoIndent && keyCode == KeyEvent.KEYCODE_ENTER ){
int a = Selection.getSelectionStart(content);
int b = Selection.getSelectionEnd(content);
if ( a == b ){
// search head of previous line
int prev = a-2;
while( prev >=0 && content.charAt(prev)!='\n' ){
prev--;
}
prev ++;
int pos = prev;
while( content.charAt(pos)==' ' || content.charAt(pos)=='\t' || content.charAt(pos)=='\u3000'){
pos++;
}
int len = pos-prev;
if ( len > 0 ){
char [] dest = new char[len];
content.getChars(prev, pos, dest, 0);
content.replace(a,b, new String(dest) );
Selection.setSelection(content, a+len);
}
}
}
if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ){
if (keyCode == KEYCODE_FORWARD_DEL ) {
if ( (event.getMetaState() & 512) == 0 ){ // workaround for Galaxy Note
forwardDelete(view, content, keyCode, event);
return true;
}
}
}
return result;
}
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
Editable currentText = getText();
if (currentText == null) return super.commitText(text, newCursorPosition);
int selectionStart = Selection.getSelectionStart(currentText);
int selectionEnd = Selection.getSelectionEnd(currentText);
int autocompleteIndex = currentText.getSpanStart(mAutocompleteSpan);
// If the text being committed is a single character that matches the next character
// in the selection (assumed to be the autocomplete text), we only move the text
// selection instead clearing the autocomplete text causing flickering as the
// autocomplete text will appear once the next suggestions are received.
//
// To be confident that the selection is an autocomplete, we ensure the selection
// is at least one character and the end of the selection is the end of the
// currently entered text.
if (newCursorPosition == 1 && selectionStart > 0 && selectionStart != selectionEnd
&& selectionEnd >= currentText.length()
&& autocompleteIndex == selectionStart
&& text.length() == 1) {
currentText.getChars(selectionStart, selectionStart + 1, mTempSelectionChar, 0);
if (mTempSelectionChar[0] == text.charAt(0)) {
// Since the text isn't changing, TalkBack won't read out the typed characters.
// To work around this, explicitly send an accessibility event. crbug.com/416595
if (mAccessibilityManager != null && mAccessibilityManager.isEnabled()) {
AccessibilityEvent event = AccessibilityEvent.obtain(
AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
event.setFromIndex(selectionStart);
event.setRemovedCount(0);
event.setAddedCount(1);
event.setBeforeText(currentText.toString().substring(0, selectionStart));
sendAccessibilityEventUnchecked(event);
}
setAutocompleteText(
currentText.subSequence(0, selectionStart + 1),
currentText.subSequence(selectionStart + 1, selectionEnd));
if (!mInBatchEditMode) {
notifyAutocompleteTextStateChanged(false);
}
return true;
}
}
boolean retVal = super.commitText(text, newCursorPosition);
// Ensure the autocomplete span is removed if it is no longer valid after committing the
// text.
if (getText().getSpanStart(mAutocompleteSpan) >= 0) clearAutocompleteSpanIfInvalid();
return retVal;
}
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
Editable currentText = getText();
if (currentText == null) return super.commitText(text, newCursorPosition);
int selectionStart = Selection.getSelectionStart(currentText);
int selectionEnd = Selection.getSelectionEnd(currentText);
int autocompleteIndex = currentText.getSpanStart(mAutocompleteSpan);
// If the text being committed is a single character that matches the next character
// in the selection (assumed to be the autocomplete text), we only move the text
// selection instead clearing the autocomplete text causing flickering as the
// autocomplete text will appear once the next suggestions are received.
//
// To be confident that the selection is an autocomplete, we ensure the selection
// is at least one character and the end of the selection is the end of the
// currently entered text.
if (newCursorPosition == 1 && selectionStart > 0 && selectionStart != selectionEnd
&& selectionEnd >= currentText.length()
&& autocompleteIndex == selectionStart
&& text.length() == 1) {
currentText.getChars(selectionStart, selectionStart + 1, mTempSelectionChar, 0);
if (mTempSelectionChar[0] == text.charAt(0)) {
// Since the text isn't changing, TalkBack won't read out the typed characters.
// To work around this, explicitly send an accessibility event. crbug.com/416595
if (mAccessibilityManager != null && mAccessibilityManager.isEnabled()) {
AccessibilityEvent event = AccessibilityEvent.obtain(
AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
event.setFromIndex(selectionStart);
event.setRemovedCount(0);
event.setAddedCount(1);
event.setBeforeText(currentText.toString().substring(0, selectionStart));
sendAccessibilityEventUnchecked(event);
}
setAutocompleteText(
currentText.subSequence(0, selectionStart + 1),
currentText.subSequence(selectionStart + 1, selectionEnd));
if (!mInBatchEditMode) {
notifyAutocompleteTextStateChanged(false);
}
return true;
}
}
boolean retVal = super.commitText(text, newCursorPosition);
// Ensure the autocomplete span is removed if it is no longer valid after committing the
// text.
if (getText().getSpanStart(mAutocompleteSpan) >= 0) clearAutocompleteSpanIfInvalid();
return retVal;
}
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
if (DEBUG) Log.i(TAG, "commitText: [%s]", text);
Editable currentText = getText();
if (currentText == null) return super.commitText(text, newCursorPosition);
int selectionStart = Selection.getSelectionStart(currentText);
int selectionEnd = Selection.getSelectionEnd(currentText);
int autocompleteIndex = currentText.getSpanStart(mAutocompleteSpan);
// If the text being committed is a single character that matches the next character
// in the selection (assumed to be the autocomplete text), we only move the text
// selection instead clearing the autocomplete text causing flickering as the
// autocomplete text will appear once the next suggestions are received.
//
// To be confident that the selection is an autocomplete, we ensure the selection
// is at least one character and the end of the selection is the end of the
// currently entered text.
if (newCursorPosition == 1 && selectionStart > 0 && selectionStart != selectionEnd
&& selectionEnd >= currentText.length() && autocompleteIndex == selectionStart
&& text.length() == 1) {
currentText.getChars(selectionStart, selectionStart + 1, mTempSelectionChar, 0);
if (mTempSelectionChar[0] == text.charAt(0)) {
// Since the text isn't changing, TalkBack won't read out the typed characters.
// To work around this, explicitly send an accessibility event. crbug.com/416595
if (mAccessibilityManager != null && mAccessibilityManager.isEnabled()) {
AccessibilityEvent event = AccessibilityEvent.obtain(
AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
event.setFromIndex(selectionStart);
event.setRemovedCount(0);
event.setAddedCount(1);
event.setBeforeText(currentText.toString().substring(0, selectionStart));
sendAccessibilityEventUnchecked(event);
}
setAutocompleteText(currentText.subSequence(0, selectionStart + 1),
currentText.subSequence(selectionStart + 1, selectionEnd));
if (!mInBatchEditMode) {
notifyAutocompleteTextStateChanged(false, false);
}
return true;
}
}
boolean retVal = super.commitText(text, newCursorPosition);
// Ensure the autocomplete span is removed if it is no longer valid after committing the
// text.
if (getText().getSpanStart(mAutocompleteSpan) >= 0) clearAutocompleteSpanIfInvalid();
return retVal;
}
@Override
public boolean onKeyDown(View view, Editable content, int keyCode, KeyEvent event) {
boolean result = super.onKeyDown(view, content, keyCode, event);
// auto indent
if ( sAutoIndent && keyCode == KeyEvent.KEYCODE_ENTER ){
int a = Selection.getSelectionStart(content);
int b = Selection.getSelectionEnd(content);
if ( a == b ){
// search head of previous line
int prev = a-2;
while( prev >=0 && content.charAt(prev)!='\n' ){
prev--;
}
prev ++;
int pos = prev;
while( content.charAt(pos)==' ' || content.charAt(pos)=='\t' || content.charAt(pos)=='\u3000'){
pos++;
}
int len = pos-prev;
if ( len > 0 ){
char [] dest = new char[len];
content.getChars(prev, pos, dest, 0);
content.replace(a,b, new String(dest) );
Selection.setSelection(content, a+len);
}
}
}
if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ){
if (keyCode == KEYCODE_FORWARD_DEL ) {
if ( (event.getMetaState() & 512) == 0 ){ // workaround for Galaxy Note
forwardDelete(view, content, keyCode, event);
return true;
}
}
}
return result;
}
/**
* Replaces this database with a version encrypted with the supplied
* passphrase, deleting the original. Do not call this while the database
* is open, which includes during any Room migrations.
*
* The passphrase is untouched in this call. If you are going to turn around
* and use it with SafeHelperFactory.fromUser(), fromUser() will clear the
* passphrase. If not, please set all bytes of the passphrase to 0 or something
* to clear out the passphrase.
*
* @param ctxt a Context
* @param dbName the name of the database, as used with Room, SQLiteOpenHelper,
* etc.
* @param editor the passphrase, such as obtained by calling getText() on an
* EditText
* @throws IOException
*/
public static void encrypt(Context ctxt, String dbName, Editable editor)
throws IOException {
char[] passphrase=new char[editor.length()];
editor.getChars(0, editor.length(), passphrase, 0);
encrypt(ctxt, dbName, passphrase);
}
/**
* Replaces this database with a version encrypted with the supplied
* passphrase, deleting the original. Do not call this while the database
* is open, which includes during any Room migrations.
*
* The passphrase is untouched in this call. If you are going to turn around
* and use it with SafeHelperFactory.fromUser(), fromUser() will clear the
* passphrase. If not, please set all bytes of the passphrase to 0 or something
* to clear out the passphrase.
*
* @param ctxt a Context
* @param dbName the name of the database, as used with Room, SQLiteOpenHelper,
* etc.
* @param editor the passphrase, such as obtained by calling getText() on an
* EditText
* @throws IOException
*/
public static void encrypt(Context ctxt, String dbName, Editable editor)
throws IOException {
char[] passphrase=new char[editor.length()];
editor.getChars(0, editor.length(), passphrase, 0);
encrypt(ctxt, dbName, passphrase);
}