1 /*
<lambda>null2  * 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.systemui.keyguard.ui.view.layout.sections.transitions
18 
19 import android.animation.Animator
20 import android.animation.ValueAnimator
21 import android.transition.Transition
22 import android.transition.TransitionValues
23 import android.view.ViewGroup
24 import com.android.app.animation.Interpolators
25 import com.android.systemui.plugins.clocks.ClockController
26 
27 class DefaultClockSteppingTransition(
28     private val clock: ClockController,
29 ) : Transition() {
30     init {
31         interpolator = Interpolators.LINEAR
32         duration = KEYGUARD_STATUS_VIEW_CUSTOM_CLOCK_MOVE_DURATION_MS
33         addTarget(clock.largeClock.view)
34     }
35 
36     private fun captureValues(transitionValues: TransitionValues) {
37         transitionValues.values[PROP_BOUNDS_LEFT] = transitionValues.view.left
38         val locationInWindowTmp = IntArray(2)
39         transitionValues.view.getLocationInWindow(locationInWindowTmp)
40         transitionValues.values[PROP_X_IN_WINDOW] = locationInWindowTmp[0]
41     }
42 
43     override fun captureEndValues(transitionValues: TransitionValues) {
44         captureValues(transitionValues)
45     }
46 
47     override fun captureStartValues(transitionValues: TransitionValues) {
48         captureValues(transitionValues)
49     }
50 
51     override fun createAnimator(
52         sceneRoot: ViewGroup,
53         startValues: TransitionValues?,
54         endValues: TransitionValues?
55     ): Animator? {
56         if (startValues == null || endValues == null) {
57             return null
58         }
59         val anim = ValueAnimator.ofFloat(0f, 1f)
60         val fromLeft = startValues.values[PROP_BOUNDS_LEFT] as Int
61         val fromWindowX = startValues.values[PROP_X_IN_WINDOW] as Int
62         val toWindowX = endValues.values[PROP_X_IN_WINDOW] as Int
63         // Using windowX, to determine direction, instead of left, as in RTL the difference of
64         // toLeft - fromLeft is always positive, even when moving left.
65         val direction = if (toWindowX - fromWindowX > 0) 1 else -1
66         anim.addUpdateListener { animation: ValueAnimator ->
67             clock.largeClock.animations.onPositionUpdated(
68                 fromLeft,
69                 direction,
70                 animation.animatedFraction
71             )
72         }
73         return anim
74     }
75 
76     override fun getTransitionProperties(): Array<String> {
77         return TRANSITION_PROPERTIES
78     }
79 
80     companion object {
81         private const val PROP_BOUNDS_LEFT = "DefaultClockSteppingTransition:boundsLeft"
82         private const val PROP_X_IN_WINDOW = "DefaultClockSteppingTransition:xInWindow"
83         private val TRANSITION_PROPERTIES = arrayOf(PROP_BOUNDS_LEFT, PROP_X_IN_WINDOW)
84         private const val KEYGUARD_STATUS_VIEW_CUSTOM_CLOCK_MOVE_DURATION_MS = 1000L
85     }
86 }
87