1 /*
2  * Copyright (C) 2016 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 /**
24  * A container that frames a timer circle of some sort. The circle is allowed to grow naturally
25  * according to its layout constraints up to the {@link R.dimen#max_timer_circle_size largest}
26  * allowable size.
27  */
28 public class TimerCircleFrameLayout extends FrameLayout {
29 
TimerCircleFrameLayout(Context context)30     public TimerCircleFrameLayout(Context context) {
31         super(context);
32     }
33 
TimerCircleFrameLayout(Context context, AttributeSet attrs)34     public TimerCircleFrameLayout(Context context, AttributeSet attrs) {
35         super(context, attrs);
36     }
37 
TimerCircleFrameLayout(Context context, AttributeSet attrs, int defStyleAttr)38     public TimerCircleFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
39         super(context, attrs, defStyleAttr);
40     }
41 
42     /**
43      * Note: this method assumes the parent container will specify {@link MeasureSpec#EXACTLY exact}
44      * width and height values.
45      *
46      * @param widthMeasureSpec horizontal space requirements as imposed by the parent
47      * @param heightMeasureSpec vertical space requirements as imposed by the parent
48      */
49     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)50     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
51         final int paddingLeft = getPaddingLeft();
52         final int paddingRight = getPaddingRight();
53 
54         final int paddingTop = getPaddingTop();
55         final int paddingBottom = getPaddingBottom();
56 
57         // Fetch the exact sizes imposed by the parent container.
58         final int width = MeasureSpec.getSize(widthMeasureSpec) - paddingLeft - paddingRight;
59         final int height = MeasureSpec.getSize(heightMeasureSpec) - paddingTop - paddingBottom;
60         final int smallestDimension = Math.min(width, height);
61 
62         // Fetch the absolute maximum circle size allowed.
63         final int maxSize = getResources().getDimensionPixelSize(R.dimen.max_timer_circle_size);
64         final int size = Math.min(smallestDimension, maxSize);
65 
66         // Set the size of this container.
67         widthMeasureSpec = MeasureSpec.makeMeasureSpec(size + paddingLeft + paddingRight,
68                 MeasureSpec.EXACTLY);
69         heightMeasureSpec = MeasureSpec.makeMeasureSpec(size + paddingTop + paddingBottom,
70                 MeasureSpec.EXACTLY);
71 
72         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
73     }
74 }
75