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