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 android.car;
18 
19 /**
20  * Internal helper utilities
21  * @hide
22  */
23 public final class CarApiUtil {
24 
25     /**
26      * CarService throws IllegalStateException with this message is re-thrown as
27      * {@link CarNotConnectedException}.
28      *
29      * @hide
30      */
31     public static final String CAR_NOT_CONNECTED_EXCEPTION_MSG = "CarNotConnected";
32 
33     /**
34      * CarService throw IllegalStateException with this message is re-thrown as
35      * {@link CarNotSupportedException}.
36      *
37      * @hide
38      */
39     public static final String CAR_NOT_SUPPORTED_EXCEPTION_MSG = "CarNotSupported";
40 
41     /**
42      * IllegalStateException from CarService with special message is re-thrown as a different
43      * exception.
44      *
45      * @param e exception from CarService
46      * @throws CarNotConnectedException
47      * @throws CarNotSupportedException
48      * @hide
49      */
checkAllIllegalStateExceptionsFromCarService(IllegalStateException e)50     public static void checkAllIllegalStateExceptionsFromCarService(IllegalStateException e)
51             throws CarNotConnectedException, CarNotSupportedException {
52         String message = e.getMessage();
53         if (message.equals(CAR_NOT_CONNECTED_EXCEPTION_MSG)) {
54             throw new CarNotConnectedException();
55         } else if (message.equals(CAR_NOT_SUPPORTED_EXCEPTION_MSG)) {
56             throw new CarNotSupportedException();
57         } else {
58             throw e;
59         }
60     }
61 
62     /**
63      * Re-throw IllegalStateException from CarService with
64      * {@link #CAR_NOT_CONNECTED_EXCEPTION_MSG} message as {@link CarNotConnectedException}.
65      * exception.
66      *
67      * @param e exception from CarService
68      * @throws CarNotConnectedException
69      * @hide
70      */
checkCarNotConnectedExceptionFromCarService(IllegalStateException e)71     public static void checkCarNotConnectedExceptionFromCarService(IllegalStateException e)
72             throws CarNotConnectedException {
73         if (e.getMessage().equals(CAR_NOT_CONNECTED_EXCEPTION_MSG)) {
74             throw new CarNotConnectedException();
75         } else {
76             throw e;
77         }
78     }
79 
80     /** do not use */
CarApiUtil()81     private CarApiUtil() {};
82 }
83