Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 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; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 25 | import android.graphics.BitmapFactory; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 26 | import android.graphics.SurfaceTexture; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 27 | import android.hardware.Sensor; |
| 28 | import android.hardware.SensorEvent; |
| 29 | import android.hardware.SensorEventListener; |
| 30 | import android.hardware.SensorManager; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 31 | import android.location.Location; |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 32 | import android.media.AudioManager; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 33 | import android.media.CameraProfile; |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 34 | import android.media.SoundPool; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 35 | import android.net.Uri; |
Sascha Haeberling | bca64bf | 2014-04-17 10:51:53 -0700 | [diff] [blame] | 36 | import android.os.AsyncTask; |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 37 | import android.os.Build; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 38 | import android.os.Bundle; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 39 | import android.os.Handler; |
| 40 | import android.os.Looper; |
| 41 | import android.os.Message; |
| 42 | import android.os.MessageQueue; |
| 43 | import android.os.SystemClock; |
| 44 | import android.provider.MediaStore; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 45 | import android.view.KeyEvent; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 46 | import android.view.OrientationEventListener; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 47 | import android.view.View; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 48 | |
Sameer Padala | 2c8cc45 | 2013-11-05 18:49:12 -0800 | [diff] [blame] | 49 | import com.android.camera.PhotoModule.NamedImages.NamedEntity; |
| 50 | import com.android.camera.app.AppController; |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 51 | import com.android.camera.app.CameraAppUI; |
Angus Kong | 0b9eb5b | 2014-04-30 15:03:33 -0700 | [diff] [blame] | 52 | import com.android.camera.app.CameraProvider; |
Sascha Haeberling | f9cba4e | 2014-04-22 09:11:28 -0700 | [diff] [blame] | 53 | import com.android.camera.app.MediaSaver; |
| 54 | import com.android.camera.app.MemoryManager; |
| 55 | import com.android.camera.app.MemoryManager.MemoryListener; |
Kevin Gabayan | fb33336 | 2014-06-02 14:48:20 -0700 | [diff] [blame] | 56 | import com.android.camera.app.MotionManager; |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame] | 57 | import com.android.camera.debug.Log; |
ztenghui | a16e7b5 | 2013-08-23 11:47:56 -0700 | [diff] [blame] | 58 | import com.android.camera.exif.ExifInterface; |
| 59 | import com.android.camera.exif.ExifTag; |
| 60 | import com.android.camera.exif.Rational; |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 61 | import com.android.camera.hardware.HardwareSpec; |
| 62 | import com.android.camera.hardware.HardwareSpecImpl; |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 63 | import com.android.camera.module.ModuleController; |
Sascha Haeberling | f2994f0 | 2014-01-23 19:55:01 -0800 | [diff] [blame] | 64 | import com.android.camera.remote.RemoteCameraModule; |
Andy Huibers | 90c7ad5 | 2014-05-20 22:13:54 -0700 | [diff] [blame] | 65 | import com.android.camera.settings.CameraPictureSizesCacher; |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 66 | import com.android.camera.settings.Keys; |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 67 | import com.android.camera.settings.ResolutionUtil; |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 68 | import com.android.camera.settings.SettingsManager; |
Sascha Haeberling | 3b0ab89 | 2014-01-29 20:54:39 +0100 | [diff] [blame] | 69 | import com.android.camera.settings.SettingsUtil; |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 70 | import com.android.camera.ui.CountDownView; |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 71 | import com.android.camera.ui.TouchCoordinate; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 72 | import com.android.camera.util.ApiHelper; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 73 | import com.android.camera.util.CameraUtil; |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 74 | import com.android.camera.util.GcamHelper; |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 75 | import com.android.camera.util.SessionStatsCollector; |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 76 | import com.android.camera.util.UsageStatistics; |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 77 | import com.android.camera.widget.AspectRatioSelector; |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 78 | import com.android.camera2.R; |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 79 | import com.android.ex.camera2.portability.CameraAgent.CameraAFCallback; |
| 80 | import com.android.ex.camera2.portability.CameraAgent.CameraAFMoveCallback; |
| 81 | import com.android.ex.camera2.portability.CameraAgent.CameraPictureCallback; |
| 82 | import com.android.ex.camera2.portability.CameraAgent.CameraProxy; |
| 83 | import com.android.ex.camera2.portability.CameraAgent.CameraShutterCallback; |
Sascha Haeberling | ee36c5f | 2014-07-29 17:05:43 -0700 | [diff] [blame] | 84 | import com.android.ex.camera2.portability.CameraCapabilities; |
Sol Boucher | 43e1813 | 2014-06-19 15:03:18 -0700 | [diff] [blame] | 85 | import com.android.ex.camera2.portability.CameraDeviceInfo.Characteristics; |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 86 | import com.android.ex.camera2.portability.CameraSettings; |
| 87 | import com.android.ex.camera2.portability.Size; |
Seth Raphael | 5e09d01 | 2013-12-18 13:45:03 -0800 | [diff] [blame] | 88 | import com.google.common.logging.eventprotos; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 89 | |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 90 | import java.io.ByteArrayOutputStream; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 91 | import java.io.File; |
| 92 | import java.io.FileNotFoundException; |
| 93 | import java.io.FileOutputStream; |
| 94 | import java.io.IOException; |
| 95 | import java.io.OutputStream; |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 96 | import java.lang.ref.WeakReference; |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 97 | import java.util.ArrayList; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 98 | import java.util.List; |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 99 | import java.util.Vector; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 100 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 101 | public class PhotoModule |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 102 | extends CameraModule |
| 103 | implements PhotoController, |
| 104 | ModuleController, |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 105 | MemoryListener, |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 106 | FocusOverlayManager.Listener, |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 107 | SensorEventListener, |
Sascha Haeberling | f2994f0 | 2014-01-23 19:55:01 -0800 | [diff] [blame] | 108 | SettingsManager.OnSettingChangedListener, |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 109 | RemoteCameraModule, |
| 110 | CountDownView.OnCountDownStatusListener { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 111 | |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 112 | private static final String PHOTO_MODULE_STRING_ID = "PhotoModule"; |
| 113 | |
| 114 | private static final Log.Tag TAG = new Log.Tag(PHOTO_MODULE_STRING_ID); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 115 | |
| 116 | // We number the request code from 1000 to avoid collision with Gallery. |
| 117 | private static final int REQUEST_CROP = 1000; |
| 118 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 119 | // Messages defined for the UI thread handler. |
Angus Kong | 1587b04 | 2014-01-02 18:09:27 -0800 | [diff] [blame] | 120 | private static final int MSG_FIRST_TIME_INIT = 1; |
| 121 | private static final int MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE = 2; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 122 | |
| 123 | // The subset of parameters we need to update in setCameraParameters(). |
| 124 | private static final int UPDATE_PARAM_INITIALIZE = 1; |
| 125 | private static final int UPDATE_PARAM_ZOOM = 2; |
| 126 | private static final int UPDATE_PARAM_PREFERENCE = 4; |
| 127 | private static final int UPDATE_PARAM_ALL = -1; |
| 128 | |
Andy Huibers | def975d | 2013-11-22 09:13:39 -0800 | [diff] [blame] | 129 | // This is the delay before we execute onResume tasks when coming |
| 130 | // from the lock screen, to allow time for onPause to execute. |
| 131 | private static final int ON_RESUME_TASKS_DELAY_MSEC = 20; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 132 | |
Ruben Brunk | d748827 | 2013-10-10 18:45:53 -0700 | [diff] [blame] | 133 | private static final String DEBUG_IMAGE_PREFIX = "DEBUG_"; |
| 134 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 135 | private CameraActivity mActivity; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 136 | private CameraProxy mCameraDevice; |
| 137 | private int mCameraId; |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 138 | private CameraCapabilities mCameraCapabilities; |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 139 | private CameraSettings mCameraSettings; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 140 | private boolean mPaused; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 141 | |
| 142 | private PhotoUI mUI; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 143 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 144 | // The activity is going to switch to the specified camera id. This is |
| 145 | // needed because texture copy is done in GL thread. -1 means camera is not |
| 146 | // switching. |
| 147 | protected int mPendingSwitchCameraId = -1; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 148 | |
| 149 | // When setCameraParametersWhenIdle() is called, we accumulate the subsets |
| 150 | // needed to be updated in mUpdateSet. |
| 151 | private int mUpdateSet; |
| 152 | |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 153 | private int mZoomValue; // The current zoom value. |
Andy Huibers | 547d7c8 | 2014-05-20 23:03:18 -0700 | [diff] [blame] | 154 | private int mTimerDuration; |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 155 | /** Set when a volume button is clicked to take photo */ |
| 156 | private boolean mVolumeButtonClickedFlag = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 157 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 158 | private boolean mFocusAreaSupported; |
| 159 | private boolean mMeteringAreaSupported; |
| 160 | private boolean mAeLockSupported; |
| 161 | private boolean mAwbLockSupported; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 162 | private boolean mContinuousFocusSupported; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 163 | |
| 164 | // The degrees of the device rotated clockwise from its natural orientation. |
| 165 | private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 166 | |
| 167 | private static final String sTempCropFilename = "crop-temp"; |
| 168 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 169 | private boolean mFaceDetectionStarted = false; |
| 170 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 171 | // mCropValue and mSaveUri are used only if isImageCaptureIntent() is true. |
| 172 | private String mCropValue; |
| 173 | private Uri mSaveUri; |
| 174 | |
Ruben Brunk | d217ed0 | 2013-10-08 23:31:13 -0700 | [diff] [blame] | 175 | private Uri mDebugUri; |
| 176 | |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 177 | // We use a queue to generated names of the images to be used later |
| 178 | // when the image is ready to be saved. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 179 | private NamedImages mNamedImages; |
| 180 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 181 | private final Runnable mDoSnapRunnable = new Runnable() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 182 | @Override |
| 183 | public void run() { |
| 184 | onShutterButtonClick(); |
| 185 | } |
| 186 | }; |
| 187 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 188 | /** |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 189 | * An unpublished intent flag requesting to return as soon as capturing is |
| 190 | * completed. TODO: consider publishing by moving into MediaStore. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 191 | */ |
| 192 | private static final String EXTRA_QUICK_CAPTURE = |
| 193 | "android.intent.extra.quickCapture"; |
| 194 | |
| 195 | // The display rotation in degrees. This is only valid when mCameraState is |
| 196 | // not PREVIEW_STOPPED. |
| 197 | private int mDisplayRotation; |
| 198 | // The value for android.hardware.Camera.setDisplayOrientation. |
| 199 | private int mCameraDisplayOrientation; |
| 200 | // The value for UI components like indicators. |
| 201 | private int mDisplayOrientation; |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 202 | // The value for cameradevice.CameraSettings.setPhotoRotationDegrees. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 203 | private int mJpegRotation; |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 204 | // Indicates whether we are using front camera |
| 205 | private boolean mMirror; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 206 | private boolean mFirstTimeInitialized; |
| 207 | private boolean mIsImageCaptureIntent; |
| 208 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 209 | private int mCameraState = PREVIEW_STOPPED; |
| 210 | private boolean mSnapshotOnIdle = false; |
| 211 | |
| 212 | private ContentResolver mContentResolver; |
| 213 | |
Doris Liu | 2b906b8 | 2013-12-10 16:34:08 -0800 | [diff] [blame] | 214 | private AppController mAppController; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 215 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 216 | private final PostViewPictureCallback mPostViewPictureCallback = |
| 217 | new PostViewPictureCallback(); |
| 218 | private final RawPictureCallback mRawPictureCallback = |
| 219 | new RawPictureCallback(); |
| 220 | private final AutoFocusCallback mAutoFocusCallback = |
| 221 | new AutoFocusCallback(); |
| 222 | private final Object mAutoFocusMoveCallback = |
| 223 | ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 224 | ? new AutoFocusMoveCallback() |
| 225 | : null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 226 | |
| 227 | private final CameraErrorCallback mErrorCallback = new CameraErrorCallback(); |
| 228 | |
| 229 | private long mFocusStartTime; |
| 230 | private long mShutterCallbackTime; |
| 231 | private long mPostViewPictureCallbackTime; |
| 232 | private long mRawPictureCallbackTime; |
| 233 | private long mJpegPictureCallbackTime; |
| 234 | private long mOnResumeTime; |
| 235 | private byte[] mJpegImageData; |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 236 | /** Touch coordinate for shutter button press. */ |
| 237 | private TouchCoordinate mShutterTouchCoordinate; |
| 238 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 239 | |
| 240 | // These latency time are for the CameraLatency test. |
| 241 | public long mAutoFocusTime; |
| 242 | public long mShutterLag; |
| 243 | public long mShutterToPictureDisplayedTime; |
| 244 | public long mPictureDisplayedToJpegCallbackTime; |
| 245 | public long mJpegCallbackFinishTime; |
| 246 | public long mCaptureStartTime; |
| 247 | |
| 248 | // This handles everything about focus. |
| 249 | private FocusOverlayManager mFocusManager; |
| 250 | |
Doris Liu | bd1b8f9 | 2014-01-03 17:59:51 -0800 | [diff] [blame] | 251 | private final int mGcamModeIndex; |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 252 | private final CountdownSoundPlayer mCountdownSoundPlayer = new CountdownSoundPlayer(); |
Doris Liu | bd1b8f9 | 2014-01-03 17:59:51 -0800 | [diff] [blame] | 253 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 254 | private CameraCapabilities.SceneMode mSceneMode; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 255 | |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 256 | private final Handler mHandler = new MainHandler(this); |
Erin Dahlgren | b09b53e | 2013-11-06 11:57:51 -0800 | [diff] [blame] | 257 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 258 | private boolean mQuickCapture; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 259 | private SensorManager mSensorManager; |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 260 | private final float[] mGData = new float[3]; |
| 261 | private final float[] mMData = new float[3]; |
| 262 | private final float[] mR = new float[16]; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 263 | private int mHeading = -1; |
| 264 | |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 265 | /** True if all the parameters needed to start preview is ready. */ |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 266 | private boolean mCameraPreviewParamsReady = false; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 267 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 268 | private final MediaSaver.OnMediaSavedListener mOnMediaSavedListener = |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 269 | new MediaSaver.OnMediaSavedListener() { |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 270 | @Override |
| 271 | public void onMediaSaved(Uri uri) { |
| 272 | if (uri != null) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 273 | mActivity.notifyNewMedia(uri); |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | }; |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 277 | private boolean mShouldResizeTo16x9 = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 278 | |
Angus Kong | 0b9eb5b | 2014-04-30 15:03:33 -0700 | [diff] [blame] | 279 | private final Runnable mResumeTaskRunnable = new Runnable() { |
| 280 | @Override |
| 281 | public void run() { |
| 282 | onResumeTasks(); |
| 283 | } |
| 284 | }; |
| 285 | |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 286 | /** |
Angus Kong | 3699c41 | 2014-05-23 15:31:09 -0700 | [diff] [blame] | 287 | * We keep the flash setting before entering scene modes (HDR) |
| 288 | * and restore it after HDR is off. |
| 289 | */ |
| 290 | private String mFlashModeBeforeSceneMode; |
| 291 | |
| 292 | /** |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 293 | * This callback gets called when user select whether or not to |
| 294 | * turn on geo-tagging. |
| 295 | */ |
| 296 | public interface LocationDialogCallback { |
| 297 | /** |
| 298 | * Gets called after user selected/unselected geo-tagging feature. |
| 299 | * |
| 300 | * @param selected whether or not geo-tagging feature is selected |
| 301 | */ |
| 302 | public void onLocationTaggingSelected(boolean selected); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * This callback defines the text that is shown in the aspect ratio selection |
| 307 | * dialog, provides the current aspect ratio, and gets notified when user changes |
| 308 | * aspect ratio selection in the dialog. |
| 309 | */ |
| 310 | public interface AspectRatioDialogCallback { |
| 311 | /** |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 312 | * Returns current aspect ratio that is being used to set as default. |
| 313 | */ |
| 314 | public AspectRatioSelector.AspectRatio getCurrentAspectRatio(); |
| 315 | |
| 316 | /** |
| 317 | * Gets notified when user has made the aspect ratio selection. |
| 318 | * |
| 319 | * @param newAspectRatio aspect ratio that user has selected |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 320 | * @param dialogHandlingFinishedRunnable runnable to run when the operations |
| 321 | * needed to handle changes from dialog |
| 322 | * are finished. |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 323 | */ |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 324 | public void onAspectRatioSelected(AspectRatioSelector.AspectRatio newAspectRatio, |
| 325 | Runnable dialogHandlingFinishedRunnable); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 328 | private void checkDisplayRotation() { |
| 329 | // Set the display orientation if display rotation has changed. |
| 330 | // Sometimes this happens when the device is held upside |
| 331 | // down and camera app is opened. Rotation animation will |
| 332 | // take some time and the rotation value we have got may be |
| 333 | // wrong. Framework does not have a callback for this now. |
| 334 | if (CameraUtil.getDisplayRotation(mActivity) != mDisplayRotation) { |
| 335 | setDisplayOrientation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 336 | } |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 337 | if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) { |
| 338 | mHandler.postDelayed(new Runnable() { |
| 339 | @Override |
| 340 | public void run() { |
| 341 | checkDisplayRotation(); |
| 342 | } |
| 343 | }, 100); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * This Handler is used to post message back onto the main thread of the |
| 349 | * application |
| 350 | */ |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 351 | private static class MainHandler extends Handler { |
| 352 | private final WeakReference<PhotoModule> mModule; |
| 353 | |
| 354 | public MainHandler(PhotoModule module) { |
Erin Dahlgren | b09b53e | 2013-11-06 11:57:51 -0800 | [diff] [blame] | 355 | super(Looper.getMainLooper()); |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 356 | mModule = new WeakReference<PhotoModule>(module); |
Erin Dahlgren | b09b53e | 2013-11-06 11:57:51 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 359 | @Override |
| 360 | public void handleMessage(Message msg) { |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 361 | PhotoModule module = mModule.get(); |
| 362 | if (module == null) { |
| 363 | return; |
| 364 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 365 | switch (msg.what) { |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 366 | case MSG_FIRST_TIME_INIT: { |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 367 | module.initializeFirstTime(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 371 | case MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE: { |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 372 | module.setCameraParametersWhenIdle(0); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 373 | break; |
| 374 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
Erin Dahlgren | 4cdaf51 | 2014-03-19 12:48:30 -0700 | [diff] [blame] | 379 | private void switchToGcamCapture() { |
| 380 | if (mActivity != null && mGcamModeIndex != 0) { |
| 381 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 382 | settingsManager.set(SettingsManager.SCOPE_GLOBAL, |
| 383 | Keys.KEY_CAMERA_HDR_PLUS, true); |
Erin Dahlgren | 4cdaf51 | 2014-03-19 12:48:30 -0700 | [diff] [blame] | 384 | |
| 385 | // Disable the HDR+ button to prevent callbacks from being |
| 386 | // queued before the correct callback is attached to the button |
| 387 | // in the new module. The new module will set the enabled/disabled |
| 388 | // of this button when the module's preferred camera becomes available. |
| 389 | ButtonManager buttonManager = mActivity.getButtonManager(); |
Erin Dahlgren | 15473d4 | 2014-05-14 14:01:31 -0700 | [diff] [blame] | 390 | buttonManager.disableButton(ButtonManager.BUTTON_HDR_PLUS); |
Erin Dahlgren | 4cdaf51 | 2014-03-19 12:48:30 -0700 | [diff] [blame] | 391 | |
Seth Raphael | 274f6e9 | 2014-05-21 17:11:53 -0700 | [diff] [blame] | 392 | mAppController.getCameraAppUI().freezeScreenUntilPreviewReady(); |
| 393 | |
Erin Dahlgren | 4cdaf51 | 2014-03-19 12:48:30 -0700 | [diff] [blame] | 394 | // Do not post this to avoid this module switch getting interleaved with |
| 395 | // other button callbacks. |
| 396 | mActivity.onModeSelected(mGcamModeIndex); |
| 397 | } |
| 398 | } |
| 399 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 400 | /** |
| 401 | * Constructs a new photo module. |
| 402 | */ |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 403 | public PhotoModule(AppController app) { |
| 404 | super(app); |
Doris Liu | bd1b8f9 | 2014-01-03 17:59:51 -0800 | [diff] [blame] | 405 | mGcamModeIndex = app.getAndroidContext().getResources() |
| 406 | .getInteger(R.integer.camera_mode_gcam); |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 407 | } |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 408 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 409 | @Override |
Spike Sprague | 159e6e9 | 2014-05-27 18:26:30 -0700 | [diff] [blame] | 410 | public String getPeekAccessibilityString() { |
| 411 | return mAppController.getAndroidContext() |
| 412 | .getResources().getString(R.string.photo_accessibility_peek); |
| 413 | } |
| 414 | |
| 415 | @Override |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 416 | public String getModuleStringIdentifier() { |
| 417 | return PHOTO_MODULE_STRING_ID; |
| 418 | } |
| 419 | |
| 420 | @Override |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 421 | public void init(CameraActivity activity, boolean isSecureCamera, boolean isCaptureIntent) { |
| 422 | mActivity = activity; |
| 423 | // TODO: Need to look at the controller interface to see if we can get |
| 424 | // rid of passing in the activity directly. |
| 425 | mAppController = mActivity; |
| 426 | |
| 427 | mUI = new PhotoUI(mActivity, this, mActivity.getModuleLayoutRoot()); |
| 428 | mActivity.setPreviewStatusListener(mUI); |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 429 | |
| 430 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 431 | mCameraId = settingsManager.getInteger(mAppController.getModuleScope(), |
| 432 | Keys.KEY_CAMERA_ID); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 433 | |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 434 | // TODO: Move this to SettingsManager as a part of upgrade procedure. |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 435 | if (!settingsManager.getBoolean(SettingsManager.SCOPE_GLOBAL, |
| 436 | Keys.KEY_USER_SELECTED_ASPECT_RATIO)) { |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 437 | // Switch to back camera to set aspect ratio. |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 438 | mCameraId = settingsManager.getIntegerDefault(Keys.KEY_CAMERA_ID); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 441 | mContentResolver = mActivity.getContentResolver(); |
| 442 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 443 | // Surface texture is from camera screen nail and startPreview needs it. |
| 444 | // This must be done before startPreview. |
| 445 | mIsImageCaptureIntent = isImageCaptureIntent(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 446 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 447 | mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false); |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 448 | mSensorManager = (SensorManager) (mActivity.getSystemService(Context.SENSOR_SERVICE)); |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 449 | mUI.setCountdownFinishedListener(this); |
| 450 | |
| 451 | // TODO: Make this a part of app controller API. |
| 452 | View cancelButton = mActivity.findViewById(R.id.shutter_cancel_button); |
| 453 | cancelButton.setOnClickListener(new View.OnClickListener() { |
| 454 | @Override |
| 455 | public void onClick(View view) { |
| 456 | cancelCountDown(); |
| 457 | } |
| 458 | }); |
| 459 | } |
| 460 | |
| 461 | private void cancelCountDown() { |
| 462 | if (mUI.isCountingDown()) { |
| 463 | // Cancel on-going countdown. |
| 464 | mUI.cancelCountDown(); |
| 465 | } |
| 466 | mAppController.getCameraAppUI().transitionToCapture(); |
| 467 | mAppController.getCameraAppUI().showModeOptions(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Erin Dahlgren | 4efa8b5 | 2013-12-17 18:31:35 -0800 | [diff] [blame] | 470 | @Override |
| 471 | public boolean isUsingBottomBar() { |
| 472 | return true; |
| 473 | } |
| 474 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 475 | private void initializeControlByIntent() { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 476 | if (mIsImageCaptureIntent) { |
Spike Sprague | 15690d0 | 2014-02-10 12:11:20 -0800 | [diff] [blame] | 477 | mActivity.getCameraAppUI().transitionToIntentCaptureLayout(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 478 | setupCaptureParams(); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | private void onPreviewStarted() { |
Doris Liu | 2b906b8 | 2013-12-10 16:34:08 -0800 | [diff] [blame] | 483 | mAppController.onPreviewStarted(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 484 | setCameraState(IDLE); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 485 | startFaceDetection(); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 486 | settingsFirstRun(); |
Seth Raphael | 3083642 | 2014-02-06 14:49:47 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 489 | /** |
| 490 | * Prompt the user to pick to record location and choose aspect ratio for the |
| 491 | * very first run of camera only. |
| 492 | */ |
| 493 | private void settingsFirstRun() { |
| 494 | final SettingsManager settingsManager = mActivity.getSettingsManager(); |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 495 | |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 496 | if (mActivity.isSecureCamera() || isImageCaptureIntent()) { |
Erin Dahlgren | 4569b70 | 2014-02-24 14:21:11 -0800 | [diff] [blame] | 497 | return; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 498 | } |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 499 | |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 500 | boolean locationPrompt = !settingsManager.isSet(SettingsManager.SCOPE_GLOBAL, |
| 501 | Keys.KEY_RECORD_LOCATION); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 502 | boolean aspectRatioPrompt = !settingsManager.getBoolean( |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 503 | SettingsManager.SCOPE_GLOBAL, Keys.KEY_USER_SELECTED_ASPECT_RATIO); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 504 | if (!locationPrompt && !aspectRatioPrompt) { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 505 | return; |
| 506 | } |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 507 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 508 | // Check if the back camera exists |
Angus Kong | d74e6a1 | 2014-01-07 11:29:44 -0800 | [diff] [blame] | 509 | int backCameraId = mAppController.getCameraProvider().getFirstBackCameraId(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 510 | if (backCameraId == -1) { |
| 511 | // If there is no back camera, do not show the prompt. |
| 512 | return; |
| 513 | } |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 514 | |
| 515 | if (locationPrompt) { |
| 516 | // Show both location and aspect ratio selection dialog. |
| 517 | mUI.showLocationAndAspectRatioDialog(new LocationDialogCallback(){ |
Sascha Haeberling | b32af4f | 2014-05-16 08:22:00 -0700 | [diff] [blame] | 518 | @Override |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 519 | public void onLocationTaggingSelected(boolean selected) { |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 520 | Keys.setLocation(mActivity.getSettingsManager(), selected, |
| 521 | mActivity.getLocationManager()); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 522 | } |
| 523 | }, createAspectRatioDialogCallback()); |
| 524 | } else { |
| 525 | // App upgrade. Only show aspect ratio selection. |
| 526 | mUI.showAspectRatioDialog(createAspectRatioDialogCallback()); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | private AspectRatioDialogCallback createAspectRatioDialogCallback() { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 531 | Size currentSize = mCameraSettings.getCurrentPhotoSize(); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 532 | float aspectRatio = (float) currentSize.width() / (float) currentSize.height(); |
| 533 | if (aspectRatio < 1f) { |
| 534 | aspectRatio = 1 / aspectRatio; |
| 535 | } |
| 536 | final AspectRatioSelector.AspectRatio currentAspectRatio; |
| 537 | if (Math.abs(aspectRatio - 4f / 3f) <= 0.1f) { |
| 538 | currentAspectRatio = AspectRatioSelector.AspectRatio.ASPECT_RATIO_4x3; |
| 539 | } else if (Math.abs(aspectRatio - 16f / 9f) <= 0.1f) { |
| 540 | currentAspectRatio = AspectRatioSelector.AspectRatio.ASPECT_RATIO_16x9; |
| 541 | } else { |
| 542 | // TODO: Log error and not show dialog. |
| 543 | return null; |
| 544 | } |
| 545 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 546 | List<Size> sizes = mCameraCapabilities.getSupportedPhotoSizes(); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 547 | List<Size> pictureSizes = ResolutionUtil |
| 548 | .getDisplayableSizesFromSupported(sizes, true); |
| 549 | |
| 550 | // This logic below finds the largest resolution for each aspect ratio. |
| 551 | // TODO: Move this somewhere that can be shared with SettingsActivity |
| 552 | int aspectRatio4x3Resolution = 0; |
| 553 | int aspectRatio16x9Resolution = 0; |
| 554 | Size largestSize4x3 = new Size(0, 0); |
| 555 | Size largestSize16x9 = new Size(0, 0); |
| 556 | for (Size size : pictureSizes) { |
| 557 | float pictureAspectRatio = (float) size.width() / (float) size.height(); |
| 558 | pictureAspectRatio = pictureAspectRatio < 1 ? |
| 559 | 1f / pictureAspectRatio : pictureAspectRatio; |
| 560 | int resolution = size.width() * size.height(); |
| 561 | if (Math.abs(pictureAspectRatio - 4f / 3f) < 0.1f) { |
| 562 | if (resolution > aspectRatio4x3Resolution) { |
| 563 | aspectRatio4x3Resolution = resolution; |
| 564 | largestSize4x3 = size; |
| 565 | } |
| 566 | } else if (Math.abs(pictureAspectRatio - 16f / 9f) < 0.1f) { |
| 567 | if (resolution > aspectRatio16x9Resolution) { |
| 568 | aspectRatio16x9Resolution = resolution; |
| 569 | largestSize16x9 = size; |
| 570 | } |
| 571 | } |
| 572 | } |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 573 | |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 574 | // Use the largest 4x3 and 16x9 sizes as candidates for picture size selection. |
| 575 | final Size size4x3ToSelect = largestSize4x3; |
| 576 | final Size size16x9ToSelect = largestSize16x9; |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 577 | |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 578 | AspectRatioDialogCallback callback = new AspectRatioDialogCallback() { |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 579 | |
| 580 | @Override |
| 581 | public AspectRatioSelector.AspectRatio getCurrentAspectRatio() { |
| 582 | return currentAspectRatio; |
| 583 | } |
| 584 | |
| 585 | @Override |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 586 | public void onAspectRatioSelected(AspectRatioSelector.AspectRatio newAspectRatio, |
| 587 | Runnable dialogHandlingFinishedRunnable) { |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 588 | if (newAspectRatio == AspectRatioSelector.AspectRatio.ASPECT_RATIO_4x3) { |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 589 | String largestSize4x3Text = SettingsUtil.sizeToSetting(size4x3ToSelect); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 590 | mActivity.getSettingsManager().set(SettingsManager.SCOPE_GLOBAL, |
| 591 | Keys.KEY_PICTURE_SIZE_BACK, |
| 592 | largestSize4x3Text); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 593 | } else if (newAspectRatio == AspectRatioSelector.AspectRatio.ASPECT_RATIO_16x9) { |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 594 | String largestSize16x9Text = SettingsUtil.sizeToSetting(size16x9ToSelect); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 595 | mActivity.getSettingsManager().set(SettingsManager.SCOPE_GLOBAL, |
| 596 | Keys.KEY_PICTURE_SIZE_BACK, |
| 597 | largestSize16x9Text); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 598 | } |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 599 | mActivity.getSettingsManager().set(SettingsManager.SCOPE_GLOBAL, |
| 600 | Keys.KEY_USER_SELECTED_ASPECT_RATIO, true); |
| 601 | String aspectRatio = mActivity.getSettingsManager().getString( |
| 602 | SettingsManager.SCOPE_GLOBAL, |
| 603 | Keys.KEY_USER_SELECTED_ASPECT_RATIO); |
| 604 | Log.e(TAG, "aspect ratio after setting it to true=" + aspectRatio); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 605 | if (newAspectRatio != currentAspectRatio) { |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 606 | stopPreview(); |
| 607 | startPreview(); |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 608 | mUI.setRunnableForNextFrame(dialogHandlingFinishedRunnable); |
| 609 | } else { |
| 610 | mHandler.post(dialogHandlingFinishedRunnable); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | }; |
| 614 | return callback; |
Doris Liu | 6a83d52 | 2013-07-02 12:03:32 -0700 | [diff] [blame] | 615 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 616 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 617 | @Override |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 618 | public void onPreviewUIReady() { |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 619 | startPreview(); |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public void onPreviewUIDestroyed() { |
| 624 | if (mCameraDevice == null) { |
| 625 | return; |
| 626 | } |
| 627 | mCameraDevice.setPreviewTexture(null); |
| 628 | stopPreview(); |
| 629 | } |
| 630 | |
Doris Liu | 1dfe782 | 2013-12-12 00:02:08 -0800 | [diff] [blame] | 631 | @Override |
| 632 | public void startPreCaptureAnimation() { |
| 633 | mAppController.startPreCaptureAnimation(); |
| 634 | } |
| 635 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 636 | private void onCameraOpened() { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 637 | openCameraCommon(); |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 638 | initializeControlByIntent(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 641 | private void switchCamera() { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 642 | if (mPaused) { |
| 643 | return; |
| 644 | } |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 645 | cancelCountDown(); |
Doris Liu | 6809eb8 | 2014-05-21 13:16:59 -0700 | [diff] [blame] | 646 | |
| 647 | mAppController.freezeScreenUntilPreviewReady(); |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 648 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 649 | |
Alan Newberger | d41766f | 2014-04-09 18:25:34 -0700 | [diff] [blame] | 650 | Log.i(TAG, "Start to switch camera. id=" + mPendingSwitchCameraId); |
Angus Kong | d4109bc | 2014-01-08 18:38:03 -0800 | [diff] [blame] | 651 | closeCamera(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 652 | mCameraId = mPendingSwitchCameraId; |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 653 | settingsManager.set(mAppController.getModuleScope(), Keys.KEY_CAMERA_ID, mCameraId); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 654 | mActivity.getCameraProvider().requestCamera(mCameraId); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 655 | mUI.clearFaces(); |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 656 | if (mFocusManager != null) { |
| 657 | mFocusManager.removeMessages(); |
| 658 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 659 | |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 660 | mMirror = isCameraFrontFacing(); |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 661 | mFocusManager.setMirror(mMirror); |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 662 | // Start switch camera animation. Post a message because |
| 663 | // onFrameAvailable from the old camera may already exist. |
Doris Liu | 48239f4 | 2013-03-04 22:19:10 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Erin Dahlgren | 0a6a8d8 | 2014-01-09 22:17:38 -0800 | [diff] [blame] | 666 | private final ButtonManager.ButtonCallback mCameraCallback = |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 667 | new ButtonManager.ButtonCallback() { |
| 668 | @Override |
| 669 | public void onStateChanged(int state) { |
Erin Dahlgren | 1ba90e3 | 2014-03-03 12:05:57 -0800 | [diff] [blame] | 670 | // At the time this callback is fired, the camera id |
| 671 | // has be set to the desired camera. |
| 672 | |
Angus Kong | 97e282a | 2014-03-04 18:44:49 -0800 | [diff] [blame] | 673 | if (mPaused || mAppController.getCameraProvider().waitingForCamera()) { |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 674 | return; |
| 675 | } |
Erin Dahlgren | 1ba90e3 | 2014-03-03 12:05:57 -0800 | [diff] [blame] | 676 | // If switching to back camera, and HDR+ is still on, |
| 677 | // switch back to gcam, otherwise handle callback normally. |
| 678 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 679 | if (Keys.isCameraBackFacing(settingsManager, |
| 680 | mAppController.getModuleScope())) { |
| 681 | if (Keys.requestsReturnToHdrPlus(settingsManager, |
| 682 | mAppController.getModuleScope())) { |
Erin Dahlgren | 4cdaf51 | 2014-03-19 12:48:30 -0700 | [diff] [blame] | 683 | switchToGcamCapture(); |
Erin Dahlgren | 1ba90e3 | 2014-03-03 12:05:57 -0800 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | } |
| 687 | |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 688 | mPendingSwitchCameraId = state; |
Erin Dahlgren | 18e2ef6 | 2013-12-05 14:53:38 -0800 | [diff] [blame] | 689 | |
Alan Newberger | d41766f | 2014-04-09 18:25:34 -0700 | [diff] [blame] | 690 | Log.d(TAG, "Start to switch camera. cameraId=" + state); |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 691 | // We need to keep a preview frame for the animation before |
| 692 | // releasing the camera. This will trigger |
| 693 | // onPreviewTextureCopied. |
| 694 | // TODO: Need to animate the camera switch |
| 695 | switchCamera(); |
| 696 | } |
| 697 | }; |
Erin Dahlgren | 18e2ef6 | 2013-12-05 14:53:38 -0800 | [diff] [blame] | 698 | |
Erin Dahlgren | 0a6a8d8 | 2014-01-09 22:17:38 -0800 | [diff] [blame] | 699 | private final ButtonManager.ButtonCallback mHdrPlusCallback = |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 700 | new ButtonManager.ButtonCallback() { |
| 701 | @Override |
| 702 | public void onStateChanged(int state) { |
Doris Liu | 8ad8ad4 | 2014-03-27 14:43:31 -0700 | [diff] [blame] | 703 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 704 | if (GcamHelper.hasGcamCapture()) { |
| 705 | // Set the camera setting to default backfacing. |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 706 | settingsManager.setToDefault(mAppController.getModuleScope(), |
| 707 | Keys.KEY_CAMERA_ID); |
Erin Dahlgren | 4cdaf51 | 2014-03-19 12:48:30 -0700 | [diff] [blame] | 708 | switchToGcamCapture(); |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 709 | } else { |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 710 | if (Keys.isHdrOn(settingsManager)) { |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 711 | settingsManager.set(mAppController.getCameraScope(), Keys.KEY_SCENE_MODE, |
| 712 | mCameraCapabilities.getStringifier().stringify( |
| 713 | CameraCapabilities.SceneMode.HDR)); |
Doris Liu | 8ad8ad4 | 2014-03-27 14:43:31 -0700 | [diff] [blame] | 714 | } else { |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 715 | settingsManager.set(mAppController.getCameraScope(), Keys.KEY_SCENE_MODE, |
| 716 | mCameraCapabilities.getStringifier().stringify( |
| 717 | CameraCapabilities.SceneMode.AUTO)); |
Doris Liu | 8ad8ad4 | 2014-03-27 14:43:31 -0700 | [diff] [blame] | 718 | } |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 719 | updateParametersSceneMode(); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 720 | mCameraDevice.applySettings(mCameraSettings); |
Doris Liu | 8ad8ad4 | 2014-03-27 14:43:31 -0700 | [diff] [blame] | 721 | updateSceneMode(); |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 722 | } |
Erin Dahlgren | ba6994d | 2013-12-12 16:04:38 -0800 | [diff] [blame] | 723 | } |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 724 | }; |
Erin Dahlgren | 18e2ef6 | 2013-12-05 14:53:38 -0800 | [diff] [blame] | 725 | |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 726 | private final View.OnClickListener mCancelCallback = new View.OnClickListener() { |
| 727 | @Override |
| 728 | public void onClick(View v) { |
| 729 | onCaptureCancelled(); |
| 730 | } |
| 731 | }; |
| 732 | |
| 733 | private final View.OnClickListener mDoneCallback = new View.OnClickListener() { |
| 734 | @Override |
| 735 | public void onClick(View v) { |
| 736 | onCaptureDone(); |
| 737 | } |
| 738 | }; |
| 739 | |
| 740 | private final View.OnClickListener mRetakeCallback = new View.OnClickListener() { |
| 741 | @Override |
| 742 | public void onClick(View v) { |
Spike Sprague | 15690d0 | 2014-02-10 12:11:20 -0800 | [diff] [blame] | 743 | mActivity.getCameraAppUI().transitionToIntentCaptureLayout(); |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 744 | onCaptureRetake(); |
| 745 | } |
| 746 | }; |
| 747 | |
Erin Dahlgren | 0a6a8d8 | 2014-01-09 22:17:38 -0800 | [diff] [blame] | 748 | @Override |
Erin Dahlgren | 1ca516f | 2014-03-28 12:44:04 -0700 | [diff] [blame] | 749 | public void hardResetSettings(SettingsManager settingsManager) { |
Erin Dahlgren | e154793 | 2014-06-23 15:17:07 -0700 | [diff] [blame] | 750 | // PhotoModule should hard reset HDR+ to off, |
| 751 | // and HDR to off if HDR+ is supported. |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 752 | settingsManager.set(SettingsManager.SCOPE_GLOBAL, Keys.KEY_CAMERA_HDR_PLUS, false); |
Erin Dahlgren | e154793 | 2014-06-23 15:17:07 -0700 | [diff] [blame] | 753 | if (GcamHelper.hasGcamCapture()) { |
Erin Dahlgren | 21a6236 | 2014-06-24 10:13:01 -0700 | [diff] [blame] | 754 | settingsManager.set(SettingsManager.SCOPE_GLOBAL, Keys.KEY_CAMERA_HDR, false); |
Erin Dahlgren | e154793 | 2014-06-23 15:17:07 -0700 | [diff] [blame] | 755 | } |
Erin Dahlgren | 1ca516f | 2014-03-28 12:44:04 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | @Override |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 759 | public HardwareSpec getHardwareSpec() { |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 760 | return (mCameraSettings != null ? |
| 761 | new HardwareSpecImpl(getCameraProvider(), mCameraCapabilities) : null); |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | @Override |
| 765 | public CameraAppUI.BottomBarUISpec getBottomBarSpec() { |
| 766 | CameraAppUI.BottomBarUISpec bottomBarSpec = new CameraAppUI.BottomBarUISpec(); |
| 767 | |
| 768 | bottomBarSpec.enableCamera = true; |
| 769 | bottomBarSpec.cameraCallback = mCameraCallback; |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 770 | bottomBarSpec.enableFlash = !mAppController.getSettingsManager() |
| 771 | .getBoolean(SettingsManager.SCOPE_GLOBAL, Keys.KEY_CAMERA_HDR); |
Erin Dahlgren | a6587a1 | 2014-02-03 13:24:55 -0800 | [diff] [blame] | 772 | bottomBarSpec.enableHdr = true; |
| 773 | bottomBarSpec.hdrCallback = mHdrPlusCallback; |
Erin Dahlgren | d5e5146 | 2014-02-07 12:38:57 -0800 | [diff] [blame] | 774 | bottomBarSpec.enableGridLines = true; |
Spike Sprague | 8f7425c | 2014-05-12 11:08:34 -0700 | [diff] [blame] | 775 | if (mCameraCapabilities != null) { |
| 776 | bottomBarSpec.enableExposureCompensation = true; |
| 777 | bottomBarSpec.exposureCompensationSetCallback = |
| 778 | new CameraAppUI.BottomBarUISpec.ExposureCompensationSetCallback() { |
| 779 | @Override |
| 780 | public void setExposure(int value) { |
| 781 | setExposureCompensation(value); |
| 782 | } |
| 783 | }; |
| 784 | bottomBarSpec.minExposureCompensation = |
| 785 | mCameraCapabilities.getMinExposureCompensation(); |
| 786 | bottomBarSpec.maxExposureCompensation = |
| 787 | mCameraCapabilities.getMaxExposureCompensation(); |
| 788 | bottomBarSpec.exposureCompensationStep = |
| 789 | mCameraCapabilities.getExposureCompensationStep(); |
| 790 | } |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 791 | |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 792 | bottomBarSpec.enableSelfTimer = true; |
Sascha Haeberling | 4333fac | 2014-05-20 16:28:11 -0700 | [diff] [blame] | 793 | bottomBarSpec.showSelfTimer = true; |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 794 | |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 795 | if (isImageCaptureIntent()) { |
| 796 | bottomBarSpec.showCancel = true; |
| 797 | bottomBarSpec.cancelCallback = mCancelCallback; |
| 798 | bottomBarSpec.showDone = true; |
| 799 | bottomBarSpec.doneCallback = mDoneCallback; |
| 800 | bottomBarSpec.showRetake = true; |
| 801 | bottomBarSpec.retakeCallback = mRetakeCallback; |
| 802 | } |
| 803 | |
| 804 | return bottomBarSpec; |
Erin Dahlgren | 0a6a8d8 | 2014-01-09 22:17:38 -0800 | [diff] [blame] | 805 | } |
| 806 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 807 | // either open a new camera or switch cameras |
| 808 | private void openCameraCommon() { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 809 | mUI.onCameraOpened(mCameraCapabilities, mCameraSettings); |
Angus Kong | 0fb819b | 2013-10-08 13:44:19 -0700 | [diff] [blame] | 810 | if (mIsImageCaptureIntent) { |
Erin Dahlgren | e419b19 | 2013-12-03 13:10:27 -0800 | [diff] [blame] | 811 | // Set hdr plus to default: off. |
| 812 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 813 | settingsManager.setToDefault(SettingsManager.SCOPE_GLOBAL, |
| 814 | Keys.KEY_CAMERA_HDR_PLUS); |
Angus Kong | 0fb819b | 2013-10-08 13:44:19 -0700 | [diff] [blame] | 815 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 816 | updateSceneMode(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 817 | } |
| 818 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 819 | @Override |
Doris Liu | 70da918 | 2013-12-17 18:41:15 -0800 | [diff] [blame] | 820 | public void updatePreviewAspectRatio(float aspectRatio) { |
| 821 | mAppController.updatePreviewAspectRatio(aspectRatio); |
| 822 | } |
| 823 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 824 | private void resetExposureCompensation() { |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 825 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
| 826 | if (settingsManager == null) { |
| 827 | Log.e(TAG, "Settings manager is null!"); |
| 828 | return; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 829 | } |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 830 | settingsManager.setToDefault(mAppController.getCameraScope(), |
| 831 | Keys.KEY_EXPOSURE); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 832 | } |
| 833 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 834 | // Snapshots can only be taken after this is called. It should be called |
| 835 | // once only. We could have done these things in onCreate() but we want to |
| 836 | // make preview screen appear as soon as possible. |
| 837 | private void initializeFirstTime() { |
Sascha Haeberling | 330dafb | 2013-10-10 19:00:41 -0700 | [diff] [blame] | 838 | if (mFirstTimeInitialized || mPaused) { |
| 839 | return; |
| 840 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 841 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 842 | mUI.initializeFirstTime(); |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 843 | |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 844 | // We set the listener only when both service and shutterbutton |
| 845 | // are initialized. |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 846 | getServices().getMemoryManager().addListener(this); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 847 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 848 | mNamedImages = new NamedImages(); |
| 849 | |
| 850 | mFirstTimeInitialized = true; |
| 851 | addIdleHandler(); |
| 852 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 853 | mActivity.updateStorageSpaceAndHint(null); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 856 | // If the activity is paused and resumed, this method will be called in |
| 857 | // onResume. |
| 858 | private void initializeSecondTime() { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 859 | getServices().getMemoryManager().addListener(this); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 860 | mNamedImages = new NamedImages(); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 861 | mUI.initializeSecondTime(mCameraCapabilities, mCameraSettings); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 864 | private void addIdleHandler() { |
| 865 | MessageQueue queue = Looper.myQueue(); |
| 866 | queue.addIdleHandler(new MessageQueue.IdleHandler() { |
| 867 | @Override |
| 868 | public boolean queueIdle() { |
| 869 | Storage.ensureOSXCompatible(); |
| 870 | return false; |
| 871 | } |
| 872 | }); |
| 873 | } |
| 874 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 875 | @Override |
| 876 | public void startFaceDetection() { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 877 | if (mFaceDetectionStarted) { |
| 878 | return; |
| 879 | } |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 880 | if (mCameraCapabilities.getMaxNumOfFacesSupported() > 0) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 881 | mFaceDetectionStarted = true; |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 882 | mUI.onStartFaceDetection(mDisplayOrientation, isCameraFrontFacing()); |
Angus Kong | 9e76552 | 2013-07-31 14:05:20 -0700 | [diff] [blame] | 883 | mCameraDevice.setFaceDetectionCallback(mHandler, mUI); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 884 | mCameraDevice.startFaceDetection(); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 885 | SessionStatsCollector.instance().faceScanActive(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 889 | @Override |
| 890 | public void stopFaceDetection() { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 891 | if (!mFaceDetectionStarted) { |
| 892 | return; |
| 893 | } |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 894 | if (mCameraCapabilities.getMaxNumOfFacesSupported() > 0) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 895 | mFaceDetectionStarted = false; |
Angus Kong | 9e76552 | 2013-07-31 14:05:20 -0700 | [diff] [blame] | 896 | mCameraDevice.setFaceDetectionCallback(null, null); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 897 | mCameraDevice.stopFaceDetection(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 898 | mUI.clearFaces(); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 899 | SessionStatsCollector.instance().faceScanActive(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 903 | private final class ShutterCallback |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 904 | implements CameraShutterCallback { |
Angus Kong | dcb0ef1 | 2013-03-25 23:11:43 -0700 | [diff] [blame] | 905 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 906 | private final boolean mNeedsAnimation; |
Angus Kong | dcb0ef1 | 2013-03-25 23:11:43 -0700 | [diff] [blame] | 907 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 908 | public ShutterCallback(boolean needsAnimation) { |
| 909 | mNeedsAnimation = needsAnimation; |
Angus Kong | dcb0ef1 | 2013-03-25 23:11:43 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 912 | @Override |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 913 | public void onShutter(CameraProxy camera) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 914 | mShutterCallbackTime = System.currentTimeMillis(); |
| 915 | mShutterLag = mShutterCallbackTime - mCaptureStartTime; |
| 916 | Log.v(TAG, "mShutterLag = " + mShutterLag + "ms"); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 917 | if (mNeedsAnimation) { |
| 918 | mActivity.runOnUiThread(new Runnable() { |
| 919 | @Override |
| 920 | public void run() { |
| 921 | animateAfterShutter(); |
| 922 | } |
| 923 | }); |
Angus Kong | dcb0ef1 | 2013-03-25 23:11:43 -0700 | [diff] [blame] | 924 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 928 | private final class PostViewPictureCallback |
| 929 | implements CameraPictureCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 930 | @Override |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 931 | public void onPictureTaken(byte[] data, CameraProxy camera) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 932 | mPostViewPictureCallbackTime = System.currentTimeMillis(); |
| 933 | Log.v(TAG, "mShutterToPostViewCallbackTime = " |
| 934 | + (mPostViewPictureCallbackTime - mShutterCallbackTime) |
| 935 | + "ms"); |
| 936 | } |
| 937 | } |
| 938 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 939 | private final class RawPictureCallback |
| 940 | implements CameraPictureCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 941 | @Override |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 942 | public void onPictureTaken(byte[] rawData, CameraProxy camera) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 943 | mRawPictureCallbackTime = System.currentTimeMillis(); |
| 944 | Log.v(TAG, "mShutterToRawCallbackTime = " |
| 945 | + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms"); |
| 946 | } |
| 947 | } |
| 948 | |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 949 | private static class ResizeBundle { |
| 950 | byte[] jpegData; |
| 951 | float targetAspectRatio; |
| 952 | ExifInterface exif; |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * @return Cropped image if the target aspect ratio is larger than the jpeg |
| 957 | * aspect ratio on the long axis. The original jpeg otherwise. |
| 958 | */ |
| 959 | private ResizeBundle cropJpegDataToAspectRatio(ResizeBundle dataBundle) { |
| 960 | |
| 961 | final byte[] jpegData = dataBundle.jpegData; |
| 962 | final ExifInterface exif = dataBundle.exif; |
| 963 | float targetAspectRatio = dataBundle.targetAspectRatio; |
| 964 | |
| 965 | Bitmap original = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length); |
| 966 | int originalWidth = original.getWidth(); |
| 967 | int originalHeight = original.getHeight(); |
| 968 | int newWidth; |
| 969 | int newHeight; |
| 970 | |
| 971 | if (originalWidth > originalHeight) { |
| 972 | newHeight = (int) (originalWidth / targetAspectRatio); |
| 973 | newWidth = originalWidth; |
| 974 | } else { |
| 975 | newWidth = (int) (originalHeight / targetAspectRatio); |
| 976 | newHeight = originalHeight; |
| 977 | } |
| 978 | int xOffset = (originalWidth - newWidth)/2; |
| 979 | int yOffset = (originalHeight - newHeight)/2; |
| 980 | |
| 981 | if (xOffset < 0 || yOffset < 0) { |
| 982 | return dataBundle; |
| 983 | } |
| 984 | |
| 985 | Bitmap resized = Bitmap.createBitmap(original,xOffset,yOffset,newWidth, newHeight); |
| 986 | exif.setTagValue(ExifInterface.TAG_PIXEL_X_DIMENSION, new Integer(newWidth)); |
| 987 | exif.setTagValue(ExifInterface.TAG_PIXEL_Y_DIMENSION, new Integer(newHeight)); |
| 988 | |
| 989 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 990 | |
| 991 | resized.compress(Bitmap.CompressFormat.JPEG, 90, stream); |
| 992 | dataBundle.jpegData = stream.toByteArray(); |
| 993 | return dataBundle; |
| 994 | } |
| 995 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 996 | private final class JpegPictureCallback |
| 997 | implements CameraPictureCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 998 | Location mLocation; |
| 999 | |
| 1000 | public JpegPictureCallback(Location loc) { |
| 1001 | mLocation = loc; |
| 1002 | } |
| 1003 | |
| 1004 | @Override |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 1005 | public void onPictureTaken(final byte[] originalJpegData, final CameraProxy camera) { |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1006 | mAppController.setShutterEnabled(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1007 | if (mPaused) { |
| 1008 | return; |
| 1009 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1010 | if (mIsImageCaptureIntent) { |
| 1011 | stopPreview(); |
| 1012 | } |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1013 | if (mSceneMode == CameraCapabilities.SceneMode.HDR) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1014 | mUI.setSwipingEnabled(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | mJpegPictureCallbackTime = System.currentTimeMillis(); |
| 1018 | // If postview callback has arrived, the captured image is displayed |
| 1019 | // in postview callback. If not, the captured image is displayed in |
| 1020 | // raw picture callback. |
| 1021 | if (mPostViewPictureCallbackTime != 0) { |
| 1022 | mShutterToPictureDisplayedTime = |
| 1023 | mPostViewPictureCallbackTime - mShutterCallbackTime; |
| 1024 | mPictureDisplayedToJpegCallbackTime = |
| 1025 | mJpegPictureCallbackTime - mPostViewPictureCallbackTime; |
| 1026 | } else { |
| 1027 | mShutterToPictureDisplayedTime = |
| 1028 | mRawPictureCallbackTime - mShutterCallbackTime; |
| 1029 | mPictureDisplayedToJpegCallbackTime = |
| 1030 | mJpegPictureCallbackTime - mRawPictureCallbackTime; |
| 1031 | } |
| 1032 | Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = " |
| 1033 | + mPictureDisplayedToJpegCallbackTime + "ms"); |
| 1034 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1035 | mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden. |
| 1036 | if (!mIsImageCaptureIntent) { |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 1037 | setupPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 1040 | long now = System.currentTimeMillis(); |
| 1041 | mJpegCallbackFinishTime = now - mJpegPictureCallbackTime; |
| 1042 | Log.v(TAG, "mJpegCallbackFinishTime = " + mJpegCallbackFinishTime + "ms"); |
| 1043 | mJpegPictureCallbackTime = 0; |
| 1044 | |
| 1045 | final ExifInterface exif = Exif.getExif(originalJpegData); |
| 1046 | |
| 1047 | if (mShouldResizeTo16x9) { |
| 1048 | final ResizeBundle dataBundle = new ResizeBundle(); |
| 1049 | dataBundle.jpegData = originalJpegData; |
| 1050 | dataBundle.targetAspectRatio = ResolutionUtil.NEXUS_5_LARGE_16_BY_9_ASPECT_RATIO; |
| 1051 | dataBundle.exif = exif; |
| 1052 | new AsyncTask<ResizeBundle, Void, ResizeBundle>() { |
| 1053 | |
| 1054 | @Override |
| 1055 | protected ResizeBundle doInBackground(ResizeBundle... resizeBundles) { |
| 1056 | return cropJpegDataToAspectRatio(resizeBundles[0]); |
| 1057 | } |
| 1058 | |
| 1059 | @Override |
| 1060 | protected void onPostExecute(ResizeBundle result) { |
| 1061 | saveFinalPhoto(result.jpegData, result.exif, camera); |
| 1062 | } |
| 1063 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataBundle); |
| 1064 | |
| 1065 | } else { |
| 1066 | saveFinalPhoto(originalJpegData, exif, camera); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | void saveFinalPhoto(final byte[] jpegData, final ExifInterface exif, CameraProxy camera) { |
| 1071 | |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 1072 | int orientation = Exif.getOrientation(exif); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1073 | |
Andy Huibers | e08bc04 | 2014-04-11 19:26:47 -0700 | [diff] [blame] | 1074 | float zoomValue = 0f; |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 1075 | if (mCameraCapabilities.supports(CameraCapabilities.Feature.ZOOM)) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1076 | int zoomIndex = mCameraSettings.getCurrentZoomIndex(); |
| 1077 | List<Integer> zoomRatios = mCameraCapabilities.getZoomRatioList(); |
Andy Huibers | e08bc04 | 2014-04-11 19:26:47 -0700 | [diff] [blame] | 1078 | if (zoomRatios != null && zoomIndex < zoomRatios.size()) { |
| 1079 | zoomValue = 0.01f * zoomRatios.get(zoomIndex); |
| 1080 | } |
| 1081 | } |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 1082 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1083 | boolean hdrOn = CameraCapabilities.SceneMode.HDR == mSceneMode; |
Andy Huibers | 203abe5 | 2014-05-19 13:59:01 -0700 | [diff] [blame] | 1084 | String flashSetting = |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 1085 | mActivity.getSettingsManager().getString(mAppController.getCameraScope(), |
| 1086 | Keys.KEY_FLASH_MODE); |
| 1087 | boolean gridLinesOn = Keys.areGridLinesOn(mActivity.getSettingsManager()); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1088 | UsageStatistics.instance().photoCaptureDoneEvent( |
| 1089 | eventprotos.NavigationChange.Mode.PHOTO_CAPTURE, |
| 1090 | mNamedImages.mQueue.lastElement().title + ".jpg", exif, |
Andy Huibers | 547d7c8 | 2014-05-20 23:03:18 -0700 | [diff] [blame] | 1091 | isCameraFrontFacing(), hdrOn, zoomValue, flashSetting, gridLinesOn, |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 1092 | (float) mTimerDuration, mShutterTouchCoordinate, mVolumeButtonClickedFlag); |
| 1093 | mShutterTouchCoordinate = null; |
| 1094 | mVolumeButtonClickedFlag = false; |
Ruben Brunk | d217ed0 | 2013-10-08 23:31:13 -0700 | [diff] [blame] | 1095 | |
Ruben Brunk | d748827 | 2013-10-10 18:45:53 -0700 | [diff] [blame] | 1096 | if (!mIsImageCaptureIntent) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1097 | // Calculate the width and the height of the jpeg. |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 1098 | Integer exifWidth = exif.getTagIntValue(ExifInterface.TAG_PIXEL_X_DIMENSION); |
| 1099 | Integer exifHeight = exif.getTagIntValue(ExifInterface.TAG_PIXEL_Y_DIMENSION); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1100 | int width, height; |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 1101 | if (mShouldResizeTo16x9 && exifWidth != null && exifHeight != null) { |
| 1102 | width = exifWidth; |
| 1103 | height = exifHeight; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1104 | } else { |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 1105 | Size s; |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1106 | s = mCameraSettings.getCurrentPhotoSize(); |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 1107 | if ((mJpegRotation + orientation) % 180 == 0) { |
| 1108 | width = s.width(); |
| 1109 | height = s.height(); |
| 1110 | } else { |
| 1111 | width = s.height(); |
| 1112 | height = s.width(); |
| 1113 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1114 | } |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 1115 | NamedEntity name = mNamedImages.getNextNameEntity(); |
| 1116 | String title = (name == null) ? null : name.title; |
| 1117 | long date = (name == null) ? -1 : name.date; |
Ruben Brunk | d748827 | 2013-10-10 18:45:53 -0700 | [diff] [blame] | 1118 | |
| 1119 | // Handle debug mode outputs |
| 1120 | if (mDebugUri != null) { |
| 1121 | // If using a debug uri, save jpeg there. |
| 1122 | saveToDebugUri(jpegData); |
| 1123 | |
| 1124 | // Adjust the title of the debug image shown in mediastore. |
| 1125 | if (title != null) { |
| 1126 | title = DEBUG_IMAGE_PREFIX + title; |
| 1127 | } |
| 1128 | } |
| 1129 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1130 | if (title == null) { |
| 1131 | Log.e(TAG, "Unbalanced name/data pair"); |
| 1132 | } else { |
Sascha Haeberling | f2994f0 | 2014-01-23 19:55:01 -0800 | [diff] [blame] | 1133 | if (date == -1) { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1134 | date = mCaptureStartTime; |
Sascha Haeberling | f2994f0 | 2014-01-23 19:55:01 -0800 | [diff] [blame] | 1135 | } |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1136 | if (mHeading >= 0) { |
| 1137 | // heading direction has been updated by the sensor. |
| 1138 | ExifTag directionRefTag = exif.buildTag( |
| 1139 | ExifInterface.TAG_GPS_IMG_DIRECTION_REF, |
| 1140 | ExifInterface.GpsTrackRef.MAGNETIC_DIRECTION); |
| 1141 | ExifTag directionTag = exif.buildTag( |
| 1142 | ExifInterface.TAG_GPS_IMG_DIRECTION, |
| 1143 | new Rational(mHeading, 1)); |
| 1144 | exif.setTag(directionRefTag); |
| 1145 | exif.setTag(directionTag); |
| 1146 | } |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1147 | getServices().getMediaSaver().addImage( |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 1148 | jpegData, title, date, mLocation, width, height, |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1149 | orientation, exif, mOnMediaSavedListener, mContentResolver); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1150 | } |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1151 | // Animate capture with real jpeg data instead of a preview |
| 1152 | // frame. |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 1153 | mUI.animateCapture(jpegData, orientation, mMirror); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1154 | } else { |
| 1155 | mJpegImageData = jpegData; |
| 1156 | if (!mQuickCapture) { |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 1157 | mUI.showCapturedImageForReview(jpegData, orientation, mMirror); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1158 | } else { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1159 | onCaptureDone(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
Sascha Haeberling | bca64bf | 2014-04-17 10:51:53 -0700 | [diff] [blame] | 1163 | // Send the taken photo to remote shutter listeners, if any are |
| 1164 | // registered. |
Sascha Haeberling | 3c6de14 | 2014-07-14 12:23:36 -0700 | [diff] [blame] | 1165 | getServices().getRemoteShutterListener().onPictureTaken(jpegData); |
Sascha Haeberling | f2994f0 | 2014-01-23 19:55:01 -0800 | [diff] [blame] | 1166 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1167 | // Check this in advance of each shot so we don't add to shutter |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1168 | // latency. It's true that someone else could write to the SD card |
| 1169 | // in the mean time and fill it, but that could have happened |
| 1170 | // between the shutter press and saving the JPEG too. |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1171 | mActivity.updateStorageSpaceAndHint(null); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1175 | private final class AutoFocusCallback implements CameraAFCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1176 | @Override |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1177 | public void onAutoFocus(boolean focused, CameraProxy camera) { |
| 1178 | SessionStatsCollector.instance().autofocusResult(focused); |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1179 | if (mPaused) { |
| 1180 | return; |
| 1181 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1182 | |
| 1183 | mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime; |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1184 | Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms focused = "+focused); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1185 | setCameraState(IDLE); |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1186 | mFocusManager.onAutoFocus(focused, false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 1190 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1191 | private final class AutoFocusMoveCallback |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1192 | implements CameraAFMoveCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1193 | @Override |
| 1194 | public void onAutoFocusMoving( |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1195 | boolean moving, CameraProxy camera) { |
| 1196 | mFocusManager.onAutoFocusMoving(moving); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1197 | SessionStatsCollector.instance().autofocusMoving(moving); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 1201 | /** |
| 1202 | * This class is just a thread-safe queue for name,date holder objects. |
| 1203 | */ |
| 1204 | public static class NamedImages { |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1205 | private final Vector<NamedEntity> mQueue; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1206 | |
| 1207 | public NamedImages() { |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 1208 | mQueue = new Vector<NamedEntity>(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 1211 | public void nameNewImage(long date) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1212 | NamedEntity r = new NamedEntity(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1213 | r.title = CameraUtil.createJpegName(date); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1214 | r.date = date; |
| 1215 | mQueue.add(r); |
| 1216 | } |
| 1217 | |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 1218 | public NamedEntity getNextNameEntity() { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1219 | synchronized (mQueue) { |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 1220 | if (!mQueue.isEmpty()) { |
| 1221 | return mQueue.remove(0); |
| 1222 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1223 | } |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 1224 | return null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 1227 | public static class NamedEntity { |
| 1228 | public String title; |
| 1229 | public long date; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | private void setCameraState(int state) { |
| 1234 | mCameraState = state; |
| 1235 | switch (state) { |
Angus Kong | 62753ae | 2014-02-10 10:53:54 -0800 | [diff] [blame] | 1236 | case PREVIEW_STOPPED: |
| 1237 | case SNAPSHOT_IN_PROGRESS: |
| 1238 | case SWITCHING_CAMERA: |
Doris Liu | f55f3c4 | 2013-11-20 00:24:46 -0800 | [diff] [blame] | 1239 | // TODO: Tell app UI to disable swipe |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1240 | break; |
| 1241 | case PhotoController.IDLE: |
Doris Liu | f55f3c4 | 2013-11-20 00:24:46 -0800 | [diff] [blame] | 1242 | // TODO: Tell app UI to enable swipe |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1243 | break; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1247 | private void animateAfterShutter() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1248 | // Only animate when in full screen capture mode |
| 1249 | // i.e. If monkey/a user swipes to the gallery during picture taking, |
| 1250 | // don't show animation |
Doris Liu | c2e9abd | 2013-06-19 14:20:51 -0700 | [diff] [blame] | 1251 | if (!mIsImageCaptureIntent) { |
| 1252 | mUI.animateFlash(); |
Doris Liu | c2e9abd | 2013-06-19 14:20:51 -0700 | [diff] [blame] | 1253 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | @Override |
| 1257 | public boolean capture() { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1258 | // If we are already in the middle of taking a snapshot or the image |
| 1259 | // save request is full then ignore. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1260 | if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1261 | || mCameraState == SWITCHING_CAMERA || !mAppController.isShutterEnabled()) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1262 | return false; |
| 1263 | } |
| 1264 | mCaptureStartTime = System.currentTimeMillis(); |
Sascha Haeberling | bca64bf | 2014-04-17 10:51:53 -0700 | [diff] [blame] | 1265 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1266 | mPostViewPictureCallbackTime = 0; |
| 1267 | mJpegImageData = null; |
| 1268 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1269 | final boolean animateBefore = (mSceneMode == CameraCapabilities.SceneMode.HDR); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1270 | |
| 1271 | if (animateBefore) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1272 | animateAfterShutter(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | // Set rotation and gps data. |
Doris Liu | 3cf565c | 2013-02-15 10:55:37 -0800 | [diff] [blame] | 1276 | int orientation; |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1277 | |
Doris Liu | 3cf565c | 2013-02-15 10:55:37 -0800 | [diff] [blame] | 1278 | if (mActivity.isAutoRotateScreen()) { |
Sol Boucher | 73ec985 | 2014-07-24 23:59:21 -0700 | [diff] [blame^] | 1279 | orientation = mDisplayRotation; |
Doris Liu | 3cf565c | 2013-02-15 10:55:37 -0800 | [diff] [blame] | 1280 | } else { |
| 1281 | orientation = mOrientation; |
| 1282 | } |
Sol Boucher | 43e1813 | 2014-06-19 15:03:18 -0700 | [diff] [blame] | 1283 | Characteristics info = |
| 1284 | mActivity.getCameraProvider().getCharacteristics(mCameraId); |
Sol Boucher | 73ec985 | 2014-07-24 23:59:21 -0700 | [diff] [blame^] | 1285 | mJpegRotation = info.getJpegOrientation(orientation); |
Erin Dahlgren | c120b0f | 2013-11-19 10:53:49 -0800 | [diff] [blame] | 1286 | Location loc = mActivity.getLocationManager().getCurrentLocation(); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1287 | CameraUtil.setGpsParameters(mCameraSettings, loc); |
| 1288 | mCameraDevice.applySettings(mCameraSettings); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1289 | |
Sascha Haeberling | 8890194 | 2013-08-28 17:49:00 -0700 | [diff] [blame] | 1290 | // We don't want user to press the button again while taking a |
| 1291 | // multi-second HDR photo. |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1292 | mAppController.setShutterEnabled(false); |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1293 | mCameraDevice.takePicture(mHandler, |
| 1294 | new ShutterCallback(!animateBefore), |
Angus Kong | dcb0ef1 | 2013-03-25 23:11:43 -0700 | [diff] [blame] | 1295 | mRawPictureCallback, mPostViewPictureCallback, |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1296 | new JpegPictureCallback(loc)); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1297 | |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 1298 | mNamedImages.nameNewImage(mCaptureStartTime); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1299 | |
| 1300 | mFaceDetectionStarted = false; |
| 1301 | setCameraState(SNAPSHOT_IN_PROGRESS); |
| 1302 | return true; |
| 1303 | } |
| 1304 | |
| 1305 | @Override |
| 1306 | public void setFocusParameters() { |
| 1307 | setCameraParameters(UPDATE_PARAM_PREFERENCE); |
| 1308 | } |
| 1309 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1310 | private void updateSceneMode() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1311 | // If scene mode is set, we cannot set flash mode, white balance, and |
| 1312 | // focus mode, instead, we read it from driver |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1313 | if (CameraCapabilities.SceneMode.AUTO != mSceneMode) { |
| 1314 | overrideCameraSettings(mCameraSettings.getCurrentFlashMode(), |
| 1315 | mCameraSettings.getCurrentFocusMode()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1319 | private void overrideCameraSettings(CameraCapabilities.FlashMode flashMode, |
| 1320 | CameraCapabilities.FocusMode focusMode) { |
| 1321 | CameraCapabilities.Stringifier stringifier = mCameraCapabilities.getStringifier(); |
Erin Dahlgren | e419b19 | 2013-12-03 13:10:27 -0800 | [diff] [blame] | 1322 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 1323 | settingsManager.set(mAppController.getCameraScope(), Keys.KEY_FLASH_MODE, |
| 1324 | stringifier.stringify(flashMode)); |
| 1325 | settingsManager.set(mAppController.getCameraScope(), Keys.KEY_FOCUS_MODE, |
| 1326 | stringifier.stringify(focusMode)); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1330 | public void onOrientationChanged(int orientation) { |
| 1331 | // We keep the last known orientation. So if the user first orient |
| 1332 | // the camera then point the camera to floor or sky, we still have |
| 1333 | // the correct orientation. |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1334 | if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) { |
| 1335 | return; |
| 1336 | } |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1337 | mOrientation = CameraUtil.roundOrientation(orientation, mOrientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | @Override |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1341 | public void onCameraAvailable(CameraProxy cameraProxy) { |
| 1342 | if (mPaused) { |
| 1343 | return; |
| 1344 | } |
| 1345 | mCameraDevice = cameraProxy; |
| 1346 | |
| 1347 | initializeCapabilities(); |
| 1348 | |
| 1349 | // Reset zoom value index. |
| 1350 | mZoomValue = 0; |
| 1351 | if (mFocusManager == null) { |
| 1352 | initializeFocusManager(); |
| 1353 | } |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1354 | mFocusManager.updateCapabilities(mCameraCapabilities); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1355 | |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 1356 | // Do camera parameter dependent initialization. |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1357 | mCameraSettings = mCameraDevice.getSettings(); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1358 | setCameraParameters(UPDATE_PARAM_ALL); |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 1359 | // Set a listener which updates camera parameters based |
| 1360 | // on changed settings. |
| 1361 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 1648c36 | 2014-01-06 15:06:04 -0800 | [diff] [blame] | 1362 | settingsManager.addListener(this); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1363 | mCameraPreviewParamsReady = true; |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 1364 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1365 | startPreview(); |
| 1366 | |
| 1367 | onCameraOpened(); |
| 1368 | } |
| 1369 | |
| 1370 | @Override |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1371 | public void onCaptureCancelled() { |
| 1372 | mActivity.setResultEx(Activity.RESULT_CANCELED, new Intent()); |
| 1373 | mActivity.finish(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1374 | } |
| 1375 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1376 | @Override |
| 1377 | public void onCaptureRetake() { |
Sascha Haeberling | f2994f0 | 2014-01-23 19:55:01 -0800 | [diff] [blame] | 1378 | if (mPaused) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1379 | return; |
Sascha Haeberling | 3b0ab89 | 2014-01-29 20:54:39 +0100 | [diff] [blame] | 1380 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1381 | mUI.hidePostCaptureAlert(); |
Spike Sprague | b4a2222 | 2014-05-22 14:40:53 -0700 | [diff] [blame] | 1382 | mUI.hideIntentReviewImageView(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1383 | setupPreview(); |
| 1384 | } |
| 1385 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1386 | @Override |
| 1387 | public void onCaptureDone() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1388 | if (mPaused) { |
| 1389 | return; |
| 1390 | } |
| 1391 | |
| 1392 | byte[] data = mJpegImageData; |
| 1393 | |
| 1394 | if (mCropValue == null) { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1395 | // First handle the no crop case -- just return the value. If the |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1396 | // caller specifies a "save uri" then write the data to its |
| 1397 | // stream. Otherwise, pass back a scaled down version of the bitmap |
| 1398 | // directly in the extras. |
| 1399 | if (mSaveUri != null) { |
| 1400 | OutputStream outputStream = null; |
| 1401 | try { |
| 1402 | outputStream = mContentResolver.openOutputStream(mSaveUri); |
| 1403 | outputStream.write(data); |
| 1404 | outputStream.close(); |
| 1405 | |
| 1406 | mActivity.setResultEx(Activity.RESULT_OK); |
| 1407 | mActivity.finish(); |
| 1408 | } catch (IOException ex) { |
| 1409 | // ignore exception |
| 1410 | } finally { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1411 | CameraUtil.closeSilently(outputStream); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1412 | } |
| 1413 | } else { |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1414 | ExifInterface exif = Exif.getExif(data); |
| 1415 | int orientation = Exif.getOrientation(exif); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1416 | Bitmap bitmap = CameraUtil.makeBitmap(data, 50 * 1024); |
| 1417 | bitmap = CameraUtil.rotate(bitmap, orientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1418 | mActivity.setResultEx(Activity.RESULT_OK, |
| 1419 | new Intent("inline-data").putExtra("data", bitmap)); |
| 1420 | mActivity.finish(); |
| 1421 | } |
| 1422 | } else { |
| 1423 | // Save the image to a temp file and invoke the cropper |
| 1424 | Uri tempUri = null; |
| 1425 | FileOutputStream tempStream = null; |
| 1426 | try { |
| 1427 | File path = mActivity.getFileStreamPath(sTempCropFilename); |
| 1428 | path.delete(); |
| 1429 | tempStream = mActivity.openFileOutput(sTempCropFilename, 0); |
| 1430 | tempStream.write(data); |
| 1431 | tempStream.close(); |
| 1432 | tempUri = Uri.fromFile(path); |
| 1433 | } catch (FileNotFoundException ex) { |
| 1434 | mActivity.setResultEx(Activity.RESULT_CANCELED); |
| 1435 | mActivity.finish(); |
| 1436 | return; |
| 1437 | } catch (IOException ex) { |
| 1438 | mActivity.setResultEx(Activity.RESULT_CANCELED); |
| 1439 | mActivity.finish(); |
| 1440 | return; |
| 1441 | } finally { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1442 | CameraUtil.closeSilently(tempStream); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | Bundle newExtras = new Bundle(); |
| 1446 | if (mCropValue.equals("circle")) { |
| 1447 | newExtras.putString("circleCrop", "true"); |
| 1448 | } |
| 1449 | if (mSaveUri != null) { |
| 1450 | newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri); |
| 1451 | } else { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1452 | newExtras.putBoolean(CameraUtil.KEY_RETURN_DATA, true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1453 | } |
| 1454 | if (mActivity.isSecureCamera()) { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1455 | newExtras.putBoolean(CameraUtil.KEY_SHOW_WHEN_LOCKED, true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1456 | } |
| 1457 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1458 | // TODO: Share this constant. |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 1459 | final String CROP_ACTION = "com.android.camera.action.CROP"; |
| 1460 | Intent cropIntent = new Intent(CROP_ACTION); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1461 | |
| 1462 | cropIntent.setData(tempUri); |
| 1463 | cropIntent.putExtras(newExtras); |
| 1464 | |
| 1465 | mActivity.startActivityForResult(cropIntent, REQUEST_CROP); |
| 1466 | } |
| 1467 | } |
| 1468 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1469 | @Override |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 1470 | public void onShutterCoordinate(TouchCoordinate coord) { |
| 1471 | mShutterTouchCoordinate = coord; |
| 1472 | } |
| 1473 | |
| 1474 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1475 | public void onShutterButtonFocus(boolean pressed) { |
Angus Kong | eaaf56d | 2014-02-20 11:59:10 -0800 | [diff] [blame] | 1476 | // Do nothing. We don't support half-press to focus anymore. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | @Override |
| 1480 | public void onShutterButtonClick() { |
Doris Liu | f9e4f8f | 2013-12-04 18:04:22 -0800 | [diff] [blame] | 1481 | if (mPaused || (mCameraState == SWITCHING_CAMERA) |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1482 | || (mCameraState == PREVIEW_STOPPED)) { |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 1483 | mVolumeButtonClickedFlag = false; |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1484 | return; |
| 1485 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1486 | |
| 1487 | // Do not take the picture if there is not enough storage. |
Angus Kong | 2dcc0a9 | 2013-09-25 13:00:08 -0700 | [diff] [blame] | 1488 | if (mActivity.getStorageSpaceBytes() <= Storage.LOW_STORAGE_THRESHOLD_BYTES) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1489 | Log.i(TAG, "Not enough space or storage not ready. remaining=" |
Angus Kong | 2dcc0a9 | 2013-09-25 13:00:08 -0700 | [diff] [blame] | 1490 | + mActivity.getStorageSpaceBytes()); |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 1491 | mVolumeButtonClickedFlag = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1492 | return; |
| 1493 | } |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 1494 | Log.d(TAG, "onShutterButtonClick: mCameraState=" + mCameraState + |
| 1495 | " mVolumeButtonClickedFlag=" + mVolumeButtonClickedFlag); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1496 | |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 1497 | int countDownDuration = mActivity.getSettingsManager() |
| 1498 | .getInteger(SettingsManager.SCOPE_GLOBAL, Keys.KEY_COUNTDOWN_DURATION); |
Andy Huibers | 547d7c8 | 2014-05-20 23:03:18 -0700 | [diff] [blame] | 1499 | mTimerDuration = countDownDuration; |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 1500 | if (countDownDuration > 0) { |
| 1501 | // Start count down. |
| 1502 | mAppController.getCameraAppUI().transitionToCancel(); |
| 1503 | mAppController.getCameraAppUI().hideModeOptions(); |
| 1504 | mUI.startCountdown(countDownDuration); |
| 1505 | return; |
| 1506 | } else { |
| 1507 | focusAndCapture(); |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | private void focusAndCapture() { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1512 | if (mSceneMode == CameraCapabilities.SceneMode.HDR) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1513 | mUI.setSwipingEnabled(false); |
Doris Liu | 9cdfe00 | 2013-04-16 09:50:56 -0700 | [diff] [blame] | 1514 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1515 | // If the user wants to do a snapshot while the previous one is still |
| 1516 | // in progress, remember the fact and do it after we finish the previous |
| 1517 | // one and re-start the preview. Snapshot in progress also includes the |
| 1518 | // state that autofocus is focusing and a picture will be taken when |
| 1519 | // focus callback arrives. |
Angus Kong | eaaf56d | 2014-02-20 11:59:10 -0800 | [diff] [blame] | 1520 | if ((mFocusManager.isFocusingSnapOnFinish() || mCameraState == SNAPSHOT_IN_PROGRESS)) { |
| 1521 | if (!mIsImageCaptureIntent) { |
| 1522 | mSnapshotOnIdle = true; |
| 1523 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1524 | return; |
| 1525 | } |
| 1526 | |
Doris Liu | f9e4f8f | 2013-12-04 18:04:22 -0800 | [diff] [blame] | 1527 | mSnapshotOnIdle = false; |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1528 | mFocusManager.focusAndCapture(mCameraSettings.getCurrentFocusMode()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1529 | } |
| 1530 | |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 1531 | @Override |
| 1532 | public void onRemainingSecondsChanged(int remainingSeconds) { |
| 1533 | mCountdownSoundPlayer.onRemainingSecondsChanged(remainingSeconds); |
| 1534 | } |
| 1535 | |
| 1536 | @Override |
| 1537 | public void onCountDownFinished() { |
Spike Sprague | 9f3d01d | 2014-06-30 14:52:57 -0700 | [diff] [blame] | 1538 | if (mIsImageCaptureIntent) { |
| 1539 | mAppController.getCameraAppUI().transitionToIntentReviewLayout(); |
| 1540 | } else { |
| 1541 | mAppController.getCameraAppUI().transitionToCapture(); |
| 1542 | } |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 1543 | mAppController.getCameraAppUI().showModeOptions(); |
| 1544 | if (mPaused) { |
| 1545 | return; |
| 1546 | } |
| 1547 | focusAndCapture(); |
| 1548 | } |
| 1549 | |
Andy Huibers | def975d | 2013-11-22 09:13:39 -0800 | [diff] [blame] | 1550 | private void onResumeTasks() { |
Angus Kong | 0b9eb5b | 2014-04-30 15:03:33 -0700 | [diff] [blame] | 1551 | if (mPaused) { |
| 1552 | return; |
| 1553 | } |
Andy Huibers | def975d | 2013-11-22 09:13:39 -0800 | [diff] [blame] | 1554 | Log.v(TAG, "Executing onResumeTasks."); |
Sascha Haeberling | dcea763 | 2014-07-17 14:06:02 -0700 | [diff] [blame] | 1555 | |
| 1556 | mCountdownSoundPlayer.loadSounds(); |
| 1557 | if (mFocusManager != null) { |
| 1558 | // If camera is not open when resume is called, focus manager will |
| 1559 | // not be initialized yet, in which case it will start listening to |
| 1560 | // preview area size change later in the initialization. |
| 1561 | mAppController.addPreviewAreaSizeChangedListener(mFocusManager); |
| 1562 | } |
| 1563 | mAppController.addPreviewAreaSizeChangedListener(mUI); |
| 1564 | |
Angus Kong | 0b9eb5b | 2014-04-30 15:03:33 -0700 | [diff] [blame] | 1565 | CameraProvider camProvider = mActivity.getCameraProvider(); |
| 1566 | if (camProvider == null) { |
| 1567 | // No camera provider, the Activity is destroyed already. |
| 1568 | return; |
| 1569 | } |
| 1570 | camProvider.requestCamera(mCameraId); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1571 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1572 | mJpegPictureCallbackTime = 0; |
| 1573 | mZoomValue = 0; |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1574 | |
| 1575 | mOnResumeTime = SystemClock.uptimeMillis(); |
| 1576 | checkDisplayRotation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1577 | |
| 1578 | // If first time initialization is not finished, put it in the |
| 1579 | // message queue. |
| 1580 | if (!mFirstTimeInitialized) { |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 1581 | mHandler.sendEmptyMessage(MSG_FIRST_TIME_INIT); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1582 | } else { |
| 1583 | initializeSecondTime(); |
| 1584 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1585 | |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1586 | Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); |
| 1587 | if (gsensor != null) { |
| 1588 | mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL); |
| 1589 | } |
| 1590 | |
| 1591 | Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); |
| 1592 | if (msensor != null) { |
| 1593 | mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_NORMAL); |
| 1594 | } |
Sascha Haeberling | dcea763 | 2014-07-17 14:06:02 -0700 | [diff] [blame] | 1595 | |
| 1596 | getServices().getRemoteShutterListener().onModuleReady(this); |
| 1597 | SessionStatsCollector.instance().sessionActive(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1600 | /** |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 1601 | * @return Whether the currently active camera is front-facing. |
| 1602 | */ |
| 1603 | private boolean isCameraFrontFacing() { |
Sol Boucher | 43e1813 | 2014-06-19 15:03:18 -0700 | [diff] [blame] | 1604 | return mAppController.getCameraProvider().getCharacteristics(mCameraId) |
| 1605 | .isFacingFront(); |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /** |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 1609 | * The focus manager is the first UI related element to get initialized, and |
| 1610 | * it requires the RenderOverlay, so initialize it here |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1611 | */ |
| 1612 | private void initializeFocusManager() { |
| 1613 | // Create FocusManager object. startPreview needs it. |
| 1614 | // if mFocusManager not null, reuse it |
| 1615 | // otherwise create a new instance |
| 1616 | if (mFocusManager != null) { |
| 1617 | mFocusManager.removeMessages(); |
| 1618 | } else { |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 1619 | mMirror = isCameraFrontFacing(); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1620 | String[] defaultFocusModesStrings = mActivity.getResources().getStringArray( |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1621 | R.array.pref_camera_focusmode_default_array); |
Sascha Haeberling | ee36c5f | 2014-07-29 17:05:43 -0700 | [diff] [blame] | 1622 | ArrayList<CameraCapabilities.FocusMode> defaultFocusModes = |
| 1623 | new ArrayList<CameraCapabilities.FocusMode>(); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1624 | CameraCapabilities.Stringifier stringifier = mCameraCapabilities.getStringifier(); |
| 1625 | for (String modeString : defaultFocusModesStrings) { |
| 1626 | CameraCapabilities.FocusMode mode = stringifier.focusModeFromString(modeString); |
| 1627 | if (mode != null) { |
| 1628 | defaultFocusModes.add(mode); |
| 1629 | } |
| 1630 | } |
| 1631 | mFocusManager = |
| 1632 | new FocusOverlayManager(mAppController, defaultFocusModes, |
| 1633 | mCameraCapabilities, this, mMirror, mActivity.getMainLooper(), |
| 1634 | mUI.getFocusUI()); |
Kevin Gabayan | fb33336 | 2014-06-02 14:48:20 -0700 | [diff] [blame] | 1635 | MotionManager motionManager = getServices().getMotionManager(); |
| 1636 | if (motionManager != null) { |
| 1637 | motionManager.addListener(mFocusManager); |
| 1638 | } |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1639 | } |
Doris Liu | 482de02 | 2013-12-18 19:18:16 -0800 | [diff] [blame] | 1640 | mAppController.addPreviewAreaSizeChangedListener(mFocusManager); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1641 | } |
| 1642 | |
Sascha Haeberling | dcea763 | 2014-07-17 14:06:02 -0700 | [diff] [blame] | 1643 | /** |
| 1644 | * @return Whether we are resuming from within the lockscreen. |
| 1645 | */ |
| 1646 | private boolean isResumeFromLockscreen() { |
| 1647 | String action = mActivity.getIntent().getAction(); |
| 1648 | return (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(action) |
| 1649 | || MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action)); |
| 1650 | } |
| 1651 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1652 | @Override |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1653 | public void resume() { |
| 1654 | mPaused = false; |
Doris Liu | 5940104 | 2014-01-14 17:51:32 -0800 | [diff] [blame] | 1655 | |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1656 | // Add delay on resume from lock screen only, in order to to speed up |
| 1657 | // the onResume --> onPause --> onResume cycle from lock screen. |
| 1658 | // Don't do always because letting go of thread can cause delay. |
Sascha Haeberling | dcea763 | 2014-07-17 14:06:02 -0700 | [diff] [blame] | 1659 | if (isResumeFromLockscreen()) { |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1660 | Log.v(TAG, "On resume, from lock screen."); |
| 1661 | // Note: onPauseAfterSuper() will delete this runnable, so we will |
| 1662 | // at most have 1 copy queued up. |
Angus Kong | 0b9eb5b | 2014-04-30 15:03:33 -0700 | [diff] [blame] | 1663 | mHandler.postDelayed(mResumeTaskRunnable, ON_RESUME_TASKS_DELAY_MSEC); |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1664 | } else { |
| 1665 | Log.v(TAG, "On resume."); |
| 1666 | onResumeTasks(); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | @Override |
| 1671 | public void pause() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1672 | mPaused = true; |
Angus Kong | 0b9eb5b | 2014-04-30 15:03:33 -0700 | [diff] [blame] | 1673 | mHandler.removeCallbacks(mResumeTaskRunnable); |
Sascha Haeberling | 1bf0889 | 2014-06-18 09:38:06 -0700 | [diff] [blame] | 1674 | getServices().getRemoteShutterListener().onModuleExit(); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1675 | SessionStatsCollector.instance().sessionActive(false); |
Spike Sprague | abf54e2 | 2014-03-27 15:41:28 -0700 | [diff] [blame] | 1676 | |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1677 | Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); |
| 1678 | if (gsensor != null) { |
| 1679 | mSensorManager.unregisterListener(this, gsensor); |
| 1680 | } |
| 1681 | |
| 1682 | Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); |
| 1683 | if (msensor != null) { |
| 1684 | mSensorManager.unregisterListener(this, msensor); |
| 1685 | } |
Andy Huibers | def975d | 2013-11-22 09:13:39 -0800 | [diff] [blame] | 1686 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1687 | // Reset the focus first. Camera CTS does not guarantee that |
| 1688 | // cancelAutoFocus is allowed after preview stops. |
| 1689 | if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) { |
| 1690 | mCameraDevice.cancelAutoFocus(); |
| 1691 | } |
Andy Huibers | def975d | 2013-11-22 09:13:39 -0800 | [diff] [blame] | 1692 | |
Erin Dahlgren | b09b53e | 2013-11-06 11:57:51 -0800 | [diff] [blame] | 1693 | // If the camera has not been opened asynchronously yet, |
| 1694 | // and startPreview hasn't been called, then this is a no-op. |
| 1695 | // (e.g. onResume -> onPause -> onResume). |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1696 | stopPreview(); |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 1697 | cancelCountDown(); |
| 1698 | mCountdownSoundPlayer.release(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1699 | |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 1700 | mNamedImages = null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1701 | // If we are in an image capture intent and has taken |
| 1702 | // a picture, we just clear it in onPause. |
| 1703 | mJpegImageData = null; |
| 1704 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1705 | // Remove the messages and runnables in the queue. |
| 1706 | mHandler.removeCallbacksAndMessages(null); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1707 | |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 1708 | closeCamera(); |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 1709 | mActivity.enableKeepScreenOn(false); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1710 | mUI.onPause(); |
| 1711 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1712 | mPendingSwitchCameraId = -1; |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1713 | if (mFocusManager != null) { |
| 1714 | mFocusManager.removeMessages(); |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 1715 | } |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1716 | getServices().getMemoryManager().removeListener(this); |
Doris Liu | 482de02 | 2013-12-18 19:18:16 -0800 | [diff] [blame] | 1717 | mAppController.removePreviewAreaSizeChangedListener(mFocusManager); |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 1718 | mAppController.removePreviewAreaSizeChangedListener(mUI); |
Erin Dahlgren | 1648c36 | 2014-01-06 15:06:04 -0800 | [diff] [blame] | 1719 | |
| 1720 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
| 1721 | settingsManager.removeListener(this); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1722 | } |
| 1723 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1724 | @Override |
| 1725 | public void destroy() { |
| 1726 | // TODO: implement this. |
| 1727 | } |
| 1728 | |
| 1729 | @Override |
Angus Kong | 2f0e4a3 | 2013-12-03 10:02:35 -0800 | [diff] [blame] | 1730 | public void onLayoutOrientationChanged(boolean isLandscape) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1731 | setDisplayOrientation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | @Override |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1735 | public void updateCameraOrientation() { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1736 | if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1737 | setDisplayOrientation(); |
| 1738 | } |
| 1739 | } |
| 1740 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1741 | private boolean canTakePicture() { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1742 | return isCameraIdle() |
| 1743 | && (mActivity.getStorageSpaceBytes() > Storage.LOW_STORAGE_THRESHOLD_BYTES); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | @Override |
| 1747 | public void autoFocus() { |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1748 | Log.v(TAG,"Starting auto focus"); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1749 | mFocusStartTime = System.currentTimeMillis(); |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1750 | mCameraDevice.autoFocus(mHandler, mAutoFocusCallback); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1751 | SessionStatsCollector.instance().autofocusManualTrigger(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1752 | setCameraState(FOCUSING); |
| 1753 | } |
| 1754 | |
| 1755 | @Override |
| 1756 | public void cancelAutoFocus() { |
| 1757 | mCameraDevice.cancelAutoFocus(); |
| 1758 | setCameraState(IDLE); |
| 1759 | setCameraParameters(UPDATE_PARAM_PREFERENCE); |
| 1760 | } |
| 1761 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1762 | @Override |
| 1763 | public void onSingleTapUp(View view, int x, int y) { |
Doris Liu | 482de02 | 2013-12-18 19:18:16 -0800 | [diff] [blame] | 1764 | if (mPaused || mCameraDevice == null || !mFirstTimeInitialized |
| 1765 | || mCameraState == SNAPSHOT_IN_PROGRESS |
| 1766 | || mCameraState == SWITCHING_CAMERA |
| 1767 | || mCameraState == PREVIEW_STOPPED) { |
| 1768 | return; |
| 1769 | } |
| 1770 | |
| 1771 | // Check if metering area or focus area is supported. |
Doris Liu | 15b9961 | 2013-12-21 11:32:28 -0800 | [diff] [blame] | 1772 | if (!mFocusAreaSupported && !mMeteringAreaSupported) { |
| 1773 | return; |
| 1774 | } |
Doris Liu | 482de02 | 2013-12-18 19:18:16 -0800 | [diff] [blame] | 1775 | mFocusManager.onSingleTapUp(x, y); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | @Override |
| 1779 | public boolean onBackPressed() { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1780 | return mUI.onBackPressed(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | @Override |
| 1784 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 1785 | switch (keyCode) { |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1786 | case KeyEvent.KEYCODE_VOLUME_UP: |
| 1787 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
| 1788 | case KeyEvent.KEYCODE_FOCUS: |
Spike Sprague | 9f3d01d | 2014-06-30 14:52:57 -0700 | [diff] [blame] | 1789 | if (/* TODO: mActivity.isInCameraApp() && */mFirstTimeInitialized && |
| 1790 | !mActivity.getCameraAppUI().isInIntentReview()) { |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1791 | if (event.getRepeatCount() == 0) { |
| 1792 | onShutterButtonFocus(true); |
| 1793 | } |
| 1794 | return true; |
| 1795 | } |
| 1796 | return false; |
| 1797 | case KeyEvent.KEYCODE_CAMERA: |
| 1798 | if (mFirstTimeInitialized && event.getRepeatCount() == 0) { |
| 1799 | onShutterButtonClick(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1800 | } |
| 1801 | return true; |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1802 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 1803 | // If we get a dpad center event without any focused view, move |
| 1804 | // the focus to the shutter button and press it. |
| 1805 | if (mFirstTimeInitialized && event.getRepeatCount() == 0) { |
| 1806 | // Start auto-focus immediately to reduce shutter lag. After |
| 1807 | // the shutter button gets the focus, onShutterButtonFocus() |
| 1808 | // will be called again but it is fine. |
| 1809 | onShutterButtonFocus(true); |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1810 | } |
| 1811 | return true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1812 | } |
| 1813 | return false; |
| 1814 | } |
| 1815 | |
| 1816 | @Override |
| 1817 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 1818 | switch (keyCode) { |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1819 | case KeyEvent.KEYCODE_VOLUME_UP: |
| 1820 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
Spike Sprague | 9f3d01d | 2014-06-30 14:52:57 -0700 | [diff] [blame] | 1821 | if (/* mActivity.isInCameraApp() && */mFirstTimeInitialized && |
| 1822 | !mActivity.getCameraAppUI().isInIntentReview()) { |
| 1823 | if (mUI.isCountingDown()) { |
| 1824 | cancelCountDown(); |
| 1825 | } else { |
| 1826 | mVolumeButtonClickedFlag = true; |
| 1827 | onShutterButtonClick(); |
| 1828 | } |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1829 | return true; |
| 1830 | } |
| 1831 | return false; |
| 1832 | case KeyEvent.KEYCODE_FOCUS: |
| 1833 | if (mFirstTimeInitialized) { |
| 1834 | onShutterButtonFocus(false); |
| 1835 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1836 | return true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1837 | } |
| 1838 | return false; |
| 1839 | } |
| 1840 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1841 | private void closeCamera() { |
| 1842 | if (mCameraDevice != null) { |
Angus Kong | 97e282a | 2014-03-04 18:44:49 -0800 | [diff] [blame] | 1843 | stopFaceDetection(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1844 | mCameraDevice.setZoomChangeListener(null); |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 1845 | mCameraDevice.setFaceDetectionCallback(null, null); |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame] | 1846 | mCameraDevice.setErrorCallback(null, null); |
Ruben Brunk | 5914783 | 2013-11-05 15:53:28 -0800 | [diff] [blame] | 1847 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1848 | mFaceDetectionStarted = false; |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1849 | mActivity.getCameraProvider().releaseCamera(mCameraDevice.getCameraId()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1850 | mCameraDevice = null; |
| 1851 | setCameraState(PREVIEW_STOPPED); |
| 1852 | mFocusManager.onCameraReleased(); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | private void setDisplayOrientation() { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1857 | mDisplayRotation = CameraUtil.getDisplayRotation(mActivity); |
Sol Boucher | 43e1813 | 2014-06-19 15:03:18 -0700 | [diff] [blame] | 1858 | Characteristics info = |
| 1859 | mActivity.getCameraProvider().getCharacteristics(mCameraId); |
Sol Boucher | 73ec985 | 2014-07-24 23:59:21 -0700 | [diff] [blame^] | 1860 | mDisplayOrientation = info.getPreviewOrientation(mDisplayRotation); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1861 | mCameraDisplayOrientation = mDisplayOrientation; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1862 | mUI.setDisplayOrientation(mDisplayOrientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1863 | if (mFocusManager != null) { |
| 1864 | mFocusManager.setDisplayOrientation(mDisplayOrientation); |
| 1865 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1866 | // Change the camera display orientation |
| 1867 | if (mCameraDevice != null) { |
Sol Boucher | 73ec985 | 2014-07-24 23:59:21 -0700 | [diff] [blame^] | 1868 | mCameraDevice.setDisplayOrientation(mDisplayRotation); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1869 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1870 | } |
| 1871 | |
Sascha Haeberling | ddef779 | 2013-08-13 14:41:10 -0700 | [diff] [blame] | 1872 | /** Only called by UI thread. */ |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1873 | private void setupPreview() { |
| 1874 | mFocusManager.resetTouchFocus(); |
| 1875 | startPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1876 | } |
| 1877 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1878 | /** |
| 1879 | * Returns whether we can/should start the preview or not. |
| 1880 | */ |
| 1881 | private boolean checkPreviewPreconditions() { |
| 1882 | if (mPaused) { |
| 1883 | return false; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1884 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1885 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1886 | if (mCameraDevice == null) { |
| 1887 | Log.w(TAG, "startPreview: camera device not ready yet."); |
| 1888 | return false; |
| 1889 | } |
| 1890 | |
Erin Dahlgren | d8de077 | 2014-02-03 10:12:27 -0800 | [diff] [blame] | 1891 | SurfaceTexture st = mActivity.getCameraAppUI().getSurfaceTexture(); |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 1892 | if (st == null) { |
| 1893 | Log.w(TAG, "startPreview: surfaceTexture is not ready."); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1894 | return false; |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | if (!mCameraPreviewParamsReady) { |
| 1898 | Log.w(TAG, "startPreview: parameters for preview is not ready."); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1899 | return false; |
| 1900 | } |
| 1901 | return true; |
| 1902 | } |
| 1903 | |
| 1904 | /** |
| 1905 | * The start/stop preview should only run on the UI thread. |
| 1906 | */ |
| 1907 | private void startPreview() { |
| 1908 | if (!checkPreviewPreconditions()) { |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 1909 | return; |
| 1910 | } |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1911 | |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame] | 1912 | mCameraDevice.setErrorCallback(mHandler, mErrorCallback); |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 1913 | setDisplayOrientation(); |
| 1914 | |
| 1915 | if (!mSnapshotOnIdle) { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1916 | // If the focus mode is continuous autofocus, call cancelAutoFocus |
| 1917 | // to resume it because it may have been paused by autoFocus call. |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1918 | if (mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode()) == |
| 1919 | CameraCapabilities.FocusMode.CONTINUOUS_PICTURE) { |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 1920 | mCameraDevice.cancelAutoFocus(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1921 | } |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 1922 | mFocusManager.setAeAwbLock(false); // Unlock AE and AWB. |
| 1923 | } |
| 1924 | setCameraParameters(UPDATE_PARAM_ALL); |
Erin Dahlgren | d8de077 | 2014-02-03 10:12:27 -0800 | [diff] [blame] | 1925 | mCameraDevice.setPreviewTexture(mActivity.getCameraAppUI().getSurfaceTexture()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1926 | |
Alan Newberger | d41766f | 2014-04-09 18:25:34 -0700 | [diff] [blame] | 1927 | Log.i(TAG, "startPreview"); |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 1928 | mCameraDevice.startPreview(); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1929 | |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 1930 | mFocusManager.onPreviewStarted(); |
| 1931 | onPreviewStarted(); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1932 | SessionStatsCollector.instance().previewActive(true); |
Erin Dahlgren | dc282e1 | 2013-11-12 09:39:08 -0800 | [diff] [blame] | 1933 | if (mSnapshotOnIdle) { |
| 1934 | mHandler.post(mDoSnapRunnable); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1938 | @Override |
| 1939 | public void stopPreview() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1940 | if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) { |
Alan Newberger | d41766f | 2014-04-09 18:25:34 -0700 | [diff] [blame] | 1941 | Log.i(TAG, "stopPreview"); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1942 | mCameraDevice.stopPreview(); |
| 1943 | mFaceDetectionStarted = false; |
| 1944 | } |
| 1945 | setCameraState(PREVIEW_STOPPED); |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1946 | if (mFocusManager != null) { |
| 1947 | mFocusManager.onPreviewStopped(); |
| 1948 | } |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1949 | SessionStatsCollector.instance().previewActive(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1950 | } |
| 1951 | |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 1952 | @Override |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 1953 | public void onSettingChanged(SettingsManager settingsManager, String key) { |
| 1954 | if (key.equals(Keys.KEY_FLASH_MODE)) { |
| 1955 | updateParametersFlashMode(); |
| 1956 | } |
| 1957 | if (key.equals(Keys.KEY_CAMERA_HDR)) { |
| 1958 | if (settingsManager.getBoolean(SettingsManager.SCOPE_GLOBAL, |
| 1959 | Keys.KEY_CAMERA_HDR)) { |
| 1960 | // HDR is on. |
| 1961 | mAppController.getButtonManager().disableButton(ButtonManager.BUTTON_FLASH); |
| 1962 | mFlashModeBeforeSceneMode = settingsManager.getString( |
| 1963 | mAppController.getCameraScope(), Keys.KEY_FLASH_MODE); |
| 1964 | } else { |
| 1965 | if (mFlashModeBeforeSceneMode != null) { |
| 1966 | settingsManager.set(mAppController.getCameraScope(), |
| 1967 | Keys.KEY_FLASH_MODE, |
| 1968 | mFlashModeBeforeSceneMode); |
| 1969 | updateParametersFlashMode(); |
| 1970 | mFlashModeBeforeSceneMode = null; |
Angus Kong | 8c07fe9 | 2014-05-22 14:42:45 -0700 | [diff] [blame] | 1971 | } |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 1972 | mAppController.getButtonManager().enableButton(ButtonManager.BUTTON_FLASH); |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 1973 | } |
| 1974 | } |
Erin Dahlgren | 1648c36 | 2014-01-06 15:06:04 -0800 | [diff] [blame] | 1975 | |
| 1976 | if (mCameraDevice != null) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1977 | mCameraDevice.applySettings(mCameraSettings); |
Erin Dahlgren | 1648c36 | 2014-01-06 15:06:04 -0800 | [diff] [blame] | 1978 | } |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 1979 | } |
| 1980 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1981 | private void updateCameraParametersInitialize() { |
| 1982 | // Reset preview frame rate to the maximum because it may be lowered by |
| 1983 | // video camera application. |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 1984 | int[] fpsRange = CameraUtil.getPhotoPreviewFpsRange(mCameraCapabilities); |
ztenghui | 16a3520 | 2013-09-23 11:35:36 -0700 | [diff] [blame] | 1985 | if (fpsRange != null && fpsRange.length > 0) { |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1986 | mCameraSettings.setPreviewFpsRange(fpsRange[0], fpsRange[1]); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1987 | } |
| 1988 | |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1989 | mCameraSettings.setRecordingHintEnabled(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1990 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1991 | if (mCameraCapabilities.supports(CameraCapabilities.Feature.VIDEO_STABILIZATION)) { |
| 1992 | mCameraSettings.setVideoStabilization(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | private void updateCameraParametersZoom() { |
| 1997 | // Set zoom. |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 1998 | if (mCameraCapabilities.supports(CameraCapabilities.Feature.ZOOM)) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1999 | mCameraSettings.setZoomIndex(mZoomValue); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2000 | } |
| 2001 | } |
| 2002 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 2003 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2004 | private void setAutoExposureLockIfSupported() { |
| 2005 | if (mAeLockSupported) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2006 | mCameraSettings.setAutoExposureLock(mFocusManager.getAeAwbLock()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 2010 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2011 | private void setAutoWhiteBalanceLockIfSupported() { |
| 2012 | if (mAwbLockSupported) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2013 | mCameraSettings.setAutoWhiteBalanceLock(mFocusManager.getAeAwbLock()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2014 | } |
| 2015 | } |
| 2016 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2017 | private void setFocusAreasIfSupported() { |
| 2018 | if (mFocusAreaSupported) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2019 | mCameraSettings.setFocusAreas(mFocusManager.getFocusAreas()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2020 | } |
| 2021 | } |
| 2022 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2023 | private void setMeteringAreasIfSupported() { |
| 2024 | if (mMeteringAreaSupported) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2025 | mCameraSettings.setMeteringAreas(mFocusManager.getMeteringAreas()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2026 | } |
| 2027 | } |
| 2028 | |
Erin Dahlgren | ba6994d | 2013-12-12 16:04:38 -0800 | [diff] [blame] | 2029 | private void updateCameraParametersPreference() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2030 | setAutoExposureLockIfSupported(); |
| 2031 | setAutoWhiteBalanceLockIfSupported(); |
| 2032 | setFocusAreasIfSupported(); |
| 2033 | setMeteringAreasIfSupported(); |
| 2034 | |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 2035 | // Initialize focus mode. |
Michael Kolb | d3253f2 | 2013-07-12 11:36:47 -0700 | [diff] [blame] | 2036 | mFocusManager.overrideFocusMode(null); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 2037 | mCameraSettings |
| 2038 | .setFocusMode(mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode())); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 2039 | SessionStatsCollector.instance().autofocusActive( |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 2040 | mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode()) == |
| 2041 | CameraCapabilities.FocusMode.CONTINUOUS_PICTURE |
| 2042 | ); |
Michael Kolb | d3253f2 | 2013-07-12 11:36:47 -0700 | [diff] [blame] | 2043 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2044 | // Set picture size. |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 2045 | updateParametersPictureSize(); |
| 2046 | |
| 2047 | // Set JPEG quality. |
| 2048 | updateParametersPictureQuality(); |
| 2049 | |
| 2050 | // For the following settings, we need to check if the settings are |
| 2051 | // still supported by latest driver, if not, ignore the settings. |
| 2052 | |
| 2053 | // Set exposure compensation |
| 2054 | updateParametersExposureCompensation(); |
| 2055 | |
| 2056 | // Set the scene mode: also sets flash and white balance. |
| 2057 | updateParametersSceneMode(); |
| 2058 | |
| 2059 | if (mContinuousFocusSupported && ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK) { |
| 2060 | updateAutoFocusMoveCallback(); |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | private void updateParametersPictureSize() { |
| 2065 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 2066 | String pictureSizeKey = isCameraFrontFacing() ? Keys.KEY_PICTURE_SIZE_FRONT |
| 2067 | : Keys.KEY_PICTURE_SIZE_BACK; |
| 2068 | String pictureSize = settingsManager.getString(SettingsManager.SCOPE_GLOBAL, |
| 2069 | pictureSizeKey); |
Sascha Haeberling | 3b0ab89 | 2014-01-29 20:54:39 +0100 | [diff] [blame] | 2070 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2071 | List<Size> supported = mCameraCapabilities.getSupportedPhotoSizes(); |
Andy Huibers | 90c7ad5 | 2014-05-20 22:13:54 -0700 | [diff] [blame] | 2072 | CameraPictureSizesCacher.updateSizesForCamera(mAppController.getAndroidContext(), |
| 2073 | mCameraDevice.getCameraId(), supported); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2074 | SettingsUtil.setCameraPictureSize(pictureSize, supported, mCameraSettings, |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 2075 | mCameraDevice.getCameraId()); |
Angus Kong | 454d63f | 2014-05-06 14:45:59 -0700 | [diff] [blame] | 2076 | |
| 2077 | Size size = SettingsUtil.getPhotoSize(pictureSize, supported, |
| 2078 | mCameraDevice.getCameraId()); |
| 2079 | if (ApiHelper.IS_NEXUS_5) { |
| 2080 | if (ResolutionUtil.NEXUS_5_LARGE_16_BY_9.equals(pictureSize)) { |
| 2081 | mShouldResizeTo16x9 = true; |
| 2082 | } else { |
| 2083 | mShouldResizeTo16x9 = false; |
| 2084 | } |
| 2085 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2086 | |
| 2087 | // Set a preview size that is closest to the viewfinder height and has |
| 2088 | // the right aspect ratio. |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2089 | List<Size> sizes = mCameraCapabilities.getSupportedPreviewSizes(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 2090 | Size optimalSize = CameraUtil.getOptimalPreviewSize(mActivity, sizes, |
Angus Kong | 00b7b10 | 2014-04-24 15:46:52 -0700 | [diff] [blame] | 2091 | (double) size.width() / size.height()); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2092 | Size original = mCameraSettings.getCurrentPreviewSize(); |
| 2093 | if (!optimalSize.equals(original)) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2094 | mCameraSettings.setPreviewSize(optimalSize); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 2095 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2096 | // Zoom related settings will be changed for different preview |
| 2097 | // sizes, so set and read the parameters to get latest values |
Michael Kolb | 4a4d4ef | 2013-05-30 08:35:26 -0700 | [diff] [blame] | 2098 | if (mHandler.getLooper() == Looper.myLooper()) { |
| 2099 | // On UI thread only, not when camera starts up |
| 2100 | setupPreview(); |
| 2101 | } else { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2102 | mCameraDevice.applySettings(mCameraSettings); |
Michael Kolb | 4a4d4ef | 2013-05-30 08:35:26 -0700 | [diff] [blame] | 2103 | } |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2104 | mCameraSettings = mCameraDevice.getSettings(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2105 | } |
Doris Liu | 9540574 | 2013-11-05 15:25:26 -0800 | [diff] [blame] | 2106 | |
Angus Kong | 00b7b10 | 2014-04-24 15:46:52 -0700 | [diff] [blame] | 2107 | if (optimalSize.width() != 0 && optimalSize.height() != 0) { |
| 2108 | mUI.updatePreviewAspectRatio((float) optimalSize.width() |
| 2109 | / (float) optimalSize.height()); |
Doris Liu | 9540574 | 2013-11-05 15:25:26 -0800 | [diff] [blame] | 2110 | } |
Angus Kong | 00b7b10 | 2014-04-24 15:46:52 -0700 | [diff] [blame] | 2111 | Log.i(TAG, "Preview size is " + optimalSize); |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 2112 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2113 | |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 2114 | private void updateParametersPictureQuality() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2115 | int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId, |
| 2116 | CameraProfile.QUALITY_HIGH); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2117 | mCameraSettings.setPhotoJpegCompressionQuality(jpegQuality); |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 2118 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2119 | |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 2120 | private void updateParametersExposureCompensation() { |
| 2121 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 2122 | if (settingsManager.getBoolean(SettingsManager.SCOPE_GLOBAL, |
| 2123 | Keys.KEY_EXPOSURE_COMPENSATION_ENABLED)) { |
| 2124 | int value = settingsManager.getInteger(mAppController.getCameraScope(), |
| 2125 | Keys.KEY_EXPOSURE); |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 2126 | int max = mCameraCapabilities.getMaxExposureCompensation(); |
| 2127 | int min = mCameraCapabilities.getMinExposureCompensation(); |
Spike Sprague | abf54e2 | 2014-03-27 15:41:28 -0700 | [diff] [blame] | 2128 | if (value >= min && value <= max) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2129 | mCameraSettings.setExposureCompensationIndex(value); |
Spike Sprague | abf54e2 | 2014-03-27 15:41:28 -0700 | [diff] [blame] | 2130 | } else { |
| 2131 | Log.w(TAG, "invalid exposure range: " + value); |
| 2132 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2133 | } else { |
Spike Sprague | abf54e2 | 2014-03-27 15:41:28 -0700 | [diff] [blame] | 2134 | // If exposure compensation is not enabled, reset the exposure compensation value. |
| 2135 | setExposureCompensation(0); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2136 | } |
Spike Sprague | abf54e2 | 2014-03-27 15:41:28 -0700 | [diff] [blame] | 2137 | |
Erin Dahlgren | ba6994d | 2013-12-12 16:04:38 -0800 | [diff] [blame] | 2138 | } |
| 2139 | |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 2140 | private void updateParametersSceneMode() { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2141 | CameraCapabilities.Stringifier stringifier = mCameraCapabilities.getStringifier(); |
Erin Dahlgren | ba6994d | 2013-12-12 16:04:38 -0800 | [diff] [blame] | 2142 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
| 2143 | |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 2144 | mSceneMode = stringifier. |
| 2145 | sceneModeFromString(settingsManager.getString(mAppController.getCameraScope(), |
| 2146 | Keys.KEY_SCENE_MODE)); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2147 | if (mCameraCapabilities.supports(mSceneMode)) { |
| 2148 | if (mCameraSettings.getCurrentSceneMode() != mSceneMode) { |
| 2149 | mCameraSettings.setSceneMode(mSceneMode); |
Erin Dahlgren | ba6994d | 2013-12-12 16:04:38 -0800 | [diff] [blame] | 2150 | |
| 2151 | // Setting scene mode will change the settings of flash mode, |
| 2152 | // white balance, and focus mode. Here we read back the |
| 2153 | // parameters, so we can know those settings. |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2154 | mCameraDevice.applySettings(mCameraSettings); |
| 2155 | mCameraSettings = mCameraDevice.getSettings(); |
Erin Dahlgren | ba6994d | 2013-12-12 16:04:38 -0800 | [diff] [blame] | 2156 | } |
| 2157 | } else { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2158 | mSceneMode = mCameraSettings.getCurrentSceneMode(); |
Erin Dahlgren | ba6994d | 2013-12-12 16:04:38 -0800 | [diff] [blame] | 2159 | if (mSceneMode == null) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2160 | mSceneMode = CameraCapabilities.SceneMode.AUTO; |
Erin Dahlgren | ba6994d | 2013-12-12 16:04:38 -0800 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2164 | if (CameraCapabilities.SceneMode.AUTO == mSceneMode) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2165 | // Set flash mode. |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 2166 | updateParametersFlashMode(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2167 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2168 | // Set focus mode. |
| 2169 | mFocusManager.overrideFocusMode(null); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 2170 | mCameraSettings.setFocusMode( |
| 2171 | mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode())); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2172 | } else { |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 2173 | mFocusManager.overrideFocusMode(mCameraSettings.getCurrentFocusMode()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2174 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2175 | } |
| 2176 | |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 2177 | private void updateParametersFlashMode() { |
| 2178 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
| 2179 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2180 | CameraCapabilities.FlashMode flashMode = mCameraCapabilities.getStringifier() |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 2181 | .flashModeFromString(settingsManager.getString(mAppController.getCameraScope(), |
| 2182 | Keys.KEY_FLASH_MODE)); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2183 | if (mCameraCapabilities.supports(flashMode)) { |
| 2184 | mCameraSettings.setFlashMode(flashMode); |
Erin Dahlgren | 7f0151d | 2014-01-02 16:08:12 -0800 | [diff] [blame] | 2185 | } |
| 2186 | } |
| 2187 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 2188 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2189 | private void updateAutoFocusMoveCallback() { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2190 | if (mCameraSettings.getCurrentFocusMode() == |
| 2191 | CameraCapabilities.FocusMode.CONTINUOUS_PICTURE) { |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 2192 | mCameraDevice.setAutoFocusMoveCallback(mHandler, |
Angus Kong | 4f795b8 | 2013-09-16 14:25:35 -0700 | [diff] [blame] | 2193 | (CameraAFMoveCallback) mAutoFocusMoveCallback); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2194 | } else { |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 2195 | mCameraDevice.setAutoFocusMoveCallback(null, null); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2196 | } |
| 2197 | } |
| 2198 | |
Spike Sprague | abf54e2 | 2014-03-27 15:41:28 -0700 | [diff] [blame] | 2199 | /** |
| 2200 | * Sets the exposure compensation to the given value and also updates settings. |
| 2201 | * |
| 2202 | * @param value exposure compensation value to be set |
| 2203 | */ |
| 2204 | public void setExposureCompensation(int value) { |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 2205 | int max = mCameraCapabilities.getMaxExposureCompensation(); |
| 2206 | int min = mCameraCapabilities.getMinExposureCompensation(); |
Spike Sprague | abf54e2 | 2014-03-27 15:41:28 -0700 | [diff] [blame] | 2207 | if (value >= min && value <= max) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2208 | mCameraSettings.setExposureCompensationIndex(value); |
Spike Sprague | abf54e2 | 2014-03-27 15:41:28 -0700 | [diff] [blame] | 2209 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 2210 | settingsManager.set(mAppController.getCameraScope(), |
| 2211 | Keys.KEY_EXPOSURE, value); |
Spike Sprague | abf54e2 | 2014-03-27 15:41:28 -0700 | [diff] [blame] | 2212 | } else { |
| 2213 | Log.w(TAG, "invalid exposure range: " + value); |
| 2214 | } |
| 2215 | } |
| 2216 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2217 | // We separate the parameters into several subsets, so we can update only |
| 2218 | // the subsets actually need updating. The PREFERENCE set needs extra |
| 2219 | // locking because the preference can be changed from GLThread as well. |
| 2220 | private void setCameraParameters(int updateSet) { |
| 2221 | if ((updateSet & UPDATE_PARAM_INITIALIZE) != 0) { |
| 2222 | updateCameraParametersInitialize(); |
| 2223 | } |
| 2224 | |
| 2225 | if ((updateSet & UPDATE_PARAM_ZOOM) != 0) { |
| 2226 | updateCameraParametersZoom(); |
| 2227 | } |
| 2228 | |
| 2229 | if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) { |
Erin Dahlgren | ba6994d | 2013-12-12 16:04:38 -0800 | [diff] [blame] | 2230 | updateCameraParametersPreference(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2231 | } |
| 2232 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2233 | mCameraDevice.applySettings(mCameraSettings); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | // If the Camera is idle, update the parameters immediately, otherwise |
| 2237 | // accumulate them in mUpdateSet and update later. |
| 2238 | private void setCameraParametersWhenIdle(int additionalUpdateSet) { |
| 2239 | mUpdateSet |= additionalUpdateSet; |
| 2240 | if (mCameraDevice == null) { |
| 2241 | // We will update all the parameters when we open the device, so |
| 2242 | // we don't need to do anything now. |
| 2243 | mUpdateSet = 0; |
| 2244 | return; |
| 2245 | } else if (isCameraIdle()) { |
| 2246 | setCameraParameters(mUpdateSet); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 2247 | updateSceneMode(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2248 | mUpdateSet = 0; |
| 2249 | } else { |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 2250 | if (!mHandler.hasMessages(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE)) { |
| 2251 | mHandler.sendEmptyMessageDelayed(MSG_SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 2256 | @Override |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 2257 | public boolean isCameraIdle() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2258 | return (mCameraState == IDLE) || |
| 2259 | (mCameraState == PREVIEW_STOPPED) || |
| 2260 | ((mFocusManager != null) && mFocusManager.isFocusCompleted() |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 2261 | && (mCameraState != SWITCHING_CAMERA)); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2262 | } |
| 2263 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 2264 | @Override |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 2265 | public boolean isImageCaptureIntent() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2266 | String action = mActivity.getIntent().getAction(); |
| 2267 | return (MediaStore.ACTION_IMAGE_CAPTURE.equals(action) |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 2268 | || CameraActivity.ACTION_IMAGE_CAPTURE_SECURE.equals(action)); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | private void setupCaptureParams() { |
| 2272 | Bundle myExtras = mActivity.getIntent().getExtras(); |
| 2273 | if (myExtras != null) { |
| 2274 | mSaveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT); |
| 2275 | mCropValue = myExtras.getString("crop"); |
| 2276 | } |
| 2277 | } |
| 2278 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2279 | private void initializeCapabilities() { |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 2280 | mCameraCapabilities = mCameraDevice.getCapabilities(); |
| 2281 | mFocusAreaSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.FOCUS_AREA); |
| 2282 | mMeteringAreaSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.METERING_AREA); |
| 2283 | mAeLockSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.AUTO_EXPOSURE_LOCK); |
| 2284 | mAwbLockSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.AUTO_WHITE_BALANCE_LOCK); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2285 | mContinuousFocusSupported = |
| 2286 | mCameraCapabilities.supports(CameraCapabilities.FocusMode.CONTINUOUS_PICTURE); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2287 | } |
| 2288 | |
Marco Nelissen | 0744e4a | 2013-11-22 01:47:37 +0000 | [diff] [blame] | 2289 | // TODO: Remove this |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2290 | @Override |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 2291 | public int onZoomChanged(int index) { |
| 2292 | // Not useful to change zoom value when the activity is paused. |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 2293 | if (mPaused) { |
| 2294 | return index; |
| 2295 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 2296 | mZoomValue = index; |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2297 | if (mCameraSettings == null || mCameraDevice == null) { |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 2298 | return index; |
| 2299 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 2300 | // Set zoom parameters asynchronously |
Sascha Haeberling | ee36c5f | 2014-07-29 17:05:43 -0700 | [diff] [blame] | 2301 | mCameraSettings.setZoomRatio(mZoomValue); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 2302 | mCameraDevice.applySettings(mCameraSettings); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 2303 | CameraSettings settings = mCameraDevice.getSettings(); |
| 2304 | if (settings != null) { |
| 2305 | return settings.getCurrentZoomIndex(); |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 2306 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 2307 | return index; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | @Override |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 2311 | public int getCameraState() { |
| 2312 | return mCameraState; |
| 2313 | } |
| 2314 | |
| 2315 | @Override |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 2316 | public void onMemoryStateChanged(int state) { |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 2317 | mAppController.setShutterEnabled(state == MemoryManager.STATE_OK); |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 2318 | } |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 2319 | |
| 2320 | @Override |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 2321 | public void onLowMemory() { |
| 2322 | // Not much we can do in the photo module. |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 2323 | } |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 2324 | |
| 2325 | @Override |
| 2326 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 2327 | } |
| 2328 | |
| 2329 | @Override |
| 2330 | public void onSensorChanged(SensorEvent event) { |
| 2331 | int type = event.sensor.getType(); |
| 2332 | float[] data; |
| 2333 | if (type == Sensor.TYPE_ACCELEROMETER) { |
| 2334 | data = mGData; |
| 2335 | } else if (type == Sensor.TYPE_MAGNETIC_FIELD) { |
| 2336 | data = mMData; |
| 2337 | } else { |
| 2338 | // we should not be here. |
| 2339 | return; |
| 2340 | } |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 2341 | for (int i = 0; i < 3; i++) { |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 2342 | data[i] = event.values[i]; |
| 2343 | } |
| 2344 | float[] orientation = new float[3]; |
| 2345 | SensorManager.getRotationMatrix(mR, null, mGData, mMData); |
| 2346 | SensorManager.getOrientation(mR, orientation); |
| 2347 | mHeading = (int) (orientation[0] * 180f / Math.PI) % 360; |
| 2348 | if (mHeading < 0) { |
| 2349 | mHeading += 360; |
| 2350 | } |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 2351 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 2352 | |
Ruben Brunk | d217ed0 | 2013-10-08 23:31:13 -0700 | [diff] [blame] | 2353 | // For debugging only. |
| 2354 | public void setDebugUri(Uri uri) { |
| 2355 | mDebugUri = uri; |
| 2356 | } |
| 2357 | |
| 2358 | // For debugging only. |
| 2359 | private void saveToDebugUri(byte[] data) { |
| 2360 | if (mDebugUri != null) { |
| 2361 | OutputStream outputStream = null; |
| 2362 | try { |
| 2363 | outputStream = mContentResolver.openOutputStream(mDebugUri); |
| 2364 | outputStream.write(data); |
| 2365 | outputStream.close(); |
| 2366 | } catch (IOException e) { |
| 2367 | Log.e(TAG, "Exception while writing debug jpeg file", e); |
| 2368 | } finally { |
| 2369 | CameraUtil.closeSilently(outputStream); |
| 2370 | } |
| 2371 | } |
| 2372 | } |
Sascha Haeberling | f2994f0 | 2014-01-23 19:55:01 -0800 | [diff] [blame] | 2373 | |
| 2374 | @Override |
| 2375 | public void onRemoteShutterPress() { |
Sascha Haeberling | 57d4f74 | 2014-06-25 11:05:49 -0700 | [diff] [blame] | 2376 | mHandler.post(new Runnable() { |
| 2377 | @Override |
| 2378 | public void run() { |
| 2379 | focusAndCapture(); |
| 2380 | } |
| 2381 | }); |
Sascha Haeberling | f2994f0 | 2014-01-23 19:55:01 -0800 | [diff] [blame] | 2382 | } |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 2383 | |
| 2384 | /** |
| 2385 | * This class manages the loading/releasing/playing of the sounds needed for |
| 2386 | * countdown timer. |
| 2387 | */ |
| 2388 | private class CountdownSoundPlayer { |
| 2389 | private SoundPool mSoundPool; |
| 2390 | private int mBeepOnce; |
| 2391 | private int mBeepTwice; |
| 2392 | |
| 2393 | void loadSounds() { |
| 2394 | // Load the beeps. |
Seth Raphael | 20e20c9 | 2014-05-27 09:45:26 -0700 | [diff] [blame] | 2395 | if (mSoundPool == null) { |
| 2396 | mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0); |
| 2397 | mBeepOnce = mSoundPool.load(mAppController.getAndroidContext(), R.raw.beep_once, 1); |
| 2398 | mBeepTwice = mSoundPool.load(mAppController.getAndroidContext(), R.raw.beep_twice, 1); |
| 2399 | } |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | void onRemainingSecondsChanged(int newVal) { |
| 2403 | if (mSoundPool == null) { |
| 2404 | Log.e(TAG, "Cannot play sound - they have not been loaded."); |
| 2405 | return; |
| 2406 | } |
| 2407 | if (newVal == 1) { |
| 2408 | mSoundPool.play(mBeepTwice, 1.0f, 1.0f, 0, 0, 1.0f); |
| 2409 | } else if (newVal == 2 || newVal == 3) { |
| 2410 | mSoundPool.play(mBeepOnce, 1.0f, 1.0f, 0, 0, 1.0f); |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | void release() { |
Seth Raphael | 20e20c9 | 2014-05-27 09:45:26 -0700 | [diff] [blame] | 2415 | if (mSoundPool != null) { |
| 2416 | mSoundPool.release(); |
| 2417 | mSoundPool = null; |
| 2418 | } |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 2419 | } |
| 2420 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 2421 | } |