1 /*
2 * Copyright (c) 2023 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 "notification_live_view_content.h"
17 #include <string>
18 #include "ans_image_util.h"
19 #include "ans_ipc_common_utils.h"
20 #include "ans_log_wrapper.h"
21 #include "want_params_wrapper.h"
22 #include "ans_const_define.h"
23
24 namespace OHOS {
25 namespace Notification {
26 const uint32_t NotificationLiveViewContent::MAX_VERSION {0xffffffff};
SetLiveViewStatus(const LiveViewStatus status)27 void NotificationLiveViewContent::SetLiveViewStatus(const LiveViewStatus status)
28 {
29 liveViewStatus_ = status;
30 }
31
GetLiveViewStatus() const32 NotificationLiveViewContent::LiveViewStatus NotificationLiveViewContent::GetLiveViewStatus() const
33 {
34 return liveViewStatus_;
35 }
36
SetVersion(uint32_t version)37 void NotificationLiveViewContent::SetVersion(uint32_t version)
38 {
39 version_ = version;
40 }
41
GetVersion() const42 uint32_t NotificationLiveViewContent::GetVersion() const
43 {
44 return version_;
45 }
46
SetExtraInfo(const std::shared_ptr<AAFwk::WantParams> & extras)47 void NotificationLiveViewContent::SetExtraInfo(const std::shared_ptr<AAFwk::WantParams> &extras)
48 {
49 extraInfo_ = extras;
50 }
51
GetExtraInfo() const52 std::shared_ptr<AAFwk::WantParams> NotificationLiveViewContent::GetExtraInfo() const
53 {
54 return extraInfo_;
55 }
56
SetPicture(const PictureMap & pictureMap)57 void NotificationLiveViewContent::SetPicture(const PictureMap &pictureMap)
58 {
59 pictureMap_ = pictureMap;
60 }
61
GetPicture() const62 PictureMap NotificationLiveViewContent::GetPicture() const
63 {
64 return pictureMap_;
65 }
66
SetIsOnlyLocalUpdate(const bool & isOnlyLocalUpdate)67 void NotificationLiveViewContent::SetIsOnlyLocalUpdate(const bool &isOnlyLocalUpdate)
68 {
69 isOnlyLocalUpdate_ = isOnlyLocalUpdate;
70 }
71
GetIsOnlyLocalUpdate() const72 bool NotificationLiveViewContent::GetIsOnlyLocalUpdate() const
73 {
74 return isOnlyLocalUpdate_;
75 }
76
Dump()77 std::string NotificationLiveViewContent::Dump()
78 {
79 std::string extraStr{"null"};
80 if (extraInfo_ != nullptr) {
81 AAFwk::WantParamWrapper wWrapper(*extraInfo_);
82 extraStr = wWrapper.ToString();
83 }
84
85 std::string pictureStr {", pictureMap = {"};
86 for (auto &picture : pictureMap_) {
87 pictureStr += " { key = " + picture.first + ", value = " +
88 (picture.second.empty() ? "empty" : "not empty") + " },";
89 }
90 if (pictureStr[pictureStr.length() - 1] == ',') {
91 pictureStr[pictureStr.length() - 1] = ' ';
92 }
93 pictureStr += "}";
94
95 return "NotificationLiveViewContent{ " + NotificationBasicContent::Dump() +
96 ", status = " + std::to_string(static_cast<int32_t>(liveViewStatus_)) + ", version = " +
97 std::to_string(static_cast<int32_t>(version_)) + ", extraInfo = " + extraStr +
98 ", isOnlyLocalUpdate_ = " + (GetIsOnlyLocalUpdate()?"true":"false") + pictureStr + "}";
99 }
100
PictureToJson(nlohmann::json & jsonObject) const101 bool NotificationLiveViewContent::PictureToJson(nlohmann::json &jsonObject) const
102 {
103 nlohmann::json pixelMap;
104
105 if (pictureMap_.empty()) {
106 return true;
107 }
108 for (const auto &picture : pictureMap_) {
109 nlohmann::json pixelRecordArr = nlohmann::json::array();
110 for (const auto &pixelMap : picture.second) {
111 pixelRecordArr.emplace_back(AnsImageUtil::PackImage(pixelMap));
112 }
113 pixelMap[picture.first] = pixelRecordArr;
114 }
115 jsonObject["pictureMap"] = pixelMap;
116 return true;
117 }
118
ToJson(nlohmann::json & jsonObject) const119 bool NotificationLiveViewContent::ToJson(nlohmann::json &jsonObject) const
120 {
121 if (!NotificationBasicContent::ToJson(jsonObject)) {
122 ANS_LOGE("Cannot convert basicContent to JSON");
123 return false;
124 }
125
126 jsonObject["status"] = static_cast<int32_t>(liveViewStatus_);
127 jsonObject["version"] = version_;
128
129 if (extraInfo_) {
130 AAFwk::WantParamWrapper wWrapper(*extraInfo_);
131 jsonObject["extraInfo"] = wWrapper.ToString();
132 }
133
134 jsonObject["isLocalUpdateOnly"] = isOnlyLocalUpdate_;
135
136 return PictureToJson(jsonObject);
137 }
138
ConvertPictureFromJson(const nlohmann::json & jsonObject)139 void NotificationLiveViewContent::ConvertPictureFromJson(const nlohmann::json &jsonObject)
140 {
141 const auto &jsonEnd = jsonObject.cend();
142 if ((jsonObject.find("pictureMap") != jsonEnd) && jsonObject.at("pictureMap").is_object()) {
143 auto pictureMap = jsonObject.at("pictureMap").get<nlohmann::json>();
144 for (auto it = pictureMap.begin(); it != pictureMap.end(); it++) {
145 if (!it.value().is_array()) {
146 continue;
147 }
148 auto pictureArray = it.value().get<std::vector<std::string>>();
149 pictureMap_[it.key()] = std::vector<std::shared_ptr<Media::PixelMap>>();
150 for (const auto &picture : pictureArray) {
151 pictureMap_[it.key()].emplace_back(AnsImageUtil::UnPackImage(picture));
152 }
153 }
154 }
155 }
156
FromJson(const nlohmann::json & jsonObject)157 NotificationLiveViewContent *NotificationLiveViewContent::FromJson(const nlohmann::json &jsonObject)
158 {
159 if (jsonObject.is_null() or !jsonObject.is_object()) {
160 ANS_LOGE("Invalid JSON object");
161 return nullptr;
162 }
163
164 auto *pContent = new (std::nothrow) NotificationLiveViewContent();
165 if (pContent == nullptr) {
166 ANS_LOGE("Failed to create liveViewContent instance");
167 return nullptr;
168 }
169
170 pContent->ReadFromJson(jsonObject);
171
172 const auto &jsonEnd = jsonObject.cend();
173 if (jsonObject.find("status") != jsonEnd && jsonObject.at("status").is_number_integer()) {
174 auto statusValue = jsonObject.at("status").get<int32_t>();
175 pContent->liveViewStatus_ = static_cast<NotificationLiveViewContent::LiveViewStatus>(statusValue);
176 }
177
178 if (jsonObject.find("version") != jsonEnd && jsonObject.at("version").is_number_integer()) {
179 pContent->version_ = jsonObject.at("version").get<uint32_t>();
180 }
181
182 if (jsonObject.find("extraInfo") != jsonEnd && jsonObject.at("extraInfo").is_string()) {
183 std::string extraInfoStr = jsonObject.at("extraInfo").get<std::string>();
184 if (!extraInfoStr.empty()) {
185 AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParamsWithBrackets(extraInfoStr);
186 pContent->extraInfo_ = std::make_shared<AAFwk::WantParams>(params);
187 }
188 }
189 if (jsonObject.find("isOnlyLocalUpdate") != jsonEnd && jsonObject.at("isOnlyLocalUpdate").is_boolean()) {
190 pContent->isOnlyLocalUpdate_ = jsonObject.at("isOnlyLocalUpdate").get<bool>();
191 }
192 pContent->ConvertPictureFromJson(jsonObject);
193 return pContent;
194 }
195
Marshalling(Parcel & parcel) const196 bool NotificationLiveViewContent::Marshalling(Parcel &parcel) const
197 {
198 if (!NotificationBasicContent::Marshalling(parcel)) {
199 ANS_LOGE("Failed to write basic");
200 return false;
201 }
202
203 if (!parcel.WriteInt32(static_cast<int32_t>(liveViewStatus_))) {
204 ANS_LOGE("Failed to write liveView status");
205 return false;
206 }
207
208 if (!parcel.WriteUint32(version_)) {
209 ANS_LOGE("Failed to write version");
210 return false;
211 }
212
213 bool valid{false};
214 if (extraInfo_ != nullptr) {
215 valid = true;
216 }
217 if (!parcel.WriteBool(valid)) {
218 ANS_LOGE("Failed to write the flag which indicate whether extraInfo is null");
219 return false;
220 }
221 if (valid) {
222 if (!parcel.WriteParcelable(extraInfo_.get())) {
223 ANS_LOGE("Failed to write additionalParams");
224 return false;
225 }
226 }
227 if (!parcel.WriteBool(isOnlyLocalUpdate_)) {
228 ANS_LOGE("OnlyLocalUpdate is Failed to write.");
229 return false;
230 }
231 if (!parcel.WriteUint64(pictureMap_.size())) {
232 ANS_LOGE("Failed to write the size of pictureMap.");
233 return false;
234 }
235
236 return MarshallingPictureMap(parcel);
237 }
238
Unmarshalling(Parcel & parcel)239 NotificationLiveViewContent *NotificationLiveViewContent::Unmarshalling(Parcel &parcel)
240 {
241 auto *pContent = new (std::nothrow) NotificationLiveViewContent();
242 if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
243 delete pContent;
244 pContent = nullptr;
245 }
246
247 return pContent;
248 }
249
ReadFromParcel(Parcel & parcel)250 bool NotificationLiveViewContent::ReadFromParcel(Parcel &parcel)
251 {
252 if (!NotificationBasicContent::ReadFromParcel(parcel)) {
253 ANS_LOGE("Failed to read basic");
254 return false;
255 }
256
257 liveViewStatus_ = static_cast<NotificationLiveViewContent::LiveViewStatus>(parcel.ReadInt32());
258 version_ = parcel.ReadUint32();
259
260 bool valid = parcel.ReadBool();
261 if (valid) {
262 extraInfo_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
263 if (!extraInfo_) {
264 ANS_LOGE("Failed to read extraInfo.");
265 return false;
266 }
267 }
268
269 isOnlyLocalUpdate_ = parcel.ReadBool();
270
271 uint64_t len = parcel.ReadUint64();
272 if (len > MAX_PARCELABLE_VECTOR_NUM) {
273 ANS_LOGE("Size exceeds the range.");
274 return false;
275 }
276 for (uint64_t i = 0; i < len; i++) {
277 auto key = parcel.ReadString();
278 std::vector<std::shared_ptr<Media::PixelMap>> pixelMapVec;
279 if (!AnsIpcCommonUtils::ReadParcelableVector(pixelMapVec, parcel)) {
280 ANS_LOGE("Failed to read extraInfo vector string.");
281 return false;
282 }
283 pictureMap_[key] = pixelMapVec;
284 }
285
286 return true;
287 }
288
MarshallingPictureMap(Parcel & parcel) const289 bool NotificationLiveViewContent::MarshallingPictureMap(Parcel &parcel) const
290 {
291 for (const auto &picture : pictureMap_) {
292 if (!parcel.WriteString(picture.first)) {
293 ANS_LOGE("Failed to write picture map key %{public}s.", picture.first.c_str());
294 return false;
295 }
296
297 if (!AnsIpcCommonUtils::WriteParcelableVector(picture.second, parcel)) {
298 ANS_LOGE("Failed to write picture vector of key %{public}s.", picture.first.c_str());
299 return false;
300 }
301 }
302 return true;
303 }
304
ClearPictureMap()305 void NotificationLiveViewContent::ClearPictureMap()
306 {
307 return pictureMap_.clear();
308 }
309
310 } // namespace Notification
311 } // namespace OHOS
312