blob: 472fd8923b4e4740b93040d6d2980540d19f748b [file] [log] [blame]
Michael Kolb8872c232013-01-29 10:33:22 -08001/*
2 * Copyright (C) 2012 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.annotation.TargetApi;
20import android.app.Activity;
Michael Kolb8872c232013-01-29 10:33:22 -080021import android.content.ContentResolver;
Angus Kong0d00a892013-03-26 11:40:40 -070022import android.content.Context;
Michael Kolb8872c232013-01-29 10:33:22 -080023import android.content.Intent;
Michael Kolb8872c232013-01-29 10:33:22 -080024import android.content.res.Configuration;
25import android.graphics.Bitmap;
Doris Liu36ebcb12013-10-28 14:44:24 -070026import android.graphics.Rect;
Michael Kolb8872c232013-01-29 10:33:22 -080027import android.graphics.SurfaceTexture;
28import android.hardware.Camera.CameraInfo;
Michael Kolb8872c232013-01-29 10:33:22 -080029import android.hardware.Camera.Parameters;
Michael Kolb8872c232013-01-29 10:33:22 -080030import android.hardware.Camera.Size;
Angus Kong0d00a892013-03-26 11:40:40 -070031import android.hardware.Sensor;
32import android.hardware.SensorEvent;
33import android.hardware.SensorEventListener;
34import android.hardware.SensorManager;
Michael Kolb8872c232013-01-29 10:33:22 -080035import android.location.Location;
36import android.media.CameraProfile;
37import android.net.Uri;
Sascha Haeberling638e6f02013-09-18 14:28:51 -070038import android.os.Build;
Michael Kolb8872c232013-01-29 10:33:22 -080039import android.os.Bundle;
Michael Kolb8872c232013-01-29 10:33:22 -080040import android.os.Handler;
41import android.os.Looper;
42import android.os.Message;
43import android.os.MessageQueue;
44import android.os.SystemClock;
45import android.provider.MediaStore;
46import android.util.Log;
Michael Kolb8872c232013-01-29 10:33:22 -080047import android.view.KeyEvent;
Michael Kolb8872c232013-01-29 10:33:22 -080048import android.view.OrientationEventListener;
Michael Kolb8872c232013-01-29 10:33:22 -080049import android.view.View;
Michael Kolb8872c232013-01-29 10:33:22 -080050
Sameer Padala2c8cc452013-11-05 18:49:12 -080051import com.android.camera.PhotoModule.NamedImages.NamedEntity;
52import com.android.camera.app.AppController;
Angus Kong20fad242013-11-11 18:23:46 -080053import com.android.camera.app.CameraManager.CameraAFCallback;
54import com.android.camera.app.CameraManager.CameraAFMoveCallback;
55import com.android.camera.app.CameraManager.CameraPictureCallback;
56import com.android.camera.app.CameraManager.CameraProxy;
57import com.android.camera.app.CameraManager.CameraShutterCallback;
Angus Kongfd4fc0e2013-11-07 15:38:09 -080058import com.android.camera.app.MediaSaver;
ztenghuia16e7b52013-08-23 11:47:56 -070059import com.android.camera.exif.ExifInterface;
60import com.android.camera.exif.ExifTag;
61import com.android.camera.exif.Rational;
Angus Kong20fad242013-11-11 18:23:46 -080062import com.android.camera.module.ModuleController;
Erin Dahlgren357b7672013-11-20 17:38:14 -080063import com.android.camera.settings.SettingsManager;
Sameer Padala2c8cc452013-11-05 18:49:12 -080064import com.android.camera.ui.CameraRootView;
Michael Kolbd6954f32013-03-08 20:43:01 -080065import com.android.camera.ui.CountDownView.OnCountDownFinishedListener;
Doris Liu1c94b7d2013-11-09 19:13:44 -080066import com.android.camera.ui.ModeListView;
Michael Kolb8872c232013-01-29 10:33:22 -080067import com.android.camera.ui.RotateTextToast;
Angus Kongdcccc512013-08-08 17:06:03 -070068import com.android.camera.util.ApiHelper;
Angus Kongb50b5cb2013-08-09 14:55:20 -070069import com.android.camera.util.CameraUtil;
Ruben Brunk4601f5d2013-09-24 18:35:22 -070070import com.android.camera.util.GcamHelper;
Sameer Padala2c8cc452013-11-05 18:49:12 -080071import com.android.camera.util.SmartCameraHelper;
Sascha Haeberling8e963a52013-08-06 11:43:02 -070072import com.android.camera.util.UsageStatistics;
73import com.android.camera2.R;
Michael Kolb8872c232013-01-29 10:33:22 -080074
Angus Kongdcccc512013-08-08 17:06:03 -070075import java.io.File;
76import java.io.FileNotFoundException;
77import java.io.FileOutputStream;
78import java.io.IOException;
79import java.io.OutputStream;
Angus Kongdcccc512013-08-08 17:06:03 -070080import java.util.List;
Ruben Brunka9d66bd2013-09-06 11:56:32 -070081import java.util.Vector;
Angus Kongdcccc512013-08-08 17:06:03 -070082
Michael Kolb8872c232013-01-29 10:33:22 -080083public class PhotoModule
Sascha Haeberling280fd3e2013-11-21 13:52:15 -080084 extends CameraModule
85 implements PhotoController,
86 ModuleController,
87 FocusOverlayManager.Listener,
88 CameraPreference.OnPreferenceChangedListener,
89 ShutterButton.OnShutterButtonListener, MediaSaver.QueueListener,
90 OnCountDownFinishedListener,
91 SensorEventListener {
Michael Kolb8872c232013-01-29 10:33:22 -080092
93 private static final String TAG = "CAM_PhotoModule";
94
95 // We number the request code from 1000 to avoid collision with Gallery.
96 private static final int REQUEST_CROP = 1000;
97
Angus Kong13e87c42013-11-25 10:02:47 -080098 // Messages defined for the UI thread handler.
99 private static final int MSG_SETUP_PREVIEW = 1;
100 private static final int MSG_FIRST_TIME_INIT = 2;
101 private static final int MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE = 3;
102 private static final int MSG_SHOW_TAP_TO_FOCUS_TOAST = 4;
103 private static final int MSG_SWITCH_CAMERA = 5;
104 private static final int MSG_SWITCH_CAMERA_START_ANIMATION = 6;
105 private static final int MSG_CAMERA_OPEN_DONE = 7;
106 private static final int MSG_OPEN_CAMERA_FAIL = 8;
107 private static final int MSG_CAMERA_DISABLED = 9;
108 private static final int MSG_SWITCH_TO_GCAM_MODULE = 10;
Michael Kolb8872c232013-01-29 10:33:22 -0800109
110 // The subset of parameters we need to update in setCameraParameters().
111 private static final int UPDATE_PARAM_INITIALIZE = 1;
112 private static final int UPDATE_PARAM_ZOOM = 2;
113 private static final int UPDATE_PARAM_PREFERENCE = 4;
114 private static final int UPDATE_PARAM_ALL = -1;
115
Andy Huibersdef975d2013-11-22 09:13:39 -0800116 // This is the delay before we execute onResume tasks when coming
117 // from the lock screen, to allow time for onPause to execute.
118 private static final int ON_RESUME_TASKS_DELAY_MSEC = 20;
Michael Kolb8872c232013-01-29 10:33:22 -0800119
Ruben Brunkd7488272013-10-10 18:45:53 -0700120 private static final String DEBUG_IMAGE_PREFIX = "DEBUG_";
121
Michael Kolb8872c232013-01-29 10:33:22 -0800122 // copied from Camera hierarchy
123 private CameraActivity mActivity;
Michael Kolb8872c232013-01-29 10:33:22 -0800124 private CameraProxy mCameraDevice;
125 private int mCameraId;
126 private Parameters mParameters;
127 private boolean mPaused;
Michael Kolbd6954f32013-03-08 20:43:01 -0800128
129 private PhotoUI mUI;
Michael Kolb8872c232013-01-29 10:33:22 -0800130
Michael Kolb8872c232013-01-29 10:33:22 -0800131 // The activity is going to switch to the specified camera id. This is
132 // needed because texture copy is done in GL thread. -1 means camera is not
133 // switching.
134 protected int mPendingSwitchCameraId = -1;
135 private boolean mOpenCameraFail;
136 private boolean mCameraDisabled;
137
138 // When setCameraParametersWhenIdle() is called, we accumulate the subsets
139 // needed to be updated in mUpdateSet.
140 private int mUpdateSet;
141
142 private static final int SCREEN_DELAY = 2 * 60 * 1000;
143
144 private int mZoomValue; // The current zoom value.
Michael Kolb8872c232013-01-29 10:33:22 -0800145
146 private Parameters mInitialParams;
147 private boolean mFocusAreaSupported;
148 private boolean mMeteringAreaSupported;
149 private boolean mAeLockSupported;
150 private boolean mAwbLockSupported;
Angus Kongdcccc512013-08-08 17:06:03 -0700151 private boolean mContinuousFocusSupported;
Michael Kolb8872c232013-01-29 10:33:22 -0800152
153 // The degrees of the device rotated clockwise from its natural orientation.
154 private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
Michael Kolb8872c232013-01-29 10:33:22 -0800155
156 private static final String sTempCropFilename = "crop-temp";
157
Michael Kolb8872c232013-01-29 10:33:22 -0800158 private boolean mFaceDetectionStarted = false;
159
Michael Kolb8872c232013-01-29 10:33:22 -0800160 // mCropValue and mSaveUri are used only if isImageCaptureIntent() is true.
161 private String mCropValue;
162 private Uri mSaveUri;
163
Ruben Brunkd217ed02013-10-08 23:31:13 -0700164 private Uri mDebugUri;
165
Angus Kongce5480e2013-01-29 17:43:48 -0800166 // We use a queue to generated names of the images to be used later
167 // when the image is ready to be saved.
Michael Kolb8872c232013-01-29 10:33:22 -0800168 private NamedImages mNamedImages;
169
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800170 private final Runnable mDoSnapRunnable = new Runnable() {
Michael Kolb8872c232013-01-29 10:33:22 -0800171 @Override
172 public void run() {
173 onShutterButtonClick();
174 }
175 };
176
Michael Kolb8872c232013-01-29 10:33:22 -0800177 /**
178 * An unpublished intent flag requesting to return as soon as capturing
179 * is completed.
180 *
181 * TODO: consider publishing by moving into MediaStore.
182 */
183 private static final String EXTRA_QUICK_CAPTURE =
184 "android.intent.extra.quickCapture";
185
186 // The display rotation in degrees. This is only valid when mCameraState is
187 // not PREVIEW_STOPPED.
188 private int mDisplayRotation;
189 // The value for android.hardware.Camera.setDisplayOrientation.
190 private int mCameraDisplayOrientation;
191 // The value for UI components like indicators.
192 private int mDisplayOrientation;
193 // The value for android.hardware.Camera.Parameters.setRotation.
194 private int mJpegRotation;
Doris Liu29da2db2013-08-28 14:28:45 -0700195 // Indicates whether we are using front camera
196 private boolean mMirror;
Michael Kolb8872c232013-01-29 10:33:22 -0800197 private boolean mFirstTimeInitialized;
198 private boolean mIsImageCaptureIntent;
199
Michael Kolb8872c232013-01-29 10:33:22 -0800200 private int mCameraState = PREVIEW_STOPPED;
201 private boolean mSnapshotOnIdle = false;
202
203 private ContentResolver mContentResolver;
204
205 private LocationManager mLocationManager;
206
Michael Kolb8872c232013-01-29 10:33:22 -0800207 private final PostViewPictureCallback mPostViewPictureCallback =
208 new PostViewPictureCallback();
209 private final RawPictureCallback mRawPictureCallback =
210 new RawPictureCallback();
211 private final AutoFocusCallback mAutoFocusCallback =
212 new AutoFocusCallback();
213 private final Object mAutoFocusMoveCallback =
214 ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700215 ? new AutoFocusMoveCallback()
216 : null;
Michael Kolb8872c232013-01-29 10:33:22 -0800217
218 private final CameraErrorCallback mErrorCallback = new CameraErrorCallback();
219
220 private long mFocusStartTime;
221 private long mShutterCallbackTime;
222 private long mPostViewPictureCallbackTime;
223 private long mRawPictureCallbackTime;
224 private long mJpegPictureCallbackTime;
225 private long mOnResumeTime;
226 private byte[] mJpegImageData;
227
228 // These latency time are for the CameraLatency test.
229 public long mAutoFocusTime;
230 public long mShutterLag;
231 public long mShutterToPictureDisplayedTime;
232 public long mPictureDisplayedToJpegCallbackTime;
233 public long mJpegCallbackFinishTime;
234 public long mCaptureStartTime;
235
236 // This handles everything about focus.
237 private FocusOverlayManager mFocusManager;
238
Michael Kolb8872c232013-01-29 10:33:22 -0800239 private String mSceneMode;
Michael Kolb8872c232013-01-29 10:33:22 -0800240
241 private final Handler mHandler = new MainHandler();
Erin Dahlgrenb09b53e2013-11-06 11:57:51 -0800242
Michael Kolb8872c232013-01-29 10:33:22 -0800243 private PreferenceGroup mPreferenceGroup;
244
245 private boolean mQuickCapture;
Angus Kong0d00a892013-03-26 11:40:40 -0700246 private SensorManager mSensorManager;
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800247 private final float[] mGData = new float[3];
248 private final float[] mMData = new float[3];
249 private final float[] mR = new float[16];
Angus Kong0d00a892013-03-26 11:40:40 -0700250 private int mHeading = -1;
251
Angus Kongdcccc512013-08-08 17:06:03 -0700252 // True if all the parameters needed to start preview is ready.
253 private boolean mCameraPreviewParamsReady = false;
Doris Liu6432cd62013-06-13 17:20:31 -0700254
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800255 private final MediaSaver.OnMediaSavedListener mOnMediaSavedListener =
Angus Kongfd4fc0e2013-11-07 15:38:09 -0800256 new MediaSaver.OnMediaSavedListener() {
Angus Kongce5480e2013-01-29 17:43:48 -0800257 @Override
258 public void onMediaSaved(Uri uri) {
259 if (uri != null) {
Doris Liu6432cd62013-06-13 17:20:31 -0700260 mActivity.notifyNewMedia(uri);
Angus Kongce5480e2013-01-29 17:43:48 -0800261 }
262 }
263 };
Michael Kolb8872c232013-01-29 10:33:22 -0800264
Angus Kongdcccc512013-08-08 17:06:03 -0700265 private void checkDisplayRotation() {
266 // Set the display orientation if display rotation has changed.
267 // Sometimes this happens when the device is held upside
268 // down and camera app is opened. Rotation animation will
269 // take some time and the rotation value we have got may be
270 // wrong. Framework does not have a callback for this now.
271 if (CameraUtil.getDisplayRotation(mActivity) != mDisplayRotation) {
272 setDisplayOrientation();
Michael Kolb8872c232013-01-29 10:33:22 -0800273 }
Angus Kongdcccc512013-08-08 17:06:03 -0700274 if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) {
275 mHandler.postDelayed(new Runnable() {
276 @Override
277 public void run() {
278 checkDisplayRotation();
279 }
280 }, 100);
Michael Kolb8872c232013-01-29 10:33:22 -0800281 }
282 }
283
284 /**
285 * This Handler is used to post message back onto the main thread of the
286 * application
287 */
288 private class MainHandler extends Handler {
Erin Dahlgrenb09b53e2013-11-06 11:57:51 -0800289 public MainHandler() {
290 super(Looper.getMainLooper());
291 }
292
Michael Kolb8872c232013-01-29 10:33:22 -0800293 @Override
294 public void handleMessage(Message msg) {
295 switch (msg.what) {
Angus Kong13e87c42013-11-25 10:02:47 -0800296 case MSG_SETUP_PREVIEW: {
Michael Kolb8872c232013-01-29 10:33:22 -0800297 setupPreview();
298 break;
299 }
300
Angus Kong13e87c42013-11-25 10:02:47 -0800301 case MSG_FIRST_TIME_INIT: {
Michael Kolb8872c232013-01-29 10:33:22 -0800302 initializeFirstTime();
303 break;
304 }
305
Angus Kong13e87c42013-11-25 10:02:47 -0800306 case MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE: {
Michael Kolb8872c232013-01-29 10:33:22 -0800307 setCameraParametersWhenIdle(0);
308 break;
309 }
310
Angus Kong13e87c42013-11-25 10:02:47 -0800311 case MSG_SHOW_TAP_TO_FOCUS_TOAST: {
Michael Kolb8872c232013-01-29 10:33:22 -0800312 showTapToFocusToast();
313 break;
314 }
315
Angus Kong13e87c42013-11-25 10:02:47 -0800316 case MSG_SWITCH_CAMERA: {
Michael Kolb8872c232013-01-29 10:33:22 -0800317 switchCamera();
318 break;
319 }
320
Angus Kong13e87c42013-11-25 10:02:47 -0800321 case MSG_SWITCH_CAMERA_START_ANIMATION: {
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700322 // TODO: Need to revisit
323 // ((CameraScreenNail) mActivity.mCameraScreenNail).animateSwitchCamera();
Michael Kolb8872c232013-01-29 10:33:22 -0800324 break;
325 }
326
Angus Kong13e87c42013-11-25 10:02:47 -0800327 case MSG_CAMERA_OPEN_DONE: {
Michael Kolbd6954f32013-03-08 20:43:01 -0800328 onCameraOpened();
Michael Kolb8872c232013-01-29 10:33:22 -0800329 break;
330 }
331
Angus Kong13e87c42013-11-25 10:02:47 -0800332 case MSG_OPEN_CAMERA_FAIL: {
Michael Kolb8872c232013-01-29 10:33:22 -0800333 mOpenCameraFail = true;
Angus Kongb50b5cb2013-08-09 14:55:20 -0700334 CameraUtil.showErrorAndFinish(mActivity,
Michael Kolb8872c232013-01-29 10:33:22 -0800335 R.string.cannot_connect_camera);
336 break;
337 }
338
Angus Kong13e87c42013-11-25 10:02:47 -0800339 case MSG_CAMERA_DISABLED: {
Michael Kolb8872c232013-01-29 10:33:22 -0800340 mCameraDisabled = true;
Angus Kongb50b5cb2013-08-09 14:55:20 -0700341 CameraUtil.showErrorAndFinish(mActivity,
Michael Kolb8872c232013-01-29 10:33:22 -0800342 R.string.camera_disabled);
343 break;
344 }
ztenghui367c7c82013-10-16 14:43:26 -0700345
Angus Kong13e87c42013-11-25 10:02:47 -0800346 case MSG_SWITCH_TO_GCAM_MODULE: {
Erin Dahlgrend7b8cb52013-11-14 17:25:37 -0800347 mActivity.onModeSelected(ModeListView.MODE_GCAM);
ztenghui367c7c82013-10-16 14:43:26 -0700348 }
Michael Kolb8872c232013-01-29 10:33:22 -0800349 }
350 }
351 }
352
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800353 /**
354 * Constructs a new photo module.
355 */
Angus Kongc4e66562013-11-22 23:03:21 -0800356 public PhotoModule(AppController app) {
357 super(app);
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800358 }
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700359
Angus Kong13e87c42013-11-25 10:02:47 -0800360
Michael Kolb8872c232013-01-29 10:33:22 -0800361 @Override
Angus Kong13e87c42013-11-25 10:02:47 -0800362 public void init(AppController app, boolean isSecureCamera, boolean isCaptureIntent) {
363 mActivity = (CameraActivity) app.getAndroidContext();
364 mUI = new PhotoUI(mActivity, this, app.getModuleLayoutRoot());
Erin Dahlgren357b7672013-11-20 17:38:14 -0800365
366 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800367 mCameraId = Integer.parseInt(settingsManager.get(SettingsManager.SETTING_CAMERA_ID));
Michael Kolb8872c232013-01-29 10:33:22 -0800368
369 mContentResolver = mActivity.getContentResolver();
370
Michael Kolb8872c232013-01-29 10:33:22 -0800371 // Surface texture is from camera screen nail and startPreview needs it.
372 // This must be done before startPreview.
373 mIsImageCaptureIntent = isImageCaptureIntent();
Michael Kolb8872c232013-01-29 10:33:22 -0800374
Angus Kong20fad242013-11-11 18:23:46 -0800375 mActivity.getCameraProvider().requestCamera(mCameraId);
376
Michael Kolb8872c232013-01-29 10:33:22 -0800377 initializeControlByIntent();
378 mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
Erin Dahlgren21c21a62013-11-19 16:37:38 -0800379 mLocationManager = mActivity.getLocationManager();
Angus Kong0d00a892013-03-26 11:40:40 -0700380 mSensorManager = (SensorManager)(mActivity.getSystemService(Context.SENSOR_SERVICE));
Michael Kolbd6954f32013-03-08 20:43:01 -0800381 }
382
383 private void initializeControlByIntent() {
384 mUI.initializeControlByIntent();
385 if (mIsImageCaptureIntent) {
386 setupCaptureParams();
387 }
388 }
389
390 private void onPreviewStarted() {
Michael Kolbd6954f32013-03-08 20:43:01 -0800391 setCameraState(IDLE);
Michael Kolbd6954f32013-03-08 20:43:01 -0800392 startFaceDetection();
Sameer Padala2c8cc452013-11-05 18:49:12 -0800393 startSmartCamera();
Michael Kolbd6954f32013-03-08 20:43:01 -0800394 locationFirstRun();
Michael Kolb8872c232013-01-29 10:33:22 -0800395 }
396
397 // Prompt the user to pick to record location for the very first run of
398 // camera only
399 private void locationFirstRun() {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800400 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800401 if (settingsManager.isSet(SettingsManager.SETTING_RECORD_LOCATION)) {
Michael Kolb8872c232013-01-29 10:33:22 -0800402 return;
403 }
404 if (mActivity.isSecureCamera()) return;
405 // Check if the back camera exists
406 int backCameraId = CameraHolder.instance().getBackCameraId();
407 if (backCameraId == -1) {
408 // If there is no back camera, do not show the prompt.
409 return;
410 }
Doris Liu6a83d522013-07-02 12:03:32 -0700411 mUI.showLocationDialog();
412 }
Michael Kolb8872c232013-01-29 10:33:22 -0800413
ztenghui7b265a62013-09-09 14:58:44 -0700414 @Override
Angus Kongdcccc512013-08-08 17:06:03 -0700415 public void onPreviewUIReady() {
Erin Dahlgrendc282e12013-11-12 09:39:08 -0800416 startPreview();
Angus Kongdcccc512013-08-08 17:06:03 -0700417 }
418
419 @Override
420 public void onPreviewUIDestroyed() {
421 if (mCameraDevice == null) {
422 return;
423 }
424 mCameraDevice.setPreviewTexture(null);
425 stopPreview();
426 }
427
Michael Kolbd6954f32013-03-08 20:43:01 -0800428 private void onCameraOpened() {
429 View root = mUI.getRootView();
Michael Kolb8872c232013-01-29 10:33:22 -0800430 // These depend on camera parameters.
Michael Kolbd6954f32013-03-08 20:43:01 -0800431
432 int width = root.getWidth();
433 int height = root.getHeight();
Doris Liu6a0de792013-02-26 10:54:25 -0800434 mFocusManager.setPreviewSize(width, height);
Michael Kolbd6954f32013-03-08 20:43:01 -0800435 openCameraCommon();
Michael Kolb8872c232013-01-29 10:33:22 -0800436 }
437
Michael Kolbd6954f32013-03-08 20:43:01 -0800438 private void switchCamera() {
439 if (mPaused) return;
Erin Dahlgren357b7672013-11-20 17:38:14 -0800440 SettingsManager settingsManager = mActivity.getSettingsManager();
Michael Kolbd6954f32013-03-08 20:43:01 -0800441
442 Log.v(TAG, "Start to switch camera. id=" + mPendingSwitchCameraId);
443 mCameraId = mPendingSwitchCameraId;
444 mPendingSwitchCameraId = -1;
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800445 settingsManager.set(SettingsManager.SETTING_CAMERA_ID, "" + mCameraId);
Angus Kong20fad242013-11-11 18:23:46 -0800446 mActivity.getCameraProvider().requestCamera(mCameraId);
Michael Kolbd6954f32013-03-08 20:43:01 -0800447
Michael Kolbd6954f32013-03-08 20:43:01 -0800448 mUI.collapseCameraControls();
449 mUI.clearFaces();
450 if (mFocusManager != null) mFocusManager.removeMessages();
451
Erin Dahlgren357b7672013-11-20 17:38:14 -0800452 // TODO: this needs to be brought into onCameraAvailable();
Angus Kong20fad242013-11-11 18:23:46 -0800453 CameraInfo info = mActivity.getCameraProvider().getCameraInfo()[mCameraId];
Doris Liu29da2db2013-08-28 14:28:45 -0700454 mMirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
455 mFocusManager.setMirror(mMirror);
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700456 // Start switch camera animation. Post a message because
457 // onFrameAvailable from the old camera may already exist.
Angus Kong13e87c42013-11-25 10:02:47 -0800458 mHandler.sendEmptyMessage(MSG_SWITCH_CAMERA_START_ANIMATION);
Doris Liu48239f42013-03-04 22:19:10 -0800459 }
460
Michael Kolbd6954f32013-03-08 20:43:01 -0800461 // either open a new camera or switch cameras
462 private void openCameraCommon() {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800463 mPreferenceGroup = loadPreferenceGroup();
Michael Kolbd6954f32013-03-08 20:43:01 -0800464
Erin Dahlgren357b7672013-11-20 17:38:14 -0800465 // TODO: remove dependency on PreferenceGroup
466 mUI.onCameraOpened(mPreferenceGroup, mParameters, this);
Angus Kong0fb819b2013-10-08 13:44:19 -0700467 if (mIsImageCaptureIntent) {
468 mUI.overrideSettings(CameraSettings.KEY_CAMERA_HDR_PLUS,
469 mActivity.getString(R.string.setting_off_value));
470 }
Michael Kolbd6954f32013-03-08 20:43:01 -0800471 updateSceneMode();
472 showTapToFocusToastIfNeeded();
Michael Kolb8872c232013-01-29 10:33:22 -0800473 }
474
ztenghui7b265a62013-09-09 14:58:44 -0700475 @Override
Doris Liu36ebcb12013-10-28 14:44:24 -0700476 public void onPreviewRectChanged(Rect previewRect) {
477 if (mFocusManager != null) mFocusManager.setPreviewRect(previewRect);
Michael Kolbd6954f32013-03-08 20:43:01 -0800478 }
Michael Kolb8872c232013-01-29 10:33:22 -0800479
480 private void resetExposureCompensation() {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800481 SettingsManager settingsManager = mActivity.getSettingsManager();
482 if (settingsManager == null) {
483 Log.e(TAG, "Settings manager is null!");
484 return;
Michael Kolb8872c232013-01-29 10:33:22 -0800485 }
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800486 settingsManager.setDefault(SettingsManager.SETTING_EXPOSURE);
Michael Kolb8872c232013-01-29 10:33:22 -0800487 }
488
Michael Kolb8872c232013-01-29 10:33:22 -0800489 // Snapshots can only be taken after this is called. It should be called
490 // once only. We could have done these things in onCreate() but we want to
491 // make preview screen appear as soon as possible.
492 private void initializeFirstTime() {
Sascha Haeberling330dafb2013-10-10 19:00:41 -0700493 if (mFirstTimeInitialized || mPaused) {
494 return;
495 }
Michael Kolb8872c232013-01-29 10:33:22 -0800496
497 // Initialize location service.
Erin Dahlgren357b7672013-11-20 17:38:14 -0800498 SettingsController settingsController = mActivity.getSettingsController();
499 settingsController.syncLocationManager();
Michael Kolb8872c232013-01-29 10:33:22 -0800500
Michael Kolbd6954f32013-03-08 20:43:01 -0800501 mUI.initializeFirstTime();
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800502 MediaSaver s = getServices().getMediaSaver();
Angus Kong86d36312013-01-31 18:22:44 -0800503 // We set the listener only when both service and shutterbutton
504 // are initialized.
505 if (s != null) {
Angus Kongfd4fc0e2013-11-07 15:38:09 -0800506 s.setQueueListener(this);
Angus Kong86d36312013-01-31 18:22:44 -0800507 }
Michael Kolb8872c232013-01-29 10:33:22 -0800508
Michael Kolb8872c232013-01-29 10:33:22 -0800509 mNamedImages = new NamedImages();
510
511 mFirstTimeInitialized = true;
512 addIdleHandler();
513
514 mActivity.updateStorageSpaceAndHint();
515 }
516
Michael Kolbd6954f32013-03-08 20:43:01 -0800517 // If the activity is paused and resumed, this method will be called in
518 // onResume.
519 private void initializeSecondTime() {
520 // Start location update if needed.
Erin Dahlgren357b7672013-11-20 17:38:14 -0800521 SettingsController settingsController = mActivity.getSettingsController();
522 settingsController.syncLocationManager();
523
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800524 MediaSaver s = getServices().getMediaSaver();
Michael Kolbd6954f32013-03-08 20:43:01 -0800525 if (s != null) {
Angus Kongfd4fc0e2013-11-07 15:38:09 -0800526 s.setQueueListener(this);
Michael Kolbd6954f32013-03-08 20:43:01 -0800527 }
528 mNamedImages = new NamedImages();
529 mUI.initializeSecondTime(mParameters);
Michael Kolbd6954f32013-03-08 20:43:01 -0800530 }
531
Michael Kolb8872c232013-01-29 10:33:22 -0800532 private void showTapToFocusToastIfNeeded() {
533 // Show the tap to focus toast if this is the first start.
Erin Dahlgren357b7672013-11-20 17:38:14 -0800534 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800535 boolean showHint = settingsManager.getBoolean(
536 SettingsManager.SETTING_CAMERA_FIRST_USE_HINT_SHOWN);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800537 // CONVERT THIS SETTING TO A STRING
538 if (mFocusAreaSupported && showHint) {
Michael Kolb8872c232013-01-29 10:33:22 -0800539 // Delay the toast for one second to wait for orientation.
Angus Kong13e87c42013-11-25 10:02:47 -0800540 mHandler.sendEmptyMessageDelayed(MSG_SHOW_TAP_TO_FOCUS_TOAST, 1000);
Michael Kolb8872c232013-01-29 10:33:22 -0800541 }
542 }
543
544 private void addIdleHandler() {
545 MessageQueue queue = Looper.myQueue();
546 queue.addIdleHandler(new MessageQueue.IdleHandler() {
547 @Override
548 public boolean queueIdle() {
549 Storage.ensureOSXCompatible();
550 return false;
551 }
552 });
553 }
554
Sameer Padala2c8cc452013-11-05 18:49:12 -0800555 private void startSmartCamera() {
556 SmartCameraHelper.register(mCameraDevice, mParameters.getPreviewSize(), mActivity,
557 (CameraRootView) mActivity.findViewById(R.id.camera_app_root));
558 }
559
560 private void stopSmartCamera() {
561 SmartCameraHelper.tearDown();
562 }
563
Michael Kolb8872c232013-01-29 10:33:22 -0800564 @Override
565 public void startFaceDetection() {
Michael Kolb8872c232013-01-29 10:33:22 -0800566 if (mFaceDetectionStarted) return;
567 if (mParameters.getMaxNumDetectedFaces() > 0) {
568 mFaceDetectionStarted = true;
Michael Kolb8872c232013-01-29 10:33:22 -0800569 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
Michael Kolbd6954f32013-03-08 20:43:01 -0800570 mUI.onStartFaceDetection(mDisplayOrientation,
571 (info.facing == CameraInfo.CAMERA_FACING_FRONT));
Angus Kong9e765522013-07-31 14:05:20 -0700572 mCameraDevice.setFaceDetectionCallback(mHandler, mUI);
Michael Kolb8872c232013-01-29 10:33:22 -0800573 mCameraDevice.startFaceDetection();
574 }
575 }
576
Michael Kolb8872c232013-01-29 10:33:22 -0800577 @Override
578 public void stopFaceDetection() {
Michael Kolb8872c232013-01-29 10:33:22 -0800579 if (!mFaceDetectionStarted) return;
580 if (mParameters.getMaxNumDetectedFaces() > 0) {
581 mFaceDetectionStarted = false;
Angus Kong9e765522013-07-31 14:05:20 -0700582 mCameraDevice.setFaceDetectionCallback(null, null);
Michael Kolb8872c232013-01-29 10:33:22 -0800583 mCameraDevice.stopFaceDetection();
Michael Kolbd6954f32013-03-08 20:43:01 -0800584 mUI.clearFaces();
Michael Kolb8872c232013-01-29 10:33:22 -0800585 }
586 }
587
Michael Kolb8872c232013-01-29 10:33:22 -0800588 private final class ShutterCallback
Angus Kong9ef99252013-07-18 18:04:19 -0700589 implements CameraShutterCallback {
Angus Kongdcb0ef12013-03-25 23:11:43 -0700590
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800591 private final boolean mNeedsAnimation;
Angus Kongdcb0ef12013-03-25 23:11:43 -0700592
Sascha Haeberling37f36112013-08-06 14:31:52 -0700593 public ShutterCallback(boolean needsAnimation) {
594 mNeedsAnimation = needsAnimation;
Angus Kongdcb0ef12013-03-25 23:11:43 -0700595 }
596
Michael Kolb8872c232013-01-29 10:33:22 -0800597 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700598 public void onShutter(CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800599 mShutterCallbackTime = System.currentTimeMillis();
600 mShutterLag = mShutterCallbackTime - mCaptureStartTime;
601 Log.v(TAG, "mShutterLag = " + mShutterLag + "ms");
Sascha Haeberling37f36112013-08-06 14:31:52 -0700602 if (mNeedsAnimation) {
603 mActivity.runOnUiThread(new Runnable() {
604 @Override
605 public void run() {
606 animateAfterShutter();
607 }
608 });
Angus Kongdcb0ef12013-03-25 23:11:43 -0700609 }
Michael Kolb8872c232013-01-29 10:33:22 -0800610 }
611 }
612
Angus Kong9ef99252013-07-18 18:04:19 -0700613 private final class PostViewPictureCallback
614 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800615 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700616 public void onPictureTaken(byte [] data, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800617 mPostViewPictureCallbackTime = System.currentTimeMillis();
618 Log.v(TAG, "mShutterToPostViewCallbackTime = "
619 + (mPostViewPictureCallbackTime - mShutterCallbackTime)
620 + "ms");
621 }
622 }
623
Angus Kong9ef99252013-07-18 18:04:19 -0700624 private final class RawPictureCallback
625 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800626 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700627 public void onPictureTaken(byte [] rawData, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800628 mRawPictureCallbackTime = System.currentTimeMillis();
629 Log.v(TAG, "mShutterToRawCallbackTime = "
630 + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms");
631 }
632 }
633
Angus Kong9ef99252013-07-18 18:04:19 -0700634 private final class JpegPictureCallback
635 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800636 Location mLocation;
637
638 public JpegPictureCallback(Location loc) {
639 mLocation = loc;
640 }
641
642 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700643 public void onPictureTaken(final byte [] jpegData, CameraProxy camera) {
Sascha Haeberling88901942013-08-28 17:49:00 -0700644 mUI.enableShutter(true);
Michael Kolb8872c232013-01-29 10:33:22 -0800645 if (mPaused) {
646 return;
647 }
Doris Liu6432cd62013-06-13 17:20:31 -0700648 if (mIsImageCaptureIntent) {
649 stopPreview();
650 }
Angus Kongb50b5cb2013-08-09 14:55:20 -0700651 if (mSceneMode == CameraUtil.SCENE_MODE_HDR) {
Doris Liu6432cd62013-06-13 17:20:31 -0700652 mUI.setSwipingEnabled(true);
Michael Kolb8872c232013-01-29 10:33:22 -0800653 }
654
655 mJpegPictureCallbackTime = System.currentTimeMillis();
656 // If postview callback has arrived, the captured image is displayed
657 // in postview callback. If not, the captured image is displayed in
658 // raw picture callback.
659 if (mPostViewPictureCallbackTime != 0) {
660 mShutterToPictureDisplayedTime =
661 mPostViewPictureCallbackTime - mShutterCallbackTime;
662 mPictureDisplayedToJpegCallbackTime =
663 mJpegPictureCallbackTime - mPostViewPictureCallbackTime;
664 } else {
665 mShutterToPictureDisplayedTime =
666 mRawPictureCallbackTime - mShutterCallbackTime;
667 mPictureDisplayedToJpegCallbackTime =
668 mJpegPictureCallbackTime - mRawPictureCallbackTime;
669 }
670 Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = "
671 + mPictureDisplayedToJpegCallbackTime + "ms");
672
Michael Kolb8872c232013-01-29 10:33:22 -0800673 mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden.
674 if (!mIsImageCaptureIntent) {
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700675 setupPreview();
Michael Kolb8872c232013-01-29 10:33:22 -0800676 }
677
Doris Liu36e56fb2013-09-11 17:38:08 -0700678 ExifInterface exif = Exif.getExif(jpegData);
679 int orientation = Exif.getOrientation(exif);
Ruben Brunkd217ed02013-10-08 23:31:13 -0700680
Ruben Brunkd7488272013-10-10 18:45:53 -0700681 if (!mIsImageCaptureIntent) {
Michael Kolb8872c232013-01-29 10:33:22 -0800682 // Calculate the width and the height of the jpeg.
683 Size s = mParameters.getPictureSize();
Michael Kolb8872c232013-01-29 10:33:22 -0800684 int width, height;
685 if ((mJpegRotation + orientation) % 180 == 0) {
686 width = s.width;
687 height = s.height;
688 } else {
689 width = s.height;
690 height = s.width;
691 }
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700692 NamedEntity name = mNamedImages.getNextNameEntity();
693 String title = (name == null) ? null : name.title;
694 long date = (name == null) ? -1 : name.date;
Ruben Brunkd7488272013-10-10 18:45:53 -0700695
696 // Handle debug mode outputs
697 if (mDebugUri != null) {
698 // If using a debug uri, save jpeg there.
699 saveToDebugUri(jpegData);
700
701 // Adjust the title of the debug image shown in mediastore.
702 if (title != null) {
703 title = DEBUG_IMAGE_PREFIX + title;
704 }
705 }
706
Michael Kolb8872c232013-01-29 10:33:22 -0800707 if (title == null) {
708 Log.e(TAG, "Unbalanced name/data pair");
709 } else {
710 if (date == -1) date = mCaptureStartTime;
Angus Kong0d00a892013-03-26 11:40:40 -0700711 if (mHeading >= 0) {
712 // heading direction has been updated by the sensor.
713 ExifTag directionRefTag = exif.buildTag(
714 ExifInterface.TAG_GPS_IMG_DIRECTION_REF,
715 ExifInterface.GpsTrackRef.MAGNETIC_DIRECTION);
716 ExifTag directionTag = exif.buildTag(
717 ExifInterface.TAG_GPS_IMG_DIRECTION,
718 new Rational(mHeading, 1));
719 exif.setTag(directionRefTag);
720 exif.setTag(directionTag);
721 }
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800722 getServices().getMediaSaver().addImage(
Angus Kong86d36312013-01-31 18:22:44 -0800723 jpegData, title, date, mLocation, width, height,
Angus Kong0d00a892013-03-26 11:40:40 -0700724 orientation, exif, mOnMediaSavedListener, mContentResolver);
Michael Kolb8872c232013-01-29 10:33:22 -0800725 }
Doris Liuce2acbc2013-08-21 18:45:29 -0700726 // Animate capture with real jpeg data instead of a preview frame.
Doris Liu29da2db2013-08-28 14:28:45 -0700727 mUI.animateCapture(jpegData, orientation, mMirror);
Michael Kolb8872c232013-01-29 10:33:22 -0800728 } else {
729 mJpegImageData = jpegData;
730 if (!mQuickCapture) {
Doris Liu36e56fb2013-09-11 17:38:08 -0700731 mUI.showCapturedImageForReview(jpegData, orientation, mMirror);
Michael Kolb8872c232013-01-29 10:33:22 -0800732 } else {
Michael Kolbd6954f32013-03-08 20:43:01 -0800733 onCaptureDone();
Michael Kolb8872c232013-01-29 10:33:22 -0800734 }
735 }
736
737 // Check this in advance of each shot so we don't add to shutter
738 // latency. It's true that someone else could write to the SD card in
739 // the mean time and fill it, but that could have happened between the
740 // shutter press and saving the JPEG too.
741 mActivity.updateStorageSpaceAndHint();
742
743 long now = System.currentTimeMillis();
744 mJpegCallbackFinishTime = now - mJpegPictureCallbackTime;
745 Log.v(TAG, "mJpegCallbackFinishTime = "
746 + mJpegCallbackFinishTime + "ms");
747 mJpegPictureCallbackTime = 0;
748 }
749 }
750
Angus Kong9ef99252013-07-18 18:04:19 -0700751 private final class AutoFocusCallback implements CameraAFCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800752 @Override
753 public void onAutoFocus(
Angus Kong9ef99252013-07-18 18:04:19 -0700754 boolean focused, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800755 if (mPaused) return;
756
757 mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime;
758 Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms");
759 setCameraState(IDLE);
Michael Kolbd6954f32013-03-08 20:43:01 -0800760 mFocusManager.onAutoFocus(focused, mUI.isShutterPressed());
Michael Kolb8872c232013-01-29 10:33:22 -0800761 }
762 }
763
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700764 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -0800765 private final class AutoFocusMoveCallback
Angus Kong9ef99252013-07-18 18:04:19 -0700766 implements CameraAFMoveCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800767 @Override
768 public void onAutoFocusMoving(
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700769 boolean moving, CameraProxy camera) {
770 mFocusManager.onAutoFocusMoving(moving);
Michael Kolb8872c232013-01-29 10:33:22 -0800771 }
772 }
773
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700774 /**
775 * This class is just a thread-safe queue for name,date holder objects.
776 */
777 public static class NamedImages {
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800778 private final Vector<NamedEntity> mQueue;
Michael Kolb8872c232013-01-29 10:33:22 -0800779
780 public NamedImages() {
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700781 mQueue = new Vector<NamedEntity>();
Michael Kolb8872c232013-01-29 10:33:22 -0800782 }
783
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700784 public void nameNewImage(long date) {
Michael Kolb8872c232013-01-29 10:33:22 -0800785 NamedEntity r = new NamedEntity();
Angus Kongb50b5cb2013-08-09 14:55:20 -0700786 r.title = CameraUtil.createJpegName(date);
Michael Kolb8872c232013-01-29 10:33:22 -0800787 r.date = date;
788 mQueue.add(r);
789 }
790
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700791 public NamedEntity getNextNameEntity() {
792 synchronized(mQueue) {
793 if (!mQueue.isEmpty()) {
794 return mQueue.remove(0);
795 }
Michael Kolb8872c232013-01-29 10:33:22 -0800796 }
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700797 return null;
Michael Kolb8872c232013-01-29 10:33:22 -0800798 }
799
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700800 public static class NamedEntity {
801 public String title;
802 public long date;
Michael Kolb8872c232013-01-29 10:33:22 -0800803 }
804 }
805
806 private void setCameraState(int state) {
807 mCameraState = state;
808 switch (state) {
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700809 case PhotoController.PREVIEW_STOPPED:
810 case PhotoController.SNAPSHOT_IN_PROGRESS:
811 case PhotoController.SWITCHING_CAMERA:
Doris Liuf55f3c42013-11-20 00:24:46 -0800812 // TODO: Tell app UI to disable swipe
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700813 break;
814 case PhotoController.IDLE:
Doris Liuf55f3c42013-11-20 00:24:46 -0800815 // TODO: Tell app UI to enable swipe
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700816 break;
Michael Kolb8872c232013-01-29 10:33:22 -0800817 }
818 }
819
Sascha Haeberling37f36112013-08-06 14:31:52 -0700820 private void animateAfterShutter() {
Michael Kolb8872c232013-01-29 10:33:22 -0800821 // Only animate when in full screen capture mode
822 // i.e. If monkey/a user swipes to the gallery during picture taking,
823 // don't show animation
Doris Liuc2e9abd2013-06-19 14:20:51 -0700824 if (!mIsImageCaptureIntent) {
825 mUI.animateFlash();
Doris Liuc2e9abd2013-06-19 14:20:51 -0700826 }
Michael Kolb8872c232013-01-29 10:33:22 -0800827 }
828
829 @Override
830 public boolean capture() {
831 // If we are already in the middle of taking a snapshot or the image save request
832 // is full then ignore.
833 if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS
Angus Kong86d36312013-01-31 18:22:44 -0800834 || mCameraState == SWITCHING_CAMERA
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800835 || getServices().getMediaSaver() == null
836 || getServices().getMediaSaver().isQueueFull()) {
Michael Kolb8872c232013-01-29 10:33:22 -0800837 return false;
838 }
839 mCaptureStartTime = System.currentTimeMillis();
840 mPostViewPictureCallbackTime = 0;
841 mJpegImageData = null;
842
Angus Kongb50b5cb2013-08-09 14:55:20 -0700843 final boolean animateBefore = (mSceneMode == CameraUtil.SCENE_MODE_HDR);
Michael Kolb8872c232013-01-29 10:33:22 -0800844
845 if (animateBefore) {
Sascha Haeberling37f36112013-08-06 14:31:52 -0700846 animateAfterShutter();
Michael Kolb8872c232013-01-29 10:33:22 -0800847 }
848
849 // Set rotation and gps data.
Doris Liu3cf565c2013-02-15 10:55:37 -0800850 int orientation;
851 // We need to be consistent with the framework orientation (i.e. the
852 // orientation of the UI.) when the auto-rotate screen setting is on.
853 if (mActivity.isAutoRotateScreen()) {
854 orientation = (360 - mDisplayRotation) % 360;
855 } else {
856 orientation = mOrientation;
857 }
Angus Kong20fad242013-11-11 18:23:46 -0800858 mJpegRotation = CameraUtil.getJpegRotation(mActivity, mCameraId, orientation);
Michael Kolb8872c232013-01-29 10:33:22 -0800859 mParameters.setRotation(mJpegRotation);
Erin Dahlgrenc120b0f2013-11-19 10:53:49 -0800860 Location loc = mActivity.getLocationManager().getCurrentLocation();
Angus Kongb50b5cb2013-08-09 14:55:20 -0700861 CameraUtil.setGpsParameters(mParameters, loc);
Michael Kolb8872c232013-01-29 10:33:22 -0800862 mCameraDevice.setParameters(mParameters);
863
Sascha Haeberling88901942013-08-28 17:49:00 -0700864 // We don't want user to press the button again while taking a
865 // multi-second HDR photo.
866 mUI.enableShutter(false);
Angus Kong9ef99252013-07-18 18:04:19 -0700867 mCameraDevice.takePicture(mHandler,
868 new ShutterCallback(!animateBefore),
Angus Kongdcb0ef12013-03-25 23:11:43 -0700869 mRawPictureCallback, mPostViewPictureCallback,
Angus Kong9ef99252013-07-18 18:04:19 -0700870 new JpegPictureCallback(loc));
Michael Kolb8872c232013-01-29 10:33:22 -0800871
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700872 mNamedImages.nameNewImage(mCaptureStartTime);
Michael Kolb8872c232013-01-29 10:33:22 -0800873
874 mFaceDetectionStarted = false;
875 setCameraState(SNAPSHOT_IN_PROGRESS);
Bobby Georgescu301b6462013-04-01 15:33:17 -0700876 UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
Seth Raphaelcbd82672013-11-05 10:12:36 -0800877 UsageStatistics.ACTION_CAPTURE_DONE, "Photo", 0,
Seth Raphael9e039852013-11-12 11:21:17 -0800878 UsageStatistics.hashFileName(mNamedImages.mQueue.lastElement().title + ".jpg"),
879 mParameters.flatten());
Michael Kolb8872c232013-01-29 10:33:22 -0800880 return true;
881 }
882
883 @Override
884 public void setFocusParameters() {
885 setCameraParameters(UPDATE_PARAM_PREFERENCE);
886 }
887
Michael Kolbd6954f32013-03-08 20:43:01 -0800888 private void updateSceneMode() {
Michael Kolb8872c232013-01-29 10:33:22 -0800889 // If scene mode is set, we cannot set flash mode, white balance, and
890 // focus mode, instead, we read it from driver
891 if (!Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
892 overrideCameraSettings(mParameters.getFlashMode(),
893 mParameters.getWhiteBalance(), mParameters.getFocusMode());
894 } else {
895 overrideCameraSettings(null, null, null);
896 }
897 }
898
899 private void overrideCameraSettings(final String flashMode,
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700900 final String whiteBalance, final String focusMode) {
Michael Kolbd6954f32013-03-08 20:43:01 -0800901 mUI.overrideSettings(
902 CameraSettings.KEY_FLASH_MODE, flashMode,
903 CameraSettings.KEY_WHITE_BALANCE, whiteBalance,
904 CameraSettings.KEY_FOCUS_MODE, focusMode);
Michael Kolb8872c232013-01-29 10:33:22 -0800905 }
906
Erin Dahlgren357b7672013-11-20 17:38:14 -0800907 private PreferenceGroup loadPreferenceGroup() {
Michael Kolb8872c232013-01-29 10:33:22 -0800908 CameraSettings settings = new CameraSettings(mActivity, mInitialParams,
Erin Dahlgren357b7672013-11-20 17:38:14 -0800909 mCameraId, CameraHolder.instance().getCameraInfo());
910 return settings.getPreferenceGroup(R.xml.camera_preferences);
Michael Kolb8872c232013-01-29 10:33:22 -0800911 }
912
913 @Override
Michael Kolb8872c232013-01-29 10:33:22 -0800914 public void onOrientationChanged(int orientation) {
915 // We keep the last known orientation. So if the user first orient
916 // the camera then point the camera to floor or sky, we still have
917 // the correct orientation.
918 if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return;
Angus Kongb50b5cb2013-08-09 14:55:20 -0700919 mOrientation = CameraUtil.roundOrientation(orientation, mOrientation);
Michael Kolb8872c232013-01-29 10:33:22 -0800920
921 // Show the toast after getting the first orientation changed.
Angus Kong13e87c42013-11-25 10:02:47 -0800922 if (mHandler.hasMessages(MSG_SHOW_TAP_TO_FOCUS_TOAST)) {
923 mHandler.removeMessages(MSG_SHOW_TAP_TO_FOCUS_TOAST);
Michael Kolb8872c232013-01-29 10:33:22 -0800924 showTapToFocusToast();
925 }
926 }
927
928 @Override
Angus Kong20fad242013-11-11 18:23:46 -0800929 public void onCameraAvailable(CameraProxy cameraProxy) {
930 if (mPaused) {
931 return;
932 }
933 mCameraDevice = cameraProxy;
934
Erin Dahlgren357b7672013-11-20 17:38:14 -0800935 resetExposureCompensation();
Angus Kong20fad242013-11-11 18:23:46 -0800936 initializeCapabilities();
937
938 // Reset zoom value index.
939 mZoomValue = 0;
940 if (mFocusManager == null) {
941 initializeFocusManager();
942 }
943 mFocusManager.setParameters(mInitialParams);
944
945 mParameters = mCameraDevice.getParameters();
946 setCameraParameters(UPDATE_PARAM_ALL);
947 mCameraPreviewParamsReady = true;
948 startPreview();
949
950 onCameraOpened();
951 }
952
953 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -0800954 public void onCaptureCancelled() {
955 mActivity.setResultEx(Activity.RESULT_CANCELED, new Intent());
956 mActivity.finish();
Michael Kolb8872c232013-01-29 10:33:22 -0800957 }
958
Michael Kolbd6954f32013-03-08 20:43:01 -0800959 @Override
960 public void onCaptureRetake() {
Michael Kolb8872c232013-01-29 10:33:22 -0800961 if (mPaused)
962 return;
Michael Kolbd6954f32013-03-08 20:43:01 -0800963 mUI.hidePostCaptureAlert();
Michael Kolb8872c232013-01-29 10:33:22 -0800964 setupPreview();
965 }
966
Michael Kolbd6954f32013-03-08 20:43:01 -0800967 @Override
968 public void onCaptureDone() {
Michael Kolb8872c232013-01-29 10:33:22 -0800969 if (mPaused) {
970 return;
971 }
972
973 byte[] data = mJpegImageData;
974
975 if (mCropValue == null) {
976 // First handle the no crop case -- just return the value. If the
977 // caller specifies a "save uri" then write the data to its
978 // stream. Otherwise, pass back a scaled down version of the bitmap
979 // directly in the extras.
980 if (mSaveUri != null) {
981 OutputStream outputStream = null;
982 try {
983 outputStream = mContentResolver.openOutputStream(mSaveUri);
984 outputStream.write(data);
985 outputStream.close();
986
987 mActivity.setResultEx(Activity.RESULT_OK);
988 mActivity.finish();
989 } catch (IOException ex) {
990 // ignore exception
991 } finally {
Angus Kongb50b5cb2013-08-09 14:55:20 -0700992 CameraUtil.closeSilently(outputStream);
Michael Kolb8872c232013-01-29 10:33:22 -0800993 }
994 } else {
Angus Kong0d00a892013-03-26 11:40:40 -0700995 ExifInterface exif = Exif.getExif(data);
996 int orientation = Exif.getOrientation(exif);
Angus Kongb50b5cb2013-08-09 14:55:20 -0700997 Bitmap bitmap = CameraUtil.makeBitmap(data, 50 * 1024);
998 bitmap = CameraUtil.rotate(bitmap, orientation);
Michael Kolb8872c232013-01-29 10:33:22 -0800999 mActivity.setResultEx(Activity.RESULT_OK,
1000 new Intent("inline-data").putExtra("data", bitmap));
1001 mActivity.finish();
1002 }
1003 } else {
1004 // Save the image to a temp file and invoke the cropper
1005 Uri tempUri = null;
1006 FileOutputStream tempStream = null;
1007 try {
1008 File path = mActivity.getFileStreamPath(sTempCropFilename);
1009 path.delete();
1010 tempStream = mActivity.openFileOutput(sTempCropFilename, 0);
1011 tempStream.write(data);
1012 tempStream.close();
1013 tempUri = Uri.fromFile(path);
1014 } catch (FileNotFoundException ex) {
1015 mActivity.setResultEx(Activity.RESULT_CANCELED);
1016 mActivity.finish();
1017 return;
1018 } catch (IOException ex) {
1019 mActivity.setResultEx(Activity.RESULT_CANCELED);
1020 mActivity.finish();
1021 return;
1022 } finally {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001023 CameraUtil.closeSilently(tempStream);
Michael Kolb8872c232013-01-29 10:33:22 -08001024 }
1025
1026 Bundle newExtras = new Bundle();
1027 if (mCropValue.equals("circle")) {
1028 newExtras.putString("circleCrop", "true");
1029 }
1030 if (mSaveUri != null) {
1031 newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
1032 } else {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001033 newExtras.putBoolean(CameraUtil.KEY_RETURN_DATA, true);
Michael Kolb8872c232013-01-29 10:33:22 -08001034 }
1035 if (mActivity.isSecureCamera()) {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001036 newExtras.putBoolean(CameraUtil.KEY_SHOW_WHEN_LOCKED, true);
Michael Kolb8872c232013-01-29 10:33:22 -08001037 }
1038
Sascha Haeberling37f36112013-08-06 14:31:52 -07001039 // TODO: Share this constant.
Sascha Haeberling8e963a52013-08-06 11:43:02 -07001040 final String CROP_ACTION = "com.android.camera.action.CROP";
1041 Intent cropIntent = new Intent(CROP_ACTION);
Michael Kolb8872c232013-01-29 10:33:22 -08001042
1043 cropIntent.setData(tempUri);
1044 cropIntent.putExtras(newExtras);
1045
1046 mActivity.startActivityForResult(cropIntent, REQUEST_CROP);
1047 }
1048 }
1049
Michael Kolb8872c232013-01-29 10:33:22 -08001050 @Override
1051 public void onShutterButtonFocus(boolean pressed) {
Michael Kolbd6954f32013-03-08 20:43:01 -08001052 if (mPaused || mUI.collapseCameraControls()
Michael Kolb8872c232013-01-29 10:33:22 -08001053 || (mCameraState == SNAPSHOT_IN_PROGRESS)
1054 || (mCameraState == PREVIEW_STOPPED)) return;
1055
1056 // Do not do focus if there is not enough storage.
1057 if (pressed && !canTakePicture()) return;
1058
1059 if (pressed) {
Michael Kolb8872c232013-01-29 10:33:22 -08001060 mFocusManager.onShutterDown();
1061 } else {
Doris Liuda50e052013-02-07 14:36:32 -08001062 // for countdown mode, we need to postpone the shutter release
1063 // i.e. lock the focus during countdown.
Michael Kolbd6954f32013-03-08 20:43:01 -08001064 if (!mUI.isCountingDown()) {
Doris Liuda50e052013-02-07 14:36:32 -08001065 mFocusManager.onShutterUp();
1066 }
Michael Kolb8872c232013-01-29 10:33:22 -08001067 }
1068 }
1069
1070 @Override
1071 public void onShutterButtonClick() {
Michael Kolbd6954f32013-03-08 20:43:01 -08001072 if (mPaused || mUI.collapseCameraControls()
Michael Kolb8872c232013-01-29 10:33:22 -08001073 || (mCameraState == SWITCHING_CAMERA)
1074 || (mCameraState == PREVIEW_STOPPED)) return;
1075
1076 // Do not take the picture if there is not enough storage.
Angus Kong2dcc0a92013-09-25 13:00:08 -07001077 if (mActivity.getStorageSpaceBytes() <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
Michael Kolb8872c232013-01-29 10:33:22 -08001078 Log.i(TAG, "Not enough space or storage not ready. remaining="
Angus Kong2dcc0a92013-09-25 13:00:08 -07001079 + mActivity.getStorageSpaceBytes());
Michael Kolb8872c232013-01-29 10:33:22 -08001080 return;
1081 }
1082 Log.v(TAG, "onShutterButtonClick: mCameraState=" + mCameraState);
1083
Angus Kongb50b5cb2013-08-09 14:55:20 -07001084 if (mSceneMode == CameraUtil.SCENE_MODE_HDR) {
Doris Liu6432cd62013-06-13 17:20:31 -07001085 mUI.setSwipingEnabled(false);
Doris Liu9cdfe002013-04-16 09:50:56 -07001086 }
Michael Kolb8872c232013-01-29 10:33:22 -08001087 // If the user wants to do a snapshot while the previous one is still
1088 // in progress, remember the fact and do it after we finish the previous
1089 // one and re-start the preview. Snapshot in progress also includes the
1090 // state that autofocus is focusing and a picture will be taken when
1091 // focus callback arrives.
1092 if ((mFocusManager.isFocusingSnapOnFinish() || mCameraState == SNAPSHOT_IN_PROGRESS)
1093 && !mIsImageCaptureIntent) {
1094 mSnapshotOnIdle = true;
1095 return;
1096 }
1097
Erin Dahlgren357b7672013-11-20 17:38:14 -08001098 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001099 String timer = settingsManager.get(SettingsManager.SETTING_TIMER);
1100 String playSound = settingsManager.get(SettingsManager.SETTING_TIMER_SOUND_EFFECTS);
Michael Kolb8872c232013-01-29 10:33:22 -08001101 int seconds = Integer.parseInt(timer);
1102 // When shutter button is pressed, check whether the previous countdown is
1103 // finished. If not, cancel the previous countdown and start a new one.
Michael Kolbd6954f32013-03-08 20:43:01 -08001104 if (mUI.isCountingDown()) {
1105 mUI.cancelCountDown();
1106 }
1107 if (seconds > 0) {
Erin Dahlgren357b7672013-11-20 17:38:14 -08001108 mUI.startCountDown(seconds, playSound.equals(SettingsManager.VALUE_ON));
Michael Kolb8872c232013-01-29 10:33:22 -08001109 } else {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001110 mSnapshotOnIdle = false;
1111 mFocusManager.doSnap();
Michael Kolb8872c232013-01-29 10:33:22 -08001112 }
1113 }
1114
Andy Huibersdef975d2013-11-22 09:13:39 -08001115 private void onResumeTasks() {
1116 Log.v(TAG, "Executing onResumeTasks.");
Michael Kolb8872c232013-01-29 10:33:22 -08001117 if (mOpenCameraFail || mCameraDisabled) return;
1118
Angus Kong20fad242013-11-11 18:23:46 -08001119 mActivity.getCameraProvider().requestCamera(mCameraId);
1120
Michael Kolb8872c232013-01-29 10:33:22 -08001121 mJpegPictureCallbackTime = 0;
1122 mZoomValue = 0;
Angus Kong20fad242013-11-11 18:23:46 -08001123
1124 mOnResumeTime = SystemClock.uptimeMillis();
1125 checkDisplayRotation();
Michael Kolb8872c232013-01-29 10:33:22 -08001126
1127 // If first time initialization is not finished, put it in the
1128 // message queue.
1129 if (!mFirstTimeInitialized) {
Angus Kong13e87c42013-11-25 10:02:47 -08001130 mHandler.sendEmptyMessage(MSG_FIRST_TIME_INIT);
Michael Kolb8872c232013-01-29 10:33:22 -08001131 } else {
1132 initializeSecondTime();
1133 }
Doris Liu59390062013-10-10 17:20:56 -07001134 mUI.initDisplayChangeListener();
Michael Kolb8872c232013-01-29 10:33:22 -08001135
Bobby Georgescu0a7dd572013-03-12 22:45:17 -07001136 UsageStatistics.onContentViewChanged(
1137 UsageStatistics.COMPONENT_CAMERA, "PhotoModule");
Angus Kong0d00a892013-03-26 11:40:40 -07001138
1139 Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1140 if (gsensor != null) {
1141 mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL);
1142 }
1143
1144 Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1145 if (msensor != null) {
1146 mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_NORMAL);
1147 }
Michael Kolb8872c232013-01-29 10:33:22 -08001148 }
1149
Angus Kongc4e66562013-11-22 23:03:21 -08001150 /**
1151 * The focus manager is the first UI related element to get initialized,
1152 * and it requires the RenderOverlay, so initialize it here
1153 */
1154 private void initializeFocusManager() {
1155 // Create FocusManager object. startPreview needs it.
1156 // if mFocusManager not null, reuse it
1157 // otherwise create a new instance
1158 if (mFocusManager != null) {
1159 mFocusManager.removeMessages();
1160 } else {
1161 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
1162 mMirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
1163 String[] defaultFocusModes = mActivity.getResources().getStringArray(
1164 R.array.pref_camera_focusmode_default_array);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001165 mFocusManager = new FocusOverlayManager(mActivity.getSettingsManager(),
1166 defaultFocusModes,
Angus Kongc4e66562013-11-22 23:03:21 -08001167 mInitialParams, this, mMirror,
1168 mActivity.getMainLooper(), mUI);
1169 }
Angus Kong20fad242013-11-11 18:23:46 -08001170 }
1171
1172 @Override
Angus Kongc4e66562013-11-22 23:03:21 -08001173 public void resume() {
1174 mPaused = false;
1175 // Add delay on resume from lock screen only, in order to to speed up
1176 // the onResume --> onPause --> onResume cycle from lock screen.
1177 // Don't do always because letting go of thread can cause delay.
1178 String action = mActivity.getIntent().getAction();
1179 if (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(action)
1180 || MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action)) {
1181 Log.v(TAG, "On resume, from lock screen.");
1182 // Note: onPauseAfterSuper() will delete this runnable, so we will
1183 // at most have 1 copy queued up.
1184 mHandler.postDelayed(new Runnable() {
Sameer Padala2c8cc452013-11-05 18:49:12 -08001185 @Override
Angus Kongc4e66562013-11-22 23:03:21 -08001186 public void run() {
1187 onResumeTasks();
1188 }
1189 }, ON_RESUME_TASKS_DELAY_MSEC);
1190 } else {
1191 Log.v(TAG, "On resume.");
1192 onResumeTasks();
1193 }
1194 }
1195
1196 @Override
1197 public void pause() {
Michael Kolb8872c232013-01-29 10:33:22 -08001198 mPaused = true;
Angus Kong0d00a892013-03-26 11:40:40 -07001199 Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1200 if (gsensor != null) {
1201 mSensorManager.unregisterListener(this, gsensor);
1202 }
1203
1204 Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1205 if (msensor != null) {
1206 mSensorManager.unregisterListener(this, msensor);
1207 }
Doris Liu3a45c332013-10-15 19:10:28 -07001208 mUI.showPreviewCover();
Andy Huibersdef975d2013-11-22 09:13:39 -08001209
Michael Kolb8872c232013-01-29 10:33:22 -08001210 // Reset the focus first. Camera CTS does not guarantee that
1211 // cancelAutoFocus is allowed after preview stops.
1212 if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
1213 mCameraDevice.cancelAutoFocus();
1214 }
Andy Huibersdef975d2013-11-22 09:13:39 -08001215
Erin Dahlgrenb09b53e2013-11-06 11:57:51 -08001216 // If the camera has not been opened asynchronously yet,
1217 // and startPreview hasn't been called, then this is a no-op.
1218 // (e.g. onResume -> onPause -> onResume).
Michael Kolb8872c232013-01-29 10:33:22 -08001219 stopPreview();
Michael Kolb8872c232013-01-29 10:33:22 -08001220
Angus Kongce5480e2013-01-29 17:43:48 -08001221 mNamedImages = null;
Michael Kolb8872c232013-01-29 10:33:22 -08001222
1223 if (mLocationManager != null) mLocationManager.recordLocation(false);
1224
1225 // If we are in an image capture intent and has taken
1226 // a picture, we just clear it in onPause.
1227 mJpegImageData = null;
1228
Angus Kongdcccc512013-08-08 17:06:03 -07001229 // Remove the messages and runnables in the queue.
1230 mHandler.removeCallbacksAndMessages(null);
Michael Kolb8872c232013-01-29 10:33:22 -08001231
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001232 closeCamera();
Angus Kong13e87c42013-11-25 10:02:47 -08001233 mActivity.enableKeepScreenOn(false);
Michael Kolbd6954f32013-03-08 20:43:01 -08001234 mUI.onPause();
1235
Michael Kolb8872c232013-01-29 10:33:22 -08001236 mPendingSwitchCameraId = -1;
1237 if (mFocusManager != null) mFocusManager.removeMessages();
Sascha Haeberling280fd3e2013-11-21 13:52:15 -08001238 MediaSaver s = getServices().getMediaSaver();
Angus Kong86d36312013-01-31 18:22:44 -08001239 if (s != null) {
Angus Kongfd4fc0e2013-11-07 15:38:09 -08001240 s.setQueueListener(null);
Angus Kong86d36312013-01-31 18:22:44 -08001241 }
Doris Liu59390062013-10-10 17:20:56 -07001242 mUI.removeDisplayChangeListener();
Michael Kolb8872c232013-01-29 10:33:22 -08001243 }
1244
Angus Kong20fad242013-11-11 18:23:46 -08001245 @Override
1246 public void destroy() {
1247 // TODO: implement this.
1248 }
1249
1250 @Override
1251 public void onPreviewSizeChanged(int width, int height) {
1252 // TODO: implement this.
1253 }
1254
1255 @Override
Michael Kolb8872c232013-01-29 10:33:22 -08001256 public void onConfigurationChanged(Configuration newConfig) {
1257 Log.v(TAG, "onConfigurationChanged");
1258 setDisplayOrientation();
Michael Kolb8872c232013-01-29 10:33:22 -08001259 }
1260
1261 @Override
Doris Liu6432cd62013-06-13 17:20:31 -07001262 public void updateCameraOrientation() {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001263 if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) {
Doris Liu6432cd62013-06-13 17:20:31 -07001264 setDisplayOrientation();
1265 }
1266 }
1267
Michael Kolb8872c232013-01-29 10:33:22 -08001268 private boolean canTakePicture() {
Angus Kong2dcc0a92013-09-25 13:00:08 -07001269 return isCameraIdle() && (mActivity.getStorageSpaceBytes() > Storage.LOW_STORAGE_THRESHOLD_BYTES);
Michael Kolb8872c232013-01-29 10:33:22 -08001270 }
1271
1272 @Override
1273 public void autoFocus() {
1274 mFocusStartTime = System.currentTimeMillis();
Angus Kong9ef99252013-07-18 18:04:19 -07001275 mCameraDevice.autoFocus(mHandler, mAutoFocusCallback);
Michael Kolb8872c232013-01-29 10:33:22 -08001276 setCameraState(FOCUSING);
1277 }
1278
1279 @Override
1280 public void cancelAutoFocus() {
1281 mCameraDevice.cancelAutoFocus();
1282 setCameraState(IDLE);
1283 setCameraParameters(UPDATE_PARAM_PREFERENCE);
1284 }
1285
1286 // Preview area is touched. Handle touch focus.
1287 @Override
1288 public void onSingleTapUp(View view, int x, int y) {
1289 if (mPaused || mCameraDevice == null || !mFirstTimeInitialized
1290 || mCameraState == SNAPSHOT_IN_PROGRESS
1291 || mCameraState == SWITCHING_CAMERA
1292 || mCameraState == PREVIEW_STOPPED) {
1293 return;
1294 }
1295
Michael Kolb8872c232013-01-29 10:33:22 -08001296 // Check if metering area or focus area is supported.
1297 if (!mFocusAreaSupported && !mMeteringAreaSupported) return;
1298 mFocusManager.onSingleTapUp(x, y);
1299 }
1300
1301 @Override
1302 public boolean onBackPressed() {
Michael Kolbd6954f32013-03-08 20:43:01 -08001303 return mUI.onBackPressed();
Michael Kolb8872c232013-01-29 10:33:22 -08001304 }
1305
1306 @Override
1307 public boolean onKeyDown(int keyCode, KeyEvent event) {
1308 switch (keyCode) {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001309 case KeyEvent.KEYCODE_VOLUME_UP:
1310 case KeyEvent.KEYCODE_VOLUME_DOWN:
1311 case KeyEvent.KEYCODE_FOCUS:
1312 if (/*TODO: mActivity.isInCameraApp() &&*/ mFirstTimeInitialized) {
1313 if (event.getRepeatCount() == 0) {
1314 onShutterButtonFocus(true);
1315 }
1316 return true;
1317 }
1318 return false;
1319 case KeyEvent.KEYCODE_CAMERA:
1320 if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1321 onShutterButtonClick();
Michael Kolb8872c232013-01-29 10:33:22 -08001322 }
1323 return true;
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001324 case KeyEvent.KEYCODE_DPAD_CENTER:
1325 // If we get a dpad center event without any focused view, move
1326 // the focus to the shutter button and press it.
1327 if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1328 // Start auto-focus immediately to reduce shutter lag. After
1329 // the shutter button gets the focus, onShutterButtonFocus()
1330 // will be called again but it is fine.
1331 onShutterButtonFocus(true);
1332 mUI.pressShutterButton();
1333 }
1334 return true;
Michael Kolb8872c232013-01-29 10:33:22 -08001335 }
1336 return false;
1337 }
1338
1339 @Override
1340 public boolean onKeyUp(int keyCode, KeyEvent event) {
1341 switch (keyCode) {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001342 case KeyEvent.KEYCODE_VOLUME_UP:
1343 case KeyEvent.KEYCODE_VOLUME_DOWN:
1344 if (/*mActivity.isInCameraApp() && */ mFirstTimeInitialized) {
1345 onShutterButtonClick();
1346 return true;
1347 }
1348 return false;
1349 case KeyEvent.KEYCODE_FOCUS:
1350 if (mFirstTimeInitialized) {
1351 onShutterButtonFocus(false);
1352 }
Michael Kolb8872c232013-01-29 10:33:22 -08001353 return true;
Michael Kolb8872c232013-01-29 10:33:22 -08001354 }
1355 return false;
1356 }
1357
Michael Kolb8872c232013-01-29 10:33:22 -08001358 private void closeCamera() {
1359 if (mCameraDevice != null) {
1360 mCameraDevice.setZoomChangeListener(null);
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001361 mCameraDevice.setFaceDetectionCallback(null, null);
Michael Kolb8872c232013-01-29 10:33:22 -08001362 mCameraDevice.setErrorCallback(null);
Ruben Brunk59147832013-11-05 15:53:28 -08001363
Michael Kolb8872c232013-01-29 10:33:22 -08001364 mFaceDetectionStarted = false;
Angus Kong20fad242013-11-11 18:23:46 -08001365 mActivity.getCameraProvider().releaseCamera(mCameraDevice.getCameraId());
Michael Kolb8872c232013-01-29 10:33:22 -08001366 mCameraDevice = null;
1367 setCameraState(PREVIEW_STOPPED);
1368 mFocusManager.onCameraReleased();
1369 }
1370 }
1371
1372 private void setDisplayOrientation() {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001373 mDisplayRotation = CameraUtil.getDisplayRotation(mActivity);
1374 mDisplayOrientation = CameraUtil.getDisplayOrientation(mDisplayRotation, mCameraId);
Doris Liu6432cd62013-06-13 17:20:31 -07001375 mCameraDisplayOrientation = mDisplayOrientation;
Michael Kolbd6954f32013-03-08 20:43:01 -08001376 mUI.setDisplayOrientation(mDisplayOrientation);
Michael Kolb8872c232013-01-29 10:33:22 -08001377 if (mFocusManager != null) {
1378 mFocusManager.setDisplayOrientation(mDisplayOrientation);
1379 }
Doris Liu6432cd62013-06-13 17:20:31 -07001380 // Change the camera display orientation
1381 if (mCameraDevice != null) {
1382 mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation);
1383 }
Michael Kolb8872c232013-01-29 10:33:22 -08001384 }
1385
Sascha Haeberlingddef7792013-08-13 14:41:10 -07001386 /** Only called by UI thread. */
Michael Kolb8872c232013-01-29 10:33:22 -08001387 private void setupPreview() {
1388 mFocusManager.resetTouchFocus();
1389 startPreview();
Michael Kolb8872c232013-01-29 10:33:22 -08001390 }
1391
Angus Kong20fad242013-11-11 18:23:46 -08001392 /**
1393 * Returns whether we can/should start the preview or not.
1394 */
1395 private boolean checkPreviewPreconditions() {
1396 if (mPaused) {
1397 return false;
Angus Kongdcccc512013-08-08 17:06:03 -07001398 }
Michael Kolb8872c232013-01-29 10:33:22 -08001399
Angus Kong20fad242013-11-11 18:23:46 -08001400 if (mCameraDevice == null) {
1401 Log.w(TAG, "startPreview: camera device not ready yet.");
1402 return false;
1403 }
1404
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001405 SurfaceTexture st = mUI.getSurfaceTexture();
1406 if (st == null) {
1407 Log.w(TAG, "startPreview: surfaceTexture is not ready.");
Angus Kong20fad242013-11-11 18:23:46 -08001408 return false;
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001409 }
1410
1411 if (!mCameraPreviewParamsReady) {
1412 Log.w(TAG, "startPreview: parameters for preview is not ready.");
Angus Kong20fad242013-11-11 18:23:46 -08001413 return false;
1414 }
1415 return true;
1416 }
1417
1418 /**
1419 * The start/stop preview should only run on the UI thread.
1420 */
1421 private void startPreview() {
1422 if (!checkPreviewPreconditions()) {
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001423 return;
1424 }
Angus Kong20fad242013-11-11 18:23:46 -08001425
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001426 mCameraDevice.setErrorCallback(mErrorCallback);
1427 // ICS camera frameworks has a bug. Face detection state is not cleared 1589
1428 // after taking a picture. Stop the preview to work around it. The bug
1429 // was fixed in JB.
1430 if (mCameraState != PREVIEW_STOPPED) {
1431 stopPreview();
1432 }
1433
1434 setDisplayOrientation();
1435
1436 if (!mSnapshotOnIdle) {
1437 // If the focus mode is continuous autofocus, call cancelAutoFocus to
1438 // resume it because it may have been paused by autoFocus call.
Erin Dahlgren357b7672013-11-20 17:38:14 -08001439 String focusMode = mFocusManager.getFocusMode();
1440 if (CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE.equals(focusMode)) {
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001441 mCameraDevice.cancelAutoFocus();
Michael Kolb8872c232013-01-29 10:33:22 -08001442 }
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001443 mFocusManager.setAeAwbLock(false); // Unlock AE and AWB.
1444 }
1445 setCameraParameters(UPDATE_PARAM_ALL);
1446 // Let UI set its expected aspect ratio
Angus Kong20fad242013-11-11 18:23:46 -08001447 mCameraDevice.setPreviewTexture(mUI.getSurfaceTexture());
Michael Kolb8872c232013-01-29 10:33:22 -08001448
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001449 Log.v(TAG, "startPreview");
1450 mCameraDevice.startPreview();
Angus Kong20fad242013-11-11 18:23:46 -08001451
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001452 mFocusManager.onPreviewStarted();
1453 onPreviewStarted();
Michael Kolb8872c232013-01-29 10:33:22 -08001454
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001455 if (mSnapshotOnIdle) {
1456 mHandler.post(mDoSnapRunnable);
Michael Kolb8872c232013-01-29 10:33:22 -08001457 }
1458 }
1459
Michael Kolbd6954f32013-03-08 20:43:01 -08001460 @Override
1461 public void stopPreview() {
Michael Kolb8872c232013-01-29 10:33:22 -08001462 if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
1463 Log.v(TAG, "stopPreview");
1464 mCameraDevice.stopPreview();
1465 mFaceDetectionStarted = false;
1466 }
1467 setCameraState(PREVIEW_STOPPED);
1468 if (mFocusManager != null) mFocusManager.onPreviewStopped();
Sameer Padala2c8cc452013-11-05 18:49:12 -08001469 stopSmartCamera();
Michael Kolb8872c232013-01-29 10:33:22 -08001470 }
1471
1472 @SuppressWarnings("deprecation")
1473 private void updateCameraParametersInitialize() {
1474 // Reset preview frame rate to the maximum because it may be lowered by
1475 // video camera application.
ztenghui16a35202013-09-23 11:35:36 -07001476 int[] fpsRange = CameraUtil.getPhotoPreviewFpsRange(mParameters);
1477 if (fpsRange != null && fpsRange.length > 0) {
Doris Liu6432cd62013-06-13 17:20:31 -07001478 mParameters.setPreviewFpsRange(
1479 fpsRange[Parameters.PREVIEW_FPS_MIN_INDEX],
1480 fpsRange[Parameters.PREVIEW_FPS_MAX_INDEX]);
Michael Kolb8872c232013-01-29 10:33:22 -08001481 }
1482
Angus Kongb50b5cb2013-08-09 14:55:20 -07001483 mParameters.set(CameraUtil.RECORDING_HINT, CameraUtil.FALSE);
Michael Kolb8872c232013-01-29 10:33:22 -08001484
1485 // Disable video stabilization. Convenience methods not available in API
1486 // level <= 14
1487 String vstabSupported = mParameters.get("video-stabilization-supported");
1488 if ("true".equals(vstabSupported)) {
1489 mParameters.set("video-stabilization", "false");
1490 }
1491 }
1492
1493 private void updateCameraParametersZoom() {
1494 // Set zoom.
1495 if (mParameters.isZoomSupported()) {
1496 mParameters.setZoom(mZoomValue);
1497 }
1498 }
1499
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001500 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08001501 private void setAutoExposureLockIfSupported() {
1502 if (mAeLockSupported) {
1503 mParameters.setAutoExposureLock(mFocusManager.getAeAwbLock());
1504 }
1505 }
1506
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001507 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08001508 private void setAutoWhiteBalanceLockIfSupported() {
1509 if (mAwbLockSupported) {
1510 mParameters.setAutoWhiteBalanceLock(mFocusManager.getAeAwbLock());
1511 }
1512 }
1513
Michael Kolb8872c232013-01-29 10:33:22 -08001514 private void setFocusAreasIfSupported() {
1515 if (mFocusAreaSupported) {
1516 mParameters.setFocusAreas(mFocusManager.getFocusAreas());
1517 }
1518 }
1519
Michael Kolb8872c232013-01-29 10:33:22 -08001520 private void setMeteringAreasIfSupported() {
1521 if (mMeteringAreaSupported) {
Michael Kolb8872c232013-01-29 10:33:22 -08001522 mParameters.setMeteringAreas(mFocusManager.getMeteringAreas());
1523 }
1524 }
1525
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001526 private boolean updateCameraParametersPreference() {
Erin Dahlgren357b7672013-11-20 17:38:14 -08001527 SettingsManager settingsManager = mActivity.getSettingsManager();
1528
Michael Kolb8872c232013-01-29 10:33:22 -08001529 setAutoExposureLockIfSupported();
1530 setAutoWhiteBalanceLockIfSupported();
1531 setFocusAreasIfSupported();
1532 setMeteringAreasIfSupported();
1533
1534 // Set picture size.
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001535 String pictureSize = settingsManager.get(SettingsManager.SETTING_PICTURE_SIZE);
Michael Kolb8872c232013-01-29 10:33:22 -08001536 if (pictureSize == null) {
Erin Dahlgren357b7672013-11-20 17:38:14 -08001537 //TODO: deprecate CameraSettings.
1538 CameraSettings.initialCameraPictureSize(
1539 mActivity, mParameters, settingsManager);
Michael Kolb8872c232013-01-29 10:33:22 -08001540 } else {
1541 List<Size> supported = mParameters.getSupportedPictureSizes();
1542 CameraSettings.setCameraPictureSize(
1543 pictureSize, supported, mParameters);
1544 }
1545 Size size = mParameters.getPictureSize();
1546
1547 // Set a preview size that is closest to the viewfinder height and has
1548 // the right aspect ratio.
1549 List<Size> sizes = mParameters.getSupportedPreviewSizes();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001550 Size optimalSize = CameraUtil.getOptimalPreviewSize(mActivity, sizes,
Michael Kolb8872c232013-01-29 10:33:22 -08001551 (double) size.width / size.height);
1552 Size original = mParameters.getPreviewSize();
1553 if (!original.equals(optimalSize)) {
1554 mParameters.setPreviewSize(optimalSize.width, optimalSize.height);
Doris Liu6432cd62013-06-13 17:20:31 -07001555
Michael Kolb8872c232013-01-29 10:33:22 -08001556 // Zoom related settings will be changed for different preview
1557 // sizes, so set and read the parameters to get latest values
Michael Kolb4a4d4ef2013-05-30 08:35:26 -07001558 if (mHandler.getLooper() == Looper.myLooper()) {
1559 // On UI thread only, not when camera starts up
1560 setupPreview();
1561 } else {
1562 mCameraDevice.setParameters(mParameters);
1563 }
Michael Kolb8872c232013-01-29 10:33:22 -08001564 mParameters = mCameraDevice.getParameters();
1565 }
Doris Liu95405742013-11-05 15:25:26 -08001566
1567 if(optimalSize.width != 0 && optimalSize.height != 0) {
1568 mUI.updatePreviewAspectRatio((float) optimalSize.width
1569 / (float) optimalSize.height);
1570 }
Michael Kolb8872c232013-01-29 10:33:22 -08001571 Log.v(TAG, "Preview size is " + optimalSize.width + "x" + optimalSize.height);
1572
1573 // Since changing scene mode may change supported values, set scene mode
1574 // first. HDR is a scene mode. To promote it in UI, it is stored in a
1575 // separate preference.
Sascha Haeberling98f38bb2013-09-24 18:58:34 -07001576 String onValue = mActivity.getString(R.string.setting_on_value);
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001577 String hdr = settingsManager.get(SettingsManager.SETTING_CAMERA_HDR);
1578 String hdrPlus = settingsManager.get(SettingsManager.SETTING_CAMERA_HDR_PLUS);
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001579 boolean hdrOn = onValue.equals(hdr);
Sascha Haeberling9bf0fd62013-10-03 12:10:48 -07001580 boolean hdrPlusOn = onValue.equals(hdrPlus);
Sascha Haeberling98f38bb2013-09-24 18:58:34 -07001581
1582 boolean doGcamModeSwitch = false;
Sascha Haeberling9bf0fd62013-10-03 12:10:48 -07001583 if (hdrPlusOn && GcamHelper.hasGcamCapture()) {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001584 // Kick off mode switch to gcam.
1585 doGcamModeSwitch = true;
Michael Kolb8872c232013-01-29 10:33:22 -08001586 } else {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001587 if (hdrOn) {
1588 mSceneMode = CameraUtil.SCENE_MODE_HDR;
1589 } else {
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001590 mSceneMode = settingsManager.get(SettingsManager.SETTING_SCENE_MODE);
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001591 }
Michael Kolb8872c232013-01-29 10:33:22 -08001592 }
Angus Kongb50b5cb2013-08-09 14:55:20 -07001593 if (CameraUtil.isSupported(mSceneMode, mParameters.getSupportedSceneModes())) {
Michael Kolb8872c232013-01-29 10:33:22 -08001594 if (!mParameters.getSceneMode().equals(mSceneMode)) {
1595 mParameters.setSceneMode(mSceneMode);
1596
1597 // Setting scene mode will change the settings of flash mode,
1598 // white balance, and focus mode. Here we read back the
1599 // parameters, so we can know those settings.
1600 mCameraDevice.setParameters(mParameters);
1601 mParameters = mCameraDevice.getParameters();
1602 }
1603 } else {
1604 mSceneMode = mParameters.getSceneMode();
1605 if (mSceneMode == null) {
1606 mSceneMode = Parameters.SCENE_MODE_AUTO;
1607 }
1608 }
1609
1610 // Set JPEG quality.
1611 int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId,
1612 CameraProfile.QUALITY_HIGH);
1613 mParameters.setJpegQuality(jpegQuality);
1614
1615 // For the following settings, we need to check if the settings are
1616 // still supported by latest driver, if not, ignore the settings.
1617
1618 // Set exposure compensation
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001619 int value = Integer.parseInt(settingsManager.get(SettingsManager.SETTING_EXPOSURE));
Michael Kolb8872c232013-01-29 10:33:22 -08001620 int max = mParameters.getMaxExposureCompensation();
1621 int min = mParameters.getMinExposureCompensation();
1622 if (value >= min && value <= max) {
1623 mParameters.setExposureCompensation(value);
1624 } else {
1625 Log.w(TAG, "invalid exposure range: " + value);
1626 }
1627
1628 if (Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
1629 // Set flash mode.
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001630 String flashMode = settingsManager.get(SettingsManager.SETTING_FLASH_MODE);
Michael Kolb8872c232013-01-29 10:33:22 -08001631 List<String> supportedFlash = mParameters.getSupportedFlashModes();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001632 if (CameraUtil.isSupported(flashMode, supportedFlash)) {
Michael Kolb8872c232013-01-29 10:33:22 -08001633 mParameters.setFlashMode(flashMode);
1634 } else {
1635 flashMode = mParameters.getFlashMode();
1636 if (flashMode == null) {
1637 flashMode = mActivity.getString(
1638 R.string.pref_camera_flashmode_no_flash);
1639 }
1640 }
1641
1642 // Set white balance parameter.
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001643 String whiteBalance = settingsManager.get(SettingsManager.SETTING_WHITE_BALANCE);
Angus Kongb50b5cb2013-08-09 14:55:20 -07001644 if (CameraUtil.isSupported(whiteBalance,
Michael Kolb8872c232013-01-29 10:33:22 -08001645 mParameters.getSupportedWhiteBalance())) {
1646 mParameters.setWhiteBalance(whiteBalance);
1647 } else {
1648 whiteBalance = mParameters.getWhiteBalance();
1649 if (whiteBalance == null) {
1650 whiteBalance = Parameters.WHITE_BALANCE_AUTO;
1651 }
1652 }
1653
1654 // Set focus mode.
1655 mFocusManager.overrideFocusMode(null);
1656 mParameters.setFocusMode(mFocusManager.getFocusMode());
1657 } else {
1658 mFocusManager.overrideFocusMode(mParameters.getFocusMode());
1659 }
1660
Angus Kongdcccc512013-08-08 17:06:03 -07001661 if (mContinuousFocusSupported && ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK) {
Michael Kolb8872c232013-01-29 10:33:22 -08001662 updateAutoFocusMoveCallback();
1663 }
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001664
1665 return doGcamModeSwitch;
Michael Kolb8872c232013-01-29 10:33:22 -08001666 }
1667
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001668 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08001669 private void updateAutoFocusMoveCallback() {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001670 if (mParameters.getFocusMode().equals(CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE)) {
Angus Kong9ef99252013-07-18 18:04:19 -07001671 mCameraDevice.setAutoFocusMoveCallback(mHandler,
Angus Kong4f795b82013-09-16 14:25:35 -07001672 (CameraAFMoveCallback) mAutoFocusMoveCallback);
Michael Kolb8872c232013-01-29 10:33:22 -08001673 } else {
Angus Kong9ef99252013-07-18 18:04:19 -07001674 mCameraDevice.setAutoFocusMoveCallback(null, null);
Michael Kolb8872c232013-01-29 10:33:22 -08001675 }
1676 }
1677
1678 // We separate the parameters into several subsets, so we can update only
1679 // the subsets actually need updating. The PREFERENCE set needs extra
1680 // locking because the preference can be changed from GLThread as well.
1681 private void setCameraParameters(int updateSet) {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001682 boolean doModeSwitch = false;
1683
Michael Kolb8872c232013-01-29 10:33:22 -08001684 if ((updateSet & UPDATE_PARAM_INITIALIZE) != 0) {
1685 updateCameraParametersInitialize();
1686 }
1687
1688 if ((updateSet & UPDATE_PARAM_ZOOM) != 0) {
1689 updateCameraParametersZoom();
1690 }
1691
1692 if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001693 doModeSwitch = updateCameraParametersPreference();
Michael Kolb8872c232013-01-29 10:33:22 -08001694 }
1695
1696 mCameraDevice.setParameters(mParameters);
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001697
Sascha Haeberling9bf0fd62013-10-03 12:10:48 -07001698 // Switch to gcam module if HDR+ was selected
Angus Kong0fb819b2013-10-08 13:44:19 -07001699 if (doModeSwitch && !mIsImageCaptureIntent) {
Angus Kong13e87c42013-11-25 10:02:47 -08001700 mHandler.sendEmptyMessage(MSG_SWITCH_TO_GCAM_MODULE);
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001701 }
Michael Kolb8872c232013-01-29 10:33:22 -08001702 }
1703
1704 // If the Camera is idle, update the parameters immediately, otherwise
1705 // accumulate them in mUpdateSet and update later.
1706 private void setCameraParametersWhenIdle(int additionalUpdateSet) {
1707 mUpdateSet |= additionalUpdateSet;
1708 if (mCameraDevice == null) {
1709 // We will update all the parameters when we open the device, so
1710 // we don't need to do anything now.
1711 mUpdateSet = 0;
1712 return;
1713 } else if (isCameraIdle()) {
1714 setCameraParameters(mUpdateSet);
Michael Kolbd6954f32013-03-08 20:43:01 -08001715 updateSceneMode();
Michael Kolb8872c232013-01-29 10:33:22 -08001716 mUpdateSet = 0;
1717 } else {
Angus Kong13e87c42013-11-25 10:02:47 -08001718 if (!mHandler.hasMessages(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE)) {
1719 mHandler.sendEmptyMessageDelayed(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000);
Michael Kolb8872c232013-01-29 10:33:22 -08001720 }
1721 }
1722 }
1723
ztenghui7b265a62013-09-09 14:58:44 -07001724 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001725 public boolean isCameraIdle() {
Michael Kolb8872c232013-01-29 10:33:22 -08001726 return (mCameraState == IDLE) ||
1727 (mCameraState == PREVIEW_STOPPED) ||
1728 ((mFocusManager != null) && mFocusManager.isFocusCompleted()
1729 && (mCameraState != SWITCHING_CAMERA));
1730 }
1731
ztenghui7b265a62013-09-09 14:58:44 -07001732 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001733 public boolean isImageCaptureIntent() {
Michael Kolb8872c232013-01-29 10:33:22 -08001734 String action = mActivity.getIntent().getAction();
1735 return (MediaStore.ACTION_IMAGE_CAPTURE.equals(action)
Angus Kong6a8e8a12013-07-19 14:55:07 -07001736 || CameraActivity.ACTION_IMAGE_CAPTURE_SECURE.equals(action));
Michael Kolb8872c232013-01-29 10:33:22 -08001737 }
1738
1739 private void setupCaptureParams() {
1740 Bundle myExtras = mActivity.getIntent().getExtras();
1741 if (myExtras != null) {
1742 mSaveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT);
1743 mCropValue = myExtras.getString("crop");
1744 }
1745 }
1746
Michael Kolb8872c232013-01-29 10:33:22 -08001747 @Override
1748 public void onSharedPreferenceChanged() {
1749 // ignore the events after "onPause()"
1750 if (mPaused) return;
1751
Erin Dahlgren357b7672013-11-20 17:38:14 -08001752 SettingsController settingsController = mActivity.getSettingsController();
1753 settingsController.syncLocationManager();
Michael Kolb8872c232013-01-29 10:33:22 -08001754
1755 setCameraParametersWhenIdle(UPDATE_PARAM_PREFERENCE);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001756 mUI.updateOnScreenIndicators(mParameters);
Michael Kolb8872c232013-01-29 10:33:22 -08001757 }
1758
1759 @Override
1760 public void onCameraPickerClicked(int cameraId) {
1761 if (mPaused || mPendingSwitchCameraId != -1) return;
1762
1763 mPendingSwitchCameraId = cameraId;
Doris Liu6432cd62013-06-13 17:20:31 -07001764
1765 Log.v(TAG, "Start to switch camera. cameraId=" + cameraId);
1766 // We need to keep a preview frame for the animation before
1767 // releasing the camera. This will trigger onPreviewTextureCopied.
1768 //TODO: Need to animate the camera switch
1769 switchCamera();
Michael Kolb8872c232013-01-29 10:33:22 -08001770 }
1771
Michael Kolb8872c232013-01-29 10:33:22 -08001772 @Override
1773 public void onOverriddenPreferencesClicked() {
1774 if (mPaused) return;
Michael Kolbd6954f32013-03-08 20:43:01 -08001775 mUI.showPreferencesToast();
Michael Kolb8872c232013-01-29 10:33:22 -08001776 }
1777
1778 private void showTapToFocusToast() {
1779 // TODO: Use a toast?
1780 new RotateTextToast(mActivity, R.string.tap_to_focus, 0).show();
1781 // Clear the preference.
Erin Dahlgren357b7672013-11-20 17:38:14 -08001782 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001783 settingsManager.setBoolean(
1784 SettingsManager.SETTING_CAMERA_FIRST_USE_HINT_SHOWN, false);
Michael Kolb8872c232013-01-29 10:33:22 -08001785 }
1786
1787 private void initializeCapabilities() {
1788 mInitialParams = mCameraDevice.getParameters();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001789 mFocusAreaSupported = CameraUtil.isFocusAreaSupported(mInitialParams);
1790 mMeteringAreaSupported = CameraUtil.isMeteringAreaSupported(mInitialParams);
1791 mAeLockSupported = CameraUtil.isAutoExposureLockSupported(mInitialParams);
1792 mAwbLockSupported = CameraUtil.isAutoWhiteBalanceLockSupported(mInitialParams);
Angus Kongdcccc512013-08-08 17:06:03 -07001793 mContinuousFocusSupported = mInitialParams.getSupportedFocusModes().contains(
Angus Kongb50b5cb2013-08-09 14:55:20 -07001794 CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE);
Michael Kolb8872c232013-01-29 10:33:22 -08001795 }
1796
Michael Kolb8872c232013-01-29 10:33:22 -08001797 @Override
1798 public void onCountDownFinished() {
1799 mSnapshotOnIdle = false;
1800 mFocusManager.doSnap();
Doris Liuda50e052013-02-07 14:36:32 -08001801 mFocusManager.onShutterUp();
Michael Kolb8872c232013-01-29 10:33:22 -08001802 }
1803
Michael Kolb8872c232013-01-29 10:33:22 -08001804 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001805 public int onZoomChanged(int index) {
1806 // Not useful to change zoom value when the activity is paused.
1807 if (mPaused) return index;
1808 mZoomValue = index;
1809 if (mParameters == null || mCameraDevice == null) return index;
1810 // Set zoom parameters asynchronously
1811 mParameters.setZoom(mZoomValue);
Angus Kong36ed8672013-04-15 12:11:15 -07001812 mCameraDevice.setParameters(mParameters);
Michael Kolbd6954f32013-03-08 20:43:01 -08001813 Parameters p = mCameraDevice.getParameters();
1814 if (p != null) return p.getZoom();
1815 return index;
Angus Kongce5480e2013-01-29 17:43:48 -08001816 }
1817
1818 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001819 public int getCameraState() {
1820 return mCameraState;
1821 }
1822
1823 @Override
1824 public void onQueueStatus(boolean full) {
1825 mUI.enableShutter(!full);
Angus Kongce5480e2013-01-29 17:43:48 -08001826 }
Angus Kong86d36312013-01-31 18:22:44 -08001827
1828 @Override
Angus Kongfd4fc0e2013-11-07 15:38:09 -08001829 public void onMediaSaverAvailable(MediaSaver s) {
Angus Kong86d36312013-01-31 18:22:44 -08001830 // We set the listener only when both service and shutterbutton
1831 // are initialized.
Michael Kolbd6954f32013-03-08 20:43:01 -08001832 if (mFirstTimeInitialized) {
Angus Kongfd4fc0e2013-11-07 15:38:09 -08001833 s.setQueueListener(this);
Michael Kolbd6954f32013-03-08 20:43:01 -08001834 }
Angus Kong86d36312013-01-31 18:22:44 -08001835 }
Angus Kong0d00a892013-03-26 11:40:40 -07001836
1837 @Override
1838 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1839 }
1840
1841 @Override
1842 public void onSensorChanged(SensorEvent event) {
1843 int type = event.sensor.getType();
1844 float[] data;
1845 if (type == Sensor.TYPE_ACCELEROMETER) {
1846 data = mGData;
1847 } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
1848 data = mMData;
1849 } else {
1850 // we should not be here.
1851 return;
1852 }
1853 for (int i = 0; i < 3 ; i++) {
1854 data[i] = event.values[i];
1855 }
1856 float[] orientation = new float[3];
1857 SensorManager.getRotationMatrix(mR, null, mGData, mMData);
1858 SensorManager.getOrientation(mR, orientation);
1859 mHeading = (int) (orientation[0] * 180f / Math.PI) % 360;
1860 if (mHeading < 0) {
1861 mHeading += 360;
1862 }
Angus Kong0d00a892013-03-26 11:40:40 -07001863 }
Doris Liu6432cd62013-06-13 17:20:31 -07001864
1865 @Override
ztenghui7b265a62013-09-09 14:58:44 -07001866 public void onPreviewFocusChanged(boolean previewFocused) {
1867 mUI.onPreviewFocusChanged(previewFocused);
Doris Liu6432cd62013-06-13 17:20:31 -07001868 }
1869
Erin Dahlgren3044d8c2013-10-10 18:23:45 -07001870 @Override
1871 public boolean arePreviewControlsVisible() {
1872 return mUI.arePreviewControlsVisible();
1873 }
1874
Ruben Brunkd217ed02013-10-08 23:31:13 -07001875 // For debugging only.
1876 public void setDebugUri(Uri uri) {
1877 mDebugUri = uri;
1878 }
1879
1880 // For debugging only.
1881 private void saveToDebugUri(byte[] data) {
1882 if (mDebugUri != null) {
1883 OutputStream outputStream = null;
1884 try {
1885 outputStream = mContentResolver.openOutputStream(mDebugUri);
1886 outputStream.write(data);
1887 outputStream.close();
1888 } catch (IOException e) {
1889 Log.e(TAG, "Exception while writing debug jpeg file", e);
1890 } finally {
1891 CameraUtil.closeSilently(outputStream);
1892 }
1893 }
1894 }
1895
Doris Liu6432cd62013-06-13 17:20:31 -07001896/* Below is no longer needed, except to get rid of compile error
1897 * TODO: Remove these
1898 */
1899
1900 // TODO: Delete this function after old camera code is removed
1901 @Override
1902 public void onRestorePreferencesClicked() {}
1903
Michael Kolb8872c232013-01-29 10:33:22 -08001904}