Home | History | Annotate | Download | only in ui
      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.camera.ui;
     18 
     19 import android.content.Context;
     20 import android.content.res.Configuration;
     21 import android.util.AttributeSet;
     22 import android.util.Log;
     23 import android.widget.RelativeLayout;
     24 
     25 /**
     26  * A layout which handles the width of the control panel, which contains
     27  * the shutter button, thumbnail, front/back camera picker, and mode picker.
     28  * The purpose of this is to have a consistent width of control panel in camera,
     29  * camcorder, and panorama modes.
     30  */
     31 public class ControlPanelLayout extends RelativeLayout {
     32     private static final String TAG = "ControlPanelLayout";
     33 
     34     public ControlPanelLayout(Context context, AttributeSet attrs) {
     35         super(context, attrs);
     36     }
     37 
     38     @Override
     39     protected void onMeasure(int widthSpec, int heightSpec) {
     40         int widthSpecSize = MeasureSpec.getSize(widthSpec);
     41         int heightSpecSize = MeasureSpec.getSize(heightSpec);
     42         int measuredSize = 0;
     43         int mode, longSideSize, shortSideSize, specSize;
     44 
     45         boolean isLandscape = (getResources().getConfiguration().orientation
     46                 == Configuration.ORIENTATION_LANDSCAPE);
     47 
     48         if (isLandscape) {
     49             mode = MeasureSpec.getMode(widthSpec);
     50             longSideSize = widthSpecSize;
     51             shortSideSize = heightSpecSize;
     52             specSize = widthSpecSize;
     53         } else {
     54             mode = MeasureSpec.getMode(heightSpec);
     55             longSideSize = heightSpecSize;
     56             shortSideSize = widthSpecSize;
     57             specSize = heightSpecSize;
     58         }
     59 
     60         if (widthSpecSize > 0 && heightSpecSize > 0 && mode == MeasureSpec.AT_MOST) {
     61             // Calculate how big 4:3 preview occupies. Then deduct it from the
     62             // width of the parent.
     63             measuredSize = (int) (longSideSize - shortSideSize / 3.0 * 4.0);
     64         } else {
     65             Log.e(TAG, "layout_xxx of ControlPanelLayout should be wrap_content");
     66         }
     67 
     68         // The size cannot be smaller than minimum constraint.
     69         int minimumSize = (isLandscape) ? getMinimumWidth() : getMinimumHeight();
     70         if (measuredSize < minimumSize) {
     71             measuredSize = minimumSize;
     72         }
     73 
     74         // The size cannot be bigger than the constraint.
     75         if (mode == MeasureSpec.AT_MOST && measuredSize > specSize) {
     76             measuredSize = specSize;
     77         }
     78 
     79         if (isLandscape) {
     80             widthSpec = MeasureSpec.makeMeasureSpec(measuredSize, MeasureSpec.EXACTLY);
     81         } else {
     82             heightSpec = MeasureSpec.makeMeasureSpec(measuredSize, MeasureSpec.EXACTLY);
     83         }
     84 
     85         super.onMeasure(widthSpec, heightSpec);
     86     }
     87 }
     88