Camera.java revision 721d2d2a963799fad4bda9bb4b278c24fc469303
1b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project/*
2b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project *
4b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project * you may not use this file except in compliance with the License.
6b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project * You may obtain a copy of the License at
7b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project *
8b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project *
10b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project * See the License for the specific language governing permissions and
14b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project * limitations under the License.
15b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project */
16b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
17b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectpackage com.android.camera;
18b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
191efb18228a45952d699af280482e1c51a4582690Owen Linimport com.android.camera.gallery.IImage;
201efb18228a45952d699af280482e1c51a4582690Owen Linimport com.android.camera.gallery.IImageList;
211efb18228a45952d699af280482e1c51a4582690Owen Linimport com.android.camera.ui.CameraHeadUpDisplay;
2259ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wangimport com.android.camera.ui.CameraPicker;
23f5bffc476b8293b30bb500bed2ab7914bb4ca411Wu-cheng Liimport com.android.camera.ui.ControlPanel;
241b6cd72127f64a5306ee4acecaf6113627d35db2Wu-cheng Liimport com.android.camera.ui.FocusRectangle;
25937ea10204656beaa848df913dc9cb9b5a9b934fWu-cheng Liimport com.android.camera.ui.GLRootView;
26937ea10204656beaa848df913dc9cb9b5a9b934fWu-cheng Liimport com.android.camera.ui.HeadUpDisplay;
271efb18228a45952d699af280482e1c51a4582690Owen Linimport com.android.camera.ui.ZoomControllerListener;
287f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Liimport com.android.camera.ui.ZoomPicker;
291efb18228a45952d699af280482e1c51a4582690Owen Lin
30b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.app.Activity;
310e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Linimport android.content.ActivityNotFoundException;
32b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.content.BroadcastReceiver;
336d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chenimport android.content.ContentProviderClient;
34b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.content.ContentResolver;
35b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.content.Context;
36b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.content.Intent;
37b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.content.IntentFilter;
3859b5974a4ae078eb7628fd855db89e5be9b0fe6fOwen Linimport android.content.SharedPreferences.Editor;
39f00d421845b4ff2eca1354419889886fec5f2538Owen Linimport android.content.res.Configuration;
403f3c857e3f34650c15d764810335024654b0fcc3Owen Linimport android.content.res.Resources;
419f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Liimport android.database.Cursor;
42b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.graphics.Bitmap;
43b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.graphics.BitmapFactory;
444aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Liimport android.hardware.Camera.CameraInfo;
454305c70dc849156be5d79d52772ea71cd890ca40Wu-cheng Liimport android.hardware.Camera.Parameters;
46b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.hardware.Camera.PictureCallback;
474305c70dc849156be5d79d52772ea71cd890ca40Wu-cheng Liimport android.hardware.Camera.Size;
48b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.location.Location;
49b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.location.LocationManager;
50b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.location.LocationProvider;
51b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.media.AudioManager;
52281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Linimport android.media.ToneGenerator;
53b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.net.Uri;
54f6ef7b960f610d557f0bab924b9f7928b46f6f0aOwen Linimport android.os.Build;
55b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.os.Bundle;
56b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.os.Handler;
57cdf69cba3617c446e4fe9a92b4b4199c6ed36d01Owen Linimport android.os.Looper;
58b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.os.Message;
59f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Changimport android.os.MessageQueue;
60b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.provider.MediaStore;
61ed32015f4a0536e842306a207d620ec5e3f665c0Owen Linimport android.provider.Settings;
621efb18228a45952d699af280482e1c51a4582690Owen Linimport android.provider.MediaStore.Images.ImageColumns;
633966b3079aeaadbf376bee6aa0e12ff15863da4fWu-cheng Liimport android.provider.MediaStore.Images.Media;
64b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.util.Log;
65aa3cc2012a1ee0cc04e1244dd977cb8bb84683e4Wu-cheng Liimport android.view.GestureDetector;
66b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.view.KeyEvent;
6761b98317dfb7b8bad4e826e1db560128e1c37374Owen Linimport android.view.LayoutInflater;
68b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.view.Menu;
69b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.view.MenuItem;
7095fc5b2c5b14bb81332570f6d74f75cd63ea04b5Wu-cheng Liimport android.view.MotionEvent;
71b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.view.OrientationEventListener;
72b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.view.SurfaceHolder;
73ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Linimport android.view.SurfaceView;
74b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.view.View;
75b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.view.ViewGroup;
76c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Liimport android.view.ViewStub;
77b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.view.Window;
78b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Projectimport android.view.WindowManager;
791efb18228a45952d699af280482e1c51a4582690Owen Linimport android.view.MenuItem.OnMenuItemClickListener;
80373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyanimport android.widget.Button;
81fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Liimport android.widget.Toast;
822e768c1b835a127f190395a91477144d8ae850acOwen Lin
830e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Linimport java.io.File;
840e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Linimport java.io.FileNotFoundException;
850e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Linimport java.io.FileOutputStream;
860e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Linimport java.io.IOException;
870e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Linimport java.io.OutputStream;
8830055f6830b3d524cfbdfeab6536bb46e390747aPetri Gyntherimport java.text.SimpleDateFormat;
890e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Linimport java.util.ArrayList;
90cc5a6356055dd8a8a4017b0af5410b53f25aaef0Wu-cheng Liimport java.util.Collections;
9130055f6830b3d524cfbdfeab6536bb46e390747aPetri Gyntherimport java.util.Date;
923a4ef93f95bed9f812fe75ef94296450833b3997James Dongimport java.util.HashMap;
9382544b4564c079ece1a9065d19add36b2635bb8fOwen Linimport java.util.List;
940e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin
95271b3095b9f763421c0547109da9de774795072dChih-Chung Chang/** The Camera activity which can preview and take pictures. */
964d2aefbe7c108bae9e4ae735c7cd230792201098Chih-Chung Changpublic class Camera extends NoSearchActivity implements View.OnClickListener,
97e239acc2185c5fb47b22a65f78ffa461e19f183cOwen Lin        ShutterButton.OnShutterButtonListener, SurfaceHolder.Callback,
98c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        Switcher.OnSwitchListener {
99b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
100b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private static final String TAG = "camera";
101b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
102b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private static final int CROP_MSG = 1;
103e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li    private static final int FIRST_TIME_INIT = 2;
104b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private static final int RESTART_PREVIEW = 3;
105b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private static final int CLEAR_SCREEN_DELAY = 4;
1061de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang    private static final int SET_CAMERA_PARAMETERS_WHEN_IDLE = 5;
107b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
108ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    // The subset of parameters we need to update in setCameraParameters().
109ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    private static final int UPDATE_PARAM_INITIALIZE = 1;
110ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    private static final int UPDATE_PARAM_ZOOM = 2;
111ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    private static final int UPDATE_PARAM_PREFERENCE = 4;
112ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    private static final int UPDATE_PARAM_ALL = -1;
113ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
114ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    // When setCameraParametersWhenIdle() is called, we accumulate the subsets
115ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    // needed to be updated in mUpdateSet.
116ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    private int mUpdateSet;
117ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
118ed32015f4a0536e842306a207d620ec5e3f665c0Owen Lin    // The brightness settings used when it is set to automatic in the system.
119ed32015f4a0536e842306a207d620ec5e3f665c0Owen Lin    // The reason why it is set to 0.7 is just because 1.0 is too bright.
120ed32015f4a0536e842306a207d620ec5e3f665c0Owen Lin    private static final float DEFAULT_CAMERA_BRIGHTNESS = 0.7f;
121ed32015f4a0536e842306a207d620ec5e3f665c0Owen Lin
122b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private static final int SCREEN_DELAY = 2 * 60 * 1000;
123b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private static final int FOCUS_BEEP_VOLUME = 100;
124b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
12533be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin    private static final int ZOOM_STOPPED = 0;
12633be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin    private static final int ZOOM_START = 1;
12733be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin    private static final int ZOOM_STOPPING = 2;
12833be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin
12933be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin    private int mZoomState = ZOOM_STOPPED;
130316af30d026dd182edb482ef6de7a330d6b1b012Wu-cheng Li    private boolean mSmoothZoomSupported = false;
131316af30d026dd182edb482ef6de7a330d6b1b012Wu-cheng Li    private int mZoomValue;  // The current zoom value.
132316af30d026dd182edb482ef6de7a330d6b1b012Wu-cheng Li    private int mZoomMax;
13333be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin    private int mTargetZoomValue;
134c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li    private ZoomPicker mZoomPicker;
13535bda2dc8ab95a7baf68b544cf97826aabff6210Wu-cheng Li
1365f6484a74fe019337e436e6e0dcb07375a74af25President Li    private Parameters mParameters;
13770fe12f4d0ba9c08be9e0f0bda75b172a21cf70aOwen Lin    private Parameters mInitialParams;
1385f6484a74fe019337e436e6e0dcb07375a74af25President Li
1394aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li    private MyOrientationEventListener mOrientationListener;
1404aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li    // The device orientation in degrees. Default is unknown.
1414aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li    private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
1424aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li    // The orientation compensation for icons and thumbnails.
1434aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li    private int mOrientationCompensation = 0;
1441ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang    private ComboPreferences mPreferences;
145b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
146b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private static final int IDLE = 1;
147b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private static final int SNAPSHOT_IN_PROGRESS = 2;
148b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
149e239acc2185c5fb47b22a65f78ffa461e19f183cOwen Lin    private static final boolean SWITCH_CAMERA = true;
150e239acc2185c5fb47b22a65f78ffa461e19f183cOwen Lin    private static final boolean SWITCH_VIDEO = false;
151e239acc2185c5fb47b22a65f78ffa461e19f183cOwen Lin
152b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private int mStatus = IDLE;
153b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private static final String sTempCropFilename = "crop-temp";
154b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
155b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private android.hardware.Camera mCameraDevice;
1566d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen    private ContentProviderClient mMediaProviderClient;
157ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin    private SurfaceView mSurfaceView;
158b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private SurfaceHolder mSurfaceHolder = null;
15995fc5b2c5b14bb81332570f6d74f75cd63ea04b5Wu-cheng Li    private ShutterButton mShutterButton;
16095fc5b2c5b14bb81332570f6d74f75cd63ea04b5Wu-cheng Li    private FocusRectangle mFocusRectangle;
16195fc5b2c5b14bb81332570f6d74f75cd63ea04b5Wu-cheng Li    private ToneGenerator mFocusToneGenerator;
16202ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li    private GestureDetector mPopupGestureDetector;
163eb1f0c8299d96bad9bb449ff9e13eff4b43272ccHung-ying Tyan    private SwitcherSet mSwitcher;
1643f3c857e3f34650c15d764810335024654b0fcc3Owen Lin    private boolean mStartPreviewFail = false;
16585337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan    private View mIndicatorWheel;
16695fc5b2c5b14bb81332570f6d74f75cd63ea04b5Wu-cheng Li
167281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin    private GLRootView mGLRootView;
1684de149ceb47f2c251f646419907424bfb67d2b64Owen Lin
16913161c7fe39bf019a334d8ea7e4eaddc1988adadWu-cheng Li    // The last captured picture.
1709f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li    private RotateImageView mThumbnailButton;
171b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
172c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang    // mCropValue and mSaveUri are used only if isImageCaptureIntent() is true.
173cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang    private String mCropValue;
174cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang    private Uri mSaveUri;
175cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang
176b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private ImageCapture mImageCapture = null;
177b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
178f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan    // GPS on-screen indicator
179f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan    private View mGpsNoSignalView;
180f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan    private View mGpsHasSignalView;
181f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan
18259ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang    // Front/Back camera pciker for xlarge layout
18359ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang    private CameraPicker mCameraPicker;
18459ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang
1856490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin    /**
1866490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin     * An unpublished intent flag requesting to return as soon as capturing
1876490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin     * is completed.
1886490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin     *
1896490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin     * TODO: consider publishing by moving into MediaStore.
1906490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin     */
1916490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin    private final static String EXTRA_QUICK_CAPTURE =
1926490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin            "android.intent.extra.quickCapture";
1936490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin
19495fc5b2c5b14bb81332570f6d74f75cd63ea04b5Wu-cheng Li    private boolean mPreviewing;
19595fc5b2c5b14bb81332570f6d74f75cd63ea04b5Wu-cheng Li    private boolean mPausing;
19695fc5b2c5b14bb81332570f6d74f75cd63ea04b5Wu-cheng Li    private boolean mFirstTimeInitialized;
19795fc5b2c5b14bb81332570f6d74f75cd63ea04b5Wu-cheng Li    private boolean mIsImageCaptureIntent;
19895fc5b2c5b14bb81332570f6d74f75cd63ea04b5Wu-cheng Li    private boolean mRecordLocation;
199b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
2009c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project    private static final int FOCUS_NOT_STARTED = 0;
2019c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project    private static final int FOCUSING = 1;
2029c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project    private static final int FOCUSING_SNAP_ON_FINISH = 2;
2039c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project    private static final int FOCUS_SUCCESS = 3;
2049c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project    private static final int FOCUS_FAIL = 4;
2059c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project    private int mFocusState = FOCUS_NOT_STARTED;
206b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
2070e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin    private ContentResolver mContentResolver;
208b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private boolean mDidRegister = false;
209b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
210bbc560b5216bbb2e3028020e426ce6a6e0e5df08Owen Lin    private final ArrayList<MenuItem> mGalleryItems = new ArrayList<MenuItem>();
211b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
212b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private LocationManager mLocationManager = null;
213b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
214bbc560b5216bbb2e3028020e426ce6a6e0e5df08Owen Lin    private final ShutterCallback mShutterCallback = new ShutterCallback();
2159324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li    private final PostViewPictureCallback mPostViewPictureCallback =
2169324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            new PostViewPictureCallback();
217bcd13fa071abe75b3275e3bc314601f33e33972aWu-cheng Li    private final RawPictureCallback mRawPictureCallback =
218bcd13fa071abe75b3275e3bc314601f33e33972aWu-cheng Li            new RawPictureCallback();
219bcd13fa071abe75b3275e3bc314601f33e33972aWu-cheng Li    private final AutoFocusCallback mAutoFocusCallback =
220bcd13fa071abe75b3275e3bc314601f33e33972aWu-cheng Li            new AutoFocusCallback();
221e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li    private final ZoomListener mZoomListener = new ZoomListener();
2226eabb9b770a7c60cb92aa2e22f360754f32f39f8Wu-cheng Li    private final CameraErrorCallback mErrorCallback = new CameraErrorCallback();
223cede87fa23b81b49786831e90a175293e9c8bc7cYu Shan Emily Lau
224b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private long mFocusStartTime;
225b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private long mFocusCallbackTime;
226b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private long mCaptureStartTime;
227b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private long mShutterCallbackTime;
2289324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li    private long mPostViewPictureCallbackTime;
229b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private long mRawPictureCallbackTime;
230bcd13fa071abe75b3275e3bc314601f33e33972aWu-cheng Li    private long mJpegPictureCallbackTime;
231c8a7fc07e3b905a723e753b0c9144de2b0343f1fChia-chi Yeh    private long mPicturesRemaining;
232b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
2330a28549a7e6db0307b2cb5d2df7444909fb03a48Chih-Chung Chang    // These latency time are for the CameraLatency test.
2340a28549a7e6db0307b2cb5d2df7444909fb03a48Chih-Chung Chang    public long mAutoFocusTime;
2350a28549a7e6db0307b2cb5d2df7444909fb03a48Chih-Chung Chang    public long mShutterLag;
2369324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li    public long mShutterToPictureDisplayedTime;
2379324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li    public long mPictureDisplayedToJpegCallbackTime;
238e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li    public long mJpegCallbackFinishTime;
23911cac263ec966e5a5ac751c53acb851de841c62eYu Shan Emily Lau
2400f56ef5704b7f1a0874528367147487c051876d9Wu-cheng Li    // Focus mode. Options are pref_camera_focusmode_entryvalues.
2410f56ef5704b7f1a0874528367147487c051876d9Wu-cheng Li    private String mFocusMode;
24218a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin    private String mSceneMode;
243fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li    private Toast mNotSelectableToast;
2440f56ef5704b7f1a0874528367147487c051876d9Wu-cheng Li
245bbc560b5216bbb2e3028020e426ce6a6e0e5df08Owen Lin    private final Handler mHandler = new MainHandler();
24670027ba6263ffc7958a24ad8524e7e0a00323816Wu-cheng Li    // xlarge devices use control panel. Other devices use head-up display.
247469a9ef839e38630e1e652d182f7e0ee5882e277Owen Lin    private CameraHeadUpDisplay mHeadUpDisplay;
248f5bffc476b8293b30bb500bed2ab7914bb4ca411Wu-cheng Li    private ControlPanel mControlPanel;
249dfee7f813adb8de209472d24553b860d2515ea0aWu-cheng Li    private PreferenceGroup mPreferenceGroup;
250b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
251ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang    // multiple cameras support
252ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang    private int mNumberOfCameras;
253ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang    private int mCameraId;
25459ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang    private int mFrontCameraId;
25559ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang    private int mBackCameraId;
256ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang
2576490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin    private boolean mQuickCapture;
2586490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin
2590e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin    /**
2600e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin     * This Handler is used to post message back onto the main thread of the
2610e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin     * application
2620e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin     */
263b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private class MainHandler extends Handler {
264b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        @Override
265b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public void handleMessage(Message msg) {
266b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            switch (msg.what) {
267b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                case RESTART_PREVIEW: {
2686cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li                    restartPreview();
269e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                    if (mJpegPictureCallbackTime != 0) {
270e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                        long now = System.currentTimeMillis();
271e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                        mJpegCallbackFinishTime = now - mJpegPictureCallbackTime;
272e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                        Log.v(TAG, "mJpegCallbackFinishTime = "
273e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                                + mJpegCallbackFinishTime + "ms");
274e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                        mJpegPictureCallbackTime = 0;
275e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                    }
276b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    break;
277b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                }
278b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
279b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                case CLEAR_SCREEN_DELAY: {
2800e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                    getWindow().clearFlags(
2810e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
282b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    break;
283b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                }
284e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
285e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                case FIRST_TIME_INIT: {
286e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                    initializeFirstTime();
287e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                    break;
288e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                }
2891de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang
2901de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang                case SET_CAMERA_PARAMETERS_WHEN_IDLE: {
291ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                    setCameraParametersWhenIdle(0);
2921de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang                    break;
2931de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang                }
294e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li            }
295e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        }
296e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li    }
297e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
29859b5974a4ae078eb7628fd855db89e5be9b0fe6fOwen Lin    private void resetExposureCompensation() {
299f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin        String value = mPreferences.getString(CameraSettings.KEY_EXPOSURE,
300f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin                CameraSettings.EXPOSURE_DEFAULT_VALUE);
301f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin        if (!CameraSettings.EXPOSURE_DEFAULT_VALUE.equals(value)) {
302f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin            Editor editor = mPreferences.edit();
303f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin            editor.putString(CameraSettings.KEY_EXPOSURE, "0");
3044714a1cf1d211aad2d8cf1802f85f649e744beb0Wu-cheng Li            editor.apply();
305f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin            if (mHeadUpDisplay != null) {
306f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin                mHeadUpDisplay.reloadPreferences();
307f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin            }
308f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin        }
30959b5974a4ae078eb7628fd855db89e5be9b0fe6fOwen Lin    }
31059b5974a4ae078eb7628fd855db89e5be9b0fe6fOwen Lin
3116d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen    private void keepMediaProviderInstance() {
3126d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen        // We want to keep a reference to MediaProvider in camera's lifecycle.
3136d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen        // TODO: Utilize mMediaProviderClient instance to replace
3146d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen        // ContentResolver calls.
3156d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen        if (mMediaProviderClient == null) {
3166d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen            mMediaProviderClient = getContentResolver()
3176d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen                    .acquireContentProviderClient(MediaStore.AUTHORITY);
3186d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen        }
3196d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen    }
3206d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen
3216cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li    // Snapshots can only be taken after this is called. It should be called
3226cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li    // once only. We could have done these things in onCreate() but we want to
3236cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li    // make preview screen appear as soon as possible.
324522e836abc90bcb2d4e7a4ac37e269154d06c715Chih-Chung Chang    private void initializeFirstTime() {
325137417dade91f8a98cefa655cbd0d48b8cf52af1Chih-Chung Chang        if (mFirstTimeInitialized) return;
326e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
327e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        // Create orientation listenter. This should be done first because it
328e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        // takes some time to get first orientation.
3294aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li        mOrientationListener = new MyOrientationEventListener(Camera.this);
330e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        mOrientationListener.enable();
331e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
332e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        // Initialize location sevice.
333e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        mLocationManager = (LocationManager)
334e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                getSystemService(Context.LOCATION_SERVICE);
335a9c2f922763d3f7d19c237f9c5bd46611b5076fdWu-cheng Li        mRecordLocation = RecordLocationPreference.get(
336a9c2f922763d3f7d19c237f9c5bd46611b5076fdWu-cheng Li                mPreferences, getContentResolver());
337f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan        initGpsOnScreenIndicator();
338e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        if (mRecordLocation) startReceivingLocationUpdates();
339e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
3406d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen        keepMediaProviderInstance();
341d12ae2098b491bb55798758da8b698ffc625d62cWu-cheng Li        checkStorage();
342d12ae2098b491bb55798758da8b698ffc625d62cWu-cheng Li
343e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        // Initialize last picture button.
344e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        mContentResolver = getContentResolver();
345e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        if (!mIsImageCaptureIntent)  {
346dad4b187eec436a2716aca5af31f23ce5de95f39Owen Lin            findViewById(R.id.camera_switch).setOnClickListener(this);
3479f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li            initThumbnailButton();
348b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
349e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
350e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        // Initialize shutter button.
351dad4b187eec436a2716aca5af31f23ce5de95f39Owen Lin        mShutterButton = (ShutterButton) findViewById(R.id.shutter_button);
352059daa36dc6e83ca24a16d9b99925e71010bccebOwen Lin        mShutterButton.setOnShutterButtonListener(this);
353e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        mShutterButton.setVisibility(View.VISIBLE);
354e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
355e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        mFocusRectangle = (FocusRectangle) findViewById(R.id.focus_rectangle);
356e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        updateFocusIndicator();
357e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
358803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang        initializeScreenBrightness();
359e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        installIntentFilter();
360e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        initializeFocusTone();
36133be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        initializeZoom();
36270027ba6263ffc7958a24ad8524e7e0a00323816Wu-cheng Li        // xlarge devices use control panel. Other devices use head-up display.
3637f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        if (mControlPanel == null) {
3647f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li            mHeadUpDisplay = new CameraHeadUpDisplay(this);
3657f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li            mHeadUpDisplay.setListener(new MyHeadUpDisplayListener());
36670027ba6263ffc7958a24ad8524e7e0a00323816Wu-cheng Li            initializeHeadUpDisplay();
3677f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        }
36833be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        mFirstTimeInitialized = true;
369f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        changeHeadUpDisplayState();
370f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang        addIdleHandler();
371f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang    }
372f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang
373f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang    private void addIdleHandler() {
374cdf69cba3617c446e4fe9a92b4b4199c6ed36d01Owen Lin        MessageQueue queue = Looper.myQueue();
375f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang        queue.addIdleHandler(new MessageQueue.IdleHandler() {
376f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang            public boolean queueIdle() {
377f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang                ImageManager.ensureOSXCompatibleFolder();
378f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang                return false;
379f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang            }
380f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang        });
381e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li    }
382e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
3839f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li    private void initThumbnailButton() {
3849f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        mThumbnailButton =
3859f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                (RotateImageView) findViewById(R.id.review_thumbnail);
3869f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        if (mThumbnailButton != null) {
3879f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li            mThumbnailButton.setOnClickListener(this);
3889f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li            mThumbnailButton.loadData(ImageManager.getLastImageThumbPath());
3899f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li            updateThumbnailButton();
3909f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        }
3919f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li    }
3929f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li
393547caa3586633adcaddfbe766ec46a0b97ee8d4crepo sync    private void updateThumbnailButton() {
3949f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        if (mThumbnailButton == null) return;
395d12ae2098b491bb55798758da8b698ffc625d62cWu-cheng Li        // Update last image if URI is invalid and the storage is ready.
3969f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        if (!mThumbnailButton.isUriValid() && mPicturesRemaining >= 0) {
3979f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li            IImageList list = ImageManager.makeImageList(
3989f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                mContentResolver,
3999f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                dataLocation(),
4009f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                ImageManager.INCLUDE_IMAGES,
4019f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                ImageManager.SORT_ASCENDING,
4029f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                ImageManager.CAMERA_IMAGE_BUCKET_ID);
4039f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li            int count = list.getCount();
4049f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li            if (count > 0) {
4059f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                IImage image = list.getImageAt(count - 1);
4069f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                Uri uri = image.fullSizeImageUri();
4079f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                mThumbnailButton.setData(uri, image.miniThumbBitmap());
4089f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li            } else {
4099f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                mThumbnailButton.setData(null, null);
4109f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li            }
4119f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li            list.close();
412547caa3586633adcaddfbe766ec46a0b97ee8d4crepo sync        }
41395b65b1c84ad44b43bd3defbdba5d881c0881d35Chung-yih Wang        mThumbnailButton.setVisibility(
41495b65b1c84ad44b43bd3defbdba5d881c0881d35Chung-yih Wang                (mThumbnailButton.getUri() != null) ? View.VISIBLE : View.GONE);
4159f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li    }
4169f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li
4179f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li    private void setLastPictureThumb(byte[] data, int degree, Uri uri) {
4189f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        if (mThumbnailButton == null) return;
4199f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        BitmapFactory.Options options = new BitmapFactory.Options();
4209f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        options.inSampleSize = 16;
4219f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        Bitmap lastPictureThumb =
4229f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                BitmapFactory.decodeByteArray(data, 0, data.length, options);
4239f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        lastPictureThumb = Util.rotate(lastPictureThumb, degree);
4249f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li        mThumbnailButton.setData(uri, lastPictureThumb);
42595b65b1c84ad44b43bd3defbdba5d881c0881d35Chung-yih Wang        if (lastPictureThumb != null) {
42695b65b1c84ad44b43bd3defbdba5d881c0881d35Chung-yih Wang            mThumbnailButton.setVisibility(View.VISIBLE);
42795b65b1c84ad44b43bd3defbdba5d881c0881d35Chung-yih Wang        }
4289f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li    }
4299f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li
430e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li    // If the activity is paused and resumed, this method will be called in
431e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li    // onResume.
432522e836abc90bcb2d4e7a4ac37e269154d06c715Chih-Chung Chang    private void initializeSecondTime() {
433e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        // Start orientation listener as soon as possible because it takes
434e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        // some time to get first orientation.
435e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        mOrientationListener.enable();
436e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
437e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        // Start location update if needed.
4380d73bccf89870682007dd32d4a84bcdb812e2cc7Owen Lin        mRecordLocation = RecordLocationPreference.get(
4390d73bccf89870682007dd32d4a84bcdb812e2cc7Owen Lin                mPreferences, getContentResolver());
440e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        if (mRecordLocation) startReceivingLocationUpdates();
441e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
442e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        installIntentFilter();
443e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        initializeFocusTone();
444f1672b4a703ea7600f17f451f6632f1cd399e839Owen Lin        initializeZoom();
445f1672b4a703ea7600f17f451f6632f1cd399e839Owen Lin        changeHeadUpDisplayState();
446547caa3586633adcaddfbe766ec46a0b97ee8d4crepo sync
4476d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen        keepMediaProviderInstance();
448d12ae2098b491bb55798758da8b698ffc625d62cWu-cheng Li        checkStorage();
449d12ae2098b491bb55798758da8b698ffc625d62cWu-cheng Li
450547caa3586633adcaddfbe766ec46a0b97ee8d4crepo sync        if (!mIsImageCaptureIntent) {
451547caa3586633adcaddfbe766ec46a0b97ee8d4crepo sync            updateThumbnailButton();
452547caa3586633adcaddfbe766ec46a0b97ee8d4crepo sync        }
453937fc48b37fafe3ffc8f4b52bd9a171bbb4d3a37Owen Lin    }
454b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
45535bda2dc8ab95a7baf68b544cf97826aabff6210Wu-cheng Li    private void initializeZoom() {
456316af30d026dd182edb482ef6de7a330d6b1b012Wu-cheng Li        if (!mParameters.isZoomSupported()) return;
45735bda2dc8ab95a7baf68b544cf97826aabff6210Wu-cheng Li
458316af30d026dd182edb482ef6de7a330d6b1b012Wu-cheng Li        mZoomMax = mParameters.getMaxZoom();
459316af30d026dd182edb482ef6de7a330d6b1b012Wu-cheng Li        mSmoothZoomSupported = mParameters.isSmoothZoomSupported();
4608974ca6b9412446d05f580c8d694d7e61eacb5ceWu-cheng Li        if (mZoomPicker != null) {
461c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li            mZoomPicker.setZoomRatios(getZoomRatios());
46217bf7bc255fffcf9ae795a5b3983cd4878383613Hung-ying Tyan            mZoomPicker.setZoomIndex(mParameters.getZoom());
463c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li            mZoomPicker.setOnZoomChangeListener(
464c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li                    new ZoomPicker.OnZoomChangedListener() {
465c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li                public void onZoomChanged(int index) {
466c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li                    onZoomValueChanged(index);
467c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li                }
468c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li            });
469c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li        }
47033be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin
471e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li        mCameraDevice.setZoomChangeListener(mZoomListener);
47235bda2dc8ab95a7baf68b544cf97826aabff6210Wu-cheng Li    }
47335bda2dc8ab95a7baf68b544cf97826aabff6210Wu-cheng Li
47433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin    private void onZoomValueChanged(int index) {
4750f405feacb2fa89d22e9fd8cb982cffad14af4a3Hung-ying Tyan        // Not useful to change zoom value when the activity is paused.
4760f405feacb2fa89d22e9fd8cb982cffad14af4a3Hung-ying Tyan        if (mPausing) return;
4770f405feacb2fa89d22e9fd8cb982cffad14af4a3Hung-ying Tyan
47833be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        if (mSmoothZoomSupported) {
47933be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            if (mTargetZoomValue != index && mZoomState != ZOOM_STOPPED) {
48033be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                mTargetZoomValue = index;
48133be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                if (mZoomState == ZOOM_START) {
48233be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                    mZoomState = ZOOM_STOPPING;
48333be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                    mCameraDevice.stopSmoothZoom();
48433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                }
48533be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            } else if (mZoomState == ZOOM_STOPPED && mZoomValue != index) {
48633be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                mTargetZoomValue = index;
48733be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                mCameraDevice.startSmoothZoom(index);
48833be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                mZoomState = ZOOM_START;
48933be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            }
49033be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        } else {
491ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            mZoomValue = index;
492ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            setCameraParametersWhenIdle(UPDATE_PARAM_ZOOM);
49333be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        }
49433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin    }
49533be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin
49633be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin    private float[] getZoomRatios() {
4977add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li        if(!mParameters.isZoomSupported()) return null;
49833be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        List<Integer> zoomRatios = mParameters.getZoomRatios();
4997add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li        float result[] = new float[zoomRatios.size()];
5007add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li        for (int i = 0, n = result.length; i < n; ++i) {
5017add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li            result[i] = (float) zoomRatios.get(i) / 100f;
50233be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        }
5037add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li        return result;
50433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin    }
50533be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin
50602ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li    private int mPopupLocations[] = new int[2];
50702ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li    private class PopupGestureListener extends
50802ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li            GestureDetector.SimpleOnGestureListener {
50902ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li        public boolean onDown(MotionEvent e) {
51002ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li            // Check if the popup window is visible.
51102ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li            View v = mControlPanel.getActivePopupWindow();
51202ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li            if (v == null) return false;
51302ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li
514937ea10204656beaa848df913dc9cb9b5a9b934fWu-cheng Li            int x = Math.round(e.getX());
515937ea10204656beaa848df913dc9cb9b5a9b934fWu-cheng Li            int y = Math.round(e.getY());
516937ea10204656beaa848df913dc9cb9b5a9b934fWu-cheng Li
51702ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li            // Dismiss the popup window if users touch on the outside.
51802ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li            v.getLocationOnScreen(mPopupLocations);
51902ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li            if (x < mPopupLocations[0] || x > mPopupLocations[0] + v.getWidth()
52002ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li                    || y < mPopupLocations[1] || y > mPopupLocations[1] + v.getHeight()) {
52102ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li                mControlPanel.dismissSettingPopup();
522937ea10204656beaa848df913dc9cb9b5a9b934fWu-cheng Li                // Let event fall through.
52302ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li            }
52402ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li            return false;
52502ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li        }
52602ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li    }
52702ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li
528aa3cc2012a1ee0cc04e1244dd977cb8bb84683e4Wu-cheng Li    @Override
529aa3cc2012a1ee0cc04e1244dd977cb8bb84683e4Wu-cheng Li    public boolean dispatchTouchEvent(MotionEvent m) {
53002ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li        // Check if the popup window should be dismissed first.
53102ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li        if (mPopupGestureDetector != null && mPopupGestureDetector.onTouchEvent(m)) {
53202ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li            return true;
533aa3cc2012a1ee0cc04e1244dd977cb8bb84683e4Wu-cheng Li        }
53402ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li
535dfc4793ddd07b8a80cdc9989afba006582443a01Wu-cheng Li        return super.dispatchTouchEvent(m);
536aa3cc2012a1ee0cc04e1244dd977cb8bb84683e4Wu-cheng Li    }
537aa3cc2012a1ee0cc04e1244dd977cb8bb84683e4Wu-cheng Li
53891227ecf9382fd0af89f6e31794ba3f286e7c3c8Wu-cheng Li    LocationListener [] mLocationListeners = new LocationListener[] {
539b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            new LocationListener(LocationManager.GPS_PROVIDER),
540b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            new LocationListener(LocationManager.NETWORK_PROVIDER)
541b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    };
542b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
543b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
544b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        @Override
545b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public void onReceive(Context context, Intent intent) {
546b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            String action = intent.getAction();
5471e50968c0a9eba523efcfdda86e298e33e95687dWu-cheng Li            if (action.equals(Intent.ACTION_MEDIA_MOUNTED)
5481e50968c0a9eba523efcfdda86e298e33e95687dWu-cheng Li                    || action.equals(Intent.ACTION_MEDIA_UNMOUNTED)
549523432315cea8f18d703a0d547b4614ce42b6918Chih-Chung Chang                    || action.equals(Intent.ACTION_MEDIA_CHECKING)) {
5501e50968c0a9eba523efcfdda86e298e33e95687dWu-cheng Li                checkStorage();
551b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
5521e50968c0a9eba523efcfdda86e298e33e95687dWu-cheng Li                checkStorage();
553d12ae2098b491bb55798758da8b698ffc625d62cWu-cheng Li                if (!mIsImageCaptureIntent)  {
554d12ae2098b491bb55798758da8b698ffc625d62cWu-cheng Li                    updateThumbnailButton();
555d12ae2098b491bb55798758da8b698ffc625d62cWu-cheng Li                }
556b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
557b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
558b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    };
559b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
56059ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang    private void initializeCameraPicker() {
56159ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang        mCameraPicker = (CameraPicker) findViewById(R.id.camera_picker);
56259ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang        if (mCameraPicker != null) {
5639e76e8d6eec20739203d213ead1fe1aab3b939aeChung-yih Wang            mCameraPicker.setImageResource(R.drawable.camera_toggle);
56459ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang            ListPreference pref = mPreferenceGroup.findPreference(
56559ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang                    CameraSettings.KEY_CAMERA_ID);
56659ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang            if (pref != null) {
56759ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang                mCameraPicker.initialize(pref);
56859ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang                mCameraPicker.setListener(new MyCameraPickerListener());
56959ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang            }
57059ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang        }
57159ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang    }
57259ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang
573f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan    private void initGpsOnScreenIndicator() {
574f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan        mGpsNoSignalView = findViewById(R.id.onscreen_gps_indicator_no_signal);
575f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan        mGpsHasSignalView = findViewById(R.id.onscreen_gps_indicator_on);
576f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan    }
577f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan
578f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan    private void showGpsOnScreenIndicator(boolean hasSignal) {
579f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan        if (hasSignal) {
580f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan            if (mGpsNoSignalView != null) mGpsNoSignalView.setVisibility(View.INVISIBLE);
581f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan            if (mGpsHasSignalView != null) mGpsHasSignalView.setVisibility(View.VISIBLE);
582f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan        } else {
583f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan            if (mGpsNoSignalView != null) mGpsNoSignalView.setVisibility(View.VISIBLE);
584f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan            if (mGpsHasSignalView != null) mGpsHasSignalView.setVisibility(View.INVISIBLE);
585f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan        }
586f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan    }
587f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan
588f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan    private void hideGpsOnScreenIndicator() {
589f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan        if (mGpsNoSignalView != null) mGpsNoSignalView.setVisibility(View.INVISIBLE);
590f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan        if (mGpsHasSignalView != null) mGpsHasSignalView.setVisibility(View.INVISIBLE);
591f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan    }
592f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan
5930e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin    private class LocationListener
5940e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            implements android.location.LocationListener {
595b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        Location mLastLocation;
596b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        boolean mValid = false;
597b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        String mProvider;
598b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
599b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public LocationListener(String provider) {
600b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mProvider = provider;
601b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mLastLocation = new Location(mProvider);
602b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
603b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
604b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public void onLocationChanged(Location newLocation) {
6050e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            if (newLocation.getLatitude() == 0.0
6060e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                    && newLocation.getLongitude() == 0.0) {
607b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                // Hack to filter out 0.0,0.0 locations
608b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                return;
609b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
61007c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin            // If GPS is available before start camera, we won't get status
61107c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin            // update so update GPS indicator when we receive data.
61207c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin            if (mRecordLocation
61307c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin                    && LocationManager.GPS_PROVIDER.equals(mProvider)) {
6144de149ceb47f2c251f646419907424bfb67d2b64Owen Lin                if (mHeadUpDisplay != null) {
6154de149ceb47f2c251f646419907424bfb67d2b64Owen Lin                    mHeadUpDisplay.setGpsHasSignal(true);
6164de149ceb47f2c251f646419907424bfb67d2b64Owen Lin                }
617f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan                showGpsOnScreenIndicator(true);
61807c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin            }
6198303435a2466803e05dd99c60cdb2de074b79162Wu-cheng Li            if (!mValid) {
6208303435a2466803e05dd99c60cdb2de074b79162Wu-cheng Li                Log.d(TAG, "Got first location.");
6218303435a2466803e05dd99c60cdb2de074b79162Wu-cheng Li            }
622b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mLastLocation.set(newLocation);
623b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mValid = true;
624b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
625b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
626b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public void onProviderEnabled(String provider) {
627b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
628b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
629b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public void onProviderDisabled(String provider) {
630b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mValid = false;
631b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
632b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
6330e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin        public void onStatusChanged(
6340e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                String provider, int status, Bundle extras) {
63507c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin            switch(status) {
63607c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin                case LocationProvider.OUT_OF_SERVICE:
63707c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin                case LocationProvider.TEMPORARILY_UNAVAILABLE: {
63807c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin                    mValid = false;
63907c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin                    if (mRecordLocation &&
64007c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin                            LocationManager.GPS_PROVIDER.equals(provider)) {
6414de149ceb47f2c251f646419907424bfb67d2b64Owen Lin                        if (mHeadUpDisplay != null) {
6424de149ceb47f2c251f646419907424bfb67d2b64Owen Lin                            mHeadUpDisplay.setGpsHasSignal(false);
6434de149ceb47f2c251f646419907424bfb67d2b64Owen Lin                        }
644f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan                        showGpsOnScreenIndicator(false);
64507c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin                    }
64607c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin                    break;
64707c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin                }
648b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
649b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
650b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
651b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public Location current() {
652b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            return mValid ? mLastLocation : null;
653b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
654937fc48b37fafe3ffc8f4b52bd9a171bbb4d3a37Owen Lin    }
655b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
6560e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin    private final class ShutterCallback
6570e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            implements android.hardware.Camera.ShutterCallback {
658b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public void onShutter() {
659bcd13fa071abe75b3275e3bc314601f33e33972aWu-cheng Li            mShutterCallbackTime = System.currentTimeMillis();
66011cac263ec966e5a5ac751c53acb851de841c62eYu Shan Emily Lau            mShutterLag = mShutterCallbackTime - mCaptureStartTime;
66111cac263ec966e5a5ac751c53acb851de841c62eYu Shan Emily Lau            Log.v(TAG, "mShutterLag = " + mShutterLag + "ms");
66275ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang            clearFocusState();
663b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
664937fc48b37fafe3ffc8f4b52bd9a171bbb4d3a37Owen Lin    }
665b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
6669324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li    private final class PostViewPictureCallback implements PictureCallback {
6679324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li        public void onPictureTaken(
6689324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                byte [] data, android.hardware.Camera camera) {
6699324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            mPostViewPictureCallbackTime = System.currentTimeMillis();
6709324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            Log.v(TAG, "mShutterToPostViewCallbackTime = "
6719324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                    + (mPostViewPictureCallbackTime - mShutterCallbackTime)
6729324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                    + "ms");
6739324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li        }
6749324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li    }
6759324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li
676b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private final class RawPictureCallback implements PictureCallback {
6770e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin        public void onPictureTaken(
6780e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                byte [] rawData, android.hardware.Camera camera) {
679b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mRawPictureCallbackTime = System.currentTimeMillis();
6809324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            Log.v(TAG, "mShutterToRawCallbackTime = "
6819324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                    + (mRawPictureCallbackTime - mShutterCallbackTime) + "ms");
682b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
683937fc48b37fafe3ffc8f4b52bd9a171bbb4d3a37Owen Lin    }
684b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
685b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private final class JpegPictureCallback implements PictureCallback {
686b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        Location mLocation;
687b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
688b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public JpegPictureCallback(Location loc) {
689b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mLocation = loc;
690b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
691b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
6920e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin        public void onPictureTaken(
6936c28119cb169369f9394d5e4e46bc7f29fc38e46repo sync                final byte [] jpegData, final android.hardware.Camera camera) {
694de365d87520c97d6b1636442e6b5c82a3e036f29The Android Open Source Project            if (mPausing) {
695e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project                return;
696e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project            }
697b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
698bcd13fa071abe75b3275e3bc314601f33e33972aWu-cheng Li            mJpegPictureCallbackTime = System.currentTimeMillis();
6999324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            // If postview callback has arrived, the captured image is displayed
7009324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            // in postview callback. If not, the captured image is displayed in
7019324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            // raw picture callback.
7029324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            if (mPostViewPictureCallbackTime != 0) {
7039324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                mShutterToPictureDisplayedTime =
7049324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                        mPostViewPictureCallbackTime - mShutterCallbackTime;
7059324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                mPictureDisplayedToJpegCallbackTime =
7069324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                        mJpegPictureCallbackTime - mPostViewPictureCallbackTime;
7079324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            } else {
7089324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                mShutterToPictureDisplayedTime =
7099324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                        mRawPictureCallbackTime - mShutterCallbackTime;
7109324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                mPictureDisplayedToJpegCallbackTime =
7119324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                        mJpegPictureCallbackTime - mRawPictureCallbackTime;
7129324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            }
7139324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            Log.v(TAG, "mPictureDisplayedToJpegCallbackTime = "
7149324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                    + mPictureDisplayedToJpegCallbackTime + "ms");
715b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
7166cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li            if (!mIsImageCaptureIntent) {
717e50d2553aeb9e55504697e2a72759e8f46ee2992Hung-ying Tyan                enableCameraControls(true);
718e50d2553aeb9e55504697e2a72759e8f46ee2992Hung-ying Tyan
71905d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang                // We want to show the taken picture for a while, so we wait
72005d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang                // for at least 1.2 second before restarting the preview.
7219324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                long delay = 1200 - mPictureDisplayedToJpegCallbackTime;
722ed8d17a1716b0c8d7d4f4451ad15de66355e9dc0Wu-cheng Li                if (delay < 0) {
72305d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang                    restartPreview();
72405d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang                } else {
72505d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang                    mHandler.sendEmptyMessageDelayed(RESTART_PREVIEW, delay);
72605d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang                }
727b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
72805d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang            mImageCapture.storeImage(jpegData, camera, mLocation);
72905d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang
730721d2d2a963799fad4bda9bb4b278c24fc469303Chia-chi Yeh            // Check this in advance of each shot so we don't add to shutter
73105d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang            // latency. It's true that someone else could write to the SD card in
73205d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang            // the mean time and fill it, but that could have happened between the
73305d20c0c53cdb3a0381f03b160a840f84d42deefChih-Chung Chang            // shutter press and saving the JPEG too.
734721d2d2a963799fad4bda9bb4b278c24fc469303Chia-chi Yeh            checkStorage();
735e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li
736e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li            if (!mHandler.hasMessages(RESTART_PREVIEW)) {
737e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                long now = System.currentTimeMillis();
738e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                mJpegCallbackFinishTime = now - mJpegPictureCallbackTime;
739e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                Log.v(TAG, "mJpegCallbackFinishTime = "
740e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                        + mJpegCallbackFinishTime + "ms");
741e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li                mJpegPictureCallbackTime = 0;
742e6ffea291dde5adde94bf337731213e7fd3c8c56Wu-cheng Li            }
743b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
744937fc48b37fafe3ffc8f4b52bd9a171bbb4d3a37Owen Lin    }
745b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
7460e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin    private final class AutoFocusCallback
7470e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            implements android.hardware.Camera.AutoFocusCallback {
7480e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin        public void onAutoFocus(
7490e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                boolean focused, android.hardware.Camera camera) {
750bcd13fa071abe75b3275e3bc314601f33e33972aWu-cheng Li            mFocusCallbackTime = System.currentTimeMillis();
75111cac263ec966e5a5ac751c53acb851de841c62eYu Shan Emily Lau            mAutoFocusTime = mFocusCallbackTime - mFocusStartTime;
75211cac263ec966e5a5ac751c53acb851de841c62eYu Shan Emily Lau            Log.v(TAG, "mAutoFocusTime = " + mAutoFocusTime + "ms");
7535fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li            if (mFocusState == FOCUSING_SNAP_ON_FINISH) {
7540e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                // Take the picture no matter focus succeeds or fails. No need
7550e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                // to play the AF sound if we're about to play the shutter
7560e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                // sound.
75775ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang                if (focused) {
75875ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang                    mFocusState = FOCUS_SUCCESS;
75975ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang                } else {
76075ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang                    mFocusState = FOCUS_FAIL;
76175ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang                }
7626c28119cb169369f9394d5e4e46bc7f29fc38e46repo sync                mImageCapture.onSnap();
7639c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project            } else if (mFocusState == FOCUSING) {
7649c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project                // User is half-pressing the focus key. Play the focus tone.
7659c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project                // Do not take the picture now.
7669c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project                ToneGenerator tg = mFocusToneGenerator;
7670e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                if (tg != null) {
7680e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                    tg.startTone(ToneGenerator.TONE_PROP_BEEP2);
7690e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                }
7709c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project                if (focused) {
7719c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project                    mFocusState = FOCUS_SUCCESS;
7720e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                } else {
7739c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project                    mFocusState = FOCUS_FAIL;
7749c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project                }
7759c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project            } else if (mFocusState == FOCUS_NOT_STARTED) {
7769c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project                // User has released the focus key before focus completes.
7779c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project                // Do nothing.
778de365d87520c97d6b1636442e6b5c82a3e036f29The Android Open Source Project            }
779b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            updateFocusIndicator();
780b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
781937fc48b37fafe3ffc8f4b52bd9a171bbb4d3a37Owen Lin    }
782b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
783e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li    private final class ZoomListener
784e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li            implements android.hardware.Camera.OnZoomChangeListener {
785e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li        public void onZoomChange(
786e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li                int value, boolean stopped, android.hardware.Camera camera) {
787e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li            Log.v(TAG, "Zoom changed: value=" + value + ". stopped="+ stopped);
788e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li            mZoomValue = value;
7892c854bb917480b69630c9ab01aa9247df57a564aWu-cheng Li            // Keep mParameters up to date. We do not getParameter again in
7902c854bb917480b69630c9ab01aa9247df57a564aWu-cheng Li            // takePicture. If we do not do this, wrong zoom value will be set.
791e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li            mParameters.setZoom(value);
792316af30d026dd182edb482ef6de7a330d6b1b012Wu-cheng Li            // We only care if the zoom is stopped. mZooming is set to true when
793316af30d026dd182edb482ef6de7a330d6b1b012Wu-cheng Li            // we start smooth zoom.
79433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            if (stopped && mZoomState != ZOOM_STOPPED) {
795e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li                if (value != mTargetZoomValue) {
79633be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                    mCameraDevice.startSmoothZoom(mTargetZoomValue);
79733be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                    mZoomState = ZOOM_START;
79833be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                } else {
79933be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                    mZoomState = ZOOM_STOPPED;
80033be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                }
80133be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            }
80235bda2dc8ab95a7baf68b544cf97826aabff6210Wu-cheng Li        }
80335bda2dc8ab95a7baf68b544cf97826aabff6210Wu-cheng Li    }
80435bda2dc8ab95a7baf68b544cf97826aabff6210Wu-cheng Li
8057b645fbd40ec88dc18f4d1c9b751e8f8b48f6bfeChih-Chung Chang    private class ImageCapture {
806b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
807b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        private Uri mLastContentUri;
808b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
809c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        byte[] mCaptureOnlyData;
810b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
8110b706b8d339d6d23b32878c398097beed25c079eChih-Chung Chang        // Returns the rotation degree in the jpeg header.
8120b706b8d339d6d23b32878c398097beed25c079eChih-Chung Chang        private int storeImage(byte[] data, Location loc) {
813b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            try {
814b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                long dateTaken = System.currentTimeMillis();
81530055f6830b3d524cfbdfeab6536bb46e390747aPetri Gynther                String title = createName(dateTaken);
81630055f6830b3d524cfbdfeab6536bb46e390747aPetri Gynther                String filename = title + ".jpg";
817140229cc988a264d9719b8dcc41196cc87092440Chih-Chung Chang                int[] degree = new int[1];
81858e94eddd371a9bb7c04936dbeb99615fd1a0c2cChih-Chung Chang                mLastContentUri = ImageManager.addImage(
819b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        mContentResolver,
82030055f6830b3d524cfbdfeab6536bb46e390747aPetri Gynther                        title,
821b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        dateTaken,
822140229cc988a264d9719b8dcc41196cc87092440Chih-Chung Chang                        loc, // location from gps/network
82330055f6830b3d524cfbdfeab6536bb46e390747aPetri Gynther                        ImageManager.CAMERA_IMAGE_BUCKET_NAME, filename,
824140229cc988a264d9719b8dcc41196cc87092440Chih-Chung Chang                        null, data,
825140229cc988a264d9719b8dcc41196cc87092440Chih-Chung Chang                        degree);
826140229cc988a264d9719b8dcc41196cc87092440Chih-Chung Chang                return degree[0];
827b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } catch (Exception ex) {
828b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                Log.e(TAG, "Exception while compressing image.", ex);
8290b706b8d339d6d23b32878c398097beed25c079eChih-Chung Chang                return 0;
830b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
831b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
832b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
83391acfc99279d5ece7ac9cb2d7a2980eb0d3b50daChih-Chung Chang        public void storeImage(final byte[] data,
83491acfc99279d5ece7ac9cb2d7a2980eb0d3b50daChih-Chung Chang                android.hardware.Camera camera, Location loc) {
8356cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li            if (!mIsImageCaptureIntent) {
8360b706b8d339d6d23b32878c398097beed25c079eChih-Chung Chang                int degree = storeImage(data, loc);
8370e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                sendBroadcast(new Intent(
8380e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                        "com.android.camera.NEW_PICTURE", mLastContentUri));
8390b706b8d339d6d23b32878c398097beed25c079eChih-Chung Chang                setLastPictureThumb(data, degree,
8400b706b8d339d6d23b32878c398097beed25c079eChih-Chung Chang                        mImageCapture.getLastCaptureUri());
841b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } else {
842c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang                mCaptureOnlyData = data;
8436490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin                if (!mQuickCapture) {
8446490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin                    showPostCaptureAlert();
8456490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin                } else {
8466490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin                    doAttach();
8476490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin                }
848b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
849b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
850b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
8510e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin        /**
852b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project         * Initiate the capture of an image.
853b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project         */
854d16a06161fe9e10ae8f656bca4d4482b235cd172Wu-cheng Li        public void initiate() {
85511c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks            if (mCameraDevice == null) {
85611c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks                return;
85711c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks            }
85811c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks
859d16a06161fe9e10ae8f656bca4d4482b235cd172Wu-cheng Li            capture();
860b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
861b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
862b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public Uri getLastCaptureUri() {
863b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            return mLastContentUri;
864b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
865b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
866c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        public byte[] getLastCaptureData() {
867c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang            return mCaptureOnlyData;
868b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
869b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
870d16a06161fe9e10ae8f656bca4d4482b235cd172Wu-cheng Li        private void capture() {
871c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang            mCaptureOnlyData = null;
872b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
873c1dd72054122befb49aa1ca11ffa589b00186f80Wu-cheng Li            // See android.hardware.Camera.Parameters.setRotation for
874c1dd72054122befb49aa1ca11ffa589b00186f80Wu-cheng Li            // documentation.
8754aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            int rotation = 0;
8764aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            if (mOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
8774aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li                CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
878c1dd72054122befb49aa1ca11ffa589b00186f80Wu-cheng Li                if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
879c1dd72054122befb49aa1ca11ffa589b00186f80Wu-cheng Li                    rotation = (info.orientation - mOrientation + 360) % 360;
880c1dd72054122befb49aa1ca11ffa589b00186f80Wu-cheng Li                } else {  // back-facing camera
881c1dd72054122befb49aa1ca11ffa589b00186f80Wu-cheng Li                    rotation = (info.orientation + mOrientation) % 360;
882c1dd72054122befb49aa1ca11ffa589b00186f80Wu-cheng Li                }
8834aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            }
8844aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            mParameters.setRotation(rotation);
88598334d24526bbbab8c5f33e426173488216c9092Chih-Chung Chang
8864305c70dc849156be5d79d52772ea71cd890ca40Wu-cheng Li            // Clear previous GPS location from the parameters.
8874305c70dc849156be5d79d52772ea71cd890ca40Wu-cheng Li            mParameters.removeGpsData();
888b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
88929fbda065e287c87b4939a541a24e4f965e0b4f3Ray Chen            // We always encode GpsTimeStamp
89029fbda065e287c87b4939a541a24e4f965e0b4f3Ray Chen            mParameters.setGpsTimestamp(System.currentTimeMillis() / 1000);
89129fbda065e287c87b4939a541a24e4f965e0b4f3Ray Chen
8924305c70dc849156be5d79d52772ea71cd890ca40Wu-cheng Li            // Set GPS location.
89307c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin            Location loc = mRecordLocation ? getCurrentLocation() : null;
894b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            if (loc != null) {
895b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                double lat = loc.getLatitude();
896b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                double lon = loc.getLongitude();
897b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                boolean hasLatLon = (lat != 0.0d) || (lon != 0.0d);
898b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
899b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                if (hasLatLon) {
9008303435a2466803e05dd99c60cdb2de074b79162Wu-cheng Li                    Log.d(TAG, "Set gps location");
9014305c70dc849156be5d79d52772ea71cd890ca40Wu-cheng Li                    mParameters.setGpsLatitude(lat);
9024305c70dc849156be5d79d52772ea71cd890ca40Wu-cheng Li                    mParameters.setGpsLongitude(lon);
9032ab82979d1701477f8aee1f78919c77edb4e1eaaRay Chen                    mParameters.setGpsProcessingMethod(loc.getProvider().toUpperCase());
904d7e9d1916fbabaecd4be9dacb4472fa3ffe62ed4The Android Open Source Project                    if (loc.hasAltitude()) {
9054305c70dc849156be5d79d52772ea71cd890ca40Wu-cheng Li                        mParameters.setGpsAltitude(loc.getAltitude());
906d7e9d1916fbabaecd4be9dacb4472fa3ffe62ed4The Android Open Source Project                    } else {
907d7e9d1916fbabaecd4be9dacb4472fa3ffe62ed4The Android Open Source Project                        // for NETWORK_PROVIDER location provider, we may have
908d7e9d1916fbabaecd4be9dacb4472fa3ffe62ed4The Android Open Source Project                        // no altitude information, but the driver needs it, so
909d7e9d1916fbabaecd4be9dacb4472fa3ffe62ed4The Android Open Source Project                        // we fake one.
9104305c70dc849156be5d79d52772ea71cd890ca40Wu-cheng Li                        mParameters.setGpsAltitude(0);
911d7e9d1916fbabaecd4be9dacb4472fa3ffe62ed4The Android Open Source Project                    }
912b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    if (loc.getTime() != 0) {
913b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        // Location.getTime() is UTC in milliseconds.
914b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        // gps-timestamp is UTC in seconds.
915b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        long utcTimeSeconds = loc.getTime() / 1000;
9164305c70dc849156be5d79d52772ea71cd890ca40Wu-cheng Li                        mParameters.setGpsTimestamp(utcTimeSeconds);
917b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    }
918b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                } else {
919b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    loc = null;
920b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                }
921b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
922b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
923b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mCameraDevice.setParameters(mParameters);
924b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
9250e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            mCameraDevice.takePicture(mShutterCallback, mRawPictureCallback,
9269324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li                    mPostViewPictureCallback, new JpegPictureCallback(loc));
9275fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li            mPreviewing = false;
928b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
929b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
930b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        public void onSnap() {
9316cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li            // If we are already in the middle of taking a snapshot then ignore.
9326cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li            if (mPausing || mStatus == SNAPSHOT_IN_PROGRESS) {
933e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project                return;
934e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project            }
935bcd13fa071abe75b3275e3bc314601f33e33972aWu-cheng Li            mCaptureStartTime = System.currentTimeMillis();
9369324f11afe9366cd5479828a214dc8de8696a5abWu-cheng Li            mPostViewPictureCallbackTime = 0;
9377f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li            enableCameraControls(false);
938b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mStatus = SNAPSHOT_IN_PROGRESS;
939b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
940d16a06161fe9e10ae8f656bca4d4482b235cd172Wu-cheng Li            mImageCapture.initiate();
941b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
942de365d87520c97d6b1636442e6b5c82a3e036f29The Android Open Source Project
943c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        private void clearLastData() {
944c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang            mCaptureOnlyData = null;
9459a379bd668227d3dcea30312025f304fd248f6e3The Android Open Source Project        }
946b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
947b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
948271b3095b9f763421c0547109da9de774795072dChih-Chung Chang    private boolean saveDataToFile(String filePath, byte[] data) {
949cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        FileOutputStream f = null;
950cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        try {
951cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang            f = new FileOutputStream(filePath);
952cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang            f.write(data);
953cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        } catch (IOException e) {
954cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang            return false;
955cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        } finally {
9565f039073a239fc8ebf94238c3dce24dc1cce865bChia-chi Yeh            Util.closeSilently(f);
957cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        }
958cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        return true;
959cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang    }
960cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang
96130055f6830b3d524cfbdfeab6536bb46e390747aPetri Gynther    private String createName(long dateTaken) {
96230055f6830b3d524cfbdfeab6536bb46e390747aPetri Gynther        Date date = new Date(dateTaken);
96330055f6830b3d524cfbdfeab6536bb46e390747aPetri Gynther        SimpleDateFormat dateFormat = new SimpleDateFormat(
96430055f6830b3d524cfbdfeab6536bb46e390747aPetri Gynther                getString(R.string.image_file_name_format));
96530055f6830b3d524cfbdfeab6536bb46e390747aPetri Gynther
96630055f6830b3d524cfbdfeab6536bb46e390747aPetri Gynther        return dateFormat.format(date);
967b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
968b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
969b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    @Override
970b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public void onCreate(Bundle icicle) {
971b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        super.onCreate(icicle);
972b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
973a2cbd31e81bf6b4a65ade4a5c34c303546d57d8cHung-ying Tyan        mIsImageCaptureIntent = isImageCaptureIntent();
974a2cbd31e81bf6b4a65ade4a5c34c303546d57d8cHung-ying Tyan        if (mIsImageCaptureIntent) {
975a2cbd31e81bf6b4a65ade4a5c34c303546d57d8cHung-ying Tyan            setContentView(R.layout.camera_attach);
976a2cbd31e81bf6b4a65ade4a5c34c303546d57d8cHung-ying Tyan        } else {
977a2cbd31e81bf6b4a65ade4a5c34c303546d57d8cHung-ying Tyan            setContentView(R.layout.camera);
978a2cbd31e81bf6b4a65ade4a5c34c303546d57d8cHung-ying Tyan        }
979ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin        mSurfaceView = (SurfaceView) findViewById(R.id.camera_preview);
980ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin
9811ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang        mPreferences = new ComboPreferences(this);
9821ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal());
9831ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang        mCameraId = CameraSettings.readPreferredCameraId(mPreferences);
9841ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang        mPreferences.setLocalId(this, mCameraId);
9851ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang        CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
98659b5974a4ae078eb7628fd855db89e5be9b0fe6fOwen Lin
987ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        mNumberOfCameras = CameraHolder.instance().getNumberOfCameras();
9886490868a51c9b8c79f30c8d257f89adb8d26b7b9Owen Lin        mQuickCapture = getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
98948f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li
990f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin        // we need to reset exposure for the preview
991f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin        resetExposureCompensation();
9920e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin        /*
99348f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li         * To reduce startup time, we start the preview in another thread.
99448f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li         * We make sure the preview is started at the end of onCreate.
9950e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin         */
99648f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        Thread startPreviewThread = new Thread(new Runnable() {
99711c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks            public void run() {
9983f3c857e3f34650c15d764810335024654b0fcc3Owen Lin                try {
9993f3c857e3f34650c15d764810335024654b0fcc3Owen Lin                    mStartPreviewFail = false;
10003f3c857e3f34650c15d764810335024654b0fcc3Owen Lin                    startPreview();
10013f3c857e3f34650c15d764810335024654b0fcc3Owen Lin                } catch (CameraHardwareException e) {
1002f6ef7b960f610d557f0bab924b9f7928b46f6f0aOwen Lin                    // In eng build, we throw the exception so that test tool
1003f6ef7b960f610d557f0bab924b9f7928b46f6f0aOwen Lin                    // can detect it and report it
1004f6ef7b960f610d557f0bab924b9f7928b46f6f0aOwen Lin                    if ("eng".equals(Build.TYPE)) {
1005f6ef7b960f610d557f0bab924b9f7928b46f6f0aOwen Lin                        throw new RuntimeException(e);
1006f6ef7b960f610d557f0bab924b9f7928b46f6f0aOwen Lin                    }
10073f3c857e3f34650c15d764810335024654b0fcc3Owen Lin                    mStartPreviewFail = true;
10083f3c857e3f34650c15d764810335024654b0fcc3Owen Lin                }
100911c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks            }
101011c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks        });
101148f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        startPreviewThread.start();
1012b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1013b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        // don't set mSurfaceHolder here. We have it set ONLY within
10146cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li        // surfaceChanged / surfaceDestroyed, other parts of the code
1015b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        // assume that when it is set, the surface is also set.
1016b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        SurfaceHolder holder = mSurfaceView.getHolder();
1017b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        holder.addCallback(this);
1018b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
1019b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1020cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        if (mIsImageCaptureIntent) {
1021cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang            setupCaptureParams();
1022cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang
102385337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan            View reviewControl = findViewById(R.id.review_control);
102485337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan            reviewControl.setVisibility(View.VISIBLE);
102585337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan            reviewControl.findViewById(R.id.btn_cancel).setOnClickListener(this);
102685337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan            reviewControl.findViewById(R.id.btn_retake).setOnClickListener(this);
102785337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan            reviewControl.findViewById(R.id.btn_done).setOnClickListener(this);
102861b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin        } else {
1029eb1f0c8299d96bad9bb449ff9e13eff4b43272ccHung-ying Tyan            mSwitcher = (SwitcherSet) findViewById(R.id.camera_switch);
103085337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan            mSwitcher.setVisibility(View.VISIBLE);
10312e768c1b835a127f190395a91477144d8ae850acOwen Lin            mSwitcher.setOnSwitchListener(this);
103284f96469f8c54fcdc61b6e854cd478abc15c4f52Wu-cheng Li        }
103385337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan        mIndicatorWheel = findViewById(R.id.indicator_wheel);
103437acf79b1cc060f355c9cd1803c2bdd5e39deb27Owen Lin
103548f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        // Make sure preview is started.
1036b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        try {
103748f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li            startPreviewThread.join();
1038f6ef7b960f610d557f0bab924b9f7928b46f6f0aOwen Lin            if (mStartPreviewFail) {
1039f6ef7b960f610d557f0bab924b9f7928b46f6f0aOwen Lin                showCameraErrorAndFinish();
1040f6ef7b960f610d557f0bab924b9f7928b46f6f0aOwen Lin                return;
1041f6ef7b960f610d557f0bab924b9f7928b46f6f0aOwen Lin            }
1042b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        } catch (InterruptedException ex) {
10430e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            // ignore
1044b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
10458974ca6b9412446d05f580c8d694d7e61eacb5ceWu-cheng Li
1046003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li        mBackCameraId = CameraHolder.instance().getBackCameraId();
1047003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li        mFrontCameraId = CameraHolder.instance().getFrontCameraId();
1048003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li
10491f09dac3d664734d099765783dae1233ef023226Wu-cheng Li        // Do this after starting preview because it depends on camera
10501f09dac3d664734d099765783dae1233ef023226Wu-cheng Li        // parameters.
10518974ca6b9412446d05f580c8d694d7e61eacb5ceWu-cheng Li        initializeControlPanel();
105259ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang        initializeCameraPicker();
10535030bd7b700869feb50083571a680fa55730229bHung-ying Tyan
10545030bd7b700869feb50083571a680fa55730229bHung-ying Tyan        if (mControlPanel != null) {
10555030bd7b700869feb50083571a680fa55730229bHung-ying Tyan            mZoomPicker = (ZoomPicker) mControlPanel.findViewById(R.id.zoom_picker);
10565030bd7b700869feb50083571a680fa55730229bHung-ying Tyan            mZoomPicker.setEnabled(true); // disabled initially in xml
10575030bd7b700869feb50083571a680fa55730229bHung-ying Tyan        }
1058503560f0dc42d6185c61be06f8060dfd50d30d69Owen Lin    }
1059503560f0dc42d6185c61be06f8060dfd50d30d69Owen Lin
1060f00d421845b4ff2eca1354419889886fec5f2538Owen Lin    private void changeHeadUpDisplayState() {
10617f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        if (mHeadUpDisplay == null) return;
1062f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        // If the camera resumes behind the lock screen, the orientation
1063f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        // will be portrait. That causes OOM when we try to allocation GPU
1064f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        // memory for the GLSurfaceView again when the orientation changes. So,
1065f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        // we delayed initialization of HeadUpDisplay until the orientation
1066f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        // becomes landscape.
1067f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        Configuration config = getResources().getConfiguration();
1068f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        if (config.orientation == Configuration.ORIENTATION_LANDSCAPE
10697add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li                && !mPausing && mFirstTimeInitialized) {
1070c8e13fecea56e679418f41ba454f867c49f888aaOwen Lin            if (mGLRootView == null) attachHeadUpDisplay();
1071f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        } else if (mGLRootView != null) {
1072c8e13fecea56e679418f41ba454f867c49f888aaOwen Lin            detachHeadUpDisplay();
1073f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        }
1074f00d421845b4ff2eca1354419889886fec5f2538Owen Lin    }
1075f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang
1076823d57389b616091827059ca6401f675e416a53bWu-cheng Li    private void overrideCameraSettings(final String flashMode,
107718a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin            final String whiteBalance, final String focusMode) {
1078823d57389b616091827059ca6401f675e416a53bWu-cheng Li        if (mHeadUpDisplay != null) {
1079823d57389b616091827059ca6401f675e416a53bWu-cheng Li            mHeadUpDisplay.overrideSettings(
1080823d57389b616091827059ca6401f675e416a53bWu-cheng Li                    CameraSettings.KEY_FLASH_MODE, flashMode,
1081823d57389b616091827059ca6401f675e416a53bWu-cheng Li                    CameraSettings.KEY_WHITE_BALANCE, whiteBalance,
1082823d57389b616091827059ca6401f675e416a53bWu-cheng Li                    CameraSettings.KEY_FOCUS_MODE, focusMode);
1083823d57389b616091827059ca6401f675e416a53bWu-cheng Li        }
1084823d57389b616091827059ca6401f675e416a53bWu-cheng Li        if (mControlPanel != null) {
1085823d57389b616091827059ca6401f675e416a53bWu-cheng Li            mControlPanel.overrideSettings(
1086823d57389b616091827059ca6401f675e416a53bWu-cheng Li                    CameraSettings.KEY_FLASH_MODE, flashMode,
1087823d57389b616091827059ca6401f675e416a53bWu-cheng Li                    CameraSettings.KEY_WHITE_BALANCE, whiteBalance,
1088823d57389b616091827059ca6401f675e416a53bWu-cheng Li                    CameraSettings.KEY_FOCUS_MODE, focusMode);
1089823d57389b616091827059ca6401f675e416a53bWu-cheng Li        }
109018a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin    }
109118a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin
1092823d57389b616091827059ca6401f675e416a53bWu-cheng Li    private void updateSceneModeUI() {
109318a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin        // If scene mode is set, we cannot set flash mode, white balance, and
10940d73bccf89870682007dd32d4a84bcdb812e2cc7Owen Lin        // focus mode, instead, we read it from driver
109518a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin        if (!Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
1096823d57389b616091827059ca6401f675e416a53bWu-cheng Li            overrideCameraSettings(mParameters.getFlashMode(),
109718a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin                    mParameters.getWhiteBalance(), mParameters.getFocusMode());
109818a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin        } else {
1099823d57389b616091827059ca6401f675e416a53bWu-cheng Li            overrideCameraSettings(null, null, null);
110018a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin        }
110118a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin    }
1102f00d421845b4ff2eca1354419889886fec5f2538Owen Lin
110359ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang    private void loadCameraPreferences() {
110459ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang        CameraSettings settings = new CameraSettings(this, mInitialParams,
110559ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang                mCameraId, CameraHolder.instance().getCameraInfo());
1106003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li        mPreferenceGroup = settings.getPreferenceGroup(R.xml.camera_preferences);
110759ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang    }
110859ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang
1109f5bffc476b8293b30bb500bed2ab7914bb4ca411Wu-cheng Li    private void initializeControlPanel() {
1110067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan        mControlPanel = (ControlPanel) findViewById(R.id.control_panel);
1111067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan        if (mControlPanel == null) return;
1112067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan        loadCameraPreferences();
1113067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan
1114f5bffc476b8293b30bb500bed2ab7914bb4ca411Wu-cheng Li        String[] keys = new String[]{CameraSettings.KEY_FLASH_MODE,
1115f5bffc476b8293b30bb500bed2ab7914bb4ca411Wu-cheng Li            CameraSettings.KEY_WHITE_BALANCE,
1116e747919853dfcbe29969647d7bf83269056d77bcWu-cheng Li            CameraSettings.KEY_COLOR_EFFECT,
1117e747919853dfcbe29969647d7bf83269056d77bcWu-cheng Li            CameraSettings.KEY_SCENE_MODE};
1118067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan        mControlPanel.initialize(this, mPreferenceGroup, keys, true);
1119067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan        mControlPanel.setListener(new MyControlPanelListener());
1120067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan        mPopupGestureDetector = new GestureDetector(this,
1121067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan                new PopupGestureListener());
1122067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan        updateSceneModeUI();
11236f2283fbf049ad1f9870a86474af295604d95600Wu-cheng Li    }
11246f2283fbf049ad1f9870a86474af295604d95600Wu-cheng Li
1125f00d421845b4ff2eca1354419889886fec5f2538Owen Lin    private void initializeHeadUpDisplay() {
11267f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        if (mHeadUpDisplay == null) return;
1127067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan        loadCameraPreferences();
1128067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan
1129c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li        // If we have zoom picker, do not show zoom control on head-up display.
1130c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li        float[] zoomRatios = null;
1131c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li        if (mZoomPicker == null) zoomRatios = getZoomRatios();
113259ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang        mHeadUpDisplay.initialize(this, mPreferenceGroup,
1133c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li                zoomRatios, mOrientationCompensation);
1134c5019f4d58412da6227b3f2e6461bba5b7c671a3Wu-cheng Li        if (mZoomPicker == null && mParameters.isZoomSupported()) {
11350d73bccf89870682007dd32d4a84bcdb812e2cc7Owen Lin            mHeadUpDisplay.setZoomListener(new ZoomControllerListener() {
1136f00d421845b4ff2eca1354419889886fec5f2538Owen Lin                public void onZoomChanged(
11370d73bccf89870682007dd32d4a84bcdb812e2cc7Owen Lin                        int index, float ratio, boolean isMoving) {
11380d73bccf89870682007dd32d4a84bcdb812e2cc7Owen Lin                    onZoomValueChanged(index);
1139f00d421845b4ff2eca1354419889886fec5f2538Owen Lin                }
1140f00d421845b4ff2eca1354419889886fec5f2538Owen Lin            });
1141f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        }
1142823d57389b616091827059ca6401f675e416a53bWu-cheng Li        updateSceneModeUI();
11437add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li    }
11447add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li
11457add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li    private void attachHeadUpDisplay() {
11464aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li        mHeadUpDisplay.setOrientation(mOrientationCompensation);
11477f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        if (mParameters.isZoomSupported()) {
1148c6e3c7823339031096af937464cb528f53e045cfWu-cheng Li            mHeadUpDisplay.setZoomIndex(mZoomValue);
1149c6e3c7823339031096af937464cb528f53e045cfWu-cheng Li        }
1150ec689e833e5730517d190a136710186a8f0c1445Hung-ying Tyan        ViewGroup frame = (ViewGroup) findViewById(R.id.frame);
1151c8e13fecea56e679418f41ba454f867c49f888aaOwen Lin        mGLRootView = new GLRootView(this);
1152c8e13fecea56e679418f41ba454f867c49f888aaOwen Lin        mGLRootView.setContentPane(mHeadUpDisplay);
1153c8e13fecea56e679418f41ba454f867c49f888aaOwen Lin        frame.addView(mGLRootView);
1154f00d421845b4ff2eca1354419889886fec5f2538Owen Lin    }
1155f00d421845b4ff2eca1354419889886fec5f2538Owen Lin
1156c8e13fecea56e679418f41ba454f867c49f888aaOwen Lin    private void detachHeadUpDisplay() {
1157f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        mHeadUpDisplay.setGpsHasSignal(false);
1158f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        mHeadUpDisplay.collapse();
1159f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        ((ViewGroup) mGLRootView.getParent()).removeView(mGLRootView);
1160f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        mGLRootView = null;
1161f00d421845b4ff2eca1354419889886fec5f2538Owen Lin    }
1162f00d421845b4ff2eca1354419889886fec5f2538Owen Lin
11637f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li    private boolean collapseCameraControls() {
11647f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        if (mHeadUpDisplay != null && mHeadUpDisplay.collapse()) {
11657f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li            return true;
11667f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        }
116702ec7c0880ded5863d73d6f173ef35ab5c888dcdWu-cheng Li        if (mControlPanel != null && mControlPanel.dismissSettingPopup()) {
11687f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li            return true;
11697f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        }
11707f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        return false;
11717f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li    }
11727f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li
11737f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li    private void enableCameraControls(boolean enable) {
11747f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        if (mHeadUpDisplay != null) mHeadUpDisplay.setEnabled(enable);
11757f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        if (mControlPanel != null) mControlPanel.setEnabled(enable);
1176e50d2553aeb9e55504697e2a72759e8f46ee2992Hung-ying Tyan        if (mCameraPicker != null) mCameraPicker.setEnabled(enable);
1177e50d2553aeb9e55504697e2a72759e8f46ee2992Hung-ying Tyan        if (mZoomPicker != null) mZoomPicker.setEnabled(enable);
1178e50d2553aeb9e55504697e2a72759e8f46ee2992Hung-ying Tyan        if (mSwitcher != null) mSwitcher.setEnabled(enable);
11797f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li    }
11807f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li
11814aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li    public static int roundOrientation(int orientation) {
11824aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li        return ((orientation + 45) / 90 * 90) % 360;
11834aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li    }
11844aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li
11854aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li    private class MyOrientationEventListener
11864aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            extends OrientationEventListener {
11874aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li        public MyOrientationEventListener(Context context) {
11884aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            super(context);
11894aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li        }
11904aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li
11914aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li        @Override
11924aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li        public void onOrientationChanged(int orientation) {
11934aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            // We keep the last known orientation. So if the user first orient
11944aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            // the camera then point the camera to floor or sky, we still have
11954aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            // the correct orientation.
11964aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            if (orientation == ORIENTATION_UNKNOWN) return;
119743b6525b4aedc5e177163cab7b6f26698a19097fWu-cheng Li            mOrientation = roundOrientation(orientation);
119843b6525b4aedc5e177163cab7b6f26698a19097fWu-cheng Li            // When the screen is unlocked, display rotation may change. Always
119943b6525b4aedc5e177163cab7b6f26698a19097fWu-cheng Li            // calculate the up-to-date orientationCompensation.
120043b6525b4aedc5e177163cab7b6f26698a19097fWu-cheng Li            int orientationCompensation = mOrientation
120143b6525b4aedc5e177163cab7b6f26698a19097fWu-cheng Li                    + Util.getDisplayRotation(Camera.this);
120243b6525b4aedc5e177163cab7b6f26698a19097fWu-cheng Li            if (mOrientationCompensation != orientationCompensation) {
120343b6525b4aedc5e177163cab7b6f26698a19097fWu-cheng Li                mOrientationCompensation = orientationCompensation;
12044aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li                if (!mIsImageCaptureIntent) {
12054aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li                    setOrientationIndicator(mOrientationCompensation);
12064aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li                }
12077f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li                if (mHeadUpDisplay != null) {
12087f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li                    mHeadUpDisplay.setOrientation(mOrientationCompensation);
12097f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li                }
12104aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li            }
12114aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li        }
12124aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li    }
12134aaa2130af7241f128ee561cafd17a4fb560b92cWu-cheng Li
1214ff58fb6921ccb7398ad30c8cec604c38c76be4fcOwen Lin    private void setOrientationIndicator(int degree) {
1215eb1f0c8299d96bad9bb449ff9e13eff4b43272ccHung-ying Tyan        RotateImageView icon = (RotateImageView) findViewById(
12169f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                R.id.review_thumbnail);
1217eb1f0c8299d96bad9bb449ff9e13eff4b43272ccHung-ying Tyan        if (icon != null) icon.setDegree(degree);
12189f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li
1219eb1f0c8299d96bad9bb449ff9e13eff4b43272ccHung-ying Tyan        icon = (RotateImageView) findViewById(R.id.camera_switch_icon);
1220eb1f0c8299d96bad9bb449ff9e13eff4b43272ccHung-ying Tyan        if (icon != null) icon.setDegree(degree);
1221eb1f0c8299d96bad9bb449ff9e13eff4b43272ccHung-ying Tyan        icon = (RotateImageView) findViewById(R.id.video_switch_icon);
1222eb1f0c8299d96bad9bb449ff9e13eff4b43272ccHung-ying Tyan        if (icon != null) icon.setDegree(degree);
1223ff58fb6921ccb7398ad30c8cec604c38c76be4fcOwen Lin    }
1224ff58fb6921ccb7398ad30c8cec604c38c76be4fcOwen Lin
1225b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    @Override
1226b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public void onStart() {
1227b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        super.onStart();
12282e768c1b835a127f190395a91477144d8ae850acOwen Lin        if (!mIsImageCaptureIntent) {
12292e768c1b835a127f190395a91477144d8ae850acOwen Lin            mSwitcher.setSwitch(SWITCH_CAMERA);
12302e768c1b835a127f190395a91477144d8ae850acOwen Lin        }
1231b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1232b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
12336d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen    @Override
12346d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen    public void onStop() {
12356d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen        super.onStop();
12366d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen        if (mMediaProviderClient != null) {
12376d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen            mMediaProviderClient.release();
12386d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen            mMediaProviderClient = null;
12396d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen        }
12406d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen    }
12416d68a36b5da24f70e1a8d5e9529720eeb8ce6d86Ray Chen
1242d12ae2098b491bb55798758da8b698ffc625d62cWu-cheng Li    private void checkStorage() {
1243721d2d2a963799fad4bda9bb4b278c24fc469303Chia-chi Yeh        mPicturesRemaining = Storage.getAvailableSpace();
1244721d2d2a963799fad4bda9bb4b278c24fc469303Chia-chi Yeh        if (mPicturesRemaining > 0) {
1245721d2d2a963799fad4bda9bb4b278c24fc469303Chia-chi Yeh            mPicturesRemaining /= 1500000;
1246721d2d2a963799fad4bda9bb4b278c24fc469303Chia-chi Yeh        }
1247c8a7fc07e3b905a723e753b0c9144de2b0343f1fChia-chi Yeh        updateStorageHint();
12481e50968c0a9eba523efcfdda86e298e33e95687dWu-cheng Li    }
1249d12ae2098b491bb55798758da8b698ffc625d62cWu-cheng Li
1250b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public void onClick(View v) {
1251b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        switch (v.getId()) {
125261b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin            case R.id.btn_retake:
125361b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin                hidePostCaptureAlert();
125461b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin                restartPreview();
125561b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin                break;
1256dad4b187eec436a2716aca5af31f23ce5de95f39Owen Lin            case R.id.review_thumbnail:
12575fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li                if (isCameraIdle()) {
12589f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                    viewImage(mThumbnailButton);
1259059daa36dc6e83ca24a16d9b99925e71010bccebOwen Lin                }
1260059daa36dc6e83ca24a16d9b99925e71010bccebOwen Lin                break;
126161b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin            case R.id.btn_done:
1262059daa36dc6e83ca24a16d9b99925e71010bccebOwen Lin                doAttach();
1263059daa36dc6e83ca24a16d9b99925e71010bccebOwen Lin                break;
126461b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin            case R.id.btn_cancel:
1265059daa36dc6e83ca24a16d9b99925e71010bccebOwen Lin                doCancel();
1266dfee7f813adb8de209472d24553b860d2515ea0aWu-cheng Li                break;
1267de751a6a61b0e2806df4b2c5902aaadc7bb1aeb4Wu-cheng Li            case R.id.btn_gallery:
1268de751a6a61b0e2806df4b2c5902aaadc7bb1aeb4Wu-cheng Li                gotoGallery();
1269de751a6a61b0e2806df4b2c5902aaadc7bb1aeb4Wu-cheng Li                break;
1270b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1271b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1272b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1273c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang    private Bitmap createCaptureBitmap(byte[] data) {
1274c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        // This is really stupid...we just want to read the orientation in
1275c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        // the jpeg header.
1276c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        String filepath = ImageManager.getTempJpegPath();
1277c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        int degree = 0;
1278c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        if (saveDataToFile(filepath, data)) {
1279c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang            degree = ImageManager.getExifOrientation(filepath);
1280c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang            new File(filepath).delete();
1281c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        }
1282c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang
1283c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        // Limit to 50k pixels so we can return it in the intent.
1284271b3095b9f763421c0547109da9de774795072dChih-Chung Chang        Bitmap bitmap = Util.makeBitmap(data, 50 * 1024);
1285c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        bitmap = Util.rotate(bitmap, degree);
1286c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        return bitmap;
1287c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang    }
1288c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang
1289b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void doAttach() {
1290de365d87520c97d6b1636442e6b5c82a3e036f29The Android Open Source Project        if (mPausing) {
1291e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project            return;
1292e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project        }
1293c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang
1294c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        byte[] data = mImageCapture.getLastCaptureData();
1295b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1296cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        if (mCropValue == null) {
12970e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            // First handle the no crop case -- just return the value.  If the
12980e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            // caller specifies a "save uri" then write the data to it's
12990e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            // stream. Otherwise, pass back a scaled down version of the bitmap
13000e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            // directly in the extras.
1301cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang            if (mSaveUri != null) {
1302b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                OutputStream outputStream = null;
1303b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                try {
1304cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang                    outputStream = mContentResolver.openOutputStream(mSaveUri);
1305c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang                    outputStream.write(data);
1306b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    outputStream.close();
1307b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1308b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    setResult(RESULT_OK);
1309b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    finish();
1310b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                } catch (IOException ex) {
13110e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                    // ignore exception
1312b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                } finally {
1313c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang                    Util.closeSilently(outputStream);
1314b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                }
1315b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } else {
1316c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang                Bitmap bitmap = createCaptureBitmap(data);
13170e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                setResult(RESULT_OK,
13180e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                        new Intent("inline-data").putExtra("data", bitmap));
1319b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                finish();
1320b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
13210e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin        } else {
13220e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            // Save the image to a temp file and invoke the cropper
1323b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            Uri tempUri = null;
1324b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            FileOutputStream tempStream = null;
1325b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            try {
1326b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                File path = getFileStreamPath(sTempCropFilename);
1327b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                path.delete();
1328b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                tempStream = openFileOutput(sTempCropFilename, 0);
1329c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang                tempStream.write(data);
1330b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                tempStream.close();
1331b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                tempUri = Uri.fromFile(path);
1332b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } catch (FileNotFoundException ex) {
1333b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                setResult(Activity.RESULT_CANCELED);
1334b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                finish();
1335b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                return;
1336b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } catch (IOException ex) {
1337b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                setResult(Activity.RESULT_CANCELED);
1338b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                finish();
1339b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                return;
1340b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } finally {
1341c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang                Util.closeSilently(tempStream);
1342b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
1343b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1344b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            Bundle newExtras = new Bundle();
1345cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang            if (mCropValue.equals("circle")) {
1346b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                newExtras.putString("circleCrop", "true");
13470e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            }
1348cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang            if (mSaveUri != null) {
1349cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang                newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
13500e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            } else {
1351b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                newExtras.putBoolean("return-data", true);
13520e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            }
1353b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1354da4608768624b1fb492f6676c510b37ad5f294baCheng-Ru Lin            Intent cropIntent = new Intent("com.android.camera.action.CROP");
1355da4608768624b1fb492f6676c510b37ad5f294baCheng-Ru Lin
1356b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            cropIntent.setData(tempUri);
1357b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            cropIntent.putExtras(newExtras);
1358b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1359b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            startActivityForResult(cropIntent, CROP_MSG);
1360b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1361b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1362b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1363b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void doCancel() {
1364b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        setResult(RESULT_CANCELED, new Intent());
1365b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        finish();
1366b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1367b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1368b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public void onShutterButtonFocus(ShutterButton button, boolean pressed) {
136911c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks        if (mPausing) {
1370e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project            return;
1371e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project        }
1372b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        switch (button.getId()) {
1373dad4b187eec436a2716aca5af31f23ce5de95f39Owen Lin            case R.id.shutter_button:
13742bb9e74cc84b60a349ec90b34e39973b019ec0bdrepo sync                doFocus(pressed);
1375b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                break;
1376b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1377b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1378b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1379b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public void onShutterButtonClick(ShutterButton button) {
138011c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks        if (mPausing) {
1381e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project            return;
1382e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project        }
1383b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        switch (button.getId()) {
1384dad4b187eec436a2716aca5af31f23ce5de95f39Owen Lin            case R.id.shutter_button:
13852bb9e74cc84b60a349ec90b34e39973b019ec0bdrepo sync                doSnap();
1386b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                break;
1387b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1388b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1389b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1390b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private OnScreenHint mStorageHint;
1391b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1392c8a7fc07e3b905a723e753b0c9144de2b0343f1fChia-chi Yeh    private void updateStorageHint() {
1393b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        String noStorageText = null;
1394b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1395c8a7fc07e3b905a723e753b0c9144de2b0343f1fChia-chi Yeh        if (mPicturesRemaining == Storage.UNAVAILABLE) {
1396c8a7fc07e3b905a723e753b0c9144de2b0343f1fChia-chi Yeh            noStorageText = getString(R.string.no_storage);
1397c8a7fc07e3b905a723e753b0c9144de2b0343f1fChia-chi Yeh        } else if (mPicturesRemaining == Storage.PREPARING) {
1398c8a7fc07e3b905a723e753b0c9144de2b0343f1fChia-chi Yeh            noStorageText = getString(R.string.preparing_sd);
1399c8a7fc07e3b905a723e753b0c9144de2b0343f1fChia-chi Yeh        } else if (mPicturesRemaining == Storage.UNKNOWN_SIZE) {
14000ab73a87e749477080c566cb9612cd3f25dc7574Wu-cheng Li            noStorageText = getString(R.string.access_sd_fail);
1401c8a7fc07e3b905a723e753b0c9144de2b0343f1fChia-chi Yeh        } else if (mPicturesRemaining < 1L) {
1402b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            noStorageText = getString(R.string.not_enough_space);
1403b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1404b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1405b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        if (noStorageText != null) {
1406b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            if (mStorageHint == null) {
1407b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                mStorageHint = OnScreenHint.makeText(this, noStorageText);
1408b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } else {
1409b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                mStorageHint.setText(noStorageText);
1410b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
1411b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mStorageHint.show();
1412b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        } else if (mStorageHint != null) {
1413b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mStorageHint.cancel();
1414b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mStorageHint = null;
1415b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1416b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1417b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1418522e836abc90bcb2d4e7a4ac37e269154d06c715Chih-Chung Chang    private void installIntentFilter() {
1419b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        // install an intent filter to receive SD card related events.
14200e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin        IntentFilter intentFilter =
14210e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
1422b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
1423b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
1424b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        intentFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
1425b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        intentFilter.addDataScheme("file");
1426b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        registerReceiver(mReceiver, intentFilter);
1427b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        mDidRegister = true;
1428e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li    }
1429b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1430522e836abc90bcb2d4e7a4ac37e269154d06c715Chih-Chung Chang    private void initializeFocusTone() {
1431e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        // Initialize focus tone generator.
1432b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        try {
14330e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            mFocusToneGenerator = new ToneGenerator(
14340e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                    AudioManager.STREAM_SYSTEM, FOCUS_BEEP_VOLUME);
1435fa1dfa2f3f1385e98cf8bdac5c48f8d13a400a8aWu-cheng Li        } catch (Throwable ex) {
1436fa1dfa2f3f1385e98cf8bdac5c48f8d13a400a8aWu-cheng Li            Log.w(TAG, "Exception caught while creating tone generator: ", ex);
1437b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mFocusToneGenerator = null;
1438b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1439b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1440b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1441803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang    private void initializeScreenBrightness() {
1442803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang        Window win = getWindow();
1443803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang        // Overright the brightness settings if it is automatic
1444803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang        int mode = Settings.System.getInt(
1445803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang                getContentResolver(),
1446803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang                Settings.System.SCREEN_BRIGHTNESS_MODE,
1447803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
1448803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang        if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
1449803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang            WindowManager.LayoutParams winParams = win.getAttributes();
1450803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang            winParams.screenBrightness = DEFAULT_CAMERA_BRIGHTNESS;
1451803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang            win.setAttributes(winParams);
1452803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang        }
1453803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang    }
1454803afcc7dae35d6a02e8882fc5fcbcdcca4eaad4Chih-Chung Chang
1455e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li    @Override
14564de149ceb47f2c251f646419907424bfb67d2b64Owen Lin    protected void onResume() {
1457e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        super.onResume();
1458281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin
1459e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        mPausing = false;
1460e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        mJpegPictureCallbackTime = 0;
1461316af30d026dd182edb482ef6de7a330d6b1b012Wu-cheng Li        mZoomValue = 0;
1462e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        mImageCapture = new ImageCapture();
1463e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
146448f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        // Start the preview if it is not started.
14653f3c857e3f34650c15d764810335024654b0fcc3Owen Lin        if (!mPreviewing && !mStartPreviewFail) {
1466f86e6af0b8833f5f8a4b03fefba54b1922280d28Chih-Chung Chang            resetExposureCompensation();
1467068a2e745b284b33af161d629adb9b7839be32d1Wu-cheng Li            if (!restartPreview()) return;
146848f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        }
1469e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
147048f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        if (mSurfaceHolder != null) {
14716cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li            // If first time initialization is not finished, put it in the
14726cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li            // message queue.
14736cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li            if (!mFirstTimeInitialized) {
14746cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li                mHandler.sendEmptyMessage(FIRST_TIME_INIT);
14756cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li            } else {
14766cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li                initializeSecondTime();
14776cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li            }
1478e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        }
1479cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin        keepScreenOnAwhile();
1480e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li    }
1481e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
1482f00d421845b4ff2eca1354419889886fec5f2538Owen Lin    @Override
1483f00d421845b4ff2eca1354419889886fec5f2538Owen Lin    public void onConfigurationChanged(Configuration config) {
1484f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        super.onConfigurationChanged(config);
1485f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        changeHeadUpDisplayState();
1486f00d421845b4ff2eca1354419889886fec5f2538Owen Lin    }
1487f00d421845b4ff2eca1354419889886fec5f2538Owen Lin
14889bc8d1b47e5bab978cc9571ddfdedc8309a0b65dChih-Chung Chang    private static ImageManager.DataLocation dataLocation() {
1489b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        return ImageManager.DataLocation.EXTERNAL;
1490b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1491b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1492b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    @Override
1493b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    protected void onPause() {
1494b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        mPausing = true;
1495b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        stopPreview();
14964904e9713a0118af6321dfe0e77a388fe8c3f45dWu-cheng Li        // Close the camera now because other activities may need to use it.
14974904e9713a0118af6321dfe0e77a388fe8c3f45dWu-cheng Li        closeCamera();
1498cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin        resetScreenOn();
14997f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        collapseCameraControls();
1500f00d421845b4ff2eca1354419889886fec5f2538Owen Lin        changeHeadUpDisplayState();
1501b2b23846d89c63ba0d28bbc7627022e6b9fa620bOwen Lin
1502e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        if (mFirstTimeInitialized) {
1503e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li            mOrientationListener.disable();
1504e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li            if (!mIsImageCaptureIntent) {
15059f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                if (mThumbnailButton != null) {
15069f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                    mThumbnailButton.storeData(
15079f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                            ImageManager.getLastImageThumbPath());
15089f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li                }
1509e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li            }
1510e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li            hidePostCaptureAlert();
1511e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        }
1512e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
1513b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        if (mDidRegister) {
1514b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            unregisterReceiver(mReceiver);
1515b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mDidRegister = false;
1516b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1517b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        stopReceivingLocationUpdates();
1518b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1519b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        if (mFocusToneGenerator != null) {
1520b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mFocusToneGenerator.release();
1521b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mFocusToneGenerator = null;
1522b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1523b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1524b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        if (mStorageHint != null) {
1525b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mStorageHint.cancel();
1526b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mStorageHint = null;
1527b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1528de365d87520c97d6b1636442e6b5c82a3e036f29The Android Open Source Project
15299a379bd668227d3dcea30312025f304fd248f6e3The Android Open Source Project        // If we are in an image capture intent and has taken
15309a379bd668227d3dcea30312025f304fd248f6e3The Android Open Source Project        // a picture, we just clear it in onPause.
1531c30e5c21c3858c15473427d82608c473d44bf3e4Chih-Chung Chang        mImageCapture.clearLastData();
15329a379bd668227d3dcea30312025f304fd248f6e3The Android Open Source Project        mImageCapture = null;
1533e3f4516c2154539cb5778ef061abf8a0ccf90a5eThe Android Open Source Project
1534d16a06161fe9e10ae8f656bca4d4482b235cd172Wu-cheng Li        // Remove the messages in the event queue.
1535d16a06161fe9e10ae8f656bca4d4482b235cd172Wu-cheng Li        mHandler.removeMessages(RESTART_PREVIEW);
15366cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li        mHandler.removeMessages(FIRST_TIME_INIT);
1537d16a06161fe9e10ae8f656bca4d4482b235cd172Wu-cheng Li
1538b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        super.onPause();
1539b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1540b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1541b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    @Override
15420e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin    protected void onActivityResult(
15430e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            int requestCode, int resultCode, Intent data) {
1544b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        switch (requestCode) {
1545b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            case CROP_MSG: {
1546b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                Intent intent = new Intent();
1547b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                if (data != null) {
1548b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    Bundle extras = data.getExtras();
1549b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    if (extras != null) {
1550b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        intent.putExtras(extras);
1551b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    }
1552b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                }
1553b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                setResult(resultCode, intent);
1554b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                finish();
1555b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1556b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                File path = getFileStreamPath(sTempCropFilename);
1557b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                path.delete();
1558b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1559b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                break;
1560b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
1561b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1562b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1563b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1564fcb65124480c7a07d8cb1b28eff3c6acd6107531repo syn && make -j    private boolean canTakePicture() {
1565fcb65124480c7a07d8cb1b28eff3c6acd6107531repo syn && make -j        return isCameraIdle() && mPreviewing && (mPicturesRemaining > 0);
1566fcb65124480c7a07d8cb1b28eff3c6acd6107531repo syn && make -j    }
1567fcb65124480c7a07d8cb1b28eff3c6acd6107531repo syn && make -j
1568b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void autoFocus() {
156905309120f233e2f3a2d6319d0efd996254e86c16Wu-cheng Li        // Initiate autofocus only when preview is started and snapshot is not
157005309120f233e2f3a2d6319d0efd996254e86c16Wu-cheng Li        // in progress.
1571fcb65124480c7a07d8cb1b28eff3c6acd6107531repo syn && make -j        if (canTakePicture()) {
15727f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li            enableCameraControls(false);
15735fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li            Log.v(TAG, "Start autofocus.");
15745fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li            mFocusStartTime = System.currentTimeMillis();
15755fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li            mFocusState = FOCUSING;
15765fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li            updateFocusIndicator();
15775fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li            mCameraDevice.autoFocus(mAutoFocusCallback);
1578b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1579b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1580b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
158162a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang    private void cancelAutoFocus() {
158262a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang        // User releases half-pressed focus key.
15838e5e83e6c5de9186e25bca70f6efc918dd2c5050Wu-cheng Li        if (mStatus != SNAPSHOT_IN_PROGRESS && (mFocusState == FOCUSING
15848e5e83e6c5de9186e25bca70f6efc918dd2c5050Wu-cheng Li                || mFocusState == FOCUS_SUCCESS || mFocusState == FOCUS_FAIL)) {
158562a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang            Log.v(TAG, "Cancel autofocus.");
15867f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li            enableCameraControls(true);
158762a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang            mCameraDevice.cancelAutoFocus();
158862a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang        }
158962a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang        if (mFocusState != FOCUSING_SNAP_ON_FINISH) {
159062a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang            clearFocusState();
159162a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang        }
159262a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang    }
159362a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang
15949c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project    private void clearFocusState() {
15959c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project        mFocusState = FOCUS_NOT_STARTED;
15965e5aa7e1e8bd03e9919d5f14b9f5df75cf68ff8fChih-Chung Chang        updateFocusIndicator();
1597b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1598b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1599b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void updateFocusIndicator() {
1600059daa36dc6e83ca24a16d9b99925e71010bccebOwen Lin        if (mFocusRectangle == null) return;
1601e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li
160275ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang        if (mFocusState == FOCUSING || mFocusState == FOCUSING_SNAP_ON_FINISH) {
160375ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang            mFocusRectangle.showStart();
160475ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang        } else if (mFocusState == FOCUS_SUCCESS) {
160575ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang            mFocusRectangle.showSuccess();
160675ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang        } else if (mFocusState == FOCUS_FAIL) {
160775ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang            mFocusRectangle.showFail();
160875ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang        } else {
160975ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang            mFocusRectangle.clear();
161075ae09f4ee606960fb1740e3ad4b5b46931ef520Chih-Chung Chang        }
1611b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1612b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1613b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    @Override
16140b8028e923b386eaa9062e076ca041914acbcd09Chih-Chung Chang    public void onBackPressed() {
16150b8028e923b386eaa9062e076ca041914acbcd09Chih-Chung Chang        if (!isCameraIdle()) {
16160b8028e923b386eaa9062e076ca041914acbcd09Chih-Chung Chang            // ignore backs while we're taking a picture
16170b8028e923b386eaa9062e076ca041914acbcd09Chih-Chung Chang            return;
16187f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        } else if (!collapseCameraControls()) {
16193eed8999c9fd92600c74225b671a2f03d7dd51d4Owen Lin            super.onBackPressed();
16200b8028e923b386eaa9062e076ca041914acbcd09Chih-Chung Chang        }
16210b8028e923b386eaa9062e076ca041914acbcd09Chih-Chung Chang    }
16220b8028e923b386eaa9062e076ca041914acbcd09Chih-Chung Chang
16230b8028e923b386eaa9062e076ca041914acbcd09Chih-Chung Chang    @Override
1624b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public boolean onKeyDown(int keyCode, KeyEvent event) {
1625b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        switch (keyCode) {
1626b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            case KeyEvent.KEYCODE_FOCUS:
1627e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
162811c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks                    doFocus(true);
1629b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                }
1630b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                return true;
1631b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            case KeyEvent.KEYCODE_CAMERA:
1632e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
163311c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks                    doSnap();
1634b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                }
1635b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                return true;
1636b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            case KeyEvent.KEYCODE_DPAD_CENTER:
16370e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                // If we get a dpad center event without any focused view, move
16380e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                // the focus to the shutter button and press it.
1639e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                if (mFirstTimeInitialized && event.getRepeatCount() == 0) {
16400e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                    // Start auto-focus immediately to reduce shutter lag. After
16410e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                    // the shutter button gets the focus, doFocus() will be
16420e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin                    // called again but it is fine.
16437f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li                    if (collapseCameraControls()) return true;
164411c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks                    doFocus(true);
164511c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks                    if (mShutterButton.isInTouchMode()) {
164611c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks                        mShutterButton.requestFocusFromTouch();
164711c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks                    } else {
164811c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks                        mShutterButton.requestFocus();
1649b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    }
165011c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks                    mShutterButton.setPressed(true);
1651b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                }
1652b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                return true;
1653b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1654b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1655b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        return super.onKeyDown(keyCode, event);
1656b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1657b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1658b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    @Override
1659b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public boolean onKeyUp(int keyCode, KeyEvent event) {
1660b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        switch (keyCode) {
1661b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            case KeyEvent.KEYCODE_FOCUS:
1662e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                if (mFirstTimeInitialized) {
1663e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                    doFocus(false);
1664e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li                }
1665b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                return true;
1666b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1667b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        return super.onKeyUp(keyCode, event);
1668b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1669b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
16709c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project    private void doSnap() {
16717f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        if (collapseCameraControls()) return;
1672f5876dbd29607a0e9762f88e9db6fa35ffb20067Owen Lin
1673083ccc1e5e06bde2102c50e8279178209fa9a056Wu-cheng Li        Log.v(TAG, "doSnap: mFocusState=" + mFocusState);
16740f56ef5704b7f1a0874528367147487c051876d9Wu-cheng Li        // If the user has half-pressed the shutter and focus is completed, we
16750f56ef5704b7f1a0874528367147487c051876d9Wu-cheng Li        // can take the photo right away. If the focus mode is infinity, we can
16760f56ef5704b7f1a0874528367147487c051876d9Wu-cheng Li        // also take the photo.
1677ed6661e212048dc3e143c48402b3935911c3741cWu-cheng Li        if (mFocusMode.equals(Parameters.FOCUS_MODE_INFINITY)
1678a73ebf03d722d965fc9b394bba2187a66559949fWu-cheng Li                || mFocusMode.equals(Parameters.FOCUS_MODE_FIXED)
1679a73ebf03d722d965fc9b394bba2187a66559949fWu-cheng Li                || mFocusMode.equals(Parameters.FOCUS_MODE_EDOF)
168091acfc99279d5ece7ac9cb2d7a2980eb0d3b50daChih-Chung Chang                || (mFocusState == FOCUS_SUCCESS
168191acfc99279d5ece7ac9cb2d7a2980eb0d3b50daChih-Chung Chang                || mFocusState == FOCUS_FAIL)) {
16825fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li            mImageCapture.onSnap();
16838d0dd0e2ee9a9f8d729ed96597102777cf14701fThe Android Open Source Project        } else if (mFocusState == FOCUSING) {
1684de365d87520c97d6b1636442e6b5c82a3e036f29The Android Open Source Project            // Half pressing the shutter (i.e. the focus button event) will
1685de365d87520c97d6b1636442e6b5c82a3e036f29The Android Open Source Project            // already have requested AF for us, so just request capture on
16869c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project            // focus here.
16879c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project            mFocusState = FOCUSING_SNAP_ON_FINISH;
16888d0dd0e2ee9a9f8d729ed96597102777cf14701fThe Android Open Source Project        } else if (mFocusState == FOCUS_NOT_STARTED) {
16898d0dd0e2ee9a9f8d729ed96597102777cf14701fThe Android Open Source Project            // Focus key down event is dropped for some reasons. Just ignore.
1690b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1691b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1692b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1693b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void doFocus(boolean pressed) {
1694cc529326d9369290abd6933f1d070859b3cadf75Wu-cheng Li        // Do the focus if the mode is not infinity.
16957f0d458b7d2ed913879db247d16abcfdec0dc897Wu-cheng Li        if (collapseCameraControls()) return;
1696a73ebf03d722d965fc9b394bba2187a66559949fWu-cheng Li        if (!(mFocusMode.equals(Parameters.FOCUS_MODE_INFINITY)
1697a73ebf03d722d965fc9b394bba2187a66559949fWu-cheng Li                  || mFocusMode.equals(Parameters.FOCUS_MODE_FIXED)
1698a73ebf03d722d965fc9b394bba2187a66559949fWu-cheng Li                  || mFocusMode.equals(Parameters.FOCUS_MODE_EDOF))) {
16990f56ef5704b7f1a0874528367147487c051876d9Wu-cheng Li            if (pressed) {  // Focus key down.
17005fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li                autoFocus();
17010f56ef5704b7f1a0874528367147487c051876d9Wu-cheng Li            } else {  // Focus key up.
170262a2ae8a96495b77bd1d11420fd352cd0b8ca7c3Chih-Chung Chang                cancelAutoFocus();
17039c9be2eb4a86fc3468dbba1ec5f7bfdb37d22c74The Android Open Source Project            }
1704b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1705b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1706b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1707b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
17084fc134dffebaddec5bcdb26dc1caeb4ab6f7f91cChih-Chung Chang        // Make sure we have a surface in the holder before proceeding.
17094fc134dffebaddec5bcdb26dc1caeb4ab6f7f91cChih-Chung Chang        if (holder.getSurface() == null) {
17104fc134dffebaddec5bcdb26dc1caeb4ab6f7f91cChih-Chung Chang            Log.d(TAG, "holder.getSurface() == null");
17114fc134dffebaddec5bcdb26dc1caeb4ab6f7f91cChih-Chung Chang            return;
17124fc134dffebaddec5bcdb26dc1caeb4ab6f7f91cChih-Chung Chang        }
17134fc134dffebaddec5bcdb26dc1caeb4ab6f7f91cChih-Chung Chang
171447dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin        // We need to save the holder for later use, even when the mCameraDevice
171547dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin        // is null. This could happen if onResume() is invoked after this
171647dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin        // function.
171747dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin        mSurfaceHolder = holder;
171847dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin
1719ba19d8b0011a528b38943458b2f816ed247f2793Wu-cheng Li        // The mCameraDevice will be null if it fails to connect to the camera
1720ba19d8b0011a528b38943458b2f816ed247f2793Wu-cheng Li        // hardware. In this case we will show a dialog and then finish the
1721ba19d8b0011a528b38943458b2f816ed247f2793Wu-cheng Li        // activity, so it's OK to ignore it.
17223f3c857e3f34650c15d764810335024654b0fcc3Owen Lin        if (mCameraDevice == null) return;
17233f3c857e3f34650c15d764810335024654b0fcc3Owen Lin
172447dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin        // Sometimes surfaceChanged is called after onPause or before onResume.
172547dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin        // Ignore it.
17263f3c857e3f34650c15d764810335024654b0fcc3Owen Lin        if (mPausing || isFinishing()) return;
17276cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li
172847dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin        if (mPreviewing && holder.isCreating()) {
172947dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin            // Set preview display if the surface is being created and preview
173047dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin            // was already started. That means preview display was set to null
173147dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin            // and we need to set it now.
173248f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li            setPreviewDisplay(holder);
173347dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin        } else {
173447dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin            // 1. Restart the preview if the size of surface was changed. The
173547dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin            // framework may not support changing preview display on the fly.
173647dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin            // 2. Start the preview now if surface was destroyed and preview
173747dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin            // stopped.
173847dd2cf1dacad3687410ea6e8e4f05542a5fdc46Owen Lin            restartPreview();
173948f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        }
17406cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li
17416cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li        // If first time initialization is not finished, send a message to do
17426cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li        // it later. We want to finish surfaceChanged as soon as possible to let
17436cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li        // user see preview first.
17446cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li        if (!mFirstTimeInitialized) {
1745e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li            mHandler.sendEmptyMessage(FIRST_TIME_INIT);
1746547caa3586633adcaddfbe766ec46a0b97ee8d4crepo sync        } else {
1747547caa3586633adcaddfbe766ec46a0b97ee8d4crepo sync            initializeSecondTime();
1748e43adb123b34432b977f57943c3c42614ccfd985Wu-cheng Li        }
1749b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1750b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1751b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public void surfaceCreated(SurfaceHolder holder) {
1752b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1753b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1754b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public void surfaceDestroyed(SurfaceHolder holder) {
1755b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        stopPreview();
1756b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        mSurfaceHolder = null;
1757b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1758b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1759b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void closeCamera() {
1760b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        if (mCameraDevice != null) {
1761cd65be31531717fb032b7423f8d5a77df465cfcaChih-Chung Chang            CameraHolder.instance().release();
1762e453bdb87ca7eb4e05b817774124731237a3d437Wu-cheng Li            mCameraDevice.setZoomChangeListener(null);
1763b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mCameraDevice = null;
1764b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            mPreviewing = false;
1765b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
1766b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1767b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
17683f3c857e3f34650c15d764810335024654b0fcc3Owen Lin    private void ensureCameraDevice() throws CameraHardwareException {
176911c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks        if (mCameraDevice == null) {
1770ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang            mCameraDevice = CameraHolder.instance().open(mCameraId);
177170fe12f4d0ba9c08be9e0f0bda75b172a21cf70aOwen Lin            mInitialParams = mCameraDevice.getParameters();
177211c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks        }
177311c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks    }
177411c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks
17753f3c857e3f34650c15d764810335024654b0fcc3Owen Lin    private void showCameraErrorAndFinish() {
17763f3c857e3f34650c15d764810335024654b0fcc3Owen Lin        Resources ress = getResources();
17773f3c857e3f34650c15d764810335024654b0fcc3Owen Lin        Util.showFatalErrorAndFinish(Camera.this,
17783f3c857e3f34650c15d764810335024654b0fcc3Owen Lin                ress.getString(R.string.camera_error_title),
17793f3c857e3f34650c15d764810335024654b0fcc3Owen Lin                ress.getString(R.string.cannot_connect_camera));
17803f3c857e3f34650c15d764810335024654b0fcc3Owen Lin    }
17813f3c857e3f34650c15d764810335024654b0fcc3Owen Lin
1782068a2e745b284b33af161d629adb9b7839be32d1Wu-cheng Li    private boolean restartPreview() {
17833f3c857e3f34650c15d764810335024654b0fcc3Owen Lin        try {
17843f3c857e3f34650c15d764810335024654b0fcc3Owen Lin            startPreview();
17853f3c857e3f34650c15d764810335024654b0fcc3Owen Lin        } catch (CameraHardwareException e) {
17863f3c857e3f34650c15d764810335024654b0fcc3Owen Lin            showCameraErrorAndFinish();
1787068a2e745b284b33af161d629adb9b7839be32d1Wu-cheng Li            return false;
17883f3c857e3f34650c15d764810335024654b0fcc3Owen Lin        }
1789068a2e745b284b33af161d629adb9b7839be32d1Wu-cheng Li        return true;
1790b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1791b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
179248f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li    private void setPreviewDisplay(SurfaceHolder holder) {
179348f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        try {
179448f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li            mCameraDevice.setPreviewDisplay(holder);
179548f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        } catch (Throwable ex) {
179648f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li            closeCamera();
179748f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li            throw new RuntimeException("setPreviewDisplay failed", ex);
179848f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        }
179948f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li    }
180048f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li
18013f3c857e3f34650c15d764810335024654b0fcc3Owen Lin    private void startPreview() throws CameraHardwareException {
18023f3c857e3f34650c15d764810335024654b0fcc3Owen Lin        if (mPausing || isFinishing()) return;
180311c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks
18043f3c857e3f34650c15d764810335024654b0fcc3Owen Lin        ensureCameraDevice();
18056eabb9b770a7c60cb92aa2e22f360754f32f39f8Wu-cheng Li        mCameraDevice.setErrorCallback(mErrorCallback);
1806b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
18076cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li        // If we're previewing already, stop the preview first (this will blank
18086cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li        // the screen).
18090e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin        if (mPreviewing) stopPreview();
1810b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
181148f6cdff77fe6b0c44b7d4c4261599e1de7225f1Wu-cheng Li        setPreviewDisplay(mSurfaceHolder);
1812d4a37518c3b73fcf1860ab10fd3bb7e7ceffba99Wu-cheng Li        Util.setCameraDisplayOrientation(this, mCameraId, mCameraDevice);
1813ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        setCameraParameters(UPDATE_PARAM_ALL);
1814b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
1815bcd13fa071abe75b3275e3bc314601f33e33972aWu-cheng Li
181611c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks        try {
18178564b8a3844d87e1ffe3d40cb8ff7ff5cb93312eWu-cheng Li            Log.v(TAG, "startPreview");
181811c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks            mCameraDevice.startPreview();
1819fa1dfa2f3f1385e98cf8bdac5c48f8d13a400a8aWu-cheng Li        } catch (Throwable ex) {
1820fa1dfa2f3f1385e98cf8bdac5c48f8d13a400a8aWu-cheng Li            closeCamera();
1821fa1dfa2f3f1385e98cf8bdac5c48f8d13a400a8aWu-cheng Li            throw new RuntimeException("startPreview failed", ex);
1822b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
182311c53782ca0a7be5e033bafdf0be6ac124bde8cbDave Sparks        mPreviewing = true;
182433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        mZoomState = ZOOM_STOPPED;
18256cc6daff9d7cb02805cd9ce71176c7b49655f8a8Wu-cheng Li        mStatus = IDLE;
1826b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
1827b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
18285fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li    private void stopPreview() {
18295fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li        if (mCameraDevice != null && mPreviewing) {
18308564b8a3844d87e1ffe3d40cb8ff7ff5cb93312eWu-cheng Li            Log.v(TAG, "stopPreview");
18315fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li            mCameraDevice.stopPreview();
18325fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li        }
18335fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li        mPreviewing = false;
18345fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li        // If auto focus was in progress, it would have been canceled.
18355fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li        clearFocusState();
18365fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li    }
18375fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li
1838dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin    private static boolean isSupported(String value, List<String> supported) {
1839dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin        return supported == null ? false : supported.indexOf(value) >= 0;
1840dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin    }
1841dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin
1842ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    private void updateCameraParametersInitialize() {
1843cc5a6356055dd8a8a4017b0af5410b53f25aaef0Wu-cheng Li        // Reset preview frame rate to the maximum because it may be lowered by
1844cc5a6356055dd8a8a4017b0af5410b53f25aaef0Wu-cheng Li        // video camera application.
1845cc5a6356055dd8a8a4017b0af5410b53f25aaef0Wu-cheng Li        List<Integer> frameRates = mParameters.getSupportedPreviewFrameRates();
1846cc5a6356055dd8a8a4017b0af5410b53f25aaef0Wu-cheng Li        if (frameRates != null) {
1847cc5a6356055dd8a8a4017b0af5410b53f25aaef0Wu-cheng Li            Integer max = Collections.max(frameRates);
1848cc5a6356055dd8a8a4017b0af5410b53f25aaef0Wu-cheng Li            mParameters.setPreviewFrameRate(max);
1849cc5a6356055dd8a8a4017b0af5410b53f25aaef0Wu-cheng Li        }
1850cc5a6356055dd8a8a4017b0af5410b53f25aaef0Wu-cheng Li
1851ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    }
1852ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
1853ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    private void updateCameraParametersZoom() {
1854ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        // Set zoom.
1855ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        if (mParameters.isZoomSupported()) {
1856ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            mParameters.setZoom(mZoomValue);
1857ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        }
1858ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    }
1859ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
1860ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    private void updateCameraParametersPreference() {
18615945773c664ada754ce5eb9239bd4c8bd95ee4b4Wu-cheng Li        // Set picture size.
18622c6c6174e2363fccb4e4f29b76290e99234fe140Owen Lin        String pictureSize = mPreferences.getString(
186332bfffa5822490ea0e105e9fe00b9df8db1b7b02Owen Lin                CameraSettings.KEY_PICTURE_SIZE, null);
186432bfffa5822490ea0e105e9fe00b9df8db1b7b02Owen Lin        if (pictureSize == null) {
186532bfffa5822490ea0e105e9fe00b9df8db1b7b02Owen Lin            CameraSettings.initialCameraPictureSize(this, mParameters);
186632bfffa5822490ea0e105e9fe00b9df8db1b7b02Owen Lin        } else {
186732bfffa5822490ea0e105e9fe00b9df8db1b7b02Owen Lin            List<Size> supported = mParameters.getSupportedPictureSizes();
186832bfffa5822490ea0e105e9fe00b9df8db1b7b02Owen Lin            CameraSettings.setCameraPictureSize(
186932bfffa5822490ea0e105e9fe00b9df8db1b7b02Owen Lin                    pictureSize, supported, mParameters);
187032bfffa5822490ea0e105e9fe00b9df8db1b7b02Owen Lin        }
1871937fc48b37fafe3ffc8f4b52bd9a171bbb4d3a37Owen Lin
1872ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin        // Set the preview frame aspect ratio according to the picture size.
1873ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin        Size size = mParameters.getPictureSize();
1874ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin        PreviewFrameLayout frameLayout =
1875ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin                (PreviewFrameLayout) findViewById(R.id.frame_layout);
1876ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin        frameLayout.setAspectRatio((double) size.width / size.height);
1877ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin
1878ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin        // Set a preview size that is closest to the viewfinder height and has
1879ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin        // the right aspect ratio.
1880ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin        List<Size> sizes = mParameters.getSupportedPreviewSizes();
18816227fa641518492a6b660c78463da18d9ec8fcd8Nipun Kwatra        Size optimalSize = Util.getOptimalPreviewSize(this,
1882ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin                sizes, (double) size.width / size.height);
18830fad45d0d64569098f4daf34ff7e5ae785601304Wu-cheng Li        Size original = mParameters.getPreviewSize();
18840fad45d0d64569098f4daf34ff7e5ae785601304Wu-cheng Li        if (!original.equals(optimalSize)) {
18850fad45d0d64569098f4daf34ff7e5ae785601304Wu-cheng Li            mParameters.setPreviewSize(optimalSize.width, optimalSize.height);
18860fad45d0d64569098f4daf34ff7e5ae785601304Wu-cheng Li
18870fad45d0d64569098f4daf34ff7e5ae785601304Wu-cheng Li            // Zoom related settings will be changed for different preview
18880fad45d0d64569098f4daf34ff7e5ae785601304Wu-cheng Li            // sizes, so set and read the parameters to get lastest values
18890fad45d0d64569098f4daf34ff7e5ae785601304Wu-cheng Li            mCameraDevice.setParameters(mParameters);
18900fad45d0d64569098f4daf34ff7e5ae785601304Wu-cheng Li            mParameters = mCameraDevice.getParameters();
1891ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin        }
18920fad45d0d64569098f4daf34ff7e5ae785601304Wu-cheng Li        Log.v(TAG, "Preview size is " + optimalSize.width + "x" + optimalSize.height);
1893ffcca7403c9a364403357d3c76a3ff256c63c786Cheng-Ru Lin
1894ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        // Since change scene mode may change supported values,
1895ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        // Set scene mode first,
189618a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin        mSceneMode = mPreferences.getString(
1897ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                CameraSettings.KEY_SCENE_MODE,
1898ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                getString(R.string.pref_camera_scenemode_default));
189918a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin        if (isSupported(mSceneMode, mParameters.getSupportedSceneModes())) {
190018a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin            if (!mParameters.getSceneMode().equals(mSceneMode)) {
190118a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin                mParameters.setSceneMode(mSceneMode);
1902ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                mCameraDevice.setParameters(mParameters);
1903ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
1904ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                // Setting scene mode will change the settings of flash mode,
1905ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                // white balance, and focus mode. Here we read back the
1906ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                // parameters, so we can know those settings.
1907ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                mParameters = mCameraDevice.getParameters();
1908ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            }
1909ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        } else {
191018a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin            mSceneMode = mParameters.getSceneMode();
191118a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin            if (mSceneMode == null) {
191218a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin                mSceneMode = Parameters.SCENE_MODE_AUTO;
1913ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            }
1914ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        }
1915ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
19165945773c664ada754ce5eb9239bd4c8bd95ee4b4Wu-cheng Li        // Set JPEG quality.
1917e0770004600f76cc4b440104e98e14712b4fd1c6Wu-cheng Li        String jpegQuality = mPreferences.getString(
1918e0770004600f76cc4b440104e98e14712b4fd1c6Wu-cheng Li                CameraSettings.KEY_JPEG_QUALITY,
1919e0770004600f76cc4b440104e98e14712b4fd1c6Wu-cheng Li                getString(R.string.pref_camera_jpegquality_default));
19203a4ef93f95bed9f812fe75ef94296450833b3997James Dong        mParameters.setJpegQuality(JpegEncodingQualityMappings.getQualityNumber(jpegQuality));
1921937fc48b37fafe3ffc8f4b52bd9a171bbb4d3a37Owen Lin
1922dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin        // For the following settings, we need to check if the settings are
1923dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin        // still supported by latest driver, if not, ignore the settings.
1924dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin
1925dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin        // Set color effect parameter.
1926dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin        String colorEffect = mPreferences.getString(
1927dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin                CameraSettings.KEY_COLOR_EFFECT,
1928dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin                getString(R.string.pref_camera_coloreffect_default));
1929dbb23f7bc9d3f3807f12926ccbfe2a95a0c1355aOwen Lin        if (isSupported(colorEffect, mParameters.getSupportedColorEffects())) {
19305f6484a74fe019337e436e6e0dcb07375a74af25President Li            mParameters.setColorEffect(colorEffect);
19315f6484a74fe019337e436e6e0dcb07375a74af25President Li        }
19325f6484a74fe019337e436e6e0dcb07375a74af25President Li
193333be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        // Set exposure compensation
193433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        String exposure = mPreferences.getString(
193533be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                CameraSettings.KEY_EXPOSURE,
193633be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                getString(R.string.pref_exposure_default));
193733be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        try {
193833be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            int value = Integer.parseInt(exposure);
193933be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            int max = mParameters.getMaxExposureCompensation();
194033be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            int min = mParameters.getMinExposureCompensation();
194133be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            if (value >= min && value <= max) {
194233be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                mParameters.setExposureCompensation(value);
194333be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            } else {
194433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin                Log.w(TAG, "invalid exposure range: " + exposure);
194533be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            }
194633be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        } catch (NumberFormatException e) {
194733be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin            Log.w(TAG, "invalid exposure: " + exposure);
194833be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin        }
194933be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin
195018a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin        if (Parameters.SCENE_MODE_AUTO.equals(mSceneMode)) {
1951301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            // Set flash mode.
19524de149ceb47f2c251f646419907424bfb67d2b64Owen Lin            String flashMode = mPreferences.getString(
1953301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin                    CameraSettings.KEY_FLASH_MODE,
1954301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin                    getString(R.string.pref_camera_flashmode_default));
1955301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            List<String> supportedFlash = mParameters.getSupportedFlashModes();
1956301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            if (isSupported(flashMode, supportedFlash)) {
1957301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin                mParameters.setFlashMode(flashMode);
1958301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            } else {
19593b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                flashMode = mParameters.getFlashMode();
19603b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                if (flashMode == null) {
1961503560f0dc42d6185c61be06f8060dfd50d30d69Owen Lin                    flashMode = getString(
1962503560f0dc42d6185c61be06f8060dfd50d30d69Owen Lin                            R.string.pref_camera_flashmode_no_flash);
19633b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                }
1964301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            }
1965301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin
1966301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            // Set white balance parameter.
19674de149ceb47f2c251f646419907424bfb67d2b64Owen Lin            String whiteBalance = mPreferences.getString(
1968301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin                    CameraSettings.KEY_WHITE_BALANCE,
1969301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin                    getString(R.string.pref_camera_whitebalance_default));
197070fe12f4d0ba9c08be9e0f0bda75b172a21cf70aOwen Lin            if (isSupported(whiteBalance,
197170fe12f4d0ba9c08be9e0f0bda75b172a21cf70aOwen Lin                    mParameters.getSupportedWhiteBalance())) {
1972301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin                mParameters.setWhiteBalance(whiteBalance);
19733b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin            } else {
19743b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                whiteBalance = mParameters.getWhiteBalance();
19753b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                if (whiteBalance == null) {
19763b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                    whiteBalance = Parameters.WHITE_BALANCE_AUTO;
19773b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                }
1978301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            }
1979301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin
1980301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            // Set focus mode.
1981301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            mFocusMode = mPreferences.getString(
1982301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin                    CameraSettings.KEY_FOCUS_MODE,
1983301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin                    getString(R.string.pref_camera_focusmode_default));
1984301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            if (isSupported(mFocusMode, mParameters.getSupportedFocusModes())) {
1985301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin                mParameters.setFocusMode(mFocusMode);
19863b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin            } else {
19873b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                mFocusMode = mParameters.getFocusMode();
19883b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                if (mFocusMode == null) {
19893b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                    mFocusMode = Parameters.FOCUS_MODE_AUTO;
19903b90cce1e143de64b29a33828d371c9c0e36c772Cheng-Ru Lin                }
1991301c088512a06ec5154da2d27761537b6607f986Cheng-Ru Lin            }
199218a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin        } else {
199318a0b5bb13b9dc9cdb01d03e901c0836742f7a3aOwen Lin            mFocusMode = mParameters.getFocusMode();
1994cc529326d9369290abd6933f1d070859b3cadf75Wu-cheng Li        }
1995e0770004600f76cc4b440104e98e14712b4fd1c6Wu-cheng Li    }
1996e0770004600f76cc4b440104e98e14712b4fd1c6Wu-cheng Li
1997ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    // We separate the parameters into several subsets, so we can update only
1998ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    // the subsets actually need updating. The PREFERENCE set needs extra
1999ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    // locking because the preference can be changed from GLThread as well.
2000ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    private void setCameraParameters(int updateSet) {
2001ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        mParameters = mCameraDevice.getParameters();
2002ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
2003ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        if ((updateSet & UPDATE_PARAM_INITIALIZE) != 0) {
2004ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            updateCameraParametersInitialize();
2005ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        }
2006ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
2007ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        if ((updateSet & UPDATE_PARAM_ZOOM) != 0) {
2008ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            updateCameraParametersZoom();
2009ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        }
2010ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
2011ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        if ((updateSet & UPDATE_PARAM_PREFERENCE) != 0) {
20120d73bccf89870682007dd32d4a84bcdb812e2cc7Owen Lin            updateCameraParametersPreference();
2013ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        }
2014ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
2015ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        mCameraDevice.setParameters(mParameters);
2016ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    }
2017ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
2018ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    // If the Camera is idle, update the parameters immediately, otherwise
2019ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    // accumulate them in mUpdateSet and update later.
2020ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang    private void setCameraParametersWhenIdle(int additionalUpdateSet) {
2021ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        mUpdateSet |= additionalUpdateSet;
20221de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang        if (mCameraDevice == null) {
2023ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            // We will update all the parameters when we open the device, so
2024ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            // we don't need to do anything now.
2025ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            mUpdateSet = 0;
20261de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang            return;
20271de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang        } else if (isCameraIdle()) {
2028ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            setCameraParameters(mUpdateSet);
20298a66efdc845a5fdf0750d0fd6a2a92ecbcf77fceWu-cheng Li            updateSceneModeUI();
2030ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            mUpdateSet = 0;
20311de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang        } else {
2032ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            if (!mHandler.hasMessages(SET_CAMERA_PARAMETERS_WHEN_IDLE)) {
2033ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                mHandler.sendEmptyMessageDelayed(
2034ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                        SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000);
2035ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            }
20361de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang        }
20371de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang    }
20381de0469fa91cee8ed0771d020042cc82576a601cChih-Chung Chang
2039522e836abc90bcb2d4e7a4ac37e269154d06c715Chih-Chung Chang    private void gotoGallery() {
2040b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        MenuHelper.gotoCameraImageGallery(this);
2041b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2042b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
20439f73bd9a85d295091fae39dc256a122e1843e2e8Wu-cheng Li    private void viewImage(RotateImageView view) {
20441efb18228a45952d699af280482e1c51a4582690Owen Lin        if(!view.isUriValid()) {
20451efb18228a45952d699af280482e1c51a4582690Owen Lin            Log.e(TAG, "Uri invalid. uri=" + view.getUri());
20461efb18228a45952d699af280482e1c51a4582690Owen Lin            return;
20471efb18228a45952d699af280482e1c51a4582690Owen Lin        }
20481efb18228a45952d699af280482e1c51a4582690Owen Lin
20491efb18228a45952d699af280482e1c51a4582690Owen Lin        try {
20501efb18228a45952d699af280482e1c51a4582690Owen Lin            startActivity(new Intent(
20511efb18228a45952d699af280482e1c51a4582690Owen Lin                    Util.REVIEW_ACTION, view.getUri()));
20521efb18228a45952d699af280482e1c51a4582690Owen Lin        } catch (ActivityNotFoundException ex) {
2053b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            try {
20541efb18228a45952d699af280482e1c51a4582690Owen Lin                startActivity(new Intent(
20551efb18228a45952d699af280482e1c51a4582690Owen Lin                        Intent.ACTION_VIEW, view.getUri()));
20561efb18228a45952d699af280482e1c51a4582690Owen Lin            } catch (ActivityNotFoundException e) {
20571efb18228a45952d699af280482e1c51a4582690Owen Lin                Log.e(TAG, "review image fail. uri=" + view.getUri(), e);
2058b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
2059b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
2060b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2061b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
2062b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void startReceivingLocationUpdates() {
2063b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        if (mLocationManager != null) {
2064b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            try {
2065b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                mLocationManager.requestLocationUpdates(
2066b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        LocationManager.NETWORK_PROVIDER,
2067b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        1000,
2068b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        0F,
2069b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        mLocationListeners[1]);
2070b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } catch (java.lang.SecurityException ex) {
2071deb57259b0055c08a44cd43933276ccf485dcd2dOwen Lin                Log.i(TAG, "fail to request location update, ignore", ex);
2072b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } catch (IllegalArgumentException ex) {
2073deb57259b0055c08a44cd43933276ccf485dcd2dOwen Lin                Log.d(TAG, "provider does not exist " + ex.getMessage());
2074b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
2075b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            try {
2076b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                mLocationManager.requestLocationUpdates(
2077b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        LocationManager.GPS_PROVIDER,
2078b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        1000,
2079b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        0F,
2080b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                        mLocationListeners[0]);
2081f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan                showGpsOnScreenIndicator(false);
2082b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } catch (java.lang.SecurityException ex) {
2083deb57259b0055c08a44cd43933276ccf485dcd2dOwen Lin                Log.i(TAG, "fail to request location update, ignore", ex);
2084b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            } catch (IllegalArgumentException ex) {
2085deb57259b0055c08a44cd43933276ccf485dcd2dOwen Lin                Log.d(TAG, "provider does not exist " + ex.getMessage());
2086b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
20878303435a2466803e05dd99c60cdb2de074b79162Wu-cheng Li            Log.d(TAG, "startReceivingLocationUpdates");
2088b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
2089b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2090b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
2091b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void stopReceivingLocationUpdates() {
2092b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        if (mLocationManager != null) {
2093b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            for (int i = 0; i < mLocationListeners.length; i++) {
2094b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                try {
2095b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                    mLocationManager.removeUpdates(mLocationListeners[i]);
2096b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                } catch (Exception ex) {
2097deb57259b0055c08a44cd43933276ccf485dcd2dOwen Lin                    Log.i(TAG, "fail to remove location listners, ignore", ex);
2098b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project                }
2099b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
21008303435a2466803e05dd99c60cdb2de074b79162Wu-cheng Li            Log.d(TAG, "stopReceivingLocationUpdates");
2101b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
2102f63366e6305e75ae289aaac20b397a3e9633d9dfHung-ying Tyan        hideGpsOnScreenIndicator();
2103b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2104b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
2105b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private Location getCurrentLocation() {
2106b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        // go in best to worst order
2107b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        for (int i = 0; i < mLocationListeners.length; i++) {
210807c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin            Location l = mLocationListeners[i].current();
210907c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin            if (l != null) return l;
2110b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
21118303435a2466803e05dd99c60cdb2de074b79162Wu-cheng Li        Log.d(TAG, "No location received yet.");
211207c90568c7b0568e6cb75559bd13e5d000ac040fOwen Lin        return null;
2113b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2114b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
21155fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li    private boolean isCameraIdle() {
21165fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li        return mStatus == IDLE && mFocusState == FOCUS_NOT_STARTED;
2117b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2118b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
2119b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private boolean isImageCaptureIntent() {
2120b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        String action = getIntent().getAction();
2121b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        return (MediaStore.ACTION_IMAGE_CAPTURE.equals(action));
2122b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2123b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
2124cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang    private void setupCaptureParams() {
2125cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        Bundle myExtras = getIntent().getExtras();
2126cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        if (myExtras != null) {
2127cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang            mSaveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT);
2128cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang            mCropValue = myExtras.getString("crop");
2129cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang        }
2130cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang    }
2131cd972b0cd67d7a45084d9757ebd49b8f22989eddChih-Chung Chang
2132b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void showPostCaptureAlert() {
21339bc8d1b47e5bab978cc9571ddfdedc8309a0b65dChih-Chung Chang        if (mIsImageCaptureIntent) {
213485337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan            if (mIndicatorWheel == null) {
213585337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan                mShutterButton.setVisibility(View.INVISIBLE);
2136e50d2553aeb9e55504697e2a72759e8f46ee2992Hung-ying Tyan            } else {
2137e50d2553aeb9e55504697e2a72759e8f46ee2992Hung-ying Tyan                mShutterButton.setEnabled(false);
213885337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan            }
213961b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin            int[] pickIds = {R.id.btn_retake, R.id.btn_done};
21400e841fe509c3b61ced7ddf4bee4728b382eae62fOwen Lin            for (int id : pickIds) {
214161b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin                View button = findViewById(id);
214261b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin                ((View) button.getParent()).setVisibility(View.VISIBLE);
2143b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            }
2144373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyan
2145373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyan            // Remove the text of the cancel button
2146373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyan            View view = findViewById(R.id.btn_cancel);
2147373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyan            if (view instanceof Button) ((Button) view).setText("");
2148b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
2149b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2150b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
2151b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void hidePostCaptureAlert() {
21529bc8d1b47e5bab978cc9571ddfdedc8309a0b65dChih-Chung Chang        if (mIsImageCaptureIntent) {
215385337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan            if (mIndicatorWheel == null) {
215485337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan                mShutterButton.setVisibility(View.VISIBLE);
2155e50d2553aeb9e55504697e2a72759e8f46ee2992Hung-ying Tyan            } else {
2156e50d2553aeb9e55504697e2a72759e8f46ee2992Hung-ying Tyan                mShutterButton.setEnabled(true);
215785337c206051c2c7d33cca83255e00d92e288421Hung-ying Tyan            }
215861b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin            int[] pickIds = {R.id.btn_retake, R.id.btn_done};
215961b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin            for (int id : pickIds) {
216061b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin                View button = findViewById(id);
216161b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin                ((View) button.getParent()).setVisibility(View.GONE);
216261b98317dfb7b8bad4e826e1db560128e1c37374Owen Lin            }
2163e50d2553aeb9e55504697e2a72759e8f46ee2992Hung-ying Tyan            enableCameraControls(true);
2164373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyan
2165373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyan            // Restore the text of the cancel button
2166373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyan            View view = findViewById(R.id.btn_cancel);
2167373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyan            if (view instanceof Button) {
2168373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyan                ((Button) view).setText(R.string.review_cancel);
2169373d10b91c8e736275a0bc52e247598eec0b3ceaHung-ying Tyan            }
2170b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
2171b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2172b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
21735fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li    @Override
21745fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li    public boolean onPrepareOptionsMenu(Menu menu) {
21755fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li        super.onPrepareOptionsMenu(menu);
21765fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li        // Only show the menu when camera is idle.
2177b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang        for (int i = 0; i < menu.size(); i++) {
2178b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang            menu.getItem(i).setVisible(isCameraIdle());
21795fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li        }
21805fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li
21815fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li        return true;
21825fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li    }
21835fa9483e0f3a31fcff7da86cdd9e32795eacf235Wu-cheng Li
2184b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    @Override
2185b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    public boolean onCreateOptionsMenu(Menu menu) {
2186b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        super.onCreateOptionsMenu(menu);
2187b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
21889bc8d1b47e5bab978cc9571ddfdedc8309a0b65dChih-Chung Chang        if (mIsImageCaptureIntent) {
2189b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            // No options menu for attach mode.
2190b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            return false;
2191b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        } else {
2192b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project            addBaseMenuItems(menu);
2193b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        }
2194b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project        return true;
2195b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2196b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project
2197b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    private void addBaseMenuItems(Menu menu) {
21989ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang        MenuHelper.addSwitchModeMenuItem(menu, true, new Runnable() {
21999ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang            public void run() {
22009ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang                switchToVideoMode();
22019ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang            }
22029ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang        });
2203d189083d760820ad0b0d88e11feb897e4ab5011cChih-Chung Chang        MenuItem gallery = menu.add(Menu.NONE, Menu.NONE,
2204d189083d760820ad0b0d88e11feb897e4ab5011cChih-Chung Chang                MenuHelper.POSITION_GOTO_GALLERY,
2205b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang                R.string.camera_gallery_photos_text)
2206b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang                .setOnMenuItemClickListener(new OnMenuItemClickListener() {
2207b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang            public boolean onMenuItemClick(MenuItem item) {
2208b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang                gotoGallery();
2209b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang                return true;
2210b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang            }
2211b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang        });
2212b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang        gallery.setIcon(android.R.drawable.ic_menu_gallery);
2213b5946549f73a3f49ccf163dda424a0319b4b6838Chih-Chung Chang        mGalleryItems.add(gallery);
2214ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang
2215ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        if (mNumberOfCameras > 1) {
2216ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang            menu.add(Menu.NONE, Menu.NONE,
2217ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang                    MenuHelper.POSITION_SWITCH_CAMERA_ID,
2218ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang                    R.string.switch_camera_id)
2219ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang                    .setOnMenuItemClickListener(new OnMenuItemClickListener() {
2220ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang                public boolean onMenuItemClick(MenuItem item) {
222159ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang                    CameraSettings.writePreferredCameraId(mPreferences,
222259ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang                            ((mCameraId == mFrontCameraId)
222359ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang                            ? mBackCameraId : mFrontCameraId));
222459ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang                    onSharedPreferenceChanged();
2225ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang                    return true;
2226ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang                }
2227ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang            }).setIcon(android.R.drawable.ic_menu_camera);
2228ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        }
2229ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang    }
2230ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang
22317add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li    private void switchCameraId(int cameraId) {
2232a79c5e1d807e5c86f5f9ced44eac50076596e1abWu-cheng Li        if (mPausing || !isCameraIdle()) return;
22337add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li        mCameraId = cameraId;
2234ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang
2235ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        stopPreview();
2236ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        closeCamera();
2237ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang
2238ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        // Remove the messages in the event queue.
2239ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        mHandler.removeMessages(RESTART_PREVIEW);
2240ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang
2241ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        // Reset variables
2242ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        mJpegPictureCallbackTime = 0;
2243ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        mZoomValue = 0;
2244ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang
22451ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang        // Reload the preferences.
22461ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang        mPreferences.setLocalId(this, mCameraId);
22471ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang        CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
22481ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang
2249ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        // Restart the preview.
2250ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        resetExposureCompensation();
2251068a2e745b284b33af161d629adb9b7839be32d1Wu-cheng Li        if (!restartPreview()) return;
2252ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang
2253ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang        initializeZoom();
2254ac9d0a1ce538eb4bd50cba3b257737a05b9ac4e5Chih-Chung Chang
22551ef634d277d550ed55b5b7089dfd56ed71815bd6Chih-Chung Chang        // Reload the UI.
2256067fe861cf3402f2bef3f83995579d3411f0a8b3Hung-ying Tyan        initializeHeadUpDisplay();
22571f09dac3d664734d099765783dae1233ef023226Wu-cheng Li        initializeControlPanel();
2258b64d345c9d51cabce43b5191532a0c185d2a70a5The Android Open Source Project    }
2259e239acc2185c5fb47b22a65f78ffa461e19f183cOwen Lin
22609ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang    private boolean switchToVideoMode() {
2261e36da237f13cb3f31ec6fe3af98f2f838cabc6b2Owen Lin        if (isFinishing() || !isCameraIdle()) return false;
2262eb1f0c8299d96bad9bb449ff9e13eff4b43272ccHung-ying Tyan        MenuHelper.gotoVideoMode(Camera.this);
2263b2b23846d89c63ba0d28bbc7627022e6b9fa620bOwen Lin        mHandler.removeMessages(FIRST_TIME_INIT);
22649ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang        finish();
22659ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang        return true;
22669ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang    }
22679ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang
2268d918cfa6846593ceb844196a6d0012f3f350252fChih-Chung Chang    public boolean onSwitchChanged(Switcher source, boolean onOff) {
2269d918cfa6846593ceb844196a6d0012f3f350252fChih-Chung Chang        if (onOff == SWITCH_VIDEO) {
22709ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang            return switchToVideoMode();
22719ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang        } else {
22729ae7d027bb8f55b85a158cddeb7ed84c5a0a7983Chih-Chung Chang            return true;
2273e239acc2185c5fb47b22a65f78ffa461e19f183cOwen Lin        }
2274e239acc2185c5fb47b22a65f78ffa461e19f183cOwen Lin    }
22758df283a6685ee0dd18cd3276b6bb716b21f269c6Chih-Chung Chang
227687695169a6b54b39258f236656b31991a3dbc2f8Wu-cheng Li    private void onSharedPreferenceChanged() {
2277c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        // ignore the events after "onPause()"
2278c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        if (mPausing) return;
2279ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
2280ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        boolean recordLocation;
2281ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
22820d73bccf89870682007dd32d4a84bcdb812e2cc7Owen Lin        recordLocation = RecordLocationPreference.get(
22830d73bccf89870682007dd32d4a84bcdb812e2cc7Owen Lin                mPreferences, getContentResolver());
2284ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang
2285ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        if (mRecordLocation != recordLocation) {
2286e391150007300e63d3fb6e2236db3f4c9247fb9cRay Chen            mRecordLocation = recordLocation;
2287ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            if (mRecordLocation) {
2288ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                startReceivingLocationUpdates();
2289ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            } else {
2290ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang                stopReceivingLocationUpdates();
2291ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang            }
2292ee1cf3f0509987e9c76626077bd8117399311cedChih-Chung Chang        }
22937add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li        int cameraId = CameraSettings.readPreferredCameraId(mPreferences);
22947add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li        if (mCameraId != cameraId) {
22957add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li            switchCameraId(cameraId);
22967add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li        } else {
22977add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li            setCameraParametersWhenIdle(UPDATE_PARAM_PREFERENCE);
22987add00693c1ec910bc8700fe046ee18cbe4e1148Wu-cheng Li        }
22998791695ba110ad0b088d2e8b3b0127c0042e1865Owen Lin    }
23008791695ba110ad0b088d2e8b3b0127c0042e1865Owen Lin
2301cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin    @Override
2302cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin    public void onUserInteraction() {
2303cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin        super.onUserInteraction();
2304cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin        keepScreenOnAwhile();
2305cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin    }
2306cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin
2307cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin    private void resetScreenOn() {
2308cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin        mHandler.removeMessages(CLEAR_SCREEN_DELAY);
2309cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2310cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin    }
2311cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin
2312cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin    private void keepScreenOnAwhile() {
2313cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin        mHandler.removeMessages(CLEAR_SCREEN_DELAY);
2314cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2315cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin        mHandler.sendEmptyMessageDelayed(CLEAR_SCREEN_DELAY, SCREEN_DELAY);
2316cc8d7565f1500b6db20dd7d4c8cc3b6434067e0bOwen Lin    }
2317faf398ff0fa2b51348b4545505112d70ba7b47a1Chih-Chung Chang
2318c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin    private class MyHeadUpDisplayListener implements HeadUpDisplay.Listener {
2319c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin
232087695169a6b54b39258f236656b31991a3dbc2f8Wu-cheng Li        public void onSharedPreferenceChanged() {
232187695169a6b54b39258f236656b31991a3dbc2f8Wu-cheng Li            Camera.this.onSharedPreferenceChanged();
2322c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        }
2323c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin
2324c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        public void onRestorePreferencesClicked() {
23250d73bccf89870682007dd32d4a84bcdb812e2cc7Owen Lin            Camera.this.onRestorePreferencesClicked();
2326c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        }
2327c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin
2328c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        public void onPopupWindowVisibilityChanged(int visibility) {
2329c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        }
2330c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin    }
2331c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin
2332c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin    protected void onRestorePreferencesClicked() {
2333c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        if (mPausing) return;
2334c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        Runnable runnable = new Runnable() {
2335c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin            public void run() {
2336003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li                restorePreferences();
2337c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin            }
2338c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        };
2339c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin        MenuHelper.confirmAction(this,
2340c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin                getString(R.string.confirm_restore_title),
2341c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin                getString(R.string.confirm_restore_message),
2342c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin                runnable);
2343c24155c65b45f554ffc29c523d7ea8e63ea10d33Owen Lin    }
2344dfee7f813adb8de209472d24553b860d2515ea0aWu-cheng Li
2345003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li    private void restorePreferences() {
2346b7893fdf578192a262dd48297c14092547c15d57Wu-cheng Li        // Reset the zoom. Zoom value is not stored in preference.
2347b7893fdf578192a262dd48297c14092547c15d57Wu-cheng Li        if (mParameters.isZoomSupported()) {
2348b7893fdf578192a262dd48297c14092547c15d57Wu-cheng Li            mZoomValue = 0;
2349b7893fdf578192a262dd48297c14092547c15d57Wu-cheng Li            setCameraParametersWhenIdle(UPDATE_PARAM_ZOOM);
2350b7893fdf578192a262dd48297c14092547c15d57Wu-cheng Li            if (mZoomPicker != null) mZoomPicker.setZoomIndex(0);
2351b7893fdf578192a262dd48297c14092547c15d57Wu-cheng Li        }
2352b7893fdf578192a262dd48297c14092547c15d57Wu-cheng Li
2353003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li        if (mHeadUpDisplay != null) {
2354003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li            mHeadUpDisplay.restorePreferences(mParameters);
2355003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li        }
2356003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li
2357003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li        if (mControlPanel != null) {
2358003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li            mControlPanel.dismissSettingPopup();
2359003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li            CameraSettings.restorePreferences(Camera.this, mPreferences,
2360003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li                    mParameters);
2361003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li            initializeControlPanel();
2362003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li            onSharedPreferenceChanged();
2363003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li        }
2364003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li    }
2365003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li
2366fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li    protected void onOverriddenPreferencesClicked() {
2367fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li        if (mPausing) return;
2368fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li        if (mNotSelectableToast == null) {
2369fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li            String str = getResources().getString(R.string.not_selectable_in_scene_mode);
2370fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li            mNotSelectableToast = Toast.makeText(Camera.this, str, Toast.LENGTH_SHORT);
2371fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li        }
2372fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li        mNotSelectableToast.cancel();
2373fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li        mNotSelectableToast.show();
2374fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li    }
2375fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li
2376f5bffc476b8293b30bb500bed2ab7914bb4ca411Wu-cheng Li    private class MyControlPanelListener implements ControlPanel.Listener {
237787695169a6b54b39258f236656b31991a3dbc2f8Wu-cheng Li        public void onSharedPreferenceChanged() {
237887695169a6b54b39258f236656b31991a3dbc2f8Wu-cheng Li            Camera.this.onSharedPreferenceChanged();
2379dfee7f813adb8de209472d24553b860d2515ea0aWu-cheng Li        }
2380003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li
2381003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li        public void onRestorePreferencesClicked() {
2382003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li            Camera.this.onRestorePreferencesClicked();
2383003dd5a52457c024a0f99a2bb222bfc6ad70bbe5Wu-cheng Li        }
2384fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li
2385fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li        public void onOverriddenPreferencesClicked() {
2386fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li            Camera.this.onOverriddenPreferencesClicked();
2387fba0439b62f19a2a19ff6559c1ae055e910400dbWu-cheng Li        }
2388dfee7f813adb8de209472d24553b860d2515ea0aWu-cheng Li    }
23895e5aa7e1e8bd03e9919d5f14b9f5df75cf68ff8fChih-Chung Chang
239059ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang    private class MyCameraPickerListener implements CameraPicker.Listener {
239159ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang        public void onSharedPreferenceChanged() {
239259ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang            Camera.this.onSharedPreferenceChanged();
239359ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang        }
239459ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang    }
239559ddc8c1add2fcffe523e3877baad1b6cc5ba2fbChung-yih Wang}
2396