• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
81     /**
82      * @brief A constructor used to create a NotificationNormalContent instance (obtained by calling
83      * GetNotificationContent()) and set the content type to NotificationContent::Type::BASIC_TEXT (obtained by calling
84      * GetContentType()).
85      *
86      * @param normalContent Indicates the NotificationNormalContent object.
87      */
88     explicit NotificationContent(const std::shared_ptr<NotificationNormalContent> &normalContent);
89 
90     /**
91      * @brief A constructor used to create a NotificationLongTextContent instance (obtained by calling
92      * GetNotificationContent()) and set the content type to NotificationContent::Type::LONG_TEXT (obtained by calling
93      * GetContentType()).
94      *
95      * @param longTextContent Indicates the NotificationLongTextContent object.
96      */
97     explicit NotificationContent(const std::shared_ptr<NotificationLongTextContent> &longTextContent);
98 
99     /**
100      * @brief A constructor used to create a NotificationPictureContent instance (obtained by calling
101      * GetNotificationContent()) and set the content type to NotificationContent::Type::PICTURE (obtained by calling
102      * GetContentType()).
103      *
104      * @param pictureContent Indicates the NotificationPictureContent object.
105      */
106     explicit NotificationContent(const std::shared_ptr<NotificationPictureContent> &pictureContent);
107 
108     /**
109      * @brief A constructor used to create a NotificationConversationalContent instance (obtained by calling
110      * GetNotificationContent()) and set the content type to NotificationContent::Type::CONVERSATION (obtained by
111      * calling GetContentType()).
112      *
113      * @param conversationContent Indicates the NotificationConversationalContent object.
114      */
115     explicit NotificationContent(const std::shared_ptr<NotificationConversationalContent> &conversationContent);
116 
117     /**
118      * @brief A constructor used to create a NotificationMultiLineContent instance (obtained by calling
119      * GetNotificationContent()) and set the content type to NotificationContent::Type::MULTILINE (obtained by calling
120      * GetContentType()).
121      *
122      * @param multiLineContent Indicates the NotificationMultiLineContent object.
123      */
124     explicit NotificationContent(const std::shared_ptr<NotificationMultiLineContent> &multiLineContent);
125 
126     /**
127      * @brief A constructor used to create a NotificationMediaContent instance (obtained by calling
128      * GetNotificationContent()) and set the content type to NotificationContent::Type::MEDIA (obtained by calling
129      * GetContentType()).
130      *
131      * @param mediaContent Indicates the NotificationMediaContent object.
132      */
133     explicit NotificationContent(const std::shared_ptr<NotificationMediaContent> &mediaContent);
134 
135     /**
136      * @brief A constructor used to create a NotificationLocalLiveViewContent instance (obtained by calling
137      * GetNotificationContent()) and set the content type to NotificationContent::Type::LOCAL_LIVE_VIEW
138      * (obtained by calling GetContentType()).
139      *
140      * @param localLiveViewContent Indicates the NotificationLocalLiveViewContent object.
141      */
142     explicit NotificationContent(const std::shared_ptr<NotificationLocalLiveViewContent> &localLiveViewContent);
143 
144     /**
145      * @brief A constructor used to create a NotificationLiveViewContent instance (obtained by calling
146      * GetNotificationContent()) and set the content type to NotificationContent::Type::LIVE_VIEW (obtained by calling
147      * GetContentType()).
148      *
149      * @param liveViewContent Indicates the NotificationMediaContent object.
150      */
151     explicit NotificationContent(const std::shared_ptr<NotificationLiveViewContent> &liveViewContent);
152     virtual ~NotificationContent();
153 
154     /**
155      * @brief Obtains the type value of the notification content.
156      *
157      * @return Returns the type value of the current content, which can be
158      * NotificationContent::Type::BASIC_TEXT,
159      * NotificationContent::Type::LONG_TEXT,
160      * NotificationContent::Type::PICTURE,
161      * NotificationContent::Type::CONVERSATION,
162      * NotificationContent::Type::MULTILINE,
163      * NotificationContent::Type::MEDIA,
164      * NotificationContent::Type::LIVE_VIEW, or
165      * NotificationContent::Type::LOCAL_LIVE_VIEW
166      */
167     NotificationContent::Type GetContentType() const;
168 
169     /**
170      * @brief Obtains the object matching the current notification content.
171      *
172      * @return Returns the content object, which can be NotificationLongTextContent,
173      * NotificationNormalContent,
174      * NotificationPictureContent,
175      * NotificationConversationalContent,
176      * NotificationMultiLineContent, or
177      * NotificationMediaContent.
178      */
179     std::shared_ptr<NotificationBasicContent> GetNotificationContent() const;
180 
181     /**
182      * @brief Returns a string representation of the object.
183      *
184      * @return Returns a string representation of the object.
185      */
186     std::string Dump();
187 
188     /**
189      * @brief Converts a NotificationContent object into a Json.
190      *
191      * @param jsonObject Indicates the Json object.
192      * @return Returns true if succeed; returns false otherwise.
193      */
194     bool ToJson(nlohmann::json &jsonObject) const override;
195 
196     /**
197      * @brief Creates a NotificationContent object from a Json.
198      *
199      * @param jsonObject Indicates the Json object.
200      * @return Returns the NotificationContent.
201      */
202     static NotificationContent *FromJson(const nlohmann::json &jsonObject);
203 
204     /**
205      * @brief Marshal a object into a Parcel.
206      *
207      * @param parcel Indicates the object into the parcel.
208      * @return Returns true if succeed; returns false otherwise.
209      */
210     virtual bool Marshalling(Parcel &parcel) const override;
211 
212     /**
213      * @brief Unmarshal object from a Parcel.
214      *
215      * @param parcel Indicates the parcel object.
216      * @return Returns the NotificationContent.
217      */
218     static NotificationContent *Unmarshalling(Parcel &parcel);
219 
220 private:
221     NotificationContent() = default;
222 
223     /**
224      * @brief Read data from a Parcel.
225      *
226      * @param parcel Indicates the parcel object.
227      * @return Returns true if read success; returns false otherwise.
228      */
229     bool ReadFromParcel(Parcel &parcel);
230 
231     /**
232      * @brief Convert JSON object to NotificationContent object.
233      *
234      * @param target Indicates the NotificationContent object.
235      * @param jsonObject Indicates the JSON object.
236      * @return Returns true if the conversion is successful; returns false otherwise.
237      */
238     static bool ConvertJsonToContent(NotificationContent *target, const nlohmann::json &jsonObject);
239 
240 private:
241     NotificationContent::Type contentType_ {NotificationContent::Type::NONE};
242     std::shared_ptr<NotificationBasicContent> content_ {};
243 };
244 }  // namespace Notification
245 }  // namespace OHOS
246 
247 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_CONTENT_H
248