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_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 "parcel.h" 28 29 namespace OHOS { 30 namespace Notification { 31 class NotificationContent : public Parcelable, public NotificationJsonConvertionBase { 32 public: 33 enum class Type { 34 /** 35 * invalid type 36 */ 37 NONE, 38 /** 39 * Indicates basic notifications. Such notifications are created using NotificationNormalContent. 40 */ 41 BASIC_TEXT, 42 /** 43 * Indicates notifications that include a conversation among multiple users. 44 * Such notifications are created using NotificationConversationalContent. 45 */ 46 CONVERSATION, 47 /** 48 * Indicates notifications that include long text. 49 * Such notifications are created using NotificationLongTextContent. 50 */ 51 LONG_TEXT, 52 /** 53 * Indicates notifications that include media playback sessions. 54 * Such notifications are created using NotificationMediaContent. 55 */ 56 MEDIA, 57 /** 58 * Indicates notifications that include multiple independent lines of text. 59 * Such notifications are created using NotificationMultiLineContent. 60 */ 61 MULTILINE, 62 /** 63 * Indicates notifications that include a picture. 64 * Such notifications are created using NotificationPictureContent. 65 */ 66 PICTURE 67 }; 68 69 /** 70 * @brief A constructor used to create a NotificationNormalContent instance (obtained by calling 71 * GetNotificationContent()) and set the content type to NotificationContent::Type::BASIC_TEXT (obtained by calling 72 * GetContentType()). 73 * 74 * @param normalContent Indicates the NotificationNormalContent object. 75 */ 76 explicit NotificationContent(const std::shared_ptr<NotificationNormalContent> &normalContent); 77 78 /** 79 * @brief A constructor used to create a NotificationLongTextContent instance (obtained by calling 80 * GetNotificationContent()) and set the content type to NotificationContent::Type::LONG_TEXT (obtained by calling 81 * GetContentType()). 82 * 83 * @param longTextContent Indicates the NotificationLongTextContent object. 84 */ 85 explicit NotificationContent(const std::shared_ptr<NotificationLongTextContent> &longTextContent); 86 87 /** 88 * @brief A constructor used to create a NotificationPictureContent instance (obtained by calling 89 * GetNotificationContent()) and set the content type to NotificationContent::Type::PICTURE (obtained by calling 90 * GetContentType()). 91 * 92 * @param pictureContent Indicates the NotificationPictureContent object. 93 */ 94 explicit NotificationContent(const std::shared_ptr<NotificationPictureContent> &pictureContent); 95 96 /** 97 * @brief A constructor used to create a NotificationConversationalContent instance (obtained by calling 98 * GetNotificationContent()) and set the content type to NotificationContent::Type::CONVERSATION (obtained by 99 * calling GetContentType()). 100 * 101 * @param conversationContent Indicates the NotificationConversationalContent object. 102 */ 103 explicit NotificationContent(const std::shared_ptr<NotificationConversationalContent> &conversationContent); 104 105 /** 106 * @brief A constructor used to create a NotificationMultiLineContent instance (obtained by calling 107 * GetNotificationContent()) and set the content type to NotificationContent::Type::MULTILINE (obtained by calling 108 * GetContentType()). 109 * 110 * @param multiLineContent Indicates the NotificationMultiLineContent object. 111 */ 112 explicit NotificationContent(const std::shared_ptr<NotificationMultiLineContent> &multiLineContent); 113 114 /** 115 * @brief A constructor used to create a NotificationMediaContent instance (obtained by calling 116 * GetNotificationContent()) and set the content type to NotificationContent::Type::MEDIA (obtained by calling 117 * GetContentType()). 118 * 119 * @param mediaContent Indicates the NotificationMediaContent object. 120 */ 121 explicit NotificationContent(const std::shared_ptr<NotificationMediaContent> &mediaContent); 122 123 virtual ~NotificationContent(); 124 125 /** 126 * @brief Obtains the type value of the notification content. 127 * 128 * @return Returns the type value of the current content, which can be 129 * NotificationContent::Type::BASIC_TEXT, 130 * NotificationContent::Type::LONG_TEXT, 131 * NotificationContent::Type::PICTURE, 132 * NotificationContent::Type::CONVERSATION, 133 * NotificationContent::Type::MULTILINE, or 134 * NotificationContent::Type::MEDIA. 135 */ 136 NotificationContent::Type GetContentType() const; 137 138 /** 139 * @brief Obtains the object matching the current notification content. 140 * 141 * @return Returns the content object, which can be NotificationLongTextContent, 142 * NotificationNormalContent, 143 * NotificationPictureContent, 144 * NotificationConversationalContent, 145 * NotificationMultiLineContent, or 146 * NotificationMediaContent. 147 */ 148 std::shared_ptr<NotificationBasicContent> GetNotificationContent() const; 149 150 /** 151 * @brief Returns a string representation of the object. 152 * 153 * @return Returns a string representation of the object. 154 */ 155 std::string Dump(); 156 157 /** 158 * @brief Converts a NotificationContent object into a Json. 159 * 160 * @param jsonObject Indicates the Json object. 161 * @return Returns true if succeed; returns false otherwise. 162 */ 163 bool ToJson(nlohmann::json &jsonObject) const override; 164 165 /** 166 * @brief Creates a NotificationContent object from a Json. 167 * 168 * @param jsonObject Indicates the Json object. 169 * @return Returns the NotificationContent. 170 */ 171 static NotificationContent *FromJson(const nlohmann::json &jsonObject); 172 173 /** 174 * @brief Marshal a object into a Parcel. 175 * 176 * @param parcel Indicates the object into the parcel. 177 * @return Returns true if succeed; returns false otherwise. 178 */ 179 virtual bool Marshalling(Parcel &parcel) const override; 180 181 /** 182 * @brief Unmarshal object from a Parcel. 183 * 184 * @param parcel Indicates the parcel object. 185 * @return Returns the NotificationContent. 186 */ 187 static NotificationContent *Unmarshalling(Parcel &parcel); 188 189 private: 190 NotificationContent() = default; 191 192 /** 193 * @brief Read data from a Parcel. 194 * 195 * @param parcel Indicates the parcel object. 196 * @return Returns true if read success; returns false otherwise. 197 */ 198 bool ReadFromParcel(Parcel &parcel); 199 200 /** 201 * @brief Convert JSON object to NotificationContent object. 202 * 203 * @param target Indicates the NotificationContent object. 204 * @param jsonObject Indicates the JSON object. 205 * @return Returns true if the conversion is successful; returns false otherwise. 206 */ 207 static bool ConvertJsonToContent(NotificationContent *target, const nlohmann::json &jsonObject); 208 209 private: 210 NotificationContent::Type contentType_ {NotificationContent::Type::NONE}; 211 std::shared_ptr<NotificationBasicContent> content_ {}; 212 }; 213 } // namespace Notification 214 } // namespace OHOS 215 216 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_CONTENT_H 217