pressing enter on a physical keyboard finished the selected task (fixes #590)

This commit is contained in:
Michael Langer 2020-10-04 21:01:15 +02:00
parent 52b7720217
commit b1986c325b
5 changed files with 82 additions and 5 deletions

View file

@ -35,7 +35,6 @@ import android.view.KeyEvent;
import android.view.View;
import android.view.ViewStub;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@ -45,6 +44,7 @@ import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Utilities.Constants;
import org.shadowice.flocke.andotp.Utilities.EditorActionHelper;
import org.shadowice.flocke.andotp.Utilities.EncryptionHelper;
import java.security.NoSuchAlgorithmException;
@ -142,7 +142,7 @@ public class AuthenticateActivity extends ThemedActivity
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
if (EditorActionHelper.isActionDoneOrKeyboardEnter(actionId, event)) {
checkPassword(v.getText().toString());
return true;
}

View file

@ -32,6 +32,7 @@ import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -56,6 +57,7 @@ import com.heinrichreimersoftware.materialintro.slide.SimpleSlide;
import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Utilities.ConfirmedPasswordTransformationHelper;
import org.shadowice.flocke.andotp.Utilities.Constants;
import org.shadowice.flocke.andotp.Utilities.EditorActionHelper;
import org.shadowice.flocke.andotp.Utilities.Settings;
import org.shadowice.flocke.andotp.Utilities.UIHelper;
@ -221,7 +223,7 @@ public class IntroScreenActivity extends IntroActivity {
}
}
public static class AuthenticationFragment extends SlideFragment {
public static class AuthenticationFragment extends SlideFragment implements TextView.OnEditorActionListener {
private Constants.EncryptionType encryptionType = Constants.EncryptionType.KEYSTORE;
private int slidePos = -1;
@ -440,11 +442,27 @@ public class IntroScreenActivity extends IntroActivity {
passwordInput.addTextChangedListener(textWatcher);
passwordConfirm.addTextChangedListener(textWatcher);
passwordConfirm.setOnEditorActionListener(this);
selection.setSelection(selectionMapping.indexOfValue(Constants.AuthMethod.PASSWORD));
return root;
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (EditorActionHelper.isActionDoneOrKeyboardEnter(actionId, event)) {
nextSlide();
return true;
} else if (EditorActionHelper.isActionUpKeyboardEnter(event)) {
// Ignore action up after keyboard enter. Otherwise the go-back button would be selected
// after pressing enter with an invalid password.
return true;
}
return false;
}
@Override
public boolean canGoForward() {
Constants.AuthMethod authMethod = selectionMapping.get(selection.getSelectedItemPosition());

View file

@ -10,16 +10,19 @@ import android.os.Build;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Utilities.ConfirmedPasswordTransformationHelper;
import org.shadowice.flocke.andotp.Utilities.EditorActionHelper;
import org.shadowice.flocke.andotp.Utilities.Tools;
public class PasswordEntryDialog extends AppCompatDialog
implements View.OnClickListener, TextWatcher {
implements View.OnClickListener, TextWatcher, TextView.OnEditorActionListener {
public enum Mode { ENTER, UPDATE }
@ -69,8 +72,10 @@ public class PasswordEntryDialog extends AppCompatDialog
passwordConfirm.setVisibility(View.VISIBLE);
passwordInput.addTextChangedListener(this);
passwordConfirm.addTextChangedListener(this);
passwordConfirm.setOnEditorActionListener(this);
} else if (this.dialogMode == Mode.ENTER) {
passwordConfirm.setVisibility(View.GONE);
passwordInput.setOnEditorActionListener(this);
}
}
@ -85,6 +90,20 @@ public class PasswordEntryDialog extends AppCompatDialog
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (EditorActionHelper.isActionDoneOrKeyboardEnter(actionId, event)) {
if (okButton.isEnabled()) okButton.performClick();
return true;
} else if (EditorActionHelper.isActionUpKeyboardEnter(event)) {
// Ignore action up after keyboard enter. Otherwise the cancel button would be selected
// after pressing enter with an invalid password.
return true;
}
return false;
}
// View.OnClickListener
public void onClick(View view) {
if (view.getId() == R.id.buttonOk) {

View file

@ -32,6 +32,7 @@ import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
@ -48,6 +49,7 @@ import com.google.android.material.textfield.TextInputLayout;
import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Utilities.ConfirmedPasswordTransformationHelper;
import org.shadowice.flocke.andotp.Utilities.Constants;
import org.shadowice.flocke.andotp.Utilities.EditorActionHelper;
import org.shadowice.flocke.andotp.Utilities.Settings;
import org.shadowice.flocke.andotp.Utilities.UIHelper;
@ -60,7 +62,7 @@ import static org.shadowice.flocke.andotp.Utilities.Constants.AuthMethod;
import static org.shadowice.flocke.andotp.Utilities.Constants.EncryptionType;
public class CredentialsPreference extends DialogPreference
implements AdapterView.OnItemClickListener, View.OnClickListener, TextWatcher {
implements AdapterView.OnItemClickListener, View.OnClickListener, TextWatcher, TextView.OnEditorActionListener {
public static final AuthMethod DEFAULT_VALUE = AuthMethod.NONE;
public interface EncryptionChangeCallback {
@ -144,6 +146,7 @@ public class CredentialsPreference extends DialogPreference
passwordInput.addTextChangedListener(this);
passwordConfirm.addTextChangedListener(this);
passwordConfirm.setOnEditorActionListener(this);
Button btnCancel = view.findViewById(R.id.btnCancel);
btnSave = view.findViewById(R.id.btnSave);
@ -303,6 +306,20 @@ public class CredentialsPreference extends DialogPreference
updateLayout();
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (EditorActionHelper.isActionDoneOrKeyboardEnter(actionId, event)) {
if (btnSave.isEnabled()) btnSave.performClick();
return true;
} else if (EditorActionHelper.isActionUpKeyboardEnter(event)) {
// Ignore action up after keyboard enter. Otherwise the cancel button would be selected
// after pressing enter with an invalid password.
return true;
}
return false;
}
// Needed stub functions
@Override
public void afterTextChanged(Editable s) {}

View file

@ -0,0 +1,23 @@
package org.shadowice.flocke.andotp.Utilities;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import static android.view.KeyEvent.ACTION_DOWN;
import static android.view.KeyEvent.ACTION_UP;
import static android.view.KeyEvent.KEYCODE_ENTER;
import static android.view.KeyEvent.KEYCODE_NUMPAD_ENTER;
public class EditorActionHelper {
private EditorActionHelper() { /* not allowed */ }
public static boolean isActionDoneOrKeyboardEnter(int actionId, KeyEvent event) {
return actionId == EditorInfo.IME_ACTION_DONE
|| event.getAction() == ACTION_DOWN && (event.getKeyCode() == KEYCODE_ENTER || event.getKeyCode() == KEYCODE_NUMPAD_ENTER);
}
public static boolean isActionUpKeyboardEnter(KeyEvent event) {
return event.getAction() == ACTION_UP && (event.getKeyCode() == KEYCODE_ENTER || event.getKeyCode() == KEYCODE_NUMPAD_ENTER);
}
}