summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/CaptureModule.java
blob: 0ade6b1643239d24916c02dc0de22d2f26cfd234 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnLayoutChangeListener;

import com.android.camera.app.AppController;
import com.android.camera.app.CameraAppUI;
import com.android.camera.app.CameraAppUI.BottomBarUISpec;
import com.android.camera.app.MediaSaver;
import com.android.camera.debug.Log;
import com.android.camera.debug.Log.Tag;
import com.android.camera.hardware.HardwareSpec;
import com.android.camera.module.ModuleController;
import com.android.camera.one.OneCamera;
import com.android.camera.one.OneCamera.CaptureReadyCallback;
import com.android.camera.one.OneCamera.Facing;
import com.android.camera.one.OneCamera.OpenCallback;
import com.android.camera.one.OneCamera.PhotoCaptureParameters;
import com.android.camera.one.OneCamera.PhotoCaptureParameters.Flash;
import com.android.camera.one.OneCameraManager;
import com.android.camera.remote.RemoteCameraModule;
import com.android.camera.session.CaptureSession;
import com.android.camera.settings.Keys;
import com.android.camera.settings.ResolutionUtil;
import com.android.camera.settings.SettingsManager;
import com.android.camera.ui.PreviewStatusListener;
import com.android.camera.ui.TouchCoordinate;
import com.android.camera.util.CameraUtil;
import com.android.camera.util.Size;
import com.android.camera2.R;
import com.android.ex.camera2.portability.CameraAgent.CameraProxy;

/**
 * New Capture module that is made to support photo and video capture on top of
 * the OneCamera API, to transparently support GCam.
 * <p>
 * This has been a re-write with pieces taken and improved from GCamModule and
 * PhotoModule, which are to be retired eventually.
 * <p>
 * TODO:
 * <ul>
 * <li>Server-side logging
 * <li>Focusing
 * <li>Show location dialog
 * <li>Show resolution dialog on certain devices
 * <li>Store location
 * <li>Timer
 * <li>Capture intent
 * </ul>
 */
