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