blob: b7dedd2fb0dad579585edc25ce679cddb8a2e055 [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.ContentProviderClient;
22import android.content.ContentResolver;
Angus Kong0d00a892013-03-26 11:40:40 -070023import android.content.Context;
Michael Kolb8872c232013-01-29 10:33:22 -080024import android.content.Intent;
25import android.content.SharedPreferences.Editor;
26import android.content.res.Configuration;
27import android.graphics.Bitmap;
28import android.graphics.SurfaceTexture;
29import android.hardware.Camera.CameraInfo;
Michael Kolb8872c232013-01-29 10:33:22 -080030import android.hardware.Camera.Parameters;
Michael Kolb8872c232013-01-29 10:33:22 -080031import android.hardware.Camera.Size;
Angus Kong0d00a892013-03-26 11:40:40 -070032import android.hardware.Sensor;
33import android.hardware.SensorEvent;
34import android.hardware.SensorEventListener;
35import android.hardware.SensorManager;
Michael Kolb8872c232013-01-29 10:33:22 -080036import android.location.Location;
37import android.media.CameraProfile;
38import android.net.Uri;
Sascha Haeberling638e6f02013-09-18 14:28:51 -070039import android.os.Build;
Michael Kolb8872c232013-01-29 10:33:22 -080040import android.os.Bundle;
Michael Kolb8872c232013-01-29 10:33:22 -080041import android.os.Handler;
42import android.os.Looper;
43import android.os.Message;
44import android.os.MessageQueue;
45import android.os.SystemClock;
46import android.provider.MediaStore;
47import android.util.Log;
Michael Kolb8872c232013-01-29 10:33:22 -080048import android.view.KeyEvent;
Michael Kolb8872c232013-01-29 10:33:22 -080049import android.view.OrientationEventListener;
Michael Kolb8872c232013-01-29 10:33:22 -080050import android.view.View;
Michael Kolb8872c232013-01-29 10:33:22 -080051import android.view.WindowManager;
Michael Kolb8872c232013-01-29 10:33:22 -080052
Angus Kong9ef99252013-07-18 18:04:19 -070053import com.android.camera.CameraManager.CameraAFCallback;
54import com.android.camera.CameraManager.CameraAFMoveCallback;
55import com.android.camera.CameraManager.CameraPictureCallback;
Michael Kolb8872c232013-01-29 10:33:22 -080056import com.android.camera.CameraManager.CameraProxy;
Angus Kong9ef99252013-07-18 18:04:19 -070057import com.android.camera.CameraManager.CameraShutterCallback;
Ruben Brunka9d66bd2013-09-06 11:56:32 -070058import com.android.camera.PhotoModule.NamedImages.NamedEntity;
ztenghuia16e7b52013-08-23 11:47:56 -070059import com.android.camera.exif.ExifInterface;
60import com.android.camera.exif.ExifTag;
61import com.android.camera.exif.Rational;
Michael Kolbd6954f32013-03-08 20:43:01 -080062import com.android.camera.ui.CountDownView.OnCountDownFinishedListener;
Ruben Brunk4601f5d2013-09-24 18:35:22 -070063import com.android.camera.ui.ModuleSwitcher;
Michael Kolb8872c232013-01-29 10:33:22 -080064import com.android.camera.ui.RotateTextToast;
Angus Kongdcccc512013-08-08 17:06:03 -070065import com.android.camera.util.ApiHelper;
Angus Kongb50b5cb2013-08-09 14:55:20 -070066import com.android.camera.util.CameraUtil;
Ruben Brunk4601f5d2013-09-24 18:35:22 -070067import com.android.camera.util.GcamHelper;
Sascha Haeberling8e963a52013-08-06 11:43:02 -070068import com.android.camera.util.UsageStatistics;
69import com.android.camera2.R;
Michael Kolb8872c232013-01-29 10:33:22 -080070
Angus Kongdcccc512013-08-08 17:06:03 -070071import java.io.File;
72import java.io.FileNotFoundException;
73import java.io.FileOutputStream;
74import java.io.IOException;
75import java.io.OutputStream;
Angus Kongdcccc512013-08-08 17:06:03 -070076import java.util.List;
Ruben Brunka9d66bd2013-09-06 11:56:32 -070077import java.util.Vector;
Angus Kongdcccc512013-08-08 17:06:03 -070078
Michael Kolb8872c232013-01-29 10:33:22 -080079public class PhotoModule
Dan Aminzade92ae10e2013-08-13 14:44:25 -070080 implements CameraModule,
81 PhotoController,
82 FocusOverlayManager.Listener,
83 CameraPreference.OnPreferenceChangedListener,
84 ShutterButton.OnShutterButtonListener,
85 MediaSaveService.Listener,
86 OnCountDownFinishedListener,
87 SensorEventListener {
Michael Kolb8872c232013-01-29 10:33:22 -080088
89 private static final String TAG = "CAM_PhotoModule";
90
91 // We number the request code from 1000 to avoid collision with Gallery.
92 private static final int REQUEST_CROP = 1000;
93
94 private static final int SETUP_PREVIEW = 1;
95 private static final int FIRST_TIME_INIT = 2;
96 private static final int CLEAR_SCREEN_DELAY = 3;
97 private static final int SET_CAMERA_PARAMETERS_WHEN_IDLE = 4;
Angus Kongdcccc512013-08-08 17:06:03 -070098 private static final int SHOW_TAP_TO_FOCUS_TOAST = 5;
99 private static final int SWITCH_CAMERA = 6;
100 private static final int SWITCH_CAMERA_START_ANIMATION = 7;
101 private static final int CAMERA_OPEN_DONE = 8;
102 private static final int OPEN_CAMERA_FAIL = 9;
103 private static final int CAMERA_DISABLED = 10;
Michael Kolb8872c232013-01-29 10:33:22 -0800104
105 // The subset of parameters we need to update in setCameraParameters().
106 private static final int UPDATE_PARAM_INITIALIZE = 1;
107 private static final int UPDATE_PARAM_ZOOM = 2;
108 private static final int UPDATE_PARAM_PREFERENCE = 4;
109 private static final int UPDATE_PARAM_ALL = -1;
110
111 // This is the timeout to keep the camera in onPause for the first time
112 // after screen on if the activity is started from secure lock screen.
113 private static final int KEEP_CAMERA_TIMEOUT = 1000; // ms
114
115 // copied from Camera hierarchy
116 private CameraActivity mActivity;
Michael Kolb8872c232013-01-29 10:33:22 -0800117 private CameraProxy mCameraDevice;
118 private int mCameraId;
119 private Parameters mParameters;
120 private boolean mPaused;
Michael Kolbd6954f32013-03-08 20:43:01 -0800121
122 private PhotoUI mUI;
Michael Kolb8872c232013-01-29 10:33:22 -0800123
Michael Kolb8872c232013-01-29 10:33:22 -0800124 // The activity is going to switch to the specified camera id. This is
125 // needed because texture copy is done in GL thread. -1 means camera is not
126 // switching.
127 protected int mPendingSwitchCameraId = -1;
128 private boolean mOpenCameraFail;
129 private boolean mCameraDisabled;
130
131 // When setCameraParametersWhenIdle() is called, we accumulate the subsets
132 // needed to be updated in mUpdateSet.
133 private int mUpdateSet;
134
135 private static final int SCREEN_DELAY = 2 * 60 * 1000;
136
137 private int mZoomValue; // The current zoom value.
Michael Kolb8872c232013-01-29 10:33:22 -0800138
139 private Parameters mInitialParams;
140 private boolean mFocusAreaSupported;
141 private boolean mMeteringAreaSupported;
142 private boolean mAeLockSupported;
143 private boolean mAwbLockSupported;
Angus Kongdcccc512013-08-08 17:06:03 -0700144 private boolean mContinuousFocusSupported;
Michael Kolb8872c232013-01-29 10:33:22 -0800145
146 // The degrees of the device rotated clockwise from its natural orientation.
147 private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
148 private ComboPreferences mPreferences;
149
150 private static final String sTempCropFilename = "crop-temp";
151
152 private ContentProviderClient mMediaProviderClient;
Michael Kolb8872c232013-01-29 10:33:22 -0800153 private boolean mFaceDetectionStarted = false;
154
Michael Kolb8872c232013-01-29 10:33:22 -0800155 // mCropValue and mSaveUri are used only if isImageCaptureIntent() is true.
156 private String mCropValue;
157 private Uri mSaveUri;
158
Ruben Brunkd217ed02013-10-08 23:31:13 -0700159 private Uri mDebugUri;
160
Angus Kongce5480e2013-01-29 17:43:48 -0800161 // We use a queue to generated names of the images to be used later
162 // when the image is ready to be saved.
Michael Kolb8872c232013-01-29 10:33:22 -0800163 private NamedImages mNamedImages;
164
165 private Runnable mDoSnapRunnable = new Runnable() {
166 @Override
167 public void run() {
168 onShutterButtonClick();
169 }
170 };
171
Michael Kolb8872c232013-01-29 10:33:22 -0800172 /**
173 * An unpublished intent flag requesting to return as soon as capturing
174 * is completed.
175 *
176 * TODO: consider publishing by moving into MediaStore.
177 */
178 private static final String EXTRA_QUICK_CAPTURE =
179 "android.intent.extra.quickCapture";
180
181 // The display rotation in degrees. This is only valid when mCameraState is
182 // not PREVIEW_STOPPED.
183 private int mDisplayRotation;
184 // The value for android.hardware.Camera.setDisplayOrientation.
185 private int mCameraDisplayOrientation;
186 // The value for UI components like indicators.
187 private int mDisplayOrientation;
188 // The value for android.hardware.Camera.Parameters.setRotation.
189 private int mJpegRotation;
Doris Liu29da2db2013-08-28 14:28:45 -0700190 // Indicates whether we are using front camera
191 private boolean mMirror;
Michael Kolb8872c232013-01-29 10:33:22 -0800192 private boolean mFirstTimeInitialized;
193 private boolean mIsImageCaptureIntent;
194
Michael Kolb8872c232013-01-29 10:33:22 -0800195 private int mCameraState = PREVIEW_STOPPED;
196 private boolean mSnapshotOnIdle = false;
197
198 private ContentResolver mContentResolver;
199
200 private LocationManager mLocationManager;
201
Michael Kolb8872c232013-01-29 10:33:22 -0800202 private final PostViewPictureCallback mPostViewPictureCallback =
203 new PostViewPictureCallback();
204 private final RawPictureCallback mRawPictureCallback =
205 new RawPictureCallback();
206 private final AutoFocusCallback mAutoFocusCallback =
207 new AutoFocusCallback();
208 private final Object mAutoFocusMoveCallback =
209 ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700210 ? new AutoFocusMoveCallback()
211 : null;
Michael Kolb8872c232013-01-29 10:33:22 -0800212
213 private final CameraErrorCallback mErrorCallback = new CameraErrorCallback();
214
215 private long mFocusStartTime;
216 private long mShutterCallbackTime;
217 private long mPostViewPictureCallbackTime;
218 private long mRawPictureCallbackTime;
219 private long mJpegPictureCallbackTime;
220 private long mOnResumeTime;
221 private byte[] mJpegImageData;
222
223 // These latency time are for the CameraLatency test.
224 public long mAutoFocusTime;
225 public long mShutterLag;
226 public long mShutterToPictureDisplayedTime;
227 public long mPictureDisplayedToJpegCallbackTime;
228 public long mJpegCallbackFinishTime;
229 public long mCaptureStartTime;
230
231 // This handles everything about focus.
232 private FocusOverlayManager mFocusManager;
233
Michael Kolb8872c232013-01-29 10:33:22 -0800234 private String mSceneMode;
Michael Kolb8872c232013-01-29 10:33:22 -0800235
236 private final Handler mHandler = new MainHandler();
237 private PreferenceGroup mPreferenceGroup;
238
239 private boolean mQuickCapture;
Angus Kong0d00a892013-03-26 11:40:40 -0700240 private SensorManager mSensorManager;
241 private float[] mGData = new float[3];
242 private float[] mMData = new float[3];
243 private float[] mR = new float[16];
244 private int mHeading = -1;
245
Angus Kongdcccc512013-08-08 17:06:03 -0700246 // True if all the parameters needed to start preview is ready.
247 private boolean mCameraPreviewParamsReady = false;
Doris Liu6432cd62013-06-13 17:20:31 -0700248
Angus Kongce5480e2013-01-29 17:43:48 -0800249 private MediaSaveService.OnMediaSavedListener mOnMediaSavedListener =
250 new MediaSaveService.OnMediaSavedListener() {
251 @Override
252 public void onMediaSaved(Uri uri) {
253 if (uri != null) {
Doris Liu6432cd62013-06-13 17:20:31 -0700254 mActivity.notifyNewMedia(uri);
Angus Kongce5480e2013-01-29 17:43:48 -0800255 }
256 }
257 };
Michael Kolb8872c232013-01-29 10:33:22 -0800258
Angus Kongdcccc512013-08-08 17:06:03 -0700259 private void checkDisplayRotation() {
260 // Set the display orientation if display rotation has changed.
261 // Sometimes this happens when the device is held upside
262 // down and camera app is opened. Rotation animation will
263 // take some time and the rotation value we have got may be
264 // wrong. Framework does not have a callback for this now.
265 if (CameraUtil.getDisplayRotation(mActivity) != mDisplayRotation) {
266 setDisplayOrientation();
Michael Kolb8872c232013-01-29 10:33:22 -0800267 }
Angus Kongdcccc512013-08-08 17:06:03 -0700268 if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) {
269 mHandler.postDelayed(new Runnable() {
270 @Override
271 public void run() {
272 checkDisplayRotation();
273 }
274 }, 100);
Michael Kolb8872c232013-01-29 10:33:22 -0800275 }
276 }
277
278 /**
279 * This Handler is used to post message back onto the main thread of the
280 * application
281 */
282 private class MainHandler extends Handler {
283 @Override
284 public void handleMessage(Message msg) {
285 switch (msg.what) {
286 case SETUP_PREVIEW: {
287 setupPreview();
288 break;
289 }
290
291 case CLEAR_SCREEN_DELAY: {
292 mActivity.getWindow().clearFlags(
293 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
294 break;
295 }
296
297 case FIRST_TIME_INIT: {
298 initializeFirstTime();
299 break;
300 }
301
302 case SET_CAMERA_PARAMETERS_WHEN_IDLE: {
303 setCameraParametersWhenIdle(0);
304 break;
305 }
306
Michael Kolb8872c232013-01-29 10:33:22 -0800307 case SHOW_TAP_TO_FOCUS_TOAST: {
308 showTapToFocusToast();
309 break;
310 }
311
312 case SWITCH_CAMERA: {
313 switchCamera();
314 break;
315 }
316
317 case SWITCH_CAMERA_START_ANIMATION: {
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700318 // TODO: Need to revisit
319 // ((CameraScreenNail) mActivity.mCameraScreenNail).animateSwitchCamera();
Michael Kolb8872c232013-01-29 10:33:22 -0800320 break;
321 }
322
323 case CAMERA_OPEN_DONE: {
Michael Kolbd6954f32013-03-08 20:43:01 -0800324 onCameraOpened();
Michael Kolb8872c232013-01-29 10:33:22 -0800325 break;
326 }
327
Michael Kolb8872c232013-01-29 10:33:22 -0800328 case OPEN_CAMERA_FAIL: {
Michael Kolb8872c232013-01-29 10:33:22 -0800329 mOpenCameraFail = true;
Angus Kongb50b5cb2013-08-09 14:55:20 -0700330 CameraUtil.showErrorAndFinish(mActivity,
Michael Kolb8872c232013-01-29 10:33:22 -0800331 R.string.cannot_connect_camera);
332 break;
333 }
334
335 case CAMERA_DISABLED: {
Michael Kolb8872c232013-01-29 10:33:22 -0800336 mCameraDisabled = true;
Angus Kongb50b5cb2013-08-09 14:55:20 -0700337 CameraUtil.showErrorAndFinish(mActivity,
Michael Kolb8872c232013-01-29 10:33:22 -0800338 R.string.camera_disabled);
339 break;
340 }
Michael Kolb8872c232013-01-29 10:33:22 -0800341 }
342 }
343 }
344
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700345
Michael Kolb8872c232013-01-29 10:33:22 -0800346 @Override
Doris Liu6432cd62013-06-13 17:20:31 -0700347 public void init(CameraActivity activity, View parent) {
Michael Kolb8872c232013-01-29 10:33:22 -0800348 mActivity = activity;
Michael Kolbd6954f32013-03-08 20:43:01 -0800349 mUI = new PhotoUI(activity, this, parent);
Michael Kolb8872c232013-01-29 10:33:22 -0800350 mPreferences = new ComboPreferences(mActivity);
351 CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal());
352 mCameraId = getPreferredCameraId(mPreferences);
353
354 mContentResolver = mActivity.getContentResolver();
355
Michael Kolb8872c232013-01-29 10:33:22 -0800356 // Surface texture is from camera screen nail and startPreview needs it.
357 // This must be done before startPreview.
358 mIsImageCaptureIntent = isImageCaptureIntent();
Michael Kolb8872c232013-01-29 10:33:22 -0800359
360 mPreferences.setLocalId(mActivity, mCameraId);
361 CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
362 // we need to reset exposure for the preview
363 resetExposureCompensation();
Michael Kolb8872c232013-01-29 10:33:22 -0800364
365 initializeControlByIntent();
366 mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
Michael Kolbd6954f32013-03-08 20:43:01 -0800367 mLocationManager = new LocationManager(mActivity, mUI);
Angus Kong0d00a892013-03-26 11:40:40 -0700368 mSensorManager = (SensorManager)(mActivity.getSystemService(Context.SENSOR_SERVICE));
Michael Kolbd6954f32013-03-08 20:43:01 -0800369 }
370
371 private void initializeControlByIntent() {
372 mUI.initializeControlByIntent();
373 if (mIsImageCaptureIntent) {
374 setupCaptureParams();
375 }
376 }
377
378 private void onPreviewStarted() {
Michael Kolbd6954f32013-03-08 20:43:01 -0800379 setCameraState(IDLE);
Michael Kolbd6954f32013-03-08 20:43:01 -0800380 startFaceDetection();
381 locationFirstRun();
Michael Kolb8872c232013-01-29 10:33:22 -0800382 }
383
384 // Prompt the user to pick to record location for the very first run of
385 // camera only
386 private void locationFirstRun() {
387 if (RecordLocationPreference.isSet(mPreferences)) {
388 return;
389 }
390 if (mActivity.isSecureCamera()) return;
391 // Check if the back camera exists
392 int backCameraId = CameraHolder.instance().getBackCameraId();
393 if (backCameraId == -1) {
394 // If there is no back camera, do not show the prompt.
395 return;
396 }
Doris Liu6a83d522013-07-02 12:03:32 -0700397 mUI.showLocationDialog();
398 }
Michael Kolb8872c232013-01-29 10:33:22 -0800399
ztenghui7b265a62013-09-09 14:58:44 -0700400 @Override
Doris Liu6a83d522013-07-02 12:03:32 -0700401 public void enableRecordingLocation(boolean enable) {
402 setLocationPreference(enable ? RecordLocationPreference.VALUE_ON
403 : RecordLocationPreference.VALUE_OFF);
Michael Kolb8872c232013-01-29 10:33:22 -0800404 }
405
Angus Kongdcccc512013-08-08 17:06:03 -0700406 @Override
407 public void onPreviewUIReady() {
408 startPreview();
409 }
410
411 @Override
412 public void onPreviewUIDestroyed() {
413 if (mCameraDevice == null) {
414 return;
415 }
416 mCameraDevice.setPreviewTexture(null);
417 stopPreview();
418 }
419
Michael Kolb8872c232013-01-29 10:33:22 -0800420 private void setLocationPreference(String value) {
421 mPreferences.edit()
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700422 .putString(CameraSettings.KEY_RECORD_LOCATION, value)
423 .apply();
Michael Kolb8872c232013-01-29 10:33:22 -0800424 // TODO: Fix this to use the actual onSharedPreferencesChanged listener
425 // instead of invoking manually
426 onSharedPreferenceChanged();
427 }
428
Michael Kolbd6954f32013-03-08 20:43:01 -0800429 private void onCameraOpened() {
430 View root = mUI.getRootView();
Michael Kolb8872c232013-01-29 10:33:22 -0800431 // These depend on camera parameters.
Michael Kolbd6954f32013-03-08 20:43:01 -0800432
433 int width = root.getWidth();
434 int height = root.getHeight();
Doris Liu6a0de792013-02-26 10:54:25 -0800435 mFocusManager.setPreviewSize(width, height);
Michael Kolbd6954f32013-03-08 20:43:01 -0800436 openCameraCommon();
Michael Kolb8872c232013-01-29 10:33:22 -0800437 }
438
Michael Kolbd6954f32013-03-08 20:43:01 -0800439 private void switchCamera() {
440 if (mPaused) return;
441
442 Log.v(TAG, "Start to switch camera. id=" + mPendingSwitchCameraId);
443 mCameraId = mPendingSwitchCameraId;
444 mPendingSwitchCameraId = -1;
445 setCameraId(mCameraId);
446
447 // from onPause
448 closeCamera();
449 mUI.collapseCameraControls();
450 mUI.clearFaces();
451 if (mFocusManager != null) mFocusManager.removeMessages();
452
453 // Restart the camera and initialize the UI. From onCreate.
454 mPreferences.setLocalId(mActivity, mCameraId);
455 CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
Angus Kong4f795b82013-09-16 14:25:35 -0700456 mCameraDevice = CameraUtil.openCamera(
457 mActivity, mCameraId, mHandler,
458 mActivity.getCameraOpenErrorCallback());
459 if (mCameraDevice == null) {
460 Log.e(TAG, "Failed to open camera:" + mCameraId + ", aborting.");
Michael Kolbd6954f32013-03-08 20:43:01 -0800461 return;
Doris Liu09106a42013-03-05 09:54:25 -0800462 }
Angus Kong4f795b82013-09-16 14:25:35 -0700463 mParameters = mCameraDevice.getParameters();
Michael Kolbd6954f32013-03-08 20:43:01 -0800464 initializeCapabilities();
465 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
Doris Liu29da2db2013-08-28 14:28:45 -0700466 mMirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
467 mFocusManager.setMirror(mMirror);
Michael Kolbd6954f32013-03-08 20:43:01 -0800468 mFocusManager.setParameters(mInitialParams);
469 setupPreview();
470
Doris Liu6432cd62013-06-13 17:20:31 -0700471 // reset zoom value index
472 mZoomValue = 0;
Michael Kolbd6954f32013-03-08 20:43:01 -0800473 openCameraCommon();
474
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700475 // Start switch camera animation. Post a message because
476 // onFrameAvailable from the old camera may already exist.
477 mHandler.sendEmptyMessage(SWITCH_CAMERA_START_ANIMATION);
Doris Liu48239f42013-03-04 22:19:10 -0800478 }
479
Michael Kolbd6954f32013-03-08 20:43:01 -0800480 protected void setCameraId(int cameraId) {
481 ListPreference pref = mPreferenceGroup.findPreference(CameraSettings.KEY_CAMERA_ID);
482 pref.setValue("" + cameraId);
483 }
484
485 // either open a new camera or switch cameras
486 private void openCameraCommon() {
Michael Kolb8872c232013-01-29 10:33:22 -0800487 loadCameraPreferences();
Michael Kolbd6954f32013-03-08 20:43:01 -0800488
489 mUI.onCameraOpened(mPreferenceGroup, mPreferences, mParameters, this);
Angus Kong0fb819b2013-10-08 13:44:19 -0700490 if (mIsImageCaptureIntent) {
491 mUI.overrideSettings(CameraSettings.KEY_CAMERA_HDR_PLUS,
492 mActivity.getString(R.string.setting_off_value));
493 }
Michael Kolbd6954f32013-03-08 20:43:01 -0800494 updateSceneMode();
495 showTapToFocusToastIfNeeded();
496
497
Michael Kolb8872c232013-01-29 10:33:22 -0800498 }
499
ztenghui7b265a62013-09-09 14:58:44 -0700500 @Override
Doris Liub3749f22013-09-25 12:22:08 -0700501 public void onScreenSizeChanged(int width, int height) {
Michael Kolbd6954f32013-03-08 20:43:01 -0800502 if (mFocusManager != null) mFocusManager.setPreviewSize(width, height);
Michael Kolbd6954f32013-03-08 20:43:01 -0800503 }
Michael Kolb8872c232013-01-29 10:33:22 -0800504
505 private void resetExposureCompensation() {
506 String value = mPreferences.getString(CameraSettings.KEY_EXPOSURE,
507 CameraSettings.EXPOSURE_DEFAULT_VALUE);
508 if (!CameraSettings.EXPOSURE_DEFAULT_VALUE.equals(value)) {
509 Editor editor = mPreferences.edit();
510 editor.putString(CameraSettings.KEY_EXPOSURE, "0");
511 editor.apply();
512 }
513 }
514
515 private void keepMediaProviderInstance() {
516 // We want to keep a reference to MediaProvider in camera's lifecycle.
517 // TODO: Utilize mMediaProviderClient instance to replace
518 // ContentResolver calls.
519 if (mMediaProviderClient == null) {
520 mMediaProviderClient = mContentResolver
521 .acquireContentProviderClient(MediaStore.AUTHORITY);
522 }
523 }
524
525 // Snapshots can only be taken after this is called. It should be called
526 // once only. We could have done these things in onCreate() but we want to
527 // make preview screen appear as soon as possible.
528 private void initializeFirstTime() {
Sascha Haeberling330dafb2013-10-10 19:00:41 -0700529 if (mFirstTimeInitialized || mPaused) {
530 return;
531 }
Michael Kolb8872c232013-01-29 10:33:22 -0800532
533 // Initialize location service.
534 boolean recordLocation = RecordLocationPreference.get(
535 mPreferences, mContentResolver);
536 mLocationManager.recordLocation(recordLocation);
537
538 keepMediaProviderInstance();
539
Michael Kolbd6954f32013-03-08 20:43:01 -0800540 mUI.initializeFirstTime();
Angus Kong86d36312013-01-31 18:22:44 -0800541 MediaSaveService s = mActivity.getMediaSaveService();
542 // We set the listener only when both service and shutterbutton
543 // are initialized.
544 if (s != null) {
545 s.setListener(this);
546 }
Michael Kolb8872c232013-01-29 10:33:22 -0800547
Michael Kolb8872c232013-01-29 10:33:22 -0800548 mNamedImages = new NamedImages();
549
550 mFirstTimeInitialized = true;
551 addIdleHandler();
552
553 mActivity.updateStorageSpaceAndHint();
554 }
555
Michael Kolbd6954f32013-03-08 20:43:01 -0800556 // If the activity is paused and resumed, this method will be called in
557 // onResume.
558 private void initializeSecondTime() {
559 // Start location update if needed.
560 boolean recordLocation = RecordLocationPreference.get(
561 mPreferences, mContentResolver);
562 mLocationManager.recordLocation(recordLocation);
563 MediaSaveService s = mActivity.getMediaSaveService();
564 if (s != null) {
565 s.setListener(this);
566 }
567 mNamedImages = new NamedImages();
568 mUI.initializeSecondTime(mParameters);
569 keepMediaProviderInstance();
570 }
571
Michael Kolb8872c232013-01-29 10:33:22 -0800572 private void showTapToFocusToastIfNeeded() {
573 // Show the tap to focus toast if this is the first start.
574 if (mFocusAreaSupported &&
575 mPreferences.getBoolean(CameraSettings.KEY_CAMERA_FIRST_USE_HINT_SHOWN, true)) {
576 // Delay the toast for one second to wait for orientation.
577 mHandler.sendEmptyMessageDelayed(SHOW_TAP_TO_FOCUS_TOAST, 1000);
578 }
579 }
580
581 private void addIdleHandler() {
582 MessageQueue queue = Looper.myQueue();
583 queue.addIdleHandler(new MessageQueue.IdleHandler() {
584 @Override
585 public boolean queueIdle() {
586 Storage.ensureOSXCompatible();
587 return false;
588 }
589 });
590 }
591
Michael Kolb8872c232013-01-29 10:33:22 -0800592 @Override
593 public void startFaceDetection() {
Michael Kolb8872c232013-01-29 10:33:22 -0800594 if (mFaceDetectionStarted) return;
595 if (mParameters.getMaxNumDetectedFaces() > 0) {
596 mFaceDetectionStarted = true;
Michael Kolb8872c232013-01-29 10:33:22 -0800597 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
Michael Kolbd6954f32013-03-08 20:43:01 -0800598 mUI.onStartFaceDetection(mDisplayOrientation,
599 (info.facing == CameraInfo.CAMERA_FACING_FRONT));
Angus Kong9e765522013-07-31 14:05:20 -0700600 mCameraDevice.setFaceDetectionCallback(mHandler, mUI);
Michael Kolb8872c232013-01-29 10:33:22 -0800601 mCameraDevice.startFaceDetection();
602 }
603 }
604
Michael Kolb8872c232013-01-29 10:33:22 -0800605 @Override
606 public void stopFaceDetection() {
Michael Kolb8872c232013-01-29 10:33:22 -0800607 if (!mFaceDetectionStarted) return;
608 if (mParameters.getMaxNumDetectedFaces() > 0) {
609 mFaceDetectionStarted = false;
Angus Kong9e765522013-07-31 14:05:20 -0700610 mCameraDevice.setFaceDetectionCallback(null, null);
Michael Kolb8872c232013-01-29 10:33:22 -0800611 mCameraDevice.stopFaceDetection();
Michael Kolbd6954f32013-03-08 20:43:01 -0800612 mUI.clearFaces();
Michael Kolb8872c232013-01-29 10:33:22 -0800613 }
614 }
615
Michael Kolb8872c232013-01-29 10:33:22 -0800616 private final class ShutterCallback
Angus Kong9ef99252013-07-18 18:04:19 -0700617 implements CameraShutterCallback {
Angus Kongdcb0ef12013-03-25 23:11:43 -0700618
Sascha Haeberling37f36112013-08-06 14:31:52 -0700619 private boolean mNeedsAnimation;
Angus Kongdcb0ef12013-03-25 23:11:43 -0700620
Sascha Haeberling37f36112013-08-06 14:31:52 -0700621 public ShutterCallback(boolean needsAnimation) {
622 mNeedsAnimation = needsAnimation;
Angus Kongdcb0ef12013-03-25 23:11:43 -0700623 }
624
Michael Kolb8872c232013-01-29 10:33:22 -0800625 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700626 public void onShutter(CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800627 mShutterCallbackTime = System.currentTimeMillis();
628 mShutterLag = mShutterCallbackTime - mCaptureStartTime;
629 Log.v(TAG, "mShutterLag = " + mShutterLag + "ms");
Sascha Haeberling37f36112013-08-06 14:31:52 -0700630 if (mNeedsAnimation) {
631 mActivity.runOnUiThread(new Runnable() {
632 @Override
633 public void run() {
634 animateAfterShutter();
635 }
636 });
Angus Kongdcb0ef12013-03-25 23:11:43 -0700637 }
Michael Kolb8872c232013-01-29 10:33:22 -0800638 }
639 }
640
Angus Kong9ef99252013-07-18 18:04:19 -0700641 private final class PostViewPictureCallback
642 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800643 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700644 public void onPictureTaken(byte [] data, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800645 mPostViewPictureCallbackTime = System.currentTimeMillis();
646 Log.v(TAG, "mShutterToPostViewCallbackTime = "
647 + (mPostViewPictureCallbackTime - mShutterCallbackTime)
648 + "ms");
649 }
650 }
651
Angus Kong9ef99252013-07-18 18:04:19 -0700652 private final class RawPictureCallback
653 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800654 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700655 public void onPictureTaken(byte [] rawData, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800656 mRawPictureCallbackTime = System.currentTimeMillis();
657 Log.v(TAG, "mShutterToRawCallbackTime = "
658 + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms");
659 }
660 }
661
Angus Kong9ef99252013-07-18 18:04:19 -0700662 private final class JpegPictureCallback
663 implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800664 Location mLocation;
665
666 public JpegPictureCallback(Location loc) {
667 mLocation = loc;
668 }
669
670 @Override
Angus Kong9ef99252013-07-18 18:04:19 -0700671 public void onPictureTaken(final byte [] jpegData, CameraProxy camera) {
Sascha Haeberling88901942013-08-28 17:49:00 -0700672 mUI.enableShutter(true);
Michael Kolb8872c232013-01-29 10:33:22 -0800673 if (mPaused) {
674 return;
675 }
Doris Liu6432cd62013-06-13 17:20:31 -0700676 if (mIsImageCaptureIntent) {
677 stopPreview();
678 }
Angus Kongb50b5cb2013-08-09 14:55:20 -0700679 if (mSceneMode == CameraUtil.SCENE_MODE_HDR) {
Doris Liu6432cd62013-06-13 17:20:31 -0700680 mUI.showSwitcher();
681 mUI.setSwipingEnabled(true);
Michael Kolb8872c232013-01-29 10:33:22 -0800682 }
683
684 mJpegPictureCallbackTime = System.currentTimeMillis();
685 // If postview callback has arrived, the captured image is displayed
686 // in postview callback. If not, the captured image is displayed in
687 // raw picture callback.
688 if (mPostViewPictureCallbackTime != 0) {
689 mShutterToPictureDisplayedTime =
690 mPostViewPictureCallbackTime - mShutterCallbackTime;
691 mPictureDisplayedToJpegCallbackTime =
692 mJpegPictureCallbackTime - mPostViewPictureCallbackTime;
693 } else {
694 mShutterToPictureDisplayedTime =
695 mRawPictureCallbackTime - mShutterCallbackTime;
696 mPictureDisplayedToJpegCallbackTime =
697 mJpegPictureCallbackTime - mRawPictureCallbackTime;
698 }
699 Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = "
700 + mPictureDisplayedToJpegCallbackTime + "ms");
701
Michael Kolb8872c232013-01-29 10:33:22 -0800702 mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden.
703 if (!mIsImageCaptureIntent) {
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700704 setupPreview();
Michael Kolb8872c232013-01-29 10:33:22 -0800705 }
706
Doris Liu36e56fb2013-09-11 17:38:08 -0700707 ExifInterface exif = Exif.getExif(jpegData);
708 int orientation = Exif.getOrientation(exif);
Ruben Brunkd217ed02013-10-08 23:31:13 -0700709
710 if (mDebugUri != null) {
711 // If using a debug uri, save jpeg there
712 saveToDebugUri(jpegData);
713 }else if (!mIsImageCaptureIntent) {
Michael Kolb8872c232013-01-29 10:33:22 -0800714 // Calculate the width and the height of the jpeg.
715 Size s = mParameters.getPictureSize();
Michael Kolb8872c232013-01-29 10:33:22 -0800716 int width, height;
717 if ((mJpegRotation + orientation) % 180 == 0) {
718 width = s.width;
719 height = s.height;
720 } else {
721 width = s.height;
722 height = s.width;
723 }
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700724 NamedEntity name = mNamedImages.getNextNameEntity();
725 String title = (name == null) ? null : name.title;
726 long date = (name == null) ? -1 : name.date;
Michael Kolb8872c232013-01-29 10:33:22 -0800727 if (title == null) {
728 Log.e(TAG, "Unbalanced name/data pair");
729 } else {
730 if (date == -1) date = mCaptureStartTime;
Angus Kong0d00a892013-03-26 11:40:40 -0700731 if (mHeading >= 0) {
732 // heading direction has been updated by the sensor.
733 ExifTag directionRefTag = exif.buildTag(
734 ExifInterface.TAG_GPS_IMG_DIRECTION_REF,
735 ExifInterface.GpsTrackRef.MAGNETIC_DIRECTION);
736 ExifTag directionTag = exif.buildTag(
737 ExifInterface.TAG_GPS_IMG_DIRECTION,
738 new Rational(mHeading, 1));
739 exif.setTag(directionRefTag);
740 exif.setTag(directionTag);
741 }
Angus Kong86d36312013-01-31 18:22:44 -0800742 mActivity.getMediaSaveService().addImage(
743 jpegData, title, date, mLocation, width, height,
Angus Kong0d00a892013-03-26 11:40:40 -0700744 orientation, exif, mOnMediaSavedListener, mContentResolver);
Michael Kolb8872c232013-01-29 10:33:22 -0800745 }
Doris Liuce2acbc2013-08-21 18:45:29 -0700746 // Animate capture with real jpeg data instead of a preview frame.
Doris Liu29da2db2013-08-28 14:28:45 -0700747 mUI.animateCapture(jpegData, orientation, mMirror);
Michael Kolb8872c232013-01-29 10:33:22 -0800748 } else {
749 mJpegImageData = jpegData;
750 if (!mQuickCapture) {
Doris Liu36e56fb2013-09-11 17:38:08 -0700751 mUI.showCapturedImageForReview(jpegData, orientation, mMirror);
Michael Kolb8872c232013-01-29 10:33:22 -0800752 } else {
Michael Kolbd6954f32013-03-08 20:43:01 -0800753 onCaptureDone();
Michael Kolb8872c232013-01-29 10:33:22 -0800754 }
755 }
756
757 // Check this in advance of each shot so we don't add to shutter
758 // latency. It's true that someone else could write to the SD card in
759 // the mean time and fill it, but that could have happened between the
760 // shutter press and saving the JPEG too.
761 mActivity.updateStorageSpaceAndHint();
762
763 long now = System.currentTimeMillis();
764 mJpegCallbackFinishTime = now - mJpegPictureCallbackTime;
765 Log.v(TAG, "mJpegCallbackFinishTime = "
766 + mJpegCallbackFinishTime + "ms");
767 mJpegPictureCallbackTime = 0;
768 }
769 }
770
Angus Kong9ef99252013-07-18 18:04:19 -0700771 private final class AutoFocusCallback implements CameraAFCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800772 @Override
773 public void onAutoFocus(
Angus Kong9ef99252013-07-18 18:04:19 -0700774 boolean focused, CameraProxy camera) {
Michael Kolb8872c232013-01-29 10:33:22 -0800775 if (mPaused) return;
776
777 mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime;
778 Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms");
779 setCameraState(IDLE);
Michael Kolbd6954f32013-03-08 20:43:01 -0800780 mFocusManager.onAutoFocus(focused, mUI.isShutterPressed());
Michael Kolb8872c232013-01-29 10:33:22 -0800781 }
782 }
783
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700784 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -0800785 private final class AutoFocusMoveCallback
Angus Kong9ef99252013-07-18 18:04:19 -0700786 implements CameraAFMoveCallback {
Michael Kolb8872c232013-01-29 10:33:22 -0800787 @Override
788 public void onAutoFocusMoving(
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700789 boolean moving, CameraProxy camera) {
790 mFocusManager.onAutoFocusMoving(moving);
Michael Kolb8872c232013-01-29 10:33:22 -0800791 }
792 }
793
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700794 /**
795 * This class is just a thread-safe queue for name,date holder objects.
796 */
797 public static class NamedImages {
798 private Vector<NamedEntity> mQueue;
Michael Kolb8872c232013-01-29 10:33:22 -0800799
800 public NamedImages() {
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700801 mQueue = new Vector<NamedEntity>();
Michael Kolb8872c232013-01-29 10:33:22 -0800802 }
803
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700804 public void nameNewImage(long date) {
Michael Kolb8872c232013-01-29 10:33:22 -0800805 NamedEntity r = new NamedEntity();
Angus Kongb50b5cb2013-08-09 14:55:20 -0700806 r.title = CameraUtil.createJpegName(date);
Michael Kolb8872c232013-01-29 10:33:22 -0800807 r.date = date;
808 mQueue.add(r);
809 }
810
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700811 public NamedEntity getNextNameEntity() {
812 synchronized(mQueue) {
813 if (!mQueue.isEmpty()) {
814 return mQueue.remove(0);
815 }
Michael Kolb8872c232013-01-29 10:33:22 -0800816 }
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700817 return null;
Michael Kolb8872c232013-01-29 10:33:22 -0800818 }
819
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700820 public static class NamedEntity {
821 public String title;
822 public long date;
Michael Kolb8872c232013-01-29 10:33:22 -0800823 }
824 }
825
826 private void setCameraState(int state) {
827 mCameraState = state;
828 switch (state) {
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700829 case PhotoController.PREVIEW_STOPPED:
830 case PhotoController.SNAPSHOT_IN_PROGRESS:
831 case PhotoController.SWITCHING_CAMERA:
832 mUI.enableGestures(false);
833 break;
834 case PhotoController.IDLE:
835 mUI.enableGestures(true);
836 break;
Michael Kolb8872c232013-01-29 10:33:22 -0800837 }
838 }
839
Sascha Haeberling37f36112013-08-06 14:31:52 -0700840 private void animateAfterShutter() {
Michael Kolb8872c232013-01-29 10:33:22 -0800841 // Only animate when in full screen capture mode
842 // i.e. If monkey/a user swipes to the gallery during picture taking,
843 // don't show animation
Doris Liuc2e9abd2013-06-19 14:20:51 -0700844 if (!mIsImageCaptureIntent) {
845 mUI.animateFlash();
Doris Liuc2e9abd2013-06-19 14:20:51 -0700846 }
Michael Kolb8872c232013-01-29 10:33:22 -0800847 }
848
849 @Override
850 public boolean capture() {
851 // If we are already in the middle of taking a snapshot or the image save request
852 // is full then ignore.
853 if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS
Angus Kong86d36312013-01-31 18:22:44 -0800854 || mCameraState == SWITCHING_CAMERA
855 || mActivity.getMediaSaveService().isQueueFull()) {
Michael Kolb8872c232013-01-29 10:33:22 -0800856 return false;
857 }
858 mCaptureStartTime = System.currentTimeMillis();
859 mPostViewPictureCallbackTime = 0;
860 mJpegImageData = null;
861
Angus Kongb50b5cb2013-08-09 14:55:20 -0700862 final boolean animateBefore = (mSceneMode == CameraUtil.SCENE_MODE_HDR);
Michael Kolb8872c232013-01-29 10:33:22 -0800863
864 if (animateBefore) {
Sascha Haeberling37f36112013-08-06 14:31:52 -0700865 animateAfterShutter();
Michael Kolb8872c232013-01-29 10:33:22 -0800866 }
867
868 // Set rotation and gps data.
Doris Liu3cf565c2013-02-15 10:55:37 -0800869 int orientation;
870 // We need to be consistent with the framework orientation (i.e. the
871 // orientation of the UI.) when the auto-rotate screen setting is on.
872 if (mActivity.isAutoRotateScreen()) {
873 orientation = (360 - mDisplayRotation) % 360;
874 } else {
875 orientation = mOrientation;
876 }
Angus Kongb50b5cb2013-08-09 14:55:20 -0700877 mJpegRotation = CameraUtil.getJpegRotation(mCameraId, orientation);
Michael Kolb8872c232013-01-29 10:33:22 -0800878 mParameters.setRotation(mJpegRotation);
879 Location loc = mLocationManager.getCurrentLocation();
Angus Kongb50b5cb2013-08-09 14:55:20 -0700880 CameraUtil.setGpsParameters(mParameters, loc);
Michael Kolb8872c232013-01-29 10:33:22 -0800881 mCameraDevice.setParameters(mParameters);
882
Sascha Haeberling88901942013-08-28 17:49:00 -0700883 // We don't want user to press the button again while taking a
884 // multi-second HDR photo.
885 mUI.enableShutter(false);
Angus Kong9ef99252013-07-18 18:04:19 -0700886 mCameraDevice.takePicture(mHandler,
887 new ShutterCallback(!animateBefore),
Angus Kongdcb0ef12013-03-25 23:11:43 -0700888 mRawPictureCallback, mPostViewPictureCallback,
Angus Kong9ef99252013-07-18 18:04:19 -0700889 new JpegPictureCallback(loc));
Michael Kolb8872c232013-01-29 10:33:22 -0800890
Ruben Brunka9d66bd2013-09-06 11:56:32 -0700891 mNamedImages.nameNewImage(mCaptureStartTime);
Michael Kolb8872c232013-01-29 10:33:22 -0800892
893 mFaceDetectionStarted = false;
894 setCameraState(SNAPSHOT_IN_PROGRESS);
Bobby Georgescu301b6462013-04-01 15:33:17 -0700895 UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
896 UsageStatistics.ACTION_CAPTURE_DONE, "Photo");
Michael Kolb8872c232013-01-29 10:33:22 -0800897 return true;
898 }
899
900 @Override
901 public void setFocusParameters() {
902 setCameraParameters(UPDATE_PARAM_PREFERENCE);
903 }
904
905 private int getPreferredCameraId(ComboPreferences preferences) {
Angus Kongb50b5cb2013-08-09 14:55:20 -0700906 int intentCameraId = CameraUtil.getCameraFacingIntentExtras(mActivity);
Michael Kolb8872c232013-01-29 10:33:22 -0800907 if (intentCameraId != -1) {
908 // Testing purpose. Launch a specific camera through the intent
909 // extras.
910 return intentCameraId;
911 } else {
912 return CameraSettings.readPreferredCameraId(preferences);
913 }
914 }
915
Michael Kolbd6954f32013-03-08 20:43:01 -0800916 private void updateSceneMode() {
Michael Kolb8872c232013-01-29 10:33:22 -0800917 // If scene mode is set, we cannot set flash mode, white balance, and
918 // focus mode, instead, we read it from driver
919 if (!Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
920 overrideCameraSettings(mParameters.getFlashMode(),
921 mParameters.getWhiteBalance(), mParameters.getFocusMode());
922 } else {
923 overrideCameraSettings(null, null, null);
924 }
925 }
926
927 private void overrideCameraSettings(final String flashMode,
Dan Aminzade92ae10e2013-08-13 14:44:25 -0700928 final String whiteBalance, final String focusMode) {
Michael Kolbd6954f32013-03-08 20:43:01 -0800929 mUI.overrideSettings(
930 CameraSettings.KEY_FLASH_MODE, flashMode,
931 CameraSettings.KEY_WHITE_BALANCE, whiteBalance,
932 CameraSettings.KEY_FOCUS_MODE, focusMode);
Michael Kolb8872c232013-01-29 10:33:22 -0800933 }
934
935 private void loadCameraPreferences() {
936 CameraSettings settings = new CameraSettings(mActivity, mInitialParams,
937 mCameraId, CameraHolder.instance().getCameraInfo());
938 mPreferenceGroup = settings.getPreferenceGroup(R.xml.camera_preferences);
939 }
940
941 @Override
Michael Kolb8872c232013-01-29 10:33:22 -0800942 public void onOrientationChanged(int orientation) {
943 // We keep the last known orientation. So if the user first orient
944 // the camera then point the camera to floor or sky, we still have
945 // the correct orientation.
946 if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return;
Angus Kongb50b5cb2013-08-09 14:55:20 -0700947 mOrientation = CameraUtil.roundOrientation(orientation, mOrientation);
Michael Kolb8872c232013-01-29 10:33:22 -0800948
949 // Show the toast after getting the first orientation changed.
950 if (mHandler.hasMessages(SHOW_TAP_TO_FOCUS_TOAST)) {
951 mHandler.removeMessages(SHOW_TAP_TO_FOCUS_TOAST);
952 showTapToFocusToast();
953 }
954 }
955
956 @Override
957 public void onStop() {
958 if (mMediaProviderClient != null) {
959 mMediaProviderClient.release();
960 mMediaProviderClient = null;
961 }
962 }
963
Michael Kolbd6954f32013-03-08 20:43:01 -0800964 @Override
965 public void onCaptureCancelled() {
966 mActivity.setResultEx(Activity.RESULT_CANCELED, new Intent());
967 mActivity.finish();
Michael Kolb8872c232013-01-29 10:33:22 -0800968 }
969
Michael Kolbd6954f32013-03-08 20:43:01 -0800970 @Override
971 public void onCaptureRetake() {
Michael Kolb8872c232013-01-29 10:33:22 -0800972 if (mPaused)
973 return;
Michael Kolbd6954f32013-03-08 20:43:01 -0800974 mUI.hidePostCaptureAlert();
Michael Kolb8872c232013-01-29 10:33:22 -0800975 setupPreview();
976 }
977
Michael Kolbd6954f32013-03-08 20:43:01 -0800978 @Override
979 public void onCaptureDone() {
Michael Kolb8872c232013-01-29 10:33:22 -0800980 if (mPaused) {
981 return;
982 }
983
984 byte[] data = mJpegImageData;
985
986 if (mCropValue == null) {
987 // First handle the no crop case -- just return the value. If the
988 // caller specifies a "save uri" then write the data to its
989 // stream. Otherwise, pass back a scaled down version of the bitmap
990 // directly in the extras.
991 if (mSaveUri != null) {
992 OutputStream outputStream = null;
993 try {
994 outputStream = mContentResolver.openOutputStream(mSaveUri);
995 outputStream.write(data);
996 outputStream.close();
997
998 mActivity.setResultEx(Activity.RESULT_OK);
999 mActivity.finish();
1000 } catch (IOException ex) {
1001 // ignore exception
1002 } finally {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001003 CameraUtil.closeSilently(outputStream);
Michael Kolb8872c232013-01-29 10:33:22 -08001004 }
1005 } else {
Angus Kong0d00a892013-03-26 11:40:40 -07001006 ExifInterface exif = Exif.getExif(data);
1007 int orientation = Exif.getOrientation(exif);
Angus Kongb50b5cb2013-08-09 14:55:20 -07001008 Bitmap bitmap = CameraUtil.makeBitmap(data, 50 * 1024);
1009 bitmap = CameraUtil.rotate(bitmap, orientation);
Michael Kolb8872c232013-01-29 10:33:22 -08001010 mActivity.setResultEx(Activity.RESULT_OK,
1011 new Intent("inline-data").putExtra("data", bitmap));
1012 mActivity.finish();
1013 }
1014 } else {
1015 // Save the image to a temp file and invoke the cropper
1016 Uri tempUri = null;
1017 FileOutputStream tempStream = null;
1018 try {
1019 File path = mActivity.getFileStreamPath(sTempCropFilename);
1020 path.delete();
1021 tempStream = mActivity.openFileOutput(sTempCropFilename, 0);
1022 tempStream.write(data);
1023 tempStream.close();
1024 tempUri = Uri.fromFile(path);
1025 } catch (FileNotFoundException ex) {
1026 mActivity.setResultEx(Activity.RESULT_CANCELED);
1027 mActivity.finish();
1028 return;
1029 } catch (IOException ex) {
1030 mActivity.setResultEx(Activity.RESULT_CANCELED);
1031 mActivity.finish();
1032 return;
1033 } finally {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001034 CameraUtil.closeSilently(tempStream);
Michael Kolb8872c232013-01-29 10:33:22 -08001035 }
1036
1037 Bundle newExtras = new Bundle();
1038 if (mCropValue.equals("circle")) {
1039 newExtras.putString("circleCrop", "true");
1040 }
1041 if (mSaveUri != null) {
1042 newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
1043 } else {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001044 newExtras.putBoolean(CameraUtil.KEY_RETURN_DATA, true);
Michael Kolb8872c232013-01-29 10:33:22 -08001045 }
1046 if (mActivity.isSecureCamera()) {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001047 newExtras.putBoolean(CameraUtil.KEY_SHOW_WHEN_LOCKED, true);
Michael Kolb8872c232013-01-29 10:33:22 -08001048 }
1049
Sascha Haeberling37f36112013-08-06 14:31:52 -07001050 // TODO: Share this constant.
Sascha Haeberling8e963a52013-08-06 11:43:02 -07001051 final String CROP_ACTION = "com.android.camera.action.CROP";
1052 Intent cropIntent = new Intent(CROP_ACTION);
Michael Kolb8872c232013-01-29 10:33:22 -08001053
1054 cropIntent.setData(tempUri);
1055 cropIntent.putExtras(newExtras);
1056
1057 mActivity.startActivityForResult(cropIntent, REQUEST_CROP);
1058 }
1059 }
1060
Michael Kolb8872c232013-01-29 10:33:22 -08001061 @Override
1062 public void onShutterButtonFocus(boolean pressed) {
Michael Kolbd6954f32013-03-08 20:43:01 -08001063 if (mPaused || mUI.collapseCameraControls()
Michael Kolb8872c232013-01-29 10:33:22 -08001064 || (mCameraState == SNAPSHOT_IN_PROGRESS)
1065 || (mCameraState == PREVIEW_STOPPED)) return;
1066
1067 // Do not do focus if there is not enough storage.
1068 if (pressed && !canTakePicture()) return;
1069
1070 if (pressed) {
Michael Kolb8872c232013-01-29 10:33:22 -08001071 mFocusManager.onShutterDown();
1072 } else {
Doris Liuda50e052013-02-07 14:36:32 -08001073 // for countdown mode, we need to postpone the shutter release
1074 // i.e. lock the focus during countdown.
Michael Kolbd6954f32013-03-08 20:43:01 -08001075 if (!mUI.isCountingDown()) {
Doris Liuda50e052013-02-07 14:36:32 -08001076 mFocusManager.onShutterUp();
1077 }
Michael Kolb8872c232013-01-29 10:33:22 -08001078 }
1079 }
1080
1081 @Override
1082 public void onShutterButtonClick() {
Michael Kolbd6954f32013-03-08 20:43:01 -08001083 if (mPaused || mUI.collapseCameraControls()
Michael Kolb8872c232013-01-29 10:33:22 -08001084 || (mCameraState == SWITCHING_CAMERA)
1085 || (mCameraState == PREVIEW_STOPPED)) return;
1086
1087 // Do not take the picture if there is not enough storage.
Angus Kong2dcc0a92013-09-25 13:00:08 -07001088 if (mActivity.getStorageSpaceBytes() <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
Michael Kolb8872c232013-01-29 10:33:22 -08001089 Log.i(TAG, "Not enough space or storage not ready. remaining="
Angus Kong2dcc0a92013-09-25 13:00:08 -07001090 + mActivity.getStorageSpaceBytes());
Michael Kolb8872c232013-01-29 10:33:22 -08001091 return;
1092 }
1093 Log.v(TAG, "onShutterButtonClick: mCameraState=" + mCameraState);
1094
Angus Kongb50b5cb2013-08-09 14:55:20 -07001095 if (mSceneMode == CameraUtil.SCENE_MODE_HDR) {
Doris Liu6432cd62013-06-13 17:20:31 -07001096 mUI.hideSwitcher();
1097 mUI.setSwipingEnabled(false);
Doris Liu9cdfe002013-04-16 09:50:56 -07001098 }
Michael Kolb8872c232013-01-29 10:33:22 -08001099 // If the user wants to do a snapshot while the previous one is still
1100 // in progress, remember the fact and do it after we finish the previous
1101 // one and re-start the preview. Snapshot in progress also includes the
1102 // state that autofocus is focusing and a picture will be taken when
1103 // focus callback arrives.
1104 if ((mFocusManager.isFocusingSnapOnFinish() || mCameraState == SNAPSHOT_IN_PROGRESS)
1105 && !mIsImageCaptureIntent) {
1106 mSnapshotOnIdle = true;
1107 return;
1108 }
1109
1110 String timer = mPreferences.getString(
1111 CameraSettings.KEY_TIMER,
1112 mActivity.getString(R.string.pref_camera_timer_default));
1113 boolean playSound = mPreferences.getString(CameraSettings.KEY_TIMER_SOUND_EFFECTS,
1114 mActivity.getString(R.string.pref_camera_timer_sound_default))
1115 .equals(mActivity.getString(R.string.setting_on_value));
1116
1117 int seconds = Integer.parseInt(timer);
1118 // When shutter button is pressed, check whether the previous countdown is
1119 // finished. If not, cancel the previous countdown and start a new one.
Michael Kolbd6954f32013-03-08 20:43:01 -08001120 if (mUI.isCountingDown()) {
1121 mUI.cancelCountDown();
1122 }
1123 if (seconds > 0) {
1124 mUI.startCountDown(seconds, playSound);
Michael Kolb8872c232013-01-29 10:33:22 -08001125 } else {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001126 mSnapshotOnIdle = false;
1127 mFocusManager.doSnap();
Michael Kolb8872c232013-01-29 10:33:22 -08001128 }
1129 }
1130
1131 @Override
1132 public void installIntentFilter() {
Sascha Haeberling4de78802013-10-06 18:07:07 -07001133 // Do nothing.
Michael Kolb8872c232013-01-29 10:33:22 -08001134 }
1135
1136 @Override
1137 public boolean updateStorageHintOnResume() {
1138 return mFirstTimeInitialized;
1139 }
1140
1141 @Override
Michael Kolb8872c232013-01-29 10:33:22 -08001142 public void onResumeBeforeSuper() {
1143 mPaused = false;
1144 }
1145
Angus Kong4f795b82013-09-16 14:25:35 -07001146 /**
1147 * Opens the camera device.
1148 *
1149 * @return Whether the camera was opened successfully.
1150 */
1151 private boolean prepareCamera() {
1152 // We need to check whether the activity is paused before long
1153 // operations to ensure that onPause() can be done ASAP.
1154 mCameraDevice = CameraUtil.openCamera(
1155 mActivity, mCameraId, mHandler,
1156 mActivity.getCameraOpenErrorCallback());
1157 if (mCameraDevice == null) {
1158 Log.e(TAG, "Failed to open camera:" + mCameraId);
1159 return false;
Angus Kongdcccc512013-08-08 17:06:03 -07001160 }
Angus Kong4f795b82013-09-16 14:25:35 -07001161 mParameters = mCameraDevice.getParameters();
1162
1163 initializeCapabilities();
1164 if (mFocusManager == null) initializeFocusManager();
1165 setCameraParameters(UPDATE_PARAM_ALL);
1166 mHandler.sendEmptyMessage(CAMERA_OPEN_DONE);
1167 mCameraPreviewParamsReady = true;
1168 startPreview();
1169 mOnResumeTime = SystemClock.uptimeMillis();
1170 checkDisplayRotation();
1171 return true;
Angus Kongdcccc512013-08-08 17:06:03 -07001172 }
1173
1174
Michael Kolb8872c232013-01-29 10:33:22 -08001175 @Override
1176 public void onResumeAfterSuper() {
1177 if (mOpenCameraFail || mCameraDisabled) return;
1178
1179 mJpegPictureCallbackTime = 0;
1180 mZoomValue = 0;
Angus Kongdcccc512013-08-08 17:06:03 -07001181 resetExposureCompensation();
Angus Kong4f795b82013-09-16 14:25:35 -07001182 if (!prepareCamera()) {
1183 // Camera failure.
1184 return;
1185 }
Michael Kolb8872c232013-01-29 10:33:22 -08001186
1187 // If first time initialization is not finished, put it in the
1188 // message queue.
1189 if (!mFirstTimeInitialized) {
1190 mHandler.sendEmptyMessage(FIRST_TIME_INIT);
1191 } else {
1192 initializeSecondTime();
1193 }
1194 keepScreenOnAwhile();
1195
Bobby Georgescu0a7dd572013-03-12 22:45:17 -07001196 UsageStatistics.onContentViewChanged(
1197 UsageStatistics.COMPONENT_CAMERA, "PhotoModule");
Angus Kong0d00a892013-03-26 11:40:40 -07001198
1199 Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1200 if (gsensor != null) {
1201 mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL);
1202 }
1203
1204 Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1205 if (msensor != null) {
1206 mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_NORMAL);
1207 }
Michael Kolb8872c232013-01-29 10:33:22 -08001208 }
1209
Michael Kolb8872c232013-01-29 10:33:22 -08001210 @Override
1211 public void onPauseBeforeSuper() {
1212 mPaused = true;
Angus Kong0d00a892013-03-26 11:40:40 -07001213 Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1214 if (gsensor != null) {
1215 mSensorManager.unregisterListener(this, gsensor);
1216 }
1217
1218 Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1219 if (msensor != null) {
1220 mSensorManager.unregisterListener(this, msensor);
1221 }
Michael Kolb8872c232013-01-29 10:33:22 -08001222 }
1223
1224 @Override
1225 public void onPauseAfterSuper() {
Michael Kolb8872c232013-01-29 10:33:22 -08001226 // When camera is started from secure lock screen for the first time
1227 // after screen on, the activity gets onCreate->onResume->onPause->onResume.
1228 // To reduce the latency, keep the camera for a short time so it does
1229 // not need to be opened again.
1230 if (mCameraDevice != null && mActivity.isSecureCamera()
Angus Kong6a8e8a12013-07-19 14:55:07 -07001231 && CameraActivity.isFirstStartAfterScreenOn()) {
1232 CameraActivity.resetFirstStartAfterScreenOn();
Michael Kolb8872c232013-01-29 10:33:22 -08001233 CameraHolder.instance().keep(KEEP_CAMERA_TIMEOUT);
1234 }
1235 // Reset the focus first. Camera CTS does not guarantee that
1236 // cancelAutoFocus is allowed after preview stops.
1237 if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
1238 mCameraDevice.cancelAutoFocus();
1239 }
1240 stopPreview();
Michael Kolb8872c232013-01-29 10:33:22 -08001241
Angus Kongce5480e2013-01-29 17:43:48 -08001242 mNamedImages = null;
Michael Kolb8872c232013-01-29 10:33:22 -08001243
1244 if (mLocationManager != null) mLocationManager.recordLocation(false);
1245
1246 // If we are in an image capture intent and has taken
1247 // a picture, we just clear it in onPause.
1248 mJpegImageData = null;
1249
Angus Kongdcccc512013-08-08 17:06:03 -07001250 // Remove the messages and runnables in the queue.
1251 mHandler.removeCallbacksAndMessages(null);
Michael Kolb8872c232013-01-29 10:33:22 -08001252
Michael Kolbd6954f32013-03-08 20:43:01 -08001253 closeCamera();
1254
1255 resetScreenOn();
1256 mUI.onPause();
1257
Michael Kolb8872c232013-01-29 10:33:22 -08001258 mPendingSwitchCameraId = -1;
1259 if (mFocusManager != null) mFocusManager.removeMessages();
Angus Kong86d36312013-01-31 18:22:44 -08001260 MediaSaveService s = mActivity.getMediaSaveService();
1261 if (s != null) {
1262 s.setListener(null);
1263 }
Michael Kolb8872c232013-01-29 10:33:22 -08001264 }
1265
Michael Kolb8872c232013-01-29 10:33:22 -08001266 /**
1267 * The focus manager is the first UI related element to get initialized,
1268 * and it requires the RenderOverlay, so initialize it here
1269 */
1270 private void initializeFocusManager() {
1271 // Create FocusManager object. startPreview needs it.
Michael Kolb8872c232013-01-29 10:33:22 -08001272 // if mFocusManager not null, reuse it
1273 // otherwise create a new instance
1274 if (mFocusManager != null) {
1275 mFocusManager.removeMessages();
1276 } else {
1277 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
Doris Liu29da2db2013-08-28 14:28:45 -07001278 mMirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
Michael Kolb8872c232013-01-29 10:33:22 -08001279 String[] defaultFocusModes = mActivity.getResources().getStringArray(
1280 R.array.pref_camera_focusmode_default_array);
1281 mFocusManager = new FocusOverlayManager(mPreferences, defaultFocusModes,
Doris Liu29da2db2013-08-28 14:28:45 -07001282 mInitialParams, this, mMirror,
Michael Kolbd6954f32013-03-08 20:43:01 -08001283 mActivity.getMainLooper(), mUI);
Michael Kolb8872c232013-01-29 10:33:22 -08001284 }
1285 }
1286
Michael Kolb8872c232013-01-29 10:33:22 -08001287 @Override
1288 public void onConfigurationChanged(Configuration newConfig) {
1289 Log.v(TAG, "onConfigurationChanged");
1290 setDisplayOrientation();
Michael Kolb8872c232013-01-29 10:33:22 -08001291 }
1292
1293 @Override
Doris Liu6432cd62013-06-13 17:20:31 -07001294 public void updateCameraOrientation() {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001295 if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) {
Doris Liu6432cd62013-06-13 17:20:31 -07001296 setDisplayOrientation();
1297 }
1298 }
1299
1300 @Override
Michael Kolb8872c232013-01-29 10:33:22 -08001301 public void onActivityResult(
1302 int requestCode, int resultCode, Intent data) {
1303 switch (requestCode) {
1304 case REQUEST_CROP: {
1305 Intent intent = new Intent();
1306 if (data != null) {
1307 Bundle extras = data.getExtras();
1308 if (extras != null) {
1309 intent.putExtras(extras);
1310 }
1311 }
1312 mActivity.setResultEx(resultCode, intent);
1313 mActivity.finish();
1314
1315 File path = mActivity.getFileStreamPath(sTempCropFilename);
1316 path.delete();
1317
1318 break;
1319 }
1320 }
1321 }
1322
1323 private boolean canTakePicture() {
Angus Kong2dcc0a92013-09-25 13:00:08 -07001324 return isCameraIdle() && (mActivity.getStorageSpaceBytes() > Storage.LOW_STORAGE_THRESHOLD_BYTES);
Michael Kolb8872c232013-01-29 10:33:22 -08001325 }
1326
1327 @Override
1328 public void autoFocus() {
1329 mFocusStartTime = System.currentTimeMillis();
Angus Kong9ef99252013-07-18 18:04:19 -07001330 mCameraDevice.autoFocus(mHandler, mAutoFocusCallback);
Michael Kolb8872c232013-01-29 10:33:22 -08001331 setCameraState(FOCUSING);
1332 }
1333
1334 @Override
1335 public void cancelAutoFocus() {
1336 mCameraDevice.cancelAutoFocus();
1337 setCameraState(IDLE);
1338 setCameraParameters(UPDATE_PARAM_PREFERENCE);
1339 }
1340
1341 // Preview area is touched. Handle touch focus.
1342 @Override
1343 public void onSingleTapUp(View view, int x, int y) {
1344 if (mPaused || mCameraDevice == null || !mFirstTimeInitialized
1345 || mCameraState == SNAPSHOT_IN_PROGRESS
1346 || mCameraState == SWITCHING_CAMERA
1347 || mCameraState == PREVIEW_STOPPED) {
1348 return;
1349 }
1350
Michael Kolb8872c232013-01-29 10:33:22 -08001351 // Check if metering area or focus area is supported.
1352 if (!mFocusAreaSupported && !mMeteringAreaSupported) return;
1353 mFocusManager.onSingleTapUp(x, y);
1354 }
1355
1356 @Override
1357 public boolean onBackPressed() {
Michael Kolbd6954f32013-03-08 20:43:01 -08001358 return mUI.onBackPressed();
Michael Kolb8872c232013-01-29 10:33:22 -08001359 }
1360
1361 @Override
1362 public boolean onKeyDown(int keyCode, KeyEvent event) {
1363 switch (keyCode) {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001364 case KeyEvent.KEYCODE_VOLUME_UP:
1365 case KeyEvent.KEYCODE_VOLUME_DOWN:
1366 case KeyEvent.KEYCODE_FOCUS:
1367 if (/*TODO: mActivity.isInCameraApp() &&*/ mFirstTimeInitialized) {
1368 if (event.getRepeatCount() == 0) {
1369 onShutterButtonFocus(true);
1370 }
1371 return true;
1372 }
1373 return false;
1374 case KeyEvent.KEYCODE_CAMERA:
1375 if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1376 onShutterButtonClick();
Michael Kolb8872c232013-01-29 10:33:22 -08001377 }
1378 return true;
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001379 case KeyEvent.KEYCODE_DPAD_CENTER:
1380 // If we get a dpad center event without any focused view, move
1381 // the focus to the shutter button and press it.
1382 if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1383 // Start auto-focus immediately to reduce shutter lag. After
1384 // the shutter button gets the focus, onShutterButtonFocus()
1385 // will be called again but it is fine.
1386 onShutterButtonFocus(true);
1387 mUI.pressShutterButton();
1388 }
1389 return true;
Michael Kolb8872c232013-01-29 10:33:22 -08001390 }
1391 return false;
1392 }
1393
1394 @Override
1395 public boolean onKeyUp(int keyCode, KeyEvent event) {
1396 switch (keyCode) {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001397 case KeyEvent.KEYCODE_VOLUME_UP:
1398 case KeyEvent.KEYCODE_VOLUME_DOWN:
1399 if (/*mActivity.isInCameraApp() && */ mFirstTimeInitialized) {
1400 onShutterButtonClick();
1401 return true;
1402 }
1403 return false;
1404 case KeyEvent.KEYCODE_FOCUS:
1405 if (mFirstTimeInitialized) {
1406 onShutterButtonFocus(false);
1407 }
Michael Kolb8872c232013-01-29 10:33:22 -08001408 return true;
Michael Kolb8872c232013-01-29 10:33:22 -08001409 }
1410 return false;
1411 }
1412
Michael Kolb8872c232013-01-29 10:33:22 -08001413 private void closeCamera() {
1414 if (mCameraDevice != null) {
1415 mCameraDevice.setZoomChangeListener(null);
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001416 mCameraDevice.setFaceDetectionCallback(null, null);
Michael Kolb8872c232013-01-29 10:33:22 -08001417 mCameraDevice.setErrorCallback(null);
1418 CameraHolder.instance().release();
1419 mFaceDetectionStarted = false;
1420 mCameraDevice = null;
1421 setCameraState(PREVIEW_STOPPED);
1422 mFocusManager.onCameraReleased();
1423 }
1424 }
1425
1426 private void setDisplayOrientation() {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001427 mDisplayRotation = CameraUtil.getDisplayRotation(mActivity);
1428 mDisplayOrientation = CameraUtil.getDisplayOrientation(mDisplayRotation, mCameraId);
Doris Liu6432cd62013-06-13 17:20:31 -07001429 mCameraDisplayOrientation = mDisplayOrientation;
Michael Kolbd6954f32013-03-08 20:43:01 -08001430 mUI.setDisplayOrientation(mDisplayOrientation);
Michael Kolb8872c232013-01-29 10:33:22 -08001431 if (mFocusManager != null) {
1432 mFocusManager.setDisplayOrientation(mDisplayOrientation);
1433 }
Doris Liu6432cd62013-06-13 17:20:31 -07001434 // Change the camera display orientation
1435 if (mCameraDevice != null) {
1436 mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation);
1437 }
Michael Kolb8872c232013-01-29 10:33:22 -08001438 }
1439
Sascha Haeberlingddef7792013-08-13 14:41:10 -07001440 /** Only called by UI thread. */
Michael Kolb8872c232013-01-29 10:33:22 -08001441 private void setupPreview() {
1442 mFocusManager.resetTouchFocus();
1443 startPreview();
Michael Kolb8872c232013-01-29 10:33:22 -08001444 }
1445
Angus Kongdcccc512013-08-08 17:06:03 -07001446 // This can only be called by UI Thread.
Michael Kolb8872c232013-01-29 10:33:22 -08001447 private void startPreview() {
Angus Kongdcccc512013-08-08 17:06:03 -07001448 if (mPaused) {
1449 return;
1450 }
1451 SurfaceTexture st = mUI.getSurfaceTexture();
1452 if (st == null) {
1453 Log.w(TAG, "startPreview: surfaceTexture is not ready.");
1454 return;
1455 }
1456 if (!mCameraPreviewParamsReady) {
1457 Log.w(TAG, "startPreview: parameters for preview is not ready.");
1458 return;
1459 }
Michael Kolb8872c232013-01-29 10:33:22 -08001460 mCameraDevice.setErrorCallback(mErrorCallback);
1461
1462 // ICS camera frameworks has a bug. Face detection state is not cleared
1463 // after taking a picture. Stop the preview to work around it. The bug
1464 // was fixed in JB.
1465 if (mCameraState != PREVIEW_STOPPED) stopPreview();
1466
1467 setDisplayOrientation();
1468
1469 if (!mSnapshotOnIdle) {
1470 // If the focus mode is continuous autofocus, call cancelAutoFocus to
1471 // resume it because it may have been paused by autoFocus call.
Angus Kongb50b5cb2013-08-09 14:55:20 -07001472 if (CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE.equals(mFocusManager.getFocusMode())) {
Michael Kolb8872c232013-01-29 10:33:22 -08001473 mCameraDevice.cancelAutoFocus();
1474 }
1475 mFocusManager.setAeAwbLock(false); // Unlock AE and AWB.
1476 }
1477 setCameraParameters(UPDATE_PARAM_ALL);
Doris Liu6432cd62013-06-13 17:20:31 -07001478 // Let UI set its expected aspect ratio
Angus Kongdcccc512013-08-08 17:06:03 -07001479 mCameraDevice.setPreviewTexture(st);
Michael Kolb8872c232013-01-29 10:33:22 -08001480
1481 Log.v(TAG, "startPreview");
Angus Kong9ef99252013-07-18 18:04:19 -07001482 mCameraDevice.startPreview();
Michael Kolb8872c232013-01-29 10:33:22 -08001483 mFocusManager.onPreviewStarted();
Angus Kongdcccc512013-08-08 17:06:03 -07001484 onPreviewStarted();
Michael Kolb8872c232013-01-29 10:33:22 -08001485
1486 if (mSnapshotOnIdle) {
1487 mHandler.post(mDoSnapRunnable);
1488 }
1489 }
1490
Michael Kolbd6954f32013-03-08 20:43:01 -08001491 @Override
1492 public void stopPreview() {
Michael Kolb8872c232013-01-29 10:33:22 -08001493 if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
1494 Log.v(TAG, "stopPreview");
1495 mCameraDevice.stopPreview();
1496 mFaceDetectionStarted = false;
1497 }
1498 setCameraState(PREVIEW_STOPPED);
1499 if (mFocusManager != null) mFocusManager.onPreviewStopped();
1500 }
1501
1502 @SuppressWarnings("deprecation")
1503 private void updateCameraParametersInitialize() {
1504 // Reset preview frame rate to the maximum because it may be lowered by
1505 // video camera application.
ztenghui16a35202013-09-23 11:35:36 -07001506 int[] fpsRange = CameraUtil.getPhotoPreviewFpsRange(mParameters);
1507 if (fpsRange != null && fpsRange.length > 0) {
Doris Liu6432cd62013-06-13 17:20:31 -07001508 mParameters.setPreviewFpsRange(
1509 fpsRange[Parameters.PREVIEW_FPS_MIN_INDEX],
1510 fpsRange[Parameters.PREVIEW_FPS_MAX_INDEX]);
Michael Kolb8872c232013-01-29 10:33:22 -08001511 }
1512
Angus Kongb50b5cb2013-08-09 14:55:20 -07001513 mParameters.set(CameraUtil.RECORDING_HINT, CameraUtil.FALSE);
Michael Kolb8872c232013-01-29 10:33:22 -08001514
1515 // Disable video stabilization. Convenience methods not available in API
1516 // level <= 14
1517 String vstabSupported = mParameters.get("video-stabilization-supported");
1518 if ("true".equals(vstabSupported)) {
1519 mParameters.set("video-stabilization", "false");
1520 }
1521 }
1522
1523 private void updateCameraParametersZoom() {
1524 // Set zoom.
1525 if (mParameters.isZoomSupported()) {
1526 mParameters.setZoom(mZoomValue);
1527 }
1528 }
1529
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001530 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08001531 private void setAutoExposureLockIfSupported() {
1532 if (mAeLockSupported) {
1533 mParameters.setAutoExposureLock(mFocusManager.getAeAwbLock());
1534 }
1535 }
1536
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001537 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08001538 private void setAutoWhiteBalanceLockIfSupported() {
1539 if (mAwbLockSupported) {
1540 mParameters.setAutoWhiteBalanceLock(mFocusManager.getAeAwbLock());
1541 }
1542 }
1543
Michael Kolb8872c232013-01-29 10:33:22 -08001544 private void setFocusAreasIfSupported() {
1545 if (mFocusAreaSupported) {
1546 mParameters.setFocusAreas(mFocusManager.getFocusAreas());
1547 }
1548 }
1549
Michael Kolb8872c232013-01-29 10:33:22 -08001550 private void setMeteringAreasIfSupported() {
1551 if (mMeteringAreaSupported) {
Michael Kolb8872c232013-01-29 10:33:22 -08001552 mParameters.setMeteringAreas(mFocusManager.getMeteringAreas());
1553 }
1554 }
1555
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001556 private boolean updateCameraParametersPreference() {
Michael Kolb8872c232013-01-29 10:33:22 -08001557 setAutoExposureLockIfSupported();
1558 setAutoWhiteBalanceLockIfSupported();
1559 setFocusAreasIfSupported();
1560 setMeteringAreasIfSupported();
1561
1562 // Set picture size.
1563 String pictureSize = mPreferences.getString(
1564 CameraSettings.KEY_PICTURE_SIZE, null);
1565 if (pictureSize == null) {
1566 CameraSettings.initialCameraPictureSize(mActivity, mParameters);
1567 } else {
1568 List<Size> supported = mParameters.getSupportedPictureSizes();
1569 CameraSettings.setCameraPictureSize(
1570 pictureSize, supported, mParameters);
1571 }
1572 Size size = mParameters.getPictureSize();
1573
1574 // Set a preview size that is closest to the viewfinder height and has
1575 // the right aspect ratio.
1576 List<Size> sizes = mParameters.getSupportedPreviewSizes();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001577 Size optimalSize = CameraUtil.getOptimalPreviewSize(mActivity, sizes,
Michael Kolb8872c232013-01-29 10:33:22 -08001578 (double) size.width / size.height);
1579 Size original = mParameters.getPreviewSize();
1580 if (!original.equals(optimalSize)) {
1581 mParameters.setPreviewSize(optimalSize.width, optimalSize.height);
Doris Liu6432cd62013-06-13 17:20:31 -07001582
Michael Kolb8872c232013-01-29 10:33:22 -08001583 // Zoom related settings will be changed for different preview
1584 // sizes, so set and read the parameters to get latest values
Michael Kolb4a4d4ef2013-05-30 08:35:26 -07001585 if (mHandler.getLooper() == Looper.myLooper()) {
1586 // On UI thread only, not when camera starts up
1587 setupPreview();
1588 } else {
1589 mCameraDevice.setParameters(mParameters);
1590 }
Michael Kolb8872c232013-01-29 10:33:22 -08001591 mParameters = mCameraDevice.getParameters();
1592 }
1593 Log.v(TAG, "Preview size is " + optimalSize.width + "x" + optimalSize.height);
1594
1595 // Since changing scene mode may change supported values, set scene mode
1596 // first. HDR is a scene mode. To promote it in UI, it is stored in a
1597 // separate preference.
Sascha Haeberling98f38bb2013-09-24 18:58:34 -07001598 String onValue = mActivity.getString(R.string.setting_on_value);
Michael Kolb8872c232013-01-29 10:33:22 -08001599 String hdr = mPreferences.getString(CameraSettings.KEY_CAMERA_HDR,
1600 mActivity.getString(R.string.pref_camera_hdr_default));
Sascha Haeberling9bf0fd62013-10-03 12:10:48 -07001601 String hdrPlus = mPreferences.getString(CameraSettings.KEY_CAMERA_HDR_PLUS,
1602 mActivity.getString(R.string.pref_camera_hdr_plus_default));
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001603 boolean hdrOn = onValue.equals(hdr);
Sascha Haeberling9bf0fd62013-10-03 12:10:48 -07001604 boolean hdrPlusOn = onValue.equals(hdrPlus);
Sascha Haeberling98f38bb2013-09-24 18:58:34 -07001605
1606 boolean doGcamModeSwitch = false;
Sascha Haeberling9bf0fd62013-10-03 12:10:48 -07001607 if (hdrPlusOn && GcamHelper.hasGcamCapture()) {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001608 // Kick off mode switch to gcam.
1609 doGcamModeSwitch = true;
Michael Kolb8872c232013-01-29 10:33:22 -08001610 } else {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001611 if (hdrOn) {
1612 mSceneMode = CameraUtil.SCENE_MODE_HDR;
1613 } else {
1614 mSceneMode = mPreferences.getString(
1615 CameraSettings.KEY_SCENE_MODE,
1616 mActivity.getString(R.string.pref_camera_scenemode_default));
1617 }
Michael Kolb8872c232013-01-29 10:33:22 -08001618 }
Angus Kongb50b5cb2013-08-09 14:55:20 -07001619 if (CameraUtil.isSupported(mSceneMode, mParameters.getSupportedSceneModes())) {
Michael Kolb8872c232013-01-29 10:33:22 -08001620 if (!mParameters.getSceneMode().equals(mSceneMode)) {
1621 mParameters.setSceneMode(mSceneMode);
1622
1623 // Setting scene mode will change the settings of flash mode,
1624 // white balance, and focus mode. Here we read back the
1625 // parameters, so we can know those settings.
1626 mCameraDevice.setParameters(mParameters);
1627 mParameters = mCameraDevice.getParameters();
1628 }
1629 } else {
1630 mSceneMode = mParameters.getSceneMode();
1631 if (mSceneMode == null) {
1632 mSceneMode = Parameters.SCENE_MODE_AUTO;
1633 }
1634 }
1635
1636 // Set JPEG quality.
1637 int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId,
1638 CameraProfile.QUALITY_HIGH);
1639 mParameters.setJpegQuality(jpegQuality);
1640
1641 // For the following settings, we need to check if the settings are
1642 // still supported by latest driver, if not, ignore the settings.
1643
1644 // Set exposure compensation
1645 int value = CameraSettings.readExposure(mPreferences);
1646 int max = mParameters.getMaxExposureCompensation();
1647 int min = mParameters.getMinExposureCompensation();
1648 if (value >= min && value <= max) {
1649 mParameters.setExposureCompensation(value);
1650 } else {
1651 Log.w(TAG, "invalid exposure range: " + value);
1652 }
1653
1654 if (Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
1655 // Set flash mode.
1656 String flashMode = mPreferences.getString(
1657 CameraSettings.KEY_FLASH_MODE,
1658 mActivity.getString(R.string.pref_camera_flashmode_default));
1659 List<String> supportedFlash = mParameters.getSupportedFlashModes();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001660 if (CameraUtil.isSupported(flashMode, supportedFlash)) {
Michael Kolb8872c232013-01-29 10:33:22 -08001661 mParameters.setFlashMode(flashMode);
1662 } else {
1663 flashMode = mParameters.getFlashMode();
1664 if (flashMode == null) {
1665 flashMode = mActivity.getString(
1666 R.string.pref_camera_flashmode_no_flash);
1667 }
1668 }
1669
1670 // Set white balance parameter.
1671 String whiteBalance = mPreferences.getString(
1672 CameraSettings.KEY_WHITE_BALANCE,
1673 mActivity.getString(R.string.pref_camera_whitebalance_default));
Angus Kongb50b5cb2013-08-09 14:55:20 -07001674 if (CameraUtil.isSupported(whiteBalance,
Michael Kolb8872c232013-01-29 10:33:22 -08001675 mParameters.getSupportedWhiteBalance())) {
1676 mParameters.setWhiteBalance(whiteBalance);
1677 } else {
1678 whiteBalance = mParameters.getWhiteBalance();
1679 if (whiteBalance == null) {
1680 whiteBalance = Parameters.WHITE_BALANCE_AUTO;
1681 }
1682 }
1683
1684 // Set focus mode.
1685 mFocusManager.overrideFocusMode(null);
1686 mParameters.setFocusMode(mFocusManager.getFocusMode());
1687 } else {
1688 mFocusManager.overrideFocusMode(mParameters.getFocusMode());
1689 }
1690
Angus Kongdcccc512013-08-08 17:06:03 -07001691 if (mContinuousFocusSupported && ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK) {
Michael Kolb8872c232013-01-29 10:33:22 -08001692 updateAutoFocusMoveCallback();
1693 }
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001694
1695 return doGcamModeSwitch;
Michael Kolb8872c232013-01-29 10:33:22 -08001696 }
1697
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001698 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Michael Kolb8872c232013-01-29 10:33:22 -08001699 private void updateAutoFocusMoveCallback() {
Angus Kongb50b5cb2013-08-09 14:55:20 -07001700 if (mParameters.getFocusMode().equals(CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE)) {
Angus Kong9ef99252013-07-18 18:04:19 -07001701 mCameraDevice.setAutoFocusMoveCallback(mHandler,
Angus Kong4f795b82013-09-16 14:25:35 -07001702 (CameraAFMoveCallback) mAutoFocusMoveCallback);
Michael Kolb8872c232013-01-29 10:33:22 -08001703 } else {
Angus Kong9ef99252013-07-18 18:04:19 -07001704 mCameraDevice.setAutoFocusMoveCallback(null, null);
Michael Kolb8872c232013-01-29 10:33:22 -08001705 }
1706 }
1707
1708 // We separate the parameters into several subsets, so we can update only
1709 // the subsets actually need updating. The PREFERENCE set needs extra
1710 // locking because the preference can be changed from GLThread as well.
1711 private void setCameraParameters(int updateSet) {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001712 boolean doModeSwitch = false;
1713
Michael Kolb8872c232013-01-29 10:33:22 -08001714 if ((updateSet & UPDATE_PARAM_INITIALIZE) != 0) {
1715 updateCameraParametersInitialize();
1716 }
1717
1718 if ((updateSet & UPDATE_PARAM_ZOOM) != 0) {
1719 updateCameraParametersZoom();
1720 }
1721
1722 if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001723 doModeSwitch = updateCameraParametersPreference();
Michael Kolb8872c232013-01-29 10:33:22 -08001724 }
1725
1726 mCameraDevice.setParameters(mParameters);
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001727
Sascha Haeberling9bf0fd62013-10-03 12:10:48 -07001728 // Switch to gcam module if HDR+ was selected
Angus Kong0fb819b2013-10-08 13:44:19 -07001729 if (doModeSwitch && !mIsImageCaptureIntent) {
Ruben Brunk4601f5d2013-09-24 18:35:22 -07001730 mActivity.onModuleSelected(ModuleSwitcher.GCAM_MODULE_INDEX);
1731 }
Michael Kolb8872c232013-01-29 10:33:22 -08001732 }
1733
1734 // If the Camera is idle, update the parameters immediately, otherwise
1735 // accumulate them in mUpdateSet and update later.
1736 private void setCameraParametersWhenIdle(int additionalUpdateSet) {
1737 mUpdateSet |= additionalUpdateSet;
1738 if (mCameraDevice == null) {
1739 // We will update all the parameters when we open the device, so
1740 // we don't need to do anything now.
1741 mUpdateSet = 0;
1742 return;
1743 } else if (isCameraIdle()) {
1744 setCameraParameters(mUpdateSet);
Michael Kolbd6954f32013-03-08 20:43:01 -08001745 updateSceneMode();
Michael Kolb8872c232013-01-29 10:33:22 -08001746 mUpdateSet = 0;
1747 } else {
1748 if (!mHandler.hasMessages(SET_CAMERA_PARAMETERS_WHEN_IDLE)) {
1749 mHandler.sendEmptyMessageDelayed(
1750 SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000);
1751 }
1752 }
1753 }
1754
ztenghui7b265a62013-09-09 14:58:44 -07001755 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001756 public boolean isCameraIdle() {
Michael Kolb8872c232013-01-29 10:33:22 -08001757 return (mCameraState == IDLE) ||
1758 (mCameraState == PREVIEW_STOPPED) ||
1759 ((mFocusManager != null) && mFocusManager.isFocusCompleted()
1760 && (mCameraState != SWITCHING_CAMERA));
1761 }
1762
ztenghui7b265a62013-09-09 14:58:44 -07001763 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001764 public boolean isImageCaptureIntent() {
Michael Kolb8872c232013-01-29 10:33:22 -08001765 String action = mActivity.getIntent().getAction();
1766 return (MediaStore.ACTION_IMAGE_CAPTURE.equals(action)
Angus Kong6a8e8a12013-07-19 14:55:07 -07001767 || CameraActivity.ACTION_IMAGE_CAPTURE_SECURE.equals(action));
Michael Kolb8872c232013-01-29 10:33:22 -08001768 }
1769
1770 private void setupCaptureParams() {
1771 Bundle myExtras = mActivity.getIntent().getExtras();
1772 if (myExtras != null) {
1773 mSaveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT);
1774 mCropValue = myExtras.getString("crop");
1775 }
1776 }
1777
Michael Kolb8872c232013-01-29 10:33:22 -08001778 @Override
1779 public void onSharedPreferenceChanged() {
1780 // ignore the events after "onPause()"
1781 if (mPaused) return;
1782
1783 boolean recordLocation = RecordLocationPreference.get(
1784 mPreferences, mContentResolver);
1785 mLocationManager.recordLocation(recordLocation);
1786
1787 setCameraParametersWhenIdle(UPDATE_PARAM_PREFERENCE);
Michael Kolbeb8adc12013-04-26 11:09:29 -07001788 mUI.updateOnScreenIndicators(mParameters, mPreferenceGroup, mPreferences);
Michael Kolb8872c232013-01-29 10:33:22 -08001789 }
1790
1791 @Override
1792 public void onCameraPickerClicked(int cameraId) {
1793 if (mPaused || mPendingSwitchCameraId != -1) return;
1794
1795 mPendingSwitchCameraId = cameraId;
Doris Liu6432cd62013-06-13 17:20:31 -07001796
1797 Log.v(TAG, "Start to switch camera. cameraId=" + cameraId);
1798 // We need to keep a preview frame for the animation before
1799 // releasing the camera. This will trigger onPreviewTextureCopied.
1800 //TODO: Need to animate the camera switch
1801 switchCamera();
Michael Kolb8872c232013-01-29 10:33:22 -08001802 }
1803
Michael Kolb8872c232013-01-29 10:33:22 -08001804 // Preview texture has been copied. Now camera can be released and the
1805 // animation can be started.
1806 @Override
1807 public void onPreviewTextureCopied() {
1808 mHandler.sendEmptyMessage(SWITCH_CAMERA);
1809 }
1810
1811 @Override
1812 public void onCaptureTextureCopied() {
1813 }
1814
1815 @Override
1816 public void onUserInteraction() {
Dan Aminzade92ae10e2013-08-13 14:44:25 -07001817 if (!mActivity.isFinishing()) keepScreenOnAwhile();
Michael Kolb8872c232013-01-29 10:33:22 -08001818 }
1819
1820 private void resetScreenOn() {
1821 mHandler.removeMessages(CLEAR_SCREEN_DELAY);
1822 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1823 }
1824
1825 private void keepScreenOnAwhile() {
1826 mHandler.removeMessages(CLEAR_SCREEN_DELAY);
1827 mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1828 mHandler.sendEmptyMessageDelayed(CLEAR_SCREEN_DELAY, SCREEN_DELAY);
1829 }
1830
Michael Kolb8872c232013-01-29 10:33:22 -08001831 @Override
1832 public void onOverriddenPreferencesClicked() {
1833 if (mPaused) return;
Michael Kolbd6954f32013-03-08 20:43:01 -08001834 mUI.showPreferencesToast();
Michael Kolb8872c232013-01-29 10:33:22 -08001835 }
1836
1837 private void showTapToFocusToast() {
1838 // TODO: Use a toast?
1839 new RotateTextToast(mActivity, R.string.tap_to_focus, 0).show();
1840 // Clear the preference.
1841 Editor editor = mPreferences.edit();
1842 editor.putBoolean(CameraSettings.KEY_CAMERA_FIRST_USE_HINT_SHOWN, false);
1843 editor.apply();
1844 }
1845
1846 private void initializeCapabilities() {
1847 mInitialParams = mCameraDevice.getParameters();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001848 mFocusAreaSupported = CameraUtil.isFocusAreaSupported(mInitialParams);
1849 mMeteringAreaSupported = CameraUtil.isMeteringAreaSupported(mInitialParams);
1850 mAeLockSupported = CameraUtil.isAutoExposureLockSupported(mInitialParams);
1851 mAwbLockSupported = CameraUtil.isAutoWhiteBalanceLockSupported(mInitialParams);
Angus Kongdcccc512013-08-08 17:06:03 -07001852 mContinuousFocusSupported = mInitialParams.getSupportedFocusModes().contains(
Angus Kongb50b5cb2013-08-09 14:55:20 -07001853 CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE);
Michael Kolb8872c232013-01-29 10:33:22 -08001854 }
1855
Michael Kolb8872c232013-01-29 10:33:22 -08001856 @Override
1857 public void onCountDownFinished() {
1858 mSnapshotOnIdle = false;
1859 mFocusManager.doSnap();
Doris Liuda50e052013-02-07 14:36:32 -08001860 mFocusManager.onShutterUp();
Michael Kolb8872c232013-01-29 10:33:22 -08001861 }
1862
Michael Kolb8872c232013-01-29 10:33:22 -08001863 @Override
Michael Kolb8872c232013-01-29 10:33:22 -08001864 public void onShowSwitcherPopup() {
Michael Kolbd6954f32013-03-08 20:43:01 -08001865 mUI.onShowSwitcherPopup();
Michael Kolb8872c232013-01-29 10:33:22 -08001866 }
1867
Angus Kongce5480e2013-01-29 17:43:48 -08001868 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001869 public int onZoomChanged(int index) {
1870 // Not useful to change zoom value when the activity is paused.
1871 if (mPaused) return index;
1872 mZoomValue = index;
1873 if (mParameters == null || mCameraDevice == null) return index;
1874 // Set zoom parameters asynchronously
1875 mParameters.setZoom(mZoomValue);
Angus Kong36ed8672013-04-15 12:11:15 -07001876 mCameraDevice.setParameters(mParameters);
Michael Kolbd6954f32013-03-08 20:43:01 -08001877 Parameters p = mCameraDevice.getParameters();
1878 if (p != null) return p.getZoom();
1879 return index;
Angus Kongce5480e2013-01-29 17:43:48 -08001880 }
1881
1882 @Override
Michael Kolbd6954f32013-03-08 20:43:01 -08001883 public int getCameraState() {
1884 return mCameraState;
1885 }
1886
1887 @Override
1888 public void onQueueStatus(boolean full) {
1889 mUI.enableShutter(!full);
Angus Kongce5480e2013-01-29 17:43:48 -08001890 }
Angus Kong86d36312013-01-31 18:22:44 -08001891
1892 @Override
1893 public void onMediaSaveServiceConnected(MediaSaveService s) {
1894 // We set the listener only when both service and shutterbutton
1895 // are initialized.
Michael Kolbd6954f32013-03-08 20:43:01 -08001896 if (mFirstTimeInitialized) {
1897 s.setListener(this);
1898 }
Angus Kong86d36312013-01-31 18:22:44 -08001899 }
Angus Kong0d00a892013-03-26 11:40:40 -07001900
1901 @Override
1902 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1903 }
1904
1905 @Override
1906 public void onSensorChanged(SensorEvent event) {
1907 int type = event.sensor.getType();
1908 float[] data;
1909 if (type == Sensor.TYPE_ACCELEROMETER) {
1910 data = mGData;
1911 } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
1912 data = mMData;
1913 } else {
1914 // we should not be here.
1915 return;
1916 }
1917 for (int i = 0; i < 3 ; i++) {
1918 data[i] = event.values[i];
1919 }
1920 float[] orientation = new float[3];
1921 SensorManager.getRotationMatrix(mR, null, mGData, mMData);
1922 SensorManager.getOrientation(mR, orientation);
1923 mHeading = (int) (orientation[0] * 180f / Math.PI) % 360;
1924 if (mHeading < 0) {
1925 mHeading += 360;
1926 }
Angus Kong0d00a892013-03-26 11:40:40 -07001927 }
Doris Liu6432cd62013-06-13 17:20:31 -07001928
1929 @Override
ztenghui7b265a62013-09-09 14:58:44 -07001930 public void onPreviewFocusChanged(boolean previewFocused) {
1931 mUI.onPreviewFocusChanged(previewFocused);
Doris Liu6432cd62013-06-13 17:20:31 -07001932 }
1933
Ruben Brunkd217ed02013-10-08 23:31:13 -07001934 // For debugging only.
1935 public void setDebugUri(Uri uri) {
1936 mDebugUri = uri;
1937 }
1938
1939 // For debugging only.
1940 private void saveToDebugUri(byte[] data) {
1941 if (mDebugUri != null) {
1942 OutputStream outputStream = null;
1943 try {
1944 outputStream = mContentResolver.openOutputStream(mDebugUri);
1945 outputStream.write(data);
1946 outputStream.close();
1947 } catch (IOException e) {
1948 Log.e(TAG, "Exception while writing debug jpeg file", e);
1949 } finally {
1950 CameraUtil.closeSilently(outputStream);
1951 }
1952 }
1953 }
1954
Doris Liu6432cd62013-06-13 17:20:31 -07001955/* Below is no longer needed, except to get rid of compile error
1956 * TODO: Remove these
1957 */
1958
1959 // TODO: Delete this function after old camera code is removed
1960 @Override
1961 public void onRestorePreferencesClicked() {}
1962
Michael Kolb8872c232013-01-29 10:33:22 -08001963}