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