From 2ec47ddef32332af2b15d88fca9f2ff4cf8af8b8 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 20 Oct 2023 09:54:24 +0200 Subject: [PATCH] Disable default keyboard dismiss behaviour Signed-off-by: alperozturk --- .../android/ui/activity/PassCodeActivity.java | 17 +++++--- .../android/ui/components/PassCodeEditText.kt | 39 +++++++++++++++++++ app/src/main/res/layout/passcodelock.xml | 18 +++------ app/src/main/res/values/styles.xml | 1 + 4 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/com/owncloud/android/ui/components/PassCodeEditText.kt diff --git a/app/src/main/java/com/owncloud/android/ui/activity/PassCodeActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/PassCodeActivity.java index 5745851b0469..10ab5cf60f92 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/PassCodeActivity.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/PassCodeActivity.java @@ -42,6 +42,7 @@ import com.owncloud.android.authentication.PassCodeManager; import com.owncloud.android.databinding.PasscodelockBinding; import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.ui.components.PassCodeEditText; import com.owncloud.android.utils.theme.ViewThemeUtils; import java.util.Arrays; @@ -74,7 +75,7 @@ public class PassCodeActivity extends AppCompatActivity implements Injectable { @Inject PassCodeManager passCodeManager; @Inject ViewThemeUtils viewThemeUtils; private PasscodelockBinding binding; - private final EditText[] passCodeEditTexts = new EditText[4]; + private final PassCodeEditText[] passCodeEditTexts = new PassCodeEditText[4]; private String[] passCodeDigits = {"", "", "", ""}; private boolean confirmingPassCode; private boolean changed = true; // to control that only one blocks jump @@ -174,7 +175,7 @@ public PasscodelockBinding getBinding() { @SuppressLint("ClickableViewAccessibility") protected void setTextListeners() { for (int i = 0; i < passCodeEditTexts.length; i++) { - final EditText editText = passCodeEditTexts[i]; + final PassCodeEditText editText = passCodeEditTexts[i]; boolean isLast = (i == 3); editText.addTextChangedListener(new PassCodeDigitTextWatcher(i, isLast)); @@ -276,8 +277,7 @@ private void hideSoftKeyboard() { (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow( focusedView.getWindowToken(), - 0 - ); + 0); } } @@ -412,7 +412,6 @@ public void run() { } } - @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -434,6 +433,7 @@ private class PassCodeDigitTextWatcher implements TextWatcher { PassCodeDigitTextWatcher(int index, boolean lastOne) { mIndex = index; mLastOne = lastOne; + if (mIndex < 0) { throw new IllegalArgumentException( "Invalid index in " + PassCodeDigitTextWatcher.class.getSimpleName() + @@ -457,8 +457,13 @@ private int next() { public void afterTextChanged(Editable s) { if (s.length() > 0) { if (!confirmingPassCode) { - passCodeDigits[mIndex] = passCodeEditTexts[mIndex].getText().toString(); + Editable passCodeText = passCodeEditTexts[mIndex].getText(); + + if (passCodeText != null) { + passCodeDigits[mIndex] = passCodeText.toString(); + } } + passCodeEditTexts[next()].requestFocus(); if (mLastOne) { diff --git a/app/src/main/java/com/owncloud/android/ui/components/PassCodeEditText.kt b/app/src/main/java/com/owncloud/android/ui/components/PassCodeEditText.kt new file mode 100644 index 000000000000..d699f02e07a3 --- /dev/null +++ b/app/src/main/java/com/owncloud/android/ui/components/PassCodeEditText.kt @@ -0,0 +1,39 @@ +/* + * Nextcloud Android client application + * + * @author Alper Ozturk + * Copyright (C) 2023 Alper Ozturk + * Copyright (C) 2023 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.owncloud.android.ui.components + +import android.content.Context +import android.util.AttributeSet +import android.view.KeyEvent +import androidx.appcompat.widget.AppCompatEditText + +class PassCodeEditText(context: Context, attrs: AttributeSet?): AppCompatEditText(context, attrs) { + + override fun onKeyPreIme(keyCode: Int, event: KeyEvent): Boolean { + val isBackButtonPressed = (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) + if (isBackButtonPressed) { + // Override default behaviour and prevent dismissing the keyboard + return true + } + return super.dispatchKeyEvent(event) + } + +} diff --git a/app/src/main/res/layout/passcodelock.xml b/app/src/main/res/layout/passcodelock.xml index 6e08fd0c1aca..81004efa37e3 100644 --- a/app/src/main/res/layout/passcodelock.xml +++ b/app/src/main/res/layout/passcodelock.xml @@ -69,34 +69,26 @@ android:layout_height="wrap_content" android:gravity="center_horizontal"> - - + - - - diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 025867e458cb..c8383be19632 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -324,6 +324,7 @@