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.keyguard.ui.viewmodel 18 19 import com.android.systemui.animation.Interpolators.EMPHASIZED_ACCELERATE 20 import com.android.systemui.dagger.SysUISingleton 21 import com.android.systemui.keyguard.domain.interactor.FromPrimaryBouncerTransitionInteractor.Companion.TO_GONE_DURATION 22 import com.android.systemui.keyguard.domain.interactor.KeyguardTransitionInteractor 23 import com.android.systemui.keyguard.domain.interactor.PrimaryBouncerInteractor 24 import com.android.systemui.keyguard.shared.model.ScrimAlpha 25 import com.android.systemui.keyguard.ui.KeyguardTransitionAnimationFlow 26 import com.android.systemui.statusbar.SysuiStatusBarStateController 27 import javax.inject.Inject 28 import kotlin.time.Duration.Companion.milliseconds 29 import kotlinx.coroutines.flow.Flow 30 import kotlinx.coroutines.flow.map 31 32 /** 33 * Breaks down PRIMARY_BOUNCER->GONE transition into discrete steps for corresponding views to 34 * consume. 35 */ 36 @SysUISingleton 37 class PrimaryBouncerToGoneTransitionViewModel 38 @Inject 39 constructor( 40 private val interactor: KeyguardTransitionInteractor, 41 private val statusBarStateController: SysuiStatusBarStateController, 42 private val primaryBouncerInteractor: PrimaryBouncerInteractor, 43 ) { 44 private val transitionAnimation = 45 KeyguardTransitionAnimationFlow( 46 transitionDuration = TO_GONE_DURATION, 47 transitionFlow = interactor.primaryBouncerToGoneTransition, 48 ) 49 50 private var leaveShadeOpen: Boolean = false 51 private var willRunDismissFromKeyguard: Boolean = false 52 53 /** Bouncer container alpha */ 54 val bouncerAlpha: Flow<Float> = 55 transitionAnimation.createFlow( 56 duration = 200.milliseconds, <lambda>null57 onStart = { 58 willRunDismissFromKeyguard = primaryBouncerInteractor.willRunDismissFromKeyguard() 59 }, <lambda>null60 onStep = { 61 if (willRunDismissFromKeyguard) { 62 0f 63 } else { 64 1f - it 65 } 66 }, 67 ) 68 69 /** Scrim alpha values */ 70 val scrimAlpha: Flow<ScrimAlpha> = 71 transitionAnimation 72 .createFlow( 73 duration = TO_GONE_DURATION, 74 interpolator = EMPHASIZED_ACCELERATE, <lambda>null75 onStart = { 76 leaveShadeOpen = statusBarStateController.leaveOpenOnKeyguardHide() 77 willRunDismissFromKeyguard = 78 primaryBouncerInteractor.willRunDismissFromKeyguard() 79 }, <lambda>null80 onStep = { 1f - it }, 81 ) <lambda>null82 .map { 83 if (willRunDismissFromKeyguard) { 84 ScrimAlpha() 85 } else if (leaveShadeOpen) { 86 ScrimAlpha( 87 behindAlpha = 1f, 88 notificationsAlpha = 1f, 89 ) 90 } else { 91 ScrimAlpha(behindAlpha = it) 92 } 93 } 94 } 95