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 import android.car.settings.CarSettings;
20 
21 /**
22  * Internal helper utilities
23  * @hide
24  */
25 public final class CarApiUtil {
26 
27     /**
28      * CarService throws IllegalStateException with this message is re-thrown as
29      * {@link CarNotConnectedException}.
30      *
31      * @hide
32      */
33     public static final String CAR_NOT_CONNECTED_EXCEPTION_MSG = "CarNotConnected";
34 
35     /**
36      * Re-throw IllegalStateException from CarService with
37      * {@link #CAR_NOT_CONNECTED_EXCEPTION_MSG} message as {@link CarNotConnectedException}.
38      * exception.
39      *
40      * @param e exception from CarService
41      * @throws CarNotConnectedException if the connection to the car service has been lost.
42      * @hide
43      */
checkCarNotConnectedExceptionFromCarService(IllegalStateException e)44     public static void checkCarNotConnectedExceptionFromCarService(IllegalStateException e)
45             throws CarNotConnectedException {
46         if (e.getMessage().equals(CAR_NOT_CONNECTED_EXCEPTION_MSG)) {
47             throw new CarNotConnectedException();
48         } else {
49             throw e;
50         }
51     }
52 
53     /** do not use */
CarApiUtil()54     private CarApiUtil() {};
55 
56     /**
57      * Return an integer array of {hour, minute} from the String presentation of the garage mode
58      * time.
59      *
60      * @hide
61      */
decodeGarageTimeSetting(String time)62     public static int[] decodeGarageTimeSetting(String time) {
63         int[] result = CarSettings.DEFAULT_GARAGE_MODE_WAKE_UP_TIME;
64         if (time == null) {
65             return result;
66         }
67 
68         String[] tokens = time.split(":");
69         if (tokens.length != 2) {
70             return result;
71         }
72         try {
73             result[0] = Integer.valueOf(tokens[0]);
74             result[1] = Integer.valueOf(tokens[1]);
75         } catch (NumberFormatException e) {
76             return CarSettings.DEFAULT_GARAGE_MODE_WAKE_UP_TIME;
77         }
78         if (result[0] >= 0 && result[0] <= 23 && result[1] >= 0 && result[1] <= 59) {
79             return result;
80         } else {
81             return CarSettings.DEFAULT_GARAGE_MODE_WAKE_UP_TIME;
82         }
83     }
84 
85     /**
86      * Return a String presentation of the garage mode "hour:minute".
87      *
88      * @hide
89      */
encodeGarageTimeSetting(int hour, int min)90     public static String encodeGarageTimeSetting(int hour, int min) {
91         return hour + ":" + min;
92     }
93 }
94