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