• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetPostTime() const145 int64_t Notification::GetPostTime() const
146 {
147     return postTime_;
148 }
149 
GetSound() const150 Uri Notification::GetSound() const
151 {
152     if (enableSound_ && sound_ != nullptr) {
153         return *sound_;
154     }
155     return Uri("");
156 }
157 
GetUid() const158 int32_t Notification::GetUid() const
159 {
160     if (request_ == nullptr) {
161         return 0;
162     }
163     return request_->GetCreatorUid();
164 }
165 
GetPid() const166 pid_t Notification::GetPid() const
167 {
168     if (request_ == nullptr) {
169         return 0;
170     }
171     return request_->GetCreatorPid();
172 }
173 
IsUnremovable() const174 bool Notification::IsUnremovable() const
175 {
176     if (request_ == nullptr) {
177         return false;
178     }
179     return request_->IsUnremovable();
180 }
181 
GetVibrationStyle() const182 std::vector<int64_t> Notification::GetVibrationStyle() const
183 {
184     return vibrationStyle_;
185 }
186 
IsGroup() const187 bool Notification::IsGroup() const
188 {
189     if (request_ == nullptr) {
190         return false;
191     }
192     return !(request_->GetGroupName() == "");
193 }
194 
IsFloatingIcon() const195 bool Notification::IsFloatingIcon() const
196 {
197     if (request_ == nullptr) {
198         return false;
199     }
200     return request_->IsFloatingIcon();
201 }
202 
GetRemindType() const203 NotificationConstant::RemindType Notification::GetRemindType() const
204 {
205     return remindType_;
206 }
207 
IsRemoveAllowed() const208 bool Notification::IsRemoveAllowed() const
209 {
210     return isRemoveAllowed_;
211 }
212 
GetSourceType() const213 NotificationConstant::SourceType Notification::GetSourceType() const
214 {
215     return sourceType_;
216 }
217 
GetDeviceId() const218 std::string Notification::GetDeviceId() const
219 {
220     return deviceId_;
221 }
222 
GetUserId() const223 int32_t Notification::GetUserId() const
224 {
225     if (request_ == nullptr) {
226         return 0;
227     }
228     return request_->GetCreatorUserId();
229 }
230 
MarshallingBool(Parcel & parcel) const231 bool Notification::MarshallingBool(Parcel &parcel) const
232 {
233     if (!parcel.WriteBool(enableLight_)) {
234         ANS_LOGE("Can't write enableLight_");
235         return false;
236     }
237 
238     if (!parcel.WriteBool(enableSound_)) {
239         ANS_LOGE("Can't write enableSound_");
240         return false;
241     }
242 
243     if (!parcel.WriteBool(enableVibration_)) {
244         ANS_LOGE("Can't write enableVibration_");
245         return false;
246     }
247 
248     if (!parcel.WriteBool(isRemoveAllowed_)) {
249         ANS_LOGE("Can't write isRemoveAllowed");
250         return false;
251     }
252 
253     return true;
254 }
255 
MarshallingString(Parcel & parcel) const256 bool Notification::MarshallingString(Parcel &parcel) const
257 {
258     if (!parcel.WriteString(key_)) {
259         ANS_LOGE("Can't write key");
260         return false;
261     }
262 
263     if (enableSound_ && sound_ != nullptr) {
264         if (!parcel.WriteString(sound_->ToString())) {
265             ANS_LOGE("Can't write sound");
266             return false;
267         }
268     }
269 
270     if (!parcel.WriteString(deviceId_)) {
271         ANS_LOGE("Can't write deviceId");
272         return false;
273     }
274 
275     return true;
276 }
277 
MarshallingInt32(Parcel & parcel) const278 bool Notification::MarshallingInt32(Parcel &parcel) const
279 {
280     if (!parcel.WriteInt32(ledLightColor_)) {
281         ANS_LOGE("Can't write ledLigthColor");
282         return false;
283     }
284 
285     if (!parcel.WriteInt32(static_cast<int32_t>(lockscreenVisibleness_))) {
286         ANS_LOGE("Can't write visbleness");
287         return false;
288     }
289 
290     if (!parcel.WriteInt32(static_cast<int32_t>(remindType_))) {
291         ANS_LOGE("Can't write remindType");
292         return false;
293     }
294 
295     if (!parcel.WriteInt32(static_cast<int32_t>(sourceType_))) {
296         ANS_LOGE("Can't write sourceType");
297         return false;
298     }
299 
300     return true;
301 }
302 
MarshallingInt64(Parcel & parcel) const303 bool Notification::MarshallingInt64(Parcel &parcel) const
304 {
305     if (!parcel.WriteInt64(postTime_)) {
306         ANS_LOGE("Can't write postTime");
307         return false;
308     }
309 
310     if (!parcel.WriteInt64Vector(vibrationStyle_)) {
311         ANS_LOGE("Can't write vibrationStyle");
312         return false;
313     }
314 
315     return true;
316 }
317 
MarshallingUint64(Parcel & parcel) const318 bool Notification::MarshallingUint64(Parcel &parcel) const
319 {
320     if (!parcel.WriteUint64(updateTimerId_)) {
321         ANS_LOGE("Can't write update timer id.");
322         return false;
323     }
324 
325     if (!parcel.WriteUint64(finishTimerId_)) {
326         ANS_LOGE("Can't write finish timer id.");
327         return false;
328     }
329 
330     if (!parcel.WriteUint64(archiveTimerId_)) {
331         ANS_LOGE("Can't write archive timer id.");
332         return false;
333     }
334 
335     return true;
336 }
337 
MarshallingParcelable(Parcel & parcel) const338 bool Notification::MarshallingParcelable(Parcel &parcel) const
339 {
340     if (!parcel.WriteStrongParcelable(request_)) {
341         ANS_LOGE("Can't write request");
342         return false;
343     }
344 
345     return true;
346 }
347 
Marshalling(Parcel & parcel) const348 bool Notification::Marshalling(Parcel &parcel) const
349 {
350     if (!MarshallingBool(parcel)) {
351         return false;
352     }
353     if (!MarshallingString(parcel)) {
354         return false;
355     }
356     if (!MarshallingInt32(parcel)) {
357         return false;
358     }
359     if (!MarshallingInt64(parcel)) {
360         return false;
361     }
362     if (!MarshallingUint64(parcel)) {
363         return false;
364     }
365     if (!MarshallingParcelable(parcel)) {
366         return false;
367     }
368 
369     return true;
370 }
371 
ReadFromParcelBool(Parcel & parcel)372 void Notification::ReadFromParcelBool(Parcel &parcel)
373 {
374     // Read enableLight_
375     enableLight_ = parcel.ReadBool();
376 
377     // Read enableSound_
378     enableSound_ = parcel.ReadBool();
379 
380     // Read enableVibration_
381     enableVibration_ = parcel.ReadBool();
382 
383     // Read isRemoveAllowed_
384     isRemoveAllowed_ = parcel.ReadBool();
385 }
386 
ReadFromParcelString(Parcel & parcel)387 void Notification::ReadFromParcelString(Parcel &parcel)
388 {
389     // Read key_
390     key_ = parcel.ReadString();
391 
392     // Read sound_
393     if (enableSound_) {
394         sound_ = std::make_shared<Uri>(parcel.ReadString());
395     }
396 
397     // Read deviceId_
398     deviceId_ = parcel.ReadString();
399 }
400 
ReadFromParcelInt32(Parcel & parcel)401 void Notification::ReadFromParcelInt32(Parcel &parcel)
402 {
403     // Read ledLightColor_
404     ledLightColor_ = parcel.ReadInt32();
405 
406     // Read lockscreenVisibleness_
407     lockscreenVisibleness_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
408 
409     // Read remindType_
410     remindType_ = static_cast<NotificationConstant::RemindType>(parcel.ReadInt32());
411 
412     // Read sourceType_
413     sourceType_ = static_cast<NotificationConstant::SourceType>(parcel.ReadInt32());
414 }
415 
ReadFromParcelInt64(Parcel & parcel)416 void Notification::ReadFromParcelInt64(Parcel &parcel)
417 {
418     // Read postTime_
419     postTime_ = parcel.ReadInt64();
420 
421     // Read vibrationStyle_
422     parcel.ReadInt64Vector(&vibrationStyle_);
423 }
424 
ReadFromParcelUint64(Parcel & parcel)425 void Notification::ReadFromParcelUint64(Parcel &parcel)
426 {
427     updateTimerId_ = parcel.ReadUint64();
428     finishTimerId_ = parcel.ReadUint64();
429     archiveTimerId_ = parcel.ReadUint64();
430 }
431 
ReadFromParcelParcelable(Parcel & parcel)432 void Notification::ReadFromParcelParcelable(Parcel &parcel)
433 {
434     // Read request_
435     request_ = parcel.ReadStrongParcelable<NotificationRequest>();
436 }
437 
ReadFromParcel(Parcel & parcel)438 bool Notification::ReadFromParcel(Parcel &parcel)
439 {
440     ReadFromParcelBool(parcel);
441     ReadFromParcelString(parcel);
442     ReadFromParcelInt32(parcel);
443     ReadFromParcelInt64(parcel);
444     ReadFromParcelUint64(parcel);
445     ReadFromParcelParcelable(parcel);
446 
447     return true;
448 }
449 
Unmarshalling(Parcel & parcel)450 Notification *Notification::Unmarshalling(Parcel &parcel)
451 {
452     Notification *n = new (std::nothrow) Notification();
453     if (n && !n->ReadFromParcel(parcel)) {
454         ANS_LOGE("Read from parcel error");
455         delete n;
456         n = nullptr;
457     }
458     return n;
459 }
460 
SetEnableSound(const bool & enable)461 void Notification::SetEnableSound(const bool &enable)
462 {
463     enableSound_ = enable;
464 }
465 
SetEnableLight(const bool & enable)466 void Notification::SetEnableLight(const bool &enable)
467 {
468     enableLight_ = enable;
469 }
470 
SetEnableVibration(const bool & enable)471 void Notification::SetEnableVibration(const bool &enable)
472 {
473     enableVibration_ = enable;
474 }
475 
SetLedLightColor(const int32_t & color)476 void Notification::SetLedLightColor(const int32_t &color)
477 {
478     ledLightColor_ = color;
479 }
480 
SetLockScreenVisbleness(const NotificationConstant::VisiblenessType & visbleness)481 void Notification::SetLockScreenVisbleness(const NotificationConstant::VisiblenessType &visbleness)
482 {
483     lockscreenVisibleness_ = visbleness;
484 }
485 
SetPostTime(const int64_t & time)486 void Notification::SetPostTime(const int64_t &time)
487 {
488     postTime_ = time;
489 }
490 
SetSound(const Uri & sound)491 void Notification::SetSound(const Uri &sound)
492 {
493     sound_ = std::make_shared<Uri>(sound.ToString());
494 }
495 
SetVibrationStyle(const std::vector<int64_t> & style)496 void Notification::SetVibrationStyle(const std::vector<int64_t> &style)
497 {
498     vibrationStyle_ = style;
499 }
500 
SetRemindType(const NotificationConstant::RemindType & reminType)501 void Notification::SetRemindType(const NotificationConstant::RemindType &reminType)
502 {
503     remindType_ = reminType;
504 }
505 
SetRemoveAllowed(bool removeAllowed)506 void Notification::SetRemoveAllowed(bool removeAllowed)
507 {
508     isRemoveAllowed_ = removeAllowed;
509 }
510 
SetSourceType(NotificationConstant::SourceType sourceType)511 void Notification::SetSourceType(NotificationConstant::SourceType sourceType)
512 {
513     sourceType_ = sourceType;
514 }
515 
Dump() const516 std::string Notification::Dump() const
517 {
518     std::string vibrationStyle = "";
519     for (const auto &style : vibrationStyle_) {
520         vibrationStyle += std::to_string(style);
521         vibrationStyle += ", ";
522     }
523     return "Notification{ "
524             "key = " + key_ +
525             ", ledLightColor = " + std::to_string(ledLightColor_) +
526             ", lockscreenVisbleness = " + std::to_string(static_cast<int32_t>(lockscreenVisibleness_)) +
527             ", remindType = " + std::to_string(static_cast<int32_t>(remindType_)) +
528             ", isRemoveAllowed = " + (isRemoveAllowed_ ? "true" : "false") +
529             ", sourceType = " + std::to_string(static_cast<int32_t>(sourceType_)) +
530             ", deviceId = " + deviceId_ +
531             ", request = " + (request_ == nullptr ? "nullptr" : request_->Dump()) +
532             ", postTime = " + std::to_string(postTime_) +
533             ", sound = " + (sound_ == nullptr ? "nullptr" : sound_->ToString()) +
534             ", vibrationStyle = [" + vibrationStyle + "]" +
535             ", updateTimer = " + std::to_string(updateTimerId_) +
536             ", finishTimer = " + std::to_string(finishTimerId_) +
537             ", archiveTimer = " + std::to_string(archiveTimerId_) +
538             " }";
539 }
540 
GetUpdateTimer() const541 uint64_t Notification::GetUpdateTimer() const
542 {
543     return updateTimerId_;
544 }
545 
SetUpdateTimer(uint64_t updateTimerId)546 void Notification::SetUpdateTimer(uint64_t updateTimerId)
547 {
548     updateTimerId_ = updateTimerId;
549 }
550 
GetFinishTimer() const551 uint64_t Notification::GetFinishTimer() const
552 {
553     return finishTimerId_;
554 }
555 
SetFinishTimer(uint64_t finishTimerId)556 void Notification::SetFinishTimer(uint64_t finishTimerId)
557 {
558     finishTimerId_ = finishTimerId;
559 }
560 
SetArchiveTimer(uint64_t archiveTimerId)561 void Notification::SetArchiveTimer(uint64_t archiveTimerId)
562 {
563     archiveTimerId_ = archiveTimerId;
564 }
565 
GetArchiveTimer() const566 uint64_t Notification::GetArchiveTimer() const
567 {
568     return archiveTimerId_;
569 }
570 }  // namespace Notification
571 }  // namespace OHOS
572