CarApiUtil.java revision d72b53500006e84b0c69e650878267c693c164a3
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
17package android.car;
18
19import android.car.settings.CarSettings;
20
21/**
22 * Internal helper utilities
23 * @hide
24 */
25public 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     * CarService throw IllegalStateException with this message is re-thrown as
37     * {@link CarOperationNotSupportedException}.
38     *
39     * @hide
40     */
41    public static final String CAR_NOT_SUPPORTED_EXCEPTION_MSG = "CarNotSupported";
42
43    /**
44     * IllegalStateException from CarService with special message is re-thrown as a different
45     * exception.
46     *
47     * @param e exception from CarService
48     * @throws CarNotConnectedException if the connection to the car service has been lost.
49     * @throws CarOperationNotSupportedException
50     * @hide
51     */
52    public static void checkAllIllegalStateExceptionsFromCarService(IllegalStateException e)
53            throws CarNotConnectedException, CarOperationNotSupportedException {
54        String message = e.getMessage();
55        if (message.equals(CAR_NOT_CONNECTED_EXCEPTION_MSG)) {
56            throw new CarNotConnectedException();
57        } else if (message.equals(CAR_NOT_SUPPORTED_EXCEPTION_MSG)) {
58            throw new CarOperationNotSupportedException();
59        } else {
60            throw e;
61        }
62    }
63
64    /**
65     * Re-throw IllegalStateException from CarService with
66     * {@link #CAR_NOT_CONNECTED_EXCEPTION_MSG} message as {@link CarNotConnectedException}.
67     * exception.
68     *
69     * @param e exception from CarService
70     * @throws CarNotConnectedException if the connection to the car service has been lost.
71     * @hide
72     */
73    public static void checkCarNotConnectedExceptionFromCarService(IllegalStateException e)
74            throws CarNotConnectedException {
75        if (e.getMessage().equals(CAR_NOT_CONNECTED_EXCEPTION_MSG)) {
76            throw new CarNotConnectedException();
77        } else {
78            throw e;
79        }
80    }
81
82    /** do not use */
83    private CarApiUtil() {};
84
85    /**
86     * Return an integer array of {hour, minute} from the String presentation of the garage mode
87     * time.
88     *
89     * @hide
90     */
91    public static int[] decodeGarageTimeSetting(String time) {
92        int[] result = CarSettings.DEFAULT_GARAGE_MODE_WAKE_UP_TIME;
93        if (time == null) {
94            return result;
95        }
96
97        String[] tokens = time.split(":");
98        if (tokens.length != 2) {
99            return result;
100        }
101        try {
102            result[0] = Integer.valueOf(tokens[0]);
103            result[1] = Integer.valueOf(tokens[1]);
104        } catch (NumberFormatException e) {
105            return CarSettings.DEFAULT_GARAGE_MODE_WAKE_UP_TIME;
106        }
107        if (result[0] >= 0 && result[0] <= 23 && result[1] >= 0 && result[1] <= 59) {
108            return result;
109        } else {
110            return CarSettings.DEFAULT_GARAGE_MODE_WAKE_UP_TIME;
111        }
112    }
113
114    /**
115     * Return a String presentation of the garage mode "hour:minute".
116     *
117     * @hide
118     */
119    public static String encodeGarageTimeSetting(int hour, int min) {
120        return hour + ":" + min;
121    }
122}
123