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 #include "notification_preferences_info.h"
16 
17 #include "ans_log_wrapper.h"
18 #include "notification_constant.h"
19 #include "bundle_manager_helper.h"
20 
21 namespace OHOS {
22 namespace Notification {
23 namespace {
24 const static std::string KEY_UNDER_LINE = "_";
25 } // namespace
26 
BundleInfo()27 NotificationPreferencesInfo::BundleInfo::BundleInfo()
28 {
29 }
30 
~BundleInfo()31 NotificationPreferencesInfo::BundleInfo::~BundleInfo()
32 {
33 }
34 
SetBundleName(const std::string & name)35 void NotificationPreferencesInfo::BundleInfo::SetBundleName(const std::string &name)
36 {
37     bundleName_ = name;
38 }
39 
GetBundleName() const40 std::string NotificationPreferencesInfo::BundleInfo::GetBundleName() const
41 {
42     return bundleName_;
43 }
44 
SetImportance(const int32_t & level)45 void NotificationPreferencesInfo::BundleInfo::SetImportance(const int32_t &level)
46 {
47     importance_ = level;
48 }
49 
GetImportance() const50 int32_t NotificationPreferencesInfo::BundleInfo::GetImportance() const
51 {
52     return importance_;
53 }
54 
SetIsShowBadge(const bool & isShowBadge)55 void NotificationPreferencesInfo::BundleInfo::SetIsShowBadge(const bool &isShowBadge)
56 {
57     isShowBadge_ = isShowBadge;
58 }
59 
GetIsShowBadge() const60 bool NotificationPreferencesInfo::BundleInfo::GetIsShowBadge() const
61 {
62     return isShowBadge_;
63 }
64 
SetBadgeTotalNum(const int32_t & num)65 void NotificationPreferencesInfo::BundleInfo::SetBadgeTotalNum(const int32_t &num)
66 {
67     badgeTotalNum_ = num;
68 }
69 
GetBadgeTotalNum() const70 int32_t NotificationPreferencesInfo::BundleInfo::GetBadgeTotalNum() const
71 {
72     return badgeTotalNum_;
73 }
74 
SetEnableNotification(const bool & enable)75 void NotificationPreferencesInfo::BundleInfo::SetEnableNotification(const bool &enable)
76 {
77     isEnabledNotification_ = enable;
78 }
79 
GetEnableNotification() const80 bool NotificationPreferencesInfo::BundleInfo::GetEnableNotification() const
81 {
82     return isEnabledNotification_;
83 }
84 
85 
SetHasPoppedDialog(const bool & hasPopped)86 void NotificationPreferencesInfo::BundleInfo::SetHasPoppedDialog(const bool &hasPopped)
87 {
88     hasPoppedDialog_ = hasPopped;
89 }
90 
GetHasPoppedDialog() const91 bool NotificationPreferencesInfo::BundleInfo::GetHasPoppedDialog() const
92 {
93     return hasPoppedDialog_;
94 }
95 
SetSlot(const sptr<NotificationSlot> & slot)96 void NotificationPreferencesInfo::BundleInfo::SetSlot(const sptr<NotificationSlot> &slot)
97 {
98     slots_.insert_or_assign(slot->GetType(), slot);
99 }
100 
GetSlot(const NotificationConstant::SlotType & type,sptr<NotificationSlot> & slot) const101 bool NotificationPreferencesInfo::BundleInfo::GetSlot(
102     const NotificationConstant::SlotType &type, sptr<NotificationSlot> &slot) const
103 {
104     auto iter = slots_.find(type);
105     if (iter != slots_.end()) {
106         slot = iter->second;
107         return true;
108     }
109     return false;
110 }
111 
GetSlotFlagsKeyFromType(const NotificationConstant::SlotType & type) const112 const char* NotificationPreferencesInfo::BundleInfo::GetSlotFlagsKeyFromType(
113     const NotificationConstant::SlotType &type) const
114 {
115     switch (type) {
116         case NotificationConstant::SlotType::SOCIAL_COMMUNICATION:
117             return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::SOCIAL_COMMUNICATION];
118         case NotificationConstant::SlotType::SERVICE_REMINDER:
119             return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::SERVICE_REMINDER];
120         case NotificationConstant::SlotType::CONTENT_INFORMATION:
121             return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::CONTENT_INFORMATION];
122         case NotificationConstant::SlotType::OTHER:
123             return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::OTHER];
124         case NotificationConstant::SlotType::CUSTOM:
125             return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::CUSTOM];
126         case NotificationConstant::SlotType::LIVE_VIEW:
127             return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::LIVE_VIEW];
128         case NotificationConstant::SlotType::CUSTOMER_SERVICE:
129             return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::CUSTOMER_SERVICE];
130         case NotificationConstant::SlotType::EMERGENCY_INFORMATION:
131             return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::EMERGENCY_INFORMATION];
132         default:
133             return nullptr;
134     }
135 }
136 
SetSlotFlagsForSlot(const NotificationConstant::SlotType & type)137 void NotificationPreferencesInfo::BundleInfo::SetSlotFlagsForSlot(
138     const NotificationConstant::SlotType &type)
139 {
140     uint32_t bundleSlotFlags = GetSlotFlags();
141     std::string key = GetSlotFlagsKeyFromType(type);
142     std::map<std::string, uint32_t>& slotFlagsDefaultMap = AdvancedNotificationService::GetDefaultSlotConfig();
143     if (slotFlagsDefaultMap.find(key) == slotFlagsDefaultMap.end()) {
144         return;
145     }
146     uint32_t finalSlotFlags = bundleSlotFlags&slotFlagsDefaultMap[key];
147     if (slotFlagsMap_.find(key) == slotFlagsMap_.end()) {
148         slotFlagsMap_.insert_or_assign(key, finalSlotFlags);
149     } else {
150         for (auto it = slotFlagsMap_.begin(); it != slotFlagsMap_.end(); ++it) {
151             if (it->first.compare(key) == 0 && it->second != finalSlotFlags) {
152                     it->second = finalSlotFlags;
153                 }
154         }
155     }
156 }
157 
GetSlotFlagsForSlot(const NotificationConstant::SlotType & type) const158 uint32_t NotificationPreferencesInfo::BundleInfo::GetSlotFlagsForSlot(const NotificationConstant::SlotType &type) const
159 {
160     std::string key = GetSlotFlagsKeyFromType(type);
161     auto it = slotFlagsMap_.find(key);
162     if (it != slotFlagsMap_.end()) {
163         return it->second;
164     } else {
165         return 0;
166     }
167 }
168 
GetAllSlots(std::vector<sptr<NotificationSlot>> & slots)169 bool NotificationPreferencesInfo::BundleInfo::GetAllSlots(std::vector<sptr<NotificationSlot>> &slots)
170 {
171     slots.clear();
172     std::for_each(slots_.begin(),
173         slots_.end(),
174         [&slots](std::map<NotificationConstant::SlotType, sptr<NotificationSlot>>::reference iter) {
175             slots.emplace_back(iter.second);
176         });
177     return true;
178 }
179 
GetAllSlotsSize()180 uint32_t NotificationPreferencesInfo::BundleInfo::GetAllSlotsSize()
181 {
182     return slots_.size();
183 }
184 
IsExsitSlot(const NotificationConstant::SlotType & type) const185 bool NotificationPreferencesInfo::BundleInfo::IsExsitSlot(const NotificationConstant::SlotType &type) const
186 {
187     auto iter = slots_.find(type);
188     return (iter != slots_.end());
189 }
190 
RemoveSlot(const NotificationConstant::SlotType & type)191 bool NotificationPreferencesInfo::BundleInfo::RemoveSlot(const NotificationConstant::SlotType &type)
192 {
193     auto iter = slots_.find(type);
194     if (iter != slots_.end()) {
195         slots_.erase(iter);
196         return true;
197     }
198     return false;
199 }
200 
GetSlotFlags()201 uint32_t NotificationPreferencesInfo::BundleInfo::GetSlotFlags()
202 {
203     return slotFlags_;
204 }
205 
SetSlotFlags(uint32_t slotFlags)206 void NotificationPreferencesInfo::BundleInfo::SetSlotFlags(uint32_t slotFlags)
207 {
208     slotFlags_ = slotFlags;
209 }
210 
RemoveAllSlots()211 void NotificationPreferencesInfo::BundleInfo::RemoveAllSlots()
212 {
213     slots_.clear();
214 }
215 
SetBundleUid(const int32_t & uid)216 void NotificationPreferencesInfo::BundleInfo::SetBundleUid(const int32_t &uid)
217 {
218     uid_ = uid;
219 }
220 
GetBundleUid() const221 int32_t NotificationPreferencesInfo::BundleInfo::GetBundleUid() const
222 {
223     return uid_;
224 }
225 
SetBundleInfo(BundleInfo & info)226 void NotificationPreferencesInfo::SetBundleInfo(BundleInfo &info)
227 {
228     std::string bundleKey = info.GetBundleName().append(std::to_string(info.GetBundleUid()));
229     infos_.insert_or_assign(bundleKey, info);
230 }
231 
GetBundleInfo(const sptr<NotificationBundleOption> & bundleOption,BundleInfo & info) const232 bool NotificationPreferencesInfo::GetBundleInfo(
233     const sptr<NotificationBundleOption> &bundleOption, BundleInfo &info) const
234 {
235     std::string bundleKey = bundleOption->GetBundleName() + std::to_string(bundleOption->GetUid());
236     auto iter = infos_.find(bundleKey);
237     if (iter != infos_.end()) {
238         info = iter->second;
239         return true;
240     }
241     return false;
242 }
243 
RemoveBundleInfo(const sptr<NotificationBundleOption> & bundleOption)244 bool NotificationPreferencesInfo::RemoveBundleInfo(const sptr<NotificationBundleOption> &bundleOption)
245 {
246     std::string bundleKey = bundleOption->GetBundleName() + std::to_string(bundleOption->GetUid());
247     auto iter = infos_.find(bundleKey);
248     if (iter != infos_.end()) {
249         infos_.erase(iter);
250         return true;
251     }
252     return false;
253 }
254 
IsExsitBundleInfo(const sptr<NotificationBundleOption> & bundleOption) const255 bool NotificationPreferencesInfo::IsExsitBundleInfo(const sptr<NotificationBundleOption> &bundleOption) const
256 {
257     std::string bundleKey = bundleOption->GetBundleName() + std::to_string(bundleOption->GetUid());
258     auto iter = infos_.find(bundleKey);
259     if (iter != infos_.end()) {
260         return true;
261     }
262     return false;
263 }
264 
ClearBundleInfo()265 void NotificationPreferencesInfo::ClearBundleInfo()
266 {
267     infos_.clear();
268 }
269 
SetDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> & doNotDisturbDate)270 void NotificationPreferencesInfo::SetDoNotDisturbDate(const int32_t &userId,
271     const sptr<NotificationDoNotDisturbDate> &doNotDisturbDate)
272 {
273     doNotDisturbDate_.insert_or_assign(userId, doNotDisturbDate);
274 }
275 
MakeDoNotDisturbProfileKey(int32_t userId,int32_t profileId)276 std::string NotificationPreferencesInfo::MakeDoNotDisturbProfileKey(int32_t userId, int32_t profileId)
277 {
278     return std::to_string(userId).append(KEY_UNDER_LINE).append(std::to_string(profileId));
279 }
280 
AddDoNotDisturbProfiles(int32_t userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)281 void NotificationPreferencesInfo::AddDoNotDisturbProfiles(
282     int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
283 {
284     for (auto profile : profiles) {
285         if (profile == nullptr) {
286             ANS_LOGE("The profile is nullptr.");
287             continue;
288         }
289         std::string key = MakeDoNotDisturbProfileKey(userId, profile->GetProfileId());
290         ANS_LOGI("AddDoNotDisturbProfiles key: %{public}s.", key.c_str());
291         doNotDisturbProfiles_.insert_or_assign(key, profile);
292     }
293 }
294 
RemoveDoNotDisturbProfiles(int32_t userId,const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)295 void NotificationPreferencesInfo::RemoveDoNotDisturbProfiles(
296     int32_t userId, const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
297 {
298     for (auto profile : profiles) {
299         if (profile == nullptr) {
300             ANS_LOGE("The profile is nullptr.");
301             continue;
302         }
303         std::string key = MakeDoNotDisturbProfileKey(userId, profile->GetProfileId());
304         ANS_LOGI("RemoveDoNotDisturbProfiles  key: %{public}s.", key.c_str());
305         doNotDisturbProfiles_.erase(key);
306     }
307 }
308 
GetDoNotDisturbProfiles(int32_t profileId,int32_t userId,sptr<NotificationDoNotDisturbProfile> & profile)309 bool NotificationPreferencesInfo::GetDoNotDisturbProfiles(
310     int32_t profileId, int32_t userId, sptr<NotificationDoNotDisturbProfile> &profile)
311 {
312     if (profile == nullptr) {
313         ANS_LOGE("The profile is nullptr.");
314         return false;
315     }
316     std::string key = MakeDoNotDisturbProfileKey(userId, profileId);
317     auto iter = doNotDisturbProfiles_.find(key);
318     if (iter != doNotDisturbProfiles_.end()) {
319         profile = iter->second;
320         return true;
321     }
322     return false;
323 }
324 
GetAllDoNotDisturbProfiles(int32_t userId,std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)325 void NotificationPreferencesInfo::GetAllDoNotDisturbProfiles(
326     int32_t userId, std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
327 {
328     for (const auto &doNotDisturbProfile : doNotDisturbProfiles_) {
329         std::string key = doNotDisturbProfile.first;
330         ANS_LOGI("GetAllDoNotDisturbProfiles key: %{public}s.", key.c_str());
331         auto result = key.find(std::to_string(userId));
332         if (result != std::string::npos) {
333             auto profile = doNotDisturbProfile.second;
334             profiles.emplace_back(profile);
335         }
336     }
337 }
338 
GetAllCLoneBundlesInfo(const int32_t & userId,const std::unordered_map<std::string,std::string> & bunlesMap,std::vector<NotificationCloneBundleInfo> & cloneBundles)339 void NotificationPreferencesInfo::GetAllCLoneBundlesInfo(const int32_t &userId,
340     const std::unordered_map<std::string, std::string> &bunlesMap,
341     std::vector<NotificationCloneBundleInfo> &cloneBundles)
342 {
343     for (const auto& bundleItem : bunlesMap) {
344         auto iter = infos_.find(bundleItem.second);
345         if (iter == infos_.end()) {
346             ANS_LOGI("No finde bundle info %{public}s.", bundleItem.second.c_str());
347             continue;
348         }
349 
350         std::vector<sptr<NotificationSlot>> slots;
351         NotificationCloneBundleInfo cloneBundleInfo;
352         int32_t index = BundleManagerHelper::GetInstance()->GetAppIndexByUid(iter->second.GetBundleUid());
353         cloneBundleInfo.SetBundleName(iter->second.GetBundleName());
354         cloneBundleInfo.SetAppIndex(index);
355         cloneBundleInfo.SetSlotFlags(iter->second.GetSlotFlags());
356         cloneBundleInfo.SetIsShowBadge(iter->second.GetIsShowBadge());
357         cloneBundleInfo.SetEnableNotification(iter->second.GetEnableNotification());
358         iter->second.GetAllSlots(slots);
359         for (auto& slot : slots) {
360             NotificationCloneBundleInfo::SlotInfo slotInfo;
361             slotInfo.slotType_ = slot->GetType();
362             slotInfo.enable_ = slot->GetEnable();
363             slotInfo.isForceControl_ = slot->GetForceControl();
364             cloneBundleInfo.AddSlotInfo(slotInfo);
365         }
366         cloneBundles.emplace_back(cloneBundleInfo);
367     }
368     ANS_LOGI("GetAllCLoneBundlesInfo size: %{public}zu.", cloneBundles.size());
369 }
370 
GetDoNotDisturbDate(const int32_t & userId,sptr<NotificationDoNotDisturbDate> & doNotDisturbDate) const371 bool NotificationPreferencesInfo::GetDoNotDisturbDate(const int32_t &userId,
372     sptr<NotificationDoNotDisturbDate> &doNotDisturbDate) const
373 {
374     auto iter = doNotDisturbDate_.find(userId);
375     if (iter != doNotDisturbDate_.end()) {
376         doNotDisturbDate = iter->second;
377         return true;
378     }
379     return false;
380 }
381 
SetEnabledAllNotification(const int32_t & userId,const bool & enable)382 void NotificationPreferencesInfo::SetEnabledAllNotification(const int32_t &userId, const bool &enable)
383 {
384     isEnabledAllNotification_.insert_or_assign(userId, enable);
385 }
386 
GetEnabledAllNotification(const int32_t & userId,bool & enable) const387 bool NotificationPreferencesInfo::GetEnabledAllNotification(const int32_t &userId, bool &enable) const
388 {
389     auto iter = isEnabledAllNotification_.find(userId);
390     if (iter != isEnabledAllNotification_.end()) {
391         enable = iter->second;
392         return true;
393     }
394     return false;
395 }
396 
RemoveNotificationEnable(const int32_t userId)397 void NotificationPreferencesInfo::RemoveNotificationEnable(const int32_t userId)
398 {
399     isEnabledAllNotification_.erase(userId);
400 }
401 
RemoveDoNotDisturbDate(const int32_t userId)402 void NotificationPreferencesInfo::RemoveDoNotDisturbDate(const int32_t userId)
403 {
404     doNotDisturbDate_.erase(userId);
405 }
406 
SetBundleInfoFromDb(BundleInfo & info,std::string bundleKey)407 void NotificationPreferencesInfo::SetBundleInfoFromDb(BundleInfo &info, std::string bundleKey)
408 {
409     infos_.insert_or_assign(bundleKey, info);
410 }
411 }  // namespace Notification
412 }  // namespace OHOS
413