• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_device_data_service.h"
17 
18 #include "ans_log_wrapper.h"
19 #include "ans_inner_errors.h"
20 #include "distributed_data_define.h"
21 
22 namespace OHOS {
23 namespace Notification {
24 
GetInstance()25 DistributedDeviceDataService& DistributedDeviceDataService::GetInstance()
26 {
27     static DistributedDeviceDataService distributedDeviceDataService;
28     return distributedDeviceDataService;
29 }
30 
ResetTargetDevice(const std::string & deviceType,const std::string & deviceId)31 void DistributedDeviceDataService::ResetTargetDevice(const std::string& deviceType, const std::string& deviceId)
32 {
33     std::lock_guard<ffrt::mutex> lock(lock_);
34     for (auto itemIter = devicesData_.begin(); itemIter != devicesData_.end(); itemIter++) {
35         if (itemIter->deviceType == deviceType && itemIter->deviceId == deviceId) {
36             devicesData_.erase(itemIter);
37             ANS_LOGI("Reset device %{public}s %{public}s", deviceType.c_str(), StringAnonymous(deviceId).c_str());
38             return;
39         }
40     }
41 }
42 
SetDeviceSyncSwitch(const std::string & deviceType,const std::string & deviceId,bool notificationEnable,bool liveViewEnable)43 int32_t DistributedDeviceDataService::SetDeviceSyncSwitch(const std::string& deviceType, const std::string& deviceId,
44     bool notificationEnable, bool liveViewEnable)
45 {
46     std::lock_guard<ffrt::mutex> lock(lock_);
47     for (auto itemIter = devicesData_.begin(); itemIter != devicesData_.end(); itemIter++) {
48         if (itemIter->deviceType == deviceType && itemIter->deviceId == deviceId) {
49             itemIter->liveViewSyncEnable = liveViewEnable;
50             itemIter->notificationSyncEnable = notificationEnable;
51             ANS_LOGI("Set device %{public}s %{public}d %{public}d", StringAnonymous(deviceId).c_str(),
52                 notificationEnable, liveViewEnable);
53             return ERR_OK;
54         }
55     }
56 
57     if (deviceType.empty() || deviceId.empty()) {
58         ANS_LOGW("Set device failed %{public}s %{public}d %{public}d", StringAnonymous(deviceId).c_str(),
59             notificationEnable, liveViewEnable);
60         return ERR_ANS_INVALID_PARAM;
61     }
62     DeviceData deviceData;
63     deviceData.deviceType = deviceType;
64     deviceData.deviceId = deviceId;
65     deviceData.liveViewSyncEnable = liveViewEnable;
66     deviceData.notificationSyncEnable = notificationEnable;
67     devicesData_.emplace_back(deviceData);
68     ANS_LOGI("Set device add %{public}s %{public}d %{public}d", StringAnonymous(deviceId).c_str(),
69         notificationEnable, liveViewEnable);
70     return ERR_OK;
71 }
72 
SetTargetDeviceBundleList(const std::string & deviceType,const std::string & deviceId,int operatorType,const std::vector<std::string> & bundleList,const std::vector<std::string> & labelList)73 int32_t DistributedDeviceDataService::SetTargetDeviceBundleList(const std::string& deviceType,
74     const std::string& deviceId, int operatorType, const std::vector<std::string>& bundleList,
75     const std::vector<std::string>& labelList)
76 {
77     std::lock_guard<ffrt::mutex> lock(lock_);
78     for (auto itemIter = devicesData_.begin(); itemIter != devicesData_.end(); itemIter++) {
79         if (itemIter->deviceType != deviceType || itemIter->deviceId != deviceId) {
80             continue;
81         }
82         ANS_LOGI("Set bundles %{public}s %{public}s %{public}d %{public}zu %{public}zu.",
83             StringAnonymous(deviceId).c_str(), deviceType.c_str(), operatorType,
84             itemIter->installedBundles.size(), bundleList.size());
85         if (operatorType == BundleListOperationType::ADD_BUNDLES) {
86             for (uint32_t i = 0; i < bundleList.size() && i < labelList.size(); i++) {
87                 itemIter->installedBundles[bundleList[i]] = labelList[i];
88             }
89         }
90 
91         if (operatorType == BundleListOperationType::REMOVE_BUNDLES) {
92             for (uint32_t i = 0; i < bundleList.size() && i < labelList.size(); i++) {
93                 itemIter->installedBundles.erase(bundleList[i]);
94             }
95         }
96 
97         if (operatorType == BundleListOperationType::RELEASE_BUNDLES) {
98             itemIter->installedBundles.clear();
99         }
100         ANS_LOGI("After Set bundles %{public}s %{public}d %{public}zu.",
101             deviceType.c_str(), operatorType, itemIter->installedBundles.size());
102         return ERR_OK;
103     }
104 
105     if (deviceType.empty() || deviceId.empty() || operatorType != BundleListOperationType::ADD_BUNDLES) {
106         ANS_LOGW("Set device failed %{public}s %{public}d", StringAnonymous(deviceId).c_str(),
107             operatorType);
108         return ERR_ANS_INVALID_PARAM;
109     }
110 
111     DeviceData deviceData;
112     deviceData.deviceType = deviceType;
113     deviceData.deviceId = deviceId;
114     for (uint32_t i = 0; i < bundleList.size() && i < labelList.size(); i++) {
115         deviceData.installedBundles[bundleList[i]] = labelList[i];
116     }
117     devicesData_.emplace_back(deviceData);
118     ANS_LOGI("Set device add %{public}s %{public}s %{public}zu", StringAnonymous(deviceId).c_str(),
119         deviceType.c_str(), bundleList.size());
120     return ERR_OK;
121 }
122 
GetTargetDeviceBundleList(const std::string & deviceType,const std::string & deviceId,std::vector<std::string> & bundleList,std::vector<std::string> & labelList)123 int32_t DistributedDeviceDataService::GetTargetDeviceBundleList(const std::string& deviceType,
124     const std::string& deviceId, std::vector<std::string>& bundleList, std::vector<std::string>& labelList)
125 {
126     std::lock_guard<ffrt::mutex> lock(lock_);
127     for (auto& item : devicesData_) {
128         if (item.deviceType == deviceType && item.deviceId == deviceId) {
129             for (auto bundleInfo : item.installedBundles) {
130                 bundleList.push_back(bundleInfo.first);
131                 labelList.push_back(bundleInfo.second);
132                 return ERR_OK;
133             }
134         }
135     }
136     ANS_LOGW("Get bundle %{public}s %{public}s", deviceType.c_str(), StringAnonymous(deviceId).c_str());
137     return ERR_ANS_INVALID_PARAM;
138 }
139 
CheckDeviceBundleExist(const std::string & deviceType,const std::string & deviceId,const std::string & bundleName,const std::string & label)140 bool DistributedDeviceDataService::CheckDeviceBundleExist(const std::string& deviceType, const std::string& deviceId,
141     const std::string& bundleName, const std::string& label)
142 {
143     std::lock_guard<ffrt::mutex> lock(lock_);
144     for (auto& item : devicesData_) {
145         if (item.deviceType != deviceType || item.deviceId != deviceId) {
146             continue;
147         }
148         for (auto installedBundle : item.installedBundles) {
149             if (installedBundle.first == bundleName || installedBundle.second == label) {
150                 return true;
151             }
152         }
153     }
154     ANS_LOGW("Get bundle failed %{public}s %{public}s", deviceType.c_str(), StringAnonymous(deviceId).c_str());
155     return false;
156 }
157 
GetDeviceNotificationEnable(const std::string & deviceType,const std::string & deviceId)158 bool DistributedDeviceDataService::GetDeviceNotificationEnable(const std::string& deviceType,
159     const std::string& deviceId)
160 {
161     std::lock_guard<ffrt::mutex> lock(lock_);
162     for (auto& item : devicesData_) {
163         if (item.deviceType == deviceType && item.deviceId == deviceId) {
164             return item.notificationSyncEnable;
165         }
166     }
167     ANS_LOGW("Get notification failed %{public}s %{public}s", deviceType.c_str(), StringAnonymous(deviceId).c_str());
168     return false;
169 }
170 
GetDeviceLiveViewEnable(const std::string & deviceType,const std::string & deviceId)171 bool DistributedDeviceDataService::GetDeviceLiveViewEnable(const std::string& deviceType, const std::string& deviceId)
172 {
173     std::lock_guard<ffrt::mutex> lock(lock_);
174     for (auto& item : devicesData_) {
175         if (item.deviceType == deviceType && item.deviceId == deviceId) {
176             return item.liveViewSyncEnable;
177         }
178     }
179     ANS_LOGW("Get live view failed %{public}s %{public}s", deviceType.c_str(), StringAnonymous(deviceId).c_str());
180     return false;
181 }
182 }
183 }
184