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