• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "bundle_icon_box.h"
17 
18 #include "ans_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace Notification {
22 
23 namespace {
24 const int32_t BUNDLE_NAME_TYPE = 1;
25 const int32_t ICON_TYPE = 2;
26 const int32_t LENGTH_TYPE = 3;
27 const int32_t BUNDLE_LABEL_TYPE = 4;
28 const int32_t ICON_START_INDEX = 10;
29 const int32_t BUNDLE_START_INDEX = 2000;
30 }
31 
BundleIconBox()32 BundleIconBox::BundleIconBox()
33 {
34     if (box_ == nullptr) {
35         return;
36     }
37     box_->SetMessageType(BUNDLE_ICON_SYNC);
38 }
39 
BundleIconBox(std::shared_ptr<TlvBox> box)40 BundleIconBox::BundleIconBox(std::shared_ptr<TlvBox> box) : BoxBase(box)
41 {
42 }
43 
SetMessageType(int32_t messageType)44 bool BundleIconBox::SetMessageType(int32_t messageType)
45 {
46     if (box_ == nullptr) {
47         return false;
48     }
49     return box_->SetMessageType(messageType);
50 }
51 
SetIconSyncType(int32_t type)52 bool BundleIconBox::SetIconSyncType(int32_t type)
53 {
54     if (box_ == nullptr) {
55         return false;
56     }
57     return box_->PutValue(std::make_shared<TlvItem>(BUNDLE_ICON_SYNC_TYPE, type));
58 }
59 
SetDataLength(int32_t length)60 bool BundleIconBox::SetDataLength(int32_t length)
61 {
62     if (box_ == nullptr) {
63         return false;
64     }
65     return box_->PutValue(std::make_shared<TlvItem>(LENGTH_TYPE, length));
66 }
67 
SetBundleList(const std::vector<std::string> & bundleList)68 bool BundleIconBox::SetBundleList(const std::vector<std::string>& bundleList)
69 {
70     if (box_ == nullptr) {
71         return false;
72     }
73     int32_t messageType;
74     int32_t index = 0;
75     int32_t startIndex = ICON_START_INDEX;
76     if (box_->GetMessageType(messageType)) {
77         startIndex = (messageType == BUNDLE_ICON_SYNC) ? ICON_START_INDEX : BUNDLE_START_INDEX;
78     }
79     for (auto& bundleName : bundleList) {
80         if (box_->PutValue(std::make_shared<TlvItem>(startIndex + index, bundleName))) {
81             index++;
82         }
83     }
84     return SetDataLength(index);
85 }
86 
SetBundlesIcon(const std::unordered_map<std::string,std::string> & bundles)87 bool BundleIconBox::SetBundlesIcon(const std::unordered_map<std::string, std::string>& bundles)
88 {
89     if (box_ == nullptr || bundles.size() > MAX_ICON_NUM) {
90         return false;
91     }
92     int32_t index = 0;
93     for (auto& bundle : bundles) {
94         TlvBox box;
95         box.PutValue(std::make_shared<TlvItem>(BUNDLE_NAME_TYPE, bundle.first));
96         box.PutValue(std::make_shared<TlvItem>(ICON_TYPE, bundle.second));
97         ANS_LOGW("SetBundlesIcon %{public}s %{public}zu.", bundle.first.c_str(), bundle.second.size());
98         if (!box.Serialize(false)) {
99             ANS_LOGW("Set bundles icon failed %{public}s.", bundle.first.c_str());
100             continue;
101         }
102         box_->PutValue(std::make_shared<TlvItem>(ICON_START_INDEX + index, box.byteBuffer_, box.bytesLength_));
103         index++;
104     }
105     return SetDataLength(index);
106 }
107 
SetLocalDeviceId(const std::string & deviceId)108 bool BundleIconBox::SetLocalDeviceId(const std::string& deviceId)
109 {
110     if (box_ == nullptr) {
111         return false;
112     }
113     return box_->PutValue(std::make_shared<TlvItem>(LOCAL_DEVICE_ID, deviceId));
114 }
115 
SetBundlesInfo(const std::vector<std::pair<std::string,std::string>> & bundles)116 bool BundleIconBox::SetBundlesInfo(const std::vector<std::pair<std::string, std::string>>& bundles)
117 {
118     if (box_ == nullptr || bundles.size() > MAX_BUNDLE_NUM) {
119         return false;
120     }
121     int32_t index = 0;
122     for (auto& bundle : bundles) {
123         TlvBox box;
124         box.PutValue(std::make_shared<TlvItem>(BUNDLE_NAME_TYPE, bundle.first));
125         box.PutValue(std::make_shared<TlvItem>(BUNDLE_LABEL_TYPE, bundle.second));
126         if (!box.Serialize(false)) {
127             ANS_LOGW("Set bundles icon failed %{public}s.", bundle.first.c_str());
128             continue;
129         }
130         box_->PutValue(std::make_shared<TlvItem>(BUNDLE_START_INDEX + index, box.byteBuffer_, box.bytesLength_));
131         index++;
132     }
133     return SetDataLength(index);
134 }
135 
GetIconSyncType(int32_t & type)136 bool BundleIconBox::GetIconSyncType(int32_t& type)
137 {
138     if (box_ == nullptr) {
139         return false;
140     }
141     return box_->GetInt32Value(BUNDLE_ICON_SYNC_TYPE, type);
142 }
143 
GetDataLength(int32_t & length)144 bool BundleIconBox::GetDataLength(int32_t& length)
145 {
146     if (box_ == nullptr) {
147         return false;
148     }
149     return box_->GetInt32Value(LENGTH_TYPE, length);
150 }
151 
GetBundleList(std::vector<std::string> & bundleList)152 bool BundleIconBox::GetBundleList(std::vector<std::string>& bundleList)
153 {
154     int32_t length = 0;
155     if (!GetDataLength(length)) {
156         return false;
157     }
158     int32_t messageType;
159     int32_t index = ICON_START_INDEX;
160     if (box_->GetMessageType(messageType)) {
161         index = (messageType == BUNDLE_ICON_SYNC) ? ICON_START_INDEX : BUNDLE_START_INDEX;
162     }
163     if (length < 0 || length > MAX_BUNDLE_NUM) {
164         ANS_LOGW("Invalid bundles %{public}d.", length);
165         return false;
166     }
167 
168     for (int i = 0; i < length; i++) {
169         std::string bundleName;
170         if (box_->GetStringValue(index + i, bundleName))
171             bundleList.push_back(bundleName);
172     }
173     return true;
174 }
175 
GetBundlesIcon(std::unordered_map<std::string,std::string> & bundles)176 bool BundleIconBox::GetBundlesIcon(std::unordered_map<std::string, std::string>& bundles)
177 {
178     int32_t length = 0;
179     if (!GetDataLength(length)) {
180         ANS_LOGW("Get GetBundlesIcon failed.");
181         return false;
182     }
183     for (int i = 0; i < MAX_ICON_NUM && i < length; i++) {
184         TlvBox box;
185         std::string icon;
186         std::string bundleName;
187         if (!box_->GetObjectValue(ICON_START_INDEX + i, box)) {
188             ANS_LOGW("Get bundles icon failed %{public}d.", i);
189             continue;
190         }
191         if (box.GetStringValue(ICON_TYPE, icon) &&
192             box.GetStringValue(BUNDLE_NAME_TYPE, bundleName)) {
193             ANS_LOGD("GetBundlesIcon %{public}s %{public}zu.", bundleName.c_str(), icon.size());
194             bundles.insert(std::make_pair(bundleName, icon));
195         }
196     }
197     return true;
198 }
199 
GetLocalDeviceId(std::string & deviceId) const200 bool BundleIconBox::GetLocalDeviceId(std::string& deviceId) const
201 {
202     if (box_ == nullptr) {
203         return false;
204     }
205     return box_->GetStringValue(LOCAL_DEVICE_ID, deviceId);
206 }
207 
GetBundlesInfo(std::vector<std::string> & bundles,std::vector<std::string> & labels)208 bool BundleIconBox::GetBundlesInfo(std::vector<std::string>& bundles, std::vector<std::string>& labels)
209 {
210     int32_t length = 0;
211     if (!GetDataLength(length)) {
212         ANS_LOGW("Get GetBundles Info failed.");
213         return false;
214     }
215 
216     if (length < 0 || length > MAX_BUNDLE_NUM) {
217         ANS_LOGW("Invalid bundles %{public}d.", length);
218         return false;
219     }
220 
221     for (int i = 0; i < length; i++) {
222         TlvBox box;
223         std::string bundleLabel;
224         std::string bundleName;
225         if (!box_->GetObjectValue(BUNDLE_START_INDEX + i, box)) {
226             ANS_LOGW("Get bundles icon failed %{public}d.", i);
227             continue;
228         }
229         if (box.GetStringValue(BUNDLE_NAME_TYPE, bundleName) &&
230             box.GetStringValue(BUNDLE_LABEL_TYPE, bundleLabel)) {
231             ANS_LOGD("Get bundle Info %{public}s %{public}s.", bundleName.c_str(), bundleLabel.c_str());
232             bundles.emplace_back(bundleName);
233             labels.emplace_back(bundleLabel);
234         }
235     }
236     return true;
237 }
238 }
239 }
240