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.initialization;
18
19import android.hardware.camera2.CaptureResult;
20
21import com.android.camera.async.Updatable;
22import com.android.camera.one.OneCamera;
23import com.android.camera.one.v2.camera2proxy.TotalCaptureResultProxy;
24
25import java.util.HashSet;
26import java.util.Set;
27
28import javax.annotation.Nonnull;
29
30/**
31 * Distributes metadata to more-specific callbacks.
32 */
33public class MetadataCallback implements Updatable<TotalCaptureResultProxy> {
34    private final Updatable<Integer> mFocusState;
35    private final Updatable<OneCamera.FocusState> mOneCameraFocusState;
36    private final Updatable<Integer> mFocusMode;
37
38    public MetadataCallback(
39            Updatable<Integer> focusState,
40            Updatable<OneCamera.FocusState> oneCameraFocusState,
41            Updatable<Integer> focusMode) {
42        mFocusState = focusState;
43        mOneCameraFocusState = oneCameraFocusState;
44        mFocusMode = focusMode;
45    }
46
47    @Override
48    public void update(@Nonnull TotalCaptureResultProxy totalCaptureResult) {
49        updateFocusMode(totalCaptureResult);
50        updateFocusState(totalCaptureResult);
51        updateOneCameraFocusState(totalCaptureResult);
52    }
53
54    private void updateFocusMode(TotalCaptureResultProxy totalCaptureResult) {
55        Integer focusMode = totalCaptureResult.get(CaptureResult.CONTROL_AF_MODE);
56        if (focusMode != null) {
57            mFocusMode.update(focusMode);
58        }
59    }
60
61    private void updateFocusState(TotalCaptureResultProxy totalCaptureResult) {
62        Integer focusState = totalCaptureResult.get(CaptureResult.CONTROL_AF_STATE);
63        if (focusState != null) {
64            mFocusState.update(focusState);
65        }
66    }
67
68    private void updateOneCameraFocusState(TotalCaptureResultProxy totalCaptureResult) {
69        Float focusDistance = totalCaptureResult.get(CaptureResult.LENS_FOCUS_DISTANCE);
70        Integer focusState = totalCaptureResult.get(CaptureResult.CONTROL_AF_STATE);
71        if (focusDistance != null && focusState != null) {
72            Set<Integer> activeStates = new HashSet<>();
73            activeStates.add(CaptureResult.CONTROL_AF_STATE_ACTIVE_SCAN);
74            activeStates.add(CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN);
75            boolean active = activeStates.contains(focusState);
76            mOneCameraFocusState.update(new OneCamera.FocusState(focusDistance, active));
77        }
78    }
79}
80