Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2011 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.email.view;
     18 
     19 import com.android.email.R;
     20 
     21 import android.content.Context;
     22 import android.content.res.TypedArray;
     23 import android.util.AttributeSet;
     24 import android.widget.FrameLayout;
     25 
     26 /**
     27  * A {@link FrameLayout} with the max width/height.
     28  */
     29 public class SizeBoundingFrameLayout extends FrameLayout {
     30     public static final int DIMENSION_DEFAULT = -1; // unspecified
     31 
     32     private int mMaxWidth = DIMENSION_DEFAULT;
     33     private int mMaxHeight = DIMENSION_DEFAULT;
     34 
     35     public SizeBoundingFrameLayout(Context context, AttributeSet attrs, int defStyle) {
     36         super(context, attrs, defStyle);
     37         initFromAttributeSet(context, attrs);
     38     }
     39 
     40     public SizeBoundingFrameLayout(Context context, AttributeSet attrs) {
     41         super(context, attrs);
     42         initFromAttributeSet(context, attrs);
     43     }
     44 
     45     public SizeBoundingFrameLayout(Context context) {
     46         super(context);
     47     }
     48 
     49     private void initFromAttributeSet(Context c, AttributeSet attrs) {
     50         TypedArray a = c.obtainStyledAttributes(attrs,
     51                 R.styleable.SizeBoundingFrameLayout_attributes);
     52         mMaxWidth = a.getDimensionPixelSize(
     53                 R.styleable.SizeBoundingFrameLayout_attributes_maxWidth, DIMENSION_DEFAULT);
     54         mMaxHeight = a.getDimensionPixelSize(
     55                 R.styleable.SizeBoundingFrameLayout_attributes_maxHeight, DIMENSION_DEFAULT);
     56         a.recycle();
     57     }
     58 
     59     /** Set the max width.  Use {@link #DIMENSION_DEFAULT} for unspecified. */
     60     public void setMaxWidth(int maxWidth) {
     61         mMaxWidth = maxWidth;
     62         requestLayout();
     63         invalidate();
     64     }
     65 
     66     public int getMaxWidth() {
     67         return mMaxWidth;
     68     }
     69 
     70     /** Set the max height.  Use {@link #DIMENSION_DEFAULT} for unspecified. */
     71     public void setMaxHeight(int maxHeight) {
     72         mMaxHeight = maxHeight;
     73         requestLayout();
     74         invalidate();
     75     }
     76 
     77     public int getMaxHeight() {
     78         return mMaxHeight;
     79     }
     80 
     81     @Override
     82     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     83         int widthMode = MeasureSpec.getMode(widthMeasureSpec);
     84         int heightMode = MeasureSpec.getMode(heightMeasureSpec);
     85         int widthSize = MeasureSpec.getSize(widthMeasureSpec);
     86         int heightSize = MeasureSpec.getSize(heightMeasureSpec);
     87 
     88         // Limit the size, unless MeasureSpec.EXACTLY
     89         if (mMaxWidth >= 0) {
     90             switch (widthMode) {
     91                 case MeasureSpec.AT_MOST:
     92                     widthSize = Math.min(widthSize, mMaxWidth);
     93                     break;
     94                 case MeasureSpec.UNSPECIFIED:
     95                     widthMode = MeasureSpec.AT_MOST;
     96                     widthSize = mMaxWidth;
     97                     break;
     98             }
     99         }
    100 
    101         if (mMaxHeight >= 0) {
    102             switch (heightMode) {
    103                 case MeasureSpec.AT_MOST:
    104                     heightSize = Math.min(heightSize, mMaxHeight);
    105                     break;
    106                 case MeasureSpec.UNSPECIFIED:
    107                     heightMode = MeasureSpec.AT_MOST;
    108                     heightSize = mMaxHeight;
    109                     break;
    110             }
    111         }
    112         super.onMeasure(MeasureSpec.makeMeasureSpec(widthSize, widthMode),
    113                 MeasureSpec.makeMeasureSpec(heightSize, heightMode));
    114     }
    115 }
    116