1 /**
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 
17 package com.android.server.broadcastradio.hal2;
18 
19 import android.annotation.NonNull;
20 import android.os.RemoteException;
21 
22 enum FrequencyBand {
23     UNKNOWN,
24     FM,
25     AM_LW,
26     AM_MW,
27     AM_SW,
28 };
29 
30 class Utils {
31     private static final String TAG = "BcRadio2Srv.utils";
32 
getBand(int freq)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     }
43 
44     interface FuncThrowingRemoteException<T> {
exec()45         T exec() throws RemoteException;
46     }
47 
maybeRethrow(@onNull FuncThrowingRemoteException<T> r)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 {
exec()58         void exec() throws RemoteException;
59     }
60 
maybeRethrow(@onNull VoidFuncThrowingRemoteException r)61     static void maybeRethrow(@NonNull VoidFuncThrowingRemoteException r) {
62         try {
63             r.exec();
64         } catch (RemoteException ex) {
65             ex.rethrowFromSystemServer();
66         }
67     }
68 }
69