blob: 8537f05ff960970c7f6437ae847416e4783a5775 [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
Angus Kong20fad242013-11-11 18:23:46 -080051import com.android.camera.app.CameraManager.CameraAFCallback;
52import com.android.camera.app.CameraManager.CameraAFMoveCallback;
53import com.android.camera.app.CameraManager.CameraPictureCallback;
54import com.android.camera.app.CameraManager.CameraProxy;
55import com.android.camera.app.CameraManager.CameraShutterCallback;
Ruben Brunka9d66bd2013-09-06 11:56:32 -070056import com.android.camera.PhotoModule.NamedImages.NamedEntity;
Angus Kong20fad242013-11-11 18:23:46 -080057import com.android.camera.app.AppController;
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;
64import com.android.camera.settings.SettingsManager.DefaultCameraIdSetting;
65import com.android.camera.settings.SettingsManager.ExposureSetting;
66import com.android.camera.settings.SettingsManager.FlashSetting;
Erin Dahlgren357b7672013-11-20 17:38:14 -080067import com.android.camera.settings.SettingsManager.HintSetting;
68import com.android.camera.settings.SettingsManager.HdrSetting;
69import com.android.camera.settings.SettingsManager.HdrPlusSetting;
70import com.android.camera.settings.SettingsManager.LocationSetting;
71import com.android.camera.settings.SettingsManager.PictureSizeSetting;
Erin Dahlgren357b7672013-11-20 17:38:14 -080072import com.android.camera.settings.SettingsManager.TimerSetting;
73import com.android.camera.settings.SettingsManager.TimerSoundSetting;
74import com.android.camera.settings.SettingsManager.SceneModeSetting;
75import com.android.camera.settings.SettingsManager.WhiteBalanceSetting;
Michael Kolbd6954f32013-03-08 20:43:01 -080076import com.android.camera.ui.CountDownView.OnCountDownFinishedListener;
Doris Liu1c94b7d2013-11-09 19:13:44 -080077import com.android.camera.ui.ModeListView;
Michael Kolb8872c232013-01-29 10:33:22 -080078import com.android.camera.ui.RotateTextToast;
Angus Kongdcccc512013-08-08 17:06:03 -070079import com.android.camera.util.ApiHelper;
Angus Kongb50b5cb2013-08-09 14:55:20 -070080import com.android.camera.util.CameraUtil;
Ruben Brunk4601f5d2013-09-24 18:35:22 -070081import com.android.camera.util.GcamHelper;
Sascha Haeberling8e963a52013-08-06 11:43:02 -070082import com.android.camera.util.UsageStatistics;
83import com.android.camera2.R;
Michael Kolb8872c232013-01-29 10:33:22 -080084
Angus Kongdcccc512013-08-08 17:06:03 -070085import java.io.File;
86import java.io.FileNotFoundException;
87import java.io.FileOutputStream;
88import java.io.IOException;
89import java.io.OutputStream;
Angus Kongdcccc512013-08-08 17:06:03 -070090import java.util.List;
Ruben Brunka9d66bd2013-09-06 11:56:32 -070091import java.util.Vector;
Angus Kongdcccc512013-08-08 17:06:03 -070092
Michael Kolb8872c232013-01-29 10:33:22 -080093public class PhotoModule
Sascha Haeberling280fd3e2013-11-21 13:52:15 -080094 extends CameraModule
95 implements PhotoController,
96 ModuleController,
97 FocusOverlayManager.Listener,
98 CameraPreference.OnPreferenceChangedListener,
99 ShutterButton.OnShutterButtonListener, MediaSaver.QueueListener,
100 OnCountDownFinishedListener,
101 SensorEventListener {
Michael Kolb8872c232013-01-29 10:33:22 -0800102
103 private static final String TAG = "CAM_PhotoModule";
104
105 // We number the request code from 1000 to avoid collision with Gallery.
106 private static final int REQUEST_CROP = 1000;
107
Angus Kong13e87c42013-11-25 10:02:47 -0800108 // Messages defined for the UI thread handler.
109 private static final int MSG_SETUP_PREVIEW = 1;
110 private static final int MSG_FIRST_TIME_INIT = 2;
111 private static final int MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE = 3;
112 private static final int MSG_SHOW_TAP_TO_FOCUS_TOAST = 4;
113 private static final int MSG_SWITCH_CAMERA = 5;
114 private static final int MSG_SWITCH_CAMERA_START_ANIMATION = 6;
115 private static final int MSG_CAMERA_OPEN_DONE = 7;
116 private static final int MSG_OPEN_CAMERA_FAIL = 8;
117 private static final int MSG_CAMERA_DISABLED = 9;
118 private static final int MSG_SWITCH_TO_GCAM_MODULE = 10;
Michael Kolb8872c232013-01-29 10:33:22 -0800119
120 // The subset of parameters we need to update in setCameraParameters().
121 private static final int UPDATE_PARAM_INITIALIZE = 1;
122 private static final int UPDATE_PARAM_ZOOM = 2;
123 private static final int UPDATE_PARAM_PREFERENCE = 4;
124 private static final int UPDATE_PARAM_ALL = -1;
125
Andy Huibersdef975d2013-11-22 09:13:39 -0800126 // This is the delay before we execute onResume tasks when coming
127 // from the lock screen, to allow time for onPause to execute.
128 private static final int ON_RESUME_TASKS_DELAY_MSEC = 20;
Michael Kolb8872c232013-01-29 10:33:22 -0800129
Ruben Brunkd7488272013-10-10 18:45:53 -0700130 private static final String DEBUG_IMAGE_PREFIX = "DEBUG_";
131
Michael Kolb8872c232013-01-29 10:33:22 -0800132 // copied from Camera hierarchy
133 private CameraActivity mActivity;
Michael Kolb8872c232013-01-29 10:33:22 -0800134 private CameraProxy mCameraDevice;
135 private int mCameraId;
136 private Parameters mParameters;
137 private boolean mPaused;
Michael Kolbd6954f32013-03-08 20:43:01 -0800138
139 private PhotoUI mUI;
Michael Kolb8872c232013-01-29 10:33:22 -0800140
Michael Kolb8872c232013-01-29 10:33:22 -0800141 // The activity is going to switch to the specified camera id. This is
142 // needed because texture copy is done in GL thread. -1 means camera is not
143 // switching.
144 protected int mPendingSwitchCameraId = -1;
145 private boolean mOpenCameraFail;
146 private boolean mCameraDisabled;
147
148 // When setCameraParametersWhenIdle() is called, we accumulate the subsets
149 // needed to be updated in mUpdateSet.
150 private int mUpdateSet;
151
152 private static final int SCREEN_DELAY = 2 * 60 * 1000;
153
154 private int mZoomValue; // The current zoom value.
Michael Kolb8872c232013-01-29 10:33:22 -0800155
156 private Parameters mInitialParams;
157 private boolean mFocusAreaSupported;
158 private boolean mMeteringAreaSupported;
159 private boolean mAeLockSupported;
160 private boolean mAwbLockSupported;
Angus Kongdcccc512013-08-08 17:06:03 -0700161 private boolean mContinuousFocusSupported;
Michael Kolb8872c232013-01-29 10:33:22 -0800162
163 // The degrees of the device rotated clockwise from its natural orientation.
164 private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
Michael Kolb8872c232013-01-29 10:33:22 -0800165
166 private static final String sTempCropFilename = "crop-temp";
167
Michael Kolb8872c232013-01-29 10:33:22 -0800168 private boolean mFaceDetectionStarted = false;
169
Michael Kolb8872c232013-01-29 10:33:22 -0800170 // mCropValue and mSaveUri are used only if isImageCaptureIntent() is true.
171 private String mCropValue;
172 private Uri mSaveUri;
173
Ruben Brunkd217ed02013-10-08 23:31:13 -0700174 private Uri mDebugUri;
175
Angus Kongce5480e2013-01-29 17:43:48 -0800176 // We use a queue to generated names of the images to be used later
177 // when the image is ready to be saved.
Michael Kolb8872c232013-01-29 10:33:22 -0800178 private NamedImages mNamedImages;
179
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800180 private final Runnable mDoSnapRunnable = new Runnable() {
Michael Kolb8872c232013-01-29 10:33:22 -0800181 @Override
182 public void run() {
183 onShutterButtonClick();
184 }
185 };
186
Michael Kolb8872c232013-01-29 10:33:22 -0800187 /**
188 * An unpublished intent flag requesting to return as soon as capturing
189 * is completed.
190 *
191 * TODO: consider publishing by moving into MediaStore.
192 */
193 private static final String EXTRA_QUICK_CAPTURE =
194 "android.intent.extra.quickCapture";
195
196 // The display rotation in degrees. This is only valid when mCameraState is
197 // not PREVIEW_STOPPED.
198 private int mDisplayRotation;
199 // The value for android.hardware.Camera.setDisplayOrientation.
200 private int mCameraDisplayOrientation;
201 // The value for UI components like indicators.
202 private int mDisplayOrientation;
203 // The value for android.hardware.Camera.Parameters.setRotation.
204 private int mJpegRotation;
Doris Liu29da2db2013-08-28 14:28:45 -0700205 // Indicates whether we are using front camera
206 private boolean mMirror;
Michael Kolb8872c232013-01-29 10:33:22 -0800207 private boolean mFirstTimeInitialized;
208 private boolean mIsImageCaptureIntent;
209
Michael Kolb8872c232013-01-29 10:33:22 -0800210 private int mCameraState = PREVIEW_STOPPED;
211 private boolean mSnapshotOnIdle = false;
212
213 private ContentResolver mContentResolver;
214
215 private LocationManager mLocationManager;
216
Michael Kolb8872c232013-01-29 10:33:22 -0800217 private final PostViewPictureCallback mPostViewPictureCallback =
218 new PostViewPictureCallback();
219 private final RawPictureCallback mRawPictureCallback =
220 new RawPictureCallback();
221 private final AutoFocusCallback mAutoFocusCallback =
222 new AutoFocusCallback();
223 private final Object mAutoFocusMoveCallback =
224 ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700225 ? new AutoFocusMoveCallback()
226 : null;
Michael Kolb8872c232013-01-29 10:33:22 -0800227
228 private final CameraErrorCallback mErrorCallback = new CameraErrorCallback();
229
230 private long mFocusStartTime;
231 private long mShutterCallbackTime;
232 private long mPostViewPictureCallbackTime;
233 private long mRawPictureCallbackTime;
234 private long mJpegPictureCallbackTime;
235 private long mOnResumeTime;
236 private byte[] mJpegImageData;
237
238 // These latency time are for the CameraLatency test.
239 public long mAutoFocusTime;
240 public long mShutterLag;
241 public long mShutterToPictureDisplayedTime;
242 public long mPictureDisplayedToJpegCallbackTime;
243 public long mJpegCallbackFinishTime;
244 public long mCaptureStartTime;
245
246 // This handles everything about focus.
247 private FocusOverlayManager mFocusManager;
248
Michael Kolb8872c232013-01-29 10:33:22 -0800249 private String mSceneMode;
Michael Kolb8872c232013-01-29 10:33:22 -0800250
251 private final Handler mHandler = new MainHandler();
Erin Dahlgrenb09b53e2013-11-06 11:57:51 -0800252
Michael Kolb8872c232013-01-29 10:33:22 -0800253 private PreferenceGroup mPreferenceGroup;
254
255 private boolean mQuickCapture;
Angus Kong0d00a892013-03-26 11:40:40 -0700256 private SensorManager mSensorManager;
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800257 private final float[] mGData = new float[3];
258 private final float[] mMData = new float[3];
259 private final float[] mR = new float[16];
Angus Kong0d00a892013-03-26 11:40:40 -0700260 private int mHeading = -1;
261
Angus Kongdcccc512013-08-08 17:06:03 -0700262 // True if all the parameters needed to start preview is ready.
263 private boolean mCameraPreviewParamsReady = false;
Doris Liu6432cd62013-06-13 17:20:31 -0700264
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800265 private final MediaSaver.OnMediaSavedListener mOnMediaSavedListener =
Angus Kongfd4fc0e2013-11-07 15:38:09 -0800266 new MediaSaver.OnMediaSavedListener() {
Angus Kongce5480e2013-01-29 17:43:48 -0800267 @Override
268 public void onMediaSaved(Uri uri) {
269 if (uri != null) {
Doris Liu6432cd62013-06-13 17:20:31 -0700270 mActivity.notifyNewMedia(uri);
Angus Kongce5480e2013-01-29 17:43:48 -0800271 }
272 }
273 };
Michael Kolb8872c232013-01-29 10:33:22 -0800274
Angus Kongdcccc512013-08-08 17:06:03 -0700275 private void checkDisplayRotation() {
276 // Set the display orientation if display rotation has changed.
277 // Sometimes this happens when the device is held upside
278 // down and camera app is opened. Rotation animation will
279 // take some time and the rotation value we have got may be
280 // wrong. Framework does not have a callback for this now.
281 if (CameraUtil.getDisplayRotation(mActivity) != mDisplayRotation) {
282 setDisplayOrientation();
Michael Kolb8872c232013-01-29 10:33:22 -0800283 }
Angus Kongdcccc512013-08-08 17:06:03 -0700284 if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) {
285 mHandler.postDelayed(new Runnable() {
286 @Override
287 public void run() {
288 checkDisplayRotation();
289 }
290 }, 100);
Michael Kolb8872c232013-01-29 10:33:22 -0800291 }
292 }
293
294 /**
295 * This Handler is used to post message back onto the main thread of the
296 * application
297 */
298 private class MainHandler extends Handler {
Erin Dahlgrenb09b53e2013-11-06 11:57:51 -0800299 public MainHandler() {
300 super(Looper.getMainLooper());
301 }
302
Michael Kolb8872c232013-01-29 10:33:22 -0800303 @Override
304 public void handleMessage(Message msg) {
305 switch (msg.what) {
Angus Kong13e87c42013-11-25 10:02:47 -0800306 case MSG_SETUP_PREVIEW: {
Michael Kolb8872c232013-01-29 10:33:22 -0800307 setupPreview();
308 break;
309 }
310
Angus Kong13e87c42013-11-25 10:02:47 -0800311 case MSG_FIRST_TIME_INIT: {
Michael Kolb8872c232013-01-29 10:33:22 -0800312 initializeFirstTime();
313 break;
314 }
315
Angus Kong13e87c42013-11-25 10:02:47 -0800316 case MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE: {
Michael Kolb8872c232013-01-29 10:33:22 -0800317 setCameraParametersWhenIdle(0);
318 break;
319 }
320
Angus Kong13e87c42013-11-25 10:02:47 -0800321 case MSG_SHOW_TAP_TO_FOCUS_TOAST: {
Michael Kolb8872c232013-01-29 10:33:22 -0800322 showTapToFocusToast();
323 break;
324 }
325
Angus Kong13e87c42013-11-25 10:02:47 -0800326 case MSG_SWITCH_CAMERA: {
Michael Kolb8872c232013-01-29 10:33:22 -0800327 switchCamera();
328 break;
329 }
330
Angus Kong13e87c42013-11-25 10:02:47 -0800331 case MSG_SWITCH_CAMERA_START_ANIMATION: {
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700332 // TODO: Need to revisit
333 // ((CameraScreenNail) mActivity.mCameraScreenNail).animateSwitchCamera();
Michael Kolb8872c232013-01-29 10:33:22 -0800334 break;
335 }
336
Angus Kong13e87c42013-11-25 10:02:47 -0800337 case MSG_CAMERA_OPEN_DONE: {
Michael Kolbd6954f32013-03-08 20:43:01 -0800338 onCameraOpened();
Michael Kolb8872c232013-01-29 10:33:22 -0800339 break;
340 }
341
Angus Kong13e87c42013-11-25 10:02:47 -0800342 case MSG_OPEN_CAMERA_FAIL: {
Michael Kolb8872c232013-01-29 10:33:22 -0800343 mOpenCameraFail = true;
Angus Kongb50b5cb2013-08-09 14:55:20 -0700344 CameraUtil.showErrorAndFinish(mActivity,
Michael Kolb8872c232013-01-29 10:33:22 -0800345 R.string.cannot_connect_camera);
346 break;
347 }
348
Angus Kong13e87c42013-11-25 10:02:47 -0800349 case MSG_CAMERA_DISABLED: {
Michael Kolb8872c232013-01-29 10:33:22 -0800350 mCameraDisabled = true;
Angus Kongb50b5cb2013-08-09 14:55:20 -0700351 CameraUtil.showErrorAndFinish(mActivity,
Michael Kolb8872c232013-01-29 10:33:22 -0800352 R.string.camera_disabled);
353 break;
354 }
ztenghui367c7c82013-10-16 14:43:26 -0700355
Angus Kong13e87c42013-11-25 10:02:47 -0800356 case MSG_SWITCH_TO_GCAM_MODULE: {
Erin Dahlgrend7b8cb52013-11-14 17:25:37 -0800357 mActivity.onModeSelected(ModeListView.MODE_GCAM);
ztenghui367c7c82013-10-16 14:43:26 -0700358 }
Michael Kolb8872c232013-01-29 10:33:22 -0800359 }
360 }
361 }
362
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800363 /**
364 * Constructs a new photo module.
365 */
Angus Kongc4e66562013-11-22 23:03:21 -0800366 public PhotoModule(AppController app) {
367 super(app);
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800368 }
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700369
Angus Kong13e87c42013-11-25 10:02:47 -0800370
Michael Kolb8872c232013-01-29 10:33:22 -0800371 @Override
Angus Kong13e87c42013-11-25 10:02:47 -0800372 public void init(AppController app, boolean isSecureCamera, boolean isCaptureIntent) {
373 mActivity = (CameraActivity) app.getAndroidContext();
374 mUI = new PhotoUI(mActivity, this, app.getModuleLayoutRoot());
Erin Dahlgren357b7672013-11-20 17:38:14 -0800375
376 SettingsManager settingsManager = mActivity.getSettingsManager();
377 mCameraId = Integer.parseInt(settingsManager.get(
378 new DefaultCameraIdSetting()));
Michael Kolb8872c232013-01-29 10:33:22 -0800379
380 mContentResolver = mActivity.getContentResolver();
381
Michael Kolb8872c232013-01-29 10:33:22 -0800382 // Surface texture is from camera screen nail and startPreview needs it.
383 // This must be done before startPreview.
384 mIsImageCaptureIntent = isImageCaptureIntent();
Michael Kolb8872c232013-01-29 10:33:22 -0800385
Angus Kong20fad242013-11-11 18:23:46 -0800386 mActivity.getCameraProvider().requestCamera(mCameraId);
387
Michael Kolb8872c232013-01-29 10:33:22 -0800388 initializeControlByIntent();
389 mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
Erin Dahlgren21c21a62013-11-19 16:37:38 -0800390 mLocationManager = mActivity.getLocationManager();
Angus Kong0d00a892013-03-26 11:40:40 -0700391 mSensorManager = (SensorManager)(mActivity.getSystemService(Context.SENSOR_SERVICE));
Michael Kolbd6954f32013-03-08 20:43:01 -0800392 }
393
394 private void initializeControlByIntent() {
395 mUI.initializeControlByIntent();
396 if (mIsImageCaptureIntent) {
397 setupCaptureParams();
398 }
399 }
400
401 private void onPreviewStarted() {
Michael Kolbd6954f32013-03-08 20:43:01 -0800402 setCameraState(IDLE);
Michael Kolbd6954f32013-03-08 20:43:01 -0800403 startFaceDetection();
404 locationFirstRun();
Michael Kolb8872c232013-01-29 10:33:22 -0800405 }
406
407 // Prompt the user to pick to record location for the very first run of
408 // camera only
409 private void locationFirstRun() {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800410 SettingsManager settingsManager = mActivity.getSettingsManager();
411 if (settingsManager.isSet(new LocationSetting())) {
Michael Kolb8872c232013-01-29 10:33:22 -0800412 return;
413 }
414 if (mActivity.isSecureCamera()) return;
415 // Check if the back camera exists
416 int backCameraId = CameraHolder.instance().getBackCameraId();
417 if (backCameraId == -1) {
418 // If there is no back camera, do not show the prompt.
419 return;
420 }
Doris Liu6a83d522013-07-02 12:03:32 -0700421 mUI.showLocationDialog();
422 }
Michael Kolb8872c232013-01-29 10:33:22 -0800423
ztenghui7b265a62013-09-09 14:58:44 -0700424 @Override
Angus Kongdcccc512013-08-08 17:06:03 -0700425 public void onPreviewUIReady() {
Erin Dahlgrendc282e12013-11-12 09:39:08 -0800426 startPreview();
Angus Kongdcccc512013-08-08 17:06:03 -0700427 }
428
429 @Override
430 public void onPreviewUIDestroyed() {
431 if (mCameraDevice == null) {
432 return;
433 }
434 mCameraDevice.setPreviewTexture(null);
435 stopPreview();
436 }
437
Michael Kolbd6954f32013-03-08 20:43:01 -0800438 private void onCameraOpened() {
439 View root = mUI.getRootView();
Michael Kolb8872c232013-01-29 10:33:22 -0800440 // These depend on camera parameters.
Michael Kolbd6954f32013-03-08 20:43:01 -0800441
442 int width = root.getWidth();
443 int height = root.getHeight();
Doris Liu6a0de792013-02-26 10:54:25 -0800444 mFocusManager.setPreviewSize(width, height);
Michael Kolbd6954f32013-03-08 20:43:01 -0800445 openCameraCommon();
Michael Kolb8872c232013-01-29 10:33:22 -0800446 }
447
Michael Kolbd6954f32013-03-08 20:43:01 -0800448 private void switchCamera() {
449 if (mPaused) return;
Erin Dahlgren357b7672013-11-20 17:38:14 -0800450 SettingsManager settingsManager = mActivity.getSettingsManager();
Michael Kolbd6954f32013-03-08 20:43:01 -0800451
452 Log.v(TAG, "Start to switch camera. id=" + mPendingSwitchCameraId);
453 mCameraId = mPendingSwitchCameraId;
454 mPendingSwitchCameraId = -1;
Erin Dahlgren357b7672013-11-20 17:38:14 -0800455 settingsManager.set(new DefaultCameraIdSetting(),
456 "" + mCameraId);
Angus Kong20fad242013-11-11 18:23:46 -0800457 mActivity.getCameraProvider().requestCamera(mCameraId);
Michael Kolbd6954f32013-03-08 20:43:01 -0800458
Michael Kolbd6954f32013-03-08 20:43:01 -0800459 mUI.collapseCameraControls();
460 mUI.clearFaces();
461 if (mFocusManager != null) mFocusManager.removeMessages();
462
Erin Dahlgren357b7672013-11-20 17:38:14 -0800463 // TODO: this needs to be brought into onCameraAvailable();
Angus Kong20fad242013-11-11 18:23:46 -0800464 CameraInfo info = mActivity.getCameraProvider().getCameraInfo()[mCameraId];
Doris Liu29da2db2013-08-28 14:28:45 -0700465 mMirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
466 mFocusManager.setMirror(mMirror);
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700467 // Start switch camera animation. Post a message because
468 // onFrameAvailable from the old camera may already exist.
Angus Kong13e87c42013-11-25 10:02:47 -0800469 mHandler.sendEmptyMessage(MSG_SWITCH_CAMERA_START_ANIMATION);
Doris Liu48239f42013-03-04 22:19:10 -0800470 }
471
Michael Kolbd6954f32013-03-08 20:43:01 -0800472 // either open a new camera or switch cameras
473 private void openCameraCommon() {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800474 mPreferenceGroup = loadPreferenceGroup();
Michael Kolbd6954f32013-03-08 20:43:01 -0800475
Erin Dahlgren357b7672013-11-20 17:38:14 -0800476 // TODO: remove dependency on PreferenceGroup
477 mUI.onCameraOpened(mPreferenceGroup, mParameters, this);
Angus Kong0fb819b2013-10-08 13:44:19 -0700478 if (mIsImageCaptureIntent) {
479 mUI.overrideSettings(CameraSettings.KEY_CAMERA_HDR_PLUS,
480 mActivity.getString(R.string.setting_off_value));
481 }
Michael Kolbd6954f32013-03-08 20:43:01 -0800482 updateSceneMode();
483 showTapToFocusToastIfNeeded();
Michael Kolb8872c232013-01-29 10:33:22 -0800484 }
485
ztenghui7b265a62013-09-09 14:58:44 -0700486 @Override
Doris Liu36ebcb12013-10-28 14:44:24 -0700487 public void onPreviewRectChanged(Rect previewRect) {
488 if (mFocusManager != null) mFocusManager.setPreviewRect(previewRect);
Michael Kolbd6954f32013-03-08 20:43:01 -0800489 }
Michael Kolb8872c232013-01-29 10:33:22 -0800490
491 private void resetExposureCompensation() {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800492 SettingsManager settingsManager = mActivity.getSettingsManager();
493 if (settingsManager == null) {
494 Log.e(TAG, "Settings manager is null!");
495 return;
Michael Kolb8872c232013-01-29 10:33:22 -0800496 }
Erin Dahlgren357b7672013-11-20 17:38:14 -0800497 settingsManager.setDefault(new ExposureSetting());
Michael Kolb8872c232013-01-29 10:33:22 -0800498 }
499
Michael Kolb8872c232013-01-29 10:33:22 -0800500 // Snapshots can only be taken after this is called. It should be called
501 // once only. We could have done these things in onCreate() but we want to
502 // make preview screen appear as soon as possible.
503 private void initializeFirstTime() {
Sascha Haeberling330dafb2013-10-10 19:00:41 -0700504 if (mFirstTimeInitialized || mPaused) {
505 return;
506 }
Michael Kolb8872c232013-01-29 10:33:22 -0800507
508 // Initialize location service.
Erin Dahlgren357b7672013-11-20 17:38:14 -0800509 SettingsController settingsController = mActivity.getSettingsController();
510 settingsController.syncLocationManager();
Michael Kolb8872c232013-01-29 10:33:22 -0800511
Michael Kolbd6954f32013-03-08 20:43:01 -0800512 mUI.initializeFirstTime();
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800513 MediaSaver s = getServices().getMediaSaver();
Angus Kong86d36312013-01-31 18:22:44 -0800514 // We set the listener only when both service and shutterbutton
515 // are initialized.
516 if (s != null) {
Angus Kongfd4fc0e2013-11-07 15:38:09 -0800517 s.setQueueListener(this);
Angus Kong86d36312013-01-31 18:22:44 -0800518 }
Michael Kolb8872c232013-01-29 10:33:22 -0800519
Michael Kolb8872c232013-01-29 10:33:22 -0800520 mNamedImages = new NamedImages();
521
522 mFirstTimeInitialized = true;
523 addIdleHandler();
524
525 mActivity.updateStorageSpaceAndHint();
526 }
527
Michael Kolbd6954f32013-03-08 20:43:01 -0800528 // If the activity is paused and resumed, this method will be called in
529 // onResume.
530 private void initializeSecondTime() {
531 // Start location update if needed.
Erin Dahlgren357b7672013-11-20 17:38:14 -0800532 SettingsController settingsController = mActivity.getSettingsController();
533 settingsController.syncLocationManager();
534
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800535 MediaSaver s = getServices().getMediaSaver();
Michael Kolbd6954f32013-03-08 20:43:01 -0800536 if (s != null) {
Angus Kongfd4fc0e2013-11-07 15:38:09 -0800537 s.setQueueListener(this);
Michael Kolbd6954f32013-03-08 20:43:01 -0800538 }
539 mNamedImages = new NamedImages();
540 mUI.initializeSecondTime(mParameters);
Michael Kolbd6954f32013-03-08 20:43:01 -0800541 }
542
Michael Kolb8872c232013-01-29 10:33:22 -0800543 private void showTapToFocusToastIfNeeded() {
544 // Show the tap to focus toast if this is the first start.
Erin Dahlgren357b7672013-11-20 17:38:14 -0800545 SettingsManager settingsManager = mActivity.getSettingsManager();
546 boolean showHint = settingsManager.getBoolean(new HintSetting());
547 // CONVERT THIS SETTING TO A STRING
548 if (mFocusAreaSupported && showHint) {
Michael Kolb8872c232013-01-29 10:33:22 -0800549 // Delay the toast for one second to wait for orientation.
Angus Kong13e87c42013-11-25 10:02:47 -0800550 mHandler.sendEmptyMessageDelayed(MSG_SHOW_TAP_TO_FOCUS_TOAST, 1000);
Michael Kolb8872c232013-01-29 10:33:22 -0800551 }
552 }
553
554 private void addIdleHandler() {
555 MessageQueue queue = Looper.myQueue();
556 queue.addIdleHandler(new MessageQueue.IdleHandler() {
557 @Override
558 public boolean queueIdle() {
559 Storage.ensureOSXCompatible();
560 return false;
561 }
562 });
563 }
564
Michael Kolb8872c232013-01-29 10:33:22 -0800565 @Override
566 public void startFaceDetection() {
Michael Kolb8872c232013-01-29 10:33:22 -0800567 if (mFaceDetectionStarted) return;
568 if (mParameters.getMaxNumDetectedFaces() > 0) {
569 mFaceDetectionStarted = true;
Michael Kolb8872c232013-01-29 10:33:22 -0800570 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
Michael Kolbd6954f32013-03-08 20:43:01 -0800571 mUI.onStartFaceDetection(mDisplayOrientation,
572 (info.facing == CameraInfo.CAMERA_FACING_FRONT));
Angus Kong9e765522013-07-31 14:05:20 -0700573 mCameraDevice.setFaceDetectionCallback(mHandler, mUI);
Michael Kolb8872c232013-01-29 10:33:22 -0800574 mCameraDevice.startFaceDetection();
575 }
576 }
577
Michael Kolb8872c232013-01-29 10:33:22 -0800578 @Override
579 public void stopFaceDetection() {
Michael Kolb8872c232013-01-29 10:33:22 -0800580 if (!mFaceDetectionStarted) return;
581 if (mParameters.getMaxNumDetectedFaces() > 0) {
582 mFaceDetectionStarted = false;
Angus Kong9e765522013-07-31 14:05:20 -0700583 mCameraDevice.setFaceDetectionCallback(null, null);
Michael Kolb8872c232013-01-29 10:33:22 -0800584 mCameraDevice.stopFaceDetection();
Michael Kolbd6954f32013-03-08 20:43:01 -0800585 mUI.clearFaces();
Michael Kolb8872c232013-01-29 10:33:22 -0800586 }
587 }
588
Michael Kolb8872c232013-01-29 10:33:22 -0800589 private final class ShutterCallback
Angus Kong9ef99252013-07-18 18:04:19 -0700590 implements CameraShutterCallback {
Angus Kongdcb0ef12013-03-25 23:11:43 -0700591
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800592 private final boolean mNeedsAnimation;
Angus Kongdcb0ef12013-03-25 23:11:43 -0700593
Sascha Haeberling37f36112013-08-06 14:31:52 -0700594 public ShutterCallback(boolean needsAnimation) {
595 mNeedsAnimation = needsAnimation;
Angus Kongdcb0ef12013-03-25 23:11:43 -0700596 }
597
Michael Kolb8872c232013-01-29 10:33:22 -0800598 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700599 public void onShutter(CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800600 mShutterCallbackTime = System.currentTimeMillis();
601 mShutterLag = mShutterCallbackTime - mCaptureStartTime;
602 Log.v(TAG, "mShutterLag = " + mShutterLag + "ms");
Sascha Haeberling37f36112013-08-06 14:31:52 -0700603 if (mNeedsAnimation) {
604 mActivity.runOnUiThread(new Runnable() {
605 @Override
606 public void run() {
607 animateAfterShutter();
608 }
609 });
Angus Kongdcb0ef12013-03-25 23:11:43 -0700610 }
Michael Kolb8872c232013-01-29 10:33:22 -0800611 }
612 }
613
Angus Kong9ef99252013-07-18 18:04:19 -0700614 private final class PostViewPictureCallback
615 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800616 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700617 public void onPictureTaken(byte [] data, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800618 mPostViewPictureCallbackTime = System.currentTimeMillis();
619 Log.v(TAG, "mShutterToPostViewCallbackTime = "
620 + (mPostViewPictureCallbackTime - mShutterCallbackTime)
621 + "ms");
622 }
623 }
624
Angus Kong9ef99252013-07-18 18:04:19 -0700625 private final class RawPictureCallback
626 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800627 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700628 public void onPictureTaken(byte [] rawData, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800629 mRawPictureCallbackTime = System.currentTimeMillis();
630 Log.v(TAG, "mShutterToRawCallbackTime = "
631 + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms");
632 }
633 }
634
Angus Kong9ef99252013-07-18 18:04:19 -0700635 private final class JpegPictureCallback
636 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800637 Location mLocation;
638
639 public JpegPictureCallback(Location loc) {
640 mLocation = loc;
641 }
642
643 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700644 public void onPictureTaken(final byte [] jpegData, CameraProxy camera) {
Sascha Haeberling88901942013-08-28 17:49:00 -0700645 mUI.enableShutter(true);
Michael Kolb8872c232013-01-29 10:33:22 -0800646 if (mPaused) {
647 return;
648 }
Doris Liu6432cd62013-06-13 17:20:31 -0700649 if (mIsImageCaptureIntent) {
650 stopPreview();
651 }
Angus Kongb50b5cb2013-08-09 14:55:20 -0700652 if (mSceneMode == CameraUtil.SCENE_MODE_HDR) {
Doris Liu6432cd62013-06-13 17:20:31 -0700653 mUI.setSwipingEnabled(true);
Michael Kolb8872c232013-01-29 10:33:22 -0800654 }
655
656 mJpegPictureCallbackTime = System.currentTimeMillis();
657 // If postview callback has arrived, the captured image is displayed
658 // in postview callback. If not, the captured image is displayed in
659 // raw picture callback.
660 if (mPostViewPictureCallbackTime != 0) {
661 mShutterToPictureDisplayedTime =
662 mPostViewPictureCallbackTime - mShutterCallbackTime;
663 mPictureDisplayedToJpegCallbackTime =
664 mJpegPictureCallbackTime - mPostViewPictureCallbackTime;
665 } else {
666 mShutterToPictureDisplayedTime =
667 mRawPictureCallbackTime - mShutterCallbackTime;
668 mPictureDisplayedToJpegCallbackTime =
669 mJpegPictureCallbackTime - mRawPictureCallbackTime;
670 }
671 Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = "
672 + mPictureDisplayedToJpegCallbackTime + "ms");
673
Michael Kolb8872c232013-01-29 10:33:22 -0800674 mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden.
675 if (!mIsImageCaptureIntent) {
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700676 setupPreview();
Michael Kolb8872c232013-01-29 10:33:22 -0800677 }
678
Doris Liu36e56fb2013-09-11 17:38:08 -0700679 ExifInterface exif = Exif.getExif(jpegData);
680 int orientation = Exif.getOrientation(exif);
Ruben Brunkd217ed02013-10-08 23:31:13 -0700681
Ruben Brunkd7488272013-10-10 18:45:53 -0700682 if (!mIsImageCaptureIntent) {
Michael Kolb8872c232013-01-29 10:33:22 -0800683 // Calculate the width and the height of the jpeg.
684 Size s = mParameters.getPictureSize();
Michael Kolb8872c232013-01-29 10:33:22 -0800685 int width, height;
686 if ((mJpegRotation + orientation) % 180 == 0) {
687 width = s.width;
688 height = s.height;
689 } else {
690 width = s.height;
691 height = s.width;
692 }
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700693 NamedEntity name = mNamedImages.getNextNameEntity();
694 String title = (name == null) ? null : name.title;
695 long date = (name == null) ? -1 : name.date;
Ruben Brunkd7488272013-10-10 18:45:53 -0700696
697 // Handle debug mode outputs
698 if (mDebugUri != null) {
699 // If using a debug uri, save jpeg there.
700 saveToDebugUri(jpegData);
701
702 // Adjust the title of the debug image shown in mediastore.
703 if (title != null) {
704 title = DEBUG_IMAGE_PREFIX + title;
705 }
706 }
707
Michael Kolb8872c232013-01-29 10:33:22 -0800708 if (title == null) {
709 Log.e(TAG, "Unbalanced name/data pair");
710 } else {
711 if (date == -1) date = mCaptureStartTime;
Angus Kong0d00a892013-03-26 11:40:40 -0700712 if (mHeading >= 0) {
713 // heading direction has been updated by the sensor.
714 ExifTag directionRefTag = exif.buildTag(
715 ExifInterface.TAG_GPS_IMG_DIRECTION_REF,
716 ExifInterface.GpsTrackRef.MAGNETIC_DIRECTION);
717 ExifTag directionTag = exif.buildTag(
718 ExifInterface.TAG_GPS_IMG_DIRECTION,
719 new Rational(mHeading, 1));
720 exif.setTag(directionRefTag);
721 exif.setTag(directionTag);
722 }
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800723 getServices().getMediaSaver().addImage(
Angus Kong86d36312013-01-31 18:22:44 -0800724 jpegData, title, date, mLocation, width, height,
Angus Kong0d00a892013-03-26 11:40:40 -0700725 orientation, exif, mOnMediaSavedListener, mContentResolver);
Michael Kolb8872c232013-01-29 10:33:22 -0800726 }
Doris Liuce2acbc2013-08-21 18:45:29 -0700727 // Animate capture with real jpeg data instead of a preview frame.
Doris Liu29da2db2013-08-28 14:28:45 -0700728 mUI.animateCapture(jpegData, orientation, mMirror);
Michael Kolb8872c232013-01-29 10:33:22 -0800729 } else {
730 mJpegImageData = jpegData;
731 if (!mQuickCapture) {
Doris Liu36e56fb2013-09-11 17:38:08 -0700732 mUI.showCapturedImageForReview(jpegData, orientation, mMirror);
Michael Kolb8872c232013-01-29 10:33:22 -0800733 } else {
Michael Kolbd6954f32013-03-08 20:43:01 -0800734 onCaptureDone();
Michael Kolb8872c232013-01-29 10:33:22 -0800735 }
736 }
737
738 // Check this in advance of each shot so we don't add to shutter
739 // latency. It's true that someone else could write to the SD card in
740 // the mean time and fill it, but that could have happened between the
741 // shutter press and saving the JPEG too.
742 mActivity.updateStorageSpaceAndHint();
743
744 long now = System.currentTimeMillis();
745 mJpegCallbackFinishTime = now - mJpegPictureCallbackTime;
746 Log.v(TAG, "mJpegCallbackFinishTime = "
747 + mJpegCallbackFinishTime + "ms");
748 mJpegPictureCallbackTime = 0;
749 }
750 }
751
Angus Kong9ef99252013-07-18 18:04:19 -0700752 private final class AutoFocusCallback implements CameraAFCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800753 @Override
754 public void onAutoFocus(
Angus Kong9ef99252013-07-18 18:04:19 -0700755 boolean focused, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800756 if (mPaused) return;
757
758 mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime;
759 Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms");
760 setCameraState(IDLE);
Michael Kolbd6954f32013-03-08 20:43:01 -0800761 mFocusManager.onAutoFocus(focused, mUI.isShutterPressed());
Michael Kolb8872c232013-01-29 10:33:22 -0800762 }
763 }
764
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700765 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -0800766 private final class AutoFocusMoveCallback
Angus Kong9ef99252013-07-18 18:04:19 -0700767 implements CameraAFMoveCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800768 @Override
769 public void onAutoFocusMoving(
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700770 boolean moving, CameraProxy camera) {
771 mFocusManager.onAutoFocusMoving(moving);
Michael Kolb8872c232013-01-29 10:33:22 -0800772 }
773 }
774
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700775 /**
776 * This class is just a thread-safe queue for name,date holder objects.
777 */
778 public static class NamedImages {
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800779 private final Vector<NamedEntity> mQueue;
Michael Kolb8872c232013-01-29 10:33:22 -0800780
781 public NamedImages() {
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700782 mQueue = new Vector<NamedEntity>();
Michael Kolb8872c232013-01-29 10:33:22 -0800783 }
784
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700785 public void nameNewImage(long date) {
Michael Kolb8872c232013-01-29 10:33:22 -0800786 NamedEntity r = new NamedEntity();
Angus Kongb50b5cb2013-08-09 14:55:20 -0700787 r.title = CameraUtil.createJpegName(date);
Michael Kolb8872c232013-01-29 10:33:22 -0800788 r.date = date;
789 mQueue.add(r);
790 }
791
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700792 public NamedEntity getNextNameEntity() {
793 synchronized(mQueue) {
794 if (!mQueue.isEmpty()) {
795 return mQueue.remove(0);
796 }
Michael Kolb8872c232013-01-29 10:33:22 -0800797 }
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700798 return null;
Michael Kolb8872c232013-01-29 10:33:22 -0800799 }
800
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700801 public static class NamedEntity {
802 public String title;
803 public long date;
Michael Kolb8872c232013-01-29 10:33:22 -0800804 }
805 }
806
807 private void setCameraState(int state) {
808 mCameraState = state;
809 switch (state) {
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700810 case PhotoController.PREVIEW_STOPPED:
811 case PhotoController.SNAPSHOT_IN_PROGRESS:
812 case PhotoController.SWITCHING_CAMERA:
Doris Liuf55f3c42013-11-20 00:24:46 -0800813 // TODO: Tell app UI to disable swipe
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700814 break;
815 case PhotoController.IDLE:
Doris Liuf55f3c42013-11-20 00:24:46 -0800816 // TODO: Tell app UI to enable swipe
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700817 break;
Michael Kolb8872c232013-01-29 10:33:22 -0800818 }
819 }
820
Sascha Haeberling37f36112013-08-06 14:31:52 -0700821 private void animateAfterShutter() {
Michael Kolb8872c232013-01-29 10:33:22 -0800822 // Only animate when in full screen capture mode
823 // i.e. If monkey/a user swipes to the gallery during picture taking,
824 // don't show animation
Doris Liuc2e9abd2013-06-19 14:20:51 -0700825 if (!mIsImageCaptureIntent) {
826 mUI.animateFlash();
Doris Liuc2e9abd2013-06-19 14:20:51 -0700827 }
Michael Kolb8872c232013-01-29 10:33:22 -0800828 }
829
830 @Override
831 public boolean capture() {
832 // If we are already in the middle of taking a snapshot or the image save request
833 // is full then ignore.
834 if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS
Angus Kong86d36312013-01-31 18:22:44 -0800835 || mCameraState == SWITCHING_CAMERA
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800836 || getServices().getMediaSaver() == null
837 || getServices().getMediaSaver().isQueueFull()) {
Michael Kolb8872c232013-01-29 10:33:22 -0800838 return false;
839 }
840 mCaptureStartTime = System.currentTimeMillis();
841 mPostViewPictureCallbackTime = 0;
842 mJpegImageData = null;
843
Angus Kongb50b5cb2013-08-09 14:55:20 -0700844 final boolean animateBefore = (mSceneMode == CameraUtil.SCENE_MODE_HDR);
Michael Kolb8872c232013-01-29 10:33:22 -0800845
846 if (animateBefore) {
Sascha Haeberling37f36112013-08-06 14:31:52 -0700847 animateAfterShutter();
Michael Kolb8872c232013-01-29 10:33:22 -0800848 }
849
850 // Set rotation and gps data.
Doris Liu3cf565c2013-02-15 10:55:37 -0800851 int orientation;
852 // We need to be consistent with the framework orientation (i.e. the
853 // orientation of the UI.) when the auto-rotate screen setting is on.
854 if (mActivity.isAutoRotateScreen()) {
855 orientation = (360 - mDisplayRotation) % 360;
856 } else {
857 orientation = mOrientation;
858 }
Angus Kong20fad242013-11-11 18:23:46 -0800859 mJpegRotation = CameraUtil.getJpegRotation(mActivity, mCameraId, orientation);
Michael Kolb8872c232013-01-29 10:33:22 -0800860 mParameters.setRotation(mJpegRotation);
Erin Dahlgrenc120b0f2013-11-19 10:53:49 -0800861 Location loc = mActivity.getLocationManager().getCurrentLocation();
Angus Kongb50b5cb2013-08-09 14:55:20 -0700862 CameraUtil.setGpsParameters(mParameters, loc);
Michael Kolb8872c232013-01-29 10:33:22 -0800863 mCameraDevice.setParameters(mParameters);
864
Sascha Haeberling88901942013-08-28 17:49:00 -0700865 // We don't want user to press the button again while taking a
866 // multi-second HDR photo.
867 mUI.enableShutter(false);
Angus Kong9ef99252013-07-18 18:04:19 -0700868 mCameraDevice.takePicture(mHandler,
869 new ShutterCallback(!animateBefore),
Angus Kongdcb0ef12013-03-25 23:11:43 -0700870 mRawPictureCallback, mPostViewPictureCallback,
Angus Kong9ef99252013-07-18 18:04:19 -0700871 new JpegPictureCallback(loc));
Michael Kolb8872c232013-01-29 10:33:22 -0800872
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700873 mNamedImages.nameNewImage(mCaptureStartTime);
Michael Kolb8872c232013-01-29 10:33:22 -0800874
875 mFaceDetectionStarted = false;
876 setCameraState(SNAPSHOT_IN_PROGRESS);
Bobby Georgescu301b6462013-04-01 15:33:17 -0700877 UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
Seth Raphaelcbd82672013-11-05 10:12:36 -0800878 UsageStatistics.ACTION_CAPTURE_DONE, "Photo", 0,
Seth Raphael9e039852013-11-12 11:21:17 -0800879 UsageStatistics.hashFileName(mNamedImages.mQueue.lastElement().title + ".jpg"),
880 mParameters.flatten());
Michael Kolb8872c232013-01-29 10:33:22 -0800881 return true;
882 }
883
884 @Override
885 public void setFocusParameters() {
886 setCameraParameters(UPDATE_PARAM_PREFERENCE);
887 }
888
Michael Kolbd6954f32013-03-08 20:43:01 -0800889 private void updateSceneMode() {
Michael Kolb8872c232013-01-29 10:33:22 -0800890 // If scene mode is set, we cannot set flash mode, white balance, and
891 // focus mode, instead, we read it from driver
892 if (!Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
893 overrideCameraSettings(mParameters.getFlashMode(),
894 mParameters.getWhiteBalance(), mParameters.getFocusMode());
895 } else {
896 overrideCameraSettings(null, null, null);
897 }
898 }
899
900 private void overrideCameraSettings(final String flashMode,
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700901 final String whiteBalance, final String focusMode) {
Michael Kolbd6954f32013-03-08 20:43:01 -0800902 mUI.overrideSettings(
903 CameraSettings.KEY_FLASH_MODE, flashMode,
904 CameraSettings.KEY_WHITE_BALANCE, whiteBalance,
905 CameraSettings.KEY_FOCUS_MODE, focusMode);
Michael Kolb8872c232013-01-29 10:33:22 -0800906 }
907
Erin Dahlgren357b7672013-11-20 17:38:14 -0800908 private PreferenceGroup loadPreferenceGroup() {
Michael Kolb8872c232013-01-29 10:33:22 -0800909 CameraSettings settings = new CameraSettings(mActivity, mInitialParams,
Erin Dahlgren357b7672013-11-20 17:38:14 -0800910 mCameraId, CameraHolder.instance().getCameraInfo());
911 return settings.getPreferenceGroup(R.xml.camera_preferences);
Michael Kolb8872c232013-01-29 10:33:22 -0800912 }
913
914 @Override
Michael Kolb8872c232013-01-29 10:33:22 -0800915 public void onOrientationChanged(int orientation) {
916 // We keep the last known orientation. So if the user first orient
917 // the camera then point the camera to floor or sky, we still have
918 // the correct orientation.
919 if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return;
Angus Kongb50b5cb2013-08-09 14:55:20 -0700920 mOrientation = CameraUtil.roundOrientation(orientation, mOrientation);
Michael Kolb8872c232013-01-29 10:33:22 -0800921
922 // Show the toast after getting the first orientation changed.
Angus Kong13e87c42013-11-25 10:02:47 -0800923 if (mHandler.hasMessages(MSG_SHOW_TAP_TO_FOCUS_TOAST)) {
924 mHandler.removeMessages(MSG_SHOW_TAP_TO_FOCUS_TOAST);
Michael Kolb8872c232013-01-29 10:33:22 -0800925 showTapToFocusToast();
926 }
927 }
928
929 @Override
Angus Kong20fad242013-11-11 18:23:46 -0800930 public void onCameraAvailable(CameraProxy cameraProxy) {
931 if (mPaused) {
932 return;
933 }
934 mCameraDevice = cameraProxy;
935
Erin Dahlgren357b7672013-11-20 17:38:14 -0800936 resetExposureCompensation();
Angus Kong20fad242013-11-11 18:23:46 -0800937 initializeCapabilities();
938
939 // Reset zoom value index.
940 mZoomValue = 0;
941 if (mFocusManager == null) {
942 initializeFocusManager();
943 }
944 mFocusManager.setParameters(mInitialParams);
945
946 mParameters = mCameraDevice.getParameters();
947 setCameraParameters(UPDATE_PARAM_ALL);
948 mCameraPreviewParamsReady = true;
949 startPreview();
950
951 onCameraOpened();
952 }
953
954 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -0800955 public void onCaptureCancelled() {
956 mActivity.setResultEx(Activity.RESULT_CANCELED, new Intent());
957 mActivity.finish();
Michael Kolb8872c232013-01-29 10:33:22 -0800958 }
959
Michael Kolbd6954f32013-03-08 20:43:01 -0800960 @Override
961 public void onCaptureRetake() {
Michael Kolb8872c232013-01-29 10:33:22 -0800962 if (mPaused)
963 return;
Michael Kolbd6954f32013-03-08 20:43:01 -0800964 mUI.hidePostCaptureAlert();
Michael Kolb8872c232013-01-29 10:33:22 -0800965 setupPreview();
966 }
967
Michael Kolbd6954f32013-03-08 20:43:01 -0800968 @Override
969 public void onCaptureDone() {
Michael Kolb8872c232013-01-29 10:33:22 -0800970 if (mPaused) {
971 return;
972 }
973
974 byte[] data = mJpegImageData;
975
976 if (mCropValue == null) {
977 // First handle the no crop case -- just return the value. If the
978 // caller specifies a "save uri" then write the data to its
979 // stream. Otherwise, pass back a scaled down version of the bitmap
980 // directly in the extras.
981 if (mSaveUri != null) {
982 OutputStream outputStream = null;
983 try {
984 outputStream = mContentResolver.openOutputStream(mSaveUri);
985 outputStream.write(data);
986 outputStream.close();
987
988 mActivity.setResultEx(Activity.RESULT_OK);
989 mActivity.finish();
990 } catch (IOException ex) {
991 // ignore exception
992 } finally {
Angus Kongb50b5cb2013-08-09 14:55:20 -0700993 CameraUtil.closeSilently(outputStream);
Michael Kolb8872c232013-01-29 10:33:22 -0800994 }
995 } else {
Angus Kong0d00a892013-03-26 11:40:40 -0700996 ExifInterface exif = Exif.getExif(data);
997 int orientation = Exif.getOrientation(exif);
Angus Kongb50b5cb2013-08-09 14:55:20 -0700998 Bitmap bitmap = CameraUtil.makeBitmap(data, 50 * 1024);
999 bitmap = CameraUtil.rotate(bitmap, orientation);
Michael Kolb8872c232013-01-29 10:33:22 -08001000 mActivity.setResultEx(Activity.RESULT_OK,
1001 new Intent("inline-data").putExtra("data", bitmap));
1002 mActivity.finish();
1003 }
1004 } else {
1005 // Save the image to a temp file and invoke the cropper
1006 Uri tempUri = null;
1007 FileOutputStream tempStream = null;
1008 try {
1009 File path = mActivity.getFileStreamPath(sTempCropFilename);
1010 path.delete();
1011 tempStream = mActivity.openFileOutput(sTempCropFilename, 0);
1012 tempStream.write(data);
1013 tempStream.close();
1014 tempUri = Uri.fromFile(path);
1015 } catch (FileNotFoundException ex) {
1016 mActivity.setResultEx(Activity.RESULT_CANCELED);
1017 mActivity.finish();
1018 return;
1019 } catch (IOException ex) {
1020 mActivity.setResultEx(Activity.RESULT_CANCELED);
1021 mActivity.finish();
1022 return;
1023 } finally {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001024 CameraUtil.closeSilently(tempStream);
Michael Kolb8872c232013-01-29 10:33:22 -08001025 }
1026
1027 Bundle newExtras = new Bundle();
1028 if (mCropValue.equals("circle")) {
1029 newExtras.putString("circleCrop", "true");
1030 }
1031 if (mSaveUri != null) {
1032 newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
1033 } else {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001034 newExtras.putBoolean(CameraUtil.KEY_RETURN_DATA, true);
Michael Kolb8872c232013-01-29 10:33:22 -08001035 }
1036 if (mActivity.isSecureCamera()) {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001037 newExtras.putBoolean(CameraUtil.KEY_SHOW_WHEN_LOCKED, true);
Michael Kolb8872c232013-01-29 10:33:22 -08001038 }
1039
Sascha Haeberling37f36112013-08-06 14:31:52 -07001040 // TODO: Share this constant.
Sascha Haeberling8e963a52013-08-06 11:43:02 -07001041 final String CROP_ACTION = "com.android.camera.action.CROP";
1042 Intent cropIntent = new Intent(CROP_ACTION);
Michael Kolb8872c232013-01-29 10:33:22 -08001043
1044 cropIntent.setData(tempUri);
1045 cropIntent.putExtras(newExtras);
1046
1047 mActivity.startActivityForResult(cropIntent, REQUEST_CROP);
1048 }
1049 }
1050
Michael Kolb8872c232013-01-29 10:33:22 -08001051 @Override
1052 public void onShutterButtonFocus(boolean pressed) {
Michael Kolbd6954f32013-03-08 20:43:01 -08001053 if (mPaused || mUI.collapseCameraControls()
Michael Kolb8872c232013-01-29 10:33:22 -08001054 || (mCameraState == SNAPSHOT_IN_PROGRESS)
1055 || (mCameraState == PREVIEW_STOPPED)) return;
1056
1057 // Do not do focus if there is not enough storage.
1058 if (pressed && !canTakePicture()) return;
1059
1060 if (pressed) {
Michael Kolb8872c232013-01-29 10:33:22 -08001061 mFocusManager.onShutterDown();
1062 } else {
Doris Liuda50e052013-02-07 14:36:32 -08001063 // for countdown mode, we need to postpone the shutter release
1064 // i.e. lock the focus during countdown.
Michael Kolbd6954f32013-03-08 20:43:01 -08001065 if (!mUI.isCountingDown()) {
Doris Liuda50e052013-02-07 14:36:32 -08001066 mFocusManager.onShutterUp();
1067 }
Michael Kolb8872c232013-01-29 10:33:22 -08001068 }
1069 }
1070
1071 @Override
1072 public void onShutterButtonClick() {
Michael Kolbd6954f32013-03-08 20:43:01 -08001073 if (mPaused || mUI.collapseCameraControls()
Michael Kolb8872c232013-01-29 10:33:22 -08001074 || (mCameraState == SWITCHING_CAMERA)
1075 || (mCameraState == PREVIEW_STOPPED)) return;
1076
1077 // Do not take the picture if there is not enough storage.
Angus Kong2dcc0a92013-09-25 13:00:08 -07001078 if (mActivity.getStorageSpaceBytes() <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
Michael Kolb8872c232013-01-29 10:33:22 -08001079 Log.i(TAG, "Not enough space or storage not ready. remaining="
Angus Kong2dcc0a92013-09-25 13:00:08 -07001080 + mActivity.getStorageSpaceBytes());
Michael Kolb8872c232013-01-29 10:33:22 -08001081 return;
1082 }
1083 Log.v(TAG, "onShutterButtonClick: mCameraState=" + mCameraState);
1084
Angus Kongb50b5cb2013-08-09 14:55:20 -07001085 if (mSceneMode == CameraUtil.SCENE_MODE_HDR) {
Doris Liu6432cd62013-06-13 17:20:31 -07001086 mUI.setSwipingEnabled(false);
Doris Liu9cdfe002013-04-16 09:50:56 -07001087 }
Michael Kolb8872c232013-01-29 10:33:22 -08001088 // If the user wants to do a snapshot while the previous one is still
1089 // in progress, remember the fact and do it after we finish the previous
1090 // one and re-start the preview. Snapshot in progress also includes the
1091 // state that autofocus is focusing and a picture will be taken when
1092 // focus callback arrives.
1093 if ((mFocusManager.isFocusingSnapOnFinish() || mCameraState == SNAPSHOT_IN_PROGRESS)
1094 && !mIsImageCaptureIntent) {
1095 mSnapshotOnIdle = true;
1096 return;
1097 }
1098
Erin Dahlgren357b7672013-11-20 17:38:14 -08001099 SettingsManager settingsManager = mActivity.getSettingsManager();
1100 String timer = settingsManager.get(new TimerSetting());
1101 String playSound = settingsManager.get(new TimerSoundSetting());
Michael Kolb8872c232013-01-29 10:33:22 -08001102 int seconds = Integer.parseInt(timer);
1103 // When shutter button is pressed, check whether the previous countdown is
1104 // finished. If not, cancel the previous countdown and start a new one.
Michael Kolbd6954f32013-03-08 20:43:01 -08001105 if (mUI.isCountingDown()) {
1106 mUI.cancelCountDown();
1107 }
1108 if (seconds > 0) {
Erin Dahlgren357b7672013-11-20 17:38:14 -08001109 mUI.startCountDown(seconds, playSound.equals(SettingsManager.VALUE_ON));
Michael Kolb8872c232013-01-29 10:33:22 -08001110 } else {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001111 mSnapshotOnIdle = false;
1112 mFocusManager.doSnap();
Michael Kolb8872c232013-01-29 10:33:22 -08001113 }
1114 }
1115
Andy Huibersdef975d2013-11-22 09:13:39 -08001116 private void onResumeTasks() {
1117 Log.v(TAG, "Executing onResumeTasks.");
Michael Kolb8872c232013-01-29 10:33:22 -08001118 if (mOpenCameraFail || mCameraDisabled) return;
1119
Angus Kong20fad242013-11-11 18:23:46 -08001120 mActivity.getCameraProvider().requestCamera(mCameraId);
1121
Michael Kolb8872c232013-01-29 10:33:22 -08001122 mJpegPictureCallbackTime = 0;
1123 mZoomValue = 0;
Angus Kong20fad242013-11-11 18:23:46 -08001124
1125 mOnResumeTime = SystemClock.uptimeMillis();
1126 checkDisplayRotation();
Michael Kolb8872c232013-01-29 10:33:22 -08001127
1128 // If first time initialization is not finished, put it in the
1129 // message queue.
1130 if (!mFirstTimeInitialized) {
Angus Kong13e87c42013-11-25 10:02:47 -08001131 mHandler.sendEmptyMessage(MSG_FIRST_TIME_INIT);
Michael Kolb8872c232013-01-29 10:33:22 -08001132 } else {
1133 initializeSecondTime();
1134 }
Doris Liu59390062013-10-10 17:20:56 -07001135 mUI.initDisplayChangeListener();
Michael Kolb8872c232013-01-29 10:33:22 -08001136
Bobby Georgescu0a7dd572013-03-12 22:45:17 -07001137 UsageStatistics.onContentViewChanged(
1138 UsageStatistics.COMPONENT_CAMERA, "PhotoModule");
Angus Kong0d00a892013-03-26 11:40:40 -07001139
1140 Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1141 if (gsensor != null) {
1142 mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL);
1143 }
1144
1145 Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1146 if (msensor != null) {
1147 mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_NORMAL);
1148 }
Michael Kolb8872c232013-01-29 10:33:22 -08001149 }
1150
Angus Kongc4e66562013-11-22 23:03:21 -08001151 /**
1152 * The focus manager is the first UI related element to get initialized,
1153 * and it requires the RenderOverlay, so initialize it here
1154 */
1155 private void initializeFocusManager() {
1156 // Create FocusManager object. startPreview needs it.
1157 // if mFocusManager not null, reuse it
1158 // otherwise create a new instance
1159 if (mFocusManager != null) {
1160 mFocusManager.removeMessages();
1161 } else {
1162 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
1163 mMirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
1164 String[] defaultFocusModes = mActivity.getResources().getStringArray(
1165 R.array.pref_camera_focusmode_default_array);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001166 mFocusManager = new FocusOverlayManager(mActivity.getSettingsManager(),
1167 defaultFocusModes,
Angus Kongc4e66562013-11-22 23:03:21 -08001168 mInitialParams, this, mMirror,
1169 mActivity.getMainLooper(), mUI);
1170 }
Angus Kong20fad242013-11-11 18:23:46 -08001171 }
1172
1173 @Override
Angus Kongc4e66562013-11-22 23:03:21 -08001174 public void resume() {
1175 mPaused = false;
1176 // Add delay on resume from lock screen only, in order to to speed up
1177 // the onResume --> onPause --> onResume cycle from lock screen.
1178 // Don't do always because letting go of thread can cause delay.
1179 String action = mActivity.getIntent().getAction();
1180 if (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(action)
1181 || MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action)) {
1182 Log.v(TAG, "On resume, from lock screen.");
1183 // Note: onPauseAfterSuper() will delete this runnable, so we will
1184 // at most have 1 copy queued up.
1185 mHandler.postDelayed(new Runnable() {
1186 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();
1469 }
1470
1471 @SuppressWarnings("deprecation")
1472 private void updateCameraParametersInitialize() {
1473 // Reset preview frame rate to the maximum because it may be lowered by
1474 // video camera application.
ztenghui16a35202013-09-23 11:35:36 -07001475 int[] fpsRange = CameraUtil.getPhotoPreviewFpsRange(mParameters);
1476 if (fpsRange != null && fpsRange.length > 0) {
Doris Liu6432cd62013-06-13 17:20:31 -07001477 mParameters.setPreviewFpsRange(
1478 fpsRange[Parameters.PREVIEW_FPS_MIN_INDEX],
1479 fpsRange[Parameters.PREVIEW_FPS_MAX_INDEX]);
Michael Kolb8872c232013-01-29 10:33:22 -08001480 }
1481
Angus Kongb50b5cb2013-08-09 14:55:20 -07001482 mParameters.set(CameraUtil.RECORDING_HINT, CameraUtil.FALSE);
Michael Kolb8872c232013-01-29 10:33:22 -08001483
1484 // Disable video stabilization. Convenience methods not available in API
1485 // level <= 14
1486 String vstabSupported = mParameters.get("video-stabilization-supported");
1487 if ("true".equals(vstabSupported)) {
1488 mParameters.set("video-stabilization", "false");
1489 }
1490 }
1491
1492 private void updateCameraParametersZoom() {
1493 // Set zoom.
1494 if (mParameters.isZoomSupported()) {
1495 mParameters.setZoom(mZoomValue);
1496 }
1497 }
1498
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001499 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08001500 private void setAutoExposureLockIfSupported() {
1501 if (mAeLockSupported) {
1502 mParameters.setAutoExposureLock(mFocusManager.getAeAwbLock());
1503 }
1504 }
1505
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001506 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08001507 private void setAutoWhiteBalanceLockIfSupported() {
1508 if (mAwbLockSupported) {
1509 mParameters.setAutoWhiteBalanceLock(mFocusManager.getAeAwbLock());
1510 }
1511 }
1512
Michael Kolb8872c232013-01-29 10:33:22 -08001513 private void setFocusAreasIfSupported() {
1514 if (mFocusAreaSupported) {
1515 mParameters.setFocusAreas(mFocusManager.getFocusAreas());
1516 }
1517 }
1518
Michael Kolb8872c232013-01-29 10:33:22 -08001519 private void setMeteringAreasIfSupported() {
1520 if (mMeteringAreaSupported) {
Michael Kolb8872c232013-01-29 10:33:22 -08001521 mParameters.setMeteringAreas(mFocusManager.getMeteringAreas());
1522 }
1523 }
1524
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001525 private boolean updateCameraParametersPreference() {
Erin Dahlgren357b7672013-11-20 17:38:14 -08001526 SettingsManager settingsManager = mActivity.getSettingsManager();
1527
Michael Kolb8872c232013-01-29 10:33:22 -08001528 setAutoExposureLockIfSupported();
1529 setAutoWhiteBalanceLockIfSupported();
1530 setFocusAreasIfSupported();
1531 setMeteringAreasIfSupported();
1532
1533 // Set picture size.
Angus Kong13e87c42013-11-25 10:02:47 -08001534 String pictureSize = settingsManager.get(new PictureSizeSetting());
Michael Kolb8872c232013-01-29 10:33:22 -08001535 if (pictureSize == null) {
Erin Dahlgren357b7672013-11-20 17:38:14 -08001536 //TODO: deprecate CameraSettings.
1537 CameraSettings.initialCameraPictureSize(
1538 mActivity, mParameters, settingsManager);
Michael Kolb8872c232013-01-29 10:33:22 -08001539 } else {
1540 List<Size> supported = mParameters.getSupportedPictureSizes();
1541 CameraSettings.setCameraPictureSize(
1542 pictureSize, supported, mParameters);
1543 }
1544 Size size = mParameters.getPictureSize();
1545
1546 // Set a preview size that is closest to the viewfinder height and has
1547 // the right aspect ratio.
1548 List<Size> sizes = mParameters.getSupportedPreviewSizes();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001549 Size optimalSize = CameraUtil.getOptimalPreviewSize(mActivity, sizes,
Michael Kolb8872c232013-01-29 10:33:22 -08001550 (double) size.width / size.height);
1551 Size original = mParameters.getPreviewSize();
1552 if (!original.equals(optimalSize)) {
1553 mParameters.setPreviewSize(optimalSize.width, optimalSize.height);
Doris Liu6432cd62013-06-13 17:20:31 -07001554
Michael Kolb8872c232013-01-29 10:33:22 -08001555 // Zoom related settings will be changed for different preview
1556 // sizes, so set and read the parameters to get latest values
Michael Kolb4a4d4ef2013-05-30 08:35:26 -07001557 if (mHandler.getLooper() == Looper.myLooper()) {
1558 // On UI thread only, not when camera starts up
1559 setupPreview();
1560 } else {
1561 mCameraDevice.setParameters(mParameters);
1562 }
Michael Kolb8872c232013-01-29 10:33:22 -08001563 mParameters = mCameraDevice.getParameters();
1564 }
Doris Liu95405742013-11-05 15:25:26 -08001565
1566 if(optimalSize.width != 0 && optimalSize.height != 0) {
1567 mUI.updatePreviewAspectRatio((float) optimalSize.width
1568 / (float) optimalSize.height);
1569 }
Michael Kolb8872c232013-01-29 10:33:22 -08001570 Log.v(TAG, "Preview size is " + optimalSize.width + "x" + optimalSize.height);
1571
1572 // Since changing scene mode may change supported values, set scene mode
1573 // first. HDR is a scene mode. To promote it in UI, it is stored in a
1574 // separate preference.
Sascha Haeberling98f38bb2013-09-24 18:58:34 -07001575 String onValue = mActivity.getString(R.string.setting_on_value);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001576 String hdr = settingsManager.get(new HdrSetting());
1577 String hdrPlus = settingsManager.get(new HdrPlusSetting());
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001578 boolean hdrOn = onValue.equals(hdr);
Sascha Haeberling9bf0fd62013-10-03 12:10:48 -07001579 boolean hdrPlusOn = onValue.equals(hdrPlus);
Sascha Haeberling98f38bb2013-09-24 18:58:34 -07001580
1581 boolean doGcamModeSwitch = false;
Sascha Haeberling9bf0fd62013-10-03 12:10:48 -07001582 if (hdrPlusOn && GcamHelper.hasGcamCapture()) {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001583 // Kick off mode switch to gcam.
1584 doGcamModeSwitch = true;
Michael Kolb8872c232013-01-29 10:33:22 -08001585 } else {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001586 if (hdrOn) {
1587 mSceneMode = CameraUtil.SCENE_MODE_HDR;
1588 } else {
Erin Dahlgren357b7672013-11-20 17:38:14 -08001589 mSceneMode = settingsManager.get(new SceneModeSetting());
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001590 }
Michael Kolb8872c232013-01-29 10:33:22 -08001591 }
Angus Kongb50b5cb2013-08-09 14:55:20 -07001592 if (CameraUtil.isSupported(mSceneMode, mParameters.getSupportedSceneModes())) {
Michael Kolb8872c232013-01-29 10:33:22 -08001593 if (!mParameters.getSceneMode().equals(mSceneMode)) {
1594 mParameters.setSceneMode(mSceneMode);
1595
1596 // Setting scene mode will change the settings of flash mode,
1597 // white balance, and focus mode. Here we read back the
1598 // parameters, so we can know those settings.
1599 mCameraDevice.setParameters(mParameters);
1600 mParameters = mCameraDevice.getParameters();
1601 }
1602 } else {
1603 mSceneMode = mParameters.getSceneMode();
1604 if (mSceneMode == null) {
1605 mSceneMode = Parameters.SCENE_MODE_AUTO;
1606 }
1607 }
1608
1609 // Set JPEG quality.
1610 int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId,
1611 CameraProfile.QUALITY_HIGH);
1612 mParameters.setJpegQuality(jpegQuality);
1613
1614 // For the following settings, we need to check if the settings are
1615 // still supported by latest driver, if not, ignore the settings.
1616
1617 // Set exposure compensation
Angus Kong13e87c42013-11-25 10:02:47 -08001618 int value = Integer.parseInt(settingsManager.get(new ExposureSetting()));
Michael Kolb8872c232013-01-29 10:33:22 -08001619 int max = mParameters.getMaxExposureCompensation();
1620 int min = mParameters.getMinExposureCompensation();
1621 if (value >= min && value <= max) {
1622 mParameters.setExposureCompensation(value);
1623 } else {
1624 Log.w(TAG, "invalid exposure range: " + value);
1625 }
1626
1627 if (Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
1628 // Set flash mode.
Erin Dahlgren357b7672013-11-20 17:38:14 -08001629 String flashMode = settingsManager.get(new FlashSetting());
Michael Kolb8872c232013-01-29 10:33:22 -08001630 List<String> supportedFlash = mParameters.getSupportedFlashModes();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001631 if (CameraUtil.isSupported(flashMode, supportedFlash)) {
Michael Kolb8872c232013-01-29 10:33:22 -08001632 mParameters.setFlashMode(flashMode);
1633 } else {
1634 flashMode = mParameters.getFlashMode();
1635 if (flashMode == null) {
1636 flashMode = mActivity.getString(
1637 R.string.pref_camera_flashmode_no_flash);
1638 }
1639 }
1640
1641 // Set white balance parameter.
Angus Kong13e87c42013-11-25 10:02:47 -08001642 String whiteBalance = settingsManager.get(new WhiteBalanceSetting());
Angus Kongb50b5cb2013-08-09 14:55:20 -07001643 if (CameraUtil.isSupported(whiteBalance,
Michael Kolb8872c232013-01-29 10:33:22 -08001644 mParameters.getSupportedWhiteBalance())) {
1645 mParameters.setWhiteBalance(whiteBalance);
1646 } else {
1647 whiteBalance = mParameters.getWhiteBalance();
1648 if (whiteBalance == null) {
1649 whiteBalance = Parameters.WHITE_BALANCE_AUTO;
1650 }
1651 }
1652
1653 // Set focus mode.
1654 mFocusManager.overrideFocusMode(null);
1655 mParameters.setFocusMode(mFocusManager.getFocusMode());
1656 } else {
1657 mFocusManager.overrideFocusMode(mParameters.getFocusMode());
1658 }
1659
Angus Kongdcccc512013-08-08 17:06:03 -07001660 if (mContinuousFocusSupported && ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK) {
Michael Kolb8872c232013-01-29 10:33:22 -08001661 updateAutoFocusMoveCallback();
1662 }
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001663
1664 return doGcamModeSwitch;
Michael Kolb8872c232013-01-29 10:33:22 -08001665 }
1666
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001667 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08001668 private void updateAutoFocusMoveCallback() {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001669 if (mParameters.getFocusMode().equals(CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE)) {
Angus Kong9ef99252013-07-18 18:04:19 -07001670 mCameraDevice.setAutoFocusMoveCallback(mHandler,
Angus Kong4f795b82013-09-16 14:25:35 -07001671 (CameraAFMoveCallback) mAutoFocusMoveCallback);
Michael Kolb8872c232013-01-29 10:33:22 -08001672 } else {
Angus Kong9ef99252013-07-18 18:04:19 -07001673 mCameraDevice.setAutoFocusMoveCallback(null, null);
Michael Kolb8872c232013-01-29 10:33:22 -08001674 }
1675 }
1676
1677 // We separate the parameters into several subsets, so we can update only
1678 // the subsets actually need updating. The PREFERENCE set needs extra
1679 // locking because the preference can be changed from GLThread as well.
1680 private void setCameraParameters(int updateSet) {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001681 boolean doModeSwitch = false;
1682
Michael Kolb8872c232013-01-29 10:33:22 -08001683 if ((updateSet & UPDATE_PARAM_INITIALIZE) != 0) {
1684 updateCameraParametersInitialize();
1685 }
1686
1687 if ((updateSet & UPDATE_PARAM_ZOOM) != 0) {
1688 updateCameraParametersZoom();
1689 }
1690
1691 if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001692 doModeSwitch = updateCameraParametersPreference();
Michael Kolb8872c232013-01-29 10:33:22 -08001693 }
1694
1695 mCameraDevice.setParameters(mParameters);
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001696
Sascha Haeberling9bf0fd62013-10-03 12:10:48 -07001697 // Switch to gcam module if HDR+ was selected
Angus Kong0fb819b2013-10-08 13:44:19 -07001698 if (doModeSwitch && !mIsImageCaptureIntent) {
Angus Kong13e87c42013-11-25 10:02:47 -08001699 mHandler.sendEmptyMessage(MSG_SWITCH_TO_GCAM_MODULE);
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001700 }
Michael Kolb8872c232013-01-29 10:33:22 -08001701 }
1702
1703 // If the Camera is idle, update the parameters immediately, otherwise
1704 // accumulate them in mUpdateSet and update later.
1705 private void setCameraParametersWhenIdle(int additionalUpdateSet) {
1706 mUpdateSet |= additionalUpdateSet;
1707 if (mCameraDevice == null) {
1708 // We will update all the parameters when we open the device, so
1709 // we don't need to do anything now.
1710 mUpdateSet = 0;
1711 return;
1712 } else if (isCameraIdle()) {
1713 setCameraParameters(mUpdateSet);
Michael Kolbd6954f32013-03-08 20:43:01 -08001714 updateSceneMode();
Michael Kolb8872c232013-01-29 10:33:22 -08001715 mUpdateSet = 0;
1716 } else {
Angus Kong13e87c42013-11-25 10:02:47 -08001717 if (!mHandler.hasMessages(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE)) {
1718 mHandler.sendEmptyMessageDelayed(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000);
Michael Kolb8872c232013-01-29 10:33:22 -08001719 }
1720 }
1721 }
1722
ztenghui7b265a62013-09-09 14:58:44 -07001723 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001724 public boolean isCameraIdle() {
Michael Kolb8872c232013-01-29 10:33:22 -08001725 return (mCameraState == IDLE) ||
1726 (mCameraState == PREVIEW_STOPPED) ||
1727 ((mFocusManager != null) && mFocusManager.isFocusCompleted()
1728 && (mCameraState != SWITCHING_CAMERA));
1729 }
1730
ztenghui7b265a62013-09-09 14:58:44 -07001731 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001732 public boolean isImageCaptureIntent() {
Michael Kolb8872c232013-01-29 10:33:22 -08001733 String action = mActivity.getIntent().getAction();
1734 return (MediaStore.ACTION_IMAGE_CAPTURE.equals(action)
Angus Kong6a8e8a12013-07-19 14:55:07 -07001735 || CameraActivity.ACTION_IMAGE_CAPTURE_SECURE.equals(action));
Michael Kolb8872c232013-01-29 10:33:22 -08001736 }
1737
1738 private void setupCaptureParams() {
1739 Bundle myExtras = mActivity.getIntent().getExtras();
1740 if (myExtras != null) {
1741 mSaveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT);
1742 mCropValue = myExtras.getString("crop");
1743 }
1744 }
1745
Michael Kolb8872c232013-01-29 10:33:22 -08001746 @Override
1747 public void onSharedPreferenceChanged() {
1748 // ignore the events after "onPause()"
1749 if (mPaused) return;
1750
Erin Dahlgren357b7672013-11-20 17:38:14 -08001751 SettingsController settingsController = mActivity.getSettingsController();
1752 settingsController.syncLocationManager();
Michael Kolb8872c232013-01-29 10:33:22 -08001753
1754 setCameraParametersWhenIdle(UPDATE_PARAM_PREFERENCE);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001755 mUI.updateOnScreenIndicators(mParameters);
Michael Kolb8872c232013-01-29 10:33:22 -08001756 }
1757
1758 @Override
1759 public void onCameraPickerClicked(int cameraId) {
1760 if (mPaused || mPendingSwitchCameraId != -1) return;
1761
1762 mPendingSwitchCameraId = cameraId;
Doris Liu6432cd62013-06-13 17:20:31 -07001763
1764 Log.v(TAG, "Start to switch camera. cameraId=" + cameraId);
1765 // We need to keep a preview frame for the animation before
1766 // releasing the camera. This will trigger onPreviewTextureCopied.
1767 //TODO: Need to animate the camera switch
1768 switchCamera();
Michael Kolb8872c232013-01-29 10:33:22 -08001769 }
1770
Michael Kolb8872c232013-01-29 10:33:22 -08001771 @Override
1772 public void onOverriddenPreferencesClicked() {
1773 if (mPaused) return;
Michael Kolbd6954f32013-03-08 20:43:01 -08001774 mUI.showPreferencesToast();
Michael Kolb8872c232013-01-29 10:33:22 -08001775 }
1776
1777 private void showTapToFocusToast() {
1778 // TODO: Use a toast?
1779 new RotateTextToast(mActivity, R.string.tap_to_focus, 0).show();
1780 // Clear the preference.
Erin Dahlgren357b7672013-11-20 17:38:14 -08001781 SettingsManager settingsManager = mActivity.getSettingsManager();
1782 settingsManager.setBoolean(new HintSetting(), false);
Michael Kolb8872c232013-01-29 10:33:22 -08001783 }
1784
1785 private void initializeCapabilities() {
1786 mInitialParams = mCameraDevice.getParameters();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001787 mFocusAreaSupported = CameraUtil.isFocusAreaSupported(mInitialParams);
1788 mMeteringAreaSupported = CameraUtil.isMeteringAreaSupported(mInitialParams);
1789 mAeLockSupported = CameraUtil.isAutoExposureLockSupported(mInitialParams);
1790 mAwbLockSupported = CameraUtil.isAutoWhiteBalanceLockSupported(mInitialParams);
Angus Kongdcccc512013-08-08 17:06:03 -07001791 mContinuousFocusSupported = mInitialParams.getSupportedFocusModes().contains(
Angus Kongb50b5cb2013-08-09 14:55:20 -07001792 CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE);
Michael Kolb8872c232013-01-29 10:33:22 -08001793 }
1794
Michael Kolb8872c232013-01-29 10:33:22 -08001795 @Override
1796 public void onCountDownFinished() {
1797 mSnapshotOnIdle = false;
1798 mFocusManager.doSnap();
Doris Liuda50e052013-02-07 14:36:32 -08001799 mFocusManager.onShutterUp();
Michael Kolb8872c232013-01-29 10:33:22 -08001800 }
1801
Michael Kolb8872c232013-01-29 10:33:22 -08001802 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001803 public int onZoomChanged(int index) {
1804 // Not useful to change zoom value when the activity is paused.
1805 if (mPaused) return index;
1806 mZoomValue = index;
1807 if (mParameters == null || mCameraDevice == null) return index;
1808 // Set zoom parameters asynchronously
1809 mParameters.setZoom(mZoomValue);
Angus Kong36ed8672013-04-15 12:11:15 -07001810 mCameraDevice.setParameters(mParameters);
Michael Kolbd6954f32013-03-08 20:43:01 -08001811 Parameters p = mCameraDevice.getParameters();
1812 if (p != null) return p.getZoom();
1813 return index;
Angus Kongce5480e2013-01-29 17:43:48 -08001814 }
1815
1816 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001817 public int getCameraState() {
1818 return mCameraState;
1819 }
1820
1821 @Override
1822 public void onQueueStatus(boolean full) {
1823 mUI.enableShutter(!full);
Angus Kongce5480e2013-01-29 17:43:48 -08001824 }
Angus Kong86d36312013-01-31 18:22:44 -08001825
1826 @Override
Angus Kongfd4fc0e2013-11-07 15:38:09 -08001827 public void onMediaSaverAvailable(MediaSaver s) {
Angus Kong86d36312013-01-31 18:22:44 -08001828 // We set the listener only when both service and shutterbutton
1829 // are initialized.
Michael Kolbd6954f32013-03-08 20:43:01 -08001830 if (mFirstTimeInitialized) {
Angus Kongfd4fc0e2013-11-07 15:38:09 -08001831 s.setQueueListener(this);
Michael Kolbd6954f32013-03-08 20:43:01 -08001832 }
Angus Kong86d36312013-01-31 18:22:44 -08001833 }
Angus Kong0d00a892013-03-26 11:40:40 -07001834
1835 @Override
1836 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1837 }
1838
1839 @Override
1840 public void onSensorChanged(SensorEvent event) {
1841 int type = event.sensor.getType();
1842 float[] data;
1843 if (type == Sensor.TYPE_ACCELEROMETER) {
1844 data = mGData;
1845 } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
1846 data = mMData;
1847 } else {
1848 // we should not be here.
1849 return;
1850 }
1851 for (int i = 0; i < 3 ; i++) {
1852 data[i] = event.values[i];
1853 }
1854 float[] orientation = new float[3];
1855 SensorManager.getRotationMatrix(mR, null, mGData, mMData);
1856 SensorManager.getOrientation(mR, orientation);
1857 mHeading = (int) (orientation[0] * 180f / Math.PI) % 360;
1858 if (mHeading < 0) {
1859 mHeading += 360;
1860 }
Angus Kong0d00a892013-03-26 11:40:40 -07001861 }
Doris Liu6432cd62013-06-13 17:20:31 -07001862
1863 @Override
ztenghui7b265a62013-09-09 14:58:44 -07001864 public void onPreviewFocusChanged(boolean previewFocused) {
1865 mUI.onPreviewFocusChanged(previewFocused);
Doris Liu6432cd62013-06-13 17:20:31 -07001866 }
1867
Erin Dahlgren3044d8c2013-10-10 18:23:45 -07001868 @Override
1869 public boolean arePreviewControlsVisible() {
1870 return mUI.arePreviewControlsVisible();
1871 }
1872
Ruben Brunkd217ed02013-10-08 23:31:13 -07001873 // For debugging only.
1874 public void setDebugUri(Uri uri) {
1875 mDebugUri = uri;
1876 }
1877
1878 // For debugging only.
1879 private void saveToDebugUri(byte[] data) {
1880 if (mDebugUri != null) {
1881 OutputStream outputStream = null;
1882 try {
1883 outputStream = mContentResolver.openOutputStream(mDebugUri);
1884 outputStream.write(data);
1885 outputStream.close();
1886 } catch (IOException e) {
1887 Log.e(TAG, "Exception while writing debug jpeg file", e);
1888 } finally {
1889 CameraUtil.closeSilently(outputStream);
1890 }
1891 }
1892 }
1893
Doris Liu6432cd62013-06-13 17:20:31 -07001894/* Below is no longer needed, except to get rid of compile error
1895 * TODO: Remove these
1896 */
1897
1898 // TODO: Delete this function after old camera code is removed
1899 @Override
1900 public void onRestorePreferencesClicked() {}
1901
Michael Kolb8872c232013-01-29 10:33:22 -08001902}