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