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 }
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
SetKey(const std::string & key)140 void Notification::SetKey(const std::string& key)
141 {
142 key_ = key;
143 }
144
GetNotificationRequest() const145 NotificationRequest Notification::GetNotificationRequest() const
146 {
147 return *request_;
148 }
149
GetNotificationRequestPoint() const150 sptr<NotificationRequest> Notification::GetNotificationRequestPoint() const
151 {
152 return request_;
153 }
154
GetPostTime() const155 int64_t Notification::GetPostTime() const
156 {
157 return postTime_;
158 }
159
GetSound() const160 Uri Notification::GetSound() const
161 {
162 if (enableSound_ && sound_ != nullptr) {
163 return *sound_;
164 }
165 return Uri("");
166 }
167
GetUid() const168 int32_t Notification::GetUid() const
169 {
170 if (request_ == nullptr) {
171 return 0;
172 }
173 return request_->GetCreatorUid();
174 }
175
GetPid() const176 pid_t Notification::GetPid() const
177 {
178 if (request_ == nullptr) {
179 return 0;
180 }
181 return request_->GetCreatorPid();
182 }
183
IsUnremovable() const184 bool Notification::IsUnremovable() const
185 {
186 if (request_ == nullptr) {
187 return false;
188 }
189 return request_->IsUnremovable();
190 }
191
GetVibrationStyle() const192 std::vector<int64_t> Notification::GetVibrationStyle() const
193 {
194 return vibrationStyle_;
195 }
196
IsGroup() const197 bool Notification::IsGroup() const
198 {
199 if (request_ == nullptr) {
200 return false;
201 }
202 return !(request_->GetGroupName() == "");
203 }
204
IsFloatingIcon() const205 bool Notification::IsFloatingIcon() const
206 {
207 if (request_ == nullptr) {
208 return false;
209 }
210 return request_->IsFloatingIcon();
211 }
212
GetRemindType() const213 NotificationConstant::RemindType Notification::GetRemindType() const
214 {
215 return remindType_;
216 }
217
IsRemoveAllowed() const218 bool Notification::IsRemoveAllowed() const
219 {
220 return isRemoveAllowed_;
221 }
222
GetSourceType() const223 NotificationConstant::SourceType Notification::GetSourceType() const
224 {
225 return sourceType_;
226 }
227
GetDeviceId() const228 std::string Notification::GetDeviceId() const
229 {
230 return deviceId_;
231 }
232
GetUserId() const233 int32_t Notification::GetUserId() const
234 {
235 if (request_ == nullptr) {
236 return 0;
237 }
238 return request_->GetCreatorUserId();
239 }
240
GetRecvUserId() const241 int32_t Notification::GetRecvUserId() const
242 {
243 if (request_ == nullptr) {
244 return 0;
245 }
246 return request_->GetReceiverUserId();
247 }
248
GetInstanceKey() const249 std::string Notification::GetInstanceKey() const
250 {
251 if (request_ == nullptr) {
252 return "";
253 }
254 return request_->GetAppInstanceKey();
255 }
256
MarshallingBool(Parcel & parcel) const257 bool Notification::MarshallingBool(Parcel &parcel) const
258 {
259 if (!parcel.WriteBool(enableLight_)) {
260 ANS_LOGE("Can't write enableLight_");
261 return false;
262 }
263
264 if (!parcel.WriteBool(enableSound_)) {
265 ANS_LOGE("Can't write enableSound_");
266 return false;
267 }
268
269 if (!parcel.WriteBool(enableVibration_)) {
270 ANS_LOGE("Can't write enableVibration_");
271 return false;
272 }
273
274 if (!parcel.WriteBool(isRemoveAllowed_)) {
275 ANS_LOGE("Can't write isRemoveAllowed");
276 return false;
277 }
278
279 return true;
280 }
281
MarshallingString(Parcel & parcel) const282 bool Notification::MarshallingString(Parcel &parcel) const
283 {
284 if (!parcel.WriteString(key_)) {
285 ANS_LOGE("Can't write key");
286 return false;
287 }
288
289 if (enableSound_ && sound_ != nullptr) {
290 if (!parcel.WriteString(sound_->ToString())) {
291 ANS_LOGE("Can't write sound");
292 return false;
293 }
294 }
295
296 if (!parcel.WriteString(deviceId_)) {
297 ANS_LOGE("Can't write deviceId");
298 return false;
299 }
300
301 return true;
302 }
303
MarshallingInt32(Parcel & parcel) const304 bool Notification::MarshallingInt32(Parcel &parcel) const
305 {
306 if (!parcel.WriteInt32(ledLightColor_)) {
307 ANS_LOGE("Can't write ledLightColor");
308 return false;
309 }
310
311 if (!parcel.WriteInt32(static_cast<int32_t>(lockscreenVisibleness_))) {
312 ANS_LOGE("Can't write visbleness");
313 return false;
314 }
315
316 if (!parcel.WriteInt32(static_cast<int32_t>(remindType_))) {
317 ANS_LOGE("Can't write remindType");
318 return false;
319 }
320
321 if (!parcel.WriteInt32(static_cast<int32_t>(sourceType_))) {
322 ANS_LOGE("Can't write sourceType");
323 return false;
324 }
325
326 return true;
327 }
328
MarshallingInt64(Parcel & parcel) const329 bool Notification::MarshallingInt64(Parcel &parcel) const
330 {
331 if (!parcel.WriteInt64(postTime_)) {
332 ANS_LOGE("Can't write postTime");
333 return false;
334 }
335
336 if (!parcel.WriteInt64Vector(vibrationStyle_)) {
337 ANS_LOGE("Can't write vibrationStyle");
338 return false;
339 }
340
341 return true;
342 }
343
MarshallingUint64(Parcel & parcel) const344 bool Notification::MarshallingUint64(Parcel &parcel) const
345 {
346 if (!parcel.WriteUint64(updateTimerId_)) {
347 ANS_LOGE("Can't write update timer id.");
348 return false;
349 }
350
351 if (!parcel.WriteUint64(finishTimerId_)) {
352 ANS_LOGE("Can't write finish timer id.");
353 return false;
354 }
355
356 if (!parcel.WriteUint64(archiveTimerId_)) {
357 ANS_LOGE("Can't write archive timer id.");
358 return false;
359 }
360
361 return true;
362 }
363
MarshallingParcelable(Parcel & parcel) const364 bool Notification::MarshallingParcelable(Parcel &parcel) const
365 {
366 if (!parcel.WriteStrongParcelable(request_)) {
367 ANS_LOGE("Can't write request");
368 return false;
369 }
370
371 return true;
372 }
373
Marshalling(Parcel & parcel) const374 bool Notification::Marshalling(Parcel &parcel) const
375 {
376 if (!MarshallingBool(parcel)) {
377 return false;
378 }
379 if (!MarshallingString(parcel)) {
380 return false;
381 }
382 if (!MarshallingInt32(parcel)) {
383 return false;
384 }
385 if (!MarshallingInt64(parcel)) {
386 return false;
387 }
388 if (!MarshallingUint64(parcel)) {
389 return false;
390 }
391 if (!MarshallingParcelable(parcel)) {
392 return false;
393 }
394
395 return true;
396 }
397
ReadFromParcelBool(Parcel & parcel)398 void Notification::ReadFromParcelBool(Parcel &parcel)
399 {
400 // Read enableLight_
401 enableLight_ = parcel.ReadBool();
402
403 // Read enableSound_
404 enableSound_ = parcel.ReadBool();
405
406 // Read enableVibration_
407 enableVibration_ = parcel.ReadBool();
408
409 // Read isRemoveAllowed_
410 isRemoveAllowed_ = parcel.ReadBool();
411 }
412
ReadFromParcelString(Parcel & parcel)413 void Notification::ReadFromParcelString(Parcel &parcel)
414 {
415 // Read key_
416 key_ = parcel.ReadString();
417
418 // Read sound_
419 if (enableSound_) {
420 sound_ = std::make_shared<Uri>(parcel.ReadString());
421 }
422
423 // Read deviceId_
424 deviceId_ = parcel.ReadString();
425 }
426
ReadFromParcelInt32(Parcel & parcel)427 void Notification::ReadFromParcelInt32(Parcel &parcel)
428 {
429 // Read ledLightColor_
430 ledLightColor_ = parcel.ReadInt32();
431
432 // Read lockscreenVisibleness_
433 lockscreenVisibleness_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
434
435 // Read remindType_
436 remindType_ = static_cast<NotificationConstant::RemindType>(parcel.ReadInt32());
437
438 // Read sourceType_
439 sourceType_ = static_cast<NotificationConstant::SourceType>(parcel.ReadInt32());
440 }
441
ReadFromParcelInt64(Parcel & parcel)442 void Notification::ReadFromParcelInt64(Parcel &parcel)
443 {
444 // Read postTime_
445 postTime_ = parcel.ReadInt64();
446
447 // Read vibrationStyle_
448 parcel.ReadInt64Vector(&vibrationStyle_);
449 }
450
ReadFromParcelUint64(Parcel & parcel)451 void Notification::ReadFromParcelUint64(Parcel &parcel)
452 {
453 updateTimerId_ = parcel.ReadUint64();
454 finishTimerId_ = parcel.ReadUint64();
455 archiveTimerId_ = parcel.ReadUint64();
456 }
457
ReadFromParcelParcelable(Parcel & parcel)458 bool Notification::ReadFromParcelParcelable(Parcel &parcel)
459 {
460 // Read request_
461 request_ = parcel.ReadStrongParcelable<NotificationRequest>();
462 return request_ != nullptr;
463 }
464
ReadFromParcel(Parcel & parcel)465 bool Notification::ReadFromParcel(Parcel &parcel)
466 {
467 ReadFromParcelBool(parcel);
468 ReadFromParcelString(parcel);
469 ReadFromParcelInt32(parcel);
470 ReadFromParcelInt64(parcel);
471 ReadFromParcelUint64(parcel);
472 if (!ReadFromParcelParcelable(parcel)) {
473 ANS_LOGE("ReadFromParcelParcelable from parcel error");
474 return false;
475 }
476
477 return true;
478 }
479
Unmarshalling(Parcel & parcel)480 Notification *Notification::Unmarshalling(Parcel &parcel)
481 {
482 Notification *n = new (std::nothrow) Notification();
483 if (n && !n->ReadFromParcel(parcel)) {
484 ANS_LOGE("Read from parcel error");
485 delete n;
486 n = nullptr;
487 }
488 return n;
489 }
490
SetEnableSound(const bool & enable)491 void Notification::SetEnableSound(const bool &enable)
492 {
493 enableSound_ = enable;
494 }
495
SetEnableLight(const bool & enable)496 void Notification::SetEnableLight(const bool &enable)
497 {
498 enableLight_ = enable;
499 }
500
SetEnableVibration(const bool & enable)501 void Notification::SetEnableVibration(const bool &enable)
502 {
503 enableVibration_ = enable;
504 }
505
SetLedLightColor(const int32_t & color)506 void Notification::SetLedLightColor(const int32_t &color)
507 {
508 ledLightColor_ = color;
509 }
510
SetLockScreenVisbleness(const NotificationConstant::VisiblenessType & visbleness)511 void Notification::SetLockScreenVisbleness(const NotificationConstant::VisiblenessType &visbleness)
512 {
513 lockscreenVisibleness_ = visbleness;
514 }
515
SetPostTime(const int64_t & time)516 void Notification::SetPostTime(const int64_t &time)
517 {
518 postTime_ = time;
519 }
520
SetSound(const Uri & sound)521 void Notification::SetSound(const Uri &sound)
522 {
523 sound_ = std::make_shared<Uri>(sound.ToString());
524 }
525
SetVibrationStyle(const std::vector<int64_t> & style)526 void Notification::SetVibrationStyle(const std::vector<int64_t> &style)
527 {
528 vibrationStyle_ = style;
529 }
530
SetRemindType(const NotificationConstant::RemindType & reminType)531 void Notification::SetRemindType(const NotificationConstant::RemindType &reminType)
532 {
533 remindType_ = reminType;
534 }
535
SetRemoveAllowed(bool removeAllowed)536 void Notification::SetRemoveAllowed(bool removeAllowed)
537 {
538 isRemoveAllowed_ = removeAllowed;
539 }
540
SetSourceType(NotificationConstant::SourceType sourceType)541 void Notification::SetSourceType(NotificationConstant::SourceType sourceType)
542 {
543 sourceType_ = sourceType;
544 }
545
Dump() const546 std::string Notification::Dump() const
547 {
548 std::string vibrationStyle = "";
549 for (const auto &style : vibrationStyle_) {
550 vibrationStyle += std::to_string(style);
551 vibrationStyle += ", ";
552 }
553 return "Notification{ "
554 "key = " + key_ +
555 ", ledLightColor = " + std::to_string(ledLightColor_) +
556 ", lockscreenVisbleness = " + std::to_string(static_cast<int32_t>(lockscreenVisibleness_)) +
557 ", remindType = " + std::to_string(static_cast<int32_t>(remindType_)) +
558 ", isRemoveAllowed = " + (isRemoveAllowed_ ? "true" : "false") +
559 ", sourceType = " + std::to_string(static_cast<int32_t>(sourceType_)) +
560 ", deviceId = " + deviceId_ +
561 ", request = " + (request_ == nullptr ? "nullptr" : request_->Dump()) +
562 ", postTime = " + std::to_string(postTime_) +
563 ", sound = " + (sound_ == nullptr ? "nullptr" : sound_->ToString()) +
564 ", vibrationStyle = [" + vibrationStyle + "]" +
565 ", updateTimer = " + std::to_string(updateTimerId_) +
566 ", finishTimer = " + std::to_string(finishTimerId_) +
567 ", archiveTimer = " + std::to_string(archiveTimerId_) +
568 " }";
569 }
570
GetUpdateTimer() const571 uint64_t Notification::GetUpdateTimer() const
572 {
573 return updateTimerId_;
574 }
575
SetUpdateTimer(uint64_t updateTimerId)576 void Notification::SetUpdateTimer(uint64_t updateTimerId)
577 {
578 updateTimerId_ = updateTimerId;
579 }
580
GetFinishTimer() const581 uint64_t Notification::GetFinishTimer() const
582 {
583 return finishTimerId_;
584 }
585
SetFinishTimer(uint64_t finishTimerId)586 void Notification::SetFinishTimer(uint64_t finishTimerId)
587 {
588 finishTimerId_ = finishTimerId;
589 }
590
SetArchiveTimer(uint64_t archiveTimerId)591 void Notification::SetArchiveTimer(uint64_t archiveTimerId)
592 {
593 archiveTimerId_ = archiveTimerId;
594 }
595
GetArchiveTimer() const596 uint64_t Notification::GetArchiveTimer() const
597 {
598 return archiveTimerId_;
599 }
600
SetAutoDeletedTimer(uint64_t autoDeletedTimerId)601 void Notification::SetAutoDeletedTimer(uint64_t autoDeletedTimerId)
602 {
603 autoDeletedTimerId_ = autoDeletedTimerId;
604 }
605
GetAutoDeletedTimer() const606 uint64_t Notification::GetAutoDeletedTimer() const
607 {
608 return autoDeletedTimerId_;
609 }
610 } // namespace Notification
611 } // namespace OHOS
612