Skip to content

Commit f021f21

Browse files
committed
fix: prevent bypassing pattern, pin or biometric lock with the back gesture
Signed-off-by: Jorge Aguado Recio <jaguado@izertis.com>
1 parent 90f1483 commit f021f21

2 files changed

Lines changed: 28 additions & 57 deletions

File tree

‎owncloudApp/src/main/java/com/owncloud/android/presentation/security/passcode/PassCodeActivity.kt‎

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import android.view.View
3939
import android.view.WindowManager
4040
import android.widget.EditText
4141
import android.widget.LinearLayout
42+
import androidx.activity.OnBackPressedCallback
4243
import com.owncloud.android.BuildConfig
4344
import com.owncloud.android.R
4445
import 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

‎owncloudApp/src/main/java/com/owncloud/android/presentation/security/pattern/PatternActivity.kt‎

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ package com.owncloud.android.presentation.security.pattern
2828
import android.content.Intent
2929
import android.os.Bundle
3030
import android.os.SystemClock
31-
import android.view.KeyEvent
3231
import android.view.Menu
3332
import android.view.MenuItem
3433
import android.view.View
3534
import android.view.WindowManager
35+
import androidx.activity.OnBackPressedCallback
3636
import androidx.core.view.isVisible
3737
import com.andrognito.patternlockview.PatternLockView.Dot
3838
import com.andrognito.patternlockview.listener.PatternLockViewListener
@@ -70,13 +70,25 @@ class PatternActivity : ToolbarActivity(), EnableBiometrics {
7070

7171
val resultIntent = Intent()
7272

73+
private val onBackPressedCallback = object : OnBackPressedCallback(true) {
74+
override fun handleOnBackPressed() {
75+
if (ACTION_REQUEST_WITH_RESULT == intent.action && intent.extras?.getBoolean(EXTRAS_LOCK_ENFORCED) != true ||
76+
ACTION_CHECK_WITH_RESULT == intent.action) {
77+
PatternManager.onActivityStopped(this@PatternActivity)
78+
finish()
79+
}
80+
}
81+
}
82+
7383
override fun onCreate(savedInstanceState: Bundle?) {
7484
super.onCreate(savedInstanceState)
7585

7686
if (!BuildConfig.DEBUG) {
7787
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
7888
}
7989

90+
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
91+
8092
_binding = PatternLockActivityBinding.inflate(layoutInflater)
8193

8294
setContentView(binding.root)
@@ -157,15 +169,10 @@ class PatternActivity : ToolbarActivity(), EnableBiometrics {
157169
}
158170

159171
override fun onOptionsItemSelected(item: MenuItem): Boolean {
160-
onBackPressed()
172+
onBackPressedDispatcher.onBackPressed()
161173
return true
162174
}
163175

164-
override fun onBackPressed() {
165-
PatternManager.onActivityStopped(this)
166-
super.onBackPressed()
167-
}
168-
169176
override fun onCreateOptionsMenu(menu: Menu): Boolean = false
170177

171178
/**
@@ -323,27 +330,6 @@ class PatternActivity : ToolbarActivity(), EnableBiometrics {
323330
}
324331
}
325332

326-
/**
327-
* Overrides click on the BACK arrow to correctly cancel ACTION_ENABLE or ACTION_DISABLE, while
328-
* preventing than ACTION_CHECK may be worked around.
329-
*
330-
* @param keyCode Key code of the key that triggered the down event.
331-
* @param event Event triggered.
332-
* @return 'True' when the key event was processed by this method.
333-
*/
334-
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
335-
if (keyCode == KeyEvent.KEYCODE_BACK && event.repeatCount == 0) {
336-
if (ACTION_REQUEST_WITH_RESULT == intent.action &&
337-
intent.extras?.getBoolean(EXTRAS_LOCK_ENFORCED) != true ||
338-
ACTION_CHECK_WITH_RESULT == intent.action
339-
) {
340-
finish()
341-
} // else, do nothing, but report that the key was consumed to stay alive
342-
return true
343-
}
344-
return super.onKeyDown(keyCode, event)
345-
}
346-
347333
override fun onOptionSelected(optionSelected: BiometricStatus) {
348334
when (optionSelected) {
349335
BiometricStatus.ENABLED_BY_USER -> {

0 commit comments

Comments
 (0)