• 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     key_ = GenerateNotificationKey("", GetUserId(), GetUid(), GetLabel(), GetId());
33 }
34 
Notification(const std::string & deviceId,const sptr<NotificationRequest> & request)35 Notification::Notification(const std::string &deviceId, const sptr<NotificationRequest> &request)
36 {
37     deviceId_ = deviceId;
38     request_ = request;
39     key_ = GenerateNotificationKey(deviceId, GetUserId(), GetUid(), GetLabel(), GetId());
40 }
41 
Notification(const Notification & other)42 Notification::Notification(const Notification &other)
43 {
44     enableSound_ = other.enableSound_;
45     enableLight_ = other.enableLight_;
46     enableVibration_ = other.enableVibration_;
47     key_ = other.key_;
48     ledLightColor_ = other.ledLightColor_;
49     lockscreenVisibleness_ = other.lockscreenVisibleness_;
50     remindType_ = other.remindType_;
51     request_ = other.request_;
52     postTime_ = other.postTime_;
53     sound_ = other.sound_;
54     vibrationStyle_ = other.vibrationStyle_;
55     isRemoveAllowed_ = other.isRemoveAllowed_;
56     sourceType_ = other.sourceType_;
57     deviceId_ = other.deviceId_;
58 }
59 
~Notification()60 Notification::~Notification()
61 {}
62 
EnableLight() const63 bool Notification::EnableLight() const
64 {
65     return enableLight_;
66 }
67 
EnableSound() const68 bool Notification::EnableSound() const
69 {
70     return enableSound_;
71 }
72 
EnableVibrate() const73 bool Notification::EnableVibrate() const
74 {
75     return enableVibration_;
76 }
77 
GetBundleName() const78 std::string Notification::GetBundleName() const
79 {
80     if (request_ == nullptr) {
81         return "";
82     }
83     return request_->GetOwnerBundleName();
84 }
85 
GetCreateBundle() const86 std::string Notification::GetCreateBundle() const
87 {
88     if (request_ == nullptr) {
89         return "";
90     }
91     return request_->GetCreatorBundleName();
92 }
93 
GetLabel() const94 std::string Notification::GetLabel() const
95 {
96     if (request_ == nullptr) {
97         return "";
98     }
99     return request_->GetLabel();
100 }
101 
GetLedLightColor() const102 int32_t Notification::GetLedLightColor() const
103 {
104     return ledLightColor_;
105 }
106 
GetLockscreenVisibleness() const107 NotificationConstant::VisiblenessType Notification::GetLockscreenVisibleness() const
108 {
109     return lockscreenVisibleness_;
110 }
111 
GetGroup() const112 std::string Notification::GetGroup() const
113 {
114     if (request_ == nullptr) {
115         return "";
116     }
117     return request_->GetGroupName();
118 }
119 
GetId() const120 int32_t Notification::GetId() const
121 {
122     if (request_ == nullptr) {
123         return -1;
124     }
125     return request_->GetNotificationId();
126 }
127 
GetKey() const128 std::string Notification::GetKey() const
129 {
130     return key_;
131 }
132 
GetNotificationRequest() const133 NotificationRequest Notification::GetNotificationRequest() const
134 {
135     return *request_;
136 }
137 
GetPostTime() const138 int64_t Notification::GetPostTime() const
139 {
140     return postTime_;
141 }
142 
GetSound() const143 Uri Notification::GetSound() const
144 {
145     if (enableSound_ && sound_ != nullptr) {
146         return *sound_;
147     }
148     return Uri("");
149 }
150 
GetUid() const151 int32_t Notification::GetUid() const
152 {
153     if (request_ == nullptr) {
154         return 0;
155     }
156     return request_->GetCreatorUid();
157 }
158 
GetPid() const159 pid_t Notification::GetPid() const
160 {
161     if (request_ == nullptr) {
162         return 0;
163     }
164     return request_->GetCreatorPid();
165 }
166 
IsUnremovable() const167 bool Notification::IsUnremovable() const
168 {
169     if (request_ == nullptr) {
170         return false;
171     }
172     return request_->IsUnremovable();
173 }
174 
GetVibrationStyle() const175 std::vector<int64_t> Notification::GetVibrationStyle() const
176 {
177     return vibrationStyle_;
178 }
179 
IsGroup() const180 bool Notification::IsGroup() const
181 {
182     if (request_ == nullptr) {
183         return false;
184     }
185     return !(request_->GetGroupName() == "");
186 }
187 
IsFloatingIcon() const188 bool Notification::IsFloatingIcon() const
189 {
190     if (request_ == nullptr) {
191         return false;
192     }
193     return request_->IsFloatingIcon();
194 }
195 
GetRemindType() const196 NotificationConstant::RemindType Notification::GetRemindType() const
197 {
198     return remindType_;
199 }
200 
IsRemoveAllowed() const201 bool Notification::IsRemoveAllowed() const
202 {
203     return isRemoveAllowed_;
204 }
205 
GetSourceType() const206 NotificationConstant::SourceType Notification::GetSourceType() const
207 {
208     return sourceType_;
209 }
210 
GetDeviceId() const211 std::string Notification::GetDeviceId() const
212 {
213     return deviceId_;
214 }
215 
GetUserId() const216 int32_t Notification::GetUserId() const
217 {
218     if (request_ == nullptr) {
219         return 0;
220     }
221     return request_->GetCreatorUserId();
222 }
223 
MarshallingBool(Parcel & parcel) const224 bool Notification::MarshallingBool(Parcel &parcel) const
225 {
226     if (!parcel.WriteBool(enableLight_)) {
227         ANS_LOGE("Can't write enableLight_");
228         return false;
229     }
230 
231     if (!parcel.WriteBool(enableSound_)) {
232         ANS_LOGE("Can't write enableSound_");
233         return false;
234     }
235 
236     if (!parcel.WriteBool(enableVibration_)) {
237         ANS_LOGE("Can't write enableVibration_");
238         return false;
239     }
240 
241     if (!parcel.WriteBool(isRemoveAllowed_)) {
242         ANS_LOGE("Can't write isRemoveAllowed");
243         return false;
244     }
245 
246     return true;
247 }
248 
MarshallingString(Parcel & parcel) const249 bool Notification::MarshallingString(Parcel &parcel) const
250 {
251     if (!parcel.WriteString(key_)) {
252         ANS_LOGE("Can't write key");
253         return false;
254     }
255 
256     if (enableSound_ && sound_ != nullptr) {
257         if (!parcel.WriteString(sound_->ToString())) {
258             ANS_LOGE("Can't write sound");
259             return false;
260         }
261     }
262 
263     if (!parcel.WriteString(deviceId_)) {
264         ANS_LOGE("Can't write deviceId");
265         return false;
266     }
267 
268     return true;
269 }
270 
MarshallingInt32(Parcel & parcel) const271 bool Notification::MarshallingInt32(Parcel &parcel) const
272 {
273     if (!parcel.WriteInt32(ledLightColor_)) {
274         ANS_LOGE("Can't write ledLigthColor");
275         return false;
276     }
277 
278     if (!parcel.WriteInt32(static_cast<int32_t>(lockscreenVisibleness_))) {
279         ANS_LOGE("Can't write visbleness");
280         return false;
281     }
282 
283     if (!parcel.WriteInt32(static_cast<int32_t>(remindType_))) {
284         ANS_LOGE("Can't write remindType");
285         return false;
286     }
287 
288     if (!parcel.WriteInt32(static_cast<int32_t>(sourceType_))) {
289         ANS_LOGE("Can't write sourceType");
290         return false;
291     }
292 
293     return true;
294 }
295 
MarshallingInt64(Parcel & parcel) const296 bool Notification::MarshallingInt64(Parcel &parcel) const
297 {
298     if (!parcel.WriteInt64(postTime_)) {
299         ANS_LOGE("Can't write postTime");
300         return false;
301     }
302 
303     if (!parcel.WriteInt64Vector(vibrationStyle_)) {
304         ANS_LOGE("Can't write vibrationStyle");
305         return false;
306     }
307 
308     return true;
309 }
310 
MarshallingParcelable(Parcel & parcel) const311 bool Notification::MarshallingParcelable(Parcel &parcel) const
312 {
313     if (!parcel.WriteStrongParcelable(request_)) {
314         ANS_LOGE("Can't write request");
315         return false;
316     }
317 
318     return true;
319 }
320 
Marshalling(Parcel & parcel) const321 bool Notification::Marshalling(Parcel &parcel) const
322 {
323     if (!MarshallingBool(parcel)) {
324         return false;
325     }
326     if (!MarshallingString(parcel)) {
327         return false;
328     }
329     if (!MarshallingInt32(parcel)) {
330         return false;
331     }
332     if (!MarshallingInt64(parcel)) {
333         return false;
334     }
335     if (!MarshallingParcelable(parcel)) {
336         return false;
337     }
338 
339     return true;
340 }
341 
ReadFromParcelBool(Parcel & parcel)342 void Notification::ReadFromParcelBool(Parcel &parcel)
343 {
344     // Read enableLight_
345     enableLight_ = parcel.ReadBool();
346 
347     // Read enableSound_
348     enableSound_ = parcel.ReadBool();
349 
350     // Read enableVibration_
351     enableVibration_ = parcel.ReadBool();
352 
353     // Read isRemoveAllowed_
354     isRemoveAllowed_ = parcel.ReadBool();
355 }
356 
ReadFromParcelString(Parcel & parcel)357 void Notification::ReadFromParcelString(Parcel &parcel)
358 {
359     // Read key_
360     key_ = parcel.ReadString();
361 
362     // Read sound_
363     if (enableSound_) {
364         sound_ = std::make_shared<Uri>(parcel.ReadString());
365     }
366 
367     // Read deviceId_
368     deviceId_ = parcel.ReadString();
369 }
370 
ReadFromParcelInt32(Parcel & parcel)371 void Notification::ReadFromParcelInt32(Parcel &parcel)
372 {
373     // Read ledLightColor_
374     ledLightColor_ = parcel.ReadInt32();
375 
376     // Read lockscreenVisibleness_
377     lockscreenVisibleness_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
378 
379     // Read remindType_
380     remindType_ = static_cast<NotificationConstant::RemindType>(parcel.ReadInt32());
381 
382     // Read sourceType_
383     sourceType_ = static_cast<NotificationConstant::SourceType>(parcel.ReadInt32());
384 }
385 
ReadFromParcelInt64(Parcel & parcel)386 void Notification::ReadFromParcelInt64(Parcel &parcel)
387 {
388     // Read postTime_
389     postTime_ = parcel.ReadInt64();
390 
391     // Read vibrationStyle_
392     parcel.ReadInt64Vector(&vibrationStyle_);
393 }
394 
ReadFromParcelParcelable(Parcel & parcel)395 void Notification::ReadFromParcelParcelable(Parcel &parcel)
396 {
397     // Read request_
398     request_ = parcel.ReadStrongParcelable<NotificationRequest>();
399 }
400 
ReadFromParcel(Parcel & parcel)401 bool Notification::ReadFromParcel(Parcel &parcel)
402 {
403     ReadFromParcelBool(parcel);
404     ReadFromParcelString(parcel);
405     ReadFromParcelInt32(parcel);
406     ReadFromParcelInt64(parcel);
407     ReadFromParcelParcelable(parcel);
408 
409     return true;
410 }
411 
Unmarshalling(Parcel & parcel)412 Notification *Notification::Unmarshalling(Parcel &parcel)
413 {
414     Notification *n = new (std::nothrow) Notification();
415     if (n && !n->ReadFromParcel(parcel)) {
416         ANS_LOGE("Read from parcel error");
417         delete n;
418         n = nullptr;
419     }
420     return n;
421 }
422 
SetEnableSound(const bool & enable)423 void Notification::SetEnableSound(const bool &enable)
424 {
425     enableSound_ = enable;
426 }
427 
SetEnableLight(const bool & enable)428 void Notification::SetEnableLight(const bool &enable)
429 {
430     enableLight_ = enable;
431 }
432 
SetEnableVibration(const bool & enable)433 void Notification::SetEnableVibration(const bool &enable)
434 {
435     enableVibration_ = enable;
436 }
437 
SetLedLightColor(const int32_t & color)438 void Notification::SetLedLightColor(const int32_t &color)
439 {
440     ledLightColor_ = color;
441 }
442 
SetLockScreenVisbleness(const NotificationConstant::VisiblenessType & visbleness)443 void Notification::SetLockScreenVisbleness(const NotificationConstant::VisiblenessType &visbleness)
444 {
445     lockscreenVisibleness_ = visbleness;
446 }
447 
SetPostTime(const int64_t & time)448 void Notification::SetPostTime(const int64_t &time)
449 {
450     postTime_ = time;
451 }
452 
SetSound(const Uri & sound)453 void Notification::SetSound(const Uri &sound)
454 {
455     sound_ = std::make_shared<Uri>(sound.ToString());
456 }
457 
SetVibrationStyle(const std::vector<int64_t> & style)458 void Notification::SetVibrationStyle(const std::vector<int64_t> &style)
459 {
460     vibrationStyle_ = style;
461 }
462 
SetRemindType(const NotificationConstant::RemindType & reminType)463 void Notification::SetRemindType(const NotificationConstant::RemindType &reminType)
464 {
465     remindType_ = reminType;
466 }
467 
GenerateNotificationKey(const std::string & deviceId,int32_t userId,int32_t uid,const std::string & label,int32_t id)468 std::string Notification::GenerateNotificationKey(
469     const std::string &deviceId, int32_t userId, int32_t uid, const std::string &label, int32_t id)
470 {
471     const char *keySpliter = "_";
472 
473     std::stringstream stream;
474     stream << deviceId << keySpliter << userId << keySpliter << uid << keySpliter << label << keySpliter << id;
475 
476     return stream.str();
477 }
478 
SetRemoveAllowed(bool removeAllowed)479 void Notification::SetRemoveAllowed(bool removeAllowed)
480 {
481     isRemoveAllowed_ = removeAllowed;
482 }
483 
SetSourceType(NotificationConstant::SourceType sourceType)484 void Notification::SetSourceType(NotificationConstant::SourceType sourceType)
485 {
486     sourceType_ = sourceType;
487 }
488 
Dump() const489 std::string Notification::Dump() const
490 {
491     std::string vibrationStyle = "";
492     for (const auto &style : vibrationStyle_) {
493         vibrationStyle += std::to_string(style);
494         vibrationStyle += ", ";
495     }
496     return "Notification{ "
497             "key = " + key_ +
498             ", ledLightColor = " + std::to_string(ledLightColor_) +
499             ", lockscreenVisbleness = " + std::to_string(static_cast<int32_t>(lockscreenVisibleness_)) +
500             ", remindType = " + std::to_string(static_cast<int32_t>(remindType_)) +
501             ", isRemoveAllowed = " + (isRemoveAllowed_ ? "true" : "false") +
502             ", sourceType = " + std::to_string(static_cast<int32_t>(sourceType_)) +
503             ", deviceId = " + deviceId_ +
504             ", request = " + (request_ == nullptr ? "nullptr" : request_->Dump()) +
505             ", postTime = " + std::to_string(postTime_) +
506             ", sound = " + (sound_ == nullptr ? "nullptr" : sound_->ToString()) +
507             ", vibrationStyle = [" + vibrationStyle + "]" +
508             " }";
509 }
510 }  // namespace Notification
511 }  // namespace OHOS