1 /**
2  * Copyright (C) 2018 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.car.radio.bands;
18 
19 import android.hardware.radio.RadioManager.AmBandDescriptor;
20 import android.hardware.radio.RadioManager.BandDescriptor;
21 import android.hardware.radio.RadioManager.FmBandDescriptor;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 
28 import com.android.car.radio.util.Log;
29 
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Comparator;
33 import java.util.List;
34 import java.util.Objects;
35 
36 /**
37  * Regional configuration for all radio technologies wrapped in a single object.
38  */
39 public final class RegionConfig implements Parcelable {
40     private static final String TAG = "BcRadioApp.region";
41 
42     private final List<ProgramType> mSupported;
43     private final List<BandDescriptor> mAmConfig;
44     private final List<BandDescriptor> mFmConfig;
45 
RegionConfig(@ullable List<BandDescriptor> amFmConfig)46     public RegionConfig(@Nullable List<BandDescriptor> amFmConfig) {
47         mAmConfig = new ArrayList<>();
48         mFmConfig = new ArrayList<>();
49         if (amFmConfig == null) {
50             mSupported = new ArrayList<>();
51             return;
52         }
53 
54         for (BandDescriptor band : amFmConfig) {
55             if (band instanceof AmBandDescriptor) {
56                 mAmConfig.add(band);
57             } else if (band instanceof FmBandDescriptor) {
58                 mFmConfig.add(band);
59             } else {
60                 Log.w(TAG, "Unknown band type: " + band);
61             }
62         }
63 
64         Comparator<BandDescriptor> cmp = (BandDescriptor a, BandDescriptor b) ->
65                 a.getLowerLimit() - b.getLowerLimit();
66         Collections.sort(mAmConfig, cmp);
67         Collections.sort(mFmConfig, cmp);
68         mSupported = createSupportedProgramTypes();
69     }
70 
RegionConfig(@onNull Parcel in)71     private RegionConfig(@NonNull Parcel in) {
72         mAmConfig = in.createTypedArrayList(BandDescriptor.CREATOR);
73         mFmConfig = in.createTypedArrayList(BandDescriptor.CREATOR);
74         mSupported = createSupportedProgramTypes();
75     }
76 
77     @NonNull
createSupportedProgramTypes()78     private List<ProgramType> createSupportedProgramTypes() {
79         List<ProgramType> supported = new ArrayList<>();
80         if (!mAmConfig.isEmpty()) supported.add(ProgramType.AM);
81         if (!mFmConfig.isEmpty()) supported.add(ProgramType.FM);
82         return supported;
83     }
84 
85     @NonNull
getSupportedProgramTypes()86     public List<ProgramType> getSupportedProgramTypes() {
87         return mSupported;
88     }
89 
90     @NonNull
getAmConfig()91     public List<BandDescriptor> getAmConfig() {
92         return mAmConfig;
93     }
94 
95     @NonNull
getFmConfig()96     public List<BandDescriptor> getFmConfig() {
97         return mFmConfig;
98     }
99 
100     @Override
toString()101     public String toString() {
102         return "RegionConfig{AM=" + mAmConfig + ", FM=" + mFmConfig + "}";
103     }
104 
105     @Override
hashCode()106     public int hashCode() {
107         return Objects.hash(mAmConfig, mFmConfig);
108     }
109 
110     @Override
equals(Object obj)111     public boolean equals(Object obj) {
112         if (this == obj) return true;
113         if (!(obj instanceof RegionConfig)) return false;
114         RegionConfig other = (RegionConfig) obj;
115         if (!mAmConfig.equals(other.mAmConfig)) return false;
116         if (!mFmConfig.equals(other.mFmConfig)) return false;
117         return true;
118     }
119 
120     @Override
writeToParcel(Parcel dest, int flags)121     public void writeToParcel(Parcel dest, int flags) {
122         dest.writeTypedList(mAmConfig);
123         dest.writeTypedList(mFmConfig);
124     }
125 
126     @Override
describeContents()127     public int describeContents() {
128         return 0;
129     }
130 
131     public static final Parcelable.Creator<RegionConfig> CREATOR =
132             new Parcelable.Creator<RegionConfig>() {
133         public RegionConfig createFromParcel(Parcel in) {
134             return new RegionConfig(in);
135         }
136 
137         public RegionConfig[] newArray(int size) {
138             return new RegionConfig[size];
139         }
140     };
141 }
142