Home | History | Annotate | Download | only in initialization
      1 /*
      2  * Copyright (C) 2014 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.one.v2.initialization;
     18 
     19 import android.hardware.camera2.CameraAccessException;
     20 import android.os.Handler;
     21 import android.view.Surface;
     22 
     23 import com.android.camera.one.v2.camera2proxy.CameraCaptureSessionProxy;
     24 import com.android.camera.one.v2.camera2proxy.CameraDeviceProxy;
     25 import com.google.common.util.concurrent.ListenableFuture;
     26 import com.google.common.util.concurrent.SettableFuture;
     27 
     28 import java.util.List;
     29 
     30 /**
     31  * Asynchronously creates capture sessions.
     32  */
     33 class CaptureSessionCreator {
     34     private final CameraDeviceProxy mDevice;
     35     private final Handler mCameraHandler;
     36 
     37     /**
     38      * @param device The device on which to create the capture session.
     39      * @param cameraHandler The handler on which to process capture session
     40      *            state callbacks.
     41      */
     42     public CaptureSessionCreator(CameraDeviceProxy device, Handler cameraHandler) {
     43         mDevice = device;
     44         mCameraHandler = cameraHandler;
     45     }
     46 
     47     /**
     48      * Asynchronously creates a capture session, returning a future to it.
     49      *
     50      * @param surfaces The set of output surfaces for the camera capture
     51      *            session.
     52      * @return A Future for the camera capture session.
     53      */
     54     public ListenableFuture<CameraCaptureSessionProxy> createCaptureSession(
     55             List<Surface> surfaces) {
     56         final SettableFuture<CameraCaptureSessionProxy> sessionFuture = SettableFuture.create();
     57         try {
     58             mDevice.createCaptureSession(surfaces, new CameraCaptureSessionProxy.StateCallback() {
     59                 @Override
     60                 public void onActive(CameraCaptureSessionProxy session) {
     61                     // Ignore.
     62                 }
     63 
     64                 @Override
     65                 public void onConfigureFailed(CameraCaptureSessionProxy session) {
     66                     sessionFuture.cancel(true);
     67                     session.close();
     68                 }
     69 
     70                 @Override
     71                 public void onConfigured(CameraCaptureSessionProxy session) {
     72                     boolean valueSet = sessionFuture.set(session);
     73                     if (!valueSet) {
     74                         // If the future was already marked with cancellation or
     75                         // an exception, close the session.
     76                         session.close();
     77                     }
     78                 }
     79 
     80                 @Override
     81                 public void onReady(CameraCaptureSessionProxy session) {
     82                     // Ignore.
     83                 }
     84 
     85                 @Override
     86                 public void onClosed(CameraCaptureSessionProxy session) {
     87                     sessionFuture.cancel(true);
     88                     session.close();
     89                 }
     90             }, mCameraHandler);
     91         } catch (CameraAccessException e) {
     92             sessionFuture.setException(e);
     93         }
     94         return sessionFuture;
     95     }
     96 }
     97