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 #ifndef DEVICE_PROFILE_SWITCH_DISABLE
69 taskList.push_back(std::make_shared<SwitchStatusCollector>());
70 #else
71 HILOGI("this device does not support switch data, not use collect switch data");
72 #endif // DEVICE_PROFILE_SWITCH_DISABLE
73 DeviceProfile deviceProfile;
74 std::vector<ServiceProfile> svrProfileList;
75 std::vector<CharacteristicProfile> charProfileList;
76 for (const auto& task : taskList) {
77 task->ConvertToProfile(deviceProfile);
78 task->ConvertToProfile(svrProfileList);
79 task->ConvertToProfile(charProfileList);
80 }
81 deviceProfile.SetDeviceId(ContentSensorManagerUtils::GetInstance().ObtainLocalUdid());
82 DeviceProfileManager::GetInstance().PutDeviceProfile(deviceProfile);
83 CollectInfoToProfileData(deviceProfile);
84 if (!svrProfileList.empty()) {
85 DeviceProfileManager::GetInstance().PutServiceProfileBatch(svrProfileList);
86 } else {
87 HILOGI("svrProfileList is empty");
88 }
89 if (!charProfileList.empty()) {
90 DeviceProfileManager::GetInstance().PutCharacteristicProfileBatch(charProfileList);
91 } else {
92 HILOGI("charProfileList is empty");
93 }
94 };
95 std::thread csTaskThread(csTask);
96 if (!csTaskThread.joinable()) {
97 HILOGE("csTaskThread joinable is false");
98 return DP_CONTENT_SENSOR_MANAGER_INIT_FAIL;
99 }
100 csTaskThread.join();
101 return DP_SUCCESS;
102 }
103
CollectInfoToProfileData(DeviceProfile & collectProfile)104 int32_t ContentSensorManager::CollectInfoToProfileData(DeviceProfile& collectProfile)
105 {
106 int32_t userID = MultiUserManager::GetInstance().GetCurrentForegroundUserID();
107 if (userID == DEFAULT_USER_ID) {
108 userID = U_100;
109 }
110 collectProfile.SetUserId(userID);
111 collectProfile.SetAccountId(ProfileCache::GetInstance().GetLocalAccountId());
112 DeviceProfileFilterOptions devFilterOptions;
113 devFilterOptions.AddDeviceIds(collectProfile.GetDeviceId());
114 devFilterOptions.SetUserId(collectProfile.GetUserId());
115 std::vector<DeviceProfile> oldDeviceProfiles;
116 int32_t ret = DeviceProfileDao::GetInstance().GetDeviceProfiles(devFilterOptions, oldDeviceProfiles);
117 if ((ret != DP_SUCCESS) && (ret != DP_NOT_FIND_DATA)) {
118 HILOGE("GetDeviceProfiles failed,ret=%{public}d", ret);
119 return ret;
120 }
121 if (!oldDeviceProfiles.empty()) {
122 DeviceProfile oldDeviceProfile = oldDeviceProfiles[0];
123 collectProfile.SetWiseDeviceId(oldDeviceProfile.GetWiseDeviceId());
124 collectProfile.SetWiseUserId(oldDeviceProfile.GetWiseUserId());
125 collectProfile.SetSetupType(oldDeviceProfile.GetSetupType());
126 collectProfile.SetRegisterTime(oldDeviceProfile.GetRegisterTime());
127 collectProfile.SetModifyTime(oldDeviceProfile.GetModifyTime());
128 collectProfile.SetShareTime(oldDeviceProfile.GetShareTime());
129 if (collectProfile.GetAccountId().empty()) {
130 collectProfile.SetAccountId(oldDeviceProfile.GetAccountId());
131 }
132 }
133 ret = ProfileDataManager::GetInstance().PutDeviceProfile(collectProfile);
134 if (ret != DP_SUCCESS) {
135 HILOGE("PutDeviceProfile failed,ret=%{public}d", ret);
136 return ret;
137 }
138 return DP_SUCCESS;
139 }
140 } // namespace DeviceProfile
141 } // namespace OHOS
142