allow tab completion in conferences

This commit is contained in:
Daniel Gultsch 2015-09-27 19:17:44 +02:00
parent 5a5e0e7121
commit bbbc30e823
2 changed files with 59 additions and 4 deletions

View File

@ -37,6 +37,7 @@ import android.widget.Toast;
import net.java.otr4j.session.SessionStatus;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -1226,6 +1227,48 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
updateSendButton();
}
private int completionIndex = 0;
private int lastCompletionLength = 0;
private String incomplete;
private int lastCompletionCursor;
private boolean firstWord = false;
@Override
public boolean onTabPressed(boolean repeated) {
if (conversation == null || conversation.getMode() == Conversation.MODE_SINGLE) {
return false;
}
if (repeated) {
completionIndex++;
} else {
lastCompletionLength = 0;
completionIndex = 0;
final String content = mEditMessage.getText().toString();
lastCompletionCursor = mEditMessage.getSelectionEnd();
int start = lastCompletionCursor > 0 ? content.lastIndexOf(" ",lastCompletionCursor-1) + 1 : 0;
firstWord = start == 0;
incomplete = content.substring(start,lastCompletionCursor);
}
List<String> completions = new ArrayList<>();
for(MucOptions.User user : conversation.getMucOptions().getUsers()) {
if (user.getName().startsWith(incomplete)) {
completions.add(user.getName()+(firstWord ? ": " : " "));
}
}
Collections.sort(completions);
if (completions.size() > completionIndex) {
String completion = completions.get(completionIndex).substring(incomplete.length());
mEditMessage.getEditableText().delete(lastCompletionCursor,lastCompletionCursor + lastCompletionLength);
mEditMessage.getEditableText().insert(lastCompletionCursor, completion);
lastCompletionLength = completion.length();
} else {
completionIndex = -1;
mEditMessage.getEditableText().delete(lastCompletionCursor,lastCompletionCursor + lastCompletionLength);
lastCompletionLength = 0;
}
return true;
}
@Override
public void onActivityResult(int requestCode, int resultCode,
final Intent data) {

View File

@ -32,14 +32,24 @@ public class EditMessage extends EditText {
private boolean isUserTyping = false;
private boolean lastInputWasTab = false;
protected KeyboardListener keyboardListener;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && !event.isShiftPressed()) {
lastInputWasTab = false;
if (keyboardListener != null && keyboardListener.onEnterPressed()) {
return true;
}
} else if (keyCode == KeyEvent.KEYCODE_TAB) {
if (keyboardListener != null && keyboardListener.onTabPressed(this.lastInputWasTab)) {
lastInputWasTab = true;
return true;
}
} else {
lastInputWasTab = false;
}
return super.onKeyDown(keyCode, event);
}
@ -47,6 +57,7 @@ public class EditMessage extends EditText {
@Override
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text,start,lengthBefore,lengthAfter);
lastInputWasTab = false;
if (this.mTypingHandler != null && this.keyboardListener != null) {
this.mTypingHandler.removeCallbacks(mTypingTimeout);
this.mTypingHandler.postDelayed(mTypingTimeout, Config.TYPING_TIMEOUT * 1000);
@ -69,10 +80,11 @@ public class EditMessage extends EditText {
}
public interface KeyboardListener {
public boolean onEnterPressed();
public void onTypingStarted();
public void onTypingStopped();
public void onTextDeleted();
boolean onEnterPressed();
void onTypingStarted();
void onTypingStopped();
void onTextDeleted();
boolean onTabPressed(boolean repeated);
}
}