1 /*
2 * Copyright (C) 2025 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 "distributed_publish_service.h"
17
18 #include <memory>
19 #include <string>
20 #include <sstream>
21
22 #include "request_box.h"
23 #include "remove_box.h"
24 #include "batch_remove_box.h"
25 #include "notification_sync_box.h"
26 #include "analytics_util.h"
27 #include "ans_image_util.h"
28 #include "distributed_client.h"
29 #include "notification_helper.h"
30 #include "in_process_call_wrapper.h"
31 #include "distributed_device_service.h"
32 #include "distributed_data_define.h"
33 #include "distributed_preference.h"
34 #include "distributed_liveview_all_scenarios_extension_wrapper.h"
35 #include "bool_wrapper.h"
36 #include "int_wrapper.h"
37 #include "string_wrapper.h"
38 #include "want_params_wrapper.h"
39 #include "distributed_subscribe_service.h"
40 #include "remove_all_distributed_box.h"
41 #include "bundle_resource_helper.h"
42 #include "distributed_service.h"
43 #include "distributed_send_adapter.h"
44
45 namespace OHOS {
46 namespace Notification {
47
48 static const std::string DISTRIBUTED_LABEL = "ans_distributed";
49 static const std::string EXTENDINFO_INFO_PRE = "notification_collaboration_";
50 static const std::string EXTENDINFO_FLAG = "flag";
51 static const std::string EXTENDINFO_USERID = "userId_";
52 static const std::string EXTENDINFO_APP_NAME = "app_name";
53 static const std::string EXTENDINFO_APP_LABEL = "app_label";
54 static const std::string EXTENDINFO_APP_ICON = "app_icon";
55 static const std::string EXTENDINFO_APP_INDEX = "app_index";
56 static const std::string EXTENDINFO_DEVICE_USERID = "userId";
57 static const std::string EXTENDINFO_DEVICE_ID = "deviceId";
58 static const std::string EXTENDINFO_ENABLE_CHECK = "check";
59 static const std::string EXTENDINFO_DEVICETYPE = "deviceType";
60 static const std::string EXTENDINFO_LOCALTYPE = "localType";
61 static const uint32_t UNLOCKED_USED_FLAG = 3;
62
GetInstance()63 DistributedPublishService& DistributedPublishService::GetInstance()
64 {
65 static DistributedPublishService distributedPublishService;
66 return distributedPublishService;
67 }
68
RemoveNotification(const std::shared_ptr<TlvBox> & boxMessage)69 void DistributedPublishService::RemoveNotification(const std::shared_ptr<TlvBox>& boxMessage)
70 {
71 std::string hashCode;
72 int32_t slotType;
73 NotificationRemoveBox removeBox = NotificationRemoveBox(boxMessage);
74 removeBox.GetNotificationHashCode(hashCode);
75 removeBox.GetNotificationSlotType(slotType);
76
77 if (hashCode.empty()) {
78 ANS_LOGW("dans remove hashCode empty");
79 return;
80 }
81 std::string deviceId;
82 removeBox.GetLocalDeviceId(deviceId);
83 #ifdef DISTRIBUTED_FEATURE_MASTER
84 std::shared_ptr<NotificationRemoveBox> forwardBox = MakeRemvoeBox(hashCode, slotType);
85 if (forwardBox != nullptr) {
86 ForWardRemove(forwardBox, deviceId);
87 }
88 #else
89 #endif
90 std::vector<std::string> hashCodes;
91 hashCodes.push_back(hashCode);
92
93 int result = RemoveDistributedNotifications(hashCodes);
94 std::string errorReason = "delete message failed";
95 if (result == 0) {
96 errorReason = "delete message success";
97 int32_t deviceType = DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH;
98 DistributedDeviceInfo device;
99 if (DistributedDeviceService::GetInstance().GetDeviceInfo(deviceId, device)) {
100 deviceType = device.deviceType_;
101 }
102 AnalyticsUtil::GetInstance().AbnormalReporting(DELETE_ERROR_EVENT_CODE, result, BRANCH4_ID, errorReason);
103 AnalyticsUtil::GetInstance().OperationalReporting(deviceType, HaOperationType::COLLABORATE_DELETE, slotType);
104 } else {
105 AnalyticsUtil::GetInstance().AbnormalReporting(DELETE_ERROR_EVENT_CODE, result, BRANCH3_ID, errorReason);
106 }
107 ANS_LOGI("dans remove message %{public}d.", result);
108 }
109
RemoveNotifications(const std::shared_ptr<TlvBox> & boxMessage)110 void DistributedPublishService::RemoveNotifications(const std::shared_ptr<TlvBox>& boxMessage)
111 {
112 BatchRemoveNotificationBox removeBox = BatchRemoveNotificationBox(boxMessage);
113 std::vector<std::string> hashCodes;
114 std::string hashCodesString;
115 removeBox.GetNotificationHashCodes(hashCodesString);
116 if (hashCodesString.empty()) {
117 ANS_LOGW("dans remove hashCodesString empty");
118 return;
119 }
120 std::string slotTypesString;
121 removeBox.GetNotificationSlotTypes(slotTypesString);
122 std::istringstream hashCodesStream(hashCodesString);
123 std::string hashCode;
124 while (hashCodesStream >> hashCode) {
125 if (!hashCode.empty()) {
126 hashCodes.push_back(hashCode);
127 }
128 }
129 std::string deviceId;
130 removeBox.GetLocalDeviceId(deviceId);
131 #ifdef DISTRIBUTED_FEATURE_MASTER
132 std::shared_ptr<BatchRemoveNotificationBox> forwardBox = MakeBatchRemvoeBox(hashCodes, slotTypesString);
133 if (forwardBox != nullptr) {
134 ForWardRemove(forwardBox, deviceId);
135 }
136 #else
137 #endif
138
139 int result = RemoveDistributedNotifications(hashCodes);
140 BatchRemoveReport(slotTypesString, deviceId, result);
141 ANS_LOGI("dans br re:%{public}d., hs:%{public}s", result, hashCodesString.c_str());
142 }
143
RemoveDistributedNotifications(const std::vector<std::string> & hashcodes)144 int DistributedPublishService::RemoveDistributedNotifications(const std::vector<std::string>& hashcodes)
145 {
146 int res = 0;
147 auto local = DistributedDeviceService::GetInstance().GetLocalDevice();
148 if (local.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
149 res = IN_PROCESS_CALL(NotificationHelper::RemoveNotifications(
150 hashcodes, NotificationConstant::DISTRIBUTED_COLLABORATIVE_DELETE));
151 } else {
152 res = IN_PROCESS_CALL(NotificationHelper::RemoveDistributedNotifications(hashcodes,
153 NotificationConstant::SlotType::SOCIAL_COMMUNICATION,
154 NotificationConstant::DistributedDeleteType::HASHCODES,
155 NotificationConstant::DISTRIBUTED_COLLABORATIVE_DELETE));
156 }
157 return res;
158 }
159
BatchRemoveReport(const std::string & slotTypesString,const std::string & deviceId,const int result)160 void DistributedPublishService::BatchRemoveReport(const std::string &slotTypesString, const std::string &deviceId,
161 const int result)
162 {
163 int32_t deviceType = DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH;
164 DistributedDeviceInfo device;
165 if (DistributedDeviceService::GetInstance().GetDeviceInfo(deviceId, device)) {
166 deviceType = device.deviceType_;
167 }
168 if (result == 0) {
169 AnalyticsUtil::GetInstance().AbnormalReporting(DELETE_ERROR_EVENT_CODE, result, BRANCH4_ID,
170 "delete message success");
171 std::istringstream slotTypesStream(slotTypesString);
172 std::string slotTypeString;
173 while (slotTypesStream >> slotTypeString) {
174 if (!slotTypeString.empty()) {
175 AnalyticsUtil::GetInstance().OperationalReporting(deviceType,
176 HaOperationType::COLLABORATE_DELETE, atoi(slotTypeString.c_str()));
177 }
178 }
179 } else {
180 AnalyticsUtil::GetInstance().AbnormalReporting(DELETE_ERROR_EVENT_CODE, result, BRANCH3_ID,
181 "delete message failed");
182 }
183 }
184
OnRemoveNotification(const DistributedDeviceInfo & peerDevice,std::string hashCode,int32_t slotTypes)185 void DistributedPublishService::OnRemoveNotification(const DistributedDeviceInfo& peerDevice,
186 std::string hashCode, int32_t slotTypes)
187 {
188 std::shared_ptr<NotificationRemoveBox> removeBox = std::make_shared<NotificationRemoveBox>();
189 if (removeBox == nullptr) {
190 ANS_LOGE("create batchRemoveBox err");
191 return;
192 }
193
194 ANS_LOGI("dans OnCanceled %{public}s", hashCode.c_str());
195 removeBox->SetNotificationHashCode(hashCode);
196 removeBox->SetNotificationSlotType(slotTypes);
197 auto local = DistributedDeviceService::GetInstance().GetLocalDevice();
198 removeBox->SetLocalDeviceId(local.deviceId_);
199
200 if (!removeBox->Serialize()) {
201 ANS_LOGW("dans OnCanceled serialize failed");
202 return;
203 }
204 TransDataType dataType = TransDataType::DATA_TYPE_MESSAGE;
205 if (peerDevice.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
206 dataType = TransDataType::DATA_TYPE_BYTES;
207 }
208 std::shared_ptr<PackageInfo> packageInfo = std::make_shared<PackageInfo>(removeBox, peerDevice,
209 dataType, DELETE_ERROR_EVENT_CODE);
210 DistributedSendAdapter::GetInstance().SendPackage(packageInfo);
211 }
212
OnRemoveNotifications(const DistributedDeviceInfo & peerDevice,std::string hashCodes,std::string slotTypes)213 void DistributedPublishService::OnRemoveNotifications(const DistributedDeviceInfo& peerDevice,
214 std::string hashCodes, std::string slotTypes)
215 {
216 std::shared_ptr<BatchRemoveNotificationBox> batchRemoveBox = std::make_shared<BatchRemoveNotificationBox>();
217 if (batchRemoveBox == nullptr) {
218 ANS_LOGE("create batchRemoveBox err");
219 return;
220 }
221 batchRemoveBox->SetNotificationHashCodes(hashCodes);
222 batchRemoveBox->SetNotificationSlotTypes(slotTypes);
223 auto local = DistributedDeviceService::GetInstance().GetLocalDevice();
224 batchRemoveBox->SetLocalDeviceId(local.deviceId_);
225
226 if (!batchRemoveBox->Serialize()) {
227 ANS_LOGW("dans OnCanceled serialize failed");
228 return;
229 }
230 TransDataType dataType = TransDataType::DATA_TYPE_MESSAGE;
231 if (peerDevice.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
232 dataType = TransDataType::DATA_TYPE_BYTES;
233 }
234 std::shared_ptr<PackageInfo> packageInfo = std::make_shared<PackageInfo>(batchRemoveBox, peerDevice,
235 dataType, DELETE_ERROR_EVENT_CODE);
236 DistributedSendAdapter::GetInstance().SendPackage(packageInfo);
237 }
238
239 #ifdef DISTRIBUTED_FEATURE_MASTER
RemoveAllDistributedNotifications(DistributedDeviceInfo & deviceInfo)240 void DistributedPublishService::RemoveAllDistributedNotifications(DistributedDeviceInfo& deviceInfo)
241 {
242 std::shared_ptr<RemoveAllDistributedNotificationsBox> removeBox =
243 std::make_shared<RemoveAllDistributedNotificationsBox>();
244 if (removeBox == nullptr) {
245 ANS_LOGW("create box error");
246 return;
247 }
248 auto local = DistributedDeviceService::GetInstance().GetLocalDevice();
249 removeBox->SetLocalDeviceId(local.deviceId_);
250
251 if (!removeBox->Serialize()) {
252 ANS_LOGW("dans OnCanceled serialize failed");
253 return;
254 }
255 ANS_LOGI("Remove all:%{public}s", StringAnonymous(deviceInfo.deviceId_).c_str());
256 std::shared_ptr<PackageInfo> packageInfo = std::make_shared<PackageInfo>(removeBox, deviceInfo,
257 TransDataType::DATA_TYPE_BYTES, DELETE_ERROR_EVENT_CODE);
258 DistributedSendAdapter::GetInstance().SendPackage(packageInfo);
259 }
260
ForWardRemove(const std::shared_ptr<BoxBase> & boxMessage,std::string & deviceId)261 bool DistributedPublishService::ForWardRemove(const std::shared_ptr<BoxBase>& boxMessage,
262 std::string& deviceId)
263 {
264 auto local = DistributedDeviceService::GetInstance().GetLocalDevice();
265 if (local.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
266 ANS_LOGD("no need forward");
267 return false;
268 }
269 std::map<std::string, DistributedDeviceInfo> peerDevices;
270 DistributedDeviceService::GetInstance().GetDeviceList(peerDevices);
271 if (peerDevices.empty()) {
272 ANS_LOGW("no peerDevices");
273 return false;
274 }
275
276 for (auto peerDevice : peerDevices) {
277 auto peerDeviceInfo = peerDevice.second;
278 if (peerDeviceInfo.deviceId_ == deviceId) {
279 ANS_LOGD("no need ForWardRemove");
280 continue;
281 }
282 std::shared_ptr<PackageInfo> packageInfo = std::make_shared<PackageInfo>(boxMessage, peerDeviceInfo,
283 TransDataType::DATA_TYPE_BYTES, DELETE_ERROR_EVENT_CODE);
284 DistributedSendAdapter::GetInstance().SendPackage(packageInfo);
285 ANS_LOGI("ForWardRemove,deviceId:%{public}s", StringAnonymous(peerDeviceInfo.deviceId_).c_str());
286 }
287 return true;
288 }
289
MakeRemvoeBox(std::string & hashCode,int32_t & slotTypes)290 std::shared_ptr<NotificationRemoveBox> DistributedPublishService::MakeRemvoeBox(
291 std::string &hashCode, int32_t &slotTypes)
292 {
293 std::shared_ptr<NotificationRemoveBox> removeBox = std::make_shared<NotificationRemoveBox>();
294 if (removeBox == nullptr) {
295 ANS_LOGE("MakeRemvoeBox ERR");
296 return nullptr;
297 }
298 removeBox->SetNotificationHashCode(DISTRIBUTED_LABEL + hashCode);
299 removeBox->SetNotificationSlotType(slotTypes);
300
301 if (!removeBox->Serialize()) {
302 ANS_LOGW("dans OnCanceled serialize failed");
303 return nullptr;
304 }
305
306 return removeBox;
307 }
308
MakeBatchRemvoeBox(std::vector<std::string> & hashCodes,std::string & slotTypes)309 std::shared_ptr<BatchRemoveNotificationBox> DistributedPublishService::MakeBatchRemvoeBox(
310 std::vector<std::string>& hashCodes, std::string &slotTypes)
311 {
312 std::shared_ptr<BatchRemoveNotificationBox> batchRemoveBox = std::make_shared<BatchRemoveNotificationBox>();
313 if (batchRemoveBox == nullptr) {
314 ANS_LOGE("MakeBatchRemvoeBox ERR");
315 return nullptr;
316 }
317 std::ostringstream keysStream;
318 for (auto hashCode : hashCodes) {
319 auto key = DISTRIBUTED_LABEL + hashCode;
320 keysStream << key << ' ';
321 }
322 std::string hashCodeStrings = keysStream.str();
323 batchRemoveBox->SetNotificationHashCodes(hashCodeStrings);
324 batchRemoveBox->SetNotificationSlotTypes(slotTypes);
325
326 if (!batchRemoveBox->Serialize()) {
327 ANS_LOGW("dans OnCanceled serialize failed");
328 return nullptr;
329 }
330 return batchRemoveBox;
331 }
332
SyncLiveViewList(const DistributedDeviceInfo device,const std::vector<sptr<Notification>> & notifications)333 void DistributedPublishService::SyncLiveViewList(const DistributedDeviceInfo device,
334 const std::vector<sptr<Notification>>& notifications)
335 {
336 if (device.IsPadOrPc()) {
337 ANS_LOGI("Dans no need sync list.");
338 return;
339 }
340
341 std::vector<std::string> notificationList;
342 for (auto& notification : notifications) {
343 if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr ||
344 !notification->GetNotificationRequestPoint()->IsCommonLiveView()) {
345 ANS_LOGI("Dans no need sync remove notification.");
346 continue;
347 }
348 notificationList.push_back(notification->GetKey());
349 }
350 SyncNotifictionList(device, notificationList);
351 }
352
SyncLiveViewContent(const DistributedDeviceInfo device,const std::vector<sptr<Notification>> & notifications)353 void DistributedPublishService::SyncLiveViewContent(const DistributedDeviceInfo device,
354 const std::vector<sptr<Notification>>& notifications)
355 {
356 std::vector<std::string> labelList;
357 std::vector<std::string> bundlesList;
358 bool checkBundleExist = false;
359 if (device.IsPadOrPc()) {
360 std::string deviceType = DistributedDeviceService::DeviceTypeToTypeString(device.deviceType_);
361 if (deviceType.empty()) {
362 ANS_LOGW("Dans %{public}s %{public}u.", StringAnonymous(device.deviceId_).c_str(), device.deviceType_);
363 return;
364 }
365 if (NotificationHelper::GetTargetDeviceBundleList(deviceType, device.udid_, bundlesList, labelList) != ERR_OK) {
366 ANS_LOGW("Get %{public}s %{public}u.", StringAnonymous(device.deviceId_).c_str(), device.deviceType_);
367 return;
368 }
369 ANS_LOGI("Get bundles size %{public}zu.", bundlesList.size());
370 checkBundleExist = true;
371 }
372
373 std::unordered_set<std::string> labelSet(labelList.begin(), labelList.end());
374 std::unordered_set<std::string> bundleSet(bundlesList.begin(), bundlesList.end());
375 for (auto& notification : notifications) {
376 if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr ||
377 !notification->GetNotificationRequestPoint()->IsCommonLiveView()) {
378 continue;
379 }
380
381 auto requestPoint = notification->GetNotificationRequestPoint();
382 if (checkBundleExist) {
383 std::string bundleName = requestPoint->GetOwnerBundleName();
384 if (bundleSet.count(bundleName)) {
385 ANS_LOGI("Dans no need sync %{public}d %{public}s.", checkBundleExist, bundleName.c_str());
386 continue;
387 }
388 int32_t userId = requestPoint->GetOwnerUserId();
389 if (DelayedSingleton<BundleResourceHelper>::GetInstance()->CheckSystemApp(bundleName, userId)) {
390 continue;
391 }
392
393 AppExecFwk::BundleResourceInfo resourceInfo;
394 if (DelayedSingleton<BundleResourceHelper>::GetInstance()->GetBundleInfo(bundleName, resourceInfo)
395 != ERR_OK) {
396 ANS_LOGW("Dans get bundle failed %{public}s.", bundleName.c_str());
397 continue;
398 }
399
400 if (checkBundleExist && labelSet.count(resourceInfo.label)) {
401 ANS_LOGI("Bundle system no sycn %{public}s.", bundleName.c_str());
402 continue;
403 }
404 }
405
406 std::shared_ptr<Notification> sharedNotification = std::make_shared<Notification>(*notification);
407 SendNotifictionRequest(sharedNotification, device, true);
408 }
409 }
410
SyncLiveViewNotification(const DistributedDeviceInfo peerDevice,bool isForce)411 void DistributedPublishService::SyncLiveViewNotification(const DistributedDeviceInfo peerDevice, bool isForce)
412 {
413 if (!DistributedDeviceService::GetInstance().IsSyncLiveView(peerDevice.deviceId_, isForce)) {
414 return;
415 }
416
417 DistributedDeviceInfo device;
418 if (!DistributedDeviceService::GetInstance().GetDeviceInfo(peerDevice.deviceId_, device)) {
419 return;
420 }
421 // wearable switch set by litewearable
422 std::string deviceType = DistributedDeviceService::DeviceTypeToTypeString(device.deviceType_);
423 if (deviceType == DistributedService::WEARABLE_DEVICE_TYPE) {
424 deviceType = DistributedService::LITEWEARABLE_DEVICE_TYPE;
425 }
426
427 bool enable = false;
428 auto result = NotificationHelper::IsDistributedEnabledBySlot(NotificationConstant::SlotType::LIVE_VIEW,
429 deviceType, enable);
430 if (result != ERR_OK || !enable) {
431 ANS_LOGW("Dans get switch %{public}s failed %{public}d.", deviceType.c_str(), result);
432 return;
433 }
434
435 std::vector<sptr<Notification>> notifications;
436 result = NotificationHelper::GetAllNotificationsBySlotType(notifications,
437 NotificationConstant::SlotType::LIVE_VIEW);
438 if (result != ERR_OK) {
439 ANS_LOGI("Dans get all active %{public}d.", result);
440 return;
441 }
442
443 SyncLiveViewList(device, notifications);
444 SyncLiveViewContent(device, notifications);
445 DistributedDeviceService::GetInstance().SetDeviceSyncData(device.deviceId_,
446 DistributedDeviceService::SYNC_LIVE_VIEW, true);
447 }
448
SyncNotifictionList(const DistributedDeviceInfo & peerDevice,const std::vector<std::string> & notificationList)449 void DistributedPublishService::SyncNotifictionList(const DistributedDeviceInfo& peerDevice,
450 const std::vector<std::string>& notificationList)
451 {
452 ANS_LOGI("Dans sync notification %{public}zu.", notificationList.size());
453 std::shared_ptr<NotificationSyncBox> notificationSyncBox = std::make_shared<NotificationSyncBox>();
454 notificationSyncBox->SetLocalDeviceId(peerDevice.deviceId_);
455 notificationSyncBox->SetNotificationEmpty(notificationList.empty());
456 if (!notificationList.empty()) {
457 notificationSyncBox->SetNotificationList(notificationList);
458 }
459
460 if (!notificationSyncBox->Serialize()) {
461 ANS_LOGW("Dans SyncNotifictionList serialize failed.");
462 return;
463 }
464 std::shared_ptr<PackageInfo> packageInfo = std::make_shared<PackageInfo>(notificationSyncBox, peerDevice,
465 TransDataType::DATA_TYPE_BYTES, PUBLISH_ERROR_EVENT_CODE);
466 DistributedSendAdapter::GetInstance().SendPackage(packageInfo);
467 ANS_LOGI("Dans SyncNotifictionList %{public}s %{public}d.",
468 StringAnonymous(peerDevice.deviceId_).c_str(), peerDevice.deviceType_);
469 }
470
SendNotifictionRequest(const std::shared_ptr<Notification> request,const DistributedDeviceInfo & peerDevice,bool isSyncNotification)471 void DistributedPublishService::SendNotifictionRequest(const std::shared_ptr<Notification> request,
472 const DistributedDeviceInfo& peerDevice, bool isSyncNotification)
473 {
474 std::shared_ptr<NotificationRequestBox> requestBox = std::make_shared<NotificationRequestBox>();
475 if (request == nullptr || request->GetNotificationRequestPoint() == nullptr) {
476 return;
477 }
478
479 auto requestPoint = request->GetNotificationRequestPoint();
480 ANS_LOGI("Dans OnConsumed Notification key = %{public}s, notificationFlag = %{public}s", request->GetKey().c_str(),
481 requestPoint->GetFlags() == nullptr ? "null" : requestPoint->GetFlags()->Dump().c_str());
482 auto local = DistributedDeviceService::GetInstance().GetLocalDevice();
483 requestBox->SetDeviceId(local.deviceId_);
484 requestBox->SetAutoDeleteTime(requestPoint->GetAutoDeletedTime());
485 requestBox->SetFinishTime(requestPoint->GetFinishDeadLine());
486 requestBox->SetNotificationHashCode(request->GetKey());
487 requestBox->SetSlotType(static_cast<int32_t>(requestPoint->GetSlotType()));
488 requestBox->SetContentType(static_cast<int32_t>(requestPoint->GetNotificationType()));
489
490 int32_t reminderFlag = isSyncNotification ? 0 : requestPoint->GetFlags()->GetReminderFlags();
491 requestBox->SetReminderFlag(reminderFlag);
492 if (!requestPoint->GetAppMessageId().empty()) {
493 requestBox->SetAppMessageId(requestPoint->GetAppMessageId());
494 }
495 if (request->GetBundleName().empty()) {
496 requestBox->SetCreatorBundleName(request->GetCreateBundle());
497 } else {
498 requestBox->SetCreatorBundleName(request->GetBundleName());
499 }
500 if (requestPoint->IsCommonLiveView()) {
501 std::vector<uint8_t> buffer;
502 std::string deviceType = DistributedDeviceService::DeviceTypeToTypeString(peerDevice.deviceType_);
503 DISTRIBUTED_LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UpdateLiveviewEncodeContent(
504 requestPoint, buffer, deviceType);
505 requestBox->SetCommonLiveView(buffer);
506 }
507 if (!SetNotificationExtendInfo(requestPoint, peerDevice.deviceType_, isSyncNotification, requestBox)) {
508 return;
509 }
510 SetNotificationButtons(requestPoint, peerDevice.deviceType_, requestPoint->GetSlotType(), requestBox);
511 SetNotificationContent(request->GetNotificationRequestPoint()->GetContent(),
512 requestPoint->GetNotificationType(), requestBox);
513 if (!requestBox->Serialize()) {
514 ANS_LOGW("Dans OnConsumed serialize failed.");
515 AnalyticsUtil::GetInstance().SendHaReport(PUBLISH_ERROR_EVENT_CODE, -1, BRANCH3_ID,
516 "serialization failed");
517 return;
518 }
519 std::shared_ptr<PackageInfo> packageInfo = std::make_shared<PackageInfo>(requestBox, peerDevice,
520 TransDataType::DATA_TYPE_BYTES, PUBLISH_ERROR_EVENT_CODE);
521 DistributedSendAdapter::GetInstance().SendPackage(packageInfo);
522 }
523
SetNotificationContent(const std::shared_ptr<NotificationContent> & content,NotificationContent::Type type,std::shared_ptr<NotificationRequestBox> & requestBox)524 void DistributedPublishService::SetNotificationContent(const std::shared_ptr<NotificationContent> &content,
525 NotificationContent::Type type, std::shared_ptr<NotificationRequestBox>& requestBox)
526 {
527 if (content == nullptr || content->GetNotificationContent() == nullptr) {
528 return;
529 }
530
531 ANS_LOGI("Set Notification notification content %{public}d.", type);
532 switch (type) {
533 case NotificationContent::Type::PICTURE: {
534 auto picture = std::static_pointer_cast<NotificationPictureContent>(content->GetNotificationContent());
535 requestBox->SetNotificationTitle(picture->GetTitle());
536 requestBox->SetNotificationText(picture->GetText());
537 requestBox->SetNotificationAdditionalText(picture->GetAdditionalText());
538 requestBox->SetNotificationExpandedTitle(picture->GetExpandedTitle());
539 requestBox->SetNotificationBriefText(picture->GetBriefText());
540 requestBox->SetNotificationBigPicture(picture->GetBigPicture());
541 break;
542 }
543 case NotificationContent::Type::MULTILINE: {
544 auto multiline = std::static_pointer_cast<NotificationMultiLineContent>(content->GetNotificationContent());
545 requestBox->SetNotificationTitle(multiline->GetTitle());
546 requestBox->SetNotificationText(multiline->GetText());
547 requestBox->SetNotificationAdditionalText(multiline->GetAdditionalText());
548 requestBox->SetNotificationExpandedTitle(multiline->GetExpandedTitle());
549 requestBox->SetNotificationBriefText(multiline->GetBriefText());
550 requestBox->SetNotificationAllLines(multiline->GetAllLines());
551 break;
552 }
553 case NotificationContent::Type::LONG_TEXT: {
554 std::shared_ptr<NotificationLongTextContent> contentLong =
555 std::static_pointer_cast<NotificationLongTextContent>(content->GetNotificationContent());
556 requestBox->SetNotificationTitle(contentLong->GetTitle());
557 requestBox->SetNotificationText(contentLong->GetText());
558 requestBox->SetNotificationAdditionalText(contentLong->GetAdditionalText());
559 requestBox->SetNotificationExpandedTitle(contentLong->GetExpandedTitle());
560 requestBox->SetNotificationBriefText(contentLong->GetBriefText());
561 requestBox->SetNotificationLongText(contentLong->GetLongText());
562 break;
563 }
564 case NotificationContent::Type::LIVE_VIEW:
565 case NotificationContent::Type::LOCAL_LIVE_VIEW:
566 case NotificationContent::Type::BASIC_TEXT:
567 default: {
568 std::shared_ptr<NotificationBasicContent> contentBasic =
569 std::static_pointer_cast<NotificationBasicContent>(content->GetNotificationContent());
570 requestBox->SetNotificationTitle(contentBasic->GetTitle());
571 requestBox->SetNotificationText(contentBasic->GetText());
572 requestBox->SetNotificationAdditionalText(contentBasic->GetAdditionalText());
573 break;
574 }
575 }
576 }
577
SetNotificationButtons(const sptr<NotificationRequest> notificationRequest,int32_t deviceType,NotificationConstant::SlotType slotType,std::shared_ptr<NotificationRequestBox> & requestBox)578 void DistributedPublishService::SetNotificationButtons(const sptr<NotificationRequest> notificationRequest,
579 int32_t deviceType, NotificationConstant::SlotType slotType, std::shared_ptr<NotificationRequestBox>& requestBox)
580 {
581 auto actionButtons = notificationRequest->GetActionButtons();
582 if (actionButtons.empty()) {
583 ANS_LOGE("Check actionButtons is null.");
584 return;
585 }
586 if (deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_PAD ||
587 deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_PC) {
588 std::vector<std::string> buttonsTitle;
589 size_t length = actionButtons.size();
590 if (length > NotificationConstant::MAX_BTN_NUM) {
591 length = NotificationConstant::MAX_BTN_NUM;
592 }
593 for (size_t i = 0; i < length; i++) {
594 if (actionButtons[i] == nullptr) {
595 return;
596 }
597 if (actionButtons[i]->GetUserInput() != nullptr) {
598 ANS_LOGI("distributed override reply button.");
599 continue;
600 }
601 buttonsTitle.push_back(actionButtons[i]->GetTitle());
602 }
603 requestBox->SetActionButtonsTitle(buttonsTitle);
604 return;
605 }
606 if (slotType == NotificationConstant::SlotType::SOCIAL_COMMUNICATION) {
607 std::shared_ptr<NotificationActionButton> button = nullptr;
608 for (std::shared_ptr<NotificationActionButton> buttonItem : actionButtons) {
609 if (buttonItem != nullptr && buttonItem->GetUserInput() != nullptr &&
610 !buttonItem->GetUserInput()->GetInputKey().empty()) {
611 button = buttonItem;
612 break;
613 }
614 }
615 if (button != nullptr && button->GetUserInput() != nullptr) {
616 requestBox->SetNotificationActionName(button->GetTitle());
617 requestBox->SetNotificationUserInput(button->GetUserInput()->GetInputKey());
618 }
619 }
620 }
621
FillSyncRequestExtendInfo(const sptr<NotificationRequest> notificationRequest,int32_t deviceTypeId,std::shared_ptr<NotificationRequestBox> & requestBox,AAFwk::WantParams & wantParam)622 bool DistributedPublishService::FillSyncRequestExtendInfo(const sptr<NotificationRequest> notificationRequest,
623 int32_t deviceTypeId, std::shared_ptr<NotificationRequestBox>& requestBox, AAFwk::WantParams& wantParam)
624 {
625 std::string appName;
626 auto params = notificationRequest->GetExtendInfo();
627 if (params != nullptr) {
628 wantParam = *params;
629 appName = params->GetStringParam("notification_collaboration_app_name");
630 }
631 std::string deviceType = DistributedDeviceService::DeviceTypeToTypeString(deviceTypeId);
632 std::string bundleName = appName.empty() ? notificationRequest->GetOwnerBundleName() : appName;
633 AppExecFwk::BundleResourceInfo resourceInfo;
634 if (DelayedSingleton<BundleResourceHelper>::GetInstance()->GetBundleInfo(bundleName, resourceInfo) != ERR_OK) {
635 ANS_LOGW("Dans get bundle icon failed %{public}s.", bundleName.c_str());
636 return false;
637 }
638
639 int32_t userId;
640 std::string deviceId;
641 if (NotificationHelper::GetMutilDeviceStatus(deviceType, UNLOCKED_USED_FLAG, deviceId, userId) != ERR_OK) {
642 ANS_LOGW("Dans get status failed %{public}s.", deviceType.c_str());
643 return false;
644 }
645
646 if (appName.empty()) {
647 int32_t ownerUserId = notificationRequest->GetOwnerUserId();
648 AppExecFwk::BundleInfo bundleInfo;
649 if (DelayedSingleton<BundleResourceHelper>::GetInstance()->GetBundleInfoV9(bundleName, ownerUserId,
650 bundleInfo) != ERR_OK) {
651 ANS_LOGE("Dans get application, %{public}d, %{public}s", deviceTypeId, bundleName.c_str());
652 return false;
653 }
654
655 AppExecFwk::ApplicationInfo appInfo = bundleInfo.applicationInfo;
656 wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_APP_NAME, AAFwk::String::Box(appInfo.name));
657 wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_APP_LABEL, AAFwk::String::Box(resourceInfo.label));
658 wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_APP_INDEX, AAFwk::Integer::Box(appInfo.appIndex));
659 wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_DEVICE_ID + "_" + deviceType,
660 AAFwk::String::Box(deviceId));
661 requestBox->SetSmallIcon(AnsImageUtil::CreatePixelMapByString(resourceInfo.icon));
662 requestBox->SetReceiverUserId(userId);
663 ANS_LOGI("Dans fill %{public}s %{public}d %{public}s %{public}d", resourceInfo.label.c_str(), appInfo.appIndex,
664 StringAnonymous(deviceId).c_str(), userId);
665 return true;
666 }
667 wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_DEVICE_ID + "_" + deviceType, AAFwk::String::Box(deviceId));
668 requestBox->SetSmallIcon(AnsImageUtil::CreatePixelMapByString(resourceInfo.icon));
669 requestBox->SetReceiverUserId(userId);
670 return true;
671 }
672
FillNotSyncRequestExtendInfo(const sptr<NotificationRequest> notificationRequest,int32_t deviceType,std::shared_ptr<NotificationRequestBox> & requestBox,AAFwk::WantParams & wantParam)673 bool DistributedPublishService::FillNotSyncRequestExtendInfo(const sptr<NotificationRequest> notificationRequest,
674 int32_t deviceType, std::shared_ptr<NotificationRequestBox>& requestBox, AAFwk::WantParams& wantParam)
675 {
676 auto params = notificationRequest->GetExtendInfo();
677 if (params == nullptr) {
678 ANS_LOGW("Fill box invalid data.");
679 return false;
680 }
681 std::string content = params->GetStringParam("notification_collaboration_app_name");
682 if (content.empty()) {
683 ANS_LOGI("Fill box invalid app name.");
684 return false;
685 }
686 AppExecFwk::BundleResourceInfo resourceInfo;
687 if (DelayedSingleton<BundleResourceHelper>::GetInstance()->GetBundleInfo(content, resourceInfo) != 0) {
688 ANS_LOGW("Dans get bundle icon failed %{public}s.", content.c_str());
689 return false;
690 }
691 std::shared_ptr<Media::PixelMap> icon = AnsImageUtil::CreatePixelMapByString(resourceInfo.icon);
692 requestBox->SetSmallIcon(icon);
693 std::string key = EXTENDINFO_INFO_PRE + EXTENDINFO_USERID +
694 DistributedDeviceService::DeviceTypeToTypeString(deviceType);
695 int32_t userId = params->GetIntParam(key, -1);
696 if (userId != -1) {
697 requestBox->SetReceiverUserId(userId);
698 }
699 wantParam = *params;
700 return true;
701 }
702
SetNotificationExtendInfo(const sptr<NotificationRequest> notificationRequest,int32_t deviceType,bool isSyncNotification,std::shared_ptr<NotificationRequestBox> & requestBox)703 bool DistributedPublishService::SetNotificationExtendInfo(const sptr<NotificationRequest> notificationRequest,
704 int32_t deviceType, bool isSyncNotification, std::shared_ptr<NotificationRequestBox>& requestBox)
705 {
706 if (notificationRequest->GetBigIcon() != nullptr) {
707 requestBox->SetBigIcon(notificationRequest->GetBigIcon(), deviceType);
708 }
709 if (notificationRequest->GetOverlayIcon() != nullptr) {
710 requestBox->SetOverlayIcon(notificationRequest->GetOverlayIcon(), deviceType);
711 }
712 if (notificationRequest->GetLittleIcon() != nullptr) {
713 requestBox->SetSmallIcon(notificationRequest->GetLittleIcon());
714 }
715 if (deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH) {
716 ANS_LOGI("Send request no extend info %{public}d.", deviceType);
717 return true;
718 }
719
720 std::string basicInfo;
721 if (!notificationRequest->CollaborationToJson(basicInfo)) {
722 ANS_LOGW("Dans OnConsumed collaboration json failed.");
723 return false;
724 }
725 requestBox->SetNotificationBasicInfo(basicInfo);
726
727 AAFwk::WantParams wantParam;
728 if (isSyncNotification) {
729 if (!FillSyncRequestExtendInfo(notificationRequest, deviceType, requestBox, wantParam)) {
730 ANS_LOGW("Dans fill sync failed.");
731 return false;
732 }
733 } else {
734 if (!FillNotSyncRequestExtendInfo(notificationRequest, deviceType, requestBox, wantParam)) {
735 ANS_LOGW("Dans fill not sync failed.");
736 return false;
737 }
738 }
739 wantParam.DumpInfo(0);
740 AAFwk::WantParamWrapper wantWrapper(wantParam);
741 requestBox->SetBoxExtendInfo(wantWrapper.ToString());
742 requestBox->SetDeviceUserId(DistributedSubscribeService::GetCurrentActiveUserId());
743 return true;
744 }
745
746 #else
RemoveAllDistributedNotifications(const std::shared_ptr<TlvBox> & boxMessage)747 void DistributedPublishService::RemoveAllDistributedNotifications(const std::shared_ptr<TlvBox>& boxMessage)
748 {
749 RemoveAllDistributedNotificationsBox removeBox = RemoveAllDistributedNotificationsBox(boxMessage);
750 std::string deviceId;
751 removeBox.GetLocalDeviceId(deviceId);
752 DistributedDeviceInfo device;
753 if (!DistributedDeviceService::GetInstance().GetDeviceInfo(deviceId, device)) {
754 ANS_LOGW("Dans bundle get device info failed %{public}s.", StringAnonymous(deviceId).c_str());
755 return;
756 }
757 std::vector<std::string> hashcodes;
758 IN_PROCESS_CALL(NotificationHelper::RemoveDistributedNotifications(hashcodes,
759 NotificationConstant::SlotType::SOCIAL_COMMUNICATION,
760 NotificationConstant::DistributedDeleteType::DEVICE_ID,
761 NotificationConstant::DISTRIBUTED_RELEASE_DELETE,
762 device.udid_));
763 }
764
PublishNotification(const std::shared_ptr<TlvBox> & boxMessage)765 void DistributedPublishService::PublishNotification(const std::shared_ptr<TlvBox>& boxMessage)
766 {
767 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
768 if (request == nullptr) {
769 ANS_LOGE("NotificationRequest is nullptr");
770 return;
771 }
772 int32_t slotType = 0;
773 int32_t contentType = 0;
774 NotificationRequestBox requestBox = NotificationRequestBox(boxMessage);
775 std::string basicInfo;
776 if (requestBox.GetNotificationBasicInfo(basicInfo)) {
777 request = NotificationRequest::CollaborationFromJson(basicInfo);
778 if (request == nullptr) {
779 ANS_LOGE("NotificationRequest is nullptr");
780 return;
781 }
782 }
783 bool isCommonLiveView = false;
784 if (requestBox.GetSlotType(slotType) && requestBox.GetContentType(contentType)) {
785 isCommonLiveView =
786 (static_cast<NotificationContent::Type>(contentType) == NotificationContent::Type::LIVE_VIEW) &&
787 (static_cast<NotificationConstant::SlotType>(slotType) == NotificationConstant::SlotType::LIVE_VIEW);
788 }
789 MakeExtendInfo(requestBox, request);
790 MakeNotificationButtons(requestBox, static_cast<NotificationConstant::SlotType>(slotType), request);
791 MakeNotificationContent(requestBox, request, isCommonLiveView, contentType);
792 MakeNotificationIcon(requestBox, request);
793 MakeNotificationReminderFlag(requestBox, request);
794 int result = IN_PROCESS_CALL(NotificationHelper::PublishNotification(*request));
795 ANS_LOGI("Dans publish message %{public}s %{public}d.", request->GetDistributedHashCode().c_str(), result);
796 }
797
PublishSynchronousLiveView(const std::shared_ptr<TlvBox> & boxMessage)798 void DistributedPublishService::PublishSynchronousLiveView(const std::shared_ptr<TlvBox>& boxMessage)
799 {
800 bool empty = true;
801 NotificationSyncBox notificationSyncBox = NotificationSyncBox(boxMessage);
802 if (!notificationSyncBox.GetNotificationEmpty(empty)) {
803 ANS_LOGW("Dans get sync notification empty failed.");
804 return;
805 }
806
807 std::unordered_set<std::string> notificationList;
808 if (!empty) {
809 if (!notificationSyncBox.GetNotificationList(notificationList)) {
810 ANS_LOGW("Dans get sync notification failed.");
811 return;
812 }
813 }
814 std::vector<sptr<Notification>> notifications;
815 auto result = NotificationHelper::GetAllNotificationsBySlotType(notifications,
816 NotificationConstant::SlotType::LIVE_VIEW);
817 if (result != ERR_OK || notifications.empty()) {
818 ANS_LOGI("Dans get all active %{public}d %{public}d.", result, notifications.empty());
819 return;
820 }
821
822 ANS_LOGI("Dans sync notification %{public}zu %{public}zu.", notificationList.size(), notifications.size());
823 for (auto item : notificationList) {
824 ANS_LOGI("Dans sync %{public}s.", item.c_str());
825 }
826 std::vector<std::string> removeList;
827 for (auto& notification : notifications) {
828 if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr ||
829 !notification->GetNotificationRequestPoint()->IsCommonLiveView()) {
830 ANS_LOGI("Dans no need sync remove notification.");
831 continue;
832 }
833 std::string hashCode = notification->GetKey();
834 ANS_LOGI("Dans sync remove %{public}s.", hashCode.c_str());
835 size_t pos = hashCode.find(DISTRIBUTED_LABEL);
836 if (pos != std::string::npos) {
837 hashCode.erase(pos, DISTRIBUTED_LABEL.length());
838 }
839 if (notificationList.find(hashCode) == notificationList.end()) {
840 removeList.push_back(notification->GetKey());
841 ANS_LOGI("Dans sync remove notification %{public}s.", notification->GetKey().c_str());
842 }
843 }
844 if (!removeList.empty()) {
845 int result = IN_PROCESS_CALL(NotificationHelper::RemoveNotifications(removeList,
846 NotificationConstant::DISTRIBUTED_COLLABORATIVE_DELETE));
847 ANS_LOGI("Dans sync remove message %{public}d.", result);
848 }
849 }
850
MakeNotificationButtons(const NotificationRequestBox & box,NotificationConstant::SlotType slotType,sptr<NotificationRequest> & request)851 void DistributedPublishService::MakeNotificationButtons(const NotificationRequestBox& box,
852 NotificationConstant::SlotType slotType, sptr<NotificationRequest>& request)
853 {
854 auto localDevice = DistributedDeviceService::GetInstance().GetLocalDevice();
855 if ((localDevice.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PAD ||
856 localDevice.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PC)) {
857 MakePadNotificationButtons(box, request);
858 return;
859 }
860
861 if (request != nullptr && slotType == NotificationConstant::SlotType::SOCIAL_COMMUNICATION) {
862 std::string actionName;
863 std::string userInputKey;
864 box.GetNotificationActionName(actionName);
865 if (actionName.empty()) {
866 ANS_LOGE("Check actionButtons is null.");
867 return;
868 }
869 box.GetNotificationUserInput(userInputKey);
870 std::shared_ptr<NotificationUserInput> userInput = NotificationUserInput::Create(userInputKey);
871 if (!userInput) {
872 ANS_LOGE("Failed to create NotificationUserInput by inputKey=%{public}s", userInputKey.c_str());
873 return;
874 }
875 std::shared_ptr<NotificationActionButton> actionButton =
876 NotificationActionButton::Create(nullptr, actionName, nullptr);
877 actionButton->AddNotificationUserInput(userInput);
878 request->AddActionButton(actionButton);
879 }
880 }
881
MakePadNotificationButtons(const NotificationRequestBox & box,sptr<NotificationRequest> & request)882 void DistributedPublishService::MakePadNotificationButtons(
883 const NotificationRequestBox& box, sptr<NotificationRequest>& request)
884 {
885 if (request == nullptr) {
886 return;
887 }
888 std::vector<std::string> buttonsTitle;
889 if (!box.GetActionButtonsTitle(buttonsTitle) || buttonsTitle.size() <= 0) {
890 return;
891 }
892 for (size_t i = 0; i < buttonsTitle.size(); i++) {
893 std::shared_ptr<NotificationActionButton> actionButton =
894 NotificationActionButton::Create(nullptr, buttonsTitle[i], nullptr);
895 request->AddActionButton(actionButton);
896 }
897 }
898
MakeNotificationReminderFlag(const NotificationRequestBox & box,sptr<NotificationRequest> & request)899 void DistributedPublishService::MakeNotificationReminderFlag(const NotificationRequestBox& box,
900 sptr<NotificationRequest>& request)
901 {
902 int32_t type = 0;
903 std::string context;
904 if (box.GetSlotType(type)) {
905 request->SetSlotType(static_cast<NotificationConstant::SlotType>(type));
906 }
907 if (box.GetReminderFlag(type)) {
908 request->SetCollaboratedReminderFlag(static_cast<uint32_t>(type));
909 }
910 if (box.GetAppMessageId(context)) {
911 request->SetAppMessageId(context);
912 }
913 if (box.GetCreatorBundleName(context)) {
914 request->SetOwnerBundleName(context);
915 request->SetCreatorBundleName(context);
916 }
917 if (box.GetNotificationHashCode(context)) {
918 request->SetDistributedHashCode(context);
919 }
920 request->SetDistributedCollaborate(true);
921 }
922
MakeExtendInfo(const NotificationRequestBox & box,sptr<NotificationRequest> & request)923 void DistributedPublishService::MakeExtendInfo(const NotificationRequestBox& box,
924 sptr<NotificationRequest>& request)
925 {
926 std::string contentInfo;
927 std::shared_ptr<AAFwk::WantParams> extendInfo = std::make_shared<AAFwk::WantParams>();
928 if (box.GetBoxExtendInfo(contentInfo)) {
929 if (!contentInfo.empty()) {
930 AAFwk::WantParams extendInfoParams = AAFwk::WantParamWrapper::ParseWantParams(contentInfo);
931 extendInfo = std::make_shared<AAFwk::WantParams>(extendInfoParams);
932 }
933 }
934 extendInfo->SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_FLAG, AAFwk::Boolean::Box(true));
935 auto local = DistributedDeviceService::GetInstance().GetLocalDevice();
936 if (local.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH) {
937 extendInfo->SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_ENABLE_CHECK, AAFwk::Boolean::Box(false));
938 } else {
939 extendInfo->SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_ENABLE_CHECK, AAFwk::Boolean::Box(true));
940 if (box.GetDeviceId(contentInfo)) {
941 DistributedDeviceInfo peerDevice;
942 if (DistributedDeviceService::GetInstance().GetDeviceInfo(contentInfo, peerDevice)) {
943 std::string deviceType = DistributedDeviceService::DeviceTypeToTypeString(peerDevice.deviceType_);
944 extendInfo->SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_DEVICETYPE, AAFwk::String::Box(deviceType));
945 extendInfo->SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_DEVICE_ID, AAFwk::String::Box(peerDevice.udid_));
946 }
947 }
948 std::string localType = DistributedDeviceService::DeviceTypeToTypeString(local.deviceType_);
949 extendInfo->SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_LOCALTYPE, AAFwk::String::Box(localType));
950 int32_t userId;
951 if (box.GetDeviceUserId(userId)) {
952 extendInfo->SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_DEVICE_USERID, AAFwk::Integer::Box(userId));
953 }
954 if (box.GetReceiverUserId(userId)) {
955 request->SetReceiverUserId(userId);
956 }
957 }
958 extendInfo->DumpInfo(0);
959 request->SetExtendInfo(extendInfo);
960 }
961
MakeNotificationIcon(const NotificationRequestBox & box,sptr<NotificationRequest> & request)962 void DistributedPublishService::MakeNotificationIcon(const NotificationRequestBox& box,
963 sptr<NotificationRequest>& request)
964 {
965 std::shared_ptr<Media::PixelMap> icon;
966 auto localDevice = DistributedDeviceService::GetInstance().GetLocalDevice();
967 if (box.GetBigIcon(icon, localDevice.deviceType_)) {
968 request->SetBigIcon(icon);
969 }
970 if (box.GetOverlayIcon(icon, localDevice.deviceType_)) {
971 request->SetOverlayIcon(icon);
972 }
973 if (box.GetSmallIcon(icon)) {
974 request->SetLittleIcon(icon);
975 }
976 }
977
MakeNotificationContent(const NotificationRequestBox & box,sptr<NotificationRequest> & request,bool isCommonLiveView,int32_t contentType)978 void DistributedPublishService::MakeNotificationContent(const NotificationRequestBox& box,
979 sptr<NotificationRequest>& request, bool isCommonLiveView, int32_t contentType)
980 {
981 if (isCommonLiveView) {
982 std::vector<uint8_t> buffer;
983 if (box.GetCommonLiveView(buffer)) {
984 int64_t deleteTime;
985 std::string context;
986 auto liveviewContent = std::make_shared<NotificationLiveViewContent>();
987 if (box.GetNotificationText(context)) {
988 liveviewContent->SetText(context);
989 }
990 if (box.GetNotificationTitle(context)) {
991 liveviewContent->SetTitle(context);
992 }
993 if (box.GetAutoDeleteTime(deleteTime)) {
994 request->SetAutoDeletedTime(deleteTime);
995 }
996 if (box.GetFinishTime(deleteTime)) {
997 request->SetFinishDeadLine(deleteTime);
998 }
999 auto content = std::make_shared<NotificationContent>(liveviewContent);
1000 request->SetContent(content);
1001 std::shared_ptr<AAFwk::WantParams> extraInfo = std::make_shared<AAFwk::WantParams>();
1002 liveviewContent->SetExtraInfo(extraInfo);
1003 auto localDevice = DistributedDeviceService::GetInstance().GetLocalDevice();
1004 std::string deviceType = DistributedDeviceService::DeviceTypeToTypeString(localDevice.deviceType_);
1005 DISTRIBUTED_LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UpdateLiveviewDecodeContent(
1006 request, buffer, deviceType);
1007 }
1008 return;
1009 }
1010 MakeNotificationBasicContent(box, request, contentType);
1011 }
1012
1013 struct TransferNotification {
1014 std::string title;
1015 std::string context;
1016 std::string additionalText;
1017 std::string briefText;
1018 std::string expandedTitle;
1019 };
1020
ConvertBoxToLongContent(const TransferNotification & notificationItem,const NotificationRequestBox & box,sptr<NotificationRequest> & request)1021 static void ConvertBoxToLongContent(const TransferNotification& notificationItem, const NotificationRequestBox& box,
1022 sptr<NotificationRequest>& request)
1023 {
1024 auto pContent = std::make_shared<NotificationLongTextContent>();
1025 pContent->SetText(notificationItem.context);
1026 pContent->SetTitle(notificationItem.title);
1027 pContent->SetAdditionalText(notificationItem.additionalText);
1028 pContent->SetBriefText(notificationItem.briefText);
1029 pContent->SetExpandedTitle(notificationItem.expandedTitle);
1030 std::string longText;
1031 box.GetNotificationLongText(longText);
1032 pContent->SetLongText(longText);
1033 auto content = std::make_shared<NotificationContent>(pContent);
1034 request->SetContent(content);
1035 }
1036
ConvertBoxToMultileContent(const TransferNotification & notificationItem,const NotificationRequestBox & box,sptr<NotificationRequest> & request)1037 static void ConvertBoxToMultileContent(const TransferNotification& notificationItem, const NotificationRequestBox& box,
1038 sptr<NotificationRequest>& request)
1039 {
1040 auto pContent = std::make_shared<NotificationMultiLineContent>();
1041 pContent->SetText(notificationItem.context);
1042 pContent->SetTitle(notificationItem.title);
1043 pContent->SetAdditionalText(notificationItem.additionalText);
1044 pContent->SetBriefText(notificationItem.briefText);
1045 pContent->SetExpandedTitle(notificationItem.expandedTitle);
1046 std::vector<std::string> allLines;
1047 box.GetNotificationAllLines(allLines);
1048 for (auto& item : allLines) {
1049 pContent->AddSingleLine(item);
1050 }
1051 auto content = std::make_shared<NotificationContent>(pContent);
1052 request->SetContent(content);
1053 }
1054
ConvertBoxToPictureContent(const TransferNotification & notificationItem,const NotificationRequestBox & box,sptr<NotificationRequest> & request)1055 static void ConvertBoxToPictureContent(const TransferNotification& notificationItem, const NotificationRequestBox& box,
1056 sptr<NotificationRequest>& request)
1057 {
1058 auto pContent = std::make_shared<NotificationPictureContent>();
1059 pContent->SetText(notificationItem.context);
1060 pContent->SetTitle(notificationItem.title);
1061 pContent->SetAdditionalText(notificationItem.additionalText);
1062 pContent->SetBriefText(notificationItem.briefText);
1063 pContent->SetExpandedTitle(notificationItem.expandedTitle);
1064 std::shared_ptr<Media::PixelMap> bigPicture;
1065 box.GetNotificationBigPicture(bigPicture);
1066 pContent->SetBigPicture(bigPicture);
1067 auto content = std::make_shared<NotificationContent>(pContent);
1068 request->SetContent(content);
1069 }
1070
MakeNotificationBasicContent(const NotificationRequestBox & box,sptr<NotificationRequest> & request,int32_t contentType)1071 void DistributedPublishService::MakeNotificationBasicContent(const NotificationRequestBox& box,
1072 sptr<NotificationRequest>& request, int32_t contentType)
1073 {
1074 TransferNotification notificationItem;
1075 box.GetNotificationText(notificationItem.context);
1076 box.GetNotificationTitle(notificationItem.title);
1077 box.GetNotificationAdditionalText(notificationItem.additionalText);
1078 NotificationContent::Type type = static_cast<NotificationContent::Type>(contentType);
1079 if (type == NotificationContent::Type::LONG_TEXT || type == NotificationContent::Type::MULTILINE ||
1080 type == NotificationContent::Type::PICTURE) {
1081 box.GetNotificationBriefText(notificationItem.briefText);
1082 box.GetNotificationExpandedTitle(notificationItem.expandedTitle);
1083 }
1084 switch (type) {
1085 case NotificationContent::Type::BASIC_TEXT: {
1086 auto pContent = std::make_shared<NotificationNormalContent>();
1087 pContent->SetText(notificationItem.context);
1088 pContent->SetTitle(notificationItem.title);
1089 pContent->SetAdditionalText(notificationItem.additionalText);
1090 auto content = std::make_shared<NotificationContent>(pContent);
1091 request->SetContent(content);
1092 break;
1093 }
1094 case NotificationContent::Type::CONVERSATION: {
1095 auto pContent = std::make_shared<NotificationConversationalContent>();
1096 pContent->SetText(notificationItem.context);
1097 pContent->SetTitle(notificationItem.title);
1098 pContent->SetAdditionalText(notificationItem.additionalText);
1099 auto content = std::make_shared<NotificationContent>(pContent);
1100 request->SetContent(content);
1101 break;
1102 }
1103 case NotificationContent::Type::LONG_TEXT: {
1104 ConvertBoxToLongContent(notificationItem, box, request);
1105 break;
1106 }
1107 case NotificationContent::Type::MULTILINE: {
1108 ConvertBoxToMultileContent(notificationItem, box, request);
1109 break;
1110 }
1111 case NotificationContent::Type::PICTURE: {
1112 ConvertBoxToPictureContent(notificationItem, box, request);
1113 break;
1114 }
1115 default: {
1116 ANS_LOGE("Set notifictaion content %{public}d", type);
1117 break;
1118 }
1119 }
1120 }
1121 #endif
1122 }
1123 }
1124