• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "want_agent_helper.h"
23 #include "ans_const_define.h"
24 
25 namespace OHOS {
26 namespace Notification {
27 const uint32_t NotificationLiveViewContent::MAX_VERSION {0xffffffff};
SetLiveViewStatus(const LiveViewStatus status)28 void NotificationLiveViewContent::SetLiveViewStatus(const LiveViewStatus status)
29 {
30     liveViewStatus_ = status;
31 }
32 
GetLiveViewStatus() const33 NotificationLiveViewContent::LiveViewStatus NotificationLiveViewContent::GetLiveViewStatus() const
34 {
35     return liveViewStatus_;
36 }
37 
SetVersion(uint32_t version)38 void NotificationLiveViewContent::SetVersion(uint32_t version)
39 {
40     version_ = version;
41 }
42 
GetVersion() const43 uint32_t NotificationLiveViewContent::GetVersion() const
44 {
45     return version_;
46 }
47 
SetExtraInfo(const std::shared_ptr<AAFwk::WantParams> & extras)48 void NotificationLiveViewContent::SetExtraInfo(const std::shared_ptr<AAFwk::WantParams> &extras)
49 {
50     extraInfo_ = extras;
51 }
52 
GetExtraInfo() const53 std::shared_ptr<AAFwk::WantParams> NotificationLiveViewContent::GetExtraInfo() const
54 {
55     return extraInfo_;
56 }
57 
SetPicture(const PictureMap & pictureMap)58 void NotificationLiveViewContent::SetPicture(const PictureMap &pictureMap)
59 {
60     pictureMap_ = pictureMap;
61 }
62 
GetPicture() const63 PictureMap NotificationLiveViewContent::GetPicture() const
64 {
65     return pictureMap_;
66 }
67 
SetIsOnlyLocalUpdate(const bool & isOnlyLocalUpdate)68 void NotificationLiveViewContent::SetIsOnlyLocalUpdate(const bool &isOnlyLocalUpdate)
69 {
70     isOnlyLocalUpdate_ = isOnlyLocalUpdate;
71 }
72 
GetIsOnlyLocalUpdate() const73 bool NotificationLiveViewContent::GetIsOnlyLocalUpdate() const
74 {
75     return isOnlyLocalUpdate_;
76 }
77 
SetExtensionWantAgent(const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> & wantAgent)78 void NotificationLiveViewContent::SetExtensionWantAgent(
79     const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> &wantAgent)
80 {
81     extensionWantAgent_ = wantAgent;
82 }
83 
GetExtensionWantAgent() const84 const std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> NotificationLiveViewContent::GetExtensionWantAgent() const
85 {
86     return extensionWantAgent_;
87 }
88 
SetUid(const int32_t uid)89 void NotificationLiveViewContent::SetUid(const int32_t uid)
90 {
91     uid_ = uid;
92 }
93 
GetUid() const94 int32_t NotificationLiveViewContent::GetUid() const
95 {
96     return uid_;
97 }
98 
Dump()99 std::string NotificationLiveViewContent::Dump()
100 {
101     std::string extraStr{"null"};
102     if (extraInfo_ != nullptr) {
103         AAFwk::WantParamWrapper wWrapper(*extraInfo_);
104         extraStr = wWrapper.ToString();
105     }
106 
107     std::string pictureStr {", pictureMap = {"};
108     for (auto &picture : pictureMap_) {
109         pictureStr += " { key = " + picture.first + ", value = " +
110             (picture.second.empty() ? "empty" : "not empty") + " },";
111     }
112     if (pictureStr[pictureStr.length() - 1] == ',') {
113         pictureStr[pictureStr.length() - 1] = ' ';
114     }
115     pictureStr += "}";
116 
117     return "NotificationLiveViewContent{ " + NotificationBasicContent::Dump() +
118         ", status = " + std::to_string(static_cast<int32_t>(liveViewStatus_)) + ", version = " +
119         std::to_string(static_cast<int32_t>(version_)) + ", extraInfo = " + extraStr +
120         ", isOnlyLocalUpdate_ = " + (GetIsOnlyLocalUpdate()?"true":"false") + pictureStr +
121         ", extensionWantAgent_ = " + (extensionWantAgent_ ? "not null" : "null") + "}";
122 }
123 
PictureToJson(nlohmann::json & jsonObject) const124 bool NotificationLiveViewContent::PictureToJson(nlohmann::json &jsonObject) const
125 {
126     nlohmann::json pixelMap;
127 
128     if (pictureMap_.empty()) {
129         return true;
130     }
131     for (const auto &picture : pictureMap_) {
132         nlohmann::json pixelRecordArr = nlohmann::json::array();
133         for (const auto &pixelMap : picture.second) {
134             pixelRecordArr.emplace_back(AnsImageUtil::PackImage(pixelMap));
135         }
136         pixelMap[picture.first] = pixelRecordArr;
137     }
138     jsonObject["pictureMap"] = pixelMap;
139     return true;
140 }
141 
ToJson(nlohmann::json & jsonObject) const142 bool NotificationLiveViewContent::ToJson(nlohmann::json &jsonObject) const
143 {
144     if (!NotificationBasicContent::ToJson(jsonObject)) {
145         ANS_LOGE("Cannot convert basicContent to JSON");
146         return false;
147     }
148 
149     jsonObject["status"] = static_cast<int32_t>(liveViewStatus_);
150     jsonObject["version"] = version_;
151 
152     if (extraInfo_) {
153         AAFwk::WantParamWrapper wWrapper(*extraInfo_);
154         jsonObject["extraInfo"] = wWrapper.ToString();
155     }
156 
157     jsonObject["isLocalUpdateOnly"] = isOnlyLocalUpdate_;
158     if (extensionWantAgent_ != nullptr) {
159         jsonObject["extensionWantAgent"] = AbilityRuntime::WantAgent::WantAgentHelper::ToString(extensionWantAgent_);
160         jsonObject["uid"] = uid_;
161     }
162 
163     return PictureToJson(jsonObject);
164 }
165 
ConvertPictureFromJson(const nlohmann::json & jsonObject)166 void NotificationLiveViewContent::ConvertPictureFromJson(const nlohmann::json &jsonObject)
167 {
168     const auto &jsonEnd = jsonObject.cend();
169     if ((jsonObject.find("pictureMap") != jsonEnd) && jsonObject.at("pictureMap").is_object()) {
170         auto pictureMap = jsonObject.at("pictureMap").get<nlohmann::json>();
171         for (auto it = pictureMap.begin(); it != pictureMap.end(); it++) {
172             if (!it.value().is_array()) {
173                 continue;
174             }
175             auto pictureArray = it.value().get<std::vector<std::string>>();
176             pictureMap_[it.key()] = std::vector<std::shared_ptr<Media::PixelMap>>();
177             for (const auto &picture : pictureArray) {
178                 pictureMap_[it.key()].emplace_back(AnsImageUtil::UnPackImage(picture));
179             }
180         }
181     }
182 }
183 
FromJson(const nlohmann::json & jsonObject)184 NotificationLiveViewContent *NotificationLiveViewContent::FromJson(const nlohmann::json &jsonObject)
185 {
186     if (jsonObject.is_null() or !jsonObject.is_object()) {
187         ANS_LOGE("Invalid JSON object");
188         return nullptr;
189     }
190 
191     auto *pContent = new (std::nothrow) NotificationLiveViewContent();
192     if (pContent == nullptr) {
193         ANS_LOGE("null pContent");
194         return nullptr;
195     }
196 
197     pContent->ReadFromJson(jsonObject);
198 
199     const auto &jsonEnd = jsonObject.cend();
200     if (jsonObject.find("status") != jsonEnd && jsonObject.at("status").is_number_integer()) {
201         auto statusValue = jsonObject.at("status").get<int32_t>();
202         pContent->liveViewStatus_ = static_cast<NotificationLiveViewContent::LiveViewStatus>(statusValue);
203     }
204 
205     if (jsonObject.find("version") != jsonEnd && jsonObject.at("version").is_number_integer()) {
206         pContent->version_ = jsonObject.at("version").get<uint32_t>();
207     }
208 
209     if (jsonObject.find("extraInfo") != jsonEnd && jsonObject.at("extraInfo").is_string()) {
210         std::string extraInfoStr = jsonObject.at("extraInfo").get<std::string>();
211         if (!extraInfoStr.empty()) {
212             AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParamsWithBrackets(extraInfoStr);
213             pContent->extraInfo_ = std::make_shared<AAFwk::WantParams>(params);
214         }
215     }
216     if (jsonObject.find("isOnlyLocalUpdate") != jsonEnd && jsonObject.at("isOnlyLocalUpdate").is_boolean()) {
217         pContent->isOnlyLocalUpdate_ = jsonObject.at("isOnlyLocalUpdate").get<bool>();
218     }
219     pContent->ConvertPictureFromJson(jsonObject);
220 
221     if (jsonObject.find("uid") != jsonEnd && jsonObject.at("uid").is_number_integer()) {
222         pContent->uid_ =jsonObject.at("uid").get<int32_t>();
223     }
224 
225     if (jsonObject.find("extensionWantAgent") != jsonEnd && jsonObject.at("extensionWantAgent").is_string()) {
226         auto extensionWantAgentString  = jsonObject.at("extensionWantAgent").get<std::string>();
227         pContent->extensionWantAgent_ = AbilityRuntime::WantAgent::WantAgentHelper::FromString(
228             extensionWantAgentString, pContent->uid_);
229     } else {
230         ANS_LOGW("no want");
231     }
232     return pContent;
233 }
234 
Marshalling(Parcel & parcel) const235 bool NotificationLiveViewContent::Marshalling(Parcel &parcel) const
236 {
237     if (!NotificationBasicContent::Marshalling(parcel)) {
238         ANS_LOGE("Failed to write basic");
239         return false;
240     }
241 
242     if (!parcel.WriteInt32(static_cast<int32_t>(liveViewStatus_))) {
243         ANS_LOGE("Failed to write liveView status");
244         return false;
245     }
246 
247     if (!parcel.WriteUint32(version_)) {
248         ANS_LOGE("Failed to write version");
249         return false;
250     }
251 
252     bool valid{false};
253     if (extraInfo_ != nullptr) {
254         valid = true;
255     }
256     if (!parcel.WriteBool(valid)) {
257         ANS_LOGE("Failed to write the flag which indicate whether extraInfo is null");
258         return false;
259     }
260     if (valid) {
261         if (!parcel.WriteParcelable(extraInfo_.get())) {
262             ANS_LOGE("Failed to write extraInfo");
263             return false;
264         }
265     }
266     if (!parcel.WriteBool(isOnlyLocalUpdate_)) {
267         ANS_LOGE("OnlyLocalUpdate is Failed to write.");
268         return false;
269     }
270     if (!parcel.WriteUint64(pictureMap_.size())) {
271         ANS_LOGE("Failed to write the size of pictureMap.");
272         return false;
273     }
274 
275     bool res = MarshallingPictureMap(parcel);
276     if (!res) {
277         return res;
278     }
279     return MarshallingExtensionWantAgent(parcel);
280 }
281 
MarshallingExtensionWantAgent(Parcel & parcel) const282 bool NotificationLiveViewContent::MarshallingExtensionWantAgent(Parcel &parcel) const
283 {
284     bool valid{false};
285 
286     valid = extensionWantAgent_ ? true : false;
287     if (!parcel.WriteBool(valid)) {
288         ANS_LOGE("Failed to write the flag which indicate whether wantAgent is null");
289         return false;
290     }
291 
292     if (valid) {
293         if (!parcel.WriteParcelable(extensionWantAgent_.get())) {
294             ANS_LOGE("Failed to write wantAgent");
295             return false;
296         }
297     }
298     return true;
299 }
300 
Unmarshalling(Parcel & parcel)301 NotificationLiveViewContent *NotificationLiveViewContent::Unmarshalling(Parcel &parcel)
302 {
303     auto *pContent = new (std::nothrow) NotificationLiveViewContent();
304     if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
305         delete pContent;
306         pContent = nullptr;
307     }
308 
309     return pContent;
310 }
311 
ReadFromParcel(Parcel & parcel)312 bool NotificationLiveViewContent::ReadFromParcel(Parcel &parcel)
313 {
314     if (!NotificationBasicContent::ReadFromParcel(parcel)) {
315         ANS_LOGE("Failed to read basic");
316         return false;
317     }
318 
319     liveViewStatus_ = static_cast<NotificationLiveViewContent::LiveViewStatus>(parcel.ReadInt32());
320     version_ = parcel.ReadUint32();
321 
322     bool valid = parcel.ReadBool();
323     if (valid) {
324         extraInfo_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
325         if (!extraInfo_) {
326             ANS_LOGE("Failed to read extraInfo.");
327             return false;
328         }
329     }
330 
331     isOnlyLocalUpdate_ = parcel.ReadBool();
332 
333     uint64_t len = parcel.ReadUint64();
334     if (len > MAX_PARCELABLE_VECTOR_NUM) {
335         ANS_LOGE("Size exceeds the range.");
336         return false;
337     }
338     for (uint64_t i = 0; i < len; i++) {
339         auto key = parcel.ReadString();
340         std::vector<std::shared_ptr<Media::PixelMap>> pixelMapVec;
341         if (!AnsIpcCommonUtils::ReadParcelableVector(pixelMapVec, parcel)) {
342             ANS_LOGE("Failed to read extraInfo vector string.");
343             return false;
344         }
345         pictureMap_[key] = pixelMapVec;
346     }
347 
348     valid = parcel.ReadBool();
349     if (valid) {
350         extensionWantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
351             parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
352         if (!extensionWantAgent_) {
353             ANS_LOGE("null wantAgent");
354             return false;
355         }
356     }
357     return true;
358 }
359 
MarshallingPictureMap(Parcel & parcel) const360 bool NotificationLiveViewContent::MarshallingPictureMap(Parcel &parcel) const
361 {
362     for (const auto &picture : pictureMap_) {
363         if (!parcel.WriteString(picture.first)) {
364             ANS_LOGE("Failed to write picture map key %{public}s.", picture.first.c_str());
365             return false;
366         }
367 
368         if (!AnsIpcCommonUtils::WriteParcelableVector(picture.second, parcel)) {
369             ANS_LOGE("Failed to write picture vector of key %{public}s.", picture.first.c_str());
370             return false;
371         }
372     }
373     return true;
374 }
375 
ClearPictureMap()376 void NotificationLiveViewContent::ClearPictureMap()
377 {
378     return pictureMap_.clear();
379 }
380 
381 }  // namespace Notification
382 }  // namespace OHOS
383