• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class NotificationSlot : public Parcelable {
27 public:
28     enum NotificationLevel {
29         LEVEL_NONE,       // the notification function is disabled.
30         LEVEL_MIN,        // the notifications function is disabled on the notification panel,
31                           // with no banner or prompt tone
32         LEVEL_LOW,        // the notifications are displayed on the notification panel,
33                           // with no banner or prompt tone
34         LEVEL_DEFAULT,    // the notification function is enabled and notifications are displayed,
35                           // on the notification panel, with a banner and a prompt tone.
36         LEVEL_HIGH,       // the notifications are displayed on the notification panel,
37                           // with a banner and a prompt tone
38         LEVEL_UNDEFINED,  // the notification does not define an level.
39     };
40 
41     /**
42      * @brief A constructor used to initialize the type of a NotificationSlot object.
43      *
44      * @param type  Specifies the type of the NotificationSlot object,
45      */
46     NotificationSlot(NotificationConstant::SlotType type = NotificationConstant::SlotType::CUSTOM);
47 
48     ~NotificationSlot();
49 
50     /**
51      * @brief Obtains whether the notification light is enabled in a NotificationSlot object,
52      * which is set by SetEnableLight(bool).
53      *
54      * @return Returns true if the notification light is enabled; returns false otherwise.
55      */
56     bool CanEnableLight() const;
57 
58     /**
59      * @brief Specifies whether to enable the notification light when a notification is received on the device,
60      * provided that this device has a notification light.
61      * @note SetEnableLight must be set before the NotificationHelper:AddNotificationSlot(NotificationSlot) method is
62      * called. Otherwise, the settings will not take effect.
63      *
64      * @param isLightEnabled Specifies whether to enable the pulse notification light.
65      *                       The value true indicates to enable the notification light,
66      *                       and the value false indicates not to enable it.
67      */
68     void SetEnableLight(bool isLightEnabled);
69 
70     /**
71      * @brief Obtains the vibration status of a NotificationSlot object,
72      * which is set by SetEnableVibration(bool).
73      *
74      * @return Returns true if vibration is enabled; returns false otherwise.
75      */
76     bool CanVibrate() const;
77 
78     /**
79      * @brief Sets whether to enable vibration when a notification is received.
80      * @note SetEnableVibration(bool) must be set before the NotificationHelper::AddNotificationSlot(NotificationSlot)
81      * method is called. Otherwise, the settings will not take effect.
82      *
83      * @param vibration Indicates whether to enable vibration when a notification is received.
84      *                  If the value is true, vibration is enabled; if the value is false, vibration is disabled.
85      */
86     void SetEnableVibration(bool vibration);
87 
88     /**
89      * @brief Obtains the description of a NotificationSlot object, which is set by SetDescription(string).
90      *
91      * @return Returns the description of the NotificationSlot object.
92      */
93     std::string GetDescription() const;
94 
95     /**
96      * @brief Sets the description for a NotificationSlot object.
97      * @note The setting of setDescription is effective regardless of whether a NotificationSlot object has been created
98      * by NotificationHelper::AddNotificationSlot(NotificationSlot).
99      *
100      * @param description describes the NotificationSlot object.
101      *                    The description is visible to users and its length must not exceed 1000 characters
102      *                    (the excessive part is automatically truncated).
103      */
104     void SetDescription(const std::string &description);
105 
106     /**
107      * @brief Obtains the ID of a NotificationSlot object.
108      *
109      * @return Returns the ID of the NotificationSlot object,
110      *         which is set by NotificationSlot(string, string, NotificationLevel).
111      */
112     std::string GetId() const;
113 
114     /**
115      * @brief Obtains the color of the notification light in a NotificationSlot object,
116      * which is set by SetLedLightColor(int32_t).
117      *
118      * @return Returns the color of the notification light.
119      */
120     int32_t GetLedLightColor() const;
121 
122     /**
123      * @brief Sets the color of the notification light to flash when a notification is received on the device,
124      * provided that this device has a notification light and setEnableLight is called with the value true.
125      * @note SetLedLightColor must be set before the NotificationHelper::AddNotificationSlot(NotificationSlot) method is
126      * called. Otherwise, the settings will not take effect.
127      *
128      * @param color Indicates the color of the notification light.
129      */
130     void SetLedLightColor(int32_t color);
131 
132     /**
133      * @brief Obtains the level of a NotificationSlot object, which is set by SetLevel(NotificationLevel).
134      *
135      * @return Returns the level of the NotificationSlot object.
136      */
137     NotificationLevel GetLevel() const;
138 
139     /**
140      * @brief Sets the level of a NotificationSlot object.
141      * @note SetLevel must be set before the NotificationHelper::AddNotificationSlot(NotificationSlot) method is called.
142      *       Otherwise, the settings will not take effect.
143      *
144      * @param level Specifies the level of the NotificationSlot object, which determines the notification display
145      * effect. The value can be LEVEL_NONE, LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, or LEVEL_HIGH.
146      */
147     void SetLevel(NotificationLevel level);
148 
149     /**
150      * @brief Obtains the type of a NotificationSlot object, which is set by SetType(SlotType).
151      *
152      * @return Returns the Type of the NotificationSlot object.
153      */
154     NotificationConstant::SlotType GetType() const;
155 
156     /**
157      * @brief Sets the type of a NotificationSlot object.
158      * @note Settype must be set before the NotificationHelper::AddNotificationSlot(NotificationSlot) method is called.
159      *       Otherwise, the settings will not take effect.
160      *
161      * @param type Specifies the type of the NotificationSlot object, which determines the notification remind mode.
162      *        The value can be DEFAULT, SOCIAL_COMMUNICATION, SERVICE_REMINDER, CONTENT_INFORMATION, or OTHER.
163      */
164     void SetType(NotificationConstant::SlotType type);
165 
166     /**
167      * @brief Obtains the notification display effect of a NotificationSlot object on the lock screen,
168      * which is set by SetLockscreenVisibleness(int32_t).
169      * @note This method specifies different effects for displaying notifications on the lock screen in order to protect
170      * user privacy. The setting takes effect only when the lock screen notifications function is enabled for an
171      * application in system notification settings.
172      *
173      * @return Returns the notification display effect of the NotificationSlot object on the lock screen.
174      */
175     NotificationConstant::VisiblenessType GetLockScreenVisibleness() const;
176 
177     /**
178      * @brief Sets whether and how to display notifications on the lock screen.
179      *
180      * @param visibleness Specifies the notification display effect on the lock screen, which can be set to
181      *                    NO_OVERRIDE, PUBLIC, PRIVATE, or SECRET.
182      */
183     void SetLockscreenVisibleness(NotificationConstant::VisiblenessType visibleness);
184 
185     /**
186      * @brief Obtains the name of a NotificationSlot object.
187      *
188      * @return Returns the name of the NotificationSlot object, which is set by SetName(string).
189      */
190     std::string GetName() const;
191 
192     /**
193      * @brief Obtains the prompt tone of a NotificationSlot object, which is set by SetSound(Uri).
194      *
195      * @return Returns the prompt tone of the NotificationSlot object.
196      */
197     Uri GetSound() const;
198 
199     /**
200      * @brief Sets a prompt tone for a NotificationSlot object, which will be played after a notification is received.
201      * @note SetSound must be set before the NotificationHelper:AddNotificationSlot(NotificationSlot) method is called.
202      *       Otherwise, the settings will not take effect.
203      *
204      * @param sound Specifies the path for the prompt tone.
205      */
206     void SetSound(const Uri &sound);
207 
208     /**
209      * @brief Obtains the vibration style of notifications in this NotificationSlot.
210      *
211      * @return Returns the vibration style of this NotificationSlot.
212      */
213     std::vector<int64_t> GetVibrationStyle() const;
214 
215     /**
216      * @brief Sets the vibration style for notifications in this NotificationSlot.
217      * @note If an empty array or null is passed to this method, the system then calls
218      *       SetEnableVibration(bool) with the input parameter set to false.
219      *       If a valid value is passed to this method, the system calls SetEnableVibration(bool) with the input
220      *       parameter set to true. This method takes effect only before
221      *       NotificationHelper::AddNotificationSlot(NotificationSlot) is called.
222      *
223      * @param vibration Indicates the vibration style to set.
224      */
225     void SetVibrationStyle(const std::vector<int64_t> &vibration);
226 
227     /**
228      * @brief Obtains whether DND mode is bypassed for a NotificationSlot object,
229      * which is set by EnableBypassDnd(bool).
230      *
231      * @return Returns true if DND mode is bypassed; returns false otherwise.
232      */
233     bool IsEnableBypassDnd() const;
234 
235     /**
236      * @brief Sets whether to bypass Do not disturb (DND) mode in the system.
237      * @note The setting of EnableBypassDnd takes effect only when the Allow interruptions function
238      *       is enabled for an application in system notification settings.
239      *
240      * @param isBypassDnd Specifies whether to bypass DND mode for an application.
241      *                    If the value is true, DND mode is bypassed;
242      *                    if the value is false, DND mode is not bypassed.
243      */
244     void EnableBypassDnd(bool isBypassDnd);
245 
246     /**
247      * @brief Obtains the application icon badge status of a NotificationSlot object,
248      * which is set by EnableBadge(bool).
249      *
250      * @return Returns true if the application icon badge is enabled; returns false otherwise.
251      */
252     bool IsShowBadge() const;
253 
254     /**
255      * @brief Sets whether to display application icon badges (digits or dots in the corner of the application icon)
256      * on the home screen after a notification is received.
257      * @note EnableBadge must be set before the NotificationHelper:AddNotificationSlot(NotificationSlot) method is
258      * called. Otherwise, the settings will not take effect.
259      *
260      * @param isShowBadge Specifies whether to display the application icon badge.
261      *                    If the value is true, the application icon badge is enabled;
262      *                    if the value is false, the application icon badge is disabled..
263      */
264     void EnableBadge(bool isShowBadge);
265 
266     /**
267      * @brief Set whether the application slot enable.
268      * @note If the slot enable status is false, the notification cannot be publish.
269      *
270      * @param enabled Specifies whether to enable slot.
271      */
272     void SetEnable(bool enabled);
273 
274     /**
275      * @brief Obtains whether the application slot is enabled.
276      *
277      * @return Returns true if the slot enabled; returns false otherwise.
278      */
279     bool GetEnable() const;
280 
281     /**
282      * @brief Dumps a string representation of the object.
283      *
284      * @return Returns a string representation of the object.
285      */
286     std::string Dump() const;
287 
288     /**
289      * @brief Marshals a NotificationSlot object into a Parcel.
290      *
291      * @param parcel Indicates the Parcel object for marshalling.
292      * @return Returns true if the marshalling is successful; returns false otherwise.
293      */
294     virtual bool Marshalling(Parcel &parcel) const override;
295 
296     /**
297      * @brief Unmarshals a NotificationSlot object from a Parcel.
298      *
299      * @param parcel Indicates the Parcel object for unmarshalling.
300      * @return Returns the NotificationSlot object.
301      */
302     static NotificationSlot *Unmarshalling(Parcel &parcel);
303 
304 private:
305     /**
306      * @brief Read NotificationSlot object from a Parcel.
307      *
308      * @param parcel Indicates the Parcel object for unmarshalling.
309      * @return Returns true if succeed; returns false otherwise.
310      */
311     bool ReadFromParcel(Parcel &parcel);
312 
313     /**
314      * @brief Merge the contents of vector and output a string
315      *
316      * @param mergeVector Indicates the vector which will be merged
317      * @return Returns the string that has contents of the vector
318      */
319     std::string MergeVectorToString(const std::vector<int64_t> &mergeVector) const;
320 
321     /**
322      * @brief If string length exceed 1000 characters, the excessive part is automatically truncated.
323      *
324      * @param in Indicates the sting which will be truncated
325      * @return Returns the string that has been truncated.
326      */
327     std::string TruncateString(const std::string &in);
328 
329     /**
330      * @brief Sets the name of a NotificationSlot object.
331      * @note The setting of SetName is effective regardless of whether a NotificationSlot object has been created by
332      *       NotificationHelper:AddNotificationSlot(NotificationSlot).
333      *
334      * @param name Specifies the name of the NotificationSlot object.
335      *             The name is visible to users, and its length must not exceed 1000 characters
336      *             (the excessive part is automatically truncated).
337      */
338     void SetName(const std::string &name);
339 
340 private:
341     std::string id_ {};
342     std::string name_ {};
343     bool isLightEnabled_ {false};
344     bool isVibrationEnabled_ {false};
345     bool isShowBadge_ {true};
346     bool isBypassDnd_ {false};
347     std::string description_ {};
348     int32_t lightColor_ {0};
349     NotificationLevel level_ {LEVEL_DEFAULT};
350     NotificationConstant::SlotType type_ {};
351     NotificationConstant::VisiblenessType lockScreenVisibleness_ {NotificationConstant::VisiblenessType::NO_OVERRIDE};
352     Uri sound_;
353     std::vector<int64_t> vibrationValues_ {};
354     bool enabled_ {true};
355 
356     // no object in parcel
357     static constexpr int32_t VALUE_NULL = -1;
358     // object exist in parcel
359     static constexpr int32_t VALUE_OBJECT = 1;
360 };
361 }  // namespace Notification
362 }  // namespace OHOS
363 
364 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SLOT_H