aboutsummaryrefslogtreecommitdiff
path: root/apps/TestingCamera2/src/com/android/testingcamera2/CameraControlPane.java
blob: b4c05011e230d82d80f2fbfcab3ef4db99106e18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.testingcamera2;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCaptureSession.CaptureCallback;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.CaptureFailure;
import android.hardware.camera2.TotalCaptureResult;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.android.testingcamera2.PaneTracker.PaneEvent;

import java.io.IOException;

/**
 *
 * Basic control pane block for the control list
 *
 */
public class CameraControlPane extends ControlPane {

    // XML attributes

    /** Name of pane tag */
    private static final String PANE_NAME = "camera_pane";

    /** Attribute: ID for pane (integer) */
    private static final String PANE_ID = "id";
    /** Attribute: ID for camera to select (String) */
    private static final String CAMERA_ID = "camera_id";

    // End XML attributes

    private static final int MAX_CACHED_RESULTS = 100;

    private static int mCameraPaneIdCounter = 0;

    /**
     * These correspond to the callbacks from
     * android.hardware.camera2.CameraDevice.StateCallback, plus UNAVAILABLE for
     * when there's not a valid camera selected.
     */
    private enum CameraState {
        UNAVAILABLE,
        CLOSED,
        OPENED,
        DISCONNECTED,
        ERROR
    }

    /**
     * These correspond to the callbacks from {@link CameraCaptureSession.StateCallback}, plus
     * {@code CONFIGURING} for before a session is returned and {@code NONE} for when there
     * is no session created.
     */
    private enum SessionState {
        NONE,
        CONFIGURED,
        CONFIGURE_FAILED,
        READY,
        ACTIVE,
        CLOSED
    }

    private enum CameraCall {
        NONE,
        CONFIGURE
    }

    private final int mPaneId;

    private CameraOps2 mCameraOps;
    private InfoDisplayer mInfoDisplayer;

    private Spinner mCameraSpinner;
    private ToggleButton mOpenButton;
    private Button mInfoButton;
    private TextView mStatusText;
    private Button mConfigureButton;
    private Button mStopButton;
    private Button mFlushButton;

    /**
     * All controls that should be enabled when there's a valid camera ID
     * selected
     */
    private final Set<View> mBaseControls = new HashSet<View>();
    /**
     * All controls that should be enabled when camera is at least in the OPEN
     * state
     */
    private final Set<View> mOpenControls = new HashSet<View>();
    /**
     * All controls that should be enabled when camera is at least in the IDLE
     * state
     */
    private final Set<View> mConfiguredControls = new HashSet<View>();

    private String[] mCameraIds;
    private String mCurrentCameraId;

    private CameraState mCameraState;
    private CameraDevice mCurrentCamera;
    private CameraCaptureSession mCurrentCaptureSession;
    private SessionState mSessionState = SessionState.NONE;
    private CameraCall mActiveCameraCall;
    private LinkedList<TotalCaptureResult> mRecentResults = new LinkedList<>();

    private List<Surface> mConfiguredSurfaces;
    private List<TargetControlPane> mConfiguredTargetPanes;

    /**
     * Constructor for tooling only
     */
    public CameraControlPane(Context context, AttributeSet attrs) {
        super(context, attrs, null, null);

        mPaneId = 0;
        setUpUI(context);
    }

    public CameraControlPane(TestingCamera21 tc, AttributeSet attrs, StatusListener listener) {

        super(tc, attrs, listener, tc.getPaneTracker());

        mPaneId = mCameraPaneIdCounter++;
        setUpUI(tc);
        initializeCameras(tc);

        if (mCameraIds != null) {
            switchToCamera(mCameraIds[0]);
        }
    }

