Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.camera; |
| 18 | |
| 19 | import android.annotation.TargetApi; |
| 20 | import android.app.Activity; |
| 21 | import android.content.ActivityNotFoundException; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.ContentValues; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.content.SharedPreferences.Editor; |
| 29 | import android.content.res.Configuration; |
| 30 | import android.graphics.Bitmap; |
| 31 | import android.graphics.SurfaceTexture; |
| 32 | import android.hardware.Camera.CameraInfo; |
| 33 | import android.hardware.Camera.Parameters; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 34 | import android.hardware.Camera.Size; |
| 35 | import android.location.Location; |
Marco Nelissen | 20694b2 | 2013-10-29 15:27:24 -0700 | [diff] [blame] | 36 | import android.media.AudioManager; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 37 | import android.media.CamcorderProfile; |
| 38 | import android.media.CameraProfile; |
| 39 | import android.media.MediaRecorder; |
| 40 | import android.net.Uri; |
| 41 | import android.os.Build; |
| 42 | import android.os.Bundle; |
| 43 | import android.os.Handler; |
| 44 | import android.os.Message; |
| 45 | import android.os.ParcelFileDescriptor; |
| 46 | import android.os.SystemClock; |
| 47 | import android.provider.MediaStore; |
Ruben Brunk | 1600796 | 2013-04-19 15:27:57 -0700 | [diff] [blame] | 48 | import android.provider.MediaStore.MediaColumns; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 49 | import android.provider.MediaStore.Video; |
| 50 | import android.util.Log; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 51 | import android.view.KeyEvent; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 52 | import android.view.OrientationEventListener; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 53 | import android.view.View; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 54 | import android.widget.Toast; |
| 55 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 56 | import com.android.camera.app.AppController; |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 57 | import com.android.camera.app.CameraManager.CameraPictureCallback; |
| 58 | import com.android.camera.app.CameraManager.CameraProxy; |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 59 | import com.android.camera.app.MediaSaver; |
ztenghui | a16e7b5 | 2013-08-23 11:47:56 -0700 | [diff] [blame] | 60 | import com.android.camera.exif.ExifInterface; |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 61 | import com.android.camera.module.ModuleController; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 62 | import com.android.camera.ui.RotateTextToast; |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 63 | import com.android.camera.util.AccessibilityUtils; |
| 64 | import com.android.camera.util.ApiHelper; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 65 | import com.android.camera.util.CameraUtil; |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 66 | import com.android.camera.util.UsageStatistics; |
| 67 | import com.android.camera2.R; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 68 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 69 | import java.io.File; |
| 70 | import java.io.IOException; |
| 71 | import java.text.SimpleDateFormat; |
| 72 | import java.util.Date; |
| 73 | import java.util.Iterator; |
| 74 | import java.util.List; |
| 75 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 76 | public class VideoModule extends CameraModule |
| 77 | implements ModuleController, |
| 78 | VideoController, |
| 79 | CameraPreference.OnPreferenceChangedListener, |
| 80 | ShutterButton.OnShutterButtonListener, |
| 81 | MediaRecorder.OnErrorListener, |
| 82 | MediaRecorder.OnInfoListener { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 83 | |
| 84 | private static final String TAG = "CAM_VideoModule"; |
| 85 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 86 | // Messages defined for the UI thread handler. |
| 87 | private static final int MSG_CHECK_DISPLAY_ROTATION = 4; |
| 88 | private static final int MSG_UPDATE_RECORD_TIME = 5; |
| 89 | private static final int MSG_ENABLE_SHUTTER_BUTTON = 6; |
| 90 | private static final int MSG_SHOW_TAP_TO_SNAPSHOT_TOAST = 7; |
| 91 | private static final int MSG_SWITCH_CAMERA = 8; |
| 92 | private static final int MSG_SWITCH_CAMERA_START_ANIMATION = 9; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 93 | |
| 94 | private static final long SHUTTER_BUTTON_TIMEOUT = 500L; // 500ms |
| 95 | |
| 96 | /** |
| 97 | * An unpublished intent flag requesting to start recording straight away |
| 98 | * and return as soon as recording is stopped. |
| 99 | * TODO: consider publishing by moving into MediaStore. |
| 100 | */ |
| 101 | private static final String EXTRA_QUICK_CAPTURE = |
| 102 | "android.intent.extra.quickCapture"; |
| 103 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 104 | // module fields |
| 105 | private CameraActivity mActivity; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 106 | private boolean mPaused; |
| 107 | private int mCameraId; |
| 108 | private Parameters mParameters; |
| 109 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 110 | private boolean mIsInReviewMode; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 111 | private boolean mSnapshotInProgress = false; |
| 112 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 113 | private final CameraErrorCallback mErrorCallback = new CameraErrorCallback(); |
| 114 | |
| 115 | private ComboPreferences mPreferences; |
| 116 | private PreferenceGroup mPreferenceGroup; |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 117 | // Preference must be read before starting preview. We check this before starting |
| 118 | // preview. |
| 119 | private boolean mPreferenceRead; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 120 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 121 | private boolean mIsVideoCaptureIntent; |
| 122 | private boolean mQuickCapture; |
| 123 | |
| 124 | private MediaRecorder mMediaRecorder; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 125 | |
| 126 | private boolean mSwitchingCamera; |
| 127 | private boolean mMediaRecorderRecording = false; |
| 128 | private long mRecordingStartTime; |
| 129 | private boolean mRecordingTimeCountsDown = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 130 | private long mOnResumeTime; |
| 131 | // The video file that the hardware camera is about to record into |
| 132 | // (or is recording into.) |
| 133 | private String mVideoFilename; |
| 134 | private ParcelFileDescriptor mVideoFileDescriptor; |
| 135 | |
| 136 | // The video file that has already been recorded, and that is being |
| 137 | // examined by the user. |
| 138 | private String mCurrentVideoFilename; |
| 139 | private Uri mCurrentVideoUri; |
ztenghui | 70bd024 | 2013-10-14 11:15:44 -0700 | [diff] [blame] | 140 | private boolean mCurrentVideoUriFromMediaSaved; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 141 | private ContentValues mCurrentVideoValues; |
| 142 | |
| 143 | private CamcorderProfile mProfile; |
| 144 | |
| 145 | // The video duration limit. 0 menas no limit. |
| 146 | private int mMaxVideoDurationInMs; |
| 147 | |
| 148 | // Time Lapse parameters. |
| 149 | private boolean mCaptureTimeLapse = false; |
| 150 | // Default 0. If it is larger than 0, the camcorder is in time lapse mode. |
| 151 | private int mTimeBetweenTimeLapseFrameCaptureMs = 0; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 152 | |
| 153 | boolean mPreviewing = false; // True if preview is started. |
| 154 | // The display rotation in degrees. This is only valid when mPreviewing is |
| 155 | // true. |
| 156 | private int mDisplayRotation; |
| 157 | private int mCameraDisplayOrientation; |
| 158 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 159 | private int mDesiredPreviewWidth; |
| 160 | private int mDesiredPreviewHeight; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 161 | private ContentResolver mContentResolver; |
| 162 | |
| 163 | private LocationManager mLocationManager; |
| 164 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 165 | private int mPendingSwitchCameraId; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 166 | private final Handler mHandler = new MainHandler(); |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 167 | private VideoUI mUI; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 168 | private CameraProxy mCameraDevice; |
| 169 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 170 | // The degrees of the device rotated clockwise from its natural orientation. |
| 171 | private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; |
| 172 | |
| 173 | private int mZoomValue; // The current zoom value. |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 174 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 175 | private final MediaSaver.OnMediaSavedListener mOnVideoSavedListener = |
| 176 | new MediaSaver.OnMediaSavedListener() { |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 177 | @Override |
| 178 | public void onMediaSaved(Uri uri) { |
| 179 | if (uri != null) { |
Doris Liu | 2a7f44c | 2013-08-12 15:18:53 -0700 | [diff] [blame] | 180 | mCurrentVideoUri = uri; |
ztenghui | 70bd024 | 2013-10-14 11:15:44 -0700 | [diff] [blame] | 181 | mCurrentVideoUriFromMediaSaved = true; |
Doris Liu | 2a7f44c | 2013-08-12 15:18:53 -0700 | [diff] [blame] | 182 | onVideoSaved(); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 183 | mActivity.notifyNewMedia(uri); |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | }; |
| 187 | |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 188 | private final MediaSaver.OnMediaSavedListener mOnPhotoSavedListener = |
| 189 | new MediaSaver.OnMediaSavedListener() { |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 190 | @Override |
| 191 | public void onMediaSaved(Uri uri) { |
| 192 | if (uri != null) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 193 | mActivity.notifyNewMedia(uri); |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | }; |
| 197 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 198 | private void openCamera() { |
Angus Kong | 4f795b8 | 2013-09-16 14:25:35 -0700 | [diff] [blame] | 199 | if (mCameraDevice == null) { |
| 200 | mCameraDevice = CameraUtil.openCamera( |
| 201 | mActivity, mCameraId, mHandler, |
| 202 | mActivity.getCameraOpenErrorCallback()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 203 | } |
Angus Kong | 4f795b8 | 2013-09-16 14:25:35 -0700 | [diff] [blame] | 204 | if (mCameraDevice == null) { |
| 205 | // Error. |
| 206 | return; |
| 207 | } |
| 208 | mParameters = mCameraDevice.getParameters(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // This Handler is used to post message back onto the main thread of the |
| 212 | // application |
| 213 | private class MainHandler extends Handler { |
| 214 | @Override |
| 215 | public void handleMessage(Message msg) { |
| 216 | switch (msg.what) { |
| 217 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 218 | case MSG_ENABLE_SHUTTER_BUTTON: |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 219 | mUI.enableShutter(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 220 | break; |
| 221 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 222 | case MSG_UPDATE_RECORD_TIME: { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 223 | updateRecordingTime(); |
| 224 | break; |
| 225 | } |
| 226 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 227 | case MSG_CHECK_DISPLAY_ROTATION: { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 228 | // Restart the preview if display rotation has changed. |
| 229 | // Sometimes this happens when the device is held upside |
| 230 | // down and camera app is opened. Rotation animation will |
| 231 | // take some time and the rotation value we have got may be |
| 232 | // wrong. Framework does not have a callback for this now. |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 233 | if ((CameraUtil.getDisplayRotation(mActivity) != mDisplayRotation) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 234 | && !mMediaRecorderRecording && !mSwitchingCamera) { |
| 235 | startPreview(); |
| 236 | } |
| 237 | if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) { |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 238 | mHandler.sendEmptyMessageDelayed(MSG_CHECK_DISPLAY_ROTATION, 100); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 239 | } |
| 240 | break; |
| 241 | } |
| 242 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 243 | case MSG_SHOW_TAP_TO_SNAPSHOT_TOAST: { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 244 | showTapToSnapshotToast(); |
| 245 | break; |
| 246 | } |
| 247 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 248 | case MSG_SWITCH_CAMERA: { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 249 | switchCamera(); |
| 250 | break; |
| 251 | } |
| 252 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 253 | case MSG_SWITCH_CAMERA_START_ANIMATION: { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 254 | //TODO: |
| 255 | //((CameraScreenNail) mActivity.mCameraScreenNail).animateSwitchCamera(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 256 | |
| 257 | // Enable all camera controls. |
| 258 | mSwitchingCamera = false; |
| 259 | break; |
| 260 | } |
| 261 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 262 | default: |
| 263 | Log.v(TAG, "Unhandled message: " + msg.what); |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private BroadcastReceiver mReceiver = null; |
| 270 | |
| 271 | private class MyBroadcastReceiver extends BroadcastReceiver { |
| 272 | @Override |
| 273 | public void onReceive(Context context, Intent intent) { |
| 274 | String action = intent.getAction(); |
| 275 | if (action.equals(Intent.ACTION_MEDIA_EJECT)) { |
| 276 | stopVideoRecording(); |
| 277 | } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) { |
| 278 | Toast.makeText(mActivity, |
| 279 | mActivity.getResources().getString(R.string.wait), Toast.LENGTH_LONG).show(); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 284 | /** |
| 285 | * Construct a new video module. |
| 286 | */ |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 287 | public VideoModule(AppController app) { |
| 288 | super(app); |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 291 | private String createName(long dateTaken) { |
| 292 | Date date = new Date(dateTaken); |
| 293 | SimpleDateFormat dateFormat = new SimpleDateFormat( |
| 294 | mActivity.getString(R.string.video_file_name_format)); |
| 295 | |
| 296 | return dateFormat.format(date); |
| 297 | } |
| 298 | |
| 299 | private int getPreferredCameraId(ComboPreferences preferences) { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 300 | int intentCameraId = CameraUtil.getCameraFacingIntentExtras(mActivity); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 301 | if (intentCameraId != -1) { |
| 302 | // Testing purpose. Launch a specific camera through the intent |
| 303 | // extras. |
| 304 | return intentCameraId; |
| 305 | } else { |
| 306 | return CameraSettings.readPreferredCameraId(preferences); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private void initializeSurfaceView() { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 311 | if (!ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) { // API level < 16 |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 312 | mUI.initializeSurfaceView(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 316 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 317 | @Override |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 318 | public void init(AppController app, boolean isSecureCamera, boolean isCaptureIntent) { |
| 319 | mActivity = (CameraActivity) app.getAndroidContext(); |
| 320 | mUI = new VideoUI(mActivity, this, app.getModuleLayoutRoot()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 321 | mPreferences = new ComboPreferences(mActivity); |
| 322 | CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal()); |
| 323 | mCameraId = getPreferredCameraId(mPreferences); |
| 324 | |
| 325 | mPreferences.setLocalId(mActivity, mCameraId); |
| 326 | CameraSettings.upgradeLocalPreferences(mPreferences.getLocal()); |
| 327 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 328 | /* |
| 329 | * To reduce startup time, we start the preview in another thread. |
| 330 | * We make sure the preview is started at the end of onCreate. |
| 331 | */ |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 332 | requestCamera(mCameraId); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 333 | |
| 334 | mContentResolver = mActivity.getContentResolver(); |
| 335 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 336 | // Surface texture is from camera screen nail and startPreview needs it. |
| 337 | // This must be done before startPreview. |
| 338 | mIsVideoCaptureIntent = isVideoCaptureIntent(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 339 | initializeSurfaceView(); |
| 340 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 341 | mUI.setPrefChangedListener(this); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 342 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 343 | mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false); |
Erin Dahlgren | 21c21a6 | 2013-11-19 16:37:38 -0800 | [diff] [blame] | 344 | mLocationManager = mActivity.getLocationManager(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 345 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 346 | mUI.setOrientationIndicator(0, false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 347 | setDisplayOrientation(); |
| 348 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 349 | mUI.showTimeLapseUI(mCaptureTimeLapse); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 350 | mPendingSwitchCameraId = -1; |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // SingleTapListener |
| 354 | // Preview area is touched. Take a picture. |
| 355 | @Override |
| 356 | public void onSingleTapUp(View view, int x, int y) { |
Doris Liu | 3860574 | 2013-08-13 15:01:52 -0700 | [diff] [blame] | 357 | takeASnapshot(); |
| 358 | } |
| 359 | |
| 360 | private void takeASnapshot() { |
Sascha Haeberling | a514b14 | 2013-08-13 16:10:01 -0700 | [diff] [blame] | 361 | // Only take snapshots if video snapshot is supported by device |
Doris Liu | 3860574 | 2013-08-13 15:01:52 -0700 | [diff] [blame] | 362 | if (CameraUtil.isVideoSnapshotSupported(mParameters) && !mIsVideoCaptureIntent) { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 363 | if (!mMediaRecorderRecording || mPaused || mSnapshotInProgress) { |
Sascha Haeberling | a514b14 | 2013-08-13 16:10:01 -0700 | [diff] [blame] | 364 | return; |
| 365 | } |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 366 | MediaSaver s = getServices().getMediaSaver(); |
Sascha Haeberling | a514b14 | 2013-08-13 16:10:01 -0700 | [diff] [blame] | 367 | if (s == null || s.isQueueFull()) { |
Doris Liu | 3860574 | 2013-08-13 15:01:52 -0700 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | |
| 371 | // Set rotation and gps data. |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 372 | int rotation = CameraUtil.getJpegRotation(mActivity, mCameraId, mOrientation); |
Doris Liu | 3860574 | 2013-08-13 15:01:52 -0700 | [diff] [blame] | 373 | mParameters.setRotation(rotation); |
| 374 | Location loc = mLocationManager.getCurrentLocation(); |
| 375 | CameraUtil.setGpsParameters(mParameters, loc); |
| 376 | mCameraDevice.setParameters(mParameters); |
| 377 | |
| 378 | Log.v(TAG, "Video snapshot start"); |
| 379 | mCameraDevice.takePicture(mHandler, |
| 380 | null, null, null, new JpegPictureCallback(loc)); |
| 381 | showVideoSnapshotUI(true); |
| 382 | mSnapshotInProgress = true; |
| 383 | UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA, |
| 384 | UsageStatistics.ACTION_CAPTURE_DONE, "VideoSnapshot"); |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 385 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 388 | private void loadCameraPreferences() { |
| 389 | CameraSettings settings = new CameraSettings(mActivity, mParameters, |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 390 | mCameraId, mActivity.getCameraProvider().getCameraInfo()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 391 | // Remove the video quality preference setting when the quality is given in the intent. |
| 392 | mPreferenceGroup = filterPreferenceScreenByIntent( |
| 393 | settings.getPreferenceGroup(R.xml.video_preferences)); |
| 394 | } |
| 395 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 396 | private void initializeVideoControl() { |
| 397 | loadCameraPreferences(); |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 398 | mUI.initializePopup(mPreferenceGroup); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 401 | @Override |
| 402 | public void onOrientationChanged(int orientation) { |
| 403 | // We keep the last known orientation. So if the user first orient |
| 404 | // the camera then point the camera to floor or sky, we still have |
| 405 | // the correct orientation. |
| 406 | if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 407 | int newOrientation = CameraUtil.roundOrientation(orientation, mOrientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 408 | |
| 409 | if (mOrientation != newOrientation) { |
| 410 | mOrientation = newOrientation; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // Show the toast after getting the first orientation changed. |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 414 | if (mHandler.hasMessages(MSG_SHOW_TAP_TO_SNAPSHOT_TOAST)) { |
| 415 | mHandler.removeMessages(MSG_SHOW_TAP_TO_SNAPSHOT_TOAST); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 416 | showTapToSnapshotToast(); |
| 417 | } |
| 418 | } |
| 419 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 420 | @Override |
| 421 | public void onCameraAvailable(CameraProxy cameraProxy) { |
| 422 | mCameraDevice = cameraProxy; |
| 423 | readVideoPreferences(); |
| 424 | resizeForPreviewAspectRatio(); |
| 425 | startPreview(); |
| 426 | initializeVideoSnapshot(); |
| 427 | initializeVideoControl(); |
| 428 | mUI.initializeZoom(mParameters); |
| 429 | } |
| 430 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 431 | private void startPlayVideoActivity() { |
| 432 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 433 | intent.setDataAndType(mCurrentVideoUri, convertOutputFormatToMimeType(mProfile.fileFormat)); |
| 434 | try { |
Sascha Haeberling | b7639c6 | 2013-09-09 14:42:43 -0700 | [diff] [blame] | 435 | mActivity |
| 436 | .startActivityForResult(intent, CameraActivity.REQ_CODE_DONT_SWITCH_TO_PREVIEW); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 437 | } catch (ActivityNotFoundException ex) { |
| 438 | Log.e(TAG, "Couldn't view video " + mCurrentVideoUri, ex); |
| 439 | } |
| 440 | } |
| 441 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 442 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 443 | @OnClickAttr |
| 444 | public void onReviewPlayClicked(View v) { |
| 445 | startPlayVideoActivity(); |
| 446 | } |
| 447 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 448 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 449 | @OnClickAttr |
| 450 | public void onReviewDoneClicked(View v) { |
Doris Liu | 69ef5ea | 2013-05-07 13:48:10 -0700 | [diff] [blame] | 451 | mIsInReviewMode = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 452 | doReturnToCaller(true); |
| 453 | } |
| 454 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 455 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 456 | @OnClickAttr |
| 457 | public void onReviewCancelClicked(View v) { |
ztenghui | af3a197 | 2013-09-17 16:51:15 -0700 | [diff] [blame] | 458 | // TODO: It should be better to not even insert the URI at all before we |
| 459 | // confirm done in review, which means we need to handle temporary video |
| 460 | // files in a quite different way than we currently had. |
ztenghui | 70bd024 | 2013-10-14 11:15:44 -0700 | [diff] [blame] | 461 | // Make sure we don't delete the Uri sent from the video capture intent. |
| 462 | if (mCurrentVideoUriFromMediaSaved) { |
ztenghui | dfe5b15 | 2013-10-08 17:07:15 -0700 | [diff] [blame] | 463 | mContentResolver.delete(mCurrentVideoUri, null, null); |
| 464 | } |
ztenghui | 638bf9a | 2013-10-09 10:52:33 -0700 | [diff] [blame] | 465 | mIsInReviewMode = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 466 | doReturnToCaller(false); |
| 467 | } |
| 468 | |
Doris Liu | 69ef5ea | 2013-05-07 13:48:10 -0700 | [diff] [blame] | 469 | @Override |
| 470 | public boolean isInReviewMode() { |
| 471 | return mIsInReviewMode; |
| 472 | } |
| 473 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 474 | private void onStopVideoRecording() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 475 | boolean recordFail = stopVideoRecording(); |
| 476 | if (mIsVideoCaptureIntent) { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 477 | if (mQuickCapture) { |
| 478 | doReturnToCaller(!recordFail); |
| 479 | } else if (!recordFail) { |
| 480 | showCaptureResult(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 481 | } |
| 482 | } else if (!recordFail){ |
| 483 | // Start capture animation. |
| 484 | if (!mPaused && ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) { |
| 485 | // The capture animation is disabled on ICS because we use SurfaceView |
| 486 | // for preview during recording. When the recording is done, we switch |
| 487 | // back to use SurfaceTexture for preview and we need to stop then start |
| 488 | // the preview. This will cause the preview flicker since the preview |
| 489 | // will not be continuous for a short period of time. |
Sascha Haeberling | 4f91ab5 | 2013-05-21 11:26:13 -0700 | [diff] [blame] | 490 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 491 | mUI.animateFlash(); |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 492 | mUI.animateCapture(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
Doris Liu | 2a7f44c | 2013-08-12 15:18:53 -0700 | [diff] [blame] | 497 | public void onVideoSaved() { |
| 498 | if (mIsVideoCaptureIntent) { |
| 499 | showCaptureResult(); |
| 500 | } |
| 501 | } |
| 502 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 503 | public void onProtectiveCurtainClick(View v) { |
| 504 | // Consume clicks |
| 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public void onShutterButtonClick() { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 509 | if (mUI.collapseCameraControls() || mSwitchingCamera) return; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 510 | |
| 511 | boolean stop = mMediaRecorderRecording; |
| 512 | |
| 513 | if (stop) { |
| 514 | onStopVideoRecording(); |
| 515 | } else { |
| 516 | startVideoRecording(); |
| 517 | } |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 518 | mUI.enableShutter(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 519 | |
| 520 | // Keep the shutter button disabled when in video capture intent |
| 521 | // mode and recording is stopped. It'll be re-enabled when |
| 522 | // re-take button is clicked. |
| 523 | if (!(mIsVideoCaptureIntent && stop)) { |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 524 | mHandler.sendEmptyMessageDelayed(MSG_ENABLE_SHUTTER_BUTTON, SHUTTER_BUTTON_TIMEOUT); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | @Override |
| 529 | public void onShutterButtonFocus(boolean pressed) { |
Doris Liu | f55f3c4 | 2013-11-20 00:24:46 -0800 | [diff] [blame] | 530 | // TODO: Remove this when old camera controls are removed from the UI. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | private void readVideoPreferences() { |
| 534 | // The preference stores values from ListPreference and is thus string type for all values. |
| 535 | // We need to convert it to int manually. |
Doris Liu | 3f7e004 | 2013-07-31 11:25:09 -0700 | [diff] [blame] | 536 | String videoQuality = mPreferences.getString(CameraSettings.KEY_VIDEO_QUALITY, |
| 537 | null); |
| 538 | if (videoQuality == null) { |
| 539 | // check for highest quality before setting default value |
| 540 | videoQuality = CameraSettings.getSupportedHighestVideoQuality(mCameraId, |
| 541 | mActivity.getResources().getString(R.string.pref_video_quality_default)); |
| 542 | mPreferences.edit().putString(CameraSettings.KEY_VIDEO_QUALITY, videoQuality); |
| 543 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 544 | int quality = Integer.valueOf(videoQuality); |
| 545 | |
| 546 | // Set video quality. |
| 547 | Intent intent = mActivity.getIntent(); |
| 548 | if (intent.hasExtra(MediaStore.EXTRA_VIDEO_QUALITY)) { |
| 549 | int extraVideoQuality = |
| 550 | intent.getIntExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); |
| 551 | if (extraVideoQuality > 0) { |
| 552 | quality = CamcorderProfile.QUALITY_HIGH; |
| 553 | } else { // 0 is mms. |
| 554 | quality = CamcorderProfile.QUALITY_LOW; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | // Set video duration limit. The limit is read from the preference, |
| 559 | // unless it is specified in the intent. |
| 560 | if (intent.hasExtra(MediaStore.EXTRA_DURATION_LIMIT)) { |
| 561 | int seconds = |
| 562 | intent.getIntExtra(MediaStore.EXTRA_DURATION_LIMIT, 0); |
| 563 | mMaxVideoDurationInMs = 1000 * seconds; |
| 564 | } else { |
| 565 | mMaxVideoDurationInMs = CameraSettings.getMaxVideoDuration(mActivity); |
| 566 | } |
| 567 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 568 | // Read time lapse recording interval. |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 569 | String frameIntervalStr = mPreferences.getString( |
| 570 | CameraSettings.KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL, |
| 571 | mActivity.getString(R.string.pref_video_time_lapse_frame_interval_default)); |
| 572 | mTimeBetweenTimeLapseFrameCaptureMs = Integer.parseInt(frameIntervalStr); |
| 573 | mCaptureTimeLapse = (mTimeBetweenTimeLapseFrameCaptureMs != 0); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 574 | // TODO: This should be checked instead directly +1000. |
| 575 | if (mCaptureTimeLapse) quality += 1000; |
| 576 | mProfile = CamcorderProfile.get(mCameraId, quality); |
| 577 | getDesiredPreviewSize(); |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 578 | mPreferenceRead = true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 581 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 582 | private void getDesiredPreviewSize() { |
ztenghui | ef01a31 | 2013-10-14 15:25:16 -0700 | [diff] [blame] | 583 | if (mCameraDevice == null) { |
| 584 | return; |
| 585 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 586 | mParameters = mCameraDevice.getParameters(); |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 587 | if (mParameters.getSupportedVideoSizes() == null) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 588 | mDesiredPreviewWidth = mProfile.videoFrameWidth; |
| 589 | mDesiredPreviewHeight = mProfile.videoFrameHeight; |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 590 | } else { // Driver supports separates outputs for preview and video. |
| 591 | List<Size> sizes = mParameters.getSupportedPreviewSizes(); |
| 592 | Size preferred = mParameters.getPreferredPreviewSizeForVideo(); |
| 593 | int product = preferred.width * preferred.height; |
| 594 | Iterator<Size> it = sizes.iterator(); |
| 595 | // Remove the preview sizes that are not preferred. |
| 596 | while (it.hasNext()) { |
| 597 | Size size = it.next(); |
| 598 | if (size.width * size.height > product) { |
| 599 | it.remove(); |
| 600 | } |
| 601 | } |
| 602 | Size optimalSize = CameraUtil.getOptimalPreviewSize(mActivity, sizes, |
| 603 | (double) mProfile.videoFrameWidth / mProfile.videoFrameHeight); |
| 604 | mDesiredPreviewWidth = optimalSize.width; |
| 605 | mDesiredPreviewHeight = optimalSize.height; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 606 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 607 | mUI.setPreviewSize(mDesiredPreviewWidth, mDesiredPreviewHeight); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 608 | Log.v(TAG, "mDesiredPreviewWidth=" + mDesiredPreviewWidth + |
| 609 | ". mDesiredPreviewHeight=" + mDesiredPreviewHeight); |
| 610 | } |
| 611 | |
| 612 | private void resizeForPreviewAspectRatio() { |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 613 | mUI.setAspectRatio((double) mProfile.videoFrameWidth / mProfile.videoFrameHeight); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 616 | private void installIntentFilter() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 617 | // install an intent filter to receive SD card related events. |
| 618 | IntentFilter intentFilter = |
| 619 | new IntentFilter(Intent.ACTION_MEDIA_EJECT); |
| 620 | intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED); |
| 621 | intentFilter.addDataScheme("file"); |
| 622 | mReceiver = new MyBroadcastReceiver(); |
| 623 | mActivity.registerReceiver(mReceiver, intentFilter); |
| 624 | } |
| 625 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 626 | private void setDisplayOrientation() { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 627 | mDisplayRotation = CameraUtil.getDisplayRotation(mActivity); |
| 628 | mCameraDisplayOrientation = CameraUtil.getDisplayOrientation(mDisplayRotation, mCameraId); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 629 | // Change the camera display orientation |
| 630 | if (mCameraDevice != null) { |
| 631 | mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 632 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | @Override |
| 636 | public void updateCameraOrientation() { |
| 637 | if (mMediaRecorderRecording) return; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 638 | if (mDisplayRotation != CameraUtil.getDisplayRotation(mActivity)) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 639 | setDisplayOrientation(); |
| 640 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 643 | @Override |
| 644 | public int onZoomChanged(int index) { |
| 645 | // Not useful to change zoom value when the activity is paused. |
| 646 | if (mPaused) return index; |
| 647 | mZoomValue = index; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 648 | if (mParameters == null || mCameraDevice == null) return index; |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 649 | // Set zoom parameters asynchronously |
| 650 | mParameters.setZoom(mZoomValue); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 651 | mCameraDevice.setParameters(mParameters); |
| 652 | Parameters p = mCameraDevice.getParameters(); |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 653 | if (p != null) return p.getZoom(); |
| 654 | return index; |
| 655 | } |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 656 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 657 | private void startPreview() { |
| 658 | Log.v(TAG, "startPreview"); |
| 659 | |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 660 | SurfaceTexture surfaceTexture = mUI.getSurfaceTexture(); |
ztenghui | ef01a31 | 2013-10-14 15:25:16 -0700 | [diff] [blame] | 661 | if (!mPreferenceRead || surfaceTexture == null || mPaused == true || |
| 662 | mCameraDevice == null) { |
| 663 | return; |
| 664 | } |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 665 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 666 | mCameraDevice.setErrorCallback(mErrorCallback); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 667 | if (mPreviewing == true) { |
| 668 | stopPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 671 | setDisplayOrientation(); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 672 | mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 673 | setCameraParameters(); |
| 674 | |
| 675 | try { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 676 | mCameraDevice.setPreviewTexture(surfaceTexture); |
| 677 | mCameraDevice.startPreview(); |
| 678 | mPreviewing = true; |
| 679 | onPreviewStarted(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 680 | } catch (Throwable ex) { |
| 681 | closeCamera(); |
| 682 | throw new RuntimeException("startPreview failed", ex); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 683 | } |
Michael Kolb | b1aeb39 | 2013-03-11 12:37:40 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | private void onPreviewStarted() { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 687 | mUI.enableShutter(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 690 | @Override |
| 691 | public void stopPreview() { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 692 | if (!mPreviewing) return; |
| 693 | mCameraDevice.stopPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 694 | mPreviewing = false; |
| 695 | } |
| 696 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 697 | private void closeCamera() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 698 | Log.v(TAG, "closeCamera"); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 699 | if (mCameraDevice == null) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 700 | Log.d(TAG, "already stopped."); |
| 701 | return; |
| 702 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 703 | mCameraDevice.setZoomChangeListener(null); |
| 704 | mCameraDevice.setErrorCallback(null); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 705 | mActivity.getCameraProvider().releaseCamera(mCameraDevice.getCameraId()); |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 706 | mCameraDevice = null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 707 | mPreviewing = false; |
| 708 | mSnapshotInProgress = false; |
| 709 | } |
| 710 | |
| 711 | private void releasePreviewResources() { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 712 | if (!ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) { |
| 713 | mUI.hideSurfaceView(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 718 | public boolean onBackPressed() { |
| 719 | if (mPaused) return true; |
| 720 | if (mMediaRecorderRecording) { |
| 721 | onStopVideoRecording(); |
| 722 | return true; |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 723 | } else if (mUI.hidePieRenderer()) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 724 | return true; |
| 725 | } else { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 726 | return mUI.removeTopLevelPopup(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| 730 | @Override |
| 731 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 732 | // Do not handle any key if the activity is paused. |
| 733 | if (mPaused) { |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | switch (keyCode) { |
| 738 | case KeyEvent.KEYCODE_CAMERA: |
| 739 | if (event.getRepeatCount() == 0) { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 740 | mUI.clickShutter(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 741 | return true; |
| 742 | } |
| 743 | break; |
| 744 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 745 | if (event.getRepeatCount() == 0) { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 746 | mUI.clickShutter(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 747 | return true; |
| 748 | } |
| 749 | break; |
| 750 | case KeyEvent.KEYCODE_MENU: |
| 751 | if (mMediaRecorderRecording) return true; |
| 752 | break; |
| 753 | } |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | @Override |
| 758 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 759 | switch (keyCode) { |
| 760 | case KeyEvent.KEYCODE_CAMERA: |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 761 | mUI.pressShutter(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 762 | return true; |
| 763 | } |
| 764 | return false; |
| 765 | } |
| 766 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 767 | @Override |
| 768 | public boolean isVideoCaptureIntent() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 769 | String action = mActivity.getIntent().getAction(); |
| 770 | return (MediaStore.ACTION_VIDEO_CAPTURE.equals(action)); |
| 771 | } |
| 772 | |
| 773 | private void doReturnToCaller(boolean valid) { |
| 774 | Intent resultIntent = new Intent(); |
| 775 | int resultCode; |
| 776 | if (valid) { |
| 777 | resultCode = Activity.RESULT_OK; |
| 778 | resultIntent.setData(mCurrentVideoUri); |
| 779 | } else { |
| 780 | resultCode = Activity.RESULT_CANCELED; |
| 781 | } |
| 782 | mActivity.setResultEx(resultCode, resultIntent); |
| 783 | mActivity.finish(); |
| 784 | } |
| 785 | |
| 786 | private void cleanupEmptyFile() { |
| 787 | if (mVideoFilename != null) { |
| 788 | File f = new File(mVideoFilename); |
| 789 | if (f.length() == 0 && f.delete()) { |
| 790 | Log.v(TAG, "Empty video file deleted: " + mVideoFilename); |
| 791 | mVideoFilename = null; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | private void setupMediaRecorderPreviewDisplay() { |
| 797 | // Nothing to do here if using SurfaceTexture. |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 798 | if (!ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 799 | // We stop the preview here before unlocking the device because we |
| 800 | // need to change the SurfaceTexture to SurfaceView for preview. |
| 801 | stopPreview(); |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 802 | mCameraDevice.setPreviewDisplay(mUI.getSurfaceHolder()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 803 | // The orientation for SurfaceTexture is different from that for |
| 804 | // SurfaceView. For SurfaceTexture we don't need to consider the |
| 805 | // display rotation. Just consider the sensor's orientation and we |
| 806 | // will set the orientation correctly when showing the texture. |
| 807 | // Gallery will handle the orientation for the preview. For |
| 808 | // SurfaceView we will have to take everything into account so the |
| 809 | // display rotation is considered. |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 810 | mCameraDevice.setDisplayOrientation( |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 811 | CameraUtil.getDisplayOrientation(mDisplayRotation, mCameraId)); |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 812 | mCameraDevice.startPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 813 | mPreviewing = true; |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 814 | mMediaRecorder.setPreviewDisplay(mUI.getSurfaceHolder().getSurface()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | // Prepares media recorder. |
| 819 | private void initializeRecorder() { |
| 820 | Log.v(TAG, "initializeRecorder"); |
| 821 | // If the mCameraDevice is null, then this activity is going to finish |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 822 | if (mCameraDevice == null) return; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 823 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 824 | if (!ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 825 | // Set the SurfaceView to visible so the surface gets created. |
| 826 | // surfaceCreated() is called immediately when the visibility is |
| 827 | // changed to visible. Thus, mSurfaceViewReady should become true |
| 828 | // right after calling setVisibility(). |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 829 | mUI.showSurfaceView(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | Intent intent = mActivity.getIntent(); |
| 833 | Bundle myExtras = intent.getExtras(); |
| 834 | |
| 835 | long requestedSizeLimit = 0; |
| 836 | closeVideoFileDescriptor(); |
ztenghui | 70bd024 | 2013-10-14 11:15:44 -0700 | [diff] [blame] | 837 | mCurrentVideoUriFromMediaSaved = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 838 | if (mIsVideoCaptureIntent && myExtras != null) { |
| 839 | Uri saveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT); |
| 840 | if (saveUri != null) { |
| 841 | try { |
| 842 | mVideoFileDescriptor = |
| 843 | mContentResolver.openFileDescriptor(saveUri, "rw"); |
| 844 | mCurrentVideoUri = saveUri; |
| 845 | } catch (java.io.FileNotFoundException ex) { |
| 846 | // invalid uri |
| 847 | Log.e(TAG, ex.toString()); |
| 848 | } |
| 849 | } |
| 850 | requestedSizeLimit = myExtras.getLong(MediaStore.EXTRA_SIZE_LIMIT); |
| 851 | } |
| 852 | mMediaRecorder = new MediaRecorder(); |
| 853 | |
| 854 | setupMediaRecorderPreviewDisplay(); |
| 855 | // Unlock the camera object before passing it to media recorder. |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 856 | mCameraDevice.unlock(); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 857 | mMediaRecorder.setCamera(mCameraDevice.getCamera()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 858 | if (!mCaptureTimeLapse) { |
| 859 | mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); |
| 860 | } |
| 861 | mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); |
| 862 | mMediaRecorder.setProfile(mProfile); |
| 863 | mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs); |
| 864 | if (mCaptureTimeLapse) { |
| 865 | double fps = 1000 / (double) mTimeBetweenTimeLapseFrameCaptureMs; |
| 866 | setCaptureRate(mMediaRecorder, fps); |
| 867 | } |
| 868 | |
| 869 | setRecordLocation(); |
| 870 | |
| 871 | // Set output file. |
| 872 | // Try Uri in the intent first. If it doesn't exist, use our own |
| 873 | // instead. |
| 874 | if (mVideoFileDescriptor != null) { |
| 875 | mMediaRecorder.setOutputFile(mVideoFileDescriptor.getFileDescriptor()); |
| 876 | } else { |
| 877 | generateVideoFilename(mProfile.fileFormat); |
| 878 | mMediaRecorder.setOutputFile(mVideoFilename); |
| 879 | } |
| 880 | |
| 881 | // Set maximum file size. |
Angus Kong | 2dcc0a9 | 2013-09-25 13:00:08 -0700 | [diff] [blame] | 882 | long maxFileSize = mActivity.getStorageSpaceBytes() - Storage.LOW_STORAGE_THRESHOLD_BYTES; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 883 | if (requestedSizeLimit > 0 && requestedSizeLimit < maxFileSize) { |
| 884 | maxFileSize = requestedSizeLimit; |
| 885 | } |
| 886 | |
| 887 | try { |
| 888 | mMediaRecorder.setMaxFileSize(maxFileSize); |
| 889 | } catch (RuntimeException exception) { |
| 890 | // We are going to ignore failure of setMaxFileSize here, as |
| 891 | // a) The composer selected may simply not support it, or |
| 892 | // b) The underlying media framework may not handle 64-bit range |
| 893 | // on the size restriction. |
| 894 | } |
| 895 | |
| 896 | // See android.hardware.Camera.Parameters.setRotation for |
| 897 | // documentation. |
| 898 | // Note that mOrientation here is the device orientation, which is the opposite of |
| 899 | // what activity.getWindowManager().getDefaultDisplay().getRotation() would return, |
| 900 | // which is the orientation the graphics need to rotate in order to render correctly. |
| 901 | int rotation = 0; |
| 902 | if (mOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) { |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 903 | CameraInfo info = mActivity.getCameraProvider().getCameraInfo()[mCameraId]; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 904 | if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { |
| 905 | rotation = (info.orientation - mOrientation + 360) % 360; |
| 906 | } else { // back-facing camera |
| 907 | rotation = (info.orientation + mOrientation) % 360; |
| 908 | } |
| 909 | } |
| 910 | mMediaRecorder.setOrientationHint(rotation); |
| 911 | |
| 912 | try { |
| 913 | mMediaRecorder.prepare(); |
| 914 | } catch (IOException e) { |
| 915 | Log.e(TAG, "prepare failed for " + mVideoFilename, e); |
| 916 | releaseMediaRecorder(); |
| 917 | throw new RuntimeException(e); |
| 918 | } |
| 919 | |
| 920 | mMediaRecorder.setOnErrorListener(this); |
| 921 | mMediaRecorder.setOnInfoListener(this); |
| 922 | } |
| 923 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 924 | private static void setCaptureRate(MediaRecorder recorder, double fps) { |
| 925 | recorder.setCaptureRate(fps); |
| 926 | } |
| 927 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 928 | private void setRecordLocation() { |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 929 | Location loc = mLocationManager.getCurrentLocation(); |
| 930 | if (loc != null) { |
| 931 | mMediaRecorder.setLocation((float) loc.getLatitude(), |
| 932 | (float) loc.getLongitude()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 936 | private void releaseMediaRecorder() { |
| 937 | Log.v(TAG, "Releasing media recorder."); |
| 938 | if (mMediaRecorder != null) { |
| 939 | cleanupEmptyFile(); |
| 940 | mMediaRecorder.reset(); |
| 941 | mMediaRecorder.release(); |
| 942 | mMediaRecorder = null; |
| 943 | } |
| 944 | mVideoFilename = null; |
| 945 | } |
| 946 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 947 | private void generateVideoFilename(int outputFileFormat) { |
| 948 | long dateTaken = System.currentTimeMillis(); |
| 949 | String title = createName(dateTaken); |
| 950 | // Used when emailing. |
| 951 | String filename = title + convertOutputFormatToFileExt(outputFileFormat); |
| 952 | String mime = convertOutputFormatToMimeType(outputFileFormat); |
| 953 | String path = Storage.DIRECTORY + '/' + filename; |
| 954 | String tmpPath = path + ".tmp"; |
Ruben Brunk | 1600796 | 2013-04-19 15:27:57 -0700 | [diff] [blame] | 955 | mCurrentVideoValues = new ContentValues(9); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 956 | mCurrentVideoValues.put(Video.Media.TITLE, title); |
| 957 | mCurrentVideoValues.put(Video.Media.DISPLAY_NAME, filename); |
| 958 | mCurrentVideoValues.put(Video.Media.DATE_TAKEN, dateTaken); |
Ruben Brunk | 1600796 | 2013-04-19 15:27:57 -0700 | [diff] [blame] | 959 | mCurrentVideoValues.put(MediaColumns.DATE_MODIFIED, dateTaken / 1000); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 960 | mCurrentVideoValues.put(Video.Media.MIME_TYPE, mime); |
| 961 | mCurrentVideoValues.put(Video.Media.DATA, path); |
| 962 | mCurrentVideoValues.put(Video.Media.RESOLUTION, |
| 963 | Integer.toString(mProfile.videoFrameWidth) + "x" + |
| 964 | Integer.toString(mProfile.videoFrameHeight)); |
| 965 | Location loc = mLocationManager.getCurrentLocation(); |
| 966 | if (loc != null) { |
| 967 | mCurrentVideoValues.put(Video.Media.LATITUDE, loc.getLatitude()); |
| 968 | mCurrentVideoValues.put(Video.Media.LONGITUDE, loc.getLongitude()); |
| 969 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 970 | mVideoFilename = tmpPath; |
| 971 | Log.v(TAG, "New video filename: " + mVideoFilename); |
| 972 | } |
| 973 | |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 974 | private void saveVideo() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 975 | if (mVideoFileDescriptor == null) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 976 | long duration = SystemClock.uptimeMillis() - mRecordingStartTime; |
| 977 | if (duration > 0) { |
| 978 | if (mCaptureTimeLapse) { |
| 979 | duration = getTimeLapseVideoLength(duration); |
| 980 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 981 | } else { |
| 982 | Log.w(TAG, "Video duration <= 0 : " + duration); |
| 983 | } |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 984 | getServices().getMediaSaver().addVideo(mCurrentVideoFilename, |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 985 | duration, mCurrentVideoValues, |
| 986 | mOnVideoSavedListener, mContentResolver); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 987 | } |
| 988 | mCurrentVideoValues = null; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | private void deleteVideoFile(String fileName) { |
| 992 | Log.v(TAG, "Deleting video " + fileName); |
| 993 | File f = new File(fileName); |
| 994 | if (!f.delete()) { |
| 995 | Log.v(TAG, "Could not delete " + fileName); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | private PreferenceGroup filterPreferenceScreenByIntent( |
| 1000 | PreferenceGroup screen) { |
| 1001 | Intent intent = mActivity.getIntent(); |
| 1002 | if (intent.hasExtra(MediaStore.EXTRA_VIDEO_QUALITY)) { |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1003 | CameraSettings.removePreferenceFromScreen(screen, CameraSettings.KEY_VIDEO_QUALITY); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | if (intent.hasExtra(MediaStore.EXTRA_DURATION_LIMIT)) { |
| 1007 | CameraSettings.removePreferenceFromScreen(screen, |
| 1008 | CameraSettings.KEY_VIDEO_QUALITY); |
| 1009 | } |
| 1010 | return screen; |
| 1011 | } |
| 1012 | |
| 1013 | // from MediaRecorder.OnErrorListener |
| 1014 | @Override |
| 1015 | public void onError(MediaRecorder mr, int what, int extra) { |
| 1016 | Log.e(TAG, "MediaRecorder error. what=" + what + ". extra=" + extra); |
| 1017 | if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { |
| 1018 | // We may have run out of space on the sdcard. |
| 1019 | stopVideoRecording(); |
| 1020 | mActivity.updateStorageSpaceAndHint(); |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | // from MediaRecorder.OnInfoListener |
| 1025 | @Override |
| 1026 | public void onInfo(MediaRecorder mr, int what, int extra) { |
| 1027 | if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) { |
| 1028 | if (mMediaRecorderRecording) onStopVideoRecording(); |
| 1029 | } else if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) { |
| 1030 | if (mMediaRecorderRecording) onStopVideoRecording(); |
| 1031 | |
| 1032 | // Show the toast. |
| 1033 | Toast.makeText(mActivity, R.string.video_reach_size_limit, |
| 1034 | Toast.LENGTH_LONG).show(); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * Make sure we're not recording music playing in the background, ask the |
| 1040 | * MediaPlaybackService to pause playback. |
| 1041 | */ |
| 1042 | private void pauseAudioPlayback() { |
Marco Nelissen | 20694b2 | 2013-10-29 15:27:24 -0700 | [diff] [blame] | 1043 | AudioManager am = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE); |
| 1044 | am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | // For testing. |
| 1048 | public boolean isRecording() { |
| 1049 | return mMediaRecorderRecording; |
| 1050 | } |
| 1051 | |
| 1052 | private void startVideoRecording() { |
| 1053 | Log.v(TAG, "startVideoRecording"); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1054 | mUI.cancelAnimations(); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1055 | mUI.setSwipingEnabled(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1056 | |
| 1057 | mActivity.updateStorageSpaceAndHint(); |
Angus Kong | 2dcc0a9 | 2013-09-25 13:00:08 -0700 | [diff] [blame] | 1058 | if (mActivity.getStorageSpaceBytes() <= Storage.LOW_STORAGE_THRESHOLD_BYTES) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1059 | Log.v(TAG, "Storage issue, ignore the start request"); |
| 1060 | return; |
| 1061 | } |
| 1062 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1063 | //?? |
| 1064 | //if (!mCameraDevice.waitDone()) return; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1065 | mCurrentVideoUri = null; |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1066 | |
| 1067 | initializeRecorder(); |
| 1068 | if (mMediaRecorder == null) { |
| 1069 | Log.e(TAG, "Fail to initialize media recorder"); |
| 1070 | return; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | pauseAudioPlayback(); |
| 1074 | |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1075 | try { |
| 1076 | mMediaRecorder.start(); // Recording is now started |
| 1077 | } catch (RuntimeException e) { |
| 1078 | Log.e(TAG, "Could not start media recorder. ", e); |
| 1079 | releaseMediaRecorder(); |
| 1080 | // If start fails, frameworks will not lock the camera for us. |
| 1081 | mCameraDevice.lock(); |
| 1082 | return; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | // Make sure the video recording has started before announcing |
| 1086 | // this in accessibility. |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1087 | AccessibilityUtils.makeAnnouncement(mUI.getShutterButton(), |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1088 | mActivity.getString(R.string.video_recording_started)); |
| 1089 | |
Angus Kong | 104e001 | 2013-04-03 16:56:18 -0700 | [diff] [blame] | 1090 | // The parameters might have been altered by MediaRecorder already. |
| 1091 | // We need to force mCameraDevice to refresh before getting it. |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1092 | mCameraDevice.refreshParameters(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1093 | // The parameters may have been changed by MediaRecorder upon starting |
| 1094 | // recording. We need to alter the parameters if we support camcorder |
| 1095 | // zoom. To reduce latency when setting the parameters during zoom, we |
| 1096 | // update mParameters here once. |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 1097 | mParameters = mCameraDevice.getParameters(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1098 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1099 | mUI.enableCameraControls(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1100 | |
| 1101 | mMediaRecorderRecording = true; |
Angus Kong | 9f1db52 | 2013-11-09 16:25:59 -0800 | [diff] [blame] | 1102 | mActivity.lockOrientation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1103 | mRecordingStartTime = SystemClock.uptimeMillis(); |
Doris Liu | fe6596c | 2013-10-08 11:03:37 -0700 | [diff] [blame] | 1104 | mUI.showRecordingUI(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1105 | |
| 1106 | updateRecordingTime(); |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 1107 | mActivity.enableKeepScreenOn(true); |
Bobby Georgescu | 301b646 | 2013-04-01 15:33:17 -0700 | [diff] [blame] | 1108 | UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA, |
| 1109 | UsageStatistics.ACTION_CAPTURE_START, "Video"); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1112 | private Bitmap getVideoThumbnail() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1113 | Bitmap bitmap = null; |
| 1114 | if (mVideoFileDescriptor != null) { |
| 1115 | bitmap = Thumbnail.createVideoThumbnailBitmap(mVideoFileDescriptor.getFileDescriptor(), |
Doris Liu | 3c2fca3 | 2013-02-13 18:28:03 -0800 | [diff] [blame] | 1116 | mDesiredPreviewWidth); |
Doris Liu | 2a7f44c | 2013-08-12 15:18:53 -0700 | [diff] [blame] | 1117 | } else if (mCurrentVideoUri != null) { |
| 1118 | try { |
| 1119 | mVideoFileDescriptor = mContentResolver.openFileDescriptor(mCurrentVideoUri, "r"); |
| 1120 | bitmap = Thumbnail.createVideoThumbnailBitmap( |
| 1121 | mVideoFileDescriptor.getFileDescriptor(), mDesiredPreviewWidth); |
| 1122 | } catch (java.io.FileNotFoundException ex) { |
| 1123 | // invalid uri |
| 1124 | Log.e(TAG, ex.toString()); |
| 1125 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1126 | } |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1127 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1128 | if (bitmap != null) { |
| 1129 | // MetadataRetriever already rotates the thumbnail. We should rotate |
| 1130 | // it to match the UI orientation (and mirror if it is front-facing camera). |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1131 | CameraInfo[] info = mActivity.getCameraProvider().getCameraInfo(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1132 | boolean mirror = (info[mCameraId].facing == CameraInfo.CAMERA_FACING_FRONT); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1133 | bitmap = CameraUtil.rotateAndMirror(bitmap, 0, mirror); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1134 | } |
| 1135 | return bitmap; |
| 1136 | } |
| 1137 | |
| 1138 | private void showCaptureResult() { |
| 1139 | mIsInReviewMode = true; |
| 1140 | Bitmap bitmap = getVideoThumbnail(); |
| 1141 | if (bitmap != null) { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1142 | mUI.showReviewImage(bitmap); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1143 | } |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1144 | mUI.showReviewControls(); |
| 1145 | mUI.enableCameraControls(false); |
| 1146 | mUI.showTimeLapseUI(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1149 | private boolean stopVideoRecording() { |
| 1150 | Log.v(TAG, "stopVideoRecording"); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1151 | mUI.setSwipingEnabled(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1152 | |
| 1153 | boolean fail = false; |
| 1154 | if (mMediaRecorderRecording) { |
| 1155 | boolean shouldAddToMediaStoreNow = false; |
| 1156 | |
| 1157 | try { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1158 | mMediaRecorder.setOnErrorListener(null); |
| 1159 | mMediaRecorder.setOnInfoListener(null); |
| 1160 | mMediaRecorder.stop(); |
| 1161 | shouldAddToMediaStoreNow = true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1162 | mCurrentVideoFilename = mVideoFilename; |
| 1163 | Log.v(TAG, "stopVideoRecording: Setting current video filename: " |
| 1164 | + mCurrentVideoFilename); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1165 | AccessibilityUtils.makeAnnouncement(mUI.getShutterButton(), |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 1166 | mActivity.getAndroidContext().getString(R.string |
| 1167 | .video_recording_stopped)); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1168 | } catch (RuntimeException e) { |
| 1169 | Log.e(TAG, "stop fail", e); |
| 1170 | if (mVideoFilename != null) deleteVideoFile(mVideoFilename); |
| 1171 | fail = true; |
| 1172 | } |
| 1173 | mMediaRecorderRecording = false; |
Angus Kong | 9f1db52 | 2013-11-09 16:25:59 -0800 | [diff] [blame] | 1174 | mActivity.unlockOrientation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1175 | |
| 1176 | // If the activity is paused, this means activity is interrupted |
| 1177 | // during recording. Release the camera as soon as possible because |
| 1178 | // face unlock or other applications may need to use the camera. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1179 | if (mPaused) { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1180 | closeCamera(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Doris Liu | fe6596c | 2013-10-08 11:03:37 -0700 | [diff] [blame] | 1183 | mUI.showRecordingUI(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1184 | if (!mIsVideoCaptureIntent) { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1185 | mUI.enableCameraControls(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1186 | } |
| 1187 | // The orientation was fixed during video recording. Now make it |
| 1188 | // reflect the device orientation as video recording is stopped. |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1189 | mUI.setOrientationIndicator(0, true); |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 1190 | mActivity.enableKeepScreenOn(false); |
Doris Liu | 2a7f44c | 2013-08-12 15:18:53 -0700 | [diff] [blame] | 1191 | if (shouldAddToMediaStoreNow && !fail) { |
| 1192 | if (mVideoFileDescriptor == null) { |
| 1193 | saveVideo(); |
| 1194 | } else if (mIsVideoCaptureIntent) { |
| 1195 | // if no file save is needed, we can show the post capture UI now |
| 1196 | showCaptureResult(); |
| 1197 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1198 | } |
| 1199 | } |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1200 | // release media recorder |
| 1201 | releaseMediaRecorder(); |
| 1202 | if (!mPaused) { |
| 1203 | mCameraDevice.lock(); |
| 1204 | if (!ApiHelper.HAS_SURFACE_TEXTURE_RECORDING) { |
| 1205 | stopPreview(); |
| 1206 | mUI.hideSurfaceView(); |
| 1207 | // Switch back to use SurfaceTexture for preview. |
| 1208 | startPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | // Update the parameters here because the parameters might have been altered |
| 1212 | // by MediaRecorder. |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1213 | if (!mPaused) mParameters = mCameraDevice.getParameters(); |
Bobby Georgescu | 301b646 | 2013-04-01 15:33:17 -0700 | [diff] [blame] | 1214 | UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA, |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 1215 | fail ? UsageStatistics.ACTION_CAPTURE_FAIL : UsageStatistics.ACTION_CAPTURE_DONE, |
| 1216 | "Video", SystemClock.uptimeMillis() - mRecordingStartTime); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1217 | return fail; |
| 1218 | } |
| 1219 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1220 | private static String millisecondToTimeString(long milliSeconds, boolean displayCentiSeconds) { |
| 1221 | long seconds = milliSeconds / 1000; // round down to compute seconds |
| 1222 | long minutes = seconds / 60; |
| 1223 | long hours = minutes / 60; |
| 1224 | long remainderMinutes = minutes - (hours * 60); |
| 1225 | long remainderSeconds = seconds - (minutes * 60); |
| 1226 | |
| 1227 | StringBuilder timeStringBuilder = new StringBuilder(); |
| 1228 | |
| 1229 | // Hours |
| 1230 | if (hours > 0) { |
| 1231 | if (hours < 10) { |
| 1232 | timeStringBuilder.append('0'); |
| 1233 | } |
| 1234 | timeStringBuilder.append(hours); |
| 1235 | |
| 1236 | timeStringBuilder.append(':'); |
| 1237 | } |
| 1238 | |
| 1239 | // Minutes |
| 1240 | if (remainderMinutes < 10) { |
| 1241 | timeStringBuilder.append('0'); |
| 1242 | } |
| 1243 | timeStringBuilder.append(remainderMinutes); |
| 1244 | timeStringBuilder.append(':'); |
| 1245 | |
| 1246 | // Seconds |
| 1247 | if (remainderSeconds < 10) { |
| 1248 | timeStringBuilder.append('0'); |
| 1249 | } |
| 1250 | timeStringBuilder.append(remainderSeconds); |
| 1251 | |
| 1252 | // Centi seconds |
| 1253 | if (displayCentiSeconds) { |
| 1254 | timeStringBuilder.append('.'); |
| 1255 | long remainderCentiSeconds = (milliSeconds - seconds * 1000) / 10; |
| 1256 | if (remainderCentiSeconds < 10) { |
| 1257 | timeStringBuilder.append('0'); |
| 1258 | } |
| 1259 | timeStringBuilder.append(remainderCentiSeconds); |
| 1260 | } |
| 1261 | |
| 1262 | return timeStringBuilder.toString(); |
| 1263 | } |
| 1264 | |
| 1265 | private long getTimeLapseVideoLength(long deltaMs) { |
| 1266 | // For better approximation calculate fractional number of frames captured. |
| 1267 | // This will update the video time at a higher resolution. |
| 1268 | double numberOfFrames = (double) deltaMs / mTimeBetweenTimeLapseFrameCaptureMs; |
| 1269 | return (long) (numberOfFrames / mProfile.videoFrameRate * 1000); |
| 1270 | } |
| 1271 | |
| 1272 | private void updateRecordingTime() { |
| 1273 | if (!mMediaRecorderRecording) { |
| 1274 | return; |
| 1275 | } |
| 1276 | long now = SystemClock.uptimeMillis(); |
| 1277 | long delta = now - mRecordingStartTime; |
| 1278 | |
| 1279 | // Starting a minute before reaching the max duration |
| 1280 | // limit, we'll countdown the remaining time instead. |
| 1281 | boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0 |
| 1282 | && delta >= mMaxVideoDurationInMs - 60000); |
| 1283 | |
| 1284 | long deltaAdjusted = delta; |
| 1285 | if (countdownRemainingTime) { |
| 1286 | deltaAdjusted = Math.max(0, mMaxVideoDurationInMs - deltaAdjusted) + 999; |
| 1287 | } |
| 1288 | String text; |
| 1289 | |
| 1290 | long targetNextUpdateDelay; |
| 1291 | if (!mCaptureTimeLapse) { |
| 1292 | text = millisecondToTimeString(deltaAdjusted, false); |
| 1293 | targetNextUpdateDelay = 1000; |
| 1294 | } else { |
| 1295 | // The length of time lapse video is different from the length |
| 1296 | // of the actual wall clock time elapsed. Display the video length |
| 1297 | // only in format hh:mm:ss.dd, where dd are the centi seconds. |
| 1298 | text = millisecondToTimeString(getTimeLapseVideoLength(delta), true); |
| 1299 | targetNextUpdateDelay = mTimeBetweenTimeLapseFrameCaptureMs; |
| 1300 | } |
| 1301 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1302 | mUI.setRecordingTime(text); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1303 | |
| 1304 | if (mRecordingTimeCountsDown != countdownRemainingTime) { |
| 1305 | // Avoid setting the color on every update, do it only |
| 1306 | // when it needs changing. |
| 1307 | mRecordingTimeCountsDown = countdownRemainingTime; |
| 1308 | |
| 1309 | int color = mActivity.getResources().getColor(countdownRemainingTime |
| 1310 | ? R.color.recording_time_remaining_text |
| 1311 | : R.color.recording_time_elapsed_text); |
| 1312 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1313 | mUI.setRecordingTimeTextColor(color); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | long actualNextUpdateDelay = targetNextUpdateDelay - (delta % targetNextUpdateDelay); |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 1317 | mHandler.sendEmptyMessageDelayed(MSG_UPDATE_RECORD_TIME, actualNextUpdateDelay); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | private static boolean isSupported(String value, List<String> supported) { |
| 1321 | return supported == null ? false : supported.indexOf(value) >= 0; |
| 1322 | } |
| 1323 | |
| 1324 | @SuppressWarnings("deprecation") |
| 1325 | private void setCameraParameters() { |
| 1326 | mParameters.setPreviewSize(mDesiredPreviewWidth, mDesiredPreviewHeight); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1327 | int[] fpsRange = CameraUtil.getMaxPreviewFpsRange(mParameters); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1328 | if (fpsRange.length > 0) { |
| 1329 | mParameters.setPreviewFpsRange( |
| 1330 | fpsRange[Parameters.PREVIEW_FPS_MIN_INDEX], |
| 1331 | fpsRange[Parameters.PREVIEW_FPS_MAX_INDEX]); |
| 1332 | } else { |
| 1333 | mParameters.setPreviewFrameRate(mProfile.videoFrameRate); |
| 1334 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1335 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1336 | forceFlashOffIfSupported(!mUI.isVisible()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1337 | |
| 1338 | // Set white balance parameter. |
| 1339 | String whiteBalance = mPreferences.getString( |
| 1340 | CameraSettings.KEY_WHITE_BALANCE, |
| 1341 | mActivity.getString(R.string.pref_camera_whitebalance_default)); |
| 1342 | if (isSupported(whiteBalance, |
| 1343 | mParameters.getSupportedWhiteBalance())) { |
| 1344 | mParameters.setWhiteBalance(whiteBalance); |
| 1345 | } else { |
| 1346 | whiteBalance = mParameters.getWhiteBalance(); |
| 1347 | if (whiteBalance == null) { |
| 1348 | whiteBalance = Parameters.WHITE_BALANCE_AUTO; |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | // Set zoom. |
| 1353 | if (mParameters.isZoomSupported()) { |
| 1354 | mParameters.setZoom(mZoomValue); |
| 1355 | } |
| 1356 | |
| 1357 | // Set continuous autofocus. |
| 1358 | List<String> supportedFocus = mParameters.getSupportedFocusModes(); |
| 1359 | if (isSupported(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO, supportedFocus)) { |
| 1360 | mParameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); |
| 1361 | } |
| 1362 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1363 | mParameters.set(CameraUtil.RECORDING_HINT, CameraUtil.TRUE); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1364 | |
| 1365 | // Enable video stabilization. Convenience methods not available in API |
| 1366 | // level <= 14 |
| 1367 | String vstabSupported = mParameters.get("video-stabilization-supported"); |
| 1368 | if ("true".equals(vstabSupported)) { |
| 1369 | mParameters.set("video-stabilization", "true"); |
| 1370 | } |
| 1371 | |
| 1372 | // Set picture size. |
| 1373 | // The logic here is different from the logic in still-mode camera. |
| 1374 | // There we determine the preview size based on the picture size, but |
| 1375 | // here we determine the picture size based on the preview size. |
| 1376 | List<Size> supported = mParameters.getSupportedPictureSizes(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1377 | Size optimalSize = CameraUtil.getOptimalVideoSnapshotPictureSize(supported, |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1378 | (double) mDesiredPreviewWidth / mDesiredPreviewHeight); |
| 1379 | Size original = mParameters.getPictureSize(); |
| 1380 | if (!original.equals(optimalSize)) { |
| 1381 | mParameters.setPictureSize(optimalSize.width, optimalSize.height); |
| 1382 | } |
| 1383 | Log.v(TAG, "Video snapshot size is " + optimalSize.width + "x" + |
| 1384 | optimalSize.height); |
| 1385 | |
| 1386 | // Set JPEG quality. |
| 1387 | int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId, |
| 1388 | CameraProfile.QUALITY_HIGH); |
| 1389 | mParameters.setJpegQuality(jpegQuality); |
| 1390 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1391 | mCameraDevice.setParameters(mParameters); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1392 | // Keep preview size up to date. |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1393 | mParameters = mCameraDevice.getParameters(); |
Sascha Haeberling | f8b877c | 2013-09-09 17:32:48 -0700 | [diff] [blame] | 1394 | |
| 1395 | // Update UI based on the new parameters. |
| 1396 | mUI.updateOnScreenIndicators(mParameters, mPreferences); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | @Override |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1400 | public void resume() { |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1401 | mPaused = false; |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 1402 | installIntentFilter(); |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1403 | mUI.enableShutter(false); |
| 1404 | mZoomValue = 0; |
| 1405 | |
| 1406 | showVideoSnapshotUI(false); |
| 1407 | |
| 1408 | if (!mPreviewing) { |
| 1409 | requestCamera(mCameraId); |
| 1410 | } else { |
| 1411 | // preview already started |
| 1412 | mUI.enableShutter(true); |
| 1413 | } |
| 1414 | |
| 1415 | mUI.initDisplayChangeListener(); |
| 1416 | |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1417 | // Initialize location service. |
| 1418 | boolean recordLocation = RecordLocationPreference.get(mPreferences, |
| 1419 | mContentResolver); |
| 1420 | mLocationManager.recordLocation(recordLocation); |
| 1421 | |
| 1422 | if (mPreviewing) { |
| 1423 | mOnResumeTime = SystemClock.uptimeMillis(); |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 1424 | mHandler.sendEmptyMessageDelayed(MSG_CHECK_DISPLAY_ROTATION, 100); |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | UsageStatistics.onContentViewChanged( |
| 1428 | UsageStatistics.COMPONENT_CAMERA, "VideoModule"); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | @Override |
| 1432 | public void pause() { |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1433 | mPaused = true; |
| 1434 | |
| 1435 | mUI.showPreviewCover(); |
| 1436 | if (mMediaRecorderRecording) { |
| 1437 | // Camera will be released in onStopVideoRecording. |
| 1438 | onStopVideoRecording(); |
| 1439 | } else { |
| 1440 | stopPreview(); |
| 1441 | closeCamera(); |
| 1442 | releaseMediaRecorder(); |
| 1443 | } |
| 1444 | |
| 1445 | closeVideoFileDescriptor(); |
| 1446 | |
| 1447 | |
| 1448 | releasePreviewResources(); |
| 1449 | |
| 1450 | if (mReceiver != null) { |
| 1451 | mActivity.unregisterReceiver(mReceiver); |
| 1452 | mReceiver = null; |
| 1453 | } |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1454 | |
| 1455 | if (mLocationManager != null) mLocationManager.recordLocation(false); |
| 1456 | |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 1457 | mHandler.removeMessages(MSG_CHECK_DISPLAY_ROTATION); |
| 1458 | mHandler.removeMessages(MSG_SWITCH_CAMERA); |
| 1459 | mHandler.removeMessages(MSG_SWITCH_CAMERA_START_ANIMATION); |
Angus Kong | c4e6656 | 2013-11-22 23:03:21 -0800 | [diff] [blame] | 1460 | mPendingSwitchCameraId = -1; |
| 1461 | mSwitchingCamera = false; |
| 1462 | mPreferenceRead = false; |
| 1463 | |
| 1464 | mUI.collapseCameraControls(); |
| 1465 | mUI.removeDisplayChangeListener(); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | @Override |
| 1469 | public void destroy() { |
| 1470 | |
| 1471 | } |
| 1472 | |
| 1473 | @Override |
| 1474 | public void onPreviewSizeChanged(int width, int height) { |
| 1475 | // TODO: implement this |
| 1476 | } |
| 1477 | |
| 1478 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1479 | public void onConfigurationChanged(Configuration newConfig) { |
Doris Liu | 6a0de79 | 2013-02-26 10:54:25 -0800 | [diff] [blame] | 1480 | Log.v(TAG, "onConfigurationChanged"); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1481 | setDisplayOrientation(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | @Override |
| 1485 | public void onOverriddenPreferencesClicked() { |
| 1486 | } |
| 1487 | |
| 1488 | @Override |
| 1489 | // TODO: Delete this after old camera code is removed |
| 1490 | public void onRestorePreferencesClicked() { |
| 1491 | } |
| 1492 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1493 | @Override |
| 1494 | public void onSharedPreferenceChanged() { |
| 1495 | // ignore the events after "onPause()" or preview has not started yet |
Sascha Haeberling | f8b877c | 2013-09-09 17:32:48 -0700 | [diff] [blame] | 1496 | if (mPaused) { |
| 1497 | return; |
| 1498 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1499 | synchronized (mPreferences) { |
| 1500 | // If mCameraDevice is not ready then we can set the parameter in |
| 1501 | // startPreview(). |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1502 | if (mCameraDevice == null) return; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1503 | |
| 1504 | boolean recordLocation = RecordLocationPreference.get( |
| 1505 | mPreferences, mContentResolver); |
| 1506 | mLocationManager.recordLocation(recordLocation); |
| 1507 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1508 | readVideoPreferences(); |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1509 | mUI.showTimeLapseUI(mCaptureTimeLapse); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1510 | // We need to restart the preview if preview size is changed. |
| 1511 | Size size = mParameters.getPreviewSize(); |
| 1512 | if (size.width != mDesiredPreviewWidth |
| 1513 | || size.height != mDesiredPreviewHeight) { |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1514 | |
| 1515 | stopPreview(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1516 | resizeForPreviewAspectRatio(); |
| 1517 | startPreview(); // Parameters will be set in startPreview(). |
| 1518 | } else { |
| 1519 | setCameraParameters(); |
| 1520 | } |
Michael Kolb | 8788079 | 2013-04-30 15:38:49 -0700 | [diff] [blame] | 1521 | mUI.updateOnScreenIndicators(mParameters, mPreferences); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1525 | protected void setCameraId(int cameraId) { |
| 1526 | ListPreference pref = mPreferenceGroup.findPreference(CameraSettings.KEY_CAMERA_ID); |
| 1527 | pref.setValue("" + cameraId); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | private void switchCamera() { |
Sascha Haeberling | f8b877c | 2013-09-09 17:32:48 -0700 | [diff] [blame] | 1531 | if (mPaused) { |
| 1532 | return; |
| 1533 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1534 | |
| 1535 | Log.d(TAG, "Start to switch camera."); |
| 1536 | mCameraId = mPendingSwitchCameraId; |
| 1537 | mPendingSwitchCameraId = -1; |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1538 | setCameraId(mCameraId); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1539 | |
| 1540 | closeCamera(); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1541 | requestCamera(mCameraId); |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1542 | mUI.collapseCameraControls(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1543 | // Restart the camera and initialize the UI. From onCreate. |
| 1544 | mPreferences.setLocalId(mActivity, mCameraId); |
| 1545 | CameraSettings.upgradeLocalPreferences(mPreferences.getLocal()); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1546 | |
| 1547 | // From onResume |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1548 | mZoomValue = 0; |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1549 | mUI.setOrientationIndicator(0, false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1550 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1551 | // Start switch camera animation. Post a message because |
| 1552 | // onFrameAvailable from the old camera may already exist. |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 1553 | mHandler.sendEmptyMessage(MSG_SWITCH_CAMERA_START_ANIMATION); |
Michael Kolb | 8788079 | 2013-04-30 15:38:49 -0700 | [diff] [blame] | 1554 | mUI.updateOnScreenIndicators(mParameters, mPreferences); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1555 | } |
| 1556 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1557 | private void initializeVideoSnapshot() { |
Doris Liu | 537718c | 2013-02-20 16:29:44 -0800 | [diff] [blame] | 1558 | if (mParameters == null) return; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1559 | if (CameraUtil.isVideoSnapshotSupported(mParameters) && !mIsVideoCaptureIntent) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1560 | // Show the tap to focus toast if this is the first start. |
| 1561 | if (mPreferences.getBoolean( |
| 1562 | CameraSettings.KEY_VIDEO_FIRST_USE_HINT_SHOWN, true)) { |
| 1563 | // Delay the toast for one second to wait for orientation. |
Angus Kong | 13e87c4 | 2013-11-25 10:02:47 -0800 | [diff] [blame^] | 1564 | mHandler.sendEmptyMessageDelayed(MSG_SHOW_TAP_TO_SNAPSHOT_TOAST, 1000); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1565 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | void showVideoSnapshotUI(boolean enabled) { |
Doris Liu | 537718c | 2013-02-20 16:29:44 -0800 | [diff] [blame] | 1570 | if (mParameters == null) return; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1571 | if (CameraUtil.isVideoSnapshotSupported(mParameters) && !mIsVideoCaptureIntent) { |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1572 | if (enabled) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 1573 | mUI.animateFlash(); |
Doris Liu | 3973deb | 2013-08-21 13:42:22 -0700 | [diff] [blame] | 1574 | mUI.animateCapture(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1575 | } else { |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1576 | mUI.showPreviewBorder(enabled); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1577 | } |
Doris Liu | 6827ce2 | 2013-03-12 19:24:28 -0700 | [diff] [blame] | 1578 | mUI.enableShutter(!enabled); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1582 | private void forceFlashOffIfSupported(boolean forceOff) { |
| 1583 | String flashMode; |
| 1584 | if (!forceOff) { |
| 1585 | flashMode = mPreferences.getString( |
| 1586 | CameraSettings.KEY_VIDEOCAMERA_FLASH_MODE, |
| 1587 | mActivity.getString(R.string.pref_camera_video_flashmode_default)); |
| 1588 | } else { |
| 1589 | flashMode = Parameters.FLASH_MODE_OFF; |
| 1590 | } |
| 1591 | List<String> supportedFlash = mParameters.getSupportedFlashModes(); |
| 1592 | if (isSupported(flashMode, supportedFlash)) { |
| 1593 | mParameters.setFlashMode(flashMode); |
| 1594 | } else { |
| 1595 | flashMode = mParameters.getFlashMode(); |
| 1596 | if (flashMode == null) { |
| 1597 | flashMode = mActivity.getString( |
| 1598 | R.string.pref_camera_flashmode_no_flash); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1599 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1603 | /** |
| 1604 | * Used to update the flash mode. Video mode can turn on the flash as torch |
| 1605 | * mode, which we would like to turn on and off when we switching in and |
| 1606 | * out to the preview. |
| 1607 | * |
| 1608 | * @param forceOff whether we want to force the flash off. |
| 1609 | */ |
| 1610 | private void forceFlashOff(boolean forceOff) { |
| 1611 | if (!mPreviewing || mParameters.getFlashMode() == null) { |
| 1612 | return; |
| 1613 | } |
| 1614 | forceFlashOffIfSupported(forceOff); |
| 1615 | mCameraDevice.setParameters(mParameters); |
ztenghui | 285a5be | 2013-10-09 15:59:47 -0700 | [diff] [blame] | 1616 | mUI.updateOnScreenIndicators(mParameters, mPreferences); |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1619 | @Override |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 1620 | public void onPreviewFocusChanged(boolean previewFocused) { |
| 1621 | mUI.onPreviewFocusChanged(previewFocused); |
| 1622 | forceFlashOff(!previewFocused); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1623 | } |
| 1624 | |
Erin Dahlgren | 3044d8c | 2013-10-10 18:23:45 -0700 | [diff] [blame] | 1625 | @Override |
| 1626 | public boolean arePreviewControlsVisible() { |
| 1627 | return mUI.arePreviewControlsVisible(); |
| 1628 | } |
| 1629 | |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1630 | private final class JpegPictureCallback implements CameraPictureCallback { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1631 | Location mLocation; |
| 1632 | |
| 1633 | public JpegPictureCallback(Location loc) { |
| 1634 | mLocation = loc; |
| 1635 | } |
| 1636 | |
| 1637 | @Override |
Angus Kong | 9ef9925 | 2013-07-18 18:04:19 -0700 | [diff] [blame] | 1638 | public void onPictureTaken(byte [] jpegData, CameraProxy camera) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1639 | Log.v(TAG, "onPictureTaken"); |
| 1640 | mSnapshotInProgress = false; |
| 1641 | showVideoSnapshotUI(false); |
| 1642 | storeImage(jpegData, mLocation); |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | private void storeImage(final byte[] data, Location loc) { |
| 1647 | long dateTaken = System.currentTimeMillis(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 1648 | String title = CameraUtil.createJpegName(dateTaken); |
Angus Kong | 0d00a89 | 2013-03-26 11:40:40 -0700 | [diff] [blame] | 1649 | ExifInterface exif = Exif.getExif(data); |
| 1650 | int orientation = Exif.getOrientation(exif); |
Doris Liu | 6df2d96 | 2013-08-20 16:31:29 -0700 | [diff] [blame] | 1651 | |
Sascha Haeberling | 280fd3e | 2013-11-21 13:52:15 -0800 | [diff] [blame] | 1652 | getServices().getMediaSaver().addImage( |
Doris Liu | 6df2d96 | 2013-08-20 16:31:29 -0700 | [diff] [blame] | 1653 | data, title, dateTaken, loc, orientation, |
Angus Kong | 83a99ae | 2013-04-17 15:37:07 -0700 | [diff] [blame] | 1654 | exif, mOnPhotoSavedListener, mContentResolver); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1655 | } |
| 1656 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1657 | private String convertOutputFormatToMimeType(int outputFileFormat) { |
| 1658 | if (outputFileFormat == MediaRecorder.OutputFormat.MPEG_4) { |
| 1659 | return "video/mp4"; |
| 1660 | } |
| 1661 | return "video/3gpp"; |
| 1662 | } |
| 1663 | |
| 1664 | private String convertOutputFormatToFileExt(int outputFileFormat) { |
| 1665 | if (outputFileFormat == MediaRecorder.OutputFormat.MPEG_4) { |
| 1666 | return ".mp4"; |
| 1667 | } |
| 1668 | return ".3gp"; |
| 1669 | } |
| 1670 | |
| 1671 | private void closeVideoFileDescriptor() { |
| 1672 | if (mVideoFileDescriptor != null) { |
| 1673 | try { |
| 1674 | mVideoFileDescriptor.close(); |
| 1675 | } catch (IOException e) { |
| 1676 | Log.e(TAG, "Fail to close fd", e); |
| 1677 | } |
| 1678 | mVideoFileDescriptor = null; |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | private void showTapToSnapshotToast() { |
| 1683 | new RotateTextToast(mActivity, R.string.video_snapshot_hint, 0) |
| 1684 | .show(); |
| 1685 | // Clear the preference. |
| 1686 | Editor editor = mPreferences.edit(); |
| 1687 | editor.putBoolean(CameraSettings.KEY_VIDEO_FIRST_USE_HINT_SHOWN, false); |
| 1688 | editor.apply(); |
| 1689 | } |
| 1690 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1691 | // required by OnPreferenceChangedListener |
| 1692 | @Override |
| 1693 | public void onCameraPickerClicked(int cameraId) { |
| 1694 | if (mPaused || mPendingSwitchCameraId != -1) return; |
| 1695 | |
| 1696 | mPendingSwitchCameraId = cameraId; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1697 | Log.d(TAG, "Start to copy texture."); |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1698 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 1699 | // Disable all camera controls. |
| 1700 | mSwitchingCamera = true; |
Sascha Haeberling | 0d63b89 | 2013-08-19 11:29:04 -0700 | [diff] [blame] | 1701 | switchCamera(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1702 | |
Doris Liu | 6a0de79 | 2013-02-26 10:54:25 -0800 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | @Override |
Angus Kong | fd4fc0e | 2013-11-07 15:38:09 -0800 | [diff] [blame] | 1706 | public void onMediaSaverAvailable(MediaSaver s) { |
Angus Kong | 86d3631 | 2013-01-31 18:22:44 -0800 | [diff] [blame] | 1707 | // do nothing. |
| 1708 | } |
Angus Kong | 395ee2d | 2013-07-15 12:42:41 -0700 | [diff] [blame] | 1709 | |
| 1710 | @Override |
| 1711 | public void onPreviewUIReady() { |
| 1712 | startPreview(); |
| 1713 | } |
| 1714 | |
| 1715 | @Override |
| 1716 | public void onPreviewUIDestroyed() { |
| 1717 | stopPreview(); |
| 1718 | } |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 1719 | |
| 1720 | private void requestCamera(int id) { |
| 1721 | mActivity.getCameraProvider().requestCamera(id); |
| 1722 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1723 | } |