Debian Codegraph @

core/java/android/hardware/radio/RadioManager.java

raw symbols SCIP
1 /**
2 * Copyright (C) 2015 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.Manifest;
20 import android.annotation.CallbackExecutor;
21 import android.annotation.IntDef;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.RequiresFeature;
25 import android.annotation.RequiresPermission;
26 import android.annotation.SystemApi;
27 import android.annotation.SystemService;
28 import android.content.Context;
29 import android.content.pm.PackageManager;
30 import android.os.Handler;
31 import android.os.Parcel;
32 import android.os.Parcelable;
33 import android.os.RemoteException;
34 import android.os.ServiceManager;
35 import android.os.ServiceManager.ServiceNotFoundException;
36 import android.text.TextUtils;
37 import android.util.Log;
38
39 import com.android.internal.util.Preconditions;
40
41 import java.lang.annotation.Retention;
42 import java.lang.annotation.RetentionPolicy;
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.Objects;
50 import java.util.Set;
51 import java.util.concurrent.Executor;
52 import java.util.stream.Collectors;
53
54 /**
55 * The RadioManager class allows to control a broadcast radio tuner present on the device.
56 * It provides data structures and methods to query for available radio modules, list their
57 * properties and open an interface to control tuning operations and receive callbacks when
58 * asynchronous operations complete or events occur.
59 * @hide
60 */
61 @SystemApi
62 @SystemService(Context.RADIO_SERVICE)
63 @RequiresFeature(PackageManager.FEATURE_BROADCAST_RADIO)
64 public class RadioManager {
65 private static final String TAG = "BroadcastRadio.manager";
66
67 /** Method return status: successful operation */
68 public static final int STATUS_OK = 0;
69 /** Method return status: unspecified error */
70 public static final int STATUS_ERROR = Integer.MIN_VALUE;
71 /** Method return status: permission denied */
72 public static final int STATUS_PERMISSION_DENIED = -1;
73 /** Method return status: initialization failure */
74 public static final int STATUS_NO_INIT = -19;
75 /** Method return status: invalid argument provided */
76 public static final int STATUS_BAD_VALUE = -22;
77 /** Method return status: cannot reach service */
78 public static final int STATUS_DEAD_OBJECT = -32;
79 /** Method return status: invalid or out of sequence operation */
80 public static final int STATUS_INVALID_OPERATION = -38;
81 /** Method return status: time out before operation completion */
82 public static final int STATUS_TIMED_OUT = -110;
83
84
85 // keep in sync with radio_class_t in /system/core/incluse/system/radio.h
86 /** Radio module class supporting FM (including HD radio) and AM */
87 public static final int CLASS_AM_FM = 0;
88 /** Radio module class supporting satellite radio */
89 public static final int CLASS_SAT = 1;
90 /** Radio module class supporting Digital terrestrial radio */
91 public static final int CLASS_DT = 2;
92
93 public static final int BAND_INVALID = -1;
94 /** AM radio band (LW/MW/SW).
95 * @see BandDescriptor */
96 public static final int BAND_AM = 0;
97 /** FM radio band.
98 * @see BandDescriptor */
99 public static final int BAND_FM = 1;
100 /** FM HD radio or DRM band.
101 * @see BandDescriptor */
102 public static final int BAND_FM_HD = 2;
103 /** AM HD radio or DRM band.
104 * @see BandDescriptor */
105 public static final int BAND_AM_HD = 3;
106 @IntDef(prefix = { "BAND_" }, value = {
107 BAND_INVALID,
108 BAND_AM,
109 BAND_FM,
110 BAND_AM_HD,
111 BAND_FM_HD,
112 })
113 @Retention(RetentionPolicy.SOURCE)
114 public @interface Band {}
115
116 // keep in sync with radio_region_t in /system/core/incluse/system/radio.h
117 /** Africa, Europe.
118 * @see BandDescriptor */
119 public static final int REGION_ITU_1 = 0;
120 /** Americas.
121 * @see BandDescriptor */
122 public static final int REGION_ITU_2 = 1;
123 /** Russia.
124 * @see BandDescriptor */
125 public static final int REGION_OIRT = 2;
126 /** Japan.
127 * @see BandDescriptor */
128 public static final int REGION_JAPAN = 3;
129 /** Korea.
130 * @see BandDescriptor */
131 public static final int REGION_KOREA = 4;
132
133 /**
134 * Forces mono audio stream reception.
135 *
136 * Analog broadcasts can recover poor reception conditions by jointing
137 * stereo channels into one. Mainly for, but not limited to AM/FM.
138 */
139 public static final int CONFIG_FORCE_MONO = 1;
140 /**
141 * Forces the analog playback for the supporting radio technology.
142 *
143 * User may disable digital playback for FM HD Radio or hybrid FM/DAB with
144 * this option. This is purely user choice, ie. does not reflect digital-
145 * analog handover state managed from the HAL implementation side.
146 *
147 * Some radio technologies may not support this, ie. DAB.
148 */
149 public static final int CONFIG_FORCE_ANALOG = 2;
150 /**
151 * Forces the digital playback for the supporting radio technology.
152 *
153 * User may disable digital-analog handover that happens with poor
154 * reception conditions. With digital forced, the radio will remain silent
155 * instead of switching to analog channel if it's available. This is purely
156 * user choice, it does not reflect the actual state of handover.
157 */
158 public static final int CONFIG_FORCE_DIGITAL = 3;
159 /**
160 * RDS Alternative Frequencies.
161 *
162 * If set and the currently tuned RDS station broadcasts on multiple
163 * channels, radio tuner automatically switches to the best available
164 * alternative.
165 */
166 public static final int CONFIG_RDS_AF = 4;
167 /**
168 * RDS region-specific program lock-down.
169 *
170 * Allows user to lock to the current region as they move into the
171 * other region.
172 */
173 public static final int CONFIG_RDS_REG = 5;
174 /** Enables DAB-DAB hard- and implicit-linking (the same content). */
175 public static final int CONFIG_DAB_DAB_LINKING = 6;
176 /** Enables DAB-FM hard- and implicit-linking (the same content). */
177 public static final int CONFIG_DAB_FM_LINKING = 7;
178 /** Enables DAB-DAB soft-linking (related content). */
179 public static final int CONFIG_DAB_DAB_SOFT_LINKING = 8;
180 /** Enables DAB-FM soft-linking (related content). */
181 public static final int CONFIG_DAB_FM_SOFT_LINKING = 9;
182
183 /** @hide */
184 @IntDef(prefix = { "CONFIG_" }, value = {
185 CONFIG_FORCE_MONO,
186 CONFIG_FORCE_ANALOG,
187 CONFIG_FORCE_DIGITAL,
188 CONFIG_RDS_AF,
189 CONFIG_RDS_REG,
190 CONFIG_DAB_DAB_LINKING,
191 CONFIG_DAB_FM_LINKING,
192 CONFIG_DAB_DAB_SOFT_LINKING,
193 CONFIG_DAB_FM_SOFT_LINKING,
194 })
195 @Retention(RetentionPolicy.SOURCE)
196 public @interface ConfigFlag {}
197
198 /*****************************************************************************
199 * Lists properties, options and radio bands supported by a given broadcast radio module.
200 * Each module has a unique ID used to address it when calling RadioManager APIs.
201 * Module properties are returned by {@link #listModules(List <ModuleProperties>)} method.
202 ****************************************************************************/
203 public static class ModuleProperties implements Parcelable {
204
205 private final int mId;
206 @NonNull private final String mServiceName;
207 private final int mClassId;
208 private final String mImplementor;
209 private final String mProduct;
210 private final String mVersion;
211 private final String mSerial;
212 private final int mNumTuners;
213 private final int mNumAudioSources;
214 private final boolean mIsInitializationRequired;
215 private final boolean mIsCaptureSupported;
216 private final BandDescriptor[] mBands;
217 private final boolean mIsBgScanSupported;
218 private final Set<Integer> mSupportedProgramTypes;
219 private final Set<Integer> mSupportedIdentifierTypes;
220 @Nullable private final Map<String, Integer> mDabFrequencyTable;
221 @NonNull private final Map<String, String> mVendorInfo;
222
223 /** @hide */
224 public ModuleProperties(int id, String serviceName, int classId, String implementor,
225 String product, String version, String serial, int numTuners, int numAudioSources,
226 boolean isInitializationRequired, boolean isCaptureSupported,
227 BandDescriptor[] bands, boolean isBgScanSupported,
228 @ProgramSelector.ProgramType int[] supportedProgramTypes,
229 @ProgramSelector.IdentifierType int[] supportedIdentifierTypes,
230 @Nullable Map<String, Integer> dabFrequencyTable,
231 Map<String, String> vendorInfo) {
232 mId = id;
233 mServiceName = TextUtils.isEmpty(serviceName) ? "default" : serviceName;
234 mClassId = classId;
235 mImplementor = implementor;
236 mProduct = product;
237 mVersion = version;
238 mSerial = serial;
239 mNumTuners = numTuners;
240 mNumAudioSources = numAudioSources;
241 mIsInitializationRequired = isInitializationRequired;
242 mIsCaptureSupported = isCaptureSupported;
243 mBands = bands;
244 mIsBgScanSupported = isBgScanSupported;
245 mSupportedProgramTypes = arrayToSet(supportedProgramTypes);
246 mSupportedIdentifierTypes = arrayToSet(supportedIdentifierTypes);
247 if (dabFrequencyTable != null) {
248 for (Map.Entry<String, Integer> entry : dabFrequencyTable.entrySet()) {
249 Objects.requireNonNull(entry.getKey());
250 Objects.requireNonNull(entry.getValue());
251 }
252 }
253 mDabFrequencyTable = dabFrequencyTable;
254 mVendorInfo = (vendorInfo == null) ? new HashMap<>() : vendorInfo;
255 }
256
257 private static Set<Integer> arrayToSet(int[] arr) {
258 return Arrays.stream(arr).boxed().collect(Collectors.toSet());
259 }
260
261 private static int[] setToArray(Set<Integer> set) {
262 return set.stream().mapToInt(Integer::intValue).toArray();
263 }
264
265 /** Unique module identifier provided by the native service.
266 * For use with {@link #openTuner(int, BandConfig, boolean, Callback, Handler)}.
267 * @return the radio module unique identifier.
268 */
269 public int getId() {
270 return mId;
271 }
272
273 /**
274 * Module service (driver) name as registered with HIDL.
275 * @return the module service name.
276 */
277 public @NonNull String getServiceName() {
278 return mServiceName;
279 }
280
281 /** Module class identifier: {@link #CLASS_AM_FM}, {@link #CLASS_SAT}, {@link #CLASS_DT}
282 * @return the radio module class identifier.
283 */
284 public int getClassId() {
285 return mClassId;
286 }
287
288 /** Human readable broadcast radio module implementor
289 * @return the name of the radio module implementator.
290 */
291 public String getImplementor() {
292 return mImplementor;
293 }
294
295 /** Human readable broadcast radio module product name
296 * @return the radio module product name.
297 */
298 public String getProduct() {
299 return mProduct;
300 }
301
302 /** Human readable broadcast radio module version number
303 * @return the radio module version.
304 */
305 public String getVersion() {
306 return mVersion;
307 }
308
309 /** Radio module serial number.
310 * Can be used for subscription services.
311 * @return the radio module serial number.
312 */
313 public String getSerial() {
314 return mSerial;
315 }
316
317 /** Number of tuners available.
318 * This is the number of tuners that can be open simultaneously.
319 * @return the number of tuners supported.
320 */
321 public int getNumTuners() {
322 return mNumTuners;
323 }
324
325 /** Number tuner audio sources available. Must be less or equal to getNumTuners().
326 * When more than one tuner is supported, one is usually for playback and has one
327 * associated audio source and the other is for pre scanning and building a
328 * program list.
329 * @return the number of audio sources available.
330 */
331 public int getNumAudioSources() {
332 return mNumAudioSources;
333 }
334
335 /**
336 * Checks, if BandConfig initialization (after {@link RadioManager#openTuner})
337 * is required to be done before other operations or not.
338 *
339 * If it is, the client has to wait for {@link RadioTuner.Callback#onConfigurationChanged}
340 * callback before executing any other operations. Otherwise, such operation will fail
341 * returning {@link RadioManager#STATUS_INVALID_OPERATION} error code.
342 */
343 public boolean isInitializationRequired() {
344 return mIsInitializationRequired;
345 }
346
347 /** {@code true} if audio capture is possible from radio tuner output.
348 * This indicates if routing to audio devices not connected to the same HAL as the FM radio
349 * is possible (e.g. to USB) or DAR (Digital Audio Recorder) feature can be implemented.
350 * @return {@code true} if audio capture is possible, {@code false} otherwise.
351 */
352 public boolean isCaptureSupported() {
353 return mIsCaptureSupported;
354 }
355
356 /**
357 * {@code true} if the module supports background scanning. At the given time it may not
358 * be available though, see {@link RadioTuner#startBackgroundScan()}.
359 *
360 * @return {@code true} if background scanning is supported (not necessary available
361 * at a given time), {@code false} otherwise.
362 */
363 public boolean isBackgroundScanningSupported() {
364 return mIsBgScanSupported;
365 }
366
367 /**
368 * Checks, if a given program type is supported by this tuner.
369 *
370 * If a program type is supported by radio module, it means it can tune
371 * to ProgramSelector of a given type.
372 *
373 * @return {@code true} if a given program type is supported.
374 */
375 public boolean isProgramTypeSupported(@ProgramSelector.ProgramType int type) {
376 return mSupportedProgramTypes.contains(type);
377 }
378
379 /**
380 * Checks, if a given program identifier is supported by this tuner.
381 *
382 * If an identifier is supported by radio module, it means it can use it for
383 * tuning to ProgramSelector with either primary or secondary Identifier of
384 * a given type.
385 *
386 * @return {@code true} if a given program type is supported.
387 */
388 public boolean isProgramIdentifierSupported(@ProgramSelector.IdentifierType int type) {
389 return mSupportedIdentifierTypes.contains(type);
390 }
391
392 /**
393 * A frequency table for Digital Audio Broadcasting (DAB).
394 *
395 * The key is a channel name, i.e. 5A, 7B.
396 *
397 * The value is a frequency, in kHz.
398 *
399 * @return a frequency table, or {@code null} if the module doesn't support DAB
400 */
401 public @Nullable Map<String, Integer> getDabFrequencyTable() {
402 return mDabFrequencyTable;
403 }
404
405 /**
406 * A map of vendor-specific opaque strings, passed from HAL without changes.
407 * Format of these strings can vary across vendors.
408 *
409 * It may be used for extra features, that's not supported by a platform,
410 * for example: preset-slots=6; ultra-hd-capable=false.
411 *
412 * Keys must be prefixed with unique vendor Java-style namespace,
413 * eg. 'com.somecompany.parameter1'.
414 */
415 public @NonNull Map<String, String> getVendorInfo() {
416 return mVendorInfo;
417 }
418
419 /** List of descriptors for all bands supported by this module.
420 * @return an array of {@link BandDescriptor}.
421 */
422 public BandDescriptor[] getBands() {
423 return mBands;
424 }
425
426 private ModuleProperties(Parcel in) {
427 mId = in.readInt();
428 String serviceName = in.readString();
429 mServiceName = TextUtils.isEmpty(serviceName) ? "default" : serviceName;
430 mClassId = in.readInt();
431 mImplementor = in.readString();
432 mProduct = in.readString();
433 mVersion = in.readString();
434 mSerial = in.readString();
435 mNumTuners = in.readInt();
436 mNumAudioSources = in.readInt();
437 mIsInitializationRequired = in.readInt() == 1;
438 mIsCaptureSupported = in.readInt() == 1;
439 Parcelable[] tmp = in.readParcelableArray(BandDescriptor.class.getClassLoader(),
440 BandDescriptor.class);
441 mBands = new BandDescriptor[tmp.length];
442 for (int i = 0; i < tmp.length; i++) {
443 mBands[i] = (BandDescriptor) tmp[i];
444 }
445 mIsBgScanSupported = in.readInt() == 1;
446 mSupportedProgramTypes = arrayToSet(in.createIntArray());
447 mSupportedIdentifierTypes = arrayToSet(in.createIntArray());
448 mDabFrequencyTable = Utils.readStringIntMap(in);
449 mVendorInfo = Utils.readStringMap(in);
450 }
451
452 public static final @android.annotation.NonNull Parcelable.Creator<ModuleProperties> CREATOR
453 = new Parcelable.Creator<ModuleProperties>() {
454 public ModuleProperties createFromParcel(Parcel in) {
455 return new ModuleProperties(in);
456 }
457
458 public ModuleProperties[] newArray(int size) {
459 return new ModuleProperties[size];
460 }
461 };
462
463 @Override
464 public void writeToParcel(Parcel dest, int flags) {
465 dest.writeInt(mId);
466 dest.writeString(mServiceName);
467 dest.writeInt(mClassId);
468 dest.writeString(mImplementor);
469 dest.writeString(mProduct);
470 dest.writeString(mVersion);
471 dest.writeString(mSerial);
472 dest.writeInt(mNumTuners);
473 dest.writeInt(mNumAudioSources);
474 dest.writeInt(mIsInitializationRequired ? 1 : 0);
475 dest.writeInt(mIsCaptureSupported ? 1 : 0);
476 dest.writeParcelableArray(mBands, flags);
477 dest.writeInt(mIsBgScanSupported ? 1 : 0);
478 dest.writeIntArray(setToArray(mSupportedProgramTypes));
479 dest.writeIntArray(setToArray(mSupportedIdentifierTypes));
480 Utils.writeStringIntMap(dest, mDabFrequencyTable);
481 Utils.writeStringMap(dest, mVendorInfo);
482 }
483
484 @Override
485 public int describeContents() {
486 return 0;
487 }
488
489 @NonNull
490 @Override
491 public String toString() {
492 return "ModuleProperties [mId=" + mId
493 + ", mServiceName=" + mServiceName + ", mClassId=" + mClassId
494 + ", mImplementor=" + mImplementor + ", mProduct=" + mProduct
495 + ", mVersion=" + mVersion + ", mSerial=" + mSerial
496 + ", mNumTuners=" + mNumTuners
497 + ", mNumAudioSources=" + mNumAudioSources
498 + ", mIsInitializationRequired=" + mIsInitializationRequired
499 + ", mIsCaptureSupported=" + mIsCaptureSupported
500 + ", mIsBgScanSupported=" + mIsBgScanSupported
501 + ", mBands=" + Arrays.toString(mBands) + "]";
502 }
503
504 @Override
505 public int hashCode() {
506 return Objects.hash(mId, mServiceName, mClassId, mImplementor, mProduct, mVersion,
507 mSerial, mNumTuners, mNumAudioSources, mIsInitializationRequired,
508 mIsCaptureSupported, Arrays.hashCode(mBands), mIsBgScanSupported,
509 mDabFrequencyTable, mVendorInfo);
510 }
511
512 @Override
513 public boolean equals(@Nullable Object obj) {
514 if (this == obj) return true;
515 if (!(obj instanceof ModuleProperties)) return false;
516 ModuleProperties other = (ModuleProperties) obj;
517
518 if (mId != other.getId()) return false;
519 if (!TextUtils.equals(mServiceName, other.mServiceName)) return false;
520 if (mClassId != other.mClassId) return false;
521 if (!Objects.equals(mImplementor, other.mImplementor)) return false;
522 if (!Objects.equals(mProduct, other.mProduct)) return false;
523 if (!Objects.equals(mVersion, other.mVersion)) return false;
524 if (!Objects.equals(mSerial, other.mSerial)) return false;
525 if (mNumTuners != other.mNumTuners) return false;
526 if (mNumAudioSources != other.mNumAudioSources) return false;
527 if (mIsInitializationRequired != other.mIsInitializationRequired) return false;
528 if (mIsCaptureSupported != other.mIsCaptureSupported) return false;
529 if (!Arrays.equals(mBands, other.mBands)) return false;
530 if (mIsBgScanSupported != other.mIsBgScanSupported) return false;
531 if (!Objects.equals(mDabFrequencyTable, other.mDabFrequencyTable)) return false;
532 if (!Objects.equals(mVendorInfo, other.mVendorInfo)) return false;
533 return true;
534 }
535 }
536
537 /** Radio band descriptor: an element in ModuleProperties bands array.
538 * It is either an instance of {@link FmBandDescriptor} or {@link AmBandDescriptor} */
539 public static class BandDescriptor implements Parcelable {
540
541 private final int mRegion;
542 private final int mType;
543 private final int mLowerLimit;
544 private final int mUpperLimit;
545 private final int mSpacing;
546
547 BandDescriptor(int region, int type, int lowerLimit, int upperLimit, int spacing) {
548 if (type != BAND_AM && type != BAND_FM && type != BAND_FM_HD && type != BAND_AM_HD) {
549 throw new IllegalArgumentException("Unsupported band: " + type);
550 }
551 mRegion = region;
552 mType = type;
553 mLowerLimit = lowerLimit;
554 mUpperLimit = upperLimit;
555 mSpacing = spacing;
556 }
557
558 /** Region this band applies to. E.g. {@link #REGION_ITU_1}
559 * @return the region this band is associated to.
560 */
561 public int getRegion() {
562 return mRegion;
563 }
564 /** Band type, e.g {@link #BAND_FM}. Defines the subclass this descriptor can be cast to:
565 * <ul>
566 * <li>{@link #BAND_FM} or {@link #BAND_FM_HD} cast to {@link FmBandDescriptor}, </li>
567 * <li>{@link #BAND_AM} cast to {@link AmBandDescriptor}, </li>
568 * </ul>
569 * @return the band type.
570 */
571 public int getType() {
572 return mType;
573 }
574
575 /**
576 * Checks if the band is either AM or AM_HD.
577 *
578 * @return {@code true}, if band is AM or AM_HD.
579 */
580 public boolean isAmBand() {
581 return mType == BAND_AM || mType == BAND_AM_HD;
582 }
583
584 /**
585 * Checks if the band is either FM or FM_HD.
586 *
587 * @return {@code true}, if band is FM or FM_HD.
588 */
589 public boolean isFmBand() {
590 return mType == BAND_FM || mType == BAND_FM_HD;
591 }
592
593 /** Lower band limit expressed in units according to band type.
594 * Currently all defined band types express channels as frequency in kHz
595 * @return the lower band limit.
596 */
597 public int getLowerLimit() {
598 return mLowerLimit;
599 }
600 /** Upper band limit expressed in units according to band type.
601 * Currently all defined band types express channels as frequency in kHz
602 * @return the upper band limit.
603 */
604 public int getUpperLimit() {
605 return mUpperLimit;
606 }
607 /** Channel spacing in units according to band type.
608 * Currently all defined band types express channels as frequency in kHz
609 * @return the channel spacing.
610 */
611 public int getSpacing() {
612 return mSpacing;
613 }
614
615 private BandDescriptor(Parcel in) {
616 mRegion = in.readInt();
617 mType = in.readInt();
618 mLowerLimit = in.readInt();
619 mUpperLimit = in.readInt();
620 mSpacing = in.readInt();
621 }
622
623 private static int lookupTypeFromParcel(Parcel in) {
624 int pos = in.dataPosition();
625 in.readInt(); // skip region
626 int type = in.readInt();
627 in.setDataPosition(pos);
628 return type;
629 }
630
631 public static final @android.annotation.NonNull Parcelable.Creator<BandDescriptor> CREATOR
632 = new Parcelable.Creator<BandDescriptor>() {
633 public BandDescriptor createFromParcel(Parcel in) {
634 int type = lookupTypeFromParcel(in);
635 switch (type) {
636 case BAND_FM:
637 case BAND_FM_HD:
638 return new FmBandDescriptor(in);
639 case BAND_AM:
640 case BAND_AM_HD:
641 return new AmBandDescriptor(in);
642 default:
643 throw new IllegalArgumentException("Unsupported band: " + type);
644 }
645 }
646
647 public BandDescriptor[] newArray(int size) {
648 return new BandDescriptor[size];
649 }
650 };
651
652 @Override
653 public void writeToParcel(Parcel dest, int flags) {
654 dest.writeInt(mRegion);
655 dest.writeInt(mType);
656 dest.writeInt(mLowerLimit);
657 dest.writeInt(mUpperLimit);
658 dest.writeInt(mSpacing);
659 }
660
661 @Override
662 public int describeContents() {
663 return 0;
664 }
665
666 @NonNull
667 @Override
668 public String toString() {
669 return "BandDescriptor [mRegion=" + mRegion + ", mType=" + mType + ", mLowerLimit="
670 + mLowerLimit + ", mUpperLimit=" + mUpperLimit + ", mSpacing=" + mSpacing + "]";
671 }
672
673 @Override
674 public int hashCode() {
675 final int prime = 31;
676 int result = 1;
677 result = prime * result + mRegion;
678 result = prime * result + mType;
679 result = prime * result + mLowerLimit;
680 result = prime * result + mUpperLimit;
681 result = prime * result + mSpacing;
682 return result;
683 }
684
685 @Override
686 public boolean equals(@Nullable Object obj) {
687 if (this == obj)
688 return true;
689 if (!(obj instanceof BandDescriptor))
690 return false;
691 BandDescriptor other = (BandDescriptor) obj;
692 if (mRegion != other.getRegion())
693 return false;
694 if (mType != other.getType())
695 return false;
696 if (mLowerLimit != other.getLowerLimit())
697 return false;
698 if (mUpperLimit != other.getUpperLimit())
699 return false;
700 if (mSpacing != other.getSpacing())
701 return false;
702 return true;
703 }
704 }
705
706 /** FM band descriptor
707 * @see #BAND_FM
708 * @see #BAND_FM_HD */
709 public static class FmBandDescriptor extends BandDescriptor {
710 private final boolean mStereo;
711 private final boolean mRds;
712 private final boolean mTa;
713 private final boolean mAf;
714 private final boolean mEa;
715
716 /** @hide */
717 public FmBandDescriptor(int region, int type, int lowerLimit, int upperLimit, int spacing,
718 boolean stereo, boolean rds, boolean ta, boolean af, boolean ea) {
719 super(region, type, lowerLimit, upperLimit, spacing);
720 mStereo = stereo;
721 mRds = rds;
722 mTa = ta;
723 mAf = af;
724 mEa = ea;
725 }
726
727 /** Stereo is supported
728 * @return {@code true} if stereo is supported, {@code false} otherwise.
729 */
730 public boolean isStereoSupported() {
731 return mStereo;
732 }
733 /** RDS or RBDS(if region is ITU2) is supported
734 * @return {@code true} if RDS or RBDS is supported, {@code false} otherwise.
735 */
736 public boolean isRdsSupported() {
737 return mRds;
738 }
739 /** Traffic announcement is supported
740 * @return {@code true} if TA is supported, {@code false} otherwise.
741 */
742 public boolean isTaSupported() {
743 return mTa;
744 }
745 /** Alternate Frequency Switching is supported
746 * @return {@code true} if AF switching is supported, {@code false} otherwise.
747 */
748 public boolean isAfSupported() {
749 return mAf;
750 }
751
752 /** Emergency Announcement is supported
753 * @return {@code true} if Emergency annoucement is supported, {@code false} otherwise.
754 */
755 public boolean isEaSupported() {
756 return mEa;
757 }
758
759 /* Parcelable implementation */
760 private FmBandDescriptor(Parcel in) {
761 super(in);
762 mStereo = in.readByte() == 1;
763 mRds = in.readByte() == 1;
764 mTa = in.readByte() == 1;
765 mAf = in.readByte() == 1;
766 mEa = in.readByte() == 1;
767 }
768
769 public static final @android.annotation.NonNull Parcelable.Creator<FmBandDescriptor> CREATOR
770 = new Parcelable.Creator<FmBandDescriptor>() {
771 public FmBandDescriptor createFromParcel(Parcel in) {
772 return new FmBandDescriptor(in);
773 }
774
775 public FmBandDescriptor[] newArray(int size) {
776 return new FmBandDescriptor[size];
777 }
778 };
779
780 @Override
781 public void writeToParcel(Parcel dest, int flags) {
782 super.writeToParcel(dest, flags);
783 dest.writeByte((byte) (mStereo ? 1 : 0));
784 dest.writeByte((byte) (mRds ? 1 : 0));
785 dest.writeByte((byte) (mTa ? 1 : 0));
786 dest.writeByte((byte) (mAf ? 1 : 0));
787 dest.writeByte((byte) (mEa ? 1 : 0));
788 }
789
790 @Override
791 public int describeContents() {
792 return 0;
793 }
794
795 @NonNull
796 @Override
797 public String toString() {
798 return "FmBandDescriptor [ "+ super.toString() + " mStereo=" + mStereo
799 + ", mRds=" + mRds + ", mTa=" + mTa + ", mAf=" + mAf +
800 ", mEa =" + mEa + "]";
801 }
802
803 @Override
804 public int hashCode() {
805 final int prime = 31;
806 int result = super.hashCode();
807 result = prime * result + (mStereo ? 1 : 0);
808 result = prime * result + (mRds ? 1 : 0);
809 result = prime * result + (mTa ? 1 : 0);
810 result = prime * result + (mAf ? 1 : 0);
811 result = prime * result + (mEa ? 1 : 0);
812 return result;
813 }
814
815 @Override
816 public boolean equals(@Nullable Object obj) {
817 if (this == obj)
818 return true;
819 if (!super.equals(obj))
820 return false;
821 if (!(obj instanceof FmBandDescriptor))
822 return false;
823 FmBandDescriptor other = (FmBandDescriptor) obj;
824 if (mStereo != other.isStereoSupported())
825 return false;
826 if (mRds != other.isRdsSupported())
827 return false;
828 if (mTa != other.isTaSupported())
829 return false;
830 if (mAf != other.isAfSupported())
831 return false;
832 if (mEa != other.isEaSupported())
833 return false;
834 return true;
835 }
836 }
837
838 /** AM band descriptor.
839 * @see #BAND_AM */
840 public static class AmBandDescriptor extends BandDescriptor {
841
842 private final boolean mStereo;
843
844 /** @hide */
845 public AmBandDescriptor(int region, int type, int lowerLimit, int upperLimit, int spacing,
846 boolean stereo) {
847 super(region, type, lowerLimit, upperLimit, spacing);
848 mStereo = stereo;
849 }
850
851 /** Stereo is supported
852 * @return {@code true} if stereo is supported, {@code false} otherwise.
853 */
854 public boolean isStereoSupported() {
855 return mStereo;
856 }
857
858 private AmBandDescriptor(Parcel in) {
859 super(in);
860 mStereo = in.readByte() == 1;
861 }
862
863 public static final @android.annotation.NonNull Parcelable.Creator<AmBandDescriptor> CREATOR
864 = new Parcelable.Creator<AmBandDescriptor>() {
865 public AmBandDescriptor createFromParcel(Parcel in) {
866 return new AmBandDescriptor(in);
867 }
868
869 public AmBandDescriptor[] newArray(int size) {
870 return new AmBandDescriptor[size];
871 }
872 };
873
874 @Override
875 public void writeToParcel(Parcel dest, int flags) {
876 super.writeToParcel(dest, flags);
877 dest.writeByte((byte) (mStereo ? 1 : 0));
878 }
879
880 @Override
881 public int describeContents() {
882 return 0;
883 }
884
885 @NonNull
886 @Override
887 public String toString() {
888 return "AmBandDescriptor [ "+ super.toString() + " mStereo=" + mStereo + "]";
889 }
890
891 @Override
892 public int hashCode() {
893 final int prime = 31;
894 int result = super.hashCode();
895 result = prime * result + (mStereo ? 1 : 0);
896 return result;
897 }
898
899 @Override
900 public boolean equals(@Nullable Object obj) {
901 if (this == obj)
902 return true;
903 if (!super.equals(obj))
904 return false;
905 if (!(obj instanceof AmBandDescriptor))
906 return false;
907 AmBandDescriptor other = (AmBandDescriptor) obj;
908 if (mStereo != other.isStereoSupported())
909 return false;
910 return true;
911 }
912 }
913
914
915 /** Radio band configuration. */
916 public static class BandConfig implements Parcelable {
917
918 @NonNull final BandDescriptor mDescriptor;
919
920 BandConfig(BandDescriptor descriptor) {
921 mDescriptor = Objects.requireNonNull(descriptor);
922 }
923
924 BandConfig(int region, int type, int lowerLimit, int upperLimit, int spacing) {
925 mDescriptor = new BandDescriptor(region, type, lowerLimit, upperLimit, spacing);
926 }
927
928 private BandConfig(Parcel in) {
929 mDescriptor = new BandDescriptor(in);
930 }
931
932 BandDescriptor getDescriptor() {
933 return mDescriptor;
934 }
935
936 /** Region this band applies to. E.g. {@link #REGION_ITU_1}
937 * @return the region associated with this band.
938 */
939 public int getRegion() {
940 return mDescriptor.getRegion();
941 }
942 /** Band type, e.g {@link #BAND_FM}. Defines the subclass this descriptor can be cast to:
943 * <ul>
944 * <li>{@link #BAND_FM} or {@link #BAND_FM_HD} cast to {@link FmBandDescriptor}, </li>
945 * <li>{@link #BAND_AM} cast to {@link AmBandDescriptor}, </li>
946 * </ul>
947 * @return the band type.
948 */
949 public int getType() {
950 return mDescriptor.getType();
951 }
952 /** Lower band limit expressed in units according to band type.
953 * Currently all defined band types express channels as frequency in kHz
954 * @return the lower band limit.
955 */
956 public int getLowerLimit() {
957 return mDescriptor.getLowerLimit();
958 }
959 /** Upper band limit expressed in units according to band type.
960 * Currently all defined band types express channels as frequency in kHz
961 * @return the upper band limit.
962 */
963 public int getUpperLimit() {
964 return mDescriptor.getUpperLimit();
965 }
966 /** Channel spacing in units according to band type.
967 * Currently all defined band types express channels as frequency in kHz
968 * @return the channel spacing.
969 */
970 public int getSpacing() {
971 return mDescriptor.getSpacing();
972 }
973
974
975 public static final @android.annotation.NonNull Parcelable.Creator<BandConfig> CREATOR
976 = new Parcelable.Creator<BandConfig>() {
977 public BandConfig createFromParcel(Parcel in) {
978 int type = BandDescriptor.lookupTypeFromParcel(in);
979 switch (type) {
980 case BAND_FM:
981 case BAND_FM_HD:
982 return new FmBandConfig(in);
983 case BAND_AM:
984 case BAND_AM_HD:
985 return new AmBandConfig(in);
986 default:
987 throw new IllegalArgumentException("Unsupported band: " + type);
988 }
989 }
990
991 public BandConfig[] newArray(int size) {
992 return new BandConfig[size];
993 }
994 };
995
996 @Override
997 public void writeToParcel(Parcel dest, int flags) {
998 mDescriptor.writeToParcel(dest, flags);
999 }
1000
1001 @Override
1002 public int describeContents() {
1003 return 0;
1004 }
1005
1006 @NonNull
1007 @Override
1008 public String toString() {
1009 return "BandConfig [ " + mDescriptor.toString() + "]";
1010 }
1011
1012 @Override
1013 public int hashCode() {
1014 final int prime = 31;
1015 int result = 1;
1016 result = prime * result + mDescriptor.hashCode();
1017 return result;
1018 }
1019
1020 @Override
1021 public boolean equals(@Nullable Object obj) {
1022 if (this == obj)
1023 return true;
1024 if (!(obj instanceof BandConfig))
1025 return false;
1026 BandConfig other = (BandConfig) obj;
1027 BandDescriptor otherDesc = other.getDescriptor();
1028 if ((mDescriptor == null) != (otherDesc == null)) return false;
1029 if (mDescriptor != null && !mDescriptor.equals(otherDesc)) return false;
1030 return true;
1031 }
1032 }
1033
1034 /** FM band configuration.
1035 * @see #BAND_FM
1036 * @see #BAND_FM_HD */
1037 public static class FmBandConfig extends BandConfig {
1038 private final boolean mStereo;
1039 private final boolean mRds;
1040 private final boolean mTa;
1041 private final boolean mAf;
1042 private final boolean mEa;
1043
1044 /** @hide */
1045 public FmBandConfig(FmBandDescriptor descriptor) {
1046 super((BandDescriptor)descriptor);
1047 mStereo = descriptor.isStereoSupported();
1048 mRds = descriptor.isRdsSupported();
1049 mTa = descriptor.isTaSupported();
1050 mAf = descriptor.isAfSupported();
1051 mEa = descriptor.isEaSupported();
1052 }
1053
1054 FmBandConfig(int region, int type, int lowerLimit, int upperLimit, int spacing,
1055 boolean stereo, boolean rds, boolean ta, boolean af, boolean ea) {
1056 super(region, type, lowerLimit, upperLimit, spacing);
1057 mStereo = stereo;
1058 mRds = rds;
1059 mTa = ta;
1060 mAf = af;
1061 mEa = ea;
1062 }
1063
1064 /** Get stereo enable state
1065 * @return the enable state.
1066 */
1067 public boolean getStereo() {
1068 return mStereo;
1069 }
1070
1071 /** Get RDS or RBDS(if region is ITU2) enable state
1072 * @return the enable state.
1073 */
1074 public boolean getRds() {
1075 return mRds;
1076 }
1077
1078 /** Get Traffic announcement enable state
1079 * @return the enable state.
1080 */
1081 public boolean getTa() {
1082 return mTa;
1083 }
1084
1085 /** Get Alternate Frequency Switching enable state
1086 * @return the enable state.
1087 */
1088 public boolean getAf() {
1089 return mAf;
1090 }
1091
1092 /**
1093 * Get Emergency announcement enable state
1094 * @return the enable state.
1095 */
1096 public boolean getEa() {
1097 return mEa;
1098 }
1099
1100 private FmBandConfig(Parcel in) {
1101 super(in);
1102 mStereo = in.readByte() == 1;
1103 mRds = in.readByte() == 1;
1104 mTa = in.readByte() == 1;
1105 mAf = in.readByte() == 1;
1106 mEa = in.readByte() == 1;
1107 }
1108
1109 public static final @android.annotation.NonNull Parcelable.Creator<FmBandConfig> CREATOR
1110 = new Parcelable.Creator<FmBandConfig>() {
1111 public FmBandConfig createFromParcel(Parcel in) {
1112 return new FmBandConfig(in);
1113 }
1114
1115 public FmBandConfig[] newArray(int size) {
1116 return new FmBandConfig[size];
1117 }
1118 };
1119
1120 @Override
1121 public void writeToParcel(Parcel dest, int flags) {
1122 super.writeToParcel(dest, flags);
1123 dest.writeByte((byte) (mStereo ? 1 : 0));
1124 dest.writeByte((byte) (mRds ? 1 : 0));
1125 dest.writeByte((byte) (mTa ? 1 : 0));
1126 dest.writeByte((byte) (mAf ? 1 : 0));
1127 dest.writeByte((byte) (mEa ? 1 : 0));
1128 }
1129
1130 @Override
1131 public int describeContents() {
1132 return 0;
1133 }
1134
1135 @NonNull
1136 @Override
1137 public String toString() {
1138 return "FmBandConfig [" + super.toString()
1139 + ", mStereo=" + mStereo + ", mRds=" + mRds + ", mTa=" + mTa
1140 + ", mAf=" + mAf + ", mEa =" + mEa + "]";
1141 }
1142
1143 @Override
1144 public int hashCode() {
1145 final int prime = 31;
1146 int result = super.hashCode();
1147 result = prime * result + (mStereo ? 1 : 0);
1148 result = prime * result + (mRds ? 1 : 0);
1149 result = prime * result + (mTa ? 1 : 0);
1150 result = prime * result + (mAf ? 1 : 0);
1151 result = prime * result + (mEa ? 1 : 0);
1152 return result;
1153 }
1154
1155 @Override
1156 public boolean equals(@Nullable Object obj) {
1157 if (this == obj)
1158 return true;
1159 if (!super.equals(obj))
1160 return false;
1161 if (!(obj instanceof FmBandConfig))
1162 return false;
1163 FmBandConfig other = (FmBandConfig) obj;
1164 if (mStereo != other.mStereo)
1165 return false;
1166 if (mRds != other.mRds)
1167 return false;
1168 if (mTa != other.mTa)
1169 return false;
1170 if (mAf != other.mAf)
1171 return false;
1172 if (mEa != other.mEa)
1173 return false;
1174 return true;
1175 }
1176
1177 /**
1178 * Builder class for {@link FmBandConfig} objects.
1179 */
1180 public static class Builder {
1181 private final BandDescriptor mDescriptor;
1182 private boolean mStereo;
1183 private boolean mRds;
1184 private boolean mTa;
1185 private boolean mAf;
1186 private boolean mEa;
1187
1188 /**
1189 * Constructs a new Builder with the defaults from an {@link FmBandDescriptor} .
1190 * @param descriptor the FmBandDescriptor defaults are read from .
1191 */
1192 public Builder(FmBandDescriptor descriptor) {
1193 mDescriptor = new BandDescriptor(descriptor.getRegion(), descriptor.getType(),
1194 descriptor.getLowerLimit(), descriptor.getUpperLimit(),
1195 descriptor.getSpacing());
1196 mStereo = descriptor.isStereoSupported();
1197 mRds = descriptor.isRdsSupported();
1198 mTa = descriptor.isTaSupported();
1199 mAf = descriptor.isAfSupported();
1200 mEa = descriptor.isEaSupported();
1201 }
1202
1203 /**
1204 * Constructs a new Builder from a given {@link FmBandConfig}
1205 * @param config the FmBandConfig object whose data will be reused in the new Builder.
1206 */
1207 public Builder(FmBandConfig config) {
1208 mDescriptor = new BandDescriptor(config.getRegion(), config.getType(),
1209 config.getLowerLimit(), config.getUpperLimit(), config.getSpacing());
1210 mStereo = config.getStereo();
1211 mRds = config.getRds();
1212 mTa = config.getTa();
1213 mAf = config.getAf();
1214 mEa = config.getEa();
1215 }
1216
1217 /**
1218 * Combines all of the parameters that have been set and return a new
1219 * {@link FmBandConfig} object.
1220 * @return a new {@link FmBandConfig} object
1221 */
1222 public FmBandConfig build() {
1223 FmBandConfig config = new FmBandConfig(mDescriptor.getRegion(),
1224 mDescriptor.getType(), mDescriptor.getLowerLimit(),
1225 mDescriptor.getUpperLimit(), mDescriptor.getSpacing(),
1226 mStereo, mRds, mTa, mAf, mEa);
1227 return config;
1228 }
1229
1230 /** Set stereo enable state
1231 * @param state The new enable state.
1232 * @return the same Builder instance.
1233 */
1234 public Builder setStereo(boolean state) {
1235 mStereo = state;
1236 return this;
1237 }
1238
1239 /** Set RDS or RBDS(if region is ITU2) enable state
1240 * @param state The new enable state.
1241 * @return the same Builder instance.
1242 */
1243 public Builder setRds(boolean state) {
1244 mRds = state;
1245 return this;
1246 }
1247
1248 /** Set Traffic announcement enable state
1249 * @param state The new enable state.
1250 * @return the same Builder instance.
1251 */
1252 public Builder setTa(boolean state) {
1253 mTa = state;
1254 return this;
1255 }
1256
1257 /** Set Alternate Frequency Switching enable state
1258 * @param state The new enable state.
1259 * @return the same Builder instance.
1260 */
1261 public Builder setAf(boolean state) {
1262 mAf = state;
1263 return this;
1264 }
1265
1266 /** Set Emergency Announcement enable state
1267 * @param state The new enable state.
1268 * @return the same Builder instance.
1269 */
1270 public Builder setEa(boolean state) {
1271 mEa = state;
1272 return this;
1273 }
1274 };
1275 }
1276
1277 /** AM band configuration.
1278 * @see #BAND_AM */
1279 public static class AmBandConfig extends BandConfig {
1280 private final boolean mStereo;
1281
1282 /** @hide */
1283 public AmBandConfig(AmBandDescriptor descriptor) {
1284 super((BandDescriptor)descriptor);
1285 mStereo = descriptor.isStereoSupported();
1286 }
1287
1288 AmBandConfig(int region, int type, int lowerLimit, int upperLimit, int spacing,
1289 boolean stereo) {
1290 super(region, type, lowerLimit, upperLimit, spacing);
1291 mStereo = stereo;
1292 }
1293
1294 /** Get stereo enable state
1295 * @return the enable state.
1296 */
1297 public boolean getStereo() {
1298 return mStereo;
1299 }
1300
1301 private AmBandConfig(Parcel in) {
1302 super(in);
1303 mStereo = in.readByte() == 1;
1304 }
1305
1306 public static final @android.annotation.NonNull Parcelable.Creator<AmBandConfig> CREATOR
1307 = new Parcelable.Creator<AmBandConfig>() {
1308 public AmBandConfig createFromParcel(Parcel in) {
1309 return new AmBandConfig(in);
1310 }
1311
1312 public AmBandConfig[] newArray(int size) {
1313 return new AmBandConfig[size];
1314 }
1315 };
1316
1317 @Override
1318 public void writeToParcel(Parcel dest, int flags) {
1319 super.writeToParcel(dest, flags);
1320 dest.writeByte((byte) (mStereo ? 1 : 0));
1321 }
1322
1323 @Override
1324 public int describeContents() {
1325 return 0;
1326 }
1327
1328 @NonNull
1329 @Override
1330 public String toString() {
1331 return "AmBandConfig [" + super.toString()
1332 + ", mStereo=" + mStereo + "]";
1333 }
1334
1335 @Override
1336 public int hashCode() {
1337 final int prime = 31;
1338 int result = super.hashCode();
1339 result = prime * result + (mStereo ? 1 : 0);
1340 return result;
1341 }
1342
1343 @Override
1344 public boolean equals(@Nullable Object obj) {
1345 if (this == obj)
1346 return true;
1347 if (!super.equals(obj))
1348 return false;
1349 if (!(obj instanceof AmBandConfig))
1350 return false;
1351 AmBandConfig other = (AmBandConfig) obj;
1352 if (mStereo != other.getStereo())
1353 return false;
1354 return true;
1355 }
1356
1357 /**
1358 * Builder class for {@link AmBandConfig} objects.
1359 */
1360 public static class Builder {
1361 private final BandDescriptor mDescriptor;
1362 private boolean mStereo;
1363
1364 /**
1365 * Constructs a new Builder with the defaults from an {@link AmBandDescriptor} .
1366 * @param descriptor the FmBandDescriptor defaults are read from .
1367 */
1368 public Builder(AmBandDescriptor descriptor) {
1369 mDescriptor = new BandDescriptor(descriptor.getRegion(), descriptor.getType(),
1370 descriptor.getLowerLimit(), descriptor.getUpperLimit(),
1371 descriptor.getSpacing());
1372 mStereo = descriptor.isStereoSupported();
1373 }
1374
1375 /**
1376 * Constructs a new Builder from a given {@link AmBandConfig}
1377 * @param config the FmBandConfig object whose data will be reused in the new Builder.
1378 */
1379 public Builder(AmBandConfig config) {
1380 mDescriptor = new BandDescriptor(config.getRegion(), config.getType(),
1381 config.getLowerLimit(), config.getUpperLimit(), config.getSpacing());
1382 mStereo = config.getStereo();
1383 }
1384
1385 /**
1386 * Combines all of the parameters that have been set and return a new
1387 * {@link AmBandConfig} object.
1388 * @return a new {@link AmBandConfig} object
1389 */
1390 public AmBandConfig build() {
1391 AmBandConfig config = new AmBandConfig(mDescriptor.getRegion(),
1392 mDescriptor.getType(), mDescriptor.getLowerLimit(),
1393 mDescriptor.getUpperLimit(), mDescriptor.getSpacing(),
1394 mStereo);
1395 return config;
1396 }
1397
1398 /** Set stereo enable state
1399 * @param state The new enable state.
1400 * @return the same Builder instance.
1401 */
1402 public Builder setStereo(boolean state) {
1403 mStereo = state;
1404 return this;
1405 }
1406 };
1407 }
1408
1409 /** Radio program information. */
1410 public static class ProgramInfo implements Parcelable {
1411
1412 // sourced from hardware/interfaces/broadcastradio/2.0/types.hal
1413 private static final int FLAG_LIVE = 1 << 0;
1414 private static final int FLAG_MUTED = 1 << 1;
1415 private static final int FLAG_TRAFFIC_PROGRAM = 1 << 2;
1416 private static final int FLAG_TRAFFIC_ANNOUNCEMENT = 1 << 3;
1417 private static final int FLAG_TUNED = 1 << 4;
1418 private static final int FLAG_STEREO = 1 << 5;
1419
1420 @NonNull private final ProgramSelector mSelector;
1421 @Nullable private final ProgramSelector.Identifier mLogicallyTunedTo;
1422 @Nullable private final ProgramSelector.Identifier mPhysicallyTunedTo;
1423 @NonNull private final Collection<ProgramSelector.Identifier> mRelatedContent;
1424 private final int mInfoFlags;
1425 private final int mSignalQuality;
1426 @Nullable private final RadioMetadata mMetadata;
1427 @NonNull private final Map<String, String> mVendorInfo;
1428
1429 /** @hide */
1430 public ProgramInfo(@NonNull ProgramSelector selector,
1431 @Nullable ProgramSelector.Identifier logicallyTunedTo,
1432 @Nullable ProgramSelector.Identifier physicallyTunedTo,
1433 @Nullable Collection<ProgramSelector.Identifier> relatedContent,
1434 int infoFlags, int signalQuality, @Nullable RadioMetadata metadata,
1435 @Nullable Map<String, String> vendorInfo) {
1436 mSelector = Objects.requireNonNull(selector);
1437 mLogicallyTunedTo = logicallyTunedTo;
1438 mPhysicallyTunedTo = physicallyTunedTo;
1439 if (relatedContent == null) {
1440 mRelatedContent = Collections.emptyList();
1441 } else {
1442 Preconditions.checkCollectionElementsNotNull(relatedContent, "relatedContent");
1443 mRelatedContent = relatedContent;
1444 }
1445 mInfoFlags = infoFlags;
1446 mSignalQuality = signalQuality;
1447 mMetadata = metadata;
1448 mVendorInfo = (vendorInfo == null) ? new HashMap<>() : vendorInfo;
1449 }
1450
1451 /**
1452 * Program selector, necessary for tuning to a program.
1453 *
1454 * @return the program selector.
1455 */
1456 public @NonNull ProgramSelector getSelector() {
1457 return mSelector;
1458 }
1459
1460 /**
1461 * Identifier currently used for program selection.
1462 *
1463 * This identifier can be used to determine which technology is
1464 * currently being used for reception.
1465 *
1466 * Some program selectors contain tuning information for different radio
1467 * technologies (i.e. FM RDS and DAB). For example, user may tune using
1468 * a ProgramSelector with RDS_PI primary identifier, but the tuner hardware
1469 * may choose to use DAB technology to make actual tuning. This identifier
1470 * must reflect that.
1471 */
1472 public @Nullable ProgramSelector.Identifier getLogicallyTunedTo() {
1473 return mLogicallyTunedTo;
1474 }
1475
1476 /**
1477 * Identifier currently used by hardware to physically tune to a channel.
1478 *
1479 * Some radio technologies broadcast the same program on multiple channels,
1480 * i.e. with RDS AF the same program may be broadcasted on multiple
1481 * alternative frequencies; the same DAB program may be broadcast on
1482 * multiple ensembles. This identifier points to the channel to which the
1483 * radio hardware is physically tuned to.
1484 */
1485 public @Nullable ProgramSelector.Identifier getPhysicallyTunedTo() {
1486 return mPhysicallyTunedTo;
1487 }
1488
1489 /**
1490 * Primary identifiers of related contents.
1491 *
1492 * Some radio technologies provide pointers to other programs that carry
1493 * related content (i.e. DAB soft-links). This field is a list of pointers
1494 * to other programs on the program list.
1495 *
1496 * Please note, that these identifiers does not have to exist on the program
1497 * list - i.e. DAB tuner may provide information on FM RDS alternatives
1498 * despite not supporting FM RDS. If the system has multiple tuners, another
1499 * one may have it on its list.
1500 */
1501 public @Nullable Collection<ProgramSelector.Identifier> getRelatedContent() {
1502 return mRelatedContent;
1503 }
1504
1505 /** Main channel expressed in units according to band type.
1506 * Currently all defined band types express channels as frequency in kHz
1507 * @return the program channel
1508 * @deprecated Use {@link getSelector()} instead.
1509 */
1510 @Deprecated
1511 public int getChannel() {
1512 try {
1513 return (int) mSelector.getFirstId(ProgramSelector.IDENTIFIER_TYPE_AMFM_FREQUENCY);
1514 } catch (IllegalArgumentException ex) {
1515 Log.w(TAG, "Not an AM/FM program");
1516 return 0;
1517 }
1518 }
1519
1520 /** Sub channel ID. E.g 1 for HD radio HD1
1521 * @return the program sub channel
1522 * @deprecated Use {@link getSelector()} instead.
1523 */
1524 @Deprecated
1525 public int getSubChannel() {
1526 try {
1527 return (int) mSelector.getFirstId(
1528 ProgramSelector.IDENTIFIER_TYPE_HD_SUBCHANNEL) + 1;
1529 } catch (IllegalArgumentException ex) {
1530 // this is a normal behavior for analog AM/FM selector
1531 return 0;
1532 }
1533 }
1534
1535 /** {@code true} if the tuner is currently tuned on a valid station
1536 * @return {@code true} if currently tuned, {@code false} otherwise.
1537 */
1538 public boolean isTuned() {
1539 return (mInfoFlags & FLAG_TUNED) != 0;
1540 }
1541
1542 /** {@code true} if the received program is stereo
1543 * @return {@code true} if stereo, {@code false} otherwise.
1544 */
1545 public boolean isStereo() {
1546 return (mInfoFlags & FLAG_STEREO) != 0;
1547 }
1548
1549 /** {@code true} if the received program is digital (e.g HD radio)
1550 * @return {@code true} if digital, {@code false} otherwise.
1551 * @deprecated Use {@link getLogicallyTunedTo()} instead.
1552 */
1553 @Deprecated
1554 public boolean isDigital() {
1555 ProgramSelector.Identifier id = mLogicallyTunedTo;
1556 if (id == null) id = mSelector.getPrimaryId();
1557
1558 int type = id.getType();
1559 return (type != ProgramSelector.IDENTIFIER_TYPE_AMFM_FREQUENCY
1560 && type != ProgramSelector.IDENTIFIER_TYPE_RDS_PI);
1561 }
1562
1563 /**
1564 * {@code true} if the program is currently playing live stream.
1565 * This may result in a slightly altered reception parameters,
1566 * usually targetted at reduced latency.
1567 */
1568 public boolean isLive() {
1569 return (mInfoFlags & FLAG_LIVE) != 0;
1570 }
1571
1572 /**
1573 * {@code true} if radio stream is not playing, ie. due to bad reception
1574 * conditions or buffering. In this state volume knob MAY be disabled to
1575 * prevent user increasing volume too much.
1576 * It does NOT mean the user has muted audio.
1577 */
1578 public boolean isMuted() {
1579 return (mInfoFlags & FLAG_MUTED) != 0;
1580 }
1581
1582 /**
1583 * {@code true} if radio station transmits traffic information
1584 * regularily.
1585 */
1586 public boolean isTrafficProgram() {
1587 return (mInfoFlags & FLAG_TRAFFIC_PROGRAM) != 0;
1588 }
1589
1590 /**
1591 * {@code true} if radio station transmits traffic information
1592 * at the very moment.
1593 */
1594 public boolean isTrafficAnnouncementActive() {
1595 return (mInfoFlags & FLAG_TRAFFIC_ANNOUNCEMENT) != 0;
1596 }
1597
1598 /**
1599 * Signal quality (as opposed to the name) indication from 0 (no signal)
1600 * to 100 (excellent)
1601 * @return the signal quality indication.
1602 */
1603 public int getSignalStrength() {
1604 return mSignalQuality;
1605 }
1606
1607 /** Metadata currently received from this station.
1608 * null if no metadata have been received
1609 * @return current meta data received from this program.
1610 */
1611 public RadioMetadata getMetadata() {
1612 return mMetadata;
1613 }
1614
1615 /**
1616 * A map of vendor-specific opaque strings, passed from HAL without changes.
1617 * Format of these strings can vary across vendors.
1618 *
1619 * It may be used for extra features, that's not supported by a platform,
1620 * for example: paid-service=true; bitrate=320kbps.
1621 *
1622 * Keys must be prefixed with unique vendor Java-style namespace,
1623 * eg. 'com.somecompany.parameter1'.
1624 */
1625 public @NonNull Map<String, String> getVendorInfo() {
1626 return mVendorInfo;
1627 }
1628
1629 private ProgramInfo(Parcel in) {
1630 mSelector = Objects.requireNonNull(in.readTypedObject(ProgramSelector.CREATOR));
1631 mLogicallyTunedTo = in.readTypedObject(ProgramSelector.Identifier.CREATOR);
1632 mPhysicallyTunedTo = in.readTypedObject(ProgramSelector.Identifier.CREATOR);
1633 mRelatedContent = in.createTypedArrayList(ProgramSelector.Identifier.CREATOR);
1634 mInfoFlags = in.readInt();
1635 mSignalQuality = in.readInt();
1636 mMetadata = in.readTypedObject(RadioMetadata.CREATOR);
1637 mVendorInfo = Utils.readStringMap(in);
1638 }
1639
1640 public static final @android.annotation.NonNull Parcelable.Creator<ProgramInfo> CREATOR
1641 = new Parcelable.Creator<ProgramInfo>() {
1642 public ProgramInfo createFromParcel(Parcel in) {
1643 return new ProgramInfo(in);
1644 }
1645
1646 public ProgramInfo[] newArray(int size) {
1647 return new ProgramInfo[size];
1648 }
1649 };
1650
1651 @Override
1652 public void writeToParcel(Parcel dest, int flags) {
1653 dest.writeTypedObject(mSelector, flags);
1654 dest.writeTypedObject(mLogicallyTunedTo, flags);
1655 dest.writeTypedObject(mPhysicallyTunedTo, flags);
1656 Utils.writeTypedCollection(dest, mRelatedContent);
1657 dest.writeInt(mInfoFlags);
1658 dest.writeInt(mSignalQuality);
1659 dest.writeTypedObject(mMetadata, flags);
1660 Utils.writeStringMap(dest, mVendorInfo);
1661 }
1662
1663 @Override
1664 public int describeContents() {
1665 return 0;
1666 }
1667
1668 @NonNull
1669 @Override
1670 public String toString() {
1671 return "ProgramInfo"
1672 + " [selector=" + mSelector
1673 + ", logicallyTunedTo=" + Objects.toString(mLogicallyTunedTo)
1674 + ", physicallyTunedTo=" + Objects.toString(mPhysicallyTunedTo)
1675 + ", relatedContent=" + mRelatedContent.size()
1676 + ", infoFlags=" + mInfoFlags
1677 + ", mSignalQuality=" + mSignalQuality
1678 + ", mMetadata=" + Objects.toString(mMetadata)
1679 + "]";
1680 }
1681
1682 @Override
1683 public int hashCode() {
1684 return Objects.hash(mSelector, mLogicallyTunedTo, mPhysicallyTunedTo,
1685 mRelatedContent, mInfoFlags, mSignalQuality, mMetadata, mVendorInfo);
1686 }
1687
1688 @Override
1689 public boolean equals(@Nullable Object obj) {
1690 if (this == obj) return true;
1691 if (!(obj instanceof ProgramInfo)) return false;
1692 ProgramInfo other = (ProgramInfo) obj;
1693
1694 if (!Objects.equals(mSelector, other.mSelector)) return false;
1695 if (!Objects.equals(mLogicallyTunedTo, other.mLogicallyTunedTo)) return false;
1696 if (!Objects.equals(mPhysicallyTunedTo, other.mPhysicallyTunedTo)) return false;
1697 if (!Objects.equals(mRelatedContent, other.mRelatedContent)) return false;
1698 if (mInfoFlags != other.mInfoFlags) return false;
1699 if (mSignalQuality != other.mSignalQuality) return false;
1700 if (!Objects.equals(mMetadata, other.mMetadata)) return false;
1701 if (!Objects.equals(mVendorInfo, other.mVendorInfo)) return false;
1702
1703 return true;
1704 }
1705 }
1706
1707
1708 /**
1709 * Returns a list of descriptors for all broadcast radio modules present on the device.
1710 * @param modules An List of {@link ModuleProperties} where the list will be returned.
1711 * @return
1712 * <ul>
1713 * <li>{@link #STATUS_OK} in case of success, </li>
1714 * <li>{@link #STATUS_ERROR} in case of unspecified error, </li>
1715 * <li>{@link #STATUS_NO_INIT} if the native service cannot be reached, </li>
1716 * <li>{@link #STATUS_BAD_VALUE} if modules is null, </li>
1717 * <li>{@link #STATUS_DEAD_OBJECT} if the binder transaction to the native service fails, </li>
1718 * </ul>
1719 */
1720 @RequiresPermission(Manifest.permission.ACCESS_BROADCAST_RADIO)
1721 public int listModules(List<ModuleProperties> modules) {
1722 if (modules == null) {
1723 Log.e(TAG, "the output list must not be empty");
1724 return STATUS_BAD_VALUE;
1725 }
1726
1727 Log.d(TAG, "Listing available tuners...");
1728 List<ModuleProperties> returnedList;
1729 try {
1730 returnedList = mService.listModules();
1731 } catch (RemoteException e) {
1732 Log.e(TAG, "Failed listing available tuners", e);
1733 return STATUS_DEAD_OBJECT;
1734 }
1735
1736 if (returnedList == null) {
1737 Log.e(TAG, "Returned list was a null");
1738 return STATUS_ERROR;
1739 }
1740
1741 modules.addAll(returnedList);
1742 return STATUS_OK;
1743 }
1744
1745 private native int nativeListModules(List<ModuleProperties> modules);
1746
1747 /**
1748 * Open an interface to control a tuner on a given broadcast radio module.
1749 * Optionally selects and applies the configuration passed as "config" argument.
1750 * @param moduleId radio module identifier {@link ModuleProperties#getId()}. Mandatory.
1751 * @param config desired band and configuration to apply when enabling the hardware module.
1752 * optional, can be null.
1753 * @param withAudio {@code true} to request a tuner with an audio source.
1754 * This tuner is intended for live listening or recording or a radio program.
1755 * If {@code false}, the tuner can only be used to retrieve program informations.
1756 * @param callback {@link RadioTuner.Callback} interface. Mandatory.
1757 * @param handler the Handler on which the callbacks will be received.
1758 * Can be null if default handler is OK.
1759 * @return a valid {@link RadioTuner} interface in case of success or null in case of error.
1760 */
1761 @RequiresPermission(Manifest.permission.ACCESS_BROADCAST_RADIO)
1762 public RadioTuner openTuner(int moduleId, BandConfig config, boolean withAudio,
1763 RadioTuner.Callback callback, Handler handler) {
1764 if (callback == null) {
1765 throw new IllegalArgumentException("callback must not be empty");
1766 }
1767
1768 Log.d(TAG, "Opening tuner " + moduleId + "...");
1769
1770 ITuner tuner;
1771 TunerCallbackAdapter halCallback = new TunerCallbackAdapter(callback, handler);
1772 try {
1773 tuner = mService.openTuner(moduleId, config, withAudio, halCallback);
1774 } catch (RemoteException | IllegalArgumentException | IllegalStateException ex) {
1775 Log.e(TAG, "Failed to open tuner", ex);
1776 return null;
1777 }
1778 if (tuner == null) {
1779 Log.e(TAG, "Failed to open tuner");
1780 return null;
1781 }
1782 return new TunerAdapter(tuner, halCallback,
1783 config != null ? config.getType() : BAND_INVALID);
1784 }
1785
1786 private final Map<Announcement.OnListUpdatedListener, ICloseHandle> mAnnouncementListeners =
1787 new HashMap<>();
1788
1789 /**
1790 * Adds new announcement listener.
1791 *
1792 * @param enabledAnnouncementTypes a set of announcement types to listen to
1793 * @param listener announcement listener
1794 */
1795 @RequiresPermission(Manifest.permission.ACCESS_BROADCAST_RADIO)
1796 public void addAnnouncementListener(@NonNull Set<Integer> enabledAnnouncementTypes,
1797 @NonNull Announcement.OnListUpdatedListener listener) {
1798 addAnnouncementListener(cmd -> cmd.run(), enabledAnnouncementTypes, listener);
1799 }
1800
1801 /**
1802 * Adds new announcement listener with executor.
1803 *
1804 * @param executor the executor
1805 * @param enabledAnnouncementTypes a set of announcement types to listen to
1806 * @param listener announcement listener
1807 */
1808 @RequiresPermission(Manifest.permission.ACCESS_BROADCAST_RADIO)
1809 public void addAnnouncementListener(@NonNull @CallbackExecutor Executor executor,
1810 @NonNull Set<Integer> enabledAnnouncementTypes,
1811 @NonNull Announcement.OnListUpdatedListener listener) {
1812 Objects.requireNonNull(executor);
1813 Objects.requireNonNull(listener);
1814 int[] types = enabledAnnouncementTypes.stream().mapToInt(Integer::intValue).toArray();
1815 IAnnouncementListener listenerIface = new IAnnouncementListener.Stub() {
1816 public void onListUpdated(List<Announcement> activeAnnouncements) {
1817 executor.execute(() -> listener.onListUpdated(activeAnnouncements));
1818 }
1819 };
1820 synchronized (mAnnouncementListeners) {
1821 ICloseHandle closeHandle = null;
1822 try {
1823 closeHandle = mService.addAnnouncementListener(types, listenerIface);
1824 } catch (RemoteException ex) {
1825 ex.rethrowFromSystemServer();
1826 }
1827 Objects.requireNonNull(closeHandle);
1828 ICloseHandle oldCloseHandle = mAnnouncementListeners.put(listener, closeHandle);
1829 if (oldCloseHandle != null) Utils.close(oldCloseHandle);
1830 }
1831 }
1832
1833 /**
1834 * Removes previously registered announcement listener.
1835 *
1836 * @param listener announcement listener, previously registered with
1837 * {@link addAnnouncementListener}
1838 */
1839 @RequiresPermission(Manifest.permission.ACCESS_BROADCAST_RADIO)
1840 public void removeAnnouncementListener(@NonNull Announcement.OnListUpdatedListener listener) {
1841 Objects.requireNonNull(listener);
1842 synchronized (mAnnouncementListeners) {
1843 ICloseHandle closeHandle = mAnnouncementListeners.remove(listener);
1844 if (closeHandle != null) Utils.close(closeHandle);
1845 }
1846 }
1847
1848 @NonNull private final Context mContext;
1849 @NonNull private final IRadioService mService;
1850
1851 /**
1852 * @hide
1853 */
1854 public RadioManager(@NonNull Context context) throws ServiceNotFoundException {
1855 mContext = context;
1856 mService = IRadioService.Stub.asInterface(
1857 ServiceManager.getServiceOrThrow(Context.RADIO_SERVICE));
1858 }
1859 }