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