public class CaptureModule extends CameraModule
        implements MediaSaver.QueueListener,
        ModuleController,
        OneCamera.PictureCallback,
        PreviewStatusListener.PreviewAreaChangedListener,
        RemoteCameraModule,
        SensorEventListener,
        SettingsManager.OnSettingChangedListener,
        TextureView.SurfaceTextureListener {

    /**
     * Called on layout changes.
     */
    private final OnLayoutChangeListener mLayoutListener = new OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
                int oldTop, int oldRight, int oldBottom) {
            int width = right - left;
            int height = bottom - top;
            updatePreviewTransform(width, height, false);
        }
    };

    /**
     * Called when the captured media has been saved.
     */
    private final MediaSaver.OnMediaSavedListener mOnMediaSavedListener =
            new MediaSaver.OnMediaSavedListener() {
                @Override
                public void onMediaSaved(Uri uri) {
                    if (uri != null) {
                        mAppController.notifyNewMedia(uri);
                    }
                }
            };

    /**
     * Called when the user pressed the back/front camera switch button.
     */
    private final ButtonManager.ButtonCallback mCameraSwitchCallback =
            new ButtonManager.ButtonCallback() {
                @Override
                public void onStateChanged(int cameraId) {
                    // At the time this callback is fired, the camera id
                    // has be set to the desired camera.
                    if (mPaused) {
                        return;
                    }

                    mSettingsManager.set(mAppController.getModuleScope(), Keys.KEY_CAMERA_ID,
                            cameraId);

                    Log.d(TAG, "Start to switch camera. cameraId=" + cameraId);
                    switchCamera(getFacingFromCameraId(cameraId));
                }
            };

    private static final Tag TAG = new Tag("CaptureModule");
    private static final String PHOTO_MODULE_STRING_ID = "PhotoModule";
    /** Enable additional debug output. */
    private static final boolean DEBUG = true;
    /**
     * This is the delay before we execute onResume tasks when coming from the
     * lock screen, to allow time for onPause to execute.
     * <p>
     * TODO: Make sure this value is in sync with what we see on L.
     */
    private static final int ON_RESUME_TASKS_DELAY_MSEC = 20;

    private final Object mDimensionLock = new Object();
    /**
     * Lock for race conditions in the SurfaceTextureListener callbacks.
     */
    private final Object mSurfaceLock = new Object();
    /** Controller giving us access to other services. */
    private final AppController mAppController;
    /** The applications settings manager. */
    private final SettingsManager mSettingsManager;
    /** Application context. */
    private final Context mContext;
    private CaptureModuleUI mUI;
    /** Your standard content resolver. */
    private ContentResolver mContentResolver;
    /** The camera manager used to open cameras. */
    private OneCameraManager mCameraManager;
    /** The currently opened camera device. */
    private OneCamera mCamera;
    /** The direction the currently opened camera is facing to. */
    private Facing mCameraFacing = Facing.BACK;
    /** The texture used to render the preview in. */
    private SurfaceTexture mPreviewTexture;

    /** State by the module state machine. */
    private static enum ModuleState {
        IDLE,
        WATCH_FOR_NEXT_FRAME_AFTER_PREVIEW_STARTED,
        UPDATE_TRANSFORM_ON_NEXT_SURFACE_TEXTURE_UPDATE,
    }

    /** The current state of the module. */
    private ModuleState mState = ModuleState.IDLE;
    /** Current orientation of the device. */
    private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;

    /** Accelerometer data. */
    private final float[] mGData = new float[3];
    /** Magnetic sensor data. */
    private final float[] mMData = new float[3];
    /** Temporary rotation matrix. */
    private final float[] mR = new float[16];
    /** Current compass heading. */
    private int mHeading = -1;

    /** Whether the module is paused right now. */
    private boolean mPaused;

    /** Whether this module was resumed from lockscreen capture intent. */
    private boolean mIsResumeFromLockScreen = false;

    private final Runnable mResumeTaskRunnable = new Runnable() {
        @Override
        public void run() {
            onResumeTasks();
        }
    };

    /** Main thread handler. */
    private Handler mMainHandler;

    /** Current display rotation in degrees. */
    private int mDisplayRotation;
    /** Current width of the screen, in pixels. */
    private int mScreenWidth;
    /** Current height of the screen, in pixels. */
    private int mScreenHeight;
    /** Current preview width, in pixels. */
    private int mPreviewBufferWidth;
    /** Current preview height, in pixels. */
    private int mPreviewBufferHeight;

    // /** Current preview area width. */
    // private float mFullPreviewWidth;
    // /** Current preview area height. */
    // private float mFullPreviewHeight;

    /** The current preview transformation matrix. */
    private Matrix mPreviewTranformationMatrix = new Matrix();
    /** TODO: This is N5 specific. */
    public static final float FULLSCREEN_ASPECT_RATIO = 16 / 9f;

    /**
     * Desires aspect ratio of the final image.
     * <p>
     * TODO: Can't we deduct this from the final image's resolution?
     */
    private Float mFinalAspectRatio;

    /** CLEAN UP START */
    // private SoundPool mSoundPool;
    // private int mCaptureStartSoundId;
    // private static final int NO_SOUND_STREAM = -999;
    // private final int mCaptureStartSoundStreamId = NO_SOUND_STREAM;
    // private int mCaptureDoneSoundId;
    // private SoundClips.Player mSoundPlayer;
    // private boolean mFirstLayout;
    // private int[] mTargetFPSRanges;
    // private float mZoomValue;
    // private int mSensorOrientation;
    // private int mLensFacing;
    // private volatile float mMaxZoomRatio = 1.0f;
    // private String mFlashMode;
    /** CLEAN UP END */

    /** Constructs a new capture module. */
    public CaptureModule(AppController appController) {
        super(appController);
        mAppController = appController;
        mContext = mAppController.getAndroidContext();
        mSettingsManager = mAppController.getSettingsManager();
        mSettingsManager.addListener(this);
    }

    @Override
    public void init(CameraActivity activity, boolean isSecureCamera, boolean isCaptureIntent) {
        Log.d(TAG, "init");
        mIsResumeFromLockScreen = isResumeFromLockscreen(activity);
        mMainHandler = new Handler(activity.getMainLooper());
        mCameraManager = mAppController.getCameraManager();
        mContentResolver = activity.getContentResolver();
        mDisplayRotation = CameraUtil.getDisplayRotation(mContext);
        mCameraFacing = getFacingFromCameraId(mSettingsManager.getInteger(
                mAppController.getModuleScope(),
                Keys.KEY_CAMERA_ID));
        mUI = new CaptureModuleUI(activity, this, mAppController.getModuleLayoutRoot(),
                mLayoutListener);
        mAppController.setPreviewStatusListener(mUI);
        mPreviewTexture = mAppController.getCameraAppUI().getSurfaceTexture();
        if (mPreviewTexture != null) {
            initSurface(mPreviewTexture);
        }
    }

    @Override
    public void onShutterButtonFocus(boolean pressed) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onShutterCoordinate(TouchCoordinate coord) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onShutterButtonClick() {
        // TODO: Add focusing.
        if (mCamera == null) {
            return;
        }
        mAppController.setShutterEnabled(false);

        // Set up the capture session.
        long sessionTime = System.currentTimeMillis();
        String title = CameraUtil.createJpegName(sessionTime);
        CaptureSession session = getServices().getCaptureSessionManager()
                .createNewSession(title, sessionTime, null);

        // TODO: Add location.

        // Set up the parameters for this capture.
        PhotoCaptureParameters params = new PhotoCaptureParameters();
        params.title = title;
        params.callback = this;
        params.orientation = getOrientation();
        params.flashMode = getFlashModeFromSettings();
        params.heading = mHeading;

        // Take the picture.
        mCamera.takePicture(params, session);
    }

    @Override
    public void onPreviewAreaChanged(RectF previewArea) {
        // mUI.updatePreviewAreaRect(previewArea);
        // mUI.positionProgressOverlay(previewArea);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // This is literally the same as the GCamModule implementation.
        int type = event.sensor.getType();
        float[] data;
        if (type == Sensor.TYPE_ACCELEROMETER) {
            data = mGData;
        } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
            data = mMData;
        } else {
            Log.w(TAG, String.format("Unexpected sensor type %s", event.sensor.getName()));
            return;
        }
        for (int i = 0; i < 3; i++) {
            data[i] = event.values[i];
        }
        float[] orientation = new float[3];
        SensorManager.getRotationMatrix(mR, null, mGData, mMData);
        SensorManager.getOrientation(mR, orientation);
        mHeading = (int) (orientation[0] * 180f / Math.PI) % 360;
        if (mHeading < 0) {
            mHeading += 360;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onQueueStatus(boolean full) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onRemoteShutterPress() {
        // TODO: Check whether shutter is enabled.
        onShutterButtonClick();
    }

    @Override
    public void onSurfaceTextureAvailable(final SurfaceTexture surface, int width, int height) {
        Log.d(TAG, "onSurfaceTextureAvailable");
        // Force to re-apply transform matrix here as a workaround for
        // b/11168275
        updatePreviewTransform(width, height, true);
        initSurface(surface);
    }

    public void initSurface(final SurfaceTexture surface) {
        mPreviewTexture = surface;
        closeCamera();

        mCameraManager.open(mCameraFacing, getPictureSizeFromSettings(), new OpenCallback() {
            @Override
            public void onFailure() {
                Log.e(TAG, "Could not open camera.");
                mCamera = null;
                mAppController.showErrorAndFinish(R.string.cannot_connect_camera);
            }

            @Override
            public void onCameraOpened(final OneCamera camera) {
                Log.d(TAG, "onCameraOpened: " + camera);
                mCamera = camera;
                updateBufferDimension();

                // If the surface texture is not destroyed, it may have the last
                // frame lingering.
                // We need to hold off setting transform until preview is
                // started.
                resetDefaultBufferSize();
                mState = ModuleState.WATCH_FOR_NEXT_FRAME_AFTER_PREVIEW_STARTED;

                Log.d(TAG, "starting preview ...");

                // TODO: Consider rolling these two calls into one.
                camera.startPreview(new Surface(surface), new CaptureReadyCallback() {

                    @Override
                    public void onSetupFailed() {
                        Log.e(TAG, "Could not set up preview.");
                        mCamera.close(null);
                        mCamera = null;
                        // TODO: Show an error message and exit.
                    }

                    @Override
                    public void onReadyForCapture() {
                        Log.d(TAG, "Ready for capture.");
                        onPreviewStarted();
                    }
                });
            }
        });
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        Log.d(TAG, "onSurfaceTextureSizeChanged");
        resetDefaultBufferSize();
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        Log.d(TAG, "onSurfaceTextureDestroyed");
        closeCamera();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        if (mState == ModuleState.UPDATE_TRANSFORM_ON_NEXT_SURFACE_TEXTURE_UPDATE) {
            Log.d(TAG, "onSurfaceTextureUpdated --> updatePreviewTransform");
            mState = ModuleState.IDLE;
            CameraAppUI appUI = mAppController.getCameraAppUI();
            updatePreviewTransform(appUI.getSurfaceWidth(), appUI.getSurfaceHeight(), true);
        }
    }

    @Override
    public String getModuleStringIdentifier() {
        return PHOTO_MODULE_STRING_ID;
    }

    @Override
    public void resume() {
        // Add delay on resume from lock screen only, in order to to speed up
        // the onResume --> onPause --> onResume cycle from lock screen.
        // Don't do always because letting go of thread can cause delay.
        if (mIsResumeFromLockScreen) {
            Log.v(TAG, "Delayng onResumeTasks from lock screen. " + System.currentTimeMillis());
            // Note: onPauseAfterSuper() will delete this runnable, so we will
            // at most have 1 copy queued up.
            mMainHandler.postDelayed(mResumeTaskRunnable, ON_RESUME_TASKS_DELAY_MSEC);
        } else {
            onResumeTasks();
        }
    }

    private void onResumeTasks() {
        Log.d(TAG, "onResumeTasks + " + System.currentTimeMillis());
        mPaused = false;
        mAppController.getCameraAppUI().onChangeCamera();
        mAppController.addPreviewAreaSizeChangedListener(this);
        resetDefaultBufferSize();
        getServices().getRemoteShutterListener().onModuleReady(this);
        mAppController.setShutterEnabled(true);
    }

    @Override
    public void pause() {
        mPaused = true;
        resetTextureBufferSize();
        closeCamera();
        // Remove delayed resume trigger, if it hasn't been executed yet.
        mMainHandler.removeCallbacksAndMessages(null);
    }

    @Override
    public void destroy() {
    }

    @Override
    public void onLayoutOrientationChanged(boolean isLandscape) {
        Log.d(TAG, "onLayoutOrientationChanged");
    }

    @Override
    public void onOrientationChanged(int orientation) {
        // We keep the last known orientation. So if the user first orient
        // the camera then point the camera to floor or sky, we still have
        // the correct orientation.
        if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
            return;
        }
        mOrientation = CameraUtil.roundOrientation(orientation, mOrientation);
    }

    @Override
    public void onCameraAvailable(CameraProxy cameraProxy) {
        // Ignore since we manage the camera ourselves until we remove this.
    }

    @Override
    public void hardResetSettings(SettingsManager settingsManager) {
        // TODO Auto-generated method stub
    }

    @Override
    public HardwareSpec getHardwareSpec() {
        return new HardwareSpec() {
            @Override
            public boolean isFrontCameraSupported() {
                return true;
            }

            @Override
            public boolean isHdrSupported() {
                return false;
            }

            @Override
            public boolean isHdrPlusSupported() {
                // TODO: Enable once we support this.
                return false;
            }

            @Override
            public boolean isFlashSupported() {
                return true;
            }
        };
    }

    @Override
    public BottomBarUISpec getBottomBarSpec() {
        CameraAppUI.BottomBarUISpec bottomBarSpec = new CameraAppUI.BottomBarUISpec();
        bottomBarSpec.enableGridLines = true;
        bottomBarSpec.enableCamera = true;
        bottomBarSpec.cameraCallback = mCameraSwitchCallback;
        // TODO: Enable once we support this.
        bottomBarSpec.enableHdr = false;
        // TODO: Enable once we support this.
        bottomBarSpec.hdrCallback = null;
        // TODO: Enable once we support this.
        bottomBarSpec.enableSelfTimer = false;
        bottomBarSpec.showSelfTimer = false;
        // TODO: Deal with e.g. HDR+ if it doesn't support it.
        bottomBarSpec.enableFlash = true;
        return bottomBarSpec;
    }

    @Override
    public boolean isUsingBottomBar() {
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public void onSingleTapUp(View view, int x, int y) {
    }

    @Override
    public String getPeekAccessibilityString() {
        return mAppController.getAndroidContext()
                .getResources().getString(R.string.photo_accessibility_peek);
    }

    @Override
    public void onThumbnailResult(Bitmap bitmap) {
        // TODO
    }

    @Override
    public void onPictureTaken(CaptureSession session) {
        // TODO, enough memory available? ProcessingService status, etc.
        mAppController.setShutterEnabled(true);
    }

    @Override
    public void onPictureSaved(Uri uri) {
        mAppController.notifyNewMedia(uri);
    }

    @Override
    public void onTakePictureProgress(int progressPercent) {
        // TODO once we have HDR+ hooked up.
    }

    @Override
    public void onPictureTakenFailed() {
        // TODO
    }

    @Override
    public void onSettingChanged(SettingsManager settingsManager, String key) {
        // TODO Auto-generated method stub
    }

    /**
     * Updates the preview transform matrix to adapt to the current preview
     * width, height, and orientation.
     */
    public void updatePreviewTransform() {
        int width;
        int height;
        synchronized (mDimensionLock) {
            width = mScreenWidth;
            height = mScreenHeight;
        }
        updatePreviewTransform(width, height);
    }

    /**
     * Called when the preview started. Informs the app controller and queues a
     * transform update when the next preview frame arrives.
     */
    private void onPreviewStarted() {
        if (mState == ModuleState.WATCH_FOR_NEXT_FRAME_AFTER_PREVIEW_STARTED) {
            mState = ModuleState.UPDATE_TRANSFORM_ON_NEXT_SURFACE_TEXTURE_UPDATE;
        }
        mAppController.onPreviewStarted();
    }

    /**
     * Update the preview transform based on the new dimensions. Will not force
     * an update, if it's not necessary.
     */
    private void updatePreviewTransform(int incomingWidth, int incomingHeight) {
        updatePreviewTransform(incomingWidth, incomingHeight, false);
    }

    /***
     * Update the preview transform based on the new dimensions.
     */
    private void updatePreviewTransform(int incomingWidth, int incomingHeight,
            boolean forceUpdate) {
        Log.d(TAG, "updatePreviewTransform: " + incomingWidth + " x " + incomingHeight);

        synchronized (mDimensionLock) {
            int incomingRotation = CameraUtil
                    .getDisplayRotation(mContext);
            // Check for an actual change:
            if (mScreenHeight == incomingHeight && mScreenWidth == incomingWidth &&
                    incomingRotation == mDisplayRotation && !forceUpdate) {
                return;
            }
            // Update display rotation and dimensions
            mDisplayRotation = incomingRotation;
            mScreenWidth = incomingWidth;
            mScreenHeight = incomingHeight;
            updateBufferDimension();

            mPreviewTranformationMatrix = mAppController.getCameraAppUI().getPreviewTransform(
                    mPreviewTranformationMatrix);
            int width = mScreenWidth;
            int height = mScreenHeight;

            // Assumptions:
            // - Aspect ratio for the sensor buffers is in landscape
            // orientation,
            // - Dimensions of buffers received are rotated to the natural
            // device orientation.
            // - The contents of each buffer are rotated by the inverse of
            // the display rotation.
            // - Surface scales the buffer to fit the current view bounds.

            // Get natural orientation and buffer dimensions
            int naturalOrientation = CaptureModuleUtil
                    .getDeviceNaturalOrientation(mContext);
            int effectiveWidth = mPreviewBufferWidth;
            int effectiveHeight = mPreviewBufferHeight;

            if (DEBUG) {
                Log.v(TAG, "Rotation: " + mDisplayRotation);
                Log.v(TAG, "Screen Width: " + mScreenWidth);
                Log.v(TAG, "Screen Height: " + mScreenHeight);
                Log.v(TAG, "Buffer width: " + mPreviewBufferWidth);
                Log.v(TAG, "Buffer height: " + mPreviewBufferHeight);
                Log.v(TAG, "Natural orientation: " + naturalOrientation);
            }

            // If natural orientation is portrait, rotate the buffer
            // dimensions
            if (naturalOrientation == Configuration.ORIENTATION_PORTRAIT) {
                int temp = effectiveWidth;
                effectiveWidth = effectiveHeight;
                effectiveHeight = temp;
            }

            // Find and center view rect and buffer rect
            RectF viewRect = new RectF(0, 0, width, height);
            RectF bufRect = new RectF(0, 0, effectiveWidth, effectiveHeight);
            float centerX = viewRect.centerX();
            float centerY = viewRect.centerY();
            bufRect.offset(centerX - bufRect.centerX(), centerY - bufRect.centerY());

            // Undo ScaleToFit.FILL done by the surface
            mPreviewTranformationMatrix.setRectToRect(viewRect, bufRect, Matrix.ScaleToFit.FILL);

            // Rotate buffer contents to proper orientation
            mPreviewTranformationMatrix.postRotate(getPreviewOrientation(mDisplayRotation),
                    centerX, centerY);

            // TODO: This is probably only working for the N5. Need to test
            // on a device like N10 with different sensor orientation.
            if ((mDisplayRotation % 180) == 90) {
                int temp = effectiveWidth;
                effectiveWidth = effectiveHeight;
                effectiveHeight = temp;
            }

            boolean is16by9 = false;

            // TODO: BACK/FRONT.
            Size pictureSize = getPictureSizeFromSettings();
            if (pictureSize != null) {
                pictureSize = ResolutionUtil.getApproximateSize(pictureSize);
                if (pictureSize.equals(new Size(16, 9))) {
                    is16by9 = true;
                }
            }

            float scale;
            if (is16by9) {
                // We are going to be clipping off edges to achieve the 16
                // by 9 aspect ratio so we will choose the max here to fill,
                // instead of fit.
                scale =
                        Math.max(width / (float) effectiveWidth, height
                                / (float) effectiveHeight);
            } else {
                // Scale to fit view, cropping the longest dimension
                scale =
                        Math.min(width / (float) effectiveWidth, height
                                / (float) effectiveHeight);
            }
            mPreviewTranformationMatrix.postScale(scale, scale, centerX, centerY);

            float previewWidth = effectiveWidth * scale;
            float previewHeight = effectiveHeight * scale;
            // mFullPreviewWidth = previewWidth;
            // mFullPreviewHeight = previewHeight;

            float previewCenterX = previewWidth / 2;
            float previewCenterY = previewHeight / 2;
            mPreviewTranformationMatrix.postTranslate(previewCenterX - centerX, previewCenterY
                    - centerY);

            if (is16by9) {
                float aspectRatio = FULLSCREEN_ASPECT_RATIO;
                RectF renderedPreviewRect = mAppController.getFullscreenRect();
                float desiredPreviewWidth = Math.max(renderedPreviewRect.height(),
                        renderedPreviewRect.width()) * 1 / aspectRatio;
                int letterBoxWidth = (int) Math.ceil((Math.min(renderedPreviewRect.width(),
                        renderedPreviewRect.height()) - desiredPreviewWidth) / 2.0f);
                mAppController.getCameraAppUI().addLetterboxing(letterBoxWidth);

                float wOffset = -(previewWidth - renderedPreviewRect.width()) / 2.0f;
                float hOffset = -(previewHeight - renderedPreviewRect.height()) / 2.0f;
                mPreviewTranformationMatrix.postTranslate(wOffset, hOffset);
                mAppController.updatePreviewTransformFullscreen(mPreviewTranformationMatrix,
                        aspectRatio);
                mFinalAspectRatio = aspectRatio;
            } else {
                mAppController.updatePreviewTransform(mPreviewTranformationMatrix);
                mFinalAspectRatio = null;
                mAppController.getCameraAppUI().hideLetterboxing();
            }
            // if (mGcamProxy != null) {
            // mGcamProxy.postSetAspectRatio(mFinalAspectRatio);
            // }
            // mUI.updatePreviewAreaRect(new RectF(0, 0, previewWidth,
            // previewHeight));

            // TODO: Add face detection.
            // Characteristics info =
            // mapp.getCameraProvider().getCharacteristics(0);
            // mUI.setupFaceDetection(CameraUtil.getDisplayOrientation(incomingRotation,
            // info), false);
            // updateCamera2FaceBoundTransform(new
            // RectF(mEffectiveCropRegion),
            // new RectF(0, 0, mBufferWidth, mBufferHeight),
            // new RectF(0, 0, previewWidth, previewHeight), getRotation());
        }
    }

    private void updateBufferDimension() {
        if (mCamera == null) {
            return;
        }

        Size picked = CaptureModuleUtil.pickBufferDimensions(
                mCamera.getSupportedSizes(),
                mCamera.getFullSizeAspectRatio(),
                mContext);
        mPreviewBufferWidth = picked.getWidth();
        mPreviewBufferHeight = picked.getHeight();
    }

    /**
     * Resets the default buffer size to the initially calculated size.
     */
    private void resetDefaultBufferSize() {
        synchronized (mSurfaceLock) {
            if (mPreviewTexture != null) {
                mPreviewTexture.setDefaultBufferSize(mPreviewBufferWidth, mPreviewBufferHeight);
            }
        }
    }

    private void closeCamera() {
        if (mCamera != null) {
            mCamera.close(null);
            mCamera = null;
        }
    }

    private int getOrientation() {
        if (mAppController.isAutoRotateScreen()) {
            return mDisplayRotation;
        } else {
            return mOrientation;
        }
    }

    /**
     * @return Whether we are resuming from within the lockscreen.
     */
    private static boolean isResumeFromLockscreen(Activity activity) {
        String action = activity.getIntent().getAction();
        return (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA.equals(action)
        || MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE.equals(action));
    }

    private void switchCamera(Facing switchTo) {
        if (mPaused || mCameraFacing == switchTo) {
            return;
        }
        // TODO: Un-comment once we have timer back.
        // cancelCountDown();

        mAppController.freezeScreenUntilPreviewReady();

        mCameraFacing = switchTo;
        initSurface(mPreviewTexture);

        // TODO: Un-comment once we have focus back.
        // if (mFocusManager != null) {
        // mFocusManager.removeMessages();
        // }
        // mFocusManager.setMirror(mMirror);
    }

    private Size getPictureSizeFromSettings() {
        String pictureSizeKey = mCameraFacing == Facing.FRONT ? Keys.KEY_PICTURE_SIZE_FRONT
                : Keys.KEY_PICTURE_SIZE_BACK;
        return mSettingsManager.getSize(SettingsManager.SCOPE_GLOBAL, pictureSizeKey);
    }

    private int getPreviewOrientation(int deviceOrientationDegrees) {
        // Important: Camera2 buffers are already rotated to the natural
        // orientation of the device (at least for the back-camera).

        // TODO: Remove this hack for the front camera as soon as b/16637957 is
        // fixed.
        if (mCameraFacing == Facing.FRONT) {
            deviceOrientationDegrees += 180;
        }
        return (360 - deviceOrientationDegrees) % 360;
    }

    /**
     * Returns which way around the camera is facing, based on it's ID.
     * <p>
     * TODO: This needs to change so that we store the direction directly in the
     * settings, rather than a Camera ID.
     */
    private static Facing getFacingFromCameraId(int cameraId) {
        return cameraId == 1 ? Facing.FRONT : Facing.BACK;
    }

    private void resetTextureBufferSize() {
        // Reset the default buffer sizes on the shared SurfaceTexture
        // so they are not scaled for gcam.
        //
        // According to the documentation for
        // SurfaceTexture.setDefaultBufferSize,
        // photo and video based image producers (presumably only Camera 1 api),
        // override this buffer size. Any module that uses egl to render to a
        // SurfaceTexture must have these buffer sizes reset manually. Otherwise
        // the SurfaceTexture cannot be transformed by matrix set on the
        // TextureView.
        if (mPreviewTexture != null) {
            mPreviewTexture.setDefaultBufferSize(mAppController.getCameraAppUI().getSurfaceWidth(),
                    mAppController.getCameraAppUI().getSurfaceHeight());
        }
    }

    /**
     * @return The currently set Flash settings. Defaults to AUTO if the setting
     *         could not be parsed.
     */
    private Flash getFlashModeFromSettings() {
        String flashSetting = mSettingsManager.getString(mAppController.getCameraScope(),
                Keys.KEY_FLASH_MODE);
        try {
            return Flash.valueOf(flashSetting.toUpperCase());
        } catch (IllegalArgumentException ex) {
            Log.w(TAG, "Could not parse Flash Setting. Defaulting to AUTO.");
            return Flash.AUTO;
        }
    }
}