PhotoModule.java revision 8a27da058a8318c876fb18b1f4964724b5508d6b
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;
1525ed54678384a922550428bb7ba1a3f2475c70bd3Michael Kolb    private List<Integer> mZoomRatios;
153d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
154d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Parameters mInitialParams;
155d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mFocusAreaSupported;
156d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mMeteringAreaSupported;
157d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mAeLockSupported;
158d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mAwbLockSupported;
159d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mContinousFocusSupported;
160d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
161d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The degrees of the device rotated clockwise from its natural orientation.
162d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
1631acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    // The orientation compensation for icons and dialogs. Ex: if the value
164d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // is 90, the UI components should be rotated 90 degrees counter-clockwise.
165d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mOrientationCompensation = 0;
166d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ComboPreferences mPreferences;
167d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
168d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final String sTempCropFilename = "crop-temp";
169d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
170d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ContentProviderClient mMediaProviderClient;
171d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ShutterButton mShutterButton;
172d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mFaceDetectionStarted = false;
173d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
174d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private PreviewFrameLayout mPreviewFrameLayout;
175d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Object mSurfaceTexture;
176d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
177d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // for API level 10
178d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private PreviewSurfaceView mPreviewSurfaceView;
179d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private volatile SurfaceHolder mCameraSurfaceHolder;
180d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
181d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private FaceView mFaceView;
182b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb    private RenderOverlay mRenderOverlay;
183d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Rotatable mReviewCancelButton;
184d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Rotatable mReviewDoneButton;
1858a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb    private View mReviewRetakeButton;
186d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
187d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // mCropValue and mSaveUri are used only if isImageCaptureIntent() is true.
188d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private String mCropValue;
189d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Uri mSaveUri;
190d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1918a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb    private View mMenu;
1928a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb    private View mBlocker;
1938a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb
194d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Small indicators which show the camera settings in the viewfinder.
1951a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    private ImageView mExposureIndicator;
196d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ImageView mFlashIndicator;
197d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ImageView mSceneIndicator;
1981a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    private ImageView mHdrIndicator;
199d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // A view group that contains all the small indicators.
2001a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    private RotateLayout mOnScreenIndicators;
201d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2021acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    // We use a thread in ImageSaver to do the work of saving images. This
2031acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li    // reduces the shot-to-shot time.
204d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ImageSaver mImageSaver;
205d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Similarly, we use a thread to generate the name of the picture and insert
206d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // it into MediaStore while picture taking is still in progress.
207d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ImageNamer mImageNamer;
208d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
209d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Runnable mDoSnapRunnable = new Runnable() {
210d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
211d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void run() {
212d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            onShutterButtonClick();
213d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
214d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    };
215d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
216d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final StringBuilder mBuilder = new StringBuilder();
217d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final Formatter mFormatter = new Formatter(mBuilder);
218d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final Object[] mFormatterArgs = new Object[1];
219d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
220d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    /**
221d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     * An unpublished intent flag requesting to return as soon as capturing
222d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     * is completed.
223d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     *
224d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     * TODO: consider publishing by moving into MediaStore.
225d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     */
226d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final String EXTRA_QUICK_CAPTURE =
227d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            "android.intent.extra.quickCapture";
228d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
229d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The display rotation in degrees. This is only valid when mCameraState is
230d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // not PREVIEW_STOPPED.
231d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mDisplayRotation;
232d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The value for android.hardware.Camera.setDisplayOrientation.
233d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mCameraDisplayOrientation;
234d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The value for UI components like indicators.
235d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mDisplayOrientation;
236d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The value for android.hardware.Camera.Parameters.setRotation.
237d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mJpegRotation;
238d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mFirstTimeInitialized;
239d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mIsImageCaptureIntent;
240d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
241d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int PREVIEW_STOPPED = 0;
242d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int IDLE = 1;  // preview is active
243d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Focus is in progress. The exact focus state is in Focus.java.
244d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int FOCUSING = 2;
245d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int SNAPSHOT_IN_PROGRESS = 3;
246d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Switching between cameras.
247d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static final int SWITCHING_CAMERA = 4;
248d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int mCameraState = PREVIEW_STOPPED;
249d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mSnapshotOnIdle = false;
250d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
251d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private ContentResolver mContentResolver;
252d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
253d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private LocationManager mLocationManager;
254d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
255d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final ShutterCallback mShutterCallback = new ShutterCallback();
256d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final PostViewPictureCallback mPostViewPictureCallback =
257d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            new PostViewPictureCallback();
258d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final RawPictureCallback mRawPictureCallback =
259d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            new RawPictureCallback();
260d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final AutoFocusCallback mAutoFocusCallback =
261d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            new AutoFocusCallback();
262d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final Object mAutoFocusMoveCallback =
263d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK
264d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            ? new AutoFocusMoveCallback()
265d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            : null;
266d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
267d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final CameraErrorCallback mErrorCallback = new CameraErrorCallback();
268d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
269d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mFocusStartTime;
270d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mShutterCallbackTime;
271d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mPostViewPictureCallbackTime;
272d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mRawPictureCallbackTime;
273d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mJpegPictureCallbackTime;
274d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private long mOnResumeTime;
275d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private byte[] mJpegImageData;
276d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
277d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // These latency time are for the CameraLatency test.
278d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mAutoFocusTime;
279d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mShutterLag;
280d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mShutterToPictureDisplayedTime;
281d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mPictureDisplayedToJpegCallbackTime;
282d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mJpegCallbackFinishTime;
283d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public long mCaptureStartTime;
284d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
285d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // This handles everything about focus.
286b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb    private FocusOverlayManager mFocusManager;
287b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb
2881373b98135b2854578111dd535e24d39c8853f14Michael Kolb    private PieRenderer mPieRenderer;
289b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb    private PhotoController mPhotoControl;
2901373b98135b2854578111dd535e24d39c8853f14Michael Kolb
2914d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb    private ZoomRenderer mZoomRenderer;
2924d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb
293d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private String mSceneMode;
294d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private Toast mNotSelectableToast;
295d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
296d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final Handler mHandler = new MainHandler();
297d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private PreferenceGroup mPreferenceGroup;
298d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
299d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean mQuickCapture;
300d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
301d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    CameraStartUpThread mCameraStartUpThread;
302d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    ConditionVariable mStartPreviewPrerequisiteReady = new ConditionVariable();
303d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
3044d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb    private PreviewGestures mGestures;
3054d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb
306d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The purpose is not to block the main thread in onCreate and onResume.
307d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private class CameraStartUpThread extends Thread {
308d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private volatile boolean mCancelled;
309d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
310d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void cancel() {
311d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCancelled = true;
312d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
313d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
314d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
315d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void run() {
316d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            try {
317d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // We need to check whether the activity is paused before long
318d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // operations to ensure that onPause() can be done ASAP.
319d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mCancelled) return;
320d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraDevice = Util.openCamera(mActivity, mCameraId);
321d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mParameters = mCameraDevice.getParameters();
322d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // Wait until all the initialization needed by startPreview are
323d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // done.
324d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mStartPreviewPrerequisiteReady.block();
325d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
326d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                initializeCapabilities();
327d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mFocusManager == null) initializeFocusManager();
328d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mCancelled) return;
329d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                setCameraParameters(UPDATE_PARAM_ALL);
330d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessage(CAMERA_OPEN_DONE);
331d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mCancelled) return;
332d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                startPreview();
333d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessage(START_PREVIEW_DONE);
334d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mOnResumeTime = SystemClock.uptimeMillis();
335d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessage(CHECK_DISPLAY_ROTATION);
336d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } catch (CameraHardwareException e) {
337d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessage(OPEN_CAMERA_FAIL);
338d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } catch (CameraDisabledException e) {
339d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessage(CAMERA_DISABLED);
340d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
341d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
342d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
343d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
344d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    /**
345d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     * This Handler is used to post message back onto the main thread of the
346d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     * application
347d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb     */
348d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private class MainHandler extends Handler {
349d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
350d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void handleMessage(Message msg) {
351d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            switch (msg.what) {
352d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case SETUP_PREVIEW: {
353d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    setupPreview();
354d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
355d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
356d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
357d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case CLEAR_SCREEN_DELAY: {
358d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mActivity.getWindow().clearFlags(
359d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
360d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
361d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
362d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
363d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case FIRST_TIME_INIT: {
364d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    initializeFirstTime();
365d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
366d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
367d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
368d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case SET_CAMERA_PARAMETERS_WHEN_IDLE: {
369d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    setCameraParametersWhenIdle(0);
370d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
371d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
372d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
373d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case CHECK_DISPLAY_ROTATION: {
374d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // Set the display orientation if display rotation has changed.
375d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // Sometimes this happens when the device is held upside
376d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // down and camera app is opened. Rotation animation will
377d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // take some time and the rotation value we have got may be
378d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // wrong. Framework does not have a callback for this now.
379d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (Util.getDisplayRotation(mActivity) != mDisplayRotation) {
380d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        setDisplayOrientation();
381d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
382d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (SystemClock.uptimeMillis() - mOnResumeTime < 5000) {
383d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mHandler.sendEmptyMessageDelayed(CHECK_DISPLAY_ROTATION, 100);
384d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
385d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
386d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
387d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
388d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case SHOW_TAP_TO_FOCUS_TOAST: {
389d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    showTapToFocusToast();
390d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
391d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
392d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
393d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case SWITCH_CAMERA: {
394d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    switchCamera();
395d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
396d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
397d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
398d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case SWITCH_CAMERA_START_ANIMATION: {
399b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb                    ((CameraScreenNail) mActivity.mCameraScreenNail).animateSwitchCamera();
400d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
401d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
402d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
403d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case CAMERA_OPEN_DONE: {
404d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    initializeAfterCameraOpen();
405d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
406d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
407d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
408d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case START_PREVIEW_DONE: {
409d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mCameraStartUpThread = null;
410d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    setCameraState(IDLE);
411d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (!ApiHelper.HAS_SURFACE_TEXTURE) {
412d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // This may happen if surfaceCreated has arrived.
413d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mCameraDevice.setPreviewDisplayAsync(mCameraSurfaceHolder);
414d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
415d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    startFaceDetection();
416eebbf302dfa981b83a1f445eee6e8674f9309accWu-cheng Li                    locationFirstRun();
417d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
418d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
419d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
420d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case OPEN_CAMERA_FAIL: {
421d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mCameraStartUpThread = null;
422d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mOpenCameraFail = true;
423d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    Util.showErrorAndFinish(mActivity,
424d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            R.string.cannot_connect_camera);
425d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
426d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
427d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
428d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                case CAMERA_DISABLED: {
429d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mCameraStartUpThread = null;
430d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mCameraDisabled = true;
431d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    Util.showErrorAndFinish(mActivity,
432d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            R.string.camera_disabled);
433d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    break;
434d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
435d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
436d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
437d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
438d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
439d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
440ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb    public void init(CameraActivity activity, View parent, boolean reuseNail) {
441d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity = activity;
442d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mRootView = parent;
443d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreferences = new ComboPreferences(mActivity);
444d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal());
445d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraId = getPreferredCameraId(mPreferences);
446d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
447d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mContentResolver = mActivity.getContentResolver();
448d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
449d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // To reduce startup time, open the camera and start the preview in
450d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // another thread.
451d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraStartUpThread = new CameraStartUpThread();
452d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraStartUpThread.start();
453d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
454d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.getLayoutInflater().inflate(R.layout.photo_module, (ViewGroup) mRootView);
455d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
456d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Surface texture is from camera screen nail and startPreview needs it.
457d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // This must be done before startPreview.
458d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mIsImageCaptureIntent = isImageCaptureIntent();
459ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb        if (reuseNail) {
460ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb            mActivity.reuseCameraScreenNail(!mIsImageCaptureIntent);
461ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb        } else {
462ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb            mActivity.createCameraScreenNail(!mIsImageCaptureIntent);
463ce1d8396924bf4ca9be076cfdf9594c4d33ebd9bMichael Kolb        }
464d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
465d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreferences.setLocalId(mActivity, mCameraId);
466d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
467d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // we need to reset exposure for the preview
468d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        resetExposureCompensation();
469d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Starting the preview needs preferences, camera screen nail, and
470d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // focus area indicator.
471d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mStartPreviewPrerequisiteReady.open();
472d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
473d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeControlByIntent();
474d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mQuickCapture = mActivity.getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
475d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeMiscControls();
476d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mLocationManager = new LocationManager(mActivity, this);
477d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initOnScreenIndicator();
478f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    }
479f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck
480f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    // Prompt the user to pick to record location for the very first run of
481f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    // camera only
482f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    private void locationFirstRun() {
483f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        if (RecordLocationPreference.isSet(mPreferences)) {
484f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            return;
485f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        }
48659d61767a832eb69f68c1d131af6c59fff7e8616Doris Liu        // Check if the back camera exists
48759d61767a832eb69f68c1d131af6c59fff7e8616Doris Liu        int backCameraId = CameraHolder.instance().getBackCameraId();
48859d61767a832eb69f68c1d131af6c59fff7e8616Doris Liu        if (backCameraId == -1) {
48959d61767a832eb69f68c1d131af6c59fff7e8616Doris Liu            // If there is no back camera, do not show the prompt.
49059d61767a832eb69f68c1d131af6c59fff7e8616Doris Liu            return;
49159d61767a832eb69f68c1d131af6c59fff7e8616Doris Liu        }
49259d61767a832eb69f68c1d131af6c59fff7e8616Doris Liu
493f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        new AlertDialog.Builder(mActivity)
494f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .setTitle(R.string.remember_location_title)
495f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .setMessage(R.string.remember_location_prompt)
496f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .setPositiveButton(R.string.remember_location_yes, new DialogInterface.OnClickListener() {
497f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                @Override
498f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                public void onClick(DialogInterface dialog, int arg1) {
499f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                    setLocationPreference(RecordLocationPreference.VALUE_ON);
500f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                }
501f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            })
502f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .setNegativeButton(R.string.remember_location_no, new DialogInterface.OnClickListener() {
503f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                @Override
504f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                public void onClick(DialogInterface dialog, int arg1) {
505f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                    dialog.cancel();
506f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                }
507f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            })
508f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .setOnCancelListener(new DialogInterface.OnCancelListener() {
509f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                @Override
510f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                public void onCancel(DialogInterface dialog) {
511f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                    setLocationPreference(RecordLocationPreference.VALUE_OFF);
512f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck                }
513f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            })
514f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .show();
515f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    }
516f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck
517f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck    private void setLocationPreference(String value) {
518f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        mPreferences.edit()
519f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .putString(RecordLocationPreference.KEY, value)
520f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck            .apply();
521f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        // TODO: Fix this to use the actual onSharedPreferencesChanged listener
522f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        // instead of invoking manually
523f1f3d8f514fc8e30632ca0935bf030d2b7b7262bJohn Reck        onSharedPreferenceChanged();
524d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
525d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
526471c997edfddccfa0762fa720c82cc95777584e8John Reck    private void initializeRenderOverlay() {
527471c997edfddccfa0762fa720c82cc95777584e8John Reck        if (mPieRenderer != null) {
528471c997edfddccfa0762fa720c82cc95777584e8John Reck            mRenderOverlay.addRenderer(mPieRenderer);
529b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu            mFocusManager.setFocusRenderer(mPieRenderer);
530471c997edfddccfa0762fa720c82cc95777584e8John Reck        }
531471c997edfddccfa0762fa720c82cc95777584e8John Reck        if (mZoomRenderer != null) {
532471c997edfddccfa0762fa720c82cc95777584e8John Reck            mRenderOverlay.addRenderer(mZoomRenderer);
533471c997edfddccfa0762fa720c82cc95777584e8John Reck        }
534471c997edfddccfa0762fa720c82cc95777584e8John Reck        if (mGestures != null) {
535471c997edfddccfa0762fa720c82cc95777584e8John Reck            mGestures.clearTouchReceivers();
536471c997edfddccfa0762fa720c82cc95777584e8John Reck            mGestures.setRenderOverlay(mRenderOverlay);
5378a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mGestures.addTouchReceiver(mBlocker);
5388a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mGestures.addTouchReceiver(mMenu);
5398a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb
540471c997edfddccfa0762fa720c82cc95777584e8John Reck            if (isImageCaptureIntent()) {
541471c997edfddccfa0762fa720c82cc95777584e8John Reck                if (mReviewCancelButton != null) {
542471c997edfddccfa0762fa720c82cc95777584e8John Reck                    mGestures.addTouchReceiver((View) mReviewCancelButton);
543471c997edfddccfa0762fa720c82cc95777584e8John Reck                }
544471c997edfddccfa0762fa720c82cc95777584e8John Reck                if (mReviewDoneButton != null) {
545471c997edfddccfa0762fa720c82cc95777584e8John Reck                    mGestures.addTouchReceiver((View) mReviewDoneButton);
546471c997edfddccfa0762fa720c82cc95777584e8John Reck                }
547471c997edfddccfa0762fa720c82cc95777584e8John Reck            }
548471c997edfddccfa0762fa720c82cc95777584e8John Reck        }
549471c997edfddccfa0762fa720c82cc95777584e8John Reck        mRenderOverlay.requestLayout();
550471c997edfddccfa0762fa720c82cc95777584e8John Reck    }
551471c997edfddccfa0762fa720c82cc95777584e8John Reck
552d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeAfterCameraOpen() {
5531373b98135b2854578111dd535e24d39c8853f14Michael Kolb        if (mPieRenderer == null) {
5541373b98135b2854578111dd535e24d39c8853f14Michael Kolb            mPieRenderer = new PieRenderer(mActivity);
5552d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb            mPhotoControl = new PhotoController(mActivity, this, mPieRenderer);
556b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb            mPhotoControl.setListener(this);
5571373b98135b2854578111dd535e24d39c8853f14Michael Kolb            mPieRenderer.setPieListener(this);
5581373b98135b2854578111dd535e24d39c8853f14Michael Kolb        }
5594d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        if (mZoomRenderer == null) {
5604d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb            mZoomRenderer = new ZoomRenderer(mActivity);
5614d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        }
5627a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb        if (mGestures == null) {
5637a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb            // this will handle gesture disambiguation and dispatching
564471c997edfddccfa0762fa720c82cc95777584e8John Reck            mGestures = new PreviewGestures(mActivity, this, mZoomRenderer, mPieRenderer);
5657a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb        }
566471c997edfddccfa0762fa720c82cc95777584e8John Reck        initializeRenderOverlay();
567b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb        initializePhotoControl();
568b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb
569d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // These depend on camera parameters.
570d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setPreviewFrameLayoutAspectRatio();
571d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.setPreviewSize(mPreviewFrameLayout.getWidth(),
572d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPreviewFrameLayout.getHeight());
573b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb        loadCameraPreferences();
574d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeZoom();
575d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateOnScreenIndicators();
576d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        showTapToFocusToastIfNeeded();
577d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
578d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
579b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb    private void initializePhotoControl() {
5801373b98135b2854578111dd535e24d39c8853f14Michael Kolb        loadCameraPreferences();
5819667bf4a16d01c58a5b91eebede8df13e95f592dMichael Kolb        if (mPhotoControl != null) {
5829667bf4a16d01c58a5b91eebede8df13e95f592dMichael Kolb            mPhotoControl.initialize(mPreferenceGroup);
5839667bf4a16d01c58a5b91eebede8df13e95f592dMichael Kolb        }
5841373b98135b2854578111dd535e24d39c8853f14Michael Kolb        updateSceneModeUI();
5851373b98135b2854578111dd535e24d39c8853f14Michael Kolb    }
5861373b98135b2854578111dd535e24d39c8853f14Michael Kolb
5871373b98135b2854578111dd535e24d39c8853f14Michael Kolb
588d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void resetExposureCompensation() {
589d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        String value = mPreferences.getString(CameraSettings.KEY_EXPOSURE,
590d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                CameraSettings.EXPOSURE_DEFAULT_VALUE);
591d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!CameraSettings.EXPOSURE_DEFAULT_VALUE.equals(value)) {
592d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Editor editor = mPreferences.edit();
593d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            editor.putString(CameraSettings.KEY_EXPOSURE, "0");
594d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            editor.apply();
595d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
596d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
597d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
598d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void keepMediaProviderInstance() {
599d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // We want to keep a reference to MediaProvider in camera's lifecycle.
600d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // TODO: Utilize mMediaProviderClient instance to replace
601d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // ContentResolver calls.
602d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mMediaProviderClient == null) {
603d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mMediaProviderClient = mContentResolver
604d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    .acquireContentProviderClient(MediaStore.AUTHORITY);
605d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
606d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
607d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
608d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Snapshots can only be taken after this is called. It should be called
609d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // once only. We could have done these things in onCreate() but we want to
610d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // make preview screen appear as soon as possible.
611d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeFirstTime() {
612d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFirstTimeInitialized) return;
613d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
614d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Initialize location service.
615d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        boolean recordLocation = RecordLocationPreference.get(
616d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPreferences, mContentResolver);
617d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mLocationManager.recordLocation(recordLocation);
618d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
619d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        keepMediaProviderInstance();
620d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
621d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Initialize shutter button.
622d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mShutterButton = mActivity.getShutterButton();
623566f11c903ceae2e53c645f8d630bb1331faf890Michael Kolb        mShutterButton.setImageResource(R.drawable.btn_new_shutter);
624d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mShutterButton.setOnShutterButtonListener(this);
625d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mShutterButton.setVisibility(View.VISIBLE);
626d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
627d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mImageSaver = new ImageSaver();
628d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mImageNamer = new ImageNamer();
629d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
630d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFirstTimeInitialized = true;
631d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        addIdleHandler();
632d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
633d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.updateStorageSpaceAndHint();
634d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
635d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
636d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void showTapToFocusToastIfNeeded() {
637d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Show the tap to focus toast if this is the first start.
638d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusAreaSupported &&
639d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPreferences.getBoolean(CameraSettings.KEY_CAMERA_FIRST_USE_HINT_SHOWN, true)) {
640d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Delay the toast for one second to wait for orientation.
641d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mHandler.sendEmptyMessageDelayed(SHOW_TAP_TO_FOCUS_TOAST, 1000);
642d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
643d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
644d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
645d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void addIdleHandler() {
646d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        MessageQueue queue = Looper.myQueue();
647d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        queue.addIdleHandler(new MessageQueue.IdleHandler() {
648d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            @Override
649d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            public boolean queueIdle() {
650d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Storage.ensureOSXCompatible();
651d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return false;
652d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
653d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        });
654d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
655d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
656d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // If the activity is paused and resumed, this method will be called in
657d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // onResume.
658d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeSecondTime() {
659d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
660d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Start location update if needed.
661d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        boolean recordLocation = RecordLocationPreference.get(
662d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPreferences, mContentResolver);
663d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mLocationManager.recordLocation(recordLocation);
664d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
665d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mImageSaver = new ImageSaver();
666d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mImageNamer = new ImageNamer();
667d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeZoom();
668d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        keepMediaProviderInstance();
669d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        hidePostCaptureAlert();
670d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
671b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb        if (mPhotoControl != null) {
672b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb            mPhotoControl.reloadPreferences();
6731373b98135b2854578111dd535e24d39c8853f14Michael Kolb        }
674d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
675d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
6764d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb    private class ZoomChangeListener implements ZoomRenderer.OnZoomChangedListener {
677d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
678d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onZoomValueChanged(int index) {
679d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Not useful to change zoom value when the activity is paused.
680d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mPaused) return;
681d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mZoomValue = index;
68250dfe58a04d7174d72a411a73845fba01ac02dd0Michael Kolb            if (mParameters == null || mCameraDevice != null) return;
683d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Set zoom parameters asynchronously
684d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setZoom(mZoomValue);
685d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setParametersAsync(mParameters);
6865ed54678384a922550428bb7ba1a3f2475c70bd3Michael Kolb            if (mZoomRenderer != null) {
6875ed54678384a922550428bb7ba1a3f2475c70bd3Michael Kolb                Parameters p = mCameraDevice.getParameters();
6885ed54678384a922550428bb7ba1a3f2475c70bd3Michael Kolb                mZoomRenderer.setZoomValue(mZoomRatios.get(p.getZoom()));
6895ed54678384a922550428bb7ba1a3f2475c70bd3Michael Kolb            }
690d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
6914d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb
6924d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        @Override
6934d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        public void onZoomStart() {
694fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb            if (mPieRenderer != null) {
695fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb                mPieRenderer.setBlockFocus(true);
6964d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb            }
6974d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        }
6984d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb
6994d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        @Override
7004d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        public void onZoomEnd() {
701fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb            if (mPieRenderer != null) {
702fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb                mPieRenderer.setBlockFocus(false);
7034d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb            }
7044d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        }
705d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
706d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
707d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeZoom() {
708ba6a355912c8f206140d85e393f7d1ab29f6b0fcMichael Kolb        if (!mParameters.isZoomSupported() || (mZoomRenderer == null)) return;
709d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mZoomMax = mParameters.getMaxZoom();
7105ed54678384a922550428bb7ba1a3f2475c70bd3Michael Kolb        mZoomRatios = mParameters.getZoomRatios();
7114d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        // Currently we use immediate zoom for fast zooming to get better UX and
7124d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        // there is no plan to take advantage of the smooth zoom.
7132ff430a16266c59549ebac205bead2e72638c53cGeorge Mount        if (mZoomRenderer != null) {
7142ff430a16266c59549ebac205bead2e72638c53cGeorge Mount            mZoomRenderer.setZoomMax(mZoomMax);
7150af8a3b2ce98424614dd2ac33ec896142851dab5Michael Kolb            mZoomRenderer.setZoom(mParameters.getZoom());
7165ed54678384a922550428bb7ba1a3f2475c70bd3Michael Kolb            mZoomRenderer.setZoomValue(mZoomRatios.get(mParameters.getZoom()));
7172ff430a16266c59549ebac205bead2e72638c53cGeorge Mount            mZoomRenderer.setOnZoomChangeListener(new ZoomChangeListener());
7182ff430a16266c59549ebac205bead2e72638c53cGeorge Mount        }
719d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
720d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
721d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
722d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
723d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void startFaceDetection() {
724d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!ApiHelper.HAS_FACE_DETECTION) return;
725d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFaceDetectionStarted) return;
726d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mParameters.getMaxNumDetectedFaces() > 0) {
727d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceDetectionStarted = true;
728d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.clear();
729d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setVisibility(View.VISIBLE);
730d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setDisplayOrientation(mDisplayOrientation);
731d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
732d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setMirror(info.facing == CameraInfo.CAMERA_FACING_FRONT);
733d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.resume();
734d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.setFaceView(mFaceView);
735d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setFaceDetectionListener(new FaceDetectionListener() {
736d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                @Override
737d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                public void onFaceDetection(Face[] faces, android.hardware.Camera camera) {
738d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mFaceView.setFaces(faces);
739d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
740d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            });
741d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.startFaceDetection();
742d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
743d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
744d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
745d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
746d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
747d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void stopFaceDetection() {
748d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!ApiHelper.HAS_FACE_DETECTION) return;
749d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!mFaceDetectionStarted) return;
750d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mParameters.getMaxNumDetectedFaces() > 0) {
751d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceDetectionStarted = false;
752d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setFaceDetectionListener(null);
753d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.stopFaceDetection();
754d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mFaceView != null) mFaceView.clear();
755d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
756d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
757d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
758d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
759d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean dispatchTouchEvent(MotionEvent m) {
760d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraState == SWITCHING_CAMERA) return true;
7617a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb        if (mPopup != null) {
7624d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb            return mActivity.superDispatchTouchEvent(m);
7637a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb        } else if (mGestures != null && mRenderOverlay != null) {
7647a6696dbdd858e07319b3981a405c575460fde3bMichael Kolb            return mGestures.dispatchTouch(m);
76515e0d3b07902a5f398e101312495125a3d5a0384Michael Kolb        }
766d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return false;
767d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
768d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
769d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initOnScreenIndicator() {
7701a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mOnScreenIndicators = (RotateLayout) mRootView.findViewById(R.id.on_screen_indicators);
7711a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mExposureIndicator = (ImageView) mOnScreenIndicators.findViewById(R.id.menu_exposure_indicator);
7721a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mFlashIndicator = (ImageView) mOnScreenIndicators.findViewById(R.id.menu_flash_indicator);
7731a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mSceneIndicator = (ImageView) mOnScreenIndicators.findViewById(R.id.menu_scenemode_indicator);
7741a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mHdrIndicator = (ImageView) mOnScreenIndicators.findViewById(R.id.menu_hdr_indicator);
775d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
776d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
777d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
7781a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    public void showGpsOnScreenIndicator(boolean hasSignal) { }
779d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
780d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
7811a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    public void hideGpsOnScreenIndicator() { }
782d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
783d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateExposureOnScreenIndicator(int value) {
784d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mExposureIndicator == null) {
785d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
786d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
7871a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        int id = 0;
7881a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        float step = mParameters.getExposureCompensationStep();
7891a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        value = (int) Math.round(value * step);
7901a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        switch(value) {
7911a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case -3:
7921a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_n3;
7931a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
7941a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case -2:
7951a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_n2;
7961a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
7971a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case -1:
7981a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_n1;
7991a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
8001a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case 0:
8011a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_0;
8021a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
8031a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case 1:
8041a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_p1;
8051a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
8061a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case 2:
8071a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_p2;
8081a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
8091a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        case 3:
8101a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            id = R.drawable.ic_indicator_ev_p3;
8111a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            break;
8121a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        }
8131a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        mExposureIndicator.setImageResource(id);
8141a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb
815d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
816d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
817d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateFlashOnScreenIndicator(String value) {
818d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFlashIndicator == null) {
819d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
820d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
821d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (value == null || Parameters.FLASH_MODE_OFF.equals(value)) {
8221a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            mFlashIndicator.setImageResource(R.drawable.ic_indicator_flash_off);
823d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
824d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (Parameters.FLASH_MODE_AUTO.equals(value)) {
8251a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb                mFlashIndicator.setImageResource(R.drawable.ic_indicator_flash_auto);
826d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else if (Parameters.FLASH_MODE_ON.equals(value)) {
8271a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb                mFlashIndicator.setImageResource(R.drawable.ic_indicator_flash_on);
828d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
8291a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb                mFlashIndicator.setImageResource(R.drawable.ic_indicator_flash_off);
830d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
831d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
832d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
833d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
834d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateSceneOnScreenIndicator(String value) {
835d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mSceneIndicator == null) {
836d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
837d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
838e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb        if ((value == null) || Parameters.SCENE_MODE_AUTO.equals(value)
839e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb                || Parameters.SCENE_MODE_HDR.equals(value)) {
8401a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            mSceneIndicator.setImageResource(R.drawable.ic_indicator_sce_off);
841d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
8421a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb            mSceneIndicator.setImageResource(R.drawable.ic_indicator_sce_on);
843d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
844d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
845d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
8461a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb    private void updateHdrOnScreenIndicator(String value) {
8471a753f543b84b99b33e1209acfe39cdf2b12297aMichael Kolb        if (mHdrIndicator == null) {
848d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
849d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
850e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb        if ((value != null) && Parameters.SCENE_MODE_HDR.equals(value)) {
851e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb            mHdrIndicator.setImageResource(R.drawable.ic_indicator_hdr_on);
852e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb        } else {
853e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb            mHdrIndicator.setImageResource(R.drawable.ic_indicator_hdr_off);
854e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb        }
855d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
856d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
857d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateOnScreenIndicators() {
858d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateSceneOnScreenIndicator(mParameters.getSceneMode());
859d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateExposureOnScreenIndicator(CameraSettings.readExposure(mPreferences));
860d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateFlashOnScreenIndicator(mParameters.getFlashMode());
861e1fa2543424be206ed29efa5f0ebcaff3766ed99Michael Kolb        updateHdrOnScreenIndicator(mParameters.getSceneMode());
862d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
863d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
864d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class ShutterCallback
865d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            implements android.hardware.Camera.ShutterCallback {
866d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
867d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onShutter() {
868d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mShutterCallbackTime = System.currentTimeMillis();
869d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mShutterLag = mShutterCallbackTime - mCaptureStartTime;
870d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mShutterLag = " + mShutterLag + "ms");
871d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
872d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
873d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
874d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class PostViewPictureCallback implements PictureCallback {
875d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
876d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onPictureTaken(
877d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                byte [] data, android.hardware.Camera camera) {
878d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPostViewPictureCallbackTime = System.currentTimeMillis();
879d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mShutterToPostViewCallbackTime = "
880d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + (mPostViewPictureCallbackTime - mShutterCallbackTime)
881d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + "ms");
882d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
883d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
884d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
885d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class RawPictureCallback implements PictureCallback {
886d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
887d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onPictureTaken(
888d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                byte [] rawData, android.hardware.Camera camera) {
889d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mRawPictureCallbackTime = System.currentTimeMillis();
890d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mShutterToRawCallbackTime = "
891d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms");
892d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
893d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
894d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
895d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class JpegPictureCallback implements PictureCallback {
896d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Location mLocation;
897d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
898d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public JpegPictureCallback(Location loc) {
899d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mLocation = loc;
900d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
901d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
902d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
903d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onPictureTaken(
904d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                final byte [] jpegData, final android.hardware.Camera camera) {
905d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mPaused) {
906d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return;
907d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
908d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
909d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mJpegPictureCallbackTime = System.currentTimeMillis();
910d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // If postview callback has arrived, the captured image is displayed
911d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // in postview callback. If not, the captured image is displayed in
912d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // raw picture callback.
913d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mPostViewPictureCallbackTime != 0) {
914d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mShutterToPictureDisplayedTime =
915d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mPostViewPictureCallbackTime - mShutterCallbackTime;
916d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPictureDisplayedToJpegCallbackTime =
917d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mJpegPictureCallbackTime - mPostViewPictureCallbackTime;
918d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
919d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mShutterToPictureDisplayedTime =
920d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mRawPictureCallbackTime - mShutterCallbackTime;
921d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPictureDisplayedToJpegCallbackTime =
922d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mJpegPictureCallbackTime - mRawPictureCallbackTime;
923d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
924d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = "
925d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + mPictureDisplayedToJpegCallbackTime + "ms");
926d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
927b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu            // Only animate when in full screen capture mode
928b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu            // i.e. If monkey/a user swipes to the gallery during picture taking,
929b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu            // don't show animation
930b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu            if (ApiHelper.HAS_SURFACE_TEXTURE && !mIsImageCaptureIntent
931b757930a43743fa6cbf3f9271cb068be7ffd4e45Doris Liu                    && mActivity.mShowCameraAppView) {
93270d41293e9dad8be7a1f2c556ff7c7334a60c8f5John Reck                // Finish capture animation
93370d41293e9dad8be7a1f2c556ff7c7334a60c8f5John Reck                ((CameraScreenNail) mActivity.mCameraScreenNail).animateSlide();
9341f6fd1f2a393f2d5808a908c6ef9b3a1542368b7John Reck            }
935d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden.
936d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (!mIsImageCaptureIntent) {
937d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (ApiHelper.CAN_START_PREVIEW_IN_JPEG_CALLBACK) {
938d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    setupPreview();
939d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } else {
940d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // Camera HAL of some devices have a bug. Starting preview
941d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // immediately after taking a picture will fail. Wait some
942d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // time before starting the preview.
943d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mHandler.sendEmptyMessageDelayed(SETUP_PREVIEW, 300);
944d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
945d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
946d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
947d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (!mIsImageCaptureIntent) {
948d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // Calculate the width and the height of the jpeg.
949d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Size s = mParameters.getPictureSize();
950d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                int orientation = Exif.getOrientation(jpegData);
951d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                int width, height;
952d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if ((mJpegRotation + orientation) % 180 == 0) {
953d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    width = s.width;
954d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    height = s.height;
955d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } else {
956d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    width = s.height;
957d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    height = s.width;
958d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
959d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Uri uri = mImageNamer.getUri();
960d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.addSecureAlbumItemIfNeeded(false, uri);
961d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                String title = mImageNamer.getTitle();
962d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mImageSaver.addImage(jpegData, uri, title, mLocation,
9631acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li                        width, height, orientation);
964d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
965d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mJpegImageData = jpegData;
966d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (!mQuickCapture) {
967d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    showPostCaptureAlert();
968d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } else {
969d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    doAttach();
970d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
971d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
972d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
973d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Check this in advance of each shot so we don't add to shutter
974d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // latency. It's true that someone else could write to the SD card in
975d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // the mean time and fill it, but that could have happened between the
976d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // shutter press and saving the JPEG too.
977d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mActivity.updateStorageSpaceAndHint();
978d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
979d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            long now = System.currentTimeMillis();
980d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mJpegCallbackFinishTime = now - mJpegPictureCallbackTime;
981d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mJpegCallbackFinishTime = "
982d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + mJpegCallbackFinishTime + "ms");
983d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mJpegPictureCallbackTime = 0;
984d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
985d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
986d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
987d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class AutoFocusCallback
988d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            implements android.hardware.Camera.AutoFocusCallback {
989d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
990d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onAutoFocus(
991d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                boolean focused, android.hardware.Camera camera) {
992d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mPaused) return;
993d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
994d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mAutoFocusTime = System.currentTimeMillis() - mFocusStartTime;
995d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms");
996d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setCameraState(IDLE);
997d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.onAutoFocus(focused);
998d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
999d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1000d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1001d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN)
1002d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private final class AutoFocusMoveCallback
1003d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            implements android.hardware.Camera.AutoFocusMoveCallback {
1004d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
1005d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onAutoFocusMoving(
1006d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            boolean moving, android.hardware.Camera camera) {
1007d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mFocusManager.onAutoFocusMoving(moving);
1008d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1009d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1010d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1011d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Each SaveRequest remembers the data needed to save an image.
1012d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static class SaveRequest {
1013d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        byte[] data;
1014d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Uri uri;
1015d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        String title;
1016d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Location loc;
1017d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int width, height;
1018d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int orientation;
1019d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1020d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1021d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // We use a queue to store the SaveRequests that have not been completed
1022d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // yet. The main thread puts the request into the queue. The saver thread
1023d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // gets it from the queue, does the work, and removes it from the queue.
1024d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    //
1025d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // The main thread needs to wait for the saver thread to finish all the work
1026d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // in the queue, when the activity's onPause() is called, we need to finish
1027d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // all the work, so other programs (like Gallery) can see all the images.
1028d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    //
1029d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // If the queue becomes too long, adding a new request will block the main
1030d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // thread until the queue length drops below the threshold (QUEUE_LIMIT).
1031d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // If we don't do this, we may face several problems: (1) We may OOM
1032d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // because we are holding all the jpeg data in memory. (2) We may ANR
1033d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // when we need to wait for saver thread finishing all the work (in
1034d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // onPause() or gotoGallery()) because the time to finishing a long queue
1035d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // of work may be too long.
1036d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private class ImageSaver extends Thread {
1037d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private static final int QUEUE_LIMIT = 3;
1038d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1039d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private ArrayList<SaveRequest> mQueue;
1040d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private boolean mStop;
1041d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1042d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1043d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public ImageSaver() {
1044d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mQueue = new ArrayList<SaveRequest>();
1045d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            start();
1046d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1047d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1048d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1049d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void addImage(final byte[] data, Uri uri, String title,
10501acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li                Location loc, int width, int height, int orientation) {
1051d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            SaveRequest r = new SaveRequest();
1052d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.data = data;
1053d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.uri = uri;
1054d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.title = title;
1055d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.loc = (loc == null) ? null : new Location(loc);  // make a copy
1056d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.width = width;
1057d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.height = height;
1058d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            r.orientation = orientation;
1059d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            synchronized (this) {
1060d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                while (mQueue.size() >= QUEUE_LIMIT) {
1061d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    try {
1062d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        wait();
1063d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    } catch (InterruptedException ex) {
1064d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // ignore.
1065d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1066d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1067d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mQueue.add(r);
1068d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                notifyAll();  // Tell saver thread there is new work to do.
1069d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1070d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1071d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1072d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in saver thread
1073d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
1074d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void run() {
1075d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            while (true) {
1076d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                SaveRequest r;
1077d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                synchronized (this) {
1078d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (mQueue.isEmpty()) {
1079d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        notifyAll();  // notify main thread in waitDone
1080d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1081d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // Note that we can only stop after we saved all images
1082d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // in the queue.
1083d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        if (mStop) break;
1084d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1085d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        try {
1086d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            wait();
1087d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        } catch (InterruptedException ex) {
1088d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            // ignore.
1089d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        }
1090d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        continue;
1091d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1092d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    r = mQueue.get(0);
1093d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1094d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                storeImage(r.data, r.uri, r.title, r.loc, r.width, r.height,
10951acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li                        r.orientation);
1096d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                synchronized (this) {
1097d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mQueue.remove(0);
1098d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    notifyAll();  // the main thread may wait in addImage
1099d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1100d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1101d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1102d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1103d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1104d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void waitDone() {
1105d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            synchronized (this) {
1106d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                while (!mQueue.isEmpty()) {
1107d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    try {
1108d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        wait();
1109d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    } catch (InterruptedException ex) {
1110d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // ignore.
1111d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1112d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1113d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1114d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1115d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1116d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1117d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void finish() {
1118d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            waitDone();
1119d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            synchronized (this) {
1120d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mStop = true;
1121d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                notifyAll();
1122d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1123d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            try {
1124d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                join();
1125d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } catch (InterruptedException ex) {
1126d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // ignore.
1127d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1128d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1129d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1130d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in saver thread
1131d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private void storeImage(final byte[] data, Uri uri, String title,
11321acc316227cf39a5c5eead6b307b8b3a76f5e284Wu-cheng Li                Location loc, int width, int height, int orientation) {
1133d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            boolean ok = Storage.updateImage(mContentResolver, uri, title, loc,
1134d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    orientation, data, width, height);
1135d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (ok) {
1136d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Util.broadcastNewPicture(mActivity, uri);
1137d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1138d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1139d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1140d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1141d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private static class ImageNamer extends Thread {
1142d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private boolean mRequestPending;
1143d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private ContentResolver mResolver;
1144d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private long mDateTaken;
1145d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private int mWidth, mHeight;
1146d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private boolean mStop;
1147d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private Uri mUri;
1148d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private String mTitle;
1149d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1150d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1151d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public ImageNamer() {
1152d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            start();
1153d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1154d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1155d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1156d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public synchronized void prepareUri(ContentResolver resolver,
1157d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                long dateTaken, int width, int height, int rotation) {
1158d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (rotation % 180 != 0) {
1159d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                int tmp = width;
1160d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                width = height;
1161d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                height = tmp;
1162d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1163d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mRequestPending = true;
1164d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mResolver = resolver;
1165d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mDateTaken = dateTaken;
1166d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mWidth = width;
1167d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mHeight = height;
1168d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            notifyAll();
1169d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1170d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1171d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1172d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public synchronized Uri getUri() {
1173d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // wait until the request is done.
1174d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            while (mRequestPending) {
1175d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                try {
1176d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    wait();
1177d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } catch (InterruptedException ex) {
1178d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // ignore.
1179d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1180d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1181d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1182d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // return the uri generated
1183d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Uri uri = mUri;
1184d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mUri = null;
1185d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return uri;
1186d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1187d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1188d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread, should be called after getUri().
1189d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public synchronized String getTitle() {
1190d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return mTitle;
1191d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1192d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1193d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in namer thread
1194d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        @Override
1195d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public synchronized void run() {
1196d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            while (true) {
1197d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mStop) break;
1198d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (!mRequestPending) {
1199d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    try {
1200d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        wait();
1201d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    } catch (InterruptedException ex) {
1202d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        // ignore.
1203d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1204d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    continue;
1205d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1206d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                cleanOldUri();
1207d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                generateUri();
1208d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mRequestPending = false;
1209d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                notifyAll();
1210d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1211d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            cleanOldUri();
1212d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1213d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1214d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in main thread
1215d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public synchronized void finish() {
1216d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mStop = true;
1217d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            notifyAll();
1218d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1219d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1220d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in namer thread
1221d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private void generateUri() {
1222d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mTitle = Util.createJpegName(mDateTaken);
1223d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mUri = Storage.newImage(mResolver, mTitle, mDateTaken, mWidth, mHeight);
1224d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1225d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1226d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Runs in namer thread
1227d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        private void cleanOldUri() {
1228d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mUri == null) return;
1229d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Storage.deleteImage(mResolver, mUri);
1230d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mUri = null;
1231d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1232d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1233d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1234d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setCameraState(int state) {
1235d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraState = state;
1236d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        switch (state) {
1237d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case PREVIEW_STOPPED:
1238d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case SNAPSHOT_IN_PROGRESS:
1239d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case FOCUSING:
1240d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case SWITCHING_CAMERA:
1241fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb                if (mGestures != null) mGestures.setEnabled(false);
1242d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                break;
1243d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case IDLE:
1244801d4f29ef2974501df14010e9ac5344d2fa8fa9Doris Liu                if (mGestures != null && mActivity.mShowCameraAppView) {
1245801d4f29ef2974501df14010e9ac5344d2fa8fa9Doris Liu                    // Enable gestures only when the camera app view is visible
1246801d4f29ef2974501df14010e9ac5344d2fa8fa9Doris Liu                    mGestures.setEnabled(true);
1247801d4f29ef2974501df14010e9ac5344d2fa8fa9Doris Liu                }
1248d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                break;
1249d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1250d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1251d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
12529b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck    private void animateFlash() {
12539b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck        // Only animate when in full screen capture mode
12549b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck        // i.e. If monkey/a user swipes to the gallery during picture taking,
12559b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck        // don't show animation
12569b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck        if (ApiHelper.HAS_SURFACE_TEXTURE && !mIsImageCaptureIntent
12579b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck                && mActivity.mShowCameraAppView) {
12589b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck            // Start capture animation.
1259bab66b2479fec953811dba84633b24f95b138c34John Reck            ((CameraScreenNail) mActivity.mCameraScreenNail).animateFlash(mDisplayRotation);
12609b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck        }
12619b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck    }
12629b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck
1263d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1264d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean capture() {
1265d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // If we are already in the middle of taking a snapshot then ignore.
1266d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS
1267d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || mCameraState == SWITCHING_CAMERA) {
1268d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return false;
1269d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1270d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCaptureStartTime = System.currentTimeMillis();
1271d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPostViewPictureCallbackTime = 0;
1272d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mJpegImageData = null;
1273d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
12749b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck        final boolean animateBefore = (mSceneMode == Util.SCENE_MODE_HDR);
12759b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck
12769b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck        if (animateBefore) {
12779b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck            animateFlash();
127870d41293e9dad8be7a1f2c556ff7c7334a60c8f5John Reck        }
127970d41293e9dad8be7a1f2c556ff7c7334a60c8f5John Reck
1280d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set rotation and gps data.
1281d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mJpegRotation = Util.getJpegRotation(mCameraId, mOrientation);
1282d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mParameters.setRotation(mJpegRotation);
1283d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Location loc = mLocationManager.getCurrentLocation();
1284d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Util.setGpsParameters(mParameters, loc);
1285d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.setParameters(mParameters);
1286d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1287dd5a6f95999ec217453fc2d1caa2856e33d122afMangesh Ghiware        mCameraDevice.takePicture2(mShutterCallback, mRawPictureCallback,
1288dd5a6f95999ec217453fc2d1caa2856e33d122afMangesh Ghiware                mPostViewPictureCallback, new JpegPictureCallback(loc),
1289dd5a6f95999ec217453fc2d1caa2856e33d122afMangesh Ghiware                mCameraState, mFocusManager.getFocusState());
1290d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
12919b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck        if (!animateBefore) {
12929b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck            animateFlash();
12939b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck        }
12949b475cfd71609cfe2936b5be2b55c334949858eeJohn Reck
1295d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Size size = mParameters.getPictureSize();
1296d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mImageNamer.prepareUri(mContentResolver, mCaptureStartTime,
1297d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                size.width, size.height, mJpegRotation);
1298d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1299d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFaceDetectionStarted = false;
1300d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraState(SNAPSHOT_IN_PROGRESS);
1301d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return true;
1302d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1303d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1304d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1305d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void setFocusParameters() {
1306d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraParameters(UPDATE_PARAM_PREFERENCE);
1307d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1308d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1309d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private int getPreferredCameraId(ComboPreferences preferences) {
1310d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int intentCameraId = Util.getCameraFacingIntentExtras(mActivity);
1311d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (intentCameraId != -1) {
1312d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Testing purpose. Launch a specific camera through the intent
1313d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // extras.
1314d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return intentCameraId;
1315d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1316d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return CameraSettings.readPreferredCameraId(preferences);
1317d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1318d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1319d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
13200ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb    private void setShowMenu(boolean show) {
13210ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb        if (mOnScreenIndicators != null) {
13220ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb            mOnScreenIndicators.setVisibility(show ? View.VISIBLE : View.GONE);
13230ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb        }
13240ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb        if (mMenu != null) {
13250ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb            mMenu.setVisibility(show ? View.VISIBLE : View.GONE);
13260ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb        }
13270ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb    }
1328d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1329d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1330d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onFullScreenChanged(boolean full) {
133141d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb        if (mFaceView != null) {
133241d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb            mFaceView.setBlockDraw(!full);
133341d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb        }
13342d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        if (mPopup != null) {
13350ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb            dismissPopup(false, full);
13362d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        }
13374d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        if (mGestures != null) {
13384d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb            mGestures.setEnabled(full);
13394d628bcbaf4e6f2330dc9619f2129770863666dcMichael Kolb        }
13408a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        if (mRenderOverlay != null) {
13418a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            // this can not happen in capture mode
13428a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mRenderOverlay.setVisibility(full ? View.VISIBLE : View.GONE);
13438a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        }
1344fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb        if (mPieRenderer != null) {
1345fe44832abc2e9fb937ac5bd9c65e103397992e8cMichael Kolb            mPieRenderer.setBlockFocus(!full);
134682ee836f814740198b193e7aa105fdbb7dc8711eMichael Kolb        }
13470ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb        setShowMenu(full);
13488a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        if (mBlocker != null) {
13498a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mBlocker.setVisibility(full ? View.VISIBLE : View.GONE);
13508a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        }
13519713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb        if (ApiHelper.HAS_SURFACE_TEXTURE) {
13529713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb            if (mActivity.mCameraScreenNail != null) {
13539713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb                ((CameraScreenNail) mActivity.mCameraScreenNail).setFullScreen(full);
13549713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb            }
13559713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb            return;
13569713c56dfe1c298ab0c2c75d324dc69395d1c809Michael Kolb        }
1357d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (full) {
1358d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPreviewSurfaceView.expand();
1359d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1360d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPreviewSurfaceView.shrink();
1361d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1362d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1363d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1364d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1365d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
1366d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "surfaceChanged:" + holder + " width=" + width + ". height="
1367d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                + height);
1368d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1369d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1370d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1371d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void surfaceCreated(SurfaceHolder holder) {
1372d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "surfaceCreated: " + holder);
1373d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraSurfaceHolder = holder;
1374d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Do not access the camera if camera start up thread is not finished.
1375d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraDevice == null || mCameraStartUpThread != null) return;
1376d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1377d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.setPreviewDisplayAsync(holder);
1378d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // This happens when onConfigurationChanged arrives, surface has been
1379d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // destroyed, and there is no onFullScreenChanged.
1380d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraState == PREVIEW_STOPPED) {
1381d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setupPreview();
1382d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1383d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1384d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1385d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1386d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void surfaceDestroyed(SurfaceHolder holder) {
1387d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "surfaceDestroyed: " + holder);
1388d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraSurfaceHolder = null;
1389d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        stopPreview();
1390d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1391d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1392d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateSceneModeUI() {
1393d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // If scene mode is set, we cannot set flash mode, white balance, and
1394d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // focus mode, instead, we read it from driver
1395d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
13961373b98135b2854578111dd535e24d39c8853f14Michael Kolb            overrideCameraSettings(mParameters.getFlashMode(),
13971373b98135b2854578111dd535e24d39c8853f14Michael Kolb                    mParameters.getWhiteBalance(), mParameters.getFocusMode());
1398d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
13991373b98135b2854578111dd535e24d39c8853f14Michael Kolb            overrideCameraSettings(null, null, null);
14001373b98135b2854578111dd535e24d39c8853f14Michael Kolb        }
14011373b98135b2854578111dd535e24d39c8853f14Michael Kolb    }
14021373b98135b2854578111dd535e24d39c8853f14Michael Kolb
14031373b98135b2854578111dd535e24d39c8853f14Michael Kolb    private void overrideCameraSettings(final String flashMode,
14041373b98135b2854578111dd535e24d39c8853f14Michael Kolb            final String whiteBalance, final String focusMode) {
1405b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb        if (mPhotoControl != null) {
14061373b98135b2854578111dd535e24d39c8853f14Michael Kolb//            mPieControl.enableFilter(true);
1407b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb            mPhotoControl.overrideSettings(
14081373b98135b2854578111dd535e24d39c8853f14Michael Kolb                    CameraSettings.KEY_FLASH_MODE, flashMode,
14091373b98135b2854578111dd535e24d39c8853f14Michael Kolb                    CameraSettings.KEY_WHITE_BALANCE, whiteBalance,
14101373b98135b2854578111dd535e24d39c8853f14Michael Kolb                    CameraSettings.KEY_FOCUS_MODE, focusMode);
14111373b98135b2854578111dd535e24d39c8853f14Michael Kolb//            mPieControl.enableFilter(false);
1412d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1413d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1414d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1415d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void loadCameraPreferences() {
1416d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        CameraSettings settings = new CameraSettings(mActivity, mInitialParams,
1417d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraId, CameraHolder.instance().getCameraInfo());
1418d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreferenceGroup = settings.getPreferenceGroup(R.xml.camera_preferences);
1419d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1420d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1421d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1422d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean collapseCameraControls() {
1423a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        // Remove all the popups/dialog boxes
1424a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        boolean ret = false;
1425a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        if (mPopup != null) {
1426f04692066fc7087560248b003c49e0377c056edfDoris Liu            dismissPopup(false);
1427a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu            ret = true;
1428a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        }
1429a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        return ret;
1430a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu    }
1431a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu
1432a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu    public boolean removeTopLevelPopup() {
1433a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu        // Remove the top level popup or dialog box and return true if there's any
14342d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        if (mPopup != null) {
1435f04692066fc7087560248b003c49e0377c056edfDoris Liu            dismissPopup(true);
14362d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb            return true;
14372d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        }
1438d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return false;
1439d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1440d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
144178711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu    @Override
144278711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu    public void onOrientationChanged(int orientation) {
144378711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // We keep the last known orientation. So if the user first orient
144478711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // the camera then point the camera to floor or sky, we still have
144578711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // the correct orientation.
144678711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return;
144778711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        mOrientation = Util.roundOrientation(orientation, mOrientation);
144878711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // When the screen is unlocked, display rotation may change. Always
144978711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // calculate the up-to-date orientationCompensation.
145078711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        int orientationCompensation =
145178711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu                (mOrientation + Util.getDisplayRotation(mActivity)) % 360;
1452032dea1d8406cde556ec0a441e4c90409edf9d63John Reck        if (mOrientationCompensation != orientationCompensation) {
145378711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu            mOrientationCompensation = orientationCompensation;
145441d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb            if (mFaceView != null) {
145541d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb                mFaceView.setOrientation(mOrientationCompensation, true);
145641d49f0fa2c9c93869bacfd6e7f76299f482e40fMichael Kolb            }
14573851712c5a250c6764c894b99984e262a8f206a4John Reck            setDisplayOrientation();
1458d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1459d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
146078711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        // Show the toast after getting the first orientation changed.
146178711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu        if (mHandler.hasMessages(SHOW_TAP_TO_FOCUS_TOAST)) {
146278711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu            mHandler.removeMessages(SHOW_TAP_TO_FOCUS_TOAST);
146378711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu            showTapToFocusToast();
1464d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1465d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1466d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1467d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1468d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onStop() {
1469d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mMediaProviderClient != null) {
1470d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mMediaProviderClient.release();
1471d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mMediaProviderClient = null;
1472d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1473d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1474d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1475d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // onClick handler for R.id.btn_done
1476d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @OnClickAttr
1477d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onReviewDoneClicked(View v) {
1478d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        doAttach();
1479d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1480d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1481d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // onClick handler for R.id.btn_cancel
1482d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @OnClickAttr
1483d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onReviewCancelClicked(View v) {
1484d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        doCancel();
1485d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1486d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
14878a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb    // onClick handler for R.id.btn_retake
14888a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb    @OnClickAttr
14898a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb    public void onReviewRetakeClicked(View v) {
14908a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb        if (mPaused)
14918a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb            return;
14928a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb
14938a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb        hidePostCaptureAlert();
14948a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb        setupPreview();
14958a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb    }
14968a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb
1497d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void doAttach() {
1498d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused) {
1499d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
1500d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1501d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1502d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        byte[] data = mJpegImageData;
1503d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1504d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCropValue == null) {
1505d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // First handle the no crop case -- just return the value.  If the
1506d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // caller specifies a "save uri" then write the data to its
1507d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // stream. Otherwise, pass back a scaled down version of the bitmap
1508d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // directly in the extras.
1509d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mSaveUri != null) {
1510d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                OutputStream outputStream = null;
1511d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                try {
1512d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    outputStream = mContentResolver.openOutputStream(mSaveUri);
1513d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    outputStream.write(data);
1514d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    outputStream.close();
1515d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1516d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mActivity.setResultEx(Activity.RESULT_OK);
1517d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mActivity.finish();
1518d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } catch (IOException ex) {
1519d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // ignore exception
1520d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } finally {
1521d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    Util.closeSilently(outputStream);
1522d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1523d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
1524d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                int orientation = Exif.getOrientation(data);
1525d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Bitmap bitmap = Util.makeBitmap(data, 50 * 1024);
1526d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                bitmap = Util.rotate(bitmap, orientation);
1527d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.setResultEx(Activity.RESULT_OK,
1528d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        new Intent("inline-data").putExtra("data", bitmap));
1529d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.finish();
1530d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1531d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1532d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Save the image to a temp file and invoke the cropper
1533d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Uri tempUri = null;
1534d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            FileOutputStream tempStream = null;
1535d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            try {
1536d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                File path = mActivity.getFileStreamPath(sTempCropFilename);
1537d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                path.delete();
1538d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                tempStream = mActivity.openFileOutput(sTempCropFilename, 0);
1539d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                tempStream.write(data);
1540d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                tempStream.close();
1541d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                tempUri = Uri.fromFile(path);
1542d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } catch (FileNotFoundException ex) {
1543d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.setResultEx(Activity.RESULT_CANCELED);
1544d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.finish();
1545d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return;
1546d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } catch (IOException ex) {
1547d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.setResultEx(Activity.RESULT_CANCELED);
1548d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.finish();
1549d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return;
1550d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } finally {
1551d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Util.closeSilently(tempStream);
1552d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1553d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1554d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Bundle newExtras = new Bundle();
1555d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mCropValue.equals("circle")) {
1556d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                newExtras.putString("circleCrop", "true");
1557d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1558d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mSaveUri != null) {
1559d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
1560d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
1561d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                newExtras.putBoolean("return-data", true);
1562d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1563ce51afce8c74f2c68e386b2a8f35c4be6772b760Wu-cheng Li            if (mActivity.isSecureCamera()) {
1564ce51afce8c74f2c68e386b2a8f35c4be6772b760Wu-cheng Li                newExtras.putBoolean(CropImage.KEY_SHOW_WHEN_LOCKED, true);
1565ce51afce8c74f2c68e386b2a8f35c4be6772b760Wu-cheng Li            }
1566d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1567d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Intent cropIntent = new Intent("com.android.camera.action.CROP");
1568d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1569d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            cropIntent.setData(tempUri);
1570d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            cropIntent.putExtras(newExtras);
1571d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1572d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mActivity.startActivityForResult(cropIntent, REQUEST_CROP);
1573d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1574d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1575d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1576d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void doCancel() {
1577d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.setResultEx(Activity.RESULT_CANCELED, new Intent());
1578d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.finish();
1579d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1580d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1581d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1582d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onShutterButtonFocus(boolean pressed) {
1583d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused || collapseCameraControls()
1584d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || (mCameraState == SNAPSHOT_IN_PROGRESS)
1585d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || (mCameraState == PREVIEW_STOPPED)) return;
1586d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1587d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Do not do focus if there is not enough storage.
1588d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (pressed && !canTakePicture()) return;
1589d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1590d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (pressed) {
1591d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.onShutterDown();
1592d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1593d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.onShutterUp();
1594d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1595d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1596d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1597d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1598d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onShutterButtonClick() {
1599d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused || collapseCameraControls()
1600d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || (mCameraState == SWITCHING_CAMERA)
1601d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || (mCameraState == PREVIEW_STOPPED)) return;
1602d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1603d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Do not take the picture if there is not enough storage.
1604d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mActivity.getStorageSpace() <= Storage.LOW_STORAGE_THRESHOLD) {
1605d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.i(TAG, "Not enough space or storage not ready. remaining="
1606d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    + mActivity.getStorageSpace());
1607d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
1608d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1609d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "onShutterButtonClick: mCameraState=" + mCameraState);
1610d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1611d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // If the user wants to do a snapshot while the previous one is still
1612d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // in progress, remember the fact and do it after we finish the previous
1613d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // one and re-start the preview. Snapshot in progress also includes the
1614d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // state that autofocus is focusing and a picture will be taken when
1615d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // focus callback arrives.
1616d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if ((mFocusManager.isFocusingSnapOnFinish() || mCameraState == SNAPSHOT_IN_PROGRESS)
1617d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                && !mIsImageCaptureIntent) {
1618d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mSnapshotOnIdle = true;
1619d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
1620d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1621d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1622d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mSnapshotOnIdle = false;
1623d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.doSnap();
1624d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1625d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1626d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1627d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void installIntentFilter() {
1628d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1629d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1630d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1631d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean updateStorageHintOnResume() {
1632d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return mFirstTimeInitialized;
1633d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1634d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1635d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1636d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void updateCameraAppView() {
1637d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1638d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1639d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1640d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onResumeBeforeSuper() {
1641d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPaused = false;
1642d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1643d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1644d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1645d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onResumeAfterSuper() {
1646d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mOpenCameraFail || mCameraDisabled) return;
1647d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1648d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mJpegPictureCallbackTime = 0;
1649d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mZoomValue = 0;
1650d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1651d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Start the preview if it is not started.
1652d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraState == PREVIEW_STOPPED && mCameraStartUpThread == null) {
1653d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            resetExposureCompensation();
1654d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraStartUpThread = new CameraStartUpThread();
1655d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraStartUpThread.start();
1656d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1657d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1658d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // If first time initialization is not finished, put it in the
1659d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // message queue.
1660d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!mFirstTimeInitialized) {
1661d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mHandler.sendEmptyMessage(FIRST_TIME_INIT);
1662d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1663d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            initializeSecondTime();
1664d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1665d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        keepScreenOnAwhile();
1666d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1667d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Dismiss open menu if exists.
1668d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        PopupManager.getInstance(mActivity).notifyShowPopup(null);
1669d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1670d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1671d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    void waitCameraStartUpThread() {
1672d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        try {
1673d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mCameraStartUpThread != null) {
1674d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraStartUpThread.cancel();
1675d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraStartUpThread.join();
1676d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraStartUpThread = null;
1677d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                setCameraState(IDLE);
1678d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1679d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } catch (InterruptedException e) {
1680d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // ignore
1681d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1682d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1683d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1684d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1685d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onPauseBeforeSuper() {
1686d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPaused = true;
1687d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1688d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1689d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1690d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onPauseAfterSuper() {
1691d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Wait the camera start up thread to finish.
1692d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        waitCameraStartUpThread();
1693d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
16947717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        // When camera is started from secure lock screen for the first time
16957717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        // after screen on, the activity gets onCreate->onResume->onPause->onResume.
16967717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        // To reduce the latency, keep the camera for a short time so it does
16977717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        // not need to be opened again.
16987717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        if (mCameraDevice != null && mActivity.isSecureCamera()
16997717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li                && ActivityBase.isFirstStartAfterScreenOn()) {
17007717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li            ActivityBase.resetFirstStartAfterScreenOn();
17017717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li            CameraHolder.instance().keep(KEEP_CAMERA_TIMEOUT);
17027717d2f0fc0e1393971cab44b3bba8a0f1297835Wu-cheng Li        }
17030505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        // Reset the focus first. Camera CTS does not guarantee that
17040505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        // cancelAutoFocus is allowed after preview stops.
17050505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
17060505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li            mCameraDevice.cancelAutoFocus();
17070505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        }
1708d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        stopPreview();
1709d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Close the camera now because other activities may need to use it.
1710d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        closeCamera();
1711d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mSurfaceTexture != null) {
1712b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb            ((CameraScreenNail) mActivity.mCameraScreenNail).releaseSurfaceTexture();
1713d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mSurfaceTexture = null;
1714d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1715d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        resetScreenOn();
1716d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1717d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Clear UI.
1718d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        collapseCameraControls();
1719d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFaceView != null) mFaceView.clear();
1720d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1721d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFirstTimeInitialized) {
1722d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mImageSaver != null) {
1723d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mImageSaver.finish();
1724d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mImageSaver = null;
1725d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mImageNamer.finish();
1726d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mImageNamer = null;
1727d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1728d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1729d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1730d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mLocationManager != null) mLocationManager.recordLocation(false);
1731d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateExposureOnScreenIndicator(0);
1732d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1733d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // If we are in an image capture intent and has taken
1734d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // a picture, we just clear it in onPause.
1735d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mJpegImageData = null;
1736d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1737d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Remove the messages in the event queue.
1738d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(SETUP_PREVIEW);
1739d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(FIRST_TIME_INIT);
1740d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(CHECK_DISPLAY_ROTATION);
1741d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(SWITCH_CAMERA);
1742d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(SWITCH_CAMERA_START_ANIMATION);
1743d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(CAMERA_OPEN_DONE);
1744d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(START_PREVIEW_DONE);
1745d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(OPEN_CAMERA_FAIL);
1746d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(CAMERA_DISABLED);
1747d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1748d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPendingSwitchCameraId = -1;
1749d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusManager != null) mFocusManager.removeMessages();
1750d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1751d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1752d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeControlByIntent() {
17538a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        mBlocker = mRootView.findViewById(R.id.blocker);
17548a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        mMenu = mRootView.findViewById(R.id.menu);
17558a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        mMenu.setOnClickListener(new OnClickListener() {
17568a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            @Override
17578a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            public void onClick(View v) {
17588a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb                if (mPieRenderer != null) {
17598a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb                    mPieRenderer.showInCenter();
17608a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb                }
17618a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            }
17628a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb        });
1763d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mIsImageCaptureIntent) {
1764fdb79c492940d46b27702b0be71903f99af7e34fMichael Kolb
176590ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            mActivity.hideSwitcher();
1766d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Cannot use RotateImageView for "done" and "cancel" button because
1767d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // the tablet layout uses RotateLayout, which cannot be cast to
1768d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // RotateImageView.
1769d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mReviewDoneButton = (Rotatable) mRootView.findViewById(R.id.btn_done);
1770d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mReviewCancelButton = (Rotatable) mRootView.findViewById(R.id.btn_cancel);
17718a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb            mReviewRetakeButton = mRootView.findViewById(R.id.btn_retake);
177290ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            ((View) mReviewCancelButton).setVisibility(View.VISIBLE);
177390ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb
177490ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            ((View) mReviewDoneButton).setOnClickListener(new OnClickListener() {
177590ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                @Override
177690ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                public void onClick(View v) {
177790ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                    onReviewDoneClicked(v);
177890ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                }
177990ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            });
178090ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            ((View) mReviewCancelButton).setOnClickListener(new OnClickListener() {
178190ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                @Override
178290ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                public void onClick(View v) {
178390ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                    onReviewCancelClicked(v);
178490ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb                }
178590ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb            });
1786d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
17878a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb            mReviewRetakeButton.setOnClickListener(new OnClickListener() {
17888a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb                @Override
17898a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb                public void onClick(View v) {
17908a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb                    onReviewRetakeClicked(v);
17918a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb                }
17928a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb            });
17938a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb
1794d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Not grayed out upon disabled, to make the follow-up fade-out
1795d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // effect look smooth. Note that the review done button in tablet
1796d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // layout is not a TwoStateImageView.
1797d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mReviewDoneButton instanceof TwoStateImageView) {
1798d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                ((TwoStateImageView) mReviewDoneButton).enableFilter(false);
1799d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1800d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1801d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setupCaptureParams();
1802d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1803d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1804d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1805b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb    /**
1806b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb     * The focus manager is the first UI related element to get initialized,
1807b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb     * and it requires the RenderOverlay, so initialize it here
1808b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb     */
1809d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeFocusManager() {
1810d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Create FocusManager object. startPreview needs it.
1811b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb        mRenderOverlay = (RenderOverlay) mRootView.findViewById(R.id.render_overlay);
1812b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu        // if mFocusManager not null, reuse it
1813b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu        // otherwise create a new instance
1814b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu        if (mFocusManager != null) {
1815b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu            mFocusManager.removeMessages();
1816b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu        } else {
181730f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu            CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
181830f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu            boolean mirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
181930f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu            String[] defaultFocusModes = mActivity.getResources().getStringArray(
182030f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu                    R.array.pref_camera_focusmode_default_array);
1821b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu            mFocusManager = new FocusOverlayManager(mPreferences, defaultFocusModes,
1822b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu                    mInitialParams, this, mirror,
1823b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu                    mActivity.getMainLooper());
1824b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu        }
1825d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1826d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1827d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeMiscControls() {
1828d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // startPreview needs this.
1829d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreviewFrameLayout = (PreviewFrameLayout) mRootView.findViewById(R.id.frame);
1830d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set touch focus listener.
1831d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.setSingleTapUpListener(mPreviewFrameLayout);
1832d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1833d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFaceView = (FaceView) mRootView.findViewById(R.id.face_view);
1834d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreviewFrameLayout.setOnSizeChangedListener(this);
1835d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreviewFrameLayout.setOnLayoutChangeListener(mActivity);
1836d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!ApiHelper.HAS_SURFACE_TEXTURE) {
1837d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPreviewSurfaceView =
1838d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    (PreviewSurfaceView) mRootView.findViewById(R.id.preview_surface_view);
1839d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPreviewSurfaceView.setVisibility(View.VISIBLE);
1840d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mPreviewSurfaceView.getHolder().addCallback(this);
1841d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1842d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1843d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1844d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1845d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onConfigurationChanged(Configuration newConfig) {
1846d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "onConfigurationChanged");
1847d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setDisplayOrientation();
1848d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1849d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        ((ViewGroup) mRootView).removeAllViews();
1850d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        LayoutInflater inflater = mActivity.getLayoutInflater();
1851d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        inflater.inflate(R.layout.photo_module, (ViewGroup) mRootView);
1852d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1853d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // from onCreate()
1854d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeControlByIntent();
1855b69cbda2db42937b2920a497f34e67c8332eb1bdDoris Liu
1856d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeFocusManager();
1857d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeMiscControls();
1858b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb        loadCameraPreferences();
1859d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1860d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // from initializeFirstTime()
1861d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mShutterButton = mActivity.getShutterButton();
1862d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mShutterButton.setOnShutterButtonListener(this);
1863d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeZoom();
1864d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initOnScreenIndicator();
1865d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateOnScreenIndicators();
1866d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFaceView != null) {
1867d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.clear();
1868d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setVisibility(View.VISIBLE);
1869d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setDisplayOrientation(mDisplayOrientation);
1870d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
1871d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setMirror(info.facing == CameraInfo.CAMERA_FACING_FRONT);
1872d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.resume();
1873d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.setFaceView(mFaceView);
1874d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1875471c997edfddccfa0762fa720c82cc95777584e8John Reck        initializeRenderOverlay();
1876812fcbcb099b5ff7b9f9ae8bb4219d57dc29c70cMichael Kolb        onFullScreenChanged(mActivity.isInCameraApp());
1877c5777bb0d17b3b633e67fec972229b717bc51993Angus Kong        if (mJpegImageData != null) {  // Jpeg data found, picture has been taken.
1878c5777bb0d17b3b633e67fec972229b717bc51993Angus Kong            showPostCaptureAlert();
1879c5777bb0d17b3b633e67fec972229b717bc51993Angus Kong        }
1880d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1881d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1882d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1883d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onActivityResult(
1884d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            int requestCode, int resultCode, Intent data) {
1885d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        switch (requestCode) {
1886d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case REQUEST_CROP: {
1887d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Intent intent = new Intent();
1888d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (data != null) {
1889d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    Bundle extras = data.getExtras();
1890d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (extras != null) {
1891d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        intent.putExtras(extras);
1892d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1893d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1894d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.setResultEx(resultCode, intent);
1895d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.finish();
1896d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1897d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                File path = mActivity.getFileStreamPath(sTempCropFilename);
1898d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                path.delete();
1899d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1900d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                break;
1901d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
1902d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1903d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1904d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1905d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean canTakePicture() {
1906d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return isCameraIdle() && (mActivity.getStorageSpace() > Storage.LOW_STORAGE_THRESHOLD);
1907d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1908d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1909d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1910d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void autoFocus() {
1911d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusStartTime = System.currentTimeMillis();
1912d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.autoFocus(mAutoFocusCallback);
1913d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraState(FOCUSING);
1914d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1915d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1916d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1917d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void cancelAutoFocus() {
1918d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.cancelAutoFocus();
1919d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraState(IDLE);
1920d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraParameters(UPDATE_PARAM_PREFERENCE);
1921d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1922d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1923d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Preview area is touched. Handle touch focus.
1924d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1925d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onSingleTapUp(View view, int x, int y) {
1926d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused || mCameraDevice == null || !mFirstTimeInitialized
1927d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || mCameraState == SNAPSHOT_IN_PROGRESS
1928d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || mCameraState == SWITCHING_CAMERA
1929d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                || mCameraState == PREVIEW_STOPPED) {
1930d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
1931d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1932d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1933d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Do not trigger touch focus if popup window is opened.
1934f04692066fc7087560248b003c49e0377c056edfDoris Liu        if (removeTopLevelPopup()) return;
1935d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1936d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Check if metering area or focus area is supported.
193717a04fb7378effcef5728b5b21442a59fb88572dMichael Kolb        if (!mFocusAreaSupported && !mMeteringAreaSupported) return;
193817a04fb7378effcef5728b5b21442a59fb88572dMichael Kolb        mFocusManager.onSingleTapUp(x, y);
1939d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1940d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1941d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1942d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean onBackPressed() {
1943fd187ac6cfd6129882ae0bcea1034f4d18fcdff8Michael Kolb        if (mPieRenderer != null && mPieRenderer.showsItems()) {
1944fd187ac6cfd6129882ae0bcea1034f4d18fcdff8Michael Kolb            mPieRenderer.hide();
1945fd187ac6cfd6129882ae0bcea1034f4d18fcdff8Michael Kolb            return true;
1946fd187ac6cfd6129882ae0bcea1034f4d18fcdff8Michael Kolb        }
1947c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu        // In image capture mode, back button should:
1948c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu        // 1) if there is any popup, dismiss them, 2) otherwise, get out of image capture
1949c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu        if (mIsImageCaptureIntent) {
1950a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu            if (!removeTopLevelPopup()) {
1951c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu                // no popup to dismiss, cancel image capture
1952c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu                doCancel();
1953c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu            }
1954c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu            return true;
1955c78f050d61fe40c395d070c6b9e8b388b561496cDoris Liu        } else if (!isCameraIdle()) {
1956d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // ignore backs while we're taking a picture
1957d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return true;
1958d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
1959a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu            return removeTopLevelPopup();
1960d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1961d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1962d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1963d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1964d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean onKeyDown(int keyCode, KeyEvent event) {
1965d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        switch (keyCode) {
1966d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case KeyEvent.KEYCODE_FOCUS:
1967d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1968d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    onShutterButtonFocus(true);
1969d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1970d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return true;
1971d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case KeyEvent.KEYCODE_CAMERA:
1972d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1973d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    onShutterButtonClick();
1974d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1975d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return true;
1976d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case KeyEvent.KEYCODE_DPAD_CENTER:
1977d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // If we get a dpad center event without any focused view, move
1978d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // the focus to the shutter button and press it.
1979d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
1980d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // Start auto-focus immediately to reduce shutter lag. After
1981d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // the shutter button gets the focus, onShutterButtonFocus()
1982d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    // will be called again but it is fine.
1983a2c4440795c9b41e1fa5343d9b99157c7730e8f0Doris Liu                    if (removeTopLevelPopup()) return true;
1984d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    onShutterButtonFocus(true);
1985d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    if (mShutterButton.isInTouchMode()) {
1986d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mShutterButton.requestFocusFromTouch();
1987d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    } else {
1988d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        mShutterButton.requestFocus();
1989d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    }
1990d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mShutterButton.setPressed(true);
1991d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
1992d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return true;
1993d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
1994d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return false;
1995d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
1996d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
1997d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
1998d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public boolean onKeyUp(int keyCode, KeyEvent event) {
1999d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        switch (keyCode) {
2000d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            case KeyEvent.KEYCODE_FOCUS:
2001d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mFirstTimeInitialized) {
2002d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    onShutterButtonFocus(false);
2003d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
2004d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                return true;
2005d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2006d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return false;
2007d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2008d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2009d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
2010d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void closeCamera() {
2011d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraDevice != null) {
2012d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setZoomChangeListener(null);
2013d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if(ApiHelper.HAS_FACE_DETECTION) {
2014d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraDevice.setFaceDetectionListener(null);
2015d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2016d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setErrorCallback(null);
2017d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            CameraHolder.instance().release();
2018d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceDetectionStarted = false;
2019d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice = null;
2020d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setCameraState(PREVIEW_STOPPED);
2021d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.onCameraReleased();
2022d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2023d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2024d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2025d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setDisplayOrientation() {
2026d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mDisplayRotation = Util.getDisplayRotation(mActivity);
2027d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mDisplayOrientation = Util.getDisplayOrientation(mDisplayRotation, mCameraId);
2028d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDisplayOrientation = Util.getDisplayOrientation(0, mCameraId);
2029d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFaceView != null) {
2030d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceView.setDisplayOrientation(mDisplayOrientation);
2031d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
203230f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu        if (mFocusManager != null) {
203330f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu            mFocusManager.setDisplayOrientation(mDisplayOrientation);
203430f75fdc8d1cd80ec088c9f5e4e7ffbaf91c813cDoris Liu        }
20353851712c5a250c6764c894b99984e262a8f206a4John Reck        // GLRoot also uses the DisplayRotation, and needs to be told to layout to update
20363851712c5a250c6764c894b99984e262a8f206a4John Reck        mActivity.getGLRoot().requestLayoutContentPane();
2037d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2038d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2039d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Only called by UI thread.
2040d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setupPreview() {
2041d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.resetTouchFocus();
2042d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        startPreview();
2043d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraState(IDLE);
2044d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        startFaceDetection();
2045d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2046d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2047d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // This can be called by UI Thread or CameraStartUpThread. So this should
2048d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // not modify the views.
2049d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void startPreview() {
2050d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.setErrorCallback(mErrorCallback);
2051d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
20520505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        // ICS camera frameworks has a bug. Face detection state is not cleared
20530505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        // after taking a picture. Stop the preview to work around it. The bug
20540505273644c4c3aaf908bcda54c6d576a3ebfae8Wu-cheng Li        // was fixed in JB.
2055d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraState != PREVIEW_STOPPED) stopPreview();
2056d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2057d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setDisplayOrientation();
2058d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2059d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!mSnapshotOnIdle) {
2060d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // If the focus mode is continuous autofocus, call cancelAutoFocus to
2061d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // resume it because it may have been paused by autoFocus call.
2062d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (Util.FOCUS_MODE_CONTINUOUS_PICTURE.equals(mFocusManager.getFocusMode())) {
2063d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraDevice.cancelAutoFocus();
2064d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2065d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.setAeAwbLock(false); // Unlock AE and AWB.
2066d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2067d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraParameters(UPDATE_PARAM_ALL);
2068d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2069d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (ApiHelper.HAS_SURFACE_TEXTURE) {
2070b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb            CameraScreenNail screenNail = (CameraScreenNail) mActivity.mCameraScreenNail;
2071d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mSurfaceTexture == null) {
2072d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Size size = mParameters.getPreviewSize();
2073d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (mCameraDisplayOrientation % 180 == 0) {
2074d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    screenNail.setSize(size.width, size.height);
2075d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                } else {
2076d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    screenNail.setSize(size.height, size.width);
2077d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
20785bf0244bc2ccc8de9950baf3edd2868b5393d716John Reck                screenNail.enableAspectRatioClamping();
2079d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.notifyScreenNailChanged();
2080d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                screenNail.acquireSurfaceTexture();
2081d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mSurfaceTexture = screenNail.getSurfaceTexture();
2082d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2083d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation);
2084d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setPreviewTextureAsync((SurfaceTexture) mSurfaceTexture);
2085d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2086d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setDisplayOrientation(mDisplayOrientation);
2087d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setPreviewDisplayAsync(mCameraSurfaceHolder);
2088d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2089d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2090d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "startPreview");
2091d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.startPreviewAsync();
2092d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2093d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.onPreviewStarted();
2094d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2095d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mSnapshotOnIdle) {
2096d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mHandler.post(mDoSnapRunnable);
2097d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2098d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2099d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2100d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void stopPreview() {
2101d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
2102d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "stopPreview");
2103d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.stopPreview();
2104d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFaceDetectionStarted = false;
2105d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2106d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraState(PREVIEW_STOPPED);
2107d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusManager != null) mFocusManager.onPreviewStopped();
2108d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2109d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2110d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @SuppressWarnings("deprecation")
2111d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateCameraParametersInitialize() {
2112d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Reset preview frame rate to the maximum because it may be lowered by
2113d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // video camera application.
2114d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        List<Integer> frameRates = mParameters.getSupportedPreviewFrameRates();
2115d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (frameRates != null) {
2116d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Integer max = Collections.max(frameRates);
2117d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setPreviewFrameRate(max);
2118d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2119d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2120d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mParameters.set(Util.RECORDING_HINT, Util.FALSE);
2121d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2122d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Disable video stabilization. Convenience methods not available in API
2123d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // level <= 14
2124d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        String vstabSupported = mParameters.get("video-stabilization-supported");
2125d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if ("true".equals(vstabSupported)) {
2126d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.set("video-stabilization", "false");
2127d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2128d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2129d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2130d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateCameraParametersZoom() {
2131d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set zoom.
2132d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mParameters.isZoomSupported()) {
2133d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setZoom(mZoomValue);
2134d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2135d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2136d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2137d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN)
2138d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setAutoExposureLockIfSupported() {
2139d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mAeLockSupported) {
2140d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setAutoExposureLock(mFocusManager.getAeAwbLock());
2141d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2142d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2143d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2144d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN)
2145d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setAutoWhiteBalanceLockIfSupported() {
2146d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mAwbLockSupported) {
2147d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setAutoWhiteBalanceLock(mFocusManager.getAeAwbLock());
2148d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2149d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2150d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2151d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
2152d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setFocusAreasIfSupported() {
2153d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusAreaSupported) {
2154d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setFocusAreas(mFocusManager.getFocusAreas());
2155d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2156d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2157d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2158d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
2159d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setMeteringAreasIfSupported() {
2160d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mMeteringAreaSupported) {
2161d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Use the same area for focus and metering.
2162d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setMeteringAreas(mFocusManager.getMeteringAreas());
2163d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2164d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2165d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2166d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateCameraParametersPreference() {
2167d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setAutoExposureLockIfSupported();
2168d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setAutoWhiteBalanceLockIfSupported();
2169d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setFocusAreasIfSupported();
2170d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setMeteringAreasIfSupported();
2171d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2172d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set picture size.
2173d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        String pictureSize = mPreferences.getString(
2174d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                CameraSettings.KEY_PICTURE_SIZE, null);
2175d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (pictureSize == null) {
2176d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            CameraSettings.initialCameraPictureSize(mActivity, mParameters);
2177d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2178d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            List<Size> supported = mParameters.getSupportedPictureSizes();
2179d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            CameraSettings.setCameraPictureSize(
2180d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    pictureSize, supported, mParameters);
2181d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2182d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Size size = mParameters.getPictureSize();
2183d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2184d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set a preview size that is closest to the viewfinder height and has
2185d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // the right aspect ratio.
2186d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        List<Size> sizes = mParameters.getSupportedPreviewSizes();
2187d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Size optimalSize = Util.getOptimalPreviewSize(mActivity, sizes,
2188d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                (double) size.width / size.height);
2189d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Size original = mParameters.getPreviewSize();
2190d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (!original.equals(optimalSize)) {
2191d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setPreviewSize(optimalSize.width, optimalSize.height);
2192d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2193d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Zoom related settings will be changed for different preview
2194d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // sizes, so set and read the parameters to get latest values
2195d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setParameters(mParameters);
2196d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters = mCameraDevice.getParameters();
2197d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2198d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "Preview size is " + optimalSize.width + "x" + optimalSize.height);
2199d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2200ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        // Since changing scene mode may change supported values, set scene mode
2201ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        // first. HDR is a scene mode. To promote it in UI, it is stored in a
2202ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        // separate preference.
2203ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        String hdr = mPreferences.getString(CameraSettings.KEY_CAMERA_HDR,
2204ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li                mActivity.getString(R.string.pref_camera_hdr_default));
2205ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        if (mActivity.getString(R.string.setting_on_value).equals(hdr)) {
2206ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li            mSceneMode = Util.SCENE_MODE_HDR;
2207ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        } else {
2208ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li            mSceneMode = mPreferences.getString(
2209d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                CameraSettings.KEY_SCENE_MODE,
2210d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mActivity.getString(R.string.pref_camera_scenemode_default));
2211ad30bda0d0103bd8a93eb834c234341b1ab1e77fWu-cheng Li        }
2212d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (Util.isSupported(mSceneMode, mParameters.getSupportedSceneModes())) {
2213d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (!mParameters.getSceneMode().equals(mSceneMode)) {
2214d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mParameters.setSceneMode(mSceneMode);
2215d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2216d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // Setting scene mode will change the settings of flash mode,
2217d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // white balance, and focus mode. Here we read back the
2218d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                // parameters, so we can know those settings.
2219d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mCameraDevice.setParameters(mParameters);
2220d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mParameters = mCameraDevice.getParameters();
2221d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2222d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2223d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mSceneMode = mParameters.getSceneMode();
2224d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (mSceneMode == null) {
2225d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mSceneMode = Parameters.SCENE_MODE_AUTO;
2226d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2227d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2228d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2229d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set JPEG quality.
2230d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(mCameraId,
2231d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                CameraProfile.QUALITY_HIGH);
2232d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mParameters.setJpegQuality(jpegQuality);
2233d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2234d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // For the following settings, we need to check if the settings are
2235d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // still supported by latest driver, if not, ignore the settings.
2236d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2237d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set exposure compensation
2238d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int value = CameraSettings.readExposure(mPreferences);
2239d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int max = mParameters.getMaxExposureCompensation();
2240d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        int min = mParameters.getMinExposureCompensation();
2241d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (value >= min && value <= max) {
2242d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setExposureCompensation(value);
2243d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2244d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.w(TAG, "invalid exposure range: " + value);
2245d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2246d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2247d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
2248d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Set flash mode.
2249d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            String flashMode = mPreferences.getString(
2250d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    CameraSettings.KEY_FLASH_MODE,
2251d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mActivity.getString(R.string.pref_camera_flashmode_default));
2252d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            List<String> supportedFlash = mParameters.getSupportedFlashModes();
2253d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (Util.isSupported(flashMode, supportedFlash)) {
2254d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mParameters.setFlashMode(flashMode);
2255d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
2256d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                flashMode = mParameters.getFlashMode();
2257d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (flashMode == null) {
2258d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    flashMode = mActivity.getString(
2259d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                            R.string.pref_camera_flashmode_no_flash);
2260d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
2261d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2262d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2263d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Set white balance parameter.
2264d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            String whiteBalance = mPreferences.getString(
2265d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    CameraSettings.KEY_WHITE_BALANCE,
2266d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mActivity.getString(R.string.pref_camera_whitebalance_default));
2267d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (Util.isSupported(whiteBalance,
2268d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    mParameters.getSupportedWhiteBalance())) {
2269d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mParameters.setWhiteBalance(whiteBalance);
2270d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            } else {
2271d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                whiteBalance = mParameters.getWhiteBalance();
2272d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                if (whiteBalance == null) {
2273d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                    whiteBalance = Parameters.WHITE_BALANCE_AUTO;
2274d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                }
2275d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2276d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2277d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Set focus mode.
2278d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.overrideFocusMode(null);
2279d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mParameters.setFocusMode(mFocusManager.getFocusMode());
2280d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2281d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mFocusManager.overrideFocusMode(mParameters.getFocusMode());
2282d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2283d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2284d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mContinousFocusSupported && ApiHelper.HAS_AUTO_FOCUS_MOVE_CALLBACK) {
2285d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            updateAutoFocusMoveCallback();
2286d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2287d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2288d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2289d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN)
2290d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void updateAutoFocusMoveCallback() {
2291d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mParameters.getFocusMode().equals(Util.FOCUS_MODE_CONTINUOUS_PICTURE)) {
2292d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setAutoFocusMoveCallback(
2293d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                (AutoFocusMoveCallback) mAutoFocusMoveCallback);
2294d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2295d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCameraDevice.setAutoFocusMoveCallback(null);
2296d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2297d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2298d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2299d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // We separate the parameters into several subsets, so we can update only
2300d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // the subsets actually need updating. The PREFERENCE set needs extra
2301d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // locking because the preference can be changed from GLThread as well.
2302d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setCameraParameters(int updateSet) {
2303d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if ((updateSet & UPDATE_PARAM_INITIALIZE) != 0) {
2304d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            updateCameraParametersInitialize();
2305d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2306d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2307d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if ((updateSet & UPDATE_PARAM_ZOOM) != 0) {
2308d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            updateCameraParametersZoom();
2309d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2310d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2311d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) {
2312d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            updateCameraParametersPreference();
2313d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2314d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2315d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraDevice.setParameters(mParameters);
2316d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2317d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2318d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // If the Camera is idle, update the parameters immediately, otherwise
2319d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // accumulate them in mUpdateSet and update later.
2320d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setCameraParametersWhenIdle(int additionalUpdateSet) {
2321d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mUpdateSet |= additionalUpdateSet;
2322d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mCameraDevice == null) {
2323d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // We will update all the parameters when we open the device, so
2324d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // we don't need to do anything now.
2325d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mUpdateSet = 0;
2326d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            return;
2327d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else if (isCameraIdle()) {
2328d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setCameraParameters(mUpdateSet);
2329d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            updateSceneModeUI();
2330d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mUpdateSet = 0;
2331d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2332d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            if (!mHandler.hasMessages(SET_CAMERA_PARAMETERS_WHEN_IDLE)) {
2333d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mHandler.sendEmptyMessageDelayed(
2334d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000);
2335d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            }
2336d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2337d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2338d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2339d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean isCameraIdle() {
2340d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        return (mCameraState == IDLE) ||
2341d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                ((mFocusManager != null) && mFocusManager.isFocusCompleted()
2342d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                        && (mCameraState != SWITCHING_CAMERA));
2343d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2344d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2345d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private boolean isImageCaptureIntent() {
2346d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        String action = mActivity.getIntent().getAction();
234753b11253fde509b01851ba022d9178c7c6f13ddeWu-cheng Li        return (MediaStore.ACTION_IMAGE_CAPTURE.equals(action)
234853b11253fde509b01851ba022d9178c7c6f13ddeWu-cheng Li                || ActivityBase.ACTION_IMAGE_CAPTURE_SECURE.equals(action));
2349d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2350d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2351d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void setupCaptureParams() {
2352d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Bundle myExtras = mActivity.getIntent().getExtras();
2353d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (myExtras != null) {
2354d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mSaveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT);
2355d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mCropValue = myExtras.getString("crop");
2356d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2357d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2358d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2359d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void showPostCaptureAlert() {
2360d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mIsImageCaptureIntent) {
2361295c3f89cd90545caf40a7dd9263805c0836625fMichael Kolb            mOnScreenIndicators.setVisibility(View.GONE);
23628a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mMenu.setVisibility(View.GONE);
2363d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Util.fadeIn((View) mReviewDoneButton);
23648a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb            mShutterButton.setVisibility(View.INVISIBLE);
23658a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb            Util.fadeIn(mReviewRetakeButton);
2366d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2367d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2368d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2369d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void hidePostCaptureAlert() {
2370d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mIsImageCaptureIntent) {
2371295c3f89cd90545caf40a7dd9263805c0836625fMichael Kolb            mOnScreenIndicators.setVisibility(View.VISIBLE);
23728a66eb641fbc210130c1257cf713a95f571d609bMichael Kolb            mMenu.setVisibility(View.VISIBLE);
2373d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Util.fadeOut((View) mReviewDoneButton);
23748a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb            mShutterButton.setVisibility(View.VISIBLE);
23758a27da058a8318c876fb18b1f4964724b5508d6bMichael Kolb            Util.fadeOut(mReviewRetakeButton);
2376d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2377d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2378d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2379d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2380d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onSharedPreferenceChanged() {
2381d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // ignore the events after "onPause()"
2382d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused) return;
2383d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2384d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        boolean recordLocation = RecordLocationPreference.get(
2385d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                mPreferences, mContentResolver);
2386d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mLocationManager.recordLocation(recordLocation);
2387d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2388d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setCameraParametersWhenIdle(UPDATE_PARAM_PREFERENCE);
2389d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setPreviewFrameLayoutAspectRatio();
2390d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateOnScreenIndicators();
2391d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2392d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2393d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2394d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onCameraPickerClicked(int cameraId) {
2395d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused || mPendingSwitchCameraId != -1) return;
2396d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2397d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPendingSwitchCameraId = cameraId;
2398d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (ApiHelper.HAS_SURFACE_TEXTURE) {
2399d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            Log.v(TAG, "Start to copy texture. cameraId=" + cameraId);
2400d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // We need to keep a preview frame for the animation before
2401d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // releasing the camera. This will trigger onPreviewTextureCopied.
2402b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb            ((CameraScreenNail) mActivity.mCameraScreenNail).copyTexture();
2403d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Disable all camera controls.
2404d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            setCameraState(SWITCHING_CAMERA);
2405d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        } else {
2406d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            switchCamera();
2407d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2408d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2409d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2410d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void switchCamera() {
2411d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused) return;
2412d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2413d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Log.v(TAG, "Start to switch camera. id=" + mPendingSwitchCameraId);
2414d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mCameraId = mPendingSwitchCameraId;
2415d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPendingSwitchCameraId = -1;
2416b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb        mPhotoControl.setCameraId(mCameraId);
2417d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2418d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // from onPause
2419d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        closeCamera();
2420d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        collapseCameraControls();
2421d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFaceView != null) mFaceView.clear();
2422d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusManager != null) mFocusManager.removeMessages();
2423d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2424d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Restart the camera and initialize the UI. From onCreate.
2425d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreferences.setLocalId(mActivity, mCameraId);
2426d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
2427d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        try {
2428f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            mCameraDevice = Util.openCamera(mActivity, mCameraId);
2429f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            mParameters = mCameraDevice.getParameters();
2430f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li        } catch (CameraHardwareException e) {
2431f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            Util.showErrorAndFinish(mActivity, R.string.cannot_connect_camera);
2432f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            return;
2433f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li        } catch (CameraDisabledException e) {
2434f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            Util.showErrorAndFinish(mActivity, R.string.camera_disabled);
2435f99fd501c9cbff7617129af36707d1e22952aa31Wu-cheng Li            return;
2436d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2437d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeCapabilities();
2438d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
2439d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        boolean mirror = (info.facing == CameraInfo.CAMERA_FACING_FRONT);
2440d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.setMirror(mirror);
2441d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusManager.setParameters(mInitialParams);
2442d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        setupPreview();
2443b19c713286b7beed376ec6c4390e7272a80aa5b9Michael Kolb        loadCameraPreferences();
2444b5b59b88683823d70faac7b3454519d53c51180bMichael Kolb        initializePhotoControl();
2445d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2446d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // from initializeFirstTime
2447d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        initializeZoom();
2448d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        updateOnScreenIndicators();
2449d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        showTapToFocusToastIfNeeded();
2450d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2451d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (ApiHelper.HAS_SURFACE_TEXTURE) {
2452d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // Start switch camera animation. Post a message because
2453d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            // onFrameAvailable from the old camera may already exist.
2454d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mHandler.sendEmptyMessage(SWITCH_CAMERA_START_ANIMATION);
2455d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2456d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2457d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
24581373b98135b2854578111dd535e24d39c8853f14Michael Kolb    @Override
24591373b98135b2854578111dd535e24d39c8853f14Michael Kolb    public void onPieOpened(int centerX, int centerY) {
24609f8b479c44e8fafda688ac0fab9882584a94d55fMichael Kolb        mActivity.cancelActivityTouchHandling();
24611373b98135b2854578111dd535e24d39c8853f14Michael Kolb        mActivity.setSwipingEnabled(false);
24628dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb        if (mFaceView != null) {
24638dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb            mFaceView.setBlockDraw(true);
24648dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb        }
24651373b98135b2854578111dd535e24d39c8853f14Michael Kolb    }
24661373b98135b2854578111dd535e24d39c8853f14Michael Kolb
24671373b98135b2854578111dd535e24d39c8853f14Michael Kolb    @Override
24681373b98135b2854578111dd535e24d39c8853f14Michael Kolb    public void onPieClosed() {
24691373b98135b2854578111dd535e24d39c8853f14Michael Kolb        mActivity.setSwipingEnabled(true);
24708dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb        if (mFaceView != null) {
24718dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb            mFaceView.setBlockDraw(false);
24728dbb2a65ef1b0404c169e8fada6c0d0f154dd08fMichael Kolb        }
24731373b98135b2854578111dd535e24d39c8853f14Michael Kolb    }
24741373b98135b2854578111dd535e24d39c8853f14Michael Kolb
2475d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // Preview texture has been copied. Now camera can be released and the
2476d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // animation can be started.
2477d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2478d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onPreviewTextureCopied() {
2479d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.sendEmptyMessage(SWITCH_CAMERA);
2480d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2481d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2482d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2483d282a28c60ca8f4ee82d519b12e8b0b057b294d4George Mount    public void onCaptureTextureCopied() {
2484d282a28c60ca8f4ee82d519b12e8b0b057b294d4George Mount    }
2485d282a28c60ca8f4ee82d519b12e8b0b057b294d4George Mount
2486d282a28c60ca8f4ee82d519b12e8b0b057b294d4George Mount    @Override
2487d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onUserInteraction() {
2488fe269902ba8522e01baa9be45755779f050c1d6cWu-cheng Li        if (!mActivity.isFinishing()) keepScreenOnAwhile();
2489d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2490d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2491d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void resetScreenOn() {
2492d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(CLEAR_SCREEN_DELAY);
2493d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2494d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2495d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2496d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void keepScreenOnAwhile() {
2497d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.removeMessages(CLEAR_SCREEN_DELAY);
2498d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2499d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mHandler.sendEmptyMessageDelayed(CLEAR_SCREEN_DELAY, SCREEN_DELAY);
2500d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2501d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
250257abb9e7d8c88de1431b1af75b2871bee3e09e42Doris Liu    // TODO: Delete this function after old camera code is removed
2503d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2504d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onRestorePreferencesClicked() {
2505d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2506d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2507d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2508d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onOverriddenPreferencesClicked() {
2509d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mPaused) return;
2510d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mNotSelectableToast == null) {
2511d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            String str = mActivity.getResources().getString(R.string.not_selectable_in_scene_mode);
2512d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb            mNotSelectableToast = Toast.makeText(mActivity, str, Toast.LENGTH_SHORT);
2513d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
2514d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mNotSelectableToast.show();
2515d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2516d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2517d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void showTapToFocusToast() {
2518d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        new RotateTextToast(mActivity, R.string.tap_to_focus, mOrientationCompensation).show();
2519d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Clear the preference.
2520d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Editor editor = mPreferences.edit();
2521d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        editor.putBoolean(CameraSettings.KEY_CAMERA_FIRST_USE_HINT_SHOWN, false);
2522d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        editor.apply();
2523d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2524d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2525d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void initializeCapabilities() {
2526d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mInitialParams = mCameraDevice.getParameters();
2527d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mFocusAreaSupported = Util.isFocusAreaSupported(mInitialParams);
2528d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mMeteringAreaSupported = Util.isMeteringAreaSupported(mInitialParams);
2529d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mAeLockSupported = Util.isAutoExposureLockSupported(mInitialParams);
2530d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mAwbLockSupported = Util.isAutoWhiteBalanceLockSupported(mInitialParams);
2531d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mContinousFocusSupported = mInitialParams.getSupportedFocusModes().contains(
2532d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb                Util.FOCUS_MODE_CONTINUOUS_PICTURE);
2533d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2534d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2535d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    // PreviewFrameLayout size has changed.
2536d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    @Override
2537d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void onSizeChanged(int width, int height) {
2538d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        if (mFocusManager != null) mFocusManager.setPreviewSize(width, height);
2539d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2540d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
2541d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    void setPreviewFrameLayoutAspectRatio() {
2542d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        // Set the preview frame aspect ratio according to the picture size.
2543d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        Size size = mParameters.getPictureSize();
2544d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mPreviewFrameLayout.setAspectRatio((double) size.width / size.height);
2545d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
2546d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
254790ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb    @Override
254890ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb    public boolean needsSwitcher() {
254990ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb        return !mIsImageCaptureIntent;
255090ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb    }
255190ef46f0248437fbcb2c80c8a65239f4da32b039Michael Kolb
25522d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb    public void showPopup(AbstractSettingPopup popup) {
25532d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        mActivity.hideUI();
25545e4ea5b07a06795a2af3bab8e07e7760d2741f78Michael Kolb        mBlocker.setVisibility(View.INVISIBLE);
25550ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb        setShowMenu(false);
25562d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        mPopup = popup;
25573ce75c9a73de62e311beb79bdc8b5a2e7a096a81Doris Liu        // Make sure popup is brought up with the right orientation
25583ce75c9a73de62e311beb79bdc8b5a2e7a096a81Doris Liu        mPopup.setOrientation(mOrientationCompensation, false);
25592d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        mPopup.setVisibility(View.VISIBLE);
25602d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
25612d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb                LayoutParams.WRAP_CONTENT);
25622d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        lp.gravity = Gravity.CENTER;
25632d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        ((FrameLayout) mRootView).addView(mPopup, lp);
25642d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb    }
25652d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb
2566f04692066fc7087560248b003c49e0377c056edfDoris Liu    public void dismissPopup(boolean topPopupOnly) {
25670ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb        dismissPopup(topPopupOnly, true);
256889fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb    }
256989fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb
257089fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb    private void dismissPopup(boolean topOnly, boolean fullScreen) {
25710ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb        if (fullScreen) {
257289fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb            mActivity.showUI();
25735e4ea5b07a06795a2af3bab8e07e7760d2741f78Michael Kolb            mBlocker.setVisibility(View.VISIBLE);
257489fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb        }
25750ad5161f6fd4790f0934de9497c10660a2186d0aMichael Kolb        setShowMenu(fullScreen);
25762d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        if (mPopup != null) {
25772d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb            ((FrameLayout) mRootView).removeView(mPopup);
25782d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb            mPopup = null;
25792d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb        }
258089fd143a6213f69ee34b225686ebb3c63531833fMichael Kolb        mPhotoControl.popupDismissed(topOnly);
25812d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb    }
25822d3af28f91481d7f3887cb0bb9c5d06375baf787Michael Kolb
2583561af39348f34db4d67bc954536d6f6cbf296054John Reck    @Override
2584561af39348f34db4d67bc954536d6f6cbf296054John Reck    public void onShowSwitcherPopup() {
2585561af39348f34db4d67bc954536d6f6cbf296054John Reck        if (mPieRenderer.showsItems()) {
2586561af39348f34db4d67bc954536d6f6cbf296054John Reck            mPieRenderer.hide();
2587561af39348f34db4d67bc954536d6f6cbf296054John Reck        }
2588561af39348f34db4d67bc954536d6f6cbf296054John Reck    }
2589561af39348f34db4d67bc954536d6f6cbf296054John Reck
2590d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb}
2591