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
17package com.android.camera.one.v2.autofocus;
18
19import android.graphics.Rect;
20import android.hardware.camera2.CaptureRequest;
21import android.hardware.camera2.params.MeteringRectangle;
22
23import com.android.camera.async.ConcurrentState;
24import com.android.camera.async.Lifetime;
25import com.android.camera.async.ResettingDelayedExecutor;
26import com.android.camera.one.Settings3A;
27import com.android.camera.one.v2.commands.CameraCommand;
28import com.android.camera.one.v2.commands.CameraCommandExecutor;
29import com.android.camera.one.v2.commands.ResettingRunnableCameraCommand;
30import com.android.camera.one.v2.core.FrameServer;
31import com.android.camera.one.v2.core.RequestBuilder;
32import com.android.camera.one.v2.core.RequestTemplate;
33import com.google.common.base.Supplier;
34
35import java.util.concurrent.ScheduledExecutorService;
36import java.util.concurrent.TimeUnit;
37
38/**
39 * Wires together "tap-to-focus" functionality, providing a
40 * {@link ManualAutoFocus} instance to trigger auto-focus and metering. It also
41 * provides a way of polling for the most up-to-date metering regions.
42 */
43public class ManualAutoFocusFactory {
44    private final ManualAutoFocus mManualAutoFocus;
45    private final Supplier<MeteringRectangle[]> mAEMeteringRegion;
46    private final Supplier<MeteringRectangle[]> mAFMeteringRegion;
47
48    private ManualAutoFocusFactory(ManualAutoFocus manualAutoFocus,
49            Supplier<MeteringRectangle[]> aeMeteringRegion,
50            Supplier<MeteringRectangle[]> afMeteringRegion) {
51        mManualAutoFocus = manualAutoFocus;
52        mAEMeteringRegion = aeMeteringRegion;
53        mAFMeteringRegion = afMeteringRegion;
54    }
55
56    /**
57     * @param lifetime The Lifetime for all created objects.
58     * @param frameServer The FrameServer on which to perform manual AF scans.
59     * @param commandExecutor The command executor on which to interact with the
60     *            camera.
61     * @param cropRegion The latest crop region.
62     * @param sensorOrientation The sensor orientation.
63     * @param previewRunner A runnable to restart the preview.
64     * @param rootBuilder The root request builder to use for all requests sent
65     * @param threadPool The executor on which to schedule delayed tasks.
66     * @param afHoldSeconds The number of seconds to hold AF after a manual AF
67     *            sweep is triggered.
68     */
69    public static ManualAutoFocusFactory create(Lifetime lifetime, FrameServer frameServer,
70            CameraCommandExecutor commandExecutor, Supplier<Rect> cropRegion,
71            int sensorOrientation,
72            Runnable previewRunner, RequestBuilder.Factory rootBuilder,
73            int templateType, Settings3A settings3A,
74            ScheduledExecutorService threadPool,
75            int afHoldSeconds) {
76        ConcurrentState<MeteringParameters> currentMeteringParameters = new ConcurrentState<>(
77                GlobalMeteringParameters.create());
78        AEMeteringRegion aeMeteringRegion = new AEMeteringRegion(currentMeteringParameters,
79                cropRegion);
80        AFMeteringRegion afMeteringRegion = new AFMeteringRegion(currentMeteringParameters,
81                cropRegion);
82
83        RequestTemplate afScanRequestBuilder = new RequestTemplate(rootBuilder);
84        afScanRequestBuilder.setParam(CaptureRequest.CONTROL_AE_REGIONS, aeMeteringRegion);
85        afScanRequestBuilder.setParam(CaptureRequest.CONTROL_AF_REGIONS, afMeteringRegion);
86
87        CameraCommand afScanCommand = new FullAFScanCommand(frameServer, afScanRequestBuilder,
88                templateType);
89
90        ResettingDelayedExecutor afHoldDelayedExecutor = new ResettingDelayedExecutor(
91                threadPool, afHoldSeconds, TimeUnit.SECONDS);
92        lifetime.add(afHoldDelayedExecutor);
93
94        CameraCommand afScanHoldResetCommand = new AFScanHoldResetCommand(afScanCommand,
95                afHoldDelayedExecutor, previewRunner, currentMeteringParameters);
96
97        Runnable afRunner = new ResettingRunnableCameraCommand(commandExecutor,
98                afScanHoldResetCommand);
99
100        ManualAutoFocusImpl manualAutoFocus = new ManualAutoFocusImpl(currentMeteringParameters,
101                afRunner, sensorOrientation, settings3A);
102
103        return new ManualAutoFocusFactory(manualAutoFocus, aeMeteringRegion, afMeteringRegion);
104    }
105
106    public ManualAutoFocus provideManualAutoFocus() {
107        return mManualAutoFocus;
108    }
109
110    public Supplier<MeteringRectangle[]> provideAEMeteringRegion() {
111        return mAEMeteringRegion;
112    }
113
114    public Supplier<MeteringRectangle[]> provideAFMeteringRegion() {
115        return mAFMeteringRegion;
116    }
117}
118