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
17package com.android.camera.ui;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.pm.ActivityInfo;
22import android.util.AttributeSet;
23import android.util.Log;
24import 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 */
32public class ControlPanelLayout extends RelativeLayout {
33    private static final String TAG = "ControlPanelLayout";
34
35    public ControlPanelLayout(Context context, AttributeSet attrs) {
36        super(context, attrs);
37    }
38
39    @Override
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 width cannot be bigger than the constraint.
70        if (mode == MeasureSpec.AT_MOST && measuredSize > specSize) {
71            measuredSize = specSize;
72        }
73
74        if (isLandscape) {
75            widthSpec = MeasureSpec.makeMeasureSpec(measuredSize, MeasureSpec.EXACTLY);
76        } else {
77            heightSpec = MeasureSpec.makeMeasureSpec(measuredSize, MeasureSpec.EXACTLY);
78        }
79
80        super.onMeasure(widthSpec, heightSpec);
81    }
82}
83