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 android.hardware.camera2.CameraAccessException;
20
21import com.android.camera.debug.Log;
22import com.android.camera.debug.Logger;
23import com.android.camera.one.v2.camera2proxy.CameraCaptureSessionClosedException;
24import com.android.camera.one.v2.camera2proxy.CameraCaptureSessionProxy;
25import com.android.camera.one.v2.commands.CameraCommandExecutor;
26import com.android.camera.stats.UsageStatistics;
27import com.google.common.logging.eventprotos;
28
29import javax.annotation.ParametersAreNonnullByDefault;
30
31/**
32 * Resets camera usage by calling abortCaptures(), flushing (interrupting) any
33 * currently-executing camera commands, and restarting the preview.
34 * <p>
35 * Workaround for Bug: 19061883
36 */
37@ParametersAreNonnullByDefault
38final class FastCameraReset implements FailureHandler {
39    private final Logger mLog;
40    private final CameraCaptureSessionProxy mCaptureSession;
41    private final CameraCommandExecutor mCommandExecutor;
42    private final Runnable mPreviewStarter;
43    private final UsageStatistics mUsageStats;
44
45    FastCameraReset(Logger.Factory logFactory, CameraCaptureSessionProxy captureSession,
46            CameraCommandExecutor commandExecutor, Runnable previewStarter,
47            UsageStatistics usageStats) {
48        mLog = logFactory.create(new Log.Tag("FastCameraReset"));
49        mCaptureSession = captureSession;
50        mCommandExecutor = commandExecutor;
51        mPreviewStarter = previewStarter;
52        mUsageStats = usageStats;
53    }
54
55    @Override
56    public void run() {
57        // TODO: Replace UNKNOWN_REASON with enum for this error.
58        mUsageStats.cameraFailure(eventprotos.CameraFailure.FailureReason.UNKNOWN_REASON,
59                "api2_repeated_failure", UsageStatistics.NONE, UsageStatistics.NONE);
60
61        mLog.w("beginning reset()");
62        try {
63            mLog.w("abortCaptures()");
64            mCaptureSession.abortCaptures();
65        } catch (CameraAccessException e) {
66            e.printStackTrace();
67        } catch (CameraCaptureSessionClosedException e) {
68            e.printStackTrace();
69        }
70        mLog.w("flushing existing camera commands");
71        mCommandExecutor.flush();
72        mLog.w("restarting the preview");
73        mPreviewStarter.run();
74        mLog.w("finished reset()");
75    }
76}
77