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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SLOT_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SLOT_H
18 
19 #include "notification_content.h"
20 #include "notification_request.h"
21 #include "parcel.h"
22 #include "uri.h"
23 
24 namespace OHOS {
25 namespace Notification {
26 static const uint32_t INVALID_REMINDER_MODE = 0xffffffff;
27 class NotificationSlot : public Parcelable {
28 public:
29     enum NotificationLevel {
30         LEVEL_NONE,       // the notification function is disabled.
31         LEVEL_MIN,        // the notifications function is disabled on the notification panel,
32                           // with no banner or prompt tone
33         LEVEL_LOW,        // the notifications are displayed on the notification panel,
34                           // with no banner or prompt tone
35         LEVEL_DEFAULT,    // the notification function is enabled and notifications are displayed,
36                           // on the notification panel, with a banner and a prompt tone.
37         LEVEL_HIGH,       // the notifications are displayed on the notification panel,
38                           // with a banner and a prompt tone
39         LEVEL_UNDEFINED,  // the notification does not define an level.
40     };
41 
42     enum AuthorizedStatus {
43         AUTHORIZED,         // the slot has been authorized
44         NOT_AUTHORIZED,     // the slot has not been authorized
45     };
46 
47     /**
48      * @brief A constructor used to initialize the type of a NotificationSlot object.
49      *
50      * @param type  Specifies the type of the NotificationSlot object,
51      */
52     NotificationSlot(NotificationConstant::SlotType type = NotificationConstant::SlotType::CUSTOM);
53 
54     ~NotificationSlot();
55 
56     /**
57      * @brief Obtains whether the notification light is enabled in a NotificationSlot object,
58      * which is set by SetEnableLight(bool).
59      *
60      * @return Returns true if the notification light is enabled; returns false otherwise.
61      */
62     bool CanEnableLight() const;
63 
64     /**
65      * @brief Specifies whether to enable the notification light when a notification is received on the device,
66      * provided that this device has a notification light.
67      * @note SetEnableLight must be set before the NotificationHelper:AddNotificationSlot(NotificationSlot) method is
68      * called. Otherwise, the settings will not take effect.
69      *
70      * @param isLightEnabled Specifies whether to enable the pulse notification light.
71      *                       The value true indicates to enable the notification light,
72      *                       and the value false indicates not to enable it.
73      */
74     void SetEnableLight(bool isLightEnabled);
75 
76     /**
77      * @brief Obtains the vibration status of a NotificationSlot object,
78      * which is set by SetEnableVibration(bool).
79      *
80      * @return Returns true if vibration is enabled; returns false otherwise.
81      */
82     bool CanVibrate() const;
83 
84     /**
85      * @brief Sets whether to enable vibration when a notification is received.
86      * @note SetEnableVibration(bool) must be set before the NotificationHelper::AddNotificationSlot(NotificationSlot)
87      * method is called. Otherwise, the settings will not take effect.
88      *
89      * @param vibration Indicates whether to enable vibration when a notification is received.
90      *                  If the value is true, vibration is enabled; if the value is false, vibration is disabled.
91      */
92     void SetEnableVibration(bool vibration);
93 
94     /**
95      * @brief Obtains the description of a NotificationSlot object, which is set by SetDescription(string).
96      *
97      * @return Returns the description of the NotificationSlot object.
98      */
99     std::string GetDescription() const;
100 
101     /**
102      * @brief Sets the description for a NotificationSlot object.
103      * @note The setting of setDescription is effective regardless of whether a NotificationSlot object has been created
104      * by NotificationHelper::AddNotificationSlot(NotificationSlot).
105      *
106      * @param description describes the NotificationSlot object.
107      *                    The description is visible to users and its length must not exceed 1000 characters
108      *                    (the excessive part is automatically truncated).
109      */
110     void SetDescription(const std::string &description);
111 
112     /**
113      * @brief Obtains the ID of a NotificationSlot object.
114      *
115      * @return Returns the ID of the NotificationSlot object,
116      *         which is set by NotificationSlot(string, string, NotificationLevel).
117      */
118     std::string GetId() const;
119 
120     /**
121      * @brief Obtains the color of the notification light in a NotificationSlot object,
122      * which is set by SetLedLightColor(int32_t).
123      *
124      * @return Returns the color of the notification light.
125      */
126     int32_t GetLedLightColor() const;
127 
128     /**
129      * @brief Sets the color of the notification light to flash when a notification is received on the device,
130      * provided that this device has a notification light and setEnableLight is called with the value true.
131      * @note SetLedLightColor must be set before the NotificationHelper::AddNotificationSlot(NotificationSlot) method is
132      * called. Otherwise, the settings will not take effect.
133      *
134      * @param color Indicates the color of the notification light.
135      */
136     void SetLedLightColor(int32_t color);
137 
138     /**
139      * @brief Obtains the level of a NotificationSlot object, which is set by SetLevel(NotificationLevel).
140      *
141      * @return Returns the level of the NotificationSlot object.
142      */
143     NotificationLevel GetLevel() const;
144 
145     /**
146      * @brief Sets the level of a NotificationSlot object.
147      * @note SetLevel must be set before the NotificationHelper::AddNotificationSlot(NotificationSlot) method is called.
148      *       Otherwise, the settings will not take effect.
149      *
150      * @param level Specifies the level of the NotificationSlot object, which determines the notification display
151      * effect. The value can be LEVEL_NONE, LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, or LEVEL_HIGH.
152      */
153     void SetLevel(NotificationLevel level);
154 
155     /**
156      * @brief Sets the slotflags of a NotificationSlot object.
157      * @note SetSlotFlags must be set before the NotificationHelper::AddNotificationSlot(NotificationSlot)
158      * method is called.
159      *       Otherwise, the settings will not take effect.
160      *
161      * @param slotFlags Specifies the slotflags of the NotificationSlot object,
162      * @note which determines the notification display effect.
163      * The value can be LEVEL_NONE, LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, or LEVEL_HIGH.
164      */
165     void SetSlotFlags(uint32_t slotFlags);
166 
167     /**
168      * @brief Obtains the slotflags of a NotificationSlot object, which is set by SetSlotFlags(uint32_t slotFlags).
169      *
170      * @return Returns the slotflags of the NotificationSlot object.
171      */
172     uint32_t GetSlotFlags() const;
173 
174     /**
175      * @brief Obtains the type of a NotificationSlot object, which is set by SetType(SlotType).
176      *
177      * @return Returns the Type of the NotificationSlot object.
178      */
179     NotificationConstant::SlotType GetType() const;
180 
181     /**
182      * @brief Sets the type of a NotificationSlot object.
183      * @note Settype must be set before the NotificationHelper::AddNotificationSlot(NotificationSlot) method is called.
184      *       Otherwise, the settings will not take effect.
185      *
186      * @param type Specifies the type of the NotificationSlot object, which determines the notification remind mode.
187      *        The value can be DEFAULT, SOCIAL_COMMUNICATION, SERVICE_REMINDER, CONTENT_INFORMATION, or OTHER.
188      */
189     void SetType(NotificationConstant::SlotType type);
190 
191     /**
192      * @brief Obtains the notification display effect of a NotificationSlot object on the lock screen,
193      * which is set by SetLockscreenVisibleness(int32_t).
194      * @note This method specifies different effects for displaying notifications on the lock screen in order to protect
195      * user privacy. The setting takes effect only when the lock screen notifications function is enabled for an
196      * application in system notification settings.
197      *
198      * @return Returns the notification display effect of the NotificationSlot object on the lock screen.
199      */
200     NotificationConstant::VisiblenessType GetLockScreenVisibleness() const;
201 
202     /**
203      * @brief Sets whether and how to display notifications on the lock screen.
204      *
205      * @param visibleness Specifies the notification display effect on the lock screen, which can be set to
206      *                    NO_OVERRIDE, PUBLIC, PRIVATE, or SECRET.
207      */
208     void SetLockscreenVisibleness(NotificationConstant::VisiblenessType visibleness);
209 
210     /**
211      * @brief Obtains the name of a NotificationSlot object.
212      *
213      * @return Returns the name of the NotificationSlot object, which is set by SetName(string).
214      */
215     std::string GetName() const;
216 
217     /**
218      * @brief Obtains the prompt tone of a NotificationSlot object, which is set by SetSound(Uri).
219      *
220      * @return Returns the prompt tone of the NotificationSlot object.
221      */
222     Uri GetSound() const;
223 
224     /**
225      * @brief Sets a prompt tone for a NotificationSlot object, which will be played after a notification is received.
226      * @note SetSound must be set before the NotificationHelper:AddNotificationSlot(NotificationSlot) method is called.
227      *       Otherwise, the settings will not take effect.
228      *
229      * @param sound Specifies the path for the prompt tone.
230      */
231     void SetSound(const Uri &sound);
232 
233     /**
234      * @brief Obtains the vibration style of notifications in this NotificationSlot.
235      *
236      * @return Returns the vibration style of this NotificationSlot.
237      */
238     std::vector<int64_t> GetVibrationStyle() const;
239 
240     /**
241      * @brief Sets the vibration style for notifications in this NotificationSlot.
242      * @note If an empty array or null is passed to this method, the system then calls
243      *       SetEnableVibration(bool) with the input parameter set to false.
244      *       If a valid value is passed to this method, the system calls SetEnableVibration(bool) with the input
245      *       parameter set to true. This method takes effect only before
246      *       NotificationHelper::AddNotificationSlot(NotificationSlot) is called.
247      *
248      * @param vibration Indicates the vibration style to set.
249      */
250     void SetVibrationStyle(const std::vector<int64_t> &vibration);
251 
252     /**
253      * @brief Obtains whether DND mode is bypassed for a NotificationSlot object,
254      * which is set by EnableBypassDnd(bool).
255      *
256      * @return Returns true if DND mode is bypassed; returns false otherwise.
257      */
258     bool IsEnableBypassDnd() const;
259 
260     /**
261      * @brief Sets whether to bypass Do not disturb (DND) mode in the system.
262      * @note The setting of EnableBypassDnd takes effect only when the Allow interruptions function
263      *       is enabled for an application in system notification settings.
264      *
265      * @param isBypassDnd Specifies whether to bypass DND mode for an application.
266      *                    If the value is true, DND mode is bypassed;
267      *                    if the value is false, DND mode is not bypassed.
268      */
269     void EnableBypassDnd(bool isBypassDnd);
270 
271     /**
272      * @brief Obtains the application icon badge status of a NotificationSlot object,
273      * which is set by EnableBadge(bool).
274      *
275      * @return Returns true if the application icon badge is enabled; returns false otherwise.
276      */
277     bool IsShowBadge() const;
278 
279     /**
280      * @brief Sets whether to display application icon badges (digits or dots in the corner of the application icon)
281      * on the home screen after a notification is received.
282      * @note EnableBadge must be set before the NotificationHelper:AddNotificationSlot(NotificationSlot) method is
283      * called. Otherwise, the settings will not take effect.
284      *
285      * @param isShowBadge Specifies whether to display the application icon badge.
286      *                    If the value is true, the application icon badge is enabled;
287      *                    if the value is false, the application icon badge is disabled..
288      */
289     void EnableBadge(bool isShowBadge);
290 
291     /**
292      * @brief Set whether the application slot enable.
293      * @note If the slot enable status is false, the notification cannot be publish.
294      *
295      * @param enabled Specifies whether to enable slot.
296      */
297     void SetEnable(bool enabled);
298 
299     /**
300      * @brief Obtains whether the application slot is enabled.
301      *
302      * @return Returns true if the slot enabled; returns false otherwise.
303      */
304     bool GetEnable() const;
305 
306     /**
307      * @brief Set whether the application slot is force control.
308      * @note If the slot is force control, the notification ability is not affected by the bundle.
309      *
310      * @param isForceControl Specifies whether is force control.
311      */
312     void SetForceControl(bool isForceControl);
313 
314     /**
315      * @brief Obtains whether the application slot is force control.
316      *
317      * @return Returns true if the slot is force control; returns false otherwise.
318      */
319     bool GetForceControl() const;
320 
321     /**
322      * @brief Sets the authorizedStatus of a NotificationSlot object.
323      * @note SetSlotFlags must be set before the NotificationHelper::AddNotificationSlot(NotificationSlot)
324      * method is called.
325      *       Otherwise, the settings will not take effect.
326      *
327      * @param slotFlags Specifies the authorizedStatus of the NotificationSlot object,
328      * @note which determines the notification authorized status.
329      * The value can be 0,1.
330      */
331     void SetAuthorizedStatus(int32_t status);
332 
333     /**
334      * @brief Obtains the authorizedStatus of a NotificationSlot object,
335      *        which is set by SetAuthorizedStatus(int32_t status).
336      *
337      * @return Returns the authorizedStatus of the NotificationSlot object.
338      */
339     int32_t GetAuthorizedStatus() const;
340 
341     /**
342      * @brief Add the authHintCnt of a NotificationSlot object.
343      * @note which determines the notification authorized hint count.
344      * The value can be 0,1.
345      */
346     void AddAuthHintCnt();
347 
348     /**
349      * @brief Set the authHintCnt of a NotificationSlot object.
350      * @param count Specifies the authHintCnt of the NotificationSlot object,
351      * @note which determines the notification authorized hint count.
352      * The value can be 0,1.
353      */
354     void SetAuthHintCnt(int32_t count);
355 
356     /**
357      * @brief Set reminder mode of a NotificationSlot object.
358      * @param reminderMode Specifies the reminder mode of the NotificationSlot object,
359      * @note which determines the notification reminder mode.
360      */
361     void SetReminderMode(uint32_t reminderMode);
362 
363     /**
364      * @brief Obtains the reminder mode of a NotificationSlot object
365      * @return Returns the reminder mode of the NotificationSlot object.
366      */
367     uint32_t GetReminderMode() const;
368 
369     /**
370      * @brief Obtains the authHintCnt of a NotificationSlot object, which is set by SetAuthHintCnt(int32_t count).
371      *
372      * @return Returns the authHintCnt of the NotificationSlot object.
373      */
374     int32_t GetAuthHintCnt() const;
375 
376     /**
377      * @brief Dumps a string representation of the object.
378      *
379      * @return Returns a string representation of the object.
380      */
381     std::string Dump() const;
382 
383     /**
384      * @brief Marshals a NotificationSlot object into a Parcel.
385      *
386      * @param parcel Indicates the Parcel object for marshalling.
387      * @return Returns true if the marshalling is successful; returns false otherwise.
388      */
389     virtual bool Marshalling(Parcel &parcel) const override;
390 
391     /**
392      * @brief Unmarshals a NotificationSlot object from a Parcel.
393      *
394      * @param parcel Indicates the Parcel object for unmarshalling.
395      * @return Returns the NotificationSlot object.
396      */
397     static NotificationSlot *Unmarshalling(Parcel &parcel);
398 
399     /**
400      * @brief convert string slottype to NotificationConstant slottype.
401      *
402      * @param strSlotType string slottype
403      * @param slotType NotificationConstant slottype
404      * @return Returns the result for converting string slottype to NotificationConstant slottype.
405      */
406     static bool GetSlotTypeByString(const std::string &strSlotType, NotificationConstant::SlotType &slotType);
407 
408 private:
409     /**
410      * @brief Read NotificationSlot object from a Parcel.
411      *
412      * @param parcel Indicates the Parcel object for unmarshalling.
413      * @return Returns true if succeed; returns false otherwise.
414      */
415     bool ReadFromParcel(Parcel &parcel);
416 
417     /**
418      * @brief Merge the contents of vector and output a string
419      *
420      * @param mergeVector Indicates the vector which will be merged
421      * @return Returns the string that has contents of the vector
422      */
423     std::string MergeVectorToString(const std::vector<int64_t> &mergeVector) const;
424 
425     /**
426      * @brief If string length exceed 1000 characters, the excessive part is automatically truncated.
427      *
428      * @param in Indicates the sting which will be truncated
429      * @return Returns the string that has been truncated.
430      */
431     std::string TruncateString(const std::string &in);
432 
433     /**
434      * @brief Sets the name of a NotificationSlot object.
435      * @note The setting of SetName is effective regardless of whether a NotificationSlot object has been created by
436      *       NotificationHelper:AddNotificationSlot(NotificationSlot).
437      *
438      * @param name Specifies the name of the NotificationSlot object.
439      *             The name is visible to users, and its length must not exceed 1000 characters
440      *             (the excessive part is automatically truncated).
441      */
442     void SetName(const std::string &name);
443 
444     /**
445      * @brief Obtains the default reminder mode of a NotificationSlot object
446      * @return Returns the default reminder mode of slot.
447      */
448     uint32_t GetDefaultReminderMode() const;
449 public:
450     constexpr static const char* SOCIAL_COMMUNICATION = "Social_communication";
451     constexpr static const char* SERVICE_REMINDER = "Service_reminder";
452     constexpr static const char* CONTENT_INFORMATION = "Content_information";
453     constexpr static const char* LIVE_VIEW = "Live_view";
454     constexpr static const char* CUSTOM_SERVICE = "Custom_service";
455     constexpr static const char* OTHER = "Other";
456     constexpr static const char* EMERGENCY_INFORMATION = "Emergency_information";
457     constexpr static const char* SPLIT_FLAG = "|";
458 
459 private:
460     std::string id_ {};
461     std::string name_ {};
462     bool isLightEnabled_ {false};
463     bool isVibrationEnabled_ {false};
464     bool isShowBadge_ {true};
465     bool isBypassDnd_ {false};
466     std::string description_ {};
467     int32_t lightColor_ {0};
468     NotificationLevel level_ {LEVEL_DEFAULT};
469     NotificationConstant::SlotType type_ {};
470     NotificationConstant::VisiblenessType lockScreenVisibleness_ {NotificationConstant::VisiblenessType::NO_OVERRIDE};
471     Uri sound_;
472     std::vector<int64_t> vibrationValues_ {};
473     bool enabled_ {true};
474     uint32_t slotFlags_{0};
475     bool isForceControl_ {false};
476     int32_t authorizedStatus_ {AuthorizedStatus::NOT_AUTHORIZED};
477     int32_t authHintCnt_ = {0};
478     uint32_t reminderMode_ = {INVALID_REMINDER_MODE};
479 
480     // no object in parcel
481     static constexpr int32_t VALUE_NULL = -1;
482     // object exist in parcel
483     static constexpr int32_t VALUE_OBJECT = 1;
484     static std::map<std::string, NotificationConstant::SlotType> convertStrToSlotType_;
485 };
486 }  // namespace Notification
487 }  // namespace OHOS
488 
489 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SLOT_H
490