2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera;
19 import android.annotation.TargetApi;
20 import android.app.Activity;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.graphics.Bitmap;
25 import android.graphics.BitmapFactory;
26 import android.graphics.SurfaceTexture;
27 import android.hardware.Camera.CameraInfo;
28 import android.hardware.Camera.Parameters;
29 import android.hardware.Sensor;
30 import android.hardware.SensorEvent;
31 import android.hardware.SensorEventListener;
32 import android.hardware.SensorManager;
33 import android.location.Location;
34 import android.media.AudioManager;
35 import android.media.CameraProfile;
36 import android.media.SoundPool;
37 import android.net.Uri;
38 import android.os.AsyncTask;
39 import android.os.Build;
40 import android.os.Bundle;
41 import android.os.Handler;
42 import android.os.Looper;
43 import android.os.Message;
44 import android.os.MessageQueue;
45 import android.os.SystemClock;
46 import android.provider.MediaStore;
47 import android.view.KeyEvent;
48 import android.view.OrientationEventListener;
49 import android.view.View;
51 import com.android.camera.PhotoModule.NamedImages.NamedEntity;
52 import com.android.camera.app.AppController;
53 import com.android.camera.app.CameraAppUI;
54 import com.android.camera.app.CameraProvider;
55 import com.android.camera.app.LocationManager;
56 import com.android.camera.app.MediaSaver;
57 import com.android.camera.app.MemoryManager;
58 import com.android.camera.app.MemoryManager.MemoryListener;
59 import com.android.camera.app.MotionManager;
60 import com.android.camera.cameradevice.CameraCapabilities;
61 import com.android.camera.cameradevice.CameraManager;
62 import com.android.camera.cameradevice.CameraManager.CameraAFCallback;
63 import com.android.camera.cameradevice.CameraManager.CameraAFMoveCallback;
64 import com.android.camera.cameradevice.CameraManager.CameraPictureCallback;
65 import com.android.camera.cameradevice.CameraManager.CameraProxy;
66 import com.android.camera.cameradevice.CameraManager.CameraShutterCallback;
67 import com.android.camera.debug.Log;
68 import com.android.camera.exif.ExifInterface;
69 import com.android.camera.exif.ExifTag;
70 import com.android.camera.exif.Rational;
71 import com.android.camera.hardware.HardwareSpec;
72 import com.android.camera.hardware.HardwareSpecImpl;
73 import com.android.camera.module.ModuleController;
74 import com.android.camera.remote.RemoteCameraModule;
75 import com.android.camera.settings.CameraPictureSizesCacher;
76 import com.android.camera.settings.ResolutionUtil;
77 import com.android.camera.settings.SettingsManager;
78 import com.android.camera.settings.SettingsUtil;
79 import com.android.camera.ui.CountDownView;
80 import com.android.camera.ui.TouchCoordinate;
81 import com.android.camera.util.ApiHelper;
82 import com.android.camera.util.CameraUtil;
83 import com.android.camera.util.GcamHelper;
84 import com.android.camera.util.SessionStatsCollector;
85 import com.android.camera.util.Size;
86 import com.android.camera.util.UsageStatistics;
87 import com.android.camera.widget.AspectRatioSelector;
88 import com.android.camera2.R;
89 import com.google.common.logging.eventprotos;
91 import java.io.ByteArrayOutputStream;
93 import java.io.FileNotFoundException;
94 import java.io.FileOutputStream;
95 import java.io.IOException;
96 import java.io.OutputStream;
97 import java.lang.ref.WeakReference;
98 import java.text.DecimalFormat;
99 import java.util.List;
100 import java.util.Vector;
102 public class PhotoModule
104 implements PhotoController,
107 FocusOverlayManager.Listener,
109 SettingsManager.OnSettingChangedListener,
111 CountDownView.OnCountDownStatusListener {
113 private static final Log.Tag TAG = new Log.Tag("PhotoModule");
115 // We number the request code from 1000 to avoid collision with Gallery.
116 private static final int REQUEST_CROP = 1000;
118 // Messages defined for the UI thread handler.
119 private static final int MSG_FIRST_TIME_INIT = 1;
120 private static final int MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE = 2;
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;
128 // 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;
132 private static final String DEBUG_IMAGE_PREFIX = "DEBUG_";
134 private static DecimalFormat sMegaPixelFormat = new DecimalFormat("##0.0");
136 private CameraActivity mActivity;
137 private CameraProxy mCameraDevice;
138 private int mCameraId;
139 private CameraCapabilities mCameraCapabilities;
140 private Parameters mParameters;
141 private boolean mPaused;
142 private boolean mShouldSetPreviewCallbacks = ApiHelper.SHOULD_HARD_RESET_PREVIEW_CALLBACK;
146 // The activity is going to switch to the specified camera id. This is
147 // needed because texture copy is done in GL thread. -1 means camera is not
149 protected int mPendingSwitchCameraId = -1;
151 // When setCameraParametersWhenIdle() is called, we accumulate the subsets
152 // needed to be updated in mUpdateSet.
153 private int mUpdateSet;
155 private static final int SCREEN_DELAY = 2 * 60 * 1000;
157 private int mZoomValue; // The current zoom value.
158 private int mTimerDuration;
159 /** Set when a volume button is clicked to take photo */
160 private boolean mVolumeButtonClickedFlag = false;
162 private Parameters mInitialParams;
163 private boolean mFocusAreaSupported;
164 private boolean mMeteringAreaSupported;
165 private boolean mAeLockSupported;
166 private boolean mAwbLockSupported;
167 private boolean mContinuousFocusSupported;
169 // The degrees of the device rotated clockwise from its natural orientation.
170 private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
172 private static final String sTempCropFilename = "crop-temp";
174 private boolean mFaceDetectionStarted = false;
176 // mCropValue and mSaveUri are used only if isImageCaptureIntent() is true.
177 private String mCropValue;
178 private Uri mSaveUri;
180 private Uri mDebugUri;
182 // We use a queue to generated names of the images to be used later
183 // when the image is ready to be saved.
184 private NamedImages mNamedImages;
186 private final Runnable mDoSnapRunnable = new Runnable() {
189 onShutterButtonClick();
194 * An unpublished intent flag requesting to return as soon as capturing is
195 * completed. TODO: consider publishing by moving into MediaStore.
197 private static final String EXTRA_QUICK_CAPTURE =
198 "android.intent.extra.quickCapture";
200 // The display rotation in degrees. This is only valid when mCameraState is
201 // not PREVIEW_STOPPED.
202 private int mDisplayRotation;
203 // The value for android.hardware.Camera.setDisplayOrientation.
204 private int mCameraDisplayOrientation;
205 // The value for UI components like indicators.
206 private int mDisplayOrientation;
207 // The value for android.hardware.Camera.Parameters.setRotation.
208 private int mJpegRotation;
209 // Indicates whether we are using front camera
210 private boolean mMirror;
211 private boolean mFirstTimeInitialized;
212 private boolean mIsImageCaptureIntent;
214 private int mCameraState = PREVIEW_STOPPED;
215 private boolean mSnapshotOnIdle = false;
217 private ContentResolver mContentResolver;
219 private LocationManager mLocationManager;
220 private AppController mAppController;
222 private final PostViewPictureCallback mPostViewPictureCallback =
223 new PostViewPictureCallback();
224 private final RawPictureCallback mRawPictureCallback =
225 new RawPictureCallback();
226 private final AutoFocusCallback mAutoFocusCallback =
227 new AutoFocusCallback();
228 private final Object mAutoFocusMoveCallback =
229 ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK
230 ? new AutoFocusMoveCallback()
233 private final CameraErrorCallback mErrorCallback = new CameraErrorCallback();
235 private long mFocusStartTime;
236 private long mShutterCallbackTime;
237 private long mPostViewPictureCallbackTime;
238 private long mRawPictureCallbackTime;
239 private long mJpegPictureCallbackTime;
240 private long mOnResumeTime;
241 private byte[] mJpegImageData;
242 /** Touch coordinate for shutter button press. */
243 private TouchCoordinate mShutterTouchCoordinate;
246 // These latency time are for the CameraLatency test.
247 public long mAutoFocusTime;
248 public long mShutterLag;
249 public long mShutterToPictureDisplayedTime;
250 public long mPictureDisplayedToJpegCallbackTime;
251 public long mJpegCallbackFinishTime;
252 public long mCaptureStartTime;
254 // This handles everything about focus.
255 private FocusOverlayManager mFocusManager;
257 private final int mGcamModeIndex;
258 private final CountdownSoundPlayer mCountdownSoundPlayer = new CountdownSoundPlayer();
260 private String mSceneMode;
262 private final Handler mHandler = new MainHandler(this);
264 private boolean mQuickCapture;
265 private SensorManager mSensorManager;
266 private final float[] mGData = new float[3];
267 private final float[] mMData = new float[3];
268 private final float[] mR = new float[16];
269 private int mHeading = -1;
271 /** True if all the parameters needed to start preview is ready. */
272 private boolean mCameraPreviewParamsReady = false;
274 private final MediaSaver.OnMediaSavedListener mOnMediaSavedListener =
275 new MediaSaver.OnMediaSavedListener() {
277 public void onMediaSaved(Uri uri) {
279 mActivity.notifyNewMedia(uri);
283 private boolean mShouldResizeTo16x9 = false;
285 private final Runnable mResumeTaskRunnable = new Runnable() {
293 * We keep the flash setting before entering scene modes (HDR)
294 * and restore it after HDR is off.
296 private String mFlashModeBeforeSceneMode;
299 * This callback gets called when user select whether or not to
300 * turn on geo-tagging.
302 public interface LocationDialogCallback {
304 * Gets called after user selected/unselected geo-tagging feature.
306 * @param selected whether or not geo-tagging feature is selected
308 public void onLocationTaggingSelected(boolean selected);
312 * This callback defines the text that is shown in the aspect ratio selection
313 * dialog, provides the current aspect ratio, and gets notified when user changes
314 * aspect ratio selection in the dialog.
316 public interface AspectRatioDialogCallback {
318 * Returns current aspect ratio that is being used to set as default.
320 public AspectRatioSelector.AspectRatio getCurrentAspectRatio();
323 * Gets notified when user has made the aspect ratio selection.
325 * @param newAspectRatio aspect ratio that user has selected
326 * @param dialogHandlingFinishedRunnable runnable to run when the operations
327 * needed to handle changes from dialog
330 public void onAspectRatioSelected(AspectRatioSelector.AspectRatio newAspectRatio,
331 Runnable dialogHandlingFinishedRunnable);
334 private void checkDisplayRotation() {
335 // Set the display orientation if display rotation has changed.
336 // Sometimes this happens when the device is held upside
337 // down and camera app is opened. Rotation animation will
338 // take some time and the rotation value we have got may be
339 // wrong. Framework does not have a callback for this now.
340 if (CameraUtil.getDisplayRotation(mActivity) != mDisplayRotation) {
341 setDisplayOrientation();
343 if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) {
344 mHandler.postDelayed(new Runnable() {
347 checkDisplayRotation();
354 * This Handler is used to post message back onto the main thread of the
357 private static class MainHandler extends Handler {
358 private final WeakReference<PhotoModule> mModule;
360 public MainHandler(PhotoModule module) {
361 super(Looper.getMainLooper());
362 mModule = new WeakReference<PhotoModule>(module);
366 public void handleMessage(Message msg) {
367 PhotoModule module = mModule.get();
368 if (module == null) {
372 case MSG_FIRST_TIME_INIT: {
373 module.initializeFirstTime();
377 case MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE: {
378 module.setCameraParametersWhenIdle(0);
385 private void switchToGcamCapture() {
386 if (mActivity != null && mGcamModeIndex != 0) {
387 SettingsManager settingsManager = mActivity.getSettingsManager();
388 settingsManager.set(SettingsManager.SETTING_CAMERA_HDR_PLUS,
389 SettingsManager.VALUE_ON);
391 // Disable the HDR+ button to prevent callbacks from being
392 // queued before the correct callback is attached to the button
393 // in the new module. The new module will set the enabled/disabled
394 // of this button when the module's preferred camera becomes available.
395 ButtonManager buttonManager = mActivity.getButtonManager();
396 buttonManager.disableButton(ButtonManager.BUTTON_HDR_PLUS);
398 mAppController.getCameraAppUI().freezeScreenUntilPreviewReady();
400 // Do not post this to avoid this module switch getting interleaved with
401 // other button callbacks.
402 mActivity.onModeSelected(mGcamModeIndex);
407 * Constructs a new photo module.
409 public PhotoModule(AppController app) {
411 mGcamModeIndex = app.getAndroidContext().getResources()
412 .getInteger(R.integer.camera_mode_gcam);
416 public void init(CameraActivity activity, boolean isSecureCamera, boolean isCaptureIntent) {
417 mActivity = activity;
418 // TODO: Need to look at the controller interface to see if we can get
419 // rid of passing in the activity directly.
420 mAppController = mActivity;
422 mUI = new PhotoUI(mActivity, this, mActivity.getModuleLayoutRoot());
423 mActivity.setPreviewStatusListener(mUI);
425 SettingsManager settingsManager = mActivity.getSettingsManager();
426 mCameraId = Integer.parseInt(settingsManager.get(SettingsManager.SETTING_CAMERA_ID));
428 // TODO: Move this to SettingsManager as a part of upgrade procedure.
429 if (!settingsManager.getBoolean(SettingsManager.SETTING_USER_SELECTED_ASPECT_RATIO)) {
430 // Switch to back camera to set aspect ratio.
431 mCameraId = Integer.parseInt(settingsManager
432 .getDefaultCameraIdSetting(activity).getDefault());
435 mContentResolver = mActivity.getContentResolver();
437 // Surface texture is from camera screen nail and startPreview needs it.
438 // This must be done before startPreview.
439 mIsImageCaptureIntent = isImageCaptureIntent();
441 mActivity.getCameraProvider().requestCamera(mCameraId);
443 mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
444 mLocationManager = mActivity.getLocationManager();
445 mSensorManager = (SensorManager) (mActivity.getSystemService(Context.SENSOR_SERVICE));
446 mUI.setCountdownFinishedListener(this);
448 // TODO: Make this a part of app controller API.
449 View cancelButton = mActivity.findViewById(R.id.shutter_cancel_button);
450 cancelButton.setOnClickListener(new View.OnClickListener() {
452 public void onClick(View view) {
458 private void cancelCountDown() {
459 if (mUI.isCountingDown()) {
460 // Cancel on-going countdown.
461 mUI.cancelCountDown();
463 mAppController.getCameraAppUI().transitionToCapture();
464 mAppController.getCameraAppUI().showModeOptions();
468 public boolean isUsingBottomBar() {
472 private void initializeControlByIntent() {
473 if (mIsImageCaptureIntent) {
474 mActivity.getCameraAppUI().transitionToIntentCaptureLayout();
475 setupCaptureParams();
479 private void onPreviewStarted() {
480 mAppController.onPreviewStarted();
481 setCameraState(IDLE);
482 startFaceDetection();
487 * Prompt the user to pick to record location and choose aspect ratio for the
488 * very first run of camera only.
490 private void settingsFirstRun() {
491 final SettingsManager settingsManager = mActivity.getSettingsManager();
493 if (mActivity.isSecureCamera() || isImageCaptureIntent()) {
497 boolean locationPrompt = !settingsManager.isSet(SettingsManager.SETTING_RECORD_LOCATION);
498 boolean aspectRatioPrompt = !settingsManager.getBoolean(
499 SettingsManager.SETTING_USER_SELECTED_ASPECT_RATIO);
500 if (!locationPrompt && !aspectRatioPrompt) {
504 // Check if the back camera exists
505 int backCameraId = mAppController.getCameraProvider().getFirstBackCameraId();
506 if (backCameraId == -1) {
507 // If there is no back camera, do not show the prompt.
511 if (locationPrompt) {
512 // Show both location and aspect ratio selection dialog.
513 mUI.showLocationAndAspectRatioDialog(new LocationDialogCallback(){
515 public void onLocationTaggingSelected(boolean selected) {
516 settingsManager.setLocation(selected, mActivity.getLocationManager());
518 }, createAspectRatioDialogCallback());
520 // App upgrade. Only show aspect ratio selection.
521 mUI.showAspectRatioDialog(createAspectRatioDialogCallback());
525 private AspectRatioDialogCallback createAspectRatioDialogCallback() {
526 Size currentSize = new Size(mParameters.getPictureSize());
527 float aspectRatio = (float) currentSize.width() / (float) currentSize.height();
528 if (aspectRatio < 1f) {
529 aspectRatio = 1 / aspectRatio;
531 final AspectRatioSelector.AspectRatio currentAspectRatio;
532 if (Math.abs(aspectRatio - 4f / 3f) <= 0.1f) {
533 currentAspectRatio = AspectRatioSelector.AspectRatio.ASPECT_RATIO_4x3;
534 } else if (Math.abs(aspectRatio - 16f / 9f) <= 0.1f) {
535 currentAspectRatio = AspectRatioSelector.AspectRatio.ASPECT_RATIO_16x9;
537 // TODO: Log error and not show dialog.
541 List<Size> sizes = Size.buildListFromCameraSizes(mParameters.getSupportedPictureSizes());
542 List<Size> pictureSizes = ResolutionUtil
543 .getDisplayableSizesFromSupported(sizes, true);
545 // This logic below finds the largest resolution for each aspect ratio.
546 // TODO: Move this somewhere that can be shared with SettingsActivity
547 int aspectRatio4x3Resolution = 0;
548 int aspectRatio16x9Resolution = 0;
549 Size largestSize4x3 = new Size(0, 0);
550 Size largestSize16x9 = new Size(0, 0);
551 for (Size size : pictureSizes) {
552 float pictureAspectRatio = (float) size.width() / (float) size.height();
553 pictureAspectRatio = pictureAspectRatio < 1 ?
554 1f / pictureAspectRatio : pictureAspectRatio;
555 int resolution = size.width() * size.height();
556 if (Math.abs(pictureAspectRatio - 4f / 3f) < 0.1f) {
557 if (resolution > aspectRatio4x3Resolution) {
558 aspectRatio4x3Resolution = resolution;
559 largestSize4x3 = size;
561 } else if (Math.abs(pictureAspectRatio - 16f / 9f) < 0.1f) {
562 if (resolution > aspectRatio16x9Resolution) {
563 aspectRatio16x9Resolution = resolution;
564 largestSize16x9 = size;
569 // Use the largest 4x3 and 16x9 sizes as candidates for picture size selection.
570 final Size size4x3ToSelect = largestSize4x3;
571 final Size size16x9ToSelect = largestSize16x9;
573 AspectRatioDialogCallback callback = new AspectRatioDialogCallback() {
576 public AspectRatioSelector.AspectRatio getCurrentAspectRatio() {
577 return currentAspectRatio;
581 public void onAspectRatioSelected(AspectRatioSelector.AspectRatio newAspectRatio,
582 Runnable dialogHandlingFinishedRunnable) {
583 if (newAspectRatio == AspectRatioSelector.AspectRatio.ASPECT_RATIO_4x3) {
584 String largestSize4x3Text = SettingsUtil.sizeToSetting(size4x3ToSelect);
585 mActivity.getSettingsManager().set(SettingsManager.SETTING_PICTURE_SIZE_BACK,
587 } else if (newAspectRatio == AspectRatioSelector.AspectRatio.ASPECT_RATIO_16x9) {
588 String largestSize16x9Text = SettingsUtil.sizeToSetting(size16x9ToSelect);
589 mActivity.getSettingsManager().set(SettingsManager.SETTING_PICTURE_SIZE_BACK,
590 largestSize16x9Text);
592 mActivity.getSettingsManager().setBoolean(
593 SettingsManager.SETTING_USER_SELECTED_ASPECT_RATIO, true);
594 if (newAspectRatio != currentAspectRatio) {
597 mUI.setRunnableForNextFrame(dialogHandlingFinishedRunnable);
599 mHandler.post(dialogHandlingFinishedRunnable);
607 public void onPreviewUIReady() {
612 public void onPreviewUIDestroyed() {
613 if (mCameraDevice == null) {
616 mCameraDevice.setPreviewTexture(null);
621 public void startPreCaptureAnimation() {
622 mAppController.startPreCaptureAnimation();
625 private void onCameraOpened() {
627 initializeControlByIntent();
630 private void switchCamera() {
636 mAppController.freezeScreenUntilPreviewReady();
637 SettingsManager settingsManager = mActivity.getSettingsManager();
639 Log.i(TAG, "Start to switch camera. id=" + mPendingSwitchCameraId);
641 mCameraId = mPendingSwitchCameraId;
642 settingsManager.set(SettingsManager.SETTING_CAMERA_ID, "" + mCameraId);
643 mActivity.getCameraProvider().requestCamera(mCameraId);
645 if (mFocusManager != null) {
646 mFocusManager.removeMessages();
649 mMirror = isCameraFrontFacing();
650 mFocusManager.setMirror(mMirror);
651 // Start switch camera animation. Post a message because
652 // onFrameAvailable from the old camera may already exist.
655 private final ButtonManager.ButtonCallback mCameraCallback =
656 new ButtonManager.ButtonCallback() {
658 public void onStateChanged(int state) {
659 // At the time this callback is fired, the camera id
660 // has be set to the desired camera.
662 if (mPaused || mAppController.getCameraProvider().waitingForCamera()) {
665 // If switching to back camera, and HDR+ is still on,
666 // switch back to gcam, otherwise handle callback normally.
667 SettingsManager settingsManager = mActivity.getSettingsManager();
668 if (settingsManager.isCameraBackFacing()) {
669 if (settingsManager.requestsReturnToHdrPlus()) {
670 switchToGcamCapture();
675 mPendingSwitchCameraId = state;
677 Log.d(TAG, "Start to switch camera. cameraId=" + state);
678 // We need to keep a preview frame for the animation before
679 // releasing the camera. This will trigger
680 // onPreviewTextureCopied.
681 // TODO: Need to animate the camera switch
686 private final ButtonManager.ButtonCallback mHdrPlusCallback =
687 new ButtonManager.ButtonCallback() {
689 public void onStateChanged(int state) {
690 SettingsManager settingsManager = mActivity.getSettingsManager();
691 if (GcamHelper.hasGcamCapture()) {
692 // Set the camera setting to default backfacing.
693 settingsManager.setDefault(SettingsManager.SETTING_CAMERA_ID);
694 switchToGcamCapture();
696 if (settingsManager.isHdrOn()) {
697 settingsManager.set(SettingsManager.SETTING_SCENE_MODE,
698 CameraUtil.SCENE_MODE_HDR);
700 settingsManager.set(SettingsManager.SETTING_SCENE_MODE,
701 Parameters.SCENE_MODE_AUTO);
703 updateParametersSceneMode();
704 mCameraDevice.setParameters(mParameters);
710 private final View.OnClickListener mCancelCallback = new View.OnClickListener() {
712 public void onClick(View v) {
713 onCaptureCancelled();
717 private final View.OnClickListener mDoneCallback = new View.OnClickListener() {
719 public void onClick(View v) {
724 private final View.OnClickListener mRetakeCallback = new View.OnClickListener() {
726 public void onClick(View v) {
727 mActivity.getCameraAppUI().transitionToIntentCaptureLayout();
733 public void hardResetSettings(SettingsManager settingsManager) {
734 // PhotoModule should hard reset HDR+ to off,
735 // and HDR to off if HDR+ is supported.
736 settingsManager.set(SettingsManager.SETTING_CAMERA_HDR_PLUS, SettingsManager.VALUE_OFF);
737 if (GcamHelper.hasGcamCapture()) {
738 settingsManager.set(SettingsManager.SETTING_CAMERA_HDR, SettingsManager.VALUE_OFF);
743 public HardwareSpec getHardwareSpec() {
744 return (mParameters != null ? new HardwareSpecImpl(mParameters) : null);
748 public CameraAppUI.BottomBarUISpec getBottomBarSpec() {
749 CameraAppUI.BottomBarUISpec bottomBarSpec = new CameraAppUI.BottomBarUISpec();
751 bottomBarSpec.enableCamera = true;
752 bottomBarSpec.cameraCallback = mCameraCallback;
753 bottomBarSpec.enableFlash = !SettingsManager.VALUE_ON
754 .equals(mAppController.getSettingsManager()
755 .get(SettingsManager.SETTING_CAMERA_HDR));
756 bottomBarSpec.enableHdr = true;
757 bottomBarSpec.hdrCallback = mHdrPlusCallback;
758 bottomBarSpec.enableGridLines = true;
759 if (mCameraCapabilities != null) {
760 bottomBarSpec.enableExposureCompensation = true;
761 bottomBarSpec.exposureCompensationSetCallback =
762 new CameraAppUI.BottomBarUISpec.ExposureCompensationSetCallback() {
764 public void setExposure(int value) {
765 setExposureCompensation(value);
768 bottomBarSpec.minExposureCompensation =
769 mCameraCapabilities.getMinExposureCompensation();
770 bottomBarSpec.maxExposureCompensation =
771 mCameraCapabilities.getMaxExposureCompensation();
772 bottomBarSpec.exposureCompensationStep =
773 mCameraCapabilities.getExposureCompensationStep();
776 bottomBarSpec.enableSelfTimer = true;
777 bottomBarSpec.showSelfTimer = true;
779 if (isImageCaptureIntent()) {
780 bottomBarSpec.showCancel = true;
781 bottomBarSpec.cancelCallback = mCancelCallback;
782 bottomBarSpec.showDone = true;
783 bottomBarSpec.doneCallback = mDoneCallback;
784 bottomBarSpec.showRetake = true;
785 bottomBarSpec.retakeCallback = mRetakeCallback;
788 return bottomBarSpec;
791 // either open a new camera or switch cameras
792 private void openCameraCommon() {
793 mUI.onCameraOpened(mParameters);
794 if (mIsImageCaptureIntent) {
795 // Set hdr plus to default: off.
796 SettingsManager settingsManager = mActivity.getSettingsManager();
797 settingsManager.setDefault(SettingsManager.SETTING_CAMERA_HDR_PLUS);
803 public void updatePreviewAspectRatio(float aspectRatio) {
804 mAppController.updatePreviewAspectRatio(aspectRatio);
807 private void resetExposureCompensation() {
808 SettingsManager settingsManager = mActivity.getSettingsManager();
809 if (settingsManager == null) {
810 Log.e(TAG, "Settings manager is null!");
813 settingsManager.setDefault(SettingsManager.SETTING_EXPOSURE_COMPENSATION_VALUE);
816 // Snapshots can only be taken after this is called. It should be called
817 // once only. We could have done these things in onCreate() but we want to
818 // make preview screen appear as soon as possible.
819 private void initializeFirstTime() {
820 if (mFirstTimeInitialized || mPaused) {
824 mUI.initializeFirstTime();
826 // We set the listener only when both service and shutterbutton
828 getServices().getMemoryManager().addListener(this);
830 mNamedImages = new NamedImages();
832 mFirstTimeInitialized = true;
835 mActivity.updateStorageSpaceAndHint(null);
838 // If the activity is paused and resumed, this method will be called in
840 private void initializeSecondTime() {
841 getServices().getMemoryManager().addListener(this);
842 mNamedImages = new NamedImages();
843 mUI.initializeSecondTime(mParameters);
846 private void addIdleHandler() {
847 MessageQueue queue = Looper.myQueue();
848 queue.addIdleHandler(new MessageQueue.IdleHandler() {
850 public boolean queueIdle() {
851 Storage.ensureOSXCompatible();
858 public void startFaceDetection() {
859 if (mFaceDetectionStarted) {
862 if (mCameraCapabilities.getMaxNumOfFacesSupported() > 0) {
863 mFaceDetectionStarted = true;
864 mUI.onStartFaceDetection(mDisplayOrientation, isCameraFrontFacing());
865 mCameraDevice.setFaceDetectionCallback(mHandler, mUI);
866 mCameraDevice.startFaceDetection();
867 SessionStatsCollector.instance().faceScanActive(true);
872 public void stopFaceDetection() {
873 if (!mFaceDetectionStarted) {
876 if (mCameraCapabilities.getMaxNumOfFacesSupported() > 0) {
877 mFaceDetectionStarted = false;
878 mCameraDevice.setFaceDetectionCallback(null, null);
879 mCameraDevice.stopFaceDetection();
881 SessionStatsCollector.instance().faceScanActive(false);
885 private final class ShutterCallback
886 implements CameraShutterCallback {
888 private final boolean mNeedsAnimation;
890 public ShutterCallback(boolean needsAnimation) {
891 mNeedsAnimation = needsAnimation;
895 public void onShutter(CameraProxy camera) {
896 mShutterCallbackTime = System.currentTimeMillis();
897 mShutterLag = mShutterCallbackTime - mCaptureStartTime;
898 Log.v(TAG, "mShutterLag = " + mShutterLag + "ms");
899 if (mNeedsAnimation) {
900 mActivity.runOnUiThread(new Runnable() {
903 animateAfterShutter();
910 private final class PostViewPictureCallback
911 implements CameraPictureCallback {
913 public void onPictureTaken(byte[] data, CameraProxy camera) {
914 mPostViewPictureCallbackTime = System.currentTimeMillis();
915 Log.v(TAG, "mShutterToPostViewCallbackTime = "
916 + (mPostViewPictureCallbackTime - mShutterCallbackTime)
921 private final class RawPictureCallback
922 implements CameraPictureCallback {
924 public void onPictureTaken(byte[] rawData, CameraProxy camera) {
925 mRawPictureCallbackTime = System.currentTimeMillis();
926 Log.v(TAG, "mShutterToRawCallbackTime = "
927 + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms");
931 private static class ResizeBundle {
933 float targetAspectRatio;
938 * @return Cropped image if the target aspect ratio is larger than the jpeg
939 * aspect ratio on the long axis. The original jpeg otherwise.
941 private ResizeBundle cropJpegDataToAspectRatio(ResizeBundle dataBundle) {
943 final byte[] jpegData = dataBundle.jpegData;
944 final ExifInterface exif = dataBundle.exif;
945 float targetAspectRatio = dataBundle.targetAspectRatio;
947 Bitmap original = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
948 int originalWidth = original.getWidth();
949 int originalHeight = original.getHeight();
953 if (originalWidth > originalHeight) {
954 newHeight = (int) (originalWidth / targetAspectRatio);
955 newWidth = originalWidth;
957 newWidth = (int) (originalHeight / targetAspectRatio);
958 newHeight = originalHeight;
960 int xOffset = (originalWidth - newWidth)/2;
961 int yOffset = (originalHeight - newHeight)/2;
963 if (xOffset < 0 || yOffset < 0) {
967 Bitmap resized = Bitmap.createBitmap(original,xOffset,yOffset,newWidth, newHeight);
968 exif.setTagValue(ExifInterface.TAG_PIXEL_X_DIMENSION, new Integer(newWidth));
969 exif.setTagValue(ExifInterface.TAG_PIXEL_Y_DIMENSION, new Integer(newHeight));
971 ByteArrayOutputStream stream = new ByteArrayOutputStream();
973 resized.compress(Bitmap.CompressFormat.JPEG, 90, stream);
974 dataBundle.jpegData = stream.toByteArray();
978 private final class JpegPictureCallback
979 implements CameraPictureCallback {
982 public JpegPictureCallback(Location loc) {
987 public void onPictureTaken(final byte[] originalJpegData, final CameraProxy camera) {
988 mAppController.setShutterEnabled(true);
992 if (mIsImageCaptureIntent) {
995 if (mSceneMode == CameraUtil.SCENE_MODE_HDR) {
996 mUI.setSwipingEnabled(true);
999 mJpegPictureCallbackTime = System.currentTimeMillis();
1000 // If postview callback has arrived, the captured image is displayed
1001 // in postview callback. If not, the captured image is displayed in
1002 // raw picture callback.
1003 if (mPostViewPictureCallbackTime != 0) {
1004 mShutterToPictureDisplayedTime =
1005 mPostViewPictureCallbackTime - mShutterCallbackTime;
1006 mPictureDisplayedToJpegCallbackTime =
1007 mJpegPictureCallbackTime - mPostViewPictureCallbackTime;
1009 mShutterToPictureDisplayedTime =
1010 mRawPictureCallbackTime - mShutterCallbackTime;
1011 mPictureDisplayedToJpegCallbackTime =
1012 mJpegPictureCallbackTime - mRawPictureCallbackTime;
1014 Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = "
1015 + mPictureDisplayedToJpegCallbackTime + "ms");
1017 mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden.
1018 if (!mIsImageCaptureIntent) {
1022 long now = System.currentTimeMillis();
1023 mJpegCallbackFinishTime = now - mJpegPictureCallbackTime;
1024 Log.v(TAG, "mJpegCallbackFinishTime = " + mJpegCallbackFinishTime + "ms");
1025 mJpegPictureCallbackTime = 0;
1027 final ExifInterface exif = Exif.getExif(originalJpegData);
1029 if (mShouldResizeTo16x9) {
1030 final ResizeBundle dataBundle = new ResizeBundle();
1031 dataBundle.jpegData = originalJpegData;
1032 dataBundle.targetAspectRatio = ResolutionUtil.NEXUS_5_LARGE_16_BY_9_ASPECT_RATIO;
1033 dataBundle.exif = exif;
1034 new AsyncTask<ResizeBundle, Void, ResizeBundle>() {
1037 protected ResizeBundle doInBackground(ResizeBundle... resizeBundles) {
1038 return cropJpegDataToAspectRatio(resizeBundles[0]);
1042 protected void onPostExecute(ResizeBundle result) {
1043 saveFinalPhoto(result.jpegData, result.exif, camera);
1045 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataBundle);
1048 saveFinalPhoto(originalJpegData, exif, camera);
1052 void saveFinalPhoto(final byte[] jpegData, final ExifInterface exif, CameraProxy camera) {
1054 int orientation = Exif.getOrientation(exif);
1056 float zoomValue = 0f;
1057 if (mCameraCapabilities.supports(CameraCapabilities.Feature.ZOOM)) {
1058 int zoomIndex = mParameters.getZoom();
1059 List<Integer> zoomRatios = mParameters.getZoomRatios();
1060 if (zoomRatios != null && zoomIndex < zoomRatios.size()) {
1061 zoomValue = 0.01f * zoomRatios.get(zoomIndex);
1065 boolean hdrOn = CameraUtil.SCENE_MODE_HDR.equals(mSceneMode);
1066 String flashSetting =
1067 mActivity.getSettingsManager().get(SettingsManager.SETTING_FLASH_MODE);
1068 boolean gridLinesOn = mActivity.getSettingsManager().areGridLinesOn();
1069 UsageStatistics.instance().photoCaptureDoneEvent(
1070 eventprotos.NavigationChange.Mode.PHOTO_CAPTURE,
1071 mNamedImages.mQueue.lastElement().title + ".jpg", exif,
1072 isCameraFrontFacing(), hdrOn, zoomValue, flashSetting, gridLinesOn,
1073 (float) mTimerDuration, mShutterTouchCoordinate, mVolumeButtonClickedFlag);
1074 mShutterTouchCoordinate = null;
1075 mVolumeButtonClickedFlag = false;
1077 if (!mIsImageCaptureIntent) {
1078 // Calculate the width and the height of the jpeg.
1079 Integer exifWidth = exif.getTagIntValue(ExifInterface.TAG_PIXEL_X_DIMENSION);
1080 Integer exifHeight = exif.getTagIntValue(ExifInterface.TAG_PIXEL_Y_DIMENSION);
1082 if (mShouldResizeTo16x9 && exifWidth != null && exifHeight != null) {
1084 height = exifHeight;
1087 s = new Size(mParameters.getPictureSize());
1088 if ((mJpegRotation + orientation) % 180 == 0) {
1090 height = s.height();
1096 NamedEntity name = mNamedImages.getNextNameEntity();
1097 String title = (name == null) ? null : name.title;
1098 long date = (name == null) ? -1 : name.date;
1100 // Handle debug mode outputs
1101 if (mDebugUri != null) {
1102 // If using a debug uri, save jpeg there.
1103 saveToDebugUri(jpegData);
1105 // Adjust the title of the debug image shown in mediastore.
1106 if (title != null) {
1107 title = DEBUG_IMAGE_PREFIX + title;
1111 if (title == null) {
1112 Log.e(TAG, "Unbalanced name/data pair");
1115 date = mCaptureStartTime;
1117 if (mHeading >= 0) {
1118 // heading direction has been updated by the sensor.
1119 ExifTag directionRefTag = exif.buildTag(
1120 ExifInterface.TAG_GPS_IMG_DIRECTION_REF,
1121 ExifInterface.GpsTrackRef.MAGNETIC_DIRECTION);
1122 ExifTag directionTag = exif.buildTag(
1123 ExifInterface.TAG_GPS_IMG_DIRECTION,
1124 new Rational(mHeading, 1));
1125 exif.setTag(directionRefTag);
1126 exif.setTag(directionTag);
1128 getServices().getMediaSaver().addImage(
1129 jpegData, title, date, mLocation, width, height,
1130 orientation, exif, mOnMediaSavedListener, mContentResolver);
1132 // Animate capture with real jpeg data instead of a preview
1134 mUI.animateCapture(jpegData, orientation, mMirror);
1136 mJpegImageData = jpegData;
1137 if (!mQuickCapture) {
1138 mUI.showCapturedImageForReview(jpegData, orientation, mMirror);
1144 // Send the taken photo to remote shutter listeners, if any are
1146 getServices().getRemoteShutterListener().onPictureTaken(jpegData);
1148 // Check this in advance of each shot so we don't add to shutter
1149 // latency. It's true that someone else could write to the SD card
1150 // in the mean time and fill it, but that could have happened
1151 // between the shutter press and saving the JPEG too.
1152 mActivity.updateStorageSpaceAndHint(null);
1156 private final class AutoFocusCallback implements CameraAFCallback {
1158 public void onAutoFocus(boolean focused, CameraProxy camera) {
1159 SessionStatsCollector.instance().autofocusResult(focused);
1164 mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime;
1165 Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms focused = "+focused);
1166 setCameraState(IDLE);
1167 mFocusManager.onAutoFocus(focused, false);
1171 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
1172 private final class AutoFocusMoveCallback
1173 implements CameraAFMoveCallback {
1175 public void onAutoFocusMoving(
1176 boolean moving, CameraProxy camera) {
1177 mFocusManager.onAutoFocusMoving(moving);
1178 SessionStatsCollector.instance().autofocusMoving(moving);
1183 * This class is just a thread-safe queue for name,date holder objects.
1185 public static class NamedImages {
1186 private final Vector<NamedEntity> mQueue;
1188 public NamedImages() {
1189 mQueue = new Vector<NamedEntity>();
1192 public void nameNewImage(long date) {
1193 NamedEntity r = new NamedEntity();
1194 r.title = CameraUtil.createJpegName(date);
1199 public NamedEntity getNextNameEntity() {
1200 synchronized (mQueue) {
1201 if (!mQueue.isEmpty()) {
1202 return mQueue.remove(0);
1208 public static class NamedEntity {
1209 public String title;
1214 private void setCameraState(int state) {
1215 mCameraState = state;
1217 case PREVIEW_STOPPED:
1218 case SNAPSHOT_IN_PROGRESS:
1219 case SWITCHING_CAMERA:
1220 // TODO: Tell app UI to disable swipe
1222 case PhotoController.IDLE:
1223 // TODO: Tell app UI to enable swipe
1228 private void animateAfterShutter() {
1229 // Only animate when in full screen capture mode
1230 // i.e. If monkey/a user swipes to the gallery during picture taking,
1231 // don't show animation
1232 if (!mIsImageCaptureIntent) {
1238 public boolean capture() {
1239 // If we are already in the middle of taking a snapshot or the image
1240 // save request is full then ignore.
1241 if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS
1242 || mCameraState == SWITCHING_CAMERA || !mAppController.isShutterEnabled()) {
1245 mCaptureStartTime = System.currentTimeMillis();
1247 mPostViewPictureCallbackTime = 0;
1248 mJpegImageData = null;
1250 final boolean animateBefore = (mSceneMode == CameraUtil.SCENE_MODE_HDR);
1252 if (animateBefore) {
1253 animateAfterShutter();
1256 // Set rotation and gps data.
1259 // We need to be consistent with the framework orientation (i.e. the
1260 // orientation of the UI.) when the auto-rotate screen setting is on.
1261 if (mActivity.isAutoRotateScreen()) {
1262 orientation = (360 - mDisplayRotation) % 360;
1264 orientation = mOrientation;
1266 CameraInfo info = mActivity.getCameraProvider().getCameraInfo()[mCameraId];
1267 mJpegRotation = CameraUtil.getJpegRotation(info, orientation);
1268 mParameters.setRotation(mJpegRotation);
1269 Location loc = mActivity.getLocationManager().getCurrentLocation();
1270 CameraUtil.setGpsParameters(mParameters, loc);
1271 mCameraDevice.setParameters(mParameters);
1273 // We don't want user to press the button again while taking a
1274 // multi-second HDR photo.
1275 mAppController.setShutterEnabled(false);
1276 mCameraDevice.takePicture(mHandler,
1277 new ShutterCallback(!animateBefore),
1278 mRawPictureCallback, mPostViewPictureCallback,
1279 new JpegPictureCallback(loc));
1281 mNamedImages.nameNewImage(mCaptureStartTime);
1283 mFaceDetectionStarted = false;
1284 setCameraState(SNAPSHOT_IN_PROGRESS);
1289 public void setFocusParameters() {
1290 setCameraParameters(UPDATE_PARAM_PREFERENCE);
1293 private void updateSceneMode() {
1294 // If scene mode is set, we cannot set flash mode, white balance, and
1295 // focus mode, instead, we read it from driver
1296 if (!Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
1297 overrideCameraSettings(mParameters.getFlashMode(), mParameters.getFocusMode());
1301 private void overrideCameraSettings(final String flashMode, final String focusMode) {
1302 SettingsManager settingsManager = mActivity.getSettingsManager();
1303 settingsManager.set(SettingsManager.SETTING_FLASH_MODE, flashMode);
1304 settingsManager.set(SettingsManager.SETTING_FOCUS_MODE, focusMode);
1308 public void onOrientationChanged(int orientation) {
1309 // We keep the last known orientation. So if the user first orient
1310 // the camera then point the camera to floor or sky, we still have
1311 // the correct orientation.
1312 if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
1315 mOrientation = CameraUtil.roundOrientation(orientation, mOrientation);
1319 public void onCameraAvailable(CameraProxy cameraProxy) {
1323 mCameraDevice = cameraProxy;
1325 initializeCapabilities();
1327 // Reset zoom value index.
1329 if (mFocusManager == null) {
1330 initializeFocusManager();
1332 mFocusManager.setParameters(mInitialParams, mCameraCapabilities);
1334 // Do camera parameter dependent initialization.
1335 mParameters = mCameraDevice.getParameters();
1336 setCameraParameters(UPDATE_PARAM_ALL);
1337 // Set a listener which updates camera parameters based
1338 // on changed settings.
1339 SettingsManager settingsManager = mActivity.getSettingsManager();
1340 settingsManager.addListener(this);
1341 mCameraPreviewParamsReady = true;
1349 public void onCaptureCancelled() {
1350 mActivity.setResultEx(Activity.RESULT_CANCELED, new Intent());
1355 public void onCaptureRetake() {
1359 mUI.hidePostCaptureAlert();
1360 mUI.hideIntentReviewImageView();
1365 public void onCaptureDone() {
1370 byte[] data = mJpegImageData;
1372 if (mCropValue == null) {
1373 // First handle the no crop case -- just return the value. If the
1374 // caller specifies a "save uri" then write the data to its
1375 // stream. Otherwise, pass back a scaled down version of the bitmap
1376 // directly in the extras.
1377 if (mSaveUri != null) {
1378 OutputStream outputStream = null;
1380 outputStream = mContentResolver.openOutputStream(mSaveUri);
1381 outputStream.write(data);
1382 outputStream.close();
1384 mActivity.setResultEx(Activity.RESULT_OK);
1386 } catch (IOException ex) {
1389 CameraUtil.closeSilently(outputStream);
1392 ExifInterface exif = Exif.getExif(data);
1393 int orientation = Exif.getOrientation(exif);
1394 Bitmap bitmap = CameraUtil.makeBitmap(data, 50 * 1024);
1395 bitmap = CameraUtil.rotate(bitmap, orientation);
1396 mActivity.setResultEx(Activity.RESULT_OK,
1397 new Intent("inline-data").putExtra("data", bitmap));
1401 // Save the image to a temp file and invoke the cropper
1403 FileOutputStream tempStream = null;
1405 File path = mActivity.getFileStreamPath(sTempCropFilename);
1407 tempStream = mActivity.openFileOutput(sTempCropFilename, 0);
1408 tempStream.write(data);
1410 tempUri = Uri.fromFile(path);
1411 } catch (FileNotFoundException ex) {
1412 mActivity.setResultEx(Activity.RESULT_CANCELED);
1415 } catch (IOException ex) {
1416 mActivity.setResultEx(Activity.RESULT_CANCELED);
1420 CameraUtil.closeSilently(tempStream);
1423 Bundle newExtras = new Bundle();
1424 if (mCropValue.equals("circle")) {
1425 newExtras.putString("circleCrop", "true");
1427 if (mSaveUri != null) {
1428 newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
1430 newExtras.putBoolean(CameraUtil.KEY_RETURN_DATA, true);
1432 if (mActivity.isSecureCamera()) {
1433 newExtras.putBoolean(CameraUtil.KEY_SHOW_WHEN_LOCKED, true);
1436 // TODO: Share this constant.
1437 final String CROP_ACTION = "com.android.camera.action.CROP";
1438 Intent cropIntent = new Intent(CROP_ACTION);
1440 cropIntent.setData(tempUri);
1441 cropIntent.putExtras(newExtras);
1443 mActivity.startActivityForResult(cropIntent, REQUEST_CROP);
1448 public void onShutterCoordinate(TouchCoordinate coord) {
1449 mShutterTouchCoordinate = coord;
1453 public void onShutterButtonFocus(boolean pressed) {
1454 // Do nothing. We don't support half-press to focus anymore.
1458 public void onShutterButtonClick() {
1459 if (mPaused || (mCameraState == SWITCHING_CAMERA)
1460 || (mCameraState == PREVIEW_STOPPED)) {
1461 mVolumeButtonClickedFlag = false;
1465 // Do not take the picture if there is not enough storage.
1466 if (mActivity.getStorageSpaceBytes() <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
1467 Log.i(TAG, "Not enough space or storage not ready. remaining="
1468 + mActivity.getStorageSpaceBytes());
1469 mVolumeButtonClickedFlag = false;
1472 Log.d(TAG, "onShutterButtonClick: mCameraState=" + mCameraState +
1473 " mVolumeButtonClickedFlag=" + mVolumeButtonClickedFlag);
1475 int countDownDuration = Integer.parseInt(mActivity.getSettingsManager()
1476 .get(SettingsManager.SETTING_COUNTDOWN_DURATION));
1477 mTimerDuration = countDownDuration;
1478 if (countDownDuration > 0) {
1479 // Start count down.
1480 mAppController.getCameraAppUI().transitionToCancel();
1481 mAppController.getCameraAppUI().hideModeOptions();
1482 mUI.startCountdown(countDownDuration);
1489 private void focusAndCapture() {
1490 if (mSceneMode == CameraUtil.SCENE_MODE_HDR) {
1491 mUI.setSwipingEnabled(false);
1493 // If the user wants to do a snapshot while the previous one is still
1494 // in progress, remember the fact and do it after we finish the previous
1495 // one and re-start the preview. Snapshot in progress also includes the
1496 // state that autofocus is focusing and a picture will be taken when
1497 // focus callback arrives.
1498 if ((mFocusManager.isFocusingSnapOnFinish() || mCameraState == SNAPSHOT_IN_PROGRESS)) {
1499 if (!mIsImageCaptureIntent) {
1500 mSnapshotOnIdle = true;
1505 mSnapshotOnIdle = false;
1506 mFocusManager.focusAndCapture();
1510 public void onRemainingSecondsChanged(int remainingSeconds) {
1511 mCountdownSoundPlayer.onRemainingSecondsChanged(remainingSeconds);
1515 public void onCountDownFinished() {
1516 if (mIsImageCaptureIntent) {
1517 mAppController.getCameraAppUI().transitionToIntentReviewLayout();
1519 mAppController.getCameraAppUI().transitionToCapture();
1521 mAppController.getCameraAppUI().showModeOptions();
1528 private void onResumeTasks() {
1532 Log.v(TAG, "Executing onResumeTasks.");
1533 CameraProvider camProvider = mActivity.getCameraProvider();
1534 if (camProvider == null) {
1535 // No camera provider, the Activity is destroyed already.
1538 camProvider.requestCamera(mCameraId);
1540 mJpegPictureCallbackTime = 0;
1543 mOnResumeTime = SystemClock.uptimeMillis();
1544 checkDisplayRotation();
1546 // If first time initialization is not finished, put it in the
1548 if (!mFirstTimeInitialized) {
1549 mHandler.sendEmptyMessage(MSG_FIRST_TIME_INIT);
1551 initializeSecondTime();
1554 Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1555 if (gsensor != null) {
1556 mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL);
1559 Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1560 if (msensor != null) {
1561 mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_NORMAL);
1566 * @return Whether the currently active camera is front-facing.
1568 private boolean isCameraFrontFacing() {
1569 CameraInfo info = mAppController.getCameraProvider().getCameraInfo()[mCameraId];
1570 return info.facing == CameraInfo.CAMERA_FACING_FRONT;
1574 * The focus manager is the first UI related element to get initialized, and
1575 * it requires the RenderOverlay, so initialize it here
1577 private void initializeFocusManager() {
1578 // Create FocusManager object. startPreview needs it.
1579 // if mFocusManager not null, reuse it
1580 // otherwise create a new instance
1581 if (mFocusManager != null) {
1582 mFocusManager.removeMessages();
1584 mMirror = isCameraFrontFacing();
1585 String[] defaultFocusModes = mActivity.getResources().getStringArray(
1586 R.array.pref_camera_focusmode_default_array);
1587 mFocusManager = new FocusOverlayManager(mActivity.getSettingsManager(),
1589 mInitialParams, this, mMirror,
1590 mActivity.getMainLooper(), mUI.getFocusUI());
1591 MotionManager motionManager = getServices().getMotionManager();
1592 if (motionManager != null) {
1593 motionManager.addListener(mFocusManager);
1596 mAppController.addPreviewAreaSizeChangedListener(mFocusManager);
1600 public void resume() {
1602 mCountdownSoundPlayer.loadSounds();
1603 if (mFocusManager != null) {
1604 // If camera is not open when resume is called, focus manager will
1606 // be initialized yet, in which case it will start listening to
1607 // preview area size change later in the initialization.
1608 mAppController.addPreviewAreaSizeChangedListener(mFocusManager);
1610 mAppController.addPreviewAreaSizeChangedListener(mUI);
1612 // Add delay on resume from lock screen only, in order to to speed up
1613 // the onResume --> onPause --> onResume cycle from lock screen.
1614 // Don't do always because letting go of thread can cause delay.
1615 String action = mActivity.getIntent().getAction();
1616 if (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(action)
1617 || MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action)) {
1618 Log.v(TAG, "On resume, from lock screen.");
1619 // Note: onPauseAfterSuper() will delete this runnable, so we will
1620 // at most have 1 copy queued up.
1621 mHandler.postDelayed(mResumeTaskRunnable, ON_RESUME_TASKS_DELAY_MSEC);
1623 Log.v(TAG, "On resume.");
1626 getServices().getRemoteShutterListener().onModuleReady(this);
1627 SessionStatsCollector.instance().sessionActive(true);
1631 public void pause() {
1633 mHandler.removeCallbacks(mResumeTaskRunnable);
1634 getServices().getRemoteShutterListener().onModuleExit();
1635 SessionStatsCollector.instance().sessionActive(false);
1637 Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1638 if (gsensor != null) {
1639 mSensorManager.unregisterListener(this, gsensor);
1642 Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1643 if (msensor != null) {
1644 mSensorManager.unregisterListener(this, msensor);
1647 // Reset the focus first. Camera CTS does not guarantee that
1648 // cancelAutoFocus is allowed after preview stops.
1649 if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
1650 mCameraDevice.cancelAutoFocus();
1653 // If the camera has not been opened asynchronously yet,
1654 // and startPreview hasn't been called, then this is a no-op.
1655 // (e.g. onResume -> onPause -> onResume).
1658 mCountdownSoundPlayer.release();
1660 mNamedImages = null;
1661 // If we are in an image capture intent and has taken
1662 // a picture, we just clear it in onPause.
1663 mJpegImageData = null;
1665 // Remove the messages and runnables in the queue.
1666 mHandler.removeCallbacksAndMessages(null);
1669 mActivity.enableKeepScreenOn(false);
1672 mPendingSwitchCameraId = -1;
1673 if (mFocusManager != null) {
1674 mFocusManager.removeMessages();
1676 getServices().getMemoryManager().removeListener(this);
1677 mAppController.removePreviewAreaSizeChangedListener(mFocusManager);
1678 mAppController.removePreviewAreaSizeChangedListener(mUI);
1680 SettingsManager settingsManager = mActivity.getSettingsManager();
1681 settingsManager.removeListener(this);
1685 public void destroy() {
1686 // TODO: implement this.
1690 public void onLayoutOrientationChanged(boolean isLandscape) {
1691 setDisplayOrientation();
1695 public void updateCameraOrientation() {
1696 if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) {
1697 setDisplayOrientation();
1701 private boolean canTakePicture() {
1702 return isCameraIdle()
1703 && (mActivity.getStorageSpaceBytes() > Storage.LOW_STORAGE_THRESHOLD_BYTES);
1707 public void autoFocus() {
1708 Log.v(TAG,"Starting auto focus");
1709 mFocusStartTime = System.currentTimeMillis();
1710 mCameraDevice.autoFocus(mHandler, mAutoFocusCallback);
1711 SessionStatsCollector.instance().autofocusManualTrigger();
1712 setCameraState(FOCUSING);
1716 public void cancelAutoFocus() {
1717 mCameraDevice.cancelAutoFocus();
1718 setCameraState(IDLE);
1719 setCameraParameters(UPDATE_PARAM_PREFERENCE);
1723 public void onSingleTapUp(View view, int x, int y) {
1724 if (mPaused || mCameraDevice == null || !mFirstTimeInitialized
1725 || mCameraState == SNAPSHOT_IN_PROGRESS
1726 || mCameraState == SWITCHING_CAMERA
1727 || mCameraState == PREVIEW_STOPPED) {
1731 // Check if metering area or focus area is supported.
1732 if (!mFocusAreaSupported && !mMeteringAreaSupported) {
1735 mFocusManager.onSingleTapUp(x, y);
1739 public boolean onBackPressed() {
1740 return mUI.onBackPressed();
1744 public boolean onKeyDown(int keyCode, KeyEvent event) {
1746 case KeyEvent.KEYCODE_VOLUME_UP:
1747 case KeyEvent.KEYCODE_VOLUME_DOWN:
1748 case KeyEvent.KEYCODE_FOCUS:
1749 if (/* TODO: mActivity.isInCameraApp() && */mFirstTimeInitialized &&
1750 !mActivity.getCameraAppUI().isInIntentReview()) {
1751 if (event.getRepeatCount() == 0) {
1752 onShutterButtonFocus(true);
1757 case KeyEvent.KEYCODE_CAMERA:
1758 if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1759 onShutterButtonClick();
1762 case KeyEvent.KEYCODE_DPAD_CENTER:
1763 // If we get a dpad center event without any focused view, move
1764 // the focus to the shutter button and press it.
1765 if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1766 // Start auto-focus immediately to reduce shutter lag. After
1767 // the shutter button gets the focus, onShutterButtonFocus()
1768 // will be called again but it is fine.
1769 onShutterButtonFocus(true);
1777 public boolean onKeyUp(int keyCode, KeyEvent event) {
1779 case KeyEvent.KEYCODE_VOLUME_UP:
1780 case KeyEvent.KEYCODE_VOLUME_DOWN:
1781 if (/* mActivity.isInCameraApp() && */mFirstTimeInitialized &&
1782 !mActivity.getCameraAppUI().isInIntentReview()) {
1783 if (mUI.isCountingDown()) {
1786 mVolumeButtonClickedFlag = true;
1787 onShutterButtonClick();
1792 case KeyEvent.KEYCODE_FOCUS:
1793 if (mFirstTimeInitialized) {
1794 onShutterButtonFocus(false);
1801 private void closeCamera() {
1802 if (mCameraDevice != null) {
1803 stopFaceDetection();
1804 mCameraDevice.setZoomChangeListener(null);
1805 mCameraDevice.setFaceDetectionCallback(null, null);
1806 mCameraDevice.setErrorCallback(null, null);
1808 mFaceDetectionStarted = false;
1809 mActivity.getCameraProvider().releaseCamera(mCameraDevice.getCameraId());
1810 mCameraDevice = null;
1811 setCameraState(PREVIEW_STOPPED);
1812 mFocusManager.onCameraReleased();
1816 private void setDisplayOrientation() {
1817 mDisplayRotation = CameraUtil.getDisplayRotation(mActivity);
1818 mDisplayOrientation = CameraUtil.getDisplayOrientation(mDisplayRotation, mCameraId);
1819 mCameraDisplayOrientation = mDisplayOrientation;
1820 mUI.setDisplayOrientation(mDisplayOrientation);
1821 if (mFocusManager != null) {
1822 mFocusManager.setDisplayOrientation(mDisplayOrientation);
1824 // Change the camera display orientation
1825 if (mCameraDevice != null) {
1826 mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation);
1830 /** Only called by UI thread. */
1831 private void setupPreview() {
1832 mFocusManager.resetTouchFocus();
1837 * Returns whether we can/should start the preview or not.
1839 private boolean checkPreviewPreconditions() {
1844 if (mCameraDevice == null) {
1845 Log.w(TAG, "startPreview: camera device not ready yet.");
1849 SurfaceTexture st = mActivity.getCameraAppUI().getSurfaceTexture();
1851 Log.w(TAG, "startPreview: surfaceTexture is not ready.");
1855 if (!mCameraPreviewParamsReady) {
1856 Log.w(TAG, "startPreview: parameters for preview is not ready.");
1863 * The start/stop preview should only run on the UI thread.
1865 private void startPreview() {
1866 if (!checkPreviewPreconditions()) {
1870 mCameraDevice.setErrorCallback(mHandler, mErrorCallback);
1871 setDisplayOrientation();
1873 if (!mSnapshotOnIdle) {
1874 // If the focus mode is continuous autofocus, call cancelAutoFocus
1875 // to resume it because it may have been paused by autoFocus call.
1876 String focusMode = mFocusManager.getFocusMode();
1877 if (CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE.equals(focusMode)) {
1878 mCameraDevice.cancelAutoFocus();
1880 mFocusManager.setAeAwbLock(false); // Unlock AE and AWB.
1882 setCameraParameters(UPDATE_PARAM_ALL);
1884 // Workaround for KitKat and KitKat MR1 which leave configured preview
1885 // callback streams lingering around when they should have been removed.
1886 // These preview callback streams are the cause for distorted preview.
1887 // For more details, see b/12210027
1888 if (mShouldSetPreviewCallbacks) {
1889 mShouldSetPreviewCallbacks = false;
1890 Size previewSize = new Size(mCameraDevice.getParameters().getPreviewSize());
1891 mCameraDevice.setPreviewDataCallbackWithBuffer(mHandler,
1892 new CameraManager.CameraPreviewDataCallback() {
1894 public void onPreviewFrame(byte[] data, CameraProxy camera) {
1895 // Remove callback after the first frame comes in.
1896 mCameraDevice.setPreviewDataCallbackWithBuffer(null, null);
1899 mCameraDevice.addCallbackBuffer(new byte[previewSize.width() * previewSize.height()]);
1901 mCameraDevice.setPreviewTexture(mActivity.getCameraAppUI().getSurfaceTexture());
1903 Log.i(TAG, "startPreview");
1904 mCameraDevice.startPreview();
1906 mFocusManager.onPreviewStarted();
1908 SessionStatsCollector.instance().previewActive(true);
1909 if (mSnapshotOnIdle) {
1910 mHandler.post(mDoSnapRunnable);
1915 public void stopPreview() {
1916 if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
1917 Log.i(TAG, "stopPreview");
1918 mCameraDevice.stopPreview();
1919 mFaceDetectionStarted = false;
1921 setCameraState(PREVIEW_STOPPED);
1922 if (mFocusManager != null) {
1923 mFocusManager.onPreviewStopped();
1925 SessionStatsCollector.instance().previewActive(false);
1929 public void onSettingChanged(SettingsManager settingsManager, int id) {
1931 case SettingsManager.SETTING_FLASH_MODE: {
1932 updateParametersFlashMode();
1935 case SettingsManager.SETTING_CAMERA_HDR: {
1936 String val = settingsManager.get(SettingsManager.SETTING_CAMERA_HDR);
1937 if (SettingsManager.VALUE_ON.equals(val)) {
1939 mAppController.getButtonManager().disableButton(ButtonManager.BUTTON_FLASH);
1940 mFlashModeBeforeSceneMode = settingsManager.get(SettingsManager
1941 .SETTING_FLASH_MODE);
1943 if (mFlashModeBeforeSceneMode != null) {
1944 settingsManager.set(SettingsManager.SETTING_FLASH_MODE,
1945 mFlashModeBeforeSceneMode);
1946 updateParametersFlashMode();
1947 mFlashModeBeforeSceneMode = null;
1949 mAppController.getButtonManager().enableButton(ButtonManager.BUTTON_FLASH);
1958 if (mCameraDevice != null) {
1959 mCameraDevice.setParameters(mParameters);
1963 private void updateCameraParametersInitialize() {
1964 // Reset preview frame rate to the maximum because it may be lowered by
1965 // video camera application.
1966 int[] fpsRange = CameraUtil.getPhotoPreviewFpsRange(mCameraCapabilities);
1967 if (fpsRange != null && fpsRange.length > 0) {
1968 mParameters.setPreviewFpsRange(
1969 fpsRange[Parameters.PREVIEW_FPS_MIN_INDEX],
1970 fpsRange[Parameters.PREVIEW_FPS_MAX_INDEX]);
1973 mParameters.set(CameraUtil.RECORDING_HINT, CameraUtil.FALSE);
1975 // Disable video stabilization. Convenience methods not available in API
1977 String vstabSupported = mParameters.get("video-stabilization-supported");
1978 if ("true".equals(vstabSupported)) {
1979 mParameters.set("video-stabilization", "false");
1983 private void updateCameraParametersZoom() {
1985 if (mCameraCapabilities.supports(CameraCapabilities.Feature.ZOOM)) {
1986 mParameters.setZoom(mZoomValue);
1990 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
1991 private void setAutoExposureLockIfSupported() {
1992 if (mAeLockSupported) {
1993 mParameters.setAutoExposureLock(mFocusManager.getAeAwbLock());
1997 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
1998 private void setAutoWhiteBalanceLockIfSupported() {
1999 if (mAwbLockSupported) {
2000 mParameters.setAutoWhiteBalanceLock(mFocusManager.getAeAwbLock());
2004 private void setFocusAreasIfSupported() {
2005 if (mFocusAreaSupported) {
2006 mParameters.setFocusAreas(mFocusManager.getFocusAreas());
2010 private void setMeteringAreasIfSupported() {
2011 if (mMeteringAreaSupported) {
2012 mParameters.setMeteringAreas(mFocusManager.getMeteringAreas());
2016 private void updateCameraParametersPreference() {
2017 setAutoExposureLockIfSupported();
2018 setAutoWhiteBalanceLockIfSupported();
2019 setFocusAreasIfSupported();
2020 setMeteringAreasIfSupported();
2022 // Initialize focus mode.
2023 mFocusManager.overrideFocusMode(null);
2024 mParameters.setFocusMode(mFocusManager.getFocusMode());
2025 SessionStatsCollector.instance().autofocusActive(
2026 mFocusManager.getFocusMode() == CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE);
2028 // Set picture size.
2029 updateParametersPictureSize();
2031 // Set JPEG quality.
2032 updateParametersPictureQuality();
2034 // For the following settings, we need to check if the settings are
2035 // still supported by latest driver, if not, ignore the settings.
2037 // Set exposure compensation
2038 updateParametersExposureCompensation();
2040 // Set the scene mode: also sets flash and white balance.
2041 updateParametersSceneMode();
2043 if (mContinuousFocusSupported && ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK) {
2044 updateAutoFocusMoveCallback();
2048 private void updateParametersPictureSize() {
2049 SettingsManager settingsManager = mActivity.getSettingsManager();
2050 String pictureSize = settingsManager
2051 .get(isCameraFrontFacing() ? SettingsManager.SETTING_PICTURE_SIZE_FRONT
2052 : SettingsManager.SETTING_PICTURE_SIZE_BACK);
2054 List<Size> supported = Size.buildListFromCameraSizes(mParameters.getSupportedPictureSizes());
2055 CameraPictureSizesCacher.updateSizesForCamera(mAppController.getAndroidContext(),
2056 mCameraDevice.getCameraId(), supported);
2057 SettingsUtil.setCameraPictureSize(pictureSize, supported, mParameters,
2058 mCameraDevice.getCameraId());
2060 Size size = SettingsUtil.getPhotoSize(pictureSize, supported,
2061 mCameraDevice.getCameraId());
2062 if (ApiHelper.IS_NEXUS_5) {
2063 if (ResolutionUtil.NEXUS_5_LARGE_16_BY_9.equals(pictureSize)) {
2064 mShouldResizeTo16x9 = true;
2066 mShouldResizeTo16x9 = false;
2070 // Set a preview size that is closest to the viewfinder height and has
2071 // the right aspect ratio.
2072 List<Size> sizes = Size.buildListFromCameraSizes(mParameters.getSupportedPreviewSizes());
2073 Size optimalSize = CameraUtil.getOptimalPreviewSize(mActivity, sizes,
2074 (double) size.width() / size.height());
2075 Size original = new Size(mParameters.getPreviewSize());
2076 if (!original.equals(optimalSize)) {
2077 if (ApiHelper.SHOULD_HARD_RESET_PREVIEW_CALLBACK) {
2078 // Compare the aspect ratio.
2079 if ((original.width() * optimalSize.height())
2080 != (original.height() * optimalSize.width())) {
2081 // If aspect ratio has changed, set preview callback again, so
2082 // that the old preview callback stream will be forced to update.
2083 // This is a workaround for b/12210027, which was fixed in Kitkat MR2.
2084 mShouldSetPreviewCallbacks = true;
2087 mParameters.setPreviewSize(optimalSize.width(), optimalSize.height());
2089 // Zoom related settings will be changed for different preview
2090 // sizes, so set and read the parameters to get latest values
2091 if (mHandler.getLooper() == Looper.myLooper()) {
2092 // On UI thread only, not when camera starts up
2095 mCameraDevice.setParameters(mParameters);
2097 mParameters = mCameraDevice.getParameters();
2100 if (optimalSize.width() != 0 && optimalSize.height() != 0) {
2101 mUI.updatePreviewAspectRatio((float) optimalSize.width()
2102 / (float) optimalSize.height());
2104 Log.i(TAG, "Preview size is " + optimalSize);
2107 private void updateParametersPictureQuality() {
2108 int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId,
2109 CameraProfile.QUALITY_HIGH);
2110 mParameters.setJpegQuality(jpegQuality);
2113 private void updateParametersExposureCompensation() {
2114 SettingsManager settingsManager = mActivity.getSettingsManager();
2115 if (settingsManager.getBoolean(SettingsManager.SETTING_EXPOSURE_COMPENSATION_ENABLED)) {
2116 int value = Integer.parseInt(settingsManager.get(SettingsManager.SETTING_EXPOSURE_COMPENSATION_VALUE));
2117 int max = mCameraCapabilities.getMaxExposureCompensation();
2118 int min = mCameraCapabilities.getMinExposureCompensation();
2119 if (value >= min && value <= max) {
2120 mParameters.setExposureCompensation(value);
2122 Log.w(TAG, "invalid exposure range: " + value);
2125 // If exposure compensation is not enabled, reset the exposure compensation value.
2126 setExposureCompensation(0);
2131 private void updateParametersSceneMode() {
2132 SettingsManager settingsManager = mActivity.getSettingsManager();
2134 mSceneMode = settingsManager.get(SettingsManager.SETTING_SCENE_MODE);
2135 if (mCameraCapabilities
2136 .supports(mCameraCapabilities.getStringifier().sceneModeFromString(mSceneMode))) {
2137 if (!mParameters.getSceneMode().equals(mSceneMode)) {
2138 mParameters.setSceneMode(mSceneMode);
2140 // Setting scene mode will change the settings of flash mode,
2141 // white balance, and focus mode. Here we read back the
2142 // parameters, so we can know those settings.
2143 mCameraDevice.setParameters(mParameters);
2144 mParameters = mCameraDevice.getParameters();
2147 mSceneMode = mParameters.getSceneMode();
2148 if (mSceneMode == null) {
2149 mSceneMode = Parameters.SCENE_MODE_AUTO;
2153 if (Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
2155 updateParametersFlashMode();
2158 mFocusManager.overrideFocusMode(null);
2159 mParameters.setFocusMode(mFocusManager.getFocusMode());
2161 mFocusManager.overrideFocusMode(mParameters.getFocusMode());
2165 private void updateParametersFlashMode() {
2166 SettingsManager settingsManager = mActivity.getSettingsManager();
2168 String flashMode = settingsManager.get(SettingsManager.SETTING_FLASH_MODE);
2169 if (mCameraCapabilities
2170 .supports(mCameraCapabilities.getStringifier().flashModeFromString(flashMode))) {
2171 mParameters.setFlashMode(flashMode);
2175 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
2176 private void updateAutoFocusMoveCallback() {
2177 if (mParameters.getFocusMode().equals(CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE)) {
2178 mCameraDevice.setAutoFocusMoveCallback(mHandler,
2179 (CameraAFMoveCallback) mAutoFocusMoveCallback);
2181 mCameraDevice.setAutoFocusMoveCallback(null, null);
2186 * Sets the exposure compensation to the given value and also updates settings.
2188 * @param value exposure compensation value to be set
2190 public void setExposureCompensation(int value) {
2191 int max = mCameraCapabilities.getMaxExposureCompensation();
2192 int min = mCameraCapabilities.getMinExposureCompensation();
2193 if (value >= min && value <= max) {
2194 mParameters.setExposureCompensation(value);
2195 SettingsManager settingsManager = mActivity.getSettingsManager();
2196 settingsManager.set(SettingsManager.SETTING_EXPOSURE_COMPENSATION_VALUE, Integer.toString(value));
2198 Log.w(TAG, "invalid exposure range: " + value);
2202 // We separate the parameters into several subsets, so we can update only
2203 // the subsets actually need updating. The PREFERENCE set needs extra
2204 // locking because the preference can be changed from GLThread as well.
2205 private void setCameraParameters(int updateSet) {
2206 if ((updateSet & UPDATE_PARAM_INITIALIZE) != 0) {
2207 updateCameraParametersInitialize();
2210 if ((updateSet & UPDATE_PARAM_ZOOM) != 0) {
2211 updateCameraParametersZoom();
2214 if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) {
2215 updateCameraParametersPreference();
2218 mCameraDevice.setParameters(mParameters);
2221 // If the Camera is idle, update the parameters immediately, otherwise
2222 // accumulate them in mUpdateSet and update later.
2223 private void setCameraParametersWhenIdle(int additionalUpdateSet) {
2224 mUpdateSet |= additionalUpdateSet;
2225 if (mCameraDevice == null) {
2226 // We will update all the parameters when we open the device, so
2227 // we don't need to do anything now.
2230 } else if (isCameraIdle()) {
2231 setCameraParameters(mUpdateSet);
2235 if (!mHandler.hasMessages(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE)) {
2236 mHandler.sendEmptyMessageDelayed(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000);
2242 public boolean isCameraIdle() {
2243 return (mCameraState == IDLE) ||
2244 (mCameraState == PREVIEW_STOPPED) ||
2245 ((mFocusManager != null) && mFocusManager.isFocusCompleted()
2246 && (mCameraState != SWITCHING_CAMERA));
2250 public boolean isImageCaptureIntent() {
2251 String action = mActivity.getIntent().getAction();
2252 return (MediaStore.ACTION_IMAGE_CAPTURE.equals(action)
2253 || CameraActivity.ACTION_IMAGE_CAPTURE_SECURE.equals(action));
2256 private void setupCaptureParams() {
2257 Bundle myExtras = mActivity.getIntent().getExtras();
2258 if (myExtras != null) {
2259 mSaveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT);
2260 mCropValue = myExtras.getString("crop");
2264 private void initializeCapabilities() {
2265 mInitialParams = mCameraDevice.getParameters();
2266 mCameraCapabilities = mCameraDevice.getCapabilities();
2267 mFocusAreaSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.FOCUS_AREA);
2268 mMeteringAreaSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.METERING_AREA);
2269 mAeLockSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.AUTO_EXPOSURE_LOCK);
2270 mAwbLockSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.AUTO_WHITE_BALANCE_LOCK);
2271 mContinuousFocusSupported = mInitialParams.getSupportedFocusModes().contains(
2272 CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE);
2275 // TODO: Remove this
2277 public int onZoomChanged(int index) {
2278 // Not useful to change zoom value when the activity is paused.
2283 if (mParameters == null || mCameraDevice == null) {
2286 // Set zoom parameters asynchronously
2287 mParameters.setZoom(mZoomValue);
2288 mCameraDevice.setParameters(mParameters);
2289 Parameters p = mCameraDevice.getParameters();
2297 public int getCameraState() {
2298 return mCameraState;
2302 public void onMemoryStateChanged(int state) {
2303 mAppController.setShutterEnabled(state == MemoryManager.STATE_OK);
2307 public void onLowMemory() {
2308 // Not much we can do in the photo module.
2312 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2316 public void onSensorChanged(SensorEvent event) {
2317 int type = event.sensor.getType();
2319 if (type == Sensor.TYPE_ACCELEROMETER) {
2321 } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
2324 // we should not be here.
2327 for (int i = 0; i < 3; i++) {
2328 data[i] = event.values[i];
2330 float[] orientation = new float[3];
2331 SensorManager.getRotationMatrix(mR, null, mGData, mMData);
2332 SensorManager.getOrientation(mR, orientation);
2333 mHeading = (int) (orientation[0] * 180f / Math.PI) % 360;
2339 // For debugging only.
2340 public void setDebugUri(Uri uri) {
2344 // For debugging only.
2345 private void saveToDebugUri(byte[] data) {
2346 if (mDebugUri != null) {
2347 OutputStream outputStream = null;
2349 outputStream = mContentResolver.openOutputStream(mDebugUri);
2350 outputStream.write(data);
2351 outputStream.close();
2352 } catch (IOException e) {
2353 Log.e(TAG, "Exception while writing debug jpeg file", e);
2355 CameraUtil.closeSilently(outputStream);
2361 public void onRemoteShutterPress() {
2362 mHandler.post(new Runnable() {
2371 * This class manages the loading/releasing/playing of the sounds needed for
2374 private class CountdownSoundPlayer {
2375 private SoundPool mSoundPool;
2376 private int mBeepOnce;
2377 private int mBeepTwice;
2381 if (mSoundPool == null) {
2382 mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
2383 mBeepOnce = mSoundPool.load(mAppController.getAndroidContext(), R.raw.beep_once, 1);
2384 mBeepTwice = mSoundPool.load(mAppController.getAndroidContext(), R.raw.beep_twice, 1);
2388 void onRemainingSecondsChanged(int newVal) {
2389 if (mSoundPool == null) {
2390 Log.e(TAG, "Cannot play sound - they have not been loaded.");
2394 mSoundPool.play(mBeepTwice, 1.0f, 1.0f, 0, 0, 1.0f);
2395 } else if (newVal == 2 || newVal == 3) {
2396 mSoundPool.play(mBeepOnce, 1.0f, 1.0f, 0, 0, 1.0f);
2401 if (mSoundPool != null) {
2402 mSoundPool.release();