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.google.common.base.Optional;
20 
21 import com.android.camera.async.RefCountBase;
22 import com.android.camera.captureintent.CaptureIntentConfig;
23 import com.android.camera.captureintent.resource.ResourceConstructed;
24 import com.android.camera.captureintent.resource.ResourceSurfaceTexture;
25 import com.android.camera.captureintent.resource.ResourceSurfaceTextureImpl;
26 import com.android.camera.captureintent.resource.ResourceSurfaceTextureNexus4Impl;
27 import com.android.camera.captureintent.stateful.EventHandler;
28 import com.android.camera.captureintent.event.EventOnSurfaceTextureAvailable;
29 import com.android.camera.captureintent.event.EventResume;
30 import com.android.camera.captureintent.stateful.State;
31 import com.android.camera.captureintent.stateful.StateImpl;
32 import com.android.camera.captureintent.stateful.StateMachine;
33 
34 /**
35  * Represents a state that module is inactive in background. This is also the
36  * initial state of CaptureIntentModule.
37  */
38 public final class StateBackground extends StateImpl {
39     private final RefCountBase<ResourceConstructed> mResourceConstructed;
40 
create( StateMachine stateMachine, RefCountBase<ResourceConstructed> resourceConstructed)41     public static StateBackground create(
42             StateMachine stateMachine,
43             RefCountBase<ResourceConstructed> resourceConstructed) {
44         return new StateBackground(stateMachine, resourceConstructed);
45     }
46 
from( State previousState, RefCountBase<ResourceConstructed> resourceConstructed)47     public static StateBackground from(
48             State previousState,
49             RefCountBase<ResourceConstructed> resourceConstructed) {
50         return new StateBackground(previousState, resourceConstructed);
51     }
52 
StateBackground( StateMachine stateMachine, RefCountBase<ResourceConstructed> resourceConstructed)53     private StateBackground(
54             StateMachine stateMachine,
55             RefCountBase<ResourceConstructed> resourceConstructed) {
56         super(stateMachine);
57         mResourceConstructed = resourceConstructed;
58         mResourceConstructed.addRef();  // Will be balanced in onLeave().
59         registerEventHandlers();
60     }
61 
StateBackground(State previousState, RefCountBase<ResourceConstructed> resourceConstructed)62     private StateBackground(State previousState,
63             RefCountBase<ResourceConstructed> resourceConstructed) {
64         super(previousState);
65         mResourceConstructed = resourceConstructed;
66         mResourceConstructed.addRef();  // Will be balanced in onLeave().
67         registerEventHandlers();
68     }
69 
registerEventHandlers()70     private void registerEventHandlers() {
71         /** Handles EventResume. */
72         EventHandler<EventResume> resumeHandler = new EventHandler<EventResume>() {
73             @Override
74             public Optional<State> processEvent(EventResume eventResume) {
75                 return Optional.of((State) StateForeground.from(
76                         StateBackground.this, mResourceConstructed));
77             }
78         };
79         setEventHandler(EventResume.class, resumeHandler);
80 
81         /** Handles EventOnSurfaceTextureAvailable */
82         EventHandler<EventOnSurfaceTextureAvailable> onSurfaceTextureAvailableHandler =
83                 new EventHandler<EventOnSurfaceTextureAvailable>() {
84                     @Override
85                     public Optional<State> processEvent(EventOnSurfaceTextureAvailable event) {
86                         RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture;
87                         if (CaptureIntentConfig.WORKAROUND_PREVIEW_STRETCH_BUG_NEXUS4) {
88                             resourceSurfaceTexture = ResourceSurfaceTextureNexus4Impl.create(
89                                     mResourceConstructed,
90                                     event.getSurfaceTexture());
91                         } else {
92                             resourceSurfaceTexture = ResourceSurfaceTextureImpl.create(
93                                     mResourceConstructed,
94                                     event.getSurfaceTexture());
95                         }
96                         State nextState = StateBackgroundWithSurfaceTexture.from(
97                                 StateBackground.this,
98                                 mResourceConstructed,
99                                 resourceSurfaceTexture);
100                         // Release ResourceSurfaceTexture after it was handed
101                         // over to StateBackgroundWithSurfaceTexture.
102                         resourceSurfaceTexture.close();
103                         return Optional.of(nextState);
104                     }
105                 };
106         setEventHandler(EventOnSurfaceTextureAvailable.class, onSurfaceTextureAvailableHandler);
107     }
108 
109     @Override
onEnter()110     public Optional<State> onEnter() {
111         return NO_CHANGE;
112     }
113 
114     @Override
onLeave()115     public void onLeave() {
116         mResourceConstructed.close();
117     }
118 }
119