OSDN Git Service

Work in #onPictureTaken will be on a forked, so no need to fork the call.
[android-x86/packages-apps-Camera2.git] / src / com / android / camera / PhotoModule.java
1 /*
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
17 package com.android.camera;
18
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;
50
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;
90
91 import java.io.ByteArrayOutputStream;
92 import java.io.File;
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;
101
102 public class PhotoModule
103         extends CameraModule
104         implements PhotoController,
105         ModuleController,
106         MemoryListener,
107         FocusOverlayManager.Listener,
108         SensorEventListener,
109         SettingsManager.OnSettingChangedListener,
110         RemoteCameraModule,
111         CountDownView.OnCountDownStatusListener {
112
113     private static final Log.Tag TAG = new Log.Tag("PhotoModule");
114
115     // We number the request code from 1000 to avoid collision with Gallery.
116     private static final int REQUEST_CROP = 1000;
117
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;
121
122     // The subset of parameters we need to update in setCameraParameters().
123     private static final int UPDATE_PARAM_INITIALIZE = 1;
124     private static final int UPDATE_PARAM_ZOOM = 2;
125     private static final int UPDATE_PARAM_PREFERENCE = 4;
126     private static final int UPDATE_PARAM_ALL = -1;
127
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;
131
132     private static final String DEBUG_IMAGE_PREFIX = "DEBUG_";
133
134     private static DecimalFormat sMegaPixelFormat = new DecimalFormat("##0.0");
135
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;
143
144     private PhotoUI mUI;
145
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
148     // switching.
149     protected int mPendingSwitchCameraId = -1;
150
151     // When setCameraParametersWhenIdle() is called, we accumulate the subsets
152     // needed to be updated in mUpdateSet.
153     private int mUpdateSet;
154
155     private static final int SCREEN_DELAY = 2 * 60 * 1000;
156
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;
161
162     private Parameters mInitialParams;
163     private boolean mFocusAreaSupported;
164     private boolean mMeteringAreaSupported;
165     private boolean mAeLockSupported;
166     private boolean mAwbLockSupported;
167     private boolean mContinuousFocusSupported;
168
169     // The degrees of the device rotated clockwise from its natural orientation.
170     private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
171
172     private static final String sTempCropFilename = "crop-temp";
173
174     private boolean mFaceDetectionStarted = false;
175
176     // mCropValue and mSaveUri are used only if isImageCaptureIntent() is true.
177     private String mCropValue;
178     private Uri mSaveUri;
179
180     private Uri mDebugUri;
181
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;
185
186     private final Runnable mDoSnapRunnable = new Runnable() {
187         @Override
188         public void run() {
189             onShutterButtonClick();
190         }
191     };
192
193     /**
194      * An unpublished intent flag requesting to return as soon as capturing is
195      * completed. TODO: consider publishing by moving into MediaStore.
196      */
197     private static final String EXTRA_QUICK_CAPTURE =
198             "android.intent.extra.quickCapture";
199
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;
213
214     private int mCameraState = PREVIEW_STOPPED;
215     private boolean mSnapshotOnIdle = false;
216
217     private ContentResolver mContentResolver;
218
219     private LocationManager mLocationManager;
220     private AppController mAppController;
221
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()
231                     : null;
232
233     private final CameraErrorCallback mErrorCallback = new CameraErrorCallback();
234
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;
244
245
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;
253
254     // This handles everything about focus.
255     private FocusOverlayManager mFocusManager;
256
257     private final int mGcamModeIndex;
258     private final CountdownSoundPlayer mCountdownSoundPlayer = new CountdownSoundPlayer();
259
260     private String mSceneMode;
261
262     private final Handler mHandler = new MainHandler(this);
263
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;
270
271     /** True if all the parameters needed to start preview is ready. */
272     private boolean mCameraPreviewParamsReady = false;
273
274     private final MediaSaver.OnMediaSavedListener mOnMediaSavedListener =
275             new MediaSaver.OnMediaSavedListener() {
276                 @Override
277                 public void onMediaSaved(Uri uri) {
278                     if (uri != null) {
279                         mActivity.notifyNewMedia(uri);
280                     }
281                 }
282             };
283     private boolean mShouldResizeTo16x9 = false;
284
285     private final Runnable mResumeTaskRunnable = new Runnable() {
286         @Override
287         public void run() {
288             onResumeTasks();
289         }
290     };
291
292     /**
293      * We keep the flash setting before entering scene modes (HDR)
294      * and restore it after HDR is off.
295      */
296     private String mFlashModeBeforeSceneMode;
297
298     /**
299      * This callback gets called when user select whether or not to
300      * turn on geo-tagging.
301      */
302     public interface LocationDialogCallback {
303         /**
304          * Gets called after user selected/unselected geo-tagging feature.
305          *
306          * @param selected whether or not geo-tagging feature is selected
307          */
308         public void onLocationTaggingSelected(boolean selected);
309     }
310
311     /**
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.
315      */
316     public interface AspectRatioDialogCallback {
317         /**
318          * Returns current aspect ratio that is being used to set as default.
319          */
320         public AspectRatioSelector.AspectRatio getCurrentAspectRatio();
321
322         /**
323          * Gets notified when user has made the aspect ratio selection.
324          *
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
328          *                                       are finished.
329          */
330         public void onAspectRatioSelected(AspectRatioSelector.AspectRatio newAspectRatio,
331                 Runnable dialogHandlingFinishedRunnable);
332     }
333
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();
342         }
343         if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) {
344             mHandler.postDelayed(new Runnable() {
345                 @Override
346                 public void run() {
347                     checkDisplayRotation();
348                 }
349             }, 100);
350         }
351     }
352
353     /**
354      * This Handler is used to post message back onto the main thread of the
355      * application
356      */
357     private static class MainHandler extends Handler {
358         private final WeakReference<PhotoModule> mModule;
359
360         public MainHandler(PhotoModule module) {
361             super(Looper.getMainLooper());
362             mModule = new WeakReference<PhotoModule>(module);
363         }
364
365         @Override
366         public void handleMessage(Message msg) {
367             PhotoModule module = mModule.get();
368             if (module == null) {
369                 return;
370             }
371             switch (msg.what) {
372                 case MSG_FIRST_TIME_INIT: {
373                     module.initializeFirstTime();
374                     break;
375                 }
376
377                 case MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE: {
378                     module.setCameraParametersWhenIdle(0);
379                     break;
380                 }
381             }
382         }
383     }
384
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);
390
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);
397
398             mAppController.getCameraAppUI().freezeScreenUntilPreviewReady();
399
400             // Do not post this to avoid this module switch getting interleaved with
401             // other button callbacks.
402             mActivity.onModeSelected(mGcamModeIndex);
403         }
404     }
405
406     /**
407      * Constructs a new photo module.
408      */
409     public PhotoModule(AppController app) {
410         super(app);
411         mGcamModeIndex = app.getAndroidContext().getResources()
412                 .getInteger(R.integer.camera_mode_gcam);
413     }
414
415     @Override
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;
421
422         mUI = new PhotoUI(mActivity, this, mActivity.getModuleLayoutRoot());
423         mActivity.setPreviewStatusListener(mUI);
424
425         SettingsManager settingsManager = mActivity.getSettingsManager();
426         mCameraId = Integer.parseInt(settingsManager.get(SettingsManager.SETTING_CAMERA_ID));
427
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());
433         }
434
435         mContentResolver = mActivity.getContentResolver();
436
437         // Surface texture is from camera screen nail and startPreview needs it.
438         // This must be done before startPreview.
439         mIsImageCaptureIntent = isImageCaptureIntent();
440
441         mActivity.getCameraProvider().requestCamera(mCameraId);
442
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);
447
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() {
451             @Override
452             public void onClick(View view) {
453                 cancelCountDown();
454             }
455         });
456     }
457
458     private void cancelCountDown() {
459         if (mUI.isCountingDown()) {
460             // Cancel on-going countdown.
461             mUI.cancelCountDown();
462         }
463         mAppController.getCameraAppUI().transitionToCapture();
464         mAppController.getCameraAppUI().showModeOptions();
465     }
466
467     @Override
468     public boolean isUsingBottomBar() {
469         return true;
470     }
471
472     private void initializeControlByIntent() {
473         if (mIsImageCaptureIntent) {
474             mActivity.getCameraAppUI().transitionToIntentCaptureLayout();
475             setupCaptureParams();
476         }
477     }
478
479     private void onPreviewStarted() {
480         mAppController.onPreviewStarted();
481         setCameraState(IDLE);
482         startFaceDetection();
483         settingsFirstRun();
484     }
485
486     /**
487      * Prompt the user to pick to record location and choose aspect ratio for the
488      * very first run of camera only.
489      */
490     private void settingsFirstRun() {
491         final SettingsManager settingsManager = mActivity.getSettingsManager();
492
493         if (mActivity.isSecureCamera() || isImageCaptureIntent()) {
494             return;
495         }
496
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) {
501             return;
502         }
503
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.
508             return;
509         }
510
511         if (locationPrompt) {
512             // Show both location and aspect ratio selection dialog.
513             mUI.showLocationAndAspectRatioDialog(new LocationDialogCallback(){
514                 @Override
515                 public void onLocationTaggingSelected(boolean selected) {
516                     settingsManager.setLocation(selected, mActivity.getLocationManager());
517                 }
518             }, createAspectRatioDialogCallback());
519         } else {
520             // App upgrade. Only show aspect ratio selection.
521             mUI.showAspectRatioDialog(createAspectRatioDialogCallback());
522         }
523     }
524
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;
530         }
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;
536         } else {
537             // TODO: Log error and not show dialog.
538             return null;
539         }
540
541         List<Size> sizes = Size.buildListFromCameraSizes(mParameters.getSupportedPictureSizes());
542         List<Size> pictureSizes = ResolutionUtil
543                 .getDisplayableSizesFromSupported(sizes, true);
544
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;
560                 }
561             } else if (Math.abs(pictureAspectRatio - 16f / 9f) < 0.1f) {
562                 if (resolution > aspectRatio16x9Resolution) {
563                     aspectRatio16x9Resolution = resolution;
564                     largestSize16x9 = size;
565                 }
566             }
567         }
568
569         // Use the largest 4x3 and 16x9 sizes as candidates for picture size selection.
570         final Size size4x3ToSelect = largestSize4x3;
571         final Size size16x9ToSelect = largestSize16x9;
572
573         AspectRatioDialogCallback callback = new AspectRatioDialogCallback() {
574
575             @Override
576             public AspectRatioSelector.AspectRatio getCurrentAspectRatio() {
577                 return currentAspectRatio;
578             }
579
580             @Override
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,
586                             largestSize4x3Text);
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);
591                 }
592                 mActivity.getSettingsManager().setBoolean(
593                         SettingsManager.SETTING_USER_SELECTED_ASPECT_RATIO, true);
594                 if (newAspectRatio != currentAspectRatio) {
595                     stopPreview();
596                     startPreview();
597                     mUI.setRunnableForNextFrame(dialogHandlingFinishedRunnable);
598                 } else {
599                     mHandler.post(dialogHandlingFinishedRunnable);
600                 }
601             }
602         };
603         return callback;
604     }
605
606     @Override
607     public void onPreviewUIReady() {
608         startPreview();
609     }
610
611     @Override
612     public void onPreviewUIDestroyed() {
613         if (mCameraDevice == null) {
614             return;
615         }
616         mCameraDevice.setPreviewTexture(null);
617         stopPreview();
618     }
619
620     @Override
621     public void startPreCaptureAnimation() {
622         mAppController.startPreCaptureAnimation();
623     }
624
625     private void onCameraOpened() {
626         openCameraCommon();
627         initializeControlByIntent();
628     }
629
630     private void switchCamera() {
631         if (mPaused) {
632             return;
633         }
634         cancelCountDown();
635
636         mAppController.freezeScreenUntilPreviewReady();
637         SettingsManager settingsManager = mActivity.getSettingsManager();
638
639         Log.i(TAG, "Start to switch camera. id=" + mPendingSwitchCameraId);
640         closeCamera();
641         mCameraId = mPendingSwitchCameraId;
642         settingsManager.set(SettingsManager.SETTING_CAMERA_ID, "" + mCameraId);
643         mActivity.getCameraProvider().requestCamera(mCameraId);
644         mUI.clearFaces();
645         if (mFocusManager != null) {
646             mFocusManager.removeMessages();
647         }
648
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.
653     }
654
655     private final ButtonManager.ButtonCallback mCameraCallback =
656             new ButtonManager.ButtonCallback() {
657                 @Override
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.
661
662                     if (mPaused || mAppController.getCameraProvider().waitingForCamera()) {
663                         return;
664                     }
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();
671                             return;
672                         }
673                     }
674
675                     mPendingSwitchCameraId = state;
676
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
682                     switchCamera();
683                 }
684             };
685
686     private final ButtonManager.ButtonCallback mHdrPlusCallback =
687             new ButtonManager.ButtonCallback() {
688                 @Override
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();
695                     } else {
696                         if (settingsManager.isHdrOn()) {
697                             settingsManager.set(SettingsManager.SETTING_SCENE_MODE,
698                                     CameraUtil.SCENE_MODE_HDR);
699                         } else {
700                             settingsManager.set(SettingsManager.SETTING_SCENE_MODE,
701                                     Parameters.SCENE_MODE_AUTO);
702                         }
703                         updateParametersSceneMode();
704                         mCameraDevice.setParameters(mParameters);
705                         updateSceneMode();
706                     }
707                 }
708             };
709
710     private final View.OnClickListener mCancelCallback = new View.OnClickListener() {
711         @Override
712         public void onClick(View v) {
713             onCaptureCancelled();
714         }
715     };
716
717     private final View.OnClickListener mDoneCallback = new View.OnClickListener() {
718         @Override
719         public void onClick(View v) {
720             onCaptureDone();
721         }
722     };
723
724     private final View.OnClickListener mRetakeCallback = new View.OnClickListener() {
725         @Override
726         public void onClick(View v) {
727             mActivity.getCameraAppUI().transitionToIntentCaptureLayout();
728             onCaptureRetake();
729         }
730     };
731
732     @Override
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);
739         }
740     }
741
742     @Override
743     public HardwareSpec getHardwareSpec() {
744         return (mParameters != null ? new HardwareSpecImpl(mParameters) : null);
745     }
746
747     @Override
748     public CameraAppUI.BottomBarUISpec getBottomBarSpec() {
749         CameraAppUI.BottomBarUISpec bottomBarSpec = new CameraAppUI.BottomBarUISpec();
750
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() {
763                 @Override
764                 public void setExposure(int value) {
765                     setExposureCompensation(value);
766                 }
767             };
768             bottomBarSpec.minExposureCompensation =
769                 mCameraCapabilities.getMinExposureCompensation();
770             bottomBarSpec.maxExposureCompensation =
771                 mCameraCapabilities.getMaxExposureCompensation();
772             bottomBarSpec.exposureCompensationStep =
773                 mCameraCapabilities.getExposureCompensationStep();
774         }
775
776         bottomBarSpec.enableSelfTimer = true;
777         bottomBarSpec.showSelfTimer = true;
778
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;
786         }
787
788         return bottomBarSpec;
789     }
790
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);
798         }
799         updateSceneMode();
800     }
801
802     @Override
803     public void updatePreviewAspectRatio(float aspectRatio) {
804         mAppController.updatePreviewAspectRatio(aspectRatio);
805     }
806
807     private void resetExposureCompensation() {
808         SettingsManager settingsManager = mActivity.getSettingsManager();
809         if (settingsManager == null) {
810             Log.e(TAG, "Settings manager is null!");
811             return;
812         }
813         settingsManager.setDefault(SettingsManager.SETTING_EXPOSURE_COMPENSATION_VALUE);
814     }
815
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) {
821             return;
822         }
823
824         mUI.initializeFirstTime();
825
826         // We set the listener only when both service and shutterbutton
827         // are initialized.
828         getServices().getMemoryManager().addListener(this);
829
830         mNamedImages = new NamedImages();
831
832         mFirstTimeInitialized = true;
833         addIdleHandler();
834
835         mActivity.updateStorageSpaceAndHint(null);
836     }
837
838     // If the activity is paused and resumed, this method will be called in
839     // onResume.
840     private void initializeSecondTime() {
841         getServices().getMemoryManager().addListener(this);
842         mNamedImages = new NamedImages();
843         mUI.initializeSecondTime(mParameters);
844     }
845
846     private void addIdleHandler() {
847         MessageQueue queue = Looper.myQueue();
848         queue.addIdleHandler(new MessageQueue.IdleHandler() {
849             @Override
850             public boolean queueIdle() {
851                 Storage.ensureOSXCompatible();
852                 return false;
853             }
854         });
855     }
856
857     @Override
858     public void startFaceDetection() {
859         if (mFaceDetectionStarted) {
860             return;
861         }
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);
868         }
869     }
870
871     @Override
872     public void stopFaceDetection() {
873         if (!mFaceDetectionStarted) {
874             return;
875         }
876         if (mCameraCapabilities.getMaxNumOfFacesSupported() > 0) {
877             mFaceDetectionStarted = false;
878             mCameraDevice.setFaceDetectionCallback(null, null);
879             mCameraDevice.stopFaceDetection();
880             mUI.clearFaces();
881             SessionStatsCollector.instance().faceScanActive(false);
882         }
883     }
884
885     private final class ShutterCallback
886             implements CameraShutterCallback {
887
888         private final boolean mNeedsAnimation;
889
890         public ShutterCallback(boolean needsAnimation) {
891             mNeedsAnimation = needsAnimation;
892         }
893
894         @Override
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() {
901                     @Override
902                     public void run() {
903                         animateAfterShutter();
904                     }
905                 });
906             }
907         }
908     }
909
910     private final class PostViewPictureCallback
911             implements CameraPictureCallback {
912         @Override
913         public void onPictureTaken(byte[] data, CameraProxy camera) {
914             mPostViewPictureCallbackTime = System.currentTimeMillis();
915             Log.v(TAG, "mShutterToPostViewCallbackTime = "
916                     + (mPostViewPictureCallbackTime - mShutterCallbackTime)
917                     + "ms");
918         }
919     }
920
921     private final class RawPictureCallback
922             implements CameraPictureCallback {
923         @Override
924         public void onPictureTaken(byte[] rawData, CameraProxy camera) {
925             mRawPictureCallbackTime = System.currentTimeMillis();
926             Log.v(TAG, "mShutterToRawCallbackTime = "
927                     + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms");
928         }
929     }
930
931     private static class ResizeBundle {
932         byte[] jpegData;
933         float targetAspectRatio;
934         ExifInterface exif;
935     }
936
937     /**
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.
940      */
941     private ResizeBundle cropJpegDataToAspectRatio(ResizeBundle dataBundle) {
942
943         final byte[] jpegData = dataBundle.jpegData;
944         final ExifInterface exif = dataBundle.exif;
945         float targetAspectRatio = dataBundle.targetAspectRatio;
946
947         Bitmap original = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
948         int originalWidth = original.getWidth();
949         int originalHeight = original.getHeight();
950         int newWidth;
951         int newHeight;
952
953         if (originalWidth > originalHeight) {
954             newHeight = (int) (originalWidth / targetAspectRatio);
955             newWidth = originalWidth;
956         } else {
957             newWidth = (int) (originalHeight / targetAspectRatio);
958             newHeight = originalHeight;
959         }
960         int xOffset = (originalWidth - newWidth)/2;
961         int yOffset = (originalHeight - newHeight)/2;
962
963         if (xOffset < 0 || yOffset < 0) {
964             return dataBundle;
965         }
966
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));
970
971         ByteArrayOutputStream stream = new ByteArrayOutputStream();
972
973         resized.compress(Bitmap.CompressFormat.JPEG, 90, stream);
974         dataBundle.jpegData = stream.toByteArray();
975         return dataBundle;
976     }
977
978     private final class JpegPictureCallback
979             implements CameraPictureCallback {
980         Location mLocation;
981
982         public JpegPictureCallback(Location loc) {
983             mLocation = loc;
984         }
985
986         @Override
987         public void onPictureTaken(final byte[] originalJpegData, final CameraProxy camera) {
988             mAppController.setShutterEnabled(true);
989             if (mPaused) {
990                 return;
991             }
992             if (mIsImageCaptureIntent) {
993                 stopPreview();
994             }
995             if (mSceneMode == CameraUtil.SCENE_MODE_HDR) {
996                 mUI.setSwipingEnabled(true);
997             }
998
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;
1008             } else {
1009                 mShutterToPictureDisplayedTime =
1010                         mRawPictureCallbackTime - mShutterCallbackTime;
1011                 mPictureDisplayedToJpegCallbackTime =
1012                         mJpegPictureCallbackTime - mRawPictureCallbackTime;
1013             }
1014             Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = "
1015                     + mPictureDisplayedToJpegCallbackTime + "ms");
1016
1017             mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden.
1018             if (!mIsImageCaptureIntent) {
1019                 setupPreview();
1020             }
1021
1022             long now = System.currentTimeMillis();
1023             mJpegCallbackFinishTime = now - mJpegPictureCallbackTime;
1024             Log.v(TAG, "mJpegCallbackFinishTime = " + mJpegCallbackFinishTime + "ms");
1025             mJpegPictureCallbackTime = 0;
1026
1027             final ExifInterface exif = Exif.getExif(originalJpegData);
1028
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>() {
1035
1036                     @Override
1037                     protected ResizeBundle doInBackground(ResizeBundle... resizeBundles) {
1038                         return cropJpegDataToAspectRatio(resizeBundles[0]);
1039                     }
1040
1041                     @Override
1042                     protected void onPostExecute(ResizeBundle result) {
1043                         saveFinalPhoto(result.jpegData, result.exif, camera);
1044                     }
1045                 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataBundle);
1046
1047             } else {
1048                 saveFinalPhoto(originalJpegData, exif, camera);
1049             }
1050         }
1051
1052         void saveFinalPhoto(final byte[] jpegData, final ExifInterface exif, CameraProxy camera) {
1053
1054             int orientation = Exif.getOrientation(exif);
1055
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);
1062                 }
1063             }
1064
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;
1076
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);
1081                 int width, height;
1082                 if (mShouldResizeTo16x9 && exifWidth != null && exifHeight != null) {
1083                     width = exifWidth;
1084                     height = exifHeight;
1085                 } else {
1086                     Size s;
1087                     s = new Size(mParameters.getPictureSize());
1088                     if ((mJpegRotation + orientation) % 180 == 0) {
1089                         width = s.width();
1090                         height = s.height();
1091                     } else {
1092                         width = s.height();
1093                         height = s.width();
1094                     }
1095                 }
1096                 NamedEntity name = mNamedImages.getNextNameEntity();
1097                 String title = (name == null) ? null : name.title;
1098                 long date = (name == null) ? -1 : name.date;
1099
1100                 // Handle debug mode outputs
1101                 if (mDebugUri != null) {
1102                     // If using a debug uri, save jpeg there.
1103                     saveToDebugUri(jpegData);
1104
1105                     // Adjust the title of the debug image shown in mediastore.
1106                     if (title != null) {
1107                         title = DEBUG_IMAGE_PREFIX + title;
1108                     }
1109                 }
1110
1111                 if (title == null) {
1112                     Log.e(TAG, "Unbalanced name/data pair");
1113                 } else {
1114                     if (date == -1) {
1115                         date = mCaptureStartTime;
1116                     }
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);
1127                     }
1128                     getServices().getMediaSaver().addImage(
1129                             jpegData, title, date, mLocation, width, height,
1130                             orientation, exif, mOnMediaSavedListener, mContentResolver);
1131                 }
1132                 // Animate capture with real jpeg data instead of a preview
1133                 // frame.
1134                 mUI.animateCapture(jpegData, orientation, mMirror);
1135             } else {
1136                 mJpegImageData = jpegData;
1137                 if (!mQuickCapture) {
1138                     mUI.showCapturedImageForReview(jpegData, orientation, mMirror);
1139                 } else {
1140                     onCaptureDone();
1141                 }
1142             }
1143
1144             // Send the taken photo to remote shutter listeners, if any are
1145             // registered.
1146             getServices().getRemoteShutterListener().onPictureTaken(jpegData);
1147
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);
1153         }
1154     }
1155
1156     private final class AutoFocusCallback implements CameraAFCallback {
1157         @Override
1158         public void onAutoFocus(boolean focused, CameraProxy camera) {
1159             SessionStatsCollector.instance().autofocusResult(focused);
1160             if (mPaused) {
1161                 return;
1162             }
1163
1164             mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime;
1165             Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms   focused = "+focused);
1166             setCameraState(IDLE);
1167             mFocusManager.onAutoFocus(focused, false);
1168         }
1169     }
1170
1171     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
1172     private final class AutoFocusMoveCallback
1173             implements CameraAFMoveCallback {
1174         @Override
1175         public void onAutoFocusMoving(
1176                 boolean moving, CameraProxy camera) {
1177             mFocusManager.onAutoFocusMoving(moving);
1178             SessionStatsCollector.instance().autofocusMoving(moving);
1179         }
1180     }
1181
1182     /**
1183      * This class is just a thread-safe queue for name,date holder objects.
1184      */
1185     public static class NamedImages {
1186         private final Vector<NamedEntity> mQueue;
1187
1188         public NamedImages() {
1189             mQueue = new Vector<NamedEntity>();
1190         }
1191
1192         public void nameNewImage(long date) {
1193             NamedEntity r = new NamedEntity();
1194             r.title = CameraUtil.createJpegName(date);
1195             r.date = date;
1196             mQueue.add(r);
1197         }
1198
1199         public NamedEntity getNextNameEntity() {
1200             synchronized (mQueue) {
1201                 if (!mQueue.isEmpty()) {
1202                     return mQueue.remove(0);
1203                 }
1204             }
1205             return null;
1206         }
1207
1208         public static class NamedEntity {
1209             public String title;
1210             public long date;
1211         }
1212     }
1213
1214     private void setCameraState(int state) {
1215         mCameraState = state;
1216         switch (state) {
1217             case PREVIEW_STOPPED:
1218             case SNAPSHOT_IN_PROGRESS:
1219             case SWITCHING_CAMERA:
1220                 // TODO: Tell app UI to disable swipe
1221                 break;
1222             case PhotoController.IDLE:
1223                 // TODO: Tell app UI to enable swipe
1224                 break;
1225         }
1226     }
1227
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) {
1233             mUI.animateFlash();
1234         }
1235     }
1236
1237     @Override
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()) {
1243             return false;
1244         }
1245         mCaptureStartTime = System.currentTimeMillis();
1246
1247         mPostViewPictureCallbackTime = 0;
1248         mJpegImageData = null;
1249
1250         final boolean animateBefore = (mSceneMode == CameraUtil.SCENE_MODE_HDR);
1251
1252         if (animateBefore) {
1253             animateAfterShutter();
1254         }
1255
1256         // Set rotation and gps data.
1257         int orientation;
1258
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;
1263         } else {
1264             orientation = mOrientation;
1265         }
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);
1272
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));
1280
1281         mNamedImages.nameNewImage(mCaptureStartTime);
1282
1283         mFaceDetectionStarted = false;
1284         setCameraState(SNAPSHOT_IN_PROGRESS);
1285         return true;
1286     }
1287
1288     @Override
1289     public void setFocusParameters() {
1290         setCameraParameters(UPDATE_PARAM_PREFERENCE);
1291     }
1292
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());
1298         }
1299     }
1300
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);
1305     }
1306
1307     @Override
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) {
1313             return;
1314         }
1315         mOrientation = CameraUtil.roundOrientation(orientation, mOrientation);
1316     }
1317
1318     @Override
1319     public void onCameraAvailable(CameraProxy cameraProxy) {
1320         if (mPaused) {
1321             return;
1322         }
1323         mCameraDevice = cameraProxy;
1324
1325         initializeCapabilities();
1326
1327         // Reset zoom value index.
1328         mZoomValue = 0;
1329         if (mFocusManager == null) {
1330             initializeFocusManager();
1331         }
1332         mFocusManager.setParameters(mInitialParams, mCameraCapabilities);
1333
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;
1342
1343         startPreview();
1344
1345         onCameraOpened();
1346     }
1347
1348     @Override
1349     public void onCaptureCancelled() {
1350         mActivity.setResultEx(Activity.RESULT_CANCELED, new Intent());
1351         mActivity.finish();
1352     }
1353
1354     @Override
1355     public void onCaptureRetake() {
1356         if (mPaused) {
1357             return;
1358         }
1359         mUI.hidePostCaptureAlert();
1360         mUI.hideIntentReviewImageView();
1361         setupPreview();
1362     }
1363
1364     @Override
1365     public void onCaptureDone() {
1366         if (mPaused) {
1367             return;
1368         }
1369
1370         byte[] data = mJpegImageData;
1371
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;
1379                 try {
1380                     outputStream = mContentResolver.openOutputStream(mSaveUri);
1381                     outputStream.write(data);
1382                     outputStream.close();
1383
1384                     mActivity.setResultEx(Activity.RESULT_OK);
1385                     mActivity.finish();
1386                 } catch (IOException ex) {
1387                     // ignore exception
1388                 } finally {
1389                     CameraUtil.closeSilently(outputStream);
1390                 }
1391             } else {
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));
1398                 mActivity.finish();
1399             }
1400         } else {
1401             // Save the image to a temp file and invoke the cropper
1402             Uri tempUri = null;
1403             FileOutputStream tempStream = null;
1404             try {
1405                 File path = mActivity.getFileStreamPath(sTempCropFilename);
1406                 path.delete();
1407                 tempStream = mActivity.openFileOutput(sTempCropFilename, 0);
1408                 tempStream.write(data);
1409                 tempStream.close();
1410                 tempUri = Uri.fromFile(path);
1411             } catch (FileNotFoundException ex) {
1412                 mActivity.setResultEx(Activity.RESULT_CANCELED);
1413                 mActivity.finish();
1414                 return;
1415             } catch (IOException ex) {
1416                 mActivity.setResultEx(Activity.RESULT_CANCELED);
1417                 mActivity.finish();
1418                 return;
1419             } finally {
1420                 CameraUtil.closeSilently(tempStream);
1421             }
1422
1423             Bundle newExtras = new Bundle();
1424             if (mCropValue.equals("circle")) {
1425                 newExtras.putString("circleCrop", "true");
1426             }
1427             if (mSaveUri != null) {
1428                 newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
1429             } else {
1430                 newExtras.putBoolean(CameraUtil.KEY_RETURN_DATA, true);
1431             }
1432             if (mActivity.isSecureCamera()) {
1433                 newExtras.putBoolean(CameraUtil.KEY_SHOW_WHEN_LOCKED, true);
1434             }
1435
1436             // TODO: Share this constant.
1437             final String CROP_ACTION = "com.android.camera.action.CROP";
1438             Intent cropIntent = new Intent(CROP_ACTION);
1439
1440             cropIntent.setData(tempUri);
1441             cropIntent.putExtras(newExtras);
1442
1443             mActivity.startActivityForResult(cropIntent, REQUEST_CROP);
1444         }
1445     }
1446
1447     @Override
1448     public void onShutterCoordinate(TouchCoordinate coord) {
1449         mShutterTouchCoordinate = coord;
1450     }
1451
1452     @Override
1453     public void onShutterButtonFocus(boolean pressed) {
1454         // Do nothing. We don't support half-press to focus anymore.
1455     }
1456
1457     @Override
1458     public void onShutterButtonClick() {
1459         if (mPaused || (mCameraState == SWITCHING_CAMERA)
1460                 || (mCameraState == PREVIEW_STOPPED)) {
1461             mVolumeButtonClickedFlag = false;
1462             return;
1463         }
1464
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;
1470             return;
1471         }
1472         Log.d(TAG, "onShutterButtonClick: mCameraState=" + mCameraState +
1473                 " mVolumeButtonClickedFlag=" + mVolumeButtonClickedFlag);
1474
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);
1483             return;
1484         } else {
1485             focusAndCapture();
1486         }
1487     }
1488
1489     private void focusAndCapture() {
1490         if (mSceneMode == CameraUtil.SCENE_MODE_HDR) {
1491             mUI.setSwipingEnabled(false);
1492         }
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;
1501             }
1502             return;
1503         }
1504
1505         mSnapshotOnIdle = false;
1506         mFocusManager.focusAndCapture();
1507     }
1508
1509     @Override
1510     public void onRemainingSecondsChanged(int remainingSeconds) {
1511         mCountdownSoundPlayer.onRemainingSecondsChanged(remainingSeconds);
1512     }
1513
1514     @Override
1515     public void onCountDownFinished() {
1516         if (mIsImageCaptureIntent) {
1517             mAppController.getCameraAppUI().transitionToIntentReviewLayout();
1518         } else {
1519             mAppController.getCameraAppUI().transitionToCapture();
1520         }
1521         mAppController.getCameraAppUI().showModeOptions();
1522         if (mPaused) {
1523             return;
1524         }
1525         focusAndCapture();
1526     }
1527
1528     private void onResumeTasks() {
1529         if (mPaused) {
1530             return;
1531         }
1532         Log.v(TAG, "Executing onResumeTasks.");
1533         CameraProvider camProvider = mActivity.getCameraProvider();
1534         if (camProvider == null) {
1535             // No camera provider, the Activity is destroyed already.
1536             return;
1537         }
1538         camProvider.requestCamera(mCameraId);
1539
1540         mJpegPictureCallbackTime = 0;
1541         mZoomValue = 0;
1542
1543         mOnResumeTime = SystemClock.uptimeMillis();
1544         checkDisplayRotation();
1545
1546         // If first time initialization is not finished, put it in the
1547         // message queue.
1548         if (!mFirstTimeInitialized) {
1549             mHandler.sendEmptyMessage(MSG_FIRST_TIME_INIT);
1550         } else {
1551             initializeSecondTime();
1552         }
1553
1554         Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1555         if (gsensor != null) {
1556             mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL);
1557         }
1558
1559         Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1560         if (msensor != null) {
1561             mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_NORMAL);
1562         }
1563     }
1564
1565     /**
1566      * @return Whether the currently active camera is front-facing.
1567      */
1568     private boolean isCameraFrontFacing() {
1569         CameraInfo info = mAppController.getCameraProvider().getCameraInfo()[mCameraId];
1570         return info.facing == CameraInfo.CAMERA_FACING_FRONT;
1571     }
1572
1573     /**
1574      * The focus manager is the first UI related element to get initialized, and
1575      * it requires the RenderOverlay, so initialize it here
1576      */
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();
1583         } else {
1584             mMirror = isCameraFrontFacing();
1585             String[] defaultFocusModes = mActivity.getResources().getStringArray(
1586                     R.array.pref_camera_focusmode_default_array);
1587             mFocusManager = new FocusOverlayManager(mActivity.getSettingsManager(),
1588                     defaultFocusModes,
1589                     mInitialParams, this, mMirror,
1590                     mActivity.getMainLooper(), mUI.getFocusUI());
1591             MotionManager motionManager = getServices().getMotionManager();
1592             if (motionManager != null) {
1593                 motionManager.addListener(mFocusManager);
1594             }
1595         }
1596         mAppController.addPreviewAreaSizeChangedListener(mFocusManager);
1597     }
1598
1599     @Override
1600     public void resume() {
1601         mPaused = false;
1602         mCountdownSoundPlayer.loadSounds();
1603         if (mFocusManager != null) {
1604             // If camera is not open when resume is called, focus manager will
1605             // not
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);
1609         }
1610         mAppController.addPreviewAreaSizeChangedListener(mUI);
1611
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);
1622         } else {
1623             Log.v(TAG, "On resume.");
1624             onResumeTasks();
1625         }
1626         getServices().getRemoteShutterListener().onModuleReady(this);
1627         SessionStatsCollector.instance().sessionActive(true);
1628     }
1629
1630     @Override
1631     public void pause() {
1632         mPaused = true;
1633         mHandler.removeCallbacks(mResumeTaskRunnable);
1634         getServices().getRemoteShutterListener().onModuleExit();
1635         SessionStatsCollector.instance().sessionActive(false);
1636
1637         Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
1638         if (gsensor != null) {
1639             mSensorManager.unregisterListener(this, gsensor);
1640         }
1641
1642         Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
1643         if (msensor != null) {
1644             mSensorManager.unregisterListener(this, msensor);
1645         }
1646
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();
1651         }
1652
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).
1656         stopPreview();
1657         cancelCountDown();
1658         mCountdownSoundPlayer.release();
1659
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;
1664
1665         // Remove the messages and runnables in the queue.
1666         mHandler.removeCallbacksAndMessages(null);
1667
1668         closeCamera();
1669         mActivity.enableKeepScreenOn(false);
1670         mUI.onPause();
1671
1672         mPendingSwitchCameraId = -1;
1673         if (mFocusManager != null) {
1674             mFocusManager.removeMessages();
1675         }
1676         getServices().getMemoryManager().removeListener(this);
1677         mAppController.removePreviewAreaSizeChangedListener(mFocusManager);
1678         mAppController.removePreviewAreaSizeChangedListener(mUI);
1679
1680         SettingsManager settingsManager = mActivity.getSettingsManager();
1681         settingsManager.removeListener(this);
1682     }
1683
1684     @Override
1685     public void destroy() {
1686         // TODO: implement this.
1687     }
1688
1689     @Override
1690     public void onLayoutOrientationChanged(boolean isLandscape) {
1691         setDisplayOrientation();
1692     }
1693
1694     @Override
1695     public void updateCameraOrientation() {
1696         if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) {
1697             setDisplayOrientation();
1698         }
1699     }
1700
1701     private boolean canTakePicture() {
1702         return isCameraIdle()
1703                 && (mActivity.getStorageSpaceBytes() > Storage.LOW_STORAGE_THRESHOLD_BYTES);
1704     }
1705
1706     @Override
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);
1713     }
1714
1715     @Override
1716     public void cancelAutoFocus() {
1717         mCameraDevice.cancelAutoFocus();
1718         setCameraState(IDLE);
1719         setCameraParameters(UPDATE_PARAM_PREFERENCE);
1720     }
1721
1722     @Override
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) {
1728             return;
1729         }
1730
1731         // Check if metering area or focus area is supported.
1732         if (!mFocusAreaSupported && !mMeteringAreaSupported) {
1733             return;
1734         }
1735         mFocusManager.onSingleTapUp(x, y);
1736     }
1737
1738     @Override
1739     public boolean onBackPressed() {
1740         return mUI.onBackPressed();
1741     }
1742
1743     @Override
1744     public boolean onKeyDown(int keyCode, KeyEvent event) {
1745         switch (keyCode) {
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);
1753                     }
1754                     return true;
1755                 }
1756                 return false;
1757             case KeyEvent.KEYCODE_CAMERA:
1758                 if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1759                     onShutterButtonClick();
1760                 }
1761                 return true;
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);
1770                 }
1771                 return true;
1772         }
1773         return false;
1774     }
1775
1776     @Override
1777     public boolean onKeyUp(int keyCode, KeyEvent event) {
1778         switch (keyCode) {
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()) {
1784                         cancelCountDown();
1785                     } else {
1786                         mVolumeButtonClickedFlag = true;
1787                         onShutterButtonClick();
1788                     }
1789                     return true;
1790                 }
1791                 return false;
1792             case KeyEvent.KEYCODE_FOCUS:
1793                 if (mFirstTimeInitialized) {
1794                     onShutterButtonFocus(false);
1795                 }
1796                 return true;
1797         }
1798         return false;
1799     }
1800
1801     private void closeCamera() {
1802         if (mCameraDevice != null) {
1803             stopFaceDetection();
1804             mCameraDevice.setZoomChangeListener(null);
1805             mCameraDevice.setFaceDetectionCallback(null, null);
1806             mCameraDevice.setErrorCallback(null, null);
1807
1808             mFaceDetectionStarted = false;
1809             mActivity.getCameraProvider().releaseCamera(mCameraDevice.getCameraId());
1810             mCameraDevice = null;
1811             setCameraState(PREVIEW_STOPPED);
1812             mFocusManager.onCameraReleased();
1813         }
1814     }
1815
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);
1823         }
1824         // Change the camera display orientation
1825         if (mCameraDevice != null) {
1826             mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation);
1827         }
1828     }
1829
1830     /** Only called by UI thread. */
1831     private void setupPreview() {
1832         mFocusManager.resetTouchFocus();
1833         startPreview();
1834     }
1835
1836     /**
1837      * Returns whether we can/should start the preview or not.
1838      */
1839     private boolean checkPreviewPreconditions() {
1840         if (mPaused) {
1841             return false;
1842         }
1843
1844         if (mCameraDevice == null) {
1845             Log.w(TAG, "startPreview: camera device not ready yet.");
1846             return false;
1847         }
1848
1849         SurfaceTexture st = mActivity.getCameraAppUI().getSurfaceTexture();
1850         if (st == null) {
1851             Log.w(TAG, "startPreview: surfaceTexture is not ready.");
1852             return false;
1853         }
1854
1855         if (!mCameraPreviewParamsReady) {
1856             Log.w(TAG, "startPreview: parameters for preview is not ready.");
1857             return false;
1858         }
1859         return true;
1860     }
1861
1862     /**
1863      * The start/stop preview should only run on the UI thread.
1864      */
1865     private void startPreview() {
1866         if (!checkPreviewPreconditions()) {
1867             return;
1868         }
1869
1870         mCameraDevice.setErrorCallback(mHandler, mErrorCallback);
1871         setDisplayOrientation();
1872
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();
1879             }
1880             mFocusManager.setAeAwbLock(false); // Unlock AE and AWB.
1881         }
1882         setCameraParameters(UPDATE_PARAM_ALL);
1883
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() {
1893                 @Override
1894                 public void onPreviewFrame(byte[] data, CameraProxy camera) {
1895                     // Remove callback after the first frame comes in.
1896                     mCameraDevice.setPreviewDataCallbackWithBuffer(null, null);
1897                 }
1898             });
1899             mCameraDevice.addCallbackBuffer(new byte[previewSize.width() * previewSize.height()]);
1900         }
1901         mCameraDevice.setPreviewTexture(mActivity.getCameraAppUI().getSurfaceTexture());
1902
1903         Log.i(TAG, "startPreview");
1904         mCameraDevice.startPreview();
1905
1906         mFocusManager.onPreviewStarted();
1907         onPreviewStarted();
1908         SessionStatsCollector.instance().previewActive(true);
1909         if (mSnapshotOnIdle) {
1910             mHandler.post(mDoSnapRunnable);
1911         }
1912     }
1913
1914     @Override
1915     public void stopPreview() {
1916         if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
1917             Log.i(TAG, "stopPreview");
1918             mCameraDevice.stopPreview();
1919             mFaceDetectionStarted = false;
1920         }
1921         setCameraState(PREVIEW_STOPPED);
1922         if (mFocusManager != null) {
1923             mFocusManager.onPreviewStopped();
1924         }
1925         SessionStatsCollector.instance().previewActive(false);
1926     }
1927
1928     @Override
1929     public void onSettingChanged(SettingsManager settingsManager, int id) {
1930         switch (id) {
1931             case SettingsManager.SETTING_FLASH_MODE: {
1932                 updateParametersFlashMode();
1933                 break;
1934             }
1935             case SettingsManager.SETTING_CAMERA_HDR: {
1936                 String val = settingsManager.get(SettingsManager.SETTING_CAMERA_HDR);
1937                 if (SettingsManager.VALUE_ON.equals(val)) {
1938                     // HDR is on.
1939                     mAppController.getButtonManager().disableButton(ButtonManager.BUTTON_FLASH);
1940                     mFlashModeBeforeSceneMode = settingsManager.get(SettingsManager
1941                             .SETTING_FLASH_MODE);
1942                 } else {
1943                     if (mFlashModeBeforeSceneMode != null) {
1944                         settingsManager.set(SettingsManager.SETTING_FLASH_MODE,
1945                                 mFlashModeBeforeSceneMode);
1946                         updateParametersFlashMode();
1947                         mFlashModeBeforeSceneMode = null;
1948                     }
1949                     mAppController.getButtonManager().enableButton(ButtonManager.BUTTON_FLASH);
1950                 }
1951                 break;
1952             }
1953             default: {
1954                 // Do nothing.
1955             }
1956         }
1957
1958         if (mCameraDevice != null) {
1959             mCameraDevice.setParameters(mParameters);
1960         }
1961     }
1962
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]);
1971         }
1972
1973         mParameters.set(CameraUtil.RECORDING_HINT, CameraUtil.FALSE);
1974
1975         // Disable video stabilization. Convenience methods not available in API
1976         // level <= 14
1977         String vstabSupported = mParameters.get("video-stabilization-supported");
1978         if ("true".equals(vstabSupported)) {
1979             mParameters.set("video-stabilization", "false");
1980         }
1981     }
1982
1983     private void updateCameraParametersZoom() {
1984         // Set zoom.
1985         if (mCameraCapabilities.supports(CameraCapabilities.Feature.ZOOM)) {
1986             mParameters.setZoom(mZoomValue);
1987         }
1988     }
1989
1990     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
1991     private void setAutoExposureLockIfSupported() {
1992         if (mAeLockSupported) {
1993             mParameters.setAutoExposureLock(mFocusManager.getAeAwbLock());
1994         }
1995     }
1996
1997     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
1998     private void setAutoWhiteBalanceLockIfSupported() {
1999         if (mAwbLockSupported) {
2000             mParameters.setAutoWhiteBalanceLock(mFocusManager.getAeAwbLock());
2001         }
2002     }
2003
2004     private void setFocusAreasIfSupported() {
2005         if (mFocusAreaSupported) {
2006             mParameters.setFocusAreas(mFocusManager.getFocusAreas());
2007         }
2008     }
2009
2010     private void setMeteringAreasIfSupported() {
2011         if (mMeteringAreaSupported) {
2012             mParameters.setMeteringAreas(mFocusManager.getMeteringAreas());
2013         }
2014     }
2015
2016     private void updateCameraParametersPreference() {
2017         setAutoExposureLockIfSupported();
2018         setAutoWhiteBalanceLockIfSupported();
2019         setFocusAreasIfSupported();
2020         setMeteringAreasIfSupported();
2021
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);
2027
2028         // Set picture size.
2029         updateParametersPictureSize();
2030
2031         // Set JPEG quality.
2032         updateParametersPictureQuality();
2033
2034         // For the following settings, we need to check if the settings are
2035         // still supported by latest driver, if not, ignore the settings.
2036
2037         // Set exposure compensation
2038         updateParametersExposureCompensation();
2039
2040         // Set the scene mode: also sets flash and white balance.
2041         updateParametersSceneMode();
2042
2043         if (mContinuousFocusSupported && ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK) {
2044             updateAutoFocusMoveCallback();
2045         }
2046     }
2047
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);
2053
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());
2059
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;
2065             } else {
2066                 mShouldResizeTo16x9 = false;
2067             }
2068         }
2069
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;
2085                 }
2086             }
2087             mParameters.setPreviewSize(optimalSize.width(), optimalSize.height());
2088
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
2093                 setupPreview();
2094             } else {
2095                 mCameraDevice.setParameters(mParameters);
2096             }
2097             mParameters = mCameraDevice.getParameters();
2098         }
2099
2100         if (optimalSize.width() != 0 && optimalSize.height() != 0) {
2101             mUI.updatePreviewAspectRatio((float) optimalSize.width()
2102                     / (float) optimalSize.height());
2103         }
2104         Log.i(TAG, "Preview size is " + optimalSize);
2105     }
2106
2107     private void updateParametersPictureQuality() {
2108         int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId,
2109                 CameraProfile.QUALITY_HIGH);
2110         mParameters.setJpegQuality(jpegQuality);
2111     }
2112
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);
2121             } else {
2122                 Log.w(TAG, "invalid exposure range: " + value);
2123             }
2124         } else {
2125             // If exposure compensation is not enabled, reset the exposure compensation value.
2126             setExposureCompensation(0);
2127         }
2128
2129     }
2130
2131     private void updateParametersSceneMode() {
2132         SettingsManager settingsManager = mActivity.getSettingsManager();
2133
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);
2139
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();
2145             }
2146         } else {
2147             mSceneMode = mParameters.getSceneMode();
2148             if (mSceneMode == null) {
2149                 mSceneMode = Parameters.SCENE_MODE_AUTO;
2150             }
2151         }
2152
2153         if (Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
2154             // Set flash mode.
2155             updateParametersFlashMode();
2156
2157             // Set focus mode.
2158             mFocusManager.overrideFocusMode(null);
2159             mParameters.setFocusMode(mFocusManager.getFocusMode());
2160         } else {
2161             mFocusManager.overrideFocusMode(mParameters.getFocusMode());
2162         }
2163     }
2164
2165     private void updateParametersFlashMode() {
2166         SettingsManager settingsManager = mActivity.getSettingsManager();
2167
2168         String flashMode = settingsManager.get(SettingsManager.SETTING_FLASH_MODE);
2169         if (mCameraCapabilities
2170                 .supports(mCameraCapabilities.getStringifier().flashModeFromString(flashMode))) {
2171             mParameters.setFlashMode(flashMode);
2172         }
2173     }
2174
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);
2180         } else {
2181             mCameraDevice.setAutoFocusMoveCallback(null, null);
2182         }
2183     }
2184
2185     /**
2186      * Sets the exposure compensation to the given value and also updates settings.
2187      *
2188      * @param value exposure compensation value to be set
2189      */
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));
2197         } else {
2198             Log.w(TAG, "invalid exposure range: " + value);
2199         }
2200     }
2201
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();
2208         }
2209
2210         if ((updateSet & UPDATE_PARAM_ZOOM) != 0) {
2211             updateCameraParametersZoom();
2212         }
2213
2214         if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) {
2215             updateCameraParametersPreference();
2216         }
2217
2218         mCameraDevice.setParameters(mParameters);
2219     }
2220
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.
2228             mUpdateSet = 0;
2229             return;
2230         } else if (isCameraIdle()) {
2231             setCameraParameters(mUpdateSet);
2232             updateSceneMode();
2233             mUpdateSet = 0;
2234         } else {
2235             if (!mHandler.hasMessages(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE)) {
2236                 mHandler.sendEmptyMessageDelayed(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000);
2237             }
2238         }
2239     }
2240
2241     @Override
2242     public boolean isCameraIdle() {
2243         return (mCameraState == IDLE) ||
2244                 (mCameraState == PREVIEW_STOPPED) ||
2245                 ((mFocusManager != null) && mFocusManager.isFocusCompleted()
2246                 && (mCameraState != SWITCHING_CAMERA));
2247     }
2248
2249     @Override
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));
2254     }
2255
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");
2261         }
2262     }
2263
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);
2273     }
2274
2275     // TODO: Remove this
2276     @Override
2277     public int onZoomChanged(int index) {
2278         // Not useful to change zoom value when the activity is paused.
2279         if (mPaused) {
2280             return index;
2281         }
2282         mZoomValue = index;
2283         if (mParameters == null || mCameraDevice == null) {
2284             return index;
2285         }
2286         // Set zoom parameters asynchronously
2287         mParameters.setZoom(mZoomValue);
2288         mCameraDevice.setParameters(mParameters);
2289         Parameters p = mCameraDevice.getParameters();
2290         if (p != null) {
2291             return p.getZoom();
2292         }
2293         return index;
2294     }
2295
2296     @Override
2297     public int getCameraState() {
2298         return mCameraState;
2299     }
2300
2301     @Override
2302     public void onMemoryStateChanged(int state) {
2303         mAppController.setShutterEnabled(state == MemoryManager.STATE_OK);
2304     }
2305
2306     @Override
2307     public void onLowMemory() {
2308         // Not much we can do in the photo module.
2309     }
2310
2311     @Override
2312     public void onAccuracyChanged(Sensor sensor, int accuracy) {
2313     }
2314
2315     @Override
2316     public void onSensorChanged(SensorEvent event) {
2317         int type = event.sensor.getType();
2318         float[] data;
2319         if (type == Sensor.TYPE_ACCELEROMETER) {
2320             data = mGData;
2321         } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
2322             data = mMData;
2323         } else {
2324             // we should not be here.
2325             return;
2326         }
2327         for (int i = 0; i < 3; i++) {
2328             data[i] = event.values[i];
2329         }
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;
2334         if (mHeading < 0) {
2335             mHeading += 360;
2336         }
2337     }
2338
2339     // For debugging only.
2340     public void setDebugUri(Uri uri) {
2341         mDebugUri = uri;
2342     }
2343
2344     // For debugging only.
2345     private void saveToDebugUri(byte[] data) {
2346         if (mDebugUri != null) {
2347             OutputStream outputStream = null;
2348             try {
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);
2354             } finally {
2355                 CameraUtil.closeSilently(outputStream);
2356             }
2357         }
2358     }
2359
2360     @Override
2361     public void onRemoteShutterPress() {
2362         mHandler.post(new Runnable() {
2363             @Override
2364             public void run() {
2365                 focusAndCapture();
2366             }
2367         });
2368     }
2369
2370     /**
2371      * This class manages the loading/releasing/playing of the sounds needed for
2372      * countdown timer.
2373      */
2374     private class CountdownSoundPlayer {
2375         private SoundPool mSoundPool;
2376         private int mBeepOnce;
2377         private int mBeepTwice;
2378
2379         void loadSounds() {
2380             // Load the beeps.
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);
2385             }
2386         }
2387
2388         void onRemainingSecondsChanged(int newVal) {
2389             if (mSoundPool == null) {
2390                 Log.e(TAG, "Cannot play sound - they have not been loaded.");
2391                 return;
2392             }
2393             if (newVal == 1) {
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);
2397             }
2398         }
2399
2400         void release() {
2401             if (mSoundPool != null) {
2402                 mSoundPool.release();
2403                 mSoundPool = null;
2404             }
2405         }
2406     }
2407 }