Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fingerprint (App Lock) #60

Merged
merged 6 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,6 @@ dependencies {


implementation "com.valentinilk.shimmer:compose-shimmer:1.2.0"

implementation "androidx.biometric:biometric-ktx:1.2.0-alpha05"
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" />

<uses-permission android:name="android.permission.USE_BIOMETRIC" />

<application
android:name=".UpdateSettingsApp"
android:allowBackup="true"
Expand Down
45 changes: 19 additions & 26 deletions app/src/main/java/app/myzel394/alibi/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package app.myzel394.alibi

import android.content.Context
import android.os.Bundle
import android.view.MotionEvent
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.platform.LocalContext
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat
import androidx.datastore.dataStore
import app.myzel394.alibi.db.AppSettings
import app.myzel394.alibi.db.AppSettingsSerializer
import app.myzel394.alibi.ui.AsLockedApp
import app.myzel394.alibi.ui.LockedAppHandlers
import app.myzel394.alibi.ui.Navigation
import app.myzel394.alibi.ui.SUPPORTS_DARK_MODE_NATIVELY
import app.myzel394.alibi.ui.theme.AlibiTheme

const val SETTINGS_FILE = "settings.json"
Expand All @@ -31,27 +30,21 @@ class MainActivity : AppCompatActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)

setContent {
val dataStore = LocalContext.current.dataStore
val settings = dataStore
.data
.collectAsState(initial = AppSettings.getDefaultInstance())
.value

LaunchedEffect(settings.theme) {
if (!SUPPORTS_DARK_MODE_NATIVELY) {
val currentValue = AppCompatDelegate.getDefaultNightMode()

if (settings.theme == AppSettings.Theme.LIGHT && currentValue != AppCompatDelegate.MODE_NIGHT_NO) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
} else if (settings.theme == AppSettings.Theme.DARK && currentValue != AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
AlibiTheme {
LockedAppHandlers()

Box(
modifier = Modifier
.fillMaxSize()
.background(
MaterialTheme.colorScheme.background
)
) {
AsLockedApp {
Navigation()
}
}
}

AlibiTheme {
Navigation()
}
}
}
}
22 changes: 21 additions & 1 deletion app/src/main/java/app/myzel394/alibi/db/AppSettings.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package app.myzel394.alibi.db

import android.content.Context
import android.content.Intent
import android.media.MediaRecorder
import android.os.Build
import androidx.camera.video.FileOutputOptions
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricManager.Authenticators
import androidx.camera.video.Quality
import androidx.camera.video.QualitySelector
import androidx.core.app.ActivityCompat.startActivityForResult
import app.myzel394.alibi.R
import app.myzel394.alibi.helpers.AudioBatchesFolder
import app.myzel394.alibi.helpers.VideoBatchesFolder
Expand All @@ -19,6 +22,8 @@ data class AppSettings(
val audioRecorderSettings: AudioRecorderSettings = AudioRecorderSettings.getDefaultInstance(),
val videoRecorderSettings: VideoRecorderSettings = VideoRecorderSettings.getDefaultInstance(),

val appLockSettings: AppLockSettings? = null,

val hasSeenOnboarding: Boolean = false,
val showAdvancedSettings: Boolean = false,
val theme: Theme = Theme.SYSTEM,
Expand Down Expand Up @@ -94,6 +99,14 @@ data class AppSettings(
return copy(saveFolder = saveFolder)
}

fun setAppLockSettings(appLockSettings: AppLockSettings?): AppSettings {
return copy(appLockSettings = appLockSettings)
}

// If the object is present, biometric authentication is enabled.
// To disable biometric authentication, set the instance to null.
fun isAppLockEnabled() = appLockSettings != null

enum class Theme {
SYSTEM,
LIGHT,
Expand Down Expand Up @@ -547,3 +560,10 @@ data class NotificationSettings(
)
}
}

@Serializable
class AppLockSettings {
companion object {
fun getDefaultInstance() = AppLockSettings()
}
}
76 changes: 76 additions & 0 deletions app/src/main/java/app/myzel394/alibi/helpers/AppLockHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package app.myzel394.alibi.helpers

