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