• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     private final RadioServiceUserController mUserController;
39 
40     private final Object mLock = new Object();
41 
42     @Override
finalize()43     protected void finalize() throws Throwable {
44         nativeFinalize(mNativeContext);
45         super.finalize();
46     }
47 
nativeInit()48     private native long nativeInit();
nativeFinalize(long nativeContext)49     private native void nativeFinalize(long nativeContext);
nativeLoadModules(long nativeContext)50     private native List<RadioManager.ModuleProperties> nativeLoadModules(long nativeContext);
nativeOpenTuner(long nativeContext, int moduleId, RadioManager.BandConfig config, boolean withAudio, ITunerCallback callback)51     private native Tuner nativeOpenTuner(long nativeContext, int moduleId,
52             RadioManager.BandConfig config, boolean withAudio, ITunerCallback callback);
53 
BroadcastRadioService(RadioServiceUserController userController)54     public BroadcastRadioService(RadioServiceUserController userController) {
55         mUserController = Objects.requireNonNull(userController, "User controller can not be null");
56     }
57 
loadModules()58     public @NonNull List<RadioManager.ModuleProperties> loadModules() {
59         synchronized (mLock) {
60             return Objects.requireNonNull(nativeLoadModules(mNativeContext));
61         }
62     }
63 
openTuner(int moduleId, RadioManager.BandConfig bandConfig, boolean withAudio, ITunerCallback callback)64     public ITuner openTuner(int moduleId, RadioManager.BandConfig bandConfig,
65             boolean withAudio, ITunerCallback callback) {
66         if (!mUserController.isCurrentOrSystemUser()) {
67             Slogf.e(TAG, "Cannot open tuner on HAL 1.x client for non-current user");
68             throw new IllegalStateException("Cannot open tuner for non-current user");
69         }
70         synchronized (mLock) {
71             return nativeOpenTuner(mNativeContext, moduleId, bandConfig, withAudio, callback);
72         }
73     }
74 }
75