• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_local_live_view_content.h"
17 
18 #include <cstdint>
19 #include <string>                            // for basic_string, operator+
20 #include <algorithm>                         // for min
21 #include <vector>
22 
23 #include "ans_log_wrapper.h"
24 #include "nlohmann/json.hpp"                 // for json, basic_json<>::obje...
25 #include "notification_action_button.h"
26 #include "notification_basic_content.h"      // for NotificationBasicContent
27 #include "notification_capsule.h"
28 #include "notification_content.h"
29 #include "notification_json_convert.h"
30 #include "notification_progress.h"
31 #include "notification_local_live_view_button.h"
32 #include "notification_time.h"
33 #include "parcel.h"                          // for Parcel
34 
35 namespace OHOS {
36 namespace Notification {
37 
SetType(int32_t type)38 void NotificationLocalLiveViewContent::SetType(int32_t type)
39 {
40     type_ = type;
41 }
42 
GetType()43 int32_t NotificationLocalLiveViewContent::GetType()
44 {
45     return type_;
46 }
47 
SetCapsule(NotificationCapsule capsule)48 void NotificationLocalLiveViewContent::SetCapsule(NotificationCapsule capsule)
49 {
50     capsule_ = capsule;
51 }
52 
GetCapsule()53 NotificationCapsule NotificationLocalLiveViewContent::GetCapsule()
54 {
55     return capsule_;
56 }
57 
SetButton(NotificationLocalLiveViewButton button)58 void NotificationLocalLiveViewContent::SetButton(NotificationLocalLiveViewButton button)
59 {
60     button_ = button;
61 }
62 
GetButton()63 NotificationLocalLiveViewButton NotificationLocalLiveViewContent::GetButton()
64 {
65     return button_;
66 }
67 
SetCardButton(std::vector<NotificationIconButton> buttons)68 void NotificationLocalLiveViewContent::SetCardButton(std::vector<NotificationIconButton> buttons)
69 {
70     card_button_ = buttons;
71 }
72 
GetCardButton()73 std::vector<NotificationIconButton> NotificationLocalLiveViewContent::GetCardButton()
74 {
75     return card_button_;
76 }
77 
SetProgress(NotificationProgress progress)78 void NotificationLocalLiveViewContent::SetProgress(NotificationProgress progress)
79 {
80     progress_ = progress;
81 }
82 
GetProgress()83 NotificationProgress NotificationLocalLiveViewContent::GetProgress()
84 {
85     return progress_;
86 }
87 
SetTime(NotificationTime time)88 void NotificationLocalLiveViewContent::SetTime(NotificationTime time)
89 {
90     time_ = time;
91 }
92 
GetTime()93 NotificationTime NotificationLocalLiveViewContent::GetTime()
94 {
95     return time_;
96 }
97 
addFlag(int32_t flag)98 void NotificationLocalLiveViewContent::addFlag(int32_t flag)
99 {
100     flags_.emplace_back(flag);
101 }
102 
isFlagExist(int32_t flag)103 bool NotificationLocalLiveViewContent::isFlagExist(int32_t flag)
104 {
105     auto it = std::find(flags_.begin(), flags_.end(), flag);
106     if (it != flags_.end()) {
107         return true;
108     } else {
109         return false;
110     }
111 }
112 
SetLiveViewType(const LiveViewTypes type)113 void NotificationLocalLiveViewContent::SetLiveViewType(const LiveViewTypes type)
114 {
115     liveviewType_ = type;
116 }
117 
GetLiveViewType() const118 NotificationLocalLiveViewContent::LiveViewTypes NotificationLocalLiveViewContent::GetLiveViewType() const
119 {
120     return liveviewType_;
121 }
122 
Dump()123 std::string NotificationLocalLiveViewContent::Dump()
124 {
125     return "NotificationLocalLiveViewContent{ " + NotificationBasicContent::Dump() +
126             ", type = " + std::to_string(type_) +
127             ", capsule = " + capsule_.Dump() +
128             ", button = " + button_.Dump() +
129             ", progress = " + progress_.Dump() +
130             ", time = " + time_.Dump() +
131             ", liveviewType = " + std::to_string(static_cast<int32_t>(liveviewType_)) +
132             " }";
133 }
134 
ToJson(nlohmann::json & jsonObject) const135 bool NotificationLocalLiveViewContent::ToJson(nlohmann::json &jsonObject) const
136 {
137     if (!NotificationBasicContent::ToJson(jsonObject)) {
138         ANS_LOGE("Cannot convert basicContent to JSON");
139         return false;
140     }
141 
142     nlohmann::json capsuleObj;
143     if (!NotificationJsonConverter::ConvertToJson(&capsule_, capsuleObj)) {
144         ANS_LOGE("Cannot convert capsule to JSON");
145         return false;
146     }
147 
148     nlohmann::json buttonObj;
149     if (!NotificationJsonConverter::ConvertToJson(&button_, buttonObj)) {
150         ANS_LOGE("Cannot convert button to JSON");
151         return false;
152     }
153 
154     nlohmann::json progressObj;
155     if (!NotificationJsonConverter::ConvertToJson(&progress_, progressObj)) {
156         ANS_LOGE("Cannot convert progress to JSON");
157         return false;
158     }
159 
160     nlohmann::json timeObj;
161     if (!NotificationJsonConverter::ConvertToJson(&time_, timeObj)) {
162         ANS_LOGE("Cannot convert time to JSON");
163         return false;
164     }
165 
166     nlohmann::json cardBtnArr = nlohmann::json::array();
167     for (auto btn : card_button_) {
168         nlohmann::json cardBtnObj;
169         if (!NotificationJsonConverter::ConvertToJson(&btn, cardBtnObj)) {
170             ANS_LOGE("Cannot convert button to JSON");
171             return false;
172         }
173         cardBtnArr.emplace_back(cardBtnObj);
174     }
175 
176     jsonObject["type"] = type_;
177     jsonObject["capsule"] = capsuleObj;
178     jsonObject["button"] = buttonObj;
179     jsonObject["cardButtons"] = cardBtnArr;
180     jsonObject["progress"] = progressObj;
181     jsonObject["time"] = timeObj;
182     jsonObject["flags"] = nlohmann::json(flags_);
183     jsonObject["liveviewType"] = static_cast<int32_t>(liveviewType_);
184 
185     return true;
186 }
187 
FromJson(const nlohmann::json & jsonObject)188 NotificationLocalLiveViewContent *NotificationLocalLiveViewContent::FromJson(const nlohmann::json &jsonObject)
189 {
190     if (jsonObject.is_null() or !jsonObject.is_object()) {
191         ANS_LOGE("Invalid JSON object");
192         return nullptr;
193     }
194 
195     auto pContent = new (std::nothrow) NotificationLocalLiveViewContent();
196     if (pContent == nullptr) {
197         ANS_LOGE("null pContent");
198         return nullptr;
199     }
200 
201     pContent->ReadFromJson(jsonObject);
202 
203     const auto &jsonEnd = jsonObject.cend();
204     if (jsonObject.find("typeCode") != jsonEnd && jsonObject.at("typeCode").is_number_integer()) {
205         pContent->type_ = jsonObject.at("typeCode").get<int32_t>();
206     }
207 
208     if (jsonObject.find("capsule") != jsonEnd) {
209         auto capsuleObj = jsonObject.at("capsule");
210         auto pCapsule = NotificationJsonConverter::ConvertFromJson<NotificationCapsule>(capsuleObj);
211         if (pCapsule != nullptr) {
212             pContent->capsule_ = *pCapsule;
213             delete pCapsule;
214             pCapsule = nullptr;
215         }
216     }
217 
218     if (jsonObject.find("button") != jsonEnd) {
219         auto buttonObj = jsonObject.at("button");
220         auto pButton = NotificationJsonConverter::ConvertFromJson<NotificationLocalLiveViewButton>(buttonObj);
221         if (pButton != nullptr) {
222             pContent->button_ = *pButton;
223             delete pButton;
224             pButton = nullptr;
225         }
226     }
227 
228     if (jsonObject.find("cardButtons") != jsonEnd && jsonObject.at("cardButtons").is_array()) {
229         std::vector<NotificationIconButton> cardButtons;
230         for (auto &item : jsonObject.at("cardButtons").items()) {
231             nlohmann::json cardBtnObject = item.value();
232             auto pButton = NotificationJsonConverter::ConvertFromJson<NotificationIconButton>(cardBtnObject);
233             if (pButton != nullptr) {
234                 cardButtons.push_back(*pButton);
235                 delete pButton;
236                 pButton = nullptr;
237             }
238         }
239         pContent->card_button_ = cardButtons;
240     }
241 
242     if (jsonObject.find("progress") != jsonEnd) {
243         auto progressObj = jsonObject.at("progress");
244         auto pProgress = NotificationJsonConverter::ConvertFromJson<NotificationProgress>(progressObj);
245         if (pProgress != nullptr) {
246             pContent->progress_ = *pProgress;
247             delete pProgress;
248             pProgress = nullptr;
249         }
250     }
251 
252     if (jsonObject.find("time") != jsonEnd) {
253         auto timeObj = jsonObject.at("time");
254         auto pTime = NotificationJsonConverter::ConvertFromJson<NotificationTime>(timeObj);
255         if (pTime != nullptr) {
256             pContent->time_ = *pTime;
257             delete pTime;
258             pTime = nullptr;
259         }
260     }
261 
262     if (jsonObject.find("flags") != jsonEnd && jsonObject.at("flags").is_array()) {
263         pContent->flags_ = jsonObject.at("flags").get<std::vector<int32_t>>();
264     }
265 
266     if (jsonObject.find("liveviewType") != jsonEnd && jsonObject.at("liveviewType").is_number_integer()) {
267         auto typeValue = jsonObject.at("liveviewType").get<int32_t>();
268         pContent->liveviewType_ = static_cast<NotificationLocalLiveViewContent::LiveViewTypes>(typeValue);
269     }
270 
271     return pContent;
272 }
273 
Marshalling(Parcel & parcel) const274 bool NotificationLocalLiveViewContent::Marshalling(Parcel &parcel) const
275 {
276     if (!NotificationBasicContent::Marshalling(parcel)) {
277         ANS_LOGE("Failed to write basic");
278         return false;
279     }
280 
281     if (contentType_ == static_cast<int32_t>(NotificationContent::Type::BASIC_TEXT)) {
282         return true;
283     }
284 
285     if (!parcel.WriteInt32(type_)) {
286         ANS_LOGE("Write type fail.");
287         return false;
288     }
289 
290     if (!parcel.WriteParcelable(&capsule_)) {
291         ANS_LOGE("Failed to write capsule");
292         return false;
293     }
294 
295     if (!parcel.WriteParcelable(&button_)) {
296         ANS_LOGE("Failed to write button");
297         return false;
298     }
299 
300     parcel.WriteInt32(static_cast<int>(card_button_.size()));
301     for (const auto& button : card_button_) {
302         if (!parcel.WriteParcelable(&button)) {
303             ANS_LOGE("Failed to write card button");
304             return false;
305         }
306     }
307 
308     if (!parcel.WriteParcelable(&progress_)) {
309         ANS_LOGE("Failed to write progress");
310         return false;
311     }
312 
313     if (!parcel.WriteParcelable(&time_)) {
314         ANS_LOGE("Failed to write time");
315         return false;
316     }
317 
318     if (!parcel.WriteInt32Vector(flags_)) {
319         ANS_LOGE("Failed to write flags");
320         return false;
321     }
322 
323     if (!parcel.WriteInt32(static_cast<int32_t>(liveviewType_))) {
324         ANS_LOGE("Write liveviewType fail.");
325         return false;
326     }
327 
328     return true;
329 }
330 
Unmarshalling(Parcel & parcel)331 NotificationLocalLiveViewContent *NotificationLocalLiveViewContent::Unmarshalling(Parcel &parcel)
332 {
333     auto pContent = new (std::nothrow) NotificationLocalLiveViewContent();
334     if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
335         delete pContent;
336         pContent = nullptr;
337     }
338 
339     return pContent;
340 }
341 
ReadFromParcel(Parcel & parcel)342 bool NotificationLocalLiveViewContent::ReadFromParcel(Parcel &parcel)
343 {
344     if (!NotificationBasicContent::ReadFromParcel(parcel)) {
345         ANS_LOGE("Failed to read basic");
346         return false;
347     }
348 
349     if (!parcel.ReadInt32(type_)) {
350         ANS_LOGE("Read type failed.");
351         return false;
352     }
353 
354     auto pCapsule = parcel.ReadParcelable<NotificationCapsule>();
355     if (pCapsule == nullptr) {
356         ANS_LOGE("null pCapsule");
357         return false;
358     }
359     capsule_ = *pCapsule;
360     delete pCapsule;
361     pCapsule = nullptr;
362 
363     auto pButton = parcel.ReadParcelable<NotificationLocalLiveViewButton>();
364     if (pButton == nullptr) {
365         ANS_LOGE("Failed to read button");
366         return false;
367     }
368     button_ = *pButton;
369     delete pButton;
370     pButton = nullptr;
371 
372     auto vsize = static_cast<int32_t>(parcel.ReadInt32());
373     vsize = (vsize < BUTTON_MAX_SIZE) ? vsize : BUTTON_MAX_SIZE;
374     card_button_.clear();
375     for (uint32_t i = 0; i < vsize; ++i) {
376         auto btn = parcel.ReadParcelable<NotificationIconButton>();
377         if (btn == nullptr) {
378             ANS_LOGE("null btn");
379             return false;
380         }
381         card_button_.push_back(*btn);
382         delete btn;
383         btn = nullptr;
384     }
385 
386     auto pProgress = parcel.ReadParcelable<NotificationProgress>();
387     if (pProgress == nullptr) {
388         ANS_LOGE("null pProgress");
389         return false;
390     }
391     progress_ = *pProgress;
392     delete pProgress;
393     pProgress = nullptr;
394 
395     auto pTime = parcel.ReadParcelable<NotificationTime>();
396     if (pTime == nullptr) {
397         ANS_LOGE("null pTime");
398         return false;
399     }
400     time_ = *pTime;
401     delete pTime;
402     pTime = nullptr;
403 
404     if (!parcel.ReadInt32Vector(&flags_)) {
405         ANS_LOGE("Failed to read flags");
406         return false;
407     }
408 
409     liveviewType_ = static_cast<NotificationLocalLiveViewContent::LiveViewTypes>(parcel.ReadInt32());
410 
411     return true;
412 }
413 
ClearButton()414 void NotificationLocalLiveViewContent::ClearButton()
415 {
416     button_.ClearButtonIcons();
417     button_.ClearButtonIconsResource();
418 }
419 
ClearCapsuleIcon()420 void NotificationLocalLiveViewContent::ClearCapsuleIcon()
421 {
422     capsule_.ResetIcon();
423 }
424 }  // namespace Notification
425 }  // namespace OHOS
426