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