PhotoModule.java revision 0505273644c4c3aaf908bcda54c6d576a3ebfae8
1d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb/*
2d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * Copyright (C) 2012 The Android Open Source Project
3d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *
4d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * Licensed under the Apache License, Version 2.0 (the "License");
5d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * you may not use this file except in compliance with the License.
6d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * You may obtain a copy of the License at
7d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *
8d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *      http://www.apache.org/licenses/LICENSE-2.0
9d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *
10d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * Unless required by applicable law or agreed to in writing, software
11d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * distributed under the License is distributed on an "AS IS" BASIS,
12d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * See the License for the specific language governing permissions and
14d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * limitations under the License.
15d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb */
16d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
17d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbpackage com.android.camera;
18d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
19d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.annotation.TargetApi;
20d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.app.Activity;
21f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reckimport android.app.AlertDialog;
22d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.content.ContentProviderClient;
23d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.content.ContentResolver;
24f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reckimport android.content.DialogInterface;
25d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.content.Intent;
26d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.content.SharedPreferences.Editor;
27d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.content.res.Configuration;
28d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.graphics.Bitmap;
29d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.graphics.SurfaceTexture;
30d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.hardware.Camera.CameraInfo;
31d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.hardware.Camera.Face;
32d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.hardware.Camera.FaceDetectionListener;
33d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.hardware.Camera.Parameters;
34d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.hardware.Camera.PictureCallback;
35d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.hardware.Camera.Size;
36d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.location.Location;
37d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.media.CameraProfile;
38d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.net.Uri;
39d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.os.Bundle;
40d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.os.ConditionVariable;
41d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.os.Handler;
42d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.os.Looper;
43d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.os.Message;
44d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.os.MessageQueue;
45d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.os.SystemClock;
46d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.provider.MediaStore;
47d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.util.Log;
482d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolbimport android.view.Gravity;
49d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.KeyEvent;
50d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.LayoutInflater;
51d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.MotionEvent;
52d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.OrientationEventListener;
53d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.SurfaceHolder;
54d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.View;
5590ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolbimport android.view.View.OnClickListener;
56d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.ViewGroup;
57d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.WindowManager;
582d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolbimport android.widget.FrameLayout;
594d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolbimport android.widget.FrameLayout.LayoutParams;
60d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.widget.ImageView;
61d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.widget.Toast;
62d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
63d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport com.android.camera.CameraManager.CameraProxy;
642d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolbimport com.android.camera.ui.AbstractSettingPopup;
65d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport com.android.camera.ui.FaceView;
661373b98135b2854578111dd535e24d39c8853f14Michael Kolbimport com.android.camera.ui.PieRenderer;
67d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport com.android.camera.ui.PopupManager;
68d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport com.android.camera.ui.PreviewSurfaceView;
69b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolbimport com.android.camera.ui.RenderOverlay;
70d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport com.android.camera.ui.Rotatable;
71d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport com.android.camera.ui.RotateLayout;
72d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport com.android.camera.ui.RotateTextToast;
73d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport com.android.camera.ui.TwoStateImageView;
744d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolbimport com.android.camera.ui.ZoomRenderer;
75ce51afce8c74f2c68e386b2a8f35c4be6772b760Wu-cheng Liimport com.android.gallery3d.app.CropImage;
764d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolbimport com.android.gallery3d.common.ApiHelper;
77d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
78d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport java.io.File;
79d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport java.io.FileNotFoundException;
80d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport java.io.FileOutputStream;
81d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport java.io.IOException;
82d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport java.io.OutputStream;
83d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport java.util.ArrayList;
84d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport java.util.Collections;
85d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport java.util.Formatter;
86d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport java.util.List;
87d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
88d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbpublic class PhotoModule
89d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    implements CameraModule,
90b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb    FocusOverlayManager.Listener,
91d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    CameraPreference.OnPreferenceChangedListener,
92d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    LocationManager.Listener,
93d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    PreviewFrameLayout.OnSizeChangedListener,
94d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    ShutterButton.OnShutterButtonListener,
951373b98135b2854578111dd535e24d39c8853f14Michael Kolb    SurfaceHolder.Callback,
968a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb    PieRenderer.PieListener {
97d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
98d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final String TAG = "CAM_PhotoModule";
99d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
100d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // We number the request code from 1000 to avoid collision with Gallery.
101d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int REQUEST_CROP = 1000;
102d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
103d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int SETUP_PREVIEW = 1;
104d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int FIRST_TIME_INIT = 2;
105d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int CLEAR_SCREEN_DELAY = 3;
106d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int SET_CAMERA_PARAMETERS_WHEN_IDLE = 4;
107d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int CHECK_DISPLAY_ROTATION = 5;
108d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int SHOW_TAP_TO_FOCUS_TOAST = 6;
1091acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    private static final int SWITCH_CAMERA = 7;
1101acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    private static final int SWITCH_CAMERA_START_ANIMATION = 8;
1111acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    private static final int CAMERA_OPEN_DONE = 9;
1121acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    private static final int START_PREVIEW_DONE = 10;
1131acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    private static final int OPEN_CAMERA_FAIL = 11;
1141acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    private static final int CAMERA_DISABLED = 12;
115d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
116d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The subset of parameters we need to update in setCameraParameters().
117d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int UPDATE_PARAM_INITIALIZE = 1;
118d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int UPDATE_PARAM_ZOOM = 2;
119d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int UPDATE_PARAM_PREFERENCE = 4;
120d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int UPDATE_PARAM_ALL = -1;
121d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1227717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li    // This is the timeout to keep the camera in onPause for the first time
1237717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li    // after screen on if the activity is started from secure lock screen.
1247717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li    private static final int KEEP_CAMERA_TIMEOUT = 1000; // ms
1257717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li
126d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // copied from Camera hierarchy
127d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private CameraActivity mActivity;
128d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private View mRootView;
129d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private CameraProxy mCameraDevice;
130d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mCameraId;
131d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Parameters mParameters;
132d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mPaused;
1332d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb    private AbstractSettingPopup mPopup;
134d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
135d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // these are only used by Camera
136d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
137d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The activity is going to switch to the specified camera id. This is
138d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // needed because texture copy is done in GL thread. -1 means camera is not
139d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // switching.
140d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    protected int mPendingSwitchCameraId = -1;
141d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mOpenCameraFail;
142d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mCameraDisabled;
143d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
144d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // When setCameraParametersWhenIdle() is called, we accumulate the subsets
145d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // needed to be updated in mUpdateSet.
146d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mUpdateSet;
147d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
148d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int SCREEN_DELAY = 2 * 60 * 1000;
149d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
150d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mZoomValue;  // The current zoom value.
151d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mZoomMax;
152d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
153d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Parameters mInitialParams;
154d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mFocusAreaSupported;
155d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mMeteringAreaSupported;
156d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mAeLockSupported;
157d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mAwbLockSupported;
158d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mContinousFocusSupported;
159d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
160d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The degrees of the device rotated clockwise from its natural orientation.
161d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
1621acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    // The orientation compensation for icons and dialogs. Ex: if the value
163d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // is 90, the UI components should be rotated 90 degrees counter-clockwise.
164d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mOrientationCompensation = 0;
165d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ComboPreferences mPreferences;
166d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
167d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final String sTempCropFilename = "crop-temp";
168d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
169d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ContentProviderClient mMediaProviderClient;
170d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ShutterButton mShutterButton;
171d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mFaceDetectionStarted = false;
172d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
173d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private PreviewFrameLayout mPreviewFrameLayout;
174d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Object mSurfaceTexture;
175d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
176d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // for API level 10
177d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private PreviewSurfaceView mPreviewSurfaceView;
178d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private volatile SurfaceHolder mCameraSurfaceHolder;
179d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
180d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private FaceView mFaceView;
181b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb    private RenderOverlay mRenderOverlay;
182d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Rotatable mReviewCancelButton;
183d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Rotatable mReviewDoneButton;
18490ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb//    private View mReviewRetakeButton;
185d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
186d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // mCropValue and mSaveUri are used only if isImageCaptureIntent() is true.
187d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private String mCropValue;
188d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Uri mSaveUri;
189d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1908a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb    private View mMenu;
1918a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb    private View mBlocker;
1928a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb
193d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Small indicators which show the camera settings in the viewfinder.
1941a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    private ImageView mExposureIndicator;
195d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ImageView mFlashIndicator;
196d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ImageView mSceneIndicator;
1971a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    private ImageView mHdrIndicator;
198d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // A view group that contains all the small indicators.
1991a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    private RotateLayout mOnScreenIndicators;
200d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2011acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    // We use a thread in ImageSaver to do the work of saving images. This
2021acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    // reduces the shot-to-shot time.
203d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ImageSaver mImageSaver;
204d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Similarly, we use a thread to generate the name of the picture and insert
205d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // it into MediaStore while picture taking is still in progress.
206d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ImageNamer mImageNamer;
207d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
208d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Runnable mDoSnapRunnable = new Runnable() {
209d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
210d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void run() {
211d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            onShutterButtonClick();
212d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
213d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    };
214d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
215d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final StringBuilder mBuilder = new StringBuilder();
216d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final Formatter mFormatter = new Formatter(mBuilder);
217d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final Object[] mFormatterArgs = new Object[1];
218d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
219d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    /**
220d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     * An unpublished intent flag requesting to return as soon as capturing
221d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     * is completed.
222d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     *
223d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     * TODO: consider publishing by moving into MediaStore.
224d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     */
225d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final String EXTRA_QUICK_CAPTURE =
226d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            "android.intent.extra.quickCapture";
227d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
228d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The display rotation in degrees. This is only valid when mCameraState is
229d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // not PREVIEW_STOPPED.
230d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mDisplayRotation;
231d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The value for android.hardware.Camera.setDisplayOrientation.
232d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mCameraDisplayOrientation;
233d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The value for UI components like indicators.
234d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mDisplayOrientation;
235d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The value for android.hardware.Camera.Parameters.setRotation.
236d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mJpegRotation;
237d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mFirstTimeInitialized;
238d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mIsImageCaptureIntent;
239d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
240d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int PREVIEW_STOPPED = 0;
241d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int IDLE = 1;  // preview is active
242d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Focus is in progress. The exact focus state is in Focus.java.
243d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int FOCUSING = 2;
244d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int SNAPSHOT_IN_PROGRESS = 3;
245d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Switching between cameras.
246d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int SWITCHING_CAMERA = 4;
247d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mCameraState = PREVIEW_STOPPED;
248d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mSnapshotOnIdle = false;
249d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
250d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ContentResolver mContentResolver;
251d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
252d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private LocationManager mLocationManager;
253d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
254d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final ShutterCallback mShutterCallback = new ShutterCallback();
255d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final PostViewPictureCallback mPostViewPictureCallback =
256d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            new PostViewPictureCallback();
257d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final RawPictureCallback mRawPictureCallback =
258d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            new RawPictureCallback();
259d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final AutoFocusCallback mAutoFocusCallback =
260d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            new AutoFocusCallback();
261d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final Object mAutoFocusMoveCallback =
262d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK
263d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            ? new AutoFocusMoveCallback()
264d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            : null;
265d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
266d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final CameraErrorCallback mErrorCallback = new CameraErrorCallback();
267d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
268d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mFocusStartTime;
269d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mShutterCallbackTime;
270d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mPostViewPictureCallbackTime;
271d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mRawPictureCallbackTime;
272d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mJpegPictureCallbackTime;
273d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mOnResumeTime;
274d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private byte[] mJpegImageData;
275d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
276d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // These latency time are for the CameraLatency test.
277d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mAutoFocusTime;
278d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mShutterLag;
279d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mShutterToPictureDisplayedTime;
280d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mPictureDisplayedToJpegCallbackTime;
281d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mJpegCallbackFinishTime;
282d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mCaptureStartTime;
283d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
284d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // This handles everything about focus.
285b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb    private FocusOverlayManager mFocusManager;
286b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb
2871373b98135b2854578111dd535e24d39c8853f14Michael Kolb    private PieRenderer mPieRenderer;
288b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb    private PhotoController mPhotoControl;
2891373b98135b2854578111dd535e24d39c8853f14Michael Kolb
2904d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb    private ZoomRenderer mZoomRenderer;
2914d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb
292d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private String mSceneMode;
293d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Toast mNotSelectableToast;
294d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
295d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final Handler mHandler = new MainHandler();
296d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private PreferenceGroup mPreferenceGroup;
297d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
298d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mQuickCapture;
299d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
300d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    CameraStartUpThread mCameraStartUpThread;
301d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    ConditionVariable mStartPreviewPrerequisiteReady = new ConditionVariable();
302d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
3034d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb    private PreviewGestures mGestures;
3044d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb
305d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The purpose is not to block the main thread in onCreate and onResume.
306d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private class CameraStartUpThread extends Thread {
307d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private volatile boolean mCancelled;
308d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
309d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void cancel() {
310d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCancelled = true;
311d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
312d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
313d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
314d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void run() {
315d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            try {
316d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // We need to check whether the activity is paused before long
317d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // operations to ensure that onPause() can be done ASAP.
318d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mCancelled) return;
319d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraDevice = Util.openCamera(mActivity, mCameraId);
320d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mParameters = mCameraDevice.getParameters();
321d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // Wait until all the initialization needed by startPreview are
322d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // done.
323d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mStartPreviewPrerequisiteReady.block();
324d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
325d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                initializeCapabilities();
326d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mFocusManager == null) initializeFocusManager();
327d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mCancelled) return;
328d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                setCameraParameters(UPDATE_PARAM_ALL);
329d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessage(CAMERA_OPEN_DONE);
330d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mCancelled) return;
331d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                startPreview();
332d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessage(START_PREVIEW_DONE);
333d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mOnResumeTime = SystemClock.uptimeMillis();
334d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessage(CHECK_DISPLAY_ROTATION);
335d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } catch (CameraHardwareException e) {
336d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessage(OPEN_CAMERA_FAIL);
337d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } catch (CameraDisabledException e) {
338d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessage(CAMERA_DISABLED);
339d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
340d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
341d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
342d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
343d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    /**
344d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     * This Handler is used to post message back onto the main thread of the
345d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     * application
346d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     */
347d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private class MainHandler extends Handler {
348d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
349d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void handleMessage(Message msg) {
350d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            switch (msg.what) {
351d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case SETUP_PREVIEW: {
352d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    setupPreview();
353d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
354d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
355d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
356d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case CLEAR_SCREEN_DELAY: {
357d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mActivity.getWindow().clearFlags(
358d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
359d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
360d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
361d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
362d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case FIRST_TIME_INIT: {
363d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    initializeFirstTime();
364d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
365d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
366d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
367d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case SET_CAMERA_PARAMETERS_WHEN_IDLE: {
368d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    setCameraParametersWhenIdle(0);
369d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
370d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
371d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
372d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case CHECK_DISPLAY_ROTATION: {
373d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // Set the display orientation if display rotation has changed.
374d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // Sometimes this happens when the device is held upside
375d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // down and camera app is opened. Rotation animation will
376d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // take some time and the rotation value we have got may be
377d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // wrong. Framework does not have a callback for this now.
378d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (Util.getDisplayRotation(mActivity) != mDisplayRotation) {
379d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        setDisplayOrientation();
380d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
381d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) {
382d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mHandler.sendEmptyMessageDelayed(CHECK_DISPLAY_ROTATION, 100);
383d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
384d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
385d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
386d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
387d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case SHOW_TAP_TO_FOCUS_TOAST: {
388d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    showTapToFocusToast();
389d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
390d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
391d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
392d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case SWITCH_CAMERA: {
393d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    switchCamera();
394d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
395d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
396d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
397d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case SWITCH_CAMERA_START_ANIMATION: {
398b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb                    ((CameraScreenNail) mActivity.mCameraScreenNail).animateSwitchCamera();
399d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
400d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
401d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
402d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case CAMERA_OPEN_DONE: {
403d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    initializeAfterCameraOpen();
404d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
405d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
406d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
407d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case START_PREVIEW_DONE: {
408d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mCameraStartUpThread = null;
409d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    setCameraState(IDLE);
410d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (!ApiHelper.HAS_SURFACE_TEXTURE) {
411d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // This may happen if surfaceCreated has arrived.
412d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mCameraDevice.setPreviewDisplayAsync(mCameraSurfaceHolder);
413d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
414d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    startFaceDetection();
415eebbf302dfa981b83a1f445eee6e8674f9309accWu-cheng Li                    locationFirstRun();
416d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
417d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
418d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
419d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case OPEN_CAMERA_FAIL: {
420d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mCameraStartUpThread = null;
421d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mOpenCameraFail = true;
422d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    Util.showErrorAndFinish(mActivity,
423d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            R.string.cannot_connect_camera);
424d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
425d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
426d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
427d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case CAMERA_DISABLED: {
428d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mCameraStartUpThread = null;
429d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mCameraDisabled = true;
430d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    Util.showErrorAndFinish(mActivity,
431d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            R.string.camera_disabled);
432d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
433d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
434d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
435d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
436d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
437d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
438d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
439ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb    public void init(CameraActivity activity, View parent, boolean reuseNail) {
440d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity = activity;
441d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mRootView = parent;
442d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreferences = new ComboPreferences(mActivity);
443d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal());
444d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraId = getPreferredCameraId(mPreferences);
445d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
446d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mContentResolver = mActivity.getContentResolver();
447d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
448d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // To reduce startup time, open the camera and start the preview in
449d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // another thread.
450d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraStartUpThread = new CameraStartUpThread();
451d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraStartUpThread.start();
452d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
453d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.getLayoutInflater().inflate(R.layout.photo_module, (ViewGroup) mRootView);
454d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
455d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Surface texture is from camera screen nail and startPreview needs it.
456d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // This must be done before startPreview.
457d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mIsImageCaptureIntent = isImageCaptureIntent();
458ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb        if (reuseNail) {
459ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb            mActivity.reuseCameraScreenNail(!mIsImageCaptureIntent);
460ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb        } else {
461ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb            mActivity.createCameraScreenNail(!mIsImageCaptureIntent);
462ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb        }
463d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
464d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreferences.setLocalId(mActivity, mCameraId);
465d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
466d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // we need to reset exposure for the preview
467d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        resetExposureCompensation();
468d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Starting the preview needs preferences, camera screen nail, and
469d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // focus area indicator.
470d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mStartPreviewPrerequisiteReady.open();
471d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
472d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeControlByIntent();
473d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
474d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeMiscControls();
475d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mLocationManager = new LocationManager(mActivity, this);
476d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initOnScreenIndicator();
477a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu
478d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Make sure all views are disabled before camera is open.
479b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb//        enableCameraControls(false);
480f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    }
481f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck
482f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    // Prompt the user to pick to record location for the very first run of
483f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    // camera only
484f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    private void locationFirstRun() {
485f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        if (RecordLocationPreference.isSet(mPreferences)) {
486f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            return;
487f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        }
488f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        new AlertDialog.Builder(mActivity)
489f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .setTitle(R.string.remember_location_title)
490f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .setMessage(R.string.remember_location_prompt)
491f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .setPositiveButton(R.string.remember_location_yes, new DialogInterface.OnClickListener() {
492f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                @Override
493f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                public void onClick(DialogInterface dialog, int arg1) {
494f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                    setLocationPreference(RecordLocationPreference.VALUE_ON);
495f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                }
496f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            })
497f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .setNegativeButton(R.string.remember_location_no, new DialogInterface.OnClickListener() {
498f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                @Override
499f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                public void onClick(DialogInterface dialog, int arg1) {
500f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                    dialog.cancel();
501f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                }
502f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            })
503f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .setOnCancelListener(new DialogInterface.OnCancelListener() {
504f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                @Override
505f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                public void onCancel(DialogInterface dialog) {
506f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                    setLocationPreference(RecordLocationPreference.VALUE_OFF);
507f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                }
508f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            })
509f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .show();
510f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    }
511f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck
512f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    private void setLocationPreference(String value) {
513f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        mPreferences.edit()
514f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .putString(RecordLocationPreference.KEY, value)
515f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .apply();
516f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        // TODO: Fix this to use the actual onSharedPreferencesChanged listener
517f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        // instead of invoking manually
518f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        onSharedPreferenceChanged();
519d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
520d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
521471c997edfddccfa0762fa720c82cc95777584e8John Reck    private void initializeRenderOverlay() {
522471c997edfddccfa0762fa720c82cc95777584e8John Reck        if (mPieRenderer != null) {
523471c997edfddccfa0762fa720c82cc95777584e8John Reck            mRenderOverlay.addRenderer(mPieRenderer);
524b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu            mFocusManager.setFocusRenderer(mPieRenderer);
525471c997edfddccfa0762fa720c82cc95777584e8John Reck        }
526471c997edfddccfa0762fa720c82cc95777584e8John Reck        if (mZoomRenderer != null) {
527471c997edfddccfa0762fa720c82cc95777584e8John Reck            mRenderOverlay.addRenderer(mZoomRenderer);
528471c997edfddccfa0762fa720c82cc95777584e8John Reck        }
529471c997edfddccfa0762fa720c82cc95777584e8John Reck        if (mGestures != null) {
530471c997edfddccfa0762fa720c82cc95777584e8John Reck            mGestures.clearTouchReceivers();
531471c997edfddccfa0762fa720c82cc95777584e8John Reck            mGestures.setRenderOverlay(mRenderOverlay);
5328a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mGestures.addTouchReceiver(mBlocker);
5338a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mGestures.addTouchReceiver(mMenu);
5348a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb
535471c997edfddccfa0762fa720c82cc95777584e8John Reck            if (isImageCaptureIntent()) {
536471c997edfddccfa0762fa720c82cc95777584e8John Reck                if (mReviewCancelButton != null) {
537471c997edfddccfa0762fa720c82cc95777584e8John Reck                    mGestures.addTouchReceiver((View) mReviewCancelButton);
538471c997edfddccfa0762fa720c82cc95777584e8John Reck                }
539471c997edfddccfa0762fa720c82cc95777584e8John Reck                if (mReviewDoneButton != null) {
540471c997edfddccfa0762fa720c82cc95777584e8John Reck                    mGestures.addTouchReceiver((View) mReviewDoneButton);
541471c997edfddccfa0762fa720c82cc95777584e8John Reck                }
542471c997edfddccfa0762fa720c82cc95777584e8John Reck            }
543471c997edfddccfa0762fa720c82cc95777584e8John Reck        }
544471c997edfddccfa0762fa720c82cc95777584e8John Reck        mRenderOverlay.requestLayout();
545471c997edfddccfa0762fa720c82cc95777584e8John Reck    }
546471c997edfddccfa0762fa720c82cc95777584e8John Reck
547d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeAfterCameraOpen() {
5481373b98135b2854578111dd535e24d39c8853f14Michael Kolb        if (mPieRenderer == null) {
5491373b98135b2854578111dd535e24d39c8853f14Michael Kolb            mPieRenderer = new PieRenderer(mActivity);
5502d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb            mPhotoControl = new PhotoController(mActivity, this, mPieRenderer);
551b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb            mPhotoControl.setListener(this);
5521373b98135b2854578111dd535e24d39c8853f14Michael Kolb            mPieRenderer.setPieListener(this);
5531373b98135b2854578111dd535e24d39c8853f14Michael Kolb        }
5544d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        if (mZoomRenderer == null) {
5554d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb            mZoomRenderer = new ZoomRenderer(mActivity);
5564d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        }
5577a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb        if (mGestures == null) {
5587a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb            // this will handle gesture disambiguation and dispatching
559471c997edfddccfa0762fa720c82cc95777584e8John Reck            mGestures = new PreviewGestures(mActivity, this, mZoomRenderer, mPieRenderer);
5607a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb        }
561471c997edfddccfa0762fa720c82cc95777584e8John Reck        initializeRenderOverlay();
562b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb        initializePhotoControl();
563b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb
564d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // These depend on camera parameters.
565d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setPreviewFrameLayoutAspectRatio();
566d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.setPreviewSize(mPreviewFrameLayout.getWidth(),
567d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPreviewFrameLayout.getHeight());
568b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb        loadCameraPreferences();
569d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeZoom();
570d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateOnScreenIndicators();
571d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        showTapToFocusToastIfNeeded();
572d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
573d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
574b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb    private void initializePhotoControl() {
5751373b98135b2854578111dd535e24d39c8853f14Michael Kolb        loadCameraPreferences();
5769667bf4a16d01c58a5b91eebede8df13e95f592dMichael Kolb        if (mPhotoControl != null) {
5779667bf4a16d01c58a5b91eebede8df13e95f592dMichael Kolb            mPhotoControl.initialize(mPreferenceGroup);
5789667bf4a16d01c58a5b91eebede8df13e95f592dMichael Kolb        }
5791373b98135b2854578111dd535e24d39c8853f14Michael Kolb        updateSceneModeUI();
5801373b98135b2854578111dd535e24d39c8853f14Michael Kolb//        mIndicatorControlContainer.setListener(this);
5811373b98135b2854578111dd535e24d39c8853f14Michael Kolb    }
5821373b98135b2854578111dd535e24d39c8853f14Michael Kolb
5831373b98135b2854578111dd535e24d39c8853f14Michael Kolb
584d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void resetExposureCompensation() {
585d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        String value = mPreferences.getString(CameraSettings.KEY_EXPOSURE,
586d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                CameraSettings.EXPOSURE_DEFAULT_VALUE);
587d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!CameraSettings.EXPOSURE_DEFAULT_VALUE.equals(value)) {
588d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Editor editor = mPreferences.edit();
589d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            editor.putString(CameraSettings.KEY_EXPOSURE, "0");
590d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            editor.apply();
591d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
592d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
593d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
594d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void keepMediaProviderInstance() {
595d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // We want to keep a reference to MediaProvider in camera's lifecycle.
596d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // TODO: Utilize mMediaProviderClient instance to replace
597d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // ContentResolver calls.
598d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mMediaProviderClient == null) {
599d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mMediaProviderClient = mContentResolver
600d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    .acquireContentProviderClient(MediaStore.AUTHORITY);
601d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
602d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
603d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
604d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Snapshots can only be taken after this is called. It should be called
605d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // once only. We could have done these things in onCreate() but we want to
606d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // make preview screen appear as soon as possible.
607d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeFirstTime() {
608d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFirstTimeInitialized) return;
609d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
610d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Initialize location service.
611d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        boolean recordLocation = RecordLocationPreference.get(
612d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPreferences, mContentResolver);
613d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mLocationManager.recordLocation(recordLocation);
614d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
615d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        keepMediaProviderInstance();
616d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
617d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Initialize shutter button.
618d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mShutterButton = mActivity.getShutterButton();
619566f11c903ceae2e53c645f8d630bb1331faf890Michael Kolb        mShutterButton.setImageResource(R.drawable.btn_new_shutter);
620d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mShutterButton.setOnShutterButtonListener(this);
621d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mShutterButton.setVisibility(View.VISIBLE);
622d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
623d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mImageSaver = new ImageSaver();
624d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mImageNamer = new ImageNamer();
625d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
626d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFirstTimeInitialized = true;
627d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        addIdleHandler();
628d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
629d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.updateStorageSpaceAndHint();
630d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
631d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
632d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void showTapToFocusToastIfNeeded() {
633d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Show the tap to focus toast if this is the first start.
634d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusAreaSupported &&
635d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPreferences.getBoolean(CameraSettings.KEY_CAMERA_FIRST_USE_HINT_SHOWN, true)) {
636d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Delay the toast for one second to wait for orientation.
637d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mHandler.sendEmptyMessageDelayed(SHOW_TAP_TO_FOCUS_TOAST, 1000);
638d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
639d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
640d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
641d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void addIdleHandler() {
642d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        MessageQueue queue = Looper.myQueue();
643d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        queue.addIdleHandler(new MessageQueue.IdleHandler() {
644d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            @Override
645d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            public boolean queueIdle() {
646d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Storage.ensureOSXCompatible();
647d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return false;
648d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
649d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        });
650d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
651d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
652d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // If the activity is paused and resumed, this method will be called in
653d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // onResume.
654d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeSecondTime() {
655d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
656d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Start location update if needed.
657d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        boolean recordLocation = RecordLocationPreference.get(
658d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPreferences, mContentResolver);
659d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mLocationManager.recordLocation(recordLocation);
660d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
661d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mImageSaver = new ImageSaver();
662d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mImageNamer = new ImageNamer();
663d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeZoom();
664d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        keepMediaProviderInstance();
665d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        hidePostCaptureAlert();
666d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
667b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb        if (mPhotoControl != null) {
668b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb            mPhotoControl.reloadPreferences();
6691373b98135b2854578111dd535e24d39c8853f14Michael Kolb        }
670d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
671d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
6724d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb    private class ZoomChangeListener implements ZoomRenderer.OnZoomChangedListener {
673d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
674d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onZoomValueChanged(int index) {
675d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Not useful to change zoom value when the activity is paused.
676d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mPaused) return;
677d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mZoomValue = index;
678d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
679d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Set zoom parameters asynchronously
680d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setZoom(mZoomValue);
681d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setParametersAsync(mParameters);
682d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
6834d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb
6844d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        @Override
6854d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        public void onZoomStart() {
686fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb            if (mPieRenderer != null) {
687fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb                mPieRenderer.setBlockFocus(true);
6884d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb            }
6894d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        }
6904d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb
6914d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        @Override
6924d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        public void onZoomEnd() {
693fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb            if (mPieRenderer != null) {
694fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb                mPieRenderer.setBlockFocus(false);
6954d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb            }
6964d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        }
697d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
698d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
699d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeZoom() {
700ba6a355912c8f206140d85e393f7d1ab29f6b0fcMichael Kolb        if (!mParameters.isZoomSupported() || (mZoomRenderer == null)) return;
701d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mZoomMax = mParameters.getMaxZoom();
7024d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        // Currently we use immediate zoom for fast zooming to get better UX and
7034d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        // there is no plan to take advantage of the smooth zoom.
7042ff430a16266c59549ebac205bead2e72638c53cGeorge Mount        if (mZoomRenderer != null) {
7052ff430a16266c59549ebac205bead2e72638c53cGeorge Mount            mZoomRenderer.setZoomMax(mZoomMax);
7062ff430a16266c59549ebac205bead2e72638c53cGeorge Mount            mZoomRenderer.setZoomIndex(mParameters.getZoom());
7072ff430a16266c59549ebac205bead2e72638c53cGeorge Mount            mZoomRenderer.setOnZoomChangeListener(new ZoomChangeListener());
7082ff430a16266c59549ebac205bead2e72638c53cGeorge Mount        }
709d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
710d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
711d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
712d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
713d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void startFaceDetection() {
714d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!ApiHelper.HAS_FACE_DETECTION) return;
715d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFaceDetectionStarted) return;
716d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mParameters.getMaxNumDetectedFaces() > 0) {
717d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceDetectionStarted = true;
718d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.clear();
719d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setVisibility(View.VISIBLE);
720d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setDisplayOrientation(mDisplayOrientation);
721d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
722d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setMirror(info.facing == CameraInfo.CAMERA_FACING_FRONT);
723d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.resume();
724d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.setFaceView(mFaceView);
725d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setFaceDetectionListener(new FaceDetectionListener() {
726d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                @Override
727d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                public void onFaceDetection(Face[] faces, android.hardware.Camera camera) {
728d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mFaceView.setFaces(faces);
729d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
730d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            });
731d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.startFaceDetection();
732d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
733d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
734d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
735d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
736d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
737d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void stopFaceDetection() {
738d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!ApiHelper.HAS_FACE_DETECTION) return;
739d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!mFaceDetectionStarted) return;
740d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mParameters.getMaxNumDetectedFaces() > 0) {
741d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceDetectionStarted = false;
742d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setFaceDetectionListener(null);
743d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.stopFaceDetection();
744d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mFaceView != null) mFaceView.clear();
745d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
746d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
747d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
748d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
749d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean dispatchTouchEvent(MotionEvent m) {
750d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraState == SWITCHING_CAMERA) return true;
7517a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb        if (mPopup != null) {
7524d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb            return mActivity.superDispatchTouchEvent(m);
7537a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb        } else if (mGestures != null && mRenderOverlay != null) {
7547a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb            return mGestures.dispatchTouch(m);
75515e0d3b07902a5f398e101312495125a3d5a0384Michael Kolb        }
756d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return false;
757d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
758d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
759d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initOnScreenIndicator() {
7601a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mOnScreenIndicators = (RotateLayout) mRootView.findViewById(R.id.on_screen_indicators);
7611a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mExposureIndicator = (ImageView) mOnScreenIndicators.findViewById(R.id.menu_exposure_indicator);
7621a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mFlashIndicator = (ImageView) mOnScreenIndicators.findViewById(R.id.menu_flash_indicator);
7631a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mSceneIndicator = (ImageView) mOnScreenIndicators.findViewById(R.id.menu_scenemode_indicator);
7641a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mHdrIndicator = (ImageView) mOnScreenIndicators.findViewById(R.id.menu_hdr_indicator);
765d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
766d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
767d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
7681a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    public void showGpsOnScreenIndicator(boolean hasSignal) { }
769d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
770d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
7711a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    public void hideGpsOnScreenIndicator() { }
772d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
773d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateExposureOnScreenIndicator(int value) {
774d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mExposureIndicator == null) {
775d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
776d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
7771a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        int id = 0;
7781a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        float step = mParameters.getExposureCompensationStep();
7791a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        value = (int) Math.round(value * step);
7801a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        switch(value) {
7811a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case -3:
7821a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_n3;
7831a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
7841a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case -2:
7851a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_n2;
7861a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
7871a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case -1:
7881a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_n1;
7891a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
7901a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case 0:
7911a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_0;
7921a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
7931a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case 1:
7941a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_p1;
7951a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
7961a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case 2:
7971a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_p2;
7981a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
7991a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case 3:
8001a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_p3;
8011a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
8021a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        }
8031a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mExposureIndicator.setImageResource(id);
8041a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb
805d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
806d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
807d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateFlashOnScreenIndicator(String value) {
808d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFlashIndicator == null) {
809d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
810d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
811d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (value == null || Parameters.FLASH_MODE_OFF.equals(value)) {
8121a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            mFlashIndicator.setImageResource(R.drawable.ic_indicator_flash_off);
813d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
814d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (Parameters.FLASH_MODE_AUTO.equals(value)) {
8151a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb                mFlashIndicator.setImageResource(R.drawable.ic_indicator_flash_auto);
816d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else if (Parameters.FLASH_MODE_ON.equals(value)) {
8171a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb                mFlashIndicator.setImageResource(R.drawable.ic_indicator_flash_on);
818d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
8191a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb                mFlashIndicator.setImageResource(R.drawable.ic_indicator_flash_off);
820d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
821d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
822d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
823d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
824d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateSceneOnScreenIndicator(String value) {
825d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mSceneIndicator == null) {
826d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
827d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
828e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb        if ((value == null) || Parameters.SCENE_MODE_AUTO.equals(value)
829e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb                || Parameters.SCENE_MODE_HDR.equals(value)) {
8301a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            mSceneIndicator.setImageResource(R.drawable.ic_indicator_sce_off);
831d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
8321a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            mSceneIndicator.setImageResource(R.drawable.ic_indicator_sce_on);
833d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
834d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
835d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
8361a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    private void updateHdrOnScreenIndicator(String value) {
8371a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        if (mHdrIndicator == null) {
838d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
839d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
840e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb        if ((value != null) && Parameters.SCENE_MODE_HDR.equals(value)) {
841e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb            mHdrIndicator.setImageResource(R.drawable.ic_indicator_hdr_on);
842e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb        } else {
843e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb            mHdrIndicator.setImageResource(R.drawable.ic_indicator_hdr_off);
844e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb        }
845d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
846d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
847d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateOnScreenIndicators() {
848d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateSceneOnScreenIndicator(mParameters.getSceneMode());
849d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateExposureOnScreenIndicator(CameraSettings.readExposure(mPreferences));
850d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateFlashOnScreenIndicator(mParameters.getFlashMode());
851e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb        updateHdrOnScreenIndicator(mParameters.getSceneMode());
852d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
853d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
854d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class ShutterCallback
855d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            implements android.hardware.Camera.ShutterCallback {
856d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
857d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onShutter() {
858d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mShutterCallbackTime = System.currentTimeMillis();
859d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mShutterLag = mShutterCallbackTime - mCaptureStartTime;
860d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mShutterLag = " + mShutterLag + "ms");
861d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
862d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
863d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
864d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class PostViewPictureCallback implements PictureCallback {
865d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
866d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onPictureTaken(
867d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                byte [] data, android.hardware.Camera camera) {
868d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPostViewPictureCallbackTime = System.currentTimeMillis();
869d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mShutterToPostViewCallbackTime = "
870d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + (mPostViewPictureCallbackTime - mShutterCallbackTime)
871d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + "ms");
872d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
873d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
874d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
875d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class RawPictureCallback implements PictureCallback {
876d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
877d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onPictureTaken(
878d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                byte [] rawData, android.hardware.Camera camera) {
879d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mRawPictureCallbackTime = System.currentTimeMillis();
880d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mShutterToRawCallbackTime = "
881d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms");
882d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
883d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
884d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
885d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class JpegPictureCallback implements PictureCallback {
886d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Location mLocation;
887d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
888d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public JpegPictureCallback(Location loc) {
889d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mLocation = loc;
890d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
891d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
892d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
893d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onPictureTaken(
894d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                final byte [] jpegData, final android.hardware.Camera camera) {
895d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mPaused) {
896d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return;
897d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
898d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
899d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mJpegPictureCallbackTime = System.currentTimeMillis();
900d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // If postview callback has arrived, the captured image is displayed
901d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // in postview callback. If not, the captured image is displayed in
902d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // raw picture callback.
903d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mPostViewPictureCallbackTime != 0) {
904d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mShutterToPictureDisplayedTime =
905d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mPostViewPictureCallbackTime - mShutterCallbackTime;
906d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPictureDisplayedToJpegCallbackTime =
907d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mJpegPictureCallbackTime - mPostViewPictureCallbackTime;
908d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
909d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mShutterToPictureDisplayedTime =
910d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mRawPictureCallbackTime - mShutterCallbackTime;
911d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPictureDisplayedToJpegCallbackTime =
912d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mJpegPictureCallbackTime - mRawPictureCallbackTime;
913d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
914d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = "
915d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + mPictureDisplayedToJpegCallbackTime + "ms");
916d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
917b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu            // Only animate when in full screen capture mode
918b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu            // i.e. If monkey/a user swipes to the gallery during picture taking,
919b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu            // don't show animation
920b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu            if (ApiHelper.HAS_SURFACE_TEXTURE && !mIsImageCaptureIntent
921b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu                    && mActivity.mShowCameraAppView) {
92270d41293e9dad8be7a1f2c556ff7c7334a60c8f5John Reck                // Finish capture animation
92370d41293e9dad8be7a1f2c556ff7c7334a60c8f5John Reck                ((CameraScreenNail) mActivity.mCameraScreenNail).animateSlide();
9241f6fd1f2a393f2d5808a908c6ef9b3a1542368b7John Reck            }
925d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden.
926d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (!mIsImageCaptureIntent) {
927d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (ApiHelper.CAN_START_PREVIEW_IN_JPEG_CALLBACK) {
928d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    setupPreview();
929d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } else {
930d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // Camera HAL of some devices have a bug. Starting preview
931d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // immediately after taking a picture will fail. Wait some
932d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // time before starting the preview.
933d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mHandler.sendEmptyMessageDelayed(SETUP_PREVIEW, 300);
934d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
935d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
936d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
937d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (!mIsImageCaptureIntent) {
938d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // Calculate the width and the height of the jpeg.
939d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Size s = mParameters.getPictureSize();
940d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                int orientation = Exif.getOrientation(jpegData);
941d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                int width, height;
942d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if ((mJpegRotation + orientation) % 180 == 0) {
943d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    width = s.width;
944d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    height = s.height;
945d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } else {
946d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    width = s.height;
947d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    height = s.width;
948d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
949d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Uri uri = mImageNamer.getUri();
950d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.addSecureAlbumItemIfNeeded(false, uri);
951d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                String title = mImageNamer.getTitle();
952d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mImageSaver.addImage(jpegData, uri, title, mLocation,
9531acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li                        width, height, orientation);
954d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
955d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mJpegImageData = jpegData;
956d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (!mQuickCapture) {
957d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    showPostCaptureAlert();
958d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } else {
959d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    doAttach();
960d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
961d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
962d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
963d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Check this in advance of each shot so we don't add to shutter
964d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // latency. It's true that someone else could write to the SD card in
965d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // the mean time and fill it, but that could have happened between the
966d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // shutter press and saving the JPEG too.
967d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mActivity.updateStorageSpaceAndHint();
968d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
969d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            long now = System.currentTimeMillis();
970d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mJpegCallbackFinishTime = now - mJpegPictureCallbackTime;
971d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mJpegCallbackFinishTime = "
972d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + mJpegCallbackFinishTime + "ms");
973d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mJpegPictureCallbackTime = 0;
974d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
975d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
976d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
977d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class AutoFocusCallback
978d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            implements android.hardware.Camera.AutoFocusCallback {
979d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
980d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onAutoFocus(
981d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                boolean focused, android.hardware.Camera camera) {
982d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mPaused) return;
983d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
984d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime;
985d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms");
986d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setCameraState(IDLE);
987d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.onAutoFocus(focused);
988d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
989d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
990d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
991d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN)
992d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class AutoFocusMoveCallback
993d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            implements android.hardware.Camera.AutoFocusMoveCallback {
994d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
995d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onAutoFocusMoving(
996d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            boolean moving, android.hardware.Camera camera) {
997d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mFocusManager.onAutoFocusMoving(moving);
998d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
999d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1000d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1001d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Each SaveRequest remembers the data needed to save an image.
1002d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static class SaveRequest {
1003d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        byte[] data;
1004d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Uri uri;
1005d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        String title;
1006d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Location loc;
1007d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int width, height;
1008d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int orientation;
1009d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1010d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1011d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // We use a queue to store the SaveRequests that have not been completed
1012d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // yet. The main thread puts the request into the queue. The saver thread
1013d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // gets it from the queue, does the work, and removes it from the queue.
1014d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    //
1015d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The main thread needs to wait for the saver thread to finish all the work
1016d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // in the queue, when the activity's onPause() is called, we need to finish
1017d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // all the work, so other programs (like Gallery) can see all the images.
1018d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    //
1019d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // If the queue becomes too long, adding a new request will block the main
1020d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // thread until the queue length drops below the threshold (QUEUE_LIMIT).
1021d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // If we don't do this, we may face several problems: (1) We may OOM
1022d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // because we are holding all the jpeg data in memory. (2) We may ANR
1023d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // when we need to wait for saver thread finishing all the work (in
1024d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // onPause() or gotoGallery()) because the time to finishing a long queue
1025d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // of work may be too long.
1026d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private class ImageSaver extends Thread {
1027d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private static final int QUEUE_LIMIT = 3;
1028d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1029d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private ArrayList<SaveRequest> mQueue;
1030d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private boolean mStop;
1031d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1032d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1033d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public ImageSaver() {
1034d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mQueue = new ArrayList<SaveRequest>();
1035d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            start();
1036d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1037d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1038d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1039d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void addImage(final byte[] data, Uri uri, String title,
10401acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li                Location loc, int width, int height, int orientation) {
1041d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            SaveRequest r = new SaveRequest();
1042d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.data = data;
1043d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.uri = uri;
1044d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.title = title;
1045d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.loc = (loc == null) ? null : new Location(loc);  // make a copy
1046d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.width = width;
1047d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.height = height;
1048d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.orientation = orientation;
1049d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            synchronized (this) {
1050d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                while (mQueue.size() >= QUEUE_LIMIT) {
1051d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    try {
1052d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        wait();
1053d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    } catch (InterruptedException ex) {
1054d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // ignore.
1055d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1056d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1057d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mQueue.add(r);
1058d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                notifyAll();  // Tell saver thread there is new work to do.
1059d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1060d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1061d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1062d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in saver thread
1063d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
1064d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void run() {
1065d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            while (true) {
1066d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                SaveRequest r;
1067d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                synchronized (this) {
1068d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (mQueue.isEmpty()) {
1069d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        notifyAll();  // notify main thread in waitDone
1070d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1071d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // Note that we can only stop after we saved all images
1072d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // in the queue.
1073d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        if (mStop) break;
1074d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1075d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        try {
1076d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            wait();
1077d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        } catch (InterruptedException ex) {
1078d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            // ignore.
1079d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        }
1080d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        continue;
1081d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1082d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    r = mQueue.get(0);
1083d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1084d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                storeImage(r.data, r.uri, r.title, r.loc, r.width, r.height,
10851acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li                        r.orientation);
1086d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                synchronized (this) {
1087d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mQueue.remove(0);
1088d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    notifyAll();  // the main thread may wait in addImage
1089d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1090d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1091d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1092d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1093d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1094d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void waitDone() {
1095d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            synchronized (this) {
1096d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                while (!mQueue.isEmpty()) {
1097d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    try {
1098d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        wait();
1099d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    } catch (InterruptedException ex) {
1100d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // ignore.
1101d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1102d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1103d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1104d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1105d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1106d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1107d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void finish() {
1108d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            waitDone();
1109d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            synchronized (this) {
1110d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mStop = true;
1111d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                notifyAll();
1112d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1113d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            try {
1114d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                join();
1115d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } catch (InterruptedException ex) {
1116d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // ignore.
1117d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1118d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1119d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1120d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in saver thread
1121d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private void storeImage(final byte[] data, Uri uri, String title,
11221acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li                Location loc, int width, int height, int orientation) {
1123d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            boolean ok = Storage.updateImage(mContentResolver, uri, title, loc,
1124d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    orientation, data, width, height);
1125d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (ok) {
1126d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Util.broadcastNewPicture(mActivity, uri);
1127d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1128d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1129d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1130d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1131d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static class ImageNamer extends Thread {
1132d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private boolean mRequestPending;
1133d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private ContentResolver mResolver;
1134d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private long mDateTaken;
1135d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private int mWidth, mHeight;
1136d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private boolean mStop;
1137d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private Uri mUri;
1138d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private String mTitle;
1139d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1140d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1141d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public ImageNamer() {
1142d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            start();
1143d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1144d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1145d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1146d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public synchronized void prepareUri(ContentResolver resolver,
1147d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                long dateTaken, int width, int height, int rotation) {
1148d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (rotation % 180 != 0) {
1149d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                int tmp = width;
1150d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                width = height;
1151d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                height = tmp;
1152d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1153d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mRequestPending = true;
1154d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mResolver = resolver;
1155d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mDateTaken = dateTaken;
1156d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mWidth = width;
1157d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mHeight = height;
1158d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            notifyAll();
1159d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1160d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1161d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1162d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public synchronized Uri getUri() {
1163d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // wait until the request is done.
1164d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            while (mRequestPending) {
1165d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                try {
1166d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    wait();
1167d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } catch (InterruptedException ex) {
1168d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // ignore.
1169d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1170d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1171d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1172d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // return the uri generated
1173d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Uri uri = mUri;
1174d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mUri = null;
1175d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return uri;
1176d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1177d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1178d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread, should be called after getUri().
1179d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public synchronized String getTitle() {
1180d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return mTitle;
1181d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1182d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1183d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in namer thread
1184d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
1185d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public synchronized void run() {
1186d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            while (true) {
1187d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mStop) break;
1188d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (!mRequestPending) {
1189d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    try {
1190d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        wait();
1191d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    } catch (InterruptedException ex) {
1192d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // ignore.
1193d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1194d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    continue;
1195d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1196d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                cleanOldUri();
1197d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                generateUri();
1198d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mRequestPending = false;
1199d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                notifyAll();
1200d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1201d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            cleanOldUri();
1202d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1203d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1204d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1205d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public synchronized void finish() {
1206d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mStop = true;
1207d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            notifyAll();
1208d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1209d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1210d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in namer thread
1211d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private void generateUri() {
1212d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mTitle = Util.createJpegName(mDateTaken);
1213d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mUri = Storage.newImage(mResolver, mTitle, mDateTaken, mWidth, mHeight);
1214d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1215d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1216d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in namer thread
1217d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private void cleanOldUri() {
1218d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mUri == null) return;
1219d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Storage.deleteImage(mResolver, mUri);
1220d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mUri = null;
1221d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1222d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1223d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1224d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setCameraState(int state) {
1225d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraState = state;
1226d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        switch (state) {
1227d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case PREVIEW_STOPPED:
1228d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case SNAPSHOT_IN_PROGRESS:
1229d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case FOCUSING:
1230d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case SWITCHING_CAMERA:
1231fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb                if (mGestures != null) mGestures.setEnabled(false);
1232d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                break;
1233d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case IDLE:
1234801d4f29ef2974501df14010e9ac5344d2fa8fa9Doris Liu                if (mGestures != null && mActivity.mShowCameraAppView) {
1235801d4f29ef2974501df14010e9ac5344d2fa8fa9Doris Liu                    // Enable gestures only when the camera app view is visible
1236801d4f29ef2974501df14010e9ac5344d2fa8fa9Doris Liu                    mGestures.setEnabled(true);
1237801d4f29ef2974501df14010e9ac5344d2fa8fa9Doris Liu                }
1238d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                break;
1239d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1240d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1241d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1242d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1243d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean capture() {
1244d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // If we are already in the middle of taking a snapshot then ignore.
1245d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS
1246d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || mCameraState == SWITCHING_CAMERA) {
1247d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return false;
1248d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1249d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCaptureStartTime = System.currentTimeMillis();
1250d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPostViewPictureCallbackTime = 0;
1251d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mJpegImageData = null;
1252d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1253b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu        // Only animate when in full screen capture mode
1254b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu        // i.e. If monkey/a user swipes to the gallery during picture taking,
1255b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu        // don't show animation
1256b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu        if (ApiHelper.HAS_SURFACE_TEXTURE && !mIsImageCaptureIntent
1257b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu                && mActivity.mShowCameraAppView) {
125870d41293e9dad8be7a1f2c556ff7c7334a60c8f5John Reck            // Start capture animation.
1259d1dccd2ec153a8aaa66e82b387cb42bb784c5a3aJohn Reck            ((CameraScreenNail) mActivity.mCameraScreenNail).animateFlash(getCameraRotation());
126070d41293e9dad8be7a1f2c556ff7c7334a60c8f5John Reck        }
126170d41293e9dad8be7a1f2c556ff7c7334a60c8f5John Reck
1262d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set rotation and gps data.
1263d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mJpegRotation = Util.getJpegRotation(mCameraId, mOrientation);
1264d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mParameters.setRotation(mJpegRotation);
1265d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Location loc = mLocationManager.getCurrentLocation();
1266d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Util.setGpsParameters(mParameters, loc);
1267d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.setParameters(mParameters);
1268d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1269dd5a6f95999ec217453fc2d1caa2856e33d122afMangesh Ghiware        mCameraDevice.takePicture2(mShutterCallback, mRawPictureCallback,
1270dd5a6f95999ec217453fc2d1caa2856e33d122afMangesh Ghiware                mPostViewPictureCallback, new JpegPictureCallback(loc),
1271dd5a6f95999ec217453fc2d1caa2856e33d122afMangesh Ghiware                mCameraState, mFocusManager.getFocusState());
1272d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1273d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Size size = mParameters.getPictureSize();
1274d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mImageNamer.prepareUri(mContentResolver, mCaptureStartTime,
1275d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                size.width, size.height, mJpegRotation);
1276d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1277d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFaceDetectionStarted = false;
1278d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraState(SNAPSHOT_IN_PROGRESS);
1279d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return true;
1280d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1281d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1282d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int getCameraRotation() {
1283d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return (mOrientationCompensation - mDisplayRotation + 360) % 360;
1284d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1285d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1286d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1287d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void setFocusParameters() {
1288d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraParameters(UPDATE_PARAM_PREFERENCE);
1289d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1290d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1291d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int getPreferredCameraId(ComboPreferences preferences) {
1292d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int intentCameraId = Util.getCameraFacingIntentExtras(mActivity);
1293d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (intentCameraId != -1) {
1294d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Testing purpose. Launch a specific camera through the intent
1295d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // extras.
1296d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return intentCameraId;
1297d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1298d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return CameraSettings.readPreferredCameraId(preferences);
1299d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1300d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1301d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1302d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1303d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1304d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onFullScreenChanged(boolean full) {
130541d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb        if (mFaceView != null) {
130641d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb            mFaceView.setBlockDraw(!full);
130741d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb        }
13082d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        if (mPopup != null) {
130989fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb            dismissPopup(false, true);
13102d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        }
13114d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        if (mGestures != null) {
13124d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb            mGestures.setEnabled(full);
13134d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        }
13148a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        if (mRenderOverlay != null) {
13158a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            // this can not happen in capture mode
13168a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mRenderOverlay.setVisibility(full ? View.VISIBLE : View.GONE);
13178a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        }
1318fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb        if (mPieRenderer != null) {
1319fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb            mPieRenderer.setBlockFocus(!full);
132082ee836f814740198b193e7aa105fdbb7dc8711eMichael Kolb        }
1321812fcbcb099b5ff7b9f9ae8bb4219d57dc29c70cMichael Kolb        if (mOnScreenIndicators != null) {
1322812fcbcb099b5ff7b9f9ae8bb4219d57dc29c70cMichael Kolb            mOnScreenIndicators.setVisibility(full ? View.VISIBLE : View.GONE);
1323812fcbcb099b5ff7b9f9ae8bb4219d57dc29c70cMichael Kolb        }
13248a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        if (mMenu != null) {
13258a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mMenu.setVisibility(full ? View.VISIBLE : View.GONE);
13268a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        }
13278a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        if (mBlocker != null) {
13288a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mBlocker.setVisibility(full ? View.VISIBLE : View.GONE);
13298a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        }
13309713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb        if (ApiHelper.HAS_SURFACE_TEXTURE) {
13319713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb            if (mActivity.mCameraScreenNail != null) {
13329713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb                ((CameraScreenNail) mActivity.mCameraScreenNail).setFullScreen(full);
13339713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb            }
13349713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb            return;
13359713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb        }
1336d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (full) {
1337d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPreviewSurfaceView.expand();
1338d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1339d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPreviewSurfaceView.shrink();
1340d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1341d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1342d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1343d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1344d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
1345d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "surfaceChanged:" + holder + " width=" + width + ". height="
1346d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                + height);
1347d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1348d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1349d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1350d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void surfaceCreated(SurfaceHolder holder) {
1351d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "surfaceCreated: " + holder);
1352d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraSurfaceHolder = holder;
1353d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Do not access the camera if camera start up thread is not finished.
1354d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraDevice == null || mCameraStartUpThread != null) return;
1355d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1356d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.setPreviewDisplayAsync(holder);
1357d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // This happens when onConfigurationChanged arrives, surface has been
1358d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // destroyed, and there is no onFullScreenChanged.
1359d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraState == PREVIEW_STOPPED) {
1360d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setupPreview();
1361d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1362d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1363d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1364d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1365d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void surfaceDestroyed(SurfaceHolder holder) {
1366d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "surfaceDestroyed: " + holder);
1367d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraSurfaceHolder = null;
1368d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        stopPreview();
1369d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1370d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1371d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateSceneModeUI() {
1372d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // If scene mode is set, we cannot set flash mode, white balance, and
1373d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // focus mode, instead, we read it from driver
1374d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
13751373b98135b2854578111dd535e24d39c8853f14Michael Kolb            overrideCameraSettings(mParameters.getFlashMode(),
13761373b98135b2854578111dd535e24d39c8853f14Michael Kolb                    mParameters.getWhiteBalance(), mParameters.getFocusMode());
1377d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
13781373b98135b2854578111dd535e24d39c8853f14Michael Kolb            overrideCameraSettings(null, null, null);
13791373b98135b2854578111dd535e24d39c8853f14Michael Kolb        }
13801373b98135b2854578111dd535e24d39c8853f14Michael Kolb    }
13811373b98135b2854578111dd535e24d39c8853f14Michael Kolb
13821373b98135b2854578111dd535e24d39c8853f14Michael Kolb    private void overrideCameraSettings(final String flashMode,
13831373b98135b2854578111dd535e24d39c8853f14Michael Kolb            final String whiteBalance, final String focusMode) {
1384b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb        if (mPhotoControl != null) {
13851373b98135b2854578111dd535e24d39c8853f14Michael Kolb//            mPieControl.enableFilter(true);
1386b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb            mPhotoControl.overrideSettings(
13871373b98135b2854578111dd535e24d39c8853f14Michael Kolb                    CameraSettings.KEY_FLASH_MODE, flashMode,
13881373b98135b2854578111dd535e24d39c8853f14Michael Kolb                    CameraSettings.KEY_WHITE_BALANCE, whiteBalance,
13891373b98135b2854578111dd535e24d39c8853f14Michael Kolb                    CameraSettings.KEY_FOCUS_MODE, focusMode);
13901373b98135b2854578111dd535e24d39c8853f14Michael Kolb//            mPieControl.enableFilter(false);
1391d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1392d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1393d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1394d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void loadCameraPreferences() {
1395d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        CameraSettings settings = new CameraSettings(mActivity, mInitialParams,
1396d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraId, CameraHolder.instance().getCameraInfo());
1397d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreferenceGroup = settings.getPreferenceGroup(R.xml.camera_preferences);
1398d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1399d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1400d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1401d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean collapseCameraControls() {
1402a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        // Remove all the popups/dialog boxes
1403a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        boolean ret = false;
1404a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        if (mPopup != null) {
1405f04692066fc7087560248b003c49e0377c056edfDoris Liu            dismissPopup(false);
1406a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu            ret = true;
1407a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        }
1408a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        return ret;
1409a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu    }
1410a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu
1411a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu    public boolean removeTopLevelPopup() {
1412a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        // Remove the top level popup or dialog box and return true if there's any
14132d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        if (mPopup != null) {
1414f04692066fc7087560248b003c49e0377c056edfDoris Liu            dismissPopup(true);
14152d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb            return true;
14162d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        }
1417d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return false;
1418d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1419d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
142078711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu    @Override
142178711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu    public void onOrientationChanged(int orientation) {
142278711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // We keep the last known orientation. So if the user first orient
142378711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // the camera then point the camera to floor or sky, we still have
142478711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // the correct orientation.
142578711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return;
142678711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        mOrientation = Util.roundOrientation(orientation, mOrientation);
142778711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // When the screen is unlocked, display rotation may change. Always
142878711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // calculate the up-to-date orientationCompensation.
142978711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        int orientationCompensation =
143078711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu                (mOrientation + Util.getDisplayRotation(mActivity)) % 360;
1431032dea1d8406cde556ec0a441e4c90409edf9d63John Reck        if (mOrientationCompensation != orientationCompensation) {
143278711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu            mOrientationCompensation = orientationCompensation;
143341d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb            if (mFaceView != null) {
143441d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb                mFaceView.setOrientation(mOrientationCompensation, true);
143541d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb            }
14363851712c5a250c6764c894b99984e262a8f206a4John Reck            setDisplayOrientation();
1437d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1438d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
143978711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // Show the toast after getting the first orientation changed.
144078711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        if (mHandler.hasMessages(SHOW_TAP_TO_FOCUS_TOAST)) {
144178711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu            mHandler.removeMessages(SHOW_TAP_TO_FOCUS_TOAST);
144278711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu            showTapToFocusToast();
1443d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1444d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1445d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1446d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1447d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onStop() {
1448d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mMediaProviderClient != null) {
1449d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mMediaProviderClient.release();
1450d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mMediaProviderClient = null;
1451d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1452d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1453d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1454d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // onClick handler for R.id.btn_retake
1455d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @OnClickAttr
1456d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onReviewRetakeClicked(View v) {
1457d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused) return;
1458d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1459d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        hidePostCaptureAlert();
1460d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setupPreview();
1461d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1462d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1463d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // onClick handler for R.id.btn_done
1464d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @OnClickAttr
1465d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onReviewDoneClicked(View v) {
1466d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        doAttach();
1467d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1468d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1469d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // onClick handler for R.id.btn_cancel
1470d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @OnClickAttr
1471d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onReviewCancelClicked(View v) {
1472d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        doCancel();
1473d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1474d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1475d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void doAttach() {
1476d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused) {
1477d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
1478d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1479d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1480d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        byte[] data = mJpegImageData;
1481d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1482d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCropValue == null) {
1483d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // First handle the no crop case -- just return the value.  If the
1484d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // caller specifies a "save uri" then write the data to its
1485d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // stream. Otherwise, pass back a scaled down version of the bitmap
1486d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // directly in the extras.
1487d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mSaveUri != null) {
1488d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                OutputStream outputStream = null;
1489d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                try {
1490d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    outputStream = mContentResolver.openOutputStream(mSaveUri);
1491d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    outputStream.write(data);
1492d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    outputStream.close();
1493d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1494d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mActivity.setResultEx(Activity.RESULT_OK);
1495d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mActivity.finish();
1496d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } catch (IOException ex) {
1497d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // ignore exception
1498d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } finally {
1499d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    Util.closeSilently(outputStream);
1500d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1501d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
1502d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                int orientation = Exif.getOrientation(data);
1503d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Bitmap bitmap = Util.makeBitmap(data, 50 * 1024);
1504d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                bitmap = Util.rotate(bitmap, orientation);
1505d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.setResultEx(Activity.RESULT_OK,
1506d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        new Intent("inline-data").putExtra("data", bitmap));
1507d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.finish();
1508d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1509d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1510d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Save the image to a temp file and invoke the cropper
1511d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Uri tempUri = null;
1512d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            FileOutputStream tempStream = null;
1513d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            try {
1514d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                File path = mActivity.getFileStreamPath(sTempCropFilename);
1515d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                path.delete();
1516d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                tempStream = mActivity.openFileOutput(sTempCropFilename, 0);
1517d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                tempStream.write(data);
1518d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                tempStream.close();
1519d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                tempUri = Uri.fromFile(path);
1520d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } catch (FileNotFoundException ex) {
1521d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.setResultEx(Activity.RESULT_CANCELED);
1522d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.finish();
1523d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return;
1524d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } catch (IOException ex) {
1525d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.setResultEx(Activity.RESULT_CANCELED);
1526d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.finish();
1527d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return;
1528d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } finally {
1529d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Util.closeSilently(tempStream);
1530d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1531d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1532d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Bundle newExtras = new Bundle();
1533d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mCropValue.equals("circle")) {
1534d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                newExtras.putString("circleCrop", "true");
1535d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1536d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mSaveUri != null) {
1537d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
1538d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
1539d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                newExtras.putBoolean("return-data", true);
1540d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1541ce51afce8c74f2c68e386b2a8f35c4be6772b760Wu-cheng Li            if (mActivity.isSecureCamera()) {
1542ce51afce8c74f2c68e386b2a8f35c4be6772b760Wu-cheng Li                newExtras.putBoolean(CropImage.KEY_SHOW_WHEN_LOCKED, true);
1543ce51afce8c74f2c68e386b2a8f35c4be6772b760Wu-cheng Li            }
1544d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1545d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Intent cropIntent = new Intent("com.android.camera.action.CROP");
1546d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1547d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            cropIntent.setData(tempUri);
1548d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            cropIntent.putExtras(newExtras);
1549d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1550d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mActivity.startActivityForResult(cropIntent, REQUEST_CROP);
1551d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1552d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1553d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1554d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void doCancel() {
1555d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.setResultEx(Activity.RESULT_CANCELED, new Intent());
1556d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.finish();
1557d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1558d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1559d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1560d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onShutterButtonFocus(boolean pressed) {
1561d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused || collapseCameraControls()
1562d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || (mCameraState == SNAPSHOT_IN_PROGRESS)
1563d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || (mCameraState == PREVIEW_STOPPED)) return;
1564d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1565d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Do not do focus if there is not enough storage.
1566d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (pressed && !canTakePicture()) return;
1567d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1568d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (pressed) {
1569d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.onShutterDown();
1570d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1571d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.onShutterUp();
1572d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1573d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1574d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1575d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1576d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onShutterButtonClick() {
1577d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused || collapseCameraControls()
1578d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || (mCameraState == SWITCHING_CAMERA)
1579d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || (mCameraState == PREVIEW_STOPPED)) return;
1580d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1581d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Do not take the picture if there is not enough storage.
1582d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mActivity.getStorageSpace() <= Storage.LOW_STORAGE_THRESHOLD) {
1583d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.i(TAG, "Not enough space or storage not ready. remaining="
1584d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + mActivity.getStorageSpace());
1585d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
1586d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1587d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "onShutterButtonClick: mCameraState=" + mCameraState);
1588d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1589d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // If the user wants to do a snapshot while the previous one is still
1590d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // in progress, remember the fact and do it after we finish the previous
1591d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // one and re-start the preview. Snapshot in progress also includes the
1592d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // state that autofocus is focusing and a picture will be taken when
1593d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // focus callback arrives.
1594d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if ((mFocusManager.isFocusingSnapOnFinish() || mCameraState == SNAPSHOT_IN_PROGRESS)
1595d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                && !mIsImageCaptureIntent) {
1596d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mSnapshotOnIdle = true;
1597d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
1598d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1599d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1600d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mSnapshotOnIdle = false;
1601d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.doSnap();
1602d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1603d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1604d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1605d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void installIntentFilter() {
1606d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1607d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1608d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1609d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean updateStorageHintOnResume() {
1610d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return mFirstTimeInitialized;
1611d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1612d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1613d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1614d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void updateCameraAppView() {
1615d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1616d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1617d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1618d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onResumeBeforeSuper() {
1619d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPaused = false;
1620d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1621d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1622d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1623d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onResumeAfterSuper() {
1624d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mOpenCameraFail || mCameraDisabled) return;
1625d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1626d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mJpegPictureCallbackTime = 0;
1627d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mZoomValue = 0;
1628d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1629d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Start the preview if it is not started.
1630d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraState == PREVIEW_STOPPED && mCameraStartUpThread == null) {
1631d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            resetExposureCompensation();
1632d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraStartUpThread = new CameraStartUpThread();
1633d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraStartUpThread.start();
1634d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1635d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1636d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // If first time initialization is not finished, put it in the
1637d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // message queue.
1638d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!mFirstTimeInitialized) {
1639d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mHandler.sendEmptyMessage(FIRST_TIME_INIT);
1640d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1641d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            initializeSecondTime();
1642d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1643d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        keepScreenOnAwhile();
1644d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1645d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Dismiss open menu if exists.
1646d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        PopupManager.getInstance(mActivity).notifyShowPopup(null);
1647d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1648d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1649d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    void waitCameraStartUpThread() {
1650d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        try {
1651d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mCameraStartUpThread != null) {
1652d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraStartUpThread.cancel();
1653d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraStartUpThread.join();
1654d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraStartUpThread = null;
1655d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                setCameraState(IDLE);
1656d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1657d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } catch (InterruptedException e) {
1658d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // ignore
1659d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1660d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1661d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1662d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1663d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onPauseBeforeSuper() {
1664d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPaused = true;
1665d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1666d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1667d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1668d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onPauseAfterSuper() {
1669d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Wait the camera start up thread to finish.
1670d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        waitCameraStartUpThread();
1671d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
16727717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        // When camera is started from secure lock screen for the first time
16737717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        // after screen on, the activity gets onCreate->onResume->onPause->onResume.
16747717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        // To reduce the latency, keep the camera for a short time so it does
16757717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        // not need to be opened again.
16767717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        if (mCameraDevice != null && mActivity.isSecureCamera()
16777717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li                && ActivityBase.isFirstStartAfterScreenOn()) {
16787717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li            ActivityBase.resetFirstStartAfterScreenOn();
16797717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li            CameraHolder.instance().keep(KEEP_CAMERA_TIMEOUT);
16807717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        }
16810505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        // Reset the focus first. Camera CTS does not guarantee that
16820505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        // cancelAutoFocus is allowed after preview stops.
16830505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
16840505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li            mCameraDevice.cancelAutoFocus();
16850505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        }
1686d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        stopPreview();
1687d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Close the camera now because other activities may need to use it.
1688d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        closeCamera();
1689d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mSurfaceTexture != null) {
1690b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb            ((CameraScreenNail) mActivity.mCameraScreenNail).releaseSurfaceTexture();
1691d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mSurfaceTexture = null;
1692d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1693d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        resetScreenOn();
1694d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1695d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Clear UI.
1696d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        collapseCameraControls();
1697d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFaceView != null) mFaceView.clear();
1698d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1699d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFirstTimeInitialized) {
1700d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mImageSaver != null) {
1701d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mImageSaver.finish();
1702d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mImageSaver = null;
1703d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mImageNamer.finish();
1704d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mImageNamer = null;
1705d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1706d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1707d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1708d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mLocationManager != null) mLocationManager.recordLocation(false);
1709d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateExposureOnScreenIndicator(0);
1710d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1711d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // If we are in an image capture intent and has taken
1712d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // a picture, we just clear it in onPause.
1713d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mJpegImageData = null;
1714d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1715d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Remove the messages in the event queue.
1716d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(SETUP_PREVIEW);
1717d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(FIRST_TIME_INIT);
1718d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(CHECK_DISPLAY_ROTATION);
1719d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(SWITCH_CAMERA);
1720d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(SWITCH_CAMERA_START_ANIMATION);
1721d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(CAMERA_OPEN_DONE);
1722d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(START_PREVIEW_DONE);
1723d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(OPEN_CAMERA_FAIL);
1724d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(CAMERA_DISABLED);
1725d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1726d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPendingSwitchCameraId = -1;
1727d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusManager != null) mFocusManager.removeMessages();
1728d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1729d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1730d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeControlByIntent() {
17318a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        mBlocker = mRootView.findViewById(R.id.blocker);
17328a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        mMenu = mRootView.findViewById(R.id.menu);
17338a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        mMenu.setOnClickListener(new OnClickListener() {
17348a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            @Override
17358a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            public void onClick(View v) {
17368a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb                if (mPieRenderer != null) {
17378a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb                    mPieRenderer.showInCenter();
17388a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb                }
17398a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            }
17408a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        });
1741d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mIsImageCaptureIntent) {
1742fdb79c492940d46b27702b0be71903f99af7e34fMichael Kolb
174390ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            mActivity.hideSwitcher();
1744d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Cannot use RotateImageView for "done" and "cancel" button because
1745d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // the tablet layout uses RotateLayout, which cannot be cast to
1746d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // RotateImageView.
1747d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mReviewDoneButton = (Rotatable) mRootView.findViewById(R.id.btn_done);
1748d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mReviewCancelButton = (Rotatable) mRootView.findViewById(R.id.btn_cancel);
17492d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb
175090ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            ((View) mReviewCancelButton).setVisibility(View.VISIBLE);
175190ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb
175290ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            ((View) mReviewDoneButton).setOnClickListener(new OnClickListener() {
175390ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                @Override
175490ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                public void onClick(View v) {
175590ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                    onReviewDoneClicked(v);
175690ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                }
175790ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            });
175890ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            ((View) mReviewCancelButton).setOnClickListener(new OnClickListener() {
175990ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                @Override
176090ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                public void onClick(View v) {
176190ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                    onReviewCancelClicked(v);
176290ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                }
176390ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            });
1764d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1765d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Not grayed out upon disabled, to make the follow-up fade-out
1766d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // effect look smooth. Note that the review done button in tablet
1767d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // layout is not a TwoStateImageView.
1768d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mReviewDoneButton instanceof TwoStateImageView) {
1769d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                ((TwoStateImageView) mReviewDoneButton).enableFilter(false);
1770d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1771d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1772d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setupCaptureParams();
1773d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1774d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1775d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1776b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb    /**
1777b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb     * The focus manager is the first UI related element to get initialized,
1778b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb     * and it requires the RenderOverlay, so initialize it here
1779b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb     */
1780d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeFocusManager() {
1781d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Create FocusManager object. startPreview needs it.
1782b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb        mRenderOverlay = (RenderOverlay) mRootView.findViewById(R.id.render_overlay);
1783b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu        // if mFocusManager not null, reuse it
1784b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu        // otherwise create a new instance
1785b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu        if (mFocusManager != null) {
1786b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu            mFocusManager.removeMessages();
1787b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu        } else {
178830f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu            CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
178930f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu            boolean mirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
179030f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu            String[] defaultFocusModes = mActivity.getResources().getStringArray(
179130f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu                    R.array.pref_camera_focusmode_default_array);
1792b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu            mFocusManager = new FocusOverlayManager(mPreferences, defaultFocusModes,
1793b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu                    mInitialParams, this, mirror,
1794b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu                    mActivity.getMainLooper());
1795b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu        }
1796d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1797d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1798d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeMiscControls() {
1799d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // startPreview needs this.
1800d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreviewFrameLayout = (PreviewFrameLayout) mRootView.findViewById(R.id.frame);
1801d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set touch focus listener.
1802d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.setSingleTapUpListener(mPreviewFrameLayout);
1803d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1804d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFaceView = (FaceView) mRootView.findViewById(R.id.face_view);
1805d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreviewFrameLayout.setOnSizeChangedListener(this);
1806d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreviewFrameLayout.setOnLayoutChangeListener(mActivity);
1807d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!ApiHelper.HAS_SURFACE_TEXTURE) {
1808d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPreviewSurfaceView =
1809d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    (PreviewSurfaceView) mRootView.findViewById(R.id.preview_surface_view);
1810d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPreviewSurfaceView.setVisibility(View.VISIBLE);
1811d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPreviewSurfaceView.getHolder().addCallback(this);
1812d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1813d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1814d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1815d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1816d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onConfigurationChanged(Configuration newConfig) {
1817d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "onConfigurationChanged");
1818d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setDisplayOrientation();
1819d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1820d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        ((ViewGroup) mRootView).removeAllViews();
1821d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        LayoutInflater inflater = mActivity.getLayoutInflater();
1822d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        inflater.inflate(R.layout.photo_module, (ViewGroup) mRootView);
1823d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1824d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // from onCreate()
1825d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeControlByIntent();
1826b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu
1827d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeFocusManager();
1828d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeMiscControls();
1829b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb        loadCameraPreferences();
1830d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1831d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // from initializeFirstTime()
1832d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mShutterButton = mActivity.getShutterButton();
1833d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mShutterButton.setOnShutterButtonListener(this);
1834d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeZoom();
1835d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initOnScreenIndicator();
1836d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateOnScreenIndicators();
1837d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFaceView != null) {
1838d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.clear();
1839d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setVisibility(View.VISIBLE);
1840d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setDisplayOrientation(mDisplayOrientation);
1841d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
1842d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setMirror(info.facing == CameraInfo.CAMERA_FACING_FRONT);
1843d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.resume();
1844d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.setFaceView(mFaceView);
1845d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1846471c997edfddccfa0762fa720c82cc95777584e8John Reck        initializeRenderOverlay();
1847812fcbcb099b5ff7b9f9ae8bb4219d57dc29c70cMichael Kolb        onFullScreenChanged(mActivity.isInCameraApp());
1848c5777bb0d17b3b633e67fec972229b717bc51993Angus Kong        if (mJpegImageData != null) {  // Jpeg data found, picture has been taken.
1849c5777bb0d17b3b633e67fec972229b717bc51993Angus Kong            showPostCaptureAlert();
1850c5777bb0d17b3b633e67fec972229b717bc51993Angus Kong        }
1851d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1852d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1853d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1854d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onActivityResult(
1855d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            int requestCode, int resultCode, Intent data) {
1856d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        switch (requestCode) {
1857d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case REQUEST_CROP: {
1858d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Intent intent = new Intent();
1859d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (data != null) {
1860d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    Bundle extras = data.getExtras();
1861d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (extras != null) {
1862d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        intent.putExtras(extras);
1863d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1864d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1865d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.setResultEx(resultCode, intent);
1866d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.finish();
1867d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1868d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                File path = mActivity.getFileStreamPath(sTempCropFilename);
1869d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                path.delete();
1870d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1871d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                break;
1872d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1873d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1874d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1875d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1876d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean canTakePicture() {
1877d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return isCameraIdle() && (mActivity.getStorageSpace() > Storage.LOW_STORAGE_THRESHOLD);
1878d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1879d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1880d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1881d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void autoFocus() {
1882d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusStartTime = System.currentTimeMillis();
1883d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.autoFocus(mAutoFocusCallback);
1884d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraState(FOCUSING);
1885d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1886d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1887d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1888d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void cancelAutoFocus() {
1889d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.cancelAutoFocus();
1890d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraState(IDLE);
1891d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraParameters(UPDATE_PARAM_PREFERENCE);
1892d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1893d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1894d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Preview area is touched. Handle touch focus.
1895d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1896d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onSingleTapUp(View view, int x, int y) {
1897d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused || mCameraDevice == null || !mFirstTimeInitialized
1898d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || mCameraState == SNAPSHOT_IN_PROGRESS
1899d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || mCameraState == SWITCHING_CAMERA
1900d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || mCameraState == PREVIEW_STOPPED) {
1901d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
1902d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1903d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1904d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Do not trigger touch focus if popup window is opened.
1905f04692066fc7087560248b003c49e0377c056edfDoris Liu        if (removeTopLevelPopup()) return;
1906d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1907d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Check if metering area or focus area is supported.
190855f0d13040df45c75704df9d633b444cb36f2465Michael Kolb        if (!mFocusAreaSupported && !mMeteringAreaSupported) {
190955f0d13040df45c75704df9d633b444cb36f2465Michael Kolb            if (mPieRenderer != null) {
191055f0d13040df45c75704df9d633b444cb36f2465Michael Kolb                mPieRenderer.setFocus(x,  y, true);
191155f0d13040df45c75704df9d633b444cb36f2465Michael Kolb            }
191255f0d13040df45c75704df9d633b444cb36f2465Michael Kolb        } else {
191355f0d13040df45c75704df9d633b444cb36f2465Michael Kolb            mFocusManager.onSingleTapUp(x, y);
191455f0d13040df45c75704df9d633b444cb36f2465Michael Kolb        }
1915d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1916d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1917d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1918d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean onBackPressed() {
1919fd187ac6cfd6129882ae0bcea1034f4d18fcdff8Michael Kolb        if (mPieRenderer != null && mPieRenderer.showsItems()) {
1920fd187ac6cfd6129882ae0bcea1034f4d18fcdff8Michael Kolb            mPieRenderer.hide();
1921fd187ac6cfd6129882ae0bcea1034f4d18fcdff8Michael Kolb            return true;
1922fd187ac6cfd6129882ae0bcea1034f4d18fcdff8Michael Kolb        }
1923c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu        // In image capture mode, back button should:
1924c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu        // 1) if there is any popup, dismiss them, 2) otherwise, get out of image capture
1925c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu        if (mIsImageCaptureIntent) {
1926a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu            if (!removeTopLevelPopup()) {
1927c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu                // no popup to dismiss, cancel image capture
1928c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu                doCancel();
1929c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu            }
1930c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu            return true;
1931c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu        } else if (!isCameraIdle()) {
1932d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // ignore backs while we're taking a picture
1933d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return true;
1934d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1935a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu            return removeTopLevelPopup();
1936d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1937d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1938d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1939d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1940d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean onKeyDown(int keyCode, KeyEvent event) {
1941d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        switch (keyCode) {
1942d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case KeyEvent.KEYCODE_FOCUS:
1943d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1944d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    onShutterButtonFocus(true);
1945d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1946d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return true;
1947d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case KeyEvent.KEYCODE_CAMERA:
1948d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1949d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    onShutterButtonClick();
1950d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1951d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return true;
1952d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case KeyEvent.KEYCODE_DPAD_CENTER:
1953d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // If we get a dpad center event without any focused view, move
1954d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // the focus to the shutter button and press it.
1955d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1956d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // Start auto-focus immediately to reduce shutter lag. After
1957d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // the shutter button gets the focus, onShutterButtonFocus()
1958d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // will be called again but it is fine.
1959a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu                    if (removeTopLevelPopup()) return true;
1960d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    onShutterButtonFocus(true);
1961d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (mShutterButton.isInTouchMode()) {
1962d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mShutterButton.requestFocusFromTouch();
1963d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    } else {
1964d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mShutterButton.requestFocus();
1965d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1966d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mShutterButton.setPressed(true);
1967d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1968d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return true;
1969d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1970d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return false;
1971d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1972d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1973d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1974d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean onKeyUp(int keyCode, KeyEvent event) {
1975d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        switch (keyCode) {
1976d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case KeyEvent.KEYCODE_FOCUS:
1977d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mFirstTimeInitialized) {
1978d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    onShutterButtonFocus(false);
1979d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1980d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return true;
1981d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1982d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return false;
1983d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1984d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1985d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
1986d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void closeCamera() {
1987d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraDevice != null) {
1988d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setZoomChangeListener(null);
1989d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if(ApiHelper.HAS_FACE_DETECTION) {
1990d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraDevice.setFaceDetectionListener(null);
1991d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1992d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setErrorCallback(null);
1993d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            CameraHolder.instance().release();
1994d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceDetectionStarted = false;
1995d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice = null;
1996d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setCameraState(PREVIEW_STOPPED);
1997d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.onCameraReleased();
1998d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1999d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2000d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2001d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setDisplayOrientation() {
2002d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mDisplayRotation = Util.getDisplayRotation(mActivity);
2003d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mDisplayOrientation = Util.getDisplayOrientation(mDisplayRotation, mCameraId);
2004d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDisplayOrientation = Util.getDisplayOrientation(0, mCameraId);
2005d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFaceView != null) {
2006d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setDisplayOrientation(mDisplayOrientation);
2007d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
200830f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu        if (mFocusManager != null) {
200930f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu            mFocusManager.setDisplayOrientation(mDisplayOrientation);
201030f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu        }
20113851712c5a250c6764c894b99984e262a8f206a4John Reck        // GLRoot also uses the DisplayRotation, and needs to be told to layout to update
20123851712c5a250c6764c894b99984e262a8f206a4John Reck        mActivity.getGLRoot().requestLayoutContentPane();
2013d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2014d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2015d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Only called by UI thread.
2016d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setupPreview() {
2017d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.resetTouchFocus();
2018d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        startPreview();
2019d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraState(IDLE);
2020d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        startFaceDetection();
2021d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2022d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2023d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // This can be called by UI Thread or CameraStartUpThread. So this should
2024d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // not modify the views.
2025d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void startPreview() {
2026d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.setErrorCallback(mErrorCallback);
2027d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
20280505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        // ICS camera frameworks has a bug. Face detection state is not cleared
20290505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        // after taking a picture. Stop the preview to work around it. The bug
20300505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        // was fixed in JB.
2031d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraState != PREVIEW_STOPPED) stopPreview();
2032d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2033d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setDisplayOrientation();
2034d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2035d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!mSnapshotOnIdle) {
2036d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // If the focus mode is continuous autofocus, call cancelAutoFocus to
2037d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // resume it because it may have been paused by autoFocus call.
2038d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (Util.FOCUS_MODE_CONTINUOUS_PICTURE.equals(mFocusManager.getFocusMode())) {
2039d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraDevice.cancelAutoFocus();
2040d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2041d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.setAeAwbLock(false); // Unlock AE and AWB.
2042d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2043d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraParameters(UPDATE_PARAM_ALL);
2044d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2045d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (ApiHelper.HAS_SURFACE_TEXTURE) {
2046b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb            CameraScreenNail screenNail = (CameraScreenNail) mActivity.mCameraScreenNail;
2047d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mSurfaceTexture == null) {
2048d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Size size = mParameters.getPreviewSize();
2049d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mCameraDisplayOrientation % 180 == 0) {
2050d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    screenNail.setSize(size.width, size.height);
2051d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } else {
2052d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    screenNail.setSize(size.height, size.width);
2053d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
2054d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.notifyScreenNailChanged();
2055d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                screenNail.acquireSurfaceTexture();
2056d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mSurfaceTexture = screenNail.getSurfaceTexture();
2057d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2058d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation);
2059d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setPreviewTextureAsync((SurfaceTexture) mSurfaceTexture);
2060d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2061d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setDisplayOrientation(mDisplayOrientation);
2062d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setPreviewDisplayAsync(mCameraSurfaceHolder);
2063d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2064d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2065d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "startPreview");
2066d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.startPreviewAsync();
2067d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2068d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.onPreviewStarted();
2069d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2070d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mSnapshotOnIdle) {
2071d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mHandler.post(mDoSnapRunnable);
2072d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2073d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2074d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2075d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void stopPreview() {
2076d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
2077d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "stopPreview");
2078d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.stopPreview();
2079d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceDetectionStarted = false;
2080d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2081d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraState(PREVIEW_STOPPED);
2082d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusManager != null) mFocusManager.onPreviewStopped();
2083d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2084d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2085d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @SuppressWarnings("deprecation")
2086d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateCameraParametersInitialize() {
2087d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Reset preview frame rate to the maximum because it may be lowered by
2088d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // video camera application.
2089d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        List<Integer> frameRates = mParameters.getSupportedPreviewFrameRates();
2090d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (frameRates != null) {
2091d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Integer max = Collections.max(frameRates);
2092d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setPreviewFrameRate(max);
2093d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2094d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2095d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mParameters.set(Util.RECORDING_HINT, Util.FALSE);
2096d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2097d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Disable video stabilization. Convenience methods not available in API
2098d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // level <= 14
2099d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        String vstabSupported = mParameters.get("video-stabilization-supported");
2100d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if ("true".equals(vstabSupported)) {
2101d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.set("video-stabilization", "false");
2102d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2103d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2104d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2105d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateCameraParametersZoom() {
2106d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set zoom.
2107d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mParameters.isZoomSupported()) {
2108d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setZoom(mZoomValue);
2109d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2110d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2111d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2112d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN)
2113d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setAutoExposureLockIfSupported() {
2114d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mAeLockSupported) {
2115d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setAutoExposureLock(mFocusManager.getAeAwbLock());
2116d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2117d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2118d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2119d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN)
2120d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setAutoWhiteBalanceLockIfSupported() {
2121d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mAwbLockSupported) {
2122d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setAutoWhiteBalanceLock(mFocusManager.getAeAwbLock());
2123d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2124d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2125d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2126d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
2127d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setFocusAreasIfSupported() {
2128d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusAreaSupported) {
2129d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setFocusAreas(mFocusManager.getFocusAreas());
2130d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2131d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2132d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2133d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
2134d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setMeteringAreasIfSupported() {
2135d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mMeteringAreaSupported) {
2136d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Use the same area for focus and metering.
2137d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setMeteringAreas(mFocusManager.getMeteringAreas());
2138d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2139d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2140d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2141d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateCameraParametersPreference() {
2142d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setAutoExposureLockIfSupported();
2143d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setAutoWhiteBalanceLockIfSupported();
2144d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setFocusAreasIfSupported();
2145d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setMeteringAreasIfSupported();
2146d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2147d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set picture size.
2148d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        String pictureSize = mPreferences.getString(
2149d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                CameraSettings.KEY_PICTURE_SIZE, null);
2150d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (pictureSize == null) {
2151d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            CameraSettings.initialCameraPictureSize(mActivity, mParameters);
2152d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2153d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            List<Size> supported = mParameters.getSupportedPictureSizes();
2154d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            CameraSettings.setCameraPictureSize(
2155d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    pictureSize, supported, mParameters);
2156d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2157d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Size size = mParameters.getPictureSize();
2158d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2159d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set a preview size that is closest to the viewfinder height and has
2160d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // the right aspect ratio.
2161d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        List<Size> sizes = mParameters.getSupportedPreviewSizes();
2162d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Size optimalSize = Util.getOptimalPreviewSize(mActivity, sizes,
2163d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                (double) size.width / size.height);
2164d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Size original = mParameters.getPreviewSize();
2165d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!original.equals(optimalSize)) {
2166d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setPreviewSize(optimalSize.width, optimalSize.height);
2167d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2168d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Zoom related settings will be changed for different preview
2169d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // sizes, so set and read the parameters to get latest values
2170d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setParameters(mParameters);
2171d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters = mCameraDevice.getParameters();
2172d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2173d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "Preview size is " + optimalSize.width + "x" + optimalSize.height);
2174d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2175ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        // Since changing scene mode may change supported values, set scene mode
2176ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        // first. HDR is a scene mode. To promote it in UI, it is stored in a
2177ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        // separate preference.
2178ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        String hdr = mPreferences.getString(CameraSettings.KEY_CAMERA_HDR,
2179ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li                mActivity.getString(R.string.pref_camera_hdr_default));
2180ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        if (mActivity.getString(R.string.setting_on_value).equals(hdr)) {
2181ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li            mSceneMode = Util.SCENE_MODE_HDR;
2182ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        } else {
2183ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li            mSceneMode = mPreferences.getString(
2184d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                CameraSettings.KEY_SCENE_MODE,
2185d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.getString(R.string.pref_camera_scenemode_default));
2186ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        }
2187d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (Util.isSupported(mSceneMode, mParameters.getSupportedSceneModes())) {
2188d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (!mParameters.getSceneMode().equals(mSceneMode)) {
2189d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mParameters.setSceneMode(mSceneMode);
2190d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2191d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // Setting scene mode will change the settings of flash mode,
2192d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // white balance, and focus mode. Here we read back the
2193d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // parameters, so we can know those settings.
2194d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraDevice.setParameters(mParameters);
2195d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mParameters = mCameraDevice.getParameters();
2196d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2197d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2198d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mSceneMode = mParameters.getSceneMode();
2199d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mSceneMode == null) {
2200d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mSceneMode = Parameters.SCENE_MODE_AUTO;
2201d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2202d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2203d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2204d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set JPEG quality.
2205d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId,
2206d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                CameraProfile.QUALITY_HIGH);
2207d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mParameters.setJpegQuality(jpegQuality);
2208d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2209d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // For the following settings, we need to check if the settings are
2210d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // still supported by latest driver, if not, ignore the settings.
2211d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2212d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set exposure compensation
2213d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int value = CameraSettings.readExposure(mPreferences);
2214d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int max = mParameters.getMaxExposureCompensation();
2215d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int min = mParameters.getMinExposureCompensation();
2216d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (value >= min && value <= max) {
2217d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setExposureCompensation(value);
2218d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2219d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.w(TAG, "invalid exposure range: " + value);
2220d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2221d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2222d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
2223d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Set flash mode.
2224d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            String flashMode = mPreferences.getString(
2225d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    CameraSettings.KEY_FLASH_MODE,
2226d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mActivity.getString(R.string.pref_camera_flashmode_default));
2227d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            List<String> supportedFlash = mParameters.getSupportedFlashModes();
2228d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (Util.isSupported(flashMode, supportedFlash)) {
2229d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mParameters.setFlashMode(flashMode);
2230d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
2231d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                flashMode = mParameters.getFlashMode();
2232d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (flashMode == null) {
2233d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    flashMode = mActivity.getString(
2234d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            R.string.pref_camera_flashmode_no_flash);
2235d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
2236d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2237d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2238d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Set white balance parameter.
2239d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            String whiteBalance = mPreferences.getString(
2240d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    CameraSettings.KEY_WHITE_BALANCE,
2241d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mActivity.getString(R.string.pref_camera_whitebalance_default));
2242d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (Util.isSupported(whiteBalance,
2243d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mParameters.getSupportedWhiteBalance())) {
2244d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mParameters.setWhiteBalance(whiteBalance);
2245d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
2246d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                whiteBalance = mParameters.getWhiteBalance();
2247d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (whiteBalance == null) {
2248d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    whiteBalance = Parameters.WHITE_BALANCE_AUTO;
2249d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
2250d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2251d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2252d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Set focus mode.
2253d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.overrideFocusMode(null);
2254d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setFocusMode(mFocusManager.getFocusMode());
2255d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2256d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.overrideFocusMode(mParameters.getFocusMode());
2257d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2258d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2259d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mContinousFocusSupported && ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK) {
2260d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            updateAutoFocusMoveCallback();
2261d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2262d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2263d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2264d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN)
2265d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateAutoFocusMoveCallback() {
2266d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mParameters.getFocusMode().equals(Util.FOCUS_MODE_CONTINUOUS_PICTURE)) {
2267d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setAutoFocusMoveCallback(
2268d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                (AutoFocusMoveCallback) mAutoFocusMoveCallback);
2269d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2270d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setAutoFocusMoveCallback(null);
2271d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2272d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2273d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2274d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // We separate the parameters into several subsets, so we can update only
2275d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // the subsets actually need updating. The PREFERENCE set needs extra
2276d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // locking because the preference can be changed from GLThread as well.
2277d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setCameraParameters(int updateSet) {
2278d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if ((updateSet & UPDATE_PARAM_INITIALIZE) != 0) {
2279d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            updateCameraParametersInitialize();
2280d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2281d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2282d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if ((updateSet & UPDATE_PARAM_ZOOM) != 0) {
2283d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            updateCameraParametersZoom();
2284d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2285d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2286d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) {
2287d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            updateCameraParametersPreference();
2288d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2289d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2290d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.setParameters(mParameters);
2291d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2292d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2293d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // If the Camera is idle, update the parameters immediately, otherwise
2294d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // accumulate them in mUpdateSet and update later.
2295d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setCameraParametersWhenIdle(int additionalUpdateSet) {
2296d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mUpdateSet |= additionalUpdateSet;
2297d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraDevice == null) {
2298d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // We will update all the parameters when we open the device, so
2299d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // we don't need to do anything now.
2300d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mUpdateSet = 0;
2301d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
2302d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else if (isCameraIdle()) {
2303d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setCameraParameters(mUpdateSet);
2304d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            updateSceneModeUI();
2305d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mUpdateSet = 0;
2306d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2307d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (!mHandler.hasMessages(SET_CAMERA_PARAMETERS_WHEN_IDLE)) {
2308d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessageDelayed(
2309d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000);
2310d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2311d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2312d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2313d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2314d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean isCameraIdle() {
2315d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return (mCameraState == IDLE) ||
2316d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                ((mFocusManager != null) && mFocusManager.isFocusCompleted()
2317d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        && (mCameraState != SWITCHING_CAMERA));
2318d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2319d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2320d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean isImageCaptureIntent() {
2321d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        String action = mActivity.getIntent().getAction();
232253b11253fde509b01851ba022d9178c7c6f13ddeWu-cheng Li        return (MediaStore.ACTION_IMAGE_CAPTURE.equals(action)
232353b11253fde509b01851ba022d9178c7c6f13ddeWu-cheng Li                || ActivityBase.ACTION_IMAGE_CAPTURE_SECURE.equals(action));
2324d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2325d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2326d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setupCaptureParams() {
2327d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Bundle myExtras = mActivity.getIntent().getExtras();
2328d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (myExtras != null) {
2329d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mSaveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT);
2330d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCropValue = myExtras.getString("crop");
2331d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2332d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2333d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2334d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void showPostCaptureAlert() {
2335d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mIsImageCaptureIntent) {
2336d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Util.fadeOut(mShutterButton);
2337295c3f89cd90545caf40a7dd9263805c0836625fMichael Kolb            mOnScreenIndicators.setVisibility(View.GONE);
23388a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mMenu.setVisibility(View.GONE);
2339d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
234090ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb//            Util.fadeIn(mReviewRetakeButton);
2341d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Util.fadeIn((View) mReviewDoneButton);
2342d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2343d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2344d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2345d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void hidePostCaptureAlert() {
2346d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mIsImageCaptureIntent) {
2347295c3f89cd90545caf40a7dd9263805c0836625fMichael Kolb            mOnScreenIndicators.setVisibility(View.VISIBLE);
23488a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mMenu.setVisibility(View.VISIBLE);
234990ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb//            Util.fadeOut(mReviewRetakeButton);
2350d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Util.fadeOut((View) mReviewDoneButton);
2351d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2352d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Util.fadeIn(mShutterButton);
2353d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2354d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2355d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2356d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2357d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onSharedPreferenceChanged() {
2358d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // ignore the events after "onPause()"
2359d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused) return;
2360d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2361d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        boolean recordLocation = RecordLocationPreference.get(
2362d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPreferences, mContentResolver);
2363d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mLocationManager.recordLocation(recordLocation);
2364d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2365d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraParametersWhenIdle(UPDATE_PARAM_PREFERENCE);
2366d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setPreviewFrameLayoutAspectRatio();
2367d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateOnScreenIndicators();
2368d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2369d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2370d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2371d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onCameraPickerClicked(int cameraId) {
2372d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused || mPendingSwitchCameraId != -1) return;
2373d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2374d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPendingSwitchCameraId = cameraId;
2375d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (ApiHelper.HAS_SURFACE_TEXTURE) {
2376d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "Start to copy texture. cameraId=" + cameraId);
2377d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // We need to keep a preview frame for the animation before
2378d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // releasing the camera. This will trigger onPreviewTextureCopied.
2379b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb            ((CameraScreenNail) mActivity.mCameraScreenNail).copyTexture();
2380d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Disable all camera controls.
2381d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setCameraState(SWITCHING_CAMERA);
2382d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2383d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            switchCamera();
2384d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2385d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2386d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2387d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void switchCamera() {
2388d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused) return;
2389d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2390d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "Start to switch camera. id=" + mPendingSwitchCameraId);
2391d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraId = mPendingSwitchCameraId;
2392d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPendingSwitchCameraId = -1;
2393b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb        mPhotoControl.setCameraId(mCameraId);
2394d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2395d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // from onPause
2396d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        closeCamera();
2397d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        collapseCameraControls();
2398d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFaceView != null) mFaceView.clear();
2399d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusManager != null) mFocusManager.removeMessages();
2400d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2401d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Restart the camera and initialize the UI. From onCreate.
2402d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreferences.setLocalId(mActivity, mCameraId);
2403d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
2404d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        try {
2405f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            mCameraDevice = Util.openCamera(mActivity, mCameraId);
2406f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            mParameters = mCameraDevice.getParameters();
2407f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li        } catch (CameraHardwareException e) {
2408f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            Util.showErrorAndFinish(mActivity, R.string.cannot_connect_camera);
2409f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            return;
2410f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li        } catch (CameraDisabledException e) {
2411f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            Util.showErrorAndFinish(mActivity, R.string.camera_disabled);
2412f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            return;
2413d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2414d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeCapabilities();
2415d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
2416d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        boolean mirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
2417d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.setMirror(mirror);
2418d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.setParameters(mInitialParams);
2419d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setupPreview();
2420b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb        loadCameraPreferences();
2421b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb        initializePhotoControl();
2422d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2423d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // from initializeFirstTime
2424d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeZoom();
2425d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateOnScreenIndicators();
2426d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        showTapToFocusToastIfNeeded();
2427d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2428d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (ApiHelper.HAS_SURFACE_TEXTURE) {
2429d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Start switch camera animation. Post a message because
2430d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // onFrameAvailable from the old camera may already exist.
2431d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mHandler.sendEmptyMessage(SWITCH_CAMERA_START_ANIMATION);
2432d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2433d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2434d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
24351373b98135b2854578111dd535e24d39c8853f14Michael Kolb    @Override
24361373b98135b2854578111dd535e24d39c8853f14Michael Kolb    public void onPieOpened(int centerX, int centerY) {
24379f8b479c44e8fafda688ac0fab9882584a94d55fMichael Kolb        mActivity.cancelActivityTouchHandling();
24381373b98135b2854578111dd535e24d39c8853f14Michael Kolb        mActivity.setSwipingEnabled(false);
24398dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb        if (mFaceView != null) {
24408dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb            mFaceView.setBlockDraw(true);
24418dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb        }
24421373b98135b2854578111dd535e24d39c8853f14Michael Kolb    }
24431373b98135b2854578111dd535e24d39c8853f14Michael Kolb
24441373b98135b2854578111dd535e24d39c8853f14Michael Kolb    @Override
24451373b98135b2854578111dd535e24d39c8853f14Michael Kolb    public void onPieClosed() {
24461373b98135b2854578111dd535e24d39c8853f14Michael Kolb        mActivity.setSwipingEnabled(true);
24478dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb        if (mFaceView != null) {
24488dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb            mFaceView.setBlockDraw(false);
24498dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb        }
24501373b98135b2854578111dd535e24d39c8853f14Michael Kolb    }
24511373b98135b2854578111dd535e24d39c8853f14Michael Kolb
2452d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Preview texture has been copied. Now camera can be released and the
2453d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // animation can be started.
2454d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2455d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onPreviewTextureCopied() {
2456d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.sendEmptyMessage(SWITCH_CAMERA);
2457d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2458d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2459d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2460d282a28c60ca8f4ee82d519b12e8b0b057b294d4George Mount    public void onCaptureTextureCopied() {
2461d282a28c60ca8f4ee82d519b12e8b0b057b294d4George Mount    }
2462d282a28c60ca8f4ee82d519b12e8b0b057b294d4George Mount
2463d282a28c60ca8f4ee82d519b12e8b0b057b294d4George Mount    @Override
2464d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onUserInteraction() {
2465fe269902ba8522e01baa9be45755779f050c1d6cWu-cheng Li        if (!mActivity.isFinishing()) keepScreenOnAwhile();
2466d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2467d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2468d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void resetScreenOn() {
2469d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(CLEAR_SCREEN_DELAY);
2470d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2471d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2472d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2473d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void keepScreenOnAwhile() {
2474d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(CLEAR_SCREEN_DELAY);
2475d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2476d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.sendEmptyMessageDelayed(CLEAR_SCREEN_DELAY, SCREEN_DELAY);
2477d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2478d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
247957abb9e7d8c88de1431b1af75b2871bee3e09e42Doris Liu    // TODO: Delete this function after old camera code is removed
2480d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2481d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onRestorePreferencesClicked() {
2482d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2483d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2484d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2485d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onOverriddenPreferencesClicked() {
2486d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused) return;
2487d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mNotSelectableToast == null) {
2488d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            String str = mActivity.getResources().getString(R.string.not_selectable_in_scene_mode);
2489d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mNotSelectableToast = Toast.makeText(mActivity, str, Toast.LENGTH_SHORT);
2490d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2491d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mNotSelectableToast.show();
2492d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2493d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2494d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void showTapToFocusToast() {
2495d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        new RotateTextToast(mActivity, R.string.tap_to_focus, mOrientationCompensation).show();
2496d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Clear the preference.
2497d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Editor editor = mPreferences.edit();
2498d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        editor.putBoolean(CameraSettings.KEY_CAMERA_FIRST_USE_HINT_SHOWN, false);
2499d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        editor.apply();
2500d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2501d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2502d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeCapabilities() {
2503d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mInitialParams = mCameraDevice.getParameters();
2504d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusAreaSupported = Util.isFocusAreaSupported(mInitialParams);
2505d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mMeteringAreaSupported = Util.isMeteringAreaSupported(mInitialParams);
2506d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mAeLockSupported = Util.isAutoExposureLockSupported(mInitialParams);
2507d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mAwbLockSupported = Util.isAutoWhiteBalanceLockSupported(mInitialParams);
2508d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mContinousFocusSupported = mInitialParams.getSupportedFocusModes().contains(
2509d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Util.FOCUS_MODE_CONTINUOUS_PICTURE);
2510d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2511d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2512d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // PreviewFrameLayout size has changed.
2513d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2514d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onSizeChanged(int width, int height) {
2515d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusManager != null) mFocusManager.setPreviewSize(width, height);
2516d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2517d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2518d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    void setPreviewFrameLayoutAspectRatio() {
2519d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set the preview frame aspect ratio according to the picture size.
2520d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Size size = mParameters.getPictureSize();
2521d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreviewFrameLayout.setAspectRatio((double) size.width / size.height);
2522d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2523d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
252490ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb    @Override
252590ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb    public boolean needsSwitcher() {
252690ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb        return !mIsImageCaptureIntent;
252790ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb    }
252890ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb
25292d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb    public void showPopup(AbstractSettingPopup popup) {
25302d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        mActivity.hideUI();
25312d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        mPopup = popup;
25323ce75c9a73de62e311beb79bdc8b5a2e7a096a81Doris Liu        // Make sure popup is brought up with the right orientation
25333ce75c9a73de62e311beb79bdc8b5a2e7a096a81Doris Liu        mPopup.setOrientation(mOrientationCompensation, false);
25342d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        mPopup.setVisibility(View.VISIBLE);
25352d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
25362d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb                LayoutParams.WRAP_CONTENT);
25372d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        lp.gravity = Gravity.CENTER;
25382d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        ((FrameLayout) mRootView).addView(mPopup, lp);
25392d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb    }
25402d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb
2541f04692066fc7087560248b003c49e0377c056edfDoris Liu    public void dismissPopup(boolean topPopupOnly) {
254289fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb        dismissPopup(topPopupOnly, false);
254389fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb    }
254489fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb
254589fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb    private void dismissPopup(boolean topOnly, boolean fullScreen) {
254689fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb        if (!fullScreen) {
254789fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb            mActivity.showUI();
254889fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb        }
25492d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        if (mPopup != null) {
25502d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb            ((FrameLayout) mRootView).removeView(mPopup);
25512d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb            mPopup = null;
25522d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        }
255389fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb        mPhotoControl.popupDismissed(topOnly);
25542d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb    }
25552d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb
2556561af39348f34db4d67bc954536d6f6cbf296054John Reck    @Override
2557561af39348f34db4d67bc954536d6f6cbf296054John Reck    public void onShowSwitcherPopup() {
2558561af39348f34db4d67bc954536d6f6cbf296054John Reck        if (mPieRenderer.showsItems()) {
2559561af39348f34db4d67bc954536d6f6cbf296054John Reck            mPieRenderer.hide();
2560561af39348f34db4d67bc954536d6f6cbf296054John Reck        }
2561561af39348f34db4d67bc954536d6f6cbf296054John Reck    }
2562561af39348f34db4d67bc954536d6f6cbf296054John Reck
2563d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb}
2564