1 /* 2 * Copyright (C) 2023 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.systemui.bouncer.ui.viewmodel 18 19 import com.android.systemui.bouncer.domain.interactor.BouncerInteractor 20 import kotlinx.coroutines.CoroutineScope 21 import kotlinx.coroutines.flow.MutableStateFlow 22 import kotlinx.coroutines.flow.StateFlow 23 import kotlinx.coroutines.flow.asStateFlow 24 import kotlinx.coroutines.launch 25 26 /** Holds UI state and handles user input for the password bouncer UI. */ 27 class PasswordBouncerViewModel( 28 private val applicationScope: CoroutineScope, 29 private val interactor: BouncerInteractor, 30 isInputEnabled: StateFlow<Boolean>, 31 ) : 32 AuthMethodBouncerViewModel( 33 isInputEnabled = isInputEnabled, 34 ) { 35 36 private val _password = MutableStateFlow("") 37 /** The password entered so far. */ 38 val password: StateFlow<String> = _password.asStateFlow() 39 40 /** Notifies that the UI has been shown to the user. */ 41 fun onShown() { 42 interactor.resetMessage() 43 } 44 45 /** Notifies that the user has changed the password input. */ 46 fun onPasswordInputChanged(password: String) { 47 if (this.password.value.isEmpty() && password.isNotEmpty()) { 48 interactor.clearMessage() 49 } 50 51 _password.value = password 52 } 53 54 /** Notifies that the user has pressed the key for attempting to authenticate the password. */ 55 fun onAuthenticateKeyPressed() { 56 val password = _password.value.toCharArray().toList() 57 _password.value = "" 58 59 applicationScope.launch { 60 if (interactor.authenticate(password) != true) { 61 showFailureAnimation() 62 } 63 } 64 } 65 } 66