• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LOGD("called");
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     int32_t userId = NotificationCloneUtil::GetActiveUserId();
73     if (!profiles_.empty()) {
74         NotificationPreferences::GetInstance()->DelBatchCloneProfileInfo(userId, profiles_);
75         profiles_.clear();
76     }
77     for (const auto &profile : jsonObject) {
78         sptr<NotificationDoNotDisturbProfile> item = new (std::nothrow) NotificationDoNotDisturbProfile();
79         if (item == nullptr) {
80             return;
81         }
82         item->FromJson(profile.dump());
83         profiles_.push_back(item);
84     }
85 
86     if (cloneDisturbQueue_ == nullptr || profiles_.empty()) {
87         ANS_LOGE("Clone queue is invalidated or empty %{public}zu.", profiles_.size());
88         return;
89     }
90 
91     cloneDisturbQueue_->submit_h(std::bind([&, userId]() {
92         int64_t profileId = -1;
93         std::string name;
94         std::map<std::string, int32_t> uidMap;
95         for (auto profile = profiles_.begin(); profile != profiles_.end();) {
96             std::vector<NotificationBundleOption> exitBunldleList;
97             std::vector<NotificationBundleOption> notExitBunldleList;
98             name = (*profile)->GetProfileName();
99             profileId = (*profile)->GetProfileId();
100             ANS_LOGI("Disturb %{public}s, %{public}zu.",
101                 std::to_string(profileId).c_str(), (*profile)->GetProfileTrustList().size());
102             GetProfileUid(userId, uidMap, (*profile)->GetProfileTrustList(), exitBunldleList, notExitBunldleList);
103             NotificationPreferences::GetInstance()->UpdateDoNotDisturbProfiles(userId,
104                 profileId, name, exitBunldleList);
105             if (notExitBunldleList.empty()) {
106                 profile = profiles_.erase(profile);
107             } else {
108                 (*profile)->SetProfileTrustList(notExitBunldleList);
109                 profile++;
110             }
111             name.clear();
112             profileId = -1;
113         }
114 
115         NotificationPreferences::GetInstance()->UpdateBatchCloneProfileInfo(userId, profiles_);
116         for (auto profile = profiles_.begin(); profile != profiles_.end(); profile++) {
117             ANS_LOGI("Clone queue left %{public}s %{public}zu.", std::to_string((*profile)->GetProfileId()).c_str(),
118                 (*profile)->GetProfileTrustList().size());
119         }
120         ANS_LOGD("end");
121     }));
122 }
123 
GetProfileUid(int32_t userId,std::map<std::string,int32_t> & uidMap,std::vector<NotificationBundleOption> trustList,std::vector<NotificationBundleOption> & exitBunldleList,std::vector<NotificationBundleOption> & notExitBunldleList)124 void NotificationCloneDisturb::GetProfileUid(int32_t userId, std::map<std::string, int32_t>& uidMap,
125     std::vector<NotificationBundleOption> trustList, std::vector<NotificationBundleOption>& exitBunldleList,
126     std::vector<NotificationBundleOption>& notExitBunldleList)
127 {
128     // get application uid with bundle name and appindex.
129     for (auto& bundle : trustList) {
130         std::string key = bundle.GetBundleName() + std::to_string(bundle.GetAppIndex());
131         if (uidMap.find(key) != uidMap.end()) {
132             bundle.SetUid(uidMap[key]);
133         } else {
134             int32_t uid = NotificationCloneUtil::GetBundleUid(bundle.GetBundleName(), userId, bundle.GetAppIndex());
135             ANS_LOGW("Notification get uid %{public}d %{public}d %{public}s.", uid, bundle.GetAppIndex(),
136                 bundle.GetBundleName().c_str());
137             bundle.SetUid(uid);
138             uidMap[key] = uid;
139         }
140         if (bundle.GetUid() == -1) {
141             notExitBunldleList.push_back(bundle);
142         } else {
143             exitBunldleList.push_back(bundle);
144         }
145     }
146 }
147 
OnUserSwitch(int32_t userId)148 void NotificationCloneDisturb::OnUserSwitch(int32_t userId)
149 {
150     ANS_LOGD("Handler user switch %{public}d", userId);
151     if (cloneDisturbQueue_ == nullptr) {
152         ANS_LOGW("null cloneDisturbQueue");
153         return;
154     }
155     cloneDisturbQueue_->submit_h(std::bind([&, userId]() {
156         profiles_.clear();
157         NotificationPreferences::GetInstance()->GetAllCloneProfileInfo(userId, profiles_);
158         for (auto profile = profiles_.begin(); profile != profiles_.end(); profile++) {
159             ANS_LOGI("Clone queue left %{public}s %{public}zu.", std::to_string((*profile)->GetProfileId()).c_str(),
160                 (*profile)->GetProfileTrustList().size());
161         }
162         ANS_LOGD("end");
163     }));
164 }
165 
OnRestoreStart(const std::string bundleName,int32_t appIndex,int32_t userId,int32_t uid)166 void NotificationCloneDisturb::OnRestoreStart(const std::string bundleName, int32_t appIndex,
167     int32_t userId, int32_t uid)
168 {
169     ANS_LOGI("Handler bundle event %{public}s %{public}d %{public}d %{public}d %{public}zu.",
170         bundleName.c_str(), appIndex, userId, uid, profiles_.size());
171     if (profiles_.empty()) {
172         return;
173     }
174 
175     NotificationBundleOption bundle(bundleName, uid);
176     bundle.SetAppIndex(appIndex);
177     if (cloneDisturbQueue_ == nullptr) {
178         ANS_LOGW("null cloneDisturbQueue");
179         return;
180     }
181     cloneDisturbQueue_->submit_h(std::bind([&, bundle, userId]() {
182         int64_t profileId = -1;
183         std::string name;
184         for (auto profile = profiles_.begin(); profile != profiles_.end();) {
185             name = (*profile)->GetProfileName();
186             profileId = (*profile)->GetProfileId();
187             std::vector<NotificationBundleOption> bundleList;
188             auto trustList = (*profile)->GetProfileTrustList();
189             CheckBundleInfo(trustList, bundleList, bundle);
190             NotificationPreferences::GetInstance()->UpdateDoNotDisturbProfiles(userId,
191                 profileId, name, bundleList);
192             if (trustList.empty()) {
193                 NotificationPreferences::GetInstance()->DelCloneProfileInfo(userId, *profile);
194                 profile = profiles_.erase(profile);
195             } else {
196                 (*profile)->SetProfileTrustList(trustList);
197                 profile++;
198             }
199             name.clear();
200             profileId = -1;
201         }
202         NotificationPreferences::GetInstance()->UpdateBatchCloneProfileInfo(userId, profiles_);
203         for (auto profile = profiles_.begin(); profile != profiles_.end(); profile++) {
204             ANS_LOGI("Event queue left %{public}s %{public}zu.", std::to_string((*profile)->GetProfileId()).c_str(),
205                 (*profile)->GetProfileTrustList().size());
206         }
207     }));
208 }
209 
CheckBundleInfo(std::vector<NotificationBundleOption> & trustList,std::vector<NotificationBundleOption> & bundleList,const NotificationBundleOption & bundle)210 void NotificationCloneDisturb::CheckBundleInfo(std::vector<NotificationBundleOption>& trustList,
211     std::vector<NotificationBundleOption>& bundleList, const NotificationBundleOption& bundle)
212 {
213     for (auto bundleItem = trustList.begin(); bundleItem != trustList.end(); bundleItem++) {
214         if (bundleItem->GetBundleName() == bundle.GetBundleName() &&
215             bundleItem->GetAppIndex() == bundle.GetAppIndex()) {
216             bundleList.push_back(bundle);
217             trustList.erase(bundleItem);
218             break;
219         }
220     }
221 }
222 }
223 }
224