1 /* 2 * Copyright (c) 2023 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_CONTENT_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_CONTENT_H 18 19 #include "notification_basic_content.h" 20 #include "notification_conversational_content.h" 21 #include "notification_json_convert.h" 22 #include "notification_long_text_content.h" 23 #include "notification_media_content.h" 24 #include "notification_multiline_content.h" 25 #include "notification_normal_content.h" 26 #include "notification_picture_content.h" 27 #include "notification_live_view_content.h" 28 #include "notification_local_live_view_content.h" 29 #include "parcel.h" 30 31 namespace OHOS { 32 namespace Notification { 33 class NotificationContent : public Parcelable, public NotificationJsonConvertionBase { 34 public: 35 enum class Type { 36 /** 37 * invalid type 38 */ 39 NONE, 40 /** 41 * Indicates basic notifications. Such notifications are created using NotificationNormalContent. 42 */ 43 BASIC_TEXT, 44 /** 45 * Indicates notifications that include a conversation among multiple users. 46 * Such notifications are created using NotificationConversationalContent. 47 */ 48 CONVERSATION, 49 /** 50 * Indicates notifications that include long text. 51 * Such notifications are created using NotificationLongTextContent. 52 */ 53 LONG_TEXT, 54 /** 55 * Indicates notifications that include media playback sessions. 56 * Such notifications are created using NotificationMediaContent. 57 */ 58 MEDIA, 59 /** 60 * Indicates notifications that include multiple independent lines of text. 61 * Such notifications are created using NotificationMultiLineContent. 62 */ 63 MULTILINE, 64 /** 65 * Indicates notifications that include a picture. 66 * Such notifications are created using NotificationPictureContent. 67 */ 68 PICTURE, 69 /** 70 * Indicates notifications that include local live view. 71 * Such notifications are created using NotificationLocalLiveViewContent. 72 */ 73 LOCAL_LIVE_VIEW, 74 /** 75 * Indicates notifications that include a live view. 76 * Such notifications are created using NotificationLiveViewContent. 77 */ 78 LIVE_VIEW, 79 /** 80 * invalid type 81 * It is used as the upper limit of the enumerated value. 82 */ 83 ILLEGAL_TYPE 84 }; 85 86 /** 87 * @brief A constructor used to create a NotificationNormalContent instance (obtained by calling 88 * GetNotificationContent()) and set the content type to NotificationContent::Type::BASIC_TEXT (obtained by calling 89 * GetContentType()). 90 * 91 * @param normalContent Indicates the NotificationNormalContent object. 92 */ 93 explicit NotificationContent(const std::shared_ptr<NotificationNormalContent> &normalContent); 94 95 /** 96 * @brief A constructor used to create a NotificationLongTextContent instance (obtained by calling 97 * GetNotificationContent()) and set the content type to NotificationContent::Type::LONG_TEXT (obtained by calling 98 * GetContentType()). 99 * 100 * @param longTextContent Indicates the NotificationLongTextContent object. 101 */ 102 explicit NotificationContent(const std::shared_ptr<NotificationLongTextContent> &longTextContent); 103 104 /** 105 * @brief A constructor used to create a NotificationPictureContent instance (obtained by calling 106 * GetNotificationContent()) and set the content type to NotificationContent::Type::PICTURE (obtained by calling 107 * GetContentType()). 108 * 109 * @param pictureContent Indicates the NotificationPictureContent object. 110 */ 111 explicit NotificationContent(const std::shared_ptr<NotificationPictureContent> &pictureContent); 112 113 /** 114 * @brief A constructor used to create a NotificationConversationalContent instance (obtained by calling 115 * GetNotificationContent()) and set the content type to NotificationContent::Type::CONVERSATION (obtained by 116 * calling GetContentType()). 117 * 118 * @param conversationContent Indicates the NotificationConversationalContent object. 119 */ 120 explicit NotificationContent(const std::shared_ptr<NotificationConversationalContent> &conversationContent); 121 122 /** 123 * @brief A constructor used to create a NotificationMultiLineContent instance (obtained by calling 124 * GetNotificationContent()) and set the content type to NotificationContent::Type::MULTILINE (obtained by calling 125 * GetContentType()). 126 * 127 * @param multiLineContent Indicates the NotificationMultiLineContent object. 128 */ 129 explicit NotificationContent(const std::shared_ptr<NotificationMultiLineContent> &multiLineContent); 130 131 /** 132 * @brief A constructor used to create a NotificationMediaContent instance (obtained by calling 133 * GetNotificationContent()) and set the content type to NotificationContent::Type::MEDIA (obtained by calling 134 * GetContentType()). 135 * 136 * @param mediaContent Indicates the NotificationMediaContent object. 137 */ 138 explicit NotificationContent(const std::shared_ptr<NotificationMediaContent> &mediaContent); 139 140 /** 141 * @brief A constructor used to create a NotificationLocalLiveViewContent instance (obtained by calling 142 * GetNotificationContent()) and set the content type to NotificationContent::Type::LOCAL_LIVE_VIEW 143 * (obtained by calling GetContentType()). 144 * 145 * @param localLiveViewContent Indicates the NotificationLocalLiveViewContent object. 146 */ 147 explicit NotificationContent(const std::shared_ptr<NotificationLocalLiveViewContent> &localLiveViewContent); 148 149 /** 150 * @brief A constructor used to create a NotificationLiveViewContent instance (obtained by calling 151 * GetNotificationContent()) and set the content type to NotificationContent::Type::LIVE_VIEW (obtained by calling 152 * GetContentType()). 153 * 154 * @param liveViewContent Indicates the NotificationMediaContent object. 155 */ 156 explicit NotificationContent(const std::shared_ptr<NotificationLiveViewContent> &liveViewContent); 157 virtual ~NotificationContent(); 158 159 /** 160 * @brief Obtains the type value of the notification content. 161 * 162 * @return Returns the type value of the current content, which can be 163 * NotificationContent::Type::BASIC_TEXT, 164 * NotificationContent::Type::LONG_TEXT, 165 * NotificationContent::Type::PICTURE, 166 * NotificationContent::Type::CONVERSATION, 167 * NotificationContent::Type::MULTILINE, 168 * NotificationContent::Type::MEDIA, 169 * NotificationContent::Type::LIVE_VIEW, or 170 * NotificationContent::Type::LOCAL_LIVE_VIEW 171 */ 172 NotificationContent::Type GetContentType() const; 173 174 /** 175 * @brief Obtains the object matching the current notification content. 176 * 177 * @return Returns the content object, which can be NotificationLongTextContent, 178 * NotificationNormalContent, 179 * NotificationPictureContent, 180 * NotificationConversationalContent, 181 * NotificationMultiLineContent, or 182 * NotificationMediaContent. 183 */ 184 std::shared_ptr<NotificationBasicContent> GetNotificationContent() const; 185 186 /** 187 * @brief Returns a string representation of the object. 188 * 189 * @return Returns a string representation of the object. 190 */ 191 std::string Dump(); 192 193 /** 194 * @brief Converts a NotificationContent object into a Json. 195 * 196 * @param jsonObject Indicates the Json object. 197 * @return Returns true if succeed; returns false otherwise. 198 */ 199 bool ToJson(nlohmann::json &jsonObject) const override; 200 201 /** 202 * @brief Creates a NotificationContent object from a Json. 203 * 204 * @param jsonObject Indicates the Json object. 205 * @return Returns the NotificationContent. 206 */ 207 static NotificationContent *FromJson(const nlohmann::json &jsonObject); 208 209 /** 210 * @brief Marshal a object into a Parcel. 211 * 212 * @param parcel Indicates the object into the parcel. 213 * @return Returns true if succeed; returns false otherwise. 214 */ 215 virtual bool Marshalling(Parcel &parcel) const override; 216 217 /** 218 * @brief Unmarshal object from a Parcel. 219 * 220 * @param parcel Indicates the parcel object. 221 * @return Returns the NotificationContent. 222 */ 223 static NotificationContent *Unmarshalling(Parcel &parcel); 224 225 /** 226 * @brief convert string contentType to NotificationContent contentType. 227 * 228 * @param strContentType string contentType 229 * @param contentType NotificationContent contentType 230 * @return Returns the result for converting string contentType to NotificationContent contentType. 231 */ 232 static bool GetContentTypeByString(const std::string &strContentType, NotificationContent::Type &contentType); 233 ResetToBasicContent()234 inline void ResetToBasicContent() const 235 { 236 contentType_ = NotificationContent::Type::BASIC_TEXT; 237 content_->SetContentType(static_cast<int32_t>(NotificationContent::Type::BASIC_TEXT)); 238 } 239 240 private: 241 NotificationContent() = default; 242 243 /** 244 * @brief Read data from a Parcel. 245 * 246 * @param parcel Indicates the parcel object. 247 * @return Returns true if read success; returns false otherwise. 248 */ 249 bool ReadFromParcel(Parcel &parcel); 250 251 /** 252 * @brief Convert JSON object to NotificationContent object. 253 * 254 * @param target Indicates the NotificationContent object. 255 * @param jsonObject Indicates the JSON object. 256 * @return Returns true if the conversion is successful; returns false otherwise. 257 */ 258 static bool ConvertJsonToContent(NotificationContent *target, const nlohmann::json &jsonObject); 259 260 public: 261 constexpr static const char* CONTENT_TYPE_NONE = "None"; 262 constexpr static const char* CONTENT_TYPE_BASIC_TEXT = "Basic_text"; 263 constexpr static const char* CONTENT_TYPE_CONVERSATION = "Conversation"; 264 constexpr static const char* CONTENT_TYPE_LONG_TEXT = "Long_text"; 265 constexpr static const char* CONTENT_TYPE_MEDIA = "Media"; 266 constexpr static const char* CONTENT_TYPE_MULTILINE = "Mutiline"; 267 constexpr static const char* CONTENT_TYPE_PICTURE = "Picture"; 268 constexpr static const char* CONTENT_TYPE_LOCAL_LIVE_VIEW = "Local_live_view"; 269 constexpr static const char* CONTENT_TYPE_LIVE_VIEW = "Live_view"; 270 271 private: 272 mutable NotificationContent::Type contentType_ {NotificationContent::Type::NONE}; 273 std::shared_ptr<NotificationBasicContent> content_ {}; 274 static std::map<std::string, NotificationContent::Type> convertStrToContentType_; 275 }; 276 } // namespace Notification 277 } // namespace OHOS 278 279 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_CONTENT_H 280