@@ -39,6 +39,7 @@ import android.view.View
3939import android.view.WindowManager
4040import android.widget.EditText
4141import android.widget.LinearLayout
42+ import androidx.activity.OnBackPressedCallback
4243import com.owncloud.android.BuildConfig
4344import com.owncloud.android.R
4445import com.owncloud.android.databinding.PasscodeLockActivityBinding
@@ -76,6 +77,15 @@ class PassCodeActivity : ToolbarActivity(), NumberKeyboardListener, EnableBiomet
7677 private var confirmingPassCode = false
7778 private val resultIntent = Intent ()
7879
80+ private val onBackPressedCallback = object : OnBackPressedCallback (true ) {
81+ override fun handleOnBackPressed () {
82+ if ((ACTION_CREATE == intent.action && intent.extras?.getBoolean(EXTRAS_LOCK_ENFORCED ) != true ) || ACTION_REMOVE == intent.action) {
83+ PassCodeManager .onActivityStopped(this @PassCodeActivity)
84+ finish()
85+ }
86+ }
87+ }
88+
7989 /* *
8090 * Initializes the activity.
8191 *
@@ -92,6 +102,8 @@ class PassCodeActivity : ToolbarActivity(), NumberKeyboardListener, EnableBiomet
92102
93103 subscribeToViewModel()
94104
105+ onBackPressedDispatcher.addCallback(this , onBackPressedCallback)
106+
95107 _binding = PasscodeLockActivityBinding .inflate(layoutInflater)
96108
97109 // protection against screen recording
@@ -180,7 +192,7 @@ class PassCodeActivity : ToolbarActivity(), NumberKeyboardListener, EnableBiomet
180192 }
181193
182194 override fun onOptionsItemSelected (item : MenuItem ): Boolean {
183- onBackPressed()
195+ onBackPressedDispatcher. onBackPressed()
184196 return true
185197 }
186198
@@ -189,11 +201,6 @@ class PassCodeActivity : ToolbarActivity(), NumberKeyboardListener, EnableBiomet
189201 outState.putString(STATE_PASSCODE , passCodeViewModel.passcode.value.orEmpty())
190202 }
191203
192- override fun onBackPressed () {
193- PassCodeManager .onActivityStopped(this )
194- super .onBackPressed()
195- }
196-
197204 override fun onCreateOptionsMenu (menu : Menu ): Boolean = false
198205
199206 private fun inflatePasscodeTxtLine () {
@@ -382,27 +389,6 @@ class PassCodeActivity : ToolbarActivity(), NumberKeyboardListener, EnableBiomet
382389 passCodeEditTexts.first()?.requestFocus()
383390 }
384391
385- /* *
386- * Overrides click on the BACK arrow to correctly cancel ACTION_ENABLE or ACTION_DISABLE, while
387- * preventing than ACTION_CHECK may be worked around.
388- *
389- * @param keyCode Key code of the key that triggered the down event.
390- * @param event Event triggered.
391- * @return 'True' when the key event was processed by this method.
392- */
393- override fun onKeyDown (keyCode : Int , event : KeyEvent ): Boolean {
394- if (keyCode == KeyEvent .KEYCODE_BACK && event.repeatCount == 0 ) {
395- if ((ACTION_CREATE == intent.action &&
396- intent.extras?.getBoolean(EXTRAS_LOCK_ENFORCED ) != true ) ||
397- ACTION_REMOVE == intent.action
398- ) {
399- finish()
400- } // else, do nothing, but report that the key was consumed to stay alive
401- return true
402- }
403- return super .onKeyDown(keyCode, event)
404- }
405-
406392 /* *
407393 * Saves the pass code input by the user as the current pass code.
408394 */
@@ -462,8 +448,7 @@ class PassCodeActivity : ToolbarActivity(), NumberKeyboardListener, EnableBiomet
462448 }
463449
464450 KeyEvent .KEYCODE_ESCAPE -> {
465- PassCodeManager .onActivityStopped(this )
466- super .onBackPressed()
451+ onBackPressedDispatcher.onBackPressed()
467452 true
468453 }
469454
0 commit comments