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.photo;
18 
19 import android.hardware.camera2.CameraAccessException;
20 
21 import com.android.camera.app.OrientationManager;
22 import com.android.camera.async.MainThread;
23 import com.android.camera.async.Updatable;
24 import com.android.camera.one.OneCamera;
25 import com.android.camera.one.v2.camera2proxy.CameraCaptureSessionClosedException;
26 import com.android.camera.one.v2.commands.CameraCommand;
27 import com.android.camera.one.v2.commands.CameraCommandExecutor;
28 import com.android.camera.one.v2.core.ResourceAcquisitionFailedException;
29 import com.android.camera.one.v2.imagesaver.ImageSaver;
30 import com.android.camera.session.CaptureSession;
31 import com.google.common.base.MoreObjects;
32 
33 class PictureTakerImpl implements PictureTaker {
34     private final MainThread mMainExecutor;
35     private final CameraCommandExecutor mCameraCommandExecutor;
36     private final ImageSaver.Builder mImageSaverBuilder;
37     private final ImageCaptureCommand mCommand;
38 
PictureTakerImpl(MainThread mainExecutor, CameraCommandExecutor cameraCommandExecutor, ImageSaver.Builder imageSaverBuilder, ImageCaptureCommand command)39     public PictureTakerImpl(MainThread mainExecutor, CameraCommandExecutor cameraCommandExecutor,
40             ImageSaver.Builder imageSaverBuilder, ImageCaptureCommand command) {
41         mMainExecutor = mainExecutor;
42         mCameraCommandExecutor = cameraCommandExecutor;
43         mImageSaverBuilder = imageSaverBuilder;
44         mCommand = command;
45     }
46 
47     private final class PictureTakerCommand implements CameraCommand {
48         private final Updatable<Void> mImageExposureCallback;
49         private final ImageSaver mImageSaver;
50         private final CaptureSession mSession;
51 
PictureTakerCommand(Updatable<Void> imageExposureCallback, ImageSaver imageSaver, CaptureSession session)52         private PictureTakerCommand(Updatable<Void> imageExposureCallback,
53                 ImageSaver imageSaver,
54                 CaptureSession session) {
55             mImageExposureCallback = imageExposureCallback;
56             mImageSaver = imageSaver;
57             mSession = session;
58         }
59 
60         @Override
run()61         public void run() throws InterruptedException, CameraAccessException,
62                 CameraCaptureSessionClosedException, ResourceAcquisitionFailedException {
63             try {
64                 mCommand.run(mImageExposureCallback, mImageSaver);
65             } catch (Exception e) {
66                 mSession.cancel();
67                 throw e;
68             }
69         }
70 
71         @Override
toString()72         public String toString() {
73             return MoreObjects.toStringHelper(this)
74                     .add("command", mCommand)
75                     .toString();
76         }
77     }
78 
79     @Override
takePicture(OneCamera.PhotoCaptureParameters params, final CaptureSession session)80     public void takePicture(OneCamera.PhotoCaptureParameters params, final CaptureSession session) {
81         OneCamera.PictureCallback pictureCallback = params.callback;
82 
83         // Wrap the pictureCallback with a thread-safe adapter which guarantees
84         // that they are always invoked on the main thread.
85         PictureCallbackAdapter pictureCallbackAdapter =
86                 new PictureCallbackAdapter(pictureCallback, mMainExecutor);
87 
88         final Updatable<Void> imageExposureCallback =
89                 pictureCallbackAdapter.provideQuickExposeUpdatable();
90 
91         final ImageSaver imageSaver = mImageSaverBuilder.build(
92                 params.saverCallback,
93                 OrientationManager.DeviceOrientation.from(params.orientation),
94                 session);
95 
96         mCameraCommandExecutor.execute(new PictureTakerCommand(
97                 imageExposureCallback, imageSaver, session));
98     }
99 }
100