1 /*
2 * Copyright (c) 2021-2024 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 isPrivileged_ = other.isPrivileged_;
66 }
67
~Notification()68 Notification::~Notification()
69 {}
70
EnableLight() const71 bool Notification::EnableLight() const
72 {
73 return enableLight_;
74 }
75
EnableSound() const76 bool Notification::EnableSound() const
77 {
78 return enableSound_;
79 }
80
EnableVibrate() const81 bool Notification::EnableVibrate() const
82 {
83 return enableVibration_;
84 }
85
GetBundleName() const86 std::string Notification::GetBundleName() const
87 {
88 if (request_ == nullptr) {
89 return "";
90 }
91 return request_->GetOwnerBundleName();
92 }
93
GetCreateBundle() const94 std::string Notification::GetCreateBundle() const
95 {
96 if (request_ == nullptr) {
97 return "";
98 }
99 return request_->GetCreatorBundleName();
100 }
101
GetLabel() const102 std::string Notification::GetLabel() const
103 {
104 if (request_ == nullptr) {
105 return "";
106 }
107 return request_->GetLabel();
108 }
109
GetLedLightColor() const110 int32_t Notification::GetLedLightColor() const
111 {
112 return ledLightColor_;
113 }
114
GetLockscreenVisibleness() const115 NotificationConstant::VisiblenessType Notification::GetLockscreenVisibleness() const
116 {
117 return lockscreenVisibleness_;
118 }
119
GetGroup() const120 std::string Notification::GetGroup() const
121 {
122 if (request_ == nullptr) {
123 return "";
124 }
125 return request_->GetGroupName();
126 }
127
GetId() const128 int32_t Notification::GetId() const
129 {
130 if (request_ == nullptr) {
131 return -1;
132 }
133 return request_->GetNotificationId();
134 }
135
GetKey() const136 std::string Notification::GetKey() const
137 {
138 return key_;
139 }
140
SetKey(const std::string & key)141 void Notification::SetKey(const std::string& key)
142 {
143 key_ = key;
144 }
145
GetNotificationRequest() const146 NotificationRequest Notification::GetNotificationRequest() const
147 {
148 return *request_;
149 }
150
GetNotificationRequestPoint() const151 sptr<NotificationRequest> Notification::GetNotificationRequestPoint() const
152 {
153 return request_;
154 }
155
GetPostTime() const156 int64_t Notification::GetPostTime() const
157 {
158 return postTime_;
159 }
160
GetSound() const161 Uri Notification::GetSound() const
162 {
163 if (enableSound_ && sound_ != nullptr) {
164 return *sound_;
165 }
166 return Uri("");
167 }
168
GetUid() const169 int32_t Notification::GetUid() const
170 {
171 if (request_ == nullptr) {
172 return 0;
173 }
174 return request_->GetCreatorUid();
175 }
176
GetPid() const177 pid_t Notification::GetPid() const
178 {
179 if (request_ == nullptr) {
180 return 0;
181 }
182 return request_->GetCreatorPid();
183 }
184
IsUnremovable() const185 bool Notification::IsUnremovable() const
186 {
187 if (request_ == nullptr) {
188 return false;
189 }
190 return request_->IsUnremovable();
191 }
192
GetVibrationStyle() const193 std::vector<int64_t> Notification::GetVibrationStyle() const
194 {
195 return vibrationStyle_;
196 }
197
IsGroup() const198 bool Notification::IsGroup() const
199 {
200 if (request_ == nullptr) {
201 return false;
202 }
203 return !(request_->GetGroupName() == "");
204 }
205
IsFloatingIcon() const206 bool Notification::IsFloatingIcon() const
207 {
208 if (request_ == nullptr) {
209 return false;
210 }
211 return request_->IsFloatingIcon();
212 }
213
GetRemindType() const214 NotificationConstant::RemindType Notification::GetRemindType() const
215 {
216 return remindType_;
217 }
218
IsRemoveAllowed() const219 bool Notification::IsRemoveAllowed() const
220 {
221 return isRemoveAllowed_;
222 }
223
GetSourceType() const224 NotificationConstant::SourceType Notification::GetSourceType() const
225 {
226 return sourceType_;
227 }
228
GetDeviceId() const229 std::string Notification::GetDeviceId() const
230 {
231 return deviceId_;
232 }
233
GetUserId() const234 int32_t Notification::GetUserId() const
235 {
236 if (request_ == nullptr) {
237 return 0;
238 }
239 return request_->GetCreatorUserId();
240 }
241
GetRecvUserId() const242 int32_t Notification::GetRecvUserId() const
243 {
244 if (request_ == nullptr) {
245 return 0;
246 }
247 return request_->GetReceiverUserId();
248 }
249
GetInstanceKey() const250 std::string Notification::GetInstanceKey() const
251 {
252 if (request_ == nullptr) {
253 return "";
254 }
255 return request_->GetAppInstanceKey();
256 }
257
GetPrivileged() const258 bool Notification::GetPrivileged() const
259 {
260 return isPrivileged_;
261 }
262
MarshallingBool(Parcel & parcel) const263 bool Notification::MarshallingBool(Parcel &parcel) const
264 {
265 if (!parcel.WriteBool(enableLight_)) {
266 ANS_LOGE("Can't write enableLight_");
267 return false;
268 }
269
270 if (!parcel.WriteBool(enableSound_)) {
271 ANS_LOGE("Can't write enableSound_");
272 return false;
273 }
274
275 if (!parcel.WriteBool(enableVibration_)) {
276 ANS_LOGE("Can't write enableVibration_");
277 return false;
278 }
279
280 if (!parcel.WriteBool(isRemoveAllowed_)) {
281 ANS_LOGE("Can't write isRemoveAllowed");
282 return false;
283 }
284
285 if (!parcel.WriteBool(isPrivileged_)) {
286 ANS_LOGE("Can't write isPrivileged");
287 return false;
288 }
289
290 return true;
291 }
292
MarshallingString(Parcel & parcel) const293 bool Notification::MarshallingString(Parcel &parcel) const
294 {
295 if (!parcel.WriteString(key_)) {
296 ANS_LOGE("Can't write key");
297 return false;
298 }
299
300 if (enableSound_ && sound_ != nullptr) {
301 if (!parcel.WriteString(sound_->ToString())) {
302 ANS_LOGE("Can't write sound");
303 return false;
304 }
305 }
306
307 if (!parcel.WriteString(deviceId_)) {
308 ANS_LOGE("Can't write deviceId");
309 return false;
310 }
311
312 return true;
313 }
314
MarshallingInt32(Parcel & parcel) const315 bool Notification::MarshallingInt32(Parcel &parcel) const
316 {
317 if (!parcel.WriteInt32(ledLightColor_)) {
318 ANS_LOGE("Can't write ledLigthColor");
319 return false;
320 }
321
322 if (!parcel.WriteInt32(static_cast<int32_t>(lockscreenVisibleness_))) {
323 ANS_LOGE("Can't write visbleness");
324 return false;
325 }
326
327 if (!parcel.WriteInt32(static_cast<int32_t>(remindType_))) {
328 ANS_LOGE("Can't write remindType");
329 return false;
330 }
331
332 if (!parcel.WriteInt32(static_cast<int32_t>(sourceType_))) {
333 ANS_LOGE("Can't write sourceType");
334 return false;
335 }
336
337 return true;
338 }
339
MarshallingInt64(Parcel & parcel) const340 bool Notification::MarshallingInt64(Parcel &parcel) const
341 {
342 if (!parcel.WriteInt64(postTime_)) {
343 ANS_LOGE("Can't write postTime");
344 return false;
345 }
346
347 if (!parcel.WriteInt64Vector(vibrationStyle_)) {
348 ANS_LOGE("Can't write vibrationStyle");
349 return false;
350 }
351
352 return true;
353 }
354
MarshallingUint64(Parcel & parcel) const355 bool Notification::MarshallingUint64(Parcel &parcel) const
356 {
357 if (!parcel.WriteUint64(updateTimerId_)) {
358 ANS_LOGE("Can't write update timer id.");
359 return false;
360 }
361
362 if (!parcel.WriteUint64(finishTimerId_)) {
363 ANS_LOGE("Can't write finish timer id.");
364 return false;
365 }
366
367 if (!parcel.WriteUint64(archiveTimerId_)) {
368 ANS_LOGE("Can't write archive timer id.");
369 return false;
370 }
371
372 return true;
373 }
374
MarshallingParcelable(Parcel & parcel) const375 bool Notification::MarshallingParcelable(Parcel &parcel) const
376 {
377 if (!parcel.WriteStrongParcelable(request_)) {
378 ANS_LOGE("Can't write request");
379 return false;
380 }
381
382 return true;
383 }
384
Marshalling(Parcel & parcel) const385 bool Notification::Marshalling(Parcel &parcel) const
386 {
387 if (!MarshallingBool(parcel)) {
388 return false;
389 }
390 if (!MarshallingString(parcel)) {
391 return false;
392 }
393 if (!MarshallingInt32(parcel)) {
394 return false;
395 }
396 if (!MarshallingInt64(parcel)) {
397 return false;
398 }
399 if (!MarshallingUint64(parcel)) {
400 return false;
401 }
402 if (!MarshallingParcelable(parcel)) {
403 return false;
404 }
405
406 return true;
407 }
408
ReadFromParcelBool(Parcel & parcel)409 void Notification::ReadFromParcelBool(Parcel &parcel)
410 {
411 // Read enableLight_
412 enableLight_ = parcel.ReadBool();
413
414 // Read enableSound_
415 enableSound_ = parcel.ReadBool();
416
417 // Read enableVibration_
418 enableVibration_ = parcel.ReadBool();
419
420 // Read isRemoveAllowed_
421 isRemoveAllowed_ = parcel.ReadBool();
422
423 // Read isPrivileged_
424 isPrivileged_ = parcel.ReadBool();
425 }
426
ReadFromParcelString(Parcel & parcel)427 void Notification::ReadFromParcelString(Parcel &parcel)
428 {
429 // Read key_
430 key_ = parcel.ReadString();
431
432 // Read sound_
433 if (enableSound_) {
434 sound_ = std::make_shared<Uri>(parcel.ReadString());
435 }
436
437 // Read deviceId_
438 deviceId_ = parcel.ReadString();
439 }
440
ReadFromParcelInt32(Parcel & parcel)441 void Notification::ReadFromParcelInt32(Parcel &parcel)
442 {
443 // Read ledLightColor_
444 ledLightColor_ = parcel.ReadInt32();
445
446 // Read lockscreenVisibleness_
447 lockscreenVisibleness_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
448
449 // Read remindType_
450 remindType_ = static_cast<NotificationConstant::RemindType>(parcel.ReadInt32());
451
452 // Read sourceType_
453 sourceType_ = static_cast<NotificationConstant::SourceType>(parcel.ReadInt32());
454 }
455
ReadFromParcelInt64(Parcel & parcel)456 void Notification::ReadFromParcelInt64(Parcel &parcel)
457 {
458 // Read postTime_
459 postTime_ = parcel.ReadInt64();
460
461 // Read vibrationStyle_
462 parcel.ReadInt64Vector(&vibrationStyle_);
463 }
464
ReadFromParcelUint64(Parcel & parcel)465 void Notification::ReadFromParcelUint64(Parcel &parcel)
466 {
467 updateTimerId_ = parcel.ReadUint64();
468 finishTimerId_ = parcel.ReadUint64();
469 archiveTimerId_ = parcel.ReadUint64();
470 }
471
ReadFromParcelParcelable(Parcel & parcel)472 bool Notification::ReadFromParcelParcelable(Parcel &parcel)
473 {
474 // Read request_
475 request_ = parcel.ReadStrongParcelable<NotificationRequest>();
476 return request_ != nullptr;
477 }
478
ReadFromParcel(Parcel & parcel)479 bool Notification::ReadFromParcel(Parcel &parcel)
480 {
481 ReadFromParcelBool(parcel);
482 ReadFromParcelString(parcel);
483 ReadFromParcelInt32(parcel);
484 ReadFromParcelInt64(parcel);
485 ReadFromParcelUint64(parcel);
486 if (!ReadFromParcelParcelable(parcel)) {
487 ANS_LOGE("ReadFromParcelParcelable from parcel error");
488 return false;
489 }
490
491 return true;
492 }
493
Unmarshalling(Parcel & parcel)494 Notification *Notification::Unmarshalling(Parcel &parcel)
495 {
496 Notification *n = new (std::nothrow) Notification();
497 if (n && !n->ReadFromParcel(parcel)) {
498 ANS_LOGE("Read from parcel error");
499 delete n;
500 n = nullptr;
501 }
502 return n;
503 }
504
SetEnableSound(const bool & enable)505 void Notification::SetEnableSound(const bool &enable)
506 {
507 enableSound_ = enable;
508 }
509
SetEnableLight(const bool & enable)510 void Notification::SetEnableLight(const bool &enable)
511 {
512 enableLight_ = enable;
513 }
514
SetEnableVibration(const bool & enable)515 void Notification::SetEnableVibration(const bool &enable)
516 {
517 enableVibration_ = enable;
518 }
519
SetLedLightColor(const int32_t & color)520 void Notification::SetLedLightColor(const int32_t &color)
521 {
522 ledLightColor_ = color;
523 }
524
SetLockScreenVisbleness(const NotificationConstant::VisiblenessType & visbleness)525 void Notification::SetLockScreenVisbleness(const NotificationConstant::VisiblenessType &visbleness)
526 {
527 lockscreenVisibleness_ = visbleness;
528 }
529
SetPostTime(const int64_t & time)530 void Notification::SetPostTime(const int64_t &time)
531 {
532 postTime_ = time;
533 }
534
SetSound(const Uri & sound)535 void Notification::SetSound(const Uri &sound)
536 {
537 sound_ = std::make_shared<Uri>(sound.ToString());
538 }
539
SetVibrationStyle(const std::vector<int64_t> & style)540 void Notification::SetVibrationStyle(const std::vector<int64_t> &style)
541 {
542 vibrationStyle_ = style;
543 }
544
SetRemindType(const NotificationConstant::RemindType & reminType)545 void Notification::SetRemindType(const NotificationConstant::RemindType &reminType)
546 {
547 remindType_ = reminType;
548 }
549
SetRemoveAllowed(bool removeAllowed)550 void Notification::SetRemoveAllowed(bool removeAllowed)
551 {
552 isRemoveAllowed_ = removeAllowed;
553 }
554
SetPrivileged(const bool & isPrivileged)555 void Notification::SetPrivileged(const bool &isPrivileged)
556 {
557 isPrivileged_ = isPrivileged;
558 }
559
SetSourceType(NotificationConstant::SourceType sourceType)560 void Notification::SetSourceType(NotificationConstant::SourceType sourceType)
561 {
562 sourceType_ = sourceType;
563 }
564
Dump() const565 std::string Notification::Dump() const
566 {
567 std::string vibrationStyle = "";
568 for (const auto &style : vibrationStyle_) {
569 vibrationStyle += std::to_string(style);
570 vibrationStyle += ", ";
571 }
572 return "Notification{ "
573 "key = " + key_ +
574 ", ledLightColor = " + std::to_string(ledLightColor_) +
575 ", lockscreenVisbleness = " + std::to_string(static_cast<int32_t>(lockscreenVisibleness_)) +
576 ", remindType = " + std::to_string(static_cast<int32_t>(remindType_)) +
577 ", isRemoveAllowed = " + (isRemoveAllowed_ ? "true" : "false") +
578 ", sourceType = " + std::to_string(static_cast<int32_t>(sourceType_)) +
579 ", deviceId = " + deviceId_ +
580 ", request = " + (request_ == nullptr ? "nullptr" : request_->Dump()) +
581 ", postTime = " + std::to_string(postTime_) +
582 ", sound = " + (sound_ == nullptr ? "nullptr" : sound_->ToString()) +
583 ", vibrationStyle = [" + vibrationStyle + "]" +
584 ", updateTimer = " + std::to_string(updateTimerId_) +
585 ", finishTimer = " + std::to_string(finishTimerId_) +
586 ", archiveTimer = " + std::to_string(archiveTimerId_) +
587 ", isPrivileged = " + (isPrivileged_ ? "true" : "false") +
588 " }";
589 }
590
GetUpdateTimer() const591 uint64_t Notification::GetUpdateTimer() const
592 {
593 return updateTimerId_;
594 }
595
SetUpdateTimer(uint64_t updateTimerId)596 void Notification::SetUpdateTimer(uint64_t updateTimerId)
597 {
598 updateTimerId_ = updateTimerId;
599 }
600
GetFinishTimer() const601 uint64_t Notification::GetFinishTimer() const
602 {
603 return finishTimerId_;
604 }
605
SetFinishTimer(uint64_t finishTimerId)606 void Notification::SetFinishTimer(uint64_t finishTimerId)
607 {
608 finishTimerId_ = finishTimerId;
609 }
610
SetArchiveTimer(uint64_t archiveTimerId)611 void Notification::SetArchiveTimer(uint64_t archiveTimerId)
612 {
613 archiveTimerId_ = archiveTimerId;
614 }
615
GetArchiveTimer() const616 uint64_t Notification::GetArchiveTimer() const
617 {
618 return archiveTimerId_;
619 }
620
SetAutoDeletedTimer(uint64_t autoDeletedTimerId)621 void Notification::SetAutoDeletedTimer(uint64_t autoDeletedTimerId)
622 {
623 autoDeletedTimerId_ = autoDeletedTimerId;
624 }
625
GetAutoDeletedTimer() const626 uint64_t Notification::GetAutoDeletedTimer() const
627 {
628 return autoDeletedTimerId_;
629 }
630 } // namespace Notification
631 } // namespace OHOS
632