1 /*
2  * Copyright (c) 2021-2022 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.h"
17 
18 #include <sstream>
19 
20 #include "ans_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace Notification {
Notification()24 Notification::Notification() {};
25 
Notification(const sptr<NotificationRequest> & request)26 Notification::Notification(const sptr<NotificationRequest> &request)
27 {
28     if (request != nullptr) {
29         isRemoveAllowed_ = request->IsRemoveAllowed();
30     }
31     request_ = request;
32     if (request != nullptr) {
33         key_ = request->GetBaseKey("");
34     }
35 }
36 
Notification(const std::string & deviceId,const sptr<NotificationRequest> & request)37 Notification::Notification(const std::string &deviceId, const sptr<NotificationRequest> &request)
38 {
39     deviceId_ = deviceId;
40     request_ = request;
41     if (request != nullptr) {
42         key_ = request->GetBaseKey(deviceId);
43     }
44 }
45 
Notification(const Notification & other)46 Notification::Notification(const Notification &other)
47 {
48     enableSound_ = other.enableSound_;
49     enableLight_ = other.enableLight_;
50     enableVibration_ = other.enableVibration_;
51     key_ = other.key_;
52     ledLightColor_ = other.ledLightColor_;
53     lockscreenVisibleness_ = other.lockscreenVisibleness_;
54     remindType_ = other.remindType_;
55     request_ = other.request_;
56     postTime_ = other.postTime_;
57     sound_ = other.sound_;
58     vibrationStyle_ = other.vibrationStyle_;
59     isRemoveAllowed_ = other.isRemoveAllowed_;
60     sourceType_ = other.sourceType_;
61     deviceId_ = other.deviceId_;
62     updateTimerId_ = other.updateTimerId_;
63     finishTimerId_ = other.finishTimerId_;
64     archiveTimerId_ = other.archiveTimerId_;
65 }
66 
~Notification()67 Notification::~Notification()
68 {}
69 
EnableLight() const70 bool Notification::EnableLight() const
71 {
72     return enableLight_;
73 }
74 
EnableSound() const75 bool Notification::EnableSound() const
76 {
77     return enableSound_;
78 }
79 
EnableVibrate() const80 bool Notification::EnableVibrate() const
81 {
82     return enableVibration_;
83 }
84 
GetBundleName() const85 std::string Notification::GetBundleName() const
86 {
87     if (request_ == nullptr) {
88         return "";
89     }
90     return request_->GetOwnerBundleName();
91 }
92 
GetCreateBundle() const93 std::string Notification::GetCreateBundle() const
94 {
95     if (request_ == nullptr) {
96         return "";
97     }
98     return request_->GetCreatorBundleName();
99 }
100 
GetLabel() const101 std::string Notification::GetLabel() const
102 {
103     if (request_ == nullptr) {
104         return "";
105     }
106     return request_->GetLabel();
107 }
108 
GetLedLightColor() const109 int32_t Notification::GetLedLightColor() const
110 {
111     return ledLightColor_;
112 }
113 
GetLockscreenVisibleness() const114 NotificationConstant::VisiblenessType Notification::GetLockscreenVisibleness() const
115 {
116     return lockscreenVisibleness_;
117 }
118 
GetGroup() const119 std::string Notification::GetGroup() const
120 {
121     if (request_ == nullptr) {
122         return "";
123     }
124     return request_->GetGroupName();
125 }
126 
GetId() const127 int32_t Notification::GetId() const
128 {
129     if (request_ == nullptr) {
130         return -1;
131     }
132     return request_->GetNotificationId();
133 }
134 
GetKey() const135 std::string Notification::GetKey() const
136 {
137     return key_;
138 }
139 
GetNotificationRequest() const140 NotificationRequest Notification::GetNotificationRequest() const
141 {
142     return *request_;
143 }
144 
GetNotificationRequestPoint() const145 sptr<NotificationRequest> Notification::GetNotificationRequestPoint() const
146 {
147     return request_;
148 }
149 
GetPostTime() const150 int64_t Notification::GetPostTime() const
151 {
152     return postTime_;
153 }
154 
GetSound() const155 Uri Notification::GetSound() const
156 {
157     if (enableSound_ && sound_ != nullptr) {
158         return *sound_;
159     }
160     return Uri("");
161 }
162 
GetUid() const163 int32_t Notification::GetUid() const
164 {
165     if (request_ == nullptr) {
166         return 0;
167     }
168     return request_->GetCreatorUid();
169 }
170 
GetPid() const171 pid_t Notification::GetPid() const
172 {
173     if (request_ == nullptr) {
174         return 0;
175     }
176     return request_->GetCreatorPid();
177 }
178 
IsUnremovable() const179 bool Notification::IsUnremovable() const
180 {
181     if (request_ == nullptr) {
182         return false;
183     }
184     return request_->IsUnremovable();
185 }
186 
GetVibrationStyle() const187 std::vector<int64_t> Notification::GetVibrationStyle() const
188 {
189     return vibrationStyle_;
190 }
191 
IsGroup() const192 bool Notification::IsGroup() const
193 {
194     if (request_ == nullptr) {
195         return false;
196     }
197     return !(request_->GetGroupName() == "");
198 }
199 
IsFloatingIcon() const200 bool Notification::IsFloatingIcon() const
201 {
202     if (request_ == nullptr) {
203         return false;
204     }
205     return request_->IsFloatingIcon();
206 }
207 
GetRemindType() const208 NotificationConstant::RemindType Notification::GetRemindType() const
209 {
210     return remindType_;
211 }
212 
IsRemoveAllowed() const213 bool Notification::IsRemoveAllowed() const
214 {
215     return isRemoveAllowed_;
216 }
217 
GetSourceType() const218 NotificationConstant::SourceType Notification::GetSourceType() const
219 {
220     return sourceType_;
221 }
222 
GetDeviceId() const223 std::string Notification::GetDeviceId() const
224 {
225     return deviceId_;
226 }
227 
GetUserId() const228 int32_t Notification::GetUserId() const
229 {
230     if (request_ == nullptr) {
231         return 0;
232     }
233     return request_->GetCreatorUserId();
234 }
235 
GetRecvUserId() const236 int32_t Notification::GetRecvUserId() const
237 {
238     if (request_ == nullptr) {
239         return 0;
240     }
241     return request_->GetReceiverUserId();
242 }
243 
GetInstanceKey() const244 int32_t Notification::GetInstanceKey() const
245 {
246     if (request_ == nullptr) {
247         return 0;
248     }
249     return request_->GetCreatorInstanceKey();
250 }
251 
MarshallingBool(Parcel & parcel) const252 bool Notification::MarshallingBool(Parcel &parcel) const
253 {
254     if (!parcel.WriteBool(enableLight_)) {
255         ANS_LOGE("Can't write enableLight_");
256         return false;
257     }
258 
259     if (!parcel.WriteBool(enableSound_)) {
260         ANS_LOGE("Can't write enableSound_");
261         return false;
262     }
263 
264     if (!parcel.WriteBool(enableVibration_)) {
265         ANS_LOGE("Can't write enableVibration_");
266         return false;
267     }
268 
269     if (!parcel.WriteBool(isRemoveAllowed_)) {
270         ANS_LOGE("Can't write isRemoveAllowed");
271         return false;
272     }
273 
274     return true;
275 }
276 
MarshallingString(Parcel & parcel) const277 bool Notification::MarshallingString(Parcel &parcel) const
278 {
279     if (!parcel.WriteString(key_)) {
280         ANS_LOGE("Can't write key");
281         return false;
282     }
283 
284     if (enableSound_ && sound_ != nullptr) {
285         if (!parcel.WriteString(sound_->ToString())) {
286             ANS_LOGE("Can't write sound");
287             return false;
288         }
289     }
290 
291     if (!parcel.WriteString(deviceId_)) {
292         ANS_LOGE("Can't write deviceId");
293         return false;
294     }
295 
296     return true;
297 }
298 
MarshallingInt32(Parcel & parcel) const299 bool Notification::MarshallingInt32(Parcel &parcel) const
300 {
301     if (!parcel.WriteInt32(ledLightColor_)) {
302         ANS_LOGE("Can't write ledLigthColor");
303         return false;
304     }
305 
306     if (!parcel.WriteInt32(static_cast<int32_t>(lockscreenVisibleness_))) {
307         ANS_LOGE("Can't write visbleness");
308         return false;
309     }
310 
311     if (!parcel.WriteInt32(static_cast<int32_t>(remindType_))) {
312         ANS_LOGE("Can't write remindType");
313         return false;
314     }
315 
316     if (!parcel.WriteInt32(static_cast<int32_t>(sourceType_))) {
317         ANS_LOGE("Can't write sourceType");
318         return false;
319     }
320 
321     return true;
322 }
323 
MarshallingInt64(Parcel & parcel) const324 bool Notification::MarshallingInt64(Parcel &parcel) const
325 {
326     if (!parcel.WriteInt64(postTime_)) {
327         ANS_LOGE("Can't write postTime");
328         return false;
329     }
330 
331     if (!parcel.WriteInt64Vector(vibrationStyle_)) {
332         ANS_LOGE("Can't write vibrationStyle");
333         return false;
334     }
335 
336     return true;
337 }
338 
MarshallingUint64(Parcel & parcel) const339 bool Notification::MarshallingUint64(Parcel &parcel) const
340 {
341     if (!parcel.WriteUint64(updateTimerId_)) {
342         ANS_LOGE("Can't write update timer id.");
343         return false;
344     }
345 
346     if (!parcel.WriteUint64(finishTimerId_)) {
347         ANS_LOGE("Can't write finish timer id.");
348         return false;
349     }
350 
351     if (!parcel.WriteUint64(archiveTimerId_)) {
352         ANS_LOGE("Can't write archive timer id.");
353         return false;
354     }
355 
356     return true;
357 }
358 
MarshallingParcelable(Parcel & parcel) const359 bool Notification::MarshallingParcelable(Parcel &parcel) const
360 {
361     if (!parcel.WriteStrongParcelable(request_)) {
362         ANS_LOGE("Can't write request");
363         return false;
364     }
365 
366     return true;
367 }
368 
Marshalling(Parcel & parcel) const369 bool Notification::Marshalling(Parcel &parcel) const
370 {
371     if (!MarshallingBool(parcel)) {
372         return false;
373     }
374     if (!MarshallingString(parcel)) {
375         return false;
376     }
377     if (!MarshallingInt32(parcel)) {
378         return false;
379     }
380     if (!MarshallingInt64(parcel)) {
381         return false;
382     }
383     if (!MarshallingUint64(parcel)) {
384         return false;
385     }
386     if (!MarshallingParcelable(parcel)) {
387         return false;
388     }
389 
390     return true;
391 }
392 
ReadFromParcelBool(Parcel & parcel)393 void Notification::ReadFromParcelBool(Parcel &parcel)
394 {
395     // Read enableLight_
396     enableLight_ = parcel.ReadBool();
397 
398     // Read enableSound_
399     enableSound_ = parcel.ReadBool();
400 
401     // Read enableVibration_
402     enableVibration_ = parcel.ReadBool();
403 
404     // Read isRemoveAllowed_
405     isRemoveAllowed_ = parcel.ReadBool();
406 }
407 
ReadFromParcelString(Parcel & parcel)408 void Notification::ReadFromParcelString(Parcel &parcel)
409 {
410     // Read key_
411     key_ = parcel.ReadString();
412 
413     // Read sound_
414     if (enableSound_) {
415         sound_ = std::make_shared<Uri>(parcel.ReadString());
416     }
417 
418     // Read deviceId_
419     deviceId_ = parcel.ReadString();
420 }
421 
ReadFromParcelInt32(Parcel & parcel)422 void Notification::ReadFromParcelInt32(Parcel &parcel)
423 {
424     // Read ledLightColor_
425     ledLightColor_ = parcel.ReadInt32();
426 
427     // Read lockscreenVisibleness_
428     lockscreenVisibleness_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
429 
430     // Read remindType_
431     remindType_ = static_cast<NotificationConstant::RemindType>(parcel.ReadInt32());
432 
433     // Read sourceType_
434     sourceType_ = static_cast<NotificationConstant::SourceType>(parcel.ReadInt32());
435 }
436 
ReadFromParcelInt64(Parcel & parcel)437 void Notification::ReadFromParcelInt64(Parcel &parcel)
438 {
439     // Read postTime_
440     postTime_ = parcel.ReadInt64();
441 
442     // Read vibrationStyle_
443     parcel.ReadInt64Vector(&vibrationStyle_);
444 }
445 
ReadFromParcelUint64(Parcel & parcel)446 void Notification::ReadFromParcelUint64(Parcel &parcel)
447 {
448     updateTimerId_ = parcel.ReadUint64();
449     finishTimerId_ = parcel.ReadUint64();
450     archiveTimerId_ = parcel.ReadUint64();
451 }
452 
ReadFromParcelParcelable(Parcel & parcel)453 bool Notification::ReadFromParcelParcelable(Parcel &parcel)
454 {
455     // Read request_
456     request_ = parcel.ReadStrongParcelable<NotificationRequest>();
457     return request_ != nullptr;
458 }
459 
ReadFromParcel(Parcel & parcel)460 bool Notification::ReadFromParcel(Parcel &parcel)
461 {
462     ReadFromParcelBool(parcel);
463     ReadFromParcelString(parcel);
464     ReadFromParcelInt32(parcel);
465     ReadFromParcelInt64(parcel);
466     ReadFromParcelUint64(parcel);
467     if (!ReadFromParcelParcelable(parcel)) {
468         ANS_LOGE("ReadFromParcelParcelable from parcel error");
469         return false;
470     }
471 
472     return true;
473 }
474 
Unmarshalling(Parcel & parcel)475 Notification *Notification::Unmarshalling(Parcel &parcel)
476 {
477     Notification *n = new (std::nothrow) Notification();
478     if (n && !n->ReadFromParcel(parcel)) {
479         ANS_LOGE("Read from parcel error");
480         delete n;
481         n = nullptr;
482     }
483     return n;
484 }
485 
SetEnableSound(const bool & enable)486 void Notification::SetEnableSound(const bool &enable)
487 {
488     enableSound_ = enable;
489 }
490 
SetEnableLight(const bool & enable)491 void Notification::SetEnableLight(const bool &enable)
492 {
493     enableLight_ = enable;
494 }
495 
SetEnableVibration(const bool & enable)496 void Notification::SetEnableVibration(const bool &enable)
497 {
498     enableVibration_ = enable;
499 }
500 
SetLedLightColor(const int32_t & color)501 void Notification::SetLedLightColor(const int32_t &color)
502 {
503     ledLightColor_ = color;
504 }
505 
SetLockScreenVisbleness(const NotificationConstant::VisiblenessType & visbleness)506 void Notification::SetLockScreenVisbleness(const NotificationConstant::VisiblenessType &visbleness)
507 {
508     lockscreenVisibleness_ = visbleness;
509 }
510 
SetPostTime(const int64_t & time)511 void Notification::SetPostTime(const int64_t &time)
512 {
513     postTime_ = time;
514 }
515 
SetSound(const Uri & sound)516 void Notification::SetSound(const Uri &sound)
517 {
518     sound_ = std::make_shared<Uri>(sound.ToString());
519 }
520 
SetVibrationStyle(const std::vector<int64_t> & style)521 void Notification::SetVibrationStyle(const std::vector<int64_t> &style)
522 {
523     vibrationStyle_ = style;
524 }
525 
SetRemindType(const NotificationConstant::RemindType & reminType)526 void Notification::SetRemindType(const NotificationConstant::RemindType &reminType)
527 {
528     remindType_ = reminType;
529 }
530 
SetRemoveAllowed(bool removeAllowed)531 void Notification::SetRemoveAllowed(bool removeAllowed)
532 {
533     isRemoveAllowed_ = removeAllowed;
534 }
535 
SetSourceType(NotificationConstant::SourceType sourceType)536 void Notification::SetSourceType(NotificationConstant::SourceType sourceType)
537 {
538     sourceType_ = sourceType;
539 }
540 
Dump() const541 std::string Notification::Dump() const
542 {
543     std::string vibrationStyle = "";
544     for (const auto &style : vibrationStyle_) {
545         vibrationStyle += std::to_string(style);
546         vibrationStyle += ", ";
547     }
548     return "Notification{ "
549             "key = " + key_ +
550             ", ledLightColor = " + std::to_string(ledLightColor_) +
551             ", lockscreenVisbleness = " + std::to_string(static_cast<int32_t>(lockscreenVisibleness_)) +
552             ", remindType = " + std::to_string(static_cast<int32_t>(remindType_)) +
553             ", isRemoveAllowed = " + (isRemoveAllowed_ ? "true" : "false") +
554             ", sourceType = " + std::to_string(static_cast<int32_t>(sourceType_)) +
555             ", deviceId = " + deviceId_ +
556             ", request = " + (request_ == nullptr ? "nullptr" : request_->Dump()) +
557             ", postTime = " + std::to_string(postTime_) +
558             ", sound = " + (sound_ == nullptr ? "nullptr" : sound_->ToString()) +
559             ", vibrationStyle = [" + vibrationStyle + "]" +
560             ", updateTimer = " + std::to_string(updateTimerId_) +
561             ", finishTimer = " + std::to_string(finishTimerId_) +
562             ", archiveTimer = " + std::to_string(archiveTimerId_) +
563             " }";
564 }
565 
GetUpdateTimer() const566 uint64_t Notification::GetUpdateTimer() const
567 {
568     return updateTimerId_;
569 }
570 
SetUpdateTimer(uint64_t updateTimerId)571 void Notification::SetUpdateTimer(uint64_t updateTimerId)
572 {
573     updateTimerId_ = updateTimerId;
574 }
575 
GetFinishTimer() const576 uint64_t Notification::GetFinishTimer() const
577 {
578     return finishTimerId_;
579 }
580 
SetFinishTimer(uint64_t finishTimerId)581 void Notification::SetFinishTimer(uint64_t finishTimerId)
582 {
583     finishTimerId_ = finishTimerId;
584 }
585 
SetArchiveTimer(uint64_t archiveTimerId)586 void Notification::SetArchiveTimer(uint64_t archiveTimerId)
587 {
588     archiveTimerId_ = archiveTimerId;
589 }
590 
GetArchiveTimer() const591 uint64_t Notification::GetArchiveTimer() const
592 {
593     return archiveTimerId_;
594 }
595 
SetAutoDeletedTimer(uint64_t autoDeletedTimerId)596 void Notification::SetAutoDeletedTimer(uint64_t autoDeletedTimerId)
597 {
598     autoDeletedTimerId_ = autoDeletedTimerId;
599 }
600 
GetAutoDeletedTimer() const601 uint64_t Notification::GetAutoDeletedTimer() const
602 {
603     return autoDeletedTimerId_;
604 }
605 }  // namespace Notification
606 }  // namespace OHOS
607