• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "content_sensor_manager.h"
17 
18 #include <list>
19 #include <vector>
20 #include <memory>
21 #include <thread>
22 #include "collaboration_info_collector.h"
23 #include "collector.h"
24 #include "content_sensor_manager_utils.h"
25 #include "device_profile_filter_options.h"
26 #include "device_profile_manager.h"
27 #include "distributed_device_profile_errors.h"
28 #include "distributed_device_profile_log.h"
29 #include "dms_info_collector.h"
30 #include "multi_user_manager.h"
31 #include "pasteboard_info_collector.h"
32 #include "profile_data_manager.h"
33 #include "profile_cache.h"
34 #include "switch_status_collector.h"
35 #include "syscap_info_collector.h"
36 #include "system_info_collector.h"
37 
38 namespace OHOS {
39 namespace DistributedDeviceProfile {
40 namespace {
41     const std::string TAG = "ContentSensorManager";
42 }
43 
44 IMPLEMENT_SINGLE_INSTANCE(ContentSensorManager);
45 
Init()46 int32_t ContentSensorManager::Init()
47 {
48     HILOGI("call!");
49     return Collect();
50 }
51 
UnInit()52 int32_t ContentSensorManager::UnInit()
53 {
54     HILOGI("ContentSensorManager UnInit");
55     return DP_SUCCESS;
56 }
57 
Collect()58 int32_t ContentSensorManager::Collect()
59 {
60     auto csTask = [this]() {
61         HILOGI("ContentSensorManager Collect");
62         std::list<std::shared_ptr<Collector>> taskList;
63         taskList.push_back(std::make_shared<SystemInfoCollector>());
64         taskList.push_back(std::make_shared<SyscapInfoCollector>());
65         taskList.push_back(std::make_shared<DmsInfoCollector>());
66         taskList.push_back(std::make_shared<CollaborationInfoCollector>());
67         taskList.push_back(std::make_shared<PasteboardInfoCollector>());
68         taskList.push_back(std::make_shared<SwitchStatusCollector>());
69         DeviceProfile deviceProfile;
70         std::vector<ServiceProfile> svrProfileList;
71         std::vector<CharacteristicProfile> charProfileList;
72         for (const auto& task : taskList) {
73             task->ConvertToProfile(deviceProfile);
74             task->ConvertToProfile(svrProfileList);
75             task->ConvertToProfile(charProfileList);
76         }
77         deviceProfile.SetDeviceId(ContentSensorManagerUtils::GetInstance().ObtainLocalUdid());
78         DeviceProfileManager::GetInstance().PutDeviceProfile(deviceProfile);
79         CollectInfoToProfileData(deviceProfile);
80         if (!svrProfileList.empty()) {
81             DeviceProfileManager::GetInstance().PutServiceProfileBatch(svrProfileList);
82         } else {
83             HILOGI("svrProfileList is empty");
84         }
85         if (!charProfileList.empty()) {
86             DeviceProfileManager::GetInstance().PutCharacteristicProfileBatch(charProfileList);
87         } else {
88             HILOGI("charProfileList is empty");
89         }
90     };
91     std::thread csTaskThread(csTask);
92     if (!csTaskThread.joinable()) {
93         HILOGE("csTaskThread joinable is false");
94         return DP_CONTENT_SENSOR_MANAGER_INIT_FAIL;
95     }
96     csTaskThread.join();
97     return DP_SUCCESS;
98 }
99 
CollectInfoToProfileData(DeviceProfile & collectProfile)100 int32_t ContentSensorManager::CollectInfoToProfileData(DeviceProfile& collectProfile)
101 {
102     int32_t userID = MultiUserManager::GetInstance().GetCurrentForegroundUserID();
103     if (userID == DEFAULT_USER_ID) {
104         userID = U_100;
105     }
106     collectProfile.SetUserId(userID);
107     collectProfile.SetAccountId(ProfileCache::GetInstance().GetLocalAccountId());
108     DeviceProfileFilterOptions devFilterOptions;
109     devFilterOptions.AddDeviceIds(collectProfile.GetDeviceId());
110     devFilterOptions.SetUserId(collectProfile.GetUserId());
111     std::vector<DeviceProfile> oldDeviceProfiles;
112     int32_t ret = DeviceProfileDao::GetInstance().GetDeviceProfiles(devFilterOptions, oldDeviceProfiles);
113     if ((ret != DP_SUCCESS) && (ret != DP_NOT_FIND_DATA)) {
114         HILOGE("GetDeviceProfiles failed,ret=%{public}d", ret);
115         return ret;
116     }
117     if (!oldDeviceProfiles.empty()) {
118         DeviceProfile oldDeviceProfile = oldDeviceProfiles[0];
119         collectProfile.SetWiseDeviceId(oldDeviceProfile.GetWiseDeviceId());
120         collectProfile.SetWiseUserId(oldDeviceProfile.GetWiseUserId());
121         collectProfile.SetSetupType(oldDeviceProfile.GetSetupType());
122         collectProfile.SetRegisterTime(oldDeviceProfile.GetRegisterTime());
123         collectProfile.SetModifyTime(oldDeviceProfile.GetModifyTime());
124         collectProfile.SetShareTime(oldDeviceProfile.GetShareTime());
125         collectProfile.SetInternalModel(oldDeviceProfile.GetInternalModel());
126         if (collectProfile.GetAccountId().empty()) {
127             collectProfile.SetAccountId(oldDeviceProfile.GetAccountId());
128         }
129     }
130     ret = ProfileDataManager::GetInstance().PutDeviceProfile(collectProfile);
131     if (ret != DP_SUCCESS) {
132         HILOGE("PutDeviceProfile failed,ret=%{public}d", ret);
133         return ret;
134     }
135     return DP_SUCCESS;
136 }
137 } // namespace DeviceProfile
138 } // namespace OHOS
139