    public CameraControlPane(TestingCamera21 tc, XmlPullParser configParser, StatusListener listener)
            throws XmlPullParserException, IOException {
        super(tc, null, listener, tc.getPaneTracker());

        configParser.require(XmlPullParser.START_TAG, XmlPullParser.NO_NAMESPACE, PANE_NAME);

        int paneId = getAttributeInt(configParser, PANE_ID, -1);
        if (paneId == -1) {
            mPaneId = mCameraPaneIdCounter++;
        } else {
            mPaneId = paneId;
            if (mPaneId >= mCameraPaneIdCounter) {
                mCameraPaneIdCounter = mPaneId + 1;
            }
        }

        String cameraId = getAttributeString(configParser, CAMERA_ID, null);

        configParser.next();
        configParser.require(XmlPullParser.END_TAG, XmlPullParser.NO_NAMESPACE, PANE_NAME);

        setUpUI(tc);
        initializeCameras(tc);

        boolean gotCamera = false;
        if (mCameraIds != null && cameraId != null) {
            for (int i = 0; i < mCameraIds.length; i++) {
                if (cameraId.equals(mCameraIds[i])) {
                    switchToCamera(mCameraIds[i]);
                    mCameraSpinner.setSelection(i);
                    gotCamera = true;
                }
            }
        }

        if (!gotCamera && mCameraIds != null) {
            switchToCamera(mCameraIds[0]);
        }
    }

    @Override
    public void remove() {
        closeCurrentCamera();
        super.remove();
    }

    /**
     * Get list of target panes that are currently actively configured for this
     * camera
     */
    public List<TargetControlPane> getCurrentConfiguredTargets() {
        return mConfiguredTargetPanes;
    }

    /**
     * Interface to be implemented by an application service for displaying a
     * camera's information.
     */
    public interface InfoDisplayer {
        public void showCameraInfo(String cameraId);
    }

    public CameraCharacteristics getCharacteristics() {
        if (mCurrentCameraId != null) {
            return mCameraOps.getCameraInfo(mCurrentCameraId);
        }
        return null;
    }

    public CaptureRequest.Builder getRequestBuilder(int template) {
        CaptureRequest.Builder request = null;
        if (mCurrentCamera != null) {
            try {
                request = mCurrentCamera.createCaptureRequest(template);
                // Workaround for b/15748139
                request.set(CaptureRequest.STATISTICS_LENS_SHADING_MAP_MODE,
                        CaptureRequest.STATISTICS_LENS_SHADING_MAP_MODE_ON);
            } catch (CameraAccessException e) {
                TLog.e("Unable to build request for camera %s with template %d.", e,
                        mCurrentCameraId, template);
            }
        }
        return request;
    }

    /**
     * Send single capture to camera device.
     *
     * @param request
     * @return true if capture sent successfully
     */
    public boolean capture(CaptureRequest request) {
        if (mCurrentCaptureSession != null) {
            try {
                mCurrentCaptureSession.capture(request, mResultListener, null);
                return true;
            } catch (CameraAccessException e) {
                TLog.e("Unable to capture for camera %s.", e, mCurrentCameraId);
            }
        }
        return false;
    }

    public boolean repeat(CaptureRequest request) {
        if (mCurrentCaptureSession != null) {
            try {
                mCurrentCaptureSession.setRepeatingRequest(request, mResultListener, null);
                return true;
            } catch (CameraAccessException e) {
                TLog.e("Unable to set repeating request for camera %s.", e, mCurrentCameraId);
            }
        }
        return false;
    }

    public TotalCaptureResult getResultAt(long timestamp) {
        for (TotalCaptureResult result : mRecentResults) {
            long resultTimestamp = result.get(CaptureResult.SENSOR_TIMESTAMP);
            if (resultTimestamp == timestamp) return result;
            if (resultTimestamp > timestamp) return null;
        }
        return null;
    }

    public void prepareSurface(Surface target) {
        if (mCurrentCaptureSession != null) {
            try {
                TLog.i("Preparing Surface " + target);
                mCurrentCaptureSession.prepare(target);
            } catch (CameraAccessException e) {
                TLog.e("Unable to prepare surface for camera %s.", e, mCurrentCameraId);
            } catch (IllegalArgumentException e) {
                TLog.e("Bad Surface passed to prepare", e);
            }
        }
    }

