VideoCamera.java revision a93a4d60eacee4e12471b45f8691c14114e113a4
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.android.camera; 18 19import com.android.camera.ui.CameraPicker; 20import com.android.camera.ui.IndicatorControlContainer; 21import com.android.camera.ui.IndicatorControlWheelContainer; 22import com.android.camera.ui.RotateImageView; 23import com.android.camera.ui.SharePopup; 24import com.android.camera.ui.ZoomControl; 25 26import android.content.ActivityNotFoundException; 27import android.content.BroadcastReceiver; 28import android.content.ContentResolver; 29import android.content.ContentValues; 30import android.content.Context; 31import android.content.Intent; 32import android.content.IntentFilter; 33import android.content.res.Configuration; 34import android.graphics.Bitmap; 35import android.hardware.Camera.CameraInfo; 36import android.hardware.Camera.Parameters; 37import android.hardware.Camera.PictureCallback; 38import android.hardware.Camera.Size; 39import android.location.Location; 40import android.media.CamcorderProfile; 41import android.media.CameraProfile; 42import android.media.MediaRecorder; 43import android.net.Uri; 44import android.os.Bundle; 45import android.os.Handler; 46import android.os.Message; 47import android.os.ParcelFileDescriptor; 48import android.os.SystemClock; 49import android.provider.MediaStore; 50import android.provider.MediaStore.Video; 51import android.provider.Settings; 52import android.util.Log; 53import android.view.GestureDetector; 54import android.view.Gravity; 55import android.view.KeyEvent; 56import android.view.Menu; 57import android.view.MenuItem; 58import android.view.MenuItem.OnMenuItemClickListener; 59import android.view.MotionEvent; 60import android.view.OrientationEventListener; 61import android.view.SurfaceHolder; 62import android.view.SurfaceView; 63import android.view.View; 64import android.view.Window; 65import android.view.WindowManager; 66import android.widget.ImageView; 67import android.widget.TextView; 68import android.widget.Toast; 69 70import java.io.File; 71import java.io.IOException; 72import java.text.SimpleDateFormat; 73import java.util.ArrayList; 74import java.util.Date; 75import java.util.HashMap; 76import java.util.Iterator; 77import java.util.List; 78 79/** 80 * The Camcorder activity. 81 */ 82public class VideoCamera extends ActivityBase 83 implements CameraPreference.OnPreferenceChangedListener, 84 ShutterButton.OnShutterButtonListener, SurfaceHolder.Callback, 85 MediaRecorder.OnErrorListener, MediaRecorder.OnInfoListener, 86 ModePicker.OnModeChangeListener, View.OnTouchListener, 87 EffectsRecorder.EffectsListener { 88 89 private static final String TAG = "videocamera"; 90 91 private static final String LAST_THUMB_FILENAME = "video_last_thumb"; 92 93 private static final int CHECK_DISPLAY_ROTATION = 3; 94 private static final int CLEAR_SCREEN_DELAY = 4; 95 private static final int UPDATE_RECORD_TIME = 5; 96 private static final int ENABLE_SHUTTER_BUTTON = 6; 97 98 private static final int SCREEN_DELAY = 2 * 60 * 1000; 99 100 // The brightness settings used when it is set to automatic in the system. 101 // The reason why it is set to 0.7 is just because 1.0 is too bright. 102 private static final float DEFAULT_CAMERA_BRIGHTNESS = 0.7f; 103 104 private static final long LOW_STORAGE_THRESHOLD = 512L * 1024L; 105 106 private static final boolean SWITCH_CAMERA = true; 107 private static final boolean SWITCH_VIDEO = false; 108 109 private static final long SHUTTER_BUTTON_TIMEOUT = 500L; // 500ms 110 111 private static final int[] TIME_LAPSE_VIDEO_QUALITY = { 112 CamcorderProfile.QUALITY_TIME_LAPSE_1080P, 113 CamcorderProfile.QUALITY_TIME_LAPSE_720P, 114 CamcorderProfile.QUALITY_TIME_LAPSE_480P, 115 CamcorderProfile.QUALITY_TIME_LAPSE_CIF, 116 CamcorderProfile.QUALITY_TIME_LAPSE_QCIF}; 117 118 private static final int[] VIDEO_QUALITY = { 119 CamcorderProfile.QUALITY_1080P, 120 CamcorderProfile.QUALITY_720P, 121 CamcorderProfile.QUALITY_480P, 122 CamcorderProfile.QUALITY_CIF, 123 CamcorderProfile.QUALITY_QCIF}; 124 125 /** 126 * An unpublished intent flag requesting to start recording straight away 127 * and return as soon as recording is stopped. 128 * TODO: consider publishing by moving into MediaStore. 129 */ 130 private final static String EXTRA_QUICK_CAPTURE = 131 "android.intent.extra.quickCapture"; 132 133 private boolean mSnapshotInProgress = false; 134 135 private final static String EFFECT_BG_FROM_GALLERY = 136 "gallery"; 137 138 private android.hardware.Camera mCameraDevice; 139 private final CameraErrorCallback mErrorCallback = new CameraErrorCallback(); 140 141 private ComboPreferences mPreferences; 142 private PreferenceGroup mPreferenceGroup; 143 144 private PreviewFrameLayout mPreviewFrameLayout; 145 private SurfaceHolder mSurfaceHolder = null; 146 private IndicatorControlContainer mIndicatorControlContainer; 147 private int mSurfaceWidth; 148 private int mSurfaceHeight; 149 private View mReviewControl; 150 151 private Toast mNoShareToast; 152 // An review image having same size as preview. It is displayed when 153 // recording is stopped in capture intent. 154 private ImageView mReviewImage; 155 // A popup window that contains a bigger thumbnail and a list of apps to share. 156 private SharePopup mSharePopup; 157 // The bitmap of the last captured video thumbnail and the URI of the 158 // original video. 159 private Thumbnail mThumbnail; 160 // An imageview showing showing the last captured picture thumbnail. 161 private RotateImageView mThumbnailView; 162 private ModePicker mModePicker; 163 private ShutterButton mShutterButton; 164 private TextView mRecordingTimeView; 165 private TextView mBgLearningMessage; 166 167 private boolean mIsVideoCaptureIntent; 168 private boolean mQuickCapture; 169 170 private boolean mOpenCameraFail = false; 171 private boolean mCameraDisabled = false; 172 173 private long mStorageSpace; 174 175 private MediaRecorder mMediaRecorder; 176 private EffectsRecorder mEffectsRecorder; 177 178 private int mEffectType = EffectsRecorder.EFFECT_NONE; 179 private Object mEffectParameter = null; 180 private String mEffectUriFromGallery = null; 181 182 private boolean mMediaRecorderRecording = false; 183 private long mRecordingStartTime; 184 private boolean mRecordingTimeCountsDown = false; 185 private long mOnResumeTime; 186 // The video file that the hardware camera is about to record into 187 // (or is recording into.) 188 private String mVideoFilename; 189 private ParcelFileDescriptor mVideoFileDescriptor; 190 191 // The video file that has already been recorded, and that is being 192 // examined by the user. 193 private String mCurrentVideoFilename; 194 private Uri mCurrentVideoUri; 195 private ContentValues mCurrentVideoValues; 196 197 private CamcorderProfile mProfile; 198 // The array of video quality profiles supported by each camera(s). Here the 199 // cameraId is the index of the array to get the profile map which contain 200 // the set of quality string and its real quality of a camera. 201 private HashMap<String, Integer>[] mProfileQuality; 202 private HashMap<String, Integer>[] mTimeLapseProfileQuality; 203 204 // The video duration limit. 0 menas no limit. 205 private int mMaxVideoDurationInMs; 206 207 // Time Lapse parameters. 208 private boolean mCaptureTimeLapse = false; 209 // Default 0. If it is larger than 0, the camcorder is in time lapse mode. 210 private int mTimeBetweenTimeLapseFrameCaptureMs = 0; 211 private View mTimeLapseLabel; 212 private View mPreviewBorder; 213 214 private int mDesiredPreviewWidth; 215 private int mDesiredPreviewHeight; 216 217 boolean mPausing = false; 218 boolean mPreviewing = false; // True if preview is started. 219 // The display rotation in degrees. This is only valid when mPreviewing is 220 // true. 221 private int mDisplayRotation; 222 223 private ContentResolver mContentResolver; 224 225 private LocationManager mLocationManager; 226 227 private final ArrayList<MenuItem> mGalleryItems = new ArrayList<MenuItem>(); 228 229 private final Handler mHandler = new MainHandler(); 230 private Parameters mParameters; 231 232 // multiple cameras support 233 private int mNumberOfCameras; 234 private int mCameraId; 235 private int mFrontCameraId; 236 private int mBackCameraId; 237 238 private GestureDetector mPopupGestureDetector; 239 240 private MyOrientationEventListener mOrientationListener; 241 // The degrees of the device rotated clockwise from its natural orientation. 242 private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; 243 // The orientation compensation for icons and thumbnails. Ex: if the value 244 // is 90, the UI components should be rotated 90 degrees counter-clockwise. 245 private int mOrientationCompensation = 0; 246 private int mOrientationHint; // the orientation hint for video playback 247 248 private static final int ZOOM_STOPPED = 0; 249 private static final int ZOOM_START = 1; 250 private static final int ZOOM_STOPPING = 2; 251 252 private int mZoomState = ZOOM_STOPPED; 253 private boolean mSmoothZoomSupported = false; 254 private int mZoomValue; // The current zoom value. 255 private int mZoomMax; 256 private int mTargetZoomValue; 257 private ZoomControl mZoomControl; 258 private final ZoomListener mZoomListener = new ZoomListener(); 259 260 // This Handler is used to post message back onto the main thread of the 261 // application 262 private class MainHandler extends Handler { 263 @Override 264 public void handleMessage(Message msg) { 265 switch (msg.what) { 266 267 case ENABLE_SHUTTER_BUTTON: 268 mShutterButton.setEnabled(true); 269 break; 270 271 case CLEAR_SCREEN_DELAY: { 272 getWindow().clearFlags( 273 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 274 break; 275 } 276 277 case UPDATE_RECORD_TIME: { 278 updateRecordingTime(); 279 break; 280 } 281 282 case CHECK_DISPLAY_ROTATION: { 283 // Restart the preview if display rotation has changed. 284 // Sometimes this happens when the device is held upside 285 // down and camera app is opened. Rotation animation will 286 // take some time and the rotation value we have got may be 287 // wrong. Framework does not have a callback for this now. 288 if ((Util.getDisplayRotation(VideoCamera.this) != mDisplayRotation) 289 && !mMediaRecorderRecording) { 290 startPreview(); 291 } 292 if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) { 293 mHandler.sendEmptyMessageDelayed(CHECK_DISPLAY_ROTATION, 100); 294 } 295 break; 296 } 297 298 default: 299 Log.v(TAG, "Unhandled message: " + msg.what); 300 break; 301 } 302 } 303 } 304 305 private BroadcastReceiver mReceiver = null; 306 307 private class MyBroadcastReceiver extends BroadcastReceiver { 308 @Override 309 public void onReceive(Context context, Intent intent) { 310 String action = intent.getAction(); 311 if (action.equals(Intent.ACTION_MEDIA_EJECT)) { 312 updateAndShowStorageHint(); 313 stopVideoRecording(); 314 } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { 315 updateAndShowStorageHint(); 316 updateThumbnailButton(); 317 } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) { 318 // SD card unavailable 319 // handled in ACTION_MEDIA_EJECT 320 } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) { 321 Toast.makeText(VideoCamera.this, 322 getResources().getString(R.string.wait), Toast.LENGTH_LONG).show(); 323 } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) { 324 updateAndShowStorageHint(); 325 } 326 } 327 } 328 329 private String createName(long dateTaken) { 330 Date date = new Date(dateTaken); 331 SimpleDateFormat dateFormat = new SimpleDateFormat( 332 getString(R.string.video_file_name_format)); 333 334 return dateFormat.format(date); 335 } 336 337 @Override 338 public void onCreate(Bundle icicle) { 339 super.onCreate(icicle); 340 341 Window win = getWindow(); 342 343 // Overright the brightness settings if it is automatic 344 int mode = Settings.System.getInt( 345 getContentResolver(), 346 Settings.System.SCREEN_BRIGHTNESS_MODE, 347 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 348 if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) { 349 WindowManager.LayoutParams winParams = win.getAttributes(); 350 winParams.screenBrightness = DEFAULT_CAMERA_BRIGHTNESS; 351 win.setAttributes(winParams); 352 } 353 354 mPreferences = new ComboPreferences(this); 355 CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal()); 356 mCameraId = CameraSettings.readPreferredCameraId(mPreferences); 357 358 //Testing purpose. Launch a specific camera through the intent extras. 359 int intentCameraId = Util.getCameraFacingIntentExtras(this); 360 if (intentCameraId != -1) { 361 mCameraId = intentCameraId; 362 } 363 364 mPreferences.setLocalId(this, mCameraId); 365 CameraSettings.upgradeLocalPreferences(mPreferences.getLocal()); 366 367 mNumberOfCameras = CameraHolder.instance().getNumberOfCameras(); 368 369 /* 370 * To reduce startup time, we start the preview in another thread. 371 * We make sure the preview is started at the end of onCreate. 372 */ 373 Thread startPreviewThread = new Thread(new Runnable() { 374 public void run() { 375 try { 376 mCameraDevice = Util.openCamera(VideoCamera.this, mCameraId); 377 readVideoPreferences(); 378 startPreview(); 379 } catch(CameraHardwareException e) { 380 mOpenCameraFail = true; 381 } catch(CameraDisabledException e) { 382 mCameraDisabled = true; 383 } 384 } 385 }); 386 startPreviewThread.start(); 387 388 mContentResolver = getContentResolver(); 389 390 requestWindowFeature(Window.FEATURE_PROGRESS); 391 mIsVideoCaptureIntent = isVideoCaptureIntent(); 392 setContentView(R.layout.video_camera); 393 if (mIsVideoCaptureIntent) { 394 findViewById(R.id.btn_cancel).setVisibility(View.VISIBLE); 395 } else { 396 initThumbnailButton(); 397 mModePicker = (ModePicker) findViewById(R.id.mode_picker); 398 mModePicker.setVisibility(View.VISIBLE); 399 mModePicker.setOnModeChangeListener(this); 400 } 401 402 mPreviewFrameLayout = (PreviewFrameLayout) 403 findViewById(R.id.frame_layout); 404 mReviewImage = (ImageView) findViewById(R.id.review_image); 405 mModePicker = (ModePicker) findViewById(R.id.mode_picker); 406 407 // don't set mSurfaceHolder here. We have it set ONLY within 408 // surfaceCreated / surfaceDestroyed, other parts of the code 409 // assume that when it is set, the surface is also set. 410 SurfaceView preview = (SurfaceView) findViewById(R.id.camera_preview); 411 SurfaceHolder holder = preview.getHolder(); 412 holder.addCallback(this); 413 holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 414 415 mQuickCapture = getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false); 416 417 mShutterButton = (ShutterButton) findViewById(R.id.shutter_button); 418 mShutterButton.setBackgroundResource(R.drawable.btn_shutter_video); 419 mShutterButton.setOnShutterButtonListener(this); 420 mShutterButton.requestFocus(); 421 422 mRecordingTimeView = (TextView) findViewById(R.id.recording_time); 423 mOrientationListener = new MyOrientationEventListener(VideoCamera.this); 424 mTimeLapseLabel = findViewById(R.id.time_lapse_label); 425 mPreviewBorder = findViewById(R.id.preview_border); 426 427 mBgLearningMessage = (TextView) findViewById(R.id.bg_replace_message); 428 429 mLocationManager = new LocationManager(this, null); 430 431 // Make sure preview is started. 432 try { 433 startPreviewThread.join(); 434 if (mOpenCameraFail) { 435 Util.showErrorAndFinish(this, R.string.cannot_connect_camera); 436 return; 437 } else if (mCameraDisabled) { 438 Util.showErrorAndFinish(this, R.string.camera_disabled); 439 return; 440 } 441 } catch (InterruptedException ex) { 442 // ignore 443 } 444 445 showTimeLapseUI(mCaptureTimeLapse); 446 resizeForPreviewAspectRatio(); 447 448 mBackCameraId = CameraHolder.instance().getBackCameraId(); 449 mFrontCameraId = CameraHolder.instance().getFrontCameraId(); 450 451 // Initialize after startPreview becuase this need mParameters. 452 initializeZoomControl(); 453 initializeIndicatorControl(); 454 if ("true".equals(mParameters.get("video-snapshot-supported")) && 455 !mIsVideoCaptureIntent) { 456 preview.setOnTouchListener(this); 457 } 458 } 459 460 private void loadCameraPreferences() { 461 CameraSettings settings = new CameraSettings(this, mParameters, 462 mCameraId, CameraHolder.instance().getCameraInfo()); 463 mPreferenceGroup = settings.getPreferenceGroup(R.xml.video_preferences); 464 } 465 466 private boolean collapseCameraControls() { 467 if ((mIndicatorControlContainer != null) 468 && mIndicatorControlContainer.dismissSettingPopup()) { 469 return true; 470 } 471 return false; 472 } 473 474 private void enableCameraControls(boolean enable) { 475 if (mIndicatorControlContainer != null) { 476 mIndicatorControlContainer.setEnabled(enable); 477 } 478 if (mModePicker != null) mModePicker.setEnabled(enable); 479 } 480 481 private void initializeIndicatorControl() { 482 mIndicatorControlContainer = 483 (IndicatorControlContainer) findViewById(R.id.indicator_control); 484 if (mIndicatorControlContainer == null) return; 485 loadCameraPreferences(); 486 487 final String[] SETTING_KEYS = { 488 CameraSettings.KEY_VIDEO_EFFECT, 489 CameraSettings.KEY_WHITE_BALANCE, 490 CameraSettings.KEY_VIDEO_QUALITY}; 491 final String[] OTHER_SETTING_KEYS = { 492 CameraSettings.KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL}; 493 494 CameraPicker.setImageResourceId(R.drawable.ic_switch_video_facing_holo_light); 495 mIndicatorControlContainer.initialize(this, mPreferenceGroup, 496 CameraSettings.KEY_VIDEOCAMERA_FLASH_MODE, 497 mParameters.isZoomSupported(), SETTING_KEYS, OTHER_SETTING_KEYS); 498 mIndicatorControlContainer.setListener(this); 499 mPopupGestureDetector = new GestureDetector(this, 500 new PopupGestureListener()); 501 } 502 503 public static int roundOrientation(int orientation) { 504 return ((orientation + 45) / 90 * 90) % 360; 505 } 506 507 private class MyOrientationEventListener 508 extends OrientationEventListener { 509 public MyOrientationEventListener(Context context) { 510 super(context); 511 } 512 513 @Override 514 public void onOrientationChanged(int orientation) { 515 // We keep the last known orientation. So if the user first orient 516 // the camera then point the camera to floor or sky, we still have 517 // the correct orientation. 518 if (orientation == ORIENTATION_UNKNOWN) return; 519 mOrientation = roundOrientation(orientation); 520 // When the screen is unlocked, display rotation may change. Always 521 // calculate the up-to-date orientationCompensation. 522 int orientationCompensation = mOrientation 523 + Util.getDisplayRotation(VideoCamera.this); 524 525 if (mOrientationCompensation != orientationCompensation) { 526 mOrientationCompensation = orientationCompensation; 527 // Do not rotate the icons during recording because the video 528 // orientation is fixed after recording. 529 if (!mMediaRecorderRecording) { 530 setOrientationIndicator(mOrientationCompensation); 531 } 532 } 533 } 534 } 535 536 private void setOrientationIndicator(int degree) { 537 if (mThumbnailView != null) mThumbnailView.setDegree(degree); 538 if (mModePicker != null) mModePicker.setDegree(degree); 539 if (mSharePopup != null) mSharePopup.setOrientation(degree); 540 if (mIndicatorControlContainer != null) mIndicatorControlContainer.setDegree(degree); 541 } 542 543 private void startPlayVideoActivity() { 544 Intent intent = new Intent(Intent.ACTION_VIEW, mCurrentVideoUri); 545 try { 546 startActivity(intent); 547 } catch (ActivityNotFoundException ex) { 548 Log.e(TAG, "Couldn't view video " + mCurrentVideoUri, ex); 549 } 550 } 551 552 @OnClickAttr 553 public void onThumbnailClicked(View v) { 554 if (!mMediaRecorderRecording && mThumbnail != null) { 555 showSharePopup(); 556 } 557 } 558 559 @OnClickAttr 560 public void onRetakeButtonClicked(View v) { 561 deleteCurrentVideo(); 562 hideAlert(); 563 } 564 565 @OnClickAttr 566 public void onPlayButtonClicked(View v) { 567 startPlayVideoActivity(); 568 } 569 570 @OnClickAttr 571 public void onDoneButtonClicked(View v) { 572 doReturnToCaller(true); 573 } 574 575 @OnClickAttr 576 public void onCancelButtonClicked(View v) { 577 stopVideoRecording(); 578 doReturnToCaller(false); 579 } 580 581 public void onShutterButtonFocus(ShutterButton button, boolean pressed) { 582 // Do nothing (everything happens in onShutterButtonClick). 583 } 584 585 private void onStopVideoRecording(boolean valid) { 586 stopVideoRecording(); 587 if (mIsVideoCaptureIntent) { 588 if (mQuickCapture) { 589 doReturnToCaller(valid); 590 } else { 591 showAlert(); 592 } 593 } else { 594 getThumbnail(); 595 } 596 } 597 598 public void onShutterButtonClick(ShutterButton button) { 599 switch (button.getId()) { 600 case R.id.shutter_button: 601 if (collapseCameraControls()) return; 602 boolean stop = mMediaRecorderRecording; 603 604 if (stop) { 605 onStopVideoRecording(true); 606 } else { 607 startVideoRecording(); 608 } 609 mShutterButton.setEnabled(false); 610 611 // Keep the shutter button disabled when in video capture intent 612 // mode and recording is stopped. It'll be re-enabled when 613 // re-take button is clicked. 614 if (!(mIsVideoCaptureIntent && stop)) { 615 mHandler.sendEmptyMessageDelayed( 616 ENABLE_SHUTTER_BUTTON, SHUTTER_BUTTON_TIMEOUT); 617 } 618 break; 619 } 620 } 621 622 private OnScreenHint mStorageHint; 623 624 private void updateAndShowStorageHint() { 625 mStorageSpace = Storage.getAvailableSpace(); 626 showStorageHint(); 627 } 628 629 private void showStorageHint() { 630 String errorMessage = null; 631 if (mStorageSpace == Storage.UNAVAILABLE) { 632 errorMessage = getString(R.string.no_storage); 633 } else if (mStorageSpace == Storage.PREPARING) { 634 errorMessage = getString(R.string.preparing_sd); 635 } else if (mStorageSpace == Storage.UNKNOWN_SIZE) { 636 errorMessage = getString(R.string.access_sd_fail); 637 } else if (mStorageSpace < LOW_STORAGE_THRESHOLD) { 638 errorMessage = getString(R.string.spaceIsLow_content); 639 } 640 641 if (errorMessage != null) { 642 if (mStorageHint == null) { 643 mStorageHint = OnScreenHint.makeText(this, errorMessage); 644 } else { 645 mStorageHint.setText(errorMessage); 646 } 647 mStorageHint.show(); 648 } else if (mStorageHint != null) { 649 mStorageHint.cancel(); 650 mStorageHint = null; 651 } 652 } 653 654 private void readVideoPreferences() { 655 String quality = mPreferences.getString( 656 CameraSettings.KEY_VIDEO_QUALITY, 657 CameraSettings.DEFAULT_VIDEO_QUALITY_VALUE); 658 boolean videoQualityHigh = 659 CameraSettings.getVideoQuality(this, quality); 660 661 // Set video quality. 662 Intent intent = getIntent(); 663 if (intent.hasExtra(MediaStore.EXTRA_VIDEO_QUALITY)) { 664 int extraVideoQuality = 665 intent.getIntExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); 666 if (extraVideoQuality > 0) { 667 quality = getString(R.string.pref_video_quality_high); 668 } else { // 0 is mms. 669 quality = getString(R.string.pref_video_quality_mms); 670 } 671 } 672 673 // Set video duration limit. The limit is read from the preference, 674 // unless it is specified in the intent. 675 if (intent.hasExtra(MediaStore.EXTRA_DURATION_LIMIT)) { 676 int seconds = 677 intent.getIntExtra(MediaStore.EXTRA_DURATION_LIMIT, 0); 678 mMaxVideoDurationInMs = 1000 * seconds; 679 } else { 680 mMaxVideoDurationInMs = 681 CameraSettings.getVideoDurationInMillis(this, quality, mCameraId); 682 } 683 684 // Read time lapse recording interval. 685 String frameIntervalStr = mPreferences.getString( 686 CameraSettings.KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL, 687 getString(R.string.pref_video_time_lapse_frame_interval_default)); 688 mTimeBetweenTimeLapseFrameCaptureMs = Integer.parseInt(frameIntervalStr); 689 690 mCaptureTimeLapse = (mTimeBetweenTimeLapseFrameCaptureMs != 0); 691 int profileQuality = getProfileQuality(mCameraId, quality, mCaptureTimeLapse); 692 mProfile = CamcorderProfile.get(mCameraId, profileQuality); 693 getDesiredPreviewSize(); 694 695 // Set effect 696 mEffectType = CameraSettings.readEffectType(mPreferences); 697 if (mEffectType != EffectsRecorder.EFFECT_NONE) { 698 mEffectParameter = CameraSettings.readEffectParameter(mPreferences); 699 // When picking from gallery, mEffectParameter should have been 700 // initialized in onActivityResult. If not, fall back to no effect 701 if (mEffectType == EffectsRecorder.EFFECT_BACKDROPPER && 702 ((String)mEffectParameter).equals(EFFECT_BG_FROM_GALLERY)) { 703 if (mEffectUriFromGallery == null) { 704 Log.w(TAG, "No URI from gallery, resetting to no effect"); 705 mEffectType = EffectsRecorder.EFFECT_NONE; 706 mEffectParameter = null; 707 ComboPreferences.Editor editor = mPreferences.edit(); 708 editor.putString(CameraSettings.KEY_VIDEO_EFFECT, "none"); 709 editor.apply(); 710 } 711 } 712 } else { 713 mEffectParameter = null; 714 } 715 716 } 717 718 int getProfileQuality(int cameraId, String quality, boolean captureTimeLapse) { 719 HashMap<String, Integer>[] qualityMap; 720 if (captureTimeLapse) { 721 if (mTimeLapseProfileQuality == null) { 722 mTimeLapseProfileQuality = new HashMap[ 723 CameraHolder.instance().getNumberOfCameras()]; 724 } 725 qualityMap = mTimeLapseProfileQuality; 726 if (qualityMap[cameraId] == null) { 727 qualityMap[cameraId] = buildProfileQuality(cameraId, TIME_LAPSE_VIDEO_QUALITY); 728 } 729 } else { 730 if (mProfileQuality == null) { 731 mProfileQuality = new HashMap[ 732 CameraHolder.instance().getNumberOfCameras()]; 733 } 734 qualityMap = mProfileQuality; 735 if (qualityMap[cameraId] == null) { 736 qualityMap[cameraId] = buildProfileQuality(cameraId, VIDEO_QUALITY); 737 } 738 } 739 return qualityMap[cameraId].get(quality); 740 } 741 742 HashMap<String, Integer> buildProfileQuality(int cameraId, 743 int qualityList[]) { 744 HashMap<String, Integer> qualityMap = new HashMap<String, Integer>(); 745 int highestQuality = -1, secondHighestQuality = -1, 746 lastEffectiveQuality = -1; 747 for (int i = 0; i < qualityList.length; i++) { 748 if (CamcorderProfile.hasProfile(cameraId, qualityList[i])) { 749 if (highestQuality == -1) { 750 highestQuality = qualityList[i]; 751 } else if (secondHighestQuality == -1) { 752 secondHighestQuality = qualityList[i]; 753 } 754 lastEffectiveQuality = qualityList[i]; 755 } 756 } 757 if (secondHighestQuality == -1) { 758 secondHighestQuality = highestQuality; 759 } 760 qualityMap.put(getString(R.string.pref_video_quality_high), highestQuality); 761 qualityMap.put(getString(R.string.pref_video_quality_low), secondHighestQuality); 762 qualityMap.put(getString(R.string.pref_video_quality_youtube), highestQuality); 763 qualityMap.put(getString(R.string.pref_video_quality_mms), lastEffectiveQuality); 764 return qualityMap; 765 } 766 767 private void getDesiredPreviewSize() { 768 mParameters = mCameraDevice.getParameters(); 769 if (mParameters.getSupportedVideoSizes() == null) { 770 mDesiredPreviewWidth = mProfile.videoFrameWidth; 771 mDesiredPreviewHeight = mProfile.videoFrameHeight; 772 } else { // Driver supports separates outputs for preview and video. 773 List<Size> sizes = mParameters.getSupportedPreviewSizes(); 774 Size preferred = mParameters.getPreferredPreviewSizeForVideo(); 775 int product = preferred.width * preferred.height; 776 Iterator it = sizes.iterator(); 777 // Remove the preview sizes that are not preferred. 778 while (it.hasNext()) { 779 Size size = (Size) it.next(); 780 if (size.width * size.height > product) { 781 it.remove(); 782 } 783 } 784 Size optimalSize = Util.getOptimalPreviewSize(this, sizes, 785 (double) mProfile.videoFrameWidth / mProfile.videoFrameHeight); 786 mDesiredPreviewWidth = optimalSize.width; 787 mDesiredPreviewHeight = optimalSize.height; 788 } 789 Log.v(TAG, "mDesiredPreviewWidth=" + mDesiredPreviewWidth + 790 ". mDesiredPreviewHeight=" + mDesiredPreviewHeight); 791 } 792 793 private void resizeForPreviewAspectRatio() { 794 mPreviewFrameLayout.setAspectRatio( 795 (double) mProfile.videoFrameWidth / mProfile.videoFrameHeight); 796 } 797 798 @Override 799 protected void onResume() { 800 super.onResume(); 801 mPausing = false; 802 if (mOpenCameraFail || mCameraDisabled) return; 803 mZoomValue = 0; 804 805 mReviewImage.setVisibility(View.GONE); 806 807 // Start orientation listener as soon as possible because it takes 808 // some time to get first orientation. 809 mOrientationListener.enable(); 810 if (!mPreviewing) { 811 try { 812 mCameraDevice = Util.openCamera(this, mCameraId); 813 readVideoPreferences(); 814 resizeForPreviewAspectRatio(); 815 startPreview(); 816 } catch(CameraHardwareException e) { 817 Util.showErrorAndFinish(this, R.string.cannot_connect_camera); 818 return; 819 } catch(CameraDisabledException e) { 820 Util.showErrorAndFinish(this, R.string.camera_disabled); 821 return; 822 } 823 } 824 keepScreenOnAwhile(); 825 826 // install an intent filter to receive SD card related events. 827 IntentFilter intentFilter = 828 new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); 829 intentFilter.addAction(Intent.ACTION_MEDIA_EJECT); 830 intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); 831 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED); 832 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); 833 intentFilter.addDataScheme("file"); 834 mReceiver = new MyBroadcastReceiver(); 835 registerReceiver(mReceiver, intentFilter); 836 mStorageSpace = Storage.getAvailableSpace(); 837 838 mHandler.postDelayed(new Runnable() { 839 public void run() { 840 showStorageHint(); 841 } 842 }, 200); 843 844 // Initialize location sevice. 845 boolean recordLocation = RecordLocationPreference.get( 846 mPreferences, getContentResolver()); 847 mLocationManager.recordLocation(recordLocation); 848 849 if (!mIsVideoCaptureIntent) { 850 updateThumbnailButton(); // Update the last video thumbnail. 851 mModePicker.setCurrentMode(ModePicker.MODE_VIDEO); 852 } 853 854 if (mPreviewing) { 855 mOnResumeTime = SystemClock.uptimeMillis(); 856 mHandler.sendEmptyMessageDelayed(CHECK_DISPLAY_ROTATION, 100); 857 } 858 } 859 860 private void setPreviewDisplay(SurfaceHolder holder) { 861 try { 862 if (effectsActive() && mPreviewing) { 863 mEffectsRecorder.setPreviewDisplay( 864 mSurfaceHolder, 865 mSurfaceWidth, 866 mSurfaceHeight); 867 } else { 868 mCameraDevice.setPreviewDisplay(holder); 869 } 870 } catch (Throwable ex) { 871 closeCamera(); 872 throw new RuntimeException("setPreviewDisplay failed", ex); 873 } 874 } 875 876 private void startPreview() { 877 Log.v(TAG, "startPreview"); 878 879 mCameraDevice.setErrorCallback(mErrorCallback); 880 if (mPreviewing == true) { 881 mCameraDevice.stopPreview(); 882 if (effectsActive() && mEffectsRecorder != null) { 883 mEffectsRecorder.release(); 884 } 885 mPreviewing = false; 886 } 887 if (!effectsActive()) { 888 setPreviewDisplay(mSurfaceHolder); 889 mDisplayRotation = Util.getDisplayRotation(this); 890 int orientation = Util.getDisplayOrientation(mDisplayRotation, mCameraId); 891 mCameraDevice.setDisplayOrientation(orientation); 892 setCameraParameters(); 893 894 try { 895 mCameraDevice.startPreview(); 896 } catch (Throwable ex) { 897 closeCamera(); 898 throw new RuntimeException("startPreview failed", ex); 899 } 900 } else { 901 setCameraParameters(); 902 903 initializeEffectsPreview(); 904 Log.v(TAG, "effectsStartPreview"); 905 mEffectsRecorder.startPreview(); 906 } 907 908 mZoomState = ZOOM_STOPPED; 909 mPreviewing = true; 910 } 911 912 private void closeCamera() { 913 Log.v(TAG, "closeCamera"); 914 if (mCameraDevice == null) { 915 Log.d(TAG, "already stopped."); 916 return; 917 } 918 if (mEffectsRecorder != null) { 919 mEffectsRecorder.release(); 920 } 921 mEffectType = EffectsRecorder.EFFECT_NONE; 922 CameraHolder.instance().release(); 923 mCameraDevice = null; 924 mPreviewing = false; 925 } 926 927 private void finishRecorderAndCloseCamera() { 928 // This is similar to what mShutterButton.performClick() does, 929 // but not quite the same. 930 if (mMediaRecorderRecording) { 931 if (mIsVideoCaptureIntent) { 932 stopVideoRecording(); 933 showAlert(); 934 } else { 935 stopVideoRecording(); 936 getThumbnail(); 937 } 938 } else { 939 stopVideoRecording(); 940 } 941 closeCamera(); 942 } 943 944 @Override 945 protected void onPause() { 946 super.onPause(); 947 mPausing = true; 948 949 if (mIndicatorControlContainer != null) { 950 mIndicatorControlContainer.dismissSettingPopup(); 951 } 952 953 finishRecorderAndCloseCamera(); 954 955 if (mSharePopup != null) mSharePopup.dismiss(); 956 957 if (mReceiver != null) { 958 unregisterReceiver(mReceiver); 959 mReceiver = null; 960 } 961 resetScreenOn(); 962 963 if (!mIsVideoCaptureIntent && mThumbnail != null) { 964 mThumbnail.saveTo(new File(getFilesDir(), LAST_THUMB_FILENAME)); 965 } 966 967 if (mStorageHint != null) { 968 mStorageHint.cancel(); 969 mStorageHint = null; 970 } 971 972 mOrientationListener.disable(); 973 mLocationManager.recordLocation(false); 974 975 mHandler.removeMessages(CHECK_DISPLAY_ROTATION); 976 } 977 978 @Override 979 public void onUserInteraction() { 980 super.onUserInteraction(); 981 if (!mMediaRecorderRecording) keepScreenOnAwhile(); 982 } 983 984 @Override 985 public void onBackPressed() { 986 if (mPausing) return; 987 if (mMediaRecorderRecording) { 988 onStopVideoRecording(false); 989 } else if (!collapseCameraControls()) { 990 super.onBackPressed(); 991 } 992 } 993 994 @Override 995 public boolean onKeyDown(int keyCode, KeyEvent event) { 996 // Do not handle any key if the activity is paused. 997 if (mPausing) { 998 return true; 999 } 1000 1001 switch (keyCode) { 1002 case KeyEvent.KEYCODE_CAMERA: 1003 if (event.getRepeatCount() == 0) { 1004 mShutterButton.performClick(); 1005 return true; 1006 } 1007 break; 1008 case KeyEvent.KEYCODE_DPAD_CENTER: 1009 if (event.getRepeatCount() == 0) { 1010 mShutterButton.performClick(); 1011 return true; 1012 } 1013 break; 1014 case KeyEvent.KEYCODE_MENU: 1015 if (mMediaRecorderRecording) return true; 1016 break; 1017 } 1018 1019 return super.onKeyDown(keyCode, event); 1020 } 1021 1022 @Override 1023 public boolean onKeyUp(int keyCode, KeyEvent event) { 1024 switch (keyCode) { 1025 case KeyEvent.KEYCODE_CAMERA: 1026 mShutterButton.setPressed(false); 1027 return true; 1028 } 1029 return super.onKeyUp(keyCode, event); 1030 } 1031 1032 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 1033 // Make sure we have a surface in the holder before proceeding. 1034 if (holder.getSurface() == null) { 1035 Log.d(TAG, "holder.getSurface() == null"); 1036 return; 1037 } 1038 1039 Log.v(TAG, "surfaceChanged. w=" + w + ". h=" + h); 1040 1041 mSurfaceHolder = holder; 1042 mSurfaceWidth = w; 1043 mSurfaceHeight = h; 1044 1045 if (mPausing) { 1046 // We're pausing, the screen is off and we already stopped 1047 // video recording. We don't want to start the camera again 1048 // in this case in order to conserve power. 1049 // The fact that surfaceChanged is called _after_ an onPause appears 1050 // to be legitimate since in that case the lockscreen always returns 1051 // to portrait orientation possibly triggering the notification. 1052 return; 1053 } 1054 1055 // The mCameraDevice will be null if it is fail to connect to the 1056 // camera hardware. In this case we will show a dialog and then 1057 // finish the activity, so it's OK to ignore it. 1058 if (mCameraDevice == null) return; 1059 1060 // Set preview display if the surface is being created. Preview was 1061 // already started. Also restart the preview if display rotation has 1062 // changed. Sometimes this happens when the device is held in portrait 1063 // and camera app is opened. Rotation animation takes some time and 1064 // display rotation in onCreate may not be what we want. 1065 if (mPreviewing && (Util.getDisplayRotation(this) == mDisplayRotation) 1066 && holder.isCreating()) { 1067 setPreviewDisplay(holder); 1068 } else { 1069 stopVideoRecording(); 1070 startPreview(); 1071 } 1072 } 1073 1074 public void surfaceCreated(SurfaceHolder holder) { 1075 } 1076 1077 public void surfaceDestroyed(SurfaceHolder holder) { 1078 mSurfaceHolder = null; 1079 } 1080 1081 private void gotoGallery() { 1082 MenuHelper.gotoCameraVideoGallery(this); 1083 } 1084 1085 @Override 1086 public boolean onCreateOptionsMenu(Menu menu) { 1087 super.onCreateOptionsMenu(menu); 1088 1089 if (mIsVideoCaptureIntent) { 1090 // No options menu for attach mode. 1091 return false; 1092 } else { 1093 addBaseMenuItems(menu); 1094 } 1095 return true; 1096 } 1097 1098 private boolean isVideoCaptureIntent() { 1099 String action = getIntent().getAction(); 1100 return (MediaStore.ACTION_VIDEO_CAPTURE.equals(action)); 1101 } 1102 1103 private void doReturnToCaller(boolean valid) { 1104 Intent resultIntent = new Intent(); 1105 int resultCode; 1106 if (valid) { 1107 resultCode = RESULT_OK; 1108 resultIntent.setData(mCurrentVideoUri); 1109 } else { 1110 resultCode = RESULT_CANCELED; 1111 } 1112 setResultEx(resultCode, resultIntent); 1113 finish(); 1114 } 1115 1116 private void cleanupEmptyFile() { 1117 if (mVideoFilename != null) { 1118 File f = new File(mVideoFilename); 1119 if (f.length() == 0 && f.delete()) { 1120 Log.v(TAG, "Empty video file deleted: " + mVideoFilename); 1121 mVideoFilename = null; 1122 } 1123 } 1124 } 1125 1126 // Prepares media recorder. 1127 private void initializeRecorder() { 1128 Log.v(TAG, "initializeRecorder"); 1129 // If the mCameraDevice is null, then this activity is going to finish 1130 if (mCameraDevice == null) return; 1131 1132 if (mSurfaceHolder == null) { 1133 Log.v(TAG, "Surface holder is null. Wait for surface changed."); 1134 return; 1135 } 1136 1137 Intent intent = getIntent(); 1138 Bundle myExtras = intent.getExtras(); 1139 1140 long requestedSizeLimit = 0; 1141 if (mIsVideoCaptureIntent && myExtras != null) { 1142 Uri saveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT); 1143 if (saveUri != null) { 1144 try { 1145 mVideoFileDescriptor = 1146 mContentResolver.openFileDescriptor(saveUri, "rw"); 1147 mCurrentVideoUri = saveUri; 1148 } catch (java.io.FileNotFoundException ex) { 1149 // invalid uri 1150 Log.e(TAG, ex.toString()); 1151 } 1152 } 1153 requestedSizeLimit = myExtras.getLong(MediaStore.EXTRA_SIZE_LIMIT); 1154 } 1155 mMediaRecorder = new MediaRecorder(); 1156 1157 // Unlock the camera object before passing it to media recorder. 1158 mCameraDevice.unlock(); 1159 mMediaRecorder.setCamera(mCameraDevice); 1160 if (!mCaptureTimeLapse) { 1161 mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 1162 } 1163 mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 1164 mMediaRecorder.setProfile(mProfile); 1165 mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs); 1166 if (mCaptureTimeLapse) { 1167 mMediaRecorder.setCaptureRate((1000 / (double) mTimeBetweenTimeLapseFrameCaptureMs)); 1168 } 1169 1170 // Set output file. 1171 // Try Uri in the intent first. If it doesn't exist, use our own 1172 // instead. 1173 if (mVideoFileDescriptor != null) { 1174 mMediaRecorder.setOutputFile(mVideoFileDescriptor.getFileDescriptor()); 1175 } else { 1176 generateVideoFilename(mProfile.fileFormat); 1177 mMediaRecorder.setOutputFile(mVideoFilename); 1178 } 1179 1180 mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); 1181 1182 // Set maximum file size. 1183 // remaining >= LOW_STORAGE_THRESHOLD at this point, reserve a quarter 1184 // of that to make it more likely that recording can complete 1185 // successfully. 1186 long maxFileSize = mStorageSpace - LOW_STORAGE_THRESHOLD / 4; 1187 if (requestedSizeLimit > 0 && requestedSizeLimit < maxFileSize) { 1188 maxFileSize = requestedSizeLimit; 1189 } 1190 1191 try { 1192 mMediaRecorder.setMaxFileSize(maxFileSize); 1193 } catch (RuntimeException exception) { 1194 // We are going to ignore failure of setMaxFileSize here, as 1195 // a) The composer selected may simply not support it, or 1196 // b) The underlying media framework may not handle 64-bit range 1197 // on the size restriction. 1198 } 1199 1200 // See android.hardware.Camera.Parameters.setRotation for 1201 // documentation. 1202 int rotation = 0; 1203 if (mOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) { 1204 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId]; 1205 if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 1206 rotation = (info.orientation - mOrientation + 360) % 360; 1207 } else { // back-facing camera 1208 rotation = (info.orientation + mOrientation) % 360; 1209 } 1210 } 1211 mMediaRecorder.setOrientationHint(rotation); 1212 mOrientationHint = rotation; 1213 1214 try { 1215 mMediaRecorder.prepare(); 1216 } catch (IOException e) { 1217 Log.e(TAG, "prepare failed for " + mVideoFilename, e); 1218 releaseMediaRecorder(); 1219 throw new RuntimeException(e); 1220 } 1221 1222 mMediaRecorder.setOnErrorListener(this); 1223 mMediaRecorder.setOnInfoListener(this); 1224 } 1225 1226 private void initializeEffectsPreview() { 1227 Log.v(TAG, "initializeEffectsPreview"); 1228 // If the mCameraDevice is null, then this activity is going to finish 1229 if (mCameraDevice == null) return; 1230 1231 mEffectsRecorder = new EffectsRecorder(this); 1232 1233 mEffectsRecorder.setCamera(mCameraDevice); 1234 mEffectsRecorder.setProfile(mProfile); 1235 mEffectsRecorder.setEffectsListener(this); 1236 mEffectsRecorder.setOnInfoListener(this); 1237 mEffectsRecorder.setOnErrorListener(this); 1238 1239 // See android.hardware.Camera.Parameters.setRotation for 1240 // documentation. 1241 int rotation = 0; 1242 if (mOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) { 1243 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId]; 1244 if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 1245 rotation = (info.orientation - mOrientation + 360) % 360; 1246 } else { // back-facing camera 1247 rotation = (info.orientation + mOrientation) % 360; 1248 } 1249 } 1250 mEffectsRecorder.setOrientationHint(rotation); 1251 mOrientationHint = rotation; 1252 1253 mEffectsRecorder.setPreviewDisplay( 1254 mSurfaceHolder, 1255 mSurfaceWidth, 1256 mSurfaceHeight); 1257 1258 if (mEffectType == EffectsRecorder.EFFECT_BACKDROPPER && 1259 ((String)mEffectParameter).equals(EFFECT_BG_FROM_GALLERY) ) { 1260 mEffectsRecorder.setEffect(mEffectType, mEffectUriFromGallery); 1261 } else { 1262 mEffectsRecorder.setEffect(mEffectType, mEffectParameter); 1263 } 1264 } 1265 1266 private void initializeEffectsRecording() { 1267 Log.v(TAG, "initializeEffectsRecording"); 1268 1269 Intent intent = getIntent(); 1270 Bundle myExtras = intent.getExtras(); 1271 1272 if (mIsVideoCaptureIntent && myExtras != null) { 1273 Uri saveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT); 1274 if (saveUri != null) { 1275 mVideoFilename = saveUri.toString(); 1276 } else { 1277 mVideoFilename = null; 1278 } 1279 } else { 1280 mVideoFilename = null; 1281 } 1282 1283 // TODO: Timelapse 1284 1285 // Set output file 1286 if (mVideoFilename == null) { 1287 generateVideoFilename(mProfile.fileFormat); 1288 } 1289 mEffectsRecorder.setOutputFile(mVideoFilename); 1290 } 1291 1292 1293 private void releaseMediaRecorder() { 1294 Log.v(TAG, "Releasing media recorder."); 1295 if (mMediaRecorder != null) { 1296 cleanupEmptyFile(); 1297 mMediaRecorder.reset(); 1298 mMediaRecorder.release(); 1299 mMediaRecorder = null; 1300 } 1301 mVideoFilename = null; 1302 if (mVideoFileDescriptor != null) { 1303 try { 1304 mVideoFileDescriptor.close(); 1305 } catch (IOException e) { 1306 Log.e(TAG, "Fail to close fd", e); 1307 } 1308 mVideoFileDescriptor = null; 1309 } 1310 } 1311 1312 private void releaseEffectsRecorder() { 1313 Log.v(TAG, "Releasing effects recorder."); 1314 if (mEffectsRecorder != null) { 1315 cleanupEmptyFile(); 1316 mEffectsRecorder.release(); 1317 mEffectsRecorder = null; 1318 } 1319 mVideoFilename = null; 1320 } 1321 1322 private void generateVideoFilename(int outputFileFormat) { 1323 long dateTaken = System.currentTimeMillis(); 1324 String title = createName(dateTaken); 1325 String filename = title + ".3gp"; // Used when emailing. 1326 String mime = "video/3gpp"; 1327 if (outputFileFormat == MediaRecorder.OutputFormat.MPEG_4) { 1328 filename = title + ".mp4"; 1329 mime = "video/mp4"; 1330 } 1331 mVideoFilename = Storage.DIRECTORY + '/' + filename; 1332 mCurrentVideoValues = new ContentValues(7); 1333 mCurrentVideoValues.put(Video.Media.TITLE, title); 1334 mCurrentVideoValues.put(Video.Media.DISPLAY_NAME, filename); 1335 mCurrentVideoValues.put(Video.Media.DATE_TAKEN, dateTaken); 1336 mCurrentVideoValues.put(Video.Media.MIME_TYPE, mime); 1337 mCurrentVideoValues.put(Video.Media.DATA, mVideoFilename); 1338 Log.v(TAG, "New video filename: " + mVideoFilename); 1339 } 1340 1341 private void addVideoToMediaStore() { 1342 if (mVideoFileDescriptor == null) { 1343 Uri videoTable = Uri.parse("content://media/external/video/media"); 1344 mCurrentVideoValues.put(Video.Media.SIZE, 1345 new File(mCurrentVideoFilename).length()); 1346 long duration = SystemClock.uptimeMillis() - mRecordingStartTime; 1347 if (duration > 0) { 1348 if (mCaptureTimeLapse) { 1349 duration = getTimeLapseVideoLength(duration); 1350 } 1351 mCurrentVideoValues.put(Video.Media.DURATION, duration); 1352 } else { 1353 Log.w(TAG, "Video duration <= 0 : " + duration); 1354 } 1355 try { 1356 mCurrentVideoUri = mContentResolver.insert(videoTable, 1357 mCurrentVideoValues); 1358 sendBroadcast(new Intent(android.hardware.Camera.ACTION_NEW_VIDEO, 1359 mCurrentVideoUri)); 1360 } catch (Exception e) { 1361 // We failed to insert into the database. This can happen if 1362 // the SD card is unmounted. 1363 mCurrentVideoUri = null; 1364 mCurrentVideoFilename = null; 1365 } finally { 1366 Log.v(TAG, "Current video URI: " + mCurrentVideoUri); 1367 } 1368 } 1369 mCurrentVideoValues = null; 1370 } 1371 1372 private void deleteCurrentVideo() { 1373 // Remove the video and the uri if the uri is not passed in by intent. 1374 if (mCurrentVideoFilename != null) { 1375 deleteVideoFile(mCurrentVideoFilename); 1376 mCurrentVideoFilename = null; 1377 if (mCurrentVideoUri != null) { 1378 mContentResolver.delete(mCurrentVideoUri, null, null); 1379 mCurrentVideoUri = null; 1380 } 1381 } 1382 updateAndShowStorageHint(); 1383 } 1384 1385 private void deleteVideoFile(String fileName) { 1386 Log.v(TAG, "Deleting video " + fileName); 1387 File f = new File(fileName); 1388 if (!f.delete()) { 1389 Log.v(TAG, "Could not delete " + fileName); 1390 } 1391 } 1392 1393 private void addBaseMenuItems(Menu menu) { 1394 MenuHelper.addSwitchModeMenuItem(menu, ModePicker.MODE_CAMERA, new Runnable() { 1395 public void run() { 1396 switchToOtherMode(ModePicker.MODE_CAMERA); 1397 } 1398 }); 1399 MenuHelper.addSwitchModeMenuItem(menu, ModePicker.MODE_PANORAMA, new Runnable() { 1400 public void run() { 1401 switchToOtherMode(ModePicker.MODE_PANORAMA); 1402 } 1403 }); 1404 MenuItem gallery = menu.add(R.string.camera_gallery_photos_text) 1405 .setOnMenuItemClickListener( 1406 new OnMenuItemClickListener() { 1407 public boolean onMenuItemClick(MenuItem item) { 1408 gotoGallery(); 1409 return true; 1410 } 1411 }); 1412 gallery.setIcon(android.R.drawable.ic_menu_gallery); 1413 mGalleryItems.add(gallery); 1414 1415 if (mNumberOfCameras > 1) { 1416 menu.add(R.string.switch_camera_id) 1417 .setOnMenuItemClickListener(new OnMenuItemClickListener() { 1418 public boolean onMenuItemClick(MenuItem item) { 1419 CameraSettings.writePreferredCameraId(mPreferences, 1420 ((mCameraId == mFrontCameraId) 1421 ? mBackCameraId : mFrontCameraId)); 1422 onSharedPreferenceChanged(); 1423 return true; 1424 } 1425 }).setIcon(android.R.drawable.ic_menu_camera); 1426 } 1427 } 1428 1429 private PreferenceGroup filterPreferenceScreenByIntent( 1430 PreferenceGroup screen) { 1431 Intent intent = getIntent(); 1432 if (intent.hasExtra(MediaStore.EXTRA_VIDEO_QUALITY)) { 1433 CameraSettings.removePreferenceFromScreen(screen, 1434 CameraSettings.KEY_VIDEO_QUALITY); 1435 } 1436 1437 if (intent.hasExtra(MediaStore.EXTRA_DURATION_LIMIT)) { 1438 CameraSettings.removePreferenceFromScreen(screen, 1439 CameraSettings.KEY_VIDEO_QUALITY); 1440 } 1441 return screen; 1442 } 1443 1444 // from MediaRecorder.OnErrorListener 1445 public void onError(MediaRecorder mr, int what, int extra) { 1446 Log.e(TAG, "MediaRecorder error. what=" + what + ". extra=" + extra); 1447 if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { 1448 // We may have run out of space on the sdcard. 1449 stopVideoRecording(); 1450 updateAndShowStorageHint(); 1451 } 1452 } 1453 1454 // from MediaRecorder.OnInfoListener 1455 public void onInfo(MediaRecorder mr, int what, int extra) { 1456 if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) { 1457 if (mMediaRecorderRecording) onStopVideoRecording(true); 1458 } else if (what 1459 == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) { 1460 if (mMediaRecorderRecording) onStopVideoRecording(true); 1461 1462 // Show the toast. 1463 Toast.makeText(VideoCamera.this, R.string.video_reach_size_limit, 1464 Toast.LENGTH_LONG).show(); 1465 } 1466 } 1467 1468 /* 1469 * Make sure we're not recording music playing in the background, ask the 1470 * MediaPlaybackService to pause playback. 1471 */ 1472 private void pauseAudioPlayback() { 1473 // Shamelessly copied from MediaPlaybackService.java, which 1474 // should be public, but isn't. 1475 Intent i = new Intent("com.android.music.musicservicecommand"); 1476 i.putExtra("command", "pause"); 1477 1478 sendBroadcast(i); 1479 } 1480 1481 // For testing. 1482 public boolean isRecording() { 1483 return mMediaRecorderRecording; 1484 } 1485 1486 private void startVideoRecording() { 1487 Log.v(TAG, "startVideoRecording"); 1488 1489 updateAndShowStorageHint(); 1490 if (mStorageSpace < LOW_STORAGE_THRESHOLD) { 1491 Log.v(TAG, "Storage issue, ignore the start request"); 1492 return; 1493 } 1494 1495 if (effectsActive()) { 1496 initializeEffectsRecording(); 1497 if (mEffectsRecorder == null) { 1498 Log.e(TAG, "Fail to initialize effect recorder"); 1499 return; 1500 } 1501 } else { 1502 initializeRecorder(); 1503 if (mMediaRecorder == null) { 1504 Log.e(TAG, "Fail to initialize media recorder"); 1505 return; 1506 } 1507 } 1508 1509 pauseAudioPlayback(); 1510 1511 if (effectsActive()) { 1512 try { 1513 mEffectsRecorder.startRecording(); 1514 } catch (RuntimeException e) { 1515 Log.e(TAG, "Could not start effects recorder. ", e); 1516 releaseEffectsRecorder(); 1517 return; 1518 } 1519 } else { 1520 try { 1521 mMediaRecorder.start(); // Recording is now started 1522 } catch (RuntimeException e) { 1523 Log.e(TAG, "Could not start media recorder. ", e); 1524 releaseMediaRecorder(); 1525 // If start fails, frameworks will not lock the camera for us. 1526 mCameraDevice.lock(); 1527 return; 1528 } 1529 } 1530 1531 enableCameraControls(false); 1532 1533 mMediaRecorderRecording = true; 1534 mRecordingStartTime = SystemClock.uptimeMillis(); 1535 showRecordingUI(true); 1536 1537 updateRecordingTime(); 1538 keepScreenOn(); 1539 } 1540 1541 private void showRecordingUI(boolean recording) { 1542 if (recording) { 1543 mShutterButton.setBackgroundResource(R.drawable.btn_shutter_video_recording); 1544 mRecordingTimeView.setText(""); 1545 mRecordingTimeView.setVisibility(View.VISIBLE); 1546 if (mReviewControl != null) mReviewControl.setVisibility(View.GONE); 1547 if (mCaptureTimeLapse) { 1548 if (Util.isTabletUI()) { 1549 ((IndicatorControlWheelContainer) mIndicatorControlContainer) 1550 .startTimeLapseAnimation( 1551 mTimeBetweenTimeLapseFrameCaptureMs, 1552 mRecordingStartTime); 1553 } 1554 } 1555 } else { 1556 mShutterButton.setBackgroundResource(R.drawable.btn_shutter_video); 1557 mRecordingTimeView.setVisibility(View.GONE); 1558 if (mReviewControl != null) mReviewControl.setVisibility(View.VISIBLE); 1559 if (mCaptureTimeLapse) { 1560 if (Util.isTabletUI()) { 1561 ((IndicatorControlWheelContainer) mIndicatorControlContainer) 1562 .stopTimeLapseAnimation(); 1563 } 1564 } 1565 } 1566 } 1567 1568 private void getThumbnail() { 1569 if (mCurrentVideoUri != null) { 1570 Bitmap videoFrame = Thumbnail.createVideoThumbnail(mCurrentVideoFilename, 1571 mPreviewFrameLayout.getWidth()); 1572 if (videoFrame != null) { 1573 mThumbnail = new Thumbnail(mCurrentVideoUri, videoFrame, 0); 1574 mThumbnailView.setBitmap(mThumbnail.getBitmap()); 1575 } 1576 } 1577 } 1578 1579 private void showAlert() { 1580 if (mCurrentVideoFilename != null) { 1581 Bitmap bitmap = Thumbnail.createVideoThumbnail(mCurrentVideoFilename, 1582 mPreviewFrameLayout.getWidth()); 1583 if (bitmap != null) { 1584 // MetadataRetriever already rotates the thumbnail. We should rotate 1585 // it back (and mirror if it is front-facing camera). 1586 CameraInfo[] info = CameraHolder.instance().getCameraInfo(); 1587 if (info[mCameraId].facing == CameraInfo.CAMERA_FACING_BACK) { 1588 bitmap = Util.rotateAndMirror(bitmap, -mOrientationHint, false); 1589 } else { 1590 bitmap = Util.rotateAndMirror(bitmap, -mOrientationHint, true); 1591 } 1592 mReviewImage.setImageBitmap(bitmap); 1593 mReviewImage.setVisibility(View.VISIBLE); 1594 } 1595 } 1596 1597 mShutterButton.setVisibility(View.GONE); 1598 mIndicatorControlContainer.setVisibility(View.GONE); 1599 int[] pickIds = {R.id.btn_retake, R.id.btn_done, R.id.btn_play}; 1600 for (int id : pickIds) { 1601 Util.fadeIn(findViewById(id)); 1602 } 1603 } 1604 1605 private void hideAlert() { 1606 mReviewImage.setVisibility(View.INVISIBLE); 1607 mShutterButton.setEnabled(true); 1608 enableCameraControls(true); 1609 1610 int[] pickIds = {R.id.btn_retake, R.id.btn_done, R.id.btn_play}; 1611 for (int id : pickIds) { 1612 (findViewById(id)).setVisibility(View.GONE); 1613 } 1614 Util.fadeIn(mShutterButton); 1615 Util.fadeIn(mIndicatorControlContainer); 1616 1617 if (mCaptureTimeLapse) { 1618 showTimeLapseUI(true); 1619 } 1620 } 1621 1622 private boolean isAlertVisible() { 1623 return this.mReviewImage.getVisibility() == View.VISIBLE; 1624 } 1625 1626 private void stopVideoRecording() { 1627 Log.v(TAG, "stopVideoRecording"); 1628 if (mMediaRecorderRecording) { 1629 boolean shouldAddToMediaStore = false; 1630 1631 try { 1632 if (effectsActive()) { 1633 mEffectsRecorder.stopRecording(); 1634 } else { 1635 mMediaRecorder.setOnErrorListener(null); 1636 mMediaRecorder.setOnInfoListener(null); 1637 mMediaRecorder.stop(); 1638 } 1639 mCurrentVideoFilename = mVideoFilename; 1640 Log.v(TAG, "Setting current video filename: " 1641 + mCurrentVideoFilename); 1642 shouldAddToMediaStore = true; 1643 } catch (RuntimeException e) { 1644 Log.e(TAG, "stop fail", e); 1645 if (mVideoFilename != null) deleteVideoFile(mVideoFilename); 1646 } 1647 1648 mMediaRecorderRecording = false; 1649 showRecordingUI(false); 1650 if (!mIsVideoCaptureIntent) { 1651 enableCameraControls(true); 1652 } 1653 keepScreenOnAwhile(); 1654 if (shouldAddToMediaStore && mStorageSpace >= LOW_STORAGE_THRESHOLD) { 1655 addVideoToMediaStore(); 1656 } 1657 } 1658 // always release media recorder 1659 if (!effectsActive()) { 1660 releaseMediaRecorder(); 1661 } 1662 } 1663 1664 private void resetScreenOn() { 1665 mHandler.removeMessages(CLEAR_SCREEN_DELAY); 1666 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 1667 } 1668 1669 private void keepScreenOnAwhile() { 1670 mHandler.removeMessages(CLEAR_SCREEN_DELAY); 1671 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 1672 mHandler.sendEmptyMessageDelayed(CLEAR_SCREEN_DELAY, SCREEN_DELAY); 1673 } 1674 1675 private void keepScreenOn() { 1676 mHandler.removeMessages(CLEAR_SCREEN_DELAY); 1677 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 1678 } 1679 1680 private void initThumbnailButton() { 1681 mThumbnailView = (RotateImageView) findViewById(R.id.thumbnail); 1682 mThumbnailView.setVisibility(View.VISIBLE); 1683 // Load the thumbnail from the disk. 1684 mThumbnail = Thumbnail.loadFrom(new File(getFilesDir(), LAST_THUMB_FILENAME)); 1685 } 1686 1687 private void updateThumbnailButton() { 1688 if (mThumbnail == null || !Util.isUriValid(mThumbnail.getUri(), mContentResolver)) { 1689 mThumbnail = Thumbnail.getLastVideoThumbnail(mContentResolver); 1690 } 1691 if (mThumbnail != null) { 1692 mThumbnailView.setBitmap(mThumbnail.getBitmap()); 1693 } else { 1694 mThumbnailView.setBitmap(null); 1695 } 1696 } 1697 1698 private static String millisecondToTimeString(long milliSeconds, boolean displayCentiSeconds) { 1699 long seconds = milliSeconds / 1000; // round down to compute seconds 1700 long minutes = seconds / 60; 1701 long hours = minutes / 60; 1702 long remainderMinutes = minutes - (hours * 60); 1703 long remainderSeconds = seconds - (minutes * 60); 1704 1705 StringBuilder timeStringBuilder = new StringBuilder(); 1706 1707 // Hours 1708 if (hours > 0) { 1709 if (hours < 10) { 1710 timeStringBuilder.append('0'); 1711 } 1712 timeStringBuilder.append(hours); 1713 1714 timeStringBuilder.append(':'); 1715 } 1716 1717 // Minutes 1718 if (remainderMinutes < 10) { 1719 timeStringBuilder.append('0'); 1720 } 1721 timeStringBuilder.append(remainderMinutes); 1722 timeStringBuilder.append(':'); 1723 1724 // Seconds 1725 if (remainderSeconds < 10) { 1726 timeStringBuilder.append('0'); 1727 } 1728 timeStringBuilder.append(remainderSeconds); 1729 1730 // Centi seconds 1731 if (displayCentiSeconds) { 1732 timeStringBuilder.append('.'); 1733 long remainderCentiSeconds = (milliSeconds - seconds * 1000) / 10; 1734 if (remainderCentiSeconds < 10) { 1735 timeStringBuilder.append('0'); 1736 } 1737 timeStringBuilder.append(remainderCentiSeconds); 1738 } 1739 1740 return timeStringBuilder.toString(); 1741 } 1742 1743 private long getTimeLapseVideoLength(long deltaMs) { 1744 // For better approximation calculate fractional number of frames captured. 1745 // This will update the video time at a higher resolution. 1746 double numberOfFrames = (double) deltaMs / mTimeBetweenTimeLapseFrameCaptureMs; 1747 return (long) (numberOfFrames / mProfile.videoFrameRate * 1000); 1748 } 1749 1750 private void updateRecordingTime() { 1751 if (!mMediaRecorderRecording) { 1752 return; 1753 } 1754 long now = SystemClock.uptimeMillis(); 1755 long delta = now - mRecordingStartTime; 1756 1757 // Starting a minute before reaching the max duration 1758 // limit, we'll countdown the remaining time instead. 1759 boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0 1760 && delta >= mMaxVideoDurationInMs - 60000); 1761 1762 long deltaAdjusted = delta; 1763 if (countdownRemainingTime) { 1764 deltaAdjusted = Math.max(0, mMaxVideoDurationInMs - deltaAdjusted) + 999; 1765 } 1766 String text; 1767 1768 long targetNextUpdateDelay; 1769 if (!mCaptureTimeLapse) { 1770 text = millisecondToTimeString(deltaAdjusted, false); 1771 targetNextUpdateDelay = 1000; 1772 } else { 1773 // The length of time lapse video is different from the length 1774 // of the actual wall clock time elapsed. Display the video length 1775 // only in format hh:mm:ss.dd, where dd are the centi seconds. 1776 text = millisecondToTimeString(getTimeLapseVideoLength(delta), true); 1777 targetNextUpdateDelay = mTimeBetweenTimeLapseFrameCaptureMs; 1778 } 1779 1780 mRecordingTimeView.setText(text); 1781 1782 if (mRecordingTimeCountsDown != countdownRemainingTime) { 1783 // Avoid setting the color on every update, do it only 1784 // when it needs changing. 1785 mRecordingTimeCountsDown = countdownRemainingTime; 1786 1787 int color = getResources().getColor(countdownRemainingTime 1788 ? R.color.recording_time_remaining_text 1789 : R.color.recording_time_elapsed_text); 1790 1791 mRecordingTimeView.setTextColor(color); 1792 } 1793 1794 long actualNextUpdateDelay = targetNextUpdateDelay - (delta % targetNextUpdateDelay); 1795 mHandler.sendEmptyMessageDelayed( 1796 UPDATE_RECORD_TIME, actualNextUpdateDelay); 1797 } 1798 1799 private static boolean isSupported(String value, List<String> supported) { 1800 return supported == null ? false : supported.indexOf(value) >= 0; 1801 } 1802 1803 private void setCameraParameters() { 1804 mParameters = mCameraDevice.getParameters(); 1805 1806 mParameters.setPreviewSize(mDesiredPreviewWidth, mDesiredPreviewHeight); 1807 mParameters.setPreviewFrameRate(mProfile.videoFrameRate); 1808 1809 // Set flash mode. 1810 String flashMode = mPreferences.getString( 1811 CameraSettings.KEY_VIDEOCAMERA_FLASH_MODE, 1812 getString(R.string.pref_camera_video_flashmode_default)); 1813 List<String> supportedFlash = mParameters.getSupportedFlashModes(); 1814 if (isSupported(flashMode, supportedFlash)) { 1815 mParameters.setFlashMode(flashMode); 1816 } else { 1817 flashMode = mParameters.getFlashMode(); 1818 if (flashMode == null) { 1819 flashMode = getString( 1820 R.string.pref_camera_flashmode_no_flash); 1821 } 1822 } 1823 1824 // Set white balance parameter. 1825 String whiteBalance = mPreferences.getString( 1826 CameraSettings.KEY_WHITE_BALANCE, 1827 getString(R.string.pref_camera_whitebalance_default)); 1828 if (isSupported(whiteBalance, 1829 mParameters.getSupportedWhiteBalance())) { 1830 mParameters.setWhiteBalance(whiteBalance); 1831 } else { 1832 whiteBalance = mParameters.getWhiteBalance(); 1833 if (whiteBalance == null) { 1834 whiteBalance = Parameters.WHITE_BALANCE_AUTO; 1835 } 1836 } 1837 1838 // Set zoom. 1839 if (mParameters.isZoomSupported()) { 1840 mParameters.setZoom(mZoomValue); 1841 } 1842 1843 // Set continuous autofocus. 1844 List<String> supportedFocus = mParameters.getSupportedFocusModes(); 1845 if (isSupported(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO, supportedFocus)) { 1846 mParameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); 1847 } 1848 1849 mParameters.setRecordingHint(true); 1850 1851 // Set JPEG quality. 1852 int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId, 1853 CameraProfile.QUALITY_HIGH); 1854 mParameters.setJpegQuality(jpegQuality); 1855 1856 mCameraDevice.setParameters(mParameters); 1857 // Keep preview size up to date. 1858 mParameters = mCameraDevice.getParameters(); 1859 } 1860 1861 private boolean switchToOtherMode(int mode) { 1862 if (isFinishing() || mMediaRecorderRecording) return false; 1863 MenuHelper.gotoMode(mode, VideoCamera.this); 1864 finish(); 1865 return true; 1866 } 1867 1868 public boolean onModeChanged(int mode) { 1869 if (mode != ModePicker.MODE_VIDEO) { 1870 return switchToOtherMode(mode); 1871 } else { 1872 return true; 1873 } 1874 } 1875 1876 @Override 1877 public void onActivityResult(int requestCode, int resultCode, Intent data) { 1878 switch (requestCode) { 1879 case EffectsRecorder.EFFECT_BACKDROPPER: 1880 if (resultCode == RESULT_OK) { 1881 // onActivityResult() runs before onResume(), so this parameter will be 1882 // seen by startPreview from onResume() 1883 mEffectUriFromGallery = ((Uri)data.getData()).toString(); 1884 Log.v(TAG, "Received URI from gallery: " + mEffectUriFromGallery); 1885 } 1886 break; 1887 default: 1888 Log.e(TAG, "Unknown activity result sent to Camera!"); 1889 break; 1890 } 1891 } 1892 1893 public void onEffectsUpdate(int effectId, int effectMsg) { 1894 if (effectId == EffectsRecorder.EFFECT_BACKDROPPER) { 1895 switch (effectMsg) { 1896 case EffectsRecorder.EFFECT_MSG_STARTED_LEARNING: 1897 mBgLearningMessage.setVisibility(View.VISIBLE); 1898 break; 1899 case EffectsRecorder.EFFECT_MSG_DONE_LEARNING: 1900 case EffectsRecorder.EFFECT_MSG_STOPPING_EFFECT: 1901 mBgLearningMessage.setVisibility(View.GONE); 1902 break; 1903 } 1904 } 1905 } 1906 1907 @Override 1908 public void onConfigurationChanged(Configuration config) { 1909 super.onConfigurationChanged(config); 1910 } 1911 1912 public void onOverriddenPreferencesClicked() { 1913 } 1914 1915 public void onRestorePreferencesClicked() { 1916 Runnable runnable = new Runnable() { 1917 public void run() { 1918 restorePreferences(); 1919 } 1920 }; 1921 MenuHelper.confirmAction(this, 1922 getString(R.string.confirm_restore_title), 1923 getString(R.string.confirm_restore_message), 1924 runnable); 1925 } 1926 1927 private void restorePreferences() { 1928 // Reset the zoom. Zoom value is not stored in preference. 1929 if (mParameters.isZoomSupported()) { 1930 mZoomValue = 0; 1931 setCameraParameters(); 1932 mZoomControl.setZoomIndex(0); 1933 } 1934 1935 if (mIndicatorControlContainer != null) { 1936 mIndicatorControlContainer.dismissSettingPopup(); 1937 CameraSettings.restorePreferences(VideoCamera.this, mPreferences, 1938 mParameters); 1939 mIndicatorControlContainer.reloadPreferences(); 1940 onSharedPreferenceChanged(); 1941 } 1942 } 1943 1944 private boolean effectsActive() { 1945 return (mEffectType != EffectsRecorder.EFFECT_NONE); 1946 } 1947 1948 public void onSharedPreferenceChanged() { 1949 // ignore the events after "onPause()" or preview has not started yet 1950 if (mPausing) return; 1951 synchronized (mPreferences) { 1952 // If mCameraDevice is not ready then we can set the parameter in 1953 // startPreview(). 1954 if (mCameraDevice == null) return; 1955 1956 boolean recordLocation = RecordLocationPreference.get( 1957 mPreferences, getContentResolver()); 1958 mLocationManager.recordLocation(recordLocation); 1959 1960 // Check if the current effects selection has changed 1961 if (updateEffectSelection()) return; 1962 1963 // Check if camera id is changed. 1964 int cameraId = CameraSettings.readPreferredCameraId(mPreferences); 1965 if (mCameraId != cameraId) { 1966 // Restart the activity to have a crossfade animation. 1967 // TODO: Use SurfaceTexture to implement a better and faster 1968 // animation. 1969 if (mIsVideoCaptureIntent) { 1970 // If the intent is video capture, stay in video capture mode. 1971 MenuHelper.gotoVideoMode(this, getIntent()); 1972 } else { 1973 MenuHelper.gotoVideoMode(this); 1974 } 1975 finish(); 1976 } else { 1977 readVideoPreferences(); 1978 // We need to restart the preview if preview size is changed. 1979 Size size = mParameters.getPreviewSize(); 1980 if (size.width != mDesiredPreviewWidth 1981 || size.height != mDesiredPreviewHeight) { 1982 if (!effectsActive()) { 1983 mCameraDevice.stopPreview(); 1984 } else { 1985 mEffectsRecorder.release(); 1986 } 1987 resizeForPreviewAspectRatio(); 1988 startPreview(); // Parameters will be set in startPreview(). 1989 } else { 1990 setCameraParameters(); 1991 } 1992 } 1993 showTimeLapseUI(mCaptureTimeLapse); 1994 } 1995 } 1996 1997 private boolean updateEffectSelection() { 1998 int currentEffectType = mEffectType; 1999 Object currentEffectParameter = mEffectParameter; 2000 mEffectType = CameraSettings.readEffectType(mPreferences); 2001 mEffectParameter = CameraSettings.readEffectParameter(mPreferences); 2002 2003 if (mEffectType == currentEffectType) { 2004 if (mEffectType == EffectsRecorder.EFFECT_NONE) return false; 2005 if (mEffectParameter.equals(currentEffectParameter)) return false; 2006 } 2007 Log.v(TAG, "New effect selection: " + mPreferences.getString(CameraSettings.KEY_VIDEO_EFFECT, "none") ); 2008 2009 if ( mEffectType == EffectsRecorder.EFFECT_NONE ) { 2010 // Stop effects and return to normal preview 2011 mEffectsRecorder.stopPreview(); 2012 return true; 2013 } 2014 if (mEffectType == EffectsRecorder.EFFECT_BACKDROPPER && 2015 ((String)mEffectParameter).equals(EFFECT_BG_FROM_GALLERY)) { 2016 // Request video from gallery to use for background 2017 Intent i = new Intent(Intent.ACTION_PICK); 2018 i.setDataAndType(Video.Media.EXTERNAL_CONTENT_URI, 2019 "video/*"); 2020 startActivityForResult(i, EffectsRecorder.EFFECT_BACKDROPPER); 2021 return true; 2022 } 2023 if (currentEffectType == EffectsRecorder.EFFECT_NONE) { 2024 // Start up effects 2025 startPreview(); 2026 } else { 2027 // Switch currently running effect 2028 mEffectsRecorder.setEffect(mEffectType, mEffectParameter); 2029 } 2030 return true; 2031 } 2032 2033 private void showTimeLapseUI(boolean enable) { 2034 if (mTimeLapseLabel != null) { 2035 mTimeLapseLabel.setVisibility(enable ? View.VISIBLE : View.GONE); 2036 } 2037 if (mPreviewBorder != null) { 2038 mPreviewBorder.setBackgroundResource(enable 2039 ? R.drawable.border_preview_time_lapse 2040 : R.drawable.border_preview); 2041 } 2042 2043 } 2044 2045 private void showSharePopup() { 2046 Uri uri = mThumbnail.getUri(); 2047 if (mSharePopup == null || !uri.equals(mSharePopup.getUri())) { 2048 mSharePopup = new SharePopup(this, uri, mThumbnail.getBitmap(), 2049 mOrientationCompensation, mPreviewFrameLayout); 2050 } 2051 mSharePopup.showAtLocation(mThumbnailView, Gravity.NO_GRAVITY, 0, 0); 2052 } 2053 2054 @Override 2055 public boolean dispatchTouchEvent(MotionEvent m) { 2056 // Check if the popup window should be dismissed first. 2057 if (mPopupGestureDetector != null && mPopupGestureDetector.onTouchEvent(m)) { 2058 return true; 2059 } 2060 2061 return super.dispatchTouchEvent(m); 2062 } 2063 2064 private class PopupGestureListener extends 2065 GestureDetector.SimpleOnGestureListener { 2066 @Override 2067 public boolean onDown(MotionEvent e) { 2068 // Check if the popup window is visible. 2069 View popup = mIndicatorControlContainer.getActiveSettingPopup(); 2070 if (popup == null) return false; 2071 2072 // Let popup window or indicator wheel handle the event by 2073 // themselves. Dismiss the popup window if users touch on other 2074 // areas. 2075 if (!Util.pointInView(e.getX(), e.getY(), popup) 2076 && !Util.pointInView(e.getX(), e.getY(), mIndicatorControlContainer)) { 2077 mIndicatorControlContainer.dismissSettingPopup(); 2078 // Let event fall through. 2079 } 2080 return false; 2081 } 2082 } 2083 2084 private class ZoomChangeListener implements ZoomControl.OnZoomChangedListener { 2085 // only for immediate zoom 2086 @Override 2087 public void onZoomValueChanged(int index) { 2088 VideoCamera.this.onZoomValueChanged(index); 2089 } 2090 2091 // only for smooth zoom 2092 @Override 2093 public void onZoomStateChanged(int state) { 2094 if (mPausing) return; 2095 2096 Log.v(TAG, "zoom picker state=" + state); 2097 if (state == ZoomControl.ZOOM_IN) { 2098 VideoCamera.this.onZoomValueChanged(mZoomMax); 2099 } else if (state == ZoomControl.ZOOM_OUT){ 2100 VideoCamera.this.onZoomValueChanged(0); 2101 } else { 2102 mTargetZoomValue = -1; 2103 if (mZoomState == ZOOM_START) { 2104 mZoomState = ZOOM_STOPPING; 2105 mCameraDevice.stopSmoothZoom(); 2106 } 2107 } 2108 } 2109 } 2110 2111 private void initializeZoomControl() { 2112 mZoomControl = (ZoomControl) findViewById(R.id.zoom_control); 2113 if (!mParameters.isZoomSupported()) return; 2114 2115 mZoomMax = mParameters.getMaxZoom(); 2116 mSmoothZoomSupported = mParameters.isSmoothZoomSupported(); 2117 mZoomControl.setZoomMax(mZoomMax); 2118 mZoomControl.setZoomIndex(mParameters.getZoom()); 2119 mZoomControl.setSmoothZoomSupported(mSmoothZoomSupported); 2120 mZoomControl.setOnZoomChangeListener(new ZoomChangeListener() ); 2121 mCameraDevice.setZoomChangeListener(mZoomListener); 2122 } 2123 2124 private final class ZoomListener 2125 implements android.hardware.Camera.OnZoomChangeListener { 2126 @Override 2127 public void onZoomChange(int value, boolean stopped, android.hardware.Camera camera) { 2128 Log.v(TAG, "Zoom changed: value=" + value + ". stopped="+ stopped); 2129 mZoomValue = value; 2130 2131 // Update the UI when we get zoom value. 2132 mZoomControl.setZoomIndex(value); 2133 2134 // Keep mParameters up to date. We do not getParameter again in 2135 // takePicture. If we do not do this, wrong zoom value will be set. 2136 mParameters.setZoom(value); 2137 2138 if (stopped && mZoomState != ZOOM_STOPPED) { 2139 if (mTargetZoomValue != -1 && value != mTargetZoomValue) { 2140 mCameraDevice.startSmoothZoom(mTargetZoomValue); 2141 mZoomState = ZOOM_START; 2142 } else { 2143 mZoomState = ZOOM_STOPPED; 2144 } 2145 } 2146 } 2147 } 2148 2149 private void onZoomValueChanged(int index) { 2150 // Not useful to change zoom value when the activity is paused. 2151 if (mPausing) return; 2152 2153 if (mSmoothZoomSupported) { 2154 if (mTargetZoomValue != index && mZoomState != ZOOM_STOPPED) { 2155 mTargetZoomValue = index; 2156 if (mZoomState == ZOOM_START) { 2157 mZoomState = ZOOM_STOPPING; 2158 mCameraDevice.stopSmoothZoom(); 2159 } 2160 } else if (mZoomState == ZOOM_STOPPED && mZoomValue != index) { 2161 mTargetZoomValue = index; 2162 mCameraDevice.startSmoothZoom(index); 2163 mZoomState = ZOOM_START; 2164 } 2165 } else { 2166 mZoomValue = index; 2167 setCameraParameters(); 2168 } 2169 } 2170 2171 // Preview area is touched. Take a picture. 2172 @Override 2173 public boolean onTouch(View v, MotionEvent e) { 2174 if (mPausing || mSnapshotInProgress || !mMediaRecorderRecording) { 2175 return false; 2176 } 2177 2178 // Set rotation and gps data. 2179 Util.setRotationParameter(mParameters, mCameraId, mOrientation); 2180 Location loc = mLocationManager.getCurrentLocation(); 2181 Util.setGpsParameters(mParameters, loc); 2182 mCameraDevice.setParameters(mParameters); 2183 2184 Log.v(TAG, "Video snapshot start"); 2185 mCameraDevice.takePicture(null, null, null, new JpegPictureCallback(loc)); 2186 mSnapshotInProgress = true; 2187 return true; 2188 } 2189 2190 private final class JpegPictureCallback implements PictureCallback { 2191 Location mLocation; 2192 2193 public JpegPictureCallback(Location loc) { 2194 mLocation = loc; 2195 } 2196 2197 @Override 2198 public void onPictureTaken(byte [] jpegData, android.hardware.Camera camera) { 2199 Log.v(TAG, "onPictureTaken"); 2200 mSnapshotInProgress = false; 2201 storeImage(jpegData, mLocation); 2202 } 2203 } 2204 2205 private void storeImage(final byte[] data, Location loc) { 2206 long dateTaken = System.currentTimeMillis(); 2207 String title = Util.createJpegName(dateTaken); 2208 int orientation = Exif.getOrientation(data); 2209 Uri uri = Storage.addImage(mContentResolver, title, dateTaken, 2210 loc, orientation, data); 2211 if (uri != null) { 2212 // Create a thumbnail whose width is equal or bigger than that of the preview. 2213 int ratio = (int) Math.ceil((double) mParameters.getPictureSize().width 2214 / mPreviewFrameLayout.getWidth()); 2215 int inSampleSize = Integer.highestOneBit(ratio); 2216 mThumbnail = Thumbnail.createThumbnail(data, orientation, inSampleSize, uri); 2217 if (mThumbnail != null) { 2218 mThumbnailView.setBitmap(mThumbnail.getBitmap()); 2219 } 2220 Util.broadcastNewPicture(this, uri); 2221 } 2222 } 2223} 2224