Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [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 android.hardware.radio; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Tomasz Wasilczyk | 4482b14 | 2017-07-17 13:57:12 -0700 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 22 | import android.os.RemoteException; |
| 23 | import android.util.Log; |
| 24 | |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 25 | import java.util.List; |
Tomasz Wasilczyk | 0f1776d | 2017-08-03 11:03:49 -0700 | [diff] [blame] | 26 | import java.util.Map; |
Tomasz Wasilczyk | 8e932c6 | 2017-11-17 16:18:40 +0000 | [diff] [blame] | 27 | import java.util.Objects; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Implements the RadioTuner interface by forwarding calls to radio service. |
| 31 | */ |
| 32 | class TunerAdapter extends RadioTuner { |
Tomasz Wasilczyk | 6b4b646 | 2017-07-19 10:52:28 -0700 | [diff] [blame] | 33 | private static final String TAG = "BroadcastRadio.TunerAdapter"; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 34 | |
| 35 | @NonNull private final ITuner mTuner; |
Tomasz Wasilczyk | d7c21d3 | 2017-04-17 17:02:06 -0700 | [diff] [blame] | 36 | private boolean mIsClosed = false; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 37 | |
Tomasz Wasilczyk | 8cfb0e8 | 2017-07-12 13:59:20 -0700 | [diff] [blame] | 38 | private @RadioManager.Band int mBand; |
| 39 | |
| 40 | TunerAdapter(ITuner tuner, @RadioManager.Band int band) { |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 41 | if (tuner == null) { |
| 42 | throw new NullPointerException(); |
| 43 | } |
| 44 | mTuner = tuner; |
Tomasz Wasilczyk | 8cfb0e8 | 2017-07-12 13:59:20 -0700 | [diff] [blame] | 45 | mBand = band; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void close() { |
Tomasz Wasilczyk | d7c21d3 | 2017-04-17 17:02:06 -0700 | [diff] [blame] | 50 | synchronized (mTuner) { |
| 51 | if (mIsClosed) { |
Tomasz Wasilczyk | 1475237 | 2017-06-21 11:58:21 -0700 | [diff] [blame] | 52 | Log.v(TAG, "Tuner is already closed"); |
Tomasz Wasilczyk | d7c21d3 | 2017-04-17 17:02:06 -0700 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | mIsClosed = true; |
| 56 | } |
| 57 | try { |
| 58 | mTuner.close(); |
| 59 | } catch (RemoteException e) { |
| 60 | Log.e(TAG, "Exception trying to close tuner", e); |
| 61 | } |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int setConfiguration(RadioManager.BandConfig config) { |
Tomasz Wasilczyk | ca98cde | 2018-01-04 12:26:40 -0800 | [diff] [blame] | 66 | if (config == null) return RadioManager.STATUS_BAD_VALUE; |
Tomasz Wasilczyk | 8b6db4f | 2017-05-01 09:28:36 -0700 | [diff] [blame] | 67 | try { |
| 68 | mTuner.setConfiguration(config); |
Tomasz Wasilczyk | 8cfb0e8 | 2017-07-12 13:59:20 -0700 | [diff] [blame] | 69 | mBand = config.getType(); |
Tomasz Wasilczyk | 8b6db4f | 2017-05-01 09:28:36 -0700 | [diff] [blame] | 70 | return RadioManager.STATUS_OK; |
| 71 | } catch (IllegalArgumentException e) { |
| 72 | Log.e(TAG, "Can't set configuration", e); |
| 73 | return RadioManager.STATUS_BAD_VALUE; |
| 74 | } catch (RemoteException e) { |
Tomasz Wasilczyk | 9fa0287 | 2017-05-03 09:09:57 -0700 | [diff] [blame] | 75 | Log.e(TAG, "service died", e); |
| 76 | return RadioManager.STATUS_DEAD_OBJECT; |
Tomasz Wasilczyk | 8b6db4f | 2017-05-01 09:28:36 -0700 | [diff] [blame] | 77 | } |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int getConfiguration(RadioManager.BandConfig[] config) { |
Tomasz Wasilczyk | 8b6db4f | 2017-05-01 09:28:36 -0700 | [diff] [blame] | 82 | if (config == null || config.length != 1) { |
| 83 | throw new IllegalArgumentException("The argument must be an array of length 1"); |
| 84 | } |
| 85 | try { |
| 86 | config[0] = mTuner.getConfiguration(); |
| 87 | return RadioManager.STATUS_OK; |
| 88 | } catch (RemoteException e) { |
Tomasz Wasilczyk | 9fa0287 | 2017-05-03 09:09:57 -0700 | [diff] [blame] | 89 | Log.e(TAG, "service died", e); |
| 90 | return RadioManager.STATUS_DEAD_OBJECT; |
Tomasz Wasilczyk | 8b6db4f | 2017-05-01 09:28:36 -0700 | [diff] [blame] | 91 | } |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public int setMute(boolean mute) { |
Tomasz Wasilczyk | 9fa0287 | 2017-05-03 09:09:57 -0700 | [diff] [blame] | 96 | try { |
| 97 | mTuner.setMuted(mute); |
| 98 | } catch (IllegalStateException e) { |
| 99 | Log.e(TAG, "Can't set muted", e); |
| 100 | return RadioManager.STATUS_ERROR; |
| 101 | } catch (RemoteException e) { |
| 102 | Log.e(TAG, "service died", e); |
| 103 | return RadioManager.STATUS_DEAD_OBJECT; |
| 104 | } |
| 105 | return RadioManager.STATUS_OK; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean getMute() { |
Tomasz Wasilczyk | 9fa0287 | 2017-05-03 09:09:57 -0700 | [diff] [blame] | 110 | try { |
| 111 | return mTuner.isMuted(); |
| 112 | } catch (RemoteException e) { |
| 113 | Log.e(TAG, "service died", e); |
| 114 | return true; |
| 115 | } |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public int step(int direction, boolean skipSubChannel) { |
Tomasz Wasilczyk | 2383793 | 2017-05-05 08:42:10 -0700 | [diff] [blame] | 120 | try { |
| 121 | mTuner.step(direction == RadioTuner.DIRECTION_DOWN, skipSubChannel); |
| 122 | } catch (IllegalStateException e) { |
| 123 | Log.e(TAG, "Can't step", e); |
| 124 | return RadioManager.STATUS_INVALID_OPERATION; |
| 125 | } catch (RemoteException e) { |
| 126 | Log.e(TAG, "service died", e); |
| 127 | return RadioManager.STATUS_DEAD_OBJECT; |
| 128 | } |
| 129 | return RadioManager.STATUS_OK; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public int scan(int direction, boolean skipSubChannel) { |
Tomasz Wasilczyk | 2383793 | 2017-05-05 08:42:10 -0700 | [diff] [blame] | 134 | try { |
| 135 | mTuner.scan(direction == RadioTuner.DIRECTION_DOWN, skipSubChannel); |
| 136 | } catch (IllegalStateException e) { |
| 137 | Log.e(TAG, "Can't scan", e); |
| 138 | return RadioManager.STATUS_INVALID_OPERATION; |
| 139 | } catch (RemoteException e) { |
| 140 | Log.e(TAG, "service died", e); |
| 141 | return RadioManager.STATUS_DEAD_OBJECT; |
| 142 | } |
| 143 | return RadioManager.STATUS_OK; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public int tune(int channel, int subChannel) { |
Tomasz Wasilczyk | 37d986d | 2017-05-08 10:41:32 -0700 | [diff] [blame] | 148 | try { |
Tomasz Wasilczyk | 8cfb0e8 | 2017-07-12 13:59:20 -0700 | [diff] [blame] | 149 | mTuner.tune(ProgramSelector.createAmFmSelector(mBand, channel, subChannel)); |
Tomasz Wasilczyk | 37d986d | 2017-05-08 10:41:32 -0700 | [diff] [blame] | 150 | } catch (IllegalStateException e) { |
| 151 | Log.e(TAG, "Can't tune", e); |
| 152 | return RadioManager.STATUS_INVALID_OPERATION; |
| 153 | } catch (IllegalArgumentException e) { |
| 154 | Log.e(TAG, "Can't tune", e); |
| 155 | return RadioManager.STATUS_BAD_VALUE; |
| 156 | } catch (RemoteException e) { |
| 157 | Log.e(TAG, "service died", e); |
| 158 | return RadioManager.STATUS_DEAD_OBJECT; |
| 159 | } |
| 160 | return RadioManager.STATUS_OK; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
Tomasz Wasilczyk | 8cfb0e8 | 2017-07-12 13:59:20 -0700 | [diff] [blame] | 164 | public void tune(@NonNull ProgramSelector selector) { |
| 165 | try { |
| 166 | mTuner.tune(selector); |
| 167 | } catch (RemoteException e) { |
| 168 | throw new RuntimeException("service died", e); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | @Override |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 173 | public int cancel() { |
Tomasz Wasilczyk | 37d986d | 2017-05-08 10:41:32 -0700 | [diff] [blame] | 174 | try { |
| 175 | mTuner.cancel(); |
| 176 | } catch (IllegalStateException e) { |
| 177 | Log.e(TAG, "Can't cancel", e); |
| 178 | return RadioManager.STATUS_INVALID_OPERATION; |
| 179 | } catch (RemoteException e) { |
| 180 | Log.e(TAG, "service died", e); |
| 181 | return RadioManager.STATUS_DEAD_OBJECT; |
| 182 | } |
| 183 | return RadioManager.STATUS_OK; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
Tomasz Wasilczyk | c4cd823 | 2017-07-14 10:46:15 -0700 | [diff] [blame] | 187 | public void cancelAnnouncement() { |
| 188 | try { |
| 189 | mTuner.cancelAnnouncement(); |
| 190 | } catch (RemoteException e) { |
| 191 | throw new RuntimeException("service died", e); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 196 | public int getProgramInformation(RadioManager.ProgramInfo[] info) { |
| 197 | if (info == null || info.length != 1) { |
| 198 | throw new IllegalArgumentException("The argument must be an array of length 1"); |
| 199 | } |
| 200 | try { |
Tomasz Wasilczyk | 37d986d | 2017-05-08 10:41:32 -0700 | [diff] [blame] | 201 | info[0] = mTuner.getProgramInformation(); |
| 202 | return RadioManager.STATUS_OK; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 203 | } catch (RemoteException e) { |
Tomasz Wasilczyk | 9fa0287 | 2017-05-03 09:09:57 -0700 | [diff] [blame] | 204 | Log.e(TAG, "service died", e); |
| 205 | return RadioManager.STATUS_DEAD_OBJECT; |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | @Override |
Tomasz Wasilczyk | 4482b14 | 2017-07-17 13:57:12 -0700 | [diff] [blame] | 210 | public @Nullable Bitmap getMetadataImage(int id) { |
| 211 | try { |
| 212 | return mTuner.getImage(id); |
| 213 | } catch (RemoteException e) { |
| 214 | throw new RuntimeException("service died", e); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | @Override |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 219 | public boolean startBackgroundScan() { |
Tomasz Wasilczyk | d3d53f6 | 2017-05-15 12:55:28 -0700 | [diff] [blame] | 220 | try { |
| 221 | return mTuner.startBackgroundScan(); |
| 222 | } catch (RemoteException e) { |
| 223 | throw new RuntimeException("service died", e); |
| 224 | } |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
Tomasz Wasilczyk | 0f1776d | 2017-08-03 11:03:49 -0700 | [diff] [blame] | 228 | public @NonNull List<RadioManager.ProgramInfo> |
| 229 | getProgramList(@Nullable Map<String, String> vendorFilter) { |
Tomasz Wasilczyk | d3d53f6 | 2017-05-15 12:55:28 -0700 | [diff] [blame] | 230 | try { |
Tomasz Wasilczyk | 0f1776d | 2017-08-03 11:03:49 -0700 | [diff] [blame] | 231 | return mTuner.getProgramList(vendorFilter); |
Tomasz Wasilczyk | d3d53f6 | 2017-05-15 12:55:28 -0700 | [diff] [blame] | 232 | } catch (RemoteException e) { |
| 233 | throw new RuntimeException("service died", e); |
| 234 | } |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public boolean isAnalogForced() { |
Tomasz Wasilczyk | d3d53f6 | 2017-05-15 12:55:28 -0700 | [diff] [blame] | 239 | try { |
| 240 | return mTuner.isAnalogForced(); |
| 241 | } catch (RemoteException e) { |
| 242 | throw new RuntimeException("service died", e); |
| 243 | } |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public void setAnalogForced(boolean isForced) { |
Tomasz Wasilczyk | d3d53f6 | 2017-05-15 12:55:28 -0700 | [diff] [blame] | 248 | try { |
| 249 | mTuner.setAnalogForced(isForced); |
| 250 | } catch (RemoteException e) { |
| 251 | throw new RuntimeException("service died", e); |
| 252 | } |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
Tomasz Wasilczyk | 8e932c6 | 2017-11-17 16:18:40 +0000 | [diff] [blame] | 256 | public @NonNull Map<String, String> setParameters(@NonNull Map<String, String> parameters) { |
| 257 | try { |
| 258 | return mTuner.setParameters(Objects.requireNonNull(parameters)); |
| 259 | } catch (RemoteException e) { |
| 260 | throw new RuntimeException("service died", e); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public @NonNull Map<String, String> getParameters(@NonNull List<String> keys) { |
| 266 | try { |
| 267 | return mTuner.getParameters(Objects.requireNonNull(keys)); |
| 268 | } catch (RemoteException e) { |
| 269 | throw new RuntimeException("service died", e); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | @Override |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 274 | public boolean isAntennaConnected() { |
Tomasz Wasilczyk | 39ac214 | 2017-05-17 14:55:17 -0700 | [diff] [blame] | 275 | try { |
| 276 | return mTuner.isAntennaConnected(); |
| 277 | } catch (RemoteException e) { |
| 278 | throw new RuntimeException("service died", e); |
| 279 | } |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public boolean hasControl() { |
Tomasz Wasilczyk | 468a53b | 2017-06-19 15:19:14 -0700 | [diff] [blame] | 284 | try { |
| 285 | // don't rely on mIsClosed, as tuner might get closed internally |
| 286 | return !mTuner.isClosed(); |
| 287 | } catch (RemoteException e) { |
| 288 | return false; |
| 289 | } |
Tomasz Wasilczyk | 347192e | 2017-04-04 11:13:44 -0700 | [diff] [blame] | 290 | } |
| 291 | } |