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.IAnnouncementListener; 23 import android.hardware.radio.ICloseHandle; 24 import android.hardware.radio.IRadioService; 25 import android.hardware.radio.ITuner; 26 import android.hardware.radio.ITunerCallback; 27 import android.hardware.radio.RadioManager; 28 import android.os.RemoteException; 29 import android.util.Slog; 30 31 import com.android.server.SystemService; 32 import com.android.server.broadcastradio.hal2.AnnouncementAggregator; 33 34 import java.util.ArrayList; 35 import java.util.Arrays; 36 import java.util.List; 37 import java.util.Objects; 38 import java.util.OptionalInt; 39 40 public class BroadcastRadioService extends SystemService { 41 private static final String TAG = "BcRadioSrv"; 42 private static final boolean DEBUG = false; 43 44 private final ServiceImpl mServiceImpl = new ServiceImpl(); 45 46 private final com.android.server.broadcastradio.hal1.BroadcastRadioService mHal1; 47 private final com.android.server.broadcastradio.hal2.BroadcastRadioService mHal2; 48 49 private final Object mLock = new Object(); 50 private List<RadioManager.ModuleProperties> mV1Modules = null; 51 BroadcastRadioService(Context context)52 public BroadcastRadioService(Context context) { 53 super(context); 54 55 mHal1 = new com.android.server.broadcastradio.hal1.BroadcastRadioService(); 56 mV1Modules = mHal1.loadModules(); 57 OptionalInt max = mV1Modules.stream().mapToInt(RadioManager.ModuleProperties::getId).max(); 58 mHal2 = new com.android.server.broadcastradio.hal2.BroadcastRadioService( 59 max.isPresent() ? max.getAsInt() + 1 : 0); 60 } 61 62 @Override onStart()63 public void onStart() { 64 publishBinderService(Context.RADIO_SERVICE, mServiceImpl); 65 } 66 67 private class ServiceImpl extends IRadioService.Stub { enforcePolicyAccess()68 private void enforcePolicyAccess() { 69 if (PackageManager.PERMISSION_GRANTED != getContext().checkCallingPermission( 70 Manifest.permission.ACCESS_BROADCAST_RADIO)) { 71 throw new SecurityException("ACCESS_BROADCAST_RADIO permission not granted"); 72 } 73 } 74 75 @Override listModules()76 public List<RadioManager.ModuleProperties> listModules() { 77 enforcePolicyAccess(); 78 List<RadioManager.ModuleProperties> modules = new ArrayList<>(); 79 modules.addAll(mV1Modules); 80 modules.addAll(mHal2.listModules()); 81 return modules; 82 } 83 84 @Override openTuner(int moduleId, RadioManager.BandConfig bandConfig, boolean withAudio, ITunerCallback callback)85 public ITuner openTuner(int moduleId, RadioManager.BandConfig bandConfig, 86 boolean withAudio, ITunerCallback callback) throws RemoteException { 87 if (DEBUG) Slog.i(TAG, "Opening module " + moduleId); 88 enforcePolicyAccess(); 89 if (callback == null) { 90 throw new IllegalArgumentException("Callback must not be empty"); 91 } 92 synchronized (mLock) { 93 if (mHal2.hasModule(moduleId)) { 94 return mHal2.openSession(moduleId, bandConfig, withAudio, callback); 95 } else { 96 return mHal1.openTuner(moduleId, bandConfig, withAudio, callback); 97 } 98 } 99 } 100 101 @Override addAnnouncementListener(int[] enabledTypes, IAnnouncementListener listener)102 public ICloseHandle addAnnouncementListener(int[] enabledTypes, 103 IAnnouncementListener listener) { 104 if (DEBUG) { 105 Slog.i(TAG, "Adding announcement listener for " + Arrays.toString(enabledTypes)); 106 } 107 Objects.requireNonNull(enabledTypes); 108 Objects.requireNonNull(listener); 109 enforcePolicyAccess(); 110 111 synchronized (mLock) { 112 if (!mHal2.hasAnyModules()) { 113 Slog.i(TAG, "There are no HAL 2.x modules registered"); 114 return new AnnouncementAggregator(listener); 115 } 116 117 return mHal2.addAnnouncementListener(enabledTypes, listener); 118 } 119 } 120 } 121 } 122