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 #include "distributed_service.h"
16
17 #include <thread>
18 #include "notification_helper.h"
19 #include "distributed_client.h"
20 #include "request_box.h"
21 #include "state_box.h"
22 #include "in_process_call_wrapper.h"
23 #include "distributed_observer_service.h"
24 #include "os_account_manager.h"
25 #include "distributed_server.h"
26 #include "common_event_support.h"
27 #include "distributed_device_data.h"
28 #include "distributed_bundle_service.h"
29 #include "distributed_device_service.h"
30 #include "distributed_operation_service.h"
31 #include "distributed_publish_service.h"
32 #include "distributed_subscribe_service.h"
33 #include "bundle_resource_helper.h"
34 #include "distributed_liveview_all_scenarios_extension_wrapper.h"
35
36 namespace OHOS {
37 namespace Notification {
38
39 static const std::string DISTRIBUTED_LABEL = "ans_distributed";
40 static const std::string LABEL_PLACEHOLDER = "label";
41
42 namespace {
43 static const int32_t ADD_DEVICE_SLEEP_TIMES_MS = 1000; // 1s
44 static const uint64_t SYNC_TASK_DELAY = 7 * 1000 * 1000;
45 }
46
GetInstance()47 DistributedService& DistributedService::GetInstance()
48 {
49 static DistributedService distributedService;
50 return distributedService;
51 }
52
DistributedService()53 DistributedService::DistributedService()
54 {
55 serviceQueue_ = std::make_shared<ffrt::queue>("ans_distributed");
56 if (serviceQueue_ == nullptr) {
57 ANS_LOGW("ffrt create failed!");
58 return;
59 }
60 ANS_LOGI("Distributed service init successfully.");
61 }
62
GetNotificationKey(const std::shared_ptr<Notification> & notification)63 std::string DistributedService::GetNotificationKey(const std::shared_ptr<Notification>& notification)
64 {
65 if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) {
66 ANS_LOGE("notification or GetNotificationRequestPoint is nullptr");
67 return "";
68 }
69 std::string notificationKey = notification->GetKey();
70 if (notification->GetNotificationRequestPoint()->GetDistributedCollaborate()) {
71 size_t pos = notificationKey.find(DISTRIBUTED_LABEL);
72 if (pos != std::string::npos) {
73 notificationKey.erase(pos, DISTRIBUTED_LABEL.length());
74 }
75 } else {
76 notificationKey = DISTRIBUTED_LABEL + notificationKey;
77 }
78 return notificationKey;
79 }
80
InitService(const std::string & deviceId,uint16_t deviceType)81 int32_t DistributedService::InitService(const std::string &deviceId, uint16_t deviceType)
82 {
83 DistributedDeviceService::GetInstance().InitLocalDevice(deviceId, deviceType);
84 if (DistributedServer::GetInstance().InitServer(deviceId, deviceType) != 0) {
85 ANS_LOGI("Distributed service init server failed.");
86 return -1;
87 }
88 OberverService::GetInstance().Init(deviceType);
89 return 0;
90 }
91
DestroyService()92 void DistributedService::DestroyService()
93 {
94 if (serviceQueue_ == nullptr) {
95 ANS_LOGE("Check handler is null.");
96 return;
97 }
98 ffrt::task_handle handler = serviceQueue_->submit_h([&]() {
99 ANS_LOGI("Start destory service.");
100 DistributedClient::GetInstance().ReleaseClient();
101 DistributedServer::GetInstance().ReleaseServer();
102 OberverService::GetInstance().Destory();
103 #ifdef DISTRIBUTED_FEATURE_MASTER
104 DISTRIBUTED_LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UnSubscribeAllConnect();
105 #endif
106 DistributedSubscribeService::GetInstance().UnSubscribeAllNotification();
107 });
108 serviceQueue_->wait(handler);
109 }
110
ConnectPeerDevice(DistributedDeviceInfo device)111 void DistributedService::ConnectPeerDevice(DistributedDeviceInfo device)
112 {
113 if (!DistributedDeviceService::GetInstance().CheckDeviceNeedSync(device.deviceId_)) {
114 ANS_LOGI("ConnectPeerDevice device is failed.");
115 return;
116 }
117
118 DistributedDeviceService::GetInstance().SyncDeviceMatch(device, MatchType::MATCH_SYN);
119 DistributedDeviceService::GetInstance().IncreaseDeviceSyncCount(device.deviceId_);
120 if (serviceQueue_ == nullptr) {
121 ANS_LOGE("Check handler is null.");
122 return;
123 }
124 serviceQueue_->submit_h([&, device]() { ConnectPeerDevice(device); },
125 ffrt::task_attr().name("sync").delay(SYNC_TASK_DELAY));
126 }
127
AddDevice(DistributedDeviceInfo device)128 void DistributedService::AddDevice(DistributedDeviceInfo device)
129 {
130 if (serviceQueue_ == nullptr) {
131 ANS_LOGE("Check handler is null.");
132 return;
133 }
134 serviceQueue_->submit_h([&, device]() {
135 ANS_LOGI("Dans AddDevice %{public}s %{public}d", StringAnonymous(device.deviceId_).c_str(),
136 device.deviceType_);
137 DistributedDeviceInfo deviceItem = device;
138 deviceItem.peerState_ = DeviceState::STATE_SYNC;
139 DistributedDeviceService::GetInstance().AddDeviceInfo(deviceItem);
140 if (device.IsPadOrPc() || DistributedDeviceService::GetInstance().IsLocalPadOrPC()) {
141 ANS_LOGI("Dans wait peer %{public}s.", StringAnonymous(device.deviceId_).c_str());
142 return;
143 }
144 // Delay linking to avoid bind failure, There is a delay in reporting the device online
145 auto sleepTime = std::chrono::milliseconds(ADD_DEVICE_SLEEP_TIMES_MS);
146 std::this_thread::sleep_for(sleepTime);
147 ConnectPeerDevice(device);
148 });
149 }
150
ReleaseDevice(const std::string & deviceId,uint16_t deviceType)151 void DistributedService::ReleaseDevice(const std::string &deviceId, uint16_t deviceType)
152 {
153 if (serviceQueue_ == nullptr) {
154 ANS_LOGE("Check handler is null.");
155 return;
156 }
157 std::function<void()> subscribeTask = std::bind([deviceId, deviceType]() {
158 DistributedDeviceInfo device;
159 if (!DistributedDeviceService::GetInstance().GetDeviceInfo(deviceId, device)) {
160 ANS_LOGW("Dans bundle get device info failed %{public}s.", StringAnonymous(deviceId).c_str());
161 return;
162 }
163 DistributedSubscribeService::GetInstance().UnSubscribeNotification(deviceId, deviceType);
164 auto localDevice = DistributedDeviceService::GetInstance().GetLocalDevice();
165 if (localDevice.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH) {
166 ANS_LOGD("watch not delete notifications");
167 return;
168 }
169 if (deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
170 std::vector<std::string> hashcodes;
171 NotificationHelper::RemoveDistributedNotifications(hashcodes,
172 NotificationConstant::SlotType::SOCIAL_COMMUNICATION,
173 NotificationConstant::DistributedDeleteType::DEVICE_ID,
174 NotificationConstant::DISTRIBUTED_RELEASE_DELETE,
175 device.udid_);
176 }
177 });
178 serviceQueue_->submit(subscribeTask);
179 }
180
DeviceStatusChange(const DeviceStatueChangeInfo & changeInfo)181 void DistributedService::DeviceStatusChange(const DeviceStatueChangeInfo& changeInfo)
182 {
183 if (serviceQueue_ == nullptr) {
184 ANS_LOGE("Check handler is null.");
185 return;
186 }
187 std::function<void()> task = std::bind([&, changeInfo]() {
188 ANS_LOGI("Device change %{public}d %{public}d %{public}d", changeInfo.changeType,
189 changeInfo.enableChange, changeInfo.liveViewChange);
190 #ifdef DISTRIBUTED_FEATURE_MASTER
191 if (changeInfo.changeType == DeviceStatueChangeType::DEVICE_USING_ONLINE) {
192 HandleDeviceUsingChange(changeInfo);
193 }
194
195 if (changeInfo.changeType == DeviceStatueChangeType::ALL_CONNECT_STATUS_CHANGE) {
196 if (DistributedDeviceService::GetInstance().CheckNeedSubscribeAllConnect()) {
197 DISTRIBUTED_LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->SubscribeAllConnect();
198 DistributedDeviceService::GetInstance().SetSubscribeAllConnect(true);
199 }
200 }
201
202 if (changeInfo.changeType == DeviceStatueChangeType::DEVICE_USING_CLOSE) {
203 DistributedDeviceInfo device;
204 if (!DistributedDeviceService::GetInstance().GetDeviceInfoByUdid(changeInfo.deviceId, device)) {
205 ANS_LOGW("get deviceId err");
206 return;
207 }
208 DistributedPublishService::GetInstance().RemoveAllDistributedNotifications(device);
209 DistributedSubscribeService::GetInstance().UnSubscribeNotification(device.deviceId_,
210 device.deviceType_, false);
211 std::string deviceType = DistributedDeviceService::DeviceTypeToTypeString(device.deviceType_);
212 if (!deviceType.empty()) {
213 auto ret = NotificationHelper::SetTargetDeviceBundleList(deviceType, device.udid_,
214 BundleListOperationType::RELEASE_BUNDLES, std::vector<std::string>(), std::vector<std::string>());
215 ANS_LOGI("Remove bundle %{public}s %{public}s %{public}d.", deviceType.c_str(),
216 StringAnonymous(device.deviceId_).c_str(), ret);
217 }
218
219 DistributedDeviceService::GetInstance().SyncDeviceMatch(device, MatchType::MATCH_OFFLINE);
220 DistributedClient::GetInstance().ReleaseDevice(device.deviceId_, device.deviceType_, false);
221 DistributedDeviceService::GetInstance().ResetDeviceInfo(device.deviceId_, DeviceState::STATE_OFFLINE);
222 }
223 #else
224 if (changeInfo.changeType == DeviceStatueChangeType::NOTIFICATION_ENABLE_CHANGE) {
225 DistributedDeviceService::GetInstance().SyncDeviceStatus(DistributedDeviceService::STATE_TYPE_SWITCH,
226 false, changeInfo.enableChange, changeInfo.liveViewChange);
227 }
228 #endif
229 });
230 serviceQueue_->submit(task);
231 }
232
OnCanceled(const std::shared_ptr<Notification> & notification,const DistributedDeviceInfo & peerDevice)233 void DistributedService::OnCanceled(const std::shared_ptr<Notification>& notification,
234 const DistributedDeviceInfo& peerDevice)
235 {
236 if (serviceQueue_ == nullptr) {
237 ANS_LOGE("check handler is null");
238 return;
239 }
240 if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) {
241 ANS_LOGE("notification or GetNotificationRequestPoint is nullptr");
242 return;
243 }
244 std::string notificationKey = GetNotificationKey(notification);
245 auto slotType = notification->GetNotificationRequestPoint()->GetSlotType();
246 std::function<void()> task = std::bind([peerDevice, notificationKey, slotType]() {
247 DistributedPublishService::GetInstance().OnRemoveNotification(peerDevice,
248 notificationKey, slotType);
249 });
250 serviceQueue_->submit(task);
251 }
252
OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> & notifications,const DistributedDeviceInfo & peerDevice)253 void DistributedService::OnBatchCanceled(const std::vector<std::shared_ptr<Notification>>& notifications,
254 const DistributedDeviceInfo& peerDevice)
255 {
256 if (serviceQueue_ == nullptr) {
257 ANS_LOGE("check handler is null.");
258 return;
259 }
260
261 std::ostringstream keysStream;
262 std::ostringstream slotTypesStream;
263 for (auto notification : notifications) {
264 if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) {
265 ANS_LOGE("notification or GetNotificationRequestPoint is nullptr");
266 continue;
267 }
268 keysStream << GetNotificationKey(notification) << ' ';
269 slotTypesStream << std::to_string(notification->GetNotificationRequestPoint()->GetSlotType()) << ' ';
270 }
271 std::string notificationKeys = keysStream.str();
272 std::string slotTypes = slotTypesStream.str();
273 std::function<void()> task = std::bind([peerDevice, notificationKeys, slotTypes]() {
274 DistributedPublishService::GetInstance().OnRemoveNotifications(peerDevice,
275 notificationKeys, slotTypes);
276 });
277 serviceQueue_->submit(task);
278 }
279
280 #ifdef DISTRIBUTED_FEATURE_MASTER
OnOperationResponse(const std::shared_ptr<NotificationOperationInfo> & operationInfo,const DistributedDeviceInfo & device)281 int32_t DistributedService::OnOperationResponse(const std::shared_ptr<NotificationOperationInfo> & operationInfo,
282 const DistributedDeviceInfo& device)
283 {
284 return ERR_OK;
285 }
286
OnConsumed(const std::shared_ptr<Notification> & request,const DistributedDeviceInfo & peerDevice)287 void DistributedService::OnConsumed(const std::shared_ptr<Notification> &request,
288 const DistributedDeviceInfo& peerDevice)
289 {
290 if (serviceQueue_ == nullptr) {
291 ANS_LOGE("Check handler is null.");
292 return;
293 }
294 std::function<void()> task = std::bind([request, peerDevice, this]() {
295 if (!OnConsumedSetFlags(request, peerDevice)) {
296 return;
297 }
298 DistributedPublishService::GetInstance().SendNotifictionRequest(request, peerDevice);
299 });
300 serviceQueue_->submit(task);
301 }
302
OnApplicationInfnChanged(const std::string & bundleName)303 void DistributedService::OnApplicationInfnChanged(const std::string& bundleName)
304 {
305 if (serviceQueue_ == nullptr) {
306 ANS_LOGE("Check handler is null.");
307 return;
308 }
309
310 std::function<void()> task = std::bind([&, bundleName]() {
311 DistributedBundleService::GetInstance().HandleBundleChanged(bundleName, false);
312 });
313 serviceQueue_->submit(task);
314 }
315
HandleBundlesEvent(const std::string & bundleName,const std::string & action)316 void DistributedService::HandleBundlesEvent(const std::string& bundleName, const std::string& action)
317 {
318 if (serviceQueue_ == nullptr) {
319 ANS_LOGE("Check handler is null.");
320 return;
321 }
322
323 std::function<void()> task = std::bind([&, bundleName, action]() {
324 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
325 DistributedBundleService::GetInstance().HandleBundleChanged(bundleName, true);
326 }
327 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
328 DistributedBundleService::GetInstance().HandleBundleRemoved(bundleName);
329 }
330 ANS_LOGI("Handle bundle event %{public}s, %{public}s.", bundleName.c_str(), action.c_str());
331 });
332 serviceQueue_->submit(task);
333 }
334
HandleDeviceUsingChange(const DeviceStatueChangeInfo & changeInfo)335 void DistributedService::HandleDeviceUsingChange(const DeviceStatueChangeInfo& changeInfo)
336 {
337 DistributedDeviceInfo device;
338 if (!DistributedDeviceService::GetInstance().GetDeviceInfoByUdid(changeInfo.deviceId, device)) {
339 return;
340 }
341 DistributedDeviceService::GetInstance().SetDeviceSyncData(device.deviceId_,
342 DistributedDeviceService::DEVICE_USAGE, true);
343 if (!DistributedDeviceService::GetInstance().IsSubscribeAllConnect()) {
344 DISTRIBUTED_LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->SubscribeAllConnect();
345 DistributedDeviceService::GetInstance().SetSubscribeAllConnect(true);
346 }
347 // sync to peer device
348 DistributedDeviceService::GetInstance().SetDeviceState(device.deviceId_, DeviceState::STATE_SYNC);
349 ConnectPeerDevice(device);
350 }
351 #else
OnConsumed(const std::shared_ptr<Notification> & request,const DistributedDeviceInfo & peerDevice)352 void DistributedService::OnConsumed(const std::shared_ptr<Notification> &request,
353 const DistributedDeviceInfo& peerDevice)
354 {
355 return;
356 }
357
OnOperationResponse(const std::shared_ptr<NotificationOperationInfo> & operationInfo,const DistributedDeviceInfo & device)358 int32_t DistributedService::OnOperationResponse(const std::shared_ptr<NotificationOperationInfo> & operationInfo,
359 const DistributedDeviceInfo& device)
360 {
361 return DistributedOperationService::GetInstance().OnOperationResponse(operationInfo, device);
362 }
363
SyncDeviceStatus(int32_t status)364 void DistributedService::SyncDeviceStatus(int32_t status)
365 {
366 if (serviceQueue_ == nullptr) {
367 ANS_LOGE("Check handler is null.");
368 return;
369 }
370 status = (static_cast<uint32_t>(status) << 1);
371 std::function<void()> task = std::bind([&, status]() {
372 DistributedDeviceService::GetInstance().SyncDeviceStatus(DistributedDeviceService::STATE_TYPE_LOCKSCREEN,
373 status, false, false);
374 });
375 serviceQueue_->submit(task);
376 }
377
SyncInstalledBundle(const std::string & bundleName,bool isAdd)378 void DistributedService::SyncInstalledBundle(const std::string& bundleName, bool isAdd)
379 {
380 if (serviceQueue_ == nullptr) {
381 ANS_LOGE("Check handler is null.");
382 return;
383 }
384 std::function<void()> task = std::bind([&, bundleName, isAdd]() {
385 std::vector<std::pair<std::string, std::string>> bundles;
386 auto localDevice = DistributedDeviceService::GetInstance().GetLocalDevice();
387 auto peerDevices = DistributedDeviceService::GetInstance().GetDeviceList();
388 bool isPad = DistributedDeviceService::GetInstance().IsLocalPadOrPC();
389 if (isAdd) {
390 int32_t userId = DistributedSubscribeService::GetCurrentActiveUserId();
391 if (DelayedSingleton<BundleResourceHelper>::GetInstance()->CheckSystemApp(bundleName, userId)) {
392 ANS_LOGI("Bundle no sycn %{public}d %{public}s.", userId, bundleName.c_str());
393 return;
394 }
395 AppExecFwk::BundleResourceInfo resourceInfo;
396 if (DelayedSingleton<BundleResourceHelper>::GetInstance()->GetBundleInfo(bundleName, resourceInfo)
397 != ERR_OK) {
398 ANS_LOGW("Dans get bundle failed %{public}s.", bundleName.c_str());
399 return;
400 }
401 bundles.push_back({bundleName, resourceInfo.label});
402 } else {
403 bundles.push_back({bundleName, LABEL_PLACEHOLDER});
404 }
405 int32_t syncType = isAdd ? BundleListOperationType::ADD_BUNDLES : BundleListOperationType::REMOVE_BUNDLES;
406 for (auto& device : peerDevices) {
407 if (isPad && device.second.peerState_ != DeviceState::STATE_ONLINE) {
408 ANS_LOGI("DeviceState bundle %{public}d %{public}d %{public}s.", syncType, device.second.deviceType_,
409 StringAnonymous(device.second.deviceId_).c_str());
410 continue;
411 }
412 DistributedBundleService::GetInstance().SendInstalledBundles(device.second, localDevice.deviceId_,
413 bundles, syncType);
414 }
415 ANS_LOGI("Sync bundle %{public}d %{public}s %{public}zu.", syncType, bundleName.c_str(), peerDevices.size());
416 });
417 serviceQueue_->submit(task);
418 }
419
OnApplicationInfnChanged(const std::string & bundleName)420 void DistributedService::OnApplicationInfnChanged(const std::string& bundleName)
421 {
422 return;
423 }
424 #endif
425
HandleMatchSync(const std::shared_ptr<TlvBox> & boxMessage)426 void DistributedService::HandleMatchSync(const std::shared_ptr<TlvBox>& boxMessage)
427 {
428 DistributedDeviceInfo peerDevice;
429 NotifticationMatchBox matchBox = NotifticationMatchBox(boxMessage);
430 if (!matchBox.GetLocalDeviceId(peerDevice.deviceId_)) {
431 ANS_LOGI("Dans handle match device id failed.");
432 return;
433 }
434 int32_t matchType = 0;
435 if (!matchBox.GetMatchType(matchType)) {
436 ANS_LOGI("Dans handle match sync failed.");
437 return;
438 }
439 ANS_LOGI("Dans handle match device type %{public}d.", matchType);
440 DistributedDeviceInfo device;
441 if (!DistributedDeviceService::GetInstance().GetDeviceInfo(peerDevice.deviceId_, device)) {
442 return;
443 }
444 #ifdef DISTRIBUTED_FEATURE_MASTER
445 if (matchType == MatchType::MATCH_SYN) {
446 DistributedDeviceService::GetInstance().SyncDeviceMatch(device, MatchType::MATCH_ACK);
447 DistributedPublishService::GetInstance().SyncLiveViewNotification(device, true);
448 } else if (matchType == MatchType::MATCH_ACK) {
449 DistributedSubscribeService::GetInstance().SubscribeNotification(device);
450 DistributedPublishService::GetInstance().SyncLiveViewNotification(device, false);
451 }
452 #else
453 if (DistributedDeviceService::GetInstance().IsLocalPadOrPC()) {
454 if (matchType == MatchType::MATCH_SYN) {
455 DistributedSubscribeService::GetInstance().SubscribeNotification(device);
456 DistributedDeviceService::GetInstance().InitCurrentDeviceStatus();
457 DistributedBundleService::GetInstance().SyncInstalledBundles(device, true);
458 DistributedDeviceService::GetInstance().SyncDeviceMatch(device, MatchType::MATCH_ACK);
459 return;
460 }
461 if (matchType == MatchType::MATCH_OFFLINE) {
462 DistributedSubscribeService::GetInstance().UnSubscribeNotification(device.deviceId_,
463 device.deviceType_, false);
464 DistributedClient::GetInstance().ReleaseDevice(device.deviceId_, device.deviceType_, false);
465 DistributedDeviceService::GetInstance().ResetDeviceInfo(device.deviceId_, DeviceState::STATE_OFFLINE);
466 }
467 }
468
469 if (matchType == MatchType::MATCH_SYN) {
470 DistributedDeviceService::GetInstance().SyncDeviceMatch(device, MatchType::MATCH_ACK);
471 } else if (matchType == MatchType::MATCH_ACK) {
472 DistributedDeviceService::GetInstance().InitCurrentDeviceStatus();
473 DistributedSubscribeService::GetInstance().SubscribeNotification(device);
474 }
475 #endif
476 }
477
OnHandleMsg(std::shared_ptr<TlvBox> & box)478 void DistributedService::OnHandleMsg(std::shared_ptr<TlvBox>& box)
479 {
480 if (serviceQueue_ == nullptr || box == nullptr) {
481 ANS_LOGE("Check handler is null.");
482 return;
483 }
484 std::function<void()> task = std::bind([&, box]() {
485 int32_t type;
486 if (!box->GetMessageType(type)) {
487 ANS_LOGW("Dans invalid message type failed.");
488 return;
489 }
490 ANS_LOGI("Dans handle message type %{public}d.", type);
491 switch (type) {
492 case NotificationEventType::NOTIFICATION_MATCH_SYNC:
493 HandleMatchSync(box);
494 break;
495 case NotificationEventType::REMOVE_NOTIFICATION:
496 DistributedPublishService::GetInstance().RemoveNotification(box);
497 break;
498 case NotificationEventType::REMOVE_ALL_NOTIFICATIONS:
499 DistributedPublishService::GetInstance().RemoveNotifications(box);
500 break;
501 case NotificationEventType::BUNDLE_ICON_SYNC:
502 DistributedBundleService::GetInstance().HandleBundleIconSync(box);
503 break;
504 case NotificationEventType::NOTIFICATION_RESPONSE_SYNC:
505 case NotificationEventType::NOTIFICATION_RESPONSE_REPLY_SYNC:
506 DistributedOperationService::GetInstance().HandleNotificationOperation(box);
507 break;
508 #ifdef DISTRIBUTED_FEATURE_MASTER
509 case NotificationEventType::NOTIFICATION_STATE_SYNC:
510 DistributedDeviceService::GetInstance().SetDeviceStatus(box);
511 break;
512 case NotificationEventType::INSTALLED_BUNDLE_SYNC:
513 DistributedBundleService::GetInstance().SetDeviceBundleList(box);
514 break;
515 #else
516 case NotificationEventType::PUBLISH_NOTIFICATION:
517 DistributedPublishService::GetInstance().PublishNotification(box);
518 break;
519 case NotificationEventType::SYNC_NOTIFICATION:
520 DistributedPublishService::GetInstance().PublishSynchronousLiveView(box);
521 break;
522 case NotificationEventType::REMOVE_ALL_DISTRIBUTED_NOTIFICATIONS:
523 DistributedPublishService::GetInstance().RemoveAllDistributedNotifications(box);
524 break;
525 #endif
526 default:
527 break;
528 }
529 });
530 serviceQueue_->submit(task);
531 }
532
OnReceiveMsg(const void * data,uint32_t dataLen)533 void DistributedService::OnReceiveMsg(const void *data, uint32_t dataLen)
534 {
535 if (!TlvBox::CheckMessageCRC((const unsigned char*)data, dataLen)) {
536 ANS_LOGW("Dans check message crc failed.");
537 return;
538 }
539 std::shared_ptr<TlvBox> box = std::make_shared<TlvBox>();
540 if (!box->Parse((const unsigned char*)data, dataLen - sizeof(uint32_t))) {
541 ANS_LOGW("Dans parse message failed.");
542 return;
543 }
544 OnHandleMsg(box);
545 }
546
OnConsumedSetFlags(const std::shared_ptr<Notification> & request,const DistributedDeviceInfo & peerDevice)547 bool DistributedService::OnConsumedSetFlags(const std::shared_ptr<Notification> &request,
548 const DistributedDeviceInfo& peerDevice)
549 {
550 std::string deviceType = DistributedDeviceService::DeviceTypeToTypeString(peerDevice.deviceType_);
551 sptr<NotificationRequest> requestPoint = request->GetNotificationRequestPoint();
552 auto flagsMap = requestPoint->GetDeviceFlags();
553 if (flagsMap == nullptr || flagsMap->size() <= 0) {
554 return false;
555 }
556 auto flagIter = flagsMap->find(deviceType);
557 if (flagIter != flagsMap->end() && flagIter->second != nullptr) {
558 requestPoint->SetFlags(flagIter->second);
559 ANS_LOGI("SetFlags-final, notificationKey = %{public}s flags = %{public}d deviceType: %{public}s.",
560 requestPoint->GetKey().c_str(), requestPoint->GetFlags()->GetReminderFlags(), deviceType.c_str());
561 } else {
562 return false;
563 }
564 return true;
565 }
566 }
567 }
568