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