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