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