• 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 ICON_START_INDEX = 10;
28 }
29 
BundleIconBox()30 BundleIconBox::BundleIconBox()
31 {
32     if (box_ == nullptr) {
33         return;
34     }
35     box_->SetMessageType(BUNDLE_ICON_SYNC);
36 }
37 
BundleIconBox(std::shared_ptr<TlvBox> box)38 BundleIconBox::BundleIconBox(std::shared_ptr<TlvBox> box) : BoxBase(box)
39 {
40 }
41 
SetIconSyncType(int32_t type)42 bool BundleIconBox::SetIconSyncType(int32_t type)
43 {
44     if (box_ == nullptr) {
45         return false;
46     }
47     return box_->PutValue(std::make_shared<TlvItem>(BUNDLE_ICON_SYNC_TYPE, type));
48 }
49 
SetDataLength(int32_t length)50 bool BundleIconBox::SetDataLength(int32_t length)
51 {
52     if (box_ == nullptr) {
53         return false;
54     }
55     return box_->PutValue(std::make_shared<TlvItem>(LENGTH_TYPE, length));
56 }
57 
SetBundleList(const std::vector<std::string> & bundleList)58 bool BundleIconBox::SetBundleList(const std::vector<std::string>& bundleList)
59 {
60     if (box_ == nullptr) {
61         return false;
62     }
63     int32_t index = 0;
64     for (auto& bundleName : bundleList) {
65         if (box_->PutValue(std::make_shared<TlvItem>(ICON_START_INDEX + index, bundleName))) {
66             index++;
67         }
68     }
69     return SetDataLength(index);
70 }
71 
SetBundlesIcon(const std::unordered_map<std::string,std::string> & bundles)72 bool BundleIconBox::SetBundlesIcon(const std::unordered_map<std::string, std::string>& bundles)
73 {
74     if (box_ == nullptr || bundles.size() > MAX_ICON_NUM) {
75         return false;
76     }
77     int32_t index = 0;
78     for (auto& bundle : bundles) {
79         TlvBox box;
80         box.PutValue(std::make_shared<TlvItem>(BUNDLE_NAME_TYPE, bundle.first));
81         box.PutValue(std::make_shared<TlvItem>(ICON_TYPE, bundle.second));
82         ANS_LOGW("SetBundlesIcon %{public}s %{public}d.", bundle.first.c_str(), (int32_t)(bundle.second.size()));
83         if (!box.Serialize(false)) {
84             ANS_LOGW("Set bundles icon failed %{public}s.", bundle.first.c_str());
85             continue;
86         }
87         box_->PutValue(std::make_shared<TlvItem>(ICON_START_INDEX + index, box.byteBuffer_, box.bytesLength_));
88         index++;
89     }
90     return SetDataLength(index);
91 }
92 
SetLocalDeviceId(const std::string & deviceId)93 bool BundleIconBox::SetLocalDeviceId(const std::string& deviceId)
94 {
95     if (box_ == nullptr) {
96         return false;
97     }
98     return box_->PutValue(std::make_shared<TlvItem>(LOCAL_DEVICE_ID, deviceId));
99 }
100 
GetIconSyncType(int32_t & type)101 bool BundleIconBox::GetIconSyncType(int32_t& type)
102 {
103     if (box_ == nullptr) {
104         return false;
105     }
106     return box_->GetInt32Value(BUNDLE_ICON_SYNC_TYPE, type);
107 }
108 
GetDataLength(int32_t & length)109 bool BundleIconBox::GetDataLength(int32_t& length)
110 {
111     if (box_ == nullptr) {
112         return false;
113     }
114     return box_->GetInt32Value(LENGTH_TYPE, length);
115 }
116 
GetBundleList(std::vector<std::string> & bundleList)117 bool BundleIconBox::GetBundleList(std::vector<std::string>& bundleList)
118 {
119     int32_t length = 0;
120     if (!GetDataLength(length)) {
121         return false;
122     }
123     for (int i = 0; i < length; i++) {
124         std::string bundleName;
125         if (box_->GetStringValue(ICON_START_INDEX + i, bundleName))
126             bundleList.push_back(bundleName);
127     }
128     return true;
129 }
130 
GetBundlesIcon(std::unordered_map<std::string,std::string> & bundles)131 bool BundleIconBox::GetBundlesIcon(std::unordered_map<std::string, std::string>& bundles)
132 {
133     int32_t length = 0;
134     if (!GetDataLength(length)) {
135         ANS_LOGW("Get GetBundlesIcon failed.");
136         return false;
137     }
138     for (int i = 0; i < MAX_ICON_NUM && i < length; i++) {
139         TlvBox box;
140         std::string icon;
141         std::string bundleName;
142         if (!box_->GetObjectValue(ICON_START_INDEX + i, box)) {
143             ANS_LOGW("Get bundles icon failed %{public}d.", i);
144             continue;
145         }
146         if (box.GetStringValue(ICON_TYPE, icon) &&
147             box.GetStringValue(BUNDLE_NAME_TYPE, bundleName)) {
148             ANS_LOGI("GetBundlesIcon %{public}s %{public}d.", bundleName.c_str(), (int32_t)(icon.size()));
149             bundles.insert(std::make_pair(bundleName, icon));
150         }
151     }
152     return true;
153 }
154 
GetLocalDeviceId(std::string & deviceId) const155 bool BundleIconBox::GetLocalDeviceId(std::string& deviceId) const
156 {
157     if (box_ == nullptr) {
158         return false;
159     }
160     return box_->GetStringValue(LOCAL_DEVICE_ID, deviceId);
161 }
162 }
163 }
164