• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 const int32_t MAX_TEXT_LENGTH = 1000;
23 
NotificationSlot(NotificationConstant::SlotType type)24 NotificationSlot::NotificationSlot(NotificationConstant::SlotType type) : sound_("")
25 {
26     SetType(type);
27 }
28 
~NotificationSlot()29 NotificationSlot::~NotificationSlot()
30 {}
31 
CanEnableLight() const32 bool NotificationSlot::CanEnableLight() const
33 {
34     return isLightEnabled_;
35 }
36 
SetEnableLight(bool isLightEnabled)37 void NotificationSlot::SetEnableLight(bool isLightEnabled)
38 {
39     isLightEnabled_ = isLightEnabled;
40 }
41 
CanVibrate() const42 bool NotificationSlot::CanVibrate() const
43 {
44     return isVibrationEnabled_;
45 }
46 
SetEnableVibration(bool vibration)47 void NotificationSlot::SetEnableVibration(bool vibration)
48 {
49     isVibrationEnabled_ = vibration;
50 }
51 
GetDescription() const52 std::string NotificationSlot::GetDescription() const
53 {
54     return description_;
55 }
56 
SetDescription(const std::string & description)57 void NotificationSlot::SetDescription(const std::string &description)
58 {
59     description_ = TruncateString(description);
60 }
61 
GetId() const62 std::string NotificationSlot::GetId() const
63 {
64     return id_;
65 }
66 
GetLedLightColor() const67 int32_t NotificationSlot::GetLedLightColor() const
68 {
69     return lightColor_;
70 }
71 
SetLedLightColor(int32_t color)72 void NotificationSlot::SetLedLightColor(int32_t color)
73 {
74     lightColor_ = color;
75 }
76 
GetLevel() const77 NotificationSlot::NotificationLevel NotificationSlot::GetLevel() const
78 {
79     return level_;
80 }
81 
SetLevel(NotificationLevel level)82 void NotificationSlot::SetLevel(NotificationLevel level)
83 {
84     level_ = level;
85 }
86 
GetType() const87 NotificationConstant::SlotType NotificationSlot::GetType() const
88 {
89     return type_;
90 }
91 
SetType(NotificationConstant::SlotType type)92 void NotificationSlot::SetType(NotificationConstant::SlotType type)
93 {
94     type_ = NotificationConstant::SlotType::CUSTOM;
95     switch (type) {
96         case NotificationConstant::SlotType::SOCIAL_COMMUNICATION:
97             id_ = "SOCIAL_COMMUNICATION";
98             SetName("SOCIAL_COMMUNICATION");
99             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
100             SetSound(DEFAULT_NOTIFICATION_SOUND);
101             SetVibrationStyle(DEFAULT_NOTIFICATION_VIBRATION);
102             SetLevel(LEVEL_HIGH);
103             break;
104         case NotificationConstant::SlotType::SERVICE_REMINDER:
105             id_ = "SERVICE_REMINDER";
106             SetName("SERVICE_REMINDER");
107             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
108             SetSound(DEFAULT_NOTIFICATION_SOUND);
109             SetVibrationStyle(DEFAULT_NOTIFICATION_VIBRATION);
110             SetLevel(LEVEL_DEFAULT);
111             break;
112         case NotificationConstant::SlotType::CONTENT_INFORMATION:
113             id_ = "CONTENT_INFORMATION";
114             SetName("CONTENT_INFORMATION");
115             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::SECRET);
116             SetEnableVibration(false);
117             SetLevel(LEVEL_LOW);
118             break;
119         case NotificationConstant::SlotType::LIVE_VIEW:
120             id_ = "LIVE_VIEW";
121             SetName("LIVE_VIEW");
122             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::PUBLIC);
123             SetSound(DEFAULT_NOTIFICATION_SOUND);
124             SetVibrationStyle(DEFAULT_NOTIFICATION_VIBRATION);
125             SetLevel(LEVEL_DEFAULT);
126             SetForceControl(true);
127             break;
128         case NotificationConstant::SlotType::CUSTOMER_SERVICE:
129             id_ = "CUSTOMER_SERVICE";
130             SetName("CUSTOMER_SERVICE");
131             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::SECRET);
132             SetEnableVibration(false);
133             SetLevel(LEVEL_LOW);
134             break;
135         case NotificationConstant::SlotType::OTHER:
136             id_ = "OTHER";
137             SetName("OTHER");
138             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::SECRET);
139             SetEnableVibration(false);
140             SetLevel(LEVEL_MIN);
141             break;
142         default:
143             break;
144     }
145     type_ = type;
146 }
147 
GetLockScreenVisibleness() const148 NotificationConstant::VisiblenessType NotificationSlot::GetLockScreenVisibleness() const
149 {
150     return lockScreenVisibleness_;
151 }
152 
SetLockscreenVisibleness(NotificationConstant::VisiblenessType visibleness)153 void NotificationSlot::SetLockscreenVisibleness(NotificationConstant::VisiblenessType visibleness)
154 {
155     lockScreenVisibleness_ = visibleness;
156 }
157 
GetName() const158 std::string NotificationSlot::GetName() const
159 {
160     return name_;
161 }
162 
SetName(const std::string & name)163 void NotificationSlot::SetName(const std::string &name)
164 {
165     name_ = TruncateString(name);
166 }
167 
GetSound() const168 Uri NotificationSlot::GetSound() const
169 {
170     return sound_;
171 }
172 
SetSound(const Uri & sound)173 void NotificationSlot::SetSound(const Uri &sound)
174 {
175     sound_ = sound;
176 }
177 
GetVibrationStyle() const178 std::vector<int64_t> NotificationSlot::GetVibrationStyle() const
179 {
180     return vibrationValues_;
181 }
182 
SetVibrationStyle(const std::vector<int64_t> & vibrationValues)183 void NotificationSlot::SetVibrationStyle(const std::vector<int64_t> &vibrationValues)
184 {
185     isVibrationEnabled_ = (vibrationValues.size() > 0);
186     vibrationValues_ = vibrationValues;
187 }
188 
IsEnableBypassDnd() const189 bool NotificationSlot::IsEnableBypassDnd() const
190 {
191     return isBypassDnd_;
192 }
193 
EnableBypassDnd(bool isBypassDnd)194 void NotificationSlot::EnableBypassDnd(bool isBypassDnd)
195 {
196     isBypassDnd_ = isBypassDnd;
197 }
198 
IsShowBadge() const199 bool NotificationSlot::IsShowBadge() const
200 {
201     return isShowBadge_;
202 }
203 
EnableBadge(bool isShowBadge)204 void NotificationSlot::EnableBadge(bool isShowBadge)
205 {
206     isShowBadge_ = isShowBadge;
207 }
208 
SetEnable(bool enabled)209 void NotificationSlot::SetEnable(bool enabled)
210 {
211     enabled_ = enabled;
212 }
213 
GetEnable() const214 bool NotificationSlot::GetEnable() const
215 {
216     return enabled_;
217 }
218 
SetSlotFlags(uint32_t slotFlags)219 void NotificationSlot::SetSlotFlags(uint32_t slotFlags)
220 {
221     slotFlags_ = slotFlags;
222 }
223 
GetSlotFlags() const224 uint32_t NotificationSlot::GetSlotFlags() const
225 {
226     return slotFlags_;
227 }
228 
SetForceControl(bool isForceControl)229 void NotificationSlot::SetForceControl(bool isForceControl)
230 {
231     isForceControl_ = isForceControl;
232 }
233 
GetForceControl() const234 bool NotificationSlot::GetForceControl() const
235 {
236     return isForceControl_;
237 }
238 
Dump() const239 std::string NotificationSlot::Dump() const
240 {
241     return "NotificationSlot{ "
242             "id = " + id_ +
243             ", name = " + name_ +
244             ", description = " + description_ +
245             ", type = " + std::to_string(static_cast<int32_t>(type_)) +
246             ", level = " + std::to_string(static_cast<int32_t>(level_)) +
247             ", isBypassDnd = " + (isBypassDnd_ ? "true" : "false") +
248             ", visibleness = " + std::to_string(static_cast<int32_t>(lockScreenVisibleness_)) +
249             ", sound = " + sound_.ToString() +
250             ", isLightEnabled = " + (isLightEnabled_ ? "true" : "false") +
251             ", lightColor = " + std::to_string(lightColor_) +
252             ", isVibrate = " + (isVibrationEnabled_ ? "true" : "false") +
253             ", vibration = " + MergeVectorToString(vibrationValues_) +
254             ", isShowBadge = " + (isShowBadge_ ? "true" : "false") +
255             ", enabled = " + (enabled_ ? "true" : "false") +
256             ", slotFlags = " + std::to_string(static_cast<int32_t>(slotFlags_)) +
257             " }";
258 }
259 
Marshalling(Parcel & parcel) const260 bool NotificationSlot::Marshalling(Parcel &parcel) const
261 {
262     if (!parcel.WriteString(id_)) {
263         ANS_LOGE("Failed to write id");
264         return false;
265     }
266 
267     if (!parcel.WriteString(name_)) {
268         ANS_LOGE("Failed to write name");
269         return false;
270     }
271 
272     if (!parcel.WriteBool(isLightEnabled_)) {
273         ANS_LOGE("Failed to write isLightEnabled");
274         return false;
275     }
276 
277     if (!parcel.WriteBool(isVibrationEnabled_)) {
278         ANS_LOGE("Failed to write isVibrationEnabled");
279         return false;
280     }
281 
282     if (!parcel.WriteBool(isShowBadge_)) {
283         ANS_LOGE("Failed to write isShowBadge");
284         return false;
285     }
286 
287     if (!parcel.WriteBool(isBypassDnd_)) {
288         ANS_LOGE("Failed to write isBypassDnd");
289         return false;
290     }
291 
292     if (!parcel.WriteString(description_)) {
293         ANS_LOGE("Failed to write description");
294         return false;
295     }
296 
297     if (!parcel.WriteInt32(lightColor_)) {
298         ANS_LOGE("Failed to write lightColor");
299         return false;
300     }
301 
302     if (!parcel.WriteInt32(static_cast<int32_t>(level_))) {
303         ANS_LOGE("Failed to write level");
304         return false;
305     }
306 
307     if (!parcel.WriteInt32(static_cast<int32_t>(type_))) {
308         ANS_LOGE("Failed to write type");
309         return false;
310     }
311 
312     if (!parcel.WriteInt32(static_cast<int32_t>(lockScreenVisibleness_))) {
313         ANS_LOGE("Failed to write lockScreenVisibleness");
314         return false;
315     }
316 
317     if (sound_.ToString().empty()) {
318         if (!parcel.WriteInt32(VALUE_NULL)) {
319             ANS_LOGE("Failed to write int");
320             return false;
321         }
322     } else {
323         if (!parcel.WriteInt32(VALUE_OBJECT)) {
324             ANS_LOGE("Failed to write int");
325             return false;
326         }
327         if (!parcel.WriteString((sound_.ToString()))) {
328             ANS_LOGE("Failed to write sound");
329             return false;
330         }
331     }
332 
333     if (!parcel.WriteInt64Vector(vibrationValues_)) {
334         ANS_LOGE("Failed to write vibrationValues");
335         return false;
336     }
337 
338     if (!parcel.WriteBool(enabled_)) {
339         ANS_LOGE("Failed to write isShowBadge");
340         return false;
341     }
342 
343     if (!parcel.WriteInt32(slotFlags_)) {
344         ANS_LOGE("Failed to write slotFlags");
345         return false;
346     }
347 
348     return true;
349 }
350 
ReadFromParcel(Parcel & parcel)351 bool NotificationSlot::ReadFromParcel(Parcel &parcel)
352 {
353     id_ = parcel.ReadString();
354     name_ = parcel.ReadString();
355     isLightEnabled_ = parcel.ReadBool();
356     isVibrationEnabled_ = parcel.ReadBool();
357     isShowBadge_ = parcel.ReadBool();
358     isBypassDnd_ = parcel.ReadBool();
359     description_ = parcel.ReadString();
360     lightColor_ = parcel.ReadInt32();
361     level_ = static_cast<NotificationLevel>(parcel.ReadInt32());
362     type_ = static_cast<NotificationConstant::SlotType>(parcel.ReadInt32());
363     lockScreenVisibleness_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
364 
365     int32_t empty = VALUE_NULL;
366     if (!parcel.ReadInt32(empty)) {
367         ANS_LOGE("Failed to read int");
368         return false;
369     }
370 
371     if (empty == VALUE_OBJECT) {
372         sound_ = Uri((parcel.ReadString()));
373     }
374 
375     parcel.ReadInt64Vector(&vibrationValues_);
376     enabled_ = parcel.ReadBool();
377     slotFlags_ = parcel.ReadInt32();
378     return true;
379 }
380 
Unmarshalling(Parcel & parcel)381 NotificationSlot *NotificationSlot::Unmarshalling(Parcel &parcel)
382 {
383     NotificationSlot *notificationSlot = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::CUSTOM);
384 
385     if (notificationSlot && !notificationSlot->ReadFromParcel(parcel)) {
386         delete notificationSlot;
387         notificationSlot = nullptr;
388     }
389 
390     return notificationSlot;
391 }
392 
MergeVectorToString(const std::vector<int64_t> & mergeVector) const393 std::string NotificationSlot::MergeVectorToString(const std::vector<int64_t> &mergeVector) const
394 {
395     std::string contents;
396     for (auto it = mergeVector.begin(); it != mergeVector.end(); ++it) {
397         contents += std::to_string(*it);
398         if (it != mergeVector.end() - 1) {
399             contents += ", ";
400         }
401     }
402     return contents;
403 }
404 
TruncateString(const std::string & in)405 std::string NotificationSlot::TruncateString(const std::string &in)
406 {
407     std::string temp = in;
408     if (in.length() > MAX_TEXT_LENGTH) {
409         temp = in.substr(0, MAX_TEXT_LENGTH);
410     }
411     return temp;
412 }
413 }  // namespace Notification
414 }  // namespace OHOS
415