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.ContentProviderClient; |
| 22 | import android.content.ContentResolver; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 24 | import android.content.Intent; |
| 25 | import android.content.SharedPreferences.Editor; |
| 26 | import android.content.res.Configuration; |
| 27 | import android.graphics.Bitmap; |
| 28 | import android.graphics.SurfaceTexture; |
| 29 | import android.hardware.Camera.CameraInfo; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 30 | import android.hardware.Camera.Parameters; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 31 | import android.hardware.Camera.Size; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 32 | import android.hardware.Sensor; |
| 33 | import android.hardware.SensorEvent; |
| 34 | import android.hardware.SensorEventListener; |
| 35 | import android.hardware.SensorManager; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 36 | import android.location.Location; |
| 37 | import android.media.CameraProfile; |
| 38 | import android.net.Uri; |
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; |
| 47 | import android.util.Log; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 48 | import android.view.KeyEvent; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 49 | import android.view.OrientationEventListener; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 50 | import android.view.View; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 51 | import android.view.WindowManager; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 52 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 53 | import com.android.camera.CameraManager.CameraAFCallback; |
| 54 | import com.android.camera.CameraManager.CameraAFMoveCallback; |
| 55 | import com.android.camera.CameraManager.CameraPictureCallback; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 56 | import com.android.camera.CameraManager.CameraProxy; |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 57 | import com.android.camera.CameraManager.CameraShutterCallback; |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 58 | import com.android.camera.PhotoModule.NamedImages.NamedEntity; |
ztenghui | a16e7b5 | 2013-08-23 11:47:56 -0700 | [diff] [blame] | 59 | import com.android.camera.exif.ExifInterface; |
| 60 | import com.android.camera.exif.ExifTag; |
| 61 | import com.android.camera.exif.Rational; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 62 | import com.android.camera.ui.CountDownView.OnCountDownFinishedListener; |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 63 | import com.android.camera.ui.ModuleSwitcher; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 64 | import com.android.camera.ui.RotateTextToast; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 65 | import com.android.camera.util.ApiHelper; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 66 | import com.android.camera.util.CameraUtil; |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 67 | import com.android.camera.util.GcamHelper; |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 68 | import com.android.camera.util.UsageStatistics; |
| 69 | import com.android.camera2.R; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 70 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 71 | import java.io.File; |
| 72 | import java.io.FileNotFoundException; |
| 73 | import java.io.FileOutputStream; |
| 74 | import java.io.IOException; |
| 75 | import java.io.OutputStream; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 76 | import java.util.List; |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 77 | import java.util.Vector; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 78 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 79 | public class PhotoModule |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 80 | implements CameraModule, |
| 81 | PhotoController, |
| 82 | FocusOverlayManager.Listener, |
| 83 | CameraPreference.OnPreferenceChangedListener, |
| 84 | ShutterButton.OnShutterButtonListener, |
| 85 | MediaSaveService.Listener, |
| 86 | OnCountDownFinishedListener, |
| 87 | SensorEventListener { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 88 | |
| 89 | private static final String TAG = "CAM_PhotoModule"; |
| 90 | |
| 91 | // We number the request code from 1000 to avoid collision with Gallery. |
| 92 | private static final int REQUEST_CROP = 1000; |
| 93 | |
| 94 | private static final int SETUP_PREVIEW = 1; |
| 95 | private static final int FIRST_TIME_INIT = 2; |
| 96 | private static final int CLEAR_SCREEN_DELAY = 3; |
| 97 | private static final int SET_CAMERA_PARAMETERS_WHEN_IDLE = 4; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 98 | private static final int SHOW_TAP_TO_FOCUS_TOAST = 5; |
| 99 | private static final int SWITCH_CAMERA = 6; |
| 100 | private static final int SWITCH_CAMERA_START_ANIMATION = 7; |
| 101 | private static final int CAMERA_OPEN_DONE = 8; |
| 102 | private static final int OPEN_CAMERA_FAIL = 9; |
| 103 | private static final int CAMERA_DISABLED = 10; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 104 | |
| 105 | // The subset of parameters we need to update in setCameraParameters(). |
| 106 | private static final int UPDATE_PARAM_INITIALIZE = 1; |
| 107 | private static final int UPDATE_PARAM_ZOOM = 2; |
| 108 | private static final int UPDATE_PARAM_PREFERENCE = 4; |
| 109 | private static final int UPDATE_PARAM_ALL = -1; |
| 110 | |
| 111 | // This is the timeout to keep the camera in onPause for the first time |
| 112 | // after screen on if the activity is started from secure lock screen. |
| 113 | private static final int KEEP_CAMERA_TIMEOUT = 1000; // ms |
| 114 | |
| 115 | // copied from Camera hierarchy |
| 116 | private CameraActivity mActivity; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 117 | private CameraProxy mCameraDevice; |
| 118 | private int mCameraId; |
| 119 | private Parameters mParameters; |
| 120 | private boolean mPaused; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 121 | |
| 122 | private PhotoUI mUI; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 123 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 124 | // The activity is going to switch to the specified camera id. This is |
| 125 | // needed because texture copy is done in GL thread. -1 means camera is not |
| 126 | // switching. |
| 127 | protected int mPendingSwitchCameraId = -1; |
| 128 | private boolean mOpenCameraFail; |
| 129 | private boolean mCameraDisabled; |
| 130 | |
| 131 | // When setCameraParametersWhenIdle() is called, we accumulate the subsets |
| 132 | // needed to be updated in mUpdateSet. |
| 133 | private int mUpdateSet; |
| 134 | |
| 135 | private static final int SCREEN_DELAY = 2 * 60 * 1000; |
| 136 | |
| 137 | private int mZoomValue; // The current zoom value. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 138 | |
| 139 | private Parameters mInitialParams; |
| 140 | private boolean mFocusAreaSupported; |
| 141 | private boolean mMeteringAreaSupported; |
| 142 | private boolean mAeLockSupported; |
| 143 | private boolean mAwbLockSupported; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 144 | private boolean mContinuousFocusSupported; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 145 | |
| 146 | // The degrees of the device rotated clockwise from its natural orientation. |
| 147 | private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; |
| 148 | private ComboPreferences mPreferences; |
| 149 | |
| 150 | private static final String sTempCropFilename = "crop-temp"; |
| 151 | |
| 152 | private ContentProviderClient mMediaProviderClient; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 153 | private boolean mFaceDetectionStarted = false; |
| 154 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 155 | // mCropValue and mSaveUri are used only if isImageCaptureIntent() is true. |
| 156 | private String mCropValue; |
| 157 | private Uri mSaveUri; |
| 158 | |
Ruben Brunk | d217ed0 | 2013-10-08 23:31:13 -0700 | [diff] [blame] | 159 | private Uri mDebugUri; |
| 160 | |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 161 | // We use a queue to generated names of the images to be used later |
| 162 | // when the image is ready to be saved. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 163 | private NamedImages mNamedImages; |
| 164 | |
| 165 | private Runnable mDoSnapRunnable = new Runnable() { |
| 166 | @Override |
| 167 | public void run() { |
| 168 | onShutterButtonClick(); |
| 169 | } |
| 170 | }; |
| 171 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 172 | /** |
| 173 | * An unpublished intent flag requesting to return as soon as capturing |
| 174 | * is completed. |
| 175 | * |
| 176 | * TODO: consider publishing by moving into MediaStore. |
| 177 | */ |
| 178 | private static final String EXTRA_QUICK_CAPTURE = |
| 179 | "android.intent.extra.quickCapture"; |
| 180 | |
| 181 | // The display rotation in degrees. This is only valid when mCameraState is |
| 182 | // not PREVIEW_STOPPED. |
| 183 | private int mDisplayRotation; |
| 184 | // The value for android.hardware.Camera.setDisplayOrientation. |
| 185 | private int mCameraDisplayOrientation; |
| 186 | // The value for UI components like indicators. |
| 187 | private int mDisplayOrientation; |
| 188 | // The value for android.hardware.Camera.Parameters.setRotation. |
| 189 | private int mJpegRotation; |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 190 | // Indicates whether we are using front camera |
| 191 | private boolean mMirror; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 192 | private boolean mFirstTimeInitialized; |
| 193 | private boolean mIsImageCaptureIntent; |
| 194 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 195 | private int mCameraState = PREVIEW_STOPPED; |
| 196 | private boolean mSnapshotOnIdle = false; |
| 197 | |
| 198 | private ContentResolver mContentResolver; |
| 199 | |
| 200 | private LocationManager mLocationManager; |
| 201 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 202 | private final PostViewPictureCallback mPostViewPictureCallback = |
| 203 | new PostViewPictureCallback(); |
| 204 | private final RawPictureCallback mRawPictureCallback = |
| 205 | new RawPictureCallback(); |
| 206 | private final AutoFocusCallback mAutoFocusCallback = |
| 207 | new AutoFocusCallback(); |
| 208 | private final Object mAutoFocusMoveCallback = |
| 209 | ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 210 | ? new AutoFocusMoveCallback() |
| 211 | : null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 212 | |
| 213 | private final CameraErrorCallback mErrorCallback = new CameraErrorCallback(); |
| 214 | |
| 215 | private long mFocusStartTime; |
| 216 | private long mShutterCallbackTime; |
| 217 | private long mPostViewPictureCallbackTime; |
| 218 | private long mRawPictureCallbackTime; |
| 219 | private long mJpegPictureCallbackTime; |
| 220 | private long mOnResumeTime; |
| 221 | private byte[] mJpegImageData; |
| 222 | |
| 223 | // These latency time are for the CameraLatency test. |
| 224 | public long mAutoFocusTime; |
| 225 | public long mShutterLag; |
| 226 | public long mShutterToPictureDisplayedTime; |
| 227 | public long mPictureDisplayedToJpegCallbackTime; |
| 228 | public long mJpegCallbackFinishTime; |
| 229 | public long mCaptureStartTime; |
| 230 | |
| 231 | // This handles everything about focus. |
| 232 | private FocusOverlayManager mFocusManager; |
| 233 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 234 | private String mSceneMode; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 235 | |
| 236 | private final Handler mHandler = new MainHandler(); |
| 237 | private PreferenceGroup mPreferenceGroup; |
| 238 | |
| 239 | private boolean mQuickCapture; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 240 | private SensorManager mSensorManager; |
| 241 | private float[] mGData = new float[3]; |
| 242 | private float[] mMData = new float[3]; |
| 243 | private float[] mR = new float[16]; |
| 244 | private int mHeading = -1; |
| 245 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 246 | // True if all the parameters needed to start preview is ready. |
| 247 | private boolean mCameraPreviewParamsReady = false; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 248 | |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 249 | private MediaSaveService.OnMediaSavedListener mOnMediaSavedListener = |
| 250 | new MediaSaveService.OnMediaSavedListener() { |
| 251 | @Override |
| 252 | public void onMediaSaved(Uri uri) { |
| 253 | if (uri != null) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 254 | mActivity.notifyNewMedia(uri); |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | }; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 258 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 259 | private void checkDisplayRotation() { |
| 260 | // Set the display orientation if display rotation has changed. |
| 261 | // Sometimes this happens when the device is held upside |
| 262 | // down and camera app is opened. Rotation animation will |
| 263 | // take some time and the rotation value we have got may be |
| 264 | // wrong. Framework does not have a callback for this now. |
| 265 | if (CameraUtil.getDisplayRotation(mActivity) != mDisplayRotation) { |
| 266 | setDisplayOrientation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 267 | } |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 268 | if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) { |
| 269 | mHandler.postDelayed(new Runnable() { |
| 270 | @Override |
| 271 | public void run() { |
| 272 | checkDisplayRotation(); |
| 273 | } |
| 274 | }, 100); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * This Handler is used to post message back onto the main thread of the |
| 280 | * application |
| 281 | */ |
| 282 | private class MainHandler extends Handler { |
| 283 | @Override |
| 284 | public void handleMessage(Message msg) { |
| 285 | switch (msg.what) { |
| 286 | case SETUP_PREVIEW: { |
| 287 | setupPreview(); |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | case CLEAR_SCREEN_DELAY: { |
| 292 | mActivity.getWindow().clearFlags( |
| 293 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | case FIRST_TIME_INIT: { |
| 298 | initializeFirstTime(); |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | case SET_CAMERA_PARAMETERS_WHEN_IDLE: { |
| 303 | setCameraParametersWhenIdle(0); |
| 304 | break; |
| 305 | } |
| 306 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 307 | case SHOW_TAP_TO_FOCUS_TOAST: { |
| 308 | showTapToFocusToast(); |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | case SWITCH_CAMERA: { |
| 313 | switchCamera(); |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | case SWITCH_CAMERA_START_ANIMATION: { |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 318 | // TODO: Need to revisit |
| 319 | // ((CameraScreenNail) mActivity.mCameraScreenNail).animateSwitchCamera(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | |
| 323 | case CAMERA_OPEN_DONE: { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 324 | onCameraOpened(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 325 | break; |
| 326 | } |
| 327 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 328 | case OPEN_CAMERA_FAIL: { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 329 | mOpenCameraFail = true; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 330 | CameraUtil.showErrorAndFinish(mActivity, |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 331 | R.string.cannot_connect_camera); |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | case CAMERA_DISABLED: { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 336 | mCameraDisabled = true; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 337 | CameraUtil.showErrorAndFinish(mActivity, |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 338 | R.string.camera_disabled); |
| 339 | break; |
| 340 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 345 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 346 | @Override |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 347 | public void init(CameraActivity activity, View parent) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 348 | mActivity = activity; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 349 | mUI = new PhotoUI(activity, this, parent); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 350 | mPreferences = new ComboPreferences(mActivity); |
| 351 | CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal()); |
| 352 | mCameraId = getPreferredCameraId(mPreferences); |
| 353 | |
| 354 | mContentResolver = mActivity.getContentResolver(); |
| 355 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 356 | // Surface texture is from camera screen nail and startPreview needs it. |
| 357 | // This must be done before startPreview. |
| 358 | mIsImageCaptureIntent = isImageCaptureIntent(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 359 | |
| 360 | mPreferences.setLocalId(mActivity, mCameraId); |
| 361 | CameraSettings.upgradeLocalPreferences(mPreferences.getLocal()); |
| 362 | // we need to reset exposure for the preview |
| 363 | resetExposureCompensation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 364 | |
| 365 | initializeControlByIntent(); |
| 366 | mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 367 | mLocationManager = new LocationManager(mActivity, mUI); |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 368 | mSensorManager = (SensorManager)(mActivity.getSystemService(Context.SENSOR_SERVICE)); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | private void initializeControlByIntent() { |
| 372 | mUI.initializeControlByIntent(); |
| 373 | if (mIsImageCaptureIntent) { |
| 374 | setupCaptureParams(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | private void onPreviewStarted() { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 379 | setCameraState(IDLE); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 380 | startFaceDetection(); |
| 381 | locationFirstRun(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // Prompt the user to pick to record location for the very first run of |
| 385 | // camera only |
| 386 | private void locationFirstRun() { |
| 387 | if (RecordLocationPreference.isSet(mPreferences)) { |
| 388 | return; |
| 389 | } |
| 390 | if (mActivity.isSecureCamera()) return; |
| 391 | // Check if the back camera exists |
| 392 | int backCameraId = CameraHolder.instance().getBackCameraId(); |
| 393 | if (backCameraId == -1) { |
| 394 | // If there is no back camera, do not show the prompt. |
| 395 | return; |
| 396 | } |
Doris Liu | 6a83d52 | 2013-07-02 12:03:32 -0700 | [diff] [blame] | 397 | mUI.showLocationDialog(); |
| 398 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 399 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 400 | @Override |
Doris Liu | 6a83d52 | 2013-07-02 12:03:32 -0700 | [diff] [blame] | 401 | public void enableRecordingLocation(boolean enable) { |
| 402 | setLocationPreference(enable ? RecordLocationPreference.VALUE_ON |
| 403 | : RecordLocationPreference.VALUE_OFF); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 406 | @Override |
| 407 | public void onPreviewUIReady() { |
| 408 | startPreview(); |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public void onPreviewUIDestroyed() { |
| 413 | if (mCameraDevice == null) { |
| 414 | return; |
| 415 | } |
| 416 | mCameraDevice.setPreviewTexture(null); |
| 417 | stopPreview(); |
| 418 | } |
| 419 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 420 | private void setLocationPreference(String value) { |
| 421 | mPreferences.edit() |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 422 | .putString(CameraSettings.KEY_RECORD_LOCATION, value) |
| 423 | .apply(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 424 | // TODO: Fix this to use the actual onSharedPreferencesChanged listener |
| 425 | // instead of invoking manually |
| 426 | onSharedPreferenceChanged(); |
| 427 | } |
| 428 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 429 | private void onCameraOpened() { |
| 430 | View root = mUI.getRootView(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 431 | // These depend on camera parameters. |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 432 | |
| 433 | int width = root.getWidth(); |
| 434 | int height = root.getHeight(); |
Doris Liu | 6a0de79 | 2013-02-26 10:54:25 -0800 | [diff] [blame] | 435 | mFocusManager.setPreviewSize(width, height); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 436 | openCameraCommon(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 439 | private void switchCamera() { |
| 440 | if (mPaused) return; |
| 441 | |
| 442 | Log.v(TAG, "Start to switch camera. id=" + mPendingSwitchCameraId); |
| 443 | mCameraId = mPendingSwitchCameraId; |
| 444 | mPendingSwitchCameraId = -1; |
| 445 | setCameraId(mCameraId); |
| 446 | |
| 447 | // from onPause |
| 448 | closeCamera(); |
| 449 | mUI.collapseCameraControls(); |
| 450 | mUI.clearFaces(); |
| 451 | if (mFocusManager != null) mFocusManager.removeMessages(); |
| 452 | |
| 453 | // Restart the camera and initialize the UI. From onCreate. |
| 454 | mPreferences.setLocalId(mActivity, mCameraId); |
| 455 | CameraSettings.upgradeLocalPreferences(mPreferences.getLocal()); |
Angus Kong | 4f795b8 | 2013-09-16 14:25:35 -0700 | [diff] [blame] | 456 | mCameraDevice = CameraUtil.openCamera( |
| 457 | mActivity, mCameraId, mHandler, |
| 458 | mActivity.getCameraOpenErrorCallback()); |
| 459 | if (mCameraDevice == null) { |
| 460 | Log.e(TAG, "Failed to open camera:" + mCameraId + ", aborting."); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 461 | return; |
Doris Liu | 09106a4 | 2013-03-05 09:54:25 -0800 | [diff] [blame] | 462 | } |
Angus Kong | 4f795b8 | 2013-09-16 14:25:35 -0700 | [diff] [blame] | 463 | mParameters = mCameraDevice.getParameters(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 464 | initializeCapabilities(); |
| 465 | CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId]; |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 466 | mMirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT); |
| 467 | mFocusManager.setMirror(mMirror); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 468 | mFocusManager.setParameters(mInitialParams); |
| 469 | setupPreview(); |
| 470 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 471 | // reset zoom value index |
| 472 | mZoomValue = 0; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 473 | openCameraCommon(); |
| 474 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 475 | // Start switch camera animation. Post a message because |
| 476 | // onFrameAvailable from the old camera may already exist. |
| 477 | mHandler.sendEmptyMessage(SWITCH_CAMERA_START_ANIMATION); |
Doris Liu | 48239f4 | 2013-03-04 22:19:10 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 480 | protected void setCameraId(int cameraId) { |
| 481 | ListPreference pref = mPreferenceGroup.findPreference(CameraSettings.KEY_CAMERA_ID); |
| 482 | pref.setValue("" + cameraId); |
| 483 | } |
| 484 | |
| 485 | // either open a new camera or switch cameras |
| 486 | private void openCameraCommon() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 487 | loadCameraPreferences(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 488 | |
| 489 | mUI.onCameraOpened(mPreferenceGroup, mPreferences, mParameters, this); |
Angus Kong | 0fb819b | 2013-10-08 13:44:19 -0700 | [diff] [blame] | 490 | if (mIsImageCaptureIntent) { |
| 491 | mUI.overrideSettings(CameraSettings.KEY_CAMERA_HDR_PLUS, |
| 492 | mActivity.getString(R.string.setting_off_value)); |
| 493 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 494 | updateSceneMode(); |
| 495 | showTapToFocusToastIfNeeded(); |
| 496 | |
| 497 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 498 | } |
| 499 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 500 | @Override |
Doris Liu | b3749f2 | 2013-09-25 12:22:08 -0700 | [diff] [blame] | 501 | public void onScreenSizeChanged(int width, int height) { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 502 | if (mFocusManager != null) mFocusManager.setPreviewSize(width, height); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 503 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 504 | |
| 505 | private void resetExposureCompensation() { |
| 506 | String value = mPreferences.getString(CameraSettings.KEY_EXPOSURE, |
| 507 | CameraSettings.EXPOSURE_DEFAULT_VALUE); |
| 508 | if (!CameraSettings.EXPOSURE_DEFAULT_VALUE.equals(value)) { |
| 509 | Editor editor = mPreferences.edit(); |
| 510 | editor.putString(CameraSettings.KEY_EXPOSURE, "0"); |
| 511 | editor.apply(); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | private void keepMediaProviderInstance() { |
| 516 | // We want to keep a reference to MediaProvider in camera's lifecycle. |
| 517 | // TODO: Utilize mMediaProviderClient instance to replace |
| 518 | // ContentResolver calls. |
| 519 | if (mMediaProviderClient == null) { |
| 520 | mMediaProviderClient = mContentResolver |
| 521 | .acquireContentProviderClient(MediaStore.AUTHORITY); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // Snapshots can only be taken after this is called. It should be called |
| 526 | // once only. We could have done these things in onCreate() but we want to |
| 527 | // make preview screen appear as soon as possible. |
| 528 | private void initializeFirstTime() { |
Sascha Haeberling | 330dafb | 2013-10-10 19:00:41 -0700 | [diff] [blame^] | 529 | if (mFirstTimeInitialized || mPaused) { |
| 530 | return; |
| 531 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 532 | |
| 533 | // Initialize location service. |
| 534 | boolean recordLocation = RecordLocationPreference.get( |
| 535 | mPreferences, mContentResolver); |
| 536 | mLocationManager.recordLocation(recordLocation); |
| 537 | |
| 538 | keepMediaProviderInstance(); |
| 539 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 540 | mUI.initializeFirstTime(); |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 541 | MediaSaveService s = mActivity.getMediaSaveService(); |
| 542 | // We set the listener only when both service and shutterbutton |
| 543 | // are initialized. |
| 544 | if (s != null) { |
| 545 | s.setListener(this); |
| 546 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 547 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 548 | mNamedImages = new NamedImages(); |
| 549 | |
| 550 | mFirstTimeInitialized = true; |
| 551 | addIdleHandler(); |
| 552 | |
| 553 | mActivity.updateStorageSpaceAndHint(); |
| 554 | } |
| 555 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 556 | // If the activity is paused and resumed, this method will be called in |
| 557 | // onResume. |
| 558 | private void initializeSecondTime() { |
| 559 | // Start location update if needed. |
| 560 | boolean recordLocation = RecordLocationPreference.get( |
| 561 | mPreferences, mContentResolver); |
| 562 | mLocationManager.recordLocation(recordLocation); |
| 563 | MediaSaveService s = mActivity.getMediaSaveService(); |
| 564 | if (s != null) { |
| 565 | s.setListener(this); |
| 566 | } |
| 567 | mNamedImages = new NamedImages(); |
| 568 | mUI.initializeSecondTime(mParameters); |
| 569 | keepMediaProviderInstance(); |
| 570 | } |
| 571 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 572 | private void showTapToFocusToastIfNeeded() { |
| 573 | // Show the tap to focus toast if this is the first start. |
| 574 | if (mFocusAreaSupported && |
| 575 | mPreferences.getBoolean(CameraSettings.KEY_CAMERA_FIRST_USE_HINT_SHOWN, true)) { |
| 576 | // Delay the toast for one second to wait for orientation. |
| 577 | mHandler.sendEmptyMessageDelayed(SHOW_TAP_TO_FOCUS_TOAST, 1000); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | private void addIdleHandler() { |
| 582 | MessageQueue queue = Looper.myQueue(); |
| 583 | queue.addIdleHandler(new MessageQueue.IdleHandler() { |
| 584 | @Override |
| 585 | public boolean queueIdle() { |
| 586 | Storage.ensureOSXCompatible(); |
| 587 | return false; |
| 588 | } |
| 589 | }); |
| 590 | } |
| 591 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 592 | @Override |
| 593 | public void startFaceDetection() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 594 | if (mFaceDetectionStarted) return; |
| 595 | if (mParameters.getMaxNumDetectedFaces() > 0) { |
| 596 | mFaceDetectionStarted = true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 597 | CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId]; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 598 | mUI.onStartFaceDetection(mDisplayOrientation, |
| 599 | (info.facing == CameraInfo.CAMERA_FACING_FRONT)); |
Angus Kong | 9e76552 | 2013-07-31 14:05:20 -0700 | [diff] [blame] | 600 | mCameraDevice.setFaceDetectionCallback(mHandler, mUI); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 601 | mCameraDevice.startFaceDetection(); |
| 602 | } |
| 603 | } |
| 604 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 605 | @Override |
| 606 | public void stopFaceDetection() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 607 | if (!mFaceDetectionStarted) return; |
| 608 | if (mParameters.getMaxNumDetectedFaces() > 0) { |
| 609 | mFaceDetectionStarted = false; |
Angus Kong | 9e76552 | 2013-07-31 14:05:20 -0700 | [diff] [blame] | 610 | mCameraDevice.setFaceDetectionCallback(null, null); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 611 | mCameraDevice.stopFaceDetection(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 612 | mUI.clearFaces(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 616 | private final class ShutterCallback |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 617 | implements CameraShutterCallback { |
Angus Kong | dcb0ef1 | 2013-03-25 23:11:43 -0700 | [diff] [blame] | 618 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 619 | private boolean mNeedsAnimation; |
Angus Kong | dcb0ef1 | 2013-03-25 23:11:43 -0700 | [diff] [blame] | 620 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 621 | public ShutterCallback(boolean needsAnimation) { |
| 622 | mNeedsAnimation = needsAnimation; |
Angus Kong | dcb0ef1 | 2013-03-25 23:11:43 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 625 | @Override |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 626 | public void onShutter(CameraProxy camera) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 627 | mShutterCallbackTime = System.currentTimeMillis(); |
| 628 | mShutterLag = mShutterCallbackTime - mCaptureStartTime; |
| 629 | Log.v(TAG, "mShutterLag = " + mShutterLag + "ms"); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 630 | if (mNeedsAnimation) { |
| 631 | mActivity.runOnUiThread(new Runnable() { |
| 632 | @Override |
| 633 | public void run() { |
| 634 | animateAfterShutter(); |
| 635 | } |
| 636 | }); |
Angus Kong | dcb0ef1 | 2013-03-25 23:11:43 -0700 | [diff] [blame] | 637 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 641 | private final class PostViewPictureCallback |
| 642 | implements CameraPictureCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 643 | @Override |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 644 | public void onPictureTaken(byte [] data, CameraProxy camera) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 645 | mPostViewPictureCallbackTime = System.currentTimeMillis(); |
| 646 | Log.v(TAG, "mShutterToPostViewCallbackTime = " |
| 647 | + (mPostViewPictureCallbackTime - mShutterCallbackTime) |
| 648 | + "ms"); |
| 649 | } |
| 650 | } |
| 651 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 652 | private final class RawPictureCallback |
| 653 | implements CameraPictureCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 654 | @Override |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 655 | public void onPictureTaken(byte [] rawData, CameraProxy camera) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 656 | mRawPictureCallbackTime = System.currentTimeMillis(); |
| 657 | Log.v(TAG, "mShutterToRawCallbackTime = " |
| 658 | + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms"); |
| 659 | } |
| 660 | } |
| 661 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 662 | private final class JpegPictureCallback |
| 663 | implements CameraPictureCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 664 | Location mLocation; |
| 665 | |
| 666 | public JpegPictureCallback(Location loc) { |
| 667 | mLocation = loc; |
| 668 | } |
| 669 | |
| 670 | @Override |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 671 | public void onPictureTaken(final byte [] jpegData, CameraProxy camera) { |
Sascha Haeberling | 8890194 | 2013-08-28 17:49:00 -0700 | [diff] [blame] | 672 | mUI.enableShutter(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 673 | if (mPaused) { |
| 674 | return; |
| 675 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 676 | if (mIsImageCaptureIntent) { |
| 677 | stopPreview(); |
| 678 | } |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 679 | if (mSceneMode == CameraUtil.SCENE_MODE_HDR) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 680 | mUI.showSwitcher(); |
| 681 | mUI.setSwipingEnabled(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | mJpegPictureCallbackTime = System.currentTimeMillis(); |
| 685 | // If postview callback has arrived, the captured image is displayed |
| 686 | // in postview callback. If not, the captured image is displayed in |
| 687 | // raw picture callback. |
| 688 | if (mPostViewPictureCallbackTime != 0) { |
| 689 | mShutterToPictureDisplayedTime = |
| 690 | mPostViewPictureCallbackTime - mShutterCallbackTime; |
| 691 | mPictureDisplayedToJpegCallbackTime = |
| 692 | mJpegPictureCallbackTime - mPostViewPictureCallbackTime; |
| 693 | } else { |
| 694 | mShutterToPictureDisplayedTime = |
| 695 | mRawPictureCallbackTime - mShutterCallbackTime; |
| 696 | mPictureDisplayedToJpegCallbackTime = |
| 697 | mJpegPictureCallbackTime - mRawPictureCallbackTime; |
| 698 | } |
| 699 | Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = " |
| 700 | + mPictureDisplayedToJpegCallbackTime + "ms"); |
| 701 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 702 | mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden. |
| 703 | if (!mIsImageCaptureIntent) { |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 704 | setupPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 705 | } |
| 706 | |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 707 | ExifInterface exif = Exif.getExif(jpegData); |
| 708 | int orientation = Exif.getOrientation(exif); |
Ruben Brunk | d217ed0 | 2013-10-08 23:31:13 -0700 | [diff] [blame] | 709 | |
| 710 | if (mDebugUri != null) { |
| 711 | // If using a debug uri, save jpeg there |
| 712 | saveToDebugUri(jpegData); |
| 713 | }else if (!mIsImageCaptureIntent) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 714 | // Calculate the width and the height of the jpeg. |
| 715 | Size s = mParameters.getPictureSize(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 716 | int width, height; |
| 717 | if ((mJpegRotation + orientation) % 180 == 0) { |
| 718 | width = s.width; |
| 719 | height = s.height; |
| 720 | } else { |
| 721 | width = s.height; |
| 722 | height = s.width; |
| 723 | } |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 724 | NamedEntity name = mNamedImages.getNextNameEntity(); |
| 725 | String title = (name == null) ? null : name.title; |
| 726 | long date = (name == null) ? -1 : name.date; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 727 | if (title == null) { |
| 728 | Log.e(TAG, "Unbalanced name/data pair"); |
| 729 | } else { |
| 730 | if (date == -1) date = mCaptureStartTime; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 731 | if (mHeading >= 0) { |
| 732 | // heading direction has been updated by the sensor. |
| 733 | ExifTag directionRefTag = exif.buildTag( |
| 734 | ExifInterface.TAG_GPS_IMG_DIRECTION_REF, |
| 735 | ExifInterface.GpsTrackRef.MAGNETIC_DIRECTION); |
| 736 | ExifTag directionTag = exif.buildTag( |
| 737 | ExifInterface.TAG_GPS_IMG_DIRECTION, |
| 738 | new Rational(mHeading, 1)); |
| 739 | exif.setTag(directionRefTag); |
| 740 | exif.setTag(directionTag); |
| 741 | } |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 742 | mActivity.getMediaSaveService().addImage( |
| 743 | jpegData, title, date, mLocation, width, height, |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 744 | orientation, exif, mOnMediaSavedListener, mContentResolver); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 745 | } |
Doris Liu | ce2acbc | 2013-08-21 18:45:29 -0700 | [diff] [blame] | 746 | // Animate capture with real jpeg data instead of a preview frame. |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 747 | mUI.animateCapture(jpegData, orientation, mMirror); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 748 | } else { |
| 749 | mJpegImageData = jpegData; |
| 750 | if (!mQuickCapture) { |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 751 | mUI.showCapturedImageForReview(jpegData, orientation, mMirror); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 752 | } else { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 753 | onCaptureDone(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | // Check this in advance of each shot so we don't add to shutter |
| 758 | // latency. It's true that someone else could write to the SD card in |
| 759 | // the mean time and fill it, but that could have happened between the |
| 760 | // shutter press and saving the JPEG too. |
| 761 | mActivity.updateStorageSpaceAndHint(); |
| 762 | |
| 763 | long now = System.currentTimeMillis(); |
| 764 | mJpegCallbackFinishTime = now - mJpegPictureCallbackTime; |
| 765 | Log.v(TAG, "mJpegCallbackFinishTime = " |
| 766 | + mJpegCallbackFinishTime + "ms"); |
| 767 | mJpegPictureCallbackTime = 0; |
| 768 | } |
| 769 | } |
| 770 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 771 | private final class AutoFocusCallback implements CameraAFCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 772 | @Override |
| 773 | public void onAutoFocus( |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 774 | boolean focused, CameraProxy camera) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 775 | if (mPaused) return; |
| 776 | |
| 777 | mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime; |
| 778 | Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms"); |
| 779 | setCameraState(IDLE); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 780 | mFocusManager.onAutoFocus(focused, mUI.isShutterPressed()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 784 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 785 | private final class AutoFocusMoveCallback |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 786 | implements CameraAFMoveCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 787 | @Override |
| 788 | public void onAutoFocusMoving( |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 789 | boolean moving, CameraProxy camera) { |
| 790 | mFocusManager.onAutoFocusMoving(moving); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 794 | /** |
| 795 | * This class is just a thread-safe queue for name,date holder objects. |
| 796 | */ |
| 797 | public static class NamedImages { |
| 798 | private Vector<NamedEntity> mQueue; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 799 | |
| 800 | public NamedImages() { |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 801 | mQueue = new Vector<NamedEntity>(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 804 | public void nameNewImage(long date) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 805 | NamedEntity r = new NamedEntity(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 806 | r.title = CameraUtil.createJpegName(date); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 807 | r.date = date; |
| 808 | mQueue.add(r); |
| 809 | } |
| 810 | |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 811 | public NamedEntity getNextNameEntity() { |
| 812 | synchronized(mQueue) { |
| 813 | if (!mQueue.isEmpty()) { |
| 814 | return mQueue.remove(0); |
| 815 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 816 | } |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 817 | return null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 818 | } |
| 819 | |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 820 | public static class NamedEntity { |
| 821 | public String title; |
| 822 | public long date; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
| 826 | private void setCameraState(int state) { |
| 827 | mCameraState = state; |
| 828 | switch (state) { |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 829 | case PhotoController.PREVIEW_STOPPED: |
| 830 | case PhotoController.SNAPSHOT_IN_PROGRESS: |
| 831 | case PhotoController.SWITCHING_CAMERA: |
| 832 | mUI.enableGestures(false); |
| 833 | break; |
| 834 | case PhotoController.IDLE: |
| 835 | mUI.enableGestures(true); |
| 836 | break; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 840 | private void animateAfterShutter() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 841 | // Only animate when in full screen capture mode |
| 842 | // i.e. If monkey/a user swipes to the gallery during picture taking, |
| 843 | // don't show animation |
Doris Liu | c2e9abd | 2013-06-19 14:20:51 -0700 | [diff] [blame] | 844 | if (!mIsImageCaptureIntent) { |
| 845 | mUI.animateFlash(); |
Doris Liu | c2e9abd | 2013-06-19 14:20:51 -0700 | [diff] [blame] | 846 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | @Override |
| 850 | public boolean capture() { |
| 851 | // If we are already in the middle of taking a snapshot or the image save request |
| 852 | // is full then ignore. |
| 853 | if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 854 | || mCameraState == SWITCHING_CAMERA |
| 855 | || mActivity.getMediaSaveService().isQueueFull()) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 856 | return false; |
| 857 | } |
| 858 | mCaptureStartTime = System.currentTimeMillis(); |
| 859 | mPostViewPictureCallbackTime = 0; |
| 860 | mJpegImageData = null; |
| 861 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 862 | final boolean animateBefore = (mSceneMode == CameraUtil.SCENE_MODE_HDR); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 863 | |
| 864 | if (animateBefore) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 865 | animateAfterShutter(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | // Set rotation and gps data. |
Doris Liu | 3cf565c | 2013-02-15 10:55:37 -0800 | [diff] [blame] | 869 | int orientation; |
| 870 | // We need to be consistent with the framework orientation (i.e. the |
| 871 | // orientation of the UI.) when the auto-rotate screen setting is on. |
| 872 | if (mActivity.isAutoRotateScreen()) { |
| 873 | orientation = (360 - mDisplayRotation) % 360; |
| 874 | } else { |
| 875 | orientation = mOrientation; |
| 876 | } |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 877 | mJpegRotation = CameraUtil.getJpegRotation(mCameraId, orientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 878 | mParameters.setRotation(mJpegRotation); |
| 879 | Location loc = mLocationManager.getCurrentLocation(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 880 | CameraUtil.setGpsParameters(mParameters, loc); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 881 | mCameraDevice.setParameters(mParameters); |
| 882 | |
Sascha Haeberling | 8890194 | 2013-08-28 17:49:00 -0700 | [diff] [blame] | 883 | // We don't want user to press the button again while taking a |
| 884 | // multi-second HDR photo. |
| 885 | mUI.enableShutter(false); |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 886 | mCameraDevice.takePicture(mHandler, |
| 887 | new ShutterCallback(!animateBefore), |
Angus Kong | dcb0ef1 | 2013-03-25 23:11:43 -0700 | [diff] [blame] | 888 | mRawPictureCallback, mPostViewPictureCallback, |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 889 | new JpegPictureCallback(loc)); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 890 | |
Ruben Brunk | a9d66bd | 2013-09-06 11:56:32 -0700 | [diff] [blame] | 891 | mNamedImages.nameNewImage(mCaptureStartTime); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 892 | |
| 893 | mFaceDetectionStarted = false; |
| 894 | setCameraState(SNAPSHOT_IN_PROGRESS); |
Bobby Georgescu | 301b646 | 2013-04-01 15:33:17 -0700 | [diff] [blame] | 895 | UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA, |
| 896 | UsageStatistics.ACTION_CAPTURE_DONE, "Photo"); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 897 | return true; |
| 898 | } |
| 899 | |
| 900 | @Override |
| 901 | public void setFocusParameters() { |
| 902 | setCameraParameters(UPDATE_PARAM_PREFERENCE); |
| 903 | } |
| 904 | |
| 905 | private int getPreferredCameraId(ComboPreferences preferences) { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 906 | int intentCameraId = CameraUtil.getCameraFacingIntentExtras(mActivity); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 907 | if (intentCameraId != -1) { |
| 908 | // Testing purpose. Launch a specific camera through the intent |
| 909 | // extras. |
| 910 | return intentCameraId; |
| 911 | } else { |
| 912 | return CameraSettings.readPreferredCameraId(preferences); |
| 913 | } |
| 914 | } |
| 915 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 916 | private void updateSceneMode() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 917 | // If scene mode is set, we cannot set flash mode, white balance, and |
| 918 | // focus mode, instead, we read it from driver |
| 919 | if (!Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) { |
| 920 | overrideCameraSettings(mParameters.getFlashMode(), |
| 921 | mParameters.getWhiteBalance(), mParameters.getFocusMode()); |
| 922 | } else { |
| 923 | overrideCameraSettings(null, null, null); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | private void overrideCameraSettings(final String flashMode, |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 928 | final String whiteBalance, final String focusMode) { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 929 | mUI.overrideSettings( |
| 930 | CameraSettings.KEY_FLASH_MODE, flashMode, |
| 931 | CameraSettings.KEY_WHITE_BALANCE, whiteBalance, |
| 932 | CameraSettings.KEY_FOCUS_MODE, focusMode); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | private void loadCameraPreferences() { |
| 936 | CameraSettings settings = new CameraSettings(mActivity, mInitialParams, |
| 937 | mCameraId, CameraHolder.instance().getCameraInfo()); |
| 938 | mPreferenceGroup = settings.getPreferenceGroup(R.xml.camera_preferences); |
| 939 | } |
| 940 | |
| 941 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 942 | public void onOrientationChanged(int orientation) { |
| 943 | // We keep the last known orientation. So if the user first orient |
| 944 | // the camera then point the camera to floor or sky, we still have |
| 945 | // the correct orientation. |
| 946 | if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 947 | mOrientation = CameraUtil.roundOrientation(orientation, mOrientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 948 | |
| 949 | // Show the toast after getting the first orientation changed. |
| 950 | if (mHandler.hasMessages(SHOW_TAP_TO_FOCUS_TOAST)) { |
| 951 | mHandler.removeMessages(SHOW_TAP_TO_FOCUS_TOAST); |
| 952 | showTapToFocusToast(); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | @Override |
| 957 | public void onStop() { |
| 958 | if (mMediaProviderClient != null) { |
| 959 | mMediaProviderClient.release(); |
| 960 | mMediaProviderClient = null; |
| 961 | } |
| 962 | } |
| 963 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 964 | @Override |
| 965 | public void onCaptureCancelled() { |
| 966 | mActivity.setResultEx(Activity.RESULT_CANCELED, new Intent()); |
| 967 | mActivity.finish(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 968 | } |
| 969 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 970 | @Override |
| 971 | public void onCaptureRetake() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 972 | if (mPaused) |
| 973 | return; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 974 | mUI.hidePostCaptureAlert(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 975 | setupPreview(); |
| 976 | } |
| 977 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 978 | @Override |
| 979 | public void onCaptureDone() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 980 | if (mPaused) { |
| 981 | return; |
| 982 | } |
| 983 | |
| 984 | byte[] data = mJpegImageData; |
| 985 | |
| 986 | if (mCropValue == null) { |
| 987 | // First handle the no crop case -- just return the value. If the |
| 988 | // caller specifies a "save uri" then write the data to its |
| 989 | // stream. Otherwise, pass back a scaled down version of the bitmap |
| 990 | // directly in the extras. |
| 991 | if (mSaveUri != null) { |
| 992 | OutputStream outputStream = null; |
| 993 | try { |
| 994 | outputStream = mContentResolver.openOutputStream(mSaveUri); |
| 995 | outputStream.write(data); |
| 996 | outputStream.close(); |
| 997 | |
| 998 | mActivity.setResultEx(Activity.RESULT_OK); |
| 999 | mActivity.finish(); |
| 1000 | } catch (IOException ex) { |
| 1001 | // ignore exception |
| 1002 | } finally { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1003 | CameraUtil.closeSilently(outputStream); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1004 | } |
| 1005 | } else { |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1006 | ExifInterface exif = Exif.getExif(data); |
| 1007 | int orientation = Exif.getOrientation(exif); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1008 | Bitmap bitmap = CameraUtil.makeBitmap(data, 50 * 1024); |
| 1009 | bitmap = CameraUtil.rotate(bitmap, orientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1010 | mActivity.setResultEx(Activity.RESULT_OK, |
| 1011 | new Intent("inline-data").putExtra("data", bitmap)); |
| 1012 | mActivity.finish(); |
| 1013 | } |
| 1014 | } else { |
| 1015 | // Save the image to a temp file and invoke the cropper |
| 1016 | Uri tempUri = null; |
| 1017 | FileOutputStream tempStream = null; |
| 1018 | try { |
| 1019 | File path = mActivity.getFileStreamPath(sTempCropFilename); |
| 1020 | path.delete(); |
| 1021 | tempStream = mActivity.openFileOutput(sTempCropFilename, 0); |
| 1022 | tempStream.write(data); |
| 1023 | tempStream.close(); |
| 1024 | tempUri = Uri.fromFile(path); |
| 1025 | } catch (FileNotFoundException ex) { |
| 1026 | mActivity.setResultEx(Activity.RESULT_CANCELED); |
| 1027 | mActivity.finish(); |
| 1028 | return; |
| 1029 | } catch (IOException ex) { |
| 1030 | mActivity.setResultEx(Activity.RESULT_CANCELED); |
| 1031 | mActivity.finish(); |
| 1032 | return; |
| 1033 | } finally { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1034 | CameraUtil.closeSilently(tempStream); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | Bundle newExtras = new Bundle(); |
| 1038 | if (mCropValue.equals("circle")) { |
| 1039 | newExtras.putString("circleCrop", "true"); |
| 1040 | } |
| 1041 | if (mSaveUri != null) { |
| 1042 | newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri); |
| 1043 | } else { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1044 | newExtras.putBoolean(CameraUtil.KEY_RETURN_DATA, true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1045 | } |
| 1046 | if (mActivity.isSecureCamera()) { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1047 | newExtras.putBoolean(CameraUtil.KEY_SHOW_WHEN_LOCKED, true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1050 | // TODO: Share this constant. |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 1051 | final String CROP_ACTION = "com.android.camera.action.CROP"; |
| 1052 | Intent cropIntent = new Intent(CROP_ACTION); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1053 | |
| 1054 | cropIntent.setData(tempUri); |
| 1055 | cropIntent.putExtras(newExtras); |
| 1056 | |
| 1057 | mActivity.startActivityForResult(cropIntent, REQUEST_CROP); |
| 1058 | } |
| 1059 | } |
| 1060 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1061 | @Override |
| 1062 | public void onShutterButtonFocus(boolean pressed) { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1063 | if (mPaused || mUI.collapseCameraControls() |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1064 | || (mCameraState == SNAPSHOT_IN_PROGRESS) |
| 1065 | || (mCameraState == PREVIEW_STOPPED)) return; |
| 1066 | |
| 1067 | // Do not do focus if there is not enough storage. |
| 1068 | if (pressed && !canTakePicture()) return; |
| 1069 | |
| 1070 | if (pressed) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1071 | mFocusManager.onShutterDown(); |
| 1072 | } else { |
Doris Liu | da50e05 | 2013-02-07 14:36:32 -0800 | [diff] [blame] | 1073 | // for countdown mode, we need to postpone the shutter release |
| 1074 | // i.e. lock the focus during countdown. |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1075 | if (!mUI.isCountingDown()) { |
Doris Liu | da50e05 | 2013-02-07 14:36:32 -0800 | [diff] [blame] | 1076 | mFocusManager.onShutterUp(); |
| 1077 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | @Override |
| 1082 | public void onShutterButtonClick() { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1083 | if (mPaused || mUI.collapseCameraControls() |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1084 | || (mCameraState == SWITCHING_CAMERA) |
| 1085 | || (mCameraState == PREVIEW_STOPPED)) return; |
| 1086 | |
| 1087 | // Do not take the picture if there is not enough storage. |
Angus Kong | 2dcc0a9 | 2013-09-25 13:00:08 -0700 | [diff] [blame] | 1088 | if (mActivity.getStorageSpaceBytes() <= Storage.LOW_STORAGE_THRESHOLD_BYTES) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1089 | Log.i(TAG, "Not enough space or storage not ready. remaining=" |
Angus Kong | 2dcc0a9 | 2013-09-25 13:00:08 -0700 | [diff] [blame] | 1090 | + mActivity.getStorageSpaceBytes()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1091 | return; |
| 1092 | } |
| 1093 | Log.v(TAG, "onShutterButtonClick: mCameraState=" + mCameraState); |
| 1094 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1095 | if (mSceneMode == CameraUtil.SCENE_MODE_HDR) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1096 | mUI.hideSwitcher(); |
| 1097 | mUI.setSwipingEnabled(false); |
Doris Liu | 9cdfe00 | 2013-04-16 09:50:56 -0700 | [diff] [blame] | 1098 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1099 | // If the user wants to do a snapshot while the previous one is still |
| 1100 | // in progress, remember the fact and do it after we finish the previous |
| 1101 | // one and re-start the preview. Snapshot in progress also includes the |
| 1102 | // state that autofocus is focusing and a picture will be taken when |
| 1103 | // focus callback arrives. |
| 1104 | if ((mFocusManager.isFocusingSnapOnFinish() || mCameraState == SNAPSHOT_IN_PROGRESS) |
| 1105 | && !mIsImageCaptureIntent) { |
| 1106 | mSnapshotOnIdle = true; |
| 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | String timer = mPreferences.getString( |
| 1111 | CameraSettings.KEY_TIMER, |
| 1112 | mActivity.getString(R.string.pref_camera_timer_default)); |
| 1113 | boolean playSound = mPreferences.getString(CameraSettings.KEY_TIMER_SOUND_EFFECTS, |
| 1114 | mActivity.getString(R.string.pref_camera_timer_sound_default)) |
| 1115 | .equals(mActivity.getString(R.string.setting_on_value)); |
| 1116 | |
| 1117 | int seconds = Integer.parseInt(timer); |
| 1118 | // When shutter button is pressed, check whether the previous countdown is |
| 1119 | // finished. If not, cancel the previous countdown and start a new one. |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1120 | if (mUI.isCountingDown()) { |
| 1121 | mUI.cancelCountDown(); |
| 1122 | } |
| 1123 | if (seconds > 0) { |
| 1124 | mUI.startCountDown(seconds, playSound); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1125 | } else { |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1126 | mSnapshotOnIdle = false; |
| 1127 | mFocusManager.doSnap(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | @Override |
| 1132 | public void installIntentFilter() { |
Sascha Haeberling | 4de7880 | 2013-10-06 18:07:07 -0700 | [diff] [blame] | 1133 | // Do nothing. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | @Override |
| 1137 | public boolean updateStorageHintOnResume() { |
| 1138 | return mFirstTimeInitialized; |
| 1139 | } |
| 1140 | |
| 1141 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1142 | public void onResumeBeforeSuper() { |
| 1143 | mPaused = false; |
| 1144 | } |
| 1145 | |
Angus Kong | 4f795b8 | 2013-09-16 14:25:35 -0700 | [diff] [blame] | 1146 | /** |
| 1147 | * Opens the camera device. |
| 1148 | * |
| 1149 | * @return Whether the camera was opened successfully. |
| 1150 | */ |
| 1151 | private boolean prepareCamera() { |
| 1152 | // We need to check whether the activity is paused before long |
| 1153 | // operations to ensure that onPause() can be done ASAP. |
| 1154 | mCameraDevice = CameraUtil.openCamera( |
| 1155 | mActivity, mCameraId, mHandler, |
| 1156 | mActivity.getCameraOpenErrorCallback()); |
| 1157 | if (mCameraDevice == null) { |
| 1158 | Log.e(TAG, "Failed to open camera:" + mCameraId); |
| 1159 | return false; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1160 | } |
Angus Kong | 4f795b8 | 2013-09-16 14:25:35 -0700 | [diff] [blame] | 1161 | mParameters = mCameraDevice.getParameters(); |
| 1162 | |
| 1163 | initializeCapabilities(); |
| 1164 | if (mFocusManager == null) initializeFocusManager(); |
| 1165 | setCameraParameters(UPDATE_PARAM_ALL); |
| 1166 | mHandler.sendEmptyMessage(CAMERA_OPEN_DONE); |
| 1167 | mCameraPreviewParamsReady = true; |
| 1168 | startPreview(); |
| 1169 | mOnResumeTime = SystemClock.uptimeMillis(); |
| 1170 | checkDisplayRotation(); |
| 1171 | return true; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1175 | @Override |
| 1176 | public void onResumeAfterSuper() { |
| 1177 | if (mOpenCameraFail || mCameraDisabled) return; |
| 1178 | |
| 1179 | mJpegPictureCallbackTime = 0; |
| 1180 | mZoomValue = 0; |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1181 | resetExposureCompensation(); |
Angus Kong | 4f795b8 | 2013-09-16 14:25:35 -0700 | [diff] [blame] | 1182 | if (!prepareCamera()) { |
| 1183 | // Camera failure. |
| 1184 | return; |
| 1185 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1186 | |
| 1187 | // If first time initialization is not finished, put it in the |
| 1188 | // message queue. |
| 1189 | if (!mFirstTimeInitialized) { |
| 1190 | mHandler.sendEmptyMessage(FIRST_TIME_INIT); |
| 1191 | } else { |
| 1192 | initializeSecondTime(); |
| 1193 | } |
| 1194 | keepScreenOnAwhile(); |
| 1195 | |
Bobby Georgescu | 0a7dd57 | 2013-03-12 22:45:17 -0700 | [diff] [blame] | 1196 | UsageStatistics.onContentViewChanged( |
| 1197 | UsageStatistics.COMPONENT_CAMERA, "PhotoModule"); |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1198 | |
| 1199 | Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); |
| 1200 | if (gsensor != null) { |
| 1201 | mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL); |
| 1202 | } |
| 1203 | |
| 1204 | Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); |
| 1205 | if (msensor != null) { |
| 1206 | mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_NORMAL); |
| 1207 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1210 | @Override |
| 1211 | public void onPauseBeforeSuper() { |
| 1212 | mPaused = true; |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1213 | Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); |
| 1214 | if (gsensor != null) { |
| 1215 | mSensorManager.unregisterListener(this, gsensor); |
| 1216 | } |
| 1217 | |
| 1218 | Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); |
| 1219 | if (msensor != null) { |
| 1220 | mSensorManager.unregisterListener(this, msensor); |
| 1221 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | @Override |
| 1225 | public void onPauseAfterSuper() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1226 | // When camera is started from secure lock screen for the first time |
| 1227 | // after screen on, the activity gets onCreate->onResume->onPause->onResume. |
| 1228 | // To reduce the latency, keep the camera for a short time so it does |
| 1229 | // not need to be opened again. |
| 1230 | if (mCameraDevice != null && mActivity.isSecureCamera() |
Angus Kong | 6a8e8a1 | 2013-07-19 14:55:07 -0700 | [diff] [blame] | 1231 | && CameraActivity.isFirstStartAfterScreenOn()) { |
| 1232 | CameraActivity.resetFirstStartAfterScreenOn(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1233 | CameraHolder.instance().keep(KEEP_CAMERA_TIMEOUT); |
| 1234 | } |
| 1235 | // Reset the focus first. Camera CTS does not guarantee that |
| 1236 | // cancelAutoFocus is allowed after preview stops. |
| 1237 | if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) { |
| 1238 | mCameraDevice.cancelAutoFocus(); |
| 1239 | } |
| 1240 | stopPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1241 | |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 1242 | mNamedImages = null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1243 | |
| 1244 | if (mLocationManager != null) mLocationManager.recordLocation(false); |
| 1245 | |
| 1246 | // If we are in an image capture intent and has taken |
| 1247 | // a picture, we just clear it in onPause. |
| 1248 | mJpegImageData = null; |
| 1249 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1250 | // Remove the messages and runnables in the queue. |
| 1251 | mHandler.removeCallbacksAndMessages(null); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1252 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1253 | closeCamera(); |
| 1254 | |
| 1255 | resetScreenOn(); |
| 1256 | mUI.onPause(); |
| 1257 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1258 | mPendingSwitchCameraId = -1; |
| 1259 | if (mFocusManager != null) mFocusManager.removeMessages(); |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 1260 | MediaSaveService s = mActivity.getMediaSaveService(); |
| 1261 | if (s != null) { |
| 1262 | s.setListener(null); |
| 1263 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1266 | /** |
| 1267 | * The focus manager is the first UI related element to get initialized, |
| 1268 | * and it requires the RenderOverlay, so initialize it here |
| 1269 | */ |
| 1270 | private void initializeFocusManager() { |
| 1271 | // Create FocusManager object. startPreview needs it. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1272 | // if mFocusManager not null, reuse it |
| 1273 | // otherwise create a new instance |
| 1274 | if (mFocusManager != null) { |
| 1275 | mFocusManager.removeMessages(); |
| 1276 | } else { |
| 1277 | CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId]; |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 1278 | mMirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1279 | String[] defaultFocusModes = mActivity.getResources().getStringArray( |
| 1280 | R.array.pref_camera_focusmode_default_array); |
| 1281 | mFocusManager = new FocusOverlayManager(mPreferences, defaultFocusModes, |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 1282 | mInitialParams, this, mMirror, |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1283 | mActivity.getMainLooper(), mUI); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1287 | @Override |
| 1288 | public void onConfigurationChanged(Configuration newConfig) { |
| 1289 | Log.v(TAG, "onConfigurationChanged"); |
| 1290 | setDisplayOrientation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | @Override |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1294 | public void updateCameraOrientation() { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1295 | if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1296 | setDisplayOrientation(); |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1301 | public void onActivityResult( |
| 1302 | int requestCode, int resultCode, Intent data) { |
| 1303 | switch (requestCode) { |
| 1304 | case REQUEST_CROP: { |
| 1305 | Intent intent = new Intent(); |
| 1306 | if (data != null) { |
| 1307 | Bundle extras = data.getExtras(); |
| 1308 | if (extras != null) { |
| 1309 | intent.putExtras(extras); |
| 1310 | } |
| 1311 | } |
| 1312 | mActivity.setResultEx(resultCode, intent); |
| 1313 | mActivity.finish(); |
| 1314 | |
| 1315 | File path = mActivity.getFileStreamPath(sTempCropFilename); |
| 1316 | path.delete(); |
| 1317 | |
| 1318 | break; |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | private boolean canTakePicture() { |
Angus Kong | 2dcc0a9 | 2013-09-25 13:00:08 -0700 | [diff] [blame] | 1324 | return isCameraIdle() && (mActivity.getStorageSpaceBytes() > Storage.LOW_STORAGE_THRESHOLD_BYTES); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | @Override |
| 1328 | public void autoFocus() { |
| 1329 | mFocusStartTime = System.currentTimeMillis(); |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1330 | mCameraDevice.autoFocus(mHandler, mAutoFocusCallback); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1331 | setCameraState(FOCUSING); |
| 1332 | } |
| 1333 | |
| 1334 | @Override |
| 1335 | public void cancelAutoFocus() { |
| 1336 | mCameraDevice.cancelAutoFocus(); |
| 1337 | setCameraState(IDLE); |
| 1338 | setCameraParameters(UPDATE_PARAM_PREFERENCE); |
| 1339 | } |
| 1340 | |
| 1341 | // Preview area is touched. Handle touch focus. |
| 1342 | @Override |
| 1343 | public void onSingleTapUp(View view, int x, int y) { |
| 1344 | if (mPaused || mCameraDevice == null || !mFirstTimeInitialized |
| 1345 | || mCameraState == SNAPSHOT_IN_PROGRESS |
| 1346 | || mCameraState == SWITCHING_CAMERA |
| 1347 | || mCameraState == PREVIEW_STOPPED) { |
| 1348 | return; |
| 1349 | } |
| 1350 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1351 | // Check if metering area or focus area is supported. |
| 1352 | if (!mFocusAreaSupported && !mMeteringAreaSupported) return; |
| 1353 | mFocusManager.onSingleTapUp(x, y); |
| 1354 | } |
| 1355 | |
| 1356 | @Override |
| 1357 | public boolean onBackPressed() { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1358 | return mUI.onBackPressed(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | @Override |
| 1362 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 1363 | switch (keyCode) { |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1364 | case KeyEvent.KEYCODE_VOLUME_UP: |
| 1365 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
| 1366 | case KeyEvent.KEYCODE_FOCUS: |
| 1367 | if (/*TODO: mActivity.isInCameraApp() &&*/ mFirstTimeInitialized) { |
| 1368 | if (event.getRepeatCount() == 0) { |
| 1369 | onShutterButtonFocus(true); |
| 1370 | } |
| 1371 | return true; |
| 1372 | } |
| 1373 | return false; |
| 1374 | case KeyEvent.KEYCODE_CAMERA: |
| 1375 | if (mFirstTimeInitialized && event.getRepeatCount() == 0) { |
| 1376 | onShutterButtonClick(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1377 | } |
| 1378 | return true; |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1379 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 1380 | // If we get a dpad center event without any focused view, move |
| 1381 | // the focus to the shutter button and press it. |
| 1382 | if (mFirstTimeInitialized && event.getRepeatCount() == 0) { |
| 1383 | // Start auto-focus immediately to reduce shutter lag. After |
| 1384 | // the shutter button gets the focus, onShutterButtonFocus() |
| 1385 | // will be called again but it is fine. |
| 1386 | onShutterButtonFocus(true); |
| 1387 | mUI.pressShutterButton(); |
| 1388 | } |
| 1389 | return true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1390 | } |
| 1391 | return false; |
| 1392 | } |
| 1393 | |
| 1394 | @Override |
| 1395 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 1396 | switch (keyCode) { |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1397 | case KeyEvent.KEYCODE_VOLUME_UP: |
| 1398 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
| 1399 | if (/*mActivity.isInCameraApp() && */ mFirstTimeInitialized) { |
| 1400 | onShutterButtonClick(); |
| 1401 | return true; |
| 1402 | } |
| 1403 | return false; |
| 1404 | case KeyEvent.KEYCODE_FOCUS: |
| 1405 | if (mFirstTimeInitialized) { |
| 1406 | onShutterButtonFocus(false); |
| 1407 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1408 | return true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1409 | } |
| 1410 | return false; |
| 1411 | } |
| 1412 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1413 | private void closeCamera() { |
| 1414 | if (mCameraDevice != null) { |
| 1415 | mCameraDevice.setZoomChangeListener(null); |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 1416 | mCameraDevice.setFaceDetectionCallback(null, null); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1417 | mCameraDevice.setErrorCallback(null); |
| 1418 | CameraHolder.instance().release(); |
| 1419 | mFaceDetectionStarted = false; |
| 1420 | mCameraDevice = null; |
| 1421 | setCameraState(PREVIEW_STOPPED); |
| 1422 | mFocusManager.onCameraReleased(); |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | private void setDisplayOrientation() { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1427 | mDisplayRotation = CameraUtil.getDisplayRotation(mActivity); |
| 1428 | mDisplayOrientation = CameraUtil.getDisplayOrientation(mDisplayRotation, mCameraId); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1429 | mCameraDisplayOrientation = mDisplayOrientation; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1430 | mUI.setDisplayOrientation(mDisplayOrientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1431 | if (mFocusManager != null) { |
| 1432 | mFocusManager.setDisplayOrientation(mDisplayOrientation); |
| 1433 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1434 | // Change the camera display orientation |
| 1435 | if (mCameraDevice != null) { |
| 1436 | mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation); |
| 1437 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
Sascha Haeberling | ddef779 | 2013-08-13 14:41:10 -0700 | [diff] [blame] | 1440 | /** Only called by UI thread. */ |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1441 | private void setupPreview() { |
| 1442 | mFocusManager.resetTouchFocus(); |
| 1443 | startPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1444 | } |
| 1445 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1446 | // This can only be called by UI Thread. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1447 | private void startPreview() { |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1448 | if (mPaused) { |
| 1449 | return; |
| 1450 | } |
| 1451 | SurfaceTexture st = mUI.getSurfaceTexture(); |
| 1452 | if (st == null) { |
| 1453 | Log.w(TAG, "startPreview: surfaceTexture is not ready."); |
| 1454 | return; |
| 1455 | } |
| 1456 | if (!mCameraPreviewParamsReady) { |
| 1457 | Log.w(TAG, "startPreview: parameters for preview is not ready."); |
| 1458 | return; |
| 1459 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1460 | mCameraDevice.setErrorCallback(mErrorCallback); |
| 1461 | |
| 1462 | // ICS camera frameworks has a bug. Face detection state is not cleared |
| 1463 | // after taking a picture. Stop the preview to work around it. The bug |
| 1464 | // was fixed in JB. |
| 1465 | if (mCameraState != PREVIEW_STOPPED) stopPreview(); |
| 1466 | |
| 1467 | setDisplayOrientation(); |
| 1468 | |
| 1469 | if (!mSnapshotOnIdle) { |
| 1470 | // If the focus mode is continuous autofocus, call cancelAutoFocus to |
| 1471 | // resume it because it may have been paused by autoFocus call. |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1472 | if (CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE.equals(mFocusManager.getFocusMode())) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1473 | mCameraDevice.cancelAutoFocus(); |
| 1474 | } |
| 1475 | mFocusManager.setAeAwbLock(false); // Unlock AE and AWB. |
| 1476 | } |
| 1477 | setCameraParameters(UPDATE_PARAM_ALL); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1478 | // Let UI set its expected aspect ratio |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1479 | mCameraDevice.setPreviewTexture(st); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1480 | |
| 1481 | Log.v(TAG, "startPreview"); |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1482 | mCameraDevice.startPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1483 | mFocusManager.onPreviewStarted(); |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1484 | onPreviewStarted(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1485 | |
| 1486 | if (mSnapshotOnIdle) { |
| 1487 | mHandler.post(mDoSnapRunnable); |
| 1488 | } |
| 1489 | } |
| 1490 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1491 | @Override |
| 1492 | public void stopPreview() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1493 | if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) { |
| 1494 | Log.v(TAG, "stopPreview"); |
| 1495 | mCameraDevice.stopPreview(); |
| 1496 | mFaceDetectionStarted = false; |
| 1497 | } |
| 1498 | setCameraState(PREVIEW_STOPPED); |
| 1499 | if (mFocusManager != null) mFocusManager.onPreviewStopped(); |
| 1500 | } |
| 1501 | |
| 1502 | @SuppressWarnings("deprecation") |
| 1503 | private void updateCameraParametersInitialize() { |
| 1504 | // Reset preview frame rate to the maximum because it may be lowered by |
| 1505 | // video camera application. |
ztenghui | 16a3520 | 2013-09-23 11:35:36 -0700 | [diff] [blame] | 1506 | int[] fpsRange = CameraUtil.getPhotoPreviewFpsRange(mParameters); |
| 1507 | if (fpsRange != null && fpsRange.length > 0) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1508 | mParameters.setPreviewFpsRange( |
| 1509 | fpsRange[Parameters.PREVIEW_FPS_MIN_INDEX], |
| 1510 | fpsRange[Parameters.PREVIEW_FPS_MAX_INDEX]); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1511 | } |
| 1512 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1513 | mParameters.set(CameraUtil.RECORDING_HINT, CameraUtil.FALSE); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1514 | |
| 1515 | // Disable video stabilization. Convenience methods not available in API |
| 1516 | // level <= 14 |
| 1517 | String vstabSupported = mParameters.get("video-stabilization-supported"); |
| 1518 | if ("true".equals(vstabSupported)) { |
| 1519 | mParameters.set("video-stabilization", "false"); |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | private void updateCameraParametersZoom() { |
| 1524 | // Set zoom. |
| 1525 | if (mParameters.isZoomSupported()) { |
| 1526 | mParameters.setZoom(mZoomValue); |
| 1527 | } |
| 1528 | } |
| 1529 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 1530 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1531 | private void setAutoExposureLockIfSupported() { |
| 1532 | if (mAeLockSupported) { |
| 1533 | mParameters.setAutoExposureLock(mFocusManager.getAeAwbLock()); |
| 1534 | } |
| 1535 | } |
| 1536 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 1537 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1538 | private void setAutoWhiteBalanceLockIfSupported() { |
| 1539 | if (mAwbLockSupported) { |
| 1540 | mParameters.setAutoWhiteBalanceLock(mFocusManager.getAeAwbLock()); |
| 1541 | } |
| 1542 | } |
| 1543 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1544 | private void setFocusAreasIfSupported() { |
| 1545 | if (mFocusAreaSupported) { |
| 1546 | mParameters.setFocusAreas(mFocusManager.getFocusAreas()); |
| 1547 | } |
| 1548 | } |
| 1549 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1550 | private void setMeteringAreasIfSupported() { |
| 1551 | if (mMeteringAreaSupported) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1552 | mParameters.setMeteringAreas(mFocusManager.getMeteringAreas()); |
| 1553 | } |
| 1554 | } |
| 1555 | |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 1556 | private boolean updateCameraParametersPreference() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1557 | setAutoExposureLockIfSupported(); |
| 1558 | setAutoWhiteBalanceLockIfSupported(); |
| 1559 | setFocusAreasIfSupported(); |
| 1560 | setMeteringAreasIfSupported(); |
| 1561 | |
| 1562 | // Set picture size. |
| 1563 | String pictureSize = mPreferences.getString( |
| 1564 | CameraSettings.KEY_PICTURE_SIZE, null); |
| 1565 | if (pictureSize == null) { |
| 1566 | CameraSettings.initialCameraPictureSize(mActivity, mParameters); |
| 1567 | } else { |
| 1568 | List<Size> supported = mParameters.getSupportedPictureSizes(); |
| 1569 | CameraSettings.setCameraPictureSize( |
| 1570 | pictureSize, supported, mParameters); |
| 1571 | } |
| 1572 | Size size = mParameters.getPictureSize(); |
| 1573 | |
| 1574 | // Set a preview size that is closest to the viewfinder height and has |
| 1575 | // the right aspect ratio. |
| 1576 | List<Size> sizes = mParameters.getSupportedPreviewSizes(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1577 | Size optimalSize = CameraUtil.getOptimalPreviewSize(mActivity, sizes, |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1578 | (double) size.width / size.height); |
| 1579 | Size original = mParameters.getPreviewSize(); |
| 1580 | if (!original.equals(optimalSize)) { |
| 1581 | mParameters.setPreviewSize(optimalSize.width, optimalSize.height); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1582 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1583 | // Zoom related settings will be changed for different preview |
| 1584 | // sizes, so set and read the parameters to get latest values |
Michael Kolb | 4a4d4ef | 2013-05-30 08:35:26 -0700 | [diff] [blame] | 1585 | if (mHandler.getLooper() == Looper.myLooper()) { |
| 1586 | // On UI thread only, not when camera starts up |
| 1587 | setupPreview(); |
| 1588 | } else { |
| 1589 | mCameraDevice.setParameters(mParameters); |
| 1590 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1591 | mParameters = mCameraDevice.getParameters(); |
| 1592 | } |
| 1593 | Log.v(TAG, "Preview size is " + optimalSize.width + "x" + optimalSize.height); |
| 1594 | |
| 1595 | // Since changing scene mode may change supported values, set scene mode |
| 1596 | // first. HDR is a scene mode. To promote it in UI, it is stored in a |
| 1597 | // separate preference. |
Sascha Haeberling | 98f38bb | 2013-09-24 18:58:34 -0700 | [diff] [blame] | 1598 | String onValue = mActivity.getString(R.string.setting_on_value); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1599 | String hdr = mPreferences.getString(CameraSettings.KEY_CAMERA_HDR, |
| 1600 | mActivity.getString(R.string.pref_camera_hdr_default)); |
Sascha Haeberling | 9bf0fd6 | 2013-10-03 12:10:48 -0700 | [diff] [blame] | 1601 | String hdrPlus = mPreferences.getString(CameraSettings.KEY_CAMERA_HDR_PLUS, |
| 1602 | mActivity.getString(R.string.pref_camera_hdr_plus_default)); |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 1603 | boolean hdrOn = onValue.equals(hdr); |
Sascha Haeberling | 9bf0fd6 | 2013-10-03 12:10:48 -0700 | [diff] [blame] | 1604 | boolean hdrPlusOn = onValue.equals(hdrPlus); |
Sascha Haeberling | 98f38bb | 2013-09-24 18:58:34 -0700 | [diff] [blame] | 1605 | |
| 1606 | boolean doGcamModeSwitch = false; |
Sascha Haeberling | 9bf0fd6 | 2013-10-03 12:10:48 -0700 | [diff] [blame] | 1607 | if (hdrPlusOn && GcamHelper.hasGcamCapture()) { |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 1608 | // Kick off mode switch to gcam. |
| 1609 | doGcamModeSwitch = true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1610 | } else { |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 1611 | if (hdrOn) { |
| 1612 | mSceneMode = CameraUtil.SCENE_MODE_HDR; |
| 1613 | } else { |
| 1614 | mSceneMode = mPreferences.getString( |
| 1615 | CameraSettings.KEY_SCENE_MODE, |
| 1616 | mActivity.getString(R.string.pref_camera_scenemode_default)); |
| 1617 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1618 | } |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1619 | if (CameraUtil.isSupported(mSceneMode, mParameters.getSupportedSceneModes())) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1620 | if (!mParameters.getSceneMode().equals(mSceneMode)) { |
| 1621 | mParameters.setSceneMode(mSceneMode); |
| 1622 | |
| 1623 | // Setting scene mode will change the settings of flash mode, |
| 1624 | // white balance, and focus mode. Here we read back the |
| 1625 | // parameters, so we can know those settings. |
| 1626 | mCameraDevice.setParameters(mParameters); |
| 1627 | mParameters = mCameraDevice.getParameters(); |
| 1628 | } |
| 1629 | } else { |
| 1630 | mSceneMode = mParameters.getSceneMode(); |
| 1631 | if (mSceneMode == null) { |
| 1632 | mSceneMode = Parameters.SCENE_MODE_AUTO; |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | // Set JPEG quality. |
| 1637 | int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId, |
| 1638 | CameraProfile.QUALITY_HIGH); |
| 1639 | mParameters.setJpegQuality(jpegQuality); |
| 1640 | |
| 1641 | // For the following settings, we need to check if the settings are |
| 1642 | // still supported by latest driver, if not, ignore the settings. |
| 1643 | |
| 1644 | // Set exposure compensation |
| 1645 | int value = CameraSettings.readExposure(mPreferences); |
| 1646 | int max = mParameters.getMaxExposureCompensation(); |
| 1647 | int min = mParameters.getMinExposureCompensation(); |
| 1648 | if (value >= min && value <= max) { |
| 1649 | mParameters.setExposureCompensation(value); |
| 1650 | } else { |
| 1651 | Log.w(TAG, "invalid exposure range: " + value); |
| 1652 | } |
| 1653 | |
| 1654 | if (Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) { |
| 1655 | // Set flash mode. |
| 1656 | String flashMode = mPreferences.getString( |
| 1657 | CameraSettings.KEY_FLASH_MODE, |
| 1658 | mActivity.getString(R.string.pref_camera_flashmode_default)); |
| 1659 | List<String> supportedFlash = mParameters.getSupportedFlashModes(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1660 | if (CameraUtil.isSupported(flashMode, supportedFlash)) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1661 | mParameters.setFlashMode(flashMode); |
| 1662 | } else { |
| 1663 | flashMode = mParameters.getFlashMode(); |
| 1664 | if (flashMode == null) { |
| 1665 | flashMode = mActivity.getString( |
| 1666 | R.string.pref_camera_flashmode_no_flash); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | // Set white balance parameter. |
| 1671 | String whiteBalance = mPreferences.getString( |
| 1672 | CameraSettings.KEY_WHITE_BALANCE, |
| 1673 | mActivity.getString(R.string.pref_camera_whitebalance_default)); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1674 | if (CameraUtil.isSupported(whiteBalance, |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1675 | mParameters.getSupportedWhiteBalance())) { |
| 1676 | mParameters.setWhiteBalance(whiteBalance); |
| 1677 | } else { |
| 1678 | whiteBalance = mParameters.getWhiteBalance(); |
| 1679 | if (whiteBalance == null) { |
| 1680 | whiteBalance = Parameters.WHITE_BALANCE_AUTO; |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | // Set focus mode. |
| 1685 | mFocusManager.overrideFocusMode(null); |
| 1686 | mParameters.setFocusMode(mFocusManager.getFocusMode()); |
| 1687 | } else { |
| 1688 | mFocusManager.overrideFocusMode(mParameters.getFocusMode()); |
| 1689 | } |
| 1690 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1691 | if (mContinuousFocusSupported && ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1692 | updateAutoFocusMoveCallback(); |
| 1693 | } |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 1694 | |
| 1695 | return doGcamModeSwitch; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1696 | } |
| 1697 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 1698 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1699 | private void updateAutoFocusMoveCallback() { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1700 | if (mParameters.getFocusMode().equals(CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE)) { |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1701 | mCameraDevice.setAutoFocusMoveCallback(mHandler, |
Angus Kong | 4f795b8 | 2013-09-16 14:25:35 -0700 | [diff] [blame] | 1702 | (CameraAFMoveCallback) mAutoFocusMoveCallback); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1703 | } else { |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1704 | mCameraDevice.setAutoFocusMoveCallback(null, null); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | // We separate the parameters into several subsets, so we can update only |
| 1709 | // the subsets actually need updating. The PREFERENCE set needs extra |
| 1710 | // locking because the preference can be changed from GLThread as well. |
| 1711 | private void setCameraParameters(int updateSet) { |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 1712 | boolean doModeSwitch = false; |
| 1713 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1714 | if ((updateSet & UPDATE_PARAM_INITIALIZE) != 0) { |
| 1715 | updateCameraParametersInitialize(); |
| 1716 | } |
| 1717 | |
| 1718 | if ((updateSet & UPDATE_PARAM_ZOOM) != 0) { |
| 1719 | updateCameraParametersZoom(); |
| 1720 | } |
| 1721 | |
| 1722 | if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) { |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 1723 | doModeSwitch = updateCameraParametersPreference(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | mCameraDevice.setParameters(mParameters); |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 1727 | |
Sascha Haeberling | 9bf0fd6 | 2013-10-03 12:10:48 -0700 | [diff] [blame] | 1728 | // Switch to gcam module if HDR+ was selected |
Angus Kong | 0fb819b | 2013-10-08 13:44:19 -0700 | [diff] [blame] | 1729 | if (doModeSwitch && !mIsImageCaptureIntent) { |
Ruben Brunk | 4601f5d | 2013-09-24 18:35:22 -0700 | [diff] [blame] | 1730 | mActivity.onModuleSelected(ModuleSwitcher.GCAM_MODULE_INDEX); |
| 1731 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | // If the Camera is idle, update the parameters immediately, otherwise |
| 1735 | // accumulate them in mUpdateSet and update later. |
| 1736 | private void setCameraParametersWhenIdle(int additionalUpdateSet) { |
| 1737 | mUpdateSet |= additionalUpdateSet; |
| 1738 | if (mCameraDevice == null) { |
| 1739 | // We will update all the parameters when we open the device, so |
| 1740 | // we don't need to do anything now. |
| 1741 | mUpdateSet = 0; |
| 1742 | return; |
| 1743 | } else if (isCameraIdle()) { |
| 1744 | setCameraParameters(mUpdateSet); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1745 | updateSceneMode(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1746 | mUpdateSet = 0; |
| 1747 | } else { |
| 1748 | if (!mHandler.hasMessages(SET_CAMERA_PARAMETERS_WHEN_IDLE)) { |
| 1749 | mHandler.sendEmptyMessageDelayed( |
| 1750 | SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000); |
| 1751 | } |
| 1752 | } |
| 1753 | } |
| 1754 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1755 | @Override |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1756 | public boolean isCameraIdle() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1757 | return (mCameraState == IDLE) || |
| 1758 | (mCameraState == PREVIEW_STOPPED) || |
| 1759 | ((mFocusManager != null) && mFocusManager.isFocusCompleted() |
| 1760 | && (mCameraState != SWITCHING_CAMERA)); |
| 1761 | } |
| 1762 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1763 | @Override |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1764 | public boolean isImageCaptureIntent() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1765 | String action = mActivity.getIntent().getAction(); |
| 1766 | return (MediaStore.ACTION_IMAGE_CAPTURE.equals(action) |
Angus Kong | 6a8e8a1 | 2013-07-19 14:55:07 -0700 | [diff] [blame] | 1767 | || CameraActivity.ACTION_IMAGE_CAPTURE_SECURE.equals(action)); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | private void setupCaptureParams() { |
| 1771 | Bundle myExtras = mActivity.getIntent().getExtras(); |
| 1772 | if (myExtras != null) { |
| 1773 | mSaveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT); |
| 1774 | mCropValue = myExtras.getString("crop"); |
| 1775 | } |
| 1776 | } |
| 1777 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1778 | @Override |
| 1779 | public void onSharedPreferenceChanged() { |
| 1780 | // ignore the events after "onPause()" |
| 1781 | if (mPaused) return; |
| 1782 | |
| 1783 | boolean recordLocation = RecordLocationPreference.get( |
| 1784 | mPreferences, mContentResolver); |
| 1785 | mLocationManager.recordLocation(recordLocation); |
| 1786 | |
| 1787 | setCameraParametersWhenIdle(UPDATE_PARAM_PREFERENCE); |
Michael Kolb | eb8adc1 | 2013-04-26 11:09:29 -0700 | [diff] [blame] | 1788 | mUI.updateOnScreenIndicators(mParameters, mPreferenceGroup, mPreferences); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | @Override |
| 1792 | public void onCameraPickerClicked(int cameraId) { |
| 1793 | if (mPaused || mPendingSwitchCameraId != -1) return; |
| 1794 | |
| 1795 | mPendingSwitchCameraId = cameraId; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1796 | |
| 1797 | Log.v(TAG, "Start to switch camera. cameraId=" + cameraId); |
| 1798 | // We need to keep a preview frame for the animation before |
| 1799 | // releasing the camera. This will trigger onPreviewTextureCopied. |
| 1800 | //TODO: Need to animate the camera switch |
| 1801 | switchCamera(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1802 | } |
| 1803 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1804 | // Preview texture has been copied. Now camera can be released and the |
| 1805 | // animation can be started. |
| 1806 | @Override |
| 1807 | public void onPreviewTextureCopied() { |
| 1808 | mHandler.sendEmptyMessage(SWITCH_CAMERA); |
| 1809 | } |
| 1810 | |
| 1811 | @Override |
| 1812 | public void onCaptureTextureCopied() { |
| 1813 | } |
| 1814 | |
| 1815 | @Override |
| 1816 | public void onUserInteraction() { |
Dan Aminzade | 92ae10e | 2013-08-13 14:44:25 -0700 | [diff] [blame] | 1817 | if (!mActivity.isFinishing()) keepScreenOnAwhile(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | private void resetScreenOn() { |
| 1821 | mHandler.removeMessages(CLEAR_SCREEN_DELAY); |
| 1822 | mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 1823 | } |
| 1824 | |
| 1825 | private void keepScreenOnAwhile() { |
| 1826 | mHandler.removeMessages(CLEAR_SCREEN_DELAY); |
| 1827 | mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 1828 | mHandler.sendEmptyMessageDelayed(CLEAR_SCREEN_DELAY, SCREEN_DELAY); |
| 1829 | } |
| 1830 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1831 | @Override |
| 1832 | public void onOverriddenPreferencesClicked() { |
| 1833 | if (mPaused) return; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1834 | mUI.showPreferencesToast(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | private void showTapToFocusToast() { |
| 1838 | // TODO: Use a toast? |
| 1839 | new RotateTextToast(mActivity, R.string.tap_to_focus, 0).show(); |
| 1840 | // Clear the preference. |
| 1841 | Editor editor = mPreferences.edit(); |
| 1842 | editor.putBoolean(CameraSettings.KEY_CAMERA_FIRST_USE_HINT_SHOWN, false); |
| 1843 | editor.apply(); |
| 1844 | } |
| 1845 | |
| 1846 | private void initializeCapabilities() { |
| 1847 | mInitialParams = mCameraDevice.getParameters(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1848 | mFocusAreaSupported = CameraUtil.isFocusAreaSupported(mInitialParams); |
| 1849 | mMeteringAreaSupported = CameraUtil.isMeteringAreaSupported(mInitialParams); |
| 1850 | mAeLockSupported = CameraUtil.isAutoExposureLockSupported(mInitialParams); |
| 1851 | mAwbLockSupported = CameraUtil.isAutoWhiteBalanceLockSupported(mInitialParams); |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 1852 | mContinuousFocusSupported = mInitialParams.getSupportedFocusModes().contains( |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1853 | CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1856 | @Override |
| 1857 | public void onCountDownFinished() { |
| 1858 | mSnapshotOnIdle = false; |
| 1859 | mFocusManager.doSnap(); |
Doris Liu | da50e05 | 2013-02-07 14:36:32 -0800 | [diff] [blame] | 1860 | mFocusManager.onShutterUp(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1861 | } |
| 1862 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1863 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1864 | public void onShowSwitcherPopup() { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1865 | mUI.onShowSwitcherPopup(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1866 | } |
| 1867 | |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 1868 | @Override |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1869 | public int onZoomChanged(int index) { |
| 1870 | // Not useful to change zoom value when the activity is paused. |
| 1871 | if (mPaused) return index; |
| 1872 | mZoomValue = index; |
| 1873 | if (mParameters == null || mCameraDevice == null) return index; |
| 1874 | // Set zoom parameters asynchronously |
| 1875 | mParameters.setZoom(mZoomValue); |
Angus Kong | 36ed867 | 2013-04-15 12:11:15 -0700 | [diff] [blame] | 1876 | mCameraDevice.setParameters(mParameters); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1877 | Parameters p = mCameraDevice.getParameters(); |
| 1878 | if (p != null) return p.getZoom(); |
| 1879 | return index; |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | @Override |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1883 | public int getCameraState() { |
| 1884 | return mCameraState; |
| 1885 | } |
| 1886 | |
| 1887 | @Override |
| 1888 | public void onQueueStatus(boolean full) { |
| 1889 | mUI.enableShutter(!full); |
Angus Kong | ce5480e | 2013-01-29 17:43:48 -0800 | [diff] [blame] | 1890 | } |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 1891 | |
| 1892 | @Override |
| 1893 | public void onMediaSaveServiceConnected(MediaSaveService s) { |
| 1894 | // We set the listener only when both service and shutterbutton |
| 1895 | // are initialized. |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1896 | if (mFirstTimeInitialized) { |
| 1897 | s.setListener(this); |
| 1898 | } |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 1899 | } |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1900 | |
| 1901 | @Override |
| 1902 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 1903 | } |
| 1904 | |
| 1905 | @Override |
| 1906 | public void onSensorChanged(SensorEvent event) { |
| 1907 | int type = event.sensor.getType(); |
| 1908 | float[] data; |
| 1909 | if (type == Sensor.TYPE_ACCELEROMETER) { |
| 1910 | data = mGData; |
| 1911 | } else if (type == Sensor.TYPE_MAGNETIC_FIELD) { |
| 1912 | data = mMData; |
| 1913 | } else { |
| 1914 | // we should not be here. |
| 1915 | return; |
| 1916 | } |
| 1917 | for (int i = 0; i < 3 ; i++) { |
| 1918 | data[i] = event.values[i]; |
| 1919 | } |
| 1920 | float[] orientation = new float[3]; |
| 1921 | SensorManager.getRotationMatrix(mR, null, mGData, mMData); |
| 1922 | SensorManager.getOrientation(mR, orientation); |
| 1923 | mHeading = (int) (orientation[0] * 180f / Math.PI) % 360; |
| 1924 | if (mHeading < 0) { |
| 1925 | mHeading += 360; |
| 1926 | } |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1927 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1928 | |
| 1929 | @Override |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1930 | public void onPreviewFocusChanged(boolean previewFocused) { |
| 1931 | mUI.onPreviewFocusChanged(previewFocused); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
Ruben Brunk | d217ed0 | 2013-10-08 23:31:13 -0700 | [diff] [blame] | 1934 | // For debugging only. |
| 1935 | public void setDebugUri(Uri uri) { |
| 1936 | mDebugUri = uri; |
| 1937 | } |
| 1938 | |
| 1939 | // For debugging only. |
| 1940 | private void saveToDebugUri(byte[] data) { |
| 1941 | if (mDebugUri != null) { |
| 1942 | OutputStream outputStream = null; |
| 1943 | try { |
| 1944 | outputStream = mContentResolver.openOutputStream(mDebugUri); |
| 1945 | outputStream.write(data); |
| 1946 | outputStream.close(); |
| 1947 | } catch (IOException e) { |
| 1948 | Log.e(TAG, "Exception while writing debug jpeg file", e); |
| 1949 | } finally { |
| 1950 | CameraUtil.closeSilently(outputStream); |
| 1951 | } |
| 1952 | } |
| 1953 | } |
| 1954 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1955 | /* Below is no longer needed, except to get rid of compile error |
| 1956 | * TODO: Remove these |
| 1957 | */ |
| 1958 | |
| 1959 | // TODO: Delete this function after old camera code is removed |
| 1960 | @Override |
| 1961 | public void onRestorePreferencesClicked() {} |
| 1962 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1963 | } |