blob: 384c9bab94b481faa0c2a56d0ce5eef198c516c2 [file] [log] [blame]
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -08001/**
2 * Copyright (C) 2017 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 com.android.server.broadcastradio.hal2;
18
Tomasz Wasilczyk3b4465e2018-01-14 21:47:44 -080019import android.annotation.NonNull;
20import android.os.RemoteException;
21
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080022enum FrequencyBand {
23 UNKNOWN,
24 FM,
25 AM_LW,
26 AM_MW,
27 AM_SW,
28};
29
30class Utils {
31 private static final String TAG = "BcRadio2Srv.utils";
32
33 static FrequencyBand getBand(int freq) {
34 // keep in sync with hardware/interfaces/broadcastradio/common/utils2x/Utils.cpp
35 if (freq < 30) return FrequencyBand.UNKNOWN;
36 if (freq < 500) return FrequencyBand.AM_LW;
37 if (freq < 1705) return FrequencyBand.AM_MW;
38 if (freq < 30000) return FrequencyBand.AM_SW;
39 if (freq < 60000) return FrequencyBand.UNKNOWN;
40 if (freq < 110000) return FrequencyBand.FM;
41 return FrequencyBand.UNKNOWN;
42 }
Tomasz Wasilczyk3b4465e2018-01-14 21:47:44 -080043
44 interface FuncThrowingRemoteException<T> {
45 T exec() throws RemoteException;
46 }
47
48 static <T> T maybeRethrow(@NonNull FuncThrowingRemoteException<T> r) {
49 try {
50 return r.exec();
51 } catch (RemoteException ex) {
52 ex.rethrowFromSystemServer();
53 return null; // unreachable
54 }
55 }
56
57 interface VoidFuncThrowingRemoteException {
58 void exec() throws RemoteException;
59 }
60
61 static void maybeRethrow(@NonNull VoidFuncThrowingRemoteException r) {
62 try {
63 r.exec();
64 } catch (RemoteException ex) {
65 ex.rethrowFromSystemServer();
66 }
67 }
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080068}