1/*
2 * Copyright (C) 2015 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.errorhandling;
18
19import com.android.camera.FatalErrorHandler;
20import com.android.camera.debug.Logger;
21import com.android.camera.one.v2.camera2proxy.CameraCaptureSessionProxy;
22import com.android.camera.one.v2.commands.CameraCommandExecutor;
23import com.android.camera.one.v2.core.ResponseListener;
24import com.android.camera.stats.UsageStatistics;
25
26import java.util.Arrays;
27import java.util.List;
28
29import javax.annotation.ParametersAreNonnullByDefault;
30
31/**
32 * Creates a ResponseListener which will handle repeat failures.
33 * <p>
34 * This is to workaround bug: 19061883
35 */
36@ParametersAreNonnullByDefault
37public final class RepeatFailureHandlerComponent {
38    private final RepeatFailureDetector mRepeatFailureHandler;
39
40    private RepeatFailureHandlerComponent(RepeatFailureDetector repeatFailureHandler) {
41        mRepeatFailureHandler = repeatFailureHandler;
42    }
43
44    public ResponseListener provideResponseListener() {
45        return mRepeatFailureHandler;
46    }
47
48    public static RepeatFailureHandlerComponent create(Logger.Factory logFactory,
49            FatalErrorHandler fatalErrorHandler, CameraCaptureSessionProxy captureSession,
50            CameraCommandExecutor commandExecutor, Runnable previewStarter,
51            UsageStatistics usageStats, int consecutiveFailureThreshold) {
52        FastCameraReset fastCameraReset = new FastCameraReset(logFactory, captureSession,
53                commandExecutor, previewStarter, usageStats);
54        FatalErrorDialogFailureHandler fatalErrorDialog = new FatalErrorDialogFailureHandler
55                (fatalErrorHandler, usageStats);
56
57        RecoverySuccessCallback recoverySuccessCallback = new RecoverySuccessCallback(usageStats);
58
59        List<FailureHandler> recoveryStrategies = Arrays.asList(fastCameraReset, fatalErrorDialog);
60        RepeatFailureDetector failureDetector = new RepeatFailureDetector(logFactory,
61                consecutiveFailureThreshold,
62                recoveryStrategies,
63                recoverySuccessCallback);
64        return new RepeatFailureHandlerComponent(failureDetector);
65    }
66}
67