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; |
| 21 | import android.content.ActivityNotFoundException; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.ContentValues; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 28 | import android.graphics.Bitmap; |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 29 | import android.graphics.Point; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 30 | import android.graphics.SurfaceTexture; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 31 | import android.location.Location; |
Marco Nelissen | 20694b2 | 2013-10-29 15:27:24 -0700 | [diff] [blame] | 32 | import android.media.AudioManager; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 33 | import android.media.CamcorderProfile; |
| 34 | import android.media.CameraProfile; |
| 35 | import android.media.MediaRecorder; |
| 36 | import android.net.Uri; |
| 37 | import android.os.Build; |
| 38 | import android.os.Bundle; |
| 39 | import android.os.Handler; |
| 40 | import android.os.Message; |
| 41 | import android.os.ParcelFileDescriptor; |
| 42 | import android.os.SystemClock; |
| 43 | import android.provider.MediaStore; |
Ruben Brunk | 1600796 | 2013-04-19 15:27:57 -0700 | [diff] [blame] | 44 | import android.provider.MediaStore.MediaColumns; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 45 | import android.provider.MediaStore.Video; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 46 | import android.view.KeyEvent; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 47 | import android.view.OrientationEventListener; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 48 | import android.view.View; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 49 | import android.widget.Toast; |
| 50 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 51 | import com.android.camera.app.AppController; |
Spike Sprague | 39f8a76 | 2014-01-13 14:22:18 -0800 | [diff] [blame] | 52 | import com.android.camera.app.CameraAppUI; |
Kevin Gabayan | ffbc43c | 2013-12-09 11:41:50 -0800 | [diff] [blame] | 53 | import com.android.camera.app.LocationManager; |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 54 | import com.android.camera.app.MediaSaver; |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 55 | import com.android.camera.app.MemoryManager; |
| 56 | import com.android.camera.app.MemoryManager.MemoryListener; |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame] | 57 | import com.android.camera.debug.Log; |
ztenghui | a16e7b5 | 2013-08-23 11:47:56 -0700 | [diff] [blame] | 58 | import com.android.camera.exif.ExifInterface; |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 59 | import com.android.camera.hardware.HardwareSpec; |
| 60 | import com.android.camera.hardware.HardwareSpecImpl; |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 61 | import com.android.camera.module.ModuleController; |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 62 | import com.android.camera.settings.Keys; |
Erin Dahlgren | bd3da26 | 2013-12-02 11:48:13 -0800 | [diff] [blame] | 63 | import com.android.camera.settings.SettingsManager; |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 64 | import com.android.camera.settings.SettingsUtil; |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 65 | import com.android.camera.ui.TouchCoordinate; |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 66 | import com.android.camera.util.ApiHelper; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 67 | import com.android.camera.util.CameraUtil; |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 68 | import com.android.camera.util.UsageStatistics; |
| 69 | import com.android.camera2.R; |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 70 | import com.android.ex.camera2.portability.CameraAgent; |
| 71 | import com.android.ex.camera2.portability.CameraAgent.CameraPictureCallback; |
| 72 | import com.android.ex.camera2.portability.CameraAgent.CameraProxy; |
Sascha Haeberling | f15d63e | 2014-07-21 11:31:13 -0700 | [diff] [blame] | 73 | import com.android.ex.camera2.portability.CameraCapabilities; |
Sol Boucher | 43e1813 | 2014-06-19 15:03:18 -0700 | [diff] [blame] | 74 | import com.android.ex.camera2.portability.CameraDeviceInfo.Characteristics; |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 75 | import com.android.ex.camera2.portability.CameraSettings; |
| 76 | import com.android.ex.camera2.portability.Size; |
Seth Raphael | 5e09d01 | 2013-12-18 13:45:03 -0800 | [diff] [blame] | 77 | import com.google.common.logging.eventprotos; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 78 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 79 | import java.io.File; |
| 80 | import java.io.IOException; |
| 81 | import java.text.SimpleDateFormat; |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 82 | import java.util.ArrayList; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 83 | import java.util.Date; |
| 84 | import java.util.Iterator; |
| 85 | import java.util.List; |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 86 | import java.util.Set; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 87 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 88 | public class VideoModule extends CameraModule |
| 89 | implements ModuleController, |
| 90 | VideoController, |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 91 | MemoryListener, |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 92 | MediaRecorder.OnErrorListener, |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 93 | MediaRecorder.OnInfoListener, FocusOverlayManager.Listener { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 94 | |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 95 | private static final String VIDEO_MODULE_STRING_ID = "VideoModule"; |
| 96 | |
| 97 | private static final Log.Tag TAG = new Log.Tag(VIDEO_MODULE_STRING_ID); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 98 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 99 | // Messages defined for the UI thread handler. |
| 100 | private static final int MSG_CHECK_DISPLAY_ROTATION = 4; |
| 101 | private static final int MSG_UPDATE_RECORD_TIME = 5; |
| 102 | private static final int MSG_ENABLE_SHUTTER_BUTTON = 6; |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 103 | private static final int MSG_SWITCH_CAMERA = 8; |
| 104 | private static final int MSG_SWITCH_CAMERA_START_ANIMATION = 9; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 105 | |
| 106 | private static final long SHUTTER_BUTTON_TIMEOUT = 500L; // 500ms |
| 107 | |
| 108 | /** |
| 109 | * An unpublished intent flag requesting to start recording straight away |
| 110 | * and return as soon as recording is stopped. |
| 111 | * TODO: consider publishing by moving into MediaStore. |
| 112 | */ |
| 113 | private static final String EXTRA_QUICK_CAPTURE = |
| 114 | "android.intent.extra.quickCapture"; |
| 115 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 116 | // module fields |
| 117 | private CameraActivity mActivity; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 118 | private boolean mPaused; |
Spike Sprague | 51c877c | 2014-02-18 11:14:12 -0800 | [diff] [blame] | 119 | |
| 120 | // if, during and intent capture, the activity is paused (e.g. when app switching or reviewing a |
| 121 | // shot video), we don't want the bottom bar intent ui to reset to the capture button |
| 122 | private boolean mDontResetIntentUiOnResume; |
| 123 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 124 | private int mCameraId; |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 125 | private CameraSettings mCameraSettings; |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 126 | private CameraCapabilities mCameraCapabilities; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 127 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 128 | private boolean mIsInReviewMode; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 129 | private boolean mSnapshotInProgress = false; |
| 130 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 131 | private final CameraErrorCallback mErrorCallback = new CameraErrorCallback(); |
| 132 | |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 133 | // Preference must be read before starting preview. We check this before starting |
| 134 | // preview. |
| 135 | private boolean mPreferenceRead; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 136 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 137 | private boolean mIsVideoCaptureIntent; |
| 138 | private boolean mQuickCapture; |
| 139 | |
| 140 | private MediaRecorder mMediaRecorder; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 141 | |
| 142 | private boolean mSwitchingCamera; |
| 143 | private boolean mMediaRecorderRecording = false; |
| 144 | private long mRecordingStartTime; |
| 145 | private boolean mRecordingTimeCountsDown = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 146 | private long mOnResumeTime; |
| 147 | // The video file that the hardware camera is about to record into |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 148 | // (or is recording into. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 149 | private String mVideoFilename; |
| 150 | private ParcelFileDescriptor mVideoFileDescriptor; |
| 151 | |
| 152 | // The video file that has already been recorded, and that is being |
| 153 | // examined by the user. |
| 154 | private String mCurrentVideoFilename; |
| 155 | private Uri mCurrentVideoUri; |
ztenghui | 70bd024 | 2013-10-14 11:15:44 -0700 | [diff] [blame] | 156 | private boolean mCurrentVideoUriFromMediaSaved; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 157 | private ContentValues mCurrentVideoValues; |
| 158 | |
| 159 | private CamcorderProfile mProfile; |
| 160 | |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 161 | // The video duration limit. 0 means no limit. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 162 | private int mMaxVideoDurationInMs; |
| 163 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 164 | boolean mPreviewing = false; // True if preview is started. |
| 165 | // The display rotation in degrees. This is only valid when mPreviewing is |
| 166 | // true. |
| 167 | private int mDisplayRotation; |
| 168 | private int mCameraDisplayOrientation; |
Doris Liu | 2b906b8 | 2013-12-10 16:34:08 -0800 | [diff] [blame] | 169 | private AppController mAppController; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 170 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 171 | private int mDesiredPreviewWidth; |
| 172 | private int mDesiredPreviewHeight; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 173 | private ContentResolver mContentResolver; |
| 174 | |
| 175 | private LocationManager mLocationManager; |
| 176 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 177 | private int mPendingSwitchCameraId; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 178 | private final Handler mHandler = new MainHandler(); |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 179 | private VideoUI mUI; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 180 | private CameraProxy mCameraDevice; |
| 181 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 182 | // The degrees of the device rotated clockwise from its natural orientation. |
| 183 | private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; |
| 184 | |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 185 | private float mZoomValue; // The current zoom ratio. |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 186 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 187 | private final MediaSaver.OnMediaSavedListener mOnVideoSavedListener = |
| 188 | new MediaSaver.OnMediaSavedListener() { |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public void onMediaSaved(Uri uri) { |
| 191 | if (uri != null) { |
Doris Liu | 2a7f44c | 2013-08-12 15:18:53 -0700 | [diff] [blame] | 192 | mCurrentVideoUri = uri; |
ztenghui | 70bd024 | 2013-10-14 11:15:44 -0700 | [diff] [blame] | 193 | mCurrentVideoUriFromMediaSaved = true; |
Doris Liu | 2a7f44c | 2013-08-12 15:18:53 -0700 | [diff] [blame] | 194 | onVideoSaved(); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 195 | mActivity.notifyNewMedia(uri); |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | }; |
| 199 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 200 | private final MediaSaver.OnMediaSavedListener mOnPhotoSavedListener = |
| 201 | new MediaSaver.OnMediaSavedListener() { |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 202 | @Override |
| 203 | public void onMediaSaved(Uri uri) { |
| 204 | if (uri != null) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 205 | mActivity.notifyNewMedia(uri); |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | }; |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 209 | private FocusOverlayManager mFocusManager; |
| 210 | private boolean mMirror; |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 211 | private boolean mFocusAreaSupported; |
| 212 | private boolean mMeteringAreaSupported; |
| 213 | |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 214 | private final CameraAgent.CameraAFCallback mAutoFocusCallback = |
| 215 | new CameraAgent.CameraAFCallback() { |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 216 | @Override |
| 217 | public void onAutoFocus(boolean focused, CameraProxy camera) { |
| 218 | if (mPaused) { |
| 219 | return; |
| 220 | } |
| 221 | mFocusManager.onAutoFocus(focused, false); |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | private final Object mAutoFocusMoveCallback = |
| 226 | ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 227 | ? new CameraAgent.CameraAFMoveCallback() { |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 228 | @Override |
| 229 | public void onAutoFocusMoving(boolean moving, CameraProxy camera) { |
Andy Huibers | c0fe0b6 | 2014-08-25 22:25:49 -0700 | [diff] [blame] | 230 | // mFocusManager.onAutoFocusMoving(moving) not called because UI |
| 231 | // not compatible with vertical video hint UI. |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 232 | } |
| 233 | } : null; |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 234 | |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 235 | /** |
| 236 | * This Handler is used to post message back onto the main thread of the |
| 237 | * application. |
| 238 | */ |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 239 | private class MainHandler extends Handler { |
| 240 | @Override |
| 241 | public void handleMessage(Message msg) { |
| 242 | switch (msg.what) { |
| 243 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 244 | case MSG_ENABLE_SHUTTER_BUTTON: |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 245 | mAppController.setShutterEnabled(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 246 | break; |
| 247 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 248 | case MSG_UPDATE_RECORD_TIME: { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 249 | updateRecordingTime(); |
| 250 | break; |
| 251 | } |
| 252 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 253 | case MSG_CHECK_DISPLAY_ROTATION: { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 254 | // Restart the preview if display rotation has changed. |
| 255 | // Sometimes this happens when the device is held upside |
| 256 | // down and camera app is opened. Rotation animation will |
| 257 | // take some time and the rotation value we have got may be |
| 258 | // wrong. Framework does not have a callback for this now. |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 259 | if ((CameraUtil.getDisplayRotation(mActivity) != mDisplayRotation) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 260 | && !mMediaRecorderRecording && !mSwitchingCamera) { |
| 261 | startPreview(); |
| 262 | } |
| 263 | if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) { |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 264 | mHandler.sendEmptyMessageDelayed(MSG_CHECK_DISPLAY_ROTATION, 100); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 265 | } |
| 266 | break; |
| 267 | } |
| 268 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 269 | case MSG_SWITCH_CAMERA: { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 270 | switchCamera(); |
| 271 | break; |
| 272 | } |
| 273 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 274 | case MSG_SWITCH_CAMERA_START_ANIMATION: { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 275 | //TODO: |
| 276 | //((CameraScreenNail) mActivity.mCameraScreenNail).animateSwitchCamera(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 277 | |
| 278 | // Enable all camera controls. |
| 279 | mSwitchingCamera = false; |
| 280 | break; |
| 281 | } |
| 282 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 283 | default: |
| 284 | Log.v(TAG, "Unhandled message: " + msg.what); |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private BroadcastReceiver mReceiver = null; |
| 291 | |
| 292 | private class MyBroadcastReceiver extends BroadcastReceiver { |
| 293 | @Override |
| 294 | public void onReceive(Context context, Intent intent) { |
| 295 | String action = intent.getAction(); |
| 296 | if (action.equals(Intent.ACTION_MEDIA_EJECT)) { |
| 297 | stopVideoRecording(); |
| 298 | } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) { |
| 299 | Toast.makeText(mActivity, |
| 300 | mActivity.getResources().getString(R.string.wait), Toast.LENGTH_LONG).show(); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
Spike Sprague | 41f6800 | 2014-01-24 16:59:25 -0800 | [diff] [blame] | 305 | private int mShutterIconId; |
| 306 | |
| 307 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 308 | /** |
| 309 | * Construct a new video module. |
| 310 | */ |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 311 | public VideoModule(AppController app) { |
| 312 | super(app); |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Spike Sprague | 159e6e9 | 2014-05-27 18:26:30 -0700 | [diff] [blame] | 315 | @Override |
| 316 | public String getPeekAccessibilityString() { |
| 317 | return mAppController.getAndroidContext() |
| 318 | .getResources().getString(R.string.video_accessibility_peek); |
| 319 | } |
| 320 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 321 | private String createName(long dateTaken) { |
| 322 | Date date = new Date(dateTaken); |
| 323 | SimpleDateFormat dateFormat = new SimpleDateFormat( |
| 324 | mActivity.getString(R.string.video_file_name_format)); |
| 325 | |
| 326 | return dateFormat.format(date); |
| 327 | } |
| 328 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 329 | @Override |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 330 | public String getModuleStringIdentifier() { |
| 331 | return VIDEO_MODULE_STRING_ID; |
| 332 | } |
| 333 | |
| 334 | @Override |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 335 | public void init(CameraActivity activity, boolean isSecureCamera, boolean isCaptureIntent) { |
| 336 | mActivity = activity; |
| 337 | // TODO: Need to look at the controller interface to see if we can get |
| 338 | // rid of passing in the activity directly. |
| 339 | mAppController = mActivity; |
Spike Sprague | 3f67379 | 2014-04-14 16:16:27 -0700 | [diff] [blame] | 340 | |
Spike Sprague | 48a3806 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 341 | mActivity.updateStorageSpaceAndHint(null); |
Spike Sprague | 3f67379 | 2014-04-14 16:16:27 -0700 | [diff] [blame] | 342 | |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 343 | mUI = new VideoUI(mActivity, this, mActivity.getModuleLayoutRoot()); |
| 344 | mActivity.setPreviewStatusListener(mUI); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 345 | |
Erin Dahlgren | bd3da26 | 2013-12-02 11:48:13 -0800 | [diff] [blame] | 346 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 347 | mCameraId = settingsManager.getInteger(mAppController.getModuleScope(), |
| 348 | Keys.KEY_CAMERA_ID); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 349 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 350 | /* |
| 351 | * To reduce startup time, we start the preview in another thread. |
| 352 | * We make sure the preview is started at the end of onCreate. |
| 353 | */ |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 354 | requestCamera(mCameraId); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 355 | |
| 356 | mContentResolver = mActivity.getContentResolver(); |
| 357 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 358 | // Surface texture is from camera screen nail and startPreview needs it. |
| 359 | // This must be done before startPreview. |
| 360 | mIsVideoCaptureIntent = isVideoCaptureIntent(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 361 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 362 | mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false); |
Erin Dahlgren | 21c21a6 | 2013-11-19 16:37:38 -0800 | [diff] [blame] | 363 | mLocationManager = mActivity.getLocationManager(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 364 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 365 | mUI.setOrientationIndicator(0, false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 366 | setDisplayOrientation(); |
| 367 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 368 | mPendingSwitchCameraId = -1; |
Spike Sprague | 41f6800 | 2014-01-24 16:59:25 -0800 | [diff] [blame] | 369 | |
| 370 | mShutterIconId = CameraUtil.getCameraShutterIconId( |
| 371 | mAppController.getCurrentModuleIndex(), mAppController.getAndroidContext()); |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Erin Dahlgren | 4efa8b5 | 2013-12-17 18:31:35 -0800 | [diff] [blame] | 374 | @Override |
| 375 | public boolean isUsingBottomBar() { |
| 376 | return true; |
| 377 | } |
| 378 | |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 379 | private void initializeControlByIntent() { |
| 380 | if (isVideoCaptureIntent()) { |
Spike Sprague | 51c877c | 2014-02-18 11:14:12 -0800 | [diff] [blame] | 381 | if (!mDontResetIntentUiOnResume) { |
| 382 | mActivity.getCameraAppUI().transitionToIntentCaptureLayout(); |
| 383 | } |
| 384 | // reset the flag |
| 385 | mDontResetIntentUiOnResume = false; |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 389 | @Override |
| 390 | public void onSingleTapUp(View view, int x, int y) { |
Doris Liu | 057b21a | 2014-04-25 17:35:27 -0700 | [diff] [blame] | 391 | if (mPaused || mCameraDevice == null) { |
| 392 | return; |
| 393 | } |
| 394 | if (mMediaRecorderRecording) { |
| 395 | if (!mSnapshotInProgress) { |
| 396 | takeASnapshot(); |
| 397 | } |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 398 | return; |
| 399 | } |
| 400 | // Check if metering area or focus area is supported. |
| 401 | if (!mFocusAreaSupported && !mMeteringAreaSupported) { |
| 402 | return; |
| 403 | } |
| 404 | // Tap to focus. |
| 405 | mFocusManager.onSingleTapUp(x, y); |
Doris Liu | 3860574 | 2013-08-13 15:01:52 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | private void takeASnapshot() { |
Sascha Haeberling | a514b14 | 2013-08-13 16:10:01 -0700 | [diff] [blame] | 409 | // Only take snapshots if video snapshot is supported by device |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 410 | if(!mCameraCapabilities.supports(CameraCapabilities.Feature.VIDEO_SNAPSHOT)) { |
Doris Liu | 4683673 | 2014-04-29 18:15:55 -0700 | [diff] [blame] | 411 | Log.w(TAG, "Cannot take a video snapshot - not supported by hardware"); |
| 412 | return; |
| 413 | } |
| 414 | if (!mIsVideoCaptureIntent) { |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 415 | if (!mMediaRecorderRecording || mPaused || mSnapshotInProgress |
Doris Liu | 057b21a | 2014-04-25 17:35:27 -0700 | [diff] [blame] | 416 | || !mAppController.isShutterEnabled()) { |
Doris Liu | 3860574 | 2013-08-13 15:01:52 -0700 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
Doris Liu | 3860574 | 2013-08-13 15:01:52 -0700 | [diff] [blame] | 420 | Location loc = mLocationManager.getCurrentLocation(); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 421 | CameraUtil.setGpsParameters(mCameraSettings, loc); |
| 422 | mCameraDevice.applySettings(mCameraSettings); |
Doris Liu | 3860574 | 2013-08-13 15:01:52 -0700 | [diff] [blame] | 423 | |
Doris Liu | 057b21a | 2014-04-25 17:35:27 -0700 | [diff] [blame] | 424 | Log.i(TAG, "Video snapshot start"); |
Doris Liu | 3860574 | 2013-08-13 15:01:52 -0700 | [diff] [blame] | 425 | mCameraDevice.takePicture(mHandler, |
| 426 | null, null, null, new JpegPictureCallback(loc)); |
| 427 | showVideoSnapshotUI(true); |
| 428 | mSnapshotInProgress = true; |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 429 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 432 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
| 433 | private void updateAutoFocusMoveCallback() { |
Sascha Haeberling | 8793eff | 2014-01-15 16:33:59 -0800 | [diff] [blame] | 434 | if (mPaused) { |
| 435 | return; |
| 436 | } |
| 437 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 438 | if (mCameraSettings.getCurrentFocusMode() == CameraCapabilities.FocusMode.CONTINUOUS_PICTURE) { |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 439 | mCameraDevice.setAutoFocusMoveCallback(mHandler, |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 440 | (CameraAgent.CameraAFMoveCallback) mAutoFocusMoveCallback); |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 441 | } else { |
| 442 | mCameraDevice.setAutoFocusMoveCallback(null, null); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /** |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 447 | * @return Whether the currently active camera is front-facing. |
| 448 | */ |
| 449 | private boolean isCameraFrontFacing() { |
Sol Boucher | 43e1813 | 2014-06-19 15:03:18 -0700 | [diff] [blame] | 450 | return mAppController.getCameraProvider().getCharacteristics(mCameraId) |
| 451 | .isFacingFront(); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * @return Whether the currently active camera is back-facing. |
| 456 | */ |
| 457 | private boolean isCameraBackFacing() { |
| 458 | return mAppController.getCameraProvider().getCharacteristics(mCameraId) |
| 459 | .isFacingBack(); |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /** |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 463 | * The focus manager gets initialized after camera is available. |
| 464 | */ |
| 465 | private void initializeFocusManager() { |
| 466 | // Create FocusManager object. startPreview needs it. |
| 467 | // if mFocusManager not null, reuse it |
| 468 | // otherwise create a new instance |
| 469 | if (mFocusManager != null) { |
| 470 | mFocusManager.removeMessages(); |
| 471 | } else { |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 472 | mMirror = isCameraFrontFacing(); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 473 | String[] defaultFocusModesStrings = mActivity.getResources().getStringArray( |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 474 | R.array.pref_camera_focusmode_default_array); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 475 | CameraCapabilities.Stringifier stringifier = mCameraCapabilities.getStringifier(); |
Sascha Haeberling | ee36c5f | 2014-07-29 17:05:43 -0700 | [diff] [blame] | 476 | ArrayList<CameraCapabilities.FocusMode> defaultFocusModes = |
| 477 | new ArrayList<CameraCapabilities.FocusMode>(); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 478 | for (String modeString : defaultFocusModesStrings) { |
| 479 | CameraCapabilities.FocusMode mode = stringifier.focusModeFromString(modeString); |
| 480 | if (mode != null) { |
| 481 | defaultFocusModes.add(mode); |
| 482 | } |
| 483 | } |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 484 | mFocusManager = new FocusOverlayManager(mAppController, |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 485 | defaultFocusModes, mCameraCapabilities, this, mMirror, |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 486 | mActivity.getMainLooper(), mUI.getFocusUI()); |
| 487 | } |
| 488 | mAppController.addPreviewAreaSizeChangedListener(mFocusManager); |
| 489 | } |
| 490 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 491 | @Override |
| 492 | public void onOrientationChanged(int orientation) { |
| 493 | // We keep the last known orientation. So if the user first orient |
| 494 | // the camera then point the camera to floor or sky, we still have |
| 495 | // the correct orientation. |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 496 | if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) { |
| 497 | return; |
| 498 | } |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 499 | int newOrientation = CameraUtil.roundOrientation(orientation, mOrientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 500 | |
| 501 | if (mOrientation != newOrientation) { |
| 502 | mOrientation = newOrientation; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 503 | } |
Doris Liu | 8dc878f | 2014-05-05 14:10:34 -0700 | [diff] [blame] | 504 | mUI.onOrientationChanged(orientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 505 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Erin Dahlgren | 0a6a8d8 | 2014-01-09 22:17:38 -0800 | [diff] [blame] | 508 | private final ButtonManager.ButtonCallback mFlashCallback = |
Erin Dahlgren | fce8a0b | 2013-12-12 17:36:32 -0800 | [diff] [blame] | 509 | new ButtonManager.ButtonCallback() { |
| 510 | @Override |
| 511 | public void onStateChanged(int state) { |
| 512 | // Update flash parameters. |
| 513 | enableTorchMode(true); |
| 514 | } |
| 515 | }; |
| 516 | |
Erin Dahlgren | 0a6a8d8 | 2014-01-09 22:17:38 -0800 | [diff] [blame] | 517 | private final ButtonManager.ButtonCallback mCameraCallback = |
Erin Dahlgren | 18e2ef6 | 2013-12-05 14:53:38 -0800 | [diff] [blame] | 518 | new ButtonManager.ButtonCallback() { |
| 519 | @Override |
| 520 | public void onStateChanged(int state) { |
Angus Kong | 97e282a | 2014-03-04 18:44:49 -0800 | [diff] [blame] | 521 | if (mPaused || mAppController.getCameraProvider().waitingForCamera()) { |
Erin Dahlgren | 18e2ef6 | 2013-12-05 14:53:38 -0800 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | mPendingSwitchCameraId = state; |
| 525 | Log.d(TAG, "Start to copy texture."); |
| 526 | |
| 527 | // Disable all camera controls. |
| 528 | mSwitchingCamera = true; |
| 529 | switchCamera(); |
| 530 | } |
| 531 | }; |
| 532 | |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 533 | private final View.OnClickListener mCancelCallback = new View.OnClickListener() { |
| 534 | @Override |
| 535 | public void onClick(View v) { |
| 536 | onReviewCancelClicked(v); |
| 537 | } |
| 538 | }; |
| 539 | |
| 540 | private final View.OnClickListener mDoneCallback = new View.OnClickListener() { |
| 541 | @Override |
| 542 | public void onClick(View v) { |
| 543 | onReviewDoneClicked(v); |
| 544 | } |
| 545 | }; |
| 546 | private final View.OnClickListener mReviewCallback = new View.OnClickListener() { |
| 547 | @Override |
| 548 | public void onClick(View v) { |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 549 | onReviewPlayClicked(v); |
| 550 | } |
| 551 | }; |
| 552 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 553 | @Override |
Erin Dahlgren | 1ca516f | 2014-03-28 12:44:04 -0700 | [diff] [blame] | 554 | public void hardResetSettings(SettingsManager settingsManager) { |
| 555 | // VideoModule does not need to hard reset any settings. |
| 556 | } |
| 557 | |
| 558 | @Override |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 559 | public HardwareSpec getHardwareSpec() { |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 560 | return (mCameraSettings != null ? |
| 561 | new HardwareSpecImpl(getCameraProvider(), mCameraCapabilities) : null); |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | @Override |
| 565 | public CameraAppUI.BottomBarUISpec getBottomBarSpec() { |
| 566 | CameraAppUI.BottomBarUISpec bottomBarSpec = new CameraAppUI.BottomBarUISpec(); |
| 567 | |
| 568 | bottomBarSpec.enableCamera = true; |
| 569 | bottomBarSpec.cameraCallback = mCameraCallback; |
| 570 | bottomBarSpec.enableTorchFlash = true; |
| 571 | bottomBarSpec.flashCallback = mFlashCallback; |
| 572 | bottomBarSpec.hideHdr = true; |
Spike Sprague | ab3adde | 2014-03-10 12:19:08 -0700 | [diff] [blame] | 573 | bottomBarSpec.enableGridLines = true; |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 574 | |
| 575 | if (isVideoCaptureIntent()) { |
| 576 | bottomBarSpec.showCancel = true; |
| 577 | bottomBarSpec.cancelCallback = mCancelCallback; |
| 578 | bottomBarSpec.showDone = true; |
| 579 | bottomBarSpec.doneCallback = mDoneCallback; |
| 580 | bottomBarSpec.showReview = true; |
| 581 | bottomBarSpec.reviewCallback = mReviewCallback; |
| 582 | } |
| 583 | |
| 584 | return bottomBarSpec; |
Erin Dahlgren | 0a6a8d8 | 2014-01-09 22:17:38 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | @Override |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 588 | public void onCameraAvailable(CameraProxy cameraProxy) { |
| 589 | mCameraDevice = cameraProxy; |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 590 | mCameraCapabilities = mCameraDevice.getCapabilities(); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 591 | mCameraSettings = mCameraDevice.getSettings(); |
| 592 | mFocusAreaSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.FOCUS_AREA); |
| 593 | mMeteringAreaSupported = |
| 594 | mCameraCapabilities.supports(CameraCapabilities.Feature.METERING_AREA); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 595 | readVideoPreferences(); |
| 596 | resizeForPreviewAspectRatio(); |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 597 | initializeFocusManager(); |
Doris Liu | 5dd85a1 | 2014-04-15 11:45:55 -0700 | [diff] [blame] | 598 | // TODO: Having focus overlay manager caching the parameters is prone to error, |
| 599 | // we should consider passing the parameters to focus overlay to ensure the |
| 600 | // parameters are up to date. |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 601 | mFocusManager.updateCapabilities(mCameraCapabilities); |
Doris Liu | 5a36754 | 2014-01-17 17:21:42 -0800 | [diff] [blame] | 602 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 603 | startPreview(); |
| 604 | initializeVideoSnapshot(); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 605 | mUI.initializeZoom(mCameraSettings, mCameraCapabilities); |
Erin Dahlgren | b1641f5 | 2014-01-14 15:58:52 -0800 | [diff] [blame] | 606 | initializeControlByIntent(); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 609 | private void startPlayVideoActivity() { |
| 610 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 611 | intent.setDataAndType(mCurrentVideoUri, convertOutputFormatToMimeType(mProfile.fileFormat)); |
Senpo Hu | 9f2b20a | 2014-08-29 16:27:03 -0700 | [diff] [blame^] | 612 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 613 | try { |
Senpo Hu | 9f2b20a | 2014-08-29 16:27:03 -0700 | [diff] [blame^] | 614 | mActivity.startActivity(intent); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 615 | } catch (ActivityNotFoundException ex) { |
| 616 | Log.e(TAG, "Couldn't view video " + mCurrentVideoUri, ex); |
| 617 | } |
| 618 | } |
| 619 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 620 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 621 | @OnClickAttr |
| 622 | public void onReviewPlayClicked(View v) { |
| 623 | startPlayVideoActivity(); |
| 624 | } |
| 625 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 626 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 627 | @OnClickAttr |
| 628 | public void onReviewDoneClicked(View v) { |
Doris Liu | 69ef5ea | 2013-05-07 13:48:10 -0700 | [diff] [blame] | 629 | mIsInReviewMode = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 630 | doReturnToCaller(true); |
| 631 | } |
| 632 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 633 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 634 | @OnClickAttr |
| 635 | public void onReviewCancelClicked(View v) { |
ztenghui | af3a197 | 2013-09-17 16:51:15 -0700 | [diff] [blame] | 636 | // TODO: It should be better to not even insert the URI at all before we |
| 637 | // confirm done in review, which means we need to handle temporary video |
| 638 | // files in a quite different way than we currently had. |
ztenghui | 70bd024 | 2013-10-14 11:15:44 -0700 | [diff] [blame] | 639 | // Make sure we don't delete the Uri sent from the video capture intent. |
| 640 | if (mCurrentVideoUriFromMediaSaved) { |
ztenghui | dfe5b15 | 2013-10-08 17:07:15 -0700 | [diff] [blame] | 641 | mContentResolver.delete(mCurrentVideoUri, null, null); |
| 642 | } |
ztenghui | 638bf9a | 2013-10-09 10:52:33 -0700 | [diff] [blame] | 643 | mIsInReviewMode = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 644 | doReturnToCaller(false); |
| 645 | } |
| 646 | |
Doris Liu | 69ef5ea | 2013-05-07 13:48:10 -0700 | [diff] [blame] | 647 | @Override |
| 648 | public boolean isInReviewMode() { |
| 649 | return mIsInReviewMode; |
| 650 | } |
| 651 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 652 | private void onStopVideoRecording() { |
Sascha Haeberling | 8793eff | 2014-01-15 16:33:59 -0800 | [diff] [blame] | 653 | mAppController.getCameraAppUI().setSwipeEnabled(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 654 | boolean recordFail = stopVideoRecording(); |
| 655 | if (mIsVideoCaptureIntent) { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 656 | if (mQuickCapture) { |
| 657 | doReturnToCaller(!recordFail); |
| 658 | } else if (!recordFail) { |
| 659 | showCaptureResult(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 660 | } |
| 661 | } else if (!recordFail){ |
| 662 | // Start capture animation. |
| 663 | if (!mPaused && ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) { |
| 664 | // The capture animation is disabled on ICS because we use SurfaceView |
| 665 | // for preview during recording. When the recording is done, we switch |
| 666 | // back to use SurfaceTexture for preview and we need to stop then start |
| 667 | // the preview. This will cause the preview flicker since the preview |
| 668 | // will not be continuous for a short period of time. |
Sascha Haeberling | 4f91ab5 | 2013-05-21 11:26:13 -0700 | [diff] [blame] | 669 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 670 | mUI.animateFlash(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
Doris Liu | 2a7f44c | 2013-08-12 15:18:53 -0700 | [diff] [blame] | 675 | public void onVideoSaved() { |
| 676 | if (mIsVideoCaptureIntent) { |
| 677 | showCaptureResult(); |
| 678 | } |
| 679 | } |
| 680 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 681 | public void onProtectiveCurtainClick(View v) { |
| 682 | // Consume clicks |
| 683 | } |
| 684 | |
| 685 | @Override |
| 686 | public void onShutterButtonClick() { |
Sascha Haeberling | 8793eff | 2014-01-15 16:33:59 -0800 | [diff] [blame] | 687 | if (mSwitchingCamera) { |
| 688 | return; |
| 689 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 690 | boolean stop = mMediaRecorderRecording; |
| 691 | |
| 692 | if (stop) { |
| 693 | onStopVideoRecording(); |
| 694 | } else { |
| 695 | startVideoRecording(); |
| 696 | } |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 697 | mAppController.setShutterEnabled(false); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 698 | mFocusManager.onShutterUp(mCameraSettings.getCurrentFocusMode()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 699 | |
| 700 | // Keep the shutter button disabled when in video capture intent |
| 701 | // mode and recording is stopped. It'll be re-enabled when |
| 702 | // re-take button is clicked. |
| 703 | if (!(mIsVideoCaptureIntent && stop)) { |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 704 | mHandler.sendEmptyMessageDelayed(MSG_ENABLE_SHUTTER_BUTTON, SHUTTER_BUTTON_TIMEOUT); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | @Override |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 709 | public void onShutterCoordinate(TouchCoordinate coord) { |
| 710 | // Do nothing. |
| 711 | } |
| 712 | |
| 713 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 714 | public void onShutterButtonFocus(boolean pressed) { |
Doris Liu | f55f3c4 | 2013-11-20 00:24:46 -0800 | [diff] [blame] | 715 | // TODO: Remove this when old camera controls are removed from the UI. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | private void readVideoPreferences() { |
| 719 | // The preference stores values from ListPreference and is thus string type for all values. |
| 720 | // We need to convert it to int manually. |
Erin Dahlgren | bd3da26 | 2013-12-02 11:48:13 -0800 | [diff] [blame] | 721 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 722 | if (!settingsManager.isSet(SettingsManager.SCOPE_GLOBAL, |
| 723 | Keys.KEY_VIDEO_QUALITY_BACK)) { |
| 724 | settingsManager.setToDefault(SettingsManager.SCOPE_GLOBAL, |
| 725 | Keys.KEY_VIDEO_QUALITY_BACK); |
Senpo Hu | 20571ff | 2014-08-18 16:45:45 -0700 | [diff] [blame] | 726 | |
| 727 | // TODO: Remove this once 4k recording is stable enough on new devices. |
| 728 | // Don't set the default resolution to be large if the device supports 4k video. |
| 729 | if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_2160P)) { |
| 730 | settingsManager.set(SettingsManager.SCOPE_GLOBAL, |
| 731 | Keys.KEY_VIDEO_QUALITY_BACK, |
| 732 | mAppController.getAndroidContext().getString(R.string.pref_video_quality_medium)); |
| 733 | } |
Doris Liu | 3f7e004 | 2013-07-31 11:25:09 -0700 | [diff] [blame] | 734 | } |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 735 | if (!settingsManager.isSet(SettingsManager.SCOPE_GLOBAL, |
| 736 | Keys.KEY_VIDEO_QUALITY_FRONT)) { |
| 737 | settingsManager.setToDefault(SettingsManager.SCOPE_GLOBAL, |
| 738 | Keys.KEY_VIDEO_QUALITY_FRONT); |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 739 | } |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 740 | String videoQualityKey = isCameraFrontFacing() ? Keys.KEY_VIDEO_QUALITY_FRONT |
| 741 | : Keys.KEY_VIDEO_QUALITY_BACK; |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 742 | String videoQuality = settingsManager |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 743 | .getString(SettingsManager.SCOPE_GLOBAL, videoQualityKey); |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 744 | int quality = SettingsUtil.getVideoQuality(videoQuality, mCameraId); |
| 745 | Log.d(TAG, "Selected video quality for '" + videoQuality + "' is " + quality); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 746 | |
| 747 | // Set video quality. |
| 748 | Intent intent = mActivity.getIntent(); |
| 749 | if (intent.hasExtra(MediaStore.EXTRA_VIDEO_QUALITY)) { |
| 750 | int extraVideoQuality = |
| 751 | intent.getIntExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); |
| 752 | if (extraVideoQuality > 0) { |
| 753 | quality = CamcorderProfile.QUALITY_HIGH; |
| 754 | } else { // 0 is mms. |
| 755 | quality = CamcorderProfile.QUALITY_LOW; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // Set video duration limit. The limit is read from the preference, |
| 760 | // unless it is specified in the intent. |
| 761 | if (intent.hasExtra(MediaStore.EXTRA_DURATION_LIMIT)) { |
| 762 | int seconds = |
| 763 | intent.getIntExtra(MediaStore.EXTRA_DURATION_LIMIT, 0); |
| 764 | mMaxVideoDurationInMs = 1000 * seconds; |
| 765 | } else { |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 766 | mMaxVideoDurationInMs = SettingsUtil.getMaxVideoDuration(mActivity |
Sascha Haeberling | 4044ab7 | 2014-04-02 16:25:03 -0700 | [diff] [blame] | 767 | .getAndroidContext()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Andy Huibers | 329e101 | 2014-02-04 08:03:35 -0800 | [diff] [blame] | 770 | // If quality is not supported, request QUALITY_HIGH which is always supported. |
| 771 | if (CamcorderProfile.hasProfile(mCameraId, quality) == false) { |
| 772 | quality = CamcorderProfile.QUALITY_HIGH; |
| 773 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 774 | mProfile = CamcorderProfile.get(mCameraId, quality); |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 775 | mPreferenceRead = true; |
ztenghui | ef01a31 | 2013-10-14 15:25:16 -0700 | [diff] [blame] | 776 | if (mCameraDevice == null) { |
| 777 | return; |
| 778 | } |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 779 | mCameraSettings = mCameraDevice.getSettings(); |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 780 | Point desiredPreviewSize = getDesiredPreviewSize(mAppController.getAndroidContext(), |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 781 | mCameraSettings, mCameraCapabilities, mProfile, mUI.getPreviewScreenSize()); |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 782 | mDesiredPreviewWidth = desiredPreviewSize.x; |
| 783 | mDesiredPreviewHeight = desiredPreviewSize.y; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 784 | mUI.setPreviewSize(mDesiredPreviewWidth, mDesiredPreviewHeight); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 785 | Log.v(TAG, "mDesiredPreviewWidth=" + mDesiredPreviewWidth + |
| 786 | ". mDesiredPreviewHeight=" + mDesiredPreviewHeight); |
| 787 | } |
| 788 | |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 789 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) |
| 790 | /** |
| 791 | * Calculates the preview size and stores it in mDesiredPreviewWidth and |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 792 | * mDesiredPreviewHeight. |
| 793 | * |
| 794 | * <p>This function checks {@link |
| 795 | * com.android.camera.cameradevice.CameraCapabilities#getPreferredPreviewSizeForVideo()} |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 796 | * but also considers the current preview area size on screen and make sure |
| 797 | * the final preview size will not be smaller than 1/2 of the current |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 798 | * on screen preview area in terms of their short sides.</p> |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 799 | * |
| 800 | * @return The preferred preview size or {@code null} if the camera is not |
| 801 | * opened yet. |
| 802 | */ |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 803 | private static Point getDesiredPreviewSize(Context context, CameraSettings settings, |
| 804 | CameraCapabilities capabilities, CamcorderProfile profile, Point previewScreenSize) { |
| 805 | if (capabilities.getSupportedVideoSizes() == null) { |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 806 | // Driver doesn't support separate outputs for preview and video. |
| 807 | return new Point(profile.videoFrameWidth, profile.videoFrameHeight); |
| 808 | } |
| 809 | |
| 810 | final int previewScreenShortSide = (previewScreenSize.x < previewScreenSize.y ? |
| 811 | previewScreenSize.x : previewScreenSize.y); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 812 | List<Size> sizes = capabilities.getSupportedPreviewSizes(); |
| 813 | Size preferred = capabilities.getPreferredPreviewSizeForVideo(); |
Angus Kong | 00b7b10 | 2014-04-24 15:46:52 -0700 | [diff] [blame] | 814 | final int preferredPreviewSizeShortSide = (preferred.width() < preferred.height() ? |
| 815 | preferred.width() : preferred.height()); |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 816 | if (preferredPreviewSizeShortSide * 2 < previewScreenShortSide) { |
Angus Kong | 6342466 | 2014-04-23 10:47:47 -0700 | [diff] [blame] | 817 | preferred = new Size(profile.videoFrameWidth, profile.videoFrameHeight); |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 818 | } |
Angus Kong | 00b7b10 | 2014-04-24 15:46:52 -0700 | [diff] [blame] | 819 | int product = preferred.width() * preferred.height(); |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 820 | Iterator<Size> it = sizes.iterator(); |
| 821 | // Remove the preview sizes that are not preferred. |
| 822 | while (it.hasNext()) { |
| 823 | Size size = it.next(); |
Angus Kong | 00b7b10 | 2014-04-24 15:46:52 -0700 | [diff] [blame] | 824 | if (size.width() * size.height() > product) { |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 825 | it.remove(); |
| 826 | } |
| 827 | } |
| 828 | Size optimalSize = CameraUtil.getOptimalPreviewSize(context, sizes, |
| 829 | (double) profile.videoFrameWidth / profile.videoFrameHeight); |
Angus Kong | 00b7b10 | 2014-04-24 15:46:52 -0700 | [diff] [blame] | 830 | return new Point(optimalSize.width(), optimalSize.height()); |
Angus Kong | 5f8c30e | 2014-03-06 17:15:08 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 833 | private void resizeForPreviewAspectRatio() { |
Doris Liu | e038c16 | 2013-12-13 23:06:11 -0800 | [diff] [blame] | 834 | mUI.setAspectRatio((float) mProfile.videoFrameWidth / mProfile.videoFrameHeight); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 837 | private void installIntentFilter() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 838 | // install an intent filter to receive SD card related events. |
| 839 | IntentFilter intentFilter = |
| 840 | new IntentFilter(Intent.ACTION_MEDIA_EJECT); |
| 841 | intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED); |
| 842 | intentFilter.addDataScheme("file"); |
| 843 | mReceiver = new MyBroadcastReceiver(); |
| 844 | mActivity.registerReceiver(mReceiver, intentFilter); |
| 845 | } |
| 846 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 847 | private void setDisplayOrientation() { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 848 | mDisplayRotation = CameraUtil.getDisplayRotation(mActivity); |
Sol Boucher | 43e1813 | 2014-06-19 15:03:18 -0700 | [diff] [blame] | 849 | Characteristics info = |
| 850 | mActivity.getCameraProvider().getCharacteristics(mCameraId); |
Sol Boucher | 73ec985 | 2014-07-24 23:59:21 -0700 | [diff] [blame] | 851 | mCameraDisplayOrientation = info.getPreviewOrientation(mDisplayRotation); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 852 | // Change the camera display orientation |
| 853 | if (mCameraDevice != null) { |
Sol Boucher | 73ec985 | 2014-07-24 23:59:21 -0700 | [diff] [blame] | 854 | mCameraDevice.setDisplayOrientation(mDisplayRotation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 855 | } |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 856 | if (mFocusManager != null) { |
| 857 | mFocusManager.setDisplayOrientation(mCameraDisplayOrientation); |
| 858 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | @Override |
| 862 | public void updateCameraOrientation() { |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 863 | if (mMediaRecorderRecording) { |
| 864 | return; |
| 865 | } |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 866 | if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 867 | setDisplayOrientation(); |
| 868 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 871 | @Override |
Doris Liu | 70da918 | 2013-12-17 18:41:15 -0800 | [diff] [blame] | 872 | public void updatePreviewAspectRatio(float aspectRatio) { |
| 873 | mAppController.updatePreviewAspectRatio(aspectRatio); |
| 874 | } |
| 875 | |
Andy Huibers | 203abe5 | 2014-05-19 13:59:01 -0700 | [diff] [blame] | 876 | /** |
| 877 | * Returns current Zoom value, with 1.0 as the value for no zoom. |
| 878 | */ |
| 879 | private float currentZoomValue() { |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 880 | return mCameraSettings.getCurrentZoomRatio(); |
Andy Huibers | 203abe5 | 2014-05-19 13:59:01 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Doris Liu | 70da918 | 2013-12-17 18:41:15 -0800 | [diff] [blame] | 883 | @Override |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 884 | public void onZoomChanged(float ratio) { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 885 | // Not useful to change zoom value when the activity is paused. |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 886 | if (mPaused) { |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 887 | return; |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 888 | } |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 889 | mZoomValue = ratio; |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 890 | if (mCameraSettings == null || mCameraDevice == null) { |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 891 | return; |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 892 | } |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 893 | // Set zoom parameters asynchronously |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 894 | mCameraSettings.setZoomRatio(mZoomValue); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 895 | mCameraDevice.applySettings(mCameraSettings); |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 896 | } |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 897 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 898 | private void startPreview() { |
Alan Newberger | d41766f | 2014-04-09 18:25:34 -0700 | [diff] [blame] | 899 | Log.i(TAG, "startPreview"); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 900 | |
Erin Dahlgren | d8de077 | 2014-02-03 10:12:27 -0800 | [diff] [blame] | 901 | SurfaceTexture surfaceTexture = mActivity.getCameraAppUI().getSurfaceTexture(); |
ztenghui | ef01a31 | 2013-10-14 15:25:16 -0700 | [diff] [blame] | 902 | if (!mPreferenceRead || surfaceTexture == null || mPaused == true || |
| 903 | mCameraDevice == null) { |
| 904 | return; |
| 905 | } |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 906 | |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame] | 907 | mCameraDevice.setErrorCallback(mHandler, mErrorCallback); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 908 | if (mPreviewing == true) { |
| 909 | stopPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 912 | setDisplayOrientation(); |
Sol Boucher | 73ec985 | 2014-07-24 23:59:21 -0700 | [diff] [blame] | 913 | mCameraDevice.setDisplayOrientation(mDisplayRotation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 914 | setCameraParameters(); |
| 915 | |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 916 | if (mFocusManager != null) { |
| 917 | // If the focus mode is continuous autofocus, call cancelAutoFocus |
| 918 | // to resume it because it may have been paused by autoFocus call. |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 919 | CameraCapabilities.FocusMode focusMode = |
| 920 | mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode()); |
| 921 | if (focusMode == CameraCapabilities.FocusMode.CONTINUOUS_PICTURE) { |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 922 | mCameraDevice.cancelAutoFocus(); |
| 923 | } |
| 924 | } |
Doris Liu | 5a36754 | 2014-01-17 17:21:42 -0800 | [diff] [blame] | 925 | |
| 926 | // This is to notify app controller that preview will start next, so app |
| 927 | // controller can set preview callbacks if needed. This has to happen before |
| 928 | // preview is started as a workaround of the framework issue related to preview |
| 929 | // callbacks that causes preview stretch and crash. (More details see b/12210027 |
Alan Newberger | fb172ac | 2014-08-21 14:42:36 -0700 | [diff] [blame] | 930 | // and b/12591410. Don't apply this to L, see b/16649297. |
| 931 | if (!ApiHelper.isLOrHigher()) { |
| 932 | Log.v(TAG, "calling onPreviewReadyToStart to set one shot callback"); |
| 933 | mAppController.onPreviewReadyToStart(); |
| 934 | } else { |
| 935 | Log.v(TAG, "on L, no one shot callback necessary"); |
| 936 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 937 | try { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 938 | mCameraDevice.setPreviewTexture(surfaceTexture); |
| 939 | mCameraDevice.startPreview(); |
| 940 | mPreviewing = true; |
| 941 | onPreviewStarted(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 942 | } catch (Throwable ex) { |
| 943 | closeCamera(); |
| 944 | throw new RuntimeException("startPreview failed", ex); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 945 | } |
Michael Kolb | b1aeb39 | 2013-03-11 12:37:40 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | private void onPreviewStarted() { |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 949 | mAppController.setShutterEnabled(true); |
Doris Liu | 2b906b8 | 2013-12-10 16:34:08 -0800 | [diff] [blame] | 950 | mAppController.onPreviewStarted(); |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 951 | if (mFocusManager != null) { |
| 952 | mFocusManager.onPreviewStarted(); |
| 953 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 954 | } |
| 955 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 956 | @Override |
Sameer Padala | db81ce6 | 2014-03-21 15:33:56 -0700 | [diff] [blame] | 957 | public void onPreviewInitialDataReceived() { |
Sameer Padala | db81ce6 | 2014-03-21 15:33:56 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | @Override |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 961 | public void stopPreview() { |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 962 | if (!mPreviewing) { |
| 963 | return; |
| 964 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 965 | mCameraDevice.stopPreview(); |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 966 | if (mFocusManager != null) { |
| 967 | mFocusManager.onPreviewStopped(); |
| 968 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 969 | mPreviewing = false; |
| 970 | } |
| 971 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 972 | private void closeCamera() { |
Alan Newberger | d41766f | 2014-04-09 18:25:34 -0700 | [diff] [blame] | 973 | Log.i(TAG, "closeCamera"); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 974 | if (mCameraDevice == null) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 975 | Log.d(TAG, "already stopped."); |
| 976 | return; |
| 977 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 978 | mCameraDevice.setZoomChangeListener(null); |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame] | 979 | mCameraDevice.setErrorCallback(null, null); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 980 | mActivity.getCameraProvider().releaseCamera(mCameraDevice.getCameraId()); |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 981 | mCameraDevice = null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 982 | mPreviewing = false; |
| 983 | mSnapshotInProgress = false; |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 984 | if (mFocusManager != null) { |
| 985 | mFocusManager.onCameraReleased(); |
| 986 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 989 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 990 | public boolean onBackPressed() { |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 991 | if (mPaused) { |
| 992 | return true; |
| 993 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 994 | if (mMediaRecorderRecording) { |
| 995 | onStopVideoRecording(); |
| 996 | return true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 997 | } else { |
Doris Liu | f9e4f8f | 2013-12-04 18:04:22 -0800 | [diff] [blame] | 998 | return false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | @Override |
| 1003 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 1004 | // Do not handle any key if the activity is paused. |
| 1005 | if (mPaused) { |
| 1006 | return true; |
| 1007 | } |
| 1008 | |
| 1009 | switch (keyCode) { |
| 1010 | case KeyEvent.KEYCODE_CAMERA: |
| 1011 | if (event.getRepeatCount() == 0) { |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1012 | onShutterButtonClick(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1013 | return true; |
| 1014 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1015 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 1016 | if (event.getRepeatCount() == 0) { |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1017 | onShutterButtonClick(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1018 | return true; |
| 1019 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1020 | case KeyEvent.KEYCODE_MENU: |
Erin Dahlgren | 15691af | 2014-03-14 14:10:57 -0700 | [diff] [blame] | 1021 | // Consume menu button presses during capture. |
| 1022 | return mMediaRecorderRecording; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1023 | } |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
| 1027 | @Override |
| 1028 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 1029 | switch (keyCode) { |
| 1030 | case KeyEvent.KEYCODE_CAMERA: |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1031 | onShutterButtonClick(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1032 | return true; |
Erin Dahlgren | 15691af | 2014-03-14 14:10:57 -0700 | [diff] [blame] | 1033 | case KeyEvent.KEYCODE_MENU: |
| 1034 | // Consume menu button presses during capture. |
| 1035 | return mMediaRecorderRecording; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1036 | } |
| 1037 | return false; |
| 1038 | } |
| 1039 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1040 | @Override |
| 1041 | public boolean isVideoCaptureIntent() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1042 | String action = mActivity.getIntent().getAction(); |
| 1043 | return (MediaStore.ACTION_VIDEO_CAPTURE.equals(action)); |
| 1044 | } |
| 1045 | |
| 1046 | private void doReturnToCaller(boolean valid) { |
| 1047 | Intent resultIntent = new Intent(); |
| 1048 | int resultCode; |
| 1049 | if (valid) { |
| 1050 | resultCode = Activity.RESULT_OK; |
| 1051 | resultIntent.setData(mCurrentVideoUri); |
| 1052 | } else { |
| 1053 | resultCode = Activity.RESULT_CANCELED; |
| 1054 | } |
| 1055 | mActivity.setResultEx(resultCode, resultIntent); |
| 1056 | mActivity.finish(); |
| 1057 | } |
| 1058 | |
| 1059 | private void cleanupEmptyFile() { |
| 1060 | if (mVideoFilename != null) { |
| 1061 | File f = new File(mVideoFilename); |
| 1062 | if (f.length() == 0 && f.delete()) { |
| 1063 | Log.v(TAG, "Empty video file deleted: " + mVideoFilename); |
| 1064 | mVideoFilename = null; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1069 | // Prepares media recorder. |
| 1070 | private void initializeRecorder() { |
Alan Newberger | d41766f | 2014-04-09 18:25:34 -0700 | [diff] [blame] | 1071 | Log.i(TAG, "initializeRecorder"); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1072 | // If the mCameraDevice is null, then this activity is going to finish |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 1073 | if (mCameraDevice == null) { |
| 1074 | return; |
| 1075 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1076 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1077 | Intent intent = mActivity.getIntent(); |
| 1078 | Bundle myExtras = intent.getExtras(); |
| 1079 | |
| 1080 | long requestedSizeLimit = 0; |
| 1081 | closeVideoFileDescriptor(); |
ztenghui | 70bd024 | 2013-10-14 11:15:44 -0700 | [diff] [blame] | 1082 | mCurrentVideoUriFromMediaSaved = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1083 | if (mIsVideoCaptureIntent && myExtras != null) { |
| 1084 | Uri saveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT); |
| 1085 | if (saveUri != null) { |
| 1086 | try { |
| 1087 | mVideoFileDescriptor = |
| 1088 | mContentResolver.openFileDescriptor(saveUri, "rw"); |
| 1089 | mCurrentVideoUri = saveUri; |
| 1090 | } catch (java.io.FileNotFoundException ex) { |
| 1091 | // invalid uri |
| 1092 | Log.e(TAG, ex.toString()); |
| 1093 | } |
| 1094 | } |
| 1095 | requestedSizeLimit = myExtras.getLong(MediaStore.EXTRA_SIZE_LIMIT); |
| 1096 | } |
| 1097 | mMediaRecorder = new MediaRecorder(); |
| 1098 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1099 | // Unlock the camera object before passing it to media recorder. |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1100 | mCameraDevice.unlock(); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1101 | mMediaRecorder.setCamera(mCameraDevice.getCamera()); |
Sascha Haeberling | f15d63e | 2014-07-21 11:31:13 -0700 | [diff] [blame] | 1102 | mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1103 | mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); |
| 1104 | mMediaRecorder.setProfile(mProfile); |
Michael Kolb | f954236 | 2013-05-30 07:45:41 -0700 | [diff] [blame] | 1105 | mMediaRecorder.setVideoSize(mProfile.videoFrameWidth, mProfile.videoFrameHeight); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1106 | mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1107 | |
| 1108 | setRecordLocation(); |
| 1109 | |
| 1110 | // Set output file. |
| 1111 | // Try Uri in the intent first. If it doesn't exist, use our own |
| 1112 | // instead. |
| 1113 | if (mVideoFileDescriptor != null) { |
| 1114 | mMediaRecorder.setOutputFile(mVideoFileDescriptor.getFileDescriptor()); |
| 1115 | } else { |
| 1116 | generateVideoFilename(mProfile.fileFormat); |
| 1117 | mMediaRecorder.setOutputFile(mVideoFilename); |
| 1118 | } |
| 1119 | |
| 1120 | // Set maximum file size. |
Angus Kong | 2dcc0a9 | 2013-09-25 13:00:08 -0700 | [diff] [blame] | 1121 | long maxFileSize = mActivity.getStorageSpaceBytes() - Storage.LOW_STORAGE_THRESHOLD_BYTES; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1122 | if (requestedSizeLimit > 0 && requestedSizeLimit < maxFileSize) { |
| 1123 | maxFileSize = requestedSizeLimit; |
| 1124 | } |
| 1125 | |
| 1126 | try { |
| 1127 | mMediaRecorder.setMaxFileSize(maxFileSize); |
| 1128 | } catch (RuntimeException exception) { |
| 1129 | // We are going to ignore failure of setMaxFileSize here, as |
| 1130 | // a) The composer selected may simply not support it, or |
| 1131 | // b) The underlying media framework may not handle 64-bit range |
| 1132 | // on the size restriction. |
| 1133 | } |
| 1134 | |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1135 | // See com.android.camera.cameradevice.CameraSettings.setPhotoRotationDegrees |
| 1136 | // for documentation. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1137 | // Note that mOrientation here is the device orientation, which is the opposite of |
| 1138 | // what activity.getWindowManager().getDefaultDisplay().getRotation() would return, |
| 1139 | // which is the orientation the graphics need to rotate in order to render correctly. |
| 1140 | int rotation = 0; |
| 1141 | if (mOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) { |
Sol Boucher | 43e1813 | 2014-06-19 15:03:18 -0700 | [diff] [blame] | 1142 | Characteristics info = |
| 1143 | mActivity.getCameraProvider().getCharacteristics(mCameraId); |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 1144 | if (isCameraFrontFacing()) { |
Sol Boucher | 43e1813 | 2014-06-19 15:03:18 -0700 | [diff] [blame] | 1145 | rotation = (info.getSensorOrientation() - mOrientation + 360) % 360; |
| 1146 | } else if (isCameraBackFacing()) { |
| 1147 | rotation = (info.getSensorOrientation() + mOrientation) % 360; |
| 1148 | } else { |
| 1149 | Log.e(TAG, "Camera is facing unhandled direction"); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | mMediaRecorder.setOrientationHint(rotation); |
| 1153 | |
| 1154 | try { |
| 1155 | mMediaRecorder.prepare(); |
| 1156 | } catch (IOException e) { |
| 1157 | Log.e(TAG, "prepare failed for " + mVideoFilename, e); |
| 1158 | releaseMediaRecorder(); |
| 1159 | throw new RuntimeException(e); |
| 1160 | } |
| 1161 | |
| 1162 | mMediaRecorder.setOnErrorListener(this); |
| 1163 | mMediaRecorder.setOnInfoListener(this); |
| 1164 | } |
| 1165 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1166 | private static void setCaptureRate(MediaRecorder recorder, double fps) { |
| 1167 | recorder.setCaptureRate(fps); |
| 1168 | } |
| 1169 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1170 | private void setRecordLocation() { |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 1171 | Location loc = mLocationManager.getCurrentLocation(); |
| 1172 | if (loc != null) { |
| 1173 | mMediaRecorder.setLocation((float) loc.getLatitude(), |
| 1174 | (float) loc.getLongitude()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1178 | private void releaseMediaRecorder() { |
Alan Newberger | d41766f | 2014-04-09 18:25:34 -0700 | [diff] [blame] | 1179 | Log.i(TAG, "Releasing media recorder."); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1180 | if (mMediaRecorder != null) { |
| 1181 | cleanupEmptyFile(); |
| 1182 | mMediaRecorder.reset(); |
| 1183 | mMediaRecorder.release(); |
| 1184 | mMediaRecorder = null; |
| 1185 | } |
| 1186 | mVideoFilename = null; |
| 1187 | } |
| 1188 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1189 | private void generateVideoFilename(int outputFileFormat) { |
| 1190 | long dateTaken = System.currentTimeMillis(); |
| 1191 | String title = createName(dateTaken); |
| 1192 | // Used when emailing. |
| 1193 | String filename = title + convertOutputFormatToFileExt(outputFileFormat); |
| 1194 | String mime = convertOutputFormatToMimeType(outputFileFormat); |
| 1195 | String path = Storage.DIRECTORY + '/' + filename; |
| 1196 | String tmpPath = path + ".tmp"; |
Ruben Brunk | 1600796 | 2013-04-19 15:27:57 -0700 | [diff] [blame] | 1197 | mCurrentVideoValues = new ContentValues(9); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1198 | mCurrentVideoValues.put(Video.Media.TITLE, title); |
| 1199 | mCurrentVideoValues.put(Video.Media.DISPLAY_NAME, filename); |
| 1200 | mCurrentVideoValues.put(Video.Media.DATE_TAKEN, dateTaken); |
Ruben Brunk | 1600796 | 2013-04-19 15:27:57 -0700 | [diff] [blame] | 1201 | mCurrentVideoValues.put(MediaColumns.DATE_MODIFIED, dateTaken / 1000); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1202 | mCurrentVideoValues.put(Video.Media.MIME_TYPE, mime); |
| 1203 | mCurrentVideoValues.put(Video.Media.DATA, path); |
Sam Judd | de3e9ab | 2014-03-17 13:07:22 -0700 | [diff] [blame] | 1204 | mCurrentVideoValues.put(Video.Media.WIDTH, mProfile.videoFrameWidth); |
| 1205 | mCurrentVideoValues.put(Video.Media.HEIGHT, mProfile.videoFrameHeight); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1206 | mCurrentVideoValues.put(Video.Media.RESOLUTION, |
| 1207 | Integer.toString(mProfile.videoFrameWidth) + "x" + |
| 1208 | Integer.toString(mProfile.videoFrameHeight)); |
| 1209 | Location loc = mLocationManager.getCurrentLocation(); |
| 1210 | if (loc != null) { |
| 1211 | mCurrentVideoValues.put(Video.Media.LATITUDE, loc.getLatitude()); |
| 1212 | mCurrentVideoValues.put(Video.Media.LONGITUDE, loc.getLongitude()); |
| 1213 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1214 | mVideoFilename = tmpPath; |
| 1215 | Log.v(TAG, "New video filename: " + mVideoFilename); |
| 1216 | } |
| 1217 | |
Andy Huibers | 203abe5 | 2014-05-19 13:59:01 -0700 | [diff] [blame] | 1218 | private void logVideoCapture(long duration) { |
| 1219 | String flashSetting = mActivity.getSettingsManager() |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 1220 | .getString(mAppController.getCameraScope(), |
| 1221 | Keys.KEY_VIDEOCAMERA_FLASH_MODE); |
| 1222 | boolean gridLinesOn = Keys.areGridLinesOn(mActivity.getSettingsManager()); |
Andy Huibers | 203abe5 | 2014-05-19 13:59:01 -0700 | [diff] [blame] | 1223 | int width = (Integer) mCurrentVideoValues.get(Video.Media.WIDTH); |
| 1224 | int height = (Integer) mCurrentVideoValues.get(Video.Media.HEIGHT); |
| 1225 | long size = new File(mCurrentVideoFilename).length(); |
| 1226 | String name = new File(mCurrentVideoValues.getAsString(Video.Media.DATA)).getName(); |
| 1227 | UsageStatistics.instance().videoCaptureDoneEvent(name, duration, isCameraFrontFacing(), |
| 1228 | currentZoomValue(), width, height, size, flashSetting, gridLinesOn); |
| 1229 | } |
| 1230 | |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 1231 | private void saveVideo() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1232 | if (mVideoFileDescriptor == null) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1233 | long duration = SystemClock.uptimeMillis() - mRecordingStartTime; |
| 1234 | if (duration > 0) { |
Spike Sprague | d020fbf | 2014-07-16 15:47:32 -0700 | [diff] [blame] | 1235 | // |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1236 | } else { |
| 1237 | Log.w(TAG, "Video duration <= 0 : " + duration); |
| 1238 | } |
Andy Huibers | 203abe5 | 2014-05-19 13:59:01 -0700 | [diff] [blame] | 1239 | mCurrentVideoValues.put(Video.Media.SIZE, new File(mCurrentVideoFilename).length()); |
| 1240 | mCurrentVideoValues.put(Video.Media.DURATION, duration); |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1241 | getServices().getMediaSaver().addVideo(mCurrentVideoFilename, |
Andy Huibers | 203abe5 | 2014-05-19 13:59:01 -0700 | [diff] [blame] | 1242 | mCurrentVideoValues, mOnVideoSavedListener, mContentResolver); |
| 1243 | logVideoCapture(duration); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1244 | } |
| 1245 | mCurrentVideoValues = null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | private void deleteVideoFile(String fileName) { |
| 1249 | Log.v(TAG, "Deleting video " + fileName); |
| 1250 | File f = new File(fileName); |
| 1251 | if (!f.delete()) { |
| 1252 | Log.v(TAG, "Could not delete " + fileName); |
| 1253 | } |
| 1254 | } |
| 1255 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1256 | // from MediaRecorder.OnErrorListener |
| 1257 | @Override |
| 1258 | public void onError(MediaRecorder mr, int what, int extra) { |
| 1259 | Log.e(TAG, "MediaRecorder error. what=" + what + ". extra=" + extra); |
| 1260 | if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { |
| 1261 | // We may have run out of space on the sdcard. |
| 1262 | stopVideoRecording(); |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1263 | mActivity.updateStorageSpaceAndHint(null); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | // from MediaRecorder.OnInfoListener |
| 1268 | @Override |
| 1269 | public void onInfo(MediaRecorder mr, int what, int extra) { |
| 1270 | if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) { |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 1271 | if (mMediaRecorderRecording) { |
| 1272 | onStopVideoRecording(); |
| 1273 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1274 | } else if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) { |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 1275 | if (mMediaRecorderRecording) { |
| 1276 | onStopVideoRecording(); |
| 1277 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1278 | |
| 1279 | // Show the toast. |
| 1280 | Toast.makeText(mActivity, R.string.video_reach_size_limit, |
| 1281 | Toast.LENGTH_LONG).show(); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Make sure we're not recording music playing in the background, ask the |
| 1287 | * MediaPlaybackService to pause playback. |
| 1288 | */ |
| 1289 | private void pauseAudioPlayback() { |
Marco Nelissen | 20694b2 | 2013-10-29 15:27:24 -0700 | [diff] [blame] | 1290 | AudioManager am = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE); |
| 1291 | am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | // For testing. |
| 1295 | public boolean isRecording() { |
| 1296 | return mMediaRecorderRecording; |
| 1297 | } |
| 1298 | |
| 1299 | private void startVideoRecording() { |
Alan Newberger | d41766f | 2014-04-09 18:25:34 -0700 | [diff] [blame] | 1300 | Log.i(TAG, "startVideoRecording"); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1301 | mUI.cancelAnimations(); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1302 | mUI.setSwipingEnabled(false); |
Doris Liu | 38c6bc3 | 2014-01-16 18:03:18 -0800 | [diff] [blame] | 1303 | mUI.showFocusUI(false); |
Doris Liu | d6487c9 | 2014-02-28 10:35:45 -0800 | [diff] [blame] | 1304 | mUI.showVideoRecordingHints(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1305 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1306 | mActivity.updateStorageSpaceAndHint(new CameraActivity.OnStorageUpdateDoneListener() { |
| 1307 | @Override |
| 1308 | public void onStorageUpdateDone(long bytes) { |
| 1309 | if (bytes <= Storage.LOW_STORAGE_THRESHOLD_BYTES) { |
| 1310 | Log.w(TAG, "Storage issue, ignore the start request"); |
| 1311 | } else { |
| 1312 | //?? |
| 1313 | //if (!mCameraDevice.waitDone()) return; |
Alan Newberger | 26b224d | 2014-08-26 18:18:48 -0700 | [diff] [blame] | 1314 | if (mPaused == true) { |
| 1315 | Log.v(TAG, "in storage callback after module paused"); |
| 1316 | return; |
| 1317 | } |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1318 | mCurrentVideoUri = null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1319 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1320 | initializeRecorder(); |
| 1321 | if (mMediaRecorder == null) { |
| 1322 | Log.e(TAG, "Fail to initialize media recorder"); |
| 1323 | return; |
| 1324 | } |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1325 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1326 | pauseAudioPlayback(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1327 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1328 | try { |
| 1329 | mMediaRecorder.start(); // Recording is now started |
| 1330 | } catch (RuntimeException e) { |
| 1331 | Log.e(TAG, "Could not start media recorder. ", e); |
| 1332 | releaseMediaRecorder(); |
| 1333 | // If start fails, frameworks will not lock the camera for us. |
| 1334 | mCameraDevice.lock(); |
| 1335 | return; |
| 1336 | } |
| 1337 | mAppController.getCameraAppUI().setSwipeEnabled(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1338 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1339 | // The parameters might have been altered by MediaRecorder already. |
| 1340 | // We need to force mCameraDevice to refresh before getting it. |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1341 | mCameraDevice.refreshSettings(); |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1342 | // The parameters may have been changed by MediaRecorder upon starting |
| 1343 | // recording. We need to alter the parameters if we support camcorder |
| 1344 | // zoom. To reduce latency when setting the parameters during zoom, we |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1345 | // update the settings here once. |
| 1346 | mCameraSettings = mCameraDevice.getSettings(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1347 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1348 | mMediaRecorderRecording = true; |
| 1349 | mActivity.lockOrientation(); |
| 1350 | mRecordingStartTime = SystemClock.uptimeMillis(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1351 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1352 | // A special case of mode options closing: during capture it should |
| 1353 | // not be possible to change mode state. |
| 1354 | mAppController.getCameraAppUI().hideModeOptions(); |
| 1355 | mAppController.getCameraAppUI().animateBottomBarToVideoStop(R.drawable.ic_stop); |
| 1356 | mUI.showRecordingUI(true); |
Doris Liu | 4df9158 | 2014-03-21 18:33:57 -0700 | [diff] [blame] | 1357 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1358 | setFocusParameters(); |
Senpo Hu | 1118631 | 2014-08-08 13:42:13 -0700 | [diff] [blame] | 1359 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1360 | updateRecordingTime(); |
| 1361 | mActivity.enableKeepScreenOn(true); |
| 1362 | } |
| 1363 | } |
| 1364 | }); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1367 | private Bitmap getVideoThumbnail() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1368 | Bitmap bitmap = null; |
| 1369 | if (mVideoFileDescriptor != null) { |
| 1370 | bitmap = Thumbnail.createVideoThumbnailBitmap(mVideoFileDescriptor.getFileDescriptor(), |
Doris Liu | 3c2fca3 | 2013-02-13 18:28:03 -0800 | [diff] [blame] | 1371 | mDesiredPreviewWidth); |
Doris Liu | 2a7f44c | 2013-08-12 15:18:53 -0700 | [diff] [blame] | 1372 | } else if (mCurrentVideoUri != null) { |
| 1373 | try { |
| 1374 | mVideoFileDescriptor = mContentResolver.openFileDescriptor(mCurrentVideoUri, "r"); |
| 1375 | bitmap = Thumbnail.createVideoThumbnailBitmap( |
| 1376 | mVideoFileDescriptor.getFileDescriptor(), mDesiredPreviewWidth); |
| 1377 | } catch (java.io.FileNotFoundException ex) { |
| 1378 | // invalid uri |
| 1379 | Log.e(TAG, ex.toString()); |
| 1380 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1381 | } |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1382 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1383 | if (bitmap != null) { |
| 1384 | // MetadataRetriever already rotates the thumbnail. We should rotate |
| 1385 | // it to match the UI orientation (and mirror if it is front-facing camera). |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 1386 | bitmap = CameraUtil.rotateAndMirror(bitmap, 0, isCameraFrontFacing()); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1387 | } |
| 1388 | return bitmap; |
| 1389 | } |
| 1390 | |
| 1391 | private void showCaptureResult() { |
| 1392 | mIsInReviewMode = true; |
| 1393 | Bitmap bitmap = getVideoThumbnail(); |
| 1394 | if (bitmap != null) { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1395 | mUI.showReviewImage(bitmap); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1396 | } |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1397 | mUI.showReviewControls(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1398 | } |
| 1399 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1400 | private boolean stopVideoRecording() { |
Alan Newberger | d41766f | 2014-04-09 18:25:34 -0700 | [diff] [blame] | 1401 | Log.i(TAG, "stopVideoRecording"); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1402 | mUI.setSwipingEnabled(true); |
Doris Liu | 38c6bc3 | 2014-01-16 18:03:18 -0800 | [diff] [blame] | 1403 | mUI.showFocusUI(true); |
Doris Liu | d6487c9 | 2014-02-28 10:35:45 -0800 | [diff] [blame] | 1404 | mUI.showVideoRecordingHints(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1405 | |
| 1406 | boolean fail = false; |
| 1407 | if (mMediaRecorderRecording) { |
| 1408 | boolean shouldAddToMediaStoreNow = false; |
| 1409 | |
| 1410 | try { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1411 | mMediaRecorder.setOnErrorListener(null); |
| 1412 | mMediaRecorder.setOnInfoListener(null); |
| 1413 | mMediaRecorder.stop(); |
| 1414 | shouldAddToMediaStoreNow = true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1415 | mCurrentVideoFilename = mVideoFilename; |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1416 | Log.v(TAG, "stopVideoRecording: current video filename: " + mCurrentVideoFilename); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1417 | } catch (RuntimeException e) { |
| 1418 | Log.e(TAG, "stop fail", e); |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 1419 | if (mVideoFilename != null) { |
| 1420 | deleteVideoFile(mVideoFilename); |
| 1421 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1422 | fail = true; |
| 1423 | } |
| 1424 | mMediaRecorderRecording = false; |
Angus Kong | 9f1db52 | 2013-11-09 16:25:59 -0800 | [diff] [blame] | 1425 | mActivity.unlockOrientation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1426 | |
| 1427 | // If the activity is paused, this means activity is interrupted |
| 1428 | // during recording. Release the camera as soon as possible because |
| 1429 | // face unlock or other applications may need to use the camera. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1430 | if (mPaused) { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1431 | closeCamera(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1432 | } |
| 1433 | |
Doris Liu | fe6596c | 2013-10-08 11:03:37 -0700 | [diff] [blame] | 1434 | mUI.showRecordingUI(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1435 | // The orientation was fixed during video recording. Now make it |
| 1436 | // reflect the device orientation as video recording is stopped. |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1437 | mUI.setOrientationIndicator(0, true); |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 1438 | mActivity.enableKeepScreenOn(false); |
Doris Liu | 2a7f44c | 2013-08-12 15:18:53 -0700 | [diff] [blame] | 1439 | if (shouldAddToMediaStoreNow && !fail) { |
| 1440 | if (mVideoFileDescriptor == null) { |
| 1441 | saveVideo(); |
| 1442 | } else if (mIsVideoCaptureIntent) { |
| 1443 | // if no file save is needed, we can show the post capture UI now |
| 1444 | showCaptureResult(); |
| 1445 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1446 | } |
| 1447 | } |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1448 | // release media recorder |
| 1449 | releaseMediaRecorder(); |
Doris Liu | 4df9158 | 2014-03-21 18:33:57 -0700 | [diff] [blame] | 1450 | |
| 1451 | mAppController.getCameraAppUI().showModeOptions(); |
| 1452 | mAppController.getCameraAppUI().animateBottomBarToFullSize(mShutterIconId); |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1453 | if (!mPaused) { |
Doris Liu | 1143ebd | 2014-01-24 14:11:50 -0800 | [diff] [blame] | 1454 | setFocusParameters(); |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1455 | mCameraDevice.lock(); |
| 1456 | if (!ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) { |
| 1457 | stopPreview(); |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1458 | // Switch back to use SurfaceTexture for preview. |
| 1459 | startPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1460 | } |
Angus Kong | 62753ae | 2014-02-10 10:53:54 -0800 | [diff] [blame] | 1461 | // Update the parameters here because the parameters might have been altered |
| 1462 | // by MediaRecorder. |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1463 | mCameraSettings = mCameraDevice.getSettings(); |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 1464 | } |
Spike Sprague | 3f67379 | 2014-04-14 16:16:27 -0700 | [diff] [blame] | 1465 | |
Spike Sprague | e6374b7 | 2014-04-25 17:24:32 -0700 | [diff] [blame] | 1466 | // Check this in advance of each shot so we don't add to shutter |
| 1467 | // latency. It's true that someone else could write to the SD card |
| 1468 | // in the mean time and fill it, but that could have happened |
| 1469 | // between the shutter press and saving the file too. |
| 1470 | mActivity.updateStorageSpaceAndHint(null); |
Spike Sprague | 3f67379 | 2014-04-14 16:16:27 -0700 | [diff] [blame] | 1471 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1472 | return fail; |
| 1473 | } |
| 1474 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1475 | private static String millisecondToTimeString(long milliSeconds, boolean displayCentiSeconds) { |
| 1476 | long seconds = milliSeconds / 1000; // round down to compute seconds |
| 1477 | long minutes = seconds / 60; |
| 1478 | long hours = minutes / 60; |
| 1479 | long remainderMinutes = minutes - (hours * 60); |
| 1480 | long remainderSeconds = seconds - (minutes * 60); |
| 1481 | |
| 1482 | StringBuilder timeStringBuilder = new StringBuilder(); |
| 1483 | |
| 1484 | // Hours |
| 1485 | if (hours > 0) { |
| 1486 | if (hours < 10) { |
| 1487 | timeStringBuilder.append('0'); |
| 1488 | } |
| 1489 | timeStringBuilder.append(hours); |
| 1490 | |
| 1491 | timeStringBuilder.append(':'); |
| 1492 | } |
| 1493 | |
| 1494 | // Minutes |
| 1495 | if (remainderMinutes < 10) { |
| 1496 | timeStringBuilder.append('0'); |
| 1497 | } |
| 1498 | timeStringBuilder.append(remainderMinutes); |
| 1499 | timeStringBuilder.append(':'); |
| 1500 | |
| 1501 | // Seconds |
| 1502 | if (remainderSeconds < 10) { |
| 1503 | timeStringBuilder.append('0'); |
| 1504 | } |
| 1505 | timeStringBuilder.append(remainderSeconds); |
| 1506 | |
| 1507 | // Centi seconds |
| 1508 | if (displayCentiSeconds) { |
| 1509 | timeStringBuilder.append('.'); |
| 1510 | long remainderCentiSeconds = (milliSeconds - seconds * 1000) / 10; |
| 1511 | if (remainderCentiSeconds < 10) { |
| 1512 | timeStringBuilder.append('0'); |
| 1513 | } |
| 1514 | timeStringBuilder.append(remainderCentiSeconds); |
| 1515 | } |
| 1516 | |
| 1517 | return timeStringBuilder.toString(); |
| 1518 | } |
| 1519 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1520 | private void updateRecordingTime() { |
| 1521 | if (!mMediaRecorderRecording) { |
| 1522 | return; |
| 1523 | } |
| 1524 | long now = SystemClock.uptimeMillis(); |
| 1525 | long delta = now - mRecordingStartTime; |
| 1526 | |
| 1527 | // Starting a minute before reaching the max duration |
| 1528 | // limit, we'll countdown the remaining time instead. |
| 1529 | boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0 |
| 1530 | && delta >= mMaxVideoDurationInMs - 60000); |
| 1531 | |
| 1532 | long deltaAdjusted = delta; |
| 1533 | if (countdownRemainingTime) { |
| 1534 | deltaAdjusted = Math.max(0, mMaxVideoDurationInMs - deltaAdjusted) + 999; |
| 1535 | } |
| 1536 | String text; |
| 1537 | |
| 1538 | long targetNextUpdateDelay; |
Spike Sprague | d020fbf | 2014-07-16 15:47:32 -0700 | [diff] [blame] | 1539 | |
| 1540 | text = millisecondToTimeString(deltaAdjusted, false); |
| 1541 | targetNextUpdateDelay = 1000; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1542 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1543 | mUI.setRecordingTime(text); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1544 | |
| 1545 | if (mRecordingTimeCountsDown != countdownRemainingTime) { |
| 1546 | // Avoid setting the color on every update, do it only |
| 1547 | // when it needs changing. |
| 1548 | mRecordingTimeCountsDown = countdownRemainingTime; |
| 1549 | |
Spike Sprague | d020fbf | 2014-07-16 15:47:32 -0700 | [diff] [blame] | 1550 | int color = mActivity.getResources().getColor(R.color.recording_time_remaining_text); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1551 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1552 | mUI.setRecordingTimeTextColor(color); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | long actualNextUpdateDelay = targetNextUpdateDelay - (delta % targetNextUpdateDelay); |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 1556 | mHandler.sendEmptyMessageDelayed(MSG_UPDATE_RECORD_TIME, actualNextUpdateDelay); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | private static boolean isSupported(String value, List<String> supported) { |
| 1560 | return supported == null ? false : supported.indexOf(value) >= 0; |
| 1561 | } |
| 1562 | |
| 1563 | @SuppressWarnings("deprecation") |
| 1564 | private void setCameraParameters() { |
Erin Dahlgren | bd3da26 | 2013-12-02 11:48:13 -0800 | [diff] [blame] | 1565 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
| 1566 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1567 | mCameraSettings.setPreviewSize(new Size(mDesiredPreviewWidth, mDesiredPreviewHeight)); |
Andy Huibers | 6dcf90c | 2014-04-14 13:53:12 -0700 | [diff] [blame] | 1568 | // This is required for Samsung SGH-I337 and probably other Samsung S4 versions |
| 1569 | if (Build.BRAND.toLowerCase().contains("samsung")) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1570 | mCameraSettings.setSetting("video-size", |
| 1571 | mProfile.videoFrameWidth + "x" + mProfile.videoFrameHeight); |
Andy Huibers | 6dcf90c | 2014-04-14 13:53:12 -0700 | [diff] [blame] | 1572 | } |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1573 | int[] fpsRange = |
| 1574 | CameraUtil.getMaxPreviewFpsRange(mCameraCapabilities.getSupportedPreviewFpsRange()); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1575 | if (fpsRange.length > 0) { |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1576 | mCameraSettings.setPreviewFpsRange(fpsRange[0], fpsRange[1]); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1577 | } else { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1578 | mCameraSettings.setPreviewFrameRate(mProfile.videoFrameRate); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1579 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1580 | |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 1581 | enableTorchMode(Keys.isCameraBackFacing(settingsManager, mAppController.getModuleScope())); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1582 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1583 | // Set zoom. |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 1584 | if (mCameraCapabilities.supports(CameraCapabilities.Feature.ZOOM)) { |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 1585 | mCameraSettings.setZoomRatio(mZoomValue); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1586 | } |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1587 | updateFocusParameters(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1588 | |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1589 | mCameraSettings.setRecordingHintEnabled(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1590 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1591 | if (mCameraCapabilities.supports(CameraCapabilities.Feature.VIDEO_STABILIZATION)) { |
| 1592 | mCameraSettings.setVideoStabilization(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | // Set picture size. |
| 1596 | // The logic here is different from the logic in still-mode camera. |
| 1597 | // There we determine the preview size based on the picture size, but |
| 1598 | // here we determine the picture size based on the preview size. |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1599 | List<Size> supported = mCameraCapabilities.getSupportedPhotoSizes(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1600 | Size optimalSize = CameraUtil.getOptimalVideoSnapshotPictureSize(supported, |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1601 | (double) mDesiredPreviewWidth / mDesiredPreviewHeight); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1602 | Size original = new Size(mCameraSettings.getCurrentPhotoSize()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1603 | if (!original.equals(optimalSize)) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1604 | mCameraSettings.setPhotoSize(optimalSize); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1605 | } |
Angus Kong | 00b7b10 | 2014-04-24 15:46:52 -0700 | [diff] [blame] | 1606 | Log.d(TAG, "Video snapshot size is " + optimalSize); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1607 | |
| 1608 | // Set JPEG quality. |
| 1609 | int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId, |
| 1610 | CameraProfile.QUALITY_HIGH); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1611 | mCameraSettings.setPhotoJpegCompressionQuality(jpegQuality); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1612 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1613 | mCameraDevice.applySettings(mCameraSettings); |
Andy Huibers | 64abfe9 | 2014-01-14 22:25:52 -0800 | [diff] [blame] | 1614 | // Nexus 5 through KitKat 4.4.2 requires a second call to |
| 1615 | // .setParameters() for frame rate settings to take effect. |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1616 | mCameraDevice.applySettings(mCameraSettings); |
Sascha Haeberling | f8b877c | 2013-09-09 17:32:48 -0700 | [diff] [blame] | 1617 | |
| 1618 | // Update UI based on the new parameters. |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1619 | mUI.updateOnScreenIndicators(mCameraSettings); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1620 | } |
| 1621 | |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1622 | private void updateFocusParameters() { |
| 1623 | // Set continuous autofocus. During recording, we use "continuous-video" |
| 1624 | // auto focus mode to ensure smooth focusing. Whereas during preview (i.e. |
| 1625 | // before recording starts) we use "continuous-picture" auto focus mode |
| 1626 | // for faster but slightly jittery focusing. |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1627 | Set<CameraCapabilities.FocusMode> supportedFocus = mCameraCapabilities |
| 1628 | .getSupportedFocusModes(); |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1629 | if (mMediaRecorderRecording) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1630 | if (mCameraCapabilities.supports(CameraCapabilities.FocusMode.CONTINUOUS_VIDEO)) { |
| 1631 | mCameraSettings.setFocusMode(CameraCapabilities.FocusMode.CONTINUOUS_VIDEO); |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1632 | mFocusManager.overrideFocusMode(CameraCapabilities.FocusMode.CONTINUOUS_VIDEO); |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1633 | } else { |
| 1634 | mFocusManager.overrideFocusMode(null); |
| 1635 | } |
| 1636 | } else { |
Senpo Hu | 1118631 | 2014-08-08 13:42:13 -0700 | [diff] [blame] | 1637 | // FIXME(b/16984793): This is broken. For some reasons, CONTINUOUS_PICTURE is not on |
| 1638 | // when preview starts. |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1639 | mFocusManager.overrideFocusMode(null); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1640 | if (mCameraCapabilities.supports(CameraCapabilities.FocusMode.CONTINUOUS_PICTURE)) { |
Angus Kong | 831347d | 2014-06-16 16:07:28 -0700 | [diff] [blame] | 1641 | mCameraSettings.setFocusMode( |
| 1642 | mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode())); |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1643 | if (mFocusAreaSupported) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1644 | mCameraSettings.setFocusAreas(mFocusManager.getFocusAreas()); |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1645 | } |
| 1646 | } |
| 1647 | } |
| 1648 | updateAutoFocusMoveCallback(); |
| 1649 | } |
| 1650 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1651 | @Override |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1652 | public void resume() { |
Spike Sprague | 51c877c | 2014-02-18 11:14:12 -0800 | [diff] [blame] | 1653 | if (isVideoCaptureIntent()) { |
| 1654 | mDontResetIntentUiOnResume = mPaused; |
| 1655 | } |
| 1656 | |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1657 | mPaused = false; |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 1658 | installIntentFilter(); |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1659 | mAppController.setShutterEnabled(false); |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 1660 | mZoomValue = 1.0f; |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1661 | |
| 1662 | showVideoSnapshotUI(false); |
| 1663 | |
| 1664 | if (!mPreviewing) { |
| 1665 | requestCamera(mCameraId); |
| 1666 | } else { |
| 1667 | // preview already started |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1668 | mAppController.setShutterEnabled(true); |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1671 | if (mFocusManager != null) { |
| 1672 | // If camera is not open when resume is called, focus manager will not |
| 1673 | // be initialized yet, in which case it will start listening to |
| 1674 | // preview area size change later in the initialization. |
| 1675 | mAppController.addPreviewAreaSizeChangedListener(mFocusManager); |
| 1676 | } |
Sascha Haeberling | de30323 | 2014-02-07 02:30:53 +0100 | [diff] [blame] | 1677 | |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1678 | if (mPreviewing) { |
| 1679 | mOnResumeTime = SystemClock.uptimeMillis(); |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 1680 | mHandler.sendEmptyMessageDelayed(MSG_CHECK_DISPLAY_ROTATION, 100); |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1681 | } |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1682 | getServices().getMemoryManager().addListener(this); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | @Override |
| 1686 | public void pause() { |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1687 | mPaused = true; |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1688 | |
| 1689 | if (mFocusManager != null) { |
| 1690 | // If camera is not open when resume is called, focus manager will not |
| 1691 | // be initialized yet, in which case it will start listening to |
| 1692 | // preview area size change later in the initialization. |
| 1693 | mAppController.removePreviewAreaSizeChangedListener(mFocusManager); |
| 1694 | mFocusManager.removeMessages(); |
| 1695 | } |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1696 | if (mMediaRecorderRecording) { |
| 1697 | // Camera will be released in onStopVideoRecording. |
| 1698 | onStopVideoRecording(); |
| 1699 | } else { |
| 1700 | stopPreview(); |
| 1701 | closeCamera(); |
| 1702 | releaseMediaRecorder(); |
| 1703 | } |
| 1704 | |
| 1705 | closeVideoFileDescriptor(); |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1706 | |
| 1707 | if (mReceiver != null) { |
| 1708 | mActivity.unregisterReceiver(mReceiver); |
| 1709 | mReceiver = null; |
| 1710 | } |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1711 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 1712 | mHandler.removeMessages(MSG_CHECK_DISPLAY_ROTATION); |
| 1713 | mHandler.removeMessages(MSG_SWITCH_CAMERA); |
| 1714 | mHandler.removeMessages(MSG_SWITCH_CAMERA_START_ANIMATION); |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1715 | mPendingSwitchCameraId = -1; |
| 1716 | mSwitchingCamera = false; |
| 1717 | mPreferenceRead = false; |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1718 | getServices().getMemoryManager().removeListener(this); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | @Override |
| 1722 | public void destroy() { |
| 1723 | |
| 1724 | } |
| 1725 | |
| 1726 | @Override |
Angus Kong | 2f0e4a3 | 2013-12-03 10:02:35 -0800 | [diff] [blame] | 1727 | public void onLayoutOrientationChanged(boolean isLandscape) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1728 | setDisplayOrientation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1729 | } |
| 1730 | |
Erin Dahlgren | a07e94c | 2013-12-04 18:44:08 -0800 | [diff] [blame] | 1731 | // TODO: integrate this into the SettingsManager listeners. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1732 | public void onSharedPreferenceChanged() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1733 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | private void switchCamera() { |
Sascha Haeberling | f8b877c | 2013-09-09 17:32:48 -0700 | [diff] [blame] | 1737 | if (mPaused) { |
| 1738 | return; |
| 1739 | } |
Erin Dahlgren | bd3da26 | 2013-12-02 11:48:13 -0800 | [diff] [blame] | 1740 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1741 | |
| 1742 | Log.d(TAG, "Start to switch camera."); |
| 1743 | mCameraId = mPendingSwitchCameraId; |
| 1744 | mPendingSwitchCameraId = -1; |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 1745 | settingsManager.set(mAppController.getModuleScope(), |
| 1746 | Keys.KEY_CAMERA_ID, mCameraId); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1747 | |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1748 | if (mFocusManager != null) { |
| 1749 | mFocusManager.removeMessages(); |
| 1750 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1751 | closeCamera(); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1752 | requestCamera(mCameraId); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1753 | |
Sascha Haeberling | 6ccec20 | 2014-03-11 09:44:34 -0700 | [diff] [blame] | 1754 | mMirror = isCameraFrontFacing(); |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1755 | if (mFocusManager != null) { |
| 1756 | mFocusManager.setMirror(mMirror); |
| 1757 | } |
| 1758 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1759 | // From onResume |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 1760 | mZoomValue = 1.0f; |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1761 | mUI.setOrientationIndicator(0, false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1762 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1763 | // Start switch camera animation. Post a message because |
| 1764 | // onFrameAvailable from the old camera may already exist. |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame] | 1765 | mHandler.sendEmptyMessage(MSG_SWITCH_CAMERA_START_ANIMATION); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1766 | mUI.updateOnScreenIndicators(mCameraSettings); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1767 | } |
| 1768 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1769 | private void initializeVideoSnapshot() { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1770 | if (mCameraSettings == null) { |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 1771 | return; |
| 1772 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | void showVideoSnapshotUI(boolean enabled) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1776 | if (mCameraSettings == null) { |
Sascha Haeberling | 846d3ab | 2014-02-04 12:48:55 +0100 | [diff] [blame] | 1777 | return; |
| 1778 | } |
Angus Kong | 8828904 | 2014-04-22 16:39:42 -0700 | [diff] [blame] | 1779 | if (mCameraCapabilities.supports(CameraCapabilities.Feature.VIDEO_SNAPSHOT) && |
| 1780 | !mIsVideoCaptureIntent) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1781 | if (enabled) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1782 | mUI.animateFlash(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1783 | } else { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1784 | mUI.showPreviewBorder(enabled); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1785 | } |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1786 | mAppController.setShutterEnabled(!enabled); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | |
Erin Dahlgren | fce8a0b | 2013-12-12 17:36:32 -0800 | [diff] [blame] | 1790 | /** |
| 1791 | * Used to update the flash mode. Video mode can turn on the flash as torch |
| 1792 | * mode, which we would like to turn on and off when we switching in and |
| 1793 | * out to the preview. |
| 1794 | * |
| 1795 | * @param enable Whether torch mode can be enabled. |
| 1796 | */ |
| 1797 | private void enableTorchMode(boolean enable) { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1798 | if (mCameraSettings.getCurrentFlashMode() == null) { |
Erin Dahlgren | fce8a0b | 2013-12-12 17:36:32 -0800 | [diff] [blame] | 1799 | return; |
| 1800 | } |
| 1801 | |
Erin Dahlgren | bd3da26 | 2013-12-02 11:48:13 -0800 | [diff] [blame] | 1802 | SettingsManager settingsManager = mActivity.getSettingsManager(); |
| 1803 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1804 | CameraCapabilities.Stringifier stringifier = mCameraCapabilities.getStringifier(); |
| 1805 | CameraCapabilities.FlashMode flashMode; |
Erin Dahlgren | fce8a0b | 2013-12-12 17:36:32 -0800 | [diff] [blame] | 1806 | if (enable) { |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 1807 | flashMode = stringifier |
| 1808 | .flashModeFromString(settingsManager.getString(mAppController.getCameraScope(), |
| 1809 | Keys.KEY_VIDEOCAMERA_FLASH_MODE)); |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1810 | } else { |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1811 | flashMode = CameraCapabilities.FlashMode.OFF; |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1812 | } |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1813 | if (mCameraCapabilities.supports(flashMode)) { |
| 1814 | mCameraSettings.setFlashMode(flashMode); |
| 1815 | } |
| 1816 | /* TODO: Find out how to deal with the following code piece: |
| 1817 | else { |
| 1818 | flashMode = mCameraSettings.getCurrentFlashMode(); |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1819 | if (flashMode == null) { |
| 1820 | flashMode = mActivity.getString( |
| 1821 | R.string.pref_camera_flashmode_no_flash); |
Angus Kong | faaee01 | 2013-12-07 00:38:46 -0800 | [diff] [blame] | 1822 | mParameters.setFlashMode(flashMode); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1823 | } |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1824 | }*/ |
| 1825 | mCameraDevice.applySettings(mCameraSettings); |
| 1826 | mUI.updateOnScreenIndicators(mCameraSettings); |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1829 | @Override |
Sascha Haeberling | 8c1a922 | 2014-02-25 09:38:06 -0800 | [diff] [blame] | 1830 | public void onPreviewVisibilityChanged(int visibility) { |
Erin Dahlgren | fce8a0b | 2013-12-12 17:36:32 -0800 | [diff] [blame] | 1831 | if (mPreviewing) { |
Sascha Haeberling | 8c1a922 | 2014-02-25 09:38:06 -0800 | [diff] [blame] | 1832 | enableTorchMode(visibility == ModuleController.VISIBILITY_VISIBLE); |
Erin Dahlgren | fce8a0b | 2013-12-12 17:36:32 -0800 | [diff] [blame] | 1833 | } |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1836 | private final class JpegPictureCallback implements CameraPictureCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1837 | Location mLocation; |
| 1838 | |
| 1839 | public JpegPictureCallback(Location loc) { |
| 1840 | mLocation = loc; |
| 1841 | } |
| 1842 | |
| 1843 | @Override |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1844 | public void onPictureTaken(byte [] jpegData, CameraProxy camera) { |
Doris Liu | 057b21a | 2014-04-25 17:35:27 -0700 | [diff] [blame] | 1845 | Log.i(TAG, "Video snapshot taken."); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1846 | mSnapshotInProgress = false; |
| 1847 | showVideoSnapshotUI(false); |
| 1848 | storeImage(jpegData, mLocation); |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | private void storeImage(final byte[] data, Location loc) { |
| 1853 | long dateTaken = System.currentTimeMillis(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1854 | String title = CameraUtil.createJpegName(dateTaken); |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1855 | ExifInterface exif = Exif.getExif(data); |
| 1856 | int orientation = Exif.getOrientation(exif); |
Doris Liu | 6df2d96 | 2013-08-20 16:31:29 -0700 | [diff] [blame] | 1857 | |
Erin Dahlgren | 6190c36 | 2014-06-13 14:12:08 -0700 | [diff] [blame] | 1858 | String flashSetting = mActivity.getSettingsManager() |
| 1859 | .getString(mAppController.getCameraScope(), Keys.KEY_VIDEOCAMERA_FLASH_MODE); |
| 1860 | Boolean gridLinesOn = Keys.areGridLinesOn(mActivity.getSettingsManager()); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1861 | UsageStatistics.instance().photoCaptureDoneEvent( |
| 1862 | eventprotos.NavigationChange.Mode.VIDEO_STILL, title + ".jpeg", exif, |
Andy Huibers | b7c7d9a | 2014-06-18 22:26:14 -0700 | [diff] [blame] | 1863 | isCameraFrontFacing(), false, currentZoomValue(), flashSetting, gridLinesOn, |
| 1864 | null, null, null); |
Andy Huibers | 10c5816 | 2014-03-29 14:06:54 -0700 | [diff] [blame] | 1865 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1866 | getServices().getMediaSaver().addImage( |
Doris Liu | 6df2d96 | 2013-08-20 16:31:29 -0700 | [diff] [blame] | 1867 | data, title, dateTaken, loc, orientation, |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 1868 | exif, mOnPhotoSavedListener, mContentResolver); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1871 | private String convertOutputFormatToMimeType(int outputFileFormat) { |
| 1872 | if (outputFileFormat == MediaRecorder.OutputFormat.MPEG_4) { |
| 1873 | return "video/mp4"; |
| 1874 | } |
| 1875 | return "video/3gpp"; |
| 1876 | } |
| 1877 | |
| 1878 | private String convertOutputFormatToFileExt(int outputFileFormat) { |
| 1879 | if (outputFileFormat == MediaRecorder.OutputFormat.MPEG_4) { |
| 1880 | return ".mp4"; |
| 1881 | } |
| 1882 | return ".3gp"; |
| 1883 | } |
| 1884 | |
| 1885 | private void closeVideoFileDescriptor() { |
| 1886 | if (mVideoFileDescriptor != null) { |
| 1887 | try { |
| 1888 | mVideoFileDescriptor.close(); |
| 1889 | } catch (IOException e) { |
| 1890 | Log.e(TAG, "Fail to close fd", e); |
| 1891 | } |
| 1892 | mVideoFileDescriptor = null; |
| 1893 | } |
| 1894 | } |
| 1895 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1896 | @Override |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 1897 | public void onPreviewUIReady() { |
| 1898 | startPreview(); |
| 1899 | } |
| 1900 | |
| 1901 | @Override |
| 1902 | public void onPreviewUIDestroyed() { |
| 1903 | stopPreview(); |
| 1904 | } |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1905 | |
Doris Liu | 1dfe782 | 2013-12-12 00:02:08 -0800 | [diff] [blame] | 1906 | @Override |
| 1907 | public void startPreCaptureAnimation() { |
| 1908 | mAppController.startPreCaptureAnimation(); |
| 1909 | } |
| 1910 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1911 | private void requestCamera(int id) { |
| 1912 | mActivity.getCameraProvider().requestCamera(id); |
| 1913 | } |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1914 | |
| 1915 | @Override |
| 1916 | public void onMemoryStateChanged(int state) { |
Erin Dahlgren | 667630d | 2014-04-01 14:03:25 -0700 | [diff] [blame] | 1917 | mAppController.setShutterEnabled(state == MemoryManager.STATE_OK); |
Sascha Haeberling | a63dbb6 | 2013-11-22 11:55:32 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | @Override |
| 1921 | public void onLowMemory() { |
| 1922 | // Not much we can do in the video module. |
| 1923 | } |
| 1924 | |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1925 | /***********************FocusOverlayManager Listener****************************/ |
| 1926 | @Override |
| 1927 | public void autoFocus() { |
| 1928 | mCameraDevice.autoFocus(mHandler, mAutoFocusCallback); |
| 1929 | } |
| 1930 | |
| 1931 | @Override |
| 1932 | public void cancelAutoFocus() { |
| 1933 | mCameraDevice.cancelAutoFocus(); |
| 1934 | setFocusParameters(); |
| 1935 | } |
| 1936 | |
| 1937 | @Override |
| 1938 | public boolean capture() { |
| 1939 | return false; |
| 1940 | } |
| 1941 | |
| 1942 | @Override |
| 1943 | public void startFaceDetection() { |
| 1944 | |
| 1945 | } |
| 1946 | |
| 1947 | @Override |
| 1948 | public void stopFaceDetection() { |
| 1949 | |
| 1950 | } |
| 1951 | |
| 1952 | @Override |
| 1953 | public void setFocusParameters() { |
| 1954 | updateFocusParameters(); |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 1955 | mCameraDevice.applySettings(mCameraSettings); |
Doris Liu | a1ec04a | 2014-01-13 17:29:40 -0800 | [diff] [blame] | 1956 | } |
| 1957 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1958 | } |