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_slot.h"
17 #include "ans_const_define.h"
18 #include "ans_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Notification {
22 std::map<std::string, NotificationConstant::SlotType> NotificationSlot::convertStrToSlotType_ = {
23 {SOCIAL_COMMUNICATION, NotificationConstant::SlotType::SOCIAL_COMMUNICATION},
24 {SERVICE_REMINDER, NotificationConstant::SlotType::SERVICE_REMINDER},
25 {CONTENT_INFORMATION, NotificationConstant::SlotType::CONTENT_INFORMATION},
26 {OTHER, NotificationConstant::SlotType::OTHER},
27 {LIVE_VIEW, NotificationConstant::SlotType::LIVE_VIEW},
28 {CUSTOM_SERVICE, NotificationConstant::SlotType::CUSTOMER_SERVICE},
29 {EMERGENCY_INFORMATION, NotificationConstant::SlotType::EMERGENCY_INFORMATION}
30 };
31
32 const int32_t MAX_TEXT_LENGTH = 1000;
33 const uint32_t SOUND_OPNE = 1 << 0;
34 const uint32_t LOCKSCREEN_OPNE = 1 << 1;
35 const uint32_t BANNER_OPNE = 1 << 2;
36 const uint32_t LIGHTSCREEN_OPNE = 1 << 3;
37 const uint32_t VIBRATION_OPNE = 1 << 4;
38 const uint32_t STATUSBAR_ICON_OPNE = 1 << 5;
39
NotificationSlot(NotificationConstant::SlotType type)40 NotificationSlot::NotificationSlot(NotificationConstant::SlotType type) : sound_("")
41 {
42 SetType(type);
43 }
44
~NotificationSlot()45 NotificationSlot::~NotificationSlot()
46 {}
47
CanEnableLight() const48 bool NotificationSlot::CanEnableLight() const
49 {
50 return isLightEnabled_;
51 }
52
SetEnableLight(bool isLightEnabled)53 void NotificationSlot::SetEnableLight(bool isLightEnabled)
54 {
55 isLightEnabled_ = isLightEnabled;
56 }
57
CanVibrate() const58 bool NotificationSlot::CanVibrate() const
59 {
60 return isVibrationEnabled_;
61 }
62
SetEnableVibration(bool vibration)63 void NotificationSlot::SetEnableVibration(bool vibration)
64 {
65 isVibrationEnabled_ = vibration;
66 }
67
GetDescription() const68 std::string NotificationSlot::GetDescription() const
69 {
70 return description_;
71 }
72
SetDescription(const std::string & description)73 void NotificationSlot::SetDescription(const std::string &description)
74 {
75 description_ = TruncateString(description);
76 }
77
GetId() const78 std::string NotificationSlot::GetId() const
79 {
80 return id_;
81 }
82
GetLedLightColor() const83 int32_t NotificationSlot::GetLedLightColor() const
84 {
85 return lightColor_;
86 }
87
SetLedLightColor(int32_t color)88 void NotificationSlot::SetLedLightColor(int32_t color)
89 {
90 lightColor_ = color;
91 }
92
GetLevel() const93 NotificationSlot::NotificationLevel NotificationSlot::GetLevel() const
94 {
95 return level_;
96 }
97
SetLevel(NotificationLevel level)98 void NotificationSlot::SetLevel(NotificationLevel level)
99 {
100 level_ = level;
101 }
102
GetType() const103 NotificationConstant::SlotType NotificationSlot::GetType() const
104 {
105 return type_;
106 }
107
GetAuthorizedStatus() const108 int32_t NotificationSlot::GetAuthorizedStatus() const
109 {
110 return authorizedStatus_;
111 }
112
SetAuthorizedStatus(int32_t status)113 void NotificationSlot::SetAuthorizedStatus(int32_t status)
114 {
115 authorizedStatus_ = status;
116 }
117
GetAuthHintCnt() const118 int32_t NotificationSlot::GetAuthHintCnt() const
119 {
120 return authHintCnt_;
121 }
122
AddAuthHintCnt()123 void NotificationSlot::AddAuthHintCnt()
124 {
125 authHintCnt_++;
126 }
127
SetAuthHintCnt(int32_t count)128 void NotificationSlot::SetAuthHintCnt(int32_t count)
129 {
130 authHintCnt_ = count;
131 }
132
133
SetType(NotificationConstant::SlotType type)134 void NotificationSlot::SetType(NotificationConstant::SlotType type)
135 {
136 type_ = NotificationConstant::SlotType::CUSTOM;
137 switch (type) {
138 case NotificationConstant::SlotType::SOCIAL_COMMUNICATION:
139 id_ = "SOCIAL_COMMUNICATION";
140 SetName("SOCIAL_COMMUNICATION");
141 SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
142 SetSound(DEFAULT_NOTIFICATION_SOUND);
143 SetVibrationStyle(DEFAULT_NOTIFICATION_VIBRATION);
144 SetLevel(LEVEL_HIGH);
145 break;
146 case NotificationConstant::SlotType::SERVICE_REMINDER:
147 id_ = "SERVICE_REMINDER";
148 SetName("SERVICE_REMINDER");
149 SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
150 SetSound(DEFAULT_NOTIFICATION_SOUND);
151 SetVibrationStyle(DEFAULT_NOTIFICATION_VIBRATION);
152 SetLevel(LEVEL_DEFAULT);
153 break;
154 case NotificationConstant::SlotType::CONTENT_INFORMATION:
155 id_ = "CONTENT_INFORMATION";
156 SetName("CONTENT_INFORMATION");
157 SetLockscreenVisibleness(NotificationConstant::VisiblenessType::SECRET);
158 SetEnableVibration(false);
159 SetLevel(LEVEL_MIN);
160 break;
161 case NotificationConstant::SlotType::LIVE_VIEW:
162 id_ = "LIVE_VIEW";
163 SetName("LIVE_VIEW");
164 SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
165 SetSound(DEFAULT_NOTIFICATION_SOUND);
166 SetVibrationStyle(DEFAULT_NOTIFICATION_VIBRATION);
167 SetLevel(LEVEL_DEFAULT);
168 SetForceControl(true);
169 break;
170 case NotificationConstant::SlotType::CUSTOMER_SERVICE:
171 id_ = "CUSTOMER_SERVICE";
172 SetName("CUSTOMER_SERVICE");
173 SetLockscreenVisibleness(NotificationConstant::VisiblenessType::SECRET);
174 SetSound(DEFAULT_NOTIFICATION_SOUND);
175 SetEnableVibration(false);
176 SetLevel(LEVEL_LOW);
177 break;
178 case NotificationConstant::SlotType::EMERGENCY_INFORMATION:
179 id_ = "EMERGENCY_INFORMATION";
180 SetName("EMERGENCY_INFORMATION");
181 SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
182 SetSound(DEFAULT_NOTIFICATION_SOUND);
183 SetVibrationStyle(DEFAULT_NOTIFICATION_VIBRATION);
184 SetLevel(LEVEL_HIGH);
185 break;
186 case NotificationConstant::SlotType::OTHER:
187 id_ = "OTHER";
188 SetName("OTHER");
189 SetLockscreenVisibleness(NotificationConstant::VisiblenessType::SECRET);
190 SetEnableVibration(false);
191 SetLevel(LEVEL_MIN);
192 break;
193 default:
194 break;
195 }
196 type_ = type;
197 }
198
GetLockScreenVisibleness() const199 NotificationConstant::VisiblenessType NotificationSlot::GetLockScreenVisibleness() const
200 {
201 return lockScreenVisibleness_;
202 }
203
SetLockscreenVisibleness(NotificationConstant::VisiblenessType visibleness)204 void NotificationSlot::SetLockscreenVisibleness(NotificationConstant::VisiblenessType visibleness)
205 {
206 lockScreenVisibleness_ = visibleness;
207 }
208
GetName() const209 std::string NotificationSlot::GetName() const
210 {
211 return name_;
212 }
213
SetName(const std::string & name)214 void NotificationSlot::SetName(const std::string &name)
215 {
216 name_ = TruncateString(name);
217 }
218
GetSound() const219 Uri NotificationSlot::GetSound() const
220 {
221 return sound_;
222 }
223
SetSound(const Uri & sound)224 void NotificationSlot::SetSound(const Uri &sound)
225 {
226 sound_ = sound;
227 }
228
GetVibrationStyle() const229 std::vector<int64_t> NotificationSlot::GetVibrationStyle() const
230 {
231 return vibrationValues_;
232 }
233
SetVibrationStyle(const std::vector<int64_t> & vibrationValues)234 void NotificationSlot::SetVibrationStyle(const std::vector<int64_t> &vibrationValues)
235 {
236 isVibrationEnabled_ = (vibrationValues.size() > 0);
237 vibrationValues_ = vibrationValues;
238 }
239
IsEnableBypassDnd() const240 bool NotificationSlot::IsEnableBypassDnd() const
241 {
242 return isBypassDnd_;
243 }
244
EnableBypassDnd(bool isBypassDnd)245 void NotificationSlot::EnableBypassDnd(bool isBypassDnd)
246 {
247 isBypassDnd_ = isBypassDnd;
248 }
249
IsShowBadge() const250 bool NotificationSlot::IsShowBadge() const
251 {
252 return isShowBadge_;
253 }
254
EnableBadge(bool isShowBadge)255 void NotificationSlot::EnableBadge(bool isShowBadge)
256 {
257 isShowBadge_ = isShowBadge;
258 }
259
SetEnable(bool enabled)260 void NotificationSlot::SetEnable(bool enabled)
261 {
262 enabled_ = enabled;
263 }
264
GetEnable() const265 bool NotificationSlot::GetEnable() const
266 {
267 return enabled_;
268 }
269
SetSlotFlags(uint32_t slotFlags)270 void NotificationSlot::SetSlotFlags(uint32_t slotFlags)
271 {
272 slotFlags_ = slotFlags;
273 }
274
GetSlotFlags() const275 uint32_t NotificationSlot::GetSlotFlags() const
276 {
277 return slotFlags_;
278 }
279
SetForceControl(bool isForceControl)280 void NotificationSlot::SetForceControl(bool isForceControl)
281 {
282 isForceControl_ = isForceControl;
283 }
284
GetForceControl() const285 bool NotificationSlot::GetForceControl() const
286 {
287 return isForceControl_;
288 }
289
SetReminderMode(uint32_t reminderMode)290 void NotificationSlot::SetReminderMode(uint32_t reminderMode)
291 {
292 reminderMode_ = reminderMode;
293 }
294
GetReminderMode() const295 uint32_t NotificationSlot::GetReminderMode() const
296 {
297 if (reminderMode_ != INVALID_REMINDER_MODE) {
298 return reminderMode_;
299 }
300 return GetDefaultReminderMode();
301 }
302
Dump() const303 std::string NotificationSlot::Dump() const
304 {
305 return "NotificationSlot{ "
306 "id = " + id_ +
307 ", name = " + name_ +
308 ", description = " + description_ +
309 ", type = " + std::to_string(static_cast<int32_t>(type_)) +
310 ", level = " + std::to_string(static_cast<int32_t>(level_)) +
311 ", isBypassDnd = " + (isBypassDnd_ ? "true" : "false") +
312 ", visibleness = " + std::to_string(static_cast<int32_t>(lockScreenVisibleness_)) +
313 ", sound = " + sound_.ToString() +
314 ", isLightEnabled = " + (isLightEnabled_ ? "true" : "false") +
315 ", lightColor = " + std::to_string(lightColor_) +
316 ", isVibrate = " + (isVibrationEnabled_ ? "true" : "false") +
317 ", vibration = " + MergeVectorToString(vibrationValues_) +
318 ", isShowBadge = " + (isShowBadge_ ? "true" : "false") +
319 ", enabled = " + (enabled_ ? "true" : "false") +
320 ", slotFlags = " + std::to_string(static_cast<int32_t>(slotFlags_)) +
321 ", remindMode = " + std::to_string(static_cast<int32_t>(GetReminderMode())) +
322 " }";
323 }
324
Marshalling(Parcel & parcel) const325 bool NotificationSlot::Marshalling(Parcel &parcel) const
326 {
327 if (!parcel.WriteString(id_)) {
328 ANS_LOGE("Failed to write id");
329 return false;
330 }
331
332 if (!parcel.WriteString(name_)) {
333 ANS_LOGE("Failed to write name");
334 return false;
335 }
336
337 if (!parcel.WriteBool(isLightEnabled_)) {
338 ANS_LOGE("Failed to write isLightEnabled");
339 return false;
340 }
341
342 if (!parcel.WriteBool(isVibrationEnabled_)) {
343 ANS_LOGE("Failed to write isVibrationEnabled");
344 return false;
345 }
346
347 if (!parcel.WriteBool(isShowBadge_)) {
348 ANS_LOGE("Failed to write isShowBadge");
349 return false;
350 }
351
352 if (!parcel.WriteBool(isBypassDnd_)) {
353 ANS_LOGE("Failed to write isBypassDnd");
354 return false;
355 }
356
357 if (!parcel.WriteString(description_)) {
358 ANS_LOGE("Failed to write description");
359 return false;
360 }
361
362 if (!parcel.WriteInt32(lightColor_)) {
363 ANS_LOGE("Failed to write lightColor");
364 return false;
365 }
366
367 if (!parcel.WriteInt32(static_cast<int32_t>(level_))) {
368 ANS_LOGE("Failed to write level");
369 return false;
370 }
371
372 if (!parcel.WriteInt32(static_cast<int32_t>(type_))) {
373 ANS_LOGE("Failed to write type");
374 return false;
375 }
376
377 if (!parcel.WriteInt32(static_cast<int32_t>(lockScreenVisibleness_))) {
378 ANS_LOGE("Failed to write lockScreenVisibleness");
379 return false;
380 }
381
382 if (sound_.ToString().empty()) {
383 if (!parcel.WriteInt32(VALUE_NULL)) {
384 ANS_LOGE("Failed to write int");
385 return false;
386 }
387 } else {
388 if (!parcel.WriteInt32(VALUE_OBJECT)) {
389 ANS_LOGE("Failed to write int");
390 return false;
391 }
392 if (!parcel.WriteString((sound_.ToString()))) {
393 ANS_LOGE("Failed to write sound");
394 return false;
395 }
396 }
397
398 if (!parcel.WriteInt64Vector(vibrationValues_)) {
399 ANS_LOGE("Failed to write vibrationValues");
400 return false;
401 }
402
403 if (!parcel.WriteBool(enabled_)) {
404 ANS_LOGE("Failed to write isShowBadge");
405 return false;
406 }
407
408 if (!parcel.WriteInt32(slotFlags_)) {
409 ANS_LOGE("Failed to write slotFlags");
410 return false;
411 }
412
413 if (!parcel.WriteInt32(authorizedStatus_)) {
414 ANS_LOGE("Failed to write authorizedStatus");
415 return false;
416 }
417
418 if (!parcel.WriteInt32(authHintCnt_)) {
419 ANS_LOGE("Failed to write authHintCnt");
420 return false;
421 }
422
423 if (!parcel.WriteInt32(reminderMode_)) {
424 ANS_LOGE("Failed to write reminderMode");
425 return false;
426 }
427
428 return true;
429 }
430
ReadFromParcel(Parcel & parcel)431 bool NotificationSlot::ReadFromParcel(Parcel &parcel)
432 {
433 id_ = parcel.ReadString();
434 name_ = parcel.ReadString();
435 isLightEnabled_ = parcel.ReadBool();
436 isVibrationEnabled_ = parcel.ReadBool();
437 isShowBadge_ = parcel.ReadBool();
438 isBypassDnd_ = parcel.ReadBool();
439 description_ = parcel.ReadString();
440 lightColor_ = parcel.ReadInt32();
441 level_ = static_cast<NotificationLevel>(parcel.ReadInt32());
442 type_ = static_cast<NotificationConstant::SlotType>(parcel.ReadInt32());
443 lockScreenVisibleness_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
444
445 int32_t empty = VALUE_NULL;
446 if (!parcel.ReadInt32(empty)) {
447 ANS_LOGE("Failed to read int");
448 return false;
449 }
450
451 if (empty == VALUE_OBJECT) {
452 sound_ = Uri((parcel.ReadString()));
453 }
454
455 parcel.ReadInt64Vector(&vibrationValues_);
456 enabled_ = parcel.ReadBool();
457 slotFlags_ = parcel.ReadInt32();
458 authorizedStatus_ = parcel.ReadInt32();
459 authHintCnt_ = parcel.ReadInt32();
460 reminderMode_ = parcel.ReadInt32();
461 return true;
462 }
463
Unmarshalling(Parcel & parcel)464 NotificationSlot *NotificationSlot::Unmarshalling(Parcel &parcel)
465 {
466 NotificationSlot *notificationSlot = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::CUSTOM);
467
468 if (notificationSlot && !notificationSlot->ReadFromParcel(parcel)) {
469 delete notificationSlot;
470 notificationSlot = nullptr;
471 }
472
473 return notificationSlot;
474 }
475
MergeVectorToString(const std::vector<int64_t> & mergeVector) const476 std::string NotificationSlot::MergeVectorToString(const std::vector<int64_t> &mergeVector) const
477 {
478 std::string contents;
479 for (auto it = mergeVector.begin(); it != mergeVector.end(); ++it) {
480 contents += std::to_string(*it);
481 if (it != mergeVector.end() - 1) {
482 contents += ", ";
483 }
484 }
485 return contents;
486 }
487
TruncateString(const std::string & in)488 std::string NotificationSlot::TruncateString(const std::string &in)
489 {
490 std::string temp = in;
491 if (in.length() > MAX_TEXT_LENGTH) {
492 temp = in.substr(0, MAX_TEXT_LENGTH);
493 }
494 return temp;
495 }
496
GetSlotTypeByString(const std::string & strSlotType,NotificationConstant::SlotType & slotType)497 bool NotificationSlot::GetSlotTypeByString(
498 const std::string &strSlotType, NotificationConstant::SlotType &slotType)
499 {
500 auto iterSlotType = convertStrToSlotType_.find(strSlotType);
501 if (iterSlotType != convertStrToSlotType_.end()) {
502 slotType = iterSlotType->second;
503 return true;
504 }
505 ANS_LOGE("GetSlotTypeByString failed as Invalid strSlotType.");
506 return false;
507 }
508
GetDefaultReminderMode() const509 uint32_t NotificationSlot::GetDefaultReminderMode() const
510 {
511 uint32_t reminderMode = 0;
512 switch (type_) {
513 case NotificationConstant::SlotType::SOCIAL_COMMUNICATION:
514 reminderMode = SOUND_OPNE + LOCKSCREEN_OPNE + BANNER_OPNE + LIGHTSCREEN_OPNE +
515 VIBRATION_OPNE + STATUSBAR_ICON_OPNE;
516 break;
517 case NotificationConstant::SlotType::SERVICE_REMINDER:
518 reminderMode = SOUND_OPNE + LOCKSCREEN_OPNE + BANNER_OPNE + LIGHTSCREEN_OPNE +
519 VIBRATION_OPNE + STATUSBAR_ICON_OPNE;
520 break;
521 case NotificationConstant::SlotType::CONTENT_INFORMATION:
522 reminderMode = 0;
523 break;
524 case NotificationConstant::SlotType::LIVE_VIEW:
525 reminderMode = SOUND_OPNE + LOCKSCREEN_OPNE + VIBRATION_OPNE + STATUSBAR_ICON_OPNE + LIGHTSCREEN_OPNE;
526 break;
527 case NotificationConstant::SlotType::CUSTOMER_SERVICE:
528 reminderMode = SOUND_OPNE + VIBRATION_OPNE + STATUSBAR_ICON_OPNE;
529 break;
530 case NotificationConstant::SlotType::EMERGENCY_INFORMATION:
531 reminderMode = SOUND_OPNE + LOCKSCREEN_OPNE + BANNER_OPNE + LIGHTSCREEN_OPNE +
532 VIBRATION_OPNE + STATUSBAR_ICON_OPNE;
533 break;
534 case NotificationConstant::SlotType::OTHER:
535 reminderMode = 0;
536 break;
537 default:
538 break;
539 }
540
541 return reminderMode;
542 }
543 } // namespace Notification
544 } // namespace OHOS
545