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_json_convert.h"
29 #include "notification_progress.h"
30 #include "notification_local_live_view_button.h"
31 #include "notification_time.h"
32 #include "parcel.h" // for Parcel
33
34 namespace OHOS {
35 namespace Notification {
36
SetType(int32_t type)37 void NotificationLocalLiveViewContent::SetType(int32_t type)
38 {
39 type_ = type;
40 }
41
GetType()42 int32_t NotificationLocalLiveViewContent::GetType()
43 {
44 return type_;
45 }
46
SetCapsule(NotificationCapsule capsule)47 void NotificationLocalLiveViewContent::SetCapsule(NotificationCapsule capsule)
48 {
49 capsule_ = capsule;
50 }
51
GetCapsule()52 NotificationCapsule NotificationLocalLiveViewContent::GetCapsule()
53 {
54 return capsule_;
55 }
56
SetButton(NotificationLocalLiveViewButton button)57 void NotificationLocalLiveViewContent::SetButton(NotificationLocalLiveViewButton button)
58 {
59 button_ = button;
60 }
61
GetButton()62 NotificationLocalLiveViewButton NotificationLocalLiveViewContent::GetButton()
63 {
64 return button_;
65 }
66
SetProgress(NotificationProgress progress)67 void NotificationLocalLiveViewContent::SetProgress(NotificationProgress progress)
68 {
69 progress_ = progress;
70 }
71
GetProgress()72 NotificationProgress NotificationLocalLiveViewContent::GetProgress()
73 {
74 return progress_;
75 }
76
SetTime(NotificationTime time)77 void NotificationLocalLiveViewContent::SetTime(NotificationTime time)
78 {
79 time_ = time;
80 }
81
GetTime()82 NotificationTime NotificationLocalLiveViewContent::GetTime()
83 {
84 return time_;
85 }
86
addFlag(int32_t flag)87 void NotificationLocalLiveViewContent::addFlag(int32_t flag)
88 {
89 flags_.emplace_back(flag);
90 }
91
isFlagExist(int32_t flag)92 bool NotificationLocalLiveViewContent::isFlagExist(int32_t flag)
93 {
94 auto it = std::find(flags_.begin(), flags_.end(), flag);
95 if (it != flags_.end()) {
96 return true;
97 } else {
98 return false;
99 }
100 }
101
Dump()102 std::string NotificationLocalLiveViewContent::Dump()
103 {
104 return "NotificationLocalLiveViewContent{ " + NotificationBasicContent::Dump() +
105 ", type = " + std::to_string(type_) +
106 ", capsule = " + capsule_.Dump() +
107 ", button = " + button_.Dump() +
108 ", progress = " + progress_.Dump() +
109 ", time = " + time_.Dump() +
110 " }";
111 }
112
ToJson(nlohmann::json & jsonObject) const113 bool NotificationLocalLiveViewContent::ToJson(nlohmann::json &jsonObject) const
114 {
115 if (!NotificationBasicContent::ToJson(jsonObject)) {
116 ANS_LOGE("Cannot convert basicContent to JSON");
117 return false;
118 }
119
120 nlohmann::json capsuleObj;
121 if (!NotificationJsonConverter::ConvertToJson(&capsule_, capsuleObj)) {
122 ANS_LOGE("Cannot convert capsule to JSON");
123 return false;
124 }
125
126 nlohmann::json buttonObj;
127 if (!NotificationJsonConverter::ConvertToJson(&button_, buttonObj)) {
128 ANS_LOGE("Cannot convert button to JSON");
129 return false;
130 }
131
132 nlohmann::json progressObj;
133 if (!NotificationJsonConverter::ConvertToJson(&progress_, progressObj)) {
134 ANS_LOGE("Cannot convert progress to JSON");
135 return false;
136 }
137
138 nlohmann::json timeObj;
139 if (!NotificationJsonConverter::ConvertToJson(&time_, timeObj)) {
140 ANS_LOGE("Cannot convert time to JSON");
141 return false;
142 }
143
144 jsonObject["type"] = type_;
145 jsonObject["capsule"] = capsuleObj;
146 jsonObject["button"] = buttonObj;
147 jsonObject["progress"] = progressObj;
148 jsonObject["time"] = timeObj;
149 jsonObject["flags"] = nlohmann::json(flags_);
150
151 return true;
152 }
153
FromJson(const nlohmann::json & jsonObject)154 NotificationLocalLiveViewContent *NotificationLocalLiveViewContent::FromJson(const nlohmann::json &jsonObject)
155 {
156 if (jsonObject.is_null() or !jsonObject.is_object()) {
157 ANS_LOGE("Invalid JSON object");
158 return nullptr;
159 }
160
161 auto pContent = new (std::nothrow) NotificationLocalLiveViewContent();
162 if (pContent == nullptr) {
163 ANS_LOGE("Failed to create localLiveViewContent instance");
164 return nullptr;
165 }
166
167 pContent->ReadFromJson(jsonObject);
168
169 const auto &jsonEnd = jsonObject.cend();
170 if (jsonObject.find("typeCode") != jsonEnd) {
171 pContent->type_ = jsonObject.at("typeCode").get<int32_t>();
172 }
173
174 if (jsonObject.find("capsule") != jsonEnd) {
175 auto capsuleObj = jsonObject.at("capsule");
176 auto pCapsule = NotificationJsonConverter::ConvertFromJson<NotificationCapsule>(capsuleObj);
177 if (pCapsule != nullptr) {
178 pContent->capsule_ = *pCapsule;
179 delete pCapsule;
180 pCapsule = nullptr;
181 }
182 }
183
184 if (jsonObject.find("button") != jsonEnd) {
185 auto buttonObj = jsonObject.at("button");
186 auto pButton = NotificationJsonConverter::ConvertFromJson<NotificationLocalLiveViewButton>(buttonObj);
187 if (pButton != nullptr) {
188 pContent->button_ = *pButton;
189 delete pButton;
190 pButton = nullptr;
191 }
192 }
193
194 if (jsonObject.find("progress") != jsonEnd) {
195 auto progressObj = jsonObject.at("progress");
196 auto pProgress = NotificationJsonConverter::ConvertFromJson<NotificationProgress>(progressObj);
197 if (pProgress != nullptr) {
198 pContent->progress_ = *pProgress;
199 delete pProgress;
200 pProgress = nullptr;
201 }
202 }
203
204 if (jsonObject.find("time") != jsonEnd) {
205 auto timeObj = jsonObject.at("time");
206 auto pTime = NotificationJsonConverter::ConvertFromJson<NotificationTime>(timeObj);
207 if (pTime != nullptr) {
208 pContent->time_ = *pTime;
209 delete pTime;
210 pTime = nullptr;
211 }
212 }
213
214 if (jsonObject.find("flags") != jsonEnd && jsonObject.at("flags").is_array()) {
215 pContent->flags_ = jsonObject.at("flags").get<std::vector<int32_t>>();
216 }
217
218 return pContent;
219 }
220
Marshalling(Parcel & parcel) const221 bool NotificationLocalLiveViewContent::Marshalling(Parcel &parcel) const
222 {
223 if (!NotificationBasicContent::Marshalling(parcel)) {
224 ANS_LOGE("Failed to write basic");
225 return false;
226 }
227
228 if (!parcel.WriteInt32(type_)) {
229 ANS_LOGE("Write type fail.");
230 return false;
231 }
232
233 if (!parcel.WriteParcelable(&capsule_)) {
234 ANS_LOGE("Failed to write capsule");
235 return false;
236 }
237
238 if (!parcel.WriteParcelable(&button_)) {
239 ANS_LOGE("Failed to write button");
240 return false;
241 }
242
243 if (!parcel.WriteParcelable(&progress_)) {
244 ANS_LOGE("Failed to write progress");
245 return false;
246 }
247
248 if (!parcel.WriteParcelable(&time_)) {
249 ANS_LOGE("Failed to write time");
250 return false;
251 }
252
253 if (!parcel.WriteInt32Vector(flags_)) {
254 ANS_LOGE("Failed to write flags");
255 return false;
256 }
257
258 return true;
259 }
260
Unmarshalling(Parcel & parcel)261 NotificationLocalLiveViewContent *NotificationLocalLiveViewContent::Unmarshalling(Parcel &parcel)
262 {
263 auto pContent = new (std::nothrow) NotificationLocalLiveViewContent();
264 if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) {
265 delete pContent;
266 pContent = nullptr;
267 }
268
269 return pContent;
270 }
271
ReadFromParcel(Parcel & parcel)272 bool NotificationLocalLiveViewContent::ReadFromParcel(Parcel &parcel)
273 {
274 if (!NotificationBasicContent::ReadFromParcel(parcel)) {
275 ANS_LOGE("Failed to read basic");
276 return false;
277 }
278
279 if (!parcel.ReadInt32(type_)) {
280 ANS_LOGE("Read type failed.");
281 return false;
282 }
283
284 auto pCapsule = parcel.ReadParcelable<NotificationCapsule>();
285 if (pCapsule == nullptr) {
286 ANS_LOGE("Failed to read capsule");
287 return false;
288 }
289 capsule_ = *pCapsule;
290 delete pCapsule;
291 pCapsule = nullptr;
292
293 auto pButton = parcel.ReadParcelable<NotificationLocalLiveViewButton>();
294 if (pButton == nullptr) {
295 ANS_LOGE("Failed to read button");
296 return false;
297 }
298 button_ = *pButton;
299 delete pButton;
300 pButton = nullptr;
301
302 auto pProgress = parcel.ReadParcelable<NotificationProgress>();
303 if (pProgress == nullptr) {
304 ANS_LOGE("Failed to read progress");
305 return false;
306 }
307 progress_ = *pProgress;
308 delete pProgress;
309 pProgress = nullptr;
310
311 auto pTime = parcel.ReadParcelable<NotificationTime>();
312 if (pTime == nullptr) {
313 ANS_LOGE("Failed to read time");
314 return false;
315 }
316 time_ = *pTime;
317 delete pTime;
318 pTime = nullptr;
319
320 if (!parcel.ReadInt32Vector(&flags_)) {
321 ANS_LOGE("Failed to read flags");
322 return false;
323 }
324
325 return true;
326 }
327 } // namespace Notification
328 } // namespace OHOS
329