Home | History | Annotate | Download | only in camera2
      1 /*
      2  * Copyright (C) 2013 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 android.hardware.camera2;
     18 
     19 import android.annotation.IntDef;
     20 import android.util.AndroidException;
     21 
     22 import java.lang.annotation.Retention;
     23 import java.lang.annotation.RetentionPolicy;
     24 
     25 /**
     26  * <p><code>CameraAccessException</code> is thrown if a camera device could not
     27  * be queried or opened by the {@link CameraManager}, or if the connection to an
     28  * opened {@link CameraDevice} is no longer valid.</p>
     29  *
     30  * @see CameraManager
     31  * @see CameraDevice
     32  */
     33 public class CameraAccessException extends AndroidException {
     34     /**
     35      * The camera device is in use already.
     36      */
     37     public static final int CAMERA_IN_USE = 4;
     38 
     39     /**
     40      * The system-wide limit for number of open cameras or camera resources has
     41      * been reached, and more camera devices cannot be opened or torch mode
     42      * cannot be turned on until previous instances are closed.
     43      */
     44     public static final int MAX_CAMERAS_IN_USE = 5;
     45 
     46     /**
     47      * The camera is disabled due to a device policy, and cannot be opened.
     48      *
     49      * @see android.app.admin.DevicePolicyManager#setCameraDisabled(android.content.ComponentName, boolean)
     50      */
     51     public static final int CAMERA_DISABLED = 1;
     52 
     53     /**
     54      * The camera device is removable and has been disconnected from the Android
     55      * device, or the camera id used with {@link android.hardware.camera2.CameraManager#openCamera}
     56      * is no longer valid, or the camera service has shut down the connection due to a
     57      * higher-priority access request for the camera device.
     58      */
     59     public static final int CAMERA_DISCONNECTED = 2;
     60 
     61     /**
     62      * The camera device is currently in the error state.
     63      *
     64      * <p>The camera has failed to open or has failed at a later time
     65      * as a result of some non-user interaction. Refer to
     66      * {@link CameraDevice.StateCallback#onError} for the exact
     67      * nature of the error.</p>
     68      *
     69      * <p>No further calls to the camera will succeed. Clean up
     70      * the camera with {@link CameraDevice#close} and try
     71      * handling the error in order to successfully re-open the camera.
     72      * </p>
     73      *
     74      */
     75     public static final int CAMERA_ERROR = 3;
     76 
     77     /**
     78      * A deprecated HAL version is in use.
     79      * @hide
     80      */
     81     public static final int CAMERA_DEPRECATED_HAL = 1000;
     82 
     83     /** @hide */
     84     @Retention(RetentionPolicy.SOURCE)
     85     @IntDef(prefix = { "CAMERA_", "MAX_CAMERAS_IN_USE" }, value = {
     86             CAMERA_IN_USE,
     87             MAX_CAMERAS_IN_USE,
     88             CAMERA_DISABLED,
     89             CAMERA_DISCONNECTED,
     90             CAMERA_ERROR
     91     })
     92     public @interface AccessError {}
     93 
     94     // Make the eclipse warning about serializable exceptions go away
     95     private static final long serialVersionUID = 5630338637471475675L; // randomly generated
     96 
     97     private final int mReason;
     98 
     99     /**
    100      * The reason for the failure to access the camera.
    101      *
    102      * @see #CAMERA_DISABLED
    103      * @see #CAMERA_DISCONNECTED
    104      * @see #CAMERA_ERROR
    105      */
    106     @AccessError
    107     public final int getReason() {
    108         return mReason;
    109     }
    110 
    111     public CameraAccessException(@AccessError int problem) {
    112         super(getDefaultMessage(problem));
    113         mReason = problem;
    114     }
    115 
    116     public CameraAccessException(@AccessError int problem, String message) {
    117         super(getCombinedMessage(problem, message));
    118         mReason = problem;
    119     }
    120 
    121     public CameraAccessException(@AccessError int problem, String message, Throwable cause) {
    122         super(getCombinedMessage(problem, message), cause);
    123         mReason = problem;
    124     }
    125 
    126     public CameraAccessException(@AccessError int problem, Throwable cause) {
    127         super(getDefaultMessage(problem), cause);
    128         mReason = problem;
    129     }
    130 
    131     /**
    132      * @hide
    133      */
    134     public static String getDefaultMessage(@AccessError int problem) {
    135         switch (problem) {
    136             case CAMERA_IN_USE:
    137                 return "The camera device is in use already";
    138             case MAX_CAMERAS_IN_USE:
    139                 return "The system-wide limit for number of open cameras has been reached, " +
    140                        "and more camera devices cannot be opened until previous instances " +
    141                        "are closed.";
    142             case CAMERA_DISCONNECTED:
    143                 return "The camera device is removable and has been disconnected from the " +
    144                         "Android device, or the camera service has shut down the connection due " +
    145                         "to a higher-priority access request for the camera device.";
    146             case CAMERA_DISABLED:
    147                 return "The camera is disabled due to a device policy, and cannot be opened.";
    148             case CAMERA_ERROR:
    149                 return "The camera device is currently in the error state; " +
    150                        "no further calls to it will succeed.";
    151         }
    152         return null;
    153     }
    154 
    155     private static String getCombinedMessage(@AccessError int problem, String message) {
    156         String problemString = getProblemString(problem);
    157         return String.format("%s (%d): %s", problemString, problem, message);
    158     }
    159 
    160     private static String getProblemString(int problem) {
    161         String problemString;
    162         switch (problem) {
    163             case CAMERA_IN_USE:
    164                 problemString = "CAMERA_IN_USE";
    165                 break;
    166             case MAX_CAMERAS_IN_USE:
    167                 problemString = "MAX_CAMERAS_IN_USE";
    168                 break;
    169             case CAMERA_DISCONNECTED:
    170                 problemString = "CAMERA_DISCONNECTED";
    171                 break;
    172             case CAMERA_DISABLED:
    173                 problemString = "CAMERA_DISABLED";
    174                 break;
    175             case CAMERA_ERROR:
    176                 problemString = "CAMERA_ERROR";
    177                 break;
    178             case CAMERA_DEPRECATED_HAL:
    179                 problemString = "CAMERA_DEPRECATED_HAL";
    180                 break;
    181             default:
    182                 problemString = "<UNKNOWN ERROR>";
    183         }
    184         return problemString;
    185     }
    186 
    187 }
    188