1 /*
2  * Copyright (C) 2019 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 package com.android.server.broadcastradio.hal2;
17 
18 import android.hardware.broadcastradio.V2_0.IdentifierType;
19 import android.hardware.broadcastradio.V2_0.ProgramIdentifier;
20 import android.hardware.broadcastradio.V2_0.ProgramInfo;
21 import android.hardware.broadcastradio.V2_0.VendorKeyValue;
22 import android.hardware.radio.ProgramSelector;
23 import android.hardware.radio.RadioManager;
24 import android.hardware.radio.RadioMetadata;
25 import android.util.ArrayMap;
26 
27 import java.util.ArrayList;
28 
29 final class TestUtils {
30 
TestUtils()31     private TestUtils() {
32         throw new UnsupportedOperationException("TestUtils class is noninstantiable");
33     }
34 
makeDefaultModuleProperties()35     static RadioManager.ModuleProperties makeDefaultModuleProperties() {
36         return new RadioManager.ModuleProperties(
37                 /* id= */ 0, /* serviceName= */ "", /* classId= */ 0, /* implementor= */ "",
38                 /* product= */ "", /* version= */ "", /* serial= */ "", /* numTuners= */ 0,
39                 /* numAudioSources= */ 0, /* isInitializationRequired= */ false,
40                 /* isCaptureSupported= */ false, /* bands= */ null,
41                 /* isBgScanSupported= */ false, new int[] {}, new int[] {},
42                 /* dabFrequencyTable= */ null, /* vendorInfo= */ null);
43     }
44 
makeProgramInfo(ProgramSelector selector, ProgramSelector.Identifier logicallyTunedTo, ProgramSelector.Identifier physicallyTunedTo, int signalQuality)45     static RadioManager.ProgramInfo makeProgramInfo(ProgramSelector selector,
46             ProgramSelector.Identifier logicallyTunedTo,
47             ProgramSelector.Identifier physicallyTunedTo, int signalQuality) {
48         return new RadioManager.ProgramInfo(selector,
49                 logicallyTunedTo, physicallyTunedTo, /* relatedContents= */ null,
50                 /* infoFlags= */ 0, signalQuality,
51                 new RadioMetadata.Builder().build(), new ArrayMap<>());
52     }
53 
makeProgramInfo(ProgramSelector selector, int signalQuality)54     static RadioManager.ProgramInfo makeProgramInfo(ProgramSelector selector, int signalQuality) {
55         return makeProgramInfo(selector, selector.getPrimaryId(), selector.getPrimaryId(),
56                 signalQuality);
57     }
58 
makeProgramInfo(int programType, ProgramSelector.Identifier identifier, int signalQuality)59     static RadioManager.ProgramInfo makeProgramInfo(int programType,
60             ProgramSelector.Identifier identifier, int signalQuality) {
61         return makeProgramInfo(makeProgramSelector(programType, identifier),
62                 /* logicallyTunedTo= */ null, /* physicallyTunedTo= */ null, signalQuality);
63     }
64 
makeFmSelector(long freq)65     static ProgramSelector makeFmSelector(long freq) {
66         return makeProgramSelector(ProgramSelector.PROGRAM_TYPE_FM,
67                 new ProgramSelector.Identifier(ProgramSelector.IDENTIFIER_TYPE_AMFM_FREQUENCY,
68                         freq));
69     }
70 
makeProgramSelector(int programType, ProgramSelector.Identifier identifier)71     static ProgramSelector makeProgramSelector(int programType,
72             ProgramSelector.Identifier identifier) {
73         return new ProgramSelector(programType, identifier, /* secondaryIds= */ null,
74                 /* vendorIds= */ null);
75     }
76 
programInfoToHal(RadioManager.ProgramInfo info)77     static ProgramInfo programInfoToHal(RadioManager.ProgramInfo info) {
78         // Note that because Convert does not by design provide functions for all conversions, this
79         // function only copies fields that are set by makeProgramInfo().
80         ProgramInfo hwInfo = new ProgramInfo();
81         hwInfo.selector = Convert.programSelectorToHal(info.getSelector());
82         hwInfo.signalQuality = info.getSignalStrength();
83         return hwInfo;
84     }
85 
makeHalFmSelector(int freq)86     static android.hardware.broadcastradio.V2_0.ProgramSelector makeHalFmSelector(int freq) {
87         ProgramIdentifier halId = new ProgramIdentifier();
88         halId.type = IdentifierType.AMFM_FREQUENCY;
89         halId.value = freq;
90 
91         android.hardware.broadcastradio.V2_0.ProgramSelector halSelector =
92                 new android.hardware.broadcastradio.V2_0.ProgramSelector();
93         halSelector.primaryId = halId;
94         halSelector.secondaryIds = new ArrayList<ProgramIdentifier>();
95         return halSelector;
96     }
97 
makeHalProgramInfo( android.hardware.broadcastradio.V2_0.ProgramSelector hwSel, int hwSignalQuality)98     static ProgramInfo makeHalProgramInfo(
99             android.hardware.broadcastradio.V2_0.ProgramSelector hwSel, int hwSignalQuality) {
100         ProgramInfo hwInfo = new ProgramInfo();
101         hwInfo.selector = hwSel;
102         hwInfo.logicallyTunedTo = hwSel.primaryId;
103         hwInfo.physicallyTunedTo = hwSel.primaryId;
104         hwInfo.signalQuality = hwSignalQuality;
105         hwInfo.relatedContent = new ArrayList<>();
106         hwInfo.metadata = new ArrayList<>();
107         return hwInfo;
108     }
109 
makeVendorKeyValue(String vendorKey, String vendorValue)110     static VendorKeyValue makeVendorKeyValue(String vendorKey, String vendorValue) {
111         VendorKeyValue vendorKeyValue = new VendorKeyValue();
112         vendorKeyValue.key = vendorKey;
113         vendorKeyValue.value = vendorValue;
114         return vendorKeyValue;
115     }
116 
makeAnnouncement(int type, int selectorFreq)117     static android.hardware.broadcastradio.V2_0.Announcement makeAnnouncement(int type,
118             int selectorFreq) {
119         android.hardware.broadcastradio.V2_0.Announcement halAnnouncement =
120                 new android.hardware.broadcastradio.V2_0.Announcement();
121         halAnnouncement.type = (byte) type;
122         halAnnouncement.selector = makeHalFmSelector(selectorFreq);
123         halAnnouncement.vendorInfo = new ArrayList<>();
124         return halAnnouncement;
125     }
126 }
127