• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "notification_utils.h"
17 #include "notification_manager_log.h"
18 
19 namespace OHOS {
20 namespace CJSystemapi {
21 namespace Notification {
22     using namespace OHOS::FFI;
23     using namespace OHOS::Notification;
24 
MallocCString(const std::string & origin)25     char *MallocCString(const std::string &origin)
26     {
27         if (origin.empty()) {
28             return nullptr;
29         }
30         auto len = origin.length() + 1;
31         char *res = static_cast<char *>(malloc(sizeof(char) * len));
32         if (res == nullptr) {
33             return nullptr;
34         }
35         return std::char_traits<char>::copy(res, origin.c_str(), len);
36     }
37 
GetNotificationSupportDisplayDevices(CDistributedOptions * distributedOption,NotificationRequest request)38     bool GetNotificationSupportDisplayDevices(
39         CDistributedOptions* distributedOption,
40         NotificationRequest request)
41     {
42         int64_t length = distributedOption->supportDisplayDevices.size;
43         if (length == 0) {
44             LOGE("The array is empty.");
45             return false;
46         }
47         std::vector<std::string> devices;
48         for (int64_t i = 0; i < length; i++) {
49             char str[STR_MAX_SIZE] = {0};
50             auto displayDevice = distributedOption->supportDisplayDevices.head[i];
51             if (strcpy_s(str, STR_MAX_SIZE, displayDevice) != EOK) {
52                 return false;
53             }
54             devices.emplace_back(str);
55             LOGI("supportDisplayDevices = %{public}s", str);
56         }
57         request.SetDevicesSupportDisplay(devices);
58         return true;
59     }
60 
GetNotificationSupportOperateDevices(CDistributedOptions * distributedOption,NotificationRequest request)61     bool GetNotificationSupportOperateDevices(
62         CDistributedOptions* distributedOption,
63         NotificationRequest request)
64     {
65         int64_t length = distributedOption->supportOperateDevices.size;
66         if (length == 0) {
67             LOGE("The array is empty.");
68             return false;
69         }
70         std::vector<std::string> devices;
71         for (int64_t i = 0; i < length; i++) {
72             char str[STR_MAX_SIZE] = {0};
73             auto operateDevice = distributedOption->supportOperateDevices.head[i];
74             if (strcpy_s(str, STR_MAX_SIZE, operateDevice) != EOK) {
75                 return false;
76             }
77             devices.emplace_back(str);
78             LOGI("supportOperateDevices = %{public}s", str);
79         }
80         request.SetDevicesSupportOperate(devices);
81         return true;
82     }
83 
GetNotificationRequestDistributedOptions(CDistributedOptions * distributedOption,NotificationRequest request)84     bool GetNotificationRequestDistributedOptions(
85         CDistributedOptions* distributedOption,
86         NotificationRequest request)
87     {
88         if (distributedOption != nullptr) {
89             // isDistributed?: boolean
90             request.SetDistributed(distributedOption->isDistributed);
91 
92             // supportDisplayDevices?: Array<string>
93             if (!GetNotificationSupportDisplayDevices(distributedOption, request)) {
94                 return false;
95             }
96 
97             // supportOperateDevices?: Array<string>
98             if (!GetNotificationSupportOperateDevices(distributedOption, request)) {
99                 return false;
100             }
101         }
102         return true;
103     }
104 
GetNotificationRequestByNumber(CNotificationRequest cjRequest,NotificationRequest & request)105     bool GetNotificationRequestByNumber(CNotificationRequest cjRequest, NotificationRequest &request)
106     {
107         // id?: int32_t
108         int32_t id = cjRequest.id;
109         request.SetNotificationId(id);
110 
111         // deliveryTime?: int64_t
112         int64_t deliveryTime = cjRequest.deliveryTime;
113         request.SetDeliveryTime(deliveryTime);
114 
115         // autoDeletedTime?: int64_t
116         int64_t autoDeletedTime = cjRequest.autoDeletedTime;
117         request.SetAutoDeletedTime(autoDeletedTime);
118 
119         // color?: uint32_t
120         request.SetColor(cjRequest.color);
121 
122         // badgeIconStyle?: int32_t
123         int32_t badgeIconStyle = cjRequest.badgeIconStyle;
124         request.SetBadgeIconStyle(static_cast<NotificationRequest::BadgeStyle>(badgeIconStyle));
125 
126         // badgeNumber?: uint32_t
127         uint32_t badgeNumber = cjRequest.badgeNumber;
128         if (badgeNumber < 0) {
129             LOGE("Wrong badge number.");
130             return false;
131         }
132         request.SetBadgeNumber(badgeNumber);
133 
134         return true;
135     }
136 
GetNotificationRequestByString(CNotificationRequest cjRequest,NotificationRequest & request)137     bool GetNotificationRequestByString(CNotificationRequest cjRequest, NotificationRequest &request)
138     {
139         // label?: string
140         char label[STR_MAX_SIZE] = {0};
141         if (strcpy_s(label, STR_MAX_SIZE, cjRequest.label) != EOK) {
142             return false;
143         }
144         request.SetLabel(std::string(label));
145 
146         // groupName?: string
147         char groupName[STR_MAX_SIZE] = {0};
148         if (strcpy_s(groupName, STR_MAX_SIZE, cjRequest.groupName) != EOK) {
149             return false;
150         }
151         request.SetGroupName(std::string(groupName));
152 
153         // groupName?: string
154         char appMessageId[STR_MAX_SIZE] = {0};
155         if (strcpy_s(appMessageId, STR_MAX_SIZE, cjRequest.appMessageId) != EOK) {
156             return false;
157         }
158         request.SetAppMessageId(std::string(appMessageId));
159 
160         return true;
161     }
162 
GetNotificationRequestByBool(CNotificationRequest cjRequest,NotificationRequest & request)163     bool GetNotificationRequestByBool(CNotificationRequest cjRequest, NotificationRequest &request)
164     {
165         // isOngoing?: boolean
166         bool isOngoing = cjRequest.isOngoing;
167         request.SetInProgress(isOngoing);
168 
169         // isUnremovable?: boolean
170         bool isUnremovable = cjRequest.isUnremovable;
171         request.SetUnremovable(isUnremovable);
172 
173         // tapDismissed?: boolean
174         bool tapDismissed = cjRequest.tapDismissed;
175         request.SetTapDismissed(tapDismissed);
176 
177         // colorEnabled?: boolean
178         bool colorEnabled = cjRequest.colorEnabled;
179         request.SetColorEnabled(colorEnabled);
180 
181         // isAlertOnce?: boolean
182         bool isAlertOnce = cjRequest.isAlertOnce;
183         request.SetAlertOneTime(isAlertOnce);
184 
185         // isStopwatch?: boole
186         bool isStopwatch = cjRequest.isStopwatch;
187         request.SetShowStopwatch(isStopwatch);
188 
189         // isCountDown?: boolean
190         bool isCountDown = cjRequest.isCountDown;
191         request.SetCountdownTimer(isCountDown);
192 
193         // showDeliveryTime?: boolean
194         bool showDeliveryTime = cjRequest.showDeliveryTime;
195         request.SetShowDeliveryTime(showDeliveryTime);
196 
197         return true;
198     }
199 
GetNotificationRequestByCustom(CNotificationRequest cjRequest,NotificationRequest & request)200     bool GetNotificationRequestByCustom(CNotificationRequest cjRequest, NotificationRequest &request)
201     {
202         // content: NotificationContent
203         if (!GetNotificationContent(cjRequest.notificationContent, request)) {
204             return false;
205         }
206         // slotType?: notification.SlotType
207         if (!GetNotificationSlotType(cjRequest.notificationSlotType, request)) {
208             return false;
209         }
210         // smallIcon?: image.PixelMap
211         if (!GetNotificationSmallIcon(cjRequest.smallIcon, request)) {
212             return false;
213         }
214         // largeIcon?: image.PixelMap
215         if (!GetNotificationLargeIcon(cjRequest.largeIcon, request)) {
216             return false;
217         }
218         // distributedOption?:DistributedOptions
219         if (!GetNotificationRequestDistributedOptions(cjRequest.distributedOption, request)) {
220             return false;
221         }
222 
223         return true;
224     }
225 
GetNotificationBasicContentDetailed(CNotificationBasicContent * contentResult,std::shared_ptr<NotificationBasicContent> basicContent)226     bool GetNotificationBasicContentDetailed(CNotificationBasicContent* contentResult,
227         std::shared_ptr<NotificationBasicContent> basicContent)
228     {
229         char str[SHORT_STR_SIZE] = {0};
230         char long_str[LONG_STR_SIZE] = {0};
231         // title: String
232         if (strcpy_s(str, SHORT_STR_SIZE, contentResult->title) != EOK) {
233             return false;
234         }
235         if (strlen(str) == 0) {
236             LOGE("Property title is empty");
237             return false;
238         }
239         basicContent->SetTitle(std::string(str));
240         // text: String
241         if (strcpy_s(long_str, LONG_STR_SIZE, contentResult->text) != EOK) {
242             return false;
243         }
244         if (strlen(long_str) == 0) {
245             LOGE("Property text is empty");
246             return false;
247         }
248         basicContent->SetText(std::string(long_str));
249         // additionalText: string
250         if (strcpy_s(long_str, LONG_STR_SIZE, contentResult->additionalText) != EOK) {
251             return false;
252         }
253         basicContent->SetAdditionalText(std::string(long_str));
254 
255         // lockScreenPicture?: pixelMap
256         if (contentResult->lockscreenPicture != -1) {
257             auto pixelMap = FFIData::GetData<Media::PixelMapImpl>(contentResult->lockscreenPicture);
258             if (pixelMap == nullptr) {
259                 LOGE("Invalid object pixelMap");
260                 return false;
261             }
262             basicContent->SetLockScreenPicture(pixelMap->GetRealPixelMap());
263         }
264         return true;
265     }
266 
GetNotificationBasicContent(CNotificationBasicContent * contentResult,NotificationRequest & request)267     bool GetNotificationBasicContent(CNotificationBasicContent* contentResult, NotificationRequest &request)
268     {
269         std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
270         if (normalContent == nullptr) {
271             LOGE("normalContent is null");
272             return false;
273         }
274 
275         if (!GetNotificationBasicContentDetailed(contentResult, normalContent)) {
276             return false;
277         }
278         request.SetContent(std::make_shared<NotificationContent>(normalContent));
279         return true;
280     }
281 
GetNotificationLongTextContentDetailed(CNotificationLongTextContent * contentResult,std::shared_ptr<NotificationLongTextContent> & longContent)282     bool GetNotificationLongTextContentDetailed(
283         CNotificationLongTextContent* contentResult,
284         std::shared_ptr<NotificationLongTextContent> &longContent)
285     {
286         char str[SHORT_STR_SIZE] = {0};
287         char long_str[LONG_STR_SIZE] = {0};
288 
289         std::shared_ptr<CNotificationBasicContent> tempContent = std::make_shared<CNotificationBasicContent>();
290         tempContent->title = contentResult->title;
291         tempContent->text = contentResult->text;
292         tempContent->additionalText = contentResult->additionalText;
293         tempContent->lockscreenPicture = contentResult->lockscreenPicture;
294         if (!GetNotificationBasicContentDetailed(tempContent.get(), longContent)) {
295             return false;
296         }
297 
298         // longText: String
299         if (strcpy_s(long_str, LONG_STR_SIZE, contentResult->longText) != EOK) {
300             return false;
301         }
302         if (strlen(long_str) == 0) {
303             LOGE("Property longText is empty");
304             return false;
305         }
306         longContent->SetLongText(std::string(long_str));
307 
308         // briefText: String
309         if (strcpy_s(str, SHORT_STR_SIZE, contentResult->briefText) != EOK) {
310             return false;
311         }
312         if (strlen(str) == 0) {
313             LOGE("Property briefText is empty");
314             return false;
315         }
316         longContent->SetBriefText(std::string(str));
317 
318         // expandedTitle: String
319         if (strcpy_s(str, SHORT_STR_SIZE, contentResult->expandedTitle) != EOK) {
320             return false;
321         }
322         if (strlen(str) == 0) {
323             LOGE("Property expandedTitle is empty");
324             return false;
325         }
326         longContent->SetExpandedTitle(std::string(str));
327 
328         return true;
329     }
330 
GetNotificationLongTextContent(CNotificationLongTextContent * contentResult,NotificationRequest & request)331     bool GetNotificationLongTextContent(
332         CNotificationLongTextContent* contentResult,
333         NotificationRequest &request)
334     {
335         std::shared_ptr<OHOS::Notification::NotificationLongTextContent> longContent =
336         std::make_shared<OHOS::Notification::NotificationLongTextContent>();
337         if (longContent == nullptr) {
338             LOGE("longContent is null");
339             return false;
340         }
341         if (!GetNotificationLongTextContentDetailed(contentResult, longContent)) {
342             return false;
343         }
344 
345         request.SetContent(std::make_shared<NotificationContent>(longContent));
346         return true;
347     }
348 
GetNotificationPictureContentDetailed(CNotificationPictureContent * contentResult,std::shared_ptr<NotificationPictureContent> & pictureContent)349     bool GetNotificationPictureContentDetailed(
350         CNotificationPictureContent* contentResult,
351         std::shared_ptr<NotificationPictureContent> &pictureContent)
352     {
353         char str[SHORT_STR_SIZE] = {0};
354         char long_str[LONG_STR_SIZE] = {0};
355 
356         std::shared_ptr<CNotificationBasicContent> tempContent = std::make_shared<CNotificationBasicContent>();
357         tempContent->title = contentResult->title;
358         tempContent->text = contentResult->text;
359         tempContent->additionalText = contentResult->additionalText;
360         tempContent->lockscreenPicture = contentResult->lockscreenPicture;
361         if (!GetNotificationBasicContentDetailed(tempContent.get(), pictureContent)) {
362             return false;
363         }
364 
365         // briefText: String
366         if (strcpy_s(str, SHORT_STR_SIZE, contentResult->briefText) != EOK) {
367             return false;
368         }
369         if (std::strlen(str) == 0) {
370             LOGE("Property briefText is empty");
371             return false;
372         }
373         pictureContent->SetBriefText(std::string(str));
374 
375         // expandedTitle: String
376         if (strcpy_s(long_str, LONG_STR_SIZE, contentResult->expandedTitle) != EOK) {
377             return false;
378         }
379         if (std::strlen(long_str) == 0) {
380             LOGE("Property expandedTitle is empty");
381             return false;
382         }
383         pictureContent->SetExpandedTitle(std::string(long_str));
384 
385         // picture: image.PixelMap
386         auto pixelMap = FFIData::GetData<Media::PixelMapImpl>(contentResult->picture);
387         if (pixelMap == nullptr) {
388             LOGE("Invalid object pixelMap");
389             return false;
390         }
391         pictureContent->SetBigPicture(pixelMap->GetRealPixelMap());
392 
393         return true;
394     }
395 
GetNotificationPictureContent(CNotificationPictureContent * contentResult,NotificationRequest & request)396     bool GetNotificationPictureContent(
397         CNotificationPictureContent* contentResult,
398         NotificationRequest &request)
399     {
400         std::shared_ptr<OHOS::Notification::NotificationPictureContent> pictureContent =
401         std::make_shared<OHOS::Notification::NotificationPictureContent>();
402         if (pictureContent == nullptr) {
403             LOGE("pictureContent is null");
404             return false;
405         }
406 
407         if (!GetNotificationPictureContentDetailed(contentResult, pictureContent)) {
408             return false;
409         }
410 
411         request.SetContent(std::make_shared<NotificationContent>(pictureContent));
412         return true;
413     }
414 
GetNotificationMultiLineContentLines(CNotificationMultiLineContent * result,std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> & multiLineContent)415     bool GetNotificationMultiLineContentLines(
416         CNotificationMultiLineContent* result,
417         std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> &multiLineContent)
418     {
419         char str[SHORT_STR_SIZE] = {0};
420         int64_t length = result->lines.size;
421         if (length == 0) {
422             LOGE("The array is empty.");
423             return false;
424         }
425         for (int64_t i = 0; i < length; i++) {
426             if (strcpy_s(str, SHORT_STR_SIZE, result->lines.head[i]) != EOK) {
427                 return false;
428             }
429             multiLineContent->AddSingleLine(std::string(str));
430         }
431         return true;
432     }
433 
GetNotificationMultiLineContent(CNotificationMultiLineContent * contentResult,NotificationRequest & request)434     bool GetNotificationMultiLineContent(
435         CNotificationMultiLineContent* contentResult,
436         NotificationRequest &request)
437     {
438         char str[SHORT_STR_SIZE] = {0};
439 
440         std::shared_ptr<OHOS::Notification::NotificationMultiLineContent> multiLineContent =
441         std::make_shared<OHOS::Notification::NotificationMultiLineContent>();
442         if (multiLineContent == nullptr) {
443             LOGE("multiLineContent is null");
444             return false;
445         }
446 
447         std::shared_ptr<CNotificationBasicContent> tempContent = std::make_shared<CNotificationBasicContent>();
448         tempContent->title = contentResult->title;
449         tempContent->text = contentResult->text;
450         tempContent->additionalText = contentResult->additionalText;
451         tempContent->lockscreenPicture = contentResult->lockscreenPicture;
452         if (!GetNotificationBasicContentDetailed(tempContent.get(), multiLineContent)) {
453             return false;
454         }
455 
456         // briefText: String
457         if (strcpy_s(str, SHORT_STR_SIZE, contentResult->briefText) != EOK) {
458             return false;
459         }
460         if (std::strlen(str) == 0) {
461             LOGE("Property briefText is empty");
462             return false;
463         }
464         multiLineContent->SetBriefText(std::string(str));
465 
466         // longTitle: String
467         if (strcpy_s(str, LONG_STR_SIZE, contentResult->longTitle) != EOK) {
468             return false;
469         }
470         if (std::strlen(str) == 0) {
471             LOGE("Property longTitle is empty");
472             return false;
473         }
474         multiLineContent->SetExpandedTitle(std::string(str));
475 
476         // lines: Array<String>
477         if (!GetNotificationMultiLineContentLines(contentResult, multiLineContent)) {
478             return false;
479         }
480 
481         request.SetContent(std::make_shared<NotificationContent>(multiLineContent));
482         return true;
483     }
484 
GetNotificationLocalLiveViewCapsule(CNotificationSystemLiveViewContent * contentResult,std::shared_ptr<NotificationLocalLiveViewContent> & content)485     bool GetNotificationLocalLiveViewCapsule(CNotificationSystemLiveViewContent* contentResult,
486         std::shared_ptr<NotificationLocalLiveViewContent> &content)
487     {
488         char str[STR_MAX_SIZE] = {0};
489         NotificationCapsule capsule;
490         if (strcpy_s(str, STR_MAX_SIZE, contentResult->capsule.title) != EOK) {
491             LOGE("copy capsule.title failed");
492             return false;
493         }
494         capsule.SetTitle(std::string(str));
495 
496         if (strcpy_s(str, STR_MAX_SIZE, contentResult->capsule.backgroundColor) != EOK) {
497             LOGE("copy capsule.backgroundColor failed");
498             return false;
499         }
500         capsule.SetBackgroundColor(std::string(str));
501 
502         if (contentResult->capsule.icon != -1) {
503             auto pixelMap = FFIData::GetData<Media::PixelMapImpl>(contentResult->capsule.icon);
504             if (pixelMap == nullptr) {
505                 LOGE("Invalid object pixelMap");
506                 return false;
507             }
508             capsule.SetIcon(pixelMap->GetRealPixelMap());
509         }
510 
511         content->SetCapsule(capsule);
512         content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
513         return true;
514     }
515 
GetNotificationLocalLiveViewButton(CNotificationSystemLiveViewContent * contentResult,std::shared_ptr<NotificationLocalLiveViewContent> & content)516     bool GetNotificationLocalLiveViewButton(CNotificationSystemLiveViewContent* contentResult,
517         std::shared_ptr<NotificationLocalLiveViewContent> &content)
518     {
519         char str[STR_MAX_SIZE] = {0};
520         NotificationLocalLiveViewButton button;
521         int64_t length = contentResult->button.names.size;
522         for (int64_t i = 0; i < length; i++) {
523             if (strcpy_s(str, STR_MAX_SIZE, contentResult->button.names.head[i]) != EOK) {
524                 LOGE("copy button.names failed");
525                 return false;
526             }
527             button.addSingleButtonName(std::string(str));
528         }
529 
530         length = contentResult->button.icons.size;
531         for (int64_t i = 0; i < length; i++) {
532             int64_t id = contentResult->button.icons.head[i];
533             auto pixelMap = FFIData::GetData<Media::PixelMapImpl>(id);
534             if (pixelMap == nullptr) {
535                 LOGE("Invalid object pixelMap");
536                 return false;
537             }
538             auto pix = pixelMap->GetRealPixelMap();
539             if (pix != nullptr && static_cast<uint32_t>(pix->GetByteCount()) <= MAX_ICON_SIZE) {
540                 button.addSingleButtonIcon(pix);
541             } else {
542                 LOGE("Invalid pixelMap object or pixelMap is over size.");
543                 return false;
544             }
545         }
546         content->SetButton(button);
547         content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
548         return true;
549     }
550 
GetNotificationLocalLiveViewProgress(CNotificationSystemLiveViewContent * contentResult,std::shared_ptr<NotificationLocalLiveViewContent> & content)551     bool GetNotificationLocalLiveViewProgress(CNotificationSystemLiveViewContent* contentResult,
552         std::shared_ptr<NotificationLocalLiveViewContent> &content)
553     {
554         NotificationProgress progress;
555         if (contentResult->progress.maxValue < 0 || contentResult->progress.currentValue < 0) {
556             LOGE("Wrong argument value. Number expected.");
557             return false;
558         }
559         progress.SetMaxValue(contentResult->progress.maxValue);
560         progress.SetCurrentValue(contentResult->progress.currentValue);
561         progress.SetIsPercentage(contentResult->progress.isPercentage);
562 
563         content->SetProgress(progress);
564         content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS);
565         return true;
566     }
567 
GetNotificationLocalLiveViewTime(CNotificationSystemLiveViewContent * contentResult,std::shared_ptr<NotificationLocalLiveViewContent> & content)568     bool GetNotificationLocalLiveViewTime(CNotificationSystemLiveViewContent* contentResult,
569         std::shared_ptr<NotificationLocalLiveViewContent> &content)
570     {
571         NotificationTime time;
572         if (contentResult->time.initialTime < 0) {
573             return false;
574         }
575         time.SetInitialTime(contentResult->time.initialTime);
576         content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::INITIAL_TIME);
577         time.SetIsCountDown(contentResult->time.isCountDown);
578         time.SetIsPaused(contentResult->time.isPaused);
579         time.SetIsInTitle(contentResult->time.isInTitle);
580 
581         content->SetTime(time);
582         content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
583 
584         return true;
585     }
586 
GetNotificationLocalLiveViewContentDetailed(CNotificationSystemLiveViewContent * contentResult,std::shared_ptr<NotificationLocalLiveViewContent> & content)587     bool GetNotificationLocalLiveViewContentDetailed(CNotificationSystemLiveViewContent* contentResult,
588         std::shared_ptr<NotificationLocalLiveViewContent> &content)
589     {
590         // title, text
591         std::shared_ptr<CNotificationBasicContent> tempContent = std::make_shared<CNotificationBasicContent>();
592         tempContent->title = contentResult->title;
593         tempContent->text = contentResult->text;
594         tempContent->additionalText = contentResult->additionalText;
595         tempContent->lockscreenPicture = contentResult->lockscreenPicture;
596         if (!GetNotificationBasicContentDetailed(tempContent.get(), content)) {
597             LOGE("Basic content get fail.");
598             return false;
599         }
600 
601         // typeCode
602         content->SetType(contentResult->typeCode);
603 
604         // capsule?
605         if (!GetNotificationLocalLiveViewCapsule(contentResult, content)) {
606             LOGE("GetNotificationLocalLiveViewCapsule fail.");
607             return false;
608         }
609 
610         // button?
611         if (!GetNotificationLocalLiveViewButton(contentResult, content)) {
612             LOGE("GetNotificationLocalLiveViewButton fail.");
613             return false;
614         }
615 
616         // progress?
617         if (!GetNotificationLocalLiveViewProgress(contentResult, content)) {
618             LOGE("GetNotificationLocalLiveViewProgress fail.");
619             return false;
620         }
621 
622         // time?
623         if (!GetNotificationLocalLiveViewTime(contentResult, content)) {
624             LOGE("GetNotificationLocalLiveViewTime fail.");
625             return false;
626         }
627 
628         return true;
629     }
630 
GetNotificationLocalLiveViewContent(CNotificationSystemLiveViewContent * contentResult,NotificationRequest & request)631     bool GetNotificationLocalLiveViewContent(CNotificationSystemLiveViewContent* contentResult,
632         NotificationRequest &request)
633     {
634         std::shared_ptr<NotificationLocalLiveViewContent> localLiveViewContent =
635             std::make_shared<NotificationLocalLiveViewContent>();
636         if (localLiveViewContent == nullptr) {
637             LOGE("localLiveViewContent is null");
638             return false;
639         }
640 
641         if (!GetNotificationLocalLiveViewContentDetailed(contentResult, localLiveViewContent)) {
642             return false;
643         }
644 
645         request.SetContent(std::make_shared<NotificationContent>(localLiveViewContent));
646 
647         // set isOnGoing of live view true
648         request.SetInProgress(true);
649         return true;
650     }
651 
SlotTypeCJToC(const SlotType & inType,NotificationConstant::SlotType & outType)652     bool SlotTypeCJToC(const SlotType &inType, NotificationConstant::SlotType &outType)
653     {
654         switch (inType) {
655             case SlotType::SOCIAL_COMMUNICATION:
656                 outType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION;
657                 break;
658             case SlotType::SERVICE_INFORMATION:
659                 outType = NotificationConstant::SlotType::SERVICE_REMINDER;
660                 break;
661             case SlotType::CONTENT_INFORMATION:
662                 outType = NotificationConstant::SlotType::CONTENT_INFORMATION;
663                 break;
664             case SlotType::LIVE_VIEW:
665                 outType = NotificationConstant::SlotType::LIVE_VIEW;
666                 break;
667             case SlotType::CUSTOMER_SERVICE:
668                 outType = NotificationConstant::SlotType::CUSTOMER_SERVICE;
669                 break;
670             case SlotType::UNKNOWN_TYPE:
671             case SlotType::OTHER_TYPES:
672                 outType = NotificationConstant::SlotType::OTHER;
673                 break;
674             default:
675                 LOGE("SlotType %{public}d is an invalid value", inType);
676                 return false;
677         }
678         return true;
679     }
680 
SlotTypeCToCJ(const NotificationConstant::SlotType & inType,SlotType & outType)681     bool SlotTypeCToCJ(const NotificationConstant::SlotType &inType, SlotType &outType)
682     {
683         switch (inType) {
684             case NotificationConstant::SlotType::CUSTOM:
685                 outType = SlotType::UNKNOWN_TYPE;
686                 break;
687             case NotificationConstant::SlotType::SOCIAL_COMMUNICATION:
688                 outType = SlotType::SOCIAL_COMMUNICATION;
689                 break;
690             case NotificationConstant::SlotType::SERVICE_REMINDER:
691                 outType = SlotType::SERVICE_INFORMATION;
692                 break;
693             case NotificationConstant::SlotType::CONTENT_INFORMATION:
694                 outType = SlotType::CONTENT_INFORMATION;
695                 break;
696             case NotificationConstant::SlotType::LIVE_VIEW:
697                 outType = SlotType::LIVE_VIEW;
698                 break;
699             case NotificationConstant::SlotType::CUSTOMER_SERVICE:
700                 outType = SlotType::CUSTOMER_SERVICE;
701                 break;
702             case NotificationConstant::SlotType::OTHER:
703                 outType = SlotType::OTHER_TYPES;
704                 break;
705             default:
706                 LOGE("SlotType %{public}d is an invalid value", inType);
707                 return false;
708         }
709         return true;
710     }
711 
SlotLevelCToCJ(const NotificationSlot::NotificationLevel & inLevel,SlotLevel & outLevel)712     bool SlotLevelCToCJ(const NotificationSlot::NotificationLevel &inLevel, SlotLevel &outLevel)
713     {
714         switch (inLevel) {
715             case NotificationSlot::NotificationLevel::LEVEL_NONE:
716             case NotificationSlot::NotificationLevel::LEVEL_UNDEFINED:
717                 outLevel = SlotLevel::LEVEL_NONE;
718                 break;
719             case NotificationSlot::NotificationLevel::LEVEL_MIN:
720                 outLevel = SlotLevel::LEVEL_MIN;
721                 break;
722             case NotificationSlot::NotificationLevel::LEVEL_LOW:
723                 outLevel = SlotLevel::LEVEL_LOW;
724                 break;
725             case NotificationSlot::NotificationLevel::LEVEL_DEFAULT:
726                 outLevel = SlotLevel::LEVEL_DEFAULT;
727                 break;
728             case NotificationSlot::NotificationLevel::LEVEL_HIGH:
729                 outLevel = SlotLevel::LEVEL_HIGH;
730                 break;
731             default:
732                 LOGE("SlotLevel %{public}d is an invalid value", inLevel);
733                 return false;
734         }
735         return true;
736     }
737 
ContentTypeCJToC(const ContentType & inType,NotificationContent::Type & outType)738     bool ContentTypeCJToC(const ContentType &inType, NotificationContent::Type &outType)
739     {
740         switch (inType) {
741             case ContentType::NOTIFICATION_CONTENT_BASIC_TEXT:
742                 outType = NotificationContent::Type::BASIC_TEXT;
743                 break;
744             case ContentType::NOTIFICATION_CONTENT_LONG_TEXT:
745                 outType = NotificationContent::Type::LONG_TEXT;
746                 break;
747             case ContentType::NOTIFICATION_CONTENT_MULTILINE:
748                 outType = NotificationContent::Type::MULTILINE;
749                 break;
750             case ContentType::NOTIFICATION_CONTENT_PICTURE:
751                 outType = NotificationContent::Type::PICTURE;
752                 break;
753             case ContentType::NOTIFICATION_CONTENT_CONVERSATION:
754                 outType = NotificationContent::Type::CONVERSATION;
755                 break;
756             case ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW:
757                 outType = NotificationContent::Type::LOCAL_LIVE_VIEW;
758                 break;
759             case ContentType::NOTIFICATION_CONTENT_LIVE_VIEW:
760                 outType = NotificationContent::Type::LIVE_VIEW;
761                 break;
762             default:
763                 LOGE("ContentType %{public}d is an invalid value", inType);
764                 return false;
765         }
766         return true;
767     }
768 
ContentTypeCToCJ(const NotificationContent::Type & inType,ContentType & outType)769     bool ContentTypeCToCJ(const NotificationContent::Type &inType, ContentType &outType)
770     {
771         switch (inType) {
772             case NotificationContent::Type::BASIC_TEXT:
773                 outType = ContentType::NOTIFICATION_CONTENT_BASIC_TEXT;
774                 break;
775             case NotificationContent::Type::LONG_TEXT:
776                 outType = ContentType::NOTIFICATION_CONTENT_LONG_TEXT;
777                 break;
778             case NotificationContent::Type::MULTILINE:
779                 outType = ContentType::NOTIFICATION_CONTENT_MULTILINE;
780                 break;
781             case NotificationContent::Type::PICTURE:
782                 outType = ContentType::NOTIFICATION_CONTENT_PICTURE;
783                 break;
784             case NotificationContent::Type::CONVERSATION:
785                 outType = ContentType::NOTIFICATION_CONTENT_CONVERSATION;
786                 break;
787             case NotificationContent::Type::LOCAL_LIVE_VIEW:
788                 outType = ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW;
789                 break;
790             case NotificationContent::Type::LIVE_VIEW:
791                 outType = ContentType::NOTIFICATION_CONTENT_LIVE_VIEW;
792                 break;
793             default:
794                 LOGE("ContentType %{public}d is an invalid value", inType);
795                 return false;
796         }
797         return true;
798     }
799 
GetNotificationSlotType(int32_t slotType,NotificationRequest & request)800     bool GetNotificationSlotType(int32_t slotType, NotificationRequest &request)
801     {
802         NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER;
803         if (!SlotTypeCJToC(SlotType(slotType), outType)) {
804             return false;
805         }
806         request.SetSlotType(outType);
807         return true;
808     }
809 
GetNotificationSmallIcon(int64_t smallIcon,NotificationRequest & request)810     bool GetNotificationSmallIcon(int64_t smallIcon, NotificationRequest &request)
811     {
812         if (smallIcon != -1) {
813             auto pixelMap = FFIData::GetData<Media::PixelMapImpl>(smallIcon);
814             if (pixelMap == nullptr) {
815                 LOGE("Invalid object pixelMap");
816                 return false;
817             }
818             request.SetLittleIcon(pixelMap->GetRealPixelMap());
819         }
820         return true;
821     }
822 
GetNotificationLargeIcon(int64_t largeIcon,NotificationRequest & request)823     bool GetNotificationLargeIcon(int64_t largeIcon, NotificationRequest &request)
824     {
825         if (largeIcon != -1) {
826             auto pixelMap = FFI::FFIData::GetData<Media::PixelMapImpl>(largeIcon);
827             if (pixelMap == nullptr) {
828                 LOGE("Invalid object pixelMap");
829                 return false;
830             }
831             request.SetBigIcon(pixelMap->GetRealPixelMap());
832         }
833         return true;
834     }
835 
GetNotificationContent(CNotificationContent & content,NotificationRequest & request)836     bool GetNotificationContent(CNotificationContent &content, NotificationRequest &request)
837     {
838         NotificationContent::Type outType = NotificationContent::Type::NONE;
839         if (!ContentTypeCJToC(ContentType(content.notificationContentType), outType)) {
840             return false;
841         }
842         switch (outType) {
843             case NotificationContent::Type::BASIC_TEXT:
844                 if (content.normal == nullptr || !GetNotificationBasicContent(content.normal, request)) {
845                     return false;
846                 }
847                 break;
848             case NotificationContent::Type::LONG_TEXT:
849                 if (content.longText == nullptr || !GetNotificationLongTextContent(content.longText, request)) {
850                     return false;
851                 }
852                 break;
853             case NotificationContent::Type::PICTURE:
854                 if (content.picture == nullptr || !GetNotificationPictureContent(content.picture, request)) {
855                     return false;
856                 }
857                 break;
858             case NotificationContent::Type::CONVERSATION:
859                 break;
860             case NotificationContent::Type::MULTILINE:
861                 if (content.multiLine == nullptr || !GetNotificationMultiLineContent(content.multiLine, request)) {
862                     return false;
863                 }
864                 break;
865             case NotificationContent::Type::LOCAL_LIVE_VIEW:
866                 if (content.systemLiveView == nullptr ||
867                     !GetNotificationLocalLiveViewContent(content.systemLiveView, request)) {
868                     return false;
869                 }
870                 break;
871             case NotificationContent::Type::LIVE_VIEW:
872                 break;
873             default:
874                 return false;
875         }
876         return true;
877     }
878 
SetNotificationSlot(const NotificationSlot & slot,CNotificationSlot & notificationSlot)879     bool SetNotificationSlot(const NotificationSlot &slot, CNotificationSlot &notificationSlot)
880     {
881         // type: SlotType
882         SlotType outType = SlotType::UNKNOWN_TYPE;
883         if (!SlotTypeCToCJ(slot.GetType(), outType)) {
884             LOGE("SetNotificationSlot SlotTypeCToCJ failed.");
885             return false;
886         }
887         // level?: int32_t
888         SlotLevel outLevel = SlotLevel::LEVEL_NONE;
889         if (!SlotLevelCToCJ(slot.GetLevel(), outLevel)) {
890             LOGE("SetNotificationSlot SlotLevelCToCJ failed.");
891             return false;
892         }
893         notificationSlot.notificationType = static_cast<int32_t>(outType);
894         notificationSlot.level = static_cast<int32_t>(outLevel);
895 
896         notificationSlot.desc = MallocCString(slot.GetDescription()); // desc?: string
897         notificationSlot.badgeFlag = slot.IsShowBadge(); // badgeFlag?: bool
898         notificationSlot.bypassDnd = slot.IsEnableBypassDnd(); // bypassDnd?: bool
899         // lockscreenVisibility?: int32_t
900         notificationSlot.lockscreenVisibility = static_cast<int32_t>(slot.GetLockScreenVisibleness());
901         notificationSlot.vibrationEnabled = slot.CanVibrate(); // vibrationEnabled?: bool
902         notificationSlot.sound = MallocCString(slot.GetSound().ToString()); // sound?: string
903         notificationSlot.lightEnabled = slot.CanEnableLight(); // lightEnabled?: bool
904         notificationSlot.lightColor = slot.GetLedLightColor(); // lightColor?: int32_t
905 
906         // vibrationValues?: Array<int64_t>
907         auto vec = slot.GetVibrationStyle();
908         CArrI64 vibrationValues = { .head = NULL, .size = 0 };
909         vibrationValues.size = static_cast<int64_t>(vec.size());
910         if (vibrationValues.size > 0) {
911             int64_t* head = static_cast<int64_t *>(malloc(sizeof(int64_t) * vec.size()));
912             if (head == nullptr) {
913                 free(notificationSlot.desc);
914                 free(notificationSlot.sound);
915                 notificationSlot.desc = nullptr;
916                 notificationSlot.sound = nullptr;
917                 LOGE("SetNotificationSlot malloc vibrationValues.head failed.");
918                 return false;
919             }
920             int i = 0;
921             for (auto value : vec) {
922                 head[i++] = static_cast<int64_t>(value);
923             }
924             vibrationValues.head = head;
925         }
926         notificationSlot.vibrationValues = vibrationValues;
927         notificationSlot.enabled = slot.GetEnable(); // enabled?: boolean
928         return true;
929     }
930 
SetNotificationRequestByString(const NotificationRequest * request,CNotificationRequest & notificationRequest)931     void SetNotificationRequestByString(
932         const NotificationRequest *request,
933         CNotificationRequest &notificationRequest)
934     {
935         // label?: string
936         notificationRequest.label = MallocCString(request->GetLabel());
937 
938         // groupName?: string
939         notificationRequest.groupName = MallocCString(request->GetGroupName());
940 
941         // readonly creatorBundleName?: string
942         notificationRequest.creatorBundleName = MallocCString(request->GetCreatorBundleName());
943     }
944 
SetNotificationRequestByNumber(const NotificationRequest * request,CNotificationRequest & notificationRequest)945     bool SetNotificationRequestByNumber(
946         const NotificationRequest *request,
947         CNotificationRequest &notificationRequest)
948     {
949         // id?: int32_t
950         notificationRequest.id = request->GetNotificationId();
951 
952         // slotType?: SlotType
953         SlotType outType = SlotType::UNKNOWN_TYPE;
954         if (!SlotTypeCToCJ(request->GetSlotType(), outType)) {
955             return false;
956         }
957         notificationRequest.notificationSlotType = static_cast<int32_t>(outType);
958 
959         // deliveryTime?: int32_t
960         notificationRequest.deliveryTime = request->GetDeliveryTime();
961 
962         // autoDeletedTime?: int32_t
963         notificationRequest.autoDeletedTime = request->GetAutoDeletedTime();
964 
965         // color ?: int32_t
966         notificationRequest.color = request->GetColor();
967 
968         // badgeIconStyle ?: int32_t
969         notificationRequest.badgeIconStyle = static_cast<int32_t>(request->GetBadgeIconStyle());
970 
971         // readonly creatorUid?: int32_t
972         notificationRequest.creatorUid = request->GetCreatorUid();
973 
974         // readonly creatorPid?: int32_t
975         notificationRequest.creatorPid = request->GetCreatorPid();
976 
977         // badgeNumber?: uint32_t
978         notificationRequest.badgeNumber = request->GetBadgeNumber();
979 
980         return true;
981     }
982 
SetNotificationRequestByBool(const NotificationRequest * request,CNotificationRequest & notificationRequest)983     void SetNotificationRequestByBool(
984         const NotificationRequest *request,
985         CNotificationRequest &notificationRequest)
986     {
987         // isOngoing?: boolean
988         notificationRequest.isOngoing = request->IsInProgress();
989 
990         // isUnremovable?: boolean
991         notificationRequest.isUnremovable = request->IsUnremovable();
992 
993         // tapDismissed?: boolean
994         notificationRequest.tapDismissed = request->IsTapDismissed();
995 
996         // colorEnabled?: boolean
997         notificationRequest.colorEnabled = request->IsColorEnabled();
998 
999         // isAlertOnce?: boolean
1000         notificationRequest.isAlertOnce = request->IsAlertOneTime();
1001 
1002         // isStopwatch?: boolean
1003         notificationRequest.isStopwatch = request->IsShowStopwatch();
1004 
1005         // isCountDown?: boolean
1006         notificationRequest.isCountDown = request->IsCountdownTimer();
1007 
1008         // isFloatingIcon?: boolean
1009         notificationRequest.isFloatingIcon = request->IsFloatingIcon();
1010 
1011         // showDeliveryTime?: boolean
1012         notificationRequest.showDeliveryTime = request->IsShowDeliveryTime();
1013     }
1014 
SetNotificationRequestByPixelMap(const NotificationRequest * request,CNotificationRequest & notificationRequest)1015     void SetNotificationRequestByPixelMap(
1016         const NotificationRequest *request,
1017         CNotificationRequest &notificationRequest)
1018     {
1019         // smallIcon?: image.PixelMap
1020         std::shared_ptr<Media::PixelMap> littleIcon = request->GetLittleIcon();
1021         notificationRequest.smallIcon = -1;
1022         if (littleIcon) {
1023             auto native = FFIData::Create<Media::PixelMapImpl>(littleIcon);
1024             if (native != nullptr) {
1025                 notificationRequest.smallIcon = native->GetID();
1026             }
1027         }
1028 
1029         // largeIcon?: image.PixelMap
1030         notificationRequest.largeIcon = -1;
1031         std::shared_ptr<Media::PixelMap> largeIcon = request->GetBigIcon();
1032         if (largeIcon) {
1033             auto native = FFIData::Create<Media::PixelMapImpl>(largeIcon);
1034             if (native != nullptr) {
1035                 notificationRequest.largeIcon = native->GetID();
1036             }
1037         }
1038     }
1039 
freeNotificationBasicContent(CNotificationBasicContent * normal)1040     static void freeNotificationBasicContent(CNotificationBasicContent* normal)
1041     {
1042         free(normal->title);
1043         free(normal->text);
1044         free(normal->additionalText);
1045         normal->title = nullptr;
1046         normal->text = nullptr;
1047         normal->additionalText = nullptr;
1048     }
1049 
SetNotificationBasicContent(const NotificationBasicContent * basicContent,CNotificationBasicContent * normal)1050     bool SetNotificationBasicContent(
1051         const NotificationBasicContent *basicContent,
1052         CNotificationBasicContent* normal)
1053     {
1054         if (basicContent == nullptr || normal == nullptr) {
1055             return false;
1056         }
1057 
1058         // title: string
1059         normal->title = MallocCString(basicContent->GetTitle());
1060 
1061         // text: string
1062         normal->text = MallocCString(basicContent->GetText());
1063 
1064         // additionalText?: string
1065         normal->additionalText = MallocCString(basicContent->GetAdditionalText());
1066 
1067         // lockScreenPicture?: pixelMap
1068         normal->lockscreenPicture = -1;
1069         if (basicContent->GetLockScreenPicture()) {
1070             std::shared_ptr<Media::PixelMap> pix = basicContent->GetLockScreenPicture();
1071             if (pix == nullptr) {
1072                 LOGE("Invalid object pixelMap");
1073                 freeNotificationBasicContent(normal);
1074                 return false;
1075             }
1076             auto native = FFIData::Create<Media::PixelMapImpl>(pix);
1077             if (native == nullptr) {
1078                 LOGE("Invalid object pixelMap");
1079                 freeNotificationBasicContent(normal);
1080                 return false;
1081             }
1082             normal->lockscreenPicture = native->GetID();
1083         }
1084         return true;
1085     }
1086 
freeNotificationLongTextContent(CNotificationLongTextContent * longText)1087     static void freeNotificationLongTextContent(CNotificationLongTextContent* longText)
1088     {
1089         free(longText->title);
1090         free(longText->text);
1091         free(longText->additionalText);
1092         free(longText->longText);
1093         free(longText->briefText);
1094         free(longText->expandedTitle);
1095         longText->title = nullptr;
1096         longText->text = nullptr;
1097         longText->additionalText = nullptr;
1098         longText->longText = nullptr;
1099         longText->briefText = nullptr;
1100         longText->expandedTitle = nullptr;
1101     }
1102 
SetNotificationLongTextContent(NotificationBasicContent * basicContent,CNotificationLongTextContent * longText)1103     bool SetNotificationLongTextContent(
1104         NotificationBasicContent *basicContent,
1105         CNotificationLongTextContent* longText)
1106     {
1107         if (basicContent == nullptr) {
1108             LOGE("basicContent is null.");
1109             return false;
1110         }
1111         if (longText == nullptr) {
1112             LOGE("malloc CNotificationLongTextContent failed, longText is null.");
1113             return false;
1114         }
1115 
1116         OHOS::Notification::NotificationLongTextContent *longTextContent =
1117             static_cast<OHOS::Notification::NotificationLongTextContent *>(basicContent);
1118         if (longTextContent == nullptr) {
1119             LOGE("longTextContent is null");
1120             return false;
1121         }
1122         // title: string
1123         longText->title = MallocCString(longTextContent->GetTitle());
1124         // text: string
1125         longText->text = MallocCString(longTextContent->GetText());
1126         // additionalText?: string
1127         longText->additionalText = MallocCString(longTextContent->GetAdditionalText());
1128         // longText: string
1129         longText->longText = MallocCString(longTextContent->GetLongText());
1130         // briefText: string
1131         longText->briefText = MallocCString(longTextContent->GetBriefText());
1132         // expandedTitle: string
1133         longText->expandedTitle = MallocCString(longTextContent->GetExpandedTitle());
1134         // lockScreenPicture?: pixelMap
1135         longText->lockscreenPicture = -1;
1136         if (longTextContent->GetLockScreenPicture()) {
1137             std::shared_ptr<Media::PixelMap> pix = longTextContent->GetLockScreenPicture();
1138             if (pix == nullptr) {
1139                 LOGE("Invalid object pixelMap");
1140                 freeNotificationLongTextContent(longText);
1141                 return false;
1142             }
1143             auto native = FFIData::Create<Media::PixelMapImpl>(pix);
1144             if (native == nullptr) {
1145                 LOGE("Invalid object pixelMap");
1146                 freeNotificationLongTextContent(longText);
1147                 return false;
1148             }
1149             longText->lockscreenPicture = native->GetID();
1150         }
1151         return true;
1152     }
1153 
freeNotificationPictureContent(CNotificationPictureContent * picture)1154     static void freeNotificationPictureContent(CNotificationPictureContent* picture)
1155     {
1156         free(picture->title);
1157         free(picture->text);
1158         free(picture->additionalText);
1159         free(picture->briefText);
1160         free(picture->expandedTitle);
1161         picture->title = nullptr;
1162         picture->text = nullptr;
1163         picture->additionalText = nullptr;
1164         picture->briefText = nullptr;
1165         picture->expandedTitle = nullptr;
1166     }
1167 
SetNotificationPictureContent(NotificationBasicContent * basicContent,CNotificationPictureContent * picture)1168     bool SetNotificationPictureContent(NotificationBasicContent *basicContent,
1169         CNotificationPictureContent* picture)
1170     {
1171         if (basicContent == nullptr) {
1172             LOGE("basicContent is null");
1173             return false;
1174         }
1175         OHOS::Notification::NotificationPictureContent *pictureContent =
1176             static_cast<OHOS::Notification::NotificationPictureContent *>(basicContent);
1177         if (pictureContent == nullptr) {
1178             LOGE("pictureContent is null");
1179             return false;
1180         }
1181         // title、text: string
1182         picture->title = MallocCString(pictureContent->GetTitle());
1183         picture->text = MallocCString(pictureContent->GetText());
1184         // additionalText?: string
1185         picture->additionalText = MallocCString(pictureContent->GetAdditionalText());
1186         // briefText、expandedTitle: string
1187         picture->briefText = MallocCString(pictureContent->GetBriefText());
1188         picture->expandedTitle = MallocCString(pictureContent->GetExpandedTitle());
1189         // picture: image.PixelMap
1190         std::shared_ptr<Media::PixelMap> pix = pictureContent->GetBigPicture();
1191         if (pix == nullptr) {
1192             LOGE("Invalid object pixelMap");
1193             freeNotificationPictureContent(picture);
1194             return false;
1195         }
1196         auto native1 = FFIData::Create<Media::PixelMapImpl>(pix);
1197         if (native1 == nullptr) {
1198             LOGE("Invalid object pixelMap");
1199             freeNotificationPictureContent(picture);
1200             return false;
1201         }
1202         picture->picture = native1->GetID();
1203         // lockScreenPicture?: pixelMap
1204         picture->lockscreenPicture = -1;
1205         if (pictureContent->GetLockScreenPicture()) {
1206             std::shared_ptr<Media::PixelMap> pixx = pictureContent->GetLockScreenPicture();
1207             if (pixx == nullptr) {
1208                 LOGE("Invalid object pixelMap");
1209                 freeNotificationPictureContent(picture);
1210                 return false;
1211             }
1212             auto native2 = FFIData::Create<Media::PixelMapImpl>(pixx);
1213             if (native2 == nullptr) {
1214                 LOGE("Invalid object pixelMap");
1215                 freeNotificationPictureContent(picture);
1216                 return false;
1217             }
1218             picture->lockscreenPicture = native2->GetID();
1219         }
1220         return true;
1221     }
1222 
freeNotificationMultiLineContent(CNotificationMultiLineContent * multiLine)1223     static void freeNotificationMultiLineContent(CNotificationMultiLineContent* multiLine)
1224     {
1225         free(multiLine->title);
1226         free(multiLine->text);
1227         free(multiLine->additionalText);
1228         free(multiLine->briefText);
1229         free(multiLine->longTitle);
1230         if (multiLine->lines.head != nullptr) {
1231             for (int64_t i = 0; i < multiLine->lines.size; i++) {
1232                 free(multiLine->lines.head[i]);
1233             }
1234             free(multiLine->lines.head);
1235             multiLine->lines.head = nullptr;
1236         }
1237         multiLine->title = nullptr;
1238         multiLine->text = nullptr;
1239         multiLine->additionalText = nullptr;
1240         multiLine->briefText = nullptr;
1241         multiLine->longTitle = nullptr;
1242     }
1243 
SetNotificationMultiLineContent(NotificationBasicContent * basicContent,CNotificationMultiLineContent * multiLine)1244     bool SetNotificationMultiLineContent(
1245         NotificationBasicContent *basicContent,
1246         CNotificationMultiLineContent* multiLine)
1247     {
1248         if (basicContent == nullptr) {
1249             LOGE("basicContent is null");
1250             return false;
1251         }
1252         OHOS::Notification::NotificationMultiLineContent *multiLineContent =
1253             static_cast<OHOS::Notification::NotificationMultiLineContent *>(basicContent);
1254         if (multiLineContent == nullptr) {
1255             LOGE("multiLineContent is null");
1256             return false;
1257         }
1258         // title、text、additionalText?: string
1259         multiLine->title = MallocCString(multiLineContent->GetTitle());
1260         multiLine->text = MallocCString(multiLineContent->GetText());
1261         multiLine->additionalText = MallocCString(multiLineContent->GetAdditionalText());
1262         // briefText、longTitle: string
1263         multiLine->briefText = MallocCString(multiLineContent->GetBriefText());
1264         multiLine->longTitle = MallocCString(multiLineContent->GetExpandedTitle());
1265         // lines: Array<String>
1266         auto vecs = multiLineContent->GetAllLines();
1267         CArrString lines = { .head = nullptr, .size = 0 };
1268         lines.head = static_cast<char **>(malloc(sizeof(char *) * vecs.size()));
1269         lines.size = static_cast<int64_t>(vecs.size());
1270         if (lines.head == nullptr) {
1271             LOGE("multiLineContent lines malloc failed");
1272             freeNotificationMultiLineContent(multiLine);
1273             return false;
1274         }
1275         int i = 0 ;
1276         for (auto vec : vecs) {
1277             lines.head[i++] = MallocCString(vec);
1278         }
1279         multiLine->lines = lines;
1280         // lockScreenPicture?: pixelMap
1281         multiLine->lockscreenPicture = -1;
1282         if (multiLineContent->GetLockScreenPicture()) {
1283             std::shared_ptr<Media::PixelMap> pix = multiLineContent->GetLockScreenPicture();
1284             if (pix == nullptr) {
1285                 LOGE("Invalid object pixelMap");
1286                 freeNotificationMultiLineContent(multiLine);
1287                 return false;
1288             }
1289             auto native2 = FFIData::Create<Media::PixelMapImpl>(pix);
1290             if (native2 == nullptr) {
1291                 LOGE("Invalid object pixelMap");
1292                 freeNotificationMultiLineContent(multiLine);
1293                 return false;
1294             }
1295             multiLine->lockscreenPicture = native2->GetID();
1296         }
1297         return true;
1298     }
1299 
SetCapsule(const NotificationCapsule & capsule,CNotificationCapsule & cCapsule)1300     bool SetCapsule(const NotificationCapsule &capsule, CNotificationCapsule &cCapsule)
1301     {
1302         // title: string
1303         cCapsule.title = MallocCString(capsule.GetTitle());
1304         // backgroundColor: string
1305         cCapsule.backgroundColor = MallocCString(capsule.GetBackgroundColor());
1306         // icon?: image.PixelMap
1307         std::shared_ptr<Media::PixelMap> icon = capsule.GetIcon();
1308         if (icon) {
1309             auto native = FFIData::Create<Media::PixelMapImpl>(icon);
1310             if (native == nullptr) {
1311                 free(cCapsule.title);
1312                 free(cCapsule.backgroundColor);
1313                 cCapsule.title = nullptr;
1314                 cCapsule.backgroundColor = nullptr;
1315                 LOGE("Invalid object pixelMap of icon");
1316                 return false;
1317             }
1318             cCapsule.icon = native->GetID();
1319         }
1320         return true;
1321     }
1322 
SetButton(const NotificationLocalLiveViewButton & button,CNotificationButton & cButton)1323     bool SetButton(const NotificationLocalLiveViewButton &button, CNotificationButton &cButton)
1324     {
1325         // buttonNames: Array<String>
1326         auto vecs = button.GetAllButtonNames();
1327         CArrString names = { .head = nullptr, .size = 0 };
1328         if (vecs.size() > 0) {
1329             names.head = static_cast<char **>(malloc(sizeof(char *) * vecs.size()));
1330             names.size = static_cast<int64_t>(vecs.size());
1331             if (names.head == nullptr) {
1332                 LOGE("NotificationButton names malloc failed");
1333                 return false;
1334             }
1335             int i = 0;
1336             for (auto vec : vecs) {
1337                 names.head[i++] = MallocCString(vec);
1338             }
1339         }
1340         cButton.names = names;
1341 
1342         // buttonIcons: Array<PixelMap>
1343         int iconCount = 0;
1344         std::vector<std::shared_ptr<Media::PixelMap>> iconsVec = button.GetAllButtonIcons();
1345         CArrI64 icons = { .head = nullptr, .size = 0 };
1346         if (iconsVec.size()) {
1347             icons.head = static_cast<int64_t *>(malloc(sizeof(int64_t) * iconsVec.size()));
1348             if (icons.head == nullptr) {
1349                 LOGE("NotificationButton icons malloc failed");
1350                 return false;
1351             }
1352             for (auto vec : iconsVec) {
1353                 if (!vec) {
1354                     continue;
1355                 }
1356                 // buttonIcon
1357                 auto native = FFIData::Create<Media::PixelMapImpl>(vec);
1358                 if (native == nullptr) {
1359                     LOGE("Invalid object pixelMap of buttonIcons.");
1360                     return false; // memory free at cj
1361                 }
1362                 icons.head[iconCount++] = native->GetID();
1363             }
1364         }
1365         icons.size = static_cast<int64_t>(iconsVec.size());
1366         cButton.icons = icons;
1367         return true;
1368     }
1369 
SetNotificationLocalLiveViewContentDetailed(NotificationLocalLiveViewContent * localLiveViewContent,CNotificationSystemLiveViewContent * systemLiveView)1370     bool SetNotificationLocalLiveViewContentDetailed(NotificationLocalLiveViewContent *localLiveViewContent,
1371         CNotificationSystemLiveViewContent* systemLiveView)
1372     {
1373         // capsule: NotificationCapsule
1374         CNotificationCapsule capsule = {
1375             .title = nullptr,
1376             .icon = -1,
1377             .backgroundColor = nullptr
1378         };
1379         if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE)) {
1380             if (!SetCapsule(localLiveViewContent->GetCapsule(), capsule)) {
1381                 LOGE("SetCapsule call failed");
1382                 return false;
1383             }
1384         }
1385         systemLiveView->capsule = capsule;
1386 
1387         // button: NotificationLocalLiveViewButton
1388         CNotificationButton cButton = {
1389             .names = { .head = nullptr, .size = 0 },
1390             .icons = { .head = nullptr, .size = 0 }
1391         };
1392         if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON)) {
1393             if (!SetButton(localLiveViewContent->GetButton(), cButton)) {
1394                 LOGE("SetButton call failed");
1395                 return false;
1396             }
1397         }
1398         systemLiveView->button = cButton;
1399 
1400         // progress: NotificationProgress
1401         CNotificationProgress cProgress;
1402         if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS)) {
1403             NotificationProgress progress = localLiveViewContent->GetProgress();
1404             cProgress.maxValue = progress.GetMaxValue();
1405             cProgress.currentValue = progress.GetCurrentValue();
1406             cProgress.isPercentage = progress.GetIsPercentage();
1407         }
1408         systemLiveView->progress = cProgress;
1409 
1410         // time: NotificationTime
1411         CNotificationTime cTime;
1412         if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::TIME)) {
1413             NotificationTime time = localLiveViewContent->GetTime();
1414             bool flag = localLiveViewContent->isFlagExist(
1415                 NotificationLocalLiveViewContent::LiveViewContentInner::INITIAL_TIME);
1416             cTime.initialTime = flag ? time.GetInitialTime() : 0;
1417             cTime.isCountDown = time.GetIsCountDown();
1418             cTime.isPaused = time.GetIsPaused();
1419             cTime.isInTitle = time.GetIsInTitle();
1420         }
1421         systemLiveView->time = cTime;
1422 
1423         return true;
1424     }
1425 
SetNotificationLocalLiveViewContent(NotificationBasicContent * basicContent,CNotificationSystemLiveViewContent * systemLiveView)1426     bool SetNotificationLocalLiveViewContent(NotificationBasicContent *basicContent,
1427         CNotificationSystemLiveViewContent* systemLiveView)
1428     {
1429         if (basicContent == nullptr) {
1430             LOGE("basicContent is null.");
1431             return false;
1432         }
1433         if (systemLiveView == nullptr) {
1434             LOGE("malloc CNotificationSystemLiveViewContent failed, systemLiveView is null");
1435             return false;
1436         }
1437         OHOS::Notification::NotificationLocalLiveViewContent *localLiveViewContent =
1438             static_cast<OHOS::Notification::NotificationLocalLiveViewContent *>(basicContent);
1439         if (localLiveViewContent == nullptr) {
1440             LOGE("localLiveViewContent is null");
1441             return false;
1442         }
1443 
1444         // title, text, additionalText?
1445         systemLiveView->title = MallocCString(localLiveViewContent->GetTitle());
1446         systemLiveView->text = MallocCString(localLiveViewContent->GetText());
1447         systemLiveView->additionalText = MallocCString(localLiveViewContent->GetAdditionalText());
1448         // typeCode: int32_t
1449         systemLiveView->typeCode = localLiveViewContent->GetType();
1450 
1451         if (!SetNotificationLocalLiveViewContentDetailed(localLiveViewContent, systemLiveView)) {
1452             LOGE("SetNotificationLocalLiveViewContentDetail call failed");
1453             return false;
1454         }
1455 
1456         // lockScreenPicture?: pixelMap
1457         systemLiveView->lockscreenPicture = -1;
1458         if (localLiveViewContent->GetLockScreenPicture()) {
1459             std::shared_ptr<Media::PixelMap> pix = localLiveViewContent->GetLockScreenPicture();
1460             if (pix == nullptr) {
1461                 LOGE("Invalid object pixelMap");
1462                 return false;
1463             }
1464             auto native2 = FFIData::Create<Media::PixelMapImpl>(pix);
1465             if (native2 == nullptr) {
1466                 LOGE("Invalid object pixelMap");
1467                 return false;
1468             }
1469             systemLiveView->lockscreenPicture = native2->GetID();
1470         }
1471         return true;
1472     }
1473 
SetNotificationContentDetailed(const ContentType & type,const std::shared_ptr<NotificationContent> & content,CNotificationContent & notificationContent)1474     bool SetNotificationContentDetailed(const ContentType &type,
1475         const std::shared_ptr<NotificationContent> &content, CNotificationContent &notificationContent)
1476     {
1477         bool ret = false;
1478         std::shared_ptr<NotificationBasicContent> basicContent = content->GetNotificationContent();
1479         if (basicContent == nullptr) {
1480             LOGE("content is null");
1481             return ret;
1482         }
1483         switch (type) {
1484             case ContentType::NOTIFICATION_CONTENT_BASIC_TEXT: // normal?: NotificationBasicContent
1485                 notificationContent.normal =
1486                     static_cast<CNotificationBasicContent *>(malloc(sizeof(CNotificationBasicContent)));
1487                 ret = SetNotificationBasicContent(basicContent.get(), notificationContent.normal);
1488                 break;
1489             case ContentType::NOTIFICATION_CONTENT_LONG_TEXT: // longText?: NotificationLongTextContent
1490                 notificationContent.longText =
1491                     static_cast<CNotificationLongTextContent *>(malloc(sizeof(CNotificationLongTextContent)));
1492                 ret = SetNotificationLongTextContent(basicContent.get(), notificationContent.longText);
1493                 break;
1494             case ContentType::NOTIFICATION_CONTENT_PICTURE: // picture?: NotificationPictureContent
1495                 notificationContent.picture =
1496                     static_cast<CNotificationPictureContent *>(malloc(sizeof(CNotificationPictureContent)));
1497                 if (notificationContent.picture == nullptr) {
1498                     LOGE("SetNotificationContentDetailed malloc CNotificationPictureContent failed.");
1499                     return false;
1500                 }
1501                 ret = SetNotificationPictureContent(basicContent.get(), notificationContent.picture);
1502                 break;
1503             case ContentType::NOTIFICATION_CONTENT_MULTILINE: // multiLine?: NotificationMultiLineContent
1504                 notificationContent.multiLine =
1505                     static_cast<CNotificationMultiLineContent *>(malloc(sizeof(CNotificationMultiLineContent)));
1506                 if (notificationContent.multiLine == nullptr) {
1507                     LOGE("SetNotificationContentDetailed malloc CNotificationMultiLineContent failed.");
1508                     return false;
1509                 }
1510                 ret = SetNotificationMultiLineContent(basicContent.get(), notificationContent.multiLine);
1511                 break;
1512             case ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW: // systemLiveView?: NotificationLocalLiveViewContent
1513                 notificationContent.systemLiveView = static_cast<CNotificationSystemLiveViewContent *>(
1514                             malloc(sizeof(CNotificationSystemLiveViewContent)));
1515                 ret = SetNotificationLocalLiveViewContent(basicContent.get(), notificationContent.systemLiveView);
1516                 break;
1517             case ContentType::NOTIFICATION_CONTENT_LIVE_VIEW: // liveView?: NotificationLiveViewContent
1518                 LOGE("ContentType::NOTIFICATION_CONTENT_LIVE_VIEW is not support");
1519                 break;
1520             default:
1521                 LOGE("ContentType is does not exist");
1522                 return ret;
1523         }
1524         return ret;
1525     }
1526 
SetNotificationContent(const std::shared_ptr<NotificationContent> & content,CNotificationContent & notificationContent)1527     bool SetNotificationContent(
1528         const std::shared_ptr<NotificationContent> &content,
1529         CNotificationContent &notificationContent)
1530     {
1531         // contentType: ContentType
1532         NotificationContent::Type type = content->GetContentType();
1533         ContentType outType = ContentType::NOTIFICATION_CONTENT_BASIC_TEXT;
1534         if (!ContentTypeCToCJ(type, outType)) {
1535             return false;
1536         }
1537         notificationContent.notificationContentType = static_cast<int32_t>(outType);
1538         if (!SetNotificationContentDetailed(outType, content, notificationContent)) {
1539             LOGE("SetNotificationContentDetailed failed");
1540             return false;
1541         }
1542         return true;
1543     }
1544 
SetNotificationFlags(const std::shared_ptr<NotificationFlags> & flags,CNotificationFlags & notificationFlags)1545     bool SetNotificationFlags(
1546         const std::shared_ptr<NotificationFlags> &flags,
1547         CNotificationFlags &notificationFlags)
1548     {
1549         if (flags == nullptr) {
1550             LOGE("flags is null");
1551             return false;
1552         }
1553         notificationFlags.soundEnabled = static_cast<int32_t>(flags->IsSoundEnabled());
1554         notificationFlags.vibrationEnabled = static_cast<int32_t>(flags->IsVibrationEnabled());
1555         return true;
1556     }
1557 
SetNotificationRequestByCustom(const NotificationRequest * request,CNotificationRequest & notificationRequest)1558     bool SetNotificationRequestByCustom(
1559         const NotificationRequest *request,
1560         CNotificationRequest &notificationRequest)
1561     {
1562         // content: NotificationContent
1563         std::shared_ptr<NotificationContent> content = request->GetContent();
1564         if (!content) {
1565             LOGE("content is nullptr");
1566             return false;
1567         }
1568         if (!SetNotificationContent(content, notificationRequest.notificationContent)) {
1569             LOGE("SetNotificationContent call failed");
1570             return false;
1571         }
1572 
1573         // readonly notificationFlags?: NotificationFlags
1574         std::shared_ptr<NotificationFlags> flags = request->GetFlags();
1575         if (flags) {
1576             if (!SetNotificationFlags(flags, notificationRequest.notificationFlags)) {
1577                 LOGE("SetNotificationFlags call failed");
1578                 return false;
1579             }
1580         }
1581         return true;
1582     }
1583 
InitNotificationRequest(CNotificationRequest & notificationRequest)1584     static void InitNotificationRequest(CNotificationRequest &notificationRequest)
1585     {
1586         notificationRequest.notificationContent = {
1587             .notificationContentType = 0,
1588             .normal = nullptr,
1589             .longText = nullptr,
1590             .multiLine = nullptr,
1591             .picture = nullptr
1592         };
1593         notificationRequest.label = nullptr;
1594         notificationRequest.creatorBundleName = nullptr;
1595         notificationRequest.groupName = nullptr;
1596         notificationRequest.distributedOption = nullptr;
1597         notificationRequest.hashCode = nullptr;
1598         notificationRequest.appMessageId = nullptr;
1599     }
1600 
SetNotificationRequest(const NotificationRequest * request,CNotificationRequest & notificationRequest)1601     bool SetNotificationRequest(
1602         const NotificationRequest *request,
1603         CNotificationRequest &notificationRequest)
1604     {
1605         if (request == nullptr) {
1606             LOGE("request is nullptr");
1607             return false;
1608         }
1609         InitNotificationRequest(notificationRequest);
1610         SetNotificationRequestByString(request, notificationRequest);
1611         SetNotificationRequestByBool(request, notificationRequest);
1612         SetNotificationRequestByPixelMap(request, notificationRequest);
1613         if (!SetNotificationRequestByNumber(request, notificationRequest)) {
1614             LOGE("SetNotificationRequestByNumber failed");
1615             return false;
1616         }
1617         if (!SetNotificationRequestByCustom(request, notificationRequest)) {
1618             LOGE("SetNotificationRequestByCustom failed");
1619             return false;
1620         }
1621         return true;
1622     }
1623 }
1624 }
1625 }