1 /*
2 * Copyright (c) 2021-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 "notification_clone_disturb_service.h"
17
18 #include "ans_log_wrapper.h"
19 #include "notification_preferences.h"
20 #include "notification_clone_util.h"
21
22 namespace OHOS {
23 namespace Notification {
GetInstance()24 std::shared_ptr<NotificationCloneDisturb> NotificationCloneDisturb::GetInstance()
25 {
26 static std::shared_ptr<NotificationCloneDisturb> instance =
27 std::make_shared<NotificationCloneDisturb>();
28 return instance;
29 }
30
NotificationCloneDisturb()31 NotificationCloneDisturb::NotificationCloneDisturb()
32 {
33 cloneDisturbQueue_ = std::make_shared<ffrt::queue>("NotificationCloneDisturbQueue");
34 if (!cloneDisturbQueue_) {
35 ANS_LOGE("ffrt create failed!");
36 return;
37 }
38 }
39
~NotificationCloneDisturb()40 NotificationCloneDisturb::~NotificationCloneDisturb()
41 {
42 }
43
OnBackup(nlohmann::json & jsonObject)44 ErrCode NotificationCloneDisturb::OnBackup(nlohmann::json &jsonObject)
45 {
46 int32_t userId = NotificationCloneUtil::GetActiveUserId();
47 std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
48 NotificationPreferences::GetInstance()->GetDoNotDisturbProfileListByUserId(userId, profiles);
49
50 if (profiles.empty()) {
51 ANS_LOGI("Notification disturb profile list is empty.");
52 return ERR_OK;
53 }
54 jsonObject = nlohmann::json::array();
55 for (size_t index = 0; index < profiles.size(); index++) {
56 nlohmann::json jsonNode;
57 profiles[index]->GetProfileJson(jsonNode);
58 jsonObject.emplace_back(jsonNode);
59 }
60 ANS_LOGD("Notification disturb profile list %{public}s", jsonObject.dump().c_str());
61 return ERR_OK;
62 }
63
OnRestore(const nlohmann::json & jsonObject)64 void NotificationCloneDisturb::OnRestore(const nlohmann::json &jsonObject)
65 {
66 ANS_LOGI("Notification disturb profile list on restore.");
67 if (jsonObject.is_null() || !jsonObject.is_array()) {
68 ANS_LOGI("Notification disturb profile list is null or not array.");
69 return;
70 }
71
72 profiles_.clear();
73 for (const auto &profile : jsonObject) {
74 sptr<NotificationDoNotDisturbProfile> item = new (std::nothrow) NotificationDoNotDisturbProfile();
75 item->FromJson(profile.dump());
76 profiles_.push_back(item);
77 }
78
79 if (cloneDisturbQueue_ == nullptr || profiles_.empty()) {
80 ANS_LOGE("Clone queue is invalidated or empty %{public}zu.", profiles_.size());
81 return;
82 }
83
84 int32_t userId = NotificationCloneUtil::GetActiveUserId();
85 cloneDisturbQueue_->submit_h(std::bind([&, userId]() {
86 ANS_LOGI("Notification disturb profile ffrt start %{public}zu.", profiles_.size());
87 int32_t profileId = -1;
88 std::string name;
89 std::map<std::string, int32_t> uidMap;
90 for (auto profile = profiles_.begin(); profile != profiles_.end();) {
91 std::vector<NotificationBundleOption> exitBunldleList;
92 std::vector<NotificationBundleOption> notExitBunldleList;
93 name = (*profile)->GetProfileName();
94 profileId = (*profile)->GetProfileId();
95 ANS_LOGI("Notification disturb profile %{public}d, %{public}zu.", profileId,
96 (*profile)->GetProfileTrustList().size());
97 GetProfileUid(userId, uidMap, (*profile)->GetProfileTrustList(), exitBunldleList, notExitBunldleList);
98 NotificationPreferences::GetInstance()->UpdateDoNotDisturbProfiles(userId,
99 profileId, name, exitBunldleList);
100 if (notExitBunldleList.empty()) {
101 profile = profiles_.erase(profile);
102 } else {
103 (*profile)->SetProfileTrustList(notExitBunldleList);
104 profile++;
105 }
106 name.clear();
107 profileId = -1;
108 }
109
110 for (auto profile = profiles_.begin(); profile != profiles_.end(); profile++) {
111 ANS_LOGI("Clone queue left %{public}d %{public}zu.", (*profile)->GetProfileId(),
112 (*profile)->GetProfileTrustList().size());
113 }
114 ANS_LOGI("Notification disturb profile list on restore end.");
115 }));
116 }
117
GetProfileUid(int32_t userId,std::map<std::string,int32_t> & uidMap,std::vector<NotificationBundleOption> trustList,std::vector<NotificationBundleOption> & exitBunldleList,std::vector<NotificationBundleOption> & notExitBunldleList)118 void NotificationCloneDisturb::GetProfileUid(int32_t userId, std::map<std::string, int32_t>& uidMap,
119 std::vector<NotificationBundleOption> trustList, std::vector<NotificationBundleOption>& exitBunldleList,
120 std::vector<NotificationBundleOption>& notExitBunldleList)
121 {
122 // get application uid with bundle name and appindex.
123 for (auto& bundle : trustList) {
124 std::string key = bundle.GetBundleName() + std::to_string(bundle.GetAppIndex());
125 if (uidMap.find(key) != uidMap.end()) {
126 bundle.SetUid(uidMap[key]);
127 } else {
128 int32_t uid = NotificationCloneUtil::GetBundleUid(bundle.GetBundleName(), userId, bundle.GetAppIndex());
129 ANS_LOGW("Notification get uid %{public}d %{public}d %{public}s.", uid, bundle.GetAppIndex(),
130 bundle.GetBundleName().c_str());
131 bundle.SetUid(uid);
132 uidMap[key] = uid;
133 }
134 if (bundle.GetUid() == -1) {
135 notExitBunldleList.push_back(bundle);
136 } else {
137 exitBunldleList.push_back(bundle);
138 }
139 }
140 }
141
OnBundleDataAdd(const sptr<NotificationBundleOption> & bundleOption)142 void NotificationCloneDisturb::OnBundleDataAdd(const sptr<NotificationBundleOption> &bundleOption)
143 {
144 HandlerBundleEvent(bundleOption->GetBundleName(), bundleOption->GetAppIndex(), bundleOption->GetUid());
145 }
146
OnBundleDataUpdate(const sptr<NotificationBundleOption> & bundleOption)147 void NotificationCloneDisturb::OnBundleDataUpdate(const sptr<NotificationBundleOption> &bundleOption)
148 {
149 HandlerBundleEvent(bundleOption->GetBundleName(), bundleOption->GetAppIndex(), bundleOption->GetUid());
150 }
151
OnUserSwitch(int32_t userId)152 void NotificationCloneDisturb::OnUserSwitch(int32_t userId)
153 {
154 ANS_LOGI("Handler user switch %{public}d", userId);
155 if (cloneDisturbQueue_ == nullptr) {
156 ANS_LOGW("Clone disturb queue is null.");
157 return;
158 }
159 cloneDisturbQueue_->submit_h(std::bind([&]() {
160 profiles_.clear();
161 }));
162 }
163
HandlerBundleEvent(const std::string bundleName,int32_t appIndex,int32_t uid)164 void NotificationCloneDisturb::HandlerBundleEvent(const std::string bundleName, int32_t appIndex, int32_t uid)
165 {
166 ANS_LOGI("Handler bundle event %{public}s %{public}d %{public}d %{public}zu.",
167 bundleName.c_str(), appIndex, uid, profiles_.size());
168 if (profiles_.empty()) {
169 return;
170 }
171
172 int32_t userId = NotificationCloneUtil::GetActiveUserId();
173 NotificationBundleOption bundle(bundleName, uid);
174 bundle.SetAppIndex(appIndex);
175 if (cloneDisturbQueue_ == nullptr) {
176 ANS_LOGW("Clone disturb queue is null.");
177 return;
178 }
179 cloneDisturbQueue_->submit_h(std::bind([&, bundle, userId]() {
180 int32_t profileId = -1;
181 std::string name;
182 for (auto profile = profiles_.begin(); profile != profiles_.end();) {
183 name = (*profile)->GetProfileName();
184 profileId = (*profile)->GetProfileId();
185 std::vector<NotificationBundleOption> bundleList;
186 auto trustList = (*profile)->GetProfileTrustList();
187 CheckBundleInfo(trustList, bundleList, bundle);
188 NotificationPreferences::GetInstance()->UpdateDoNotDisturbProfiles(userId,
189 profileId, name, bundleList);
190 if (trustList.empty()) {
191 profile = profiles_.erase(profile);
192 } else {
193 (*profile)->SetProfileTrustList(trustList);
194 profile++;
195 }
196 name.clear();
197 profileId = -1;
198 }
199 for (auto profile = profiles_.begin(); profile != profiles_.end(); profile++) {
200 ANS_LOGI("Event queue left %{public}d %{public}zu.", (*profile)->GetProfileId(),
201 (*profile)->GetProfileTrustList().size());
202 }
203 }));
204 }
205
CheckBundleInfo(std::vector<NotificationBundleOption> & trustList,std::vector<NotificationBundleOption> & bundleList,const NotificationBundleOption & bundle)206 void NotificationCloneDisturb::CheckBundleInfo(std::vector<NotificationBundleOption>& trustList,
207 std::vector<NotificationBundleOption>& bundleList, const NotificationBundleOption& bundle)
208 {
209 for (auto bundleItem = trustList.begin(); bundleItem != trustList.end(); bundleItem++) {
210 if (bundleItem->GetBundleName() == bundle.GetBundleName() &&
211 bundleItem->GetAppIndex() == bundle.GetAppIndex()) {
212 bundleList.push_back(bundle);
213 trustList.erase(bundleItem);
214 break;
215 }
216 }
217 }
218 }
219 }
220