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 com.android.camera.CameraPreference.OnPreferenceChangedListener;
20import com.android.camera.PreferenceGroup;
21import com.android.camera.R;
22import com.android.camera.Util;
23
24import android.content.Context;
25import android.util.AttributeSet;
26import android.view.MotionEvent;
27import android.view.View;
28
29/**
30 * On the tablet UI, we have IndicatorControlWheelContainer which contains a
31 * ShutterButton, an IndicatorControlWheel(which combines first-level and
32 * second-level indicators and a ZoomControlWheel).
33 */
34public class IndicatorControlWheelContainer extends IndicatorControlContainer {
35    public static final int STROKE_WIDTH = 87;
36    public static final int SHUTTER_BUTTON_RADIUS = 74;
37    public static final int FULL_WHEEL_RADIUS = 93;
38
39    private static final String TAG = "IndicatorControlWheelContainer";
40
41    private View mShutterButton;
42    private double mShutterButtonRadius;
43    private IndicatorControlWheel mIndicatorControlWheel;
44    private int mCenterX, mCenterY;
45
46    public IndicatorControlWheelContainer(Context context, AttributeSet attrs) {
47        super(context, attrs);
48    }
49
50    @Override
51    protected void onFinishInflate() {
52        mShutterButton = findViewById(R.id.shutter_button);
53        mShutterButtonRadius = Util.dpToPixel(SHUTTER_BUTTON_RADIUS);
54
55        mIndicatorControlWheel = (IndicatorControlWheel) findViewById(
56                R.id.indicator_control_wheel);
57    }
58
59    public void initialize(Context context, PreferenceGroup group,
60            boolean isZoomSupported, String[] keys, String[] otherSettingKeys) {
61        mIndicatorControlWheel.initialize(context, group, isZoomSupported,
62                keys, otherSettingKeys);
63    }
64
65    public void onIndicatorEvent(int event) {
66    }
67
68    @Override
69    public boolean dispatchTouchEvent(MotionEvent event) {
70        if (!onFilterTouchEventForSecurity(event)) return false;
71
72        int action = event.getAction();
73
74        double dx = event.getX() - mCenterX;
75        double dy = mCenterY - event.getY();
76        double radius = Math.sqrt(dx * dx + dy * dy);
77
78        // Check if the event should be dispatched to the shutter button.
79        if (radius <= mShutterButtonRadius) {
80            if (mIndicatorControlWheel.getVisibility() == View.VISIBLE) {
81                mIndicatorControlWheel.onTouchOutBound();
82            }
83            if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
84                return mShutterButton.dispatchTouchEvent(event);
85            }
86            return false;
87        }
88
89        if (mShutterButton.isPressed()) {
90            // Send cancel to the shutter button if it was pressed.
91            event.setAction(MotionEvent.ACTION_CANCEL);
92            mShutterButton.dispatchTouchEvent(event);
93            return true;
94        }
95
96        return mIndicatorControlWheel.dispatchTouchEvent(event);
97    }
98
99    @Override
100    protected void onLayout(
101            boolean changed, int left, int top, int right, int bottom) {
102
103        // Layout the shutter button.
104        int shutterButtonWidth = mShutterButton.getMeasuredWidth();
105        int shutterButtonHeight = mShutterButton.getMeasuredHeight();
106        mCenterX = right - left - Util.dpToPixel(FULL_WHEEL_RADIUS);
107        mCenterY = (bottom - top) / 2;
108        mShutterButton.layout(right - left - shutterButtonWidth,
109                mCenterY - shutterButtonHeight / 2,
110                right - left,
111                mCenterY + shutterButtonHeight - shutterButtonHeight / 2);
112        // Layout the control wheel.
113        mIndicatorControlWheel.layout(0, 0, right - left, bottom - top);
114    }
115
116    @Override
117    protected void onMeasure(int widthSpec, int heightSpec) {
118        // Measure all children.
119        int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
120        mShutterButton.measure(freeSpec, freeSpec);
121        mIndicatorControlWheel.measure(freeSpec, freeSpec);
122
123        // Measure myself. Add some buffer for highlight arc.
124        int desiredWidth = mShutterButton.getMeasuredWidth()
125                + IndicatorControlWheel.HIGHLIGHT_WIDTH * 4;
126        int desiredHeight = mShutterButton.getMeasuredHeight()
127                + IndicatorControlWheel.HIGHLIGHT_WIDTH * 4;
128        int widthMode = MeasureSpec.getMode(widthSpec);
129        int heightMode = MeasureSpec.getMode(heightSpec);
130        int measuredWidth, measuredHeight;
131        if (widthMode == MeasureSpec.UNSPECIFIED) {
132            measuredWidth = desiredWidth;
133        } else if (widthMode == MeasureSpec.AT_MOST) {
134            measuredWidth = Math.min(desiredWidth, MeasureSpec.getSize(widthSpec));
135        } else {  // MeasureSpec.EXACTLY
136            measuredWidth = MeasureSpec.getSize(widthSpec);
137        }
138        if (heightMode == MeasureSpec.UNSPECIFIED) {
139            measuredHeight = desiredHeight;
140        } else if (heightMode == MeasureSpec.AT_MOST) {
141            measuredHeight = Math.min(desiredHeight, MeasureSpec.getSize(heightSpec));
142        } else {  // MeasureSpec.EXACTLY
143            measuredHeight = MeasureSpec.getSize(heightSpec);
144        }
145        setMeasuredDimension(measuredWidth, measuredHeight);
146    }
147
148    @Override
149    public void setListener(OnPreferenceChangedListener listener) {
150        mIndicatorControlWheel.setListener(listener);
151    }
152
153    @Override
154    public void reloadPreferences() {
155        mIndicatorControlWheel.reloadPreferences();
156    }
157
158    @Override
159    public View getActiveSettingPopup() {
160        return mIndicatorControlWheel.getActiveSettingPopup();
161    }
162
163    @Override
164    public boolean dismissSettingPopup() {
165        return mIndicatorControlWheel.dismissSettingPopup();
166    }
167
168    @Override
169    public void setOrientation(int orientation) {
170        mIndicatorControlWheel.setOrientation(orientation);
171    }
172
173    public void startTimeLapseAnimation(int timeLapseInterval, long startTime) {
174        mIndicatorControlWheel.startTimeLapseAnimation(
175                timeLapseInterval, startTime);
176    }
177
178    public void stopTimeLapseAnimation() {
179        mIndicatorControlWheel.stopTimeLapseAnimation();
180    }
181
182    @Override
183    public void setEnabled(boolean enabled) {
184        mIndicatorControlWheel.setEnabled(enabled);
185    }
186
187    @Override
188    public void enableZoom(boolean enabled) {
189        mIndicatorControlWheel.enableZoom(enabled);
190    }
191
192    @Override
193    public void overrideSettings(final String ... keyvalues) {
194        mIndicatorControlWheel.overrideSettings(keyvalues);
195    }
196
197    @Override
198    public void dismissSecondLevelIndicator() {
199        mIndicatorControlWheel.dismissSecondLevelIndicator();
200    }
201}
202