    private CaptureCallback mResultListener = new CaptureCallback() {
        @Override
        public void onCaptureStarted(CameraCaptureSession session,
                CaptureRequest request, long timestamp, long frameNumber) {
        }

        @Override
        public void onCaptureProgressed(CameraCaptureSession session,
                CaptureRequest request, CaptureResult partialResult) {
        }

        @Override
        public void onCaptureCompleted(
                CameraCaptureSession session,
                CaptureRequest request,
                TotalCaptureResult result) {
            mRecentResults.add(result);
            if (mRecentResults.size() > MAX_CACHED_RESULTS) {
                mRecentResults.remove();
            }
        }

        @Override
        public void onCaptureFailed(CameraCaptureSession session,
                CaptureRequest request, CaptureFailure failure) {
            TLog.e("Capture failed for request " + request +
                    " on frame  " + failure.getFrameNumber() + ": Reason " + failure.getReason() +
                    ". Images captured: " + failure.wasImageCaptured());
        }

        @Override
        public void onCaptureSequenceCompleted(CameraCaptureSession session,
                int sequenceId, long frameNumber) {
        }

        @Override
        public void onCaptureSequenceAborted(CameraCaptureSession session,
                int sequenceId) {
        }

        @Override
        public void onCaptureBufferLost(CameraCaptureSession session,
                CaptureRequest request, Surface target, long frameNumber) {
            TLog.e("Lost buffer for Surface " + target + " for request " + request +
                    " on frame " + frameNumber);
        }

    };

    private void setUpUI(Context context) {
        String paneName =
                String.format(Locale.US, "%s %c",
                        context.getResources().getString(R.string.camera_pane_title),
                        (char) ('A' + mPaneId));
        this.setName(paneName);

        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        inflater.inflate(R.layout.camera_pane, this);

        mCameraSpinner = (Spinner) findViewById(R.id.camera_pane_camera_spinner);
        mCameraSpinner.setOnItemSelectedListener(mCameraSpinnerListener);

        mOpenButton = (ToggleButton) findViewById(R.id.camera_pane_open_button);
        mOpenButton.setOnCheckedChangeListener(mOpenButtonListener);
        mBaseControls.add(mOpenButton);

        mInfoButton = (Button) findViewById(R.id.camera_pane_info_button);
        mInfoButton.setOnClickListener(mInfoButtonListener);
        mBaseControls.add(mInfoButton);

        mStatusText = (TextView) findViewById(R.id.camera_pane_status_text);

        mConfigureButton = (Button) findViewById(R.id.camera_pane_configure_button);
        mConfigureButton.setOnClickListener(mConfigureButtonListener);
        mOpenControls.add(mConfigureButton);

        mStopButton = (Button) findViewById(R.id.camera_pane_stop_button);
        mStopButton.setOnClickListener(mStopButtonListener);
        mConfiguredControls.add(mStopButton);
        mFlushButton = (Button) findViewById(R.id.camera_pane_flush_button);
        mFlushButton.setOnClickListener(mFlushButtonListener);
        mConfiguredControls.add(mFlushButton);
    }

    private void initializeCameras(TestingCamera21 tc) {
        mCameraOps = tc.getCameraOps();
        mInfoDisplayer = tc;

        updateCameraList();
    }

