1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.biometrics.fingerprint2.domain.interactor 18 19 import android.content.Context 20 import android.database.ContentObserver 21 import android.provider.Settings 22 import android.util.Log 23 import kotlinx.coroutines.CoroutineDispatcher 24 import kotlinx.coroutines.channels.awaitClose 25 import kotlinx.coroutines.flow.Flow 26 import kotlinx.coroutines.flow.callbackFlow 27 import kotlinx.coroutines.flow.flowOn 28 29 /** Interface that indicates if press to auth is on or off. */ 30 interface PressToAuthInteractor { 31 /** Indicates true if the PressToAuth feature is enabled, false otherwise. */ 32 val isEnabled: Flow<Boolean> 33 } 34 35 /** Indicates whether or not the press to auth feature is enabled. */ 36 class PressToAuthInteractorImpl( 37 private val context: Context, 38 private val backgroundDispatcher: CoroutineDispatcher, 39 ) : PressToAuthInteractor { 40 41 /** A flow that contains the status of the press to auth feature. */ 42 override val isEnabled: Flow<Boolean> = <lambda>null43 callbackFlow { 44 val callback = 45 object : ContentObserver(null) { 46 override fun onChange(selfChange: Boolean) { 47 Log.d(TAG, "SFPS_PERFORMANT_AUTH_ENABLED#onchange") 48 trySend(getPressToAuth()) 49 } 50 } 51 52 context.contentResolver.registerContentObserver( 53 Settings.Secure.getUriFor(Settings.Secure.SFPS_PERFORMANT_AUTH_ENABLED), 54 false, 55 callback, 56 context.userId, 57 ) 58 trySend(getPressToAuth()) 59 awaitClose { context.contentResolver.unregisterContentObserver(callback) } 60 } 61 .flowOn(backgroundDispatcher) 62 63 /** Returns true if press to auth is enabled */ getPressToAuthnull64 private fun getPressToAuth(): Boolean { 65 var toReturn: Int = 66 Settings.Secure.getIntForUser( 67 context.contentResolver, 68 Settings.Secure.SFPS_PERFORMANT_AUTH_ENABLED, 69 -1, 70 context.userId, 71 ) 72 if (toReturn == -1) { 73 toReturn = 74 if ( 75 context.resources.getBoolean(com.android.internal.R.bool.config_performantAuthDefault) 76 ) { 77 1 78 } else { 79 0 80 } 81 Settings.Secure.putIntForUser( 82 context.contentResolver, 83 Settings.Secure.SFPS_PERFORMANT_AUTH_ENABLED, 84 toReturn, 85 context.userId, 86 ) 87 } 88 return toReturn == 1 89 } 90 91 companion object { 92 const val TAG = "PressToAuthInteractor" 93 } 94 } 95