├── app ├── .gitignore ├── release │ └── app-release.aab ├── src │ ├── main │ │ ├── ic_launcher-playstore.png │ │ ├── res │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── values │ │ │ │ ├── ic_launcher_background.xml │ │ │ │ ├── strings.xml │ │ │ │ ├── themes.xml │ │ │ │ └── colors.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── values-v31 │ │ │ │ └── themes.xml │ │ │ ├── drawable │ │ │ │ ├── ic_baseline_fingerprint_24.xml │ │ │ │ ├── ic_launcher_foreground.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── values-night │ │ │ │ └── themes.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── bharathvishal │ │ │ │ └── biometricauthentication │ │ │ │ ├── theme │ │ │ │ ├── Shape.kt │ │ │ │ ├── Type.kt │ │ │ │ ├── Color.kt │ │ │ │ └── Theme.kt │ │ │ │ ├── ApplicationClass │ │ │ │ └── MyApplication.kt │ │ │ │ ├── Constants │ │ │ │ └── Constants.kt │ │ │ │ ├── Activities │ │ │ │ ├── SplashActivity.kt │ │ │ │ ├── MainActivity.kt │ │ │ │ └── MainActivityCompose.kt │ │ │ │ └── Utilities │ │ │ │ └── Utilities.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── bharathvishal │ │ │ └── biometricauthentication │ │ │ └── ExampleUnitTest.kt │ └── androidTest │ │ └── java │ │ └── com │ │ └── bharathvishal │ │ └── biometricauthentication │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle.kts ├── .idea ├── .name ├── codeStyles │ ├── codeStyleConfig.xml │ └── Project.xml ├── vcs.xml ├── compiler.xml ├── AndroidProjectSystem.xml ├── deploymentTargetDropDown.xml ├── migrations.xml ├── kotlinc.xml ├── deploymentTargetSelector.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml ├── jarRepositories.xml └── inspectionProfiles │ └── Project_Default.xml ├── .gitattributes ├── Logo └── icon.png ├── Screenshots ├── 1.png ├── 2.png └── 3.jpg ├── Preview └── PreviewGif.gif ├── gradle ├── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties └── libs.versions.toml ├── .gitignore ├── settings.gradle.kts ├── gradle.properties ├── README.md ├── gradlew.bat ├── gradlew └── LICENSE /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | Biometric Authentication Android -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Logo/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/Logo/icon.png -------------------------------------------------------------------------------- /Screenshots/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/Screenshots/1.png -------------------------------------------------------------------------------- /Screenshots/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/Screenshots/2.png -------------------------------------------------------------------------------- /Screenshots/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/Screenshots/3.jpg -------------------------------------------------------------------------------- /Preview/PreviewGif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/Preview/PreviewGif.gif -------------------------------------------------------------------------------- /app/release/app-release.aab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/release/app-release.aab -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/ic_launcher-playstore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/ic_launcher-playstore.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFFFFF 4 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BharathVishal/Biometric-Authentication-Android/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/AndroidProjectSystem.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /.idea/deploymentTargetDropDown.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Biometric Authentication 3 | Authenticate Using Fingerprint 4 | Device Has PIN/Password 5 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 21 11:48:50 IST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-all.zip 7 | -------------------------------------------------------------------------------- /.idea/migrations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/java/com/bharathvishal/biometricauthentication/theme/Shape.kt: -------------------------------------------------------------------------------- 1 | package com.bharathvishal.biometricauthentication.theme 2 | 3 | 4 | import androidx.compose.foundation.shape.RoundedCornerShape 5 | import androidx.compose.material3.Shapes 6 | import androidx.compose.ui.unit.dp 7 | 8 | 9 | val Shapes = Shapes( 10 | small = RoundedCornerShape(4.dp), 11 | medium = RoundedCornerShape(8.dp), 12 | large = RoundedCornerShape(12.dp) 13 | ) -------------------------------------------------------------------------------- /app/src/test/java/com/bharathvishal/biometricauthentication/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.bharathvishal.biometricauthentication 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 9 | 10 | 13 | 14 | 16 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | google { 4 | content { 5 | includeGroupByRegex("com\\.android.*") 6 | includeGroupByRegex("com\\.google.*") 7 | includeGroupByRegex("androidx.*") 8 | } 9 | } 10 | mavenCentral() 11 | gradlePluginPortal() 12 | } 13 | } 14 | dependencyResolutionManagement { 15 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 16 | repositories { 17 | google() 18 | mavenCentral() 19 | maven { 20 | setUrl("https://jitpack.io") 21 | } 22 | } 23 | } 24 | 25 | rootProject.name = "Biometric Authentication Android" 26 | include(":app") 27 | -------------------------------------------------------------------------------- /.idea/deploymentTargetSelector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 19 | 20 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle.kts. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/main/res/values-v31/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/bharathvishal/biometricauthentication/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.bharathvishal.biometricauthentication 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.bharathvishal.androidbiometricauthentication", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/java/com/bharathvishal/biometricauthentication/theme/Type.kt: -------------------------------------------------------------------------------- 1 | package com.bharathvishal.biometricauthentication.theme 2 | 3 | 4 | import androidx.compose.material3.Typography 5 | import androidx.compose.ui.text.TextStyle 6 | import androidx.compose.ui.text.font.FontFamily 7 | import androidx.compose.ui.text.font.FontWeight 8 | import androidx.compose.ui.unit.sp 9 | 10 | // Set of Material typography styles to start with 11 | val Typography = Typography( 12 | bodyMedium = TextStyle( 13 | fontFamily = FontFamily.Default, 14 | fontWeight = FontWeight.Normal, 15 | fontSize = 16.sp, 16 | lineHeight = 20.sp, 17 | letterSpacing = 0.5.sp 18 | ), 19 | bodyLarge = TextStyle( 20 | fontFamily = FontFamily.Default, 21 | fontWeight = FontWeight.Normal, 22 | fontSize = 16.sp, 23 | lineHeight = 24.sp, 24 | letterSpacing = 0.5.sp 25 | ) 26 | ) -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/bharathvishal/biometricauthentication/ApplicationClass/MyApplication.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2018-2025 Bharath Vishal G. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | **/ 18 | 19 | package com.bharathvishal.biometricauthentication.applicationClass 20 | 21 | import android.app.Application 22 | import com.google.android.material.color.DynamicColors 23 | 24 | class MyApplication : Application() { 25 | override fun onCreate() { 26 | super.onCreate() 27 | 28 | //For dynamic theming on Android 12 and above 29 | DynamicColors.applyToActivitiesIfAvailable(this) 30 | } 31 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx3072m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app"s APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official 22 | android.defaults.buildfeatures.buildconfig=true 23 | android.nonTransitiveRClass=false 24 | android.nonFinalResIds=false -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Biometric-Authentication-Android 2 | 3 |

4 | 5 |

6 | 7 | A sample implementation of AndroidX biometrics API using Kotlin. Authenticate using biometrics or PIN/Password if biometrics isn't available on device. Fully implemented in Jetpack compose using Material 3 dynamic theming and also has a separate implementation with xml 8 | 9 |   10 | ### Usage: 11 | 1. Open project and sync gradle. 12 | 2. Modify the code to suit your needs. 13 | 3. Watch out for more features. 14 | 15 |   16 | ### Stats Displayed : 17 | - Availability of biometric hardware 18 | - Whether device has user fingerprint 19 | - Whether device has PIN/Password 20 | 21 | 22 |   23 | ### Preview : 24 | ![Preview](https://github.com/BharathVishal/Biometric-Authentication-Android/blob/master/Preview/PreviewGif.gif) 25 | 26 | 27 |   28 | ### Screenshots : 29 | ![Screenshot 1](https://github.com/BharathVishal/Biometric-Authentication-Android/blob/master/Screenshots/1.png?s=10) 30 | ![Screenshot 2](https://github.com/BharathVishal/Biometric-Authentication-Android/blob/master/Screenshots/2.png?s=10) 31 | ![Screenshot 2](https://github.com/BharathVishal/Biometric-Authentication-Android/blob/master/Screenshots/3.jpg?s=10) 32 | 33 | 34 | 35 |   36 | ### Year developed : 37 | 2020 38 | 39 | 40 |   41 | 42 | ### SDK Info : 43 | Min SDK : 23 | Target SDK : 36 | Gradle : 8.13.0 | Kotlin | Jetpack Compose 44 | 45 | 46 |   47 | 48 | 49 | ### Android Studio Version : 50 | Android Studio Narwhal 4 Feature Drop | 2025.1.4 51 | 52 | 53 | 54 |   55 | 56 | #### License : 57 | [Apache License 2.0](https://github.com/BharathVishal/Biometric-Authentication-Android/blob/master/LICENSE) 58 |   59 | 60 |   61 | #### 62 | Android is a trademark of Google LLC. 63 | 64 |   65 | 66 | © 2020-2025. Developed by Bharath Vishal G (https://github.com/BharathVishal). 67 | App logo - Fingerprint material icon. 68 | 69 | Thank you. :slightly_smiling_face: 70 | 71 | -------------------------------------------------------------------------------- /app/src/main/java/com/bharathvishal/biometricauthentication/Constants/Constants.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2018-2025 Bharath Vishal G. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | **/ 18 | 19 | package com.bharathvishal.biometricauthentication.constants 20 | 21 | object Constants { 22 | const val AUTHENTICATION_FAILED = "Authentication failed" 23 | const val AUTHENTICATION_SUCCEEDED = "Authentication succeeded" 24 | const val AUTHENTICATION_ERROR = "Authentication error" 25 | 26 | const val BIOMETRIC_AUTHENTICATION = "Biometric Authentication" 27 | const val USE_DEVICE_PASSWORD = "Use device password" 28 | const val BIOMETRIC_AUTHENTICATION_SUBTITLE = "Use your fingerprint to authenticate" 29 | const val BIOMETRIC_AUTHENTICATION_DESCRIPTION = "This app uses your makes use of device biometrics (user fingerprint) to authenticate the dialog." 30 | 31 | const val AUTHENTICATE_OTHER="Authenticate using Device Password/PIN" 32 | const val AUTHENTICATE_FINGERPRINT="Authenticate using Fingerprint" 33 | 34 | const val AVAILABLE="Available" 35 | const val UNAVAILABLE="Unavailable" 36 | const val TRUE="True" 37 | const val FALSE="False" 38 | const val CANCEL="Cancel" 39 | 40 | const val PASSWORD_PIN_AUTHENTICATION = "Password/PIN Authentication" 41 | const val PASSWORD_PIN_AUTHENTICATION_SUBTITLE = "Authenticate using Device Password/PIN" 42 | const val PASSWORD_PIN_AUTHENTICATION_DESCRIPTION = "This app uses your makes use of device password/pin to authenticate the dialog." 43 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 19 | 20 | 28 | 29 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 47 | 48 | 49 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/bharathvishal/biometricauthentication/Activities/SplashActivity.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2018-2025 Bharath Vishal G. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | **/ 18 | 19 | package com.bharathvishal.biometricauthentication.activities 20 | 21 | import android.annotation.SuppressLint 22 | import android.content.Context 23 | import android.content.Intent 24 | import android.os.Build 25 | import android.os.Bundle 26 | import androidx.appcompat.app.AppCompatActivity 27 | import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen 28 | 29 | class SplashActivity : AppCompatActivity() { 30 | private var actvityContext: Context? = null 31 | 32 | @SuppressLint("SetTextI18n") 33 | override fun onCreate(savedInstanceState: Bundle?) { 34 | super.onCreate(savedInstanceState) 35 | 36 | if (isOSSandAbove) { 37 | val splashScreen = installSplashScreen() 38 | } 39 | 40 | actvityContext = this@SplashActivity 41 | 42 | 43 | //For testing purpose 44 | //1 - Default Main Activity with xml 45 | //2 - Main activity with Jetpack Compose 46 | val activityTypeToLaunch = 2 47 | 48 | if (activityTypeToLaunch == 1) { 49 | val intent = Intent(actvityContext, MainActivity::class.java) 50 | startActivity(intent) 51 | finish() 52 | } else { 53 | val intent = Intent(actvityContext, MainActivityCompose::class.java) 54 | startActivity(intent) 55 | finish() 56 | } 57 | } 58 | 59 | 60 | private val isOSSandAbove: Boolean 61 | get() { 62 | val sdkInt = Build.VERSION.SDK_INT 63 | return sdkInt >= 31 64 | } 65 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_baseline_fingerprint_24.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 34 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 11 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 11 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | alias(libs.plugins.android.application) 3 | alias(libs.plugins.jetbrains.kotlin.android) 4 | alias(libs.plugins.compose.compiler) 5 | } 6 | 7 | android { 8 | namespace = "com.bharathvishal.biometricauthentication" 9 | compileSdk = 36 10 | defaultConfig { 11 | applicationId = "com.bharathvishal.biometricauthentication" 12 | vectorDrawables { 13 | useSupportLibrary = true 14 | } 15 | minSdk = 23 16 | targetSdk = 36 17 | versionCode = 93 18 | versionName = "4.1" 19 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" 20 | } 21 | buildFeatures { 22 | viewBinding = true 23 | compose = true 24 | } 25 | buildTypes { 26 | debug { 27 | isDebuggable = true 28 | isMinifyEnabled = true 29 | proguardFiles( 30 | getDefaultProguardFile("proguard-android-optimize.txt"), 31 | "proguard-rules.pro" 32 | ) 33 | } 34 | release { 35 | isDebuggable = false 36 | isMinifyEnabled = true 37 | proguardFiles( 38 | getDefaultProguardFile("proguard-android-optimize.txt"), 39 | "proguard-rules.pro" 40 | ) 41 | } 42 | } 43 | compileOptions { 44 | sourceCompatibility = JavaVersion.VERSION_17 45 | targetCompatibility = JavaVersion.VERSION_17 46 | } 47 | 48 | kotlinOptions { 49 | jvmTarget = "17" 50 | } 51 | composeOptions { 52 | kotlinCompilerExtensionVersion = "1.5.15" 53 | } 54 | 55 | packaging { 56 | jniLibs { 57 | excludes += "/META-INF/{AL2.0,LGPL2.1}" 58 | } 59 | resources { 60 | excludes += "/META-INF/{AL2.0,LGPL2.1}" 61 | } 62 | } 63 | } 64 | 65 | dependencies { 66 | implementation(libs.androidx.appcompat) 67 | implementation(libs.androidx.legacy.legacysupport) 68 | implementation(libs.jetbrains.kotlinx.couroutine) 69 | implementation(libs.coil.kt) 70 | implementation(libs.jetbrains.kotlin.stdlib) 71 | implementation(libs.google.android.material) 72 | implementation(libs.androidx.constraintlayout) 73 | testImplementation(libs.junit) 74 | implementation(libs.glide) 75 | annotationProcessor(libs.glide.compiler) 76 | androidTestImplementation(libs.androidx.espresso.core) 77 | implementation(libs.androidx.cardview) 78 | implementation(libs.androidx.compose.material3) 79 | implementation(libs.androidx.compose.material3.windowsize) 80 | implementation(libs.androidx.compose.material3.windowsize) 81 | implementation(libs.androidx.lifecycle.runtime.ktx) 82 | implementation(libs.androidx.activity.compose) 83 | androidTestImplementation(libs.androidx.ui.test.junit4) 84 | debugImplementation(libs.androidx.compose.ui.tooling) 85 | implementation(libs.androidx.compose.ui.tooling.preview) 86 | implementation(libs.androidx.core.core.splashscreen) 87 | implementation(libs.androidx.compose.material.material.icons) 88 | implementation(libs.google.accompanist.accompanist) 89 | implementation(libs.androidx.biometric) 90 | } 91 | 92 | -------------------------------------------------------------------------------- /app/src/main/java/com/bharathvishal/biometricauthentication/theme/Color.kt: -------------------------------------------------------------------------------- 1 | package com.bharathvishal.biometricauthentication.theme 2 | 3 | import androidx.compose.ui.graphics.Color 4 | 5 | val md_theme_light_primary = Color(0xFF7A564A) 6 | val md_theme_light_onPrimary = Color(0xFFFFFFFF) 7 | val md_theme_light_primaryContainer = Color(0xFFFFDBD0) 8 | val md_theme_light_onPrimaryContainer = Color(0xFF2E140C) 9 | val md_theme_light_secondary = Color(0xFF695C58) 10 | val md_theme_light_onSecondary = Color(0xFFFFFFFF) 11 | val md_theme_light_secondaryContainer = Color(0xFFF1DFD9) 12 | val md_theme_light_onSecondaryContainer = Color(0xFF231917) 13 | val md_theme_light_tertiary = Color(0xFF645E4D) 14 | val md_theme_light_onTertiary = Color(0xFFFFFFFF) 15 | val md_theme_light_tertiaryContainer = Color(0xFFEBE2CC) 16 | val md_theme_light_onTertiaryContainer = Color(0xFF1F1B0E) 17 | val md_theme_light_error = Color(0xFFBA1A1A) 18 | val md_theme_light_onError = Color(0xFFFFFFFF) 19 | val md_theme_light_errorContainer = Color(0xFFFFDAD6) 20 | val md_theme_light_onErrorContainer = Color(0xFF410002) 21 | val md_theme_light_background = Color(0xFFFFFBFF) 22 | val md_theme_light_onBackground = Color(0xFF1E1B1A) 23 | val md_theme_light_surface = Color(0xFFFFFBFF) 24 | val md_theme_light_onSurface = Color(0xFF1E1B1A) 25 | val md_theme_light_surfaceVariant = Color(0xFFEBE0DD) 26 | val md_theme_light_onSurfaceVariant = Color(0xFF4C4544) 27 | val md_theme_light_outline = Color(0xFF7D7573) 28 | val md_theme_light_shadow = Color(0xFF000000) 29 | val md_theme_light_inverseSurface = Color(0xFF33302F) 30 | val md_theme_light_inverseOnSurface = Color(0xFFF7EFED) 31 | val md_theme_light_inversePrimary = Color(0xFFEBBCAD) 32 | val md_theme_light_surfaceTint = Color(0xFF7A564A) 33 | 34 | val md_theme_dark_primary = Color(0xFFEBBCAD) 35 | val md_theme_dark_onPrimary = Color(0xFF47291F) 36 | val md_theme_dark_primaryContainer = Color(0xFF603F34) 37 | val md_theme_dark_onPrimaryContainer = Color(0xFFFFDBD0) 38 | val md_theme_dark_secondary = Color(0xFFD4C3BE) 39 | val md_theme_dark_onSecondary = Color(0xFF392E2B) 40 | val md_theme_dark_secondaryContainer = Color(0xFF504441) 41 | val md_theme_dark_onSecondaryContainer = Color(0xFFF1DFD9) 42 | val md_theme_dark_tertiary = Color(0xFFCEC6B1) 43 | val md_theme_dark_onTertiary = Color(0xFF353022) 44 | val md_theme_dark_tertiaryContainer = Color(0xFF4C4637) 45 | val md_theme_dark_onTertiaryContainer = Color(0xFFEBE2CC) 46 | val md_theme_dark_error = Color(0xFFFFB4AB) 47 | val md_theme_dark_onError = Color(0xFF690005) 48 | val md_theme_dark_errorContainer = Color(0xFF93000A) 49 | val md_theme_dark_onErrorContainer = Color(0xFFFFB4AB) 50 | val md_theme_dark_background = Color(0xFF1E1B1A) 51 | val md_theme_dark_onBackground = Color(0xFFE8E1DF) 52 | val md_theme_dark_surface = Color(0xFF1E1B1A) 53 | val md_theme_dark_onSurface = Color(0xFFE8E1DF) 54 | val md_theme_dark_surfaceVariant = Color(0xFF4C4544) 55 | val md_theme_dark_onSurfaceVariant = Color(0xFFCEC4C2) 56 | val md_theme_dark_outline = Color(0xFF978F8D) 57 | val md_theme_dark_shadow = Color(0xFF000000) 58 | val md_theme_dark_inverseSurface = Color(0xFFE8E1DF) 59 | val md_theme_dark_inverseOnSurface = Color(0xFF33302F) 60 | val md_theme_dark_inversePrimary = Color(0xFF7A564A) 61 | val md_theme_dark_surfaceTint = Color(0xFFEBBCAD) 62 | 63 | 64 | val seed = Color(0xFFFECDBE) 65 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 34 | 35 | 36 | 54 | 55 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | #6D23F8 3 | #FFFFFF 4 | #EADDFF 5 | #21005E 6 | #006B5F 7 | #FFFFFF 8 | #4FFBE5 9 | #00201B 10 | #5E42D8 11 | #FFFFFF 12 | #E6DEFF 13 | #190065 14 | #BA1B1B 15 | #FFDAD4 16 | #FFFFFF 17 | #410001 18 | #FFFBFE 19 | #1C1B1E 20 | #FFFBFE 21 | #1C1B1E 22 | #E6E0EB 23 | #48454E 24 | #79757F 25 | #F4EFF3 26 | #313033 27 | #D0BCFF 28 | #D0BCFF 29 | #380094 30 | #5200CE 31 | #EADDFF 32 | #19DEC9 33 | #003730 34 | #005047 35 | #4FFBE5 36 | #CABEFF 37 | #2D009D 38 | #4522C0 39 | #E6DEFF 40 | #FFB4A9 41 | #930006 42 | #680003 43 | #FFDAD4 44 | #1C1B1E 45 | #E6E1E6 46 | #1C1B1E 47 | #E6E1E6 48 | #48454E 49 | #CAC4D0 50 | #948F99 51 | #1C1B1E 52 | #E6E1E6 53 | #6D23F8 54 | #6200EE 55 | #BA1B1B 56 | 57 | #303030 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/bharathvishal/biometricauthentication/Utilities/Utilities.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2018-2025 Bharath Vishal G. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | **/ 18 | 19 | package com.bharathvishal.biometricauthentication.utilities 20 | 21 | import android.app.Activity 22 | import android.app.KeyguardManager 23 | import android.content.Context 24 | import android.os.Build 25 | import android.view.Gravity 26 | import android.view.View 27 | import android.widget.TextView 28 | import androidx.appcompat.app.AppCompatActivity 29 | import androidx.biometric.BiometricManager 30 | import androidx.constraintlayout.widget.ConstraintLayout 31 | import com.bharathvishal.biometricauthentication.R 32 | import com.google.android.material.snackbar.Snackbar 33 | 34 | object Utilities { 35 | fun showSnackBar(snackTitle: String?, act: Activity) { 36 | try { 37 | val view1 = act.findViewById(R.id.constraintlayoutMain)!! 38 | val snackbar: Snackbar = Snackbar.make(view1, snackTitle!!, Snackbar.LENGTH_SHORT) 39 | val view: View = snackbar.view 40 | 41 | if (!act.isFinishing) 42 | snackbar.show() 43 | 44 | val txtv = view.findViewById(R.id.snackbar_text) as TextView 45 | txtv.gravity = Gravity.CENTER_HORIZONTAL 46 | } catch (e: java.lang.Exception) { 47 | e.printStackTrace() 48 | } 49 | } 50 | 51 | 52 | fun isBiometricHardWareAvailable(con: Context): Boolean { 53 | var result = false 54 | val biometricManager = BiometricManager.from(con) 55 | 56 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { 57 | when (biometricManager.canAuthenticate(BiometricManager.Authenticators.DEVICE_CREDENTIAL or BiometricManager.Authenticators.BIOMETRIC_STRONG)) { 58 | BiometricManager.BIOMETRIC_SUCCESS -> result = true 59 | BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> result = false 60 | BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> result = false 61 | BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> result = false 62 | BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> 63 | result = true 64 | 65 | BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> 66 | result = true 67 | 68 | BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> 69 | result = false 70 | } 71 | } else { 72 | when (biometricManager.canAuthenticate()) { 73 | BiometricManager.BIOMETRIC_SUCCESS -> result = true 74 | BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> result = false 75 | BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> result = false 76 | BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> result = false 77 | } 78 | } 79 | return result 80 | } 81 | 82 | 83 | fun deviceHasPasswordPinLock(con: Context): Boolean { 84 | val keymgr = con.getSystemService(AppCompatActivity.KEYGUARD_SERVICE) as KeyguardManager 85 | if (keymgr.isKeyguardSecure) 86 | return true 87 | return false 88 | } 89 | } -------------------------------------------------------------------------------- /gradle/libs.versions.toml: -------------------------------------------------------------------------------- 1 | [versions] 2 | agp = "8.13.0" 3 | appCompatVer="1.7.1" 4 | kotlinxCoroutinesVer="1.10.2" 5 | coilVer="2.7.0" 6 | kotlinVer = "2.2.0" 7 | materialVer = "1.12.0" 8 | constraintlayoutVer="2.2.1" 9 | coreKtxVer = "1.16.0" 10 | glideVer="4.16.0" 11 | cardViewVer="1.0.0" 12 | compose3MaterialVer="1.3.2" 13 | lifecycleRuntimeKtxVer="2.9.1" 14 | activityComposeVer="1.10.1" 15 | composeUItestVer="1.8.3" 16 | uitoolingVer="1.8.3" 17 | uitoolingpreviewVer="1.8.3" 18 | splashScreenVer="1.0.1" 19 | junitVersion = "4.13.2" 20 | materialiconsVer="1.7.8" 21 | espressoCoreVer = "3.6.1" 22 | accompanistVer = "0.36.0" 23 | legacySupportVer="1.0.0" 24 | biometricSupportVer="1.1.0" 25 | 26 | 27 | [libraries] 28 | androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appCompatVer" } 29 | jetbrains-kotlinx-couroutine = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "kotlinxCoroutinesVer" } 30 | coil-kt = { group = "io.coil-kt", name = "coil-compose", version.ref = "coilVer" } 31 | jetbrains-kotlin-stdlib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib", version.ref = "kotlinVer" } 32 | google-android-material = { group = "com.google.android.material", name = "material", version.ref = "materialVer" } 33 | androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayoutVer" } 34 | androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtxVer" } 35 | junit = { group = "junit", name = "junit", version.ref = "junitVersion" } 36 | glide = { group = "com.github.bumptech.glide", name = "glide", version.ref = "glideVer" } 37 | glide-compiler = { group = "com.github.bumptech.glide", name = "compiler", version.ref = "glideVer" } 38 | androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCoreVer" } 39 | androidx-cardview = { group = "androidx.cardview", name = "cardview", version.ref = "cardViewVer" } 40 | androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3", version.ref = "compose3MaterialVer" } 41 | androidx-compose-material3-windowsize= { group = "androidx.compose.material3", name = "material3-window-size-class", version.ref = "compose3MaterialVer" } 42 | androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtxVer" } 43 | androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityComposeVer" } 44 | androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4",version.ref="composeUItestVer" } 45 | androidx-compose-ui-tooling = { group = "androidx.compose.ui", name="ui-tooling",version.ref = "uitoolingVer" } 46 | androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name="ui-tooling-preview",version.ref = "uitoolingpreviewVer" } 47 | androidx-core-core-splashscreen= { group = "androidx.core", name = "core-splashscreen",version.ref="splashScreenVer" } 48 | androidx-compose-material-material-icons = { group = "androidx.compose.material", name = "material-icons-extended", version.ref = "materialiconsVer" } 49 | google-accompanist-accompanist = { group = "com.google.accompanist", name = "accompanist-systemuicontroller", version.ref = "accompanistVer" } 50 | androidx-legacy-legacysupport = { group = "androidx.legacy", name = "legacy-support-v4", version.ref = "legacySupportVer" } 51 | androidx-biometric = { group = "androidx.biometric", name = "biometric", version.ref = "biometricSupportVer" } 52 | 53 | 54 | [plugins] 55 | android-application = { id = "com.android.application", version.ref = "agp" } 56 | jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlinVer" } 57 | compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlinVer" } -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 73 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 20 | 22 | 23 | 24 | 26 | 27 | 28 |
29 | 30 | 31 | 32 | xmlns:android 33 | 34 | ^$ 35 | 36 | 37 | 38 |
39 |
40 | 41 | 42 | 43 | xmlns:.* 44 | 45 | ^$ 46 | 47 | 48 | BY_NAME 49 | 50 |
51 |
52 | 53 | 54 | 55 | .*:id 56 | 57 | http://schemas.android.com/apk/res/android 58 | 59 | 60 | 61 |
62 |
63 | 64 | 65 | 66 | .*:name 67 | 68 | http://schemas.android.com/apk/res/android 69 | 70 | 71 | 72 |
73 |
74 | 75 | 76 | 77 | name 78 | 79 | ^$ 80 | 81 | 82 | 83 |
84 |
85 | 86 | 87 | 88 | style 89 | 90 | ^$ 91 | 92 | 93 | 94 |
95 |
96 | 97 | 98 | 99 | .* 100 | 101 | ^$ 102 | 103 | 104 | BY_NAME 105 | 106 |
107 |
108 | 109 | 110 | 111 | .* 112 | 113 | http://schemas.android.com/apk/res/android 114 | 115 | 116 | ANDROID_ATTRIBUTE_ORDER 117 | 118 |
119 |
120 | 121 | 122 | 123 | .* 124 | 125 | .* 126 | 127 | 128 | BY_NAME 129 | 130 |
131 |
132 |
133 |
134 | 135 | 137 |
138 |
-------------------------------------------------------------------------------- /app/src/main/java/com/bharathvishal/biometricauthentication/theme/Theme.kt: -------------------------------------------------------------------------------- 1 | package com.bharathvishal.biometricauthentication.theme 2 | 3 | 4 | import android.os.Build 5 | import android.util.Log 6 | import androidx.compose.foundation.isSystemInDarkTheme 7 | import androidx.compose.material3.* 8 | import androidx.compose.runtime.Composable 9 | import androidx.compose.runtime.SideEffect 10 | import androidx.compose.ui.platform.LocalContext 11 | import androidx.compose.ui.platform.LocalView 12 | import com.google.accompanist.systemuicontroller.rememberSystemUiController 13 | 14 | val LightColorScheme = lightColorScheme( 15 | primary = md_theme_light_primary, 16 | onPrimary = md_theme_light_onPrimary, 17 | primaryContainer = md_theme_light_primaryContainer, 18 | onPrimaryContainer = md_theme_light_onPrimaryContainer, 19 | secondary = md_theme_light_secondary, 20 | onSecondary = md_theme_light_onSecondary, 21 | secondaryContainer = md_theme_light_secondaryContainer, 22 | onSecondaryContainer = md_theme_light_onSecondaryContainer, 23 | tertiary = md_theme_light_tertiary, 24 | onTertiary = md_theme_light_onTertiary, 25 | tertiaryContainer = md_theme_light_tertiaryContainer, 26 | onTertiaryContainer = md_theme_light_onTertiaryContainer, 27 | error = md_theme_light_error, 28 | onError = md_theme_light_onError, 29 | errorContainer = md_theme_light_errorContainer, 30 | onErrorContainer = md_theme_light_onErrorContainer, 31 | background = md_theme_light_background, 32 | onBackground = md_theme_light_onBackground, 33 | surface = md_theme_light_surface, 34 | onSurface = md_theme_light_onSurface, 35 | surfaceVariant = md_theme_light_surfaceVariant, 36 | onSurfaceVariant = md_theme_light_onSurfaceVariant, 37 | outline = md_theme_light_outline, 38 | inverseSurface = md_theme_light_inverseSurface, 39 | inverseOnSurface = md_theme_light_inverseOnSurface, 40 | inversePrimary = md_theme_light_inversePrimary, 41 | surfaceTint = md_theme_light_surfaceTint, 42 | ) 43 | 44 | 45 | val DarkColorScheme = darkColorScheme( 46 | primary = md_theme_dark_primary, 47 | onPrimary = md_theme_dark_onPrimary, 48 | primaryContainer = md_theme_dark_primaryContainer, 49 | onPrimaryContainer = md_theme_dark_onPrimaryContainer, 50 | secondary = md_theme_dark_secondary, 51 | onSecondary = md_theme_dark_onSecondary, 52 | secondaryContainer = md_theme_dark_secondaryContainer, 53 | onSecondaryContainer = md_theme_dark_onSecondaryContainer, 54 | tertiary = md_theme_dark_tertiary, 55 | onTertiary = md_theme_dark_onTertiary, 56 | tertiaryContainer = md_theme_dark_tertiaryContainer, 57 | onTertiaryContainer = md_theme_dark_onTertiaryContainer, 58 | error = md_theme_dark_error, 59 | onError = md_theme_dark_onError, 60 | errorContainer = md_theme_dark_errorContainer, 61 | onErrorContainer = md_theme_dark_onErrorContainer, 62 | background = md_theme_dark_background, 63 | onBackground = md_theme_dark_onBackground, 64 | surface = md_theme_dark_surface, 65 | onSurface = md_theme_dark_onSurface, 66 | surfaceVariant = md_theme_dark_surfaceVariant, 67 | onSurfaceVariant = md_theme_dark_onSurfaceVariant, 68 | outline = md_theme_dark_outline, 69 | inverseSurface = md_theme_dark_inverseSurface, 70 | inverseOnSurface = md_theme_dark_inverseOnSurface, 71 | inversePrimary = md_theme_dark_inversePrimary, 72 | surfaceTint = md_theme_dark_surfaceTint, 73 | ) 74 | 75 | 76 | @Suppress("IMPLICIT_CAST_TO_ANY") 77 | @Composable 78 | fun Material3AppTheme( 79 | darkTheme: Boolean = isSystemInDarkTheme(), 80 | // Dynamic color is available on Android 12+ 81 | dynamicColor: Boolean = true, 82 | content: @Composable () -> Unit 83 | ) { 84 | 85 | val systemUiController = rememberSystemUiController() 86 | val colorScheme = when { 87 | dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { 88 | val context = LocalContext.current 89 | if (darkTheme) { 90 | systemUiController.setStatusBarColor( 91 | color = DarkColorScheme.surface, 92 | darkIcons = false 93 | ) 94 | Log.d("came1","came into dark theme") 95 | dynamicDarkColorScheme(context) 96 | } else { 97 | systemUiController.setStatusBarColor( 98 | color = LightColorScheme.surface, 99 | darkIcons = true 100 | ) 101 | Log.d("came1","came into light theme") 102 | 103 | dynamicLightColorScheme(context) 104 | } 105 | } 106 | darkTheme -> { 107 | systemUiController.setStatusBarColor(color = DarkColorScheme.surface, darkIcons = false) 108 | DarkColorScheme 109 | } 110 | else -> { 111 | systemUiController.setStatusBarColor( 112 | color = LightColorScheme.surface, 113 | darkIcons = true 114 | ) 115 | LightColorScheme 116 | } 117 | } 118 | val view = LocalView.current 119 | if (!view.isInEditMode) { 120 | SideEffect { 121 | //(view.context as Activity).window.statusBarColor = colorScheme.primary.toArgb() 122 | //WindowCompat.getInsetsController((view.context as Activity).window, view).isAppearanceLightStatusBars = darkTheme 123 | //ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme 124 | } 125 | } 126 | 127 | MaterialTheme( 128 | colorScheme = colorScheme, 129 | typography = Typography, 130 | content = content, 131 | shapes = Shapes 132 | ) 133 | } -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/java/com/bharathvishal/biometricauthentication/Activities/MainActivity.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2018-2025 Bharath Vishal G. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | **/ 18 | 19 | package com.bharathvishal.biometricauthentication.activities 20 | 21 | import android.content.Context 22 | import android.content.res.Configuration 23 | import android.graphics.Color 24 | import android.os.Build 25 | import android.os.Bundle 26 | import android.util.Log 27 | import android.view.View 28 | import android.view.WindowInsets 29 | import androidx.activity.enableEdgeToEdge 30 | import androidx.appcompat.app.AppCompatActivity 31 | import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG 32 | import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL 33 | import androidx.biometric.BiometricPrompt 34 | import androidx.core.content.ContextCompat 35 | import androidx.core.view.ViewCompat 36 | import androidx.core.view.WindowCompat 37 | import androidx.core.view.WindowInsetsCompat 38 | import androidx.core.view.updatePadding 39 | import com.bharathvishal.biometricauthentication.R 40 | import com.bharathvishal.biometricauthentication.constants.Constants 41 | import com.bharathvishal.biometricauthentication.databinding.ActivityMainBinding 42 | import com.bharathvishal.biometricauthentication.utilities.Utilities 43 | import com.google.android.material.color.DynamicColors 44 | import java.util.concurrent.Executor 45 | 46 | 47 | class MainActivity : AppCompatActivity() { 48 | private lateinit var activityContext: Context 49 | 50 | private lateinit var executor: Executor 51 | private lateinit var biometricPrompt: BiometricPrompt 52 | private lateinit var promptInfo: BiometricPrompt.PromptInfo 53 | 54 | private lateinit var binding: ActivityMainBinding 55 | 56 | 57 | override fun onCreate(savedInstanceState: Bundle?) { 58 | try { 59 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { 60 | enableEdgeToEdge() 61 | } 62 | } catch (e: Exception) { 63 | e.printStackTrace() 64 | } 65 | 66 | super.onCreate(savedInstanceState) 67 | binding = ActivityMainBinding.inflate(layoutInflater) 68 | 69 | val view = binding.root 70 | setContentView(view) 71 | 72 | activityContext = this 73 | 74 | try { 75 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { 76 | val viewTempAppBar = findViewById(R.id.appbarLayout) 77 | viewTempAppBar.setOnApplyWindowInsetsListener { view, insets -> 78 | val statusBarInsets = insets.getInsets(WindowInsets.Type.statusBars()) 79 | 80 | val nightModeFlags: Int = view.resources 81 | .configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK 82 | val isDarkMode = nightModeFlags == Configuration.UI_MODE_NIGHT_YES 83 | val isDynamicTheme = DynamicColors.isDynamicColorAvailable() 84 | // Adjust padding to avoid overlap 85 | view.setPadding(0, statusBarInsets.top, 0, 0) 86 | //insets 87 | WindowInsets.CONSUMED 88 | } 89 | 90 | val tempL: View = findViewById(R.id.cardviewMain1) 91 | ViewCompat.setOnApplyWindowInsetsListener(tempL) { view, windowInsets -> 92 | val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemGestures()) 93 | // Apply the insets as padding to the view. Here, set all the dimensions 94 | // as appropriate to your layout. You can also update the view's margin if 95 | // more appropriate. 96 | tempL.updatePadding(0, 0, 0, insets.bottom) 97 | 98 | // Return CONSUMED if you don't want the window insets to keep passing down 99 | // to descendant views. 100 | WindowInsetsCompat.CONSUMED 101 | } 102 | } 103 | } catch (e: Exception) { 104 | e.printStackTrace() 105 | } 106 | 107 | if (Utilities.deviceHasPasswordPinLock(activityContext)) 108 | binding.DeviceHasPINPasswordLock.text = Constants.TRUE 109 | else 110 | binding.DeviceHasPINPasswordLock.text = Constants.FALSE 111 | 112 | executor = ContextCompat.getMainExecutor(activityContext) 113 | 114 | setPrompt() 115 | 116 | if (Utilities.isBiometricHardWareAvailable(activityContext)) { 117 | binding.DeviceHasBiometricFeatures.text = Constants.AVAILABLE 118 | binding.DeviceHasFingerPrint.text = Constants.TRUE 119 | 120 | //Enable the button if the device has biometric hardware available 121 | binding.authenticatefingerprintbutton.isEnabled = true 122 | 123 | initBiometricPrompt( 124 | Constants.BIOMETRIC_AUTHENTICATION, 125 | Constants.BIOMETRIC_AUTHENTICATION_SUBTITLE, 126 | Constants.BIOMETRIC_AUTHENTICATION_DESCRIPTION, 127 | false 128 | ) 129 | } else { 130 | binding.DeviceHasBiometricFeatures.text = Constants.UNAVAILABLE 131 | binding.DeviceHasFingerPrint.text = Constants.FALSE 132 | binding.authenticatefingerprintbutton.isEnabled = false 133 | 134 | //Fallback, use device password/pin 135 | if (Utilities.deviceHasPasswordPinLock(activityContext)) { 136 | binding.authenticatefingerprintbutton.isEnabled = true 137 | binding.authenticatefingerprintbutton.text = Constants.AUTHENTICATE_OTHER 138 | 139 | initBiometricPrompt( 140 | Constants.PASSWORD_PIN_AUTHENTICATION, 141 | Constants.PASSWORD_PIN_AUTHENTICATION_SUBTITLE, 142 | Constants.PASSWORD_PIN_AUTHENTICATION_DESCRIPTION, 143 | true 144 | ) 145 | } 146 | } 147 | } 148 | 149 | private fun setPrompt() { 150 | biometricPrompt = BiometricPrompt(this, executor, 151 | object : BiometricPrompt.AuthenticationCallback() { 152 | override fun onAuthenticationError( 153 | errorCode: Int, 154 | errString: CharSequence 155 | ) { 156 | super.onAuthenticationError(errorCode, errString) 157 | Utilities.showSnackBar( 158 | Constants.AUTHENTICATION_ERROR + " " + errString, 159 | activityContext as MainActivity 160 | ) 161 | } 162 | 163 | override fun onAuthenticationSucceeded( 164 | result: BiometricPrompt.AuthenticationResult 165 | ) { 166 | super.onAuthenticationSucceeded(result) 167 | Utilities.showSnackBar( 168 | Constants.AUTHENTICATION_SUCCEEDED, 169 | activityContext as MainActivity 170 | ) 171 | binding.textViewAuthResult.visibility = View.VISIBLE 172 | } 173 | 174 | override fun onAuthenticationFailed() { 175 | super.onAuthenticationFailed() 176 | Utilities.showSnackBar( 177 | Constants.AUTHENTICATION_FAILED, 178 | activityContext as MainActivity 179 | ) 180 | } 181 | }) 182 | } 183 | 184 | private fun initBiometricPrompt( 185 | title: String, 186 | subtitle: String, 187 | description: String, 188 | setDeviceCred: Boolean 189 | ) { 190 | if (setDeviceCred) { 191 | /*For API level > 30 192 | Newer API setAllowedAuthenticators is used*/ 193 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { 194 | val authFlag = DEVICE_CREDENTIAL or BIOMETRIC_STRONG 195 | promptInfo = BiometricPrompt.PromptInfo.Builder() 196 | .setTitle(title) 197 | .setSubtitle(subtitle) 198 | .setDescription(description) 199 | .setAllowedAuthenticators(authFlag) 200 | .build() 201 | } else { 202 | /*SetDeviceCredentials method deprecation is ignored here 203 | as this block is for API level<30*/ 204 | @Suppress("DEPRECATION") 205 | promptInfo = BiometricPrompt.PromptInfo.Builder() 206 | .setTitle(title) 207 | .setSubtitle(subtitle) 208 | .setDescription(description) 209 | .setDeviceCredentialAllowed(true) 210 | .build() 211 | } 212 | } else { 213 | promptInfo = BiometricPrompt.PromptInfo.Builder() 214 | .setTitle(title) 215 | .setSubtitle(subtitle) 216 | .setDescription(description) 217 | .setNegativeButtonText(Constants.CANCEL) 218 | .build() 219 | } 220 | 221 | binding.authenticatefingerprintbutton.setOnClickListener { 222 | biometricPrompt.authenticate(promptInfo) 223 | } 224 | } 225 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 25 | 26 | 33 | 34 | 39 | 40 | 41 | 42 | 54 | 55 | 56 | 62 | 63 | 64 | 68 | 69 | 70 | 75 | 76 | 82 | 83 | 84 | 85 | 89 | 90 | 91 | 104 | 105 | 106 | 107 | 108 | 115 | 116 | 117 | 121 | 122 | 123 | 135 | 136 | 137 | 149 | 150 | 151 | 152 | 153 | 154 | 161 | 162 | 163 | 167 | 168 | 169 | 181 | 182 | 183 | 195 | 196 | 197 | 198 | 199 | 206 | 207 | 208 | 212 | 213 | 214 | 226 | 227 | 228 | 240 | 241 | 242 | 243 | 250 | 251 | 252 | 263 | 264 | 265 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | -------------------------------------------------------------------------------- /app/src/main/java/com/bharathvishal/biometricauthentication/Activities/MainActivityCompose.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2018-2025 Bharath Vishal G. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | **/ 18 | 19 | package com.bharathvishal.biometricauthentication.activities 20 | 21 | import android.annotation.SuppressLint 22 | import android.content.Context 23 | import android.content.res.Configuration 24 | import android.os.Build 25 | import android.os.Bundle 26 | import android.util.Log 27 | import androidx.activity.compose.setContent 28 | import androidx.activity.enableEdgeToEdge 29 | import androidx.appcompat.app.AppCompatActivity 30 | import androidx.biometric.BiometricManager 31 | import androidx.biometric.BiometricPrompt 32 | import androidx.compose.animation.AnimatedVisibility 33 | import androidx.compose.foundation.Image 34 | import androidx.compose.foundation.isSystemInDarkTheme 35 | import androidx.compose.foundation.layout.* 36 | import androidx.compose.foundation.shape.RoundedCornerShape 37 | import androidx.compose.material3.* 38 | import androidx.compose.runtime.Composable 39 | import androidx.compose.runtime.mutableStateOf 40 | import androidx.compose.ui.Alignment 41 | import androidx.compose.ui.Modifier 42 | import androidx.compose.ui.graphics.Color 43 | import androidx.compose.ui.res.painterResource 44 | import androidx.compose.ui.text.style.TextAlign 45 | import androidx.compose.ui.tooling.preview.Preview 46 | import androidx.compose.ui.unit.dp 47 | import androidx.core.content.ContextCompat 48 | import androidx.core.graphics.toColorInt 49 | import com.bharathvishal.biometricauthentication.R 50 | import com.bharathvishal.biometricauthentication.constants.Constants 51 | import com.bharathvishal.biometricauthentication.theme.Material3AppTheme 52 | import com.bharathvishal.biometricauthentication.utilities.Utilities 53 | import com.google.android.material.color.DynamicColors 54 | import kotlinx.coroutines.* 55 | import java.util.concurrent.Executor 56 | 57 | class MainActivityCompose : AppCompatActivity(), CoroutineScope by MainScope() { 58 | private lateinit var activityContext: Context 59 | 60 | private lateinit var executor: Executor 61 | private lateinit var biometricPrompt: BiometricPrompt 62 | private lateinit var promptInfo: BiometricPrompt.PromptInfo 63 | 64 | private var biometricHardwareVal = mutableStateOf("Unknown") 65 | private var deviceHasUserFingerPrintVal = mutableStateOf("Unknown") 66 | private var deviceHasPinPasswordVal = mutableStateOf("Unknown") 67 | private var authFingerprintButtonEnabledVal = mutableStateOf(true) 68 | private var authFingerprintButtonTextVal = mutableStateOf("Authenticate using Fingerprint") 69 | private var biometricAuthResultVisibilityVal = mutableStateOf(false) 70 | private var showSnackBarVal = mutableStateOf(false) 71 | private var snackBarMessageVal = mutableStateOf("-") 72 | 73 | override fun onCreate(savedInstanceState: Bundle?) { 74 | 75 | try { 76 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { 77 | enableEdgeToEdge() 78 | } 79 | } catch (e: Exception) { 80 | e.printStackTrace() 81 | } 82 | 83 | 84 | super.onCreate(savedInstanceState) 85 | 86 | //Applies Material dynamic theming 87 | try { 88 | DynamicColors.applyToActivityIfAvailable(this) 89 | } catch (e: Exception) { 90 | e.printStackTrace() 91 | } 92 | 93 | activityContext = this 94 | 95 | setContent { 96 | Material3AppTheme(darkTheme = isSystemInDarkTheme()) { 97 | Surface( 98 | modifier = Modifier.fillMaxSize(), 99 | color = MaterialTheme.colorScheme.background 100 | ) { 101 | MainViewImplementation() 102 | } 103 | } 104 | } 105 | 106 | 107 | if (Utilities.deviceHasPasswordPinLock(activityContext)) 108 | deviceHasPinPasswordVal.value = Constants.TRUE 109 | else 110 | deviceHasPinPasswordVal.value = Constants.FALSE 111 | 112 | executor = ContextCompat.getMainExecutor(activityContext) 113 | 114 | setPrompt() 115 | 116 | if (Utilities.isBiometricHardWareAvailable(activityContext)) { 117 | biometricHardwareVal.value = Constants.AVAILABLE 118 | deviceHasUserFingerPrintVal.value = Constants.TRUE 119 | authFingerprintButtonTextVal.value = Constants.AUTHENTICATE_FINGERPRINT 120 | 121 | //Enable the button if the device has biometric hardware available 122 | authFingerprintButtonEnabledVal.value = true 123 | 124 | initBiometricPrompt( 125 | Constants.BIOMETRIC_AUTHENTICATION, 126 | Constants.BIOMETRIC_AUTHENTICATION_SUBTITLE, 127 | Constants.BIOMETRIC_AUTHENTICATION_DESCRIPTION, 128 | false 129 | ) 130 | } else { 131 | biometricHardwareVal.value = Constants.UNAVAILABLE 132 | deviceHasUserFingerPrintVal.value = Constants.FALSE 133 | authFingerprintButtonEnabledVal.value = false 134 | 135 | //Fallback, use device password/pin 136 | if (Utilities.deviceHasPasswordPinLock(activityContext)) { 137 | authFingerprintButtonEnabledVal.value = true 138 | authFingerprintButtonTextVal.value = Constants.AUTHENTICATE_OTHER 139 | 140 | initBiometricPrompt( 141 | Constants.PASSWORD_PIN_AUTHENTICATION, 142 | Constants.PASSWORD_PIN_AUTHENTICATION_SUBTITLE, 143 | Constants.PASSWORD_PIN_AUTHENTICATION_DESCRIPTION, 144 | true 145 | ) 146 | } 147 | } 148 | } 149 | 150 | @OptIn(ExperimentalMaterial3Api::class) 151 | @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") 152 | @Composable 153 | fun MainViewImplementation() { 154 | Column { 155 | TopAppBarMain() 156 | Box( 157 | modifier = Modifier 158 | .padding(1.dp) 159 | .fillMaxWidth() 160 | .fillMaxHeight() 161 | ) { 162 | CardViewMain() 163 | Box( 164 | modifier = Modifier 165 | .fillMaxWidth() 166 | .wrapContentHeight() 167 | .align(Alignment.BottomStart), 168 | ) { 169 | SnackBarViewComposable(showSnackBarVal.value, snackBarMessageVal.value) 170 | } 171 | } 172 | } 173 | } 174 | 175 | //Top App bar composable function 176 | @OptIn(ExperimentalMaterial3Api::class) 177 | @Composable 178 | fun TopAppBarMain() { 179 | TopAppBar( 180 | title = { Text("Biometric Authentication") }, 181 | colors = TopAppBarDefaults.topAppBarColors( 182 | titleContentColor = MaterialTheme.colorScheme.onSurfaceVariant, 183 | containerColor = MaterialTheme.colorScheme.surface 184 | ) 185 | ) 186 | } 187 | 188 | //CardView composable function 189 | @Suppress("UNNECESSARY_SAFE_CALL") 190 | @Composable 191 | fun CardViewMain() { 192 | Column { 193 | Spacer(modifier = Modifier.padding(top = 6.dp)) 194 | Card( 195 | modifier = Modifier 196 | .padding(10.dp) 197 | .fillMaxWidth() 198 | .wrapContentHeight(), 199 | colors = CardDefaults.cardColors(), 200 | shape = RoundedCornerShape(12.dp) 201 | ) { 202 | Column( 203 | modifier = Modifier 204 | .padding(16.dp) 205 | .fillMaxWidth() 206 | .wrapContentHeight(), 207 | verticalArrangement = Arrangement.Top, 208 | horizontalAlignment = Alignment.CenterHorizontally, 209 | ) 210 | { 211 | ImageLogo() 212 | TextHeader() 213 | 214 | RowComponentInCard("Biometric Hardware", biometricHardwareVal.value) 215 | Divider(thickness = 0.5.dp) 216 | RowComponentInCard( 217 | "Device Has User Fingerprint", 218 | deviceHasUserFingerPrintVal.value 219 | ) 220 | Divider(thickness = 0.5.dp) 221 | RowComponentInCard("Device Has PIN/Password", deviceHasPinPasswordVal.value) 222 | Spacer(modifier = Modifier.padding(top = 6.dp)) 223 | ButtonAuthenticate( 224 | authFingerprintButtonTextVal.value, 225 | authFingerprintButtonEnabledVal.value 226 | ) 227 | Spacer(modifier = Modifier.padding(top = 6.dp)) 228 | TextAuthenticationResult(biometricAuthResultVisibilityVal.value) 229 | 230 | }//end of column 231 | }//end of card 232 | }//end of outer column 233 | }//end of card view main 234 | 235 | 236 | //Biometric Image Logo composable function 237 | @Composable 238 | fun ImageLogo() { 239 | Image( 240 | painter = painterResource(R.drawable.ic_baseline_fingerprint_24), 241 | contentDescription = "Image Logo", 242 | modifier = Modifier 243 | .requiredHeight(90.dp) 244 | .requiredWidth(90.dp) 245 | .padding(5.dp) 246 | ) 247 | } 248 | 249 | //Biometric Authentication app name Text 250 | @Composable 251 | fun TextHeader() { 252 | Text( 253 | text = "BIOMETRIC AUTHENTICATION", 254 | textAlign = TextAlign.Center, 255 | modifier = Modifier 256 | .padding(5.dp) 257 | .fillMaxWidth(), 258 | style = MaterialTheme.typography.labelLarge 259 | ) 260 | } 261 | 262 | 263 | //Row component composable function for Biometric related info 264 | @Composable 265 | fun RowComponentInCard(strDesc: String, mutableVal: String) { 266 | Row( 267 | modifier = Modifier 268 | .fillMaxWidth() 269 | .padding(5.dp), 270 | verticalAlignment = Alignment.CenterVertically, 271 | horizontalArrangement = Arrangement.Center, 272 | ) { 273 | Column( 274 | modifier = Modifier.weight(0.5f), 275 | horizontalAlignment = Alignment.CenterHorizontally, 276 | verticalArrangement = Arrangement.Center 277 | ) { 278 | Text( 279 | text = strDesc, 280 | textAlign = TextAlign.Left, 281 | modifier = Modifier 282 | .padding(5.dp), 283 | style = MaterialTheme.typography.bodyMedium, 284 | color = MaterialTheme.colorScheme.primary 285 | ) 286 | } 287 | Column( 288 | modifier = Modifier.weight(0.5f), 289 | horizontalAlignment = Alignment.CenterHorizontally, 290 | verticalArrangement = Arrangement.Center 291 | ) { 292 | Text( 293 | text = mutableVal, 294 | textAlign = TextAlign.Right, 295 | modifier = Modifier 296 | .padding(5.dp), 297 | style = MaterialTheme.typography.bodyMedium, 298 | ) 299 | } 300 | } 301 | } 302 | 303 | @Composable 304 | fun ButtonAuthenticate(btnText: String, enabledState: Boolean) { 305 | OutlinedButton( 306 | onClick = { biometricPrompt.authenticate(promptInfo) }, 307 | contentPadding = PaddingValues( 308 | start = 20.dp, 309 | top = 12.dp, 310 | end = 20.dp, 311 | bottom = 12.dp 312 | ), 313 | enabled = enabledState 314 | ) { 315 | Spacer(Modifier.size(ButtonDefaults.IconSpacing)) 316 | Text(text = btnText) 317 | } 318 | } 319 | 320 | 321 | @Composable 322 | fun TextAuthenticationResult(visibilityState: Boolean) { 323 | AnimatedVisibility(visible = visibilityState) { 324 | Text( 325 | text = "Authentication success", 326 | textAlign = TextAlign.Center, 327 | modifier = Modifier 328 | .padding(5.dp), 329 | style = MaterialTheme.typography.bodyLarge, 330 | color = Color("#9ccc65".toColorInt()) 331 | ) 332 | Spacer(modifier = Modifier.padding(bottom = 10.dp)) 333 | } 334 | } 335 | 336 | @Composable 337 | fun SnackBarViewComposable(visibilityState: Boolean, message: String) { 338 | AnimatedVisibility(visible = visibilityState) { 339 | Snackbar(action = {}) { 340 | Text(text = message) 341 | } 342 | } 343 | } 344 | 345 | private fun setPrompt() { 346 | biometricPrompt = BiometricPrompt(this, executor, 347 | object : BiometricPrompt.AuthenticationCallback() { 348 | override fun onAuthenticationError( 349 | errorCode: Int, 350 | errString: CharSequence 351 | ) { 352 | super.onAuthenticationError(errorCode, errString) 353 | biometricAuthResultVisibilityVal.value = false 354 | 355 | launch(Dispatchers.Default) { 356 | showSnackBarVal.value = true 357 | snackBarMessageVal.value = Constants.AUTHENTICATION_ERROR 358 | delay(1500) 359 | withContext(Dispatchers.Main) { 360 | showSnackBarVal.value = false 361 | } 362 | } 363 | } 364 | 365 | override fun onAuthenticationSucceeded( 366 | result: BiometricPrompt.AuthenticationResult 367 | ) { 368 | super.onAuthenticationSucceeded(result) 369 | biometricAuthResultVisibilityVal.value = true 370 | 371 | launch(Dispatchers.Default) { 372 | showSnackBarVal.value = true 373 | snackBarMessageVal.value = Constants.AUTHENTICATION_SUCCEEDED 374 | delay(1500) 375 | withContext(Dispatchers.Main) { 376 | showSnackBarVal.value = false 377 | } 378 | } 379 | } 380 | 381 | override fun onAuthenticationFailed() { 382 | super.onAuthenticationFailed() 383 | Utilities.showSnackBar( 384 | Constants.AUTHENTICATION_FAILED, 385 | activityContext as MainActivityCompose 386 | ) 387 | } 388 | }) 389 | } 390 | 391 | private fun initBiometricPrompt( 392 | title: String, 393 | subtitle: String, 394 | description: String, 395 | setDeviceCred: Boolean 396 | ) { 397 | if (setDeviceCred) { 398 | /*For API level > 30 399 | Newer API setAllowedAuthenticators is used*/ 400 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { 401 | val authFlag = 402 | BiometricManager.Authenticators.DEVICE_CREDENTIAL or BiometricManager.Authenticators.BIOMETRIC_STRONG 403 | promptInfo = BiometricPrompt.PromptInfo.Builder() 404 | .setTitle(title) 405 | .setSubtitle(subtitle) 406 | .setDescription(description) 407 | .setAllowedAuthenticators(authFlag) 408 | .build() 409 | } else { 410 | /*SetDeviceCredentials method deprecation is ignored here 411 | as this block is for API level<30*/ 412 | @Suppress("DEPRECATION") 413 | promptInfo = BiometricPrompt.PromptInfo.Builder() 414 | .setTitle(title) 415 | .setSubtitle(subtitle) 416 | .setDescription(description) 417 | .setDeviceCredentialAllowed(true) 418 | .build() 419 | } 420 | } else { 421 | promptInfo = BiometricPrompt.PromptInfo.Builder() 422 | .setTitle(title) 423 | .setSubtitle(subtitle) 424 | .setDescription(description) 425 | .setNegativeButtonText(Constants.CANCEL) 426 | .build() 427 | } 428 | } 429 | 430 | 431 | override fun onDestroy() { 432 | super.onDestroy() 433 | 434 | //Cancels this coroutine score 435 | cancel() 436 | } 437 | 438 | //Preview for jetpack composable view 439 | @Preview(showBackground = true) 440 | @Composable 441 | fun DefaultPreview() { 442 | Material3AppTheme { 443 | MainViewImplementation() 444 | } 445 | } 446 | } --------------------------------------------------------------------------------