| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.radio; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.graphics.Bitmap; |
| 22 |
import android.os.RemoteException; |
| 23 |
import android.util.Log; |
| 24 |
|
| 25 |
import java.util.List; |
| 26 |
import java.util.Map; |
| 27 |
import java.util.Objects; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class TunerAdapter extends RadioTuner { |
| 33 |
private static final String TAG = "BroadcastRadio.TunerAdapter"; |
| 34 |
|
| 35 |
@NonNull private final ITuner mTuner; |
| 36 |
@NonNull private final TunerCallbackAdapter mCallback; |
| 37 |
private boolean mIsClosed = false; |
| 38 |
|
| 39 |
private @RadioManager.Band int mBand; |
| 40 |
|
| 41 |
private ProgramList mLegacyListProxy; |
| 42 |
private Map<String, String> mLegacyListFilter; |
| 43 |
|
| 44 |
TunerAdapter(@NonNull ITuner tuner, @NonNull TunerCallbackAdapter callback, |
| 45 |
@RadioManager.Band int band) { |
| 46 |
mTuner = Objects.requireNonNull(tuner); |
| 47 |
mCallback = Objects.requireNonNull(callback); |
| 48 |
mBand = band; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public void close() { |
| 53 |
synchronized (mTuner) { |
| 54 |
if (mIsClosed) { |
| 55 |
Log.v(TAG, "Tuner is already closed"); |
| 56 |
return; |
| 57 |
} |
| 58 |
mIsClosed = true; |
| 59 |
if (mLegacyListProxy != null) { |
| 60 |
mLegacyListProxy.close(); |
| 61 |
mLegacyListProxy = null; |
| 62 |
} |
| 63 |
mCallback.close(); |
| 64 |
} |
| 65 |
try { |
| 66 |
mTuner.close(); |
| 67 |
} catch (RemoteException e) { |
| 68 |
Log.e(TAG, "Exception trying to close tuner", e); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public int setConfiguration(RadioManager.BandConfig config) { |
| 74 |
if (config == null) return RadioManager.STATUS_BAD_VALUE; |
| 75 |
try { |
| 76 |
mTuner.setConfiguration(config); |
| 77 |
mBand = config.getType(); |
| 78 |
return RadioManager.STATUS_OK; |
| 79 |
} catch (IllegalArgumentException e) { |
| 80 |
Log.e(TAG, "Can't set configuration", e); |
| 81 |
return RadioManager.STATUS_BAD_VALUE; |
| 82 |
} catch (RemoteException e) { |
| 83 |
Log.e(TAG, "service died", e); |
| 84 |
return RadioManager.STATUS_DEAD_OBJECT; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public int getConfiguration(RadioManager.BandConfig[] config) { |
| 90 |
if (config == null || config.length != 1) { |
| 91 |
throw new IllegalArgumentException("The argument must be an array of length 1"); |
| 92 |
} |
| 93 |
try { |
| 94 |
config[0] = mTuner.getConfiguration(); |
| 95 |
return RadioManager.STATUS_OK; |
| 96 |
} catch (RemoteException e) { |
| 97 |
Log.e(TAG, "service died", e); |
| 98 |
return RadioManager.STATUS_DEAD_OBJECT; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public int setMute(boolean mute) { |
| 104 |
try { |
| 105 |
mTuner.setMuted(mute); |
| 106 |
} catch (IllegalStateException e) { |
| 107 |
Log.e(TAG, "Can't set muted", e); |
| 108 |
return RadioManager.STATUS_ERROR; |
| 109 |
} catch (RemoteException e) { |
| 110 |
Log.e(TAG, "service died", e); |
| 111 |
return RadioManager.STATUS_DEAD_OBJECT; |
| 112 |
} |
| 113 |
return RadioManager.STATUS_OK; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public boolean getMute() { |
| 118 |
try { |
| 119 |
return mTuner.isMuted(); |
| 120 |
} catch (RemoteException e) { |
| 121 |
Log.e(TAG, "service died", e); |
| 122 |
return true; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public int step(int direction, boolean skipSubChannel) { |
| 128 |
try { |
| 129 |
mTuner.step(direction == RadioTuner.DIRECTION_DOWN, skipSubChannel); |
| 130 |
} catch (IllegalStateException e) { |
| 131 |
Log.e(TAG, "Can't step", e); |
| 132 |
return RadioManager.STATUS_INVALID_OPERATION; |
| 133 |
} catch (RemoteException e) { |
| 134 |
Log.e(TAG, "service died", e); |
| 135 |
return RadioManager.STATUS_DEAD_OBJECT; |
| 136 |
} |
| 137 |
return RadioManager.STATUS_OK; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public int scan(int direction, boolean skipSubChannel) { |
| 142 |
try { |
| 143 |
mTuner.scan(direction == RadioTuner.DIRECTION_DOWN, skipSubChannel); |
| 144 |
} catch (IllegalStateException e) { |
| 145 |
Log.e(TAG, "Can't scan", e); |
| 146 |
return RadioManager.STATUS_INVALID_OPERATION; |
| 147 |
} catch (RemoteException e) { |
| 148 |
Log.e(TAG, "service died", e); |
| 149 |
return RadioManager.STATUS_DEAD_OBJECT; |
| 150 |
} |
| 151 |
return RadioManager.STATUS_OK; |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public int tune(int channel, int subChannel) { |
| 156 |
try { |
| 157 |
mTuner.tune(ProgramSelector.createAmFmSelector(mBand, channel, subChannel)); |
| 158 |
} catch (IllegalStateException e) { |
| 159 |
Log.e(TAG, "Can't tune", e); |
| 160 |
return RadioManager.STATUS_INVALID_OPERATION; |
| 161 |
} catch (IllegalArgumentException e) { |
| 162 |
Log.e(TAG, "Can't tune", e); |
| 163 |
return RadioManager.STATUS_BAD_VALUE; |
| 164 |
} catch (RemoteException e) { |
| 165 |
Log.e(TAG, "service died", e); |
| 166 |
return RadioManager.STATUS_DEAD_OBJECT; |
| 167 |
} |
| 168 |
return RadioManager.STATUS_OK; |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public void tune(@NonNull ProgramSelector selector) { |
| 173 |
try { |
| 174 |
mTuner.tune(selector); |
| 175 |
} catch (RemoteException e) { |
| 176 |
throw new RuntimeException("service died", e); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
public int cancel() { |
| 182 |
try { |
| 183 |
mTuner.cancel(); |
| 184 |
} catch (IllegalStateException e) { |
| 185 |
Log.e(TAG, "Can't cancel", e); |
| 186 |
return RadioManager.STATUS_INVALID_OPERATION; |
| 187 |
} catch (RemoteException e) { |
| 188 |
Log.e(TAG, "service died", e); |
| 189 |
return RadioManager.STATUS_DEAD_OBJECT; |
| 190 |
} |
| 191 |
return RadioManager.STATUS_OK; |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public void cancelAnnouncement() { |
| 196 |
try { |
| 197 |
mTuner.cancelAnnouncement(); |
| 198 |
} catch (RemoteException e) { |
| 199 |
throw new RuntimeException("service died", e); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public int getProgramInformation(RadioManager.ProgramInfo[] info) { |
| 205 |
if (info == null || info.length != 1) { |
| 206 |
Log.e(TAG, "The argument must be an array of length 1"); |
| 207 |
return RadioManager.STATUS_BAD_VALUE; |
| 208 |
} |
| 209 |
|
| 210 |
RadioManager.ProgramInfo current = mCallback.getCurrentProgramInformation(); |
| 211 |
if (current == null) { |
| 212 |
Log.w(TAG, "Didn't get program info yet"); |
| 213 |
return RadioManager.STATUS_INVALID_OPERATION; |
| 214 |
} |
| 215 |
info[0] = current; |
| 216 |
return RadioManager.STATUS_OK; |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public @Nullable Bitmap getMetadataImage(int id) { |
| 221 |
try { |
| 222 |
return mTuner.getImage(id); |
| 223 |
} catch (RemoteException e) { |
| 224 |
throw new RuntimeException("service died", e); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public boolean startBackgroundScan() { |
| 230 |
try { |
| 231 |
return mTuner.startBackgroundScan(); |
| 232 |
} catch (RemoteException e) { |
| 233 |
throw new RuntimeException("service died", e); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public @NonNull List<RadioManager.ProgramInfo> |
| 239 |
getProgramList(@Nullable Map<String, String> vendorFilter) { |
| 240 |
synchronized (mTuner) { |
| 241 |
if (mLegacyListProxy == null || !Objects.equals(mLegacyListFilter, vendorFilter)) { |
| 242 |
Log.i(TAG, "Program list filter has changed, requesting new list"); |
| 243 |
mLegacyListProxy = new ProgramList(); |
| 244 |
mLegacyListFilter = vendorFilter; |
| 245 |
|
| 246 |
mCallback.clearLastCompleteList(); |
| 247 |
mCallback.setProgramListObserver(mLegacyListProxy, () -> { }); |
| 248 |
try { |
| 249 |
mTuner.startProgramListUpdates(new ProgramList.Filter(vendorFilter)); |
| 250 |
} catch (RemoteException ex) { |
| 251 |
throw new RuntimeException("service died", ex); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
List<RadioManager.ProgramInfo> list = mCallback.getLastCompleteList(); |
| 256 |
if (list == null) throw new IllegalStateException("Program list is not ready yet"); |
| 257 |
return list; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public @Nullable ProgramList getDynamicProgramList(@Nullable ProgramList.Filter filter) { |
| 263 |
synchronized (mTuner) { |
| 264 |
if (mLegacyListProxy != null) { |
| 265 |
mLegacyListProxy.close(); |
| 266 |
mLegacyListProxy = null; |
| 267 |
} |
| 268 |
mLegacyListFilter = null; |
| 269 |
|
| 270 |
ProgramList list = new ProgramList(); |
| 271 |
mCallback.setProgramListObserver(list, () -> { |
| 272 |
try { |
| 273 |
mTuner.stopProgramListUpdates(); |
| 274 |
} catch (IllegalStateException ex) { |
| 275 |
|
| 276 |
} catch (RemoteException ex) { |
| 277 |
Log.e(TAG, "Couldn't stop program list updates", ex); |
| 278 |
} |
| 279 |
}); |
| 280 |
|
| 281 |
try { |
| 282 |
mTuner.startProgramListUpdates(filter); |
| 283 |
} catch (UnsupportedOperationException ex) { |
| 284 |
Log.i(TAG, "Program list is not supported with this hardware"); |
| 285 |
return null; |
| 286 |
} catch (RemoteException ex) { |
| 287 |
mCallback.setProgramListObserver(null, () -> { }); |
| 288 |
throw new RuntimeException("service died", ex); |
| 289 |
} |
| 290 |
|
| 291 |
return list; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
@Override |
| 296 |
public boolean isAnalogForced() { |
| 297 |
try { |
| 298 |
return isConfigFlagSet(RadioManager.CONFIG_FORCE_ANALOG); |
| 299 |
} catch (UnsupportedOperationException ex) { |
| 300 |
throw new IllegalStateException(ex); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
@Override |
| 305 |
public void setAnalogForced(boolean isForced) { |
| 306 |
try { |
| 307 |
setConfigFlag(RadioManager.CONFIG_FORCE_ANALOG, isForced); |
| 308 |
} catch (UnsupportedOperationException ex) { |
| 309 |
throw new IllegalStateException(ex); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public boolean isConfigFlagSupported(@RadioManager.ConfigFlag int flag) { |
| 315 |
try { |
| 316 |
return mTuner.isConfigFlagSupported(flag); |
| 317 |
} catch (RemoteException e) { |
| 318 |
throw new RuntimeException("service died", e); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
@Override |
| 323 |
public boolean isConfigFlagSet(@RadioManager.ConfigFlag int flag) { |
| 324 |
try { |
| 325 |
return mTuner.isConfigFlagSet(flag); |
| 326 |
} catch (RemoteException e) { |
| 327 |
throw new RuntimeException("service died", e); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
@Override |
| 332 |
public void setConfigFlag(@RadioManager.ConfigFlag int flag, boolean value) { |
| 333 |
try { |
| 334 |
mTuner.setConfigFlag(flag, value); |
| 335 |
} catch (RemoteException e) { |
| 336 |
throw new RuntimeException("service died", e); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
@Override |
| 341 |
public @NonNull Map<String, String> setParameters(@NonNull Map<String, String> parameters) { |
| 342 |
try { |
| 343 |
return mTuner.setParameters(Objects.requireNonNull(parameters)); |
| 344 |
} catch (RemoteException e) { |
| 345 |
throw new RuntimeException("service died", e); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
@Override |
| 350 |
public @NonNull Map<String, String> getParameters(@NonNull List<String> keys) { |
| 351 |
try { |
| 352 |
return mTuner.getParameters(Objects.requireNonNull(keys)); |
| 353 |
} catch (RemoteException e) { |
| 354 |
throw new RuntimeException("service died", e); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
@Override |
| 359 |
public boolean isAntennaConnected() { |
| 360 |
return mCallback.isAntennaConnected(); |
| 361 |
} |
| 362 |
|
| 363 |
@Override |
| 364 |
public boolean hasControl() { |
| 365 |
try { |
| 366 |
|
| 367 |
return !mTuner.isClosed(); |
| 368 |
} catch (RemoteException e) { |
| 369 |
return false; |
| 370 |
} |
| 371 |
} |
| 372 |
} |