OSDN Git Service

Adjust capture indicator position while open/close mode options.
[android-x86/packages-apps-Camera2.git] / src / com / android / camera / widget / ModeOptionsOverlay.java
1 /*
2  * Copyright (C) 2014 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.widget;
18
19 import android.content.Context;
20 import android.content.res.Configuration;
21 import android.graphics.RectF;
22 import android.util.AttributeSet;
23 import android.view.Gravity;
24 import android.view.MotionEvent;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.FrameLayout;
28 import android.widget.ImageView;
29 import android.widget.LinearLayout;
30
31 import com.android.camera.CaptureLayoutHelper;
32 import com.android.camera.ShutterButton;
33 import com.android.camera.debug.Log;
34 import com.android.camera.ui.PreviewOverlay;
35 import com.android.camera.ui.TouchCoordinate;
36 import com.android.camera2.R;
37
38 /**
39  * ModeOptionsOverlay is a FrameLayout which positions mode options in
40  * in the bottom of the preview that is visible above the bottom bar.
41  */
42 public class ModeOptionsOverlay extends FrameLayout
43     implements PreviewOverlay.OnPreviewTouchedListener,
44                ShutterButton.OnShutterButtonListener {
45
46     private final static Log.Tag TAG = new Log.Tag("ModeOptionsOverlay");
47
48     private static final int BOTTOMBAR_OPTIONS_TIMEOUT_MS = 2000;
49     private static final int BOTTOM_RIGHT = Gravity.BOTTOM | Gravity.RIGHT;
50     private static final int TOP_RIGHT = Gravity.TOP | Gravity.RIGHT;
51
52     private ModeOptions mModeOptions;
53     // need a reference to set the onClickListener and fix the layout gravity on orientation change
54     private LinearLayout mModeOptionsToggle;
55     // need a reference to fix the rotation on orientation change
56     private ImageView mThreeDots;
57     private CaptureLayoutHelper mCaptureLayoutHelper = null;
58
59     public ModeOptionsOverlay(Context context, AttributeSet attrs) {
60         super(context, attrs);
61     }
62
63     /**
64      * Whether the mode options are hidden.
65      */
66     public boolean isModeOptionsHidden() {
67         return mModeOptions.isHiddenOrHiding();
68     }
69
70     /**
71      * Gets the current width of the mode options toggle including the three dots and various mode
72      * option indicators.
73      */
74     public float getModeOptionsToggleWidth() {
75         return mModeOptionsToggle.getWidth();
76     }
77
78     /**
79      * Sets a capture layout helper to query layout rect from.
80      */
81     public void setCaptureLayoutHelper(CaptureLayoutHelper helper) {
82         mCaptureLayoutHelper = helper;
83     }
84
85     public void setToggleClickable(boolean clickable) {
86         mModeOptionsToggle.setClickable(clickable);
87     }
88
89     /**
90      * Sets the mode options listener.
91      *
92      * @param listener The listener to be set.
93      */
94     public void setModeOptionsListener(ModeOptions.Listener listener) {
95         mModeOptions.setListener(listener);
96     }
97
98     @Override
99     public void onFinishInflate() {
100         mModeOptions = (ModeOptions) findViewById(R.id.mode_options);
101         mModeOptions.setClickable(true);
102         mModeOptions.setOnClickListener(new View.OnClickListener() {
103             @Override
104             public void onClick(View v) {
105                 closeModeOptions();
106             }
107         });
108
109         mModeOptionsToggle = (LinearLayout) findViewById(R.id.mode_options_toggle);
110         mModeOptionsToggle.setOnClickListener(new View.OnClickListener() {
111             @Override
112             public void onClick(View v) {
113                 mModeOptions.animateVisible();
114             }
115         });
116         mModeOptions.setViewToShowHide(mModeOptionsToggle);
117
118         mThreeDots = (ImageView) findViewById(R.id.three_dots);
119     }
120
121     @Override
122     public void onPreviewTouched(MotionEvent ev) {
123         closeModeOptions();
124     }
125
126     @Override
127     public void onShutterButtonClick() {
128         closeModeOptions();
129     }
130
131     @Override
132     public void onShutterCoordinate(TouchCoordinate coord) {
133         // Do nothing.
134     }
135
136     @Override
137     public void onShutterButtonFocus(boolean pressed) {
138         // noop
139     }
140
141     @Override
142     public void onShutterButtonLongPressed() {
143         // noop
144     }
145
146     /**
147      * Schedule (or re-schedule) the options menu to be closed after a number
148      * of milliseconds.  If the options menu is already closed, nothing is
149      * scheduled.
150      */
151     public void closeModeOptions() {
152         mModeOptions.animateHidden();
153     }
154
155     @Override
156     public void onConfigurationChanged(Configuration configuration) {
157         super.onConfigurationChanged(configuration);
158         checkOrientation(configuration.orientation);
159     }
160
161     @Override
162     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
163         if (mCaptureLayoutHelper == null) {
164             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
165             Log.e(TAG, "Capture layout helper needs to be set first.");
166         } else {
167             RectF uncoveredPreviewRect = mCaptureLayoutHelper.getUncoveredPreviewRect();
168             super.onMeasure(MeasureSpec.makeMeasureSpec(
169                             (int) uncoveredPreviewRect.width(), MeasureSpec.EXACTLY),
170                     MeasureSpec.makeMeasureSpec((int) uncoveredPreviewRect.height(),
171                             MeasureSpec.EXACTLY)
172             );
173         }
174     }
175
176     /**
177      * Set the layout gravity of the child layout to be bottom or top right
178      * depending on orientation.
179      */
180     private void checkOrientation(int orientation) {
181         final boolean isPortrait = (Configuration.ORIENTATION_PORTRAIT == orientation);
182
183         final int modeOptionsDimension = (int) getResources()
184             .getDimension(R.dimen.mode_options_height);
185
186         FrameLayout.LayoutParams modeOptionsParams
187             = (FrameLayout.LayoutParams) mModeOptions.getLayoutParams();
188         FrameLayout.LayoutParams modeOptionsToggleParams
189             = (FrameLayout.LayoutParams) mModeOptionsToggle.getLayoutParams();
190
191         if (isPortrait) {
192             modeOptionsParams.height = modeOptionsDimension;
193             modeOptionsParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
194             modeOptionsParams.gravity = Gravity.BOTTOM;
195
196             modeOptionsToggleParams.gravity = BOTTOM_RIGHT;
197
198             mThreeDots.setImageResource(R.drawable.ic_options_port);
199         } else {
200             modeOptionsParams.width = modeOptionsDimension;
201             modeOptionsParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
202             modeOptionsParams.gravity = Gravity.RIGHT;
203
204             modeOptionsToggleParams.gravity = TOP_RIGHT;
205
206             mThreeDots.setImageResource(R.drawable.ic_options_land);
207         }
208
209         requestLayout();
210     }
211 }