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 "socket.h"
18 #include "session.h"
19 #include "distributed_device_data.h"
20 #include "dm_device_info.h"
21 #include "ans_log_wrapper.h"
22 #include "distributed_socket.h"
23 #include "distributed_preference.h"
24 #include "bundle_resource_helper.h"
25 #include "common_event_manager.h"
26 #include "common_event_support.h"
27 #include "distributed_client.h"
28 #include "ans_image_util.h"
29 #include "os_account_manager.h"
30
31 namespace OHOS {
32 namespace Notification {
33
GetCurrentActiveUserId()34 int32_t DistributedService::GetCurrentActiveUserId()
35 {
36 int32_t userId = DEFAULT_USER_ID;
37 int32_t ret = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
38 if (ret != ERR_OK) {
39 ANS_LOGW("Dans get Current userId failed %{public}d.", ret);
40 return DEFAULT_USER_ID;
41 }
42 return userId;
43 }
44
GetBundleResourceInfo(const std::string bundleName,std::string & icon)45 bool DistributedService::GetBundleResourceInfo(const std::string bundleName, std::string& icon)
46 {
47 AppExecFwk::BundleResourceInfo resourceInfo;
48 if (DelayedSingleton<BundleResourceHelper>::GetInstance()->GetBundleInfo(bundleName, resourceInfo) != 0) {
49 ANS_LOGW("Dans get bundle icon failed %{public}s.", bundleName.c_str());
50 return false;
51 }
52 std::shared_ptr<Media::PixelMap> iconPixelmap = AnsImageUtil::CreatePixelMapByString(resourceInfo.icon);
53 if (!AnsImageUtil::ImageScale(iconPixelmap, DEFAULT_ICON_WITHE, DEFAULT_ICON_HEIGHT)) {
54 return false;
55 }
56 icon = AnsImageUtil::PackImage(iconPixelmap);
57 ANS_LOGI("Dans get bundle icon bundle %{public}s %{public}d.", bundleName.c_str(),
58 (int32_t)(resourceInfo.icon.size()));
59 return true;
60 }
61
GetNeedUpdateDevice(bool updatedExit,const std::string & bundleName,std::vector<DistributedDeviceInfo> & updateDeviceList)62 void DistributedService::GetNeedUpdateDevice(bool updatedExit, const std::string& bundleName,
63 std::vector<DistributedDeviceInfo>& updateDeviceList)
64 {
65 for (auto& device : peerDevice_) {
66 if (device.second.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
67 continue;
68 }
69 auto iter = bundleIconCache_.find(device.first);
70 if (updatedExit) {
71 if (iter == bundleIconCache_.end() ||
72 iter->second.find(bundleName) == iter->second.end()) {
73 continue;
74 }
75 updateDeviceList.push_back(device.second);
76 } else {
77 if (iter != bundleIconCache_.end() &&
78 iter->second.find(bundleName) != iter->second.end()) {
79 continue;
80 }
81 if (iter == bundleIconCache_.end()) {
82 std::set<std::string> cachedIcons = { bundleName };
83 bundleIconCache_.insert(std::make_pair(device.first, cachedIcons));
84 } else {
85 iter->second.insert(bundleName);
86 }
87 updateDeviceList.push_back(device.second);
88 }
89 }
90 }
91
HandleBundleChanged(const std::string & bundleName,bool updatedExit)92 void DistributedService::HandleBundleChanged(const std::string& bundleName, bool updatedExit)
93 {
94 if (serviceQueue_ == nullptr) {
95 ANS_LOGE("Check handler is null.");
96 return;
97 }
98
99 std::function<void()> task = std::bind([&, bundleName, updatedExit]() {
100 std::vector<DistributedDeviceInfo> updateDeviceList;
101 GetNeedUpdateDevice(updatedExit, bundleName, updateDeviceList);
102 if (updateDeviceList.empty()) {
103 ANS_LOGI("No need update %{public}s.", bundleName.c_str());
104 return;
105 }
106 std::string icon;
107 if (!GetBundleResourceInfo(bundleName, icon)) {
108 return;
109 }
110 std::unordered_map<std::string, std::string> icons;
111 icons.insert(std::make_pair(bundleName, icon));
112 for (auto& device : updateDeviceList) {
113 UpdateBundlesIcon(icons, device);
114 }
115 });
116 serviceQueue_->submit(task);
117 }
118
HandleBundleRemoved(const std::string & bundleName)119 void DistributedService::HandleBundleRemoved(const std::string& bundleName)
120 {
121 if (serviceQueue_ == nullptr) {
122 ANS_LOGE("Check handler is null.");
123 return;
124 }
125
126 std::function<void()> task = std::bind([&, bundleName]() {
127 for (auto& device : peerDevice_) {
128 auto iter = bundleIconCache_.find(device.first);
129 if (iter == bundleIconCache_.end() ||
130 iter->second.find(bundleName) == iter->second.end()) {
131 continue;
132 }
133 iter->second.erase(bundleName);
134 if (device.second.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PHONE) {
135 continue;
136 }
137 BundleIconBox iconBox;
138 iconBox.SetIconSyncType(IconSyncType::REMOVE_BUNDLE_ICON);
139 iconBox.SetBundleList({bundleName});
140 iconBox.SetLocalDeviceId(localDevice_.deviceId_);
141 if (!iconBox.Serialize()) {
142 ANS_LOGW("Dans HandleBundleRemove serialize failed.");
143 continue;
144 }
145
146 DistributedClient::GetInstance().SendMessage(iconBox.GetByteBuffer(),
147 iconBox.GetByteLength(), TransDataType::DATA_TYPE_MESSAGE,
148 device.second.deviceId_, device.second.deviceType_);
149 ANS_LOGI("Dans ReportBundleIconList %{public}s %{public}d %{public}s %{public}d.",
150 StringAnonymous(device.second.deviceId_).c_str(), device.second.deviceType_,
151 StringAnonymous(localDevice_.deviceId_).c_str(), localDevice_.deviceType_);
152 }
153 });
154 serviceQueue_->submit(task);
155 }
156
HandleBundlesEvent(const std::string & bundleName,const std::string & action)157 void DistributedService::HandleBundlesEvent(const std::string& bundleName, const std::string& action)
158 {
159 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
160 HandleBundleChanged(bundleName, true);
161 }
162 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
163 HandleBundleRemoved(bundleName);
164 }
165 }
166
167 }
168 }
169