import android.app.Activity
import android.content.Context
import androidx.biometric.BiometricManager
import androidx.core.content.ContextCompat
import androidx.biometric.BiometricPrompt
import androidx.fragment.app.FragmentActivity
import kotlinx.coroutines.CompletableDeferred
import kotlin.system.exitProcess

class AppLockHelper {
enum class SupportType {
AVAILABLE,
UNAVAILABLE,
NONE_ENROLLED,
}

companion object {
fun getSupportType(context: Context): SupportType {
val biometricManager = BiometricManager.from(context)
return when (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG or BiometricManager.Authenticators.DEVICE_CREDENTIAL)) {
BiometricManager.BIOMETRIC_SUCCESS -> SupportType.AVAILABLE
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> SupportType.NONE_ENROLLED

else -> SupportType.UNAVAILABLE
}
}

fun authenticate(
context: Context,
title: String,
subtitle: String
): CompletableDeferred<Boolean> {
val deferred = CompletableDeferred<Boolean>()

val mainExecutor = ContextCompat.getMainExecutor(context)
val biometricPrompt = BiometricPrompt(
context as FragmentActivity,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
deferred.complete(false)
}

override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
deferred.complete(true)
}

override fun onAuthenticationFailed() {
deferred.complete(false)
}
}
)

val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(title)
.setSubtitle(subtitle)
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG or BiometricManager.Authenticators.DEVICE_CREDENTIAL)
.build()

biometricPrompt.authenticate(promptInfo)

return deferred
}

fun closeApp(context: Context) {
(context as? Activity)?.let {
it.finishAndRemoveTask()
it.finishAffinity()
it.finish()
}

exitProcess(0)
}
}
}
152 changes: 152 additions & 0 deletions app/src/main/java/app/myzel394/alibi/ui/AsLockedApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package app.myzel394.alibi.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Fingerprint
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import app.myzel394.alibi.R
import app.myzel394.alibi.dataStore
import app.myzel394.alibi.helpers.AppLockHelper
import kotlinx.coroutines.launch

// After this amount, close the app
const val MAX_TRIES = 5

// Makes sure the app needs to be unlocked first, if app lock is enabled
@Composable
fun AsLockedApp(
content: (@Composable () -> Unit),
) {
val scope = rememberCoroutineScope()
val context = LocalContext.current

val settings = context
.dataStore
.data
.collectAsState(initial = null)
.value ?: return

// -1 = Unlocked, any other value = locked
var tries by remember { mutableIntStateOf(0) }

LaunchedEffect(settings.isAppLockEnabled()) {
if (!settings.isAppLockEnabled()) {
tries = -1
}
}

if (tries == -1) {
return content()
}

val title = stringResource(R.string.identityVerificationRequired_title)
val subtitle = stringResource(R.string.identityVerificationRequired_subtitle)

fun openAuthentication() {
if (tries >= MAX_TRIES) {
AppLockHelper.closeApp(context)
return
}

scope.launch {
val successful = AppLockHelper.authenticate(
context,
title,
subtitle,
).await()

if (successful) {
tries = -1
return@launch
}

tries++

if (tries >= MAX_TRIES) {
AppLockHelper.closeApp(context)
}
}
}

LaunchedEffect(settings.isAppLockEnabled()) {
if (settings.isAppLockEnabled()) {
openAuthentication()
}
}

Scaffold { paddingValues ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceBetween,
) {
Box {}
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
Icon(
Icons.Default.Fingerprint,
contentDescription = null,
modifier = Modifier
.size(64.dp)
)
Text(
text = stringResource(R.string.ui_locked_title),
style = MaterialTheme.typography.bodyLarge,
)
}
Button(
modifier = Modifier
.fillMaxWidth()
.height(BIG_PRIMARY_BUTTON_SIZE),
onClick = ::openAuthentication,
colors = ButtonDefaults.filledTonalButtonColors(),
) {
Icon(
Icons.Default.Lock,
contentDescription = null,
modifier = Modifier
.size(ButtonDefaults.IconSize)
)
Spacer(modifier = Modifier.width(ButtonDefaults.IconSpacing))
Text(
text = stringResource(R.string.ui_locked_unlocked),
style = MaterialTheme.typography.bodyLarge,
)
}
}
}
}
Loading