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.hal1;
18 
19 import android.annotation.NonNull;
20 import android.hardware.radio.ITuner;
21 import android.hardware.radio.ITunerCallback;
22 import android.hardware.radio.RadioManager;
23 
24 import java.util.List;
25 import java.util.Objects;
26 
27 public class BroadcastRadioService {
28     /**
29      * This field is used by native code, do not access or modify.
30      */
31     private final long mNativeContext = nativeInit();
32 
33     private final Object mLock;
34 
35     @Override
finalize()36     protected void finalize() throws Throwable {
37         nativeFinalize(mNativeContext);
38         super.finalize();
39     }
40 
nativeInit()41     private native long nativeInit();
nativeFinalize(long nativeContext)42     private native void nativeFinalize(long nativeContext);
nativeLoadModules(long nativeContext)43     private native List<RadioManager.ModuleProperties> nativeLoadModules(long nativeContext);
nativeOpenTuner(long nativeContext, int moduleId, RadioManager.BandConfig config, boolean withAudio, ITunerCallback callback)44     private native Tuner nativeOpenTuner(long nativeContext, int moduleId,
45             RadioManager.BandConfig config, boolean withAudio, ITunerCallback callback);
46 
47     /**
48      * Constructor. should pass
49      * {@code com.android.server.broadcastradio.BroadcastRadioService#mLock} for lock.
50      */
BroadcastRadioService(@onNull Object lock)51     public BroadcastRadioService(@NonNull Object lock) {
52         mLock = lock;
53     }
54 
loadModules()55     public @NonNull List<RadioManager.ModuleProperties> loadModules() {
56         synchronized (mLock) {
57             return Objects.requireNonNull(nativeLoadModules(mNativeContext));
58         }
59     }
60 
openTuner(int moduleId, RadioManager.BandConfig bandConfig, boolean withAudio, @NonNull ITunerCallback callback)61     public ITuner openTuner(int moduleId, RadioManager.BandConfig bandConfig,
62             boolean withAudio, @NonNull ITunerCallback callback) {
63         synchronized (mLock) {
64             return nativeOpenTuner(mNativeContext, moduleId, bandConfig, withAudio, callback);
65         }
66     }
67 }
68