    private void updateCameraList() {
        mCameraIds = null;
        try {
            mCameraIds = mCameraOps.getCamerasAndListen(mCameraAvailabilityCallback);
            String[] cameraSpinnerItems = new String[mCameraIds.length];
            for (int i = 0; i < mCameraIds.length; i++) {
                cameraSpinnerItems[i] = String.format("Camera %s", mCameraIds[i]);
            }
            mCameraSpinner.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.spinner_item,
                    cameraSpinnerItems));

        } catch (CameraAccessException e) {
            TLog.e("Exception trying to get list of cameras: " + e);
        }
    }

    private final CompoundButton.OnCheckedChangeListener mOpenButtonListener =
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        // Open camera
                        mCurrentCamera = null;
                        mCameraOps.openCamera(mCurrentCameraId, mCameraListener);
                    } else {
                        // Close camera
                        closeCurrentCamera();
                    }
                }
            };

    private final OnClickListener mInfoButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            mInfoDisplayer.showCameraInfo(mCurrentCameraId);
        }
    };

    private final OnClickListener mStopButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mCurrentCaptureSession != null) {
                try {
                    mCurrentCaptureSession.stopRepeating();
                } catch (CameraAccessException e) {
                    TLog.e("Unable to stop repeating request for camera %s.", e, mCurrentCameraId);
                }
            }
        }
    };

    private final OnClickListener mFlushButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mCurrentCaptureSession != null) {
                try {
                    mCurrentCaptureSession.abortCaptures();
                } catch (CameraAccessException e) {
                    TLog.e("Unable to flush camera %s.", e, mCurrentCameraId);
                }
            }
        }
    };

    private final OnClickListener mConfigureButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            List<Surface> targetSurfaces = new ArrayList<Surface>();
            List<TargetControlPane> targetPanes = new ArrayList<TargetControlPane>();
            for (TargetControlPane targetPane : mPaneTracker.getPanes(TargetControlPane.class)) {
                Surface target = targetPane.getTargetSurfaceForCameraPane(getPaneName());
                if (target != null) {
                    targetSurfaces.add(target);
                    targetPanes.add(targetPane);
                }
            }
            try {
                TLog.i("Configuring camera %s with %d surfaces", mCurrentCamera.getId(),
                        targetSurfaces.size());
                mActiveCameraCall = CameraCall.CONFIGURE;
                if (targetSurfaces.size() > 0) {
                    mCurrentCamera.createCaptureSession(targetSurfaces, mSessionListener, /*handler*/null);
                } else if (mCurrentCaptureSession != null) {
                    mCurrentCaptureSession.close();
                    mCurrentCaptureSession = null;
                }
                mConfiguredSurfaces = targetSurfaces;
                mConfiguredTargetPanes = targetPanes;
            } catch (CameraAccessException e) {
                mActiveCameraCall = CameraCall.NONE;
                TLog.e("Unable to configure camera %s.", e, mCurrentCamera.getId());
            } catch (IllegalArgumentException e) {
                mActiveCameraCall = CameraCall.NONE;
                TLog.e("Unable to configure camera %s.", e, mCurrentCamera.getId());
            } catch (IllegalStateException e) {
                mActiveCameraCall = CameraCall.NONE;
                TLog.e("Unable to configure camera %s.", e, mCurrentCamera.getId());
            }
        }
    };

    private final CameraCaptureSession.StateCallback mSessionListener =
            new CameraCaptureSession.StateCallback() {

        @Override
        public void onConfigured(CameraCaptureSession session) {
            mCurrentCaptureSession = session;
            TLog.i("Configuration completed for camera %s.", mCurrentCamera.getId());

            setSessionState(SessionState.CONFIGURED);
        }

        @Override
        public void onConfigureFailed(CameraCaptureSession session) {
            mActiveCameraCall = CameraCall.NONE;
            TLog.e("Configuration failed for camera %s.", mCurrentCamera.getId());

            setSessionState(SessionState.CONFIGURE_FAILED);
        }

        @Override
        public void onReady(CameraCaptureSession session) {
            setSessionState(SessionState.READY);
        }

        /**
         * This method is called when the session starts actively processing capture requests.
         *
         * <p>If capture requests are submitted prior to {@link #onConfigured} being called,
         * then the session will start processing those requests immediately after the callback,
         * and this method will be immediately called after {@link #onConfigured}.
         *
         * <p>If the session runs out of capture requests to process and calls {@link #onReady},
         * then this callback will be invoked again once new requests are submitted for capture.</p>
         */
        @Override
        public void onActive(CameraCaptureSession session) {
            setSessionState(SessionState.ACTIVE);
        }

        /**
         * This method is called when the session is closed.
         *
         * <p>A session is closed when a new session is created by the parent camera device,
         * or when the parent camera device is closed (either by the user closing the device,
         * or due to a camera device disconnection or fatal error).</p>
         *
         * <p>Once a session is closed, all methods on it will throw an IllegalStateException, and
         * any repeating requests or bursts are stopped (as if {@link #stopRepeating()} was called).
         * However, any in-progress capture requests submitted to the session will be completed
         * as normal.</p>
         */
        @Override
        public void onClosed(CameraCaptureSession session) {
            // Ignore closes if the session has been replaced
            if (mCurrentCaptureSession != null && session != mCurrentCaptureSession) {
                return;
            }
            setSessionState(SessionState.CLOSED);
        }

        @Override
        public void onSurfacePrepared(CameraCaptureSession session, Surface surface) {
            TLog.i("Surface preparation complete for Surface " + surface);
        }

    };

    private final CameraDevice.StateCallback mCameraListener = new CameraDevice.StateCallback() {
        @Override
        public void onClosed(CameraDevice camera) {
            // Don't change state on close, tracked by callers of close()
            mOpenButton.setChecked(false);
        }

        @Override
        public void onDisconnected(CameraDevice camera) {
            setCameraState(CameraState.DISCONNECTED);
        }

        @Override
        public void onError(CameraDevice camera, int error) {
            setCameraState(CameraState.ERROR);
        }

        @Override
        public void onOpened(CameraDevice camera) {
            mCurrentCamera = camera;
            setCameraState(CameraState.OPENED);
        }
    };

    private void switchToCamera(String newCameraId) {
        closeCurrentCamera();

        mCurrentCameraId = newCameraId;

        if (mCurrentCameraId == null) {
            setCameraState(CameraState.UNAVAILABLE);
        } else {
            setCameraState(CameraState.CLOSED);
        }

        mPaneTracker.notifyOtherPanes(this, PaneTracker.PaneEvent.NEW_CAMERA_SELECTED);
    }

    private void closeCurrentCamera() {
        if (mCurrentCamera != null) {
            mCurrentCamera.close();
            mCurrentCamera = null;
            setCameraState(CameraState.CLOSED);
        }
    }

    private void setSessionState(SessionState newState) {
        mSessionState = newState;
        mStatusText.setText("S." + mSessionState.toString());

        switch (mSessionState) {
            case CONFIGURE_FAILED:
                mActiveCameraCall = CameraCall.NONE;
                // fall-through
            case CLOSED:
                enableBaseControls(true);
                enableOpenControls(true);
                enableConfiguredControls(false);
                mConfiguredTargetPanes = null;
                break;
            case NONE:
                enableBaseControls(true);
                enableOpenControls(true);
                enableConfiguredControls(false);
                mConfiguredTargetPanes = null;
                break;
            case CONFIGURED:
                if (mActiveCameraCall != CameraCall.CONFIGURE) {
                    throw new AssertionError();
                }
                mPaneTracker.notifyOtherPanes(this, PaneEvent.CAMERA_CONFIGURED);
                mActiveCameraCall = CameraCall.NONE;
                // fall-through
            case READY:
            case ACTIVE:
                enableBaseControls(true);
                enableOpenControls(true);
                enableConfiguredControls(true);
                break;
            default:
                throw new AssertionError("Unhandled case " + mSessionState);
        }
    }

    private void setCameraState(CameraState newState) {
        mCameraState = newState;
        mStatusText.setText("C." + mCameraState.toString());
        switch (mCameraState) {
            case UNAVAILABLE:
                enableBaseControls(false);
                enableOpenControls(false);
                enableConfiguredControls(false);
                mConfiguredTargetPanes = null;
                break;
            case CLOSED:
            case DISCONNECTED:
            case ERROR:
                enableBaseControls(true);
                enableOpenControls(false);
                enableConfiguredControls(false);
                mConfiguredTargetPanes = null;
                break;
            case OPENED:
                enableBaseControls(true);
                enableOpenControls(true);
                enableConfiguredControls(false);
                mConfiguredTargetPanes = null;
                break;
        }
    }

    private void enableBaseControls(boolean enabled) {
        for (View v : mBaseControls) {
            v.setEnabled(enabled);
        }
        if (!enabled) {
            mOpenButton.setChecked(false);
        }
    }

    private void enableOpenControls(boolean enabled) {
        for (View v : mOpenControls) {
            v.setEnabled(enabled);
        }
    }

    private void enableConfiguredControls(boolean enabled) {
        for (View v : mConfiguredControls) {
            v.setEnabled(enabled);
        }
    }

    private final CameraManager.AvailabilityCallback mCameraAvailabilityCallback =
            new CameraManager.AvailabilityCallback() {
        @Override
        public void onCameraAvailable(String cameraId) {
            // TODO: Update camera list in an intelligent fashion
            // (can't just call updateCameraList or the selected camera may change)
        }

        @Override
        public void onCameraUnavailable(String cameraId) {
            // TODO: Update camera list in an intelligent fashion
            // (can't just call updateCameraList or the selected camera may change)
        }
    };

    private final OnItemSelectedListener mCameraSpinnerListener = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            String newCameraId = mCameraIds[pos];
            if (newCameraId != mCurrentCameraId) {
                switchToCamera(newCameraId);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            switchToCamera(null);
        }
    };
}