1 /*
2  * Copyright (c) 2024 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 OHOS_NOTIFICATION_UTILS_H
17 #define OHOS_NOTIFICATION_UTILS_H
18 
19 #include "cj_ffi/cj_common_ffi.h"
20 #include "cj_lambda.h"
21 #include "notification_constant.h"
22 #include "notification_capsule.h"
23 #include "notification_slot.h"
24 #include "notification_request.h"
25 #include "notification_constant.h"
26 #include "notification_content.h"
27 #include "notification_flags.h"
28 #include "notification_helper.h"
29 #include "notification_multiline_content.h"
30 #include "notification_normal_content.h"
31 #include "notification_picture_content.h"
32 #include "notification_long_text_content.h"
33 #include "notification_local_live_view_button.h"
34 #include "notification_local_live_view_content.h"
35 #include "notification_progress.h"
36 #include "notification_time.h"
37 #include "pixel_map_impl.h"
38 
39 #include "ans_notification.h"
40 #include "singleton.h"
41 #include "securec.h"
42 
43 extern "C" {
44     struct CNotificationBasicContent {
45         char* title;
46         char* text;
47         char* additionalText;
48         int64_t lockscreenPicture;
49     };
50 
51     struct CNotificationLongTextContent {
52         char* title;
53         char* text;
54         char* additionalText;
55         int64_t lockscreenPicture;
56         char* longText;
57         char* briefText;
58         char* expandedTitle;
59     };
60 
61     struct CNotificationMultiLineContent {
62         char* title;
63         char* text;
64         char* additionalText;
65         int64_t lockscreenPicture;
66         char* briefText;
67         char* longTitle;
68         CArrString lines;
69     };
70 
71     struct CNotificationPictureContent {
72         char* title;
73         char* text;
74         char* additionalText;
75         int64_t lockscreenPicture;
76         char* briefText;
77         char* expandedTitle;
78         int64_t picture;
79     };
80 
81     struct CNotificationCapsule {
82         char* title;
83         int64_t icon;
84         char* backgroundColor;
85     };
86 
87     struct CNotificationButton {
88         CArrString names;
89         CArrI64 icons;
90     };
91 
92     struct CNotificationTime {
93         int32_t initialTime;
94         bool isCountDown;
95         bool isPaused;
96         bool isInTitle;
97     };
98 
99     struct CNotificationProgress {
100         int32_t maxValue;
101         int32_t currentValue;
102         bool isPercentage;
103     };
104 
105     struct CNotificationSystemLiveViewContent {
106         char* title;
107         char* text;
108         char* additionalText;
109         int64_t lockscreenPicture;
110         int32_t typeCode;
111         CNotificationCapsule capsule;
112         CNotificationButton button;
113         CNotificationTime time;
114         CNotificationProgress progress;
115     };
116 
117     struct CNotificationContent {
118         int32_t notificationContentType;
119         CNotificationBasicContent* normal;
120         CNotificationLongTextContent* longText;
121         CNotificationMultiLineContent* multiLine;
122         CNotificationPictureContent* picture;
123         CNotificationSystemLiveViewContent* systemLiveView;
124     };
125 
126     struct CDistributedOptions {
127         bool isDistributed;
128         CArrString supportDisplayDevices;
129         CArrString supportOperateDevices;
130     };
131 
132     enum class ContentType {
133         NOTIFICATION_CONTENT_BASIC_TEXT,
134         NOTIFICATION_CONTENT_LONG_TEXT,
135         NOTIFICATION_CONTENT_PICTURE,
136         NOTIFICATION_CONTENT_CONVERSATION,
137         NOTIFICATION_CONTENT_MULTILINE,
138         NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW,
139         NOTIFICATION_CONTENT_LIVE_VIEW
140     };
141 
142     enum class SlotType {
143         UNKNOWN_TYPE = 0,
144         SOCIAL_COMMUNICATION = 1,
145         SERVICE_INFORMATION = 2,
146         CONTENT_INFORMATION = 3,
147         LIVE_VIEW = 4,
148         CUSTOMER_SERVICE = 5,
149         OTHER_TYPES = 0xFFFF,
150     };
151 
152     enum class SlotLevel {
153         LEVEL_NONE = 0,       // the notification function is disabled.
154         LEVEL_MIN = 1,        // the notifications function is disabled on the notification panel,
155                             // with no banner or prompt tone
156         LEVEL_LOW = 2,        // the notifications are displayed on the notification panel,
157                             // with no banner or prompt tone
158         LEVEL_DEFAULT = 3,    // the notification function is enabled and notifications are displayed,
159                             // on the notification panel, with a banner and a prompt tone.
160         LEVEL_HIGH = 4,       // the notifications are displayed on the notification panel,
161                             // with a banner and a prompt tone
162         LEVEL_UNDEFINED = 0xFFFF,  // the notification does not define an level.
163     };
164 
165     enum class FlagStatus {
166         NONE = 0,
167         OPEN = 1,
168         CLOSE = 2,
169     };
170 
171     struct CNotificationFlags {
172         int32_t soundEnabled = 0;
173         int32_t vibrationEnabled = 0;
174     };
175 
176     struct CNotificationRequest {
177         CNotificationContent notificationContent;
178         int32_t id;
179         int32_t notificationSlotType;
180         bool isOngoing;
181         bool isUnremovable;
182         int64_t deliveryTime;
183         bool tapDismissed;
184         int64_t autoDeletedTime;
185         uint32_t color;
186         bool colorEnabled;
187         bool isAlertOnce;
188         bool isStopwatch;
189         bool isCountDown;
190         bool isFloatingIcon;
191         char* label;
192         int32_t badgeIconStyle;
193         bool showDeliveryTime;
194         int64_t smallIcon;
195         int64_t largeIcon;
196         char* creatorBundleName;
197         int32_t creatorUid;
198         int32_t creatorPid;
199         int32_t creatorUserId;
200         char* hashCode;
201         char* groupName;
202         CDistributedOptions* distributedOption;
203         CNotificationFlags notificationFlags;
204         uint32_t badgeNumber;
205         char* appMessageId;
206     };
207 
208     struct CArrayNotificationRequest {
209         CNotificationRequest** head;
210         int64_t size;
211     };
212 
213     struct CNotificationSlot {
214         int32_t notificationType;
215         int32_t level;
216         char* desc;
217         bool badgeFlag;
218         bool bypassDnd;
219         int32_t lockscreenVisibility;
220         bool vibrationEnabled;
221         char* sound;
222         bool lightEnabled;
223         int32_t lightColor;
224         CArrI64 vibrationValues;
225         bool enabled;
226     };
227 
228     struct CArrayNotificationSlots {
229         CNotificationSlot* head;
230         int64_t size;
231     };
232 
233     struct CNotificationBundleOption {
234         char* bundle;
235         int32_t uid;
236     };
237 }
238 
239 namespace OHOS {
240 namespace CJSystemapi {
241 namespace Notification {
242 constexpr int32_t STR_MAX_SIZE = 204;
243 constexpr int32_t LONG_STR_MAX_SIZE = 1028;
244 constexpr int32_t ERR_OK = 0;
245 constexpr uint32_t MAX_ICON_SIZE = 192 * 1024;
246 
247 char *MallocCString(const std::string &origin);
248 bool GetNotificationSupportDisplayDevices(
249     CDistributedOptions* distributedOption,
250     OHOS::Notification::NotificationRequest request);
251 bool GetNotificationSupportOperateDevices(
252     CDistributedOptions* distributedOption,
253     OHOS::Notification::NotificationRequest request);
254 bool GetNotificationRequestDistributedOptions(
255     CDistributedOptions* distributedOption,
256     OHOS::Notification::NotificationRequest request);
257 bool GetNotificationRequestByNumber(CNotificationRequest cjRequest, OHOS::Notification::NotificationRequest &request);
258 bool GetNotificationRequestByString(CNotificationRequest cjRequest, OHOS::Notification::NotificationRequest &request);
259 bool GetNotificationRequestByBool(CNotificationRequest cjRequest, OHOS::Notification::NotificationRequest &request);
260 bool GetNotificationRequestByCustom(CNotificationRequest cjRequest, OHOS::Notification::NotificationRequest &request);
261 bool GetNotificationBasicContentDetailed(CNotificationBasicContent* contentResult,
262     std::shared_ptr<OHOS::Notification::NotificationBasicContent> basicContent);
263 bool GetNotificationBasicContent(CNotificationBasicContent* contentResult,
264     OHOS::Notification::NotificationRequest &request);
265 bool GetNotificationLongTextContentDetailed(CNotificationLongTextContent* contentResult,
266     std::shared_ptr<OHOS::Notification::NotificationLongTextContent> &longContent);
267 bool GetNotificationLongTextContent(CNotificationLongTextContent* contentResult,
268     OHOS::Notification::NotificationRequest &request);
269 bool GetNotificationPictureContentDetailed(CNotificationPictureContent* contentResult,
270     std::shared_ptr<OHOS::Notification::NotificationPictureContent> &pictureContent);
271 bool GetNotificationPictureContent(CNotificationPictureContent* contentResult,
272     OHOS::Notification::NotificationRequest &request);
273 bool GetNotificationMultiLineContentLines(CNotificationMultiLineContent* result,
274     std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> &multiLineContent);
275 bool GetNotificationMultiLineContent(CNotificationMultiLineContent* contentResult,
276     OHOS::Notification::NotificationRequest &request);
277 bool GetNotificationLocalLiveViewCapsule(CNotificationSystemLiveViewContent* contentResult,
278     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> &content);
279 bool GetNotificationLocalLiveViewButton(CNotificationSystemLiveViewContent* contentResult,
280     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> &content);
281 bool GetNotificationLocalLiveViewProgress(CNotificationSystemLiveViewContent* contentResult,
282     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> &content);
283 bool GetNotificationLocalLiveViewTime(CNotificationSystemLiveViewContent* contentResult,
284     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> &content);
285 bool GetNotificationLocalLiveViewContentDetailed(CNotificationSystemLiveViewContent* contentResult,
286     std::shared_ptr<OHOS::Notification::NotificationLocalLiveViewContent> &content);
287 bool GetNotificationLocalLiveViewContent(CNotificationSystemLiveViewContent* contentResult,
288     OHOS::Notification::NotificationRequest &request);
289 bool SlotTypeCJToC(const SlotType &inType, OHOS::Notification::NotificationConstant::SlotType &outType);
290 bool SlotTypeCToCJ(const OHOS::Notification::NotificationConstant::SlotType &inType, SlotType &outType);
291 bool SlotLevelCToCJ(const OHOS::Notification::NotificationSlot::NotificationLevel &inLevel, SlotLevel &outLevel);
292 bool ContentTypeCJToC(const ContentType &inType, OHOS::Notification::NotificationContent::Type &outType);
293 bool ContentTypeCToCJ(const OHOS::Notification::NotificationContent::Type &inType, ContentType &outType);
294 bool GetNotificationSlotType(int32_t slotType, OHOS::Notification::NotificationRequest &request);
295 bool GetNotificationContent(CNotificationContent &content, OHOS::Notification::NotificationRequest &request);
296 bool GetNotificationSmallIcon(int64_t smallIcon, OHOS::Notification::NotificationRequest &request);
297 bool GetNotificationLargeIcon(int64_t largeIcon, OHOS::Notification::NotificationRequest &request);
298 bool SetNotificationSlot(const OHOS::Notification::NotificationSlot &slot, CNotificationSlot &notificationSlot);
299 void SetNotificationRequestByString(const OHOS::Notification::NotificationRequest *request,
300     CNotificationRequest &notificationRequest);
301 bool SetNotificationRequestByNumber(const OHOS::Notification::NotificationRequest *request,
302     CNotificationRequest &notificationRequest);
303 void SetNotificationRequestByBool(const OHOS::Notification::NotificationRequest *request,
304     CNotificationRequest &notificationRequest);
305 void SetNotificationRequestByPixelMap(const OHOS::Notification::NotificationRequest *request,
306     CNotificationRequest &notificationRequest);
307 bool SetNotificationBasicContent(const OHOS::Notification::NotificationBasicContent *basicContent,
308     CNotificationBasicContent* normal);
309 bool SetNotificationLongTextContent(OHOS::Notification::NotificationBasicContent *basicContent,
310     CNotificationLongTextContent* longText);
311 bool SetNotificationPictureContent(OHOS::Notification::NotificationBasicContent *basicContent,
312     CNotificationPictureContent* picture);
313 bool SetNotificationMultiLineContent(OHOS::Notification::NotificationBasicContent *basicContent,
314     CNotificationMultiLineContent* multiLine);
315 bool SetCapsule(const OHOS::Notification::NotificationCapsule &capsule, CNotificationCapsule &cCapsule);
316 bool SetButton(const OHOS::Notification::NotificationLocalLiveViewButton &button, CNotificationButton &cButton);
317 bool SetNotificationLocalLiveViewContentDetailed(
318     OHOS::Notification::NotificationLocalLiveViewContent *localLiveViewContent,
319     CNotificationSystemLiveViewContent* systemLiveView);
320 bool SetNotificationLocalLiveViewContent(OHOS::Notification::NotificationBasicContent *basicContent,
321     CNotificationSystemLiveViewContent* systemLiveView);
322 bool SetNotificationContentDetailed(const ContentType &type,
323     const std::shared_ptr<OHOS::Notification::NotificationContent> &content,
324     CNotificationContent &notificationContent);
325 bool SetNotificationContent(const std::shared_ptr<OHOS::Notification::NotificationContent> &content,
326     CNotificationContent &notificationContent);
327 bool SetNotificationFlags(const std::shared_ptr<OHOS::Notification::NotificationFlags> &flags,
328     CNotificationFlags &notificationFlags);
329 bool SetNotificationRequestByCustom(const OHOS::Notification::NotificationRequest *request,
330     CNotificationRequest &notificationRequest);
331 bool SetNotificationRequest(const OHOS::Notification::NotificationRequest *request,
332     CNotificationRequest &notificationRequest);
333 }
334 } // namespace CJSystemapi
335 } // namespace OHOS
336 
337 #endif // OHOS_NOTIFICATION_UTILS_H