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.ex.camera2.utils;
18
19import android.hardware.camera2.CameraCaptureSession;
20import android.hardware.camera2.CameraCaptureSession.CaptureCallback;
21import android.hardware.camera2.CaptureFailure;
22import android.hardware.camera2.CaptureRequest;
23import android.hardware.camera2.CaptureResult;
24import android.hardware.camera2.TotalCaptureResult;
25
26import java.util.Arrays;
27import java.util.LinkedList;
28import java.util.List;
29
30/**
31 * Junction that allows notifying multiple {@link CaptureCallback}s whenever
32 * the {@link CameraCaptureSession} posts a capture-related update.
33 */
34public class Camera2CaptureCallbackSplitter extends CaptureCallback {
35    private final List<CaptureCallback> mRecipients = new LinkedList<>();
36
37    /**
38     * @param recipients The listeners to notify. Any {@code null} passed here
39     *                   will be completely ignored.
40     */
41    public Camera2CaptureCallbackSplitter(CaptureCallback... recipients) {
42        for (CaptureCallback listener : recipients) {
43            if (listener != null) {
44                mRecipients.add(listener);
45            }
46        }
47    }
48
49    @Override
50    public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
51                                   TotalCaptureResult result) {
52        for (CaptureCallback target : mRecipients) {
53            target.onCaptureCompleted(session, request, result);
54        }
55    }
56
57    @Override
58    public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request,
59                                CaptureFailure failure) {
60        for (CaptureCallback target : mRecipients) {
61            target.onCaptureFailed(session, request, failure);
62        }
63    }
64
65    @Override
66    public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
67                                    CaptureResult partialResult) {
68        for (CaptureCallback target : mRecipients) {
69            target.onCaptureProgressed(session, request, partialResult);
70        }
71    }
72
73    @Override
74    public void onCaptureSequenceAborted(CameraCaptureSession session, int sequenceId) {
75        for (CaptureCallback target : mRecipients) {
76            target.onCaptureSequenceAborted(session, sequenceId);
77        }
78    }
79
80    @Override
81    public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId,
82                                           long frameNumber) {
83        for (CaptureCallback target : mRecipients) {
84            target.onCaptureSequenceCompleted(session, sequenceId, frameNumber);
85        }
86    }
87
88    @Override
89    public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request,
90                                 long timestamp, long frameNumber) {
91        for (CaptureCallback target : mRecipients) {
92            target.onCaptureStarted(session, request, timestamp, frameNumber);
93        }
94    }
95}
96