Home | History | Annotate | Download | only in camera
      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 
     17 package com.android.camera;
     18 
     19 import android.hardware.camera2.CameraDevice;
     20 
     21 import com.android.camera2.R;
     22 
     23 /**
     24  * Handles fatal application errors.
     25  * <p>
     26  * Usage:
     27  *
     28  * <pre>
     29  * if (unrecoverableErrorDetected) {
     30  *     fatalErrorHandler.handleFatalError(Reason.CANNOT_CONNECT_TO_CAMERA);
     31  * }
     32  * </pre>
     33  */
     34 public interface FatalErrorHandler {
     35     public static enum Reason {
     36         CANNOT_CONNECT_TO_CAMERA(
     37                 R.string.error_cannot_connect_camera,
     38                 R.string.feedback_description_camera_access,
     39                 true),
     40         CAMERA_HAL_FAILED(
     41                 R.string.error_cannot_connect_camera,
     42                 R.string.feedback_description_camera_access,
     43                 true),
     44         CAMERA_DISABLED_BY_SECURITY_POLICY(
     45                 R.string.error_camera_disabled,
     46                 R.string.feedback_description_camera_access,
     47                 true),
     48         MEDIA_STORAGE_FAILURE(
     49                 R.string.error_media_storage_failure,
     50                 R.string.feedback_description_save_photo,
     51                 false);
     52 
     53         private final int mDialogMsgId;
     54         private final int mFeedbackMsgId;
     55         private final boolean mFinishActivity;
     56 
     57         /**
     58          * @param dialogMsgId The resource ID of string to display in the fatal
     59          *            error dialog.
     60          * @param feedbackMsgId The resource ID of default string to display in
     61          *            the feedback dialog, if the user chooses to submit
     62          *            feedback from the dialog.
     63          * @param finishActivity Whether the activity should be finished as a
     64          *            result of this error.
     65          */
     66         Reason(int dialogMsgId, int feedbackMsgId, boolean finishActivity) {
     67             mDialogMsgId = dialogMsgId;
     68             mFeedbackMsgId = feedbackMsgId;
     69             mFinishActivity = finishActivity;
     70         }
     71 
     72         /**
     73          * @return The resource ID of the string to display in the fatal error
     74          *         dialog.
     75          */
     76         public int getFeedbackMsgId() {
     77             return mFeedbackMsgId;
     78         }
     79 
     80         /**
     81          * @return The resource ID of the default string to display in the
     82          *         feedback dialog, if the user chooses to submit feedback from
     83          *         the dialog.
     84          */
     85         public int getDialogMsgId() {
     86             return mDialogMsgId;
     87         }
     88 
     89         /**
     90          * @return Whether the activity should be finished as a result of this
     91          *         error.
     92          */
     93         public boolean doesFinishActivity() {
     94             return mFinishActivity;
     95         }
     96 
     97         /**
     98          * Creates a new Reason based on an error code for
     99          * {@link CameraDevice.StateCallback#onError}.
    100          *
    101          * @param error The error code. One of
    102          *            CameraDevice.StateCallback.ERROR_*
    103          * @return The appropriate Reason.
    104          */
    105         public static Reason fromCamera2CameraDeviceStateCallbackError(int error) {
    106             // TODO Use a more descriptive reason to distinguish between
    107             // different types of errors.
    108             switch (error) {
    109                 case CameraDevice.StateCallback.ERROR_CAMERA_DEVICE:
    110                 case CameraDevice.StateCallback.ERROR_CAMERA_DISABLED:
    111                 case CameraDevice.StateCallback.ERROR_CAMERA_IN_USE:
    112                 case CameraDevice.StateCallback.ERROR_CAMERA_SERVICE:
    113                 case CameraDevice.StateCallback.ERROR_MAX_CAMERAS_IN_USE:
    114                 default:
    115                     return CANNOT_CONNECT_TO_CAMERA;
    116             }
    117         }
    118     }
    119 
    120     /**
    121      * Handles Media Storage Failures - ie. images aren't being saved to disk.
    122      */
    123     public void onMediaStorageFailure();
    124 
    125     /**
    126      * Handles error where the camera cannot be opened.
    127      */
    128     public void onCameraOpenFailure();
    129 
    130     /**
    131      * Handles error where the camera cannot be reconnected.
    132      */
    133     public void onCameraReconnectFailure();
    134 
    135     /**
    136      * Handles generic error where the camera is unavailable. Only use this if
    137      * you are unsure what caused the error, such as a reconnection or open.
    138      * failure
    139      */
    140     public void onGenericCameraAccessFailure();
    141 
    142     /**
    143      * Handles error where the camera is disabled due to security.
    144      */
    145     public void onCameraDisabledFailure();
    146 
    147 
    148     /**
    149      * Handles a fatal error, e.g. by displaying the appropriate dialog and
    150      * exiting the activity.
    151      * @deprecated use specific implementations above instead
    152      */
    153     @Deprecated
    154     public void handleFatalError(Reason reason);
    155 }
    156