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