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