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