AndroidCamera2AgentImpl.java revision 415cf3ee18640ad0799e5c9a738afd1d19c14883
1a0842b40441db5332a5290f941021636b1182761Sol Boucher/*
2a0842b40441db5332a5290f941021636b1182761Sol Boucher * Copyright (C) 2014 The Android Open Source Project
3a0842b40441db5332a5290f941021636b1182761Sol Boucher *
4a0842b40441db5332a5290f941021636b1182761Sol Boucher * Licensed under the Apache License, Version 2.0 (the "License");
5a0842b40441db5332a5290f941021636b1182761Sol Boucher * you may not use this file except in compliance with the License.
6a0842b40441db5332a5290f941021636b1182761Sol Boucher * You may obtain a copy of the License at
7a0842b40441db5332a5290f941021636b1182761Sol Boucher *
8a0842b40441db5332a5290f941021636b1182761Sol Boucher *      http://www.apache.org/licenses/LICENSE-2.0
9a0842b40441db5332a5290f941021636b1182761Sol Boucher *
10a0842b40441db5332a5290f941021636b1182761Sol Boucher * Unless required by applicable law or agreed to in writing, software
11a0842b40441db5332a5290f941021636b1182761Sol Boucher * distributed under the License is distributed on an "AS IS" BASIS,
12a0842b40441db5332a5290f941021636b1182761Sol Boucher * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a0842b40441db5332a5290f941021636b1182761Sol Boucher * See the License for the specific language governing permissions and
14a0842b40441db5332a5290f941021636b1182761Sol Boucher * limitations under the License.
15a0842b40441db5332a5290f941021636b1182761Sol Boucher */
16a0842b40441db5332a5290f941021636b1182761Sol Boucher
17a0842b40441db5332a5290f941021636b1182761Sol Boucherpackage com.android.ex.camera2.portability;
18a0842b40441db5332a5290f941021636b1182761Sol Boucher
19a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.annotation.TargetApi;
20a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.content.Context;
21de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucherimport android.graphics.ImageFormat;
22f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucherimport android.graphics.Matrix;
23a97b7d1192e246a5f738991adca37cce282e1382Sol Boucherimport android.graphics.Rect;
24f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucherimport android.graphics.RectF;
25a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.graphics.SurfaceTexture;
26a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.hardware.camera2.CameraAccessException;
27a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.hardware.camera2.CameraCaptureSession;
28a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.hardware.camera2.CameraCharacteristics;
29a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.hardware.camera2.CameraDevice;
30a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.hardware.camera2.CameraManager;
31a97b7d1192e246a5f738991adca37cce282e1382Sol Boucherimport android.hardware.camera2.CaptureFailure;
32a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.hardware.camera2.CaptureRequest;
33a97b7d1192e246a5f738991adca37cce282e1382Sol Boucherimport android.hardware.camera2.CaptureResult;
34a97b7d1192e246a5f738991adca37cce282e1382Sol Boucherimport android.hardware.camera2.TotalCaptureResult;
35a97b7d1192e246a5f738991adca37cce282e1382Sol Boucherimport android.hardware.camera2.params.MeteringRectangle;
36de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucherimport android.media.Image;
37de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucherimport android.media.ImageReader;
38aa907a3b6637b4f95dbf572d0cf790a70ba3aeb0Sol Boucherimport android.media.MediaActionSound;
39a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.os.Build;
40a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.os.Handler;
41a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.os.HandlerThread;
42a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.os.Looper;
43a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.os.Message;
44a0842b40441db5332a5290f941021636b1182761Sol Boucherimport android.view.Surface;
45a0842b40441db5332a5290f941021636b1182761Sol Boucher
46a0842b40441db5332a5290f941021636b1182761Sol Boucherimport com.android.ex.camera2.portability.debug.Log;
47de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucherimport com.android.ex.camera2.utils.Camera2RequestSettingsSet;
48a0842b40441db5332a5290f941021636b1182761Sol Boucher
49de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucherimport java.nio.ByteBuffer;
50a0842b40441db5332a5290f941021636b1182761Sol Boucherimport java.util.ArrayList;
51a0842b40441db5332a5290f941021636b1182761Sol Boucherimport java.util.Arrays;
52a0842b40441db5332a5290f941021636b1182761Sol Boucherimport java.util.HashSet;
53a0842b40441db5332a5290f941021636b1182761Sol Boucherimport java.util.List;
54a0842b40441db5332a5290f941021636b1182761Sol Boucherimport java.util.Set;
55a0842b40441db5332a5290f941021636b1182761Sol Boucher
56a0842b40441db5332a5290f941021636b1182761Sol Boucher/**
57a0842b40441db5332a5290f941021636b1182761Sol Boucher * A class to implement {@link CameraAgent} of the Android camera2 framework.
58a0842b40441db5332a5290f941021636b1182761Sol Boucher */
59a0842b40441db5332a5290f941021636b1182761Sol Boucherclass AndroidCamera2AgentImpl extends CameraAgent {
60a0842b40441db5332a5290f941021636b1182761Sol Boucher    private static final Log.Tag TAG = new Log.Tag("AndCam2AgntImp");
61a0842b40441db5332a5290f941021636b1182761Sol Boucher
62a0842b40441db5332a5290f941021636b1182761Sol Boucher    private final Camera2Handler mCameraHandler;
63a0842b40441db5332a5290f941021636b1182761Sol Boucher    private final HandlerThread mCameraHandlerThread;
64a0842b40441db5332a5290f941021636b1182761Sol Boucher    private final CameraStateHolder mCameraState;
65a0842b40441db5332a5290f941021636b1182761Sol Boucher    private final DispatchThread mDispatchThread;
66a0842b40441db5332a5290f941021636b1182761Sol Boucher    private final CameraManager mCameraManager;
67aa907a3b6637b4f95dbf572d0cf790a70ba3aeb0Sol Boucher    private final MediaActionSound mNoisemaker;
68a0842b40441db5332a5290f941021636b1182761Sol Boucher
69a0842b40441db5332a5290f941021636b1182761Sol Boucher    /**
70a0842b40441db5332a5290f941021636b1182761Sol Boucher     * Number of camera devices.  The length of {@code mCameraDevices} does not reveal this
71a0842b40441db5332a5290f941021636b1182761Sol Boucher     * information because that list may contain since-invalidated indices.
72a0842b40441db5332a5290f941021636b1182761Sol Boucher     */
73a0842b40441db5332a5290f941021636b1182761Sol Boucher    private int mNumCameraDevices;
74a0842b40441db5332a5290f941021636b1182761Sol Boucher
75a0842b40441db5332a5290f941021636b1182761Sol Boucher    /**
76a0842b40441db5332a5290f941021636b1182761Sol Boucher     * Transformation between integral camera indices and the {@link java.lang.String} indices used
77a0842b40441db5332a5290f941021636b1182761Sol Boucher     * by the underlying API.  Note that devices may disappear because they've been disconnected or
78a0842b40441db5332a5290f941021636b1182761Sol Boucher     * have otherwise gone offline.  Because we need to keep the meanings of whatever indices we
79a0842b40441db5332a5290f941021636b1182761Sol Boucher     * expose stable, we cannot simply remove them in such a case; instead, we insert {@code null}s
80a0842b40441db5332a5290f941021636b1182761Sol Boucher     * to invalidate any such indices.  Whenever new devices appear, they are appended to the end of
81a0842b40441db5332a5290f941021636b1182761Sol Boucher     * the list, and thereby assigned the lowest index that has never yet been used.
82a0842b40441db5332a5290f941021636b1182761Sol Boucher     */
83a0842b40441db5332a5290f941021636b1182761Sol Boucher    private final List<String> mCameraDevices;
84a0842b40441db5332a5290f941021636b1182761Sol Boucher
85a0842b40441db5332a5290f941021636b1182761Sol Boucher    AndroidCamera2AgentImpl(Context context) {
86a0842b40441db5332a5290f941021636b1182761Sol Boucher        mCameraHandlerThread = new HandlerThread("Camera2 Handler Thread");
87a0842b40441db5332a5290f941021636b1182761Sol Boucher        mCameraHandlerThread.start();
88a0842b40441db5332a5290f941021636b1182761Sol Boucher        mCameraHandler = new Camera2Handler(mCameraHandlerThread.getLooper());
89a0842b40441db5332a5290f941021636b1182761Sol Boucher        mCameraState = new AndroidCamera2StateHolder();
90a0842b40441db5332a5290f941021636b1182761Sol Boucher        mDispatchThread = new DispatchThread(mCameraHandler, mCameraHandlerThread);
91a0842b40441db5332a5290f941021636b1182761Sol Boucher        mDispatchThread.start();
92a0842b40441db5332a5290f941021636b1182761Sol Boucher        mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
93aa907a3b6637b4f95dbf572d0cf790a70ba3aeb0Sol Boucher        mNoisemaker = new MediaActionSound();
94aa907a3b6637b4f95dbf572d0cf790a70ba3aeb0Sol Boucher        mNoisemaker.load(MediaActionSound.SHUTTER_CLICK);
95a0842b40441db5332a5290f941021636b1182761Sol Boucher
96a0842b40441db5332a5290f941021636b1182761Sol Boucher        mNumCameraDevices = 0;
97a0842b40441db5332a5290f941021636b1182761Sol Boucher        mCameraDevices = new ArrayList<String>();
98a0842b40441db5332a5290f941021636b1182761Sol Boucher        updateCameraDevices();
99a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
100a0842b40441db5332a5290f941021636b1182761Sol Boucher
101a0842b40441db5332a5290f941021636b1182761Sol Boucher    /**
102a0842b40441db5332a5290f941021636b1182761Sol Boucher     * Updates the camera device index assignments stored in {@link mCameraDevices}, without
103a0842b40441db5332a5290f941021636b1182761Sol Boucher     * reappropriating any currently-assigned index.
104a0842b40441db5332a5290f941021636b1182761Sol Boucher     * @return Whether the operation was successful
105a0842b40441db5332a5290f941021636b1182761Sol Boucher     */
106a0842b40441db5332a5290f941021636b1182761Sol Boucher    private boolean updateCameraDevices() {
107a0842b40441db5332a5290f941021636b1182761Sol Boucher        try {
108a0842b40441db5332a5290f941021636b1182761Sol Boucher            String[] currentCameraDevices = mCameraManager.getCameraIdList();
109a0842b40441db5332a5290f941021636b1182761Sol Boucher            Set<String> currentSet = new HashSet<String>(Arrays.asList(currentCameraDevices));
110a0842b40441db5332a5290f941021636b1182761Sol Boucher
111a0842b40441db5332a5290f941021636b1182761Sol Boucher            // Invalidate the indices assigned to any camera devices that are no longer present
112a0842b40441db5332a5290f941021636b1182761Sol Boucher            for (int index = 0; index < mCameraDevices.size(); ++index) {
113a0842b40441db5332a5290f941021636b1182761Sol Boucher                if (!currentSet.contains(mCameraDevices.get(index))) {
114a0842b40441db5332a5290f941021636b1182761Sol Boucher                    mCameraDevices.set(index, null);
115a0842b40441db5332a5290f941021636b1182761Sol Boucher                    --mNumCameraDevices;
116a0842b40441db5332a5290f941021636b1182761Sol Boucher                }
117a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
118a0842b40441db5332a5290f941021636b1182761Sol Boucher
119a0842b40441db5332a5290f941021636b1182761Sol Boucher            // Assign fresh indices to any new camera devices
120a0842b40441db5332a5290f941021636b1182761Sol Boucher            currentSet.removeAll(mCameraDevices); // The devices we didn't know about
121a0842b40441db5332a5290f941021636b1182761Sol Boucher            for (String device : currentCameraDevices) {
122a0842b40441db5332a5290f941021636b1182761Sol Boucher                if (currentSet.contains(device)) {
123a0842b40441db5332a5290f941021636b1182761Sol Boucher                    mCameraDevices.add(device);
124a0842b40441db5332a5290f941021636b1182761Sol Boucher                    ++mNumCameraDevices;
125a0842b40441db5332a5290f941021636b1182761Sol Boucher                }
126a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
127a0842b40441db5332a5290f941021636b1182761Sol Boucher
128a0842b40441db5332a5290f941021636b1182761Sol Boucher            return true;
129a0842b40441db5332a5290f941021636b1182761Sol Boucher        } catch (CameraAccessException ex) {
130a0842b40441db5332a5290f941021636b1182761Sol Boucher            Log.e(TAG, "Could not get device listing from camera subsystem", ex);
131a0842b40441db5332a5290f941021636b1182761Sol Boucher            return false;
132a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
133a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
134a0842b40441db5332a5290f941021636b1182761Sol Boucher
135a0842b40441db5332a5290f941021636b1182761Sol Boucher    // TODO: Implement
136a0842b40441db5332a5290f941021636b1182761Sol Boucher    @Override
137a0842b40441db5332a5290f941021636b1182761Sol Boucher    public void setCameraDefaultExceptionCallback(CameraExceptionCallback callback,
138a0842b40441db5332a5290f941021636b1182761Sol Boucher            Handler handler) {}
139a0842b40441db5332a5290f941021636b1182761Sol Boucher
140a0842b40441db5332a5290f941021636b1182761Sol Boucher    // TODO: Implement
141a0842b40441db5332a5290f941021636b1182761Sol Boucher    @Override
142a0842b40441db5332a5290f941021636b1182761Sol Boucher    public void recycle() {}
143a0842b40441db5332a5290f941021636b1182761Sol Boucher
144a0842b40441db5332a5290f941021636b1182761Sol Boucher    // TODO: Some indices may now be invalid; ensure everyone can handle that and update the docs
145a0842b40441db5332a5290f941021636b1182761Sol Boucher    @Override
146a0842b40441db5332a5290f941021636b1182761Sol Boucher    public CameraDeviceInfo getCameraDeviceInfo() {
147a0842b40441db5332a5290f941021636b1182761Sol Boucher        updateCameraDevices();
148a0842b40441db5332a5290f941021636b1182761Sol Boucher        return new AndroidCamera2DeviceInfo(mCameraManager, mCameraDevices.toArray(new String[0]),
149a0842b40441db5332a5290f941021636b1182761Sol Boucher                mNumCameraDevices);
150a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
151a0842b40441db5332a5290f941021636b1182761Sol Boucher
152a0842b40441db5332a5290f941021636b1182761Sol Boucher    @Override
153a0842b40441db5332a5290f941021636b1182761Sol Boucher    protected Handler getCameraHandler() {
154a0842b40441db5332a5290f941021636b1182761Sol Boucher        return mCameraHandler;
155a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
156a0842b40441db5332a5290f941021636b1182761Sol Boucher
157a0842b40441db5332a5290f941021636b1182761Sol Boucher    @Override
158a0842b40441db5332a5290f941021636b1182761Sol Boucher    protected DispatchThread getDispatchThread() {
159a0842b40441db5332a5290f941021636b1182761Sol Boucher        return mDispatchThread;
160a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
161a0842b40441db5332a5290f941021636b1182761Sol Boucher
162de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher    private static abstract class CaptureAvailableListener
163de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            extends CameraCaptureSession.CaptureListener
164de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            implements ImageReader.OnImageAvailableListener {};
165de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher
166a0842b40441db5332a5290f941021636b1182761Sol Boucher    private class Camera2Handler extends HistoryHandler {
167a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // Caller-provided when leaving CAMERA_UNOPENED state:
168a0842b40441db5332a5290f941021636b1182761Sol Boucher        private CameraOpenCallback mOpenCallback;
169a0842b40441db5332a5290f941021636b1182761Sol Boucher        private int mCameraIndex;
170a0842b40441db5332a5290f941021636b1182761Sol Boucher        private String mCameraId;
171a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
172a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // Available in CAMERA_UNCONFIGURED state and above:
173a0842b40441db5332a5290f941021636b1182761Sol Boucher        private CameraDevice mCamera;
174a0842b40441db5332a5290f941021636b1182761Sol Boucher        private AndroidCamera2ProxyImpl mCameraProxy;
175de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher        private Camera2RequestSettingsSet mPersistentSettings;
176a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        private Rect mActiveArray;
177de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher        private boolean mLegacyDevice;
178a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
179a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // Available in CAMERA_CONFIGURED state and above:
180a0842b40441db5332a5290f941021636b1182761Sol Boucher        private Size mPreviewSize;
181a0842b40441db5332a5290f941021636b1182761Sol Boucher        private Size mPhotoSize;
182a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
183a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // Available in PREVIEW_READY state and above:
184a0842b40441db5332a5290f941021636b1182761Sol Boucher        private SurfaceTexture mPreviewTexture;
185a0842b40441db5332a5290f941021636b1182761Sol Boucher        private Surface mPreviewSurface;
186a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        private CameraCaptureSession mSession;
187de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher        private ImageReader mCaptureReader;
188a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
189a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // Available from the beginning of PREVIEW_ACTIVE until the first preview frame arrives:
190a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        private CameraStartPreviewCallback mOneshotPreviewingCallback;
191a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
192a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // Available in FOCUS_LOCKED between AF trigger receipt and whenever the lens stops moving:
193a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        private CameraAFCallback mOneshotAfCallback;
194a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
195de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher        // Available when taking picture between AE trigger receipt and autoexposure convergence
196de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher        private CaptureAvailableListener mOneshotCaptureCallback;
197de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher
198a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // Available whenever setAutoFocusMoveCallback() was last invoked with a non-null argument:
199a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        private CameraAFMoveCallback mPassiveAfCallback;
200a0842b40441db5332a5290f941021636b1182761Sol Boucher
201984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher        // Gets reset on every state change
202984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher        private int mCurrentAeState = CaptureResult.CONTROL_AE_STATE_INACTIVE;
203984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
204a0842b40441db5332a5290f941021636b1182761Sol Boucher        Camera2Handler(Looper looper) {
205a0842b40441db5332a5290f941021636b1182761Sol Boucher            super(looper);
206a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
207a0842b40441db5332a5290f941021636b1182761Sol Boucher
208a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
209a0842b40441db5332a5290f941021636b1182761Sol Boucher        public void handleMessage(final Message msg) {
210a0842b40441db5332a5290f941021636b1182761Sol Boucher            super.handleMessage(msg);
211a0842b40441db5332a5290f941021636b1182761Sol Boucher            try {
212a0842b40441db5332a5290f941021636b1182761Sol Boucher                switch(msg.what) {
213a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.OPEN_CAMERA:
214a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.RECONNECT: {
215a0842b40441db5332a5290f941021636b1182761Sol Boucher                        CameraOpenCallback openCallback = (CameraOpenCallback) msg.obj;
216a0842b40441db5332a5290f941021636b1182761Sol Boucher                        int cameraIndex = msg.arg1;
217a0842b40441db5332a5290f941021636b1182761Sol Boucher
218772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher                        if (mCameraState.getState() > AndroidCamera2StateHolder.CAMERA_UNOPENED) {
219a0842b40441db5332a5290f941021636b1182761Sol Boucher                            openCallback.onDeviceOpenedAlready(cameraIndex,
220a0842b40441db5332a5290f941021636b1182761Sol Boucher                                    generateHistoryString(cameraIndex));
221a0842b40441db5332a5290f941021636b1182761Sol Boucher                            break;
222a0842b40441db5332a5290f941021636b1182761Sol Boucher                        }
223a0842b40441db5332a5290f941021636b1182761Sol Boucher
224a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mOpenCallback = openCallback;
225a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mCameraIndex = cameraIndex;
226a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mCameraId = mCameraDevices.get(mCameraIndex);
22750f5b019ba3f333a09a1beb9667fd7290082dc31Sol Boucher                        Log.i(TAG, String.format("Opening camera index %d (id %s) with camera2 API",
22850f5b019ba3f333a09a1beb9667fd7290082dc31Sol Boucher                                cameraIndex, mCameraId));
229a0842b40441db5332a5290f941021636b1182761Sol Boucher
230a0842b40441db5332a5290f941021636b1182761Sol Boucher                        if (mCameraId == null) {
231a0842b40441db5332a5290f941021636b1182761Sol Boucher                            mOpenCallback.onCameraDisabled(msg.arg1);
232a0842b40441db5332a5290f941021636b1182761Sol Boucher                            break;
233a0842b40441db5332a5290f941021636b1182761Sol Boucher                        }
234a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mCameraManager.openCamera(mCameraId, mCameraDeviceStateListener, this);
235a0842b40441db5332a5290f941021636b1182761Sol Boucher
236a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
237a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
238a0842b40441db5332a5290f941021636b1182761Sol Boucher
239a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.RELEASE: {
240a0842b40441db5332a5290f941021636b1182761Sol Boucher                        if (mCameraState.getState() == AndroidCamera2StateHolder.CAMERA_UNOPENED) {
241a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            Log.w(TAG, "Ignoring release at inappropriate time");
242a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            break;
243a0842b40441db5332a5290f941021636b1182761Sol Boucher                        }
244a0842b40441db5332a5290f941021636b1182761Sol Boucher
245a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        if (mSession != null) {
246a0842b40441db5332a5290f941021636b1182761Sol Boucher                            closePreviewSession();
247a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            mSession = null;
248a0842b40441db5332a5290f941021636b1182761Sol Boucher                        }
249a0842b40441db5332a5290f941021636b1182761Sol Boucher                        if (mCamera != null) {
250a0842b40441db5332a5290f941021636b1182761Sol Boucher                            mCamera.close();
251a0842b40441db5332a5290f941021636b1182761Sol Boucher                            mCamera = null;
252a0842b40441db5332a5290f941021636b1182761Sol Boucher                        }
253a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mCameraProxy = null;
254de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        mPersistentSettings = null;
255a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        mActiveArray = null;
256a0842b40441db5332a5290f941021636b1182761Sol Boucher                        if (mPreviewSurface != null) {
257a0842b40441db5332a5290f941021636b1182761Sol Boucher                            mPreviewSurface.release();
258a0842b40441db5332a5290f941021636b1182761Sol Boucher                            mPreviewSurface = null;
259a0842b40441db5332a5290f941021636b1182761Sol Boucher                        }
260a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mPreviewTexture = null;
261de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        if (mCaptureReader != null) {
262de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            mCaptureReader.close();
263de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            mCaptureReader = null;
264de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        }
265a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mPreviewSize = null;
266a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mPhotoSize = null;
267a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mCameraIndex = 0;
268a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mCameraId = null;
269984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        changeState(AndroidCamera2StateHolder.CAMERA_UNOPENED);
270a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
271a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
272a0842b40441db5332a5290f941021636b1182761Sol Boucher
273a0842b40441db5332a5290f941021636b1182761Sol Boucher                    /*case CameraActions.UNLOCK: {
274a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
275a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
276a0842b40441db5332a5290f941021636b1182761Sol Boucher
277a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.LOCK: {
278a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
279a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }*/
280a0842b40441db5332a5290f941021636b1182761Sol Boucher
281a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.SET_PREVIEW_TEXTURE_ASYNC: {
282a0842b40441db5332a5290f941021636b1182761Sol Boucher                        setPreviewTexture((SurfaceTexture) msg.obj);
283a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
284a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
285a0842b40441db5332a5290f941021636b1182761Sol Boucher
286a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.START_PREVIEW_ASYNC: {
287a0842b40441db5332a5290f941021636b1182761Sol Boucher                        if (mCameraState.getState() !=
288a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                        AndroidCamera2StateHolder.CAMERA_PREVIEW_READY) {
289a0842b40441db5332a5290f941021636b1182761Sol Boucher                            // TODO: Provide better feedback here?
290a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            Log.w(TAG, "Refusing to start preview at inappropriate time");
291a0842b40441db5332a5290f941021636b1182761Sol Boucher                            break;
292a0842b40441db5332a5290f941021636b1182761Sol Boucher                        }
293a0842b40441db5332a5290f941021636b1182761Sol Boucher
294a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        mOneshotPreviewingCallback = (CameraStartPreviewCallback) msg.obj;
295984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        changeState(AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE);
296a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        try {
297de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            mSession.setRepeatingRequest(
298de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                    mPersistentSettings.createRequest(mCamera,
299de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                            CameraDevice.TEMPLATE_PREVIEW, mPreviewSurface),
300984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    /*listener*/mCameraResultStateListener, /*handler*/this);
301a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        } catch(CameraAccessException ex) {
302a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            Log.w(TAG, "Unable to start preview", ex);
303984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            changeState(AndroidCamera2StateHolder.CAMERA_PREVIEW_READY);
304a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        }
305a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
306a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
307a0842b40441db5332a5290f941021636b1182761Sol Boucher
308a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.STOP_PREVIEW: {
309a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        if (mCameraState.getState() <
310a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                        AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE) {
311a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            Log.w(TAG, "Refusing to stop preview at inappropriate time");
312a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            break;
313a0842b40441db5332a5290f941021636b1182761Sol Boucher                        }
314a0842b40441db5332a5290f941021636b1182761Sol Boucher
315a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        mSession.stopRepeating();
316984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        changeState(AndroidCamera2StateHolder.CAMERA_PREVIEW_READY);
317a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
318a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
319a0842b40441db5332a5290f941021636b1182761Sol Boucher
320a0842b40441db5332a5290f941021636b1182761Sol Boucher                    /*case CameraActions.SET_PREVIEW_CALLBACK_WITH_BUFFER: {
321a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
322a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
323a0842b40441db5332a5290f941021636b1182761Sol Boucher
324a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.ADD_CALLBACK_BUFFER: {
325a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
326a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
327a0842b40441db5332a5290f941021636b1182761Sol Boucher
328a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.SET_PREVIEW_DISPLAY_ASYNC: {
329a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
330a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
331a0842b40441db5332a5290f941021636b1182761Sol Boucher
332a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.SET_PREVIEW_CALLBACK: {
333a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
334a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
335a0842b40441db5332a5290f941021636b1182761Sol Boucher
336a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.SET_ONE_SHOT_PREVIEW_CALLBACK: {
337a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
338a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
339a0842b40441db5332a5290f941021636b1182761Sol Boucher
340a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.SET_PARAMETERS: {
341a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
342a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
343a0842b40441db5332a5290f941021636b1182761Sol Boucher
344a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.GET_PARAMETERS: {
345a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
346a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
347a0842b40441db5332a5290f941021636b1182761Sol Boucher
348a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.REFRESH_PARAMETERS: {
349a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
350a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }*/
351a0842b40441db5332a5290f941021636b1182761Sol Boucher
352a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.APPLY_SETTINGS: {
353de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        AndroidCamera2Settings settings = (AndroidCamera2Settings) msg.obj;
354a0842b40441db5332a5290f941021636b1182761Sol Boucher                        applyToRequest(settings);
355a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
356a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
357a0842b40441db5332a5290f941021636b1182761Sol Boucher
358a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    case CameraActions.AUTO_FOCUS: {
359a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // We only support locking the focus while a preview is being displayed.
360a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // However, it can be requested multiple times in succession; the effect of
361a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // the subsequent invocations is determined by the focus mode defined in the
362a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // provided CameraSettings object. In passive (CONTINUOUS_*) mode, the
363a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // duplicate requests are no-ops and leave the lens locked at its current
364a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // position, but in active (AUTO) mode, they perform another scan and lock
365a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // once that is finished. In any manual focus mode, this call is a no-op,
366a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // and most notably, this is the only case where the callback isn't invoked.
367a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        if (mCameraState.getState() <
368a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                        AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE) {
369a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            Log.w(TAG, "Ignoring attempt to autofocus without preview");
370a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            break;
371a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        }
372a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
373a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // The earliest we can reliably tell whether the autofocus has locked in
374984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        // response to our latest request is when our one-time capture progresses.
375a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // However, it will probably take longer than that, so once that happens,
376a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // just start checking the repeating preview requests as they complete.
377a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        final CameraAFCallback callback = (CameraAFCallback) msg.obj;
378a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        CameraCaptureSession.CaptureListener deferredCallbackSetter =
379a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                new CameraCaptureSession.CaptureListener() {
380984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            private boolean mAlreadyDispatched = false;
381984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
382984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            @Override
383984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            public void onCaptureProgressed(CameraCaptureSession session,
384984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                                            CaptureRequest request,
385984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                                            CaptureResult result) {
386984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                checkAfState(result);
387984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            }
388984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
389a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            @Override
390a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            public void onCaptureCompleted(CameraCaptureSession session,
391a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                                           CaptureRequest request,
392a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                                           TotalCaptureResult result) {
393984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                checkAfState(result);
394984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            }
395984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
396984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            private void checkAfState(CaptureResult result) {
397984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                if (result.get(CaptureResult.CONTROL_AF_STATE) != null &&
398984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        !mAlreadyDispatched) {
399984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // Now our mCameraResultStateListener will invoke the callback
400984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // the first time it finds the focus motor to be locked.
401984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    mAlreadyDispatched = true;
402984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    mOneshotAfCallback = callback;
403984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // This is an optimization: check the AF state of this frame
404984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // instead of simply waiting for the next.
405984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    mCameraResultStateListener.monitorControlStates(result);
406984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                }
407a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            }
408a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
409a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            @Override
410a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            public void onCaptureFailed(CameraCaptureSession session,
411a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                                        CaptureRequest request,
412a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                                        CaptureFailure failure) {
413a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                Log.e(TAG, "Focusing failed with reason " + failure.getReason());
414a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                callback.onAutoFocus(false, mCameraProxy);
415a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            }};
416a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
417a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // Send a one-time capture to trigger the camera driver to lock focus.
418984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        changeState(AndroidCamera2StateHolder.CAMERA_FOCUS_LOCKED);
419de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        Camera2RequestSettingsSet trigger =
420de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                new Camera2RequestSettingsSet(mPersistentSettings);
421de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        trigger.set(CaptureRequest.CONTROL_AF_TRIGGER,
422a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                CaptureRequest.CONTROL_AF_TRIGGER_START);
423a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        try {
424de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            mSession.capture(
425de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                    trigger.createRequest(mCamera, CameraDevice.TEMPLATE_PREVIEW,
426de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                            mPreviewSurface),
427a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                    /*listener*/deferredCallbackSetter, /*handler*/ this);
428a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        } catch(CameraAccessException ex) {
429a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            Log.e(TAG, "Unable to lock autofocus", ex);
430984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            changeState(AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE);
431a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        }
432a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
433a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
434a0842b40441db5332a5290f941021636b1182761Sol Boucher
435a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.CANCEL_AUTO_FOCUS: {
436a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // Why would you want to unlock the lens if it isn't already locked?
437a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        if (mCameraState.getState() <
438a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE) {
439a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            Log.w(TAG, "Ignoring attempt to release focus lock without preview");
440a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            break;
441a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        }
442a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
443a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        // Send a one-time capture to trigger the camera driver to resume scanning.
444984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        changeState(AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE);
445de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        Camera2RequestSettingsSet cancel =
446de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                new Camera2RequestSettingsSet(mPersistentSettings);
447de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        cancel.set(CaptureRequest.CONTROL_AF_TRIGGER,
448a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
449a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        try {
450de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            mSession.capture(
451de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                    cancel.createRequest(mCamera, CameraDevice.TEMPLATE_PREVIEW,
452de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                            mPreviewSurface),
453a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                    /*listener*/null, /*handler*/this);
454a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        } catch(CameraAccessException ex) {
455a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            Log.e(TAG, "Unable to cancel autofocus", ex);
456984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            changeState(AndroidCamera2StateHolder.CAMERA_FOCUS_LOCKED);
457a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        }
458a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
459a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
460a0842b40441db5332a5290f941021636b1182761Sol Boucher
461a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.SET_AUTO_FOCUS_MOVE_CALLBACK: {
462a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        mPassiveAfCallback = (CameraAFMoveCallback) msg.obj;
463a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
464a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
465a0842b40441db5332a5290f941021636b1182761Sol Boucher
466a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    /*case CameraActions.SET_ZOOM_CHANGE_LISTENER: {
467a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
468a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
469a0842b40441db5332a5290f941021636b1182761Sol Boucher
470a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.SET_FACE_DETECTION_LISTENER: {
471a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
472a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
473a0842b40441db5332a5290f941021636b1182761Sol Boucher
474a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.START_FACE_DETECTION: {
475a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
476a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
477a0842b40441db5332a5290f941021636b1182761Sol Boucher
478a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.STOP_FACE_DETECTION: {
479a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
480a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
481a0842b40441db5332a5290f941021636b1182761Sol Boucher
482a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.SET_ERROR_CALLBACK: {
483a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
484a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
485a0842b40441db5332a5290f941021636b1182761Sol Boucher
486a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.ENABLE_SHUTTER_SOUND: {
487a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
488de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    }*/
489a0842b40441db5332a5290f941021636b1182761Sol Boucher
490a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.SET_DISPLAY_ORIENTATION: {
491de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        // Only set the JPEG capture orientation if requested to do so; otherwise,
492de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        // capture in the sensor's physical orientation
493de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        mPersistentSettings.set(CaptureRequest.JPEG_ORIENTATION, msg.arg2 > 0 ?
494de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                mCameraProxy.getCharacteristics().getJpegOrientation(msg.arg1) : 0);
495a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
496a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
497a0842b40441db5332a5290f941021636b1182761Sol Boucher
498a0842b40441db5332a5290f941021636b1182761Sol Boucher                    case CameraActions.CAPTURE_PHOTO: {
499de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        if (mCameraState.getState() <
500de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                        AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE) {
501de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            Log.e(TAG, "Photos may only be taken when a preview is active");
502de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            break;
503de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        }
504de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        if (mCameraState.getState() !=
505de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                AndroidCamera2StateHolder.CAMERA_FOCUS_LOCKED) {
506de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            Log.w(TAG, "Taking a (likely blurry) photo without the lens locked");
507de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        }
508de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher
509de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        final CaptureAvailableListener listener =
510de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                (CaptureAvailableListener) msg.obj;
511984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        if (mLegacyDevice ||
512984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                (mCurrentAeState == CaptureResult.CONTROL_AE_STATE_CONVERGED &&
513984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                !mPersistentSettings.matches(CaptureRequest.CONTROL_AE_MODE,
514984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH) &&
515984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                !mPersistentSettings.matches(CaptureRequest.FLASH_MODE,
516984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        CaptureRequest.FLASH_MODE_SINGLE)))
517984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                {
518984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            // Legacy devices don't support the precapture state keys and instead
519984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            // perform autoexposure convergence automatically upon capture.
520984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
521984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            // On other devices, as long as it has already converged, it determined
522984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            // that flash was not required, and we're not going to invalidate the
523984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            // current exposure levels by forcing the force on, we can save
524984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            // significant capture time by not forcing a recalculation.
525984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            Log.i(TAG, "Skipping pre-capture autoexposure convergence");
526de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            mCaptureReader.setOnImageAvailableListener(listener, /*handler*/this);
527de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            try {
528de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                mSession.capture(
529de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                        mPersistentSettings.createRequest(mCamera,
530de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                                CameraDevice.TEMPLATE_STILL_CAPTURE,
531de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                                mCaptureReader.getSurface()),
532de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                        listener, /*handler*/this);
533de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            } catch (CameraAccessException ex) {
534984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                Log.e(TAG, "Unable to initiate immediate capture", ex);
535de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            }
536de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        } else {
537984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            // We need to let AE converge before capturing. Once our one-time
538984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            // trigger capture has made it into the pipeline, we'll start checking
539984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            // for the completion of that convergence, capturing when that happens.
540984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            Log.i(TAG, "Forcing pre-capture autoexposure convergence");
541de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            CameraCaptureSession.CaptureListener deferredCallbackSetter =
542de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                    new CameraCaptureSession.CaptureListener() {
543984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                private boolean mAlreadyDispatched = false;
544984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
545984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                @Override
546984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                public void onCaptureProgressed(CameraCaptureSession session,
547984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                                                CaptureRequest request,
548984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                                                CaptureResult result) {
549984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    checkAeState(result);
550984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                }
551984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
552de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                @Override
553de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                public void onCaptureCompleted(CameraCaptureSession session,
554de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                                               CaptureRequest request,
555de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                                               TotalCaptureResult result) {
556984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    checkAeState(result);
557984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                }
558984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
559984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                private void checkAeState(CaptureResult result) {
560984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    if (result.get(CaptureResult.CONTROL_AE_STATE) != null &&
561984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                            !mAlreadyDispatched) {
562984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        // Now our mCameraResultStateListener will invoke the
563984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        // callback once the autoexposure routine has converged.
564984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        mAlreadyDispatched = true;
565984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        mOneshotCaptureCallback = listener;
566984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        // This is an optimization: check the AE state of this frame
567984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        // instead of simply waiting for the next.
568984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        mCameraResultStateListener.monitorControlStates(result);
569984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    }
570de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                }
571de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher
572de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                @Override
573de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                public void onCaptureFailed(CameraCaptureSession session,
574de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                                            CaptureRequest request,
575de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                                            CaptureFailure failure) {
576de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                    Log.e(TAG, "Autoexposure and capture failed with reason " +
577de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                            failure.getReason());
578de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                    // TODO: Make an error callback?
579de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                }};
580de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher
581de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            // Set a one-time capture to trigger the camera driver's autoexposure:
582de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            Camera2RequestSettingsSet expose =
583de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                    new Camera2RequestSettingsSet(mPersistentSettings);
584de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            expose.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
585de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                    CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
586de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            try {
587de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                mSession.capture(
588de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                        expose.createRequest(mCamera, CameraDevice.TEMPLATE_PREVIEW,
589de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                                mPreviewSurface),
590de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                        /*listener*/deferredCallbackSetter, /*handler*/this);
591de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            } catch (CameraAccessException ex) {
592de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                Log.e(TAG, "Unable to run autoexposure and perform capture", ex);
593de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            }
594de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        }
595a0842b40441db5332a5290f941021636b1182761Sol Boucher                        break;
596de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    }
597a0842b40441db5332a5290f941021636b1182761Sol Boucher
598a0842b40441db5332a5290f941021636b1182761Sol Boucher                    default: {
599a0842b40441db5332a5290f941021636b1182761Sol Boucher                        // TODO: Rephrase once everything has been implemented
600a0842b40441db5332a5290f941021636b1182761Sol Boucher                        throw new RuntimeException("Unimplemented CameraProxy message=" + msg.what);
601a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
602a0842b40441db5332a5290f941021636b1182761Sol Boucher                }
603a0842b40441db5332a5290f941021636b1182761Sol Boucher            } catch (final Exception ex) {
604a0842b40441db5332a5290f941021636b1182761Sol Boucher                if (msg.what != CameraActions.RELEASE && mCamera != null) {
605a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    // TODO: Handle this better
606a0842b40441db5332a5290f941021636b1182761Sol Boucher                    mCamera.close();
607a0842b40441db5332a5290f941021636b1182761Sol Boucher                    mCamera = null;
608a0842b40441db5332a5290f941021636b1182761Sol Boucher                } else if (mCamera == null) {
609a0842b40441db5332a5290f941021636b1182761Sol Boucher                    if (msg.what == CameraActions.OPEN_CAMERA) {
610a0842b40441db5332a5290f941021636b1182761Sol Boucher                        if (mOpenCallback != null) {
611a0842b40441db5332a5290f941021636b1182761Sol Boucher                            mOpenCallback.onDeviceOpenFailure(mCameraIndex,
612a0842b40441db5332a5290f941021636b1182761Sol Boucher                                    generateHistoryString(mCameraIndex));
613a0842b40441db5332a5290f941021636b1182761Sol Boucher                        }
614a0842b40441db5332a5290f941021636b1182761Sol Boucher                    } else {
615a0842b40441db5332a5290f941021636b1182761Sol Boucher                        Log.w(TAG, "Cannot handle message " + msg.what + ", mCamera is null");
616a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
617a0842b40441db5332a5290f941021636b1182761Sol Boucher                    return;
618a0842b40441db5332a5290f941021636b1182761Sol Boucher                }
619a0842b40441db5332a5290f941021636b1182761Sol Boucher
620a0842b40441db5332a5290f941021636b1182761Sol Boucher                if (ex instanceof RuntimeException) {
621a0842b40441db5332a5290f941021636b1182761Sol Boucher                    post(new Runnable() {
622a0842b40441db5332a5290f941021636b1182761Sol Boucher                        @Override
623a0842b40441db5332a5290f941021636b1182761Sol Boucher                        public void run() {
624a0842b40441db5332a5290f941021636b1182761Sol Boucher                            sCameraExceptionCallback.onCameraException((RuntimeException) ex);
625a0842b40441db5332a5290f941021636b1182761Sol Boucher                        }});
626a0842b40441db5332a5290f941021636b1182761Sol Boucher                }
627a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
628a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
629a0842b40441db5332a5290f941021636b1182761Sol Boucher
630a0842b40441db5332a5290f941021636b1182761Sol Boucher        public CameraSettings buildSettings(AndroidCamera2Capabilities caps) {
631de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            try {
632de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                return new AndroidCamera2Settings(mCamera, CameraDevice.TEMPLATE_PREVIEW,
633de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        mActiveArray, mPreviewSize, mPhotoSize);
634de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            } catch (CameraAccessException ex) {
635de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                Log.e(TAG, "Unable to query camera device to build settings representation");
636de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                return null;
637de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            }
638a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
639a0842b40441db5332a5290f941021636b1182761Sol Boucher
640a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        /**
641a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher         * Simply propagates settings from provided {@link CameraSettings}
642a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher         * object to our {@link CaptureRequest.Builder} for use in captures.
643a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher         * <p>Most conversions to match the API 2 formats are performed by
644a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher         * {@link AndroidCamera2Capabilities.IntegralStringifier}; otherwise
645a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher         * any final adjustments are done here before updating the builder.</p>
646a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher         *
647a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher         * @param settings The new/updated settings
648a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher         */
649de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher        private void applyToRequest(AndroidCamera2Settings settings) {
650a0842b40441db5332a5290f941021636b1182761Sol Boucher            // TODO: If invoked when in PREVIEW_READY state, a new preview size will not take effect
651de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher
652de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            mPersistentSettings.union(settings.getRequestSettings());
653a0842b40441db5332a5290f941021636b1182761Sol Boucher            mPreviewSize = settings.getCurrentPreviewSize();
654a0842b40441db5332a5290f941021636b1182761Sol Boucher            mPhotoSize = settings.getCurrentPhotoSize();
655a0842b40441db5332a5290f941021636b1182761Sol Boucher
656a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            if (mCameraState.getState() >= AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE) {
657a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                // If we're already previewing, reflect most settings immediately
658a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                try {
659de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    mSession.setRepeatingRequest(
660de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            mPersistentSettings.createRequest(mCamera,
661de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                    CameraDevice.TEMPLATE_PREVIEW, mPreviewSurface),
662984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            /*listener*/mCameraResultStateListener, /*handler*/this);
663a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                } catch (CameraAccessException ex) {
664a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    Log.e(TAG, "Failed to apply updated request settings", ex);
665a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                }
666a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            } else if (mCameraState.getState() < AndroidCamera2StateHolder.CAMERA_PREVIEW_READY) {
667a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                // If we're already ready to preview, this doesn't regress our state
668984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                changeState(AndroidCamera2StateHolder.CAMERA_CONFIGURED);
669a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
670a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
671a0842b40441db5332a5290f941021636b1182761Sol Boucher
672a0842b40441db5332a5290f941021636b1182761Sol Boucher        private void setPreviewTexture(SurfaceTexture surfaceTexture) {
673a0842b40441db5332a5290f941021636b1182761Sol Boucher            // TODO: Must be called after providing a .*Settings populated with sizes
674a0842b40441db5332a5290f941021636b1182761Sol Boucher            // TODO: We don't technically offer a selection of sizes tailored to SurfaceTextures!
675a0842b40441db5332a5290f941021636b1182761Sol Boucher
676a0842b40441db5332a5290f941021636b1182761Sol Boucher            // TODO: Handle this error condition with a callback or exception
677a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            if (mCameraState.getState() < AndroidCamera2StateHolder.CAMERA_CONFIGURED) {
678a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                Log.w(TAG, "Ignoring texture setting at inappropriate time");
679a0842b40441db5332a5290f941021636b1182761Sol Boucher                return;
680a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
681a0842b40441db5332a5290f941021636b1182761Sol Boucher
682a0842b40441db5332a5290f941021636b1182761Sol Boucher            // Avoid initializing another capture session unless we absolutely have to
683a0842b40441db5332a5290f941021636b1182761Sol Boucher            if (surfaceTexture == mPreviewTexture) {
684a0842b40441db5332a5290f941021636b1182761Sol Boucher                Log.i(TAG, "Optimizing out redundant preview texture setting");
685a0842b40441db5332a5290f941021636b1182761Sol Boucher                return;
686a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
687a0842b40441db5332a5290f941021636b1182761Sol Boucher
688a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            if (mSession != null) {
689a0842b40441db5332a5290f941021636b1182761Sol Boucher                closePreviewSession();
690a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
691a0842b40441db5332a5290f941021636b1182761Sol Boucher
692a0842b40441db5332a5290f941021636b1182761Sol Boucher            mPreviewTexture = surfaceTexture;
693a0842b40441db5332a5290f941021636b1182761Sol Boucher            surfaceTexture.setDefaultBufferSize(mPreviewSize.width(), mPreviewSize.height());
694a0842b40441db5332a5290f941021636b1182761Sol Boucher
695a0842b40441db5332a5290f941021636b1182761Sol Boucher            if (mPreviewSurface != null) {
696a0842b40441db5332a5290f941021636b1182761Sol Boucher                mPreviewSurface.release();
697a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
698a0842b40441db5332a5290f941021636b1182761Sol Boucher            mPreviewSurface = new Surface(surfaceTexture);
699de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher
700de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            if (mCaptureReader != null) {
701de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                mCaptureReader.close();
702de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            }
703de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            mCaptureReader = ImageReader.newInstance(
704de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    mPhotoSize.width(), mPhotoSize.height(), ImageFormat.JPEG, 1);
705a0842b40441db5332a5290f941021636b1182761Sol Boucher
706a0842b40441db5332a5290f941021636b1182761Sol Boucher            try {
707de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                mCamera.createCaptureSession(
708de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        Arrays.asList(mPreviewSurface, mCaptureReader.getSurface()),
709a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mCameraPreviewStateListener, this);
710a0842b40441db5332a5290f941021636b1182761Sol Boucher            } catch (CameraAccessException ex) {
711a0842b40441db5332a5290f941021636b1182761Sol Boucher                Log.e(TAG, "Failed to create camera capture session", ex);
712a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
713a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
714a0842b40441db5332a5290f941021636b1182761Sol Boucher
715a0842b40441db5332a5290f941021636b1182761Sol Boucher        private void closePreviewSession() {
716a0842b40441db5332a5290f941021636b1182761Sol Boucher            try {
717a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                mSession.abortCaptures();
718a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                mSession = null;
719a0842b40441db5332a5290f941021636b1182761Sol Boucher            } catch (CameraAccessException ex) {
720a0842b40441db5332a5290f941021636b1182761Sol Boucher                Log.e(TAG, "Failed to close existing camera capture session", ex);
721a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
722984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            changeState(AndroidCamera2StateHolder.CAMERA_CONFIGURED);
723984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher        }
724984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
725984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher        private void changeState(int newState) {
726984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            if (mCameraState.getState() != newState) {
727984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                mCameraState.setState(newState);
728984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                if (newState < AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE) {
729984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    mCurrentAeState = CaptureResult.CONTROL_AE_STATE_INACTIVE;
730984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    mCameraResultStateListener.resetState();
731984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                }
732984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            }
733a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
734a0842b40441db5332a5290f941021636b1182761Sol Boucher
735a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // This listener monitors our connection to and disconnection from camera devices.
736a0842b40441db5332a5290f941021636b1182761Sol Boucher        private CameraDevice.StateListener mCameraDeviceStateListener =
737a0842b40441db5332a5290f941021636b1182761Sol Boucher                new CameraDevice.StateListener() {
738a0842b40441db5332a5290f941021636b1182761Sol Boucher            @Override
739a0842b40441db5332a5290f941021636b1182761Sol Boucher            public void onOpened(CameraDevice camera) {
740a0842b40441db5332a5290f941021636b1182761Sol Boucher                mCamera = camera;
741a0842b40441db5332a5290f941021636b1182761Sol Boucher                if (mOpenCallback != null) {
742a0842b40441db5332a5290f941021636b1182761Sol Boucher                    try {
743a0842b40441db5332a5290f941021636b1182761Sol Boucher                        CameraCharacteristics props =
744a0842b40441db5332a5290f941021636b1182761Sol Boucher                                mCameraManager.getCameraCharacteristics(mCameraId);
745a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mCameraProxy = new AndroidCamera2ProxyImpl(mCameraIndex, mCamera,
746a0842b40441db5332a5290f941021636b1182761Sol Boucher                                    getCameraDeviceInfo().getCharacteristics(mCameraIndex), props);
747de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        mPersistentSettings = new Camera2RequestSettingsSet();
748a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        mActiveArray =
749a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                props.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
750de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        mLegacyDevice =
751de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                props.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) ==
752de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                        CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY;
753984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        changeState(AndroidCamera2StateHolder.CAMERA_UNCONFIGURED);
754a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mOpenCallback.onCameraOpened(mCameraProxy);
755a0842b40441db5332a5290f941021636b1182761Sol Boucher                    } catch (CameraAccessException ex) {
756a0842b40441db5332a5290f941021636b1182761Sol Boucher                        mOpenCallback.onDeviceOpenFailure(mCameraIndex,
757a0842b40441db5332a5290f941021636b1182761Sol Boucher                                generateHistoryString(mCameraIndex));
758a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
759a0842b40441db5332a5290f941021636b1182761Sol Boucher                }
760a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
761a0842b40441db5332a5290f941021636b1182761Sol Boucher
762a0842b40441db5332a5290f941021636b1182761Sol Boucher            @Override
763a0842b40441db5332a5290f941021636b1182761Sol Boucher            public void onDisconnected(CameraDevice camera) {
764a0842b40441db5332a5290f941021636b1182761Sol Boucher                Log.w(TAG, "Camera device '" + mCameraIndex + "' was disconnected");
765a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
766a0842b40441db5332a5290f941021636b1182761Sol Boucher
767a0842b40441db5332a5290f941021636b1182761Sol Boucher            @Override
768a0842b40441db5332a5290f941021636b1182761Sol Boucher            public void onError(CameraDevice camera, int error) {
769a0842b40441db5332a5290f941021636b1182761Sol Boucher                Log.e(TAG, "Camera device '" + mCameraIndex + "' encountered error code '" +
770a0842b40441db5332a5290f941021636b1182761Sol Boucher                        error + '\'');
771a0842b40441db5332a5290f941021636b1182761Sol Boucher                if (mOpenCallback != null) {
772a0842b40441db5332a5290f941021636b1182761Sol Boucher                    mOpenCallback.onDeviceOpenFailure(mCameraIndex,
773a0842b40441db5332a5290f941021636b1182761Sol Boucher                            generateHistoryString(mCameraIndex));
774a0842b40441db5332a5290f941021636b1182761Sol Boucher                }
775a0842b40441db5332a5290f941021636b1182761Sol Boucher            }};
776a0842b40441db5332a5290f941021636b1182761Sol Boucher
777a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // This listener monitors our camera session (i.e. our transition into and out of preview).
778a0842b40441db5332a5290f941021636b1182761Sol Boucher        private CameraCaptureSession.StateListener mCameraPreviewStateListener =
779a0842b40441db5332a5290f941021636b1182761Sol Boucher                new CameraCaptureSession.StateListener() {
780a0842b40441db5332a5290f941021636b1182761Sol Boucher            @Override
781a0842b40441db5332a5290f941021636b1182761Sol Boucher            public void onConfigured(CameraCaptureSession session) {
782a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                mSession = session;
783984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                changeState(AndroidCamera2StateHolder.CAMERA_PREVIEW_READY);
784a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
785a0842b40441db5332a5290f941021636b1182761Sol Boucher
786a0842b40441db5332a5290f941021636b1182761Sol Boucher            @Override
787a0842b40441db5332a5290f941021636b1182761Sol Boucher            public void onConfigureFailed(CameraCaptureSession session) {
788a0842b40441db5332a5290f941021636b1182761Sol Boucher                // TODO: Invoke a callback
789a0842b40441db5332a5290f941021636b1182761Sol Boucher                Log.e(TAG, "Failed to configure the camera for capture");
790a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            }
791a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
792a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            @Override
793a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            public void onActive(CameraCaptureSession session) {
794a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                if (mOneshotPreviewingCallback != null) {
795a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    // The session is up and processing preview requests. Inform the caller.
796a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    mOneshotPreviewingCallback.onPreviewStarted();
797a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    mOneshotPreviewingCallback = null;
798a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                }
799a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            }};
800a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
801984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher        private abstract class CameraResultStateListener
802984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                extends CameraCaptureSession.CaptureListener {
803984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            public abstract void monitorControlStates(CaptureResult result);
804984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
805984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            public abstract void resetState();
806984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher        }
807984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
808a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // This listener monitors requested captures and notifies any relevant callbacks.
809984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher        private CameraResultStateListener mCameraResultStateListener =
810984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                new CameraResultStateListener() {
811a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            private int mLastAfState = -1;
812984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            private long mLastAfFrameNumber = -1;
813984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            private long mLastAeFrameNumber = -1;
814984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
815984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            @Override
816984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
817984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                            CaptureResult result) {
818984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                monitorControlStates(result);
819984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            }
820a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
821a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            @Override
822a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
823a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                           TotalCaptureResult result) {
824984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                monitorControlStates(result);
825984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            }
826984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
827984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            @Override
828984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            public void monitorControlStates(CaptureResult result) {
829a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                Integer afStateMaybe = result.get(CaptureResult.CONTROL_AF_STATE);
830a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                if (afStateMaybe != null) {
831a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    int afState = afStateMaybe;
832984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    // Since we handle both partial and total results for multiple frames here, we
833984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    // might get the final callbacks for an earlier frame after receiving one or
834984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    // more that correspond to the next one. To prevent our data from oscillating,
835984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    // we never consider AF states that are older than the last one we've seen.
836984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    if (result.getFrameNumber() > mLastAfFrameNumber) {
837984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        boolean afStateChanged = afState != mLastAfState;
838a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        mLastAfState = afState;
839984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        mLastAfFrameNumber = result.getFrameNumber();
840984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
841984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        switch (afState) {
842984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            case CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN:
843984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            case CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED:
844984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            case CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED: {
845984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                if (afStateChanged && mPassiveAfCallback != null) {
846984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // A CameraAFMoveCallback is attached. If we just started to
847984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // scan, the motor is moving; otherwise, it has settled.
848984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    mPassiveAfCallback.onAutoFocusMoving(
849984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                            afState == CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN,
850984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                            mCameraProxy);
851984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                }
852984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                break;
853a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            }
854a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
855984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            case CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED:
856984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            case CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED: {
8577d71703202c2fba36653293eba2772745db46c00Sol Boucher                                // This check must be made regardless of whether the focus state has
8587d71703202c2fba36653293eba2772745db46c00Sol Boucher                                // changed recently to avoid infinite waiting during autoFocus()
8597d71703202c2fba36653293eba2772745db46c00Sol Boucher                                // when the algorithm has already either converged or failed to.
860984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                if (mOneshotAfCallback != null) {
861984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // A call to autoFocus() was just made to request a focus lock.
862984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // Notify the caller that the lens is now indefinitely fixed,
863984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // and report whether the image we're stuck with is in focus.
864984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    mOneshotAfCallback.onAutoFocus(
865984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                            afState == CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED,
866984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                            mCameraProxy);
867984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    mOneshotAfCallback = null;
868984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                }
869984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                break;
870a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            }
871a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        }
872a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    }
873a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                }
874de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher
875de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                Integer aeStateMaybe = result.get(CaptureResult.CONTROL_AE_STATE);
876de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                if (aeStateMaybe != null) {
877de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    int aeState = aeStateMaybe;
878984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    // Since we handle both partial and total results for multiple frames here, we
879984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    // might get the final callbacks for an earlier frame after receiving one or
880984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    // more that correspond to the next one. To prevent our data from oscillating,
881984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                    // we never consider AE states that are older than the last one we've seen.
8827d71703202c2fba36653293eba2772745db46c00Sol Boucher                    if (result.getFrameNumber() > mLastAeFrameNumber) {
883984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        mCurrentAeState = aeStateMaybe;
884984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        mLastAeFrameNumber = result.getFrameNumber();
885984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
886984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                        switch (aeState) {
887984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            case CaptureResult.CONTROL_AE_STATE_CONVERGED:
888984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            case CaptureResult.CONTROL_AE_STATE_FLASH_REQUIRED:
889984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                            case CaptureResult.CONTROL_AE_STATE_LOCKED: {
8907d71703202c2fba36653293eba2772745db46c00Sol Boucher                                // This check must be made regardless of whether the exposure state
8917d71703202c2fba36653293eba2772745db46c00Sol Boucher                                // has changed recently to avoid infinite waiting during
8927d71703202c2fba36653293eba2772745db46c00Sol Boucher                                // takePicture() when the algorithm has already converged.
893984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                if (mOneshotCaptureCallback != null) {
894984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // A call to takePicture() was just made, and autoexposure
895984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    // converged so it's time to initiate the capture!
896984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    mCaptureReader.setOnImageAvailableListener(
897de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                            /*listener*/mOneshotCaptureCallback,
898de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                            /*handler*/Camera2Handler.this);
899984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    try {
900984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        mSession.capture(
901984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                                mPersistentSettings.createRequest(mCamera,
902984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                                        CameraDevice.TEMPLATE_STILL_CAPTURE,
903984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                                        mCaptureReader.getSurface()),
904984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                                /*listener*/mOneshotCaptureCallback,
905984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                                /*handler*/Camera2Handler.this);
906984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    } catch (CameraAccessException ex) {
907984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        Log.e(TAG, "Unable to initiate capture", ex);
908984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    } finally {
909984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                        mOneshotCaptureCallback = null;
910984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                    }
911de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                }
912984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                                break;
913de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            }
914de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        }
915de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    }
916de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                }
917a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            }
918a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
919a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            @Override
920984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            public void resetState() {
921984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                mLastAfState = -1;
922984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                mLastAfFrameNumber = -1;
923984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher                mLastAeFrameNumber = -1;
924984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            }
925984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher
926984a086412a94ebea1bd9af8cd8bbf4afab38034Sol Boucher            @Override
927a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request,
928a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                        CaptureFailure failure) {
929a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                Log.e(TAG, "Capture attempt failed with reason " + failure.getReason());
930a0842b40441db5332a5290f941021636b1182761Sol Boucher            }};
931a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
932a0842b40441db5332a5290f941021636b1182761Sol Boucher
933a0842b40441db5332a5290f941021636b1182761Sol Boucher    private class AndroidCamera2ProxyImpl extends CameraAgent.CameraProxy {
934a0842b40441db5332a5290f941021636b1182761Sol Boucher        private final int mCameraIndex;
935a0842b40441db5332a5290f941021636b1182761Sol Boucher        private final CameraDevice mCamera;
936a0842b40441db5332a5290f941021636b1182761Sol Boucher        private final CameraDeviceInfo.Characteristics mCharacteristics;
937a0842b40441db5332a5290f941021636b1182761Sol Boucher        private final AndroidCamera2Capabilities mCapabilities;
93880cda8dae801f09c2ce928d0f3feb8a569939fc2Sol Boucher        private CameraSettings mLastSettings;
939415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher        private boolean mShutterSoundEnabled;
940a0842b40441db5332a5290f941021636b1182761Sol Boucher
941a0842b40441db5332a5290f941021636b1182761Sol Boucher        public AndroidCamera2ProxyImpl(int cameraIndex, CameraDevice camera,
942a0842b40441db5332a5290f941021636b1182761Sol Boucher                CameraDeviceInfo.Characteristics characteristics,
943a0842b40441db5332a5290f941021636b1182761Sol Boucher                CameraCharacteristics properties) {
944a0842b40441db5332a5290f941021636b1182761Sol Boucher            mCameraIndex = cameraIndex;
945a0842b40441db5332a5290f941021636b1182761Sol Boucher            mCamera = camera;
946a0842b40441db5332a5290f941021636b1182761Sol Boucher            mCharacteristics = characteristics;
947a0842b40441db5332a5290f941021636b1182761Sol Boucher            mCapabilities = new AndroidCamera2Capabilities(properties);
94880cda8dae801f09c2ce928d0f3feb8a569939fc2Sol Boucher            mLastSettings = null;
949415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher            mShutterSoundEnabled = true;
950a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
951a0842b40441db5332a5290f941021636b1182761Sol Boucher
952a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Implement
953a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
954a0842b40441db5332a5290f941021636b1182761Sol Boucher        public android.hardware.Camera getCamera() { return null; }
955a0842b40441db5332a5290f941021636b1182761Sol Boucher
956a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
957a0842b40441db5332a5290f941021636b1182761Sol Boucher        public int getCameraId() {
958a0842b40441db5332a5290f941021636b1182761Sol Boucher            return mCameraIndex;
959a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
960a0842b40441db5332a5290f941021636b1182761Sol Boucher
961a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
962a0842b40441db5332a5290f941021636b1182761Sol Boucher        public CameraDeviceInfo.Characteristics getCharacteristics() {
963a0842b40441db5332a5290f941021636b1182761Sol Boucher            return mCharacteristics;
964a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
965a0842b40441db5332a5290f941021636b1182761Sol Boucher
966a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
967a0842b40441db5332a5290f941021636b1182761Sol Boucher        public CameraCapabilities getCapabilities() {
968a0842b40441db5332a5290f941021636b1182761Sol Boucher            return mCapabilities;
969a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
970a0842b40441db5332a5290f941021636b1182761Sol Boucher
971a0842b40441db5332a5290f941021636b1182761Sol Boucher        private AndroidCamera2Capabilities getSpecializedCapabilities() {
972a0842b40441db5332a5290f941021636b1182761Sol Boucher            return mCapabilities;
973a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
974a0842b40441db5332a5290f941021636b1182761Sol Boucher
975a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Implement
976a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
977a0842b40441db5332a5290f941021636b1182761Sol Boucher        public void setPreviewDataCallback(Handler handler, CameraPreviewDataCallback cb) {}
978a0842b40441db5332a5290f941021636b1182761Sol Boucher
979a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Implement
980a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
981a0842b40441db5332a5290f941021636b1182761Sol Boucher        public void setOneShotPreviewCallback(Handler handler, CameraPreviewDataCallback cb) {}
982a0842b40441db5332a5290f941021636b1182761Sol Boucher
983a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Implement
984a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
985a0842b40441db5332a5290f941021636b1182761Sol Boucher        public void setPreviewDataCallbackWithBuffer(Handler handler, CameraPreviewDataCallback cb)
986a0842b40441db5332a5290f941021636b1182761Sol Boucher                {}
987a0842b40441db5332a5290f941021636b1182761Sol Boucher
98850f5b019ba3f333a09a1beb9667fd7290082dc31Sol Boucher        // TODO: Implement
98950f5b019ba3f333a09a1beb9667fd7290082dc31Sol Boucher        public void addCallbackBuffer(final byte[] callbackBuffer) {}
99050f5b019ba3f333a09a1beb9667fd7290082dc31Sol Boucher
991a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
992a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        public void autoFocus(final Handler handler, final CameraAFCallback cb) {
993a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            mDispatchThread.runJob(new Runnable() {
994a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                @Override
995a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                public void run() {
996a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    CameraAFCallback cbForward = null;
997a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    if (cb != null) {
998a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        cbForward = new CameraAFCallback() {
999a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            @Override
1000a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            public void onAutoFocus(final boolean focused,
1001a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                                    final CameraProxy camera) {
1002a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                handler.post(new Runnable() {
1003a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                    @Override
1004a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                    public void run() {
1005a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                        cb.onAutoFocus(focused, camera);
1006a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                    }});
1007a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            }};
1008a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    }
1009a0842b40441db5332a5290f941021636b1182761Sol Boucher
1010a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    mCameraState.waitForStates(AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE |
1011a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            AndroidCamera2StateHolder.CAMERA_FOCUS_LOCKED);
1012a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    mCameraHandler.obtainMessage(CameraActions.AUTO_FOCUS, cbForward)
1013a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            .sendToTarget();
1014a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                }});
1015a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        }
1016a0842b40441db5332a5290f941021636b1182761Sol Boucher
1017a0842b40441db5332a5290f941021636b1182761Sol Boucher        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
1018a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1019a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        public void setAutoFocusMoveCallback(final Handler handler, final CameraAFMoveCallback cb) {
1020a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher            mDispatchThread.runJob(new Runnable() {
1021a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                @Override
1022a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                public void run() {
1023a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    CameraAFMoveCallback cbForward = null;
1024a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    if (cb != null) {
1025a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                        cbForward = new CameraAFMoveCallback() {
1026a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            @Override
1027a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            public void onAutoFocusMoving(final boolean moving,
1028a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                                          final CameraProxy camera) {
1029a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                handler.post(new Runnable() {
1030a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                    @Override
1031a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                    public void run() {
1032a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                        cb.onAutoFocusMoving(moving, camera);
1033a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                    }});
1034a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                                }};
1035a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    }
1036a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
1037a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                    mCameraHandler.obtainMessage(CameraActions.SET_AUTO_FOCUS_MOVE_CALLBACK,
1038a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                            cbForward).sendToTarget();
1039a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher                }});
1040a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        }
1041a0842b40441db5332a5290f941021636b1182761Sol Boucher
1042a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1043de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher        public void takePicture(final Handler handler,
1044de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                final CameraShutterCallback shutter,
1045a0842b40441db5332a5290f941021636b1182761Sol Boucher                                CameraPictureCallback raw,
1046a0842b40441db5332a5290f941021636b1182761Sol Boucher                                CameraPictureCallback postview,
1047de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                final CameraPictureCallback jpeg) {
1048de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            // TODO: We never call raw or postview
1049de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            final CaptureAvailableListener picListener =
1050de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    new CaptureAvailableListener() {
1051de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                @Override
1052de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request,
1053de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                             long timestamp) {
1054de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    if (shutter != null) {
1055de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        handler.post(new Runnable() {
1056de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            @Override
1057de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            public void run() {
1058415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher                                if (mShutterSoundEnabled) {
1059415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher                                    mNoisemaker.play(MediaActionSound.SHUTTER_CLICK);
1060415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher                                }
1061de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                shutter.onShutter(AndroidCamera2ProxyImpl.this);
1062de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            }});
1063de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    }
1064de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                }
1065a0842b40441db5332a5290f941021636b1182761Sol Boucher
1066de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                @Override
1067de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                public void onImageAvailable(ImageReader reader) {
1068de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    try (Image image = reader.acquireNextImage()) {
1069de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        if (jpeg != null) {
1070de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            ByteBuffer buffer = image.getPlanes()[0].getBuffer();
1071de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            final byte[] pixels = new byte[buffer.remaining()];
1072de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            buffer.get(pixels);
1073de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            handler.post(new Runnable() {
1074de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                @Override
1075de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                public void run() {
1076de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                    jpeg.onPictureTaken(pixels, AndroidCamera2ProxyImpl.this);
1077de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                                }});
1078de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                        }
1079de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    }
1080de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                }};
1081de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            mDispatchThread.runJob(new Runnable() {
1082de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                @Override
1083de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                public void run() {
1084772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher                    // Wait until PREVIEW_ACTIVE or better
1085772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher                    mCameraState.waitForStates(
1086772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher                            ~(AndroidCamera2StateHolder.CAMERA_PREVIEW_ACTIVE - 1));
1087de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                    mCameraHandler.obtainMessage(CameraActions.CAPTURE_PHOTO, picListener)
1088de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                            .sendToTarget();
1089de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                }});
1090de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher        }
1091a0842b40441db5332a5290f941021636b1182761Sol Boucher
1092a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Implement
1093a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1094a0842b40441db5332a5290f941021636b1182761Sol Boucher        public void setZoomChangeListener(android.hardware.Camera.OnZoomChangeListener listener) {}
1095a0842b40441db5332a5290f941021636b1182761Sol Boucher
1096a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Implement
1097a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1098a0842b40441db5332a5290f941021636b1182761Sol Boucher        public void setFaceDetectionCallback(Handler handler, CameraFaceDetectionCallback callback)
1099a0842b40441db5332a5290f941021636b1182761Sol Boucher                {}
1100a0842b40441db5332a5290f941021636b1182761Sol Boucher
1101a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Remove this method override once we handle this message
1102a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1103a0842b40441db5332a5290f941021636b1182761Sol Boucher        public void startFaceDetection() {}
1104a0842b40441db5332a5290f941021636b1182761Sol Boucher
1105a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // TODO: Remove this method override once we handle this message
1106a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        @Override
1107a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        public void stopFaceDetection() {}
1108a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher
1109a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Implement
1110a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1111a0842b40441db5332a5290f941021636b1182761Sol Boucher        public void setErrorCallback(Handler handler, CameraErrorCallback cb) {}
1112a0842b40441db5332a5290f941021636b1182761Sol Boucher
1113a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Implement
1114a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1115a0842b40441db5332a5290f941021636b1182761Sol Boucher        public void setParameters(android.hardware.Camera.Parameters params) {}
1116a0842b40441db5332a5290f941021636b1182761Sol Boucher
1117a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Implement
1118a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1119a0842b40441db5332a5290f941021636b1182761Sol Boucher        public android.hardware.Camera.Parameters getParameters() { return null; }
1120a0842b40441db5332a5290f941021636b1182761Sol Boucher
1121a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1122a0842b40441db5332a5290f941021636b1182761Sol Boucher        public CameraSettings getSettings() {
112380cda8dae801f09c2ce928d0f3feb8a569939fc2Sol Boucher            if (mLastSettings == null) {
112480cda8dae801f09c2ce928d0f3feb8a569939fc2Sol Boucher                mLastSettings = mCameraHandler.buildSettings(mCapabilities);
112580cda8dae801f09c2ce928d0f3feb8a569939fc2Sol Boucher            }
112680cda8dae801f09c2ce928d0f3feb8a569939fc2Sol Boucher            return mLastSettings;
1127a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1128a0842b40441db5332a5290f941021636b1182761Sol Boucher
1129a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1130a0842b40441db5332a5290f941021636b1182761Sol Boucher        public boolean applySettings(CameraSettings settings) {
1131de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            if (settings == null) {
1132de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                Log.w(TAG, "null parameters in applySettings()");
1133de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                return false;
1134de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            }
1135de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            if (!(settings instanceof AndroidCamera2Settings)) {
1136de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                Log.e(TAG, "Provided settings not compatible with the backing framework API");
1137de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher                return false;
1138de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher            }
1139de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher
1140772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher            // Wait for any state that isn't OPENED
1141772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher            if (applySettingsHelper(settings, ~AndroidCamera2StateHolder.CAMERA_UNOPENED)) {
114280cda8dae801f09c2ce928d0f3feb8a569939fc2Sol Boucher                mLastSettings = settings;
114380cda8dae801f09c2ce928d0f3feb8a569939fc2Sol Boucher                return true;
114480cda8dae801f09c2ce928d0f3feb8a569939fc2Sol Boucher            }
114580cda8dae801f09c2ce928d0f3feb8a569939fc2Sol Boucher            return false;
1146a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1147a0842b40441db5332a5290f941021636b1182761Sol Boucher
1148415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher        @Override
1149415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher        public void enableShutterSound(boolean enable) {
1150415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher            mShutterSoundEnabled = enable;
1151415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher        }
1152415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher
1153a0842b40441db5332a5290f941021636b1182761Sol Boucher        // TODO: Implement
1154a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1155a0842b40441db5332a5290f941021636b1182761Sol Boucher        public String dumpDeviceSettings() { return null; }
1156a0842b40441db5332a5290f941021636b1182761Sol Boucher
1157a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1158a0842b40441db5332a5290f941021636b1182761Sol Boucher        public Handler getCameraHandler() {
1159a0842b40441db5332a5290f941021636b1182761Sol Boucher            return AndroidCamera2AgentImpl.this.getCameraHandler();
1160a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1161a0842b40441db5332a5290f941021636b1182761Sol Boucher
1162a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1163a0842b40441db5332a5290f941021636b1182761Sol Boucher        public DispatchThread getDispatchThread() {
1164a0842b40441db5332a5290f941021636b1182761Sol Boucher            return AndroidCamera2AgentImpl.this.getDispatchThread();
1165a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1166a0842b40441db5332a5290f941021636b1182761Sol Boucher
1167a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1168a0842b40441db5332a5290f941021636b1182761Sol Boucher        public CameraStateHolder getCameraState() {
1169a0842b40441db5332a5290f941021636b1182761Sol Boucher            return mCameraState;
1170a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1171a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
1172a0842b40441db5332a5290f941021636b1182761Sol Boucher
1173a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher    /** A linear state machine: each state entails all the states below it. */
1174a0842b40441db5332a5290f941021636b1182761Sol Boucher    private static class AndroidCamera2StateHolder extends CameraStateHolder {
1175a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        // Usage flow: openCamera() -> applySettings() -> setPreviewTexture() -> startPreview() ->
1176a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        //             autoFocus() -> takePicture()
1177772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher        // States are mutually exclusive, but must be separate bits so that they can be used with
1178772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher        // the StateHolder#waitForStates() and StateHolder#waitToAvoidStates() methods.
1179772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher        // Do not set the state to be a combination of these values!
1180a0842b40441db5332a5290f941021636b1182761Sol Boucher        /* Camera states */
1181a0842b40441db5332a5290f941021636b1182761Sol Boucher        /** No camera device is opened. */
1182772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher        public static final int CAMERA_UNOPENED = 1 << 0;
1183a0842b40441db5332a5290f941021636b1182761Sol Boucher        /** A camera is opened, but no settings have been provided. */
1184772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher        public static final int CAMERA_UNCONFIGURED = 1 << 1;
1185a0842b40441db5332a5290f941021636b1182761Sol Boucher        /** The open camera has been configured by providing it with settings. */
1186772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher        public static final int CAMERA_CONFIGURED = 1 << 2;
1187a0842b40441db5332a5290f941021636b1182761Sol Boucher        /** A capture session is ready to stream a preview, but still has no repeating request. */
1188772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher        public static final int CAMERA_PREVIEW_READY = 1 << 3;
1189a0842b40441db5332a5290f941021636b1182761Sol Boucher        /** A preview is currently being streamed. */
1190772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher        public static final int CAMERA_PREVIEW_ACTIVE = 1 << 4;
1191a97b7d1192e246a5f738991adca37cce282e1382Sol Boucher        /** The lens is locked on a particular region. */
1192772fcce3e51a9c7d33df6a9c278a908ac6902880Sol Boucher        public static final int CAMERA_FOCUS_LOCKED = 1 << 5;
1193a0842b40441db5332a5290f941021636b1182761Sol Boucher
1194a0842b40441db5332a5290f941021636b1182761Sol Boucher        public AndroidCamera2StateHolder() {
1195a0842b40441db5332a5290f941021636b1182761Sol Boucher            this(CAMERA_UNOPENED);
1196a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1197a0842b40441db5332a5290f941021636b1182761Sol Boucher
1198a0842b40441db5332a5290f941021636b1182761Sol Boucher        public AndroidCamera2StateHolder(int state) {
11997e0d39bf7b6e0f0df606e3f6c15f673f70fed3f7Sol Boucher            super(state);
1200a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1201a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
1202a0842b40441db5332a5290f941021636b1182761Sol Boucher
1203a0842b40441db5332a5290f941021636b1182761Sol Boucher    private static class AndroidCamera2DeviceInfo implements CameraDeviceInfo {
1204a0842b40441db5332a5290f941021636b1182761Sol Boucher        private final CameraManager mCameraManager;
1205a0842b40441db5332a5290f941021636b1182761Sol Boucher        private final String[] mCameraIds;
1206a0842b40441db5332a5290f941021636b1182761Sol Boucher        private final int mNumberOfCameras;
1207a0842b40441db5332a5290f941021636b1182761Sol Boucher        private final int mFirstBackCameraId;
1208a0842b40441db5332a5290f941021636b1182761Sol Boucher        private final int mFirstFrontCameraId;
1209a0842b40441db5332a5290f941021636b1182761Sol Boucher
1210a0842b40441db5332a5290f941021636b1182761Sol Boucher        public AndroidCamera2DeviceInfo(CameraManager cameraManager,
1211a0842b40441db5332a5290f941021636b1182761Sol Boucher                                        String[] cameraIds, int numberOfCameras) {
1212a0842b40441db5332a5290f941021636b1182761Sol Boucher            mCameraManager = cameraManager;
1213a0842b40441db5332a5290f941021636b1182761Sol Boucher            mCameraIds = cameraIds;
1214a0842b40441db5332a5290f941021636b1182761Sol Boucher            mNumberOfCameras = numberOfCameras;
1215a0842b40441db5332a5290f941021636b1182761Sol Boucher
1216a0842b40441db5332a5290f941021636b1182761Sol Boucher            int firstBackId = NO_DEVICE;
1217a0842b40441db5332a5290f941021636b1182761Sol Boucher            int firstFrontId = NO_DEVICE;
1218a0842b40441db5332a5290f941021636b1182761Sol Boucher            for (int id = 0; id < cameraIds.length; ++id) {
1219a0842b40441db5332a5290f941021636b1182761Sol Boucher                try {
1220a0842b40441db5332a5290f941021636b1182761Sol Boucher                    int lensDirection = cameraManager.getCameraCharacteristics(cameraIds[id])
1221a0842b40441db5332a5290f941021636b1182761Sol Boucher                            .get(CameraCharacteristics.LENS_FACING);
1222a0842b40441db5332a5290f941021636b1182761Sol Boucher                    if (firstBackId == NO_DEVICE &&
1223a0842b40441db5332a5290f941021636b1182761Sol Boucher                            lensDirection == CameraCharacteristics.LENS_FACING_BACK) {
1224a0842b40441db5332a5290f941021636b1182761Sol Boucher                        firstBackId = id;
1225a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
1226a0842b40441db5332a5290f941021636b1182761Sol Boucher                    if (firstFrontId == NO_DEVICE &&
1227a0842b40441db5332a5290f941021636b1182761Sol Boucher                            lensDirection == CameraCharacteristics.LENS_FACING_FRONT) {
1228a0842b40441db5332a5290f941021636b1182761Sol Boucher                        firstFrontId = id;
1229a0842b40441db5332a5290f941021636b1182761Sol Boucher                    }
1230a0842b40441db5332a5290f941021636b1182761Sol Boucher                } catch (CameraAccessException ex) {
1231a0842b40441db5332a5290f941021636b1182761Sol Boucher                    Log.w(TAG, "Couldn't get characteristics of camera '" + id + "'", ex);
1232a0842b40441db5332a5290f941021636b1182761Sol Boucher                }
1233a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
1234a0842b40441db5332a5290f941021636b1182761Sol Boucher            mFirstBackCameraId = firstBackId;
1235a0842b40441db5332a5290f941021636b1182761Sol Boucher            mFirstFrontCameraId = firstFrontId;
1236a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1237a0842b40441db5332a5290f941021636b1182761Sol Boucher
1238a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1239a0842b40441db5332a5290f941021636b1182761Sol Boucher        public Characteristics getCharacteristics(int cameraId) {
1240a0842b40441db5332a5290f941021636b1182761Sol Boucher            String actualId = mCameraIds[cameraId];
1241a0842b40441db5332a5290f941021636b1182761Sol Boucher            try {
1242a0842b40441db5332a5290f941021636b1182761Sol Boucher                CameraCharacteristics info = mCameraManager.getCameraCharacteristics(actualId);
1243a0842b40441db5332a5290f941021636b1182761Sol Boucher                return new AndroidCharacteristics2(info);
1244a0842b40441db5332a5290f941021636b1182761Sol Boucher            } catch (CameraAccessException ex) {
1245a0842b40441db5332a5290f941021636b1182761Sol Boucher                return null;
1246a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
1247a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1248a0842b40441db5332a5290f941021636b1182761Sol Boucher
1249a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1250a0842b40441db5332a5290f941021636b1182761Sol Boucher        public int getNumberOfCameras() {
1251a0842b40441db5332a5290f941021636b1182761Sol Boucher            return mNumberOfCameras;
1252a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1253a0842b40441db5332a5290f941021636b1182761Sol Boucher
1254a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1255a0842b40441db5332a5290f941021636b1182761Sol Boucher        public int getFirstBackCameraId() {
1256a0842b40441db5332a5290f941021636b1182761Sol Boucher            return mFirstBackCameraId;
1257a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1258a0842b40441db5332a5290f941021636b1182761Sol Boucher
1259a0842b40441db5332a5290f941021636b1182761Sol Boucher        @Override
1260a0842b40441db5332a5290f941021636b1182761Sol Boucher        public int getFirstFrontCameraId() {
1261a0842b40441db5332a5290f941021636b1182761Sol Boucher            return mFirstFrontCameraId;
1262a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1263a0842b40441db5332a5290f941021636b1182761Sol Boucher
1264de48004068f8c16f9a56c60b0ed2485a67687b4bSol Boucher        private static class AndroidCharacteristics2 extends Characteristics {
1265a0842b40441db5332a5290f941021636b1182761Sol Boucher            private CameraCharacteristics mCameraInfo;
1266a0842b40441db5332a5290f941021636b1182761Sol Boucher
1267a0842b40441db5332a5290f941021636b1182761Sol Boucher            AndroidCharacteristics2(CameraCharacteristics cameraInfo) {
1268a0842b40441db5332a5290f941021636b1182761Sol Boucher                mCameraInfo = cameraInfo;
1269a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
1270a0842b40441db5332a5290f941021636b1182761Sol Boucher
1271a0842b40441db5332a5290f941021636b1182761Sol Boucher            @Override
1272a0842b40441db5332a5290f941021636b1182761Sol Boucher            public boolean isFacingBack() {
1273a0842b40441db5332a5290f941021636b1182761Sol Boucher                return mCameraInfo.get(CameraCharacteristics.LENS_FACING)
1274a0842b40441db5332a5290f941021636b1182761Sol Boucher                        .equals(CameraCharacteristics.LENS_FACING_BACK);
1275a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
1276a0842b40441db5332a5290f941021636b1182761Sol Boucher
1277a0842b40441db5332a5290f941021636b1182761Sol Boucher            @Override
1278a0842b40441db5332a5290f941021636b1182761Sol Boucher            public boolean isFacingFront() {
1279a0842b40441db5332a5290f941021636b1182761Sol Boucher                return mCameraInfo.get(CameraCharacteristics.LENS_FACING)
1280a0842b40441db5332a5290f941021636b1182761Sol Boucher                        .equals(CameraCharacteristics.LENS_FACING_FRONT);
1281a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
1282a0842b40441db5332a5290f941021636b1182761Sol Boucher
1283a0842b40441db5332a5290f941021636b1182761Sol Boucher            @Override
1284a0842b40441db5332a5290f941021636b1182761Sol Boucher            public int getSensorOrientation() {
1285a0842b40441db5332a5290f941021636b1182761Sol Boucher                return mCameraInfo.get(CameraCharacteristics.SENSOR_ORIENTATION);
1286a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
1287a0842b40441db5332a5290f941021636b1182761Sol Boucher
1288a0842b40441db5332a5290f941021636b1182761Sol Boucher            @Override
1289f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher            public Matrix getPreviewTransform(int currentDisplayOrientation,
1290f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                                              RectF surfaceDimensions,
1291f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                                              RectF desiredBounds) {
1292f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                if (!orientationIsValid(currentDisplayOrientation)) {
1293f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                    return new Matrix();
1294f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                }
1295f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher
1296f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                // The system transparently transforms the image to fill the surface
1297f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                // when the device is in its natural orientation. We rotate the
1298f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                // coordinates of the rectangle's corners to be relative to the
1299f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                // original image, instead of to the current screen orientation.
1300f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                float[] surfacePolygon = rotate(convertRectToPoly(surfaceDimensions),
1301f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                        2 * currentDisplayOrientation / 90);
1302f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                float[] desiredPolygon = convertRectToPoly(desiredBounds);
1303f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher
1304f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                Matrix transform = new Matrix();
1305f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                // Use polygons instead of rectangles so that rotation will be
1306f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                // calculated, since that is not done by the new camera API.
1307f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                transform.setPolyToPoly(surfacePolygon, 0, desiredPolygon, 0, 4);
1308f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                return transform;
1309f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher            }
1310f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher
1311f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher            @Override
1312a0842b40441db5332a5290f941021636b1182761Sol Boucher            public boolean canDisableShutterSound() {
1313415cf3ee18640ad0799e5c9a738afd1d19c14883Sol Boucher                return true;
1314a0842b40441db5332a5290f941021636b1182761Sol Boucher            }
1315f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher
1316f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher            private static float[] convertRectToPoly(RectF rf) {
1317f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                return new float[] {rf.left, rf.top, rf.right, rf.top,
1318f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                        rf.right, rf.bottom, rf.left, rf.bottom};
1319f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher            }
1320f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher
1321f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher            private static float[] rotate(float[] arr, int times) {
1322f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                if (times < 0) {
1323f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                    times = times % arr.length + arr.length;
1324f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                }
1325f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher
1326f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                float[] res = new float[arr.length];
1327f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                for (int offset = 0; offset < arr.length; ++offset) {
1328f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                    res[offset] = arr[(times + offset) % arr.length];
1329f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                }
1330f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher                return res;
1331f9feab9a826e5b33d811e757bdfdbfa0738fcfa5Sol Boucher            }
1332a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
1333a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
1334a0842b40441db5332a5290f941021636b1182761Sol Boucher
1335a0842b40441db5332a5290f941021636b1182761Sol Boucher    private static final CameraExceptionCallback sCameraExceptionCallback =
1336a0842b40441db5332a5290f941021636b1182761Sol Boucher            new CameraExceptionCallback() {
1337a0842b40441db5332a5290f941021636b1182761Sol Boucher                @Override
1338a0842b40441db5332a5290f941021636b1182761Sol Boucher                public synchronized void onCameraException(RuntimeException e) {
1339a0842b40441db5332a5290f941021636b1182761Sol Boucher                    throw e;
1340a0842b40441db5332a5290f941021636b1182761Sol Boucher                }
1341a0842b40441db5332a5290f941021636b1182761Sol Boucher            };
1342a0842b40441db5332a5290f941021636b1182761Sol Boucher}
1343