From 5763de5cbdea08e466be35ba68efd0d82fea6ab0 Mon Sep 17 00:00:00 2001 From: lucky Date: Sun, 10 Jul 2022 12:13:10 +0300 Subject: [PATCH] biometric --- app/build.gradle | 5 +- .../main/java/me/lucky/wasted/MainActivity.kt | 49 ++++++++++++++++++- .../trigger/broadcast/BroadcastReceiver.kt | 2 +- app/src/main/res/layout/fragment_main.xml | 1 + app/src/main/res/values/strings.xml | 1 + .../metadata/android/en-US/changelogs/33.txt | 1 + 6 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 fastlane/metadata/android/en-US/changelogs/33.txt diff --git a/app/build.gradle b/app/build.gradle index cd7e456..5bd8df2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "me.lucky.wasted" minSdk 23 targetSdk 32 - versionCode 32 - versionName "1.5.3" + versionCode 33 + versionName "1.5.4" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -49,6 +49,7 @@ dependencies { implementation 'androidx.security:security-crypto:1.0.0' implementation 'androidx.preference:preference-ktx:1.2.0' + implementation 'androidx.biometric:biometric:1.1.0' implementation 'androidx.drawerlayout:drawerlayout:1.1.1' implementation 'info.guardianproject.panic:panic:1.0' } \ No newline at end of file diff --git a/app/src/main/java/me/lucky/wasted/MainActivity.kt b/app/src/main/java/me/lucky/wasted/MainActivity.kt index a5d87f3..a1c3e98 100644 --- a/app/src/main/java/me/lucky/wasted/MainActivity.kt +++ b/app/src/main/java/me/lucky/wasted/MainActivity.kt @@ -2,6 +2,9 @@ package me.lucky.wasted import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.biometric.BiometricManager +import androidx.biometric.BiometricPrompt +import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import me.lucky.wasted.databinding.ActivityMainBinding @@ -17,18 +20,60 @@ open class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) - init() + init1() + if (initBiometric()) return + init2() setup() } - private fun init() { + private fun init1() { prefs = Preferences(this) prefsdb = Preferences(this, encrypted = false) prefs.copyTo(prefsdb) NotificationManager(this).createNotificationChannels() + } + + private fun init2() { replaceFragment(MainFragment()) } + private fun initBiometric(): Boolean { + val authenticators = BiometricManager.Authenticators.BIOMETRIC_STRONG or + BiometricManager.Authenticators.DEVICE_CREDENTIAL + when (BiometricManager + .from(this) + .canAuthenticate(authenticators)) + { + BiometricManager.BIOMETRIC_SUCCESS -> {} + else -> return false + } + val executor = ContextCompat.getMainExecutor(this) + val prompt = BiometricPrompt( + this, + executor, + object : BiometricPrompt.AuthenticationCallback() + { + override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { + super.onAuthenticationError(errorCode, errString) + finishAndRemoveTask() + } + + override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { + super.onAuthenticationSucceeded(result) + init2() + setup() + } + }) + try { + prompt.authenticate(BiometricPrompt.PromptInfo.Builder() + .setTitle(getString(R.string.authentication)) + .setConfirmationRequired(false) + .setAllowedAuthenticators(authenticators) + .build()) + } catch (exc: Exception) { return false } + return true + } + private fun setup() = binding.apply { appBar.setNavigationOnClickListener { drawer.open() diff --git a/app/src/main/java/me/lucky/wasted/trigger/broadcast/BroadcastReceiver.kt b/app/src/main/java/me/lucky/wasted/trigger/broadcast/BroadcastReceiver.kt index db79190..f1ab228 100644 --- a/app/src/main/java/me/lucky/wasted/trigger/broadcast/BroadcastReceiver.kt +++ b/app/src/main/java/me/lucky/wasted/trigger/broadcast/BroadcastReceiver.kt @@ -19,7 +19,7 @@ class BroadcastReceiver : BroadcastReceiver() { if (!prefs.isEnabled) return val secret = prefs.secret assert(secret.isNotEmpty()) - if (intent.getStringExtra(KEY) != secret) return + if (intent.getStringExtra(KEY)?.trim() != secret) return val admin = DeviceAdminManager(context) try { admin.lockNow() diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml index ff3db33..7a45aa5 100644 --- a/app/src/main/res/layout/fragment_main.xml +++ b/app/src/main/res/layout/fragment_main.xml @@ -31,6 +31,7 @@ android:padding="16dp" android:textAlignment="center" android:textStyle="bold" + android:textColor="@color/black" android:textAppearance="?attr/textAppearanceTitleLarge" /> Main Settings GOTO + Authentication 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. 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. Enable icon shortcut. It is a button you will see when you make a long tap on the Wasted icon. diff --git a/fastlane/metadata/android/en-US/changelogs/33.txt b/fastlane/metadata/android/en-US/changelogs/33.txt new file mode 100644 index 0000000..08feb1c --- /dev/null +++ b/fastlane/metadata/android/en-US/changelogs/33.txt @@ -0,0 +1 @@ +biometric