• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
160             SetSound(DEFAULT_NOTIFICATION_SOUND);
161 #endif
162             SetLevel(LEVEL_MIN);
163             break;
164         case NotificationConstant::SlotType::LIVE_VIEW:
165             id_ = "LIVE_VIEW";
166             SetName("LIVE_VIEW");
167             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
168             SetSound(DEFAULT_NOTIFICATION_SOUND);
169             SetVibrationStyle(DEFAULT_NOTIFICATION_VIBRATION);
170             SetLevel(LEVEL_DEFAULT);
171             SetForceControl(true);
172             break;
173         case NotificationConstant::SlotType::CUSTOMER_SERVICE:
174             id_ = "CUSTOMER_SERVICE";
175             SetName("CUSTOMER_SERVICE");
176             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::SECRET);
177             SetSound(DEFAULT_NOTIFICATION_SOUND);
178             SetEnableVibration(false);
179             SetLevel(LEVEL_LOW);
180             break;
181         case NotificationConstant::SlotType::EMERGENCY_INFORMATION:
182             id_ = "EMERGENCY_INFORMATION";
183             SetName("EMERGENCY_INFORMATION");
184             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
185             SetSound(DEFAULT_NOTIFICATION_SOUND);
186             SetVibrationStyle(DEFAULT_NOTIFICATION_VIBRATION);
187             SetLevel(LEVEL_HIGH);
188             break;
189         case NotificationConstant::SlotType::OTHER:
190             id_ = "OTHER";
191             SetName("OTHER");
192             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::SECRET);
193             SetEnableVibration(false);
194 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
195             SetSound(DEFAULT_NOTIFICATION_SOUND);
196 #endif
197             SetLevel(LEVEL_MIN);
198             break;
199         default:
200             break;
201     }
202     type_ = type;
203 }
204 
GetLockScreenVisibleness() const205 NotificationConstant::VisiblenessType NotificationSlot::GetLockScreenVisibleness() const
206 {
207     return lockScreenVisibleness_;
208 }
209 
SetLockscreenVisibleness(NotificationConstant::VisiblenessType visibleness)210 void NotificationSlot::SetLockscreenVisibleness(NotificationConstant::VisiblenessType visibleness)
211 {
212     lockScreenVisibleness_ = visibleness;
213 }
214 
GetName() const215 std::string NotificationSlot::GetName() const
216 {
217     return name_;
218 }
219 
SetName(const std::string & name)220 void NotificationSlot::SetName(const std::string &name)
221 {
222     name_ = TruncateString(name);
223 }
224 
GetSound() const225 Uri NotificationSlot::GetSound() const
226 {
227     return sound_;
228 }
229 
SetSound(const Uri & sound)230 void NotificationSlot::SetSound(const Uri &sound)
231 {
232     sound_ = sound;
233 }
234 
GetVibrationStyle() const235 std::vector<int64_t> NotificationSlot::GetVibrationStyle() const
236 {
237     return vibrationValues_;
238 }
239 
SetVibrationStyle(const std::vector<int64_t> & vibrationValues)240 void NotificationSlot::SetVibrationStyle(const std::vector<int64_t> &vibrationValues)
241 {
242     isVibrationEnabled_ = (vibrationValues.size() > 0);
243     vibrationValues_ = vibrationValues;
244 }
245 
IsEnableBypassDnd() const246 bool NotificationSlot::IsEnableBypassDnd() const
247 {
248     return isBypassDnd_;
249 }
250 
EnableBypassDnd(bool isBypassDnd)251 void NotificationSlot::EnableBypassDnd(bool isBypassDnd)
252 {
253     isBypassDnd_ = isBypassDnd;
254 }
255 
IsShowBadge() const256 bool NotificationSlot::IsShowBadge() const
257 {
258     return isShowBadge_;
259 }
260 
EnableBadge(bool isShowBadge)261 void NotificationSlot::EnableBadge(bool isShowBadge)
262 {
263     isShowBadge_ = isShowBadge;
264 }
265 
SetEnable(bool enabled)266 void NotificationSlot::SetEnable(bool enabled)
267 {
268     enabled_ = enabled;
269 }
270 
GetEnable() const271 bool NotificationSlot::GetEnable() const
272 {
273     return enabled_;
274 }
275 
SetSlotFlags(uint32_t slotFlags)276 void NotificationSlot::SetSlotFlags(uint32_t slotFlags)
277 {
278     slotFlags_ = slotFlags;
279 }
280 
GetSlotFlags() const281 uint32_t NotificationSlot::GetSlotFlags() const
282 {
283     return slotFlags_;
284 }
285 
SetForceControl(bool isForceControl)286 void NotificationSlot::SetForceControl(bool isForceControl)
287 {
288     isForceControl_ = isForceControl;
289 }
290 
GetForceControl() const291 bool NotificationSlot::GetForceControl() const
292 {
293     return isForceControl_;
294 }
295 
SetReminderMode(uint32_t reminderMode)296 void NotificationSlot::SetReminderMode(uint32_t reminderMode)
297 {
298     reminderMode_ = reminderMode;
299 }
300 
GetReminderMode() const301 uint32_t NotificationSlot::GetReminderMode() const
302 {
303     if (reminderMode_ != INVALID_REMINDER_MODE) {
304         return reminderMode_;
305     }
306     return GetDefaultReminderMode();
307 }
308 
Dump() const309 std::string NotificationSlot::Dump() const
310 {
311     return "NotificationSlot{ "
312             "id = " + id_ +
313             ", name = " + name_ +
314             ", description = " + description_ +
315             ", type = " + std::to_string(static_cast<int32_t>(type_)) +
316             ", level = " + std::to_string(static_cast<int32_t>(level_)) +
317             ", isBypassDnd = " + (isBypassDnd_ ? "true" : "false") +
318             ", visibleness = " + std::to_string(static_cast<int32_t>(lockScreenVisibleness_)) +
319             ", sound = " + sound_.ToString() +
320             ", isLightEnabled = " + (isLightEnabled_ ? "true" : "false") +
321             ", lightColor = " + std::to_string(lightColor_) +
322             ", isVibrate = " + (isVibrationEnabled_ ? "true" : "false") +
323             ", vibration = " + MergeVectorToString(vibrationValues_) +
324             ", isShowBadge = " + (isShowBadge_ ? "true" : "false") +
325             ", enabled = " + (enabled_ ? "true" : "false") +
326             ", slotFlags = " + std::to_string(static_cast<int32_t>(slotFlags_)) +
327             ", remindMode = " + std::to_string(static_cast<int32_t>(GetReminderMode())) +
328             " }";
329 }
330 
Marshalling(Parcel & parcel) const331 bool NotificationSlot::Marshalling(Parcel &parcel) const
332 {
333     if (!parcel.WriteString(id_)) {
334         ANS_LOGE("Failed to write id");
335         return false;
336     }
337 
338     if (!parcel.WriteString(name_)) {
339         ANS_LOGE("Failed to write name");
340         return false;
341     }
342 
343     if (!parcel.WriteBool(isLightEnabled_)) {
344         ANS_LOGE("Failed to write isLightEnabled");
345         return false;
346     }
347 
348     if (!parcel.WriteBool(isVibrationEnabled_)) {
349         ANS_LOGE("Failed to write isVibrationEnabled");
350         return false;
351     }
352 
353     if (!parcel.WriteBool(isShowBadge_)) {
354         ANS_LOGE("Failed to write isShowBadge");
355         return false;
356     }
357 
358     if (!parcel.WriteBool(isBypassDnd_)) {
359         ANS_LOGE("Failed to write isBypassDnd");
360         return false;
361     }
362 
363     if (!parcel.WriteString(description_)) {
364         ANS_LOGE("Failed to write description");
365         return false;
366     }
367 
368     if (!parcel.WriteInt32(lightColor_)) {
369         ANS_LOGE("Failed to write lightColor");
370         return false;
371     }
372 
373     if (!parcel.WriteInt32(static_cast<int32_t>(level_))) {
374         ANS_LOGE("Failed to write level");
375         return false;
376     }
377 
378     if (!parcel.WriteInt32(static_cast<int32_t>(type_))) {
379         ANS_LOGE("Failed to write type");
380         return false;
381     }
382 
383     if (!parcel.WriteInt32(static_cast<int32_t>(lockScreenVisibleness_))) {
384         ANS_LOGE("Failed to write lockScreenVisibleness");
385         return false;
386     }
387 
388     if (sound_.ToString().empty()) {
389         if (!parcel.WriteInt32(VALUE_NULL)) {
390             ANS_LOGE("Failed to write int");
391             return false;
392         }
393     } else {
394         if (!parcel.WriteInt32(VALUE_OBJECT)) {
395             ANS_LOGE("Failed to write int");
396             return false;
397         }
398         if (!parcel.WriteString((sound_.ToString()))) {
399             ANS_LOGE("Failed to write sound");
400             return false;
401         }
402     }
403 
404     if (!parcel.WriteInt64Vector(vibrationValues_)) {
405         ANS_LOGE("Failed to write vibrationValues");
406         return false;
407     }
408 
409     if (!parcel.WriteBool(enabled_)) {
410         ANS_LOGE("Failed to write isShowBadge");
411         return false;
412     }
413 
414     if (!parcel.WriteInt32(slotFlags_)) {
415         ANS_LOGE("Failed to write slotFlags");
416         return false;
417     }
418 
419     if (!parcel.WriteInt32(authorizedStatus_)) {
420         ANS_LOGE("Failed to write authorizedStatus");
421         return false;
422     }
423 
424     if (!parcel.WriteInt32(authHintCnt_)) {
425         ANS_LOGE("Failed to write authHintCnt");
426         return false;
427     }
428 
429     if (!parcel.WriteInt32(reminderMode_)) {
430         ANS_LOGE("Failed to write reminderMode");
431         return false;
432     }
433 
434     return true;
435 }
436 
ReadFromParcel(Parcel & parcel)437 bool NotificationSlot::ReadFromParcel(Parcel &parcel)
438 {
439     id_ = parcel.ReadString();
440     name_ = parcel.ReadString();
441     isLightEnabled_ = parcel.ReadBool();
442     isVibrationEnabled_ = parcel.ReadBool();
443     isShowBadge_ = parcel.ReadBool();
444     isBypassDnd_ = parcel.ReadBool();
445     description_ = parcel.ReadString();
446     lightColor_ = parcel.ReadInt32();
447     level_ = static_cast<NotificationLevel>(parcel.ReadInt32());
448     type_ = static_cast<NotificationConstant::SlotType>(parcel.ReadInt32());
449     lockScreenVisibleness_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
450 
451     int32_t empty = VALUE_NULL;
452     if (!parcel.ReadInt32(empty)) {
453         ANS_LOGE("Failed to read int");
454         return false;
455     }
456 
457     if (empty == VALUE_OBJECT) {
458         sound_ = Uri((parcel.ReadString()));
459     }
460 
461     parcel.ReadInt64Vector(&vibrationValues_);
462     enabled_ = parcel.ReadBool();
463     slotFlags_ = parcel.ReadInt32();
464     authorizedStatus_ = parcel.ReadInt32();
465     authHintCnt_ = parcel.ReadInt32();
466     reminderMode_ = parcel.ReadInt32();
467     return true;
468 }
469 
Unmarshalling(Parcel & parcel)470 NotificationSlot *NotificationSlot::Unmarshalling(Parcel &parcel)
471 {
472     NotificationSlot *notificationSlot = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::CUSTOM);
473 
474     if (notificationSlot && !notificationSlot->ReadFromParcel(parcel)) {
475         delete notificationSlot;
476         notificationSlot = nullptr;
477     }
478 
479     return notificationSlot;
480 }
481 
MergeVectorToString(const std::vector<int64_t> & mergeVector) const482 std::string NotificationSlot::MergeVectorToString(const std::vector<int64_t> &mergeVector) const
483 {
484     std::string contents;
485     for (auto it = mergeVector.begin(); it != mergeVector.end(); ++it) {
486         contents += std::to_string(*it);
487         if (it != mergeVector.end() - 1) {
488             contents += ", ";
489         }
490     }
491     return contents;
492 }
493 
TruncateString(const std::string & in)494 std::string NotificationSlot::TruncateString(const std::string &in)
495 {
496     std::string temp = in;
497     if (in.length() > MAX_TEXT_LENGTH) {
498         temp = in.substr(0, MAX_TEXT_LENGTH);
499     }
500     return temp;
501 }
502 
GetSlotTypeByString(const std::string & strSlotType,NotificationConstant::SlotType & slotType)503 bool NotificationSlot::GetSlotTypeByString(
504     const std::string &strSlotType, NotificationConstant::SlotType &slotType)
505 {
506     auto iterSlotType = convertStrToSlotType_.find(strSlotType);
507     if (iterSlotType != convertStrToSlotType_.end()) {
508         slotType = iterSlotType->second;
509         return true;
510     }
511     ANS_LOGE("GetSlotTypeByString failed as Invalid strSlotType.");
512     return false;
513 }
514 
GetDefaultReminderMode() const515 uint32_t NotificationSlot::GetDefaultReminderMode() const
516 {
517     uint32_t reminderMode = 0;
518     switch (type_) {
519         case NotificationConstant::SlotType::SOCIAL_COMMUNICATION:
520             reminderMode = SOUND_OPNE + LOCKSCREEN_OPNE + BANNER_OPNE + LIGHTSCREEN_OPNE +
521                 VIBRATION_OPNE + STATUSBAR_ICON_OPNE;
522             break;
523         case NotificationConstant::SlotType::SERVICE_REMINDER:
524             reminderMode = SOUND_OPNE + LOCKSCREEN_OPNE + BANNER_OPNE + LIGHTSCREEN_OPNE +
525                 VIBRATION_OPNE + STATUSBAR_ICON_OPNE;
526             break;
527         case NotificationConstant::SlotType::CONTENT_INFORMATION:
528             reminderMode = 0;
529             break;
530         case NotificationConstant::SlotType::LIVE_VIEW:
531             reminderMode = SOUND_OPNE + LOCKSCREEN_OPNE + VIBRATION_OPNE + STATUSBAR_ICON_OPNE + LIGHTSCREEN_OPNE;
532             break;
533         case NotificationConstant::SlotType::CUSTOMER_SERVICE:
534             reminderMode = SOUND_OPNE + VIBRATION_OPNE + STATUSBAR_ICON_OPNE;
535             break;
536         case NotificationConstant::SlotType::EMERGENCY_INFORMATION:
537             reminderMode = SOUND_OPNE + LOCKSCREEN_OPNE + BANNER_OPNE + LIGHTSCREEN_OPNE +
538                 VIBRATION_OPNE + STATUSBAR_ICON_OPNE;
539             break;
540         case NotificationConstant::SlotType::OTHER:
541             reminderMode = 0;
542             break;
543         default:
544             break;
545     }
546 
547     return reminderMode;
548 }
549 }  // namespace Notification
550 }  // namespace OHOS
551