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 
17 package com.android.camera.captureintent.state;
18 
19 import com.android.camera.async.RefCountBase;
20 import com.android.camera.captureintent.resource.ResourceConstructed;
21 import com.android.camera.captureintent.resource.ResourceSurfaceTexture;
22 import com.android.camera.captureintent.stateful.State;
23 import com.android.camera.captureintent.stateful.StateImpl;
24 import com.android.camera.device.CameraId;
25 import com.android.camera.one.OneCamera;
26 import com.android.camera.one.OneCameraAccessException;
27 import com.android.camera.one.OneCameraCharacteristics;
28 import com.google.common.annotations.VisibleForTesting;
29 import com.google.common.base.Optional;
30 
31 /**
32  * Represents a state that the surface texture is available to the module.
33  */
34 public final class StateForegroundWithSurfaceTexture extends StateImpl {
35     private final RefCountBase<ResourceConstructed> mResourceConstructed;
36     private final RefCountBase<ResourceSurfaceTexture> mResourceSurfaceTexture;
37 
38     // Used to transition from BackgroundWithSurfaceTexture on module is resumed.
from( State previousState, RefCountBase<ResourceConstructed> resourceConstructed, RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture)39     public static StateForegroundWithSurfaceTexture from(
40             State previousState,
41             RefCountBase<ResourceConstructed> resourceConstructed,
42             RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture) {
43         return new StateForegroundWithSurfaceTexture(
44                 previousState,
45                 resourceConstructed,
46                 resourceSurfaceTexture);
47     }
48 
StateForegroundWithSurfaceTexture( State previousState, RefCountBase<ResourceConstructed> resourceConstructed, RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture)49     private StateForegroundWithSurfaceTexture(
50             State previousState,
51             RefCountBase<ResourceConstructed> resourceConstructed,
52             RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture) {
53         super(previousState);
54         mResourceConstructed = resourceConstructed;
55         mResourceConstructed.addRef();     // Will be balanced in onLeave().
56         mResourceSurfaceTexture = resourceSurfaceTexture;
57         mResourceSurfaceTexture.addRef();
58     }
59 
60     @Override
onEnter()61     public Optional<State> onEnter() {
62         try {
63             // Pick a preview size with the right aspect ratio.
64             final OneCamera.Facing cameraFacing =
65                     mResourceConstructed.get().getCameraFacingSetting().getCameraFacing();
66 
67             CameraId key = mResourceConstructed.get().getOneCameraManager()
68                   .findFirstCameraFacing(cameraFacing);
69 
70             final OneCameraCharacteristics characteristics =
71                     mResourceConstructed.get().getOneCameraManager().getOneCameraCharacteristics(
72                           key);
73             return Optional.of((State) StateOpeningCamera.from(this, mResourceConstructed,
74                     mResourceSurfaceTexture, cameraFacing, key, characteristics));
75         } catch (OneCameraAccessException ex) {
76             return Optional.of((State) StateFatal.from(this, mResourceConstructed));
77         }
78     }
79 
80     @Override
onLeave()81     public void onLeave() {
82         mResourceConstructed.close();
83         mResourceSurfaceTexture.close();
84     }
85 
86     @VisibleForTesting
getResourceSurfaceTexture()87     public RefCountBase<ResourceSurfaceTexture> getResourceSurfaceTexture() {
88         return mResourceSurfaceTexture;
89     }
90 }
91