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