Skip to content

Commit

Permalink
Merge pull request #54 from mchowning/soft-keyboard-delete
Browse files Browse the repository at this point in the history
Added InputConnectionWrapper to enable reliable deletion of selected object
  • Loading branch information
mgod committed May 14, 2014
2 parents 9c5391b + 97ff5fd commit 337ba55
Showing 1 changed file with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.MultiAutoCompleteTextView;
Expand Down Expand Up @@ -306,7 +307,7 @@ public void performCompletion() {
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
//Override normal multiline text handling of enter/done and force a done button
InputConnection connection = super.onCreateInputConnection(outAttrs);
TokenInputConnection connection = new TokenInputConnection(super.onCreateInputConnection(outAttrs), true);
int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) {
// clear the existing action
Expand Down Expand Up @@ -363,24 +364,30 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
}
break;
case KeyEvent.KEYCODE_DEL:
if (tokenClickStyle != null && tokenClickStyle.isSelectable()) {
Editable text = getText();
if (text == null) break;

TokenImageSpan[] spans = text.getSpans(0, text.length(), TokenImageSpan.class);
for (TokenImageSpan span: spans) {
if (span.view.isSelected()) {
removeSpan(span);
handled = true;
break;
}
}
}
handled = deleteSelectedObject(handled);
break;
}

return handled || super.onKeyDown(keyCode, event);
}

private boolean deleteSelectedObject(boolean handled) {
if (tokenClickStyle != null && tokenClickStyle.isSelectable()) {
Editable text = getText();
if (text == null) return handled;

TokenImageSpan[] spans = text.getSpans(0, text.length(), TokenImageSpan.class);
for (TokenImageSpan span: spans) {
if (span.view.isSelected()) {
removeSpan(span);
handled = true;
break;
}
}
}
return handled;
}

@Override
public boolean onEditorAction(TextView view, int action, KeyEvent keyEvent) {
if (action == EditorInfo.IME_ACTION_DONE) {
Expand Down Expand Up @@ -1224,4 +1231,22 @@ public SavedState[] newArray(int size) {
}
};
}

private class TokenInputConnection extends InputConnectionWrapper {

public TokenInputConnection(InputConnection target, boolean mutable) {
super(target, mutable);
}

// This will fire if the soft keyboard delete key is pressed.
// The onKeyPressed method does not always do this.
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
if (deleteSelectedObject(false)) {
return true;
} else {
return super.deleteSurroundingText(beforeLength, afterLength);
}
}
}
}

0 comments on commit 337ba55

Please sign in to comment.