Home | History | Annotate | Download | only in state
      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.annotations.VisibleForTesting;
     20 import com.google.common.base.Optional;
     21 
     22 import com.android.camera.async.RefCountBase;
     23 import com.android.camera.captureintent.resource.ResourceConstructed;
     24 import com.android.camera.captureintent.resource.ResourceSurfaceTexture;
     25 import com.android.camera.captureintent.stateful.EventHandler;
     26 import com.android.camera.captureintent.event.EventOnSurfaceTextureDestroyed;
     27 import com.android.camera.captureintent.event.EventResume;
     28 import com.android.camera.captureintent.stateful.State;
     29 import com.android.camera.captureintent.stateful.StateImpl;
     30 
     31 /**
     32  * Represents a state that module is inactive in background but surface texture
     33  * is available.
     34  * <p>
     35  * Module is in this state when first run dialog is still presented. The module
     36  * will be resumed after people finish first run dialog (b/19531554).
     37  */
     38 public class StateBackgroundWithSurfaceTexture extends StateImpl {
     39     private final RefCountBase<ResourceConstructed> mResourceConstructed;
     40     private final RefCountBase<ResourceSurfaceTexture> mResourceSurfaceTexture;
     41 
     42     /**
     43      * Used to transition from StateOpeningCamera, StateStartingPreview and
     44      * StateReadyForCapture on module got paused.
     45      */
     46     public static StateBackgroundWithSurfaceTexture from(
     47             State previousState,
     48             RefCountBase<ResourceConstructed> resourceConstructed,
     49             RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture) {
     50         return new StateBackgroundWithSurfaceTexture(
     51                 previousState, resourceConstructed, resourceSurfaceTexture);
     52     }
     53 
     54     private StateBackgroundWithSurfaceTexture(
     55             State previousState,
     56             RefCountBase<ResourceConstructed> resourceConstructed,
     57             RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture) {
     58         super(previousState);
     59         mResourceConstructed = resourceConstructed;
     60         mResourceConstructed.addRef();     // Will be balanced in onLeave().
     61         mResourceSurfaceTexture = resourceSurfaceTexture;
     62         mResourceSurfaceTexture.addRef();  // Will be balanced in onLeave().
     63         registerEventHandlers();
     64     }
     65 
     66     private void registerEventHandlers() {
     67         /** Handles EventResume. */
     68         EventHandler<EventResume> resumeHandler = new EventHandler<EventResume>() {
     69             @Override
     70             public Optional<State> processEvent(EventResume eventResume) {
     71                 return Optional.of((State) StateForegroundWithSurfaceTexture.from(
     72                         StateBackgroundWithSurfaceTexture.this,
     73                         mResourceConstructed,
     74                         mResourceSurfaceTexture));
     75             }
     76         };
     77         setEventHandler(EventResume.class, resumeHandler);
     78 
     79         /** Handles EventOnSurfaceTextureDestroyed. */
     80         EventHandler<EventOnSurfaceTextureDestroyed> surfaceTextureDestroyedHandler =
     81                 new EventHandler<EventOnSurfaceTextureDestroyed>() {
     82                     @Override
     83                     public Optional<State> processEvent(EventOnSurfaceTextureDestroyed event) {
     84                         return Optional.of((State) StateBackground.from(
     85                                 StateBackgroundWithSurfaceTexture.this, mResourceConstructed));
     86                     }
     87                 };
     88         setEventHandler(
     89                 EventOnSurfaceTextureDestroyed.class, surfaceTextureDestroyedHandler);
     90     }
     91 
     92     @Override
     93     public Optional<State> onEnter() {
     94         // Do nothing unless the module is resumed.
     95         return Optional.absent();
     96     }
     97 
     98     @Override
     99     public void onLeave() {
    100         mResourceConstructed.close();
    101         mResourceSurfaceTexture.close();
    102     }
    103 
    104     @VisibleForTesting
    105     public RefCountBase<ResourceSurfaceTexture> getResourceSurfaceTexture() {
    106         return mResourceSurfaceTexture;
    107     }
    108 }
    109