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; 18 19 import android.Manifest; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.hardware.radio.IRadioService; 23 import android.hardware.radio.ITuner; 24 import android.hardware.radio.ITunerCallback; 25 import android.hardware.radio.RadioManager; 26 import android.os.ParcelableException; 27 28 import com.android.server.SystemService; 29 30 import java.util.List; 31 32 public class BroadcastRadioService extends SystemService { 33 private final ServiceImpl mServiceImpl = new ServiceImpl(); 34 35 /** 36 * This field is used by native code, do not access or modify. 37 */ 38 private final long mNativeContext = nativeInit(); 39 40 private final Object mLock = new Object(); 41 private List<RadioManager.ModuleProperties> mModules = null; 42 43 public BroadcastRadioService(Context context) { 44 super(context); 45 } 46 47 @Override 48 protected void finalize() throws Throwable { 49 nativeFinalize(mNativeContext); 50 super.finalize(); 51 } 52 53 private native long nativeInit(); 54 private native void nativeFinalize(long nativeContext); 55 private native List<RadioManager.ModuleProperties> nativeLoadModules(long nativeContext); 56 private native Tuner nativeOpenTuner(long nativeContext, int moduleId, 57 RadioManager.BandConfig config, boolean withAudio, ITunerCallback callback); 58 59 @Override 60 public void onStart() { 61 publishBinderService(Context.RADIO_SERVICE, mServiceImpl); 62 } 63 64 private class ServiceImpl extends IRadioService.Stub { 65 private void enforcePolicyAccess() { 66 if (PackageManager.PERMISSION_GRANTED != getContext().checkCallingPermission( 67 Manifest.permission.ACCESS_BROADCAST_RADIO)) { 68 throw new SecurityException("ACCESS_BROADCAST_RADIO permission not granted"); 69 } 70 } 71 72 @Override 73 public List<RadioManager.ModuleProperties> listModules() { 74 enforcePolicyAccess(); 75 synchronized (mLock) { 76 if (mModules != null) return mModules; 77 78 mModules = nativeLoadModules(mNativeContext); 79 if (mModules == null) { 80 throw new ParcelableException(new NullPointerException( 81 "couldn't load radio modules")); 82 } 83 84 return mModules; 85 } 86 } 87 88 @Override 89 public ITuner openTuner(int moduleId, RadioManager.BandConfig bandConfig, 90 boolean withAudio, ITunerCallback callback) { 91 enforcePolicyAccess(); 92 if (callback == null) { 93 throw new IllegalArgumentException("Callback must not be empty"); 94 } 95 synchronized (mLock) { 96 return nativeOpenTuner(mNativeContext, moduleId, bandConfig, withAudio, callback); 97 } 98 } 99 } 100 } 101