Skip to content

Commit 5763de5

Browse files
author
lucky
committed
biometric
1 parent b5bebfe commit 5763de5

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010
applicationId "me.lucky.wasted"
1111
minSdk 23
1212
targetSdk 32
13-
versionCode 32
14-
versionName "1.5.3"
13+
versionCode 33
14+
versionName "1.5.4"
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
}
@@ -49,6 +49,7 @@ dependencies {
4949

5050
implementation 'androidx.security:security-crypto:1.0.0'
5151
implementation 'androidx.preference:preference-ktx:1.2.0'
52+
implementation 'androidx.biometric:biometric:1.1.0'
5253
implementation 'androidx.drawerlayout:drawerlayout:1.1.1'
5354
implementation 'info.guardianproject.panic:panic:1.0'
5455
}

app/src/main/java/me/lucky/wasted/MainActivity.kt

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package me.lucky.wasted
22

33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.biometric.BiometricManager
6+
import androidx.biometric.BiometricPrompt
7+
import androidx.core.content.ContextCompat
58
import androidx.fragment.app.Fragment
69

710
import me.lucky.wasted.databinding.ActivityMainBinding
@@ -17,18 +20,60 @@ open class MainActivity : AppCompatActivity() {
1720
super.onCreate(savedInstanceState)
1821
binding = ActivityMainBinding.inflate(layoutInflater)
1922
setContentView(binding.root)
20-
init()
23+
init1()
24+
if (initBiometric()) return
25+
init2()
2126
setup()
2227
}
2328

24-
private fun init() {
29+
private fun init1() {
2530
prefs = Preferences(this)
2631
prefsdb = Preferences(this, encrypted = false)
2732
prefs.copyTo(prefsdb)
2833
NotificationManager(this).createNotificationChannels()
34+
}
35+
36+
private fun init2() {
2937
replaceFragment(MainFragment())
3038
}
3139

40+
private fun initBiometric(): Boolean {
41+
val authenticators = BiometricManager.Authenticators.BIOMETRIC_STRONG or
42+
BiometricManager.Authenticators.DEVICE_CREDENTIAL
43+
when (BiometricManager
44+
.from(this)
45+
.canAuthenticate(authenticators))
46+
{
47+
BiometricManager.BIOMETRIC_SUCCESS -> {}
48+
else -> return false
49+
}
50+
val executor = ContextCompat.getMainExecutor(this)
51+
val prompt = BiometricPrompt(
52+
this,
53+
executor,
54+
object : BiometricPrompt.AuthenticationCallback()
55+
{
56+
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
57+
super.onAuthenticationError(errorCode, errString)
58+
finishAndRemoveTask()
59+
}
60+
61+
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
62+
super.onAuthenticationSucceeded(result)
63+
init2()
64+
setup()
65+
}
66+
})
67+
try {
68+
prompt.authenticate(BiometricPrompt.PromptInfo.Builder()
69+
.setTitle(getString(R.string.authentication))
70+
.setConfirmationRequired(false)
71+
.setAllowedAuthenticators(authenticators)
72+
.build())
73+
} catch (exc: Exception) { return false }
74+
return true
75+
}
76+
3277
private fun setup() = binding.apply {
3378
appBar.setNavigationOnClickListener {
3479
drawer.open()

app/src/main/java/me/lucky/wasted/trigger/broadcast/BroadcastReceiver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class BroadcastReceiver : BroadcastReceiver() {
1919
if (!prefs.isEnabled) return
2020
val secret = prefs.secret
2121
assert(secret.isNotEmpty())
22-
if (intent.getStringExtra(KEY) != secret) return
22+
if (intent.getStringExtra(KEY)?.trim() != secret) return
2323
val admin = DeviceAdminManager(context)
2424
try {
2525
admin.lockNow()

app/src/main/res/layout/fragment_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
android:padding="16dp"
3232
android:textAlignment="center"
3333
android:textStyle="bold"
34+
android:textColor="@color/black"
3435
android:textAppearance="?attr/textAppearanceTitleLarge" />
3536

3637
<Space

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<string name="main">Main</string>
2727
<string name="settings">Settings</string>
2828
<string name="goto_button">GOTO</string>
29+
<string name="authentication">Authentication</string>
2930
<string name="trigger_panic_kit_description">Enable panic responder. PanicKit is a collection of tools for creating “panic buttons” that can trigger a system-wide response when the user is in an anxious or dangerous situation. It enables trigger apps and responder apps to safely and easily connect to each other. The user engages with the trigger app when in a panic situation. The responder apps receive that trigger signal, and individually execute the steps that they were configured to do.</string>
3031
<string name="trigger_tile_description">Enable tile service. It is a button in quick settings panel when you swipe from the top of the screen. This button will mimic to the airplane mode.</string>
3132
<string name="trigger_shortcut_description">Enable icon shortcut. It is a button you will see when you make a long tap on the Wasted icon.</string>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
biometric

0 commit comments

Comments
 (0)