-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unlock app with biometrics (WPB-4696) (#2321)
- Loading branch information
Showing
8 changed files
with
264 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/src/main/kotlin/com/wire/android/GlobalObserversManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
app/src/main/kotlin/com/wire/android/biomitric/BiometricPromptUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
|
||
package com.wire.android.biomitric | ||
|
||
import android.content.Context | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.biometric.BiometricManager | ||
import androidx.biometric.BiometricPrompt | ||
import androidx.biometric.BiometricPrompt.ERROR_NEGATIVE_BUTTON | ||
import androidx.core.content.ContextCompat | ||
import com.wire.android.R | ||
import com.wire.android.appLogger | ||
|
||
private const val TAG = "BiometricPromptUtils" | ||
|
||
object BiometricPromptUtils { | ||
fun createBiometricPrompt( | ||
activity: AppCompatActivity, | ||
onSuccess: () -> Unit, | ||
onCancel: () -> Unit, | ||
onRequestPasscode: () -> Unit | ||
): BiometricPrompt { | ||
val executor = ContextCompat.getMainExecutor(activity) | ||
|
||
val callback = object : BiometricPrompt.AuthenticationCallback() { | ||
|
||
override fun onAuthenticationError(errorCode: Int, errorString: CharSequence) { | ||
super.onAuthenticationError(errorCode, errorString) | ||
appLogger.i("$TAG errorCode is $errorCode and errorString is: $errorString") | ||
if (errorCode == ERROR_NEGATIVE_BUTTON) { | ||
onRequestPasscode() | ||
} else { | ||
onCancel() | ||
} | ||
} | ||
|
||
override fun onAuthenticationFailed() { | ||
super.onAuthenticationFailed() | ||
appLogger.i("$TAG User biometric rejected") | ||
} | ||
|
||
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { | ||
super.onAuthenticationSucceeded(result) | ||
appLogger.i("$TAG User biometric accepted") | ||
onSuccess() | ||
} | ||
} | ||
return BiometricPrompt(activity, executor, callback) | ||
} | ||
|
||
fun createPromptInfo(context: Context): BiometricPrompt.PromptInfo = | ||
BiometricPrompt.PromptInfo.Builder().apply { | ||
setTitle(context.getString(R.string.biometrics_prompt_dialog_title)) | ||
setSubtitle(context.getString(R.string.biometrics_prompt_dialog_subtitle)) | ||
setConfirmationRequired(false) | ||
setNegativeButtonText(context.getString(R.string.biometrics_use_passcode_button)) | ||
}.build() | ||
} | ||
|
||
fun AppCompatActivity.showBiometricPrompt( | ||
onSuccess: () -> Unit, | ||
onCancel: () -> Unit, | ||
onRequestPasscode: () -> Unit | ||
) { | ||
appLogger.i("$TAG showing biometrics dialog...") | ||
|
||
val canAuthenticate = BiometricManager.from(this) | ||
.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG) | ||
if (canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS) { | ||
val biometricPrompt = BiometricPromptUtils.createBiometricPrompt( | ||
this, | ||
onSuccess, | ||
onCancel, | ||
onRequestPasscode, | ||
) | ||
val promptInfo = BiometricPromptUtils.createPromptInfo(this) | ||
biometricPrompt.authenticate(promptInfo) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
app/src/main/kotlin/com/wire/android/ui/home/appLock/AppUnlockWithBiometricsScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.ui.home.appLock | ||
|
||
import androidx.activity.compose.BackHandler | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.res.vectorResource | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import com.ramcosta.composedestinations.annotation.Destination | ||
import com.ramcosta.composedestinations.annotation.RootNavGraph | ||
import com.wire.android.R | ||
import com.wire.android.biomitric.showBiometricPrompt | ||
import com.wire.android.navigation.BackStackMode | ||
import com.wire.android.navigation.NavigationCommand | ||
import com.wire.android.navigation.Navigator | ||
import com.wire.android.ui.common.colorsScheme | ||
import com.wire.android.ui.common.dimensions | ||
import com.wire.android.ui.destinations.EnterLockCodeScreenDestination | ||
|
||
@RootNavGraph | ||
@Destination | ||
@Composable | ||
fun AppUnlockWithBiometricsScreen( | ||
appUnlockWithBiometricsViewModel: AppUnlockWithBiometricsViewModel = hiltViewModel(), | ||
navigator: Navigator, | ||
) { | ||
Box( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.background(colorsScheme().background) | ||
) { | ||
Icon( | ||
modifier = Modifier | ||
.padding(top = dimensions().spacing80x) | ||
.align(Alignment.TopCenter), | ||
imageVector = ImageVector.vectorResource(id = R.drawable.ic_wire_logo), | ||
tint = MaterialTheme.colorScheme.onBackground, | ||
contentDescription = stringResource(id = R.string.content_description_welcome_wire_logo) | ||
) | ||
|
||
val activity = LocalContext.current | ||
LaunchedEffect(Unit) { | ||
(activity as AppCompatActivity).showBiometricPrompt( | ||
onSuccess = { | ||
appUnlockWithBiometricsViewModel.onAppUnlocked() | ||
navigator.navigateBack() | ||
}, | ||
onCancel = { | ||
navigator.finish() | ||
}, | ||
onRequestPasscode = { | ||
navigator.navigate( | ||
NavigationCommand( | ||
EnterLockCodeScreenDestination(), | ||
BackStackMode.CLEAR_WHOLE | ||
) | ||
) | ||
} | ||
) | ||
} | ||
} | ||
BackHandler { | ||
navigator.finish() | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/kotlin/com/wire/android/ui/home/appLock/AppUnlockWithBiometricsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.ui.home.appLock | ||
|
||
import androidx.lifecycle.ViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class AppUnlockWithBiometricsViewModel @Inject constructor( | ||
private val lockCodeTimeManager: LockCodeTimeManager | ||
) : ViewModel() { | ||
|
||
fun onAppUnlocked() { | ||
lockCodeTimeManager.appUnlocked() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters