aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/BiometricUtils.kt
blob: 136fe28f120e20abe3ba4d2753dea6e75511ca50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright 2019 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.utils

import android.content.Context
import android.hardware.fingerprint.FingerprintManager
import android.os.Build
import androidx.biometric.BiometricPrompt
import androidx.fragment.app.FragmentActivity
import ca.allanwang.kau.utils.string
import com.pitchedapps.frost.R
import kotlinx.coroutines.CompletableDeferred
import java.util.concurrent.Executor
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

typealias BiometricDeferred = CompletableDeferred<BiometricPrompt.CryptoObject?>

/**
 * Container for [BiometricPrompt]
 * Inspired by coroutine's CommonPool
 */
object BiometricUtils {

    private val executor: Executor
        get() = pool ?: getOrCreatePoolSync()

    @Volatile
    private var pool: ExecutorService? = null

    private var lastUnlockTime = -1L

    private const val UNLOCK_TIME_INTERVAL = 15 * 60 * 1000

    /**
     * Checks if biometric authentication is possible
     * Currently, this means checking for enrolled fingerprints
     */
    @Suppress("DEPRECATION")
    fun isSupported(context: Context): Boolean {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false
        val fingerprintManager =
            context.getSystemService(FingerprintManager::class.java) ?: return false
        return fingerprintManager.isHardwareDetected && fingerprintManager.hasEnrolledFingerprints()
    }

    private fun getOrCreatePoolSync(): Executor =
        pool ?: Executors.newSingleThreadExecutor().also { pool = it }

    private fun shouldPrompt(context: Context): Boolean {
        return Prefs.biometricsEnabled && System.currentTimeMillis() - lastUnlockTime > UNLOCK_TIME_INTERVAL
    }

    /**
     * Generates a prompt dialog and attempt to return an auth object.
     * Note that the underlying request will call [androidx.fragment.app.FragmentTransaction.commit],
     * so this cannot happen after onSaveInstanceState.
     */
    fun authenticate(activity: FragmentActivity, force: Boolean = false): BiometricDeferred {
        val deferred: BiometricDeferred = CompletableDeferred()
        if (!force && !shouldPrompt(activity)) {
            deferred.complete(null)
            return deferred
        }
        val info = BiometricPrompt.PromptInfo.Builder()
            .setTitle(activity.string(R.string.biometrics_prompt_title))
            .setNegativeButtonText(activity.string(R.string.kau_cancel))
            .build()
        BiometricPrompt(activity, executor, Callback(activity, deferred)).authenticate(info)
        return deferred
    }

    private class Callback(val activity: FragmentActivity, val deferred: BiometricDeferred) :
        BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            deferred.cancel()
            activity.finish()
        }

        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            lastUnlockTime = System.currentTimeMillis()
            deferred.complete(result.cryptoObject)
        }

        override fun onAuthenticationFailed() {
            deferred.cancel()
            activity.finish()
        }
    }

    /**
     * For completeness we provide a shutdown function.
     * In practice, we initialize the executor only when it is first used,
     * and keep it alive throughout the app lifecycle, as it will be used an arbitrary number of times,
     * with unknown frequency
     */
    @Synchronized
    fun shutdown() {
        pool?.shutdown()
        pool = null
    }
}