1 /*
2  * Copyright (C) 2017 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 #include "VirtualProgram.h"
17 
18 #include <broadcastradio-utils-1x/Utils.h>
19 
20 #include "resources.h"
21 
22 namespace android {
23 namespace hardware {
24 namespace broadcastradio {
25 namespace V1_1 {
26 namespace implementation {
27 
28 using std::vector;
29 
30 using V1_0::MetaData;
31 using V1_0::MetadataKey;
32 using V1_0::MetadataType;
33 using V1_1::IdentifierType;
34 using V1_1::ProgramInfo;
35 using V1_1::VendorKeyValue;
36 using utils::HalRevision;
37 
createDemoBitmap(MetadataKey key,HalRevision halRev)38 static MetaData createDemoBitmap(MetadataKey key, HalRevision halRev) {
39     MetaData bmp = {MetadataType::INT, key, resources::demoPngId, {}, {}, {}};
40     if (halRev < HalRevision::V1_1) {
41         bmp.type = MetadataType::RAW;
42         bmp.intValue = 0;
43         bmp.rawValue = hidl_vec<uint8_t>(resources::demoPng, std::end(resources::demoPng));
44     }
45     return bmp;
46 }
47 
getProgramInfo(HalRevision halRev) const48 ProgramInfo VirtualProgram::getProgramInfo(HalRevision halRev) const {
49     ProgramInfo info11 = {};
50     auto& info10 = info11.base;
51 
52     utils::getLegacyChannel(selector, &info10.channel, &info10.subChannel);
53     info11.selector = selector;
54     info10.tuned = true;
55     info10.stereo = true;
56     info10.digital = utils::isDigital(selector);
57     info10.signalStrength = info10.digital ? 100 : 80;
58 
59     info10.metadata = hidl_vec<MetaData>({
60         {MetadataType::TEXT, MetadataKey::RDS_PS, {}, {}, programName, {}},
61         {MetadataType::TEXT, MetadataKey::TITLE, {}, {}, songTitle, {}},
62         {MetadataType::TEXT, MetadataKey::ARTIST, {}, {}, songArtist, {}},
63         createDemoBitmap(MetadataKey::ICON, halRev),
64         createDemoBitmap(MetadataKey::ART, halRev),
65     });
66 
67     info11.vendorInfo = hidl_vec<VendorKeyValue>({
68         {"com.google.dummy", "dummy"},
69         {"com.google.dummy.VirtualProgram", std::to_string(reinterpret_cast<uintptr_t>(this))},
70     });
71 
72     return info11;
73 }
74 
75 // Defining order on virtual programs, how they appear on band.
76 // It's mostly for default implementation purposes, may not be complete or correct.
operator <(const VirtualProgram & lhs,const VirtualProgram & rhs)77 bool operator<(const VirtualProgram& lhs, const VirtualProgram& rhs) {
78     auto& l = lhs.selector;
79     auto& r = rhs.selector;
80 
81     // Two programs with the same primaryId is considered the same.
82     if (l.programType != r.programType) return l.programType < r.programType;
83     if (l.primaryId.type != r.primaryId.type) return l.primaryId.type < r.primaryId.type;
84     if (l.primaryId.value != r.primaryId.value) return l.primaryId.value < r.primaryId.value;
85 
86     return false;
87 }
88 
getProgramInfoVector(const vector<VirtualProgram> & vec,HalRevision halRev)89 vector<ProgramInfo> getProgramInfoVector(const vector<VirtualProgram>& vec, HalRevision halRev) {
90     vector<ProgramInfo> out;
91     out.reserve(vec.size());
92     for (auto&& program : vec) {
93         out.push_back(program.getProgramInfo(halRev));
94     }
95     return out;
96 }
97 
98 }  // namespace implementation
99 }  // namespace V1_1
100 }  // namespace broadcastradio
101 }  // namespace hardware
102 }  // namespace android
103