StateOpeningCamera.java revision 24be7cc6d138129b4087ef28f114701de54aba3c
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.camera.captureintent.state;
18
19import com.google.common.base.Optional;
20
21import com.android.camera.async.RefCountBase;
22import com.android.camera.burst.BurstFacadeFactory;
23import com.android.camera.debug.Log;
24import com.android.camera.one.OneCamera;
25import com.android.camera.one.OneCameraAccessException;
26import com.android.camera.one.OneCameraCharacteristics;
27import com.android.camera.one.v2.photo.ImageRotationCalculator;
28import com.android.camera.one.v2.photo.ImageRotationCalculatorImpl;
29import com.android.camera.util.Size;
30
31public final class StateOpeningCamera extends State {
32    private static final Log.Tag TAG = new Log.Tag("StateOpeningCamera");
33
34    private final RefCountBase<ResourceConstructed> mResourceConstructed;
35    private final RefCountBase<ResourceSurfaceTexture> mResourceSurfaceTexture;
36    private final OneCamera.Facing mCameraFacing;
37    private final OneCameraCharacteristics mCameraCharacteristics;
38
39    /** The desired picture size. */
40    private Size mPictureSize;
41
42    /** Whether is paused in the middle of opening camera. */
43    private boolean mIsPaused;
44
45    public static StateOpeningCamera from(
46            StateForegroundWithSurfaceTexture foregroundWithSurfaceTexture,
47            RefCountBase<ResourceConstructed> resourceConstructed,
48            RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture,
49            OneCamera.Facing cameraFacing,
50            OneCameraCharacteristics cameraCharacteristics) {
51        return new StateOpeningCamera(foregroundWithSurfaceTexture, resourceConstructed,
52                resourceSurfaceTexture, cameraFacing, cameraCharacteristics);
53    }
54
55    public static StateOpeningCamera from(
56            StateReadyForCapture readyForCapture,
57            RefCountBase<ResourceConstructed> resourceConstructed,
58            RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture) {
59        OneCamera.Facing cameraFacing =
60                resourceConstructed.get().getCameraFacingSetting().getCameraFacing();
61        OneCameraCharacteristics characteristics;
62        try {
63            characteristics =
64                    resourceConstructed.get().getCameraManager().getCameraCharacteristics(cameraFacing);
65        } catch (OneCameraAccessException ex) {
66            characteristics = null;
67        }
68        return new StateOpeningCamera(readyForCapture, resourceConstructed, resourceSurfaceTexture,
69                cameraFacing, characteristics);
70    }
71
72    private StateOpeningCamera(State previousState,
73            RefCountBase<ResourceConstructed> resourceConstructed,
74            RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture,
75            OneCamera.Facing cameraFacing,
76            OneCameraCharacteristics cameraCharacteristics) {
77        super(ID.OpeningCamera, previousState);
78        mResourceConstructed = resourceConstructed;
79        mResourceConstructed.addRef();
80        mResourceSurfaceTexture = resourceSurfaceTexture;
81        mResourceSurfaceTexture.addRef();
82        mCameraFacing = cameraFacing;
83        mCameraCharacteristics = cameraCharacteristics;
84        mIsPaused = false;
85    }
86
87    @Override
88    public Optional<State> onEnter() {
89        if (mCameraCharacteristics == null) {
90            Log.e(TAG, "mCameraCharacteristics is null");
91            return Optional.of((State) StateFatal.from(this, mResourceConstructed));
92        }
93
94        try {
95            /** Read the picture size from the setting. */
96            mPictureSize = mResourceConstructed.get().getResolutionSetting().getPictureSize(
97                    mCameraFacing);
98        } catch (OneCameraAccessException ex) {
99            Log.e(TAG, "Failed while open camera", ex);
100            return Optional.of((State) StateFatal.from(this, mResourceConstructed));
101        }
102
103        final ImageRotationCalculator imageRotationCalculator = ImageRotationCalculatorImpl.from(
104                mResourceConstructed.get().getOrientationManager(), mCameraCharacteristics);
105        mResourceConstructed.get().getCameraManager().open(
106                mCameraFacing,
107                false,
108                mPictureSize,
109                mResourceSurfaceTexture.get().getCameraOpenCallback(),
110                mResourceConstructed.get().getCameraHandler(),
111                mResourceConstructed.get().getMainThread(),
112                imageRotationCalculator,
113                new BurstFacadeFactory.BurstFacadeStub());
114        return Optional.absent();
115    }
116
117    @Override
118    public void onLeave() {
119        mResourceConstructed.close();
120        mResourceSurfaceTexture.close();
121    }
122
123    @Override
124    public Optional<State> processPause() {
125        mIsPaused = true;
126        return NO_CHANGE;
127    }
128
129    @Override
130    public Optional<State> processOnCameraOpened(
131            OneCamera camera,
132            OneCamera.CaptureReadyCallback captureReadyCallback) {
133        if (mIsPaused) {
134            // Just close the camera and finish.
135            camera.close();
136            return Optional.of((State) StateBackground.from(this, mResourceConstructed));
137        }
138        return Optional.of((State) StateStartingPreview.from(
139                this, mResourceConstructed, mResourceSurfaceTexture, camera, mCameraFacing,
140                mCameraCharacteristics, mPictureSize, captureReadyCallback));
141    }
142
143    @Override
144    public Optional<State> processOnCameraOpenFailure() {
145        Log.e(TAG, "processOnCameraOpenFailure");
146        return Optional.of((State) StateFatal.from(this, mResourceConstructed));
147    }
148}