1 /*
2  * Copyright (C) 2023 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.google.android.car.kitchensink.radio;
18 
19 import android.hardware.radio.RadioManager;
20 import android.util.SparseArray;
21 
22 import com.android.car.broadcastradio.support.platform.ProgramSelectorExt;
23 
24 import java.util.Iterator;
25 import java.util.Map;
26 
27 final class RadioTestFragmentUtils {
28 
RadioTestFragmentUtils()29     private RadioTestFragmentUtils() {
30         throw new UnsupportedOperationException("RadioTestFragmentUtils class is noninstantiable");
31     }
32 
getDabChannelName(RadioManager.ProgramInfo info, SparseArray<String> dabFrequencyToLabelMap)33     static String getDabChannelName(RadioManager.ProgramInfo info,
34                                     SparseArray<String> dabFrequencyToLabelMap) {
35         StringBuilder channelTextBuilder = new StringBuilder();
36         channelTextBuilder.append("DAB");
37 
38         int dabFrequency = ProgramSelectorExt.getFrequency(info.getSelector());
39         if (dabFrequency != ProgramSelectorExt.INVALID_IDENTIFIER_VALUE) {
40             channelTextBuilder.append(' ').append(dabFrequencyToLabelMap.get(dabFrequency, ""));
41         }
42 
43         int dabEnsemble = ProgramSelectorExt.getDabEnsemble(info.getSelector());
44         if (dabEnsemble != ProgramSelectorExt.INVALID_IDENTIFIER_VALUE) {
45             channelTextBuilder.append(" Ensemble:0x").append(Integer.toHexString(dabEnsemble));
46         }
47         return channelTextBuilder.toString();
48     }
49 
getDabFrequencyToLabelMap(Map<String, Integer> dabFrequencyTable)50     static SparseArray<String> getDabFrequencyToLabelMap(Map<String, Integer> dabFrequencyTable) {
51         SparseArray<String> dabFrequencyToLabelMap = new SparseArray<>();
52         if (dabFrequencyTable == null) {
53             return dabFrequencyToLabelMap;
54         }
55         Iterator<Map.Entry<String, Integer>> it = dabFrequencyTable.entrySet().iterator();
56         while (it.hasNext()) {
57             Map.Entry<String, Integer> dabFrequencyEntry = it.next();
58             dabFrequencyToLabelMap.put(dabFrequencyEntry.getValue(), dabFrequencyEntry.getKey());
59         }
60         return dabFrequencyToLabelMap;
61     }
62 }
63