• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.utils;
18 
19 import android.hardware.ICameraService;
20 import android.hardware.camera2.CameraAccessException;
21 import android.os.DeadObjectException;
22 import android.os.RemoteException;
23 import android.os.ServiceSpecificException;
24 
25 /**
26  * @hide
27  */
28 public class ExceptionUtils {
29     /**
30      * Converts and throws {@link ServiceSpecificException} from camera binder interfaces as
31      * {@link CameraAccessException}, {@link IllegalArgumentException}, or {@link SecurityException}
32      * based on {@link ServiceSpecificException#errorCode}
33      * <p>
34      * Usage: {@code throw ExceptionUtils.throwAsPublicException(e)}
35      * <p>
36      * Notice the preceding `throw` before calling this method. The throw is essentially
37      * useless but lets the compiler know that execution will terminate at that statement
38      * preventing false "missing return statement" errors.
39      * <p>
40      * The return type is set to the only checked exception this method throws to ensure
41      * that the caller knows exactly which checked exception to declare/handle.
42      *
43      * @hide
44      */
throwAsPublicException(ServiceSpecificException e)45     public static CameraAccessException throwAsPublicException(ServiceSpecificException e)
46             throws CameraAccessException {
47         int reason;
48         switch(e.errorCode) {
49             case ICameraService.ERROR_DISCONNECTED:
50                 reason = CameraAccessException.CAMERA_DISCONNECTED;
51                 break;
52             case ICameraService.ERROR_DISABLED:
53                 reason = CameraAccessException.CAMERA_DISABLED;
54                 break;
55             case ICameraService.ERROR_CAMERA_IN_USE:
56                 reason = CameraAccessException.CAMERA_IN_USE;
57                 break;
58             case ICameraService.ERROR_MAX_CAMERAS_IN_USE:
59                 reason = CameraAccessException.MAX_CAMERAS_IN_USE;
60                 break;
61             case ICameraService.ERROR_DEPRECATED_HAL:
62                 reason = CameraAccessException.CAMERA_DEPRECATED_HAL;
63                 break;
64             case ICameraService.ERROR_ILLEGAL_ARGUMENT:
65             case ICameraService.ERROR_ALREADY_EXISTS:
66                 throw new IllegalArgumentException(e.getMessage(), e);
67             case ICameraService.ERROR_PERMISSION_DENIED:
68                 throw new SecurityException(e.getMessage(), e);
69             case ICameraService.ERROR_TIMED_OUT:
70             case ICameraService.ERROR_INVALID_OPERATION:
71             default:
72                 reason = CameraAccessException.CAMERA_ERROR;
73         }
74 
75         throw new CameraAccessException(reason, e.getMessage(), e);
76     }
77 
78     /**
79      * Converts and throws Binder {@link DeadObjectException} and {@link RemoteException} from
80      * camera binder interfaces as {@link CameraAccessException} or
81      * {@link UnsupportedOperationException}
82      * <p>
83      * Usage: {@code throw ExceptionUtils.throwAsPublicException(e)}
84      * <p>
85      * Notice the preceding `throw` before calling this method. The throw is essentially
86      * useless but lets the compiler know that execution will terminate at that statement
87      * preventing false "missing return statement" errors.
88      * <p>
89      * The return type is set to the only checked exception this method throws to ensure
90      * that the caller knows exactly which checked exception to declare/handle.
91      *
92      * @hide
93      */
throwAsPublicException(RemoteException e)94     public static CameraAccessException throwAsPublicException(RemoteException e)
95             throws CameraAccessException {
96         if (e instanceof DeadObjectException) {
97             throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED,
98                     "Camera service has died unexpectedly", e);
99         }
100 
101         throw new UnsupportedOperationException("An unknown RemoteException was thrown"
102                 + " which should never happen.", e);
103     }
104 
105     /**
106      * Static methods only. Do not initialize.
107      * @hide
108      */
ExceptionUtils()109     private ExceptionUtils() {}
110 }
111