1 /*
2  * Copyright (C) 2020 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.commands;
18 
19 import android.hardware.camera2.CameraAccessException;
20 import android.hardware.camera2.CaptureRequest;
21 
22 import com.android.camera.one.v2.camera2proxy.CameraCaptureSessionClosedException;
23 import com.android.camera.one.v2.commands.CameraCommand;
24 import com.android.camera.one.v2.core.FrameServer;
25 import com.android.camera.one.v2.core.RequestBuilder;
26 import com.android.camera.one.v2.core.ResourceAcquisitionFailedException;
27 
28 import java.util.Arrays;
29 
30 import javax.annotation.Nullable;
31 import javax.annotation.ParametersAreNonnullByDefault;
32 
33 /**
34  * Update repeating request.
35  */
36 @ParametersAreNonnullByDefault
37 public final class UpdateRequestCommand implements CameraCommand {
38     private final FrameServer mFrameServer;
39     private final RequestBuilder.Factory mBuilderFactory;
40     private final int mTemplateType;
41 
42     /**
43      * @param frameServer Used for sending requests to the camera.
44      * @param builder Used for building requests.
45      * @param templateType See
46      *            {@link android.hardware.camera2.CameraDevice#createCaptureRequest}
47      */
UpdateRequestCommand(FrameServer frameServer, RequestBuilder.Factory builder, int templateType)48     public UpdateRequestCommand(FrameServer frameServer, RequestBuilder.Factory builder, int
49             templateType) {
50         mFrameServer = frameServer;
51         mBuilderFactory = builder;
52         mTemplateType = templateType;
53     }
54 
55     /**
56      * Update repeating request.
57      */
58     @Override
run()59     public void run() throws InterruptedException, CameraAccessException,
60             CameraCaptureSessionClosedException, ResourceAcquisitionFailedException {
61         FrameServer.Session session = mFrameServer.tryCreateExclusiveSession();
62         if (session == null) {
63             // If there are already other commands interacting with the
64             // FrameServer just abort.
65             return;
66         }
67 
68         try {
69             RequestBuilder builder = mBuilderFactory.create(mTemplateType);
70             session.submitRequest(Arrays.asList(builder.build()),
71                     FrameServer.RequestType.REPEATING);
72         } finally {
73             session.close();
74         }
75     }
76 }
77