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