blob: 6d7166066ffda103b18e382062c85086a9940948 [file] [log] [blame]
Sascha Haeberling08b3c942014-07-30 17:48:52 -07001/*
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
17package com.android.camera;
18
19import android.graphics.Bitmap;
20import android.graphics.Matrix;
21import android.graphics.RectF;
22import android.graphics.SurfaceTexture;
23import android.view.GestureDetector;
24import android.view.MotionEvent;
25import android.view.TextureView;
26import android.view.View;
27import android.view.ViewGroup;
Sascha Haeberling08b3c942014-07-30 17:48:52 -070028
Andy Huibers26b33342014-08-20 00:02:19 -070029import com.android.camera.debug.Log;
Andy Huibersa31162c2014-09-02 11:27:11 -070030import com.android.camera.ui.CountDownView;
Andy Huibers26b33342014-08-20 00:02:19 -070031import com.android.camera.ui.PreviewOverlay;
32import com.android.camera.ui.PreviewOverlay.OnZoomChangedListener;
Sascha Haeberling08b3c942014-07-30 17:48:52 -070033import com.android.camera.ui.PreviewStatusListener;
34import com.android.camera.ui.ProgressOverlay;
Paul Rohde987ee642014-12-05 12:17:15 -080035import com.android.camera.ui.focus.FocusRing;
Sascha Haeberling08b3c942014-07-30 17:48:52 -070036import com.android.camera2.R;
37
38/**
39 * Contains the UI for the CaptureModule.
40 */
41public class CaptureModuleUI implements
42 PreviewStatusListener {
43
Andy Huibers26b33342014-08-20 00:02:19 -070044 private static final Log.Tag TAG = new Log.Tag("CaptureModuleUI");
45
Sascha Haeberling08b3c942014-07-30 17:48:52 -070046 private final CameraActivity mActivity;
47 private final CaptureModule mModule;
48 private final View mRootView;
49
Andy Huibers26b33342014-08-20 00:02:19 -070050 private final PreviewOverlay mPreviewOverlay;
Sascha Haeberling08b3c942014-07-30 17:48:52 -070051 private final ProgressOverlay mProgressOverlay;
52 private final View.OnLayoutChangeListener mLayoutListener;
53 private final TextureView mPreviewView;
Sascha Haeberling08b3c942014-07-30 17:48:52 -070054
55 private final GestureDetector.OnGestureListener mPreviewGestureListener = new GestureDetector.SimpleOnGestureListener() {
56 @Override
57 public boolean onSingleTapUp(MotionEvent ev) {
58 mModule.onSingleTapUp(null, (int) ev.getX(), (int) ev.getY());
59 return true;
60 }
61 };
Paul Rohde987ee642014-12-05 12:17:15 -080062 private final FocusRing mFocusRing;
Andy Huibersa31162c2014-09-02 11:27:11 -070063 private final CountDownView mCountdownView;
64
Sascha Haeberling08b3c942014-07-30 17:48:52 -070065 private int mPreviewAreaWidth;
66 private int mPreviewAreaHeight;
67
Andy Huibers26b33342014-08-20 00:02:19 -070068 /** Maximum zoom; intialize to 1.0 (disabled) */
69 private float mMaxZoom = 1f;
70
71 /** Set up listener to receive zoom changes from View and send to module. */
72 private final OnZoomChangedListener mZoomChancedListener = new OnZoomChangedListener() {
73 @Override
Sol Boucher2192fba2014-08-19 17:24:07 -070074 public void onZoomValueChanged(float ratio) {
75 mModule.setZoom(ratio);
Andy Huibers26b33342014-08-20 00:02:19 -070076 }
77
78 @Override
79 public void onZoomStart() {
80 }
81
82 @Override
83 public void onZoomEnd() {
84 }
85 };
86
Andy Huibersa31162c2014-09-02 11:27:11 -070087 public void onPreviewAreaChanged(RectF previewArea) {
88 // TODO: mFaceView.onPreviewAreaChanged(previewArea);
89 mCountdownView.onPreviewAreaChanged(previewArea);
90 }
91
Sascha Haeberling08b3c942014-07-30 17:48:52 -070092 @Override
93 public void onPreviewLayoutChanged(View v, int left, int top, int right,
94 int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
95 if (mLayoutListener != null) {
96 mLayoutListener.onLayoutChange(v, left, top, right, bottom, oldLeft, oldTop, oldRight,
97 oldBottom);
98 }
99 }
100
101 @Override
102 public boolean shouldAutoAdjustTransformMatrixOnLayout() {
103 return false;
104 }
105
106 @Override
107 public boolean shouldAutoAdjustBottomBar() {
108 return true;
109 }
110
111 @Override
112 public void onPreviewFlipped() {
113 // Do nothing because when preview is flipped, TextureView will lay
114 // itself out again, which will then trigger a transform matrix update.
115 }
116
117 @Override
118 public GestureDetector.OnGestureListener getGestureListener() {
119 return mPreviewGestureListener;
120 }
121
122 @Override
123 public View.OnTouchListener getTouchListener() {
124 return null;
125 }
126
127 public CaptureModuleUI(CameraActivity activity, CaptureModule module, View parent,
128 View.OnLayoutChangeListener layoutListener) {
129 mActivity = activity;
130 mModule = module;
131 mRootView = parent;
132 mLayoutListener = layoutListener;
133
134 ViewGroup moduleRoot = (ViewGroup) mRootView.findViewById(R.id.module_layout);
135 mActivity.getLayoutInflater().inflate(R.layout.capture_module,
136 moduleRoot, true);
137
138 mPreviewView = (TextureView) mRootView.findViewById(R.id.preview_content);
139
Andy Huibers26b33342014-08-20 00:02:19 -0700140 mPreviewOverlay = (PreviewOverlay) mRootView.findViewById(R.id.preview_overlay);
Sascha Haeberling08b3c942014-07-30 17:48:52 -0700141 mProgressOverlay = (ProgressOverlay) mRootView.findViewById(R.id.progress_overlay);
142
Paul Rohde987ee642014-12-05 12:17:15 -0800143 mFocusRing = (FocusRing) mRootView.findViewById(R.id.focus_ring);
Andy Huibersa31162c2014-09-02 11:27:11 -0700144 mCountdownView = (CountDownView) mRootView.findViewById(R.id.count_down_view);
Sascha Haeberling08b3c942014-07-30 17:48:52 -0700145 }
146
147 @Override
148 public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
149 mModule.onSurfaceTextureAvailable(surface, width, height);
150 }
151
152 @Override
153 public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
154 return mModule.onSurfaceTextureDestroyed(surface);
155 }
156
157 @Override
158 public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
159 mModule.onSurfaceTextureSizeChanged(surface, width, height);
160 }
161
162 @Override
163 public void onSurfaceTextureUpdated(SurfaceTexture surface) {
164 mModule.onSurfaceTextureUpdated(surface);
165 }
166
167 public void positionProgressOverlay(RectF area) {
168 mProgressOverlay.setBounds(area);
169 }
170
171 /**
172 * Getter for the width of the visible area of the preview.
173 */
174 public int getPreviewAreaWidth() {
175 return mPreviewAreaWidth;
176 }
177
178 /**
179 * Getter for the height of the visible area of the preview.
180 */
181 public int getPreviewAreaHeight() {
182 return mPreviewAreaHeight;
183 }
184
185 public Matrix getPreviewTransform(Matrix m) {
186 return mPreviewView.getTransform(m);
187 }
188
Paul Rohde987ee642014-12-05 12:17:15 -0800189 public FocusRing getFocusRing() {
190 return mFocusRing;
Andy Huibersca3c5642014-09-06 20:44:25 -0700191 }
192
193 public void showDebugMessage(String message) {
Paul Rohde987ee642014-12-05 12:17:15 -0800194 /* NoOp */
Sascha Haeberling08b3c942014-07-30 17:48:52 -0700195 }
Andy Huibers26b33342014-08-20 00:02:19 -0700196
Sascha Haeberling08b3c942014-07-30 17:48:52 -0700197 /**
Andy Huibersa31162c2014-09-02 11:27:11 -0700198 * Starts the countdown timer.
199 *
200 * @param sec seconds to countdown
201 */
202 public void startCountdown(int sec) {
203 mCountdownView.startCountDown(sec);
204 }
205
206 /**
207 * Sets a listener that gets notified when the countdown is finished.
208 */
209 public void setCountdownFinishedListener(CountDownView.OnCountDownStatusListener listener) {
210 mCountdownView.setCountDownStatusListener(listener);
211 }
212
213 /**
214 * Returns whether the countdown is on-going.
215 */
216 public boolean isCountingDown() {
217 return mCountdownView.isCountingDown();
218 }
219
220 /**
221 * Cancels the on-going countdown, if any.
222 */
223 public void cancelCountDown() {
224 mCountdownView.cancelCountDown();
225 }
226
227 /**
Sascha Haeberling08b3c942014-07-30 17:48:52 -0700228 * Sets the progress of the gcam picture taking.
229 *
230 * @param percent amount of process done in percent 0-100.
231 */
232 public void setPictureTakingProgress(int percent) {
233 mProgressOverlay.setProgress(percent);
234 }
235
236 public Bitmap getBitMapFromPreview() {
237 Matrix m = new Matrix();
238 m = getPreviewTransform(m);
239 Bitmap src = mPreviewView.getBitmap();
240 return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true);
241 }
Andy Huibers26b33342014-08-20 00:02:19 -0700242
243 /**
244 * Enables zoom UI, setting maximum zoom.
245 * Called from Module when camera is available.
246 *
247 * @param maxZoom maximum zoom value.
248 */
249 public void initializeZoom(float maxZoom) {
250 mMaxZoom = maxZoom;
251 mPreviewOverlay.setupZoom(mMaxZoom, 0, mZoomChancedListener);
252 }
Sascha Haeberling08b3c942014-07-30 17:48:52 -0700253}