blob: 5c30a8cf2c6498c8827bb308bf019b2a45c35ca2 [file] [log] [blame]
Michael Kolb8872c232013-01-29 10:33:22 -08001/*
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
17package com.android.camera;
18
19import android.annotation.TargetApi;
20import android.app.Activity;
21import android.content.ActivityNotFoundException;
22import android.content.BroadcastReceiver;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
Michael Kolb8872c232013-01-29 10:33:22 -080028import android.graphics.Bitmap;
Angus Kong5f8c30e2014-03-06 17:15:08 -080029import android.graphics.Point;
Michael Kolb8872c232013-01-29 10:33:22 -080030import android.graphics.SurfaceTexture;
Michael Kolb8872c232013-01-29 10:33:22 -080031import android.location.Location;
Marco Nelissen20694b22013-10-29 15:27:24 -070032import android.media.AudioManager;
Michael Kolb8872c232013-01-29 10:33:22 -080033import android.media.CamcorderProfile;
34import android.media.CameraProfile;
35import android.media.MediaRecorder;
36import android.net.Uri;
37import android.os.Build;
38import android.os.Bundle;
39import android.os.Handler;
Spike Sprague3c3b31d2014-09-08 10:43:04 -070040import android.os.Looper;
Michael Kolb8872c232013-01-29 10:33:22 -080041import android.os.Message;
42import android.os.ParcelFileDescriptor;
43import android.os.SystemClock;
44import android.provider.MediaStore;
Ruben Brunk16007962013-04-19 15:27:57 -070045import android.provider.MediaStore.MediaColumns;
Michael Kolb8872c232013-01-29 10:33:22 -080046import android.provider.MediaStore.Video;
Michael Kolb8872c232013-01-29 10:33:22 -080047import android.view.KeyEvent;
Michael Kolb8872c232013-01-29 10:33:22 -080048import android.view.View;
Michael Kolb8872c232013-01-29 10:33:22 -080049import android.widget.Toast;
50
Sascha Haeberling280fd3e2013-11-21 13:52:15 -080051import com.android.camera.app.AppController;
Spike Sprague39f8a762014-01-13 14:22:18 -080052import com.android.camera.app.CameraAppUI;
Kevin Gabayanffbc43c2013-12-09 11:41:50 -080053import com.android.camera.app.LocationManager;
Angus Kongfd4fc0e2013-11-07 15:38:09 -080054import com.android.camera.app.MediaSaver;
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -080055import com.android.camera.app.MemoryManager;
56import com.android.camera.app.MemoryManager.MemoryListener;
Senpo Hu0bdc4b52014-11-17 22:56:31 -080057import com.android.camera.app.OrientationManager;
Angus Kong2bca2102014-03-11 16:27:30 -070058import com.android.camera.debug.Log;
ztenghuia16e7b52013-08-23 11:47:56 -070059import com.android.camera.exif.ExifInterface;
Erin Dahlgrenb1641f52014-01-14 15:58:52 -080060import com.android.camera.hardware.HardwareSpec;
61import com.android.camera.hardware.HardwareSpecImpl;
Angus Kong20fad242013-11-11 18:23:46 -080062import com.android.camera.module.ModuleController;
Erin Dahlgren6190c362014-06-13 14:12:08 -070063import com.android.camera.settings.Keys;
Erin Dahlgrenbd3da262013-12-02 11:48:13 -080064import com.android.camera.settings.SettingsManager;
Sascha Haeberlingde303232014-02-07 02:30:53 +010065import com.android.camera.settings.SettingsUtil;
Andy Huibersb7c7d9a2014-06-18 22:26:14 -070066import com.android.camera.ui.TouchCoordinate;
Sascha Haeberling638e6f02013-09-18 14:28:51 -070067import com.android.camera.util.ApiHelper;
Angus Kongb50b5cb2013-08-09 14:55:20 -070068import com.android.camera.util.CameraUtil;
Sascha Haeberling8e963a52013-08-06 11:43:02 -070069import com.android.camera.util.UsageStatistics;
70import com.android.camera2.R;
Sol Boucher5a344962014-06-17 14:05:08 -070071import com.android.ex.camera2.portability.CameraAgent;
72import com.android.ex.camera2.portability.CameraAgent.CameraPictureCallback;
73import com.android.ex.camera2.portability.CameraAgent.CameraProxy;
Sascha Haeberlingf15d63e2014-07-21 11:31:13 -070074import com.android.ex.camera2.portability.CameraCapabilities;
Sol Boucher43e18132014-06-19 15:03:18 -070075import com.android.ex.camera2.portability.CameraDeviceInfo.Characteristics;
Sol Boucher5a344962014-06-17 14:05:08 -070076import com.android.ex.camera2.portability.CameraSettings;
77import com.android.ex.camera2.portability.Size;
Seth Raphael5e09d012013-12-18 13:45:03 -080078import com.google.common.logging.eventprotos;
Michael Kolb8872c232013-01-29 10:33:22 -080079
Angus Kongb50b5cb2013-08-09 14:55:20 -070080import java.io.File;
81import java.io.IOException;
82import java.text.SimpleDateFormat;
Angus Kong831347d2014-06-16 16:07:28 -070083import java.util.ArrayList;
Angus Kongb50b5cb2013-08-09 14:55:20 -070084import java.util.Date;
85import java.util.Iterator;
86import java.util.List;
Angus Kong6607dae2014-06-10 16:07:45 -070087import java.util.Set;
Angus Kongb50b5cb2013-08-09 14:55:20 -070088
Sascha Haeberling280fd3e2013-11-21 13:52:15 -080089public class VideoModule extends CameraModule
Senpo Hu0bdc4b52014-11-17 22:56:31 -080090 implements FocusOverlayManager.Listener, MediaRecorder.OnErrorListener,
91 MediaRecorder.OnInfoListener, MemoryListener,
92 OrientationManager.OnOrientationChangeListener, VideoController {
Michael Kolb8872c232013-01-29 10:33:22 -080093
Erin Dahlgren6190c362014-06-13 14:12:08 -070094 private static final String VIDEO_MODULE_STRING_ID = "VideoModule";
95
96 private static final Log.Tag TAG = new Log.Tag(VIDEO_MODULE_STRING_ID);
Michael Kolb8872c232013-01-29 10:33:22 -080097
Angus Kong13e87c42013-11-25 10:02:47 -080098 // Messages defined for the UI thread handler.
99 private static final int MSG_CHECK_DISPLAY_ROTATION = 4;
100 private static final int MSG_UPDATE_RECORD_TIME = 5;
101 private static final int MSG_ENABLE_SHUTTER_BUTTON = 6;
Angus Kong13e87c42013-11-25 10:02:47 -0800102 private static final int MSG_SWITCH_CAMERA = 8;
103 private static final int MSG_SWITCH_CAMERA_START_ANIMATION = 9;
Michael Kolb8872c232013-01-29 10:33:22 -0800104
105 private static final long SHUTTER_BUTTON_TIMEOUT = 500L; // 500ms
106
107 /**
108 * An unpublished intent flag requesting to start recording straight away
109 * and return as soon as recording is stopped.
110 * TODO: consider publishing by moving into MediaStore.
111 */
112 private static final String EXTRA_QUICK_CAPTURE =
113 "android.intent.extra.quickCapture";
114
Michael Kolb8872c232013-01-29 10:33:22 -0800115 // module fields
116 private CameraActivity mActivity;
Michael Kolb8872c232013-01-29 10:33:22 -0800117 private boolean mPaused;
Spike Sprague51c877c2014-02-18 11:14:12 -0800118
119 // if, during and intent capture, the activity is paused (e.g. when app switching or reviewing a
120 // shot video), we don't want the bottom bar intent ui to reset to the capture button
121 private boolean mDontResetIntentUiOnResume;
122
Michael Kolb8872c232013-01-29 10:33:22 -0800123 private int mCameraId;
Angus Kong6607dae2014-06-10 16:07:45 -0700124 private CameraSettings mCameraSettings;
Angus Kong88289042014-04-22 16:39:42 -0700125 private CameraCapabilities mCameraCapabilities;
Michael Kolb8872c232013-01-29 10:33:22 -0800126
Doris Liu6432cd62013-06-13 17:20:31 -0700127 private boolean mIsInReviewMode;
Michael Kolb8872c232013-01-29 10:33:22 -0800128 private boolean mSnapshotInProgress = false;
129
Angus Kong395ee2d2013-07-15 12:42:41 -0700130 // Preference must be read before starting preview. We check this before starting
131 // preview.
132 private boolean mPreferenceRead;
Michael Kolb8872c232013-01-29 10:33:22 -0800133
Michael Kolb8872c232013-01-29 10:33:22 -0800134 private boolean mIsVideoCaptureIntent;
135 private boolean mQuickCapture;
136
137 private MediaRecorder mMediaRecorder;
Sascha Haeberling92217bd2014-12-11 12:20:43 -0800138 /** Manager used to mute sounds and vibrations during video recording. */
139 private AudioManager mAudioManager;
140 /*
141 * The ringer mode that was set when video recording started. We use this to
142 * reset the mode once video recording has stopped.
143 */
144 private int mOriginalRingerMode;
Michael Kolb8872c232013-01-29 10:33:22 -0800145
146 private boolean mSwitchingCamera;
147 private boolean mMediaRecorderRecording = false;
148 private long mRecordingStartTime;
149 private boolean mRecordingTimeCountsDown = false;
Michael Kolb8872c232013-01-29 10:33:22 -0800150 private long mOnResumeTime;
151 // The video file that the hardware camera is about to record into
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800152 // (or is recording into.
Michael Kolb8872c232013-01-29 10:33:22 -0800153 private String mVideoFilename;
154 private ParcelFileDescriptor mVideoFileDescriptor;
155
156 // The video file that has already been recorded, and that is being
157 // examined by the user.
158 private String mCurrentVideoFilename;
159 private Uri mCurrentVideoUri;
ztenghui70bd0242013-10-14 11:15:44 -0700160 private boolean mCurrentVideoUriFromMediaSaved;
Michael Kolb8872c232013-01-29 10:33:22 -0800161 private ContentValues mCurrentVideoValues;
162
163 private CamcorderProfile mProfile;
164
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800165 // The video duration limit. 0 means no limit.
Michael Kolb8872c232013-01-29 10:33:22 -0800166 private int mMaxVideoDurationInMs;
167
Michael Kolb8872c232013-01-29 10:33:22 -0800168 boolean mPreviewing = false; // True if preview is started.
169 // The display rotation in degrees. This is only valid when mPreviewing is
170 // true.
171 private int mDisplayRotation;
172 private int mCameraDisplayOrientation;
Doris Liu2b906b82013-12-10 16:34:08 -0800173 private AppController mAppController;
Michael Kolb8872c232013-01-29 10:33:22 -0800174
Doris Liu6827ce22013-03-12 19:24:28 -0700175 private int mDesiredPreviewWidth;
176 private int mDesiredPreviewHeight;
Michael Kolb8872c232013-01-29 10:33:22 -0800177 private ContentResolver mContentResolver;
178
179 private LocationManager mLocationManager;
180
Michael Kolb8872c232013-01-29 10:33:22 -0800181 private int mPendingSwitchCameraId;
Michael Kolb8872c232013-01-29 10:33:22 -0800182 private final Handler mHandler = new MainHandler();
Doris Liu6827ce22013-03-12 19:24:28 -0700183 private VideoUI mUI;
Doris Liu6432cd62013-06-13 17:20:31 -0700184 private CameraProxy mCameraDevice;
185
Sol Boucher2192fba2014-08-19 17:24:07 -0700186 private float mZoomValue; // The current zoom ratio.
Doris Liu6827ce22013-03-12 19:24:28 -0700187
Angus Kongfd4fc0e2013-11-07 15:38:09 -0800188 private final MediaSaver.OnMediaSavedListener mOnVideoSavedListener =
189 new MediaSaver.OnMediaSavedListener() {
Angus Kong83a99ae2013-04-17 15:37:07 -0700190 @Override
191 public void onMediaSaved(Uri uri) {
192 if (uri != null) {
Doris Liu2a7f44c2013-08-12 15:18:53 -0700193 mCurrentVideoUri = uri;
ztenghui70bd0242013-10-14 11:15:44 -0700194 mCurrentVideoUriFromMediaSaved = true;
Doris Liu2a7f44c2013-08-12 15:18:53 -0700195 onVideoSaved();
Sascha Haeberling37f36112013-08-06 14:31:52 -0700196 mActivity.notifyNewMedia(uri);
Angus Kong83a99ae2013-04-17 15:37:07 -0700197 }
198 }
199 };
200
Angus Kongfd4fc0e2013-11-07 15:38:09 -0800201 private final MediaSaver.OnMediaSavedListener mOnPhotoSavedListener =
202 new MediaSaver.OnMediaSavedListener() {
Angus Kong86d36312013-01-31 18:22:44 -0800203 @Override
204 public void onMediaSaved(Uri uri) {
205 if (uri != null) {
Sascha Haeberling37f36112013-08-06 14:31:52 -0700206 mActivity.notifyNewMedia(uri);
Angus Kong86d36312013-01-31 18:22:44 -0800207 }
208 }
209 };
Doris Liua1ec04a2014-01-13 17:29:40 -0800210 private FocusOverlayManager mFocusManager;
211 private boolean mMirror;
Doris Liua1ec04a2014-01-13 17:29:40 -0800212 private boolean mFocusAreaSupported;
213 private boolean mMeteringAreaSupported;
214
Sol Boucher5a344962014-06-17 14:05:08 -0700215 private final CameraAgent.CameraAFCallback mAutoFocusCallback =
216 new CameraAgent.CameraAFCallback() {
Doris Liua1ec04a2014-01-13 17:29:40 -0800217 @Override
218 public void onAutoFocus(boolean focused, CameraProxy camera) {
219 if (mPaused) {
220 return;
221 }
222 mFocusManager.onAutoFocus(focused, false);
223 }
224 };
225
226 private final Object mAutoFocusMoveCallback =
227 ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK
Sol Boucher5a344962014-06-17 14:05:08 -0700228 ? new CameraAgent.CameraAFMoveCallback() {
Doris Liua1ec04a2014-01-13 17:29:40 -0800229 @Override
230 public void onAutoFocusMoving(boolean moving, CameraProxy camera) {
Andy Huibersc0fe0b62014-08-25 22:25:49 -0700231 // mFocusManager.onAutoFocusMoving(moving) not called because UI
232 // not compatible with vertical video hint UI.
Doris Liua1ec04a2014-01-13 17:29:40 -0800233 }
234 } : null;
Angus Kong86d36312013-01-31 18:22:44 -0800235
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -0800236 /**
237 * This Handler is used to post message back onto the main thread of the
238 * application.
239 */
Michael Kolb8872c232013-01-29 10:33:22 -0800240 private class MainHandler extends Handler {
241 @Override
242 public void handleMessage(Message msg) {
243 switch (msg.what) {
244
Angus Kong13e87c42013-11-25 10:02:47 -0800245 case MSG_ENABLE_SHUTTER_BUTTON:
Erin Dahlgren667630d2014-04-01 14:03:25 -0700246 mAppController.setShutterEnabled(true);
Michael Kolb8872c232013-01-29 10:33:22 -0800247 break;
248
Angus Kong13e87c42013-11-25 10:02:47 -0800249 case MSG_UPDATE_RECORD_TIME: {
Michael Kolb8872c232013-01-29 10:33:22 -0800250 updateRecordingTime();
251 break;
252 }
253
Angus Kong13e87c42013-11-25 10:02:47 -0800254 case MSG_CHECK_DISPLAY_ROTATION: {
Michael Kolb8872c232013-01-29 10:33:22 -0800255 // Restart the preview if display rotation has changed.
256 // Sometimes this happens when the device is held upside
257 // down and camera app is opened. Rotation animation will
258 // take some time and the rotation value we have got may be
259 // wrong. Framework does not have a callback for this now.
Angus Kongb50b5cb2013-08-09 14:55:20 -0700260 if ((CameraUtil.getDisplayRotation(mActivity) != mDisplayRotation)
Michael Kolb8872c232013-01-29 10:33:22 -0800261 && !mMediaRecorderRecording && !mSwitchingCamera) {
262 startPreview();
263 }
264 if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) {
Angus Kong13e87c42013-11-25 10:02:47 -0800265 mHandler.sendEmptyMessageDelayed(MSG_CHECK_DISPLAY_ROTATION, 100);
Michael Kolb8872c232013-01-29 10:33:22 -0800266 }
267 break;
268 }
269
Angus Kong13e87c42013-11-25 10:02:47 -0800270 case MSG_SWITCH_CAMERA: {
Michael Kolb8872c232013-01-29 10:33:22 -0800271 switchCamera();
272 break;
273 }
274
Angus Kong13e87c42013-11-25 10:02:47 -0800275 case MSG_SWITCH_CAMERA_START_ANIMATION: {
Doris Liu6432cd62013-06-13 17:20:31 -0700276 //TODO:
277 //((CameraScreenNail) mActivity.mCameraScreenNail).animateSwitchCamera();
Michael Kolb8872c232013-01-29 10:33:22 -0800278
279 // Enable all camera controls.
280 mSwitchingCamera = false;
281 break;
282 }
283
Michael Kolb8872c232013-01-29 10:33:22 -0800284 default:
285 Log.v(TAG, "Unhandled message: " + msg.what);
286 break;
287 }
288 }
289 }
290
291 private BroadcastReceiver mReceiver = null;
292
293 private class MyBroadcastReceiver extends BroadcastReceiver {
294 @Override
295 public void onReceive(Context context, Intent intent) {
296 String action = intent.getAction();
297 if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
298 stopVideoRecording();
299 } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
300 Toast.makeText(mActivity,
301 mActivity.getResources().getString(R.string.wait), Toast.LENGTH_LONG).show();
302 }
303 }
304 }
305
Spike Sprague41f68002014-01-24 16:59:25 -0800306 private int mShutterIconId;
307
308
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800309 /**
310 * Construct a new video module.
311 */
Angus Kongc4e66562013-11-22 23:03:21 -0800312 public VideoModule(AppController app) {
313 super(app);
Sascha Haeberling280fd3e2013-11-21 13:52:15 -0800314 }
315
Spike Sprague159e6e92014-05-27 18:26:30 -0700316 @Override
317 public String getPeekAccessibilityString() {
318 return mAppController.getAndroidContext()
319 .getResources().getString(R.string.video_accessibility_peek);
320 }
321
Michael Kolb8872c232013-01-29 10:33:22 -0800322 private String createName(long dateTaken) {
323 Date date = new Date(dateTaken);
324 SimpleDateFormat dateFormat = new SimpleDateFormat(
325 mActivity.getString(R.string.video_file_name_format));
326
327 return dateFormat.format(date);
328 }
329
Michael Kolb8872c232013-01-29 10:33:22 -0800330 @Override
Erin Dahlgren6190c362014-06-13 14:12:08 -0700331 public String getModuleStringIdentifier() {
332 return VIDEO_MODULE_STRING_ID;
333 }
334
335 @Override
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100336 public void init(CameraActivity activity, boolean isSecureCamera, boolean isCaptureIntent) {
337 mActivity = activity;
338 // TODO: Need to look at the controller interface to see if we can get
339 // rid of passing in the activity directly.
340 mAppController = mActivity;
Sascha Haeberling92217bd2014-12-11 12:20:43 -0800341 mAudioManager = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE);
Spike Sprague3f673792014-04-14 16:16:27 -0700342
Spike Sprague48a38062014-04-29 18:04:02 -0700343 mActivity.updateStorageSpaceAndHint(null);
Spike Sprague3f673792014-04-14 16:16:27 -0700344
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100345 mUI = new VideoUI(mActivity, this, mActivity.getModuleLayoutRoot());
346 mActivity.setPreviewStatusListener(mUI);
Michael Kolb8872c232013-01-29 10:33:22 -0800347
Erin Dahlgrenbd3da262013-12-02 11:48:13 -0800348 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren6190c362014-06-13 14:12:08 -0700349 mCameraId = settingsManager.getInteger(mAppController.getModuleScope(),
350 Keys.KEY_CAMERA_ID);
Michael Kolb8872c232013-01-29 10:33:22 -0800351
Michael Kolb8872c232013-01-29 10:33:22 -0800352 /*
353 * To reduce startup time, we start the preview in another thread.
354 * We make sure the preview is started at the end of onCreate.
355 */
Angus Kong20fad242013-11-11 18:23:46 -0800356 requestCamera(mCameraId);
Michael Kolb8872c232013-01-29 10:33:22 -0800357
358 mContentResolver = mActivity.getContentResolver();
359
Michael Kolb8872c232013-01-29 10:33:22 -0800360 // Surface texture is from camera screen nail and startPreview needs it.
361 // This must be done before startPreview.
362 mIsVideoCaptureIntent = isVideoCaptureIntent();
Michael Kolb8872c232013-01-29 10:33:22 -0800363
Michael Kolb8872c232013-01-29 10:33:22 -0800364 mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
Erin Dahlgren21c21a62013-11-19 16:37:38 -0800365 mLocationManager = mActivity.getLocationManager();
Michael Kolb8872c232013-01-29 10:33:22 -0800366
Doris Liu6827ce22013-03-12 19:24:28 -0700367 mUI.setOrientationIndicator(0, false);
Michael Kolb8872c232013-01-29 10:33:22 -0800368 setDisplayOrientation();
369
Michael Kolb8872c232013-01-29 10:33:22 -0800370 mPendingSwitchCameraId = -1;
Spike Sprague41f68002014-01-24 16:59:25 -0800371
372 mShutterIconId = CameraUtil.getCameraShutterIconId(
373 mAppController.getCurrentModuleIndex(), mAppController.getAndroidContext());
Doris Liu6827ce22013-03-12 19:24:28 -0700374 }
375
Erin Dahlgren4efa8b52013-12-17 18:31:35 -0800376 @Override
377 public boolean isUsingBottomBar() {
378 return true;
379 }
380
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800381 private void initializeControlByIntent() {
382 if (isVideoCaptureIntent()) {
Spike Sprague51c877c2014-02-18 11:14:12 -0800383 if (!mDontResetIntentUiOnResume) {
384 mActivity.getCameraAppUI().transitionToIntentCaptureLayout();
385 }
386 // reset the flag
387 mDontResetIntentUiOnResume = false;
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800388 }
389 }
390
Doris Liu6827ce22013-03-12 19:24:28 -0700391 @Override
392 public void onSingleTapUp(View view, int x, int y) {
Doris Liu057b21a2014-04-25 17:35:27 -0700393 if (mPaused || mCameraDevice == null) {
394 return;
395 }
396 if (mMediaRecorderRecording) {
397 if (!mSnapshotInProgress) {
398 takeASnapshot();
399 }
Doris Liua1ec04a2014-01-13 17:29:40 -0800400 return;
401 }
402 // Check if metering area or focus area is supported.
403 if (!mFocusAreaSupported && !mMeteringAreaSupported) {
404 return;
405 }
406 // Tap to focus.
407 mFocusManager.onSingleTapUp(x, y);
Doris Liu38605742013-08-13 15:01:52 -0700408 }
409
410 private void takeASnapshot() {
Sascha Haeberlinga514b142013-08-13 16:10:01 -0700411 // Only take snapshots if video snapshot is supported by device
Angus Kong88289042014-04-22 16:39:42 -0700412 if(!mCameraCapabilities.supports(CameraCapabilities.Feature.VIDEO_SNAPSHOT)) {
Doris Liu46836732014-04-29 18:15:55 -0700413 Log.w(TAG, "Cannot take a video snapshot - not supported by hardware");
414 return;
415 }
416 if (!mIsVideoCaptureIntent) {
Erin Dahlgren667630d2014-04-01 14:03:25 -0700417 if (!mMediaRecorderRecording || mPaused || mSnapshotInProgress
Senpo Hu4ba28292014-09-26 11:05:08 -0700418 || !mAppController.isShutterEnabled() || mCameraDevice == null) {
Doris Liu38605742013-08-13 15:01:52 -0700419 return;
420 }
421
Doris Liu38605742013-08-13 15:01:52 -0700422 Location loc = mLocationManager.getCurrentLocation();
Angus Kong6607dae2014-06-10 16:07:45 -0700423 CameraUtil.setGpsParameters(mCameraSettings, loc);
424 mCameraDevice.applySettings(mCameraSettings);
Doris Liu38605742013-08-13 15:01:52 -0700425
Doris Liu057b21a2014-04-25 17:35:27 -0700426 Log.i(TAG, "Video snapshot start");
Doris Liu38605742013-08-13 15:01:52 -0700427 mCameraDevice.takePicture(mHandler,
428 null, null, null, new JpegPictureCallback(loc));
429 showVideoSnapshotUI(true);
430 mSnapshotInProgress = true;
Doris Liu6827ce22013-03-12 19:24:28 -0700431 }
Michael Kolb8872c232013-01-29 10:33:22 -0800432 }
433
Doris Liua1ec04a2014-01-13 17:29:40 -0800434 private void updateAutoFocusMoveCallback() {
Senpo Hu4ba28292014-09-26 11:05:08 -0700435 if (mPaused || mCameraDevice == null) {
Sascha Haeberling8793eff2014-01-15 16:33:59 -0800436 return;
437 }
438
Angus Kong6607dae2014-06-10 16:07:45 -0700439 if (mCameraSettings.getCurrentFocusMode() == CameraCapabilities.FocusMode.CONTINUOUS_PICTURE) {
Doris Liua1ec04a2014-01-13 17:29:40 -0800440 mCameraDevice.setAutoFocusMoveCallback(mHandler,
Sol Boucher5a344962014-06-17 14:05:08 -0700441 (CameraAgent.CameraAFMoveCallback) mAutoFocusMoveCallback);
Doris Liua1ec04a2014-01-13 17:29:40 -0800442 } else {
443 mCameraDevice.setAutoFocusMoveCallback(null, null);
444 }
445 }
446
447 /**
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700448 * @return Whether the currently active camera is front-facing.
449 */
450 private boolean isCameraFrontFacing() {
Sol Boucher43e18132014-06-19 15:03:18 -0700451 return mAppController.getCameraProvider().getCharacteristics(mCameraId)
452 .isFacingFront();
453 }
454
455 /**
456 * @return Whether the currently active camera is back-facing.
457 */
458 private boolean isCameraBackFacing() {
459 return mAppController.getCameraProvider().getCharacteristics(mCameraId)
460 .isFacingBack();
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700461 }
462
463 /**
Doris Liua1ec04a2014-01-13 17:29:40 -0800464 * The focus manager gets initialized after camera is available.
465 */
466 private void initializeFocusManager() {
467 // Create FocusManager object. startPreview needs it.
468 // if mFocusManager not null, reuse it
469 // otherwise create a new instance
470 if (mFocusManager != null) {
471 mFocusManager.removeMessages();
472 } else {
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700473 mMirror = isCameraFrontFacing();
Angus Kong831347d2014-06-16 16:07:28 -0700474 String[] defaultFocusModesStrings = mActivity.getResources().getStringArray(
Doris Liua1ec04a2014-01-13 17:29:40 -0800475 R.array.pref_camera_focusmode_default_array);
Angus Kong831347d2014-06-16 16:07:28 -0700476 CameraCapabilities.Stringifier stringifier = mCameraCapabilities.getStringifier();
Sascha Haeberlingee36c5f2014-07-29 17:05:43 -0700477 ArrayList<CameraCapabilities.FocusMode> defaultFocusModes =
478 new ArrayList<CameraCapabilities.FocusMode>();
Angus Kong831347d2014-06-16 16:07:28 -0700479 for (String modeString : defaultFocusModesStrings) {
480 CameraCapabilities.FocusMode mode = stringifier.focusModeFromString(modeString);
481 if (mode != null) {
482 defaultFocusModes.add(mode);
483 }
484 }
Erin Dahlgren6190c362014-06-13 14:12:08 -0700485 mFocusManager = new FocusOverlayManager(mAppController,
Angus Kong831347d2014-06-16 16:07:28 -0700486 defaultFocusModes, mCameraCapabilities, this, mMirror,
Paul Rohde987ee642014-12-05 12:17:15 -0800487 mActivity.getMainLooper(), mUI.getFocusRing());
Doris Liua1ec04a2014-01-13 17:29:40 -0800488 }
489 mAppController.addPreviewAreaSizeChangedListener(mFocusManager);
490 }
491
Michael Kolb8872c232013-01-29 10:33:22 -0800492 @Override
Senpo Hu0bdc4b52014-11-17 22:56:31 -0800493 public void onOrientationChanged(OrientationManager orientationManager,
494 OrientationManager.DeviceOrientation deviceOrientation) {
495 mUI.onOrientationChanged(orientationManager, deviceOrientation);
Michael Kolb8872c232013-01-29 10:33:22 -0800496 }
497
Erin Dahlgren0a6a8d82014-01-09 22:17:38 -0800498 private final ButtonManager.ButtonCallback mFlashCallback =
Erin Dahlgrenfce8a0b2013-12-12 17:36:32 -0800499 new ButtonManager.ButtonCallback() {
500 @Override
501 public void onStateChanged(int state) {
502 // Update flash parameters.
503 enableTorchMode(true);
504 }
505 };
506
Erin Dahlgren0a6a8d82014-01-09 22:17:38 -0800507 private final ButtonManager.ButtonCallback mCameraCallback =
Erin Dahlgren18e2ef62013-12-05 14:53:38 -0800508 new ButtonManager.ButtonCallback() {
509 @Override
510 public void onStateChanged(int state) {
Angus Kong97e282a2014-03-04 18:44:49 -0800511 if (mPaused || mAppController.getCameraProvider().waitingForCamera()) {
Erin Dahlgren18e2ef62013-12-05 14:53:38 -0800512 return;
513 }
514 mPendingSwitchCameraId = state;
515 Log.d(TAG, "Start to copy texture.");
516
517 // Disable all camera controls.
518 mSwitchingCamera = true;
519 switchCamera();
520 }
521 };
522
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800523 private final View.OnClickListener mCancelCallback = new View.OnClickListener() {
524 @Override
525 public void onClick(View v) {
526 onReviewCancelClicked(v);
527 }
528 };
529
530 private final View.OnClickListener mDoneCallback = new View.OnClickListener() {
531 @Override
532 public void onClick(View v) {
533 onReviewDoneClicked(v);
534 }
535 };
536 private final View.OnClickListener mReviewCallback = new View.OnClickListener() {
537 @Override
538 public void onClick(View v) {
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800539 onReviewPlayClicked(v);
540 }
541 };
542
Angus Kong20fad242013-11-11 18:23:46 -0800543 @Override
Erin Dahlgren1ca516f2014-03-28 12:44:04 -0700544 public void hardResetSettings(SettingsManager settingsManager) {
545 // VideoModule does not need to hard reset any settings.
546 }
547
548 @Override
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800549 public HardwareSpec getHardwareSpec() {
Angus Kong831347d2014-06-16 16:07:28 -0700550 return (mCameraSettings != null ?
551 new HardwareSpecImpl(getCameraProvider(), mCameraCapabilities) : null);
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800552 }
553
554 @Override
555 public CameraAppUI.BottomBarUISpec getBottomBarSpec() {
556 CameraAppUI.BottomBarUISpec bottomBarSpec = new CameraAppUI.BottomBarUISpec();
557
558 bottomBarSpec.enableCamera = true;
559 bottomBarSpec.cameraCallback = mCameraCallback;
560 bottomBarSpec.enableTorchFlash = true;
561 bottomBarSpec.flashCallback = mFlashCallback;
562 bottomBarSpec.hideHdr = true;
Spike Spragueab3adde2014-03-10 12:19:08 -0700563 bottomBarSpec.enableGridLines = true;
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800564
565 if (isVideoCaptureIntent()) {
566 bottomBarSpec.showCancel = true;
567 bottomBarSpec.cancelCallback = mCancelCallback;
568 bottomBarSpec.showDone = true;
569 bottomBarSpec.doneCallback = mDoneCallback;
570 bottomBarSpec.showReview = true;
571 bottomBarSpec.reviewCallback = mReviewCallback;
572 }
573
574 return bottomBarSpec;
Erin Dahlgren0a6a8d82014-01-09 22:17:38 -0800575 }
576
577 @Override
Angus Kong20fad242013-11-11 18:23:46 -0800578 public void onCameraAvailable(CameraProxy cameraProxy) {
Senpo Hu4ba28292014-09-26 11:05:08 -0700579 if (cameraProxy == null) {
580 Log.w(TAG, "onCameraAvailable returns a null CameraProxy object");
581 return;
582 }
Angus Kong20fad242013-11-11 18:23:46 -0800583 mCameraDevice = cameraProxy;
Angus Kong88289042014-04-22 16:39:42 -0700584 mCameraCapabilities = mCameraDevice.getCapabilities();
Angus Kong831347d2014-06-16 16:07:28 -0700585 mCameraSettings = mCameraDevice.getSettings();
586 mFocusAreaSupported = mCameraCapabilities.supports(CameraCapabilities.Feature.FOCUS_AREA);
587 mMeteringAreaSupported =
588 mCameraCapabilities.supports(CameraCapabilities.Feature.METERING_AREA);
Angus Kong20fad242013-11-11 18:23:46 -0800589 readVideoPreferences();
I-Jong Lin4ed861a2014-10-10 16:34:34 -0700590 updateDesiredPreviewSize();
Angus Kong20fad242013-11-11 18:23:46 -0800591 resizeForPreviewAspectRatio();
Doris Liua1ec04a2014-01-13 17:29:40 -0800592 initializeFocusManager();
Doris Liu5dd85a12014-04-15 11:45:55 -0700593 // TODO: Having focus overlay manager caching the parameters is prone to error,
594 // we should consider passing the parameters to focus overlay to ensure the
595 // parameters are up to date.
Angus Kong831347d2014-06-16 16:07:28 -0700596 mFocusManager.updateCapabilities(mCameraCapabilities);
Doris Liu5a367542014-01-17 17:21:42 -0800597
Angus Kong20fad242013-11-11 18:23:46 -0800598 startPreview();
599 initializeVideoSnapshot();
Angus Kong6607dae2014-06-10 16:07:45 -0700600 mUI.initializeZoom(mCameraSettings, mCameraCapabilities);
Erin Dahlgrenb1641f52014-01-14 15:58:52 -0800601 initializeControlByIntent();
Angus Kong20fad242013-11-11 18:23:46 -0800602 }
603
Michael Kolb8872c232013-01-29 10:33:22 -0800604 private void startPlayVideoActivity() {
605 Intent intent = new Intent(Intent.ACTION_VIEW);
606 intent.setDataAndType(mCurrentVideoUri, convertOutputFormatToMimeType(mProfile.fileFormat));
607 try {
Senpo Hube707312014-09-10 14:43:16 -0700608 mActivity.launchActivityByIntent(intent);
Michael Kolb8872c232013-01-29 10:33:22 -0800609 } catch (ActivityNotFoundException ex) {
610 Log.e(TAG, "Couldn't view video " + mCurrentVideoUri, ex);
611 }
612 }
613
ztenghui7b265a62013-09-09 14:58:44 -0700614 @Override
Michael Kolb8872c232013-01-29 10:33:22 -0800615 public void onReviewPlayClicked(View v) {
616 startPlayVideoActivity();
617 }
618
ztenghui7b265a62013-09-09 14:58:44 -0700619 @Override
Michael Kolb8872c232013-01-29 10:33:22 -0800620 public void onReviewDoneClicked(View v) {
Doris Liu69ef5ea2013-05-07 13:48:10 -0700621 mIsInReviewMode = false;
Michael Kolb8872c232013-01-29 10:33:22 -0800622 doReturnToCaller(true);
623 }
624
ztenghui7b265a62013-09-09 14:58:44 -0700625 @Override
Michael Kolb8872c232013-01-29 10:33:22 -0800626 public void onReviewCancelClicked(View v) {
ztenghuiaf3a1972013-09-17 16:51:15 -0700627 // TODO: It should be better to not even insert the URI at all before we
628 // confirm done in review, which means we need to handle temporary video
629 // files in a quite different way than we currently had.
ztenghui70bd0242013-10-14 11:15:44 -0700630 // Make sure we don't delete the Uri sent from the video capture intent.
631 if (mCurrentVideoUriFromMediaSaved) {
ztenghuidfe5b152013-10-08 17:07:15 -0700632 mContentResolver.delete(mCurrentVideoUri, null, null);
633 }
ztenghui638bf9a2013-10-09 10:52:33 -0700634 mIsInReviewMode = false;
Michael Kolb8872c232013-01-29 10:33:22 -0800635 doReturnToCaller(false);
636 }
637
Doris Liu69ef5ea2013-05-07 13:48:10 -0700638 @Override
639 public boolean isInReviewMode() {
640 return mIsInReviewMode;
641 }
642
Michael Kolb8872c232013-01-29 10:33:22 -0800643 private void onStopVideoRecording() {
Sascha Haeberling8793eff2014-01-15 16:33:59 -0800644 mAppController.getCameraAppUI().setSwipeEnabled(true);
Michael Kolb8872c232013-01-29 10:33:22 -0800645 boolean recordFail = stopVideoRecording();
646 if (mIsVideoCaptureIntent) {
Doris Liu3973deb2013-08-21 13:42:22 -0700647 if (mQuickCapture) {
648 doReturnToCaller(!recordFail);
649 } else if (!recordFail) {
650 showCaptureResult();
Michael Kolb8872c232013-01-29 10:33:22 -0800651 }
652 } else if (!recordFail){
653 // Start capture animation.
654 if (!mPaused && ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) {
655 // The capture animation is disabled on ICS because we use SurfaceView
656 // for preview during recording. When the recording is done, we switch
657 // back to use SurfaceTexture for preview and we need to stop then start
658 // the preview. This will cause the preview flicker since the preview
659 // will not be continuous for a short period of time.
Senpo Hu427a9152014-11-19 17:11:46 -0800660 mAppController.startFlashAnimation(false);
Michael Kolb8872c232013-01-29 10:33:22 -0800661 }
662 }
663 }
664
Doris Liu2a7f44c2013-08-12 15:18:53 -0700665 public void onVideoSaved() {
666 if (mIsVideoCaptureIntent) {
667 showCaptureResult();
668 }
669 }
670
Michael Kolb8872c232013-01-29 10:33:22 -0800671 public void onProtectiveCurtainClick(View v) {
672 // Consume clicks
673 }
674
675 @Override
676 public void onShutterButtonClick() {
Sascha Haeberling8793eff2014-01-15 16:33:59 -0800677 if (mSwitchingCamera) {
678 return;
679 }
Michael Kolb8872c232013-01-29 10:33:22 -0800680 boolean stop = mMediaRecorderRecording;
681
682 if (stop) {
Spike Spraguec19d97b2014-09-22 14:53:04 -0700683 // CameraAppUI mishandles mode option enable/disable
684 // for video, override that
685 mAppController.getCameraAppUI().enableModeOptions();
Michael Kolb8872c232013-01-29 10:33:22 -0800686 onStopVideoRecording();
687 } else {
Spike Spraguec19d97b2014-09-22 14:53:04 -0700688 // CameraAppUI mishandles mode option enable/disable
689 // for video, override that
690 mAppController.getCameraAppUI().disableModeOptions();
Michael Kolb8872c232013-01-29 10:33:22 -0800691 startVideoRecording();
692 }
Erin Dahlgren667630d2014-04-01 14:03:25 -0700693 mAppController.setShutterEnabled(false);
Senpo Hu36e045a2014-09-29 18:49:10 -0700694 if (mCameraSettings != null) {
695 mFocusManager.onShutterUp(mCameraSettings.getCurrentFocusMode());
696 }
Michael Kolb8872c232013-01-29 10:33:22 -0800697
698 // Keep the shutter button disabled when in video capture intent
699 // mode and recording is stopped. It'll be re-enabled when
700 // re-take button is clicked.
701 if (!(mIsVideoCaptureIntent && stop)) {
Angus Kong13e87c42013-11-25 10:02:47 -0800702 mHandler.sendEmptyMessageDelayed(MSG_ENABLE_SHUTTER_BUTTON, SHUTTER_BUTTON_TIMEOUT);
Michael Kolb8872c232013-01-29 10:33:22 -0800703 }
704 }
705
706 @Override
Andy Huibersb7c7d9a2014-06-18 22:26:14 -0700707 public void onShutterCoordinate(TouchCoordinate coord) {
708 // Do nothing.
709 }
710
711 @Override
Michael Kolb8872c232013-01-29 10:33:22 -0800712 public void onShutterButtonFocus(boolean pressed) {
Doris Liuf55f3c42013-11-20 00:24:46 -0800713 // TODO: Remove this when old camera controls are removed from the UI.
Michael Kolb8872c232013-01-29 10:33:22 -0800714 }
715
716 private void readVideoPreferences() {
717 // The preference stores values from ListPreference and is thus string type for all values.
718 // We need to convert it to int manually.
Erin Dahlgrenbd3da262013-12-02 11:48:13 -0800719 SettingsManager settingsManager = mActivity.getSettingsManager();
Erin Dahlgren6190c362014-06-13 14:12:08 -0700720 String videoQualityKey = isCameraFrontFacing() ? Keys.KEY_VIDEO_QUALITY_FRONT
721 : Keys.KEY_VIDEO_QUALITY_BACK;
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700722 String videoQuality = settingsManager
Erin Dahlgren6190c362014-06-13 14:12:08 -0700723 .getString(SettingsManager.SCOPE_GLOBAL, videoQualityKey);
Sascha Haeberlingde303232014-02-07 02:30:53 +0100724 int quality = SettingsUtil.getVideoQuality(videoQuality, mCameraId);
725 Log.d(TAG, "Selected video quality for '" + videoQuality + "' is " + quality);
Michael Kolb8872c232013-01-29 10:33:22 -0800726
727 // Set video quality.
728 Intent intent = mActivity.getIntent();
729 if (intent.hasExtra(MediaStore.EXTRA_VIDEO_QUALITY)) {
730 int extraVideoQuality =
731 intent.getIntExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
732 if (extraVideoQuality > 0) {
733 quality = CamcorderProfile.QUALITY_HIGH;
734 } else { // 0 is mms.
735 quality = CamcorderProfile.QUALITY_LOW;
736 }
737 }
738
739 // Set video duration limit. The limit is read from the preference,
740 // unless it is specified in the intent.
741 if (intent.hasExtra(MediaStore.EXTRA_DURATION_LIMIT)) {
742 int seconds =
743 intent.getIntExtra(MediaStore.EXTRA_DURATION_LIMIT, 0);
744 mMaxVideoDurationInMs = 1000 * seconds;
745 } else {
Erin Dahlgren6190c362014-06-13 14:12:08 -0700746 mMaxVideoDurationInMs = SettingsUtil.getMaxVideoDuration(mActivity
Sascha Haeberling4044ab72014-04-02 16:25:03 -0700747 .getAndroidContext());
Michael Kolb8872c232013-01-29 10:33:22 -0800748 }
749
Andy Huibers329e1012014-02-04 08:03:35 -0800750 // If quality is not supported, request QUALITY_HIGH which is always supported.
751 if (CamcorderProfile.hasProfile(mCameraId, quality) == false) {
752 quality = CamcorderProfile.QUALITY_HIGH;
753 }
Michael Kolb8872c232013-01-29 10:33:22 -0800754 mProfile = CamcorderProfile.get(mCameraId, quality);
Angus Kong395ee2d2013-07-15 12:42:41 -0700755 mPreferenceRead = true;
I-Jong Lin4ed861a2014-10-10 16:34:34 -0700756 }
757
758 /**
759 * Calculates and sets local class variables for Desired Preview sizes.
760 * This function should be called after every change in preview camera
761 * resolution and/or before the preview starts. Note that these values still
762 * need to be pushed to the CameraSettings to actually change the preview
763 * resolution. Does nothing when camera pointer is null.
764 */
765 private void updateDesiredPreviewSize() {
ztenghuief01a312013-10-14 15:25:16 -0700766 if (mCameraDevice == null) {
767 return;
768 }
I-Jong Lin4ed861a2014-10-10 16:34:34 -0700769
Angus Kong6607dae2014-06-10 16:07:45 -0700770 mCameraSettings = mCameraDevice.getSettings();
Angus Kong5f8c30e2014-03-06 17:15:08 -0800771 Point desiredPreviewSize = getDesiredPreviewSize(mAppController.getAndroidContext(),
Angus Kong6607dae2014-06-10 16:07:45 -0700772 mCameraSettings, mCameraCapabilities, mProfile, mUI.getPreviewScreenSize());
Angus Kong5f8c30e2014-03-06 17:15:08 -0800773 mDesiredPreviewWidth = desiredPreviewSize.x;
774 mDesiredPreviewHeight = desiredPreviewSize.y;
Doris Liu6432cd62013-06-13 17:20:31 -0700775 mUI.setPreviewSize(mDesiredPreviewWidth, mDesiredPreviewHeight);
I-Jong Lin4ed861a2014-10-10 16:34:34 -0700776 Log.v(TAG, "Updated DesiredPreview=" + mDesiredPreviewWidth + "x"
777 + mDesiredPreviewHeight);
Michael Kolb8872c232013-01-29 10:33:22 -0800778 }
779
Angus Kong5f8c30e2014-03-06 17:15:08 -0800780 /**
781 * Calculates the preview size and stores it in mDesiredPreviewWidth and
Angus Kong831347d2014-06-16 16:07:28 -0700782 * mDesiredPreviewHeight.
783 *
784 * <p>This function checks {@link
785 * com.android.camera.cameradevice.CameraCapabilities#getPreferredPreviewSizeForVideo()}
Angus Kong5f8c30e2014-03-06 17:15:08 -0800786 * but also considers the current preview area size on screen and make sure
787 * the final preview size will not be smaller than 1/2 of the current
I-Jong Lin4ed861a2014-10-10 16:34:34 -0700788 * on screen preview area in terms of their short sides. This function has
789 * highest priority of WYSIWYG, 1:1 matching as its best match, even if
790 * there's a larger preview that meets the condition above. </p>
Angus Kong5f8c30e2014-03-06 17:15:08 -0800791 *
792 * @return The preferred preview size or {@code null} if the camera is not
793 * opened yet.
794 */
Angus Kong6607dae2014-06-10 16:07:45 -0700795 private static Point getDesiredPreviewSize(Context context, CameraSettings settings,
796 CameraCapabilities capabilities, CamcorderProfile profile, Point previewScreenSize) {
797 if (capabilities.getSupportedVideoSizes() == null) {
Angus Kong5f8c30e2014-03-06 17:15:08 -0800798 // Driver doesn't support separate outputs for preview and video.
799 return new Point(profile.videoFrameWidth, profile.videoFrameHeight);
800 }
801
802 final int previewScreenShortSide = (previewScreenSize.x < previewScreenSize.y ?
803 previewScreenSize.x : previewScreenSize.y);
Angus Kong6607dae2014-06-10 16:07:45 -0700804 List<Size> sizes = capabilities.getSupportedPreviewSizes();
805 Size preferred = capabilities.getPreferredPreviewSizeForVideo();
Angus Kong00b7b102014-04-24 15:46:52 -0700806 final int preferredPreviewSizeShortSide = (preferred.width() < preferred.height() ?
807 preferred.width() : preferred.height());
Angus Kong5f8c30e2014-03-06 17:15:08 -0800808 if (preferredPreviewSizeShortSide * 2 < previewScreenShortSide) {
Angus Kong63424662014-04-23 10:47:47 -0700809 preferred = new Size(profile.videoFrameWidth, profile.videoFrameHeight);
Angus Kong5f8c30e2014-03-06 17:15:08 -0800810 }
Angus Kong00b7b102014-04-24 15:46:52 -0700811 int product = preferred.width() * preferred.height();
Angus Kong5f8c30e2014-03-06 17:15:08 -0800812 Iterator<Size> it = sizes.iterator();
813 // Remove the preview sizes that are not preferred.
814 while (it.hasNext()) {
815 Size size = it.next();
Angus Kong00b7b102014-04-24 15:46:52 -0700816 if (size.width() * size.height() > product) {
Angus Kong5f8c30e2014-03-06 17:15:08 -0800817 it.remove();
818 }
819 }
I-Jong Lin4ed861a2014-10-10 16:34:34 -0700820
821 // Take highest priority for WYSIWYG when the preview exactly matches
822 // video frame size. The variable sizes is assumed to be filtered
823 // for sizes beyond the UI size.
824 for (Size size : sizes) {
825 if (size.width() == profile.videoFrameWidth
826 && size.height() == profile.videoFrameHeight) {
827 Log.v(TAG, "Selected =" + size.width() + "x" + size.height()
828 + " on WYSIWYG Priority");
829 return new Point(profile.videoFrameWidth, profile.videoFrameHeight);
830 }
831 }
832
Angus Kong5f8c30e2014-03-06 17:15:08 -0800833 Size optimalSize = CameraUtil.getOptimalPreviewSize(context, sizes,
834 (double) profile.videoFrameWidth / profile.videoFrameHeight);
Angus Kong00b7b102014-04-24 15:46:52 -0700835 return new Point(optimalSize.width(), optimalSize.height());
Angus Kong5f8c30e2014-03-06 17:15:08 -0800836 }
837
Michael Kolb8872c232013-01-29 10:33:22 -0800838 private void resizeForPreviewAspectRatio() {
Doris Liue038c162013-12-13 23:06:11 -0800839 mUI.setAspectRatio((float) mProfile.videoFrameWidth / mProfile.videoFrameHeight);
Michael Kolb8872c232013-01-29 10:33:22 -0800840 }
841
Angus Kong13e87c42013-11-25 10:02:47 -0800842 private void installIntentFilter() {
Michael Kolb8872c232013-01-29 10:33:22 -0800843 // install an intent filter to receive SD card related events.
844 IntentFilter intentFilter =
845 new IntentFilter(Intent.ACTION_MEDIA_EJECT);
846 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
847 intentFilter.addDataScheme("file");
848 mReceiver = new MyBroadcastReceiver();
849 mActivity.registerReceiver(mReceiver, intentFilter);
850 }
851
Michael Kolb8872c232013-01-29 10:33:22 -0800852 private void setDisplayOrientation() {
Angus Kongb50b5cb2013-08-09 14:55:20 -0700853 mDisplayRotation = CameraUtil.getDisplayRotation(mActivity);
Sol Boucher43e18132014-06-19 15:03:18 -0700854 Characteristics info =
855 mActivity.getCameraProvider().getCharacteristics(mCameraId);
Sol Boucher73ec9852014-07-24 23:59:21 -0700856 mCameraDisplayOrientation = info.getPreviewOrientation(mDisplayRotation);
Doris Liu6432cd62013-06-13 17:20:31 -0700857 // Change the camera display orientation
858 if (mCameraDevice != null) {
Sol Boucher73ec9852014-07-24 23:59:21 -0700859 mCameraDevice.setDisplayOrientation(mDisplayRotation);
Michael Kolb8872c232013-01-29 10:33:22 -0800860 }
Doris Liua1ec04a2014-01-13 17:29:40 -0800861 if (mFocusManager != null) {
862 mFocusManager.setDisplayOrientation(mCameraDisplayOrientation);
863 }
Doris Liu6432cd62013-06-13 17:20:31 -0700864 }
865
866 @Override
867 public void updateCameraOrientation() {
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100868 if (mMediaRecorderRecording) {
869 return;
870 }
Angus Kongb50b5cb2013-08-09 14:55:20 -0700871 if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) {
Doris Liu6432cd62013-06-13 17:20:31 -0700872 setDisplayOrientation();
873 }
Michael Kolb8872c232013-01-29 10:33:22 -0800874 }
875
Doris Liu6827ce22013-03-12 19:24:28 -0700876 @Override
Doris Liu70da9182013-12-17 18:41:15 -0800877 public void updatePreviewAspectRatio(float aspectRatio) {
878 mAppController.updatePreviewAspectRatio(aspectRatio);
879 }
880
Andy Huibers203abe52014-05-19 13:59:01 -0700881 /**
882 * Returns current Zoom value, with 1.0 as the value for no zoom.
883 */
884 private float currentZoomValue() {
Sol Boucher2192fba2014-08-19 17:24:07 -0700885 return mCameraSettings.getCurrentZoomRatio();
Andy Huibers203abe52014-05-19 13:59:01 -0700886 }
887
Doris Liu70da9182013-12-17 18:41:15 -0800888 @Override
Sol Boucher2192fba2014-08-19 17:24:07 -0700889 public void onZoomChanged(float ratio) {
Doris Liu6827ce22013-03-12 19:24:28 -0700890 // Not useful to change zoom value when the activity is paused.
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100891 if (mPaused) {
Sol Boucher2192fba2014-08-19 17:24:07 -0700892 return;
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100893 }
Sol Boucher2192fba2014-08-19 17:24:07 -0700894 mZoomValue = ratio;
Angus Kong6607dae2014-06-10 16:07:45 -0700895 if (mCameraSettings == null || mCameraDevice == null) {
Sol Boucher2192fba2014-08-19 17:24:07 -0700896 return;
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100897 }
Doris Liu6827ce22013-03-12 19:24:28 -0700898 // Set zoom parameters asynchronously
Sol Boucher2192fba2014-08-19 17:24:07 -0700899 mCameraSettings.setZoomRatio(mZoomValue);
Angus Kong6607dae2014-06-10 16:07:45 -0700900 mCameraDevice.applySettings(mCameraSettings);
Doris Liu6827ce22013-03-12 19:24:28 -0700901 }
Angus Kong395ee2d2013-07-15 12:42:41 -0700902
Michael Kolb8872c232013-01-29 10:33:22 -0800903 private void startPreview() {
Alan Newbergerd41766f2014-04-09 18:25:34 -0700904 Log.i(TAG, "startPreview");
Michael Kolb8872c232013-01-29 10:33:22 -0800905
Erin Dahlgrend8de0772014-02-03 10:12:27 -0800906 SurfaceTexture surfaceTexture = mActivity.getCameraAppUI().getSurfaceTexture();
ztenghuief01a312013-10-14 15:25:16 -0700907 if (!mPreferenceRead || surfaceTexture == null || mPaused == true ||
908 mCameraDevice == null) {
909 return;
910 }
Angus Kong395ee2d2013-07-15 12:42:41 -0700911
Michael Kolb8872c232013-01-29 10:33:22 -0800912 if (mPreviewing == true) {
913 stopPreview();
Michael Kolb8872c232013-01-29 10:33:22 -0800914 }
915
Michael Kolb8872c232013-01-29 10:33:22 -0800916 setDisplayOrientation();
Sol Boucher73ec9852014-07-24 23:59:21 -0700917 mCameraDevice.setDisplayOrientation(mDisplayRotation);
Michael Kolb8872c232013-01-29 10:33:22 -0800918 setCameraParameters();
919
Doris Liua1ec04a2014-01-13 17:29:40 -0800920 if (mFocusManager != null) {
921 // If the focus mode is continuous autofocus, call cancelAutoFocus
922 // to resume it because it may have been paused by autoFocus call.
Angus Kong831347d2014-06-16 16:07:28 -0700923 CameraCapabilities.FocusMode focusMode =
924 mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode());
925 if (focusMode == CameraCapabilities.FocusMode.CONTINUOUS_PICTURE) {
Doris Liua1ec04a2014-01-13 17:29:40 -0800926 mCameraDevice.cancelAutoFocus();
927 }
928 }
Doris Liu5a367542014-01-17 17:21:42 -0800929
930 // This is to notify app controller that preview will start next, so app
931 // controller can set preview callbacks if needed. This has to happen before
932 // preview is started as a workaround of the framework issue related to preview
933 // callbacks that causes preview stretch and crash. (More details see b/12210027
Alan Newbergerfb172ac2014-08-21 14:42:36 -0700934 // and b/12591410. Don't apply this to L, see b/16649297.
935 if (!ApiHelper.isLOrHigher()) {
936 Log.v(TAG, "calling onPreviewReadyToStart to set one shot callback");
937 mAppController.onPreviewReadyToStart();
938 } else {
939 Log.v(TAG, "on L, no one shot callback necessary");
940 }
Michael Kolb8872c232013-01-29 10:33:22 -0800941 try {
Doris Liu3973deb2013-08-21 13:42:22 -0700942 mCameraDevice.setPreviewTexture(surfaceTexture);
Spike Sprague3c3b31d2014-09-08 10:43:04 -0700943 mCameraDevice.startPreviewWithCallback(new Handler(Looper.getMainLooper()),
944 new CameraAgent.CameraStartPreviewCallback() {
945 @Override
946 public void onPreviewStarted() {
947 VideoModule.this.onPreviewStarted();
948 }
949 });
Doris Liu3973deb2013-08-21 13:42:22 -0700950 mPreviewing = true;
Michael Kolb8872c232013-01-29 10:33:22 -0800951 } catch (Throwable ex) {
952 closeCamera();
953 throw new RuntimeException("startPreview failed", ex);
Michael Kolb8872c232013-01-29 10:33:22 -0800954 }
Michael Kolbb1aeb392013-03-11 12:37:40 -0700955 }
956
957 private void onPreviewStarted() {
Erin Dahlgren667630d2014-04-01 14:03:25 -0700958 mAppController.setShutterEnabled(true);
Doris Liu2b906b82013-12-10 16:34:08 -0800959 mAppController.onPreviewStarted();
Doris Liua1ec04a2014-01-13 17:29:40 -0800960 if (mFocusManager != null) {
961 mFocusManager.onPreviewStarted();
962 }
Michael Kolb8872c232013-01-29 10:33:22 -0800963 }
964
Doris Liu6827ce22013-03-12 19:24:28 -0700965 @Override
Sameer Padaladb81ce62014-03-21 15:33:56 -0700966 public void onPreviewInitialDataReceived() {
Sameer Padaladb81ce62014-03-21 15:33:56 -0700967 }
968
969 @Override
Doris Liu6827ce22013-03-12 19:24:28 -0700970 public void stopPreview() {
Senpo Hu36e045a2014-09-29 18:49:10 -0700971 if (!mPreviewing) {
972 Log.v(TAG, "Skip stopPreview since it's not mPreviewing");
Sascha Haeberling846d3ab2014-02-04 12:48:55 +0100973 return;
974 }
Senpo Hu36e045a2014-09-29 18:49:10 -0700975 if (mCameraDevice == null) {
976 Log.v(TAG, "Skip stopPreview since mCameraDevice is null");
977 return;
978 }
979
980 Log.v(TAG, "stopPreview");
Doris Liu6432cd62013-06-13 17:20:31 -0700981 mCameraDevice.stopPreview();
Doris Liua1ec04a2014-01-13 17:29:40 -0800982 if (mFocusManager != null) {
983 mFocusManager.onPreviewStopped();
984 }
Michael Kolb8872c232013-01-29 10:33:22 -0800985 mPreviewing = false;
986 }
987
Michael Kolb8872c232013-01-29 10:33:22 -0800988 private void closeCamera() {
Alan Newbergerd41766f2014-04-09 18:25:34 -0700989 Log.i(TAG, "closeCamera");
Doris Liu6432cd62013-06-13 17:20:31 -0700990 if (mCameraDevice == null) {
Michael Kolb8872c232013-01-29 10:33:22 -0800991 Log.d(TAG, "already stopped.");
992 return;
993 }
Doris Liu6432cd62013-06-13 17:20:31 -0700994 mCameraDevice.setZoomChangeListener(null);
Angus Kong20fad242013-11-11 18:23:46 -0800995 mActivity.getCameraProvider().releaseCamera(mCameraDevice.getCameraId());
Angus Kong395ee2d2013-07-15 12:42:41 -0700996 mCameraDevice = null;
Michael Kolb8872c232013-01-29 10:33:22 -0800997 mPreviewing = false;
998 mSnapshotInProgress = false;
Doris Liua1ec04a2014-01-13 17:29:40 -0800999 if (mFocusManager != null) {
1000 mFocusManager.onCameraReleased();
1001 }
Michael Kolb8872c232013-01-29 10:33:22 -08001002 }
1003
Michael Kolb8872c232013-01-29 10:33:22 -08001004 @Override
Michael Kolb8872c232013-01-29 10:33:22 -08001005 public boolean onBackPressed() {
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01001006 if (mPaused) {
1007 return true;
1008 }
Michael Kolb8872c232013-01-29 10:33:22 -08001009 if (mMediaRecorderRecording) {
1010 onStopVideoRecording();
1011 return true;
Michael Kolb8872c232013-01-29 10:33:22 -08001012 } else {
Doris Liuf9e4f8f2013-12-04 18:04:22 -08001013 return false;
Michael Kolb8872c232013-01-29 10:33:22 -08001014 }
1015 }
1016
1017 @Override
1018 public boolean onKeyDown(int keyCode, KeyEvent event) {
1019 // Do not handle any key if the activity is paused.
1020 if (mPaused) {
1021 return true;
1022 }
1023
1024 switch (keyCode) {
1025 case KeyEvent.KEYCODE_CAMERA:
1026 if (event.getRepeatCount() == 0) {
Erin Dahlgren667630d2014-04-01 14:03:25 -07001027 onShutterButtonClick();
Michael Kolb8872c232013-01-29 10:33:22 -08001028 return true;
1029 }
Michael Kolb8872c232013-01-29 10:33:22 -08001030 case KeyEvent.KEYCODE_DPAD_CENTER:
1031 if (event.getRepeatCount() == 0) {
Erin Dahlgren667630d2014-04-01 14:03:25 -07001032 onShutterButtonClick();
Michael Kolb8872c232013-01-29 10:33:22 -08001033 return true;
1034 }
Michael Kolb8872c232013-01-29 10:33:22 -08001035 case KeyEvent.KEYCODE_MENU:
Erin Dahlgren15691af2014-03-14 14:10:57 -07001036 // Consume menu button presses during capture.
1037 return mMediaRecorderRecording;
Michael Kolb8872c232013-01-29 10:33:22 -08001038 }
1039 return false;
1040 }
1041
1042 @Override
1043 public boolean onKeyUp(int keyCode, KeyEvent event) {
1044 switch (keyCode) {
1045 case KeyEvent.KEYCODE_CAMERA:
Erin Dahlgren667630d2014-04-01 14:03:25 -07001046 onShutterButtonClick();
Michael Kolb8872c232013-01-29 10:33:22 -08001047 return true;
Erin Dahlgren15691af2014-03-14 14:10:57 -07001048 case KeyEvent.KEYCODE_MENU:
1049 // Consume menu button presses during capture.
1050 return mMediaRecorderRecording;
Michael Kolb8872c232013-01-29 10:33:22 -08001051 }
1052 return false;
1053 }
1054
Doris Liu6827ce22013-03-12 19:24:28 -07001055 @Override
1056 public boolean isVideoCaptureIntent() {
Michael Kolb8872c232013-01-29 10:33:22 -08001057 String action = mActivity.getIntent().getAction();
1058 return (MediaStore.ACTION_VIDEO_CAPTURE.equals(action));
1059 }
1060
1061 private void doReturnToCaller(boolean valid) {
1062 Intent resultIntent = new Intent();
1063 int resultCode;
1064 if (valid) {
1065 resultCode = Activity.RESULT_OK;
1066 resultIntent.setData(mCurrentVideoUri);
Nicolas Prevot44917f62014-09-09 20:35:56 +01001067 resultIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Michael Kolb8872c232013-01-29 10:33:22 -08001068 } else {
1069 resultCode = Activity.RESULT_CANCELED;
1070 }
1071 mActivity.setResultEx(resultCode, resultIntent);
1072 mActivity.finish();
1073 }
1074
1075 private void cleanupEmptyFile() {
1076 if (mVideoFilename != null) {
1077 File f = new File(mVideoFilename);
1078 if (f.length() == 0 && f.delete()) {
1079 Log.v(TAG, "Empty video file deleted: " + mVideoFilename);
1080 mVideoFilename = null;
1081 }
1082 }
1083 }
1084
Michael Kolb8872c232013-01-29 10:33:22 -08001085 // Prepares media recorder.
1086 private void initializeRecorder() {
Senpo Hu2d3475a2014-09-28 04:30:15 -07001087 Log.i(TAG, "initializeRecorder: " + Thread.currentThread());
Michael Kolb8872c232013-01-29 10:33:22 -08001088 // If the mCameraDevice is null, then this activity is going to finish
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01001089 if (mCameraDevice == null) {
1090 return;
1091 }
Michael Kolb8872c232013-01-29 10:33:22 -08001092
Michael Kolb8872c232013-01-29 10:33:22 -08001093 Intent intent = mActivity.getIntent();
1094 Bundle myExtras = intent.getExtras();
1095
1096 long requestedSizeLimit = 0;
1097 closeVideoFileDescriptor();
ztenghui70bd0242013-10-14 11:15:44 -07001098 mCurrentVideoUriFromMediaSaved = false;
Michael Kolb8872c232013-01-29 10:33:22 -08001099 if (mIsVideoCaptureIntent && myExtras != null) {
1100 Uri saveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT);
1101 if (saveUri != null) {
1102 try {
1103 mVideoFileDescriptor =
1104 mContentResolver.openFileDescriptor(saveUri, "rw");
1105 mCurrentVideoUri = saveUri;
1106 } catch (java.io.FileNotFoundException ex) {
1107 // invalid uri
1108 Log.e(TAG, ex.toString());
1109 }
1110 }
1111 requestedSizeLimit = myExtras.getLong(MediaStore.EXTRA_SIZE_LIMIT);
1112 }
1113 mMediaRecorder = new MediaRecorder();
Michael Kolb8872c232013-01-29 10:33:22 -08001114 // Unlock the camera object before passing it to media recorder.
Senpo Hu2d3475a2014-09-28 04:30:15 -07001115 mCameraDevice.unlock();
1116 mMediaRecorder.setCamera(mCameraDevice.getCamera());
Sascha Haeberlingf15d63e2014-07-21 11:31:13 -07001117 mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
Michael Kolb8872c232013-01-29 10:33:22 -08001118 mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
1119 mMediaRecorder.setProfile(mProfile);
Michael Kolbf9542362013-05-30 07:45:41 -07001120 mMediaRecorder.setVideoSize(mProfile.videoFrameWidth, mProfile.videoFrameHeight);
Michael Kolb8872c232013-01-29 10:33:22 -08001121 mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs);
Michael Kolb8872c232013-01-29 10:33:22 -08001122
1123 setRecordLocation();
1124
1125 // Set output file.
1126 // Try Uri in the intent first. If it doesn't exist, use our own
1127 // instead.
1128 if (mVideoFileDescriptor != null) {
1129 mMediaRecorder.setOutputFile(mVideoFileDescriptor.getFileDescriptor());
1130 } else {
1131 generateVideoFilename(mProfile.fileFormat);
1132 mMediaRecorder.setOutputFile(mVideoFilename);
1133 }
1134
1135 // Set maximum file size.
Angus Kong2dcc0a92013-09-25 13:00:08 -07001136 long maxFileSize = mActivity.getStorageSpaceBytes() - Storage.LOW_STORAGE_THRESHOLD_BYTES;
Michael Kolb8872c232013-01-29 10:33:22 -08001137 if (requestedSizeLimit > 0 && requestedSizeLimit < maxFileSize) {
1138 maxFileSize = requestedSizeLimit;
1139 }
1140
1141 try {
1142 mMediaRecorder.setMaxFileSize(maxFileSize);
1143 } catch (RuntimeException exception) {
1144 // We are going to ignore failure of setMaxFileSize here, as
1145 // a) The composer selected may simply not support it, or
1146 // b) The underlying media framework may not handle 64-bit range
1147 // on the size restriction.
1148 }
1149
Senpo Hu0bdc4b52014-11-17 22:56:31 -08001150 int sensorOrientation =
1151 mActivity.getCameraProvider().getCharacteristics(mCameraId).getSensorOrientation();
1152 int deviceOrientation =
1153 mAppController.getOrientationManager().getDeviceOrientation().getDegrees();
1154 int rotation = CameraUtil.getImageRotation(
1155 sensorOrientation, deviceOrientation, isCameraFrontFacing());
Michael Kolb8872c232013-01-29 10:33:22 -08001156 mMediaRecorder.setOrientationHint(rotation);
1157
1158 try {
1159 mMediaRecorder.prepare();
1160 } catch (IOException e) {
1161 Log.e(TAG, "prepare failed for " + mVideoFilename, e);
1162 releaseMediaRecorder();
1163 throw new RuntimeException(e);
1164 }
1165
1166 mMediaRecorder.setOnErrorListener(this);
1167 mMediaRecorder.setOnInfoListener(this);
1168 }
1169
Michael Kolb8872c232013-01-29 10:33:22 -08001170 private static void setCaptureRate(MediaRecorder recorder, double fps) {
1171 recorder.setCaptureRate(fps);
1172 }
1173
Michael Kolb8872c232013-01-29 10:33:22 -08001174 private void setRecordLocation() {
Sascha Haeberling638e6f02013-09-18 14:28:51 -07001175 Location loc = mLocationManager.getCurrentLocation();
1176 if (loc != null) {
1177 mMediaRecorder.setLocation((float) loc.getLatitude(),
1178 (float) loc.getLongitude());
Michael Kolb8872c232013-01-29 10:33:22 -08001179 }
1180 }
1181
Michael Kolb8872c232013-01-29 10:33:22 -08001182 private void releaseMediaRecorder() {
Alan Newbergerd41766f2014-04-09 18:25:34 -07001183 Log.i(TAG, "Releasing media recorder.");
Michael Kolb8872c232013-01-29 10:33:22 -08001184 if (mMediaRecorder != null) {
1185 cleanupEmptyFile();
1186 mMediaRecorder.reset();
1187 mMediaRecorder.release();
1188 mMediaRecorder = null;
1189 }
1190 mVideoFilename = null;
1191 }
1192
Michael Kolb8872c232013-01-29 10:33:22 -08001193 private void generateVideoFilename(int outputFileFormat) {
1194 long dateTaken = System.currentTimeMillis();
1195 String title = createName(dateTaken);
1196 // Used when emailing.
1197 String filename = title + convertOutputFormatToFileExt(outputFileFormat);
1198 String mime = convertOutputFormatToMimeType(outputFileFormat);
1199 String path = Storage.DIRECTORY + '/' + filename;
1200 String tmpPath = path + ".tmp";
Ruben Brunk16007962013-04-19 15:27:57 -07001201 mCurrentVideoValues = new ContentValues(9);
Michael Kolb8872c232013-01-29 10:33:22 -08001202 mCurrentVideoValues.put(Video.Media.TITLE, title);
1203 mCurrentVideoValues.put(Video.Media.DISPLAY_NAME, filename);
1204 mCurrentVideoValues.put(Video.Media.DATE_TAKEN, dateTaken);
Ruben Brunk16007962013-04-19 15:27:57 -07001205 mCurrentVideoValues.put(MediaColumns.DATE_MODIFIED, dateTaken / 1000);
Michael Kolb8872c232013-01-29 10:33:22 -08001206 mCurrentVideoValues.put(Video.Media.MIME_TYPE, mime);
1207 mCurrentVideoValues.put(Video.Media.DATA, path);
Sam Juddde3e9ab2014-03-17 13:07:22 -07001208 mCurrentVideoValues.put(Video.Media.WIDTH, mProfile.videoFrameWidth);
1209 mCurrentVideoValues.put(Video.Media.HEIGHT, mProfile.videoFrameHeight);
Michael Kolb8872c232013-01-29 10:33:22 -08001210 mCurrentVideoValues.put(Video.Media.RESOLUTION,
1211 Integer.toString(mProfile.videoFrameWidth) + "x" +
1212 Integer.toString(mProfile.videoFrameHeight));
1213 Location loc = mLocationManager.getCurrentLocation();
1214 if (loc != null) {
1215 mCurrentVideoValues.put(Video.Media.LATITUDE, loc.getLatitude());
1216 mCurrentVideoValues.put(Video.Media.LONGITUDE, loc.getLongitude());
1217 }
Michael Kolb8872c232013-01-29 10:33:22 -08001218 mVideoFilename = tmpPath;
1219 Log.v(TAG, "New video filename: " + mVideoFilename);
1220 }
1221
Andy Huibers203abe52014-05-19 13:59:01 -07001222 private void logVideoCapture(long duration) {
1223 String flashSetting = mActivity.getSettingsManager()
Erin Dahlgren6190c362014-06-13 14:12:08 -07001224 .getString(mAppController.getCameraScope(),
1225 Keys.KEY_VIDEOCAMERA_FLASH_MODE);
1226 boolean gridLinesOn = Keys.areGridLinesOn(mActivity.getSettingsManager());
Andy Huibers203abe52014-05-19 13:59:01 -07001227 int width = (Integer) mCurrentVideoValues.get(Video.Media.WIDTH);
1228 int height = (Integer) mCurrentVideoValues.get(Video.Media.HEIGHT);
1229 long size = new File(mCurrentVideoFilename).length();
1230 String name = new File(mCurrentVideoValues.getAsString(Video.Media.DATA)).getName();
1231 UsageStatistics.instance().videoCaptureDoneEvent(name, duration, isCameraFrontFacing(),
1232 currentZoomValue(), width, height, size, flashSetting, gridLinesOn);
1233 }
1234
Angus Kong83a99ae2013-04-17 15:37:07 -07001235 private void saveVideo() {
Michael Kolb8872c232013-01-29 10:33:22 -08001236 if (mVideoFileDescriptor == null) {
Michael Kolb8872c232013-01-29 10:33:22 -08001237 long duration = SystemClock.uptimeMillis() - mRecordingStartTime;
1238 if (duration > 0) {
Spike Spragued020fbf2014-07-16 15:47:32 -07001239 //
Michael Kolb8872c232013-01-29 10:33:22 -08001240 } else {
1241 Log.w(TAG, "Video duration <= 0 : " + duration);
1242 }
Andy Huibers203abe52014-05-19 13:59:01 -07001243 mCurrentVideoValues.put(Video.Media.SIZE, new File(mCurrentVideoFilename).length());
1244 mCurrentVideoValues.put(Video.Media.DURATION, duration);
Sascha Haeberling280fd3e2013-11-21 13:52:15 -08001245 getServices().getMediaSaver().addVideo(mCurrentVideoFilename,
Andy Huibers203abe52014-05-19 13:59:01 -07001246 mCurrentVideoValues, mOnVideoSavedListener, mContentResolver);
1247 logVideoCapture(duration);
Michael Kolb8872c232013-01-29 10:33:22 -08001248 }
1249 mCurrentVideoValues = null;
Michael Kolb8872c232013-01-29 10:33:22 -08001250 }
1251
1252 private void deleteVideoFile(String fileName) {
1253 Log.v(TAG, "Deleting video " + fileName);
1254 File f = new File(fileName);
1255 if (!f.delete()) {
1256 Log.v(TAG, "Could not delete " + fileName);
1257 }
1258 }
1259
Michael Kolb8872c232013-01-29 10:33:22 -08001260 // from MediaRecorder.OnErrorListener
1261 @Override
1262 public void onError(MediaRecorder mr, int what, int extra) {
1263 Log.e(TAG, "MediaRecorder error. what=" + what + ". extra=" + extra);
1264 if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) {
1265 // We may have run out of space on the sdcard.
1266 stopVideoRecording();
Spike Spraguee6374b72014-04-25 17:24:32 -07001267 mActivity.updateStorageSpaceAndHint(null);
Michael Kolb8872c232013-01-29 10:33:22 -08001268 }
1269 }
1270
1271 // from MediaRecorder.OnInfoListener
1272 @Override
1273 public void onInfo(MediaRecorder mr, int what, int extra) {
1274 if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01001275 if (mMediaRecorderRecording) {
1276 onStopVideoRecording();
1277 }
Michael Kolb8872c232013-01-29 10:33:22 -08001278 } else if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01001279 if (mMediaRecorderRecording) {
1280 onStopVideoRecording();
1281 }
Michael Kolb8872c232013-01-29 10:33:22 -08001282
1283 // Show the toast.
1284 Toast.makeText(mActivity, R.string.video_reach_size_limit,
1285 Toast.LENGTH_LONG).show();
1286 }
1287 }
1288
1289 /*
1290 * Make sure we're not recording music playing in the background, ask the
1291 * MediaPlaybackService to pause playback.
1292 */
Sascha Haeberling92217bd2014-12-11 12:20:43 -08001293 private void silenceSoundsAndVibrations() {
1294 // Get the audio focus which causes other music players to stop.
1295 mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
1296 AudioManager.AUDIOFOCUS_GAIN);
1297 // Store current ringer mode so we can set it once video recording is
1298 // finished.
1299 mOriginalRingerMode = mAudioManager.getRingerMode();
1300 // Make sure no system sounds and vibrations happen during video
1301 // recording.
1302 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1303 }
1304
1305 private void restoreRingerMode() {
1306 // First check if ringer mode was changed during the recording. If not,
1307 // re-set the mode that was set before video recording started.
1308 if (mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
1309 mAudioManager.setRingerMode(mOriginalRingerMode);
1310 }
Michael Kolb8872c232013-01-29 10:33:22 -08001311 }
1312
1313 // For testing.
1314 public boolean isRecording() {
1315 return mMediaRecorderRecording;
1316 }
1317
1318 private void startVideoRecording() {
Senpo Hu2d3475a2014-09-28 04:30:15 -07001319 Log.i(TAG, "startVideoRecording: " + Thread.currentThread());
Sascha Haeberling37f36112013-08-06 14:31:52 -07001320 mUI.cancelAnimations();
Doris Liu6432cd62013-06-13 17:20:31 -07001321 mUI.setSwipingEnabled(false);
Paul Rohde987ee642014-12-05 12:17:15 -08001322 mUI.hidePassiveFocusIndicator();
Doris Liud6487c92014-02-28 10:35:45 -08001323 mUI.showVideoRecordingHints(false);
Senpo Hu427a9152014-11-19 17:11:46 -08001324 mAppController.getCameraAppUI().hideCaptureIndicator();
Michael Kolb8872c232013-01-29 10:33:22 -08001325
Spike Spraguee6374b72014-04-25 17:24:32 -07001326 mActivity.updateStorageSpaceAndHint(new CameraActivity.OnStorageUpdateDoneListener() {
1327 @Override
1328 public void onStorageUpdateDone(long bytes) {
1329 if (bytes <= Storage.LOW_STORAGE_THRESHOLD_BYTES) {
1330 Log.w(TAG, "Storage issue, ignore the start request");
1331 } else {
Senpo Hu4ba28292014-09-26 11:05:08 -07001332 if (mCameraDevice == null) {
1333 Log.v(TAG, "in storage callback after camera closed");
1334 return;
1335 }
Alan Newberger26b224d2014-08-26 18:18:48 -07001336 if (mPaused == true) {
1337 Log.v(TAG, "in storage callback after module paused");
1338 return;
1339 }
Senpo Hu2d3475a2014-09-28 04:30:15 -07001340
1341 // Monkey is so fast so it could trigger startVideoRecording twice. To prevent
1342 // app crash (b/17313985), do nothing here for the second storage-checking
1343 // callback because recording is already started.
1344 if (mMediaRecorderRecording) {
1345 Log.v(TAG, "in storage callback after recording started");
1346 return;
1347 }
1348
Spike Spraguee6374b72014-04-25 17:24:32 -07001349 mCurrentVideoUri = null;
Michael Kolb8872c232013-01-29 10:33:22 -08001350
Spike Spraguee6374b72014-04-25 17:24:32 -07001351 initializeRecorder();
1352 if (mMediaRecorder == null) {
1353 Log.e(TAG, "Fail to initialize media recorder");
1354 return;
1355 }
Doris Liu3973deb2013-08-21 13:42:22 -07001356
Spike Spraguee6374b72014-04-25 17:24:32 -07001357 try {
1358 mMediaRecorder.start(); // Recording is now started
1359 } catch (RuntimeException e) {
1360 Log.e(TAG, "Could not start media recorder. ", e);
1361 releaseMediaRecorder();
1362 // If start fails, frameworks will not lock the camera for us.
1363 mCameraDevice.lock();
1364 return;
1365 }
Sascha Haeberling367d1382014-12-11 14:15:04 -08001366 // Make sure we stop playing sounds and disable the
1367 // vibrations during video recording.
1368 silenceSoundsAndVibrations();
1369
Spike Spraguee6374b72014-04-25 17:24:32 -07001370 mAppController.getCameraAppUI().setSwipeEnabled(false);
Michael Kolb8872c232013-01-29 10:33:22 -08001371
Spike Spraguee6374b72014-04-25 17:24:32 -07001372 // The parameters might have been altered by MediaRecorder already.
1373 // We need to force mCameraDevice to refresh before getting it.
Angus Kong6607dae2014-06-10 16:07:45 -07001374 mCameraDevice.refreshSettings();
Spike Spraguee6374b72014-04-25 17:24:32 -07001375 // The parameters may have been changed by MediaRecorder upon starting
1376 // recording. We need to alter the parameters if we support camcorder
1377 // zoom. To reduce latency when setting the parameters during zoom, we
Angus Kong6607dae2014-06-10 16:07:45 -07001378 // update the settings here once.
1379 mCameraSettings = mCameraDevice.getSettings();
Michael Kolb8872c232013-01-29 10:33:22 -08001380
Spike Spraguee6374b72014-04-25 17:24:32 -07001381 mMediaRecorderRecording = true;
1382 mActivity.lockOrientation();
1383 mRecordingStartTime = SystemClock.uptimeMillis();
Michael Kolb8872c232013-01-29 10:33:22 -08001384
Spike Spraguee6374b72014-04-25 17:24:32 -07001385 // A special case of mode options closing: during capture it should
1386 // not be possible to change mode state.
1387 mAppController.getCameraAppUI().hideModeOptions();
1388 mAppController.getCameraAppUI().animateBottomBarToVideoStop(R.drawable.ic_stop);
1389 mUI.showRecordingUI(true);
Doris Liu4df91582014-03-21 18:33:57 -07001390
Spike Spraguee6374b72014-04-25 17:24:32 -07001391 setFocusParameters();
Senpo Hu11186312014-08-08 13:42:13 -07001392
Spike Spraguee6374b72014-04-25 17:24:32 -07001393 updateRecordingTime();
1394 mActivity.enableKeepScreenOn(true);
1395 }
1396 }
1397 });
Michael Kolb8872c232013-01-29 10:33:22 -08001398 }
1399
Sascha Haeberling37f36112013-08-06 14:31:52 -07001400 private Bitmap getVideoThumbnail() {
Michael Kolb8872c232013-01-29 10:33:22 -08001401 Bitmap bitmap = null;
1402 if (mVideoFileDescriptor != null) {
1403 bitmap = Thumbnail.createVideoThumbnailBitmap(mVideoFileDescriptor.getFileDescriptor(),
Doris Liu3c2fca32013-02-13 18:28:03 -08001404 mDesiredPreviewWidth);
Doris Liu2a7f44c2013-08-12 15:18:53 -07001405 } else if (mCurrentVideoUri != null) {
1406 try {
1407 mVideoFileDescriptor = mContentResolver.openFileDescriptor(mCurrentVideoUri, "r");
1408 bitmap = Thumbnail.createVideoThumbnailBitmap(
1409 mVideoFileDescriptor.getFileDescriptor(), mDesiredPreviewWidth);
1410 } catch (java.io.FileNotFoundException ex) {
1411 // invalid uri
1412 Log.e(TAG, ex.toString());
1413 }
Michael Kolb8872c232013-01-29 10:33:22 -08001414 }
Doris Liu3973deb2013-08-21 13:42:22 -07001415
Michael Kolb8872c232013-01-29 10:33:22 -08001416 if (bitmap != null) {
1417 // MetadataRetriever already rotates the thumbnail. We should rotate
1418 // it to match the UI orientation (and mirror if it is front-facing camera).
Sascha Haeberling6ccec202014-03-11 09:44:34 -07001419 bitmap = CameraUtil.rotateAndMirror(bitmap, 0, isCameraFrontFacing());
Sascha Haeberling37f36112013-08-06 14:31:52 -07001420 }
1421 return bitmap;
1422 }
1423
1424 private void showCaptureResult() {
1425 mIsInReviewMode = true;
1426 Bitmap bitmap = getVideoThumbnail();
1427 if (bitmap != null) {
Doris Liu6827ce22013-03-12 19:24:28 -07001428 mUI.showReviewImage(bitmap);
Michael Kolb8872c232013-01-29 10:33:22 -08001429 }
Doris Liu6827ce22013-03-12 19:24:28 -07001430 mUI.showReviewControls();
Michael Kolb8872c232013-01-29 10:33:22 -08001431 }
1432
Michael Kolb8872c232013-01-29 10:33:22 -08001433 private boolean stopVideoRecording() {
Senpo Hu2d3475a2014-09-28 04:30:15 -07001434 // Do nothing if camera device is still capturing photo. Monkey test can trigger app crashes
1435 // (b/17313985) without this check. Crash could also be reproduced by continuously tapping
1436 // on shutter button and preview with two fingers.
1437 if (mSnapshotInProgress) {
Senpo Hu36e045a2014-09-29 18:49:10 -07001438 Log.v(TAG, "Skip stopVideoRecording since snapshot in progress");
Senpo Hu2d3475a2014-09-28 04:30:15 -07001439 return true;
1440 }
Senpo Hu36e045a2014-09-29 18:49:10 -07001441 Log.v(TAG, "stopVideoRecording");
Senpo Hu2d3475a2014-09-28 04:30:15 -07001442
Doris Liu6432cd62013-06-13 17:20:31 -07001443 mUI.setSwipingEnabled(true);
Paul Rohde987ee642014-12-05 12:17:15 -08001444 mUI.showPassiveFocusIndicator();
Doris Liud6487c92014-02-28 10:35:45 -08001445 mUI.showVideoRecordingHints(true);
Michael Kolb8872c232013-01-29 10:33:22 -08001446
1447 boolean fail = false;
1448 if (mMediaRecorderRecording) {
1449 boolean shouldAddToMediaStoreNow = false;
1450
1451 try {
Doris Liu3973deb2013-08-21 13:42:22 -07001452 mMediaRecorder.setOnErrorListener(null);
1453 mMediaRecorder.setOnInfoListener(null);
1454 mMediaRecorder.stop();
1455 shouldAddToMediaStoreNow = true;
Michael Kolb8872c232013-01-29 10:33:22 -08001456 mCurrentVideoFilename = mVideoFilename;
Andy Huibers10c58162014-03-29 14:06:54 -07001457 Log.v(TAG, "stopVideoRecording: current video filename: " + mCurrentVideoFilename);
Michael Kolb8872c232013-01-29 10:33:22 -08001458 } catch (RuntimeException e) {
1459 Log.e(TAG, "stop fail", e);
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01001460 if (mVideoFilename != null) {
1461 deleteVideoFile(mVideoFilename);
1462 }
Michael Kolb8872c232013-01-29 10:33:22 -08001463 fail = true;
1464 }
Sascha Haeberling92217bd2014-12-11 12:20:43 -08001465 restoreRingerMode();
Michael Kolb8872c232013-01-29 10:33:22 -08001466 mMediaRecorderRecording = false;
Angus Kong9f1db522013-11-09 16:25:59 -08001467 mActivity.unlockOrientation();
Michael Kolb8872c232013-01-29 10:33:22 -08001468
1469 // If the activity is paused, this means activity is interrupted
1470 // during recording. Release the camera as soon as possible because
1471 // face unlock or other applications may need to use the camera.
Michael Kolb8872c232013-01-29 10:33:22 -08001472 if (mPaused) {
Senpo Hu36e045a2014-09-29 18:49:10 -07001473 // b/16300704: Monkey is fast so it could pause the module while recording.
1474 // stopPreview should definitely be called before switching off.
1475 stopPreview();
Doris Liu3973deb2013-08-21 13:42:22 -07001476 closeCamera();
Michael Kolb8872c232013-01-29 10:33:22 -08001477 }
1478
Doris Liufe6596c2013-10-08 11:03:37 -07001479 mUI.showRecordingUI(false);
Michael Kolb8872c232013-01-29 10:33:22 -08001480 // The orientation was fixed during video recording. Now make it
1481 // reflect the device orientation as video recording is stopped.
Doris Liu6827ce22013-03-12 19:24:28 -07001482 mUI.setOrientationIndicator(0, true);
Angus Kong13e87c42013-11-25 10:02:47 -08001483 mActivity.enableKeepScreenOn(false);
Doris Liu2a7f44c2013-08-12 15:18:53 -07001484 if (shouldAddToMediaStoreNow && !fail) {
1485 if (mVideoFileDescriptor == null) {
1486 saveVideo();
1487 } else if (mIsVideoCaptureIntent) {
1488 // if no file save is needed, we can show the post capture UI now
1489 showCaptureResult();
1490 }
Michael Kolb8872c232013-01-29 10:33:22 -08001491 }
1492 }
Doris Liu3973deb2013-08-21 13:42:22 -07001493 // release media recorder
1494 releaseMediaRecorder();
Doris Liu4df91582014-03-21 18:33:57 -07001495
1496 mAppController.getCameraAppUI().showModeOptions();
1497 mAppController.getCameraAppUI().animateBottomBarToFullSize(mShutterIconId);
Senpo Hu4ba28292014-09-26 11:05:08 -07001498 if (!mPaused && mCameraDevice != null) {
Doris Liu1143ebd2014-01-24 14:11:50 -08001499 setFocusParameters();
Doris Liu3973deb2013-08-21 13:42:22 -07001500 mCameraDevice.lock();
1501 if (!ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) {
1502 stopPreview();
Doris Liu3973deb2013-08-21 13:42:22 -07001503 // Switch back to use SurfaceTexture for preview.
1504 startPreview();
Michael Kolb8872c232013-01-29 10:33:22 -08001505 }
Angus Kong62753ae2014-02-10 10:53:54 -08001506 // Update the parameters here because the parameters might have been altered
1507 // by MediaRecorder.
Angus Kong6607dae2014-06-10 16:07:45 -07001508 mCameraSettings = mCameraDevice.getSettings();
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01001509 }
Spike Sprague3f673792014-04-14 16:16:27 -07001510
Spike Spraguee6374b72014-04-25 17:24:32 -07001511 // Check this in advance of each shot so we don't add to shutter
1512 // latency. It's true that someone else could write to the SD card
1513 // in the mean time and fill it, but that could have happened
1514 // between the shutter press and saving the file too.
1515 mActivity.updateStorageSpaceAndHint(null);
Spike Sprague3f673792014-04-14 16:16:27 -07001516
Michael Kolb8872c232013-01-29 10:33:22 -08001517 return fail;
1518 }
1519
Michael Kolb8872c232013-01-29 10:33:22 -08001520 private static String millisecondToTimeString(long milliSeconds, boolean displayCentiSeconds) {
1521 long seconds = milliSeconds / 1000; // round down to compute seconds
1522 long minutes = seconds / 60;
1523 long hours = minutes / 60;
1524 long remainderMinutes = minutes - (hours * 60);
1525 long remainderSeconds = seconds - (minutes * 60);
1526
1527 StringBuilder timeStringBuilder = new StringBuilder();
1528
1529 // Hours
1530 if (hours > 0) {
1531 if (hours < 10) {
1532 timeStringBuilder.append('0');
1533 }
1534 timeStringBuilder.append(hours);
1535
1536 timeStringBuilder.append(':');
1537 }
1538
1539 // Minutes
1540 if (remainderMinutes < 10) {
1541 timeStringBuilder.append('0');
1542 }
1543 timeStringBuilder.append(remainderMinutes);
1544 timeStringBuilder.append(':');
1545
1546 // Seconds
1547 if (remainderSeconds < 10) {
1548 timeStringBuilder.append('0');
1549 }
1550 timeStringBuilder.append(remainderSeconds);
1551
1552 // Centi seconds
1553 if (displayCentiSeconds) {
1554 timeStringBuilder.append('.');
1555 long remainderCentiSeconds = (milliSeconds - seconds * 1000) / 10;
1556 if (remainderCentiSeconds < 10) {
1557 timeStringBuilder.append('0');
1558 }
1559 timeStringBuilder.append(remainderCentiSeconds);
1560 }
1561
1562 return timeStringBuilder.toString();
1563 }
1564
Michael Kolb8872c232013-01-29 10:33:22 -08001565 private void updateRecordingTime() {
1566 if (!mMediaRecorderRecording) {
1567 return;
1568 }
1569 long now = SystemClock.uptimeMillis();
1570 long delta = now - mRecordingStartTime;
1571
1572 // Starting a minute before reaching the max duration
1573 // limit, we'll countdown the remaining time instead.
1574 boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0
1575 && delta >= mMaxVideoDurationInMs - 60000);
1576
1577 long deltaAdjusted = delta;
1578 if (countdownRemainingTime) {
1579 deltaAdjusted = Math.max(0, mMaxVideoDurationInMs - deltaAdjusted) + 999;
1580 }
1581 String text;
1582
1583 long targetNextUpdateDelay;
Spike Spragued020fbf2014-07-16 15:47:32 -07001584
1585 text = millisecondToTimeString(deltaAdjusted, false);
1586 targetNextUpdateDelay = 1000;
Michael Kolb8872c232013-01-29 10:33:22 -08001587
Doris Liu6827ce22013-03-12 19:24:28 -07001588 mUI.setRecordingTime(text);
Michael Kolb8872c232013-01-29 10:33:22 -08001589
1590 if (mRecordingTimeCountsDown != countdownRemainingTime) {
1591 // Avoid setting the color on every update, do it only
1592 // when it needs changing.
1593 mRecordingTimeCountsDown = countdownRemainingTime;
1594
Spike Spragued020fbf2014-07-16 15:47:32 -07001595 int color = mActivity.getResources().getColor(R.color.recording_time_remaining_text);
Michael Kolb8872c232013-01-29 10:33:22 -08001596
Doris Liu6827ce22013-03-12 19:24:28 -07001597 mUI.setRecordingTimeTextColor(color);
Michael Kolb8872c232013-01-29 10:33:22 -08001598 }
1599
1600 long actualNextUpdateDelay = targetNextUpdateDelay - (delta % targetNextUpdateDelay);
Angus Kong13e87c42013-11-25 10:02:47 -08001601 mHandler.sendEmptyMessageDelayed(MSG_UPDATE_RECORD_TIME, actualNextUpdateDelay);
Michael Kolb8872c232013-01-29 10:33:22 -08001602 }
1603
1604 private static boolean isSupported(String value, List<String> supported) {
1605 return supported == null ? false : supported.indexOf(value) >= 0;
1606 }
1607
1608 @SuppressWarnings("deprecation")
1609 private void setCameraParameters() {
Erin Dahlgrenbd3da262013-12-02 11:48:13 -08001610 SettingsManager settingsManager = mActivity.getSettingsManager();
1611
I-Jong Lin4ed861a2014-10-10 16:34:34 -07001612 // Update Desired Preview size in case video camera resolution has changed.
1613 updateDesiredPreviewSize();
1614
Angus Kong6607dae2014-06-10 16:07:45 -07001615 mCameraSettings.setPreviewSize(new Size(mDesiredPreviewWidth, mDesiredPreviewHeight));
Andy Huibers6dcf90c2014-04-14 13:53:12 -07001616 // This is required for Samsung SGH-I337 and probably other Samsung S4 versions
1617 if (Build.BRAND.toLowerCase().contains("samsung")) {
Angus Kong6607dae2014-06-10 16:07:45 -07001618 mCameraSettings.setSetting("video-size",
1619 mProfile.videoFrameWidth + "x" + mProfile.videoFrameHeight);
Andy Huibers6dcf90c2014-04-14 13:53:12 -07001620 }
Angus Kong6607dae2014-06-10 16:07:45 -07001621 int[] fpsRange =
1622 CameraUtil.getMaxPreviewFpsRange(mCameraCapabilities.getSupportedPreviewFpsRange());
Doris Liu6432cd62013-06-13 17:20:31 -07001623 if (fpsRange.length > 0) {
Angus Kong831347d2014-06-16 16:07:28 -07001624 mCameraSettings.setPreviewFpsRange(fpsRange[0], fpsRange[1]);
Doris Liu6432cd62013-06-13 17:20:31 -07001625 } else {
Angus Kong6607dae2014-06-10 16:07:45 -07001626 mCameraSettings.setPreviewFrameRate(mProfile.videoFrameRate);
Doris Liu6432cd62013-06-13 17:20:31 -07001627 }
Michael Kolb8872c232013-01-29 10:33:22 -08001628
Erin Dahlgren6190c362014-06-13 14:12:08 -07001629 enableTorchMode(Keys.isCameraBackFacing(settingsManager, mAppController.getModuleScope()));
Michael Kolb8872c232013-01-29 10:33:22 -08001630
Michael Kolb8872c232013-01-29 10:33:22 -08001631 // Set zoom.
Angus Kong88289042014-04-22 16:39:42 -07001632 if (mCameraCapabilities.supports(CameraCapabilities.Feature.ZOOM)) {
Sol Boucher2192fba2014-08-19 17:24:07 -07001633 mCameraSettings.setZoomRatio(mZoomValue);
Michael Kolb8872c232013-01-29 10:33:22 -08001634 }
Doris Liua1ec04a2014-01-13 17:29:40 -08001635 updateFocusParameters();
Michael Kolb8872c232013-01-29 10:33:22 -08001636
Angus Kong831347d2014-06-16 16:07:28 -07001637 mCameraSettings.setRecordingHintEnabled(true);
Michael Kolb8872c232013-01-29 10:33:22 -08001638
Angus Kong6607dae2014-06-10 16:07:45 -07001639 if (mCameraCapabilities.supports(CameraCapabilities.Feature.VIDEO_STABILIZATION)) {
1640 mCameraSettings.setVideoStabilization(true);
Michael Kolb8872c232013-01-29 10:33:22 -08001641 }
1642
1643 // Set picture size.
1644 // The logic here is different from the logic in still-mode camera.
1645 // There we determine the preview size based on the picture size, but
1646 // here we determine the picture size based on the preview size.
Angus Kong6607dae2014-06-10 16:07:45 -07001647 List<Size> supported = mCameraCapabilities.getSupportedPhotoSizes();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001648 Size optimalSize = CameraUtil.getOptimalVideoSnapshotPictureSize(supported,
I-Jong Lin4ed861a2014-10-10 16:34:34 -07001649 mDesiredPreviewWidth, mDesiredPreviewHeight);
Angus Kong6607dae2014-06-10 16:07:45 -07001650 Size original = new Size(mCameraSettings.getCurrentPhotoSize());
Michael Kolb8872c232013-01-29 10:33:22 -08001651 if (!original.equals(optimalSize)) {
Angus Kong6607dae2014-06-10 16:07:45 -07001652 mCameraSettings.setPhotoSize(optimalSize);
Michael Kolb8872c232013-01-29 10:33:22 -08001653 }
Angus Kong00b7b102014-04-24 15:46:52 -07001654 Log.d(TAG, "Video snapshot size is " + optimalSize);
Michael Kolb8872c232013-01-29 10:33:22 -08001655
1656 // Set JPEG quality.
1657 int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId,
1658 CameraProfile.QUALITY_HIGH);
Angus Kong6607dae2014-06-10 16:07:45 -07001659 mCameraSettings.setPhotoJpegCompressionQuality(jpegQuality);
Michael Kolb8872c232013-01-29 10:33:22 -08001660
Senpo Hu4ba28292014-09-26 11:05:08 -07001661 if (mCameraDevice != null) {
1662 mCameraDevice.applySettings(mCameraSettings);
1663 // Nexus 5 through KitKat 4.4.2 requires a second call to
1664 // .setParameters() for frame rate settings to take effect.
1665 mCameraDevice.applySettings(mCameraSettings);
1666 }
Sascha Haeberlingf8b877c2013-09-09 17:32:48 -07001667
1668 // Update UI based on the new parameters.
Angus Kong6607dae2014-06-10 16:07:45 -07001669 mUI.updateOnScreenIndicators(mCameraSettings);
Michael Kolb8872c232013-01-29 10:33:22 -08001670 }
1671
Doris Liua1ec04a2014-01-13 17:29:40 -08001672 private void updateFocusParameters() {
1673 // Set continuous autofocus. During recording, we use "continuous-video"
1674 // auto focus mode to ensure smooth focusing. Whereas during preview (i.e.
1675 // before recording starts) we use "continuous-picture" auto focus mode
1676 // for faster but slightly jittery focusing.
Angus Kong6607dae2014-06-10 16:07:45 -07001677 Set<CameraCapabilities.FocusMode> supportedFocus = mCameraCapabilities
1678 .getSupportedFocusModes();
Doris Liua1ec04a2014-01-13 17:29:40 -08001679 if (mMediaRecorderRecording) {
Angus Kong6607dae2014-06-10 16:07:45 -07001680 if (mCameraCapabilities.supports(CameraCapabilities.FocusMode.CONTINUOUS_VIDEO)) {
1681 mCameraSettings.setFocusMode(CameraCapabilities.FocusMode.CONTINUOUS_VIDEO);
Angus Kong831347d2014-06-16 16:07:28 -07001682 mFocusManager.overrideFocusMode(CameraCapabilities.FocusMode.CONTINUOUS_VIDEO);
Doris Liua1ec04a2014-01-13 17:29:40 -08001683 } else {
1684 mFocusManager.overrideFocusMode(null);
1685 }
1686 } else {
Senpo Hu11186312014-08-08 13:42:13 -07001687 // FIXME(b/16984793): This is broken. For some reasons, CONTINUOUS_PICTURE is not on
1688 // when preview starts.
Doris Liua1ec04a2014-01-13 17:29:40 -08001689 mFocusManager.overrideFocusMode(null);
Angus Kong6607dae2014-06-10 16:07:45 -07001690 if (mCameraCapabilities.supports(CameraCapabilities.FocusMode.CONTINUOUS_PICTURE)) {
Angus Kong831347d2014-06-16 16:07:28 -07001691 mCameraSettings.setFocusMode(
1692 mFocusManager.getFocusMode(mCameraSettings.getCurrentFocusMode()));
Doris Liua1ec04a2014-01-13 17:29:40 -08001693 if (mFocusAreaSupported) {
Angus Kong6607dae2014-06-10 16:07:45 -07001694 mCameraSettings.setFocusAreas(mFocusManager.getFocusAreas());
Doris Liua1ec04a2014-01-13 17:29:40 -08001695 }
1696 }
1697 }
1698 updateAutoFocusMoveCallback();
1699 }
1700
Michael Kolb8872c232013-01-29 10:33:22 -08001701 @Override
Angus Kong20fad242013-11-11 18:23:46 -08001702 public void resume() {
Spike Sprague51c877c2014-02-18 11:14:12 -08001703 if (isVideoCaptureIntent()) {
1704 mDontResetIntentUiOnResume = mPaused;
1705 }
1706
Angus Kongc4e66562013-11-22 23:03:21 -08001707 mPaused = false;
Angus Kong13e87c42013-11-25 10:02:47 -08001708 installIntentFilter();
Erin Dahlgren667630d2014-04-01 14:03:25 -07001709 mAppController.setShutterEnabled(false);
Sol Boucher2192fba2014-08-19 17:24:07 -07001710 mZoomValue = 1.0f;
Angus Kongc4e66562013-11-22 23:03:21 -08001711
Senpo Hu0bdc4b52014-11-17 22:56:31 -08001712 OrientationManager orientationManager = mAppController.getOrientationManager();
1713 orientationManager.addOnOrientationChangeListener(this);
1714 mUI.onOrientationChanged(orientationManager, orientationManager.getDeviceOrientation());
1715
Angus Kongc4e66562013-11-22 23:03:21 -08001716 showVideoSnapshotUI(false);
1717
1718 if (!mPreviewing) {
1719 requestCamera(mCameraId);
1720 } else {
1721 // preview already started
Erin Dahlgren667630d2014-04-01 14:03:25 -07001722 mAppController.setShutterEnabled(true);
Angus Kongc4e66562013-11-22 23:03:21 -08001723 }
1724
Doris Liua1ec04a2014-01-13 17:29:40 -08001725 if (mFocusManager != null) {
1726 // If camera is not open when resume is called, focus manager will not
1727 // be initialized yet, in which case it will start listening to
1728 // preview area size change later in the initialization.
1729 mAppController.addPreviewAreaSizeChangedListener(mFocusManager);
1730 }
Sascha Haeberlingde303232014-02-07 02:30:53 +01001731
Angus Kongc4e66562013-11-22 23:03:21 -08001732 if (mPreviewing) {
1733 mOnResumeTime = SystemClock.uptimeMillis();
Angus Kong13e87c42013-11-25 10:02:47 -08001734 mHandler.sendEmptyMessageDelayed(MSG_CHECK_DISPLAY_ROTATION, 100);
Angus Kongc4e66562013-11-22 23:03:21 -08001735 }
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001736 getServices().getMemoryManager().addListener(this);
Angus Kong20fad242013-11-11 18:23:46 -08001737 }
1738
1739 @Override
1740 public void pause() {
Angus Kongc4e66562013-11-22 23:03:21 -08001741 mPaused = true;
Doris Liua1ec04a2014-01-13 17:29:40 -08001742
Senpo Hu0bdc4b52014-11-17 22:56:31 -08001743 mAppController.getOrientationManager().removeOnOrientationChangeListener(this);
1744
Doris Liua1ec04a2014-01-13 17:29:40 -08001745 if (mFocusManager != null) {
1746 // If camera is not open when resume is called, focus manager will not
1747 // be initialized yet, in which case it will start listening to
1748 // preview area size change later in the initialization.
1749 mAppController.removePreviewAreaSizeChangedListener(mFocusManager);
1750 mFocusManager.removeMessages();
1751 }
Angus Kongc4e66562013-11-22 23:03:21 -08001752 if (mMediaRecorderRecording) {
1753 // Camera will be released in onStopVideoRecording.
1754 onStopVideoRecording();
1755 } else {
1756 stopPreview();
1757 closeCamera();
1758 releaseMediaRecorder();
1759 }
1760
1761 closeVideoFileDescriptor();
Angus Kongc4e66562013-11-22 23:03:21 -08001762
1763 if (mReceiver != null) {
1764 mActivity.unregisterReceiver(mReceiver);
1765 mReceiver = null;
1766 }
Angus Kongc4e66562013-11-22 23:03:21 -08001767
Angus Kong13e87c42013-11-25 10:02:47 -08001768 mHandler.removeMessages(MSG_CHECK_DISPLAY_ROTATION);
1769 mHandler.removeMessages(MSG_SWITCH_CAMERA);
1770 mHandler.removeMessages(MSG_SWITCH_CAMERA_START_ANIMATION);
Angus Kongc4e66562013-11-22 23:03:21 -08001771 mPendingSwitchCameraId = -1;
1772 mSwitchingCamera = false;
1773 mPreferenceRead = false;
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001774 getServices().getMemoryManager().removeListener(this);
Alan Newberger73514152014-09-10 15:03:27 -07001775 mUI.onPause();
Angus Kong20fad242013-11-11 18:23:46 -08001776 }
1777
1778 @Override
1779 public void destroy() {
1780
1781 }
1782
1783 @Override
Angus Kong2f0e4a32013-12-03 10:02:35 -08001784 public void onLayoutOrientationChanged(boolean isLandscape) {
Michael Kolb8872c232013-01-29 10:33:22 -08001785 setDisplayOrientation();
Michael Kolb8872c232013-01-29 10:33:22 -08001786 }
1787
Erin Dahlgrena07e94c2013-12-04 18:44:08 -08001788 // TODO: integrate this into the SettingsManager listeners.
Michael Kolb8872c232013-01-29 10:33:22 -08001789 public void onSharedPreferenceChanged() {
Michael Kolb8872c232013-01-29 10:33:22 -08001790
Michael Kolb8872c232013-01-29 10:33:22 -08001791 }
1792
1793 private void switchCamera() {
Sascha Haeberlingf8b877c2013-09-09 17:32:48 -07001794 if (mPaused) {
1795 return;
1796 }
Erin Dahlgrenbd3da262013-12-02 11:48:13 -08001797 SettingsManager settingsManager = mActivity.getSettingsManager();
Michael Kolb8872c232013-01-29 10:33:22 -08001798
1799 Log.d(TAG, "Start to switch camera.");
1800 mCameraId = mPendingSwitchCameraId;
1801 mPendingSwitchCameraId = -1;
Erin Dahlgren6190c362014-06-13 14:12:08 -07001802 settingsManager.set(mAppController.getModuleScope(),
1803 Keys.KEY_CAMERA_ID, mCameraId);
Michael Kolb8872c232013-01-29 10:33:22 -08001804
Doris Liua1ec04a2014-01-13 17:29:40 -08001805 if (mFocusManager != null) {
1806 mFocusManager.removeMessages();
1807 }
Michael Kolb8872c232013-01-29 10:33:22 -08001808 closeCamera();
Angus Kong20fad242013-11-11 18:23:46 -08001809 requestCamera(mCameraId);
Michael Kolb8872c232013-01-29 10:33:22 -08001810
Sascha Haeberling6ccec202014-03-11 09:44:34 -07001811 mMirror = isCameraFrontFacing();
Doris Liua1ec04a2014-01-13 17:29:40 -08001812 if (mFocusManager != null) {
1813 mFocusManager.setMirror(mMirror);
1814 }
1815
Michael Kolb8872c232013-01-29 10:33:22 -08001816 // From onResume
Sol Boucher2192fba2014-08-19 17:24:07 -07001817 mZoomValue = 1.0f;
Doris Liu6827ce22013-03-12 19:24:28 -07001818 mUI.setOrientationIndicator(0, false);
Michael Kolb8872c232013-01-29 10:33:22 -08001819
Doris Liu6432cd62013-06-13 17:20:31 -07001820 // Start switch camera animation. Post a message because
1821 // onFrameAvailable from the old camera may already exist.
Angus Kong13e87c42013-11-25 10:02:47 -08001822 mHandler.sendEmptyMessage(MSG_SWITCH_CAMERA_START_ANIMATION);
Angus Kong6607dae2014-06-10 16:07:45 -07001823 mUI.updateOnScreenIndicators(mCameraSettings);
Michael Kolb8872c232013-01-29 10:33:22 -08001824 }
1825
Michael Kolb8872c232013-01-29 10:33:22 -08001826 private void initializeVideoSnapshot() {
Angus Kong6607dae2014-06-10 16:07:45 -07001827 if (mCameraSettings == null) {
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01001828 return;
1829 }
Michael Kolb8872c232013-01-29 10:33:22 -08001830 }
1831
1832 void showVideoSnapshotUI(boolean enabled) {
Angus Kong6607dae2014-06-10 16:07:45 -07001833 if (mCameraSettings == null) {
Sascha Haeberling846d3ab2014-02-04 12:48:55 +01001834 return;
1835 }
Angus Kong88289042014-04-22 16:39:42 -07001836 if (mCameraCapabilities.supports(CameraCapabilities.Feature.VIDEO_SNAPSHOT) &&
1837 !mIsVideoCaptureIntent) {
Doris Liu6432cd62013-06-13 17:20:31 -07001838 if (enabled) {
Senpo Hu427a9152014-11-19 17:11:46 -08001839 mAppController.startFlashAnimation(false);
Michael Kolb8872c232013-01-29 10:33:22 -08001840 } else {
Doris Liu6827ce22013-03-12 19:24:28 -07001841 mUI.showPreviewBorder(enabled);
Michael Kolb8872c232013-01-29 10:33:22 -08001842 }
Erin Dahlgren667630d2014-04-01 14:03:25 -07001843 mAppController.setShutterEnabled(!enabled);
Michael Kolb8872c232013-01-29 10:33:22 -08001844 }
1845 }
1846
Erin Dahlgrenfce8a0b2013-12-12 17:36:32 -08001847 /**
1848 * Used to update the flash mode. Video mode can turn on the flash as torch
1849 * mode, which we would like to turn on and off when we switching in and
1850 * out to the preview.
1851 *
1852 * @param enable Whether torch mode can be enabled.
1853 */
1854 private void enableTorchMode(boolean enable) {
Angus Kong6607dae2014-06-10 16:07:45 -07001855 if (mCameraSettings.getCurrentFlashMode() == null) {
Erin Dahlgrenfce8a0b2013-12-12 17:36:32 -08001856 return;
1857 }
1858
Erin Dahlgrenbd3da262013-12-02 11:48:13 -08001859 SettingsManager settingsManager = mActivity.getSettingsManager();
1860
Angus Kong6607dae2014-06-10 16:07:45 -07001861 CameraCapabilities.Stringifier stringifier = mCameraCapabilities.getStringifier();
1862 CameraCapabilities.FlashMode flashMode;
Erin Dahlgrenfce8a0b2013-12-12 17:36:32 -08001863 if (enable) {
Erin Dahlgren6190c362014-06-13 14:12:08 -07001864 flashMode = stringifier
1865 .flashModeFromString(settingsManager.getString(mAppController.getCameraScope(),
1866 Keys.KEY_VIDEOCAMERA_FLASH_MODE));
ztenghui7b265a62013-09-09 14:58:44 -07001867 } else {
Angus Kong6607dae2014-06-10 16:07:45 -07001868 flashMode = CameraCapabilities.FlashMode.OFF;
ztenghui7b265a62013-09-09 14:58:44 -07001869 }
Angus Kong6607dae2014-06-10 16:07:45 -07001870 if (mCameraCapabilities.supports(flashMode)) {
1871 mCameraSettings.setFlashMode(flashMode);
1872 }
1873 /* TODO: Find out how to deal with the following code piece:
1874 else {
1875 flashMode = mCameraSettings.getCurrentFlashMode();
ztenghui7b265a62013-09-09 14:58:44 -07001876 if (flashMode == null) {
1877 flashMode = mActivity.getString(
1878 R.string.pref_camera_flashmode_no_flash);
Angus Kongfaaee012013-12-07 00:38:46 -08001879 mParameters.setFlashMode(flashMode);
Michael Kolb8872c232013-01-29 10:33:22 -08001880 }
Angus Kong6607dae2014-06-10 16:07:45 -07001881 }*/
Senpo Hu4ba28292014-09-26 11:05:08 -07001882 if (mCameraDevice != null) {
1883 mCameraDevice.applySettings(mCameraSettings);
1884 }
Angus Kong6607dae2014-06-10 16:07:45 -07001885 mUI.updateOnScreenIndicators(mCameraSettings);
ztenghui7b265a62013-09-09 14:58:44 -07001886 }
1887
Michael Kolb8872c232013-01-29 10:33:22 -08001888 @Override
Sascha Haeberling8c1a9222014-02-25 09:38:06 -08001889 public void onPreviewVisibilityChanged(int visibility) {
Erin Dahlgrenfce8a0b2013-12-12 17:36:32 -08001890 if (mPreviewing) {
Sascha Haeberling8c1a9222014-02-25 09:38:06 -08001891 enableTorchMode(visibility == ModuleController.VISIBILITY_VISIBLE);
Erin Dahlgrenfce8a0b2013-12-12 17:36:32 -08001892 }
Erin Dahlgren3044d8c2013-10-10 18:23:45 -07001893 }
1894
Angus Kong9ef99252013-07-18 18:04:19 -07001895 private final class JpegPictureCallback implements CameraPictureCallback {
Michael Kolb8872c232013-01-29 10:33:22 -08001896 Location mLocation;
1897
1898 public JpegPictureCallback(Location loc) {
1899 mLocation = loc;
1900 }
1901
1902 @Override
Angus Kong9ef99252013-07-18 18:04:19 -07001903 public void onPictureTaken(byte [] jpegData, CameraProxy camera) {
Doris Liu057b21a2014-04-25 17:35:27 -07001904 Log.i(TAG, "Video snapshot taken.");
Michael Kolb8872c232013-01-29 10:33:22 -08001905 mSnapshotInProgress = false;
1906 showVideoSnapshotUI(false);
1907 storeImage(jpegData, mLocation);
1908 }
1909 }
1910
1911 private void storeImage(final byte[] data, Location loc) {
1912 long dateTaken = System.currentTimeMillis();
Angus Kongb50b5cb2013-08-09 14:55:20 -07001913 String title = CameraUtil.createJpegName(dateTaken);
Angus Kong0d00a892013-03-26 11:40:40 -07001914 ExifInterface exif = Exif.getExif(data);
1915 int orientation = Exif.getOrientation(exif);
Doris Liu6df2d962013-08-20 16:31:29 -07001916
Erin Dahlgren6190c362014-06-13 14:12:08 -07001917 String flashSetting = mActivity.getSettingsManager()
1918 .getString(mAppController.getCameraScope(), Keys.KEY_VIDEOCAMERA_FLASH_MODE);
1919 Boolean gridLinesOn = Keys.areGridLinesOn(mActivity.getSettingsManager());
Andy Huibers10c58162014-03-29 14:06:54 -07001920 UsageStatistics.instance().photoCaptureDoneEvent(
1921 eventprotos.NavigationChange.Mode.VIDEO_STILL, title + ".jpeg", exif,
Andy Huibersb7c7d9a2014-06-18 22:26:14 -07001922 isCameraFrontFacing(), false, currentZoomValue(), flashSetting, gridLinesOn,
1923 null, null, null);
Andy Huibers10c58162014-03-29 14:06:54 -07001924
Sascha Haeberling280fd3e2013-11-21 13:52:15 -08001925 getServices().getMediaSaver().addImage(
Doris Liu6df2d962013-08-20 16:31:29 -07001926 data, title, dateTaken, loc, orientation,
Angus Kong83a99ae2013-04-17 15:37:07 -07001927 exif, mOnPhotoSavedListener, mContentResolver);
Michael Kolb8872c232013-01-29 10:33:22 -08001928 }
1929
Michael Kolb8872c232013-01-29 10:33:22 -08001930 private String convertOutputFormatToMimeType(int outputFileFormat) {
1931 if (outputFileFormat == MediaRecorder.OutputFormat.MPEG_4) {
1932 return "video/mp4";
1933 }
1934 return "video/3gpp";
1935 }
1936
1937 private String convertOutputFormatToFileExt(int outputFileFormat) {
1938 if (outputFileFormat == MediaRecorder.OutputFormat.MPEG_4) {
1939 return ".mp4";
1940 }
1941 return ".3gp";
1942 }
1943
1944 private void closeVideoFileDescriptor() {
1945 if (mVideoFileDescriptor != null) {
1946 try {
1947 mVideoFileDescriptor.close();
1948 } catch (IOException e) {
1949 Log.e(TAG, "Fail to close fd", e);
1950 }
1951 mVideoFileDescriptor = null;
1952 }
1953 }
1954
Michael Kolb8872c232013-01-29 10:33:22 -08001955 @Override
Angus Kong395ee2d2013-07-15 12:42:41 -07001956 public void onPreviewUIReady() {
1957 startPreview();
1958 }
1959
1960 @Override
1961 public void onPreviewUIDestroyed() {
1962 stopPreview();
1963 }
Angus Kong20fad242013-11-11 18:23:46 -08001964
1965 private void requestCamera(int id) {
1966 mActivity.getCameraProvider().requestCamera(id);
1967 }
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001968
1969 @Override
1970 public void onMemoryStateChanged(int state) {
Erin Dahlgren667630d2014-04-01 14:03:25 -07001971 mAppController.setShutterEnabled(state == MemoryManager.STATE_OK);
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -08001972 }
1973
1974 @Override
1975 public void onLowMemory() {
1976 // Not much we can do in the video module.
1977 }
1978
Doris Liua1ec04a2014-01-13 17:29:40 -08001979 /***********************FocusOverlayManager Listener****************************/
1980 @Override
1981 public void autoFocus() {
Senpo Hu4ba28292014-09-26 11:05:08 -07001982 if (mCameraDevice != null) {
1983 mCameraDevice.autoFocus(mHandler, mAutoFocusCallback);
1984 }
Doris Liua1ec04a2014-01-13 17:29:40 -08001985 }
1986
1987 @Override
1988 public void cancelAutoFocus() {
Senpo Hu4ba28292014-09-26 11:05:08 -07001989 if (mCameraDevice != null) {
1990 mCameraDevice.cancelAutoFocus();
1991 setFocusParameters();
1992 }
Doris Liua1ec04a2014-01-13 17:29:40 -08001993 }
1994
1995 @Override
1996 public boolean capture() {
1997 return false;
1998 }
1999
2000 @Override
2001 public void startFaceDetection() {
2002
2003 }
2004
2005 @Override
2006 public void stopFaceDetection() {
2007
2008 }
2009
2010 @Override
2011 public void setFocusParameters() {
Senpo Hu4ba28292014-09-26 11:05:08 -07002012 if (mCameraDevice != null) {
2013 updateFocusParameters();
2014 mCameraDevice.applySettings(mCameraSettings);
2015 }
Doris Liua1ec04a2014-01-13 17:29:40 -08002016 }
Michael Kolb8872c232013-01-29 10:33:22 -08002017}