• 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_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     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         item->FromJson(profile.dump());
80         profiles_.push_back(item);
81     }
82 
83     if (cloneDisturbQueue_ == nullptr || profiles_.empty()) {
84         ANS_LOGE("Clone queue is invalidated or empty %{public}zu.", profiles_.size());
85         return;
86     }
87 
88     cloneDisturbQueue_->submit_h(std::bind([&, userId]() {
89         int32_t profileId = -1;
90         std::string name;
91         std::map<std::string, int32_t> uidMap;
92         for (auto profile = profiles_.begin(); profile != profiles_.end();) {
93             std::vector<NotificationBundleOption> exitBunldleList;
94             std::vector<NotificationBundleOption> notExitBunldleList;
95             name = (*profile)->GetProfileName();
96             profileId = (*profile)->GetProfileId();
97             ANS_LOGI("Disturb %{public}d, %{public}zu.", profileId, (*profile)->GetProfileTrustList().size());
98             GetProfileUid(userId, uidMap, (*profile)->GetProfileTrustList(), exitBunldleList, notExitBunldleList);
99             NotificationPreferences::GetInstance()->UpdateDoNotDisturbProfiles(userId,
100                 profileId, name, exitBunldleList);
101             if (notExitBunldleList.empty()) {
102                 profile = profiles_.erase(profile);
103             } else {
104                 (*profile)->SetProfileTrustList(notExitBunldleList);
105                 profile++;
106             }
107             name.clear();
108             profileId = -1;
109         }
110 
111         NotificationPreferences::GetInstance()->UpdateBatchCloneProfileInfo(userId, profiles_);
112         for (auto profile = profiles_.begin(); profile != profiles_.end(); profile++) {
113             ANS_LOGI("Clone queue left %{public}d %{public}zu.", (*profile)->GetProfileId(),
114                 (*profile)->GetProfileTrustList().size());
115         }
116         ANS_LOGI("Notification disturb profile list on restore end.");
117     }));
118 }
119 
GetProfileUid(int32_t userId,std::map<std::string,int32_t> & uidMap,std::vector<NotificationBundleOption> trustList,std::vector<NotificationBundleOption> & exitBunldleList,std::vector<NotificationBundleOption> & notExitBunldleList)120 void NotificationCloneDisturb::GetProfileUid(int32_t userId, std::map<std::string, int32_t>& uidMap,
121     std::vector<NotificationBundleOption> trustList, std::vector<NotificationBundleOption>& exitBunldleList,
122     std::vector<NotificationBundleOption>& notExitBunldleList)
123 {
124     // get application uid with bundle name and appindex.
125     for (auto& bundle : trustList) {
126         std::string key = bundle.GetBundleName() + std::to_string(bundle.GetAppIndex());
127         if (uidMap.find(key) != uidMap.end()) {
128             bundle.SetUid(uidMap[key]);
129         } else {
130             int32_t uid = NotificationCloneUtil::GetBundleUid(bundle.GetBundleName(), userId, bundle.GetAppIndex());
131             ANS_LOGW("Notification get uid %{public}d %{public}d %{public}s.", uid, bundle.GetAppIndex(),
132                 bundle.GetBundleName().c_str());
133             bundle.SetUid(uid);
134             uidMap[key] = uid;
135         }
136         if (bundle.GetUid() == -1) {
137             notExitBunldleList.push_back(bundle);
138         } else {
139             exitBunldleList.push_back(bundle);
140         }
141     }
142 }
143 
OnUserSwitch(int32_t userId)144 void NotificationCloneDisturb::OnUserSwitch(int32_t userId)
145 {
146     ANS_LOGI("Handler user switch %{public}d", userId);
147     if (cloneDisturbQueue_ == nullptr) {
148         ANS_LOGW("Clone disturb queue is null.");
149         return;
150     }
151     cloneDisturbQueue_->submit_h(std::bind([&, userId]() {
152         profiles_.clear();
153         NotificationPreferences::GetInstance()->GetAllCloneProfileInfo(userId, profiles_);
154         for (auto profile = profiles_.begin(); profile != profiles_.end(); profile++) {
155             ANS_LOGI("Clone queue left %{public}d %{public}zu.", (*profile)->GetProfileId(),
156                 (*profile)->GetProfileTrustList().size());
157         }
158         ANS_LOGI("Notification disturb profile list on recover end.");
159     }));
160 }
161 
OnRestoreStart(const std::string bundleName,int32_t appIndex,int32_t userId,int32_t uid)162 void NotificationCloneDisturb::OnRestoreStart(const std::string bundleName, int32_t appIndex,
163     int32_t userId, int32_t uid)
164 {
165     ANS_LOGI("Handler bundle event %{public}s %{public}d %{public}d %{public}d %{public}zu.",
166         bundleName.c_str(), appIndex, userId, uid, profiles_.size());
167     if (profiles_.empty()) {
168         return;
169     }
170 
171     NotificationBundleOption bundle(bundleName, uid);
172     bundle.SetAppIndex(appIndex);
173     if (cloneDisturbQueue_ == nullptr) {
174         ANS_LOGW("Clone disturb queue is null.");
175         return;
176     }
177     cloneDisturbQueue_->submit_h(std::bind([&, bundle, userId]() {
178         int32_t profileId = -1;
179         std::string name;
180         for (auto profile = profiles_.begin(); profile != profiles_.end();) {
181             name = (*profile)->GetProfileName();
182             profileId = (*profile)->GetProfileId();
183             std::vector<NotificationBundleOption> bundleList;
184             auto trustList = (*profile)->GetProfileTrustList();
185             CheckBundleInfo(trustList, bundleList, bundle);
186             NotificationPreferences::GetInstance()->UpdateDoNotDisturbProfiles(userId,
187                 profileId, name, bundleList);
188             if (trustList.empty()) {
189                 NotificationPreferences::GetInstance()->DelCloneProfileInfo(userId, *profile);
190                 profile = profiles_.erase(profile);
191             } else {
192                 (*profile)->SetProfileTrustList(trustList);
193                 profile++;
194             }
195             name.clear();
196             profileId = -1;
197         }
198         NotificationPreferences::GetInstance()->UpdateBatchCloneProfileInfo(userId, profiles_);
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