1 /*
<lambda>null2  * 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 
18 package com.android.wallpaper.picker
19 
20 import android.content.Context
21 import android.util.AttributeSet
22 import android.widget.FrameLayout
23 import androidx.core.view.children
24 import com.android.wallpaper.util.ScreenSizeCalculator
25 
26 /**
27  * [FrameLayout] that sizes itself and its children layout with a given fixed width and a calculated
28  * height according to the screen aspect ratio.
29  */
30 class FixedWidthDisplayRatioFrameLayout(
31     context: Context,
32     attrs: AttributeSet?,
33 ) : FrameLayout(context, attrs) {
34 
35     override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
36         val screenAspectRatio = ScreenSizeCalculator.getInstance().getScreenAspectRatio(context)
37         val width = MeasureSpec.getSize(widthMeasureSpec)
38         val height = (width * screenAspectRatio).toInt()
39         super.onMeasure(
40             MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
41             MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY),
42         )
43         children.forEach { child ->
44             child.measure(
45                 MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
46                 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY),
47             )
48         }
49     }
50 }
51