• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CONTENT_H
17 #define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_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      * A constructor used to create a NotificationNormalContent instance (obtained by calling GetNotificationContent())
71      * and set the content type to NotificationContent::Type::BASIC_TEXT (obtained by calling GetContentType()).
72      * @param normalContent Indicates the NotificationNormalContent object.
73      */
74     explicit NotificationContent(const std::shared_ptr<NotificationNormalContent> &normalContent);
75 
76     /**
77      * A constructor used to create a NotificationLongTextContent instance (obtained by calling
78      * GetNotificationContent()) and set the content type to NotificationContent::Type::LONG_TEXT (obtained by calling
79      * GetContentType()).
80      * @param longTextContent Indicates the NotificationLongTextContent object.
81      */
82     explicit NotificationContent(const std::shared_ptr<NotificationLongTextContent> &longTextContent);
83 
84     /**
85      * A constructor used to create a NotificationPictureContent instance (obtained by calling GetNotificationContent())
86      * and set the content type to NotificationContent::Type::PICTURE (obtained by calling GetContentType()).
87      * @param pictureContent Indicates the NotificationPictureContent object.
88      */
89     explicit NotificationContent(const std::shared_ptr<NotificationPictureContent> &pictureContent);
90 
91     /**
92      * A constructor used to create a NotificationConversationalContent instance
93      * (obtained by calling GetNotificationContent()) and set the content type to
94      * NotificationContent::Type::CONVERSATION (obtained by calling GetContentType()).
95      * @param conversationContent Indicates the NotificationConversationalContent object.
96      */
97     explicit NotificationContent(const std::shared_ptr<NotificationConversationalContent> &conversationContent);
98 
99     /**
100      * A constructor used to create a NotificationMultiLineContent instance
101      * (obtained by calling GetNotificationContent()) and set the content type to
102      * NotificationContent::Type::MULTILINE (obtained by calling GetContentType()).
103      * @param multiLineContent Indicates the NotificationMultiLineContent object.
104      */
105     explicit NotificationContent(const std::shared_ptr<NotificationMultiLineContent> &multiLineContent);
106 
107     /**
108      * A constructor used to create a NotificationMediaContent instance
109      * (obtained by calling GetNotificationContent()) and set the content type to
110      * NotificationContent::Type::MEDIA (obtained by calling GetContentType()).
111      * @param mediaContent Indicates the NotificationMediaContent object.
112      */
113     explicit NotificationContent(const std::shared_ptr<NotificationMediaContent> &mediaContent);
114 
115     /**
116      * Default deconstructor used to deconstruct.
117      */
118     virtual ~NotificationContent();
119 
120     /**
121      * Obtains the type value of the notification content.
122      * @return the type value of the current content, which can be
123      * NotificationContent::Type::BASIC_TEXT,
124      * NotificationContent::Type::LONG_TEXT,
125      * NotificationContent::Type::PICTURE,
126      * NotificationContent::Type::CONVERSATION,
127      * NotificationContent::Type::MULTILINE, or
128      * NotificationContent::Type::MEDIA.
129      */
130     NotificationContent::Type GetContentType() const;
131 
132     /**
133      * Obtains the object matching the current notification content.
134      * @return the content object, which can be NotificationLongTextContent,
135      * NotificationNormalContent,
136      * NotificationPictureContent,
137      * NotificationConversationalContent,
138      * NotificationMultiLineContent, or
139      * NotificationMediaContent.
140      */
141     std::shared_ptr<NotificationBasicContent> GetNotificationContent() const;
142 
143     /**
144      * Returns a string representation of the object.
145      * @return a string representation of the object.
146      */
147     std::string Dump();
148 
149     /**
150      * Converts a NotificationContent object into a Json.
151      * @param jsonObject Indicates the Json object.
152      */
153     bool ToJson(nlohmann::json &jsonObject) const override;
154 
155     /**
156      * Creates a NotificationContent object from a Json.
157      * @param jsonObject Indicates the Json object.
158      * @return the NotificationContent.
159      */
160     static NotificationContent *FromJson(const nlohmann::json &jsonObject);
161 
162     /**
163      * Marshal a object into a Parcel.
164      * @param parcel the object into the parcel
165      */
166     virtual bool Marshalling(Parcel &parcel) const override;
167 
168     /**
169      * Unmarshal object from a Parcel.
170      * @return the NotificationContent
171      */
172     static NotificationContent *Unmarshalling(Parcel &parcel);
173 
174 private:
175     /**
176      * Default constructor used to create an empty NotificationContent instance.
177      */
178     NotificationContent() = default;
179 
180     /**
181      * Read data from a Parcel.
182      * @param parcel the parcel
183      * @return true if read success; returns false otherwise.
184      */
185     bool ReadFromParcel(Parcel &parcel);
186 
187     static bool ConvertJsonToContent(NotificationContent *target, const nlohmann::json &jsonObject);
188 
189 private:
190     NotificationContent::Type contentType_ {NotificationContent::Type::NONE};
191     std::shared_ptr<NotificationBasicContent> content_ {};
192 };
193 }  // namespace Notification
194 }  // namespace OHOS
195 
196 #endif  // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_CONTENT_H
197