summaryrefslogtreecommitdiff
path: root/src/com/android/customization/picker/clock/ui/view/ClockCarouselView.kt
blob: 059e498df8033130d1f76cf723e5f304f4f9f96c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.customization.picker.clock.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.constraintlayout.helper.widget.Carousel
import androidx.constraintlayout.motion.widget.MotionLayout
import com.android.systemui.plugins.ClockController
import com.android.wallpaper.R
import java.lang.Float.max

class ClockCarouselView(
    context: Context,
    attrs: AttributeSet,
) :
    FrameLayout(
        context,
        attrs,
    ) {

    var isCarouselInTransition = false

    val carousel: Carousel
    private val motionLayout: MotionLayout
    private lateinit var adapter: ClockCarouselAdapter
    private var scalingUpClockController: ClockController? = null
    private var scalingDownClockController: ClockController? = null
    private var scalingUpClockView: View? = null
    private var scalingDownClockView: View? = null
    private var showingCardView: View? = null
    private var hidingCardView: View? = null

    init {
        val clockCarousel = LayoutInflater.from(context).inflate(R.layout.clock_carousel, this)
        carousel = clockCarousel.requireViewById(R.id.carousel)
        motionLayout = clockCarousel.requireViewById(R.id.motion_container)
    }

    fun setUpClockCarouselView(
        clockIds: List<String>,
        onGetClockController: (clockId: String) -> ClockController,
        onClockSelected: (clockId: String) -> Unit,
        previewRatio: Float,
    ) {
        adapter =
            ClockCarouselAdapter(clockIds, onGetClockController, onClockSelected, previewRatio)
        carousel.setAdapter(adapter)
        carousel.refresh()
        motionLayout.setTransitionListener(
            object : MotionLayout.TransitionListener {

                override fun onTransitionStarted(
                    motionLayout: MotionLayout?,
                    startId: Int,
                    endId: Int
                ) {
                    isCarouselInTransition = true
                    val scalingDownClockId = adapter.clockIds[carousel.currentIndex]
                    val scalingUpIdx =
                        if (endId == R.id.next) (carousel.currentIndex + 1) % adapter.count()
                        else (carousel.currentIndex - 1 + adapter.count()) % adapter.count()
                    val scalingUpClockId = adapter.clockIds[scalingUpIdx]
                    scalingDownClockController = adapter.onGetClockController(scalingDownClockId)
                    scalingUpClockController = adapter.onGetClockController(scalingUpClockId)
                    scalingDownClockView = motionLayout?.findViewById(R.id.clock_scale_view_2)
                    scalingUpClockView =
                        motionLayout?.findViewById(
                            if (endId == R.id.next) R.id.clock_scale_view_3
                            else R.id.clock_scale_view_1
                        )
                    showingCardView = motionLayout?.findViewById(R.id.item_card_2)
                    hidingCardView =
                        motionLayout?.findViewById(
                            if (endId == R.id.next) R.id.item_card_3 else R.id.item_card_1
                        )
                    setCardAnimationState(true)
                }

                override fun onTransitionChange(
                    motionLayout: MotionLayout?,
                    startId: Int,
                    endId: Int,
                    progress: Float
                ) {
                    scalingDownClockController
                        ?.largeClock
                        ?.animations
                        ?.onPickerCarouselSwiping(
                            1 - progress,
                            previewRatio,
                        )
                    scalingUpClockController
                        ?.largeClock
                        ?.animations
                        ?.onPickerCarouselSwiping(
                            progress,
                            previewRatio,
                        )
                    val scalingUpScale = getScalingUpScale(progress)
                    val scalingDownScale = getScalingDownScale(progress)
                    scalingUpClockView?.scaleX = scalingUpScale
                    scalingUpClockView?.scaleY = scalingUpScale
                    scalingDownClockView?.scaleX = scalingDownScale
                    scalingDownClockView?.scaleY = scalingDownScale
                    showingCardView?.alpha = getShowingAlpha(progress)
                    hidingCardView?.alpha = getHidingAlpha(progress)
                }

                override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) {
                    isCarouselInTransition = false
                    setCardAnimationState(currentId == R.id.start)
                }

                private fun setCardAnimationState(isStart: Boolean) {
                    scalingDownClockView?.scaleX = if (isStart) 1f else CLOCK_CAROUSEL_VIEW_SCALE
                    scalingDownClockView?.scaleY = if (isStart) 1f else CLOCK_CAROUSEL_VIEW_SCALE
                    scalingUpClockView?.scaleX = if (isStart) CLOCK_CAROUSEL_VIEW_SCALE else 1f
                    scalingUpClockView?.scaleY = if (isStart) CLOCK_CAROUSEL_VIEW_SCALE else 1f
                    scalingDownClockController
                        ?.largeClock
                        ?.animations
                        ?.onPickerCarouselSwiping(
                            if (isStart) 1f else 0f,
                            previewRatio,
                        )
                    scalingUpClockController
                        ?.largeClock
                        ?.animations
                        ?.onPickerCarouselSwiping(
                            if (isStart) 0f else 1f,
                            previewRatio,
                        )
                    showingCardView?.alpha = if (isStart) 0f else 1f
                    hidingCardView?.alpha = if (isStart) 1f else 0f
                }

                override fun onTransitionTrigger(
                    motionLayout: MotionLayout?,
                    triggerId: Int,
                    positive: Boolean,
                    progress: Float
                ) {}
            }
        )
    }

    fun setSelectedClockIndex(
        index: Int,
    ) {
        // jumpToIndex to the same position can cause the views unnecessarily populate again.
        // Only call jumpToIndex when the jump-to index is different from the current carousel.
        if (index != carousel.currentIndex) {
            carousel.jumpToIndex(index)
        }
    }

    class ClockCarouselAdapter(
        val clockIds: List<String>,
        val onGetClockController: (clockId: String) -> ClockController,
        private val onClockSelected: (clockId: String) -> Unit,
        private val previewRatio: Float,
    ) : Carousel.Adapter {

        override fun count(): Int {
            return clockIds.size
        }

        override fun populate(view: View?, index: Int) {
            val viewRoot = view as? ViewGroup ?: return
            val cardView =
                getClockCardViewId(viewRoot.id)?.let { viewRoot.findViewById(it) as? View }
                    ?: return
            val clockScaleView =
                getClockScaleViewId(viewRoot.id)?.let { viewRoot.findViewById(it) as? View }
                    ?: return
            val clockHostView =
                getClockHostViewId(viewRoot.id)?.let { viewRoot.findViewById(it) as? ViewGroup }
                    ?: return

            clockHostView.removeAllViews()
            val clockView = onGetClockController(clockIds[index]).largeClock.view
            // Making sure the large clock tick to the correct time
            onGetClockController(clockIds[index]).largeClock.events.onTimeTick()
            // The clock view might still be attached to an existing parent. Detach before adding to
            // another parent.
            (clockView.parent as? ViewGroup)?.removeView(clockView)
            clockHostView.addView(clockView)
            // initialize scaling state for all clocks
            if (!isMiddleView(viewRoot.id)) {
                cardView.alpha = 1f
                clockScaleView.scaleX = CLOCK_CAROUSEL_VIEW_SCALE
                clockScaleView.scaleY = CLOCK_CAROUSEL_VIEW_SCALE
                onGetClockController(clockIds[index])
                    .largeClock
                    .animations
                    .onPickerCarouselSwiping(0F, previewRatio)
            } else {
                cardView.alpha = 0f
                clockScaleView.scaleX = 1f
                clockScaleView.scaleY = 1f
                onGetClockController(clockIds[index])
                    .largeClock
                    .animations
                    .onPickerCarouselSwiping(1F, previewRatio)
            }
        }

        override fun onNewItem(index: Int) {
            onClockSelected.invoke(clockIds[index])
        }
    }

    companion object {
        const val CLOCK_CAROUSEL_VIEW_SCALE = 0.5f

        fun getScalingUpScale(progress: Float) =
            CLOCK_CAROUSEL_VIEW_SCALE + progress * (1f - CLOCK_CAROUSEL_VIEW_SCALE)

        fun getScalingDownScale(progress: Float) = 1f - progress * (1f - CLOCK_CAROUSEL_VIEW_SCALE)

        // This makes the card only starts to reveal in the last quarter of the trip so
        // the card won't overlap the preview.
        fun getShowingAlpha(progress: Float) = max(progress - 0.75f, 0f) * 4

        // This makes the card starts to hide in the first quarter of the trip so the
        // card won't overlap the preview.
        fun getHidingAlpha(progress: Float) = max(1f - progress * 4, 0f)

        fun getClockHostViewId(rootViewId: Int): Int? {
            return when (rootViewId) {
                R.id.item_view_0 -> R.id.clock_host_view_0
                R.id.item_view_1 -> R.id.clock_host_view_1
                R.id.item_view_2 -> R.id.clock_host_view_2
                R.id.item_view_3 -> R.id.clock_host_view_3
                R.id.item_view_4 -> R.id.clock_host_view_4
                else -> null
            }
        }

        fun getClockScaleViewId(rootViewId: Int): Int? {
            return when (rootViewId) {
                R.id.item_view_0 -> R.id.clock_scale_view_0
                R.id.item_view_1 -> R.id.clock_scale_view_1
                R.id.item_view_2 -> R.id.clock_scale_view_2
                R.id.item_view_3 -> R.id.clock_scale_view_3
                R.id.item_view_4 -> R.id.clock_scale_view_4
                else -> null
            }
        }

        fun getClockCardViewId(rootViewId: Int): Int? {
            return when (rootViewId) {
                R.id.item_view_0 -> R.id.item_card_0
                R.id.item_view_1 -> R.id.item_card_1
                R.id.item_view_2 -> R.id.item_card_2
                R.id.item_view_3 -> R.id.item_card_3
                R.id.item_view_4 -> R.id.item_card_4
                else -> null
            }
        }

        fun isMiddleView(rootViewId: Int): Boolean {
            return rootViewId == R.id.item_view_2
        }
    }
}