• 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::OTHER:
120             id_ = "OTHER";
121             SetName("OTHER");
122             SetLockscreenVisibleness(NotificationConstant::VisiblenessType::SECRET);
123             SetEnableVibration(false);
124             SetLevel(LEVEL_MIN);
125             break;
126         default:
127             break;
128     }
129     type_ = type;
130 }
131 
GetLockScreenVisibleness() const132 NotificationConstant::VisiblenessType NotificationSlot::GetLockScreenVisibleness() const
133 {
134     return lockScreenVisibleness_;
135 }
136 
SetLockscreenVisibleness(NotificationConstant::VisiblenessType visibleness)137 void NotificationSlot::SetLockscreenVisibleness(NotificationConstant::VisiblenessType visibleness)
138 {
139     lockScreenVisibleness_ = visibleness;
140 }
141 
GetName() const142 std::string NotificationSlot::GetName() const
143 {
144     return name_;
145 }
146 
SetName(const std::string & name)147 void NotificationSlot::SetName(const std::string &name)
148 {
149     name_ = TruncateString(name);
150 }
151 
GetSound() const152 Uri NotificationSlot::GetSound() const
153 {
154     return sound_;
155 }
156 
SetSound(const Uri & sound)157 void NotificationSlot::SetSound(const Uri &sound)
158 {
159     sound_ = sound;
160 }
161 
GetVibrationStyle() const162 std::vector<int64_t> NotificationSlot::GetVibrationStyle() const
163 {
164     return vibrationValues_;
165 }
166 
SetVibrationStyle(const std::vector<int64_t> & vibrationValues)167 void NotificationSlot::SetVibrationStyle(const std::vector<int64_t> &vibrationValues)
168 {
169     isVibrationEnabled_ = (vibrationValues.size() > 0);
170     vibrationValues_ = vibrationValues;
171 }
172 
IsEnableBypassDnd() const173 bool NotificationSlot::IsEnableBypassDnd() const
174 {
175     return isBypassDnd_;
176 }
177 
EnableBypassDnd(bool isBypassDnd)178 void NotificationSlot::EnableBypassDnd(bool isBypassDnd)
179 {
180     isBypassDnd_ = isBypassDnd;
181 }
182 
IsShowBadge() const183 bool NotificationSlot::IsShowBadge() const
184 {
185     return isShowBadge_;
186 }
187 
EnableBadge(bool isShowBadge)188 void NotificationSlot::EnableBadge(bool isShowBadge)
189 {
190     isShowBadge_ = isShowBadge;
191 }
192 
SetEnable(bool enabled)193 void NotificationSlot::SetEnable(bool enabled)
194 {
195     enabled_ = enabled;
196 }
197 
GetEnable() const198 bool NotificationSlot::GetEnable() const
199 {
200     return enabled_;
201 }
202 
Dump() const203 std::string NotificationSlot::Dump() const
204 {
205     return "NotificationSlot{ "
206             "id = " + id_ +
207             ", name = " + name_ +
208             ", description = " + description_ +
209             ", type = " + std::to_string(static_cast<int32_t>(type_)) +
210             ", level = " + std::to_string(static_cast<int32_t>(level_)) +
211             ", isBypassDnd = " + (isBypassDnd_ ? "true" : "false") +
212             ", visibleness = " + std::to_string(static_cast<int32_t>(lockScreenVisibleness_)) +
213             ", sound = " + sound_.ToString() +
214             ", isLightEnabled = " + (isLightEnabled_ ? "true" : "false") +
215             ", lightColor = " + std::to_string(lightColor_) +
216             ", isVibrate = " + (isVibrationEnabled_ ? "true" : "false") +
217             ", vibration = " + MergeVectorToString(vibrationValues_) +
218             ", isShowBadge = " + (isShowBadge_ ? "true" : "false") +
219             ", enabled = " + (enabled_ ? "true" : "false") +
220             " }";
221 }
222 
Marshalling(Parcel & parcel) const223 bool NotificationSlot::Marshalling(Parcel &parcel) const
224 {
225     if (!parcel.WriteString(id_)) {
226         ANS_LOGE("Failed to write id");
227         return false;
228     }
229 
230     if (!parcel.WriteString(name_)) {
231         ANS_LOGE("Failed to write name");
232         return false;
233     }
234 
235     if (!parcel.WriteBool(isLightEnabled_)) {
236         ANS_LOGE("Failed to write isLightEnabled");
237         return false;
238     }
239 
240     if (!parcel.WriteBool(isVibrationEnabled_)) {
241         ANS_LOGE("Failed to write isVibrationEnabled");
242         return false;
243     }
244 
245     if (!parcel.WriteBool(isShowBadge_)) {
246         ANS_LOGE("Failed to write isShowBadge");
247         return false;
248     }
249 
250     if (!parcel.WriteBool(isBypassDnd_)) {
251         ANS_LOGE("Failed to write isBypassDnd");
252         return false;
253     }
254 
255     if (!parcel.WriteString(description_)) {
256         ANS_LOGE("Failed to write description");
257         return false;
258     }
259 
260     if (!parcel.WriteInt32(lightColor_)) {
261         ANS_LOGE("Failed to write lightColor");
262         return false;
263     }
264 
265     if (!parcel.WriteInt32(static_cast<int32_t>(level_))) {
266         ANS_LOGE("Failed to write level");
267         return false;
268     }
269 
270     if (!parcel.WriteInt32(static_cast<int32_t>(type_))) {
271         ANS_LOGE("Failed to write type");
272         return false;
273     }
274 
275     if (!parcel.WriteInt32(static_cast<int32_t>(lockScreenVisibleness_))) {
276         ANS_LOGE("Failed to write lockScreenVisibleness");
277         return false;
278     }
279 
280     if (sound_.ToString().empty()) {
281         if (!parcel.WriteInt32(VALUE_NULL)) {
282             ANS_LOGE("Failed to write int");
283             return false;
284         }
285     } else {
286         if (!parcel.WriteInt32(VALUE_OBJECT)) {
287             ANS_LOGE("Failed to write int");
288             return false;
289         }
290         if (!parcel.WriteString((sound_.ToString()))) {
291             ANS_LOGE("Failed to write sound");
292             return false;
293         }
294     }
295 
296     if (!parcel.WriteInt64Vector(vibrationValues_)) {
297         ANS_LOGE("Failed to write vibrationValues");
298         return false;
299     }
300 
301     if (!parcel.WriteBool(enabled_)) {
302         ANS_LOGE("Failed to write isShowBadge");
303         return false;
304     }
305 
306     return true;
307 }
308 
ReadFromParcel(Parcel & parcel)309 bool NotificationSlot::ReadFromParcel(Parcel &parcel)
310 {
311     id_ = parcel.ReadString();
312     name_ = parcel.ReadString();
313     isLightEnabled_ = parcel.ReadBool();
314     isVibrationEnabled_ = parcel.ReadBool();
315     isShowBadge_ = parcel.ReadBool();
316     isBypassDnd_ = parcel.ReadBool();
317     description_ = parcel.ReadString();
318     lightColor_ = parcel.ReadInt32();
319     level_ = static_cast<NotificationLevel>(parcel.ReadInt32());
320     type_ = static_cast<NotificationConstant::SlotType>(parcel.ReadInt32());
321     lockScreenVisibleness_ = static_cast<NotificationConstant::VisiblenessType>(parcel.ReadInt32());
322 
323     int32_t empty = VALUE_NULL;
324     if (!parcel.ReadInt32(empty)) {
325         ANS_LOGE("Failed to read int");
326         return false;
327     }
328 
329     if (empty == VALUE_OBJECT) {
330         sound_ = Uri((parcel.ReadString()));
331     }
332 
333     parcel.ReadInt64Vector(&vibrationValues_);
334     enabled_ = parcel.ReadBool();
335     return true;
336 }
337 
Unmarshalling(Parcel & parcel)338 NotificationSlot *NotificationSlot::Unmarshalling(Parcel &parcel)
339 {
340     NotificationSlot *notificationSlot = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::CUSTOM);
341 
342     if (notificationSlot && !notificationSlot->ReadFromParcel(parcel)) {
343         delete notificationSlot;
344         notificationSlot = nullptr;
345     }
346 
347     return notificationSlot;
348 }
349 
MergeVectorToString(const std::vector<int64_t> & mergeVector) const350 std::string NotificationSlot::MergeVectorToString(const std::vector<int64_t> &mergeVector) const
351 {
352     std::string contents;
353     for (auto it = mergeVector.begin(); it != mergeVector.end(); ++it) {
354         contents += std::to_string(*it);
355         if (it != mergeVector.end() - 1) {
356             contents += ", ";
357         }
358     }
359     return contents;
360 }
361 
TruncateString(const std::string & in)362 std::string NotificationSlot::TruncateString(const std::string &in)
363 {
364     std::string temp = in;
365     if (in.length() > MAX_TEXT_LENGTH) {
366         temp = in.substr(0, MAX_TEXT_LENGTH);
367     }
368     return temp;
369 }
370 }  // namespace Notification
371 }  // namespace OHOS