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