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