1 /*
2  * Copyright (C) 2021 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.keyguard
18 
19 import android.content.Context
20 import android.view.View
21 import android.view.ViewGroup
22 import com.android.systemui.R
23 import com.android.systemui.unfold.SysUIUnfoldScope
24 import com.android.systemui.unfold.UnfoldTransitionProgressProvider.TransitionProgressListener
25 import com.android.systemui.unfold.util.NaturalRotationUnfoldProgressProvider
26 import javax.inject.Inject
27 
28 /**
29  * Translates items away/towards the hinge when the device is opened/closed. This is controlled by
30  * the set of ids, which also dictact which direction to move and when, via a filter function.
31  */
32 @SysUIUnfoldScope
33 class KeyguardUnfoldTransition @Inject constructor(
34     val context: Context,
35     val unfoldProgressProvider: NaturalRotationUnfoldProgressProvider
36 ) {
37 
38     companion object {
39         final val LEFT = -1
40         final val RIGHT = 1
41     }
42 
43     private val filterSplitShadeOnly = { !statusViewCentered }
44     private val filterNever = { true }
45 
46     private val ids = setOf(
47         Triple(R.id.keyguard_status_area, LEFT, filterNever),
48         Triple(R.id.controls_button, LEFT, filterNever),
49         Triple(R.id.lockscreen_clock_view_large, LEFT, filterSplitShadeOnly),
50         Triple(R.id.lockscreen_clock_view, LEFT, filterNever),
51         Triple(R.id.notification_stack_scroller, RIGHT, filterSplitShadeOnly),
52         Triple(R.id.wallet_button, RIGHT, filterNever)
53     )
54     private var parent: ViewGroup? = null
55     private var views = listOf<Triple<View, Int, () -> Boolean>>()
56     private var xTranslationMax = 0f
57 
58     /**
59      * Certain views only need to move if they are not currently centered
60      */
61     var statusViewCentered = false
62 
63     init {
64         unfoldProgressProvider.addCallback(
65             object : TransitionProgressListener {
66                 override fun onTransitionStarted() {
67                     findViews()
68                 }
69 
70                 override fun onTransitionProgress(progress: Float) {
71                     translateViews(progress)
72                 }
73 
74                 override fun onTransitionFinished() {
75                     translateViews(1f)
76                 }
77             }
78         )
79     }
80 
81     /**
82      * Relies on the [parent] to locate views to translate
83      */
84     fun setup(parent: ViewGroup) {
85         this.parent = parent
86         xTranslationMax = context.resources.getDimensionPixelSize(
87             R.dimen.keyguard_unfold_translation_x).toFloat()
88     }
89 
90     /**
91      * Manually translate views based on set direction. At the moment
92      * [UnfoldMoveFromCenterAnimator] exists but moves all views a dynamic distance
93      * from their mid-point. This code instead will only ever translate by a fixed amount.
94      */
95     private fun translateViews(progress: Float) {
96         val xTrans = progress * xTranslationMax - xTranslationMax
97         views.forEach {
98             (view, direction, pred) -> if (pred()) {
99                 view.setTranslationX(xTrans * direction)
100             }
101         }
102     }
103 
104     private fun findViews() {
105         parent?.let { p ->
106             views = ids.mapNotNull {
107                 (id, direction, pred) -> p.findViewById<View>(id)?.let {
108                     Triple(it, direction, pred)
109                 }
110             }
111         }
112     }
113 }
114