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