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 package com.android.customization.picker.clock.ui.view
17 
18 import android.content.Context
19 import android.util.AttributeSet
20 import android.view.View
21 import android.view.View.MeasureSpec.EXACTLY
22 import android.view.ViewGroup
23 import androidx.constraintlayout.widget.ConstraintLayout
24 import com.android.customization.picker.clock.shared.ClockSize
25 import com.android.systemui.plugins.clocks.ClockController
26 import com.android.wallpaper.util.ScreenSizeCalculator
27 
28 /**
29  * Parent view for the clock view. We will calculate the current display size and the preview size
30  * and scale down the clock view to fit in the preview.
31  */
32 class ClockConstraintLayoutHostView(context: Context, attrs: AttributeSet?) :
33     ConstraintLayout(context, attrs) {
34     override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
35         val screenSize = ScreenSizeCalculator.getInstance().getScreenSize(display)
36         super.onMeasure(
37             MeasureSpec.makeMeasureSpec(screenSize.x, EXACTLY),
38             MeasureSpec.makeMeasureSpec(screenSize.y, EXACTLY),
39         )
40         val ratio = MeasureSpec.getSize(widthMeasureSpec) / screenSize.x.toFloat()
41         scaleX = ratio
42         scaleY = ratio
43     }
44 
45     companion object {
46         fun addClockViews(
47             clockController: ClockController,
48             rootView: ClockConstraintLayoutHostView,
49             size: ClockSize,
50         ) {
51             clockController.let { clock ->
52                 when (size) {
53                     ClockSize.DYNAMIC -> {
54                         clock.largeClock.layout.views.forEach {
55                             if (it.parent != null) {
56                                 (it.parent as ViewGroup).removeView(it)
57                             }
58                             rootView.addView(it).apply { it.visibility = View.VISIBLE }
59                         }
60                     }
61 
62                     ClockSize.SMALL -> {
63                         clock.smallClock.layout.views.forEach {
64                             if (it.parent != null) {
65                                 (it.parent as ViewGroup).removeView(it)
66                             }
67                             rootView.addView(it).apply { it.visibility = View.VISIBLE }
68                         }
69                     }
70                 }
71             }
72         }
73     }
74 }
75