1 /* 2 * Copyright (C) 2020 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.deskclock 18 19 import android.content.Context 20 import android.util.AttributeSet 21 import android.widget.FrameLayout 22 23 import kotlin.math.min 24 25 /** 26 * A container that frames a timer circle of some sort. The circle is allowed to grow naturally 27 * according to its layout constraints up to the [largest][R.dimen.max_timer_circle_size] 28 * allowable size. 29 */ 30 class TimerCircleFrameLayout : FrameLayout { 31 constructor(context: Context) : super(context) 32 33 constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) 34 35 constructor( 36 context: Context, 37 attrs: AttributeSet?, 38 defStyleAttr: Int 39 ) : super(context, attrs, defStyleAttr) 40 41 /** 42 * Note: this method assumes the parent container will specify [exact][MeasureSpec.EXACTLY] 43 * width and height values. 44 * 45 * @param widthMeasureSpec horizontal space requirements as imposed by the parent 46 * @param heightMeasureSpec vertical space requirements as imposed by the parent 47 */ onMeasurenull48 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 49 var variableWidthMeasureSpec = widthMeasureSpec 50 var variableHeightMeasureSpec = heightMeasureSpec 51 val paddingLeft = paddingLeft 52 val paddingRight = paddingRight 53 54 val paddingTop = paddingTop 55 val paddingBottom = paddingBottom 56 57 // Fetch the exact sizes imposed by the parent container. 58 val width = MeasureSpec.getSize(variableWidthMeasureSpec) - paddingLeft - paddingRight 59 val height = MeasureSpec.getSize(variableHeightMeasureSpec) - paddingTop - paddingBottom 60 val smallestDimension = min(width, height) 61 62 // Fetch the absolute maximum circle size allowed. 63 val maxSize = resources.getDimensionPixelSize(R.dimen.max_timer_circle_size) 64 val size = min(smallestDimension, maxSize) 65 66 // Set the size of this container. 67 variableWidthMeasureSpec = MeasureSpec.makeMeasureSpec(size + paddingLeft + paddingRight, 68 MeasureSpec.EXACTLY) 69 variableHeightMeasureSpec = MeasureSpec.makeMeasureSpec(size + paddingTop + paddingBottom, 70 MeasureSpec.EXACTLY) 71 72 super.onMeasure(variableWidthMeasureSpec, variableHeightMeasureSpec) 73 } 74 }