blob: 63265e056ba88a929fa4e4003e81d9fd340c545d [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.graphics.Bitmap;
Angus Kong454d63f2014-05-06 14:45:59 -070025import android.graphics.BitmapFactory;
Michael Kolb8872c232013-01-29 10:33:22 -080026import android.graphics.SurfaceTexture;
Angus Kong0d00a892013-03-26 11:40:40 -070027import android.hardware.Sensor;
28import android.hardware.SensorEvent;
29import android.hardware.SensorEventListener;
30import android.hardware.SensorManager;
Michael Kolb8872c232013-01-29 10:33:22 -080031import android.location.Location;
32import android.media.CameraProfile;
33import android.net.Uri;
Sascha Haeberlingbca64bf2014-04-17 10:51:53 -070034import android.os.AsyncTask;
Sascha Haeberling638e6f02013-09-18 14:28:51 -070035import android.os.Build;
Michael Kolb8872c232013-01-29 10:33:22 -080036import android.os.Bundle;
Michael Kolb8872c232013-01-29 10:33:22 -080037import android.os.Handler;
38import android.os.Looper;
39import android.os.Message;
40import android.os.MessageQueue;
41import android.os.SystemClock;
42import android.provider.MediaStore;
Michael Kolb8872c232013-01-29 10:33:22 -080043import android.view.KeyEvent;
Michael Kolb8872c232013-01-29 10:33:22 -080044import android.view.OrientationEventListener;
Michael Kolb8872c232013-01-29 10:33:22 -080045import android.view.View;
Michael Kolb8872c232013-01-29 10:33:22 -080046
Sameer Padala2c8cc452013-11-05 18:49:12 -080047import com.android.camera.PhotoModule.NamedImages.NamedEntity;
48import com.android.camera.app.AppController;
Erin Dahlgrenb1641f52014-01-14 15:58:52 -080049import com.android.camera.app.CameraAppUI;
Angus Kong0b9eb5b2014-04-30 15:03:33 -070050import com.android.camera.app.CameraProvider;
Sascha Haeberlingf9cba4e2014-04-22 09:11:28 -070051import com.android.camera.app.MediaSaver;
52import com.android.camera.app.MemoryManager;
53import com.android.camera.app.MemoryManager.MemoryListener;
Kevin Gabayanfb333362014-06-02 14:48:20 -070054import com.android.camera.app.MotionManager;
Angus Kong2bca2102014-03-11 16:27:30 -070055import com.android.camera.debug.Log;
ztenghuia16e7b52013-08-23 11:47:56 -070056import com.android.camera.exif.ExifInterface;
57import com.android.camera.exif.ExifTag;
58import com.android.camera.exif.Rational;
Erin Dahlgrenb1641f52014-01-14 15:58:52 -080059import com.android.camera.hardware.HardwareSpec;
60import com.android.camera.hardware.HardwareSpecImpl;
Angus Kong20fad242013-11-11 18:23:46 -080061import com.android.camera.module.ModuleController;
Sascha Haeberlingf2994f02014-01-23 19:55:01 -080062import com.android.camera.remote.RemoteCameraModule;
Andy Huibers90c7ad52014-05-20 22:13:54 -070063import com.android.camera.settings.CameraPictureSizesCacher;
Erin Dahlgren6190c362014-06-13 14:12:08 -070064import com.android.camera.settings.Keys;
Angus Kong454d63f2014-05-06 14:45:59 -070065import com.android.camera.settings.ResolutionUtil;
Erin Dahlgren357b7672013-11-20 17:38:14 -080066import com.android.camera.settings.SettingsManager;
Sascha Haeberling3b0ab892014-01-29 20:54:39 +010067import com.android.camera.settings.SettingsUtil;
Doris Liu6c751642014-05-05 18:43:26 -070068import com.android.camera.ui.CountDownView;
Andy Huibersb7c7d9a2014-06-18 22:26:14 -070069import com.android.camera.ui.TouchCoordinate;
Angus Kongdcccc512013-08-08 17:06:03 -070070import com.android.camera.util.ApiHelper;
Angus Kongb50b5cb2013-08-09 14:55:20 -070071import com.android.camera.util.CameraUtil;
Ruben Brunk4601f5d2013-09-24 18:35:22 -070072import com.android.camera.util.GcamHelper;
Sol Boucher44ce4b22014-08-04 23:41:38 -070073import com.android.camera.util.GservicesHelper;
Andy Huibers10c58162014-03-29 14:06:54 -070074import com.android.camera.util.SessionStatsCollector;
Sascha Haeberling8e963a52013-08-06 11:43:02 -070075import com.android.camera.util.UsageStatistics;
Doris Liudb8f9752014-05-12 15:25:13 -070076import com.android.camera.widget.AspectRatioSelector;
Sascha Haeberling8e963a52013-08-06 11:43:02 -070077import com.android.camera2.R;
Sol Boucher5a344962014-06-17 14:05:08 -070078import com.android.ex.camera2.portability.CameraAgent.CameraAFCallback;
79import com.android.ex.camera2.portability.CameraAgent.CameraAFMoveCallback;
80import com.android.ex.camera2.portability.CameraAgent.CameraPictureCallback;
81import com.android.ex.camera2.portability.CameraAgent.CameraProxy;
82import com.android.ex.camera2.portability.CameraAgent.CameraShutterCallback;
Sascha Haeberlingee36c5f2014-07-29 17:05:43 -070083import com.android.ex.camera2.portability.CameraCapabilities;
Sol Boucher43e18132014-06-19 15:03:18 -070084import com.android.ex.camera2.portability.CameraDeviceInfo.Characteristics;
Sol Boucher5a344962014-06-17 14:05:08 -070085import com.android.ex.camera2.portability.CameraSettings;
86import com.android.ex.camera2.portability.Size;
Seth Raphael5e09d012013-12-18 13:45:03 -080087import com.google.common.logging.eventprotos;
Michael Kolb8872c232013-01-29 10:33:22 -080088
Angus Kong454d63f2014-05-06 14:45:59 -070089import java.io.ByteArrayOutputStream;
Angus Kongdcccc512013-08-08 17:06:03 -070090import java.io.File;
91import java.io.FileNotFoundException;
92import java.io.FileOutputStream;
93import java.io.IOException;
94import java.io.OutputStream;
Sascha Haeberling846d3ab2014-02-04 12:48:55 +010095import java.lang.ref.WeakReference;
Angus Kong831347d2014-06-16 16:07:28 -070096import java.util.ArrayList;
Angus Kongdcccc512013-08-08 17:06:03 -070097import java.util.List;
Ruben Brunka9d66bd2013-09-06 11:56:32 -070098import java.util.Vector;
Angus Kongdcccc512013-08-08 17:06:03 -070099
Michael Kolb8872c232013-01-29 10:33:22 -0800100public class PhotoModule
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800101 extends CameraModule
102 implements PhotoController,
103 ModuleController,
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800104 MemoryListener,
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800105 FocusOverlayManager.Listener,
Erin Dahlgren7f0151d2014-01-02 16:08:12 -0800106 SensorEventListener,
Sascha Haeberlingf2994f02014-01-23 19:55:01 -0800107 SettingsManager.OnSettingChangedListener,
Doris Liu6c751642014-05-05 18:43:26 -0700108 RemoteCameraModule,
109 CountDownView.OnCountDownStatusListener {
Michael Kolb8872c232013-01-29 10:33:22 -0800110
Alan Newberger00a390e2014-08-18 18:40:47 -0700111 public static final String PHOTO_MODULE_STRING_ID = "PhotoModule";
Erin Dahlgren6190c362014-06-13 14:12:08 -0700112
113 private static final Log.Tag TAG = new Log.Tag(PHOTO_MODULE_STRING_ID);
Michael Kolb8872c232013-01-29 10:33:22 -0800114
115 // We number the request code from 1000 to avoid collision with Gallery.
116 private static final int REQUEST_CROP = 1000;
117
Angus Kong13e87c42013-11-25 10:02:47 -0800118 // Messages defined for the UI thread handler.
Angus Kong1587b042014-01-02 18:09:27 -0800119 private static final int MSG_FIRST_TIME_INIT = 1;
120 private static final int MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE = 2;
Michael Kolb8872c232013-01-29 10:33:22 -0800121
122 // The subset of parameters we need to update in setCameraParameters().
123 private static final int UPDATE_PARAM_INITIALIZE = 1;
124 private static final int UPDATE_PARAM_ZOOM = 2;
125 private static final int UPDATE_PARAM_PREFERENCE = 4;
126 private static final int UPDATE_PARAM_ALL = -1;
127
Andy Huibersdef975d2013-11-22 09:13:39 -0800128 // This is the delay before we execute onResume tasks when coming
129 // from the lock screen, to allow time for onPause to execute.
130 private static final int ON_RESUME_TASKS_DELAY_MSEC = 20;
Michael Kolb8872c232013-01-29 10:33:22 -0800131
Ruben Brunkd7488272013-10-10 18:45:53 -0700132 private static final String DEBUG_IMAGE_PREFIX = "DEBUG_";
133
Michael Kolb8872c232013-01-29 10:33:22 -0800134 private CameraActivity mActivity;
Michael Kolb8872c232013-01-29 10:33:22 -0800135 private CameraProxy mCameraDevice;
136 private int mCameraId;
Angus Kong88289042014-04-22 16:39:42 -0700137 private CameraCapabilities mCameraCapabilities;
Angus Kong6607dae2014-06-10 16:07:45 -0700138 private CameraSettings mCameraSettings;
Michael Kolb8872c232013-01-29 10:33:22 -0800139 private boolean mPaused;
Michael Kolbd6954f32013-03-08 20:43:01 -0800140
141 private PhotoUI mUI;
Michael Kolb8872c232013-01-29 10:33:22 -0800142
Michael Kolb8872c232013-01-29 10:33:22 -0800143 // The activity is going to switch to the specified camera id. This is
144 // needed because texture copy is done in GL thread. -1 means camera is not
145 // switching.
146 protected int mPendingSwitchCameraId = -1;
Michael Kolb8872c232013-01-29 10:33:22 -0800147
148 // When setCameraParametersWhenIdle() is called, we accumulate the subsets
149 // needed to be updated in mUpdateSet.
150 private int mUpdateSet;
151
Sol Boucher2192fba2014-08-19 17:24:07 -0700152 private float mZoomValue; // The current zoom ratio.
Andy Huibers547d7c82014-05-20 23:03:18 -0700153 private int mTimerDuration;
Andy Huibersb7c7d9a2014-06-18 22:26:14 -0700154 /** Set when a volume button is clicked to take photo */
155 private boolean mVolumeButtonClickedFlag = false;
Michael Kolb8872c232013-01-29 10:33:22 -0800156
Michael Kolb8872c232013-01-29 10:33:22 -0800157 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 /**
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800188 * An unpublished intent flag requesting to return as soon as capturing is
189 * completed. TODO: consider publishing by moving into MediaStore.
Michael Kolb8872c232013-01-29 10:33:22 -0800190 */
191 private static final String EXTRA_QUICK_CAPTURE =
192 "android.intent.extra.quickCapture";
193
194 // The display rotation in degrees. This is only valid when mCameraState is
195 // not PREVIEW_STOPPED.
196 private int mDisplayRotation;
197 // The value for android.hardware.Camera.setDisplayOrientation.
198 private int mCameraDisplayOrientation;
199 // The value for UI components like indicators.
200 private int mDisplayOrientation;
Angus Kong831347d2014-06-16 16:07:28 -0700201 // The value for cameradevice.CameraSettings.setPhotoRotationDegrees.
Michael Kolb8872c232013-01-29 10:33:22 -0800202 private int mJpegRotation;
Doris Liu29da2db2013-08-28 14:28:45 -0700203 // Indicates whether we are using front camera
204 private boolean mMirror;
Michael Kolb8872c232013-01-29 10:33:22 -0800205 private boolean mFirstTimeInitialized;
206 private boolean mIsImageCaptureIntent;
207
Michael Kolb8872c232013-01-29 10:33:22 -0800208 private int mCameraState = PREVIEW_STOPPED;
209 private boolean mSnapshotOnIdle = false;
210
211 private ContentResolver mContentResolver;
212
Doris Liu2b906b82013-12-10 16:34:08 -0800213 private AppController mAppController;
Michael Kolb8872c232013-01-29 10:33:22 -0800214
Michael Kolb8872c232013-01-29 10:33:22 -0800215 private final PostViewPictureCallback mPostViewPictureCallback =
216 new PostViewPictureCallback();
217 private final RawPictureCallback mRawPictureCallback =
218 new RawPictureCallback();
219 private final AutoFocusCallback mAutoFocusCallback =
220 new AutoFocusCallback();
221 private final Object mAutoFocusMoveCallback =
222 ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700223 ? new AutoFocusMoveCallback()
224 : null;
Michael Kolb8872c232013-01-29 10:33:22 -0800225
226 private final CameraErrorCallback mErrorCallback = new CameraErrorCallback();
227
228 private long mFocusStartTime;
229 private long mShutterCallbackTime;
230 private long mPostViewPictureCallbackTime;
231 private long mRawPictureCallbackTime;
232 private long mJpegPictureCallbackTime;
233 private long mOnResumeTime;
234 private byte[] mJpegImageData;
Andy Huibersb7c7d9a2014-06-18 22:26:14 -0700235 /** Touch coordinate for shutter button press. */
236 private TouchCoordinate mShutterTouchCoordinate;
237
Michael Kolb8872c232013-01-29 10:33:22 -0800238
239 // These latency time are for the CameraLatency test.
240 public long mAutoFocusTime;
241 public long mShutterLag;
242 public long mShutterToPictureDisplayedTime;
243 public long mPictureDisplayedToJpegCallbackTime;
244 public long mJpegCallbackFinishTime;
245 public long mCaptureStartTime;
246
247 // This handles everything about focus.
248 private FocusOverlayManager mFocusManager;
249
Doris Liubd1b8f92014-01-03 17:59:51 -0800250 private final int mGcamModeIndex;
Andy Huibersa31162c2014-09-02 11:27:11 -0700251 private SoundPlayer mCountdownSoundPlayer;
Doris Liubd1b8f92014-01-03 17:59:51 -0800252
Angus Kong6607dae2014-06-10 16:07:45 -0700253 private CameraCapabilities.SceneMode mSceneMode;
Michael Kolb8872c232013-01-29 10:33:22 -0800254
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100255 private final Handler mHandler = new MainHandler(this);
Erin Dahlgrenb09b53e2013-11-06 11:57:51 -0800256
Michael Kolb8872c232013-01-29 10:33:22 -0800257 private boolean mQuickCapture;
Angus Kong0d00a892013-03-26 11:40:40 -0700258 private SensorManager mSensorManager;
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800259 private final float[] mGData = new float[3];
260 private final float[] mMData = new float[3];
261 private final float[] mR = new float[16];
Angus Kong0d00a892013-03-26 11:40:40 -0700262 private int mHeading = -1;
263
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800264 /** True if all the parameters needed to start preview is ready. */
Angus Kongdcccc512013-08-08 17:06:03 -0700265 private boolean mCameraPreviewParamsReady = false;
Doris Liu6432cd62013-06-13 17:20:31 -0700266
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800267 private final MediaSaver.OnMediaSavedListener mOnMediaSavedListener =
Angus Kongfd4fc0e2013-11-07 15:38:09 -0800268 new MediaSaver.OnMediaSavedListener() {
Angus Kongce5480e2013-01-29 17:43:48 -0800269 @Override
270 public void onMediaSaved(Uri uri) {
271 if (uri != null) {
Doris Liu6432cd62013-06-13 17:20:31 -0700272 mActivity.notifyNewMedia(uri);
Angus Kongce5480e2013-01-29 17:43:48 -0800273 }
274 }
275 };
Angus Kong454d63f2014-05-06 14:45:59 -0700276 private boolean mShouldResizeTo16x9 = false;
Michael Kolb8872c232013-01-29 10:33:22 -0800277
Angus Kong0b9eb5b2014-04-30 15:03:33 -0700278 private final Runnable mResumeTaskRunnable = new Runnable() {
279 @Override
280 public void run() {
281 onResumeTasks();
282 }
283 };
284
Doris Liudb8f9752014-05-12 15:25:13 -0700285 /**
Angus Kong3699c412014-05-23 15:31:09 -0700286 * We keep the flash setting before entering scene modes (HDR)
287 * and restore it after HDR is off.
288 */
289 private String mFlashModeBeforeSceneMode;
290
291 /**
Doris Liudb8f9752014-05-12 15:25:13 -0700292 * This callback gets called when user select whether or not to
293 * turn on geo-tagging.
294 */
295 public interface LocationDialogCallback {
296 /**
297 * Gets called after user selected/unselected geo-tagging feature.
298 *
299 * @param selected whether or not geo-tagging feature is selected
300 */
301 public void onLocationTaggingSelected(boolean selected);
302 }
303
304 /**
305 * This callback defines the text that is shown in the aspect ratio selection
306 * dialog, provides the current aspect ratio, and gets notified when user changes
307 * aspect ratio selection in the dialog.
308 */
309 public interface AspectRatioDialogCallback {
310 /**
Doris Liudb8f9752014-05-12 15:25:13 -0700311 * Returns current aspect ratio that is being used to set as default.
312 */
313 public AspectRatioSelector.AspectRatio getCurrentAspectRatio();
314
315 /**
316 * Gets notified when user has made the aspect ratio selection.
317 *
318 * @param newAspectRatio aspect ratio that user has selected
Doris Liu08b0cdd2014-05-13 19:19:55 -0700319 * @param dialogHandlingFinishedRunnable runnable to run when the operations
320 * needed to handle changes from dialog
321 * are finished.
Doris Liudb8f9752014-05-12 15:25:13 -0700322 */
Doris Liu08b0cdd2014-05-13 19:19:55 -0700323 public void onAspectRatioSelected(AspectRatioSelector.AspectRatio newAspectRatio,
324 Runnable dialogHandlingFinishedRunnable);
Doris Liudb8f9752014-05-12 15:25:13 -0700325 }
326
Angus Kongdcccc512013-08-08 17:06:03 -0700327 private void checkDisplayRotation() {
328 // Set the display orientation if display rotation has changed.
329 // Sometimes this happens when the device is held upside
330 // down and camera app is opened. Rotation animation will
331 // take some time and the rotation value we have got may be
332 // wrong. Framework does not have a callback for this now.
333 if (CameraUtil.getDisplayRotation(mActivity) != mDisplayRotation) {
334 setDisplayOrientation();
Michael Kolb8872c232013-01-29 10:33:22 -0800335 }
Angus Kongdcccc512013-08-08 17:06:03 -0700336 if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) {
337 mHandler.postDelayed(new Runnable() {
338 @Override
339 public void run() {
340 checkDisplayRotation();
341 }
342 }, 100);
Michael Kolb8872c232013-01-29 10:33:22 -0800343 }
344 }
345
346 /**
347 * This Handler is used to post message back onto the main thread of the
348 * application
349 */
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100350 private static class MainHandler extends Handler {
351 private final WeakReference<PhotoModule> mModule;
352
353 public MainHandler(PhotoModule module) {
Erin Dahlgrenb09b53e2013-11-06 11:57:51 -0800354 super(Looper.getMainLooper());
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100355 mModule = new WeakReference<PhotoModule>(module);
Erin Dahlgrenb09b53e2013-11-06 11:57:51 -0800356 }
357
Michael Kolb8872c232013-01-29 10:33:22 -0800358 @Override
359 public void handleMessage(Message msg) {
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100360 PhotoModule module = mModule.get();
361 if (module == null) {
362 return;
363 }
Michael Kolb8872c232013-01-29 10:33:22 -0800364 switch (msg.what) {
Angus Kong13e87c42013-11-25 10:02:47 -0800365 case MSG_FIRST_TIME_INIT: {
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100366 module.initializeFirstTime();
Michael Kolb8872c232013-01-29 10:33:22 -0800367 break;
368 }
369
Angus Kong13e87c42013-11-25 10:02:47 -0800370 case MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE: {
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100371 module.setCameraParametersWhenIdle(0);
Michael Kolb8872c232013-01-29 10:33:22 -0800372 break;
373 }
Michael Kolb8872c232013-01-29 10:33:22 -0800374 }
375 }
376 }
377
Erin Dahlgren4cdaf512014-03-19 12:48:30 -0700378 private void switchToGcamCapture() {
379 if (mActivity != null && mGcamModeIndex != 0) {
380 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren6190c362014-06-13 14:12:08 -0700381 settingsManager.set(SettingsManager.SCOPE_GLOBAL,
382 Keys.KEY_CAMERA_HDR_PLUS, true);
Erin Dahlgren4cdaf512014-03-19 12:48:30 -0700383
384 // Disable the HDR+ button to prevent callbacks from being
385 // queued before the correct callback is attached to the button
386 // in the new module. The new module will set the enabled/disabled
387 // of this button when the module's preferred camera becomes available.
388 ButtonManager buttonManager = mActivity.getButtonManager();
Spike Sprague66d3d0d2014-08-18 14:11:33 -0700389
390 buttonManager.disableButtonClick(ButtonManager.BUTTON_HDR_PLUS);
Erin Dahlgren4cdaf512014-03-19 12:48:30 -0700391
Seth Raphael274f6e92014-05-21 17:11:53 -0700392 mAppController.getCameraAppUI().freezeScreenUntilPreviewReady();
393
Erin Dahlgren4cdaf512014-03-19 12:48:30 -0700394 // Do not post this to avoid this module switch getting interleaved with
395 // other button callbacks.
396 mActivity.onModeSelected(mGcamModeIndex);
Spike Sprague66d3d0d2014-08-18 14:11:33 -0700397
398 buttonManager.enableButtonClick(ButtonManager.BUTTON_HDR_PLUS);
Erin Dahlgren4cdaf512014-03-19 12:48:30 -0700399 }
400 }
401
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800402 /**
403 * Constructs a new photo module.
404 */
Angus Kongc4e66562013-11-22 23:03:21 -0800405 public PhotoModule(AppController app) {
406 super(app);
Doris Liubd1b8f92014-01-03 17:59:51 -0800407 mGcamModeIndex = app.getAndroidContext().getResources()
408 .getInteger(R.integer.camera_mode_gcam);
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800409 }
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700410
Michael Kolb8872c232013-01-29 10:33:22 -0800411 @Override
Spike Sprague159e6e92014-05-27 18:26:30 -0700412 public String getPeekAccessibilityString() {
413 return mAppController.getAndroidContext()
414 .getResources().getString(R.string.photo_accessibility_peek);
415 }
416
417 @Override
Erin Dahlgren6190c362014-06-13 14:12:08 -0700418 public String getModuleStringIdentifier() {
419 return PHOTO_MODULE_STRING_ID;
420 }
421
422 @Override
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100423 public void init(CameraActivity activity, boolean isSecureCamera, boolean isCaptureIntent) {
424 mActivity = activity;
425 // TODO: Need to look at the controller interface to see if we can get
426 // rid of passing in the activity directly.
427 mAppController = mActivity;
428
429 mUI = new PhotoUI(mActivity, this, mActivity.getModuleLayoutRoot());
430 mActivity.setPreviewStatusListener(mUI);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800431
432 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren6190c362014-06-13 14:12:08 -0700433 mCameraId = settingsManager.getInteger(mAppController.getModuleScope(),
434 Keys.KEY_CAMERA_ID);
Michael Kolb8872c232013-01-29 10:33:22 -0800435
Doris Liudb8f9752014-05-12 15:25:13 -0700436 // TODO: Move this to SettingsManager as a part of upgrade procedure.
Erin Dahlgren6190c362014-06-13 14:12:08 -0700437 if (!settingsManager.getBoolean(SettingsManager.SCOPE_GLOBAL,
438 Keys.KEY_USER_SELECTED_ASPECT_RATIO)) {
Doris Liudb8f9752014-05-12 15:25:13 -0700439 // Switch to back camera to set aspect ratio.
Erin Dahlgren6190c362014-06-13 14:12:08 -0700440 mCameraId = settingsManager.getIntegerDefault(Keys.KEY_CAMERA_ID);
Doris Liudb8f9752014-05-12 15:25:13 -0700441 }
442
Michael Kolb8872c232013-01-29 10:33:22 -0800443 mContentResolver = mActivity.getContentResolver();
444
Michael Kolb8872c232013-01-29 10:33:22 -0800445 // Surface texture is from camera screen nail and startPreview needs it.
446 // This must be done before startPreview.
447 mIsImageCaptureIntent = isImageCaptureIntent();
Michael Kolb8872c232013-01-29 10:33:22 -0800448
Michael Kolb8872c232013-01-29 10:33:22 -0800449 mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800450 mSensorManager = (SensorManager) (mActivity.getSystemService(Context.SENSOR_SERVICE));
Doris Liu6c751642014-05-05 18:43:26 -0700451 mUI.setCountdownFinishedListener(this);
Andy Huibersa31162c2014-09-02 11:27:11 -0700452 mCountdownSoundPlayer = new SoundPlayer(mAppController.getAndroidContext());
Doris Liu6c751642014-05-05 18:43:26 -0700453
454 // TODO: Make this a part of app controller API.
455 View cancelButton = mActivity.findViewById(R.id.shutter_cancel_button);
456 cancelButton.setOnClickListener(new View.OnClickListener() {
457 @Override
458 public void onClick(View view) {
459 cancelCountDown();
460 }
461 });
462 }
463
464 private void cancelCountDown() {
465 if (mUI.isCountingDown()) {
466 // Cancel on-going countdown.
467 mUI.cancelCountDown();
468 }
469 mAppController.getCameraAppUI().transitionToCapture();
470 mAppController.getCameraAppUI().showModeOptions();
Michael Kolbd6954f32013-03-08 20:43:01 -0800471 }
472
Erin Dahlgren4efa8b52013-12-17 18:31:35 -0800473 @Override
474 public boolean isUsingBottomBar() {
475 return true;
476 }
477
Michael Kolbd6954f32013-03-08 20:43:01 -0800478 private void initializeControlByIntent() {
Michael Kolbd6954f32013-03-08 20:43:01 -0800479 if (mIsImageCaptureIntent) {
Spike Sprague15690d02014-02-10 12:11:20 -0800480 mActivity.getCameraAppUI().transitionToIntentCaptureLayout();
Michael Kolbd6954f32013-03-08 20:43:01 -0800481 setupCaptureParams();
482 }
483 }
484
485 private void onPreviewStarted() {
Doris Liu2b906b82013-12-10 16:34:08 -0800486 mAppController.onPreviewStarted();
Michael Kolbd6954f32013-03-08 20:43:01 -0800487 setCameraState(IDLE);
Michael Kolbd6954f32013-03-08 20:43:01 -0800488 startFaceDetection();
Doris Liudb8f9752014-05-12 15:25:13 -0700489 settingsFirstRun();
Seth Raphael30836422014-02-06 14:49:47 -0800490 }
491
Doris Liudb8f9752014-05-12 15:25:13 -0700492 /**
493 * Prompt the user to pick to record location and choose aspect ratio for the
494 * very first run of camera only.
495 */
496 private void settingsFirstRun() {
497 final SettingsManager settingsManager = mActivity.getSettingsManager();
Sascha Haeberlingde303232014-02-07 02:30:53 +0100498
Doris Liudb8f9752014-05-12 15:25:13 -0700499 if (mActivity.isSecureCamera() || isImageCaptureIntent()) {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800500 return;
Michael Kolb8872c232013-01-29 10:33:22 -0800501 }
Doris Liudb8f9752014-05-12 15:25:13 -0700502
Erin Dahlgren6190c362014-06-13 14:12:08 -0700503 boolean locationPrompt = !settingsManager.isSet(SettingsManager.SCOPE_GLOBAL,
504 Keys.KEY_RECORD_LOCATION);
Doris Liudb8f9752014-05-12 15:25:13 -0700505 boolean aspectRatioPrompt = !settingsManager.getBoolean(
Erin Dahlgren6190c362014-06-13 14:12:08 -0700506 SettingsManager.SCOPE_GLOBAL, Keys.KEY_USER_SELECTED_ASPECT_RATIO);
Doris Liudb8f9752014-05-12 15:25:13 -0700507 if (!locationPrompt && !aspectRatioPrompt) {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800508 return;
509 }
Doris Liudb8f9752014-05-12 15:25:13 -0700510
Michael Kolb8872c232013-01-29 10:33:22 -0800511 // Check if the back camera exists
Angus Kongd74e6a12014-01-07 11:29:44 -0800512 int backCameraId = mAppController.getCameraProvider().getFirstBackCameraId();
Michael Kolb8872c232013-01-29 10:33:22 -0800513 if (backCameraId == -1) {
514 // If there is no back camera, do not show the prompt.
515 return;
516 }
Doris Liudb8f9752014-05-12 15:25:13 -0700517
518 if (locationPrompt) {
519 // Show both location and aspect ratio selection dialog.
520 mUI.showLocationAndAspectRatioDialog(new LocationDialogCallback(){
Sascha Haeberlingb32af4f2014-05-16 08:22:00 -0700521 @Override
Doris Liudb8f9752014-05-12 15:25:13 -0700522 public void onLocationTaggingSelected(boolean selected) {
Erin Dahlgren6190c362014-06-13 14:12:08 -0700523 Keys.setLocation(mActivity.getSettingsManager(), selected,
524 mActivity.getLocationManager());
Doris Liudb8f9752014-05-12 15:25:13 -0700525 }
526 }, createAspectRatioDialogCallback());
527 } else {
528 // App upgrade. Only show aspect ratio selection.
Sascha Haeberling4c1bffe2014-08-21 10:01:00 -0700529 boolean wasShown = mUI.showAspectRatioDialog(createAspectRatioDialogCallback());
530 if (!wasShown) {
531 // If the dialog was not shown, set this flag to true so that we
532 // never have to check for it again. It means that we don't need
533 // to show the dialog on this device.
534 mActivity.getSettingsManager().set(SettingsManager.SCOPE_GLOBAL,
535 Keys.KEY_USER_SELECTED_ASPECT_RATIO, true);
536 }
Doris Liudb8f9752014-05-12 15:25:13 -0700537 }
538 }
539
540 private AspectRatioDialogCallback createAspectRatioDialogCallback() {
Angus Kong6607dae2014-06-10 16:07:45 -0700541 Size currentSize = mCameraSettings.getCurrentPhotoSize();
Doris Liudb8f9752014-05-12 15:25:13 -0700542 float aspectRatio = (float) currentSize.width() / (float) currentSize.height();
543 if (aspectRatio < 1f) {
544 aspectRatio = 1 / aspectRatio;
545 }
546 final AspectRatioSelector.AspectRatio currentAspectRatio;
547 if (Math.abs(aspectRatio - 4f / 3f) <= 0.1f) {
548 currentAspectRatio = AspectRatioSelector.AspectRatio.ASPECT_RATIO_4x3;
549 } else if (Math.abs(aspectRatio - 16f / 9f) <= 0.1f) {
550 currentAspectRatio = AspectRatioSelector.AspectRatio.ASPECT_RATIO_16x9;
551 } else {
552 // TODO: Log error and not show dialog.
553 return null;
554 }
555
Angus Kong6607dae2014-06-10 16:07:45 -0700556 List<Size> sizes = mCameraCapabilities.getSupportedPhotoSizes();
Doris Liudb8f9752014-05-12 15:25:13 -0700557 List<Size> pictureSizes = ResolutionUtil
558 .getDisplayableSizesFromSupported(sizes, true);
559
560 // This logic below finds the largest resolution for each aspect ratio.
561 // TODO: Move this somewhere that can be shared with SettingsActivity
562 int aspectRatio4x3Resolution = 0;
563 int aspectRatio16x9Resolution = 0;
564 Size largestSize4x3 = new Size(0, 0);
565 Size largestSize16x9 = new Size(0, 0);
566 for (Size size : pictureSizes) {
567 float pictureAspectRatio = (float) size.width() / (float) size.height();
568 pictureAspectRatio = pictureAspectRatio < 1 ?
569 1f / pictureAspectRatio : pictureAspectRatio;
570 int resolution = size.width() * size.height();
571 if (Math.abs(pictureAspectRatio - 4f / 3f) < 0.1f) {
572 if (resolution > aspectRatio4x3Resolution) {
573 aspectRatio4x3Resolution = resolution;
574 largestSize4x3 = size;
575 }
576 } else if (Math.abs(pictureAspectRatio - 16f / 9f) < 0.1f) {
577 if (resolution > aspectRatio16x9Resolution) {
578 aspectRatio16x9Resolution = resolution;
579 largestSize16x9 = size;
580 }
581 }
582 }
Doris Liudb8f9752014-05-12 15:25:13 -0700583
Doris Liu08b0cdd2014-05-13 19:19:55 -0700584 // Use the largest 4x3 and 16x9 sizes as candidates for picture size selection.
585 final Size size4x3ToSelect = largestSize4x3;
586 final Size size16x9ToSelect = largestSize16x9;
Doris Liudb8f9752014-05-12 15:25:13 -0700587
Doris Liudb8f9752014-05-12 15:25:13 -0700588 AspectRatioDialogCallback callback = new AspectRatioDialogCallback() {
Doris Liudb8f9752014-05-12 15:25:13 -0700589
590 @Override
591 public AspectRatioSelector.AspectRatio getCurrentAspectRatio() {
592 return currentAspectRatio;
593 }
594
595 @Override
Doris Liu08b0cdd2014-05-13 19:19:55 -0700596 public void onAspectRatioSelected(AspectRatioSelector.AspectRatio newAspectRatio,
597 Runnable dialogHandlingFinishedRunnable) {
Doris Liudb8f9752014-05-12 15:25:13 -0700598 if (newAspectRatio == AspectRatioSelector.AspectRatio.ASPECT_RATIO_4x3) {
Doris Liu08b0cdd2014-05-13 19:19:55 -0700599 String largestSize4x3Text = SettingsUtil.sizeToSetting(size4x3ToSelect);
Erin Dahlgren6190c362014-06-13 14:12:08 -0700600 mActivity.getSettingsManager().set(SettingsManager.SCOPE_GLOBAL,
601 Keys.KEY_PICTURE_SIZE_BACK,
602 largestSize4x3Text);
Doris Liudb8f9752014-05-12 15:25:13 -0700603 } else if (newAspectRatio == AspectRatioSelector.AspectRatio.ASPECT_RATIO_16x9) {
Doris Liu08b0cdd2014-05-13 19:19:55 -0700604 String largestSize16x9Text = SettingsUtil.sizeToSetting(size16x9ToSelect);
Erin Dahlgren6190c362014-06-13 14:12:08 -0700605 mActivity.getSettingsManager().set(SettingsManager.SCOPE_GLOBAL,
606 Keys.KEY_PICTURE_SIZE_BACK,
607 largestSize16x9Text);
Doris Liudb8f9752014-05-12 15:25:13 -0700608 }
Erin Dahlgren6190c362014-06-13 14:12:08 -0700609 mActivity.getSettingsManager().set(SettingsManager.SCOPE_GLOBAL,
610 Keys.KEY_USER_SELECTED_ASPECT_RATIO, true);
611 String aspectRatio = mActivity.getSettingsManager().getString(
612 SettingsManager.SCOPE_GLOBAL,
613 Keys.KEY_USER_SELECTED_ASPECT_RATIO);
614 Log.e(TAG, "aspect ratio after setting it to true=" + aspectRatio);
Doris Liudb8f9752014-05-12 15:25:13 -0700615 if (newAspectRatio != currentAspectRatio) {
Doris Liudb8f9752014-05-12 15:25:13 -0700616 stopPreview();
617 startPreview();
Doris Liu08b0cdd2014-05-13 19:19:55 -0700618 mUI.setRunnableForNextFrame(dialogHandlingFinishedRunnable);
619 } else {
620 mHandler.post(dialogHandlingFinishedRunnable);
Doris Liudb8f9752014-05-12 15:25:13 -0700621 }
622 }
623 };
624 return callback;
Doris Liu6a83d522013-07-02 12:03:32 -0700625 }
Michael Kolb8872c232013-01-29 10:33:22 -0800626
ztenghui7b265a62013-09-09 14:58:44 -0700627 @Override
Angus Kongdcccc512013-08-08 17:06:03 -0700628 public void onPreviewUIReady() {
Erin Dahlgrendc282e12013-11-12 09:39:08 -0800629 startPreview();
Angus Kongdcccc512013-08-08 17:06:03 -0700630 }
631
632 @Override
633 public void onPreviewUIDestroyed() {
634 if (mCameraDevice == null) {
635 return;
636 }
637 mCameraDevice.setPreviewTexture(null);
638 stopPreview();
639 }
640
Doris Liu1dfe7822013-12-12 00:02:08 -0800641 @Override
642 public void startPreCaptureAnimation() {
643 mAppController.startPreCaptureAnimation();
644 }
645
Michael Kolbd6954f32013-03-08 20:43:01 -0800646 private void onCameraOpened() {
Michael Kolbd6954f32013-03-08 20:43:01 -0800647 openCameraCommon();
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800648 initializeControlByIntent();
Michael Kolb8872c232013-01-29 10:33:22 -0800649 }
650
Michael Kolbd6954f32013-03-08 20:43:01 -0800651 private void switchCamera() {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800652 if (mPaused) {
653 return;
654 }
Doris Liu6c751642014-05-05 18:43:26 -0700655 cancelCountDown();
Doris Liu6809eb82014-05-21 13:16:59 -0700656
657 mAppController.freezeScreenUntilPreviewReady();
Erin Dahlgren357b7672013-11-20 17:38:14 -0800658 SettingsManager settingsManager = mActivity.getSettingsManager();
Michael Kolbd6954f32013-03-08 20:43:01 -0800659
Alan Newbergerd41766f2014-04-09 18:25:34 -0700660 Log.i(TAG, "Start to switch camera. id=" + mPendingSwitchCameraId);
Angus Kongd4109bc2014-01-08 18:38:03 -0800661 closeCamera();
Michael Kolbd6954f32013-03-08 20:43:01 -0800662 mCameraId = mPendingSwitchCameraId;
Sascha Haeberling4c1bffe2014-08-21 10:01:00 -0700663
Erin Dahlgren6190c362014-06-13 14:12:08 -0700664 settingsManager.set(mAppController.getModuleScope(), Keys.KEY_CAMERA_ID, mCameraId);
Sol Boucher44ce4b22014-08-04 23:41:38 -0700665 requestCameraOpen();
Michael Kolbd6954f32013-03-08 20:43:01 -0800666 mUI.clearFaces();
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800667 if (mFocusManager != null) {
668 mFocusManager.removeMessages();
669 }
Michael Kolbd6954f32013-03-08 20:43:01 -0800670
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700671 mMirror = isCameraFrontFacing();
Doris Liu29da2db2013-08-28 14:28:45 -0700672 mFocusManager.setMirror(mMirror);
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700673 // Start switch camera animation. Post a message because
674 // onFrameAvailable from the old camera may already exist.
Doris Liu48239f42013-03-04 22:19:10 -0800675 }
676
Sol Boucher44ce4b22014-08-04 23:41:38 -0700677 /**
678 * Uses the {@link CameraProvider} to open the currently-selected camera
679 * device, using {@link GservicesHelper} to choose between API-1 and API-2.
680 */
681 private void requestCameraOpen() {
682 mActivity.getCameraProvider().requestCamera(mCameraId,
683 GservicesHelper.useCamera2ApiThroughPortabilityLayer(mActivity));
684 }
685
Erin Dahlgren0a6a8d82014-01-09 22:17:38 -0800686 private final ButtonManager.ButtonCallback mCameraCallback =
Sascha Haeberlingde303232014-02-07 02:30:53 +0100687 new ButtonManager.ButtonCallback() {
688 @Override
689 public void onStateChanged(int state) {
Erin Dahlgren1ba90e32014-03-03 12:05:57 -0800690 // At the time this callback is fired, the camera id
691 // has be set to the desired camera.
692
Angus Kong97e282a2014-03-04 18:44:49 -0800693 if (mPaused || mAppController.getCameraProvider().waitingForCamera()) {
Sascha Haeberlingde303232014-02-07 02:30:53 +0100694 return;
695 }
Erin Dahlgren1ba90e32014-03-03 12:05:57 -0800696 // If switching to back camera, and HDR+ is still on,
697 // switch back to gcam, otherwise handle callback normally.
698 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren6190c362014-06-13 14:12:08 -0700699 if (Keys.isCameraBackFacing(settingsManager,
700 mAppController.getModuleScope())) {
701 if (Keys.requestsReturnToHdrPlus(settingsManager,
702 mAppController.getModuleScope())) {
Erin Dahlgren4cdaf512014-03-19 12:48:30 -0700703 switchToGcamCapture();
Erin Dahlgren1ba90e32014-03-03 12:05:57 -0800704 return;
705 }
706 }
707
Sascha Haeberlingde303232014-02-07 02:30:53 +0100708 mPendingSwitchCameraId = state;
Erin Dahlgren18e2ef62013-12-05 14:53:38 -0800709
Alan Newbergerd41766f2014-04-09 18:25:34 -0700710 Log.d(TAG, "Start to switch camera. cameraId=" + state);
Sascha Haeberlingde303232014-02-07 02:30:53 +0100711 // We need to keep a preview frame for the animation before
712 // releasing the camera. This will trigger
713 // onPreviewTextureCopied.
714 // TODO: Need to animate the camera switch
715 switchCamera();
716 }
717 };
Erin Dahlgren18e2ef62013-12-05 14:53:38 -0800718
Erin Dahlgren0a6a8d82014-01-09 22:17:38 -0800719 private final ButtonManager.ButtonCallback mHdrPlusCallback =
Sascha Haeberlingde303232014-02-07 02:30:53 +0100720 new ButtonManager.ButtonCallback() {
721 @Override
722 public void onStateChanged(int state) {
Doris Liu8ad8ad42014-03-27 14:43:31 -0700723 SettingsManager settingsManager = mActivity.getSettingsManager();
Sascha Haeberling4c1bffe2014-08-21 10:01:00 -0700724 if (GcamHelper.hasGcamAsSeparateModule()) {
Sascha Haeberlingde303232014-02-07 02:30:53 +0100725 // Set the camera setting to default backfacing.
Erin Dahlgren6190c362014-06-13 14:12:08 -0700726 settingsManager.setToDefault(mAppController.getModuleScope(),
727 Keys.KEY_CAMERA_ID);
Erin Dahlgren4cdaf512014-03-19 12:48:30 -0700728 switchToGcamCapture();
Sascha Haeberlingde303232014-02-07 02:30:53 +0100729 } else {
Erin Dahlgren6190c362014-06-13 14:12:08 -0700730 if (Keys.isHdrOn(settingsManager)) {
Angus Kong831347d2014-06-16 16:07:28 -0700731 settingsManager.set(mAppController.getCameraScope(), Keys.KEY_SCENE_MODE,
732 mCameraCapabilities.getStringifier().stringify(
733 CameraCapabilities.SceneMode.HDR));
Doris Liu8ad8ad42014-03-27 14:43:31 -0700734 } else {
Angus Kong831347d2014-06-16 16:07:28 -0700735 settingsManager.set(mAppController.getCameraScope(), Keys.KEY_SCENE_MODE,
736 mCameraCapabilities.getStringifier().stringify(
737 CameraCapabilities.SceneMode.AUTO));
Doris Liu8ad8ad42014-03-27 14:43:31 -0700738 }
Sascha Haeberlingde303232014-02-07 02:30:53 +0100739 updateParametersSceneMode();
Angus Kong6607dae2014-06-10 16:07:45 -0700740 mCameraDevice.applySettings(mCameraSettings);
Doris Liu8ad8ad42014-03-27 14:43:31 -0700741 updateSceneMode();
Sascha Haeberlingde303232014-02-07 02:30:53 +0100742 }
Erin Dahlgrenba6994d2013-12-12 16:04:38 -0800743 }
Sascha Haeberlingde303232014-02-07 02:30:53 +0100744 };
Erin Dahlgren18e2ef62013-12-05 14:53:38 -0800745
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800746 private final View.OnClickListener mCancelCallback = new View.OnClickListener() {
747 @Override
748 public void onClick(View v) {
749 onCaptureCancelled();
750 }
751 };
752
753 private final View.OnClickListener mDoneCallback = new View.OnClickListener() {
754 @Override
755 public void onClick(View v) {
756 onCaptureDone();
757 }
758 };
759
760 private final View.OnClickListener mRetakeCallback = new View.OnClickListener() {
761 @Override
762 public void onClick(View v) {
Spike Sprague15690d02014-02-10 12:11:20 -0800763 mActivity.getCameraAppUI().transitionToIntentCaptureLayout();
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800764 onCaptureRetake();
765 }
766 };
767
Erin Dahlgren0a6a8d82014-01-09 22:17:38 -0800768 @Override
Erin Dahlgren1ca516f2014-03-28 12:44:04 -0700769 public void hardResetSettings(SettingsManager settingsManager) {
Erin Dahlgrene1547932014-06-23 15:17:07 -0700770 // PhotoModule should hard reset HDR+ to off,
771 // and HDR to off if HDR+ is supported.
Erin Dahlgren6190c362014-06-13 14:12:08 -0700772 settingsManager.set(SettingsManager.SCOPE_GLOBAL, Keys.KEY_CAMERA_HDR_PLUS, false);
Sascha Haeberling4c1bffe2014-08-21 10:01:00 -0700773 if (GcamHelper.hasGcamAsSeparateModule()) {
Erin Dahlgren21a62362014-06-24 10:13:01 -0700774 settingsManager.set(SettingsManager.SCOPE_GLOBAL, Keys.KEY_CAMERA_HDR, false);
Erin Dahlgrene1547932014-06-23 15:17:07 -0700775 }
Erin Dahlgren1ca516f2014-03-28 12:44:04 -0700776 }
777
778 @Override
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800779 public HardwareSpec getHardwareSpec() {
Angus Kong831347d2014-06-16 16:07:28 -0700780 return (mCameraSettings != null ?
781 new HardwareSpecImpl(getCameraProvider(), mCameraCapabilities) : null);
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800782 }
783
784 @Override
785 public CameraAppUI.BottomBarUISpec getBottomBarSpec() {
786 CameraAppUI.BottomBarUISpec bottomBarSpec = new CameraAppUI.BottomBarUISpec();
787
788 bottomBarSpec.enableCamera = true;
789 bottomBarSpec.cameraCallback = mCameraCallback;
Erin Dahlgren6190c362014-06-13 14:12:08 -0700790 bottomBarSpec.enableFlash = !mAppController.getSettingsManager()
791 .getBoolean(SettingsManager.SCOPE_GLOBAL, Keys.KEY_CAMERA_HDR);
Erin Dahlgrena6587a12014-02-03 13:24:55 -0800792 bottomBarSpec.enableHdr = true;
793 bottomBarSpec.hdrCallback = mHdrPlusCallback;
Erin Dahlgrend5e51462014-02-07 12:38:57 -0800794 bottomBarSpec.enableGridLines = true;
Spike Sprague8f7425c2014-05-12 11:08:34 -0700795 if (mCameraCapabilities != null) {
796 bottomBarSpec.enableExposureCompensation = true;
797 bottomBarSpec.exposureCompensationSetCallback =
798 new CameraAppUI.BottomBarUISpec.ExposureCompensationSetCallback() {
799 @Override
800 public void setExposure(int value) {
801 setExposureCompensation(value);
802 }
803 };
804 bottomBarSpec.minExposureCompensation =
805 mCameraCapabilities.getMinExposureCompensation();
806 bottomBarSpec.maxExposureCompensation =
807 mCameraCapabilities.getMaxExposureCompensation();
808 bottomBarSpec.exposureCompensationStep =
809 mCameraCapabilities.getExposureCompensationStep();
810 }
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800811
Doris Liu6c751642014-05-05 18:43:26 -0700812 bottomBarSpec.enableSelfTimer = true;
Sascha Haeberling4333fac2014-05-20 16:28:11 -0700813 bottomBarSpec.showSelfTimer = true;
Doris Liu6c751642014-05-05 18:43:26 -0700814
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800815 if (isImageCaptureIntent()) {
816 bottomBarSpec.showCancel = true;
817 bottomBarSpec.cancelCallback = mCancelCallback;
818 bottomBarSpec.showDone = true;
819 bottomBarSpec.doneCallback = mDoneCallback;
820 bottomBarSpec.showRetake = true;
821 bottomBarSpec.retakeCallback = mRetakeCallback;
822 }
823
824 return bottomBarSpec;
Erin Dahlgren0a6a8d82014-01-09 22:17:38 -0800825 }
826
Michael Kolbd6954f32013-03-08 20:43:01 -0800827 // either open a new camera or switch cameras
828 private void openCameraCommon() {
Angus Kong6607dae2014-06-10 16:07:45 -0700829 mUI.onCameraOpened(mCameraCapabilities, mCameraSettings);
Angus Kong0fb819b2013-10-08 13:44:19 -0700830 if (mIsImageCaptureIntent) {
Erin Dahlgrene419b192013-12-03 13:10:27 -0800831 // Set hdr plus to default: off.
832 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren6190c362014-06-13 14:12:08 -0700833 settingsManager.setToDefault(SettingsManager.SCOPE_GLOBAL,
834 Keys.KEY_CAMERA_HDR_PLUS);
Angus Kong0fb819b2013-10-08 13:44:19 -0700835 }
Michael Kolbd6954f32013-03-08 20:43:01 -0800836 updateSceneMode();
Michael Kolb8872c232013-01-29 10:33:22 -0800837 }
838
ztenghui7b265a62013-09-09 14:58:44 -0700839 @Override
Doris Liu70da9182013-12-17 18:41:15 -0800840 public void updatePreviewAspectRatio(float aspectRatio) {
841 mAppController.updatePreviewAspectRatio(aspectRatio);
842 }
843
Michael Kolb8872c232013-01-29 10:33:22 -0800844 private void resetExposureCompensation() {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800845 SettingsManager settingsManager = mActivity.getSettingsManager();
846 if (settingsManager == null) {
847 Log.e(TAG, "Settings manager is null!");
848 return;
Michael Kolb8872c232013-01-29 10:33:22 -0800849 }
Erin Dahlgren6190c362014-06-13 14:12:08 -0700850 settingsManager.setToDefault(mAppController.getCameraScope(),
851 Keys.KEY_EXPOSURE);
Michael Kolb8872c232013-01-29 10:33:22 -0800852 }
853
Michael Kolb8872c232013-01-29 10:33:22 -0800854 // Snapshots can only be taken after this is called. It should be called
855 // once only. We could have done these things in onCreate() but we want to
856 // make preview screen appear as soon as possible.
857 private void initializeFirstTime() {
Sascha Haeberling330dafb2013-10-10 19:00:41 -0700858 if (mFirstTimeInitialized || mPaused) {
859 return;
860 }
Michael Kolb8872c232013-01-29 10:33:22 -0800861
Michael Kolbd6954f32013-03-08 20:43:01 -0800862 mUI.initializeFirstTime();
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800863
Angus Kong86d36312013-01-31 18:22:44 -0800864 // We set the listener only when both service and shutterbutton
865 // are initialized.
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800866 getServices().getMemoryManager().addListener(this);
Michael Kolb8872c232013-01-29 10:33:22 -0800867
Michael Kolb8872c232013-01-29 10:33:22 -0800868 mNamedImages = new NamedImages();
869
870 mFirstTimeInitialized = true;
871 addIdleHandler();
872
Spike Spraguee6374b72014-04-25 17:24:32 -0700873 mActivity.updateStorageSpaceAndHint(null);
Michael Kolb8872c232013-01-29 10:33:22 -0800874 }
875
Michael Kolbd6954f32013-03-08 20:43:01 -0800876 // If the activity is paused and resumed, this method will be called in
877 // onResume.
878 private void initializeSecondTime() {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800879 getServices().getMemoryManager().addListener(this);
Michael Kolbd6954f32013-03-08 20:43:01 -0800880 mNamedImages = new NamedImages();
Angus Kong6607dae2014-06-10 16:07:45 -0700881 mUI.initializeSecondTime(mCameraCapabilities, mCameraSettings);
Michael Kolbd6954f32013-03-08 20:43:01 -0800882 }
883
Michael Kolb8872c232013-01-29 10:33:22 -0800884 private void addIdleHandler() {
885 MessageQueue queue = Looper.myQueue();
886 queue.addIdleHandler(new MessageQueue.IdleHandler() {
887 @Override
888 public boolean queueIdle() {
889 Storage.ensureOSXCompatible();
890 return false;
891 }
892 });
893 }
894
Michael Kolb8872c232013-01-29 10:33:22 -0800895 @Override
896 public void startFaceDetection() {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800897 if (mFaceDetectionStarted) {
898 return;
899 }
Angus Kong88289042014-04-22 16:39:42 -0700900 if (mCameraCapabilities.getMaxNumOfFacesSupported() > 0) {
Michael Kolb8872c232013-01-29 10:33:22 -0800901 mFaceDetectionStarted = true;
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700902 mUI.onStartFaceDetection(mDisplayOrientation, isCameraFrontFacing());
Angus Kong9e765522013-07-31 14:05:20 -0700903 mCameraDevice.setFaceDetectionCallback(mHandler, mUI);
Michael Kolb8872c232013-01-29 10:33:22 -0800904 mCameraDevice.startFaceDetection();
Andy Huibers10c58162014-03-29 14:06:54 -0700905 SessionStatsCollector.instance().faceScanActive(true);
Michael Kolb8872c232013-01-29 10:33:22 -0800906 }
907 }
908
Michael Kolb8872c232013-01-29 10:33:22 -0800909 @Override
910 public void stopFaceDetection() {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800911 if (!mFaceDetectionStarted) {
912 return;
913 }
Angus Kong88289042014-04-22 16:39:42 -0700914 if (mCameraCapabilities.getMaxNumOfFacesSupported() > 0) {
Michael Kolb8872c232013-01-29 10:33:22 -0800915 mFaceDetectionStarted = false;
Angus Kong9e765522013-07-31 14:05:20 -0700916 mCameraDevice.setFaceDetectionCallback(null, null);
Michael Kolb8872c232013-01-29 10:33:22 -0800917 mCameraDevice.stopFaceDetection();
Michael Kolbd6954f32013-03-08 20:43:01 -0800918 mUI.clearFaces();
Andy Huibers10c58162014-03-29 14:06:54 -0700919 SessionStatsCollector.instance().faceScanActive(false);
Michael Kolb8872c232013-01-29 10:33:22 -0800920 }
921 }
922
Michael Kolb8872c232013-01-29 10:33:22 -0800923 private final class ShutterCallback
Angus Kong9ef99252013-07-18 18:04:19 -0700924 implements CameraShutterCallback {
Angus Kongdcb0ef12013-03-25 23:11:43 -0700925
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800926 private final boolean mNeedsAnimation;
Angus Kongdcb0ef12013-03-25 23:11:43 -0700927
Sascha Haeberling37f36112013-08-06 14:31:52 -0700928 public ShutterCallback(boolean needsAnimation) {
929 mNeedsAnimation = needsAnimation;
Angus Kongdcb0ef12013-03-25 23:11:43 -0700930 }
931
Michael Kolb8872c232013-01-29 10:33:22 -0800932 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700933 public void onShutter(CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800934 mShutterCallbackTime = System.currentTimeMillis();
935 mShutterLag = mShutterCallbackTime - mCaptureStartTime;
936 Log.v(TAG, "mShutterLag = " + mShutterLag + "ms");
Sascha Haeberling37f36112013-08-06 14:31:52 -0700937 if (mNeedsAnimation) {
938 mActivity.runOnUiThread(new Runnable() {
939 @Override
940 public void run() {
941 animateAfterShutter();
942 }
943 });
Angus Kongdcb0ef12013-03-25 23:11:43 -0700944 }
Michael Kolb8872c232013-01-29 10:33:22 -0800945 }
946 }
947
Angus Kong9ef99252013-07-18 18:04:19 -0700948 private final class PostViewPictureCallback
949 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800950 @Override
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800951 public void onPictureTaken(byte[] data, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800952 mPostViewPictureCallbackTime = System.currentTimeMillis();
953 Log.v(TAG, "mShutterToPostViewCallbackTime = "
954 + (mPostViewPictureCallbackTime - mShutterCallbackTime)
955 + "ms");
956 }
957 }
958
Angus Kong9ef99252013-07-18 18:04:19 -0700959 private final class RawPictureCallback
960 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800961 @Override
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800962 public void onPictureTaken(byte[] rawData, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800963 mRawPictureCallbackTime = System.currentTimeMillis();
964 Log.v(TAG, "mShutterToRawCallbackTime = "
965 + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms");
966 }
967 }
968
Angus Kong454d63f2014-05-06 14:45:59 -0700969 private static class ResizeBundle {
970 byte[] jpegData;
971 float targetAspectRatio;
972 ExifInterface exif;
973 }
974
975 /**
976 * @return Cropped image if the target aspect ratio is larger than the jpeg
977 * aspect ratio on the long axis. The original jpeg otherwise.
978 */
979 private ResizeBundle cropJpegDataToAspectRatio(ResizeBundle dataBundle) {
980
981 final byte[] jpegData = dataBundle.jpegData;
982 final ExifInterface exif = dataBundle.exif;
983 float targetAspectRatio = dataBundle.targetAspectRatio;
984
985 Bitmap original = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
986 int originalWidth = original.getWidth();
987 int originalHeight = original.getHeight();
988 int newWidth;
989 int newHeight;
990
991 if (originalWidth > originalHeight) {
992 newHeight = (int) (originalWidth / targetAspectRatio);
993 newWidth = originalWidth;
994 } else {
995 newWidth = (int) (originalHeight / targetAspectRatio);
996 newHeight = originalHeight;
997 }
998 int xOffset = (originalWidth - newWidth)/2;
999 int yOffset = (originalHeight - newHeight)/2;
1000
1001 if (xOffset < 0 || yOffset < 0) {
1002 return dataBundle;
1003 }
1004
1005 Bitmap resized = Bitmap.createBitmap(original,xOffset,yOffset,newWidth, newHeight);
1006 exif.setTagValue(ExifInterface.TAG_PIXEL_X_DIMENSION, new Integer(newWidth));
1007 exif.setTagValue(ExifInterface.TAG_PIXEL_Y_DIMENSION, new Integer(newHeight));
1008
1009 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1010
1011 resized.compress(Bitmap.CompressFormat.JPEG, 90, stream);
1012 dataBundle.jpegData = stream.toByteArray();
1013 return dataBundle;
1014 }
1015
Angus Kong9ef99252013-07-18 18:04:19 -07001016 private final class JpegPictureCallback
1017 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -08001018 Location mLocation;
1019
1020 public JpegPictureCallback(Location loc) {
1021 mLocation = loc;
1022 }
1023
1024 @Override
Angus Kong454d63f2014-05-06 14:45:59 -07001025 public void onPictureTaken(final byte[] originalJpegData, final CameraProxy camera) {
Erin Dahlgren667630d2014-04-01 14:03:25 -07001026 mAppController.setShutterEnabled(true);
Michael Kolb8872c232013-01-29 10:33:22 -08001027 if (mPaused) {
1028 return;
1029 }
Doris Liu6432cd62013-06-13 17:20:31 -07001030 if (mIsImageCaptureIntent) {
1031 stopPreview();
1032 }
Angus Kong6607dae2014-06-10 16:07:45 -07001033 if (mSceneMode == CameraCapabilities.SceneMode.HDR) {
Doris Liu6432cd62013-06-13 17:20:31 -07001034 mUI.setSwipingEnabled(true);
Michael Kolb8872c232013-01-29 10:33:22 -08001035 }
1036
1037 mJpegPictureCallbackTime = System.currentTimeMillis();
1038 // If postview callback has arrived, the captured image is displayed
1039 // in postview callback. If not, the captured image is displayed in
1040 // raw picture callback.
1041 if (mPostViewPictureCallbackTime != 0) {
1042 mShutterToPictureDisplayedTime =
1043 mPostViewPictureCallbackTime - mShutterCallbackTime;
1044 mPictureDisplayedToJpegCallbackTime =
1045 mJpegPictureCallbackTime - mPostViewPictureCallbackTime;
1046 } else {
1047 mShutterToPictureDisplayedTime =
1048 mRawPictureCallbackTime - mShutterCallbackTime;
1049 mPictureDisplayedToJpegCallbackTime =
1050 mJpegPictureCallbackTime - mRawPictureCallbackTime;
1051 }
1052 Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = "
1053 + mPictureDisplayedToJpegCallbackTime + "ms");
1054
Michael Kolb8872c232013-01-29 10:33:22 -08001055 mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden.
1056 if (!mIsImageCaptureIntent) {
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001057 setupPreview();
Michael Kolb8872c232013-01-29 10:33:22 -08001058 }
1059
Angus Kong454d63f2014-05-06 14:45:59 -07001060 long now = System.currentTimeMillis();
1061 mJpegCallbackFinishTime = now - mJpegPictureCallbackTime;
1062 Log.v(TAG, "mJpegCallbackFinishTime = " + mJpegCallbackFinishTime + "ms");
1063 mJpegPictureCallbackTime = 0;
1064
1065 final ExifInterface exif = Exif.getExif(originalJpegData);
1066
1067 if (mShouldResizeTo16x9) {
1068 final ResizeBundle dataBundle = new ResizeBundle();
1069 dataBundle.jpegData = originalJpegData;
1070 dataBundle.targetAspectRatio = ResolutionUtil.NEXUS_5_LARGE_16_BY_9_ASPECT_RATIO;
1071 dataBundle.exif = exif;
1072 new AsyncTask<ResizeBundle, Void, ResizeBundle>() {
1073
1074 @Override
1075 protected ResizeBundle doInBackground(ResizeBundle... resizeBundles) {
1076 return cropJpegDataToAspectRatio(resizeBundles[0]);
1077 }
1078
1079 @Override
1080 protected void onPostExecute(ResizeBundle result) {
1081 saveFinalPhoto(result.jpegData, result.exif, camera);
1082 }
1083 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataBundle);
1084
1085 } else {
1086 saveFinalPhoto(originalJpegData, exif, camera);
1087 }
1088 }
1089
1090 void saveFinalPhoto(final byte[] jpegData, final ExifInterface exif, CameraProxy camera) {
1091
Doris Liu36e56fb2013-09-11 17:38:08 -07001092 int orientation = Exif.getOrientation(exif);
Andy Huibers10c58162014-03-29 14:06:54 -07001093
Sol Boucher2192fba2014-08-19 17:24:07 -07001094 float zoomValue = 1.0f;
Angus Kong88289042014-04-22 16:39:42 -07001095 if (mCameraCapabilities.supports(CameraCapabilities.Feature.ZOOM)) {
Sol Boucher2192fba2014-08-19 17:24:07 -07001096 zoomValue = mCameraSettings.getCurrentZoomRatio();
Andy Huiberse08bc042014-04-11 19:26:47 -07001097 }
Angus Kong6607dae2014-06-10 16:07:45 -07001098 boolean hdrOn = CameraCapabilities.SceneMode.HDR == mSceneMode;
Andy Huibers203abe52014-05-19 13:59:01 -07001099 String flashSetting =
Erin Dahlgren6190c362014-06-13 14:12:08 -07001100 mActivity.getSettingsManager().getString(mAppController.getCameraScope(),
1101 Keys.KEY_FLASH_MODE);
1102 boolean gridLinesOn = Keys.areGridLinesOn(mActivity.getSettingsManager());
Andy Huibers10c58162014-03-29 14:06:54 -07001103 UsageStatistics.instance().photoCaptureDoneEvent(
1104 eventprotos.NavigationChange.Mode.PHOTO_CAPTURE,
1105 mNamedImages.mQueue.lastElement().title + ".jpg", exif,
Andy Huibers547d7c82014-05-20 23:03:18 -07001106 isCameraFrontFacing(), hdrOn, zoomValue, flashSetting, gridLinesOn,
Andy Huibersb7c7d9a2014-06-18 22:26:14 -07001107 (float) mTimerDuration, mShutterTouchCoordinate, mVolumeButtonClickedFlag);
1108 mShutterTouchCoordinate = null;
1109 mVolumeButtonClickedFlag = false;
Ruben Brunkd217ed02013-10-08 23:31:13 -07001110
Ruben Brunkd7488272013-10-10 18:45:53 -07001111 if (!mIsImageCaptureIntent) {
Michael Kolb8872c232013-01-29 10:33:22 -08001112 // Calculate the width and the height of the jpeg.
Angus Kong454d63f2014-05-06 14:45:59 -07001113 Integer exifWidth = exif.getTagIntValue(ExifInterface.TAG_PIXEL_X_DIMENSION);
1114 Integer exifHeight = exif.getTagIntValue(ExifInterface.TAG_PIXEL_Y_DIMENSION);
Michael Kolb8872c232013-01-29 10:33:22 -08001115 int width, height;
Angus Kong454d63f2014-05-06 14:45:59 -07001116 if (mShouldResizeTo16x9 && exifWidth != null && exifHeight != null) {
1117 width = exifWidth;
1118 height = exifHeight;
Michael Kolb8872c232013-01-29 10:33:22 -08001119 } else {
Angus Kong454d63f2014-05-06 14:45:59 -07001120 Size s;
Angus Kong6607dae2014-06-10 16:07:45 -07001121 s = mCameraSettings.getCurrentPhotoSize();
Angus Kong454d63f2014-05-06 14:45:59 -07001122 if ((mJpegRotation + orientation) % 180 == 0) {
1123 width = s.width();
1124 height = s.height();
1125 } else {
1126 width = s.height();
1127 height = s.width();
1128 }
Michael Kolb8872c232013-01-29 10:33:22 -08001129 }
Ruben Brunka9d66bd2013-09-06 11:56:32 -07001130 NamedEntity name = mNamedImages.getNextNameEntity();
1131 String title = (name == null) ? null : name.title;
1132 long date = (name == null) ? -1 : name.date;
Ruben Brunkd7488272013-10-10 18:45:53 -07001133
1134 // Handle debug mode outputs
1135 if (mDebugUri != null) {
1136 // If using a debug uri, save jpeg there.
1137 saveToDebugUri(jpegData);
1138
1139 // Adjust the title of the debug image shown in mediastore.
1140 if (title != null) {
1141 title = DEBUG_IMAGE_PREFIX + title;
1142 }
1143 }
1144
Michael Kolb8872c232013-01-29 10:33:22 -08001145 if (title == null) {
1146 Log.e(TAG, "Unbalanced name/data pair");
1147 } else {
Sascha Haeberlingf2994f02014-01-23 19:55:01 -08001148 if (date == -1) {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001149 date = mCaptureStartTime;
Sascha Haeberlingf2994f02014-01-23 19:55:01 -08001150 }
Angus Kong0d00a892013-03-26 11:40:40 -07001151 if (mHeading >= 0) {
1152 // heading direction has been updated by the sensor.
1153 ExifTag directionRefTag = exif.buildTag(
1154 ExifInterface.TAG_GPS_IMG_DIRECTION_REF,
1155 ExifInterface.GpsTrackRef.MAGNETIC_DIRECTION);
1156 ExifTag directionTag = exif.buildTag(
1157 ExifInterface.TAG_GPS_IMG_DIRECTION,
1158 new Rational(mHeading, 1));
1159 exif.setTag(directionRefTag);
1160 exif.setTag(directionTag);
1161 }
Sascha Haeberling280fd3e2013-11-21 13:52:15 -08001162 getServices().getMediaSaver().addImage(
Angus Kong86d36312013-01-31 18:22:44 -08001163 jpegData, title, date, mLocation, width, height,
Angus Kong0d00a892013-03-26 11:40:40 -07001164 orientation, exif, mOnMediaSavedListener, mContentResolver);
Michael Kolb8872c232013-01-29 10:33:22 -08001165 }
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001166 // Animate capture with real jpeg data instead of a preview
1167 // frame.
Doris Liu29da2db2013-08-28 14:28:45 -07001168 mUI.animateCapture(jpegData, orientation, mMirror);
Michael Kolb8872c232013-01-29 10:33:22 -08001169 } else {
1170 mJpegImageData = jpegData;
1171 if (!mQuickCapture) {
Doris Liu36e56fb2013-09-11 17:38:08 -07001172 mUI.showCapturedImageForReview(jpegData, orientation, mMirror);
Michael Kolb8872c232013-01-29 10:33:22 -08001173 } else {
Michael Kolbd6954f32013-03-08 20:43:01 -08001174 onCaptureDone();
Michael Kolb8872c232013-01-29 10:33:22 -08001175 }
1176 }
1177
Sascha Haeberlingbca64bf2014-04-17 10:51:53 -07001178 // Send the taken photo to remote shutter listeners, if any are
1179 // registered.
Sascha Haeberling3c6de142014-07-14 12:23:36 -07001180 getServices().getRemoteShutterListener().onPictureTaken(jpegData);
Sascha Haeberlingf2994f02014-01-23 19:55:01 -08001181
Michael Kolb8872c232013-01-29 10:33:22 -08001182 // Check this in advance of each shot so we don't add to shutter
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001183 // latency. It's true that someone else could write to the SD card
1184 // in the mean time and fill it, but that could have happened
1185 // between the shutter press and saving the JPEG too.
Spike Spraguee6374b72014-04-25 17:24:32 -07001186 mActivity.updateStorageSpaceAndHint(null);
Michael Kolb8872c232013-01-29 10:33:22 -08001187 }
1188 }
1189
Angus Kong9ef99252013-07-18 18:04:19 -07001190 private final class AutoFocusCallback implements CameraAFCallback {
Michael Kolb8872c232013-01-29 10:33:22 -08001191 @Override
Andy Huibers10c58162014-03-29 14:06:54 -07001192 public void onAutoFocus(boolean focused, CameraProxy camera) {
1193 SessionStatsCollector.instance().autofocusResult(focused);
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001194 if (mPaused) {
1195 return;
1196 }
Michael Kolb8872c232013-01-29 10:33:22 -08001197
1198 mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime;
Andy Huibers10c58162014-03-29 14:06:54 -07001199 Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms focused = "+focused);
Michael Kolb8872c232013-01-29 10:33:22 -08001200 setCameraState(IDLE);
Erin Dahlgren667630d2014-04-01 14:03:25 -07001201 mFocusManager.onAutoFocus(focused, false);
Michael Kolb8872c232013-01-29 10:33:22 -08001202 }
1203 }
1204
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001205 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08001206 private final class AutoFocusMoveCallback
Angus Kong9ef99252013-07-18 18:04:19 -07001207 implements CameraAFMoveCallback {
Michael Kolb8872c232013-01-29 10:33:22 -08001208 @Override
1209 public void onAutoFocusMoving(
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001210 boolean moving, CameraProxy camera) {
1211 mFocusManager.onAutoFocusMoving(moving);
Andy Huibers10c58162014-03-29 14:06:54 -07001212 SessionStatsCollector.instance().autofocusMoving(moving);
Michael Kolb8872c232013-01-29 10:33:22 -08001213 }
1214 }
1215
Ruben Brunka9d66bd2013-09-06 11:56:32 -07001216 /**
1217 * This class is just a thread-safe queue for name,date holder objects.
1218 */
1219 public static class NamedImages {
Sascha Haeberling280fd3e2013-11-21 13:52:15 -08001220 private final Vector<NamedEntity> mQueue;
Michael Kolb8872c232013-01-29 10:33:22 -08001221
1222 public NamedImages() {
Ruben Brunka9d66bd2013-09-06 11:56:32 -07001223 mQueue = new Vector<NamedEntity>();
Michael Kolb8872c232013-01-29 10:33:22 -08001224 }
1225
Ruben Brunka9d66bd2013-09-06 11:56:32 -07001226 public void nameNewImage(long date) {
Michael Kolb8872c232013-01-29 10:33:22 -08001227 NamedEntity r = new NamedEntity();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001228 r.title = CameraUtil.createJpegName(date);
Michael Kolb8872c232013-01-29 10:33:22 -08001229 r.date = date;
1230 mQueue.add(r);
1231 }
1232
Ruben Brunka9d66bd2013-09-06 11:56:32 -07001233 public NamedEntity getNextNameEntity() {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001234 synchronized (mQueue) {
Ruben Brunka9d66bd2013-09-06 11:56:32 -07001235 if (!mQueue.isEmpty()) {
1236 return mQueue.remove(0);
1237 }
Michael Kolb8872c232013-01-29 10:33:22 -08001238 }
Ruben Brunka9d66bd2013-09-06 11:56:32 -07001239 return null;
Michael Kolb8872c232013-01-29 10:33:22 -08001240 }
1241
Ruben Brunka9d66bd2013-09-06 11:56:32 -07001242 public static class NamedEntity {
1243 public String title;
1244 public long date;
Michael Kolb8872c232013-01-29 10:33:22 -08001245 }
1246 }
1247
1248 private void setCameraState(int state) {
1249 mCameraState = state;
1250 switch (state) {
Angus Kong62753ae2014-02-10 10:53:54 -08001251 case PREVIEW_STOPPED:
1252 case SNAPSHOT_IN_PROGRESS:
1253 case SWITCHING_CAMERA:
Doris Liuf55f3c42013-11-20 00:24:46 -08001254 // TODO: Tell app UI to disable swipe
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001255 break;
1256 case PhotoController.IDLE:
Doris Liuf55f3c42013-11-20 00:24:46 -08001257 // TODO: Tell app UI to enable swipe
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001258 break;
Michael Kolb8872c232013-01-29 10:33:22 -08001259 }
1260 }
1261
Sascha Haeberling37f36112013-08-06 14:31:52 -07001262 private void animateAfterShutter() {
Michael Kolb8872c232013-01-29 10:33:22 -08001263 // Only animate when in full screen capture mode
1264 // i.e. If monkey/a user swipes to the gallery during picture taking,
1265 // don't show animation
Doris Liuc2e9abd2013-06-19 14:20:51 -07001266 if (!mIsImageCaptureIntent) {
1267 mUI.animateFlash();
Doris Liuc2e9abd2013-06-19 14:20:51 -07001268 }
Michael Kolb8872c232013-01-29 10:33:22 -08001269 }
1270
1271 @Override
1272 public boolean capture() {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001273 // If we are already in the middle of taking a snapshot or the image
1274 // save request is full then ignore.
Michael Kolb8872c232013-01-29 10:33:22 -08001275 if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS
Erin Dahlgren667630d2014-04-01 14:03:25 -07001276 || mCameraState == SWITCHING_CAMERA || !mAppController.isShutterEnabled()) {
Michael Kolb8872c232013-01-29 10:33:22 -08001277 return false;
1278 }
1279 mCaptureStartTime = System.currentTimeMillis();
Sascha Haeberlingbca64bf2014-04-17 10:51:53 -07001280
Michael Kolb8872c232013-01-29 10:33:22 -08001281 mPostViewPictureCallbackTime = 0;
1282 mJpegImageData = null;
1283
Angus Kong6607dae2014-06-10 16:07:45 -07001284 final boolean animateBefore = (mSceneMode == CameraCapabilities.SceneMode.HDR);
Michael Kolb8872c232013-01-29 10:33:22 -08001285
1286 if (animateBefore) {
Sascha Haeberling37f36112013-08-06 14:31:52 -07001287 animateAfterShutter();
Michael Kolb8872c232013-01-29 10:33:22 -08001288 }
1289
1290 // Set rotation and gps data.
Doris Liu3cf565c2013-02-15 10:55:37 -08001291 int orientation;
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001292
Doris Liu3cf565c2013-02-15 10:55:37 -08001293 if (mActivity.isAutoRotateScreen()) {
Sol Boucher73ec9852014-07-24 23:59:21 -07001294 orientation = mDisplayRotation;
Doris Liu3cf565c2013-02-15 10:55:37 -08001295 } else {
1296 orientation = mOrientation;
1297 }
Sol Boucher43e18132014-06-19 15:03:18 -07001298 Characteristics info =
1299 mActivity.getCameraProvider().getCharacteristics(mCameraId);
Sol Boucher73ec9852014-07-24 23:59:21 -07001300 mJpegRotation = info.getJpegOrientation(orientation);
Erin Dahlgrenc120b0f2013-11-19 10:53:49 -08001301 Location loc = mActivity.getLocationManager().getCurrentLocation();
Angus Kong6607dae2014-06-10 16:07:45 -07001302 CameraUtil.setGpsParameters(mCameraSettings, loc);
1303 mCameraDevice.applySettings(mCameraSettings);
Michael Kolb8872c232013-01-29 10:33:22 -08001304
Sascha Haeberling88901942013-08-28 17:49:00 -07001305 // We don't want user to press the button again while taking a
1306 // multi-second HDR photo.
Erin Dahlgren667630d2014-04-01 14:03:25 -07001307 mAppController.setShutterEnabled(false);
Angus Kong9ef99252013-07-18 18:04:19 -07001308 mCameraDevice.takePicture(mHandler,
1309 new ShutterCallback(!animateBefore),
Angus Kongdcb0ef12013-03-25 23:11:43 -07001310 mRawPictureCallback, mPostViewPictureCallback,
Angus Kong9ef99252013-07-18 18:04:19 -07001311 new JpegPictureCallback(loc));
Michael Kolb8872c232013-01-29 10:33:22 -08001312
Ruben Brunka9d66bd2013-09-06 11:56:32 -07001313 mNamedImages.nameNewImage(mCaptureStartTime);
Michael Kolb8872c232013-01-29 10:33:22 -08001314
1315 mFaceDetectionStarted = false;
1316 setCameraState(SNAPSHOT_IN_PROGRESS);
1317 return true;
1318 }
1319
1320 @Override
1321 public void setFocusParameters() {
1322 setCameraParameters(UPDATE_PARAM_PREFERENCE);
1323 }
1324
Michael Kolbd6954f32013-03-08 20:43:01 -08001325 private void updateSceneMode() {
Michael Kolb8872c232013-01-29 10:33:22 -08001326 // If scene mode is set, we cannot set flash mode, white balance, and
1327 // focus mode, instead, we read it from driver
Angus Kong6607dae2014-06-10 16:07:45 -07001328 if (CameraCapabilities.SceneMode.AUTO != mSceneMode) {
1329 overrideCameraSettings(mCameraSettings.getCurrentFlashMode(),
1330 mCameraSettings.getCurrentFocusMode());
Michael Kolb8872c232013-01-29 10:33:22 -08001331 }
1332 }
1333
Angus Kong6607dae2014-06-10 16:07:45 -07001334 private void overrideCameraSettings(CameraCapabilities.FlashMode flashMode,
1335 CameraCapabilities.FocusMode focusMode) {
1336 CameraCapabilities.Stringifier stringifier = mCameraCapabilities.getStringifier();
Erin Dahlgrene419b192013-12-03 13:10:27 -08001337 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren6190c362014-06-13 14:12:08 -07001338 settingsManager.set(mAppController.getCameraScope(), Keys.KEY_FLASH_MODE,
1339 stringifier.stringify(flashMode));
1340 settingsManager.set(mAppController.getCameraScope(), Keys.KEY_FOCUS_MODE,
1341 stringifier.stringify(focusMode));
Michael Kolb8872c232013-01-29 10:33:22 -08001342 }
1343
1344 @Override
Michael Kolb8872c232013-01-29 10:33:22 -08001345 public void onOrientationChanged(int orientation) {
1346 // We keep the last known orientation. So if the user first orient
1347 // the camera then point the camera to floor or sky, we still have
1348 // the correct orientation.
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001349 if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
1350 return;
1351 }
Angus Kongb50b5cb2013-08-09 14:55:20 -07001352 mOrientation = CameraUtil.roundOrientation(orientation, mOrientation);
Michael Kolb8872c232013-01-29 10:33:22 -08001353 }
1354
1355 @Override
Angus Kong20fad242013-11-11 18:23:46 -08001356 public void onCameraAvailable(CameraProxy cameraProxy) {
1357 if (mPaused) {
1358 return;
1359 }
1360 mCameraDevice = cameraProxy;
1361
1362 initializeCapabilities();
1363
1364 // Reset zoom value index.
Sol Boucher2192fba2014-08-19 17:24:07 -07001365 mZoomValue = 1.0f;
Angus Kong20fad242013-11-11 18:23:46 -08001366 if (mFocusManager == null) {
1367 initializeFocusManager();
1368 }
Angus Kong831347d2014-06-16 16:07:28 -07001369 mFocusManager.updateCapabilities(mCameraCapabilities);
Angus Kong20fad242013-11-11 18:23:46 -08001370
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08001371 // Do camera parameter dependent initialization.
Angus Kong6607dae2014-06-10 16:07:45 -07001372 mCameraSettings = mCameraDevice.getSettings();
Angus Kong20fad242013-11-11 18:23:46 -08001373 setCameraParameters(UPDATE_PARAM_ALL);
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08001374 // Set a listener which updates camera parameters based
1375 // on changed settings.
1376 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren1648c362014-01-06 15:06:04 -08001377 settingsManager.addListener(this);
Angus Kong20fad242013-11-11 18:23:46 -08001378 mCameraPreviewParamsReady = true;
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08001379
Angus Kong20fad242013-11-11 18:23:46 -08001380 startPreview();
1381
1382 onCameraOpened();
1383 }
1384
1385 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001386 public void onCaptureCancelled() {
1387 mActivity.setResultEx(Activity.RESULT_CANCELED, new Intent());
1388 mActivity.finish();
Michael Kolb8872c232013-01-29 10:33:22 -08001389 }
1390
Michael Kolbd6954f32013-03-08 20:43:01 -08001391 @Override
1392 public void onCaptureRetake() {
Sascha Haeberlingf2994f02014-01-23 19:55:01 -08001393 if (mPaused) {
Michael Kolb8872c232013-01-29 10:33:22 -08001394 return;
Sascha Haeberling3b0ab892014-01-29 20:54:39 +01001395 }
Michael Kolbd6954f32013-03-08 20:43:01 -08001396 mUI.hidePostCaptureAlert();
Spike Spragueb4a22222014-05-22 14:40:53 -07001397 mUI.hideIntentReviewImageView();
Michael Kolb8872c232013-01-29 10:33:22 -08001398 setupPreview();
1399 }
1400
Michael Kolbd6954f32013-03-08 20:43:01 -08001401 @Override
1402 public void onCaptureDone() {
Michael Kolb8872c232013-01-29 10:33:22 -08001403 if (mPaused) {
1404 return;
1405 }
1406
1407 byte[] data = mJpegImageData;
1408
1409 if (mCropValue == null) {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001410 // First handle the no crop case -- just return the value. If the
Michael Kolb8872c232013-01-29 10:33:22 -08001411 // caller specifies a "save uri" then write the data to its
1412 // stream. Otherwise, pass back a scaled down version of the bitmap
1413 // directly in the extras.
1414 if (mSaveUri != null) {
1415 OutputStream outputStream = null;
1416 try {
1417 outputStream = mContentResolver.openOutputStream(mSaveUri);
1418 outputStream.write(data);
1419 outputStream.close();
1420
1421 mActivity.setResultEx(Activity.RESULT_OK);
1422 mActivity.finish();
1423 } catch (IOException ex) {
1424 // ignore exception
1425 } finally {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001426 CameraUtil.closeSilently(outputStream);
Michael Kolb8872c232013-01-29 10:33:22 -08001427 }
1428 } else {
Angus Kong0d00a892013-03-26 11:40:40 -07001429 ExifInterface exif = Exif.getExif(data);
1430 int orientation = Exif.getOrientation(exif);
Angus Kongb50b5cb2013-08-09 14:55:20 -07001431 Bitmap bitmap = CameraUtil.makeBitmap(data, 50 * 1024);
1432 bitmap = CameraUtil.rotate(bitmap, orientation);
Michael Kolb8872c232013-01-29 10:33:22 -08001433 mActivity.setResultEx(Activity.RESULT_OK,
1434 new Intent("inline-data").putExtra("data", bitmap));
1435 mActivity.finish();
1436 }
1437 } else {
1438 // Save the image to a temp file and invoke the cropper
1439 Uri tempUri = null;
1440 FileOutputStream tempStream = null;
1441 try {
1442 File path = mActivity.getFileStreamPath(sTempCropFilename);
1443 path.delete();
1444 tempStream = mActivity.openFileOutput(sTempCropFilename, 0);
1445 tempStream.write(data);
1446 tempStream.close();
1447 tempUri = Uri.fromFile(path);
1448 } catch (FileNotFoundException ex) {
1449 mActivity.setResultEx(Activity.RESULT_CANCELED);
1450 mActivity.finish();
1451 return;
1452 } catch (IOException ex) {
1453 mActivity.setResultEx(Activity.RESULT_CANCELED);
1454 mActivity.finish();
1455 return;
1456 } finally {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001457 CameraUtil.closeSilently(tempStream);
Michael Kolb8872c232013-01-29 10:33:22 -08001458 }
1459
1460 Bundle newExtras = new Bundle();
1461 if (mCropValue.equals("circle")) {
1462 newExtras.putString("circleCrop", "true");
1463 }
1464 if (mSaveUri != null) {
1465 newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
1466 } else {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001467 newExtras.putBoolean(CameraUtil.KEY_RETURN_DATA, true);
Michael Kolb8872c232013-01-29 10:33:22 -08001468 }
1469 if (mActivity.isSecureCamera()) {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001470 newExtras.putBoolean(CameraUtil.KEY_SHOW_WHEN_LOCKED, true);
Michael Kolb8872c232013-01-29 10:33:22 -08001471 }
1472
Sascha Haeberling37f36112013-08-06 14:31:52 -07001473 // TODO: Share this constant.
Sascha Haeberling8e963a52013-08-06 11:43:02 -07001474 final String CROP_ACTION = "com.android.camera.action.CROP";
1475 Intent cropIntent = new Intent(CROP_ACTION);
Michael Kolb8872c232013-01-29 10:33:22 -08001476
1477 cropIntent.setData(tempUri);
1478 cropIntent.putExtras(newExtras);
1479
1480 mActivity.startActivityForResult(cropIntent, REQUEST_CROP);
1481 }
1482 }
1483
Michael Kolb8872c232013-01-29 10:33:22 -08001484 @Override
Andy Huibersb7c7d9a2014-06-18 22:26:14 -07001485 public void onShutterCoordinate(TouchCoordinate coord) {
1486 mShutterTouchCoordinate = coord;
1487 }
1488
1489 @Override
Michael Kolb8872c232013-01-29 10:33:22 -08001490 public void onShutterButtonFocus(boolean pressed) {
Angus Kongeaaf56d2014-02-20 11:59:10 -08001491 // Do nothing. We don't support half-press to focus anymore.
Michael Kolb8872c232013-01-29 10:33:22 -08001492 }
1493
1494 @Override
1495 public void onShutterButtonClick() {
Doris Liuf9e4f8f2013-12-04 18:04:22 -08001496 if (mPaused || (mCameraState == SWITCHING_CAMERA)
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001497 || (mCameraState == PREVIEW_STOPPED)) {
Andy Huibersb7c7d9a2014-06-18 22:26:14 -07001498 mVolumeButtonClickedFlag = false;
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001499 return;
1500 }
Michael Kolb8872c232013-01-29 10:33:22 -08001501
1502 // Do not take the picture if there is not enough storage.
Angus Kong2dcc0a92013-09-25 13:00:08 -07001503 if (mActivity.getStorageSpaceBytes() <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
Michael Kolb8872c232013-01-29 10:33:22 -08001504 Log.i(TAG, "Not enough space or storage not ready. remaining="
Angus Kong2dcc0a92013-09-25 13:00:08 -07001505 + mActivity.getStorageSpaceBytes());
Andy Huibersb7c7d9a2014-06-18 22:26:14 -07001506 mVolumeButtonClickedFlag = false;
Michael Kolb8872c232013-01-29 10:33:22 -08001507 return;
1508 }
Andy Huibersb7c7d9a2014-06-18 22:26:14 -07001509 Log.d(TAG, "onShutterButtonClick: mCameraState=" + mCameraState +
1510 " mVolumeButtonClickedFlag=" + mVolumeButtonClickedFlag);
Michael Kolb8872c232013-01-29 10:33:22 -08001511
Erin Dahlgren6190c362014-06-13 14:12:08 -07001512 int countDownDuration = mActivity.getSettingsManager()
1513 .getInteger(SettingsManager.SCOPE_GLOBAL, Keys.KEY_COUNTDOWN_DURATION);
Andy Huibers547d7c82014-05-20 23:03:18 -07001514 mTimerDuration = countDownDuration;
Doris Liu6c751642014-05-05 18:43:26 -07001515 if (countDownDuration > 0) {
1516 // Start count down.
1517 mAppController.getCameraAppUI().transitionToCancel();
1518 mAppController.getCameraAppUI().hideModeOptions();
1519 mUI.startCountdown(countDownDuration);
1520 return;
1521 } else {
1522 focusAndCapture();
1523 }
1524 }
1525
1526 private void focusAndCapture() {
Angus Kong6607dae2014-06-10 16:07:45 -07001527 if (mSceneMode == CameraCapabilities.SceneMode.HDR) {
Doris Liu6432cd62013-06-13 17:20:31 -07001528 mUI.setSwipingEnabled(false);
Doris Liu9cdfe002013-04-16 09:50:56 -07001529 }
Michael Kolb8872c232013-01-29 10:33:22 -08001530 // If the user wants to do a snapshot while the previous one is still
1531 // in progress, remember the fact and do it after we finish the previous
1532 // one and re-start the preview. Snapshot in progress also includes the
1533 // state that autofocus is focusing and a picture will be taken when
1534 // focus callback arrives.
Angus Kongeaaf56d2014-02-20 11:59:10 -08001535 if ((mFocusManager.isFocusingSnapOnFinish() || mCameraState == SNAPSHOT_IN_PROGRESS)) {
1536 if (!mIsImageCaptureIntent) {
1537 mSnapshotOnIdle = true;
1538 }
Michael Kolb8872c232013-01-29 10:33:22 -08001539 return;
1540 }
1541
Doris Liuf9e4f8f2013-12-04 18:04:22 -08001542 mSnapshotOnIdle = false;
Angus Kong831347d2014-06-16 16:07:28 -07001543 mFocusManager.focusAndCapture(mCameraSettings.getCurrentFocusMode());
Michael Kolb8872c232013-01-29 10:33:22 -08001544 }
1545
Doris Liu6c751642014-05-05 18:43:26 -07001546 @Override
1547 public void onRemainingSecondsChanged(int remainingSeconds) {
Andy Huibersa31162c2014-09-02 11:27:11 -07001548 if (remainingSeconds == 1) {
1549 mCountdownSoundPlayer.play(R.raw.beep_twice, 0.6f);
1550 } else if (remainingSeconds == 2 || remainingSeconds == 3) {
1551 mCountdownSoundPlayer.play(R.raw.beep_once, 0.6f);
1552 }
Doris Liu6c751642014-05-05 18:43:26 -07001553 }
1554
1555 @Override
1556 public void onCountDownFinished() {
Spike Sprague9f3d01d2014-06-30 14:52:57 -07001557 if (mIsImageCaptureIntent) {
1558 mAppController.getCameraAppUI().transitionToIntentReviewLayout();
1559 } else {
1560 mAppController.getCameraAppUI().transitionToCapture();
1561 }
Doris Liu6c751642014-05-05 18:43:26 -07001562 mAppController.getCameraAppUI().showModeOptions();
1563 if (mPaused) {
1564 return;
1565 }
1566 focusAndCapture();
1567 }
1568
Andy Huibersdef975d2013-11-22 09:13:39 -08001569 private void onResumeTasks() {
Angus Kong0b9eb5b2014-04-30 15:03:33 -07001570 if (mPaused) {
1571 return;
1572 }
Andy Huibersdef975d2013-11-22 09:13:39 -08001573 Log.v(TAG, "Executing onResumeTasks.");
Sascha Haeberlingdcea7632014-07-17 14:06:02 -07001574
Andy Huibersa31162c2014-09-02 11:27:11 -07001575 mCountdownSoundPlayer.loadSound(R.raw.beep_once);
1576 mCountdownSoundPlayer.loadSound(R.raw.beep_twice);
Sascha Haeberlingdcea7632014-07-17 14:06:02 -07001577 if (mFocusManager != null) {
1578 // If camera is not open when resume is called, focus manager will
1579 // not be initialized yet, in which case it will start listening to
1580 // preview area size change later in the initialization.
1581 mAppController.addPreviewAreaSizeChangedListener(mFocusManager);
1582 }
1583 mAppController.addPreviewAreaSizeChangedListener(mUI);
1584
Angus Kong0b9eb5b2014-04-30 15:03:33 -07001585 CameraProvider camProvider = mActivity.getCameraProvider();
1586 if (camProvider == null) {
1587 // No camera provider, the Activity is destroyed already.
1588 return;
1589 }
Sol Boucher44ce4b22014-08-04 23:41:38 -07001590 requestCameraOpen();
Angus Kong20fad242013-11-11 18:23:46 -08001591
Michael Kolb8872c232013-01-29 10:33:22 -08001592 mJpegPictureCallbackTime = 0;
Sol Boucher2192fba2014-08-19 17:24:07 -07001593 mZoomValue = 1.0f;
Angus Kong20fad242013-11-11 18:23:46 -08001594
1595 mOnResumeTime = SystemClock.uptimeMillis();
1596 checkDisplayRotation();
Michael Kolb8872c232013-01-29 10:33:22 -08001597
1598 // If first time initialization is not finished, put it in the
1599 // message queue.
1600 if (!mFirstTimeInitialized) {
Angus Kong13e87c42013-11-25 10:02:47 -08001601 mHandler.sendEmptyMessage(MSG_FIRST_TIME_INIT);
Michael Kolb8872c232013-01-29 10:33:22 -08001602 } else {
1603 initializeSecondTime();
1604 }
Michael Kolb8872c232013-01-29 10:33:22 -08001605
Angus Kong0d00a892013-03-26 11:40:40 -07001606 Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1607 if (gsensor != null) {
1608 mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL);
1609 }
1610
1611 Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1612 if (msensor != null) {
1613 mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_NORMAL);
1614 }
Sascha Haeberlingdcea7632014-07-17 14:06:02 -07001615
1616 getServices().getRemoteShutterListener().onModuleReady(this);
1617 SessionStatsCollector.instance().sessionActive(true);
Michael Kolb8872c232013-01-29 10:33:22 -08001618 }
1619
Angus Kongc4e66562013-11-22 23:03:21 -08001620 /**
Sascha Haeberling6ccec202014-03-11 09:44:34 -07001621 * @return Whether the currently active camera is front-facing.
1622 */
1623 private boolean isCameraFrontFacing() {
Sol Boucher43e18132014-06-19 15:03:18 -07001624 return mAppController.getCameraProvider().getCharacteristics(mCameraId)
1625 .isFacingFront();
Sascha Haeberling6ccec202014-03-11 09:44:34 -07001626 }
1627
1628 /**
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01001629 * The focus manager is the first UI related element to get initialized, and
1630 * it requires the RenderOverlay, so initialize it here
Angus Kongc4e66562013-11-22 23:03:21 -08001631 */
1632 private void initializeFocusManager() {
1633 // Create FocusManager object. startPreview needs it.
1634 // if mFocusManager not null, reuse it
1635 // otherwise create a new instance
1636 if (mFocusManager != null) {
1637 mFocusManager.removeMessages();
1638 } else {
Sascha Haeberling6ccec202014-03-11 09:44:34 -07001639 mMirror = isCameraFrontFacing();
Angus Kong831347d2014-06-16 16:07:28 -07001640 String[] defaultFocusModesStrings = mActivity.getResources().getStringArray(
Angus Kongc4e66562013-11-22 23:03:21 -08001641 R.array.pref_camera_focusmode_default_array);
Sascha Haeberlingee36c5f2014-07-29 17:05:43 -07001642 ArrayList<CameraCapabilities.FocusMode> defaultFocusModes =
1643 new ArrayList<CameraCapabilities.FocusMode>();
Angus Kong831347d2014-06-16 16:07:28 -07001644 CameraCapabilities.Stringifier stringifier = mCameraCapabilities.getStringifier();
1645 for (String modeString : defaultFocusModesStrings) {
1646 CameraCapabilities.FocusMode mode = stringifier.focusModeFromString(modeString);
1647 if (mode != null) {
1648 defaultFocusModes.add(mode);
1649 }
1650 }
1651 mFocusManager =
1652 new FocusOverlayManager(mAppController, defaultFocusModes,
1653 mCameraCapabilities, this, mMirror, mActivity.getMainLooper(),
1654 mUI.getFocusUI());
Kevin Gabayanfb333362014-06-02 14:48:20 -07001655 MotionManager motionManager = getServices().getMotionManager();
1656 if (motionManager != null) {
1657 motionManager.addListener(mFocusManager);
1658 }
Angus Kongc4e66562013-11-22 23:03:21 -08001659 }
Doris Liu482de022013-12-18 19:18:16 -08001660 mAppController.addPreviewAreaSizeChangedListener(mFocusManager);
Angus Kong20fad242013-11-11 18:23:46 -08001661 }
1662
Sascha Haeberlingdcea7632014-07-17 14:06:02 -07001663 /**
1664 * @return Whether we are resuming from within the lockscreen.
1665 */
1666 private boolean isResumeFromLockscreen() {
1667 String action = mActivity.getIntent().getAction();
1668 return (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(action)
1669 || MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action));
1670 }
1671
Angus Kong20fad242013-11-11 18:23:46 -08001672 @Override
Angus Kongc4e66562013-11-22 23:03:21 -08001673 public void resume() {
1674 mPaused = false;
Doris Liu59401042014-01-14 17:51:32 -08001675
Angus Kongc4e66562013-11-22 23:03:21 -08001676 // Add delay on resume from lock screen only, in order to to speed up
1677 // the onResume --> onPause --> onResume cycle from lock screen.
1678 // Don't do always because letting go of thread can cause delay.
Sascha Haeberlingdcea7632014-07-17 14:06:02 -07001679 if (isResumeFromLockscreen()) {
Angus Kongc4e66562013-11-22 23:03:21 -08001680 Log.v(TAG, "On resume, from lock screen.");
1681 // Note: onPauseAfterSuper() will delete this runnable, so we will
1682 // at most have 1 copy queued up.
Angus Kong0b9eb5b2014-04-30 15:03:33 -07001683 mHandler.postDelayed(mResumeTaskRunnable, ON_RESUME_TASKS_DELAY_MSEC);
Angus Kongc4e66562013-11-22 23:03:21 -08001684 } else {
1685 Log.v(TAG, "On resume.");
1686 onResumeTasks();
1687 }
1688 }
1689
1690 @Override
1691 public void pause() {
Michael Kolb8872c232013-01-29 10:33:22 -08001692 mPaused = true;
Angus Kong0b9eb5b2014-04-30 15:03:33 -07001693 mHandler.removeCallbacks(mResumeTaskRunnable);
Sascha Haeberling1bf08892014-06-18 09:38:06 -07001694 getServices().getRemoteShutterListener().onModuleExit();
Andy Huibers10c58162014-03-29 14:06:54 -07001695 SessionStatsCollector.instance().sessionActive(false);
Spike Spragueabf54e22014-03-27 15:41:28 -07001696
Angus Kong0d00a892013-03-26 11:40:40 -07001697 Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1698 if (gsensor != null) {
1699 mSensorManager.unregisterListener(this, gsensor);
1700 }
1701
1702 Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1703 if (msensor != null) {
1704 mSensorManager.unregisterListener(this, msensor);
1705 }
Andy Huibersdef975d2013-11-22 09:13:39 -08001706
Michael Kolb8872c232013-01-29 10:33:22 -08001707 // Reset the focus first. Camera CTS does not guarantee that
1708 // cancelAutoFocus is allowed after preview stops.
1709 if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
1710 mCameraDevice.cancelAutoFocus();
1711 }
Andy Huibersdef975d2013-11-22 09:13:39 -08001712
Erin Dahlgrenb09b53e2013-11-06 11:57:51 -08001713 // If the camera has not been opened asynchronously yet,
1714 // and startPreview hasn't been called, then this is a no-op.
1715 // (e.g. onResume -> onPause -> onResume).
Michael Kolb8872c232013-01-29 10:33:22 -08001716 stopPreview();
Doris Liu6c751642014-05-05 18:43:26 -07001717 cancelCountDown();
1718 mCountdownSoundPlayer.release();
Michael Kolb8872c232013-01-29 10:33:22 -08001719
Angus Kongce5480e2013-01-29 17:43:48 -08001720 mNamedImages = null;
Michael Kolb8872c232013-01-29 10:33:22 -08001721 // If we are in an image capture intent and has taken
1722 // a picture, we just clear it in onPause.
1723 mJpegImageData = null;
1724
Angus Kongdcccc512013-08-08 17:06:03 -07001725 // Remove the messages and runnables in the queue.
1726 mHandler.removeCallbacksAndMessages(null);
Michael Kolb8872c232013-01-29 10:33:22 -08001727
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001728 closeCamera();
Angus Kong13e87c42013-11-25 10:02:47 -08001729 mActivity.enableKeepScreenOn(false);
Michael Kolbd6954f32013-03-08 20:43:01 -08001730 mUI.onPause();
1731
Michael Kolb8872c232013-01-29 10:33:22 -08001732 mPendingSwitchCameraId = -1;
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001733 if (mFocusManager != null) {
1734 mFocusManager.removeMessages();
Angus Kong86d36312013-01-31 18:22:44 -08001735 }
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001736 getServices().getMemoryManager().removeListener(this);
Doris Liu482de022013-12-18 19:18:16 -08001737 mAppController.removePreviewAreaSizeChangedListener(mFocusManager);
Doris Liu6c751642014-05-05 18:43:26 -07001738 mAppController.removePreviewAreaSizeChangedListener(mUI);
Erin Dahlgren1648c362014-01-06 15:06:04 -08001739
1740 SettingsManager settingsManager = mActivity.getSettingsManager();
1741 settingsManager.removeListener(this);
Michael Kolb8872c232013-01-29 10:33:22 -08001742 }
1743
Angus Kong20fad242013-11-11 18:23:46 -08001744 @Override
1745 public void destroy() {
1746 // TODO: implement this.
1747 }
1748
1749 @Override
Angus Kong2f0e4a32013-12-03 10:02:35 -08001750 public void onLayoutOrientationChanged(boolean isLandscape) {
Michael Kolb8872c232013-01-29 10:33:22 -08001751 setDisplayOrientation();
Michael Kolb8872c232013-01-29 10:33:22 -08001752 }
1753
1754 @Override
Doris Liu6432cd62013-06-13 17:20:31 -07001755 public void updateCameraOrientation() {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001756 if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) {
Doris Liu6432cd62013-06-13 17:20:31 -07001757 setDisplayOrientation();
1758 }
1759 }
1760
Michael Kolb8872c232013-01-29 10:33:22 -08001761 private boolean canTakePicture() {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001762 return isCameraIdle()
1763 && (mActivity.getStorageSpaceBytes() > Storage.LOW_STORAGE_THRESHOLD_BYTES);
Michael Kolb8872c232013-01-29 10:33:22 -08001764 }
1765
1766 @Override
1767 public void autoFocus() {
Andy Huibers10c58162014-03-29 14:06:54 -07001768 Log.v(TAG,"Starting auto focus");
Michael Kolb8872c232013-01-29 10:33:22 -08001769 mFocusStartTime = System.currentTimeMillis();
Angus Kong9ef99252013-07-18 18:04:19 -07001770 mCameraDevice.autoFocus(mHandler, mAutoFocusCallback);
Andy Huibers10c58162014-03-29 14:06:54 -07001771 SessionStatsCollector.instance().autofocusManualTrigger();
Michael Kolb8872c232013-01-29 10:33:22 -08001772 setCameraState(FOCUSING);
1773 }
1774
1775 @Override
1776 public void cancelAutoFocus() {
1777 mCameraDevice.cancelAutoFocus();
1778 setCameraState(IDLE);
1779 setCameraParameters(UPDATE_PARAM_PREFERENCE);
1780 }
1781
Michael Kolb8872c232013-01-29 10:33:22 -08001782 @Override
1783 public void onSingleTapUp(View view, int x, int y) {
Doris Liu482de022013-12-18 19:18:16 -08001784 if (mPaused || mCameraDevice == null || !mFirstTimeInitialized
1785 || mCameraState == SNAPSHOT_IN_PROGRESS
1786 || mCameraState == SWITCHING_CAMERA
1787 || mCameraState == PREVIEW_STOPPED) {
1788 return;
1789 }
1790
1791 // Check if metering area or focus area is supported.
Doris Liu15b99612013-12-21 11:32:28 -08001792 if (!mFocusAreaSupported && !mMeteringAreaSupported) {
1793 return;
1794 }
Doris Liu482de022013-12-18 19:18:16 -08001795 mFocusManager.onSingleTapUp(x, y);
Michael Kolb8872c232013-01-29 10:33:22 -08001796 }
1797
1798 @Override
1799 public boolean onBackPressed() {
Michael Kolbd6954f32013-03-08 20:43:01 -08001800 return mUI.onBackPressed();
Michael Kolb8872c232013-01-29 10:33:22 -08001801 }
1802
1803 @Override
1804 public boolean onKeyDown(int keyCode, KeyEvent event) {
1805 switch (keyCode) {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001806 case KeyEvent.KEYCODE_VOLUME_UP:
1807 case KeyEvent.KEYCODE_VOLUME_DOWN:
1808 case KeyEvent.KEYCODE_FOCUS:
Spike Sprague9f3d01d2014-06-30 14:52:57 -07001809 if (/* TODO: mActivity.isInCameraApp() && */mFirstTimeInitialized &&
1810 !mActivity.getCameraAppUI().isInIntentReview()) {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001811 if (event.getRepeatCount() == 0) {
1812 onShutterButtonFocus(true);
1813 }
1814 return true;
1815 }
1816 return false;
1817 case KeyEvent.KEYCODE_CAMERA:
1818 if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1819 onShutterButtonClick();
Michael Kolb8872c232013-01-29 10:33:22 -08001820 }
1821 return true;
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001822 case KeyEvent.KEYCODE_DPAD_CENTER:
1823 // If we get a dpad center event without any focused view, move
1824 // the focus to the shutter button and press it.
1825 if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1826 // Start auto-focus immediately to reduce shutter lag. After
1827 // the shutter button gets the focus, onShutterButtonFocus()
1828 // will be called again but it is fine.
1829 onShutterButtonFocus(true);
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001830 }
1831 return true;
Michael Kolb8872c232013-01-29 10:33:22 -08001832 }
1833 return false;
1834 }
1835
1836 @Override
1837 public boolean onKeyUp(int keyCode, KeyEvent event) {
1838 switch (keyCode) {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001839 case KeyEvent.KEYCODE_VOLUME_UP:
1840 case KeyEvent.KEYCODE_VOLUME_DOWN:
Spike Sprague9f3d01d2014-06-30 14:52:57 -07001841 if (/* mActivity.isInCameraApp() && */mFirstTimeInitialized &&
1842 !mActivity.getCameraAppUI().isInIntentReview()) {
1843 if (mUI.isCountingDown()) {
1844 cancelCountDown();
1845 } else {
1846 mVolumeButtonClickedFlag = true;
1847 onShutterButtonClick();
1848 }
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001849 return true;
1850 }
1851 return false;
1852 case KeyEvent.KEYCODE_FOCUS:
1853 if (mFirstTimeInitialized) {
1854 onShutterButtonFocus(false);
1855 }
Michael Kolb8872c232013-01-29 10:33:22 -08001856 return true;
Michael Kolb8872c232013-01-29 10:33:22 -08001857 }
1858 return false;
1859 }
1860
Michael Kolb8872c232013-01-29 10:33:22 -08001861 private void closeCamera() {
1862 if (mCameraDevice != null) {
Angus Kong97e282a2014-03-04 18:44:49 -08001863 stopFaceDetection();
Michael Kolb8872c232013-01-29 10:33:22 -08001864 mCameraDevice.setZoomChangeListener(null);
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001865 mCameraDevice.setFaceDetectionCallback(null, null);
Angus Kong2bca2102014-03-11 16:27:30 -07001866 mCameraDevice.setErrorCallback(null, null);
Ruben Brunk59147832013-11-05 15:53:28 -08001867
Michael Kolb8872c232013-01-29 10:33:22 -08001868 mFaceDetectionStarted = false;
Angus Kong20fad242013-11-11 18:23:46 -08001869 mActivity.getCameraProvider().releaseCamera(mCameraDevice.getCameraId());
Michael Kolb8872c232013-01-29 10:33:22 -08001870 mCameraDevice = null;
1871 setCameraState(PREVIEW_STOPPED);
1872 mFocusManager.onCameraReleased();
1873 }
1874 }
1875
1876 private void setDisplayOrientation() {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001877 mDisplayRotation = CameraUtil.getDisplayRotation(mActivity);
Sol Boucher43e18132014-06-19 15:03:18 -07001878 Characteristics info =
1879 mActivity.getCameraProvider().getCharacteristics(mCameraId);
Sol Boucher73ec9852014-07-24 23:59:21 -07001880 mDisplayOrientation = info.getPreviewOrientation(mDisplayRotation);
Doris Liu6432cd62013-06-13 17:20:31 -07001881 mCameraDisplayOrientation = mDisplayOrientation;
Michael Kolbd6954f32013-03-08 20:43:01 -08001882 mUI.setDisplayOrientation(mDisplayOrientation);
Michael Kolb8872c232013-01-29 10:33:22 -08001883 if (mFocusManager != null) {
1884 mFocusManager.setDisplayOrientation(mDisplayOrientation);
1885 }
Doris Liu6432cd62013-06-13 17:20:31 -07001886 // Change the camera display orientation
1887 if (mCameraDevice != null) {
Sol Boucher73ec9852014-07-24 23:59:21 -07001888 mCameraDevice.setDisplayOrientation(mDisplayRotation);
Doris Liu6432cd62013-06-13 17:20:31 -07001889 }
Michael Kolb8872c232013-01-29 10:33:22 -08001890 }
1891
Sascha Haeberlingddef7792013-08-13 14:41:10 -07001892 /** Only called by UI thread. */
Michael Kolb8872c232013-01-29 10:33:22 -08001893 private void setupPreview() {
1894 mFocusManager.resetTouchFocus();
1895 startPreview();
Michael Kolb8872c232013-01-29 10:33:22 -08001896 }
1897
Angus Kong20fad242013-11-11 18:23:46 -08001898 /**
1899 * Returns whether we can/should start the preview or not.
1900 */
1901 private boolean checkPreviewPreconditions() {
1902 if (mPaused) {
1903 return false;
Angus Kongdcccc512013-08-08 17:06:03 -07001904 }
Michael Kolb8872c232013-01-29 10:33:22 -08001905
Angus Kong20fad242013-11-11 18:23:46 -08001906 if (mCameraDevice == null) {
1907 Log.w(TAG, "startPreview: camera device not ready yet.");
1908 return false;
1909 }
1910
Erin Dahlgrend8de0772014-02-03 10:12:27 -08001911 SurfaceTexture st = mActivity.getCameraAppUI().getSurfaceTexture();
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001912 if (st == null) {
1913 Log.w(TAG, "startPreview: surfaceTexture is not ready.");
Angus Kong20fad242013-11-11 18:23:46 -08001914 return false;
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001915 }
1916
1917 if (!mCameraPreviewParamsReady) {
1918 Log.w(TAG, "startPreview: parameters for preview is not ready.");
Angus Kong20fad242013-11-11 18:23:46 -08001919 return false;
1920 }
1921 return true;
1922 }
1923
1924 /**
1925 * The start/stop preview should only run on the UI thread.
1926 */
1927 private void startPreview() {
1928 if (!checkPreviewPreconditions()) {
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001929 return;
1930 }
Angus Kong20fad242013-11-11 18:23:46 -08001931
Angus Kong2bca2102014-03-11 16:27:30 -07001932 mCameraDevice.setErrorCallback(mHandler, mErrorCallback);
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001933 setDisplayOrientation();
1934
1935 if (!mSnapshotOnIdle) {
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001936 // If the focus mode is continuous autofocus, call cancelAutoFocus
1937 // to resume it because it may have been paused by autoFocus call.
Angus Kong831347d2014-06-16 16:07:28 -07001938 if (mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode()) ==
1939 CameraCapabilities.FocusMode.CONTINUOUS_PICTURE) {
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001940 mCameraDevice.cancelAutoFocus();
Michael Kolb8872c232013-01-29 10:33:22 -08001941 }
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001942 mFocusManager.setAeAwbLock(false); // Unlock AE and AWB.
1943 }
1944 setCameraParameters(UPDATE_PARAM_ALL);
Erin Dahlgrend8de0772014-02-03 10:12:27 -08001945 mCameraDevice.setPreviewTexture(mActivity.getCameraAppUI().getSurfaceTexture());
Michael Kolb8872c232013-01-29 10:33:22 -08001946
Alan Newbergerd41766f2014-04-09 18:25:34 -07001947 Log.i(TAG, "startPreview");
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001948 mCameraDevice.startPreview();
Angus Kong20fad242013-11-11 18:23:46 -08001949
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001950 mFocusManager.onPreviewStarted();
1951 onPreviewStarted();
Andy Huibers10c58162014-03-29 14:06:54 -07001952 SessionStatsCollector.instance().previewActive(true);
Erin Dahlgrendc282e12013-11-12 09:39:08 -08001953 if (mSnapshotOnIdle) {
1954 mHandler.post(mDoSnapRunnable);
Michael Kolb8872c232013-01-29 10:33:22 -08001955 }
1956 }
1957
Michael Kolbd6954f32013-03-08 20:43:01 -08001958 @Override
1959 public void stopPreview() {
Michael Kolb8872c232013-01-29 10:33:22 -08001960 if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
Alan Newbergerd41766f2014-04-09 18:25:34 -07001961 Log.i(TAG, "stopPreview");
Michael Kolb8872c232013-01-29 10:33:22 -08001962 mCameraDevice.stopPreview();
1963 mFaceDetectionStarted = false;
1964 }
1965 setCameraState(PREVIEW_STOPPED);
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001966 if (mFocusManager != null) {
1967 mFocusManager.onPreviewStopped();
1968 }
Andy Huibers10c58162014-03-29 14:06:54 -07001969 SessionStatsCollector.instance().previewActive(false);
Michael Kolb8872c232013-01-29 10:33:22 -08001970 }
1971
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08001972 @Override
Erin Dahlgren6190c362014-06-13 14:12:08 -07001973 public void onSettingChanged(SettingsManager settingsManager, String key) {
1974 if (key.equals(Keys.KEY_FLASH_MODE)) {
1975 updateParametersFlashMode();
1976 }
1977 if (key.equals(Keys.KEY_CAMERA_HDR)) {
1978 if (settingsManager.getBoolean(SettingsManager.SCOPE_GLOBAL,
1979 Keys.KEY_CAMERA_HDR)) {
1980 // HDR is on.
1981 mAppController.getButtonManager().disableButton(ButtonManager.BUTTON_FLASH);
1982 mFlashModeBeforeSceneMode = settingsManager.getString(
1983 mAppController.getCameraScope(), Keys.KEY_FLASH_MODE);
1984 } else {
1985 if (mFlashModeBeforeSceneMode != null) {
1986 settingsManager.set(mAppController.getCameraScope(),
1987 Keys.KEY_FLASH_MODE,
1988 mFlashModeBeforeSceneMode);
1989 updateParametersFlashMode();
1990 mFlashModeBeforeSceneMode = null;
Angus Kong8c07fe92014-05-22 14:42:45 -07001991 }
Erin Dahlgren6190c362014-06-13 14:12:08 -07001992 mAppController.getButtonManager().enableButton(ButtonManager.BUTTON_FLASH);
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08001993 }
1994 }
Erin Dahlgren1648c362014-01-06 15:06:04 -08001995
1996 if (mCameraDevice != null) {
Angus Kong6607dae2014-06-10 16:07:45 -07001997 mCameraDevice.applySettings(mCameraSettings);
Erin Dahlgren1648c362014-01-06 15:06:04 -08001998 }
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08001999 }
2000
Michael Kolb8872c232013-01-29 10:33:22 -08002001 private void updateCameraParametersInitialize() {
2002 // Reset preview frame rate to the maximum because it may be lowered by
2003 // video camera application.
Angus Kong88289042014-04-22 16:39:42 -07002004 int[] fpsRange = CameraUtil.getPhotoPreviewFpsRange(mCameraCapabilities);
ztenghui16a35202013-09-23 11:35:36 -07002005 if (fpsRange != null && fpsRange.length > 0) {
Angus Kong831347d2014-06-16 16:07:28 -07002006 mCameraSettings.setPreviewFpsRange(fpsRange[0], fpsRange[1]);
Michael Kolb8872c232013-01-29 10:33:22 -08002007 }
2008
Angus Kong831347d2014-06-16 16:07:28 -07002009 mCameraSettings.setRecordingHintEnabled(false);
Michael Kolb8872c232013-01-29 10:33:22 -08002010
Angus Kong6607dae2014-06-10 16:07:45 -07002011 if (mCameraCapabilities.supports(CameraCapabilities.Feature.VIDEO_STABILIZATION)) {
2012 mCameraSettings.setVideoStabilization(false);
Michael Kolb8872c232013-01-29 10:33:22 -08002013 }
2014 }
2015
2016 private void updateCameraParametersZoom() {
2017 // Set zoom.
Angus Kong88289042014-04-22 16:39:42 -07002018 if (mCameraCapabilities.supports(CameraCapabilities.Feature.ZOOM)) {
Sol Boucher2192fba2014-08-19 17:24:07 -07002019 mCameraSettings.setZoomRatio(mZoomValue);
Michael Kolb8872c232013-01-29 10:33:22 -08002020 }
2021 }
2022
Sascha Haeberling638e6f02013-09-18 14:28:51 -07002023 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08002024 private void setAutoExposureLockIfSupported() {
2025 if (mAeLockSupported) {
Angus Kong6607dae2014-06-10 16:07:45 -07002026 mCameraSettings.setAutoExposureLock(mFocusManager.getAeAwbLock());
Michael Kolb8872c232013-01-29 10:33:22 -08002027 }
2028 }
2029
Sascha Haeberling638e6f02013-09-18 14:28:51 -07002030 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08002031 private void setAutoWhiteBalanceLockIfSupported() {
2032 if (mAwbLockSupported) {
Angus Kong6607dae2014-06-10 16:07:45 -07002033 mCameraSettings.setAutoWhiteBalanceLock(mFocusManager.getAeAwbLock());
Michael Kolb8872c232013-01-29 10:33:22 -08002034 }
2035 }
2036
Michael Kolb8872c232013-01-29 10:33:22 -08002037 private void setFocusAreasIfSupported() {
2038 if (mFocusAreaSupported) {
Angus Kong6607dae2014-06-10 16:07:45 -07002039 mCameraSettings.setFocusAreas(mFocusManager.getFocusAreas());
Michael Kolb8872c232013-01-29 10:33:22 -08002040 }
2041 }
2042
Michael Kolb8872c232013-01-29 10:33:22 -08002043 private void setMeteringAreasIfSupported() {
2044 if (mMeteringAreaSupported) {
Angus Kong6607dae2014-06-10 16:07:45 -07002045 mCameraSettings.setMeteringAreas(mFocusManager.getMeteringAreas());
Michael Kolb8872c232013-01-29 10:33:22 -08002046 }
2047 }
2048
Erin Dahlgrenba6994d2013-12-12 16:04:38 -08002049 private void updateCameraParametersPreference() {
Michael Kolb8872c232013-01-29 10:33:22 -08002050 setAutoExposureLockIfSupported();
2051 setAutoWhiteBalanceLockIfSupported();
2052 setFocusAreasIfSupported();
2053 setMeteringAreasIfSupported();
2054
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08002055 // Initialize focus mode.
Michael Kolbd3253f22013-07-12 11:36:47 -07002056 mFocusManager.overrideFocusMode(null);
Angus Kong831347d2014-06-16 16:07:28 -07002057 mCameraSettings
2058 .setFocusMode(mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode()));
Andy Huibers10c58162014-03-29 14:06:54 -07002059 SessionStatsCollector.instance().autofocusActive(
Angus Kong831347d2014-06-16 16:07:28 -07002060 mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode()) ==
2061 CameraCapabilities.FocusMode.CONTINUOUS_PICTURE
2062 );
Michael Kolbd3253f22013-07-12 11:36:47 -07002063
Michael Kolb8872c232013-01-29 10:33:22 -08002064 // Set picture size.
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08002065 updateParametersPictureSize();
2066
2067 // Set JPEG quality.
2068 updateParametersPictureQuality();
2069
2070 // For the following settings, we need to check if the settings are
2071 // still supported by latest driver, if not, ignore the settings.
2072
2073 // Set exposure compensation
2074 updateParametersExposureCompensation();
2075
2076 // Set the scene mode: also sets flash and white balance.
2077 updateParametersSceneMode();
2078
2079 if (mContinuousFocusSupported && ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK) {
2080 updateAutoFocusMoveCallback();
2081 }
2082 }
2083
2084 private void updateParametersPictureSize() {
2085 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren6190c362014-06-13 14:12:08 -07002086 String pictureSizeKey = isCameraFrontFacing() ? Keys.KEY_PICTURE_SIZE_FRONT
2087 : Keys.KEY_PICTURE_SIZE_BACK;
2088 String pictureSize = settingsManager.getString(SettingsManager.SCOPE_GLOBAL,
2089 pictureSizeKey);
Sascha Haeberling3b0ab892014-01-29 20:54:39 +01002090
Angus Kong6607dae2014-06-10 16:07:45 -07002091 List<Size> supported = mCameraCapabilities.getSupportedPhotoSizes();
Andy Huibers90c7ad52014-05-20 22:13:54 -07002092 CameraPictureSizesCacher.updateSizesForCamera(mAppController.getAndroidContext(),
2093 mCameraDevice.getCameraId(), supported);
Angus Kong6607dae2014-06-10 16:07:45 -07002094 SettingsUtil.setCameraPictureSize(pictureSize, supported, mCameraSettings,
Sascha Haeberling6ccec202014-03-11 09:44:34 -07002095 mCameraDevice.getCameraId());
Angus Kong454d63f2014-05-06 14:45:59 -07002096
2097 Size size = SettingsUtil.getPhotoSize(pictureSize, supported,
2098 mCameraDevice.getCameraId());
2099 if (ApiHelper.IS_NEXUS_5) {
2100 if (ResolutionUtil.NEXUS_5_LARGE_16_BY_9.equals(pictureSize)) {
2101 mShouldResizeTo16x9 = true;
2102 } else {
2103 mShouldResizeTo16x9 = false;
2104 }
2105 }
Michael Kolb8872c232013-01-29 10:33:22 -08002106
2107 // Set a preview size that is closest to the viewfinder height and has
2108 // the right aspect ratio.
Angus Kong6607dae2014-06-10 16:07:45 -07002109 List<Size> sizes = mCameraCapabilities.getSupportedPreviewSizes();
Angus Kongb50b5cb2013-08-09 14:55:20 -07002110 Size optimalSize = CameraUtil.getOptimalPreviewSize(mActivity, sizes,
Angus Kong00b7b102014-04-24 15:46:52 -07002111 (double) size.width() / size.height());
Angus Kong6607dae2014-06-10 16:07:45 -07002112 Size original = mCameraSettings.getCurrentPreviewSize();
2113 if (!optimalSize.equals(original)) {
Angus Kong6607dae2014-06-10 16:07:45 -07002114 mCameraSettings.setPreviewSize(optimalSize);
Doris Liu6432cd62013-06-13 17:20:31 -07002115
Michael Kolb8872c232013-01-29 10:33:22 -08002116 // Zoom related settings will be changed for different preview
2117 // sizes, so set and read the parameters to get latest values
Michael Kolb4a4d4ef2013-05-30 08:35:26 -07002118 if (mHandler.getLooper() == Looper.myLooper()) {
2119 // On UI thread only, not when camera starts up
2120 setupPreview();
2121 } else {
Angus Kong6607dae2014-06-10 16:07:45 -07002122 mCameraDevice.applySettings(mCameraSettings);
Michael Kolb4a4d4ef2013-05-30 08:35:26 -07002123 }
Angus Kong6607dae2014-06-10 16:07:45 -07002124 mCameraSettings = mCameraDevice.getSettings();
Michael Kolb8872c232013-01-29 10:33:22 -08002125 }
Doris Liu95405742013-11-05 15:25:26 -08002126
Angus Kong00b7b102014-04-24 15:46:52 -07002127 if (optimalSize.width() != 0 && optimalSize.height() != 0) {
2128 mUI.updatePreviewAspectRatio((float) optimalSize.width()
2129 / (float) optimalSize.height());
Doris Liu95405742013-11-05 15:25:26 -08002130 }
Angus Kong00b7b102014-04-24 15:46:52 -07002131 Log.i(TAG, "Preview size is " + optimalSize);
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08002132 }
Michael Kolb8872c232013-01-29 10:33:22 -08002133
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08002134 private void updateParametersPictureQuality() {
Michael Kolb8872c232013-01-29 10:33:22 -08002135 int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId,
2136 CameraProfile.QUALITY_HIGH);
Angus Kong6607dae2014-06-10 16:07:45 -07002137 mCameraSettings.setPhotoJpegCompressionQuality(jpegQuality);
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08002138 }
Michael Kolb8872c232013-01-29 10:33:22 -08002139
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08002140 private void updateParametersExposureCompensation() {
2141 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren6190c362014-06-13 14:12:08 -07002142 if (settingsManager.getBoolean(SettingsManager.SCOPE_GLOBAL,
2143 Keys.KEY_EXPOSURE_COMPENSATION_ENABLED)) {
2144 int value = settingsManager.getInteger(mAppController.getCameraScope(),
2145 Keys.KEY_EXPOSURE);
Angus Kong88289042014-04-22 16:39:42 -07002146 int max = mCameraCapabilities.getMaxExposureCompensation();
2147 int min = mCameraCapabilities.getMinExposureCompensation();
Spike Spragueabf54e22014-03-27 15:41:28 -07002148 if (value >= min && value <= max) {
Angus Kong6607dae2014-06-10 16:07:45 -07002149 mCameraSettings.setExposureCompensationIndex(value);
Spike Spragueabf54e22014-03-27 15:41:28 -07002150 } else {
2151 Log.w(TAG, "invalid exposure range: " + value);
2152 }
Michael Kolb8872c232013-01-29 10:33:22 -08002153 } else {
Spike Spragueabf54e22014-03-27 15:41:28 -07002154 // If exposure compensation is not enabled, reset the exposure compensation value.
2155 setExposureCompensation(0);
Michael Kolb8872c232013-01-29 10:33:22 -08002156 }
Spike Spragueabf54e22014-03-27 15:41:28 -07002157
Erin Dahlgrenba6994d2013-12-12 16:04:38 -08002158 }
2159
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08002160 private void updateParametersSceneMode() {
Angus Kong6607dae2014-06-10 16:07:45 -07002161 CameraCapabilities.Stringifier stringifier = mCameraCapabilities.getStringifier();
Erin Dahlgrenba6994d2013-12-12 16:04:38 -08002162 SettingsManager settingsManager = mActivity.getSettingsManager();
2163
Erin Dahlgren6190c362014-06-13 14:12:08 -07002164 mSceneMode = stringifier.
2165 sceneModeFromString(settingsManager.getString(mAppController.getCameraScope(),
2166 Keys.KEY_SCENE_MODE));
Angus Kong6607dae2014-06-10 16:07:45 -07002167 if (mCameraCapabilities.supports(mSceneMode)) {
2168 if (mCameraSettings.getCurrentSceneMode() != mSceneMode) {
2169 mCameraSettings.setSceneMode(mSceneMode);
Erin Dahlgrenba6994d2013-12-12 16:04:38 -08002170
2171 // Setting scene mode will change the settings of flash mode,
2172 // white balance, and focus mode. Here we read back the
2173 // parameters, so we can know those settings.
Angus Kong6607dae2014-06-10 16:07:45 -07002174 mCameraDevice.applySettings(mCameraSettings);
2175 mCameraSettings = mCameraDevice.getSettings();
Erin Dahlgrenba6994d2013-12-12 16:04:38 -08002176 }
2177 } else {
Angus Kong6607dae2014-06-10 16:07:45 -07002178 mSceneMode = mCameraSettings.getCurrentSceneMode();
Erin Dahlgrenba6994d2013-12-12 16:04:38 -08002179 if (mSceneMode == null) {
Angus Kong6607dae2014-06-10 16:07:45 -07002180 mSceneMode = CameraCapabilities.SceneMode.AUTO;
Erin Dahlgrenba6994d2013-12-12 16:04:38 -08002181 }
2182 }
2183
Angus Kong6607dae2014-06-10 16:07:45 -07002184 if (CameraCapabilities.SceneMode.AUTO == mSceneMode) {
Michael Kolb8872c232013-01-29 10:33:22 -08002185 // Set flash mode.
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08002186 updateParametersFlashMode();
Michael Kolb8872c232013-01-29 10:33:22 -08002187
Michael Kolb8872c232013-01-29 10:33:22 -08002188 // Set focus mode.
2189 mFocusManager.overrideFocusMode(null);
Angus Kong831347d2014-06-16 16:07:28 -07002190 mCameraSettings.setFocusMode(
2191 mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode()));
Michael Kolb8872c232013-01-29 10:33:22 -08002192 } else {
Angus Kong831347d2014-06-16 16:07:28 -07002193 mFocusManager.overrideFocusMode(mCameraSettings.getCurrentFocusMode());
Michael Kolb8872c232013-01-29 10:33:22 -08002194 }
Michael Kolb8872c232013-01-29 10:33:22 -08002195 }
2196
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08002197 private void updateParametersFlashMode() {
2198 SettingsManager settingsManager = mActivity.getSettingsManager();
2199
Angus Kong6607dae2014-06-10 16:07:45 -07002200 CameraCapabilities.FlashMode flashMode = mCameraCapabilities.getStringifier()
Erin Dahlgren6190c362014-06-13 14:12:08 -07002201 .flashModeFromString(settingsManager.getString(mAppController.getCameraScope(),
2202 Keys.KEY_FLASH_MODE));
Angus Kong6607dae2014-06-10 16:07:45 -07002203 if (mCameraCapabilities.supports(flashMode)) {
2204 mCameraSettings.setFlashMode(flashMode);
Erin Dahlgren7f0151d2014-01-02 16:08:12 -08002205 }
2206 }
2207
Sascha Haeberling638e6f02013-09-18 14:28:51 -07002208 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08002209 private void updateAutoFocusMoveCallback() {
Angus Kong6607dae2014-06-10 16:07:45 -07002210 if (mCameraSettings.getCurrentFocusMode() ==
2211 CameraCapabilities.FocusMode.CONTINUOUS_PICTURE) {
Angus Kong9ef99252013-07-18 18:04:19 -07002212 mCameraDevice.setAutoFocusMoveCallback(mHandler,
Angus Kong4f795b82013-09-16 14:25:35 -07002213 (CameraAFMoveCallback) mAutoFocusMoveCallback);
Michael Kolb8872c232013-01-29 10:33:22 -08002214 } else {
Angus Kong9ef99252013-07-18 18:04:19 -07002215 mCameraDevice.setAutoFocusMoveCallback(null, null);
Michael Kolb8872c232013-01-29 10:33:22 -08002216 }
2217 }
2218
Spike Spragueabf54e22014-03-27 15:41:28 -07002219 /**
2220 * Sets the exposure compensation to the given value and also updates settings.
2221 *
2222 * @param value exposure compensation value to be set
2223 */
2224 public void setExposureCompensation(int value) {
Angus Kong88289042014-04-22 16:39:42 -07002225 int max = mCameraCapabilities.getMaxExposureCompensation();
2226 int min = mCameraCapabilities.getMinExposureCompensation();
Spike Spragueabf54e22014-03-27 15:41:28 -07002227 if (value >= min && value <= max) {
Angus Kong6607dae2014-06-10 16:07:45 -07002228 mCameraSettings.setExposureCompensationIndex(value);
Spike Spragueabf54e22014-03-27 15:41:28 -07002229 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren6190c362014-06-13 14:12:08 -07002230 settingsManager.set(mAppController.getCameraScope(),
2231 Keys.KEY_EXPOSURE, value);
Spike Spragueabf54e22014-03-27 15:41:28 -07002232 } else {
2233 Log.w(TAG, "invalid exposure range: " + value);
2234 }
2235 }
2236
Michael Kolb8872c232013-01-29 10:33:22 -08002237 // We separate the parameters into several subsets, so we can update only
2238 // the subsets actually need updating. The PREFERENCE set needs extra
2239 // locking because the preference can be changed from GLThread as well.
2240 private void setCameraParameters(int updateSet) {
2241 if ((updateSet & UPDATE_PARAM_INITIALIZE) != 0) {
2242 updateCameraParametersInitialize();
2243 }
2244
2245 if ((updateSet & UPDATE_PARAM_ZOOM) != 0) {
2246 updateCameraParametersZoom();
2247 }
2248
2249 if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) {
Erin Dahlgrenba6994d2013-12-12 16:04:38 -08002250 updateCameraParametersPreference();
Michael Kolb8872c232013-01-29 10:33:22 -08002251 }
2252
Angus Kong6607dae2014-06-10 16:07:45 -07002253 mCameraDevice.applySettings(mCameraSettings);
Michael Kolb8872c232013-01-29 10:33:22 -08002254 }
2255
2256 // If the Camera is idle, update the parameters immediately, otherwise
2257 // accumulate them in mUpdateSet and update later.
2258 private void setCameraParametersWhenIdle(int additionalUpdateSet) {
2259 mUpdateSet |= additionalUpdateSet;
2260 if (mCameraDevice == null) {
2261 // We will update all the parameters when we open the device, so
2262 // we don't need to do anything now.
2263 mUpdateSet = 0;
2264 return;
2265 } else if (isCameraIdle()) {
2266 setCameraParameters(mUpdateSet);
Michael Kolbd6954f32013-03-08 20:43:01 -08002267 updateSceneMode();
Michael Kolb8872c232013-01-29 10:33:22 -08002268 mUpdateSet = 0;
2269 } else {
Angus Kong13e87c42013-11-25 10:02:47 -08002270 if (!mHandler.hasMessages(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE)) {
2271 mHandler.sendEmptyMessageDelayed(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000);
Michael Kolb8872c232013-01-29 10:33:22 -08002272 }
2273 }
2274 }
2275
ztenghui7b265a62013-09-09 14:58:44 -07002276 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08002277 public boolean isCameraIdle() {
Michael Kolb8872c232013-01-29 10:33:22 -08002278 return (mCameraState == IDLE) ||
2279 (mCameraState == PREVIEW_STOPPED) ||
2280 ((mFocusManager != null) && mFocusManager.isFocusCompleted()
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01002281 && (mCameraState != SWITCHING_CAMERA));
Michael Kolb8872c232013-01-29 10:33:22 -08002282 }
2283
ztenghui7b265a62013-09-09 14:58:44 -07002284 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08002285 public boolean isImageCaptureIntent() {
Michael Kolb8872c232013-01-29 10:33:22 -08002286 String action = mActivity.getIntent().getAction();
2287 return (MediaStore.ACTION_IMAGE_CAPTURE.equals(action)
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01002288 || CameraActivity.ACTION_IMAGE_CAPTURE_SECURE.equals(action));
Michael Kolb8872c232013-01-29 10:33:22 -08002289 }
2290
2291 private void setupCaptureParams() {
2292 Bundle myExtras = mActivity.getIntent().getExtras();
2293 if (myExtras != null) {
2294 mSaveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT);
2295 mCropValue = myExtras.getString("crop");
2296 }
2297 }
2298
Michael Kolb8872c232013-01-29 10:33:22 -08002299 private void initializeCapabilities() {
Angus Kong88289042014-04-22 16:39:42 -07002300 mCameraCapabilities = mCameraDevice.getCapabilities();
2301 mFocusAreaSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.FOCUS_AREA);
2302 mMeteringAreaSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.METERING_AREA);
2303 mAeLockSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.AUTO_EXPOSURE_LOCK);
2304 mAwbLockSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.AUTO_WHITE_BALANCE_LOCK);
Angus Kong6607dae2014-06-10 16:07:45 -07002305 mContinuousFocusSupported =
2306 mCameraCapabilities.supports(CameraCapabilities.FocusMode.CONTINUOUS_PICTURE);
Michael Kolb8872c232013-01-29 10:33:22 -08002307 }
2308
Michael Kolb8872c232013-01-29 10:33:22 -08002309 @Override
Sol Boucher2192fba2014-08-19 17:24:07 -07002310 public void onZoomChanged(float ratio) {
Michael Kolbd6954f32013-03-08 20:43:01 -08002311 // Not useful to change zoom value when the activity is paused.
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08002312 if (mPaused) {
Sol Boucher2192fba2014-08-19 17:24:07 -07002313 return;
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08002314 }
Sol Boucher2192fba2014-08-19 17:24:07 -07002315 mZoomValue = ratio;
Angus Kong6607dae2014-06-10 16:07:45 -07002316 if (mCameraSettings == null || mCameraDevice == null) {
Sol Boucher2192fba2014-08-19 17:24:07 -07002317 return;
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08002318 }
Michael Kolbd6954f32013-03-08 20:43:01 -08002319 // Set zoom parameters asynchronously
Sol Boucher2192fba2014-08-19 17:24:07 -07002320 mCameraSettings.setZoomRatio(mZoomValue);
Angus Kong6607dae2014-06-10 16:07:45 -07002321 mCameraDevice.applySettings(mCameraSettings);
Angus Kongce5480e2013-01-29 17:43:48 -08002322 }
2323
2324 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08002325 public int getCameraState() {
2326 return mCameraState;
2327 }
2328
2329 @Override
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08002330 public void onMemoryStateChanged(int state) {
Erin Dahlgren667630d2014-04-01 14:03:25 -07002331 mAppController.setShutterEnabled(state == MemoryManager.STATE_OK);
Angus Kongce5480e2013-01-29 17:43:48 -08002332 }
Angus Kong86d36312013-01-31 18:22:44 -08002333
2334 @Override
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08002335 public void onLowMemory() {
2336 // Not much we can do in the photo module.
Angus Kong86d36312013-01-31 18:22:44 -08002337 }
Angus Kong0d00a892013-03-26 11:40:40 -07002338
2339 @Override
2340 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2341 }
2342
2343 @Override
2344 public void onSensorChanged(SensorEvent event) {
2345 int type = event.sensor.getType();
2346 float[] data;
2347 if (type == Sensor.TYPE_ACCELEROMETER) {
2348 data = mGData;
2349 } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
2350 data = mMData;
2351 } else {
2352 // we should not be here.
2353 return;
2354 }
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08002355 for (int i = 0; i < 3; i++) {
Angus Kong0d00a892013-03-26 11:40:40 -07002356 data[i] = event.values[i];
2357 }
2358 float[] orientation = new float[3];
2359 SensorManager.getRotationMatrix(mR, null, mGData, mMData);
2360 SensorManager.getOrientation(mR, orientation);
2361 mHeading = (int) (orientation[0] * 180f / Math.PI) % 360;
2362 if (mHeading < 0) {
2363 mHeading += 360;
2364 }
Angus Kong0d00a892013-03-26 11:40:40 -07002365 }
Doris Liu6432cd62013-06-13 17:20:31 -07002366
Ruben Brunkd217ed02013-10-08 23:31:13 -07002367 // For debugging only.
2368 public void setDebugUri(Uri uri) {
2369 mDebugUri = uri;
2370 }
2371
2372 // For debugging only.
2373 private void saveToDebugUri(byte[] data) {
2374 if (mDebugUri != null) {
2375 OutputStream outputStream = null;
2376 try {
2377 outputStream = mContentResolver.openOutputStream(mDebugUri);
2378 outputStream.write(data);
2379 outputStream.close();
2380 } catch (IOException e) {
2381 Log.e(TAG, "Exception while writing debug jpeg file", e);
2382 } finally {
2383 CameraUtil.closeSilently(outputStream);
2384 }
2385 }
2386 }
Sascha Haeberlingf2994f02014-01-23 19:55:01 -08002387
2388 @Override
2389 public void onRemoteShutterPress() {
Sascha Haeberling57d4f742014-06-25 11:05:49 -07002390 mHandler.post(new Runnable() {
2391 @Override
2392 public void run() {
2393 focusAndCapture();
2394 }
2395 });
Sascha Haeberlingf2994f02014-01-23 19:55:01 -08002396 }
Michael Kolb8872c232013-01-29 10:33:22 -08002397}