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 com.android.server.broadcastradio.RadioServiceUserController;
25 import com.android.server.utils.Slogf;
26 
27 import java.util.List;
28 import java.util.Objects;
29 
30 public class BroadcastRadioService {
31 
32     private static final String TAG = "BcRadio1Srv";
33 
34     /**
35      * This field is used by native code, do not access or modify.
36      */
37     private final long mNativeContext = nativeInit();
38 
39     private final Object mLock = new Object();
40 
41     @Override
finalize()42     protected void finalize() throws Throwable {
43         nativeFinalize(mNativeContext);
44         super.finalize();
45     }
46 
nativeInit()47     private native long nativeInit();
nativeFinalize(long nativeContext)48     private native void nativeFinalize(long nativeContext);
nativeLoadModules(long nativeContext)49     private native List<RadioManager.ModuleProperties> nativeLoadModules(long nativeContext);
nativeOpenTuner(long nativeContext, int moduleId, RadioManager.BandConfig config, boolean withAudio, ITunerCallback callback)50     private native Tuner nativeOpenTuner(long nativeContext, int moduleId,
51             RadioManager.BandConfig config, boolean withAudio, ITunerCallback callback);
52 
loadModules()53     public @NonNull List<RadioManager.ModuleProperties> loadModules() {
54         synchronized (mLock) {
55             return Objects.requireNonNull(nativeLoadModules(mNativeContext));
56         }
57     }
58 
openTuner(int moduleId, RadioManager.BandConfig bandConfig, boolean withAudio, ITunerCallback callback)59     public ITuner openTuner(int moduleId, RadioManager.BandConfig bandConfig,
60             boolean withAudio, ITunerCallback callback) {
61         if (!RadioServiceUserController.isCurrentOrSystemUser()) {
62             Slogf.e(TAG, "Cannot open tuner on HAL 1.x client for non-current user");
63             throw new IllegalStateException("Cannot open tuner for non-current user");
64         }
65         synchronized (mLock) {
66             return nativeOpenTuner(mNativeContext, moduleId, bandConfig, withAudio, callback);
67         }
68     }
69 }
70