Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 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.hal2; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.graphics.Bitmap; |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 21 | import android.hardware.broadcastradio.V2_0.ConfigFlag; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 22 | import android.hardware.broadcastradio.V2_0.ITunerSession; |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 23 | import android.hardware.broadcastradio.V2_0.Result; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 24 | import android.hardware.radio.ITuner; |
| 25 | import android.hardware.radio.ProgramSelector; |
| 26 | import android.hardware.radio.RadioManager; |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 27 | import android.media.AudioSystem; |
| 28 | import android.os.RemoteException; |
| 29 | import android.util.MutableBoolean; |
| 30 | import android.util.MutableInt; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 31 | import android.util.Slog; |
| 32 | |
| 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | import java.util.Objects; |
| 36 | |
| 37 | class TunerSession extends ITuner.Stub { |
| 38 | private static final String TAG = "BcRadio2Srv.session"; |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 39 | private static final String kAudioDeviceName = "Radio tuner source"; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 40 | |
| 41 | private final Object mLock = new Object(); |
| 42 | |
| 43 | private final ITunerSession mHwSession; |
| 44 | private final TunerCallback mCallback; |
| 45 | private boolean mIsClosed = false; |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 46 | private boolean mIsAudioConnected = false; |
| 47 | private boolean mIsMuted = false; |
| 48 | |
| 49 | // necessary only for older APIs compatibility |
| 50 | private RadioManager.BandConfig mDummyConfig = null; |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 51 | |
| 52 | TunerSession(@NonNull ITunerSession hwSession, @NonNull TunerCallback callback) { |
| 53 | mHwSession = Objects.requireNonNull(hwSession); |
| 54 | mCallback = Objects.requireNonNull(callback); |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 55 | notifyAudioServiceLocked(true); |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void close() { |
| 60 | synchronized (mLock) { |
| 61 | if (mIsClosed) return; |
| 62 | mIsClosed = true; |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 63 | notifyAudioServiceLocked(false); |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean isClosed() { |
| 69 | return mIsClosed; |
| 70 | } |
| 71 | |
| 72 | private void checkNotClosedLocked() { |
| 73 | if (mIsClosed) { |
| 74 | throw new IllegalStateException("Tuner is closed, no further operations are allowed"); |
| 75 | } |
| 76 | } |
| 77 | |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 78 | private void notifyAudioServiceLocked(boolean connected) { |
| 79 | if (mIsAudioConnected == connected) return; |
| 80 | |
| 81 | Slog.d(TAG, "Notifying AudioService about new state: " + connected); |
| 82 | int ret = AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_FM_TUNER, |
| 83 | connected ? AudioSystem.DEVICE_STATE_AVAILABLE : AudioSystem.DEVICE_STATE_UNAVAILABLE, |
| 84 | null, kAudioDeviceName); |
| 85 | |
| 86 | if (ret == AudioSystem.AUDIO_STATUS_OK) { |
| 87 | mIsAudioConnected = connected; |
| 88 | } else { |
| 89 | Slog.e(TAG, "Failed to notify AudioService about new state: " + connected); |
| 90 | } |
| 91 | } |
| 92 | |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 93 | @Override |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 94 | public void setConfiguration(RadioManager.BandConfig config) { |
| 95 | synchronized (mLock) { |
| 96 | checkNotClosedLocked(); |
| 97 | mDummyConfig = Objects.requireNonNull(config); |
| 98 | Slog.i(TAG, "Ignoring setConfiguration - not applicable for broadcastradio HAL 2.x"); |
| 99 | TunerCallback.dispatch(() -> mCallback.mClientCb.onConfigurationChanged(config)); |
| 100 | } |
| 101 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 102 | |
| 103 | @Override |
| 104 | public RadioManager.BandConfig getConfiguration() { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 105 | synchronized (mLock) { |
| 106 | checkNotClosedLocked(); |
| 107 | return mDummyConfig; |
| 108 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 112 | public void setMuted(boolean mute) { |
| 113 | synchronized (mLock) { |
| 114 | checkNotClosedLocked(); |
| 115 | if (mIsMuted == mute) return; |
| 116 | mIsMuted = mute; |
| 117 | notifyAudioServiceLocked(!mute); |
| 118 | } |
| 119 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 120 | |
| 121 | @Override |
| 122 | public boolean isMuted() { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 123 | synchronized (mLock) { |
| 124 | checkNotClosedLocked(); |
| 125 | return mIsMuted; |
| 126 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 130 | public void step(boolean directionDown, boolean skipSubChannel) { |
| 131 | synchronized (mLock) { |
| 132 | checkNotClosedLocked(); |
| 133 | } |
| 134 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 135 | |
| 136 | @Override |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 137 | public void scan(boolean directionDown, boolean skipSubChannel) { |
| 138 | synchronized (mLock) { |
| 139 | checkNotClosedLocked(); |
| 140 | } |
| 141 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 142 | |
| 143 | @Override |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 144 | public void tune(ProgramSelector selector) { |
| 145 | synchronized (mLock) { |
| 146 | checkNotClosedLocked(); |
| 147 | } |
| 148 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 149 | |
| 150 | @Override |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 151 | public void cancel() { |
| 152 | synchronized (mLock) { |
| 153 | checkNotClosedLocked(); |
| 154 | } |
| 155 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 156 | |
| 157 | @Override |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 158 | public void cancelAnnouncement() { |
| 159 | synchronized (mLock) { |
| 160 | checkNotClosedLocked(); |
| 161 | } |
| 162 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 163 | |
| 164 | @Override |
| 165 | public RadioManager.ProgramInfo getProgramInformation() { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 166 | synchronized (mLock) { |
| 167 | checkNotClosedLocked(); |
| 168 | return null; |
| 169 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public Bitmap getImage(int id) { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 174 | synchronized (mLock) { |
| 175 | checkNotClosedLocked(); |
| 176 | return null; |
| 177 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public boolean startBackgroundScan() { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 182 | Slog.i(TAG, "Explicit background scan trigger is not supported with HAL 2.x"); |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public List<RadioManager.ProgramInfo> getProgramList(Map vendorFilter) { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 188 | synchronized (mLock) { |
| 189 | checkNotClosedLocked(); |
| 190 | return null; |
| 191 | } |
| 192 | } |
| 193 | |
Tomasz Wasilczyk | ce40fe9 | 2018-01-04 20:52:39 -0800 | [diff] [blame] | 194 | @Override |
| 195 | public boolean isConfigFlagSupported(int flag) { |
| 196 | try { |
| 197 | isConfigFlagSet(flag); |
| 198 | return true; |
| 199 | } catch (IllegalStateException ex) { |
| 200 | return true; |
| 201 | } catch (UnsupportedOperationException ex) { |
| 202 | return false; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public boolean isConfigFlagSet(int flag) { |
| 208 | Slog.v(TAG, "isConfigFlagSet " + ConfigFlag.toString(flag)); |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 209 | synchronized (mLock) { |
| 210 | checkNotClosedLocked(); |
| 211 | |
| 212 | MutableInt halResult = new MutableInt(Result.UNKNOWN_ERROR); |
| 213 | MutableBoolean flagState = new MutableBoolean(false); |
| 214 | try { |
| 215 | mHwSession.getConfigFlag(flag, (int result, boolean value) -> { |
| 216 | halResult.value = result; |
| 217 | flagState.value = value; |
| 218 | }); |
| 219 | } catch (RemoteException ex) { |
Tomasz Wasilczyk | ce40fe9 | 2018-01-04 20:52:39 -0800 | [diff] [blame] | 220 | throw new RuntimeException("Failed to check flag " + ConfigFlag.toString(flag), ex); |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 221 | } |
Tomasz Wasilczyk | ce40fe9 | 2018-01-04 20:52:39 -0800 | [diff] [blame] | 222 | Convert.throwOnError("isConfigFlagSet", halResult.value); |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 223 | |
| 224 | return flagState.value; |
| 225 | } |
| 226 | } |
| 227 | |
Tomasz Wasilczyk | ce40fe9 | 2018-01-04 20:52:39 -0800 | [diff] [blame] | 228 | @Override |
| 229 | public void setConfigFlag(int flag, boolean value) { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 230 | Slog.v(TAG, "setConfigFlag " + ConfigFlag.toString(flag) + " = " + value); |
| 231 | synchronized (mLock) { |
| 232 | checkNotClosedLocked(); |
| 233 | |
| 234 | int halResult; |
| 235 | try { |
| 236 | halResult = mHwSession.setConfigFlag(flag, value); |
| 237 | } catch (RemoteException ex) { |
| 238 | throw new RuntimeException("Failed to set flag " + ConfigFlag.toString(flag), ex); |
| 239 | } |
| 240 | Convert.throwOnError("setConfigFlag", halResult); |
| 241 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 245 | public Map setParameters(Map parameters) { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 246 | synchronized (mLock) { |
| 247 | checkNotClosedLocked(); |
| 248 | return null; |
| 249 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public Map getParameters(List<String> keys) { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 254 | synchronized (mLock) { |
| 255 | checkNotClosedLocked(); |
| 256 | return null; |
| 257 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public boolean isAntennaConnected() { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 262 | synchronized (mLock) { |
| 263 | checkNotClosedLocked(); |
| 264 | return true; |
| 265 | } |
Tomasz Wasilczyk | f58305d | 2017-12-28 14:03:15 -0800 | [diff] [blame] | 266 | } |
| 267 | } |