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 "distributed_collaboration_service.h" 17 18 #include "advanced_notification_inline.h" 19 20 namespace OHOS { 21 namespace Notification { 22 23 static const int64_t DEFAULT_CONFLICT_TIME = 5 * 1000; // 5s 24 GetInstance()25DistributedCollaborationService& DistributedCollaborationService::GetInstance() 26 { 27 static DistributedCollaborationService distributedCollaborationService; 28 return distributedCollaborationService; 29 } 30 AddCollaborativeDeleteItem(const sptr<Notification> & notification)31void DistributedCollaborationService::AddCollaborativeDeleteItem(const sptr<Notification>& notification) 32 { 33 if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { 34 return; 35 } 36 auto request = notification->GetNotificationRequestPoint(); 37 if (!request->GetDistributedCollaborate() || !request->IsCommonLiveView()) { 38 return; 39 } 40 std::string hashCode = notification->GetKey(); 41 std::lock_guard<ffrt::mutex> lock(mapLock); 42 collaborativeDeleteMap_[hashCode] = GetCurrentTime(); 43 ANS_LOGI("Collaborate add %{public}s", hashCode.c_str()); 44 } 45 CheckCollaborativePublish(const sptr<Notification> & notification)46bool DistributedCollaborationService::CheckCollaborativePublish(const sptr<Notification>& notification) 47 { 48 if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr) { 49 return true; 50 } 51 auto request = notification->GetNotificationRequestPoint(); 52 if (!request->GetDistributedCollaborate() || !request->IsCommonLiveView()) { 53 return true; 54 } 55 56 auto content = request->GetContent(); 57 if (content == nullptr || content->GetNotificationContent() == nullptr) { 58 return true; 59 } 60 std::string hashCode = notification->GetKey(); 61 auto liveView = std::static_pointer_cast<NotificationLiveViewContent>(content->GetNotificationContent()); 62 if (liveView->GetLiveViewStatus() == NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE) { 63 std::lock_guard<ffrt::mutex> lock(mapLock); 64 collaborativeDeleteMap_.erase(hashCode); 65 return true; 66 } 67 68 int64_t currentTime = GetCurrentTime(); 69 std::lock_guard<ffrt::mutex> lock(mapLock); 70 for (auto item = collaborativeDeleteMap_.begin(); item != collaborativeDeleteMap_.end();) { 71 if (currentTime < item->second || currentTime - item->second >= DEFAULT_CONFLICT_TIME) { 72 collaborativeDeleteMap_.erase(item++); 73 } else { 74 item++; 75 } 76 } 77 if (collaborativeDeleteMap_.find(hashCode) != collaborativeDeleteMap_.end()) { 78 ANS_LOGW("Collaborate conflict %{public}s", hashCode.c_str()); 79 return false; 80 } 81 return true; 82 } 83 84 } 85 } 86