1 /*
2 * Copyright (c) 2021-2022 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 #include "notification_constant.h"
17
18 namespace OHOS {
19 namespace Notification {
BundleInfo()20 NotificationPreferencesInfo::BundleInfo::BundleInfo()
21 {
22 }
23
~BundleInfo()24 NotificationPreferencesInfo::BundleInfo::~BundleInfo()
25 {
26 }
27
SetBundleName(const std::string & name)28 void NotificationPreferencesInfo::BundleInfo::SetBundleName(const std::string &name)
29 {
30 bundleName_ = name;
31 }
32
GetBundleName() const33 std::string NotificationPreferencesInfo::BundleInfo::GetBundleName() const
34 {
35 return bundleName_;
36 }
37
SetImportance(const int32_t & level)38 void NotificationPreferencesInfo::BundleInfo::SetImportance(const int32_t &level)
39 {
40 importance_ = level;
41 }
42
GetImportance() const43 int32_t NotificationPreferencesInfo::BundleInfo::GetImportance() const
44 {
45 return importance_;
46 }
47
SetIsShowBadge(const bool & isShowBadge)48 void NotificationPreferencesInfo::BundleInfo::SetIsShowBadge(const bool &isShowBadge)
49 {
50 isShowBadge_ = isShowBadge;
51 }
52
GetIsShowBadge() const53 bool NotificationPreferencesInfo::BundleInfo::GetIsShowBadge() const
54 {
55 return isShowBadge_;
56 }
57
SetBadgeTotalNum(const int32_t & num)58 void NotificationPreferencesInfo::BundleInfo::SetBadgeTotalNum(const int32_t &num)
59 {
60 badgeTotalNum_ = num;
61 }
62
GetBadgeTotalNum() const63 int32_t NotificationPreferencesInfo::BundleInfo::GetBadgeTotalNum() const
64 {
65 return badgeTotalNum_;
66 }
67
SetEnableNotification(const bool & enable)68 void NotificationPreferencesInfo::BundleInfo::SetEnableNotification(const bool &enable)
69 {
70 isEnabledNotification_ = enable;
71 }
72
GetEnableNotification() const73 bool NotificationPreferencesInfo::BundleInfo::GetEnableNotification() const
74 {
75 return isEnabledNotification_;
76 }
77
78
SetHasPoppedDialog(const bool & hasPopped)79 void NotificationPreferencesInfo::BundleInfo::SetHasPoppedDialog(const bool &hasPopped)
80 {
81 hasPoppedDialog_ = hasPopped;
82 }
83
GetHasPoppedDialog() const84 bool NotificationPreferencesInfo::BundleInfo::GetHasPoppedDialog() const
85 {
86 return hasPoppedDialog_;
87 }
88
SetSlot(const sptr<NotificationSlot> & slot)89 void NotificationPreferencesInfo::BundleInfo::SetSlot(const sptr<NotificationSlot> &slot)
90 {
91 slots_.insert_or_assign(slot->GetType(), slot);
92 }
93
GetSlot(const NotificationConstant::SlotType & type,sptr<NotificationSlot> & slot) const94 bool NotificationPreferencesInfo::BundleInfo::GetSlot(
95 const NotificationConstant::SlotType &type, sptr<NotificationSlot> &slot) const
96 {
97 auto iter = slots_.find(type);
98 if (iter != slots_.end()) {
99 slot = iter->second;
100 return true;
101 }
102 return false;
103 }
104
GetSlotFlagsKeyFromType(const NotificationConstant::SlotType & type) const105 const char* NotificationPreferencesInfo::BundleInfo::GetSlotFlagsKeyFromType(
106 const NotificationConstant::SlotType &type) const
107 {
108 switch (type) {
109 case NotificationConstant::SlotType::SOCIAL_COMMUNICATION:
110 return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::SOCIAL_COMMUNICATION];
111 case NotificationConstant::SlotType::SERVICE_REMINDER:
112 return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::SERVICE_REMINDER];
113 case NotificationConstant::SlotType::CONTENT_INFORMATION:
114 return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::CONTENT_INFORMATION];
115 case NotificationConstant::SlotType::OTHER:
116 return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::OTHER];
117 case NotificationConstant::SlotType::CUSTOM:
118 return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::CUSTOM];
119 case NotificationConstant::SlotType::LIVE_VIEW:
120 return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::LIVE_VIEW];
121 case NotificationConstant::SlotType::CUSTOMER_SERVICE:
122 return NotificationConstant::SLOTTYPECCMNAMES[NotificationConstant::SlotType::CUSTOMER_SERVICE];
123 default:
124 return nullptr;
125 }
126 }
127
SetSlotFlagsForSlot(const NotificationConstant::SlotType & type)128 void NotificationPreferencesInfo::BundleInfo::SetSlotFlagsForSlot(
129 const NotificationConstant::SlotType &type)
130 {
131 uint32_t bundleSlotFlags = GetSlotFlags();
132 std::string key = GetSlotFlagsKeyFromType(type);
133 std::map<std::string, uint32_t>& slotFlagsDefaultMap = AdvancedNotificationService::GetDefaultSlotConfig();
134 if (slotFlagsDefaultMap.find(key) == slotFlagsDefaultMap.end()) {
135 return;
136 }
137 uint32_t finalSlotFlags = bundleSlotFlags&slotFlagsDefaultMap[key];
138 if (slotFlagsMap_.find(key) == slotFlagsMap_.end()) {
139 slotFlagsMap_.insert_or_assign(key, finalSlotFlags);
140 } else {
141 for (auto it = slotFlagsMap_.begin(); it != slotFlagsMap_.end(); ++it) {
142 if (it->first.compare(key) == 0 && it->second != finalSlotFlags) {
143 it->second = finalSlotFlags;
144 }
145 }
146 }
147 }
148
GetSlotFlagsForSlot(const NotificationConstant::SlotType & type) const149 uint32_t NotificationPreferencesInfo::BundleInfo::GetSlotFlagsForSlot(const NotificationConstant::SlotType &type) const
150 {
151 std::string key = GetSlotFlagsKeyFromType(type);
152 auto it = slotFlagsMap_.find(key);
153 if (it != slotFlagsMap_.end()) {
154 return it->second;
155 } else {
156 return 0;
157 }
158 }
159
GetAllSlots(std::vector<sptr<NotificationSlot>> & slots)160 bool NotificationPreferencesInfo::BundleInfo::GetAllSlots(std::vector<sptr<NotificationSlot>> &slots)
161 {
162 slots.clear();
163 std::for_each(slots_.begin(),
164 slots_.end(),
165 [&slots](std::map<NotificationConstant::SlotType, sptr<NotificationSlot>>::reference iter) {
166 slots.emplace_back(iter.second);
167 });
168 return true;
169 }
170
GetAllSlotsSize()171 uint32_t NotificationPreferencesInfo::BundleInfo::GetAllSlotsSize()
172 {
173 return slots_.size();
174 }
175
IsExsitSlot(const NotificationConstant::SlotType & type) const176 bool NotificationPreferencesInfo::BundleInfo::IsExsitSlot(const NotificationConstant::SlotType &type) const
177 {
178 auto iter = slots_.find(type);
179 return (iter != slots_.end());
180 }
181
RemoveSlot(const NotificationConstant::SlotType & type)182 bool NotificationPreferencesInfo::BundleInfo::RemoveSlot(const NotificationConstant::SlotType &type)
183 {
184 auto iter = slots_.find(type);
185 if (iter != slots_.end()) {
186 slots_.erase(iter);
187 return true;
188 }
189 return false;
190 }
191
GetSlotFlags()192 uint32_t NotificationPreferencesInfo::BundleInfo::GetSlotFlags()
193 {
194 return slotFlags_;
195 }
196
SetSlotFlags(uint32_t slotFlags)197 void NotificationPreferencesInfo::BundleInfo::SetSlotFlags(uint32_t slotFlags)
198 {
199 slotFlags_ = slotFlags;
200 }
201
RemoveAllSlots()202 void NotificationPreferencesInfo::BundleInfo::RemoveAllSlots()
203 {
204 slots_.clear();
205 }
206
SetBundleUid(const int32_t & uid)207 void NotificationPreferencesInfo::BundleInfo::SetBundleUid(const int32_t &uid)
208 {
209 uid_ = uid;
210 }
211
GetBundleUid() const212 int32_t NotificationPreferencesInfo::BundleInfo::GetBundleUid() const
213 {
214 return uid_;
215 }
216
SetBundleInfo(const BundleInfo & info)217 void NotificationPreferencesInfo::SetBundleInfo(const BundleInfo &info)
218 {
219 std::string bundleKey = info.GetBundleName().append(std::to_string(info.GetBundleUid()));
220 infos_.insert_or_assign(bundleKey, info);
221 }
222
GetBundleInfo(const sptr<NotificationBundleOption> & bundleOption,BundleInfo & info) const223 bool NotificationPreferencesInfo::GetBundleInfo(
224 const sptr<NotificationBundleOption> &bundleOption, BundleInfo &info) const
225 {
226 std::string bundleKey = bundleOption->GetBundleName() + std::to_string(bundleOption->GetUid());
227 auto iter = infos_.find(bundleKey);
228 if (iter != infos_.end()) {
229 info = iter->second;
230 return true;
231 }
232 return false;
233 }
234
RemoveBundleInfo(const sptr<NotificationBundleOption> & bundleOption)235 bool NotificationPreferencesInfo::RemoveBundleInfo(const sptr<NotificationBundleOption> &bundleOption)
236 {
237 std::string bundleKey = bundleOption->GetBundleName() + std::to_string(bundleOption->GetUid());
238 auto iter = infos_.find(bundleKey);
239 if (iter != infos_.end()) {
240 infos_.erase(iter);
241 return true;
242 }
243 return false;
244 }
245
IsExsitBundleInfo(const sptr<NotificationBundleOption> & bundleOption) const246 bool NotificationPreferencesInfo::IsExsitBundleInfo(const sptr<NotificationBundleOption> &bundleOption) const
247 {
248 std::string bundleKey = bundleOption->GetBundleName() + std::to_string(bundleOption->GetUid());
249 auto iter = infos_.find(bundleKey);
250 if (iter != infos_.end()) {
251 return true;
252 }
253 return false;
254 }
255
ClearBundleInfo()256 void NotificationPreferencesInfo::ClearBundleInfo()
257 {
258 infos_.clear();
259 }
260
SetDoNotDisturbDate(const int32_t & userId,const sptr<NotificationDoNotDisturbDate> & doNotDisturbDate)261 void NotificationPreferencesInfo::SetDoNotDisturbDate(const int32_t &userId,
262 const sptr<NotificationDoNotDisturbDate> &doNotDisturbDate)
263 {
264 doNotDisturbDate_.insert_or_assign(userId, doNotDisturbDate);
265 }
266
GetDoNotDisturbDate(const int32_t & userId,sptr<NotificationDoNotDisturbDate> & doNotDisturbDate) const267 bool NotificationPreferencesInfo::GetDoNotDisturbDate(const int32_t &userId,
268 sptr<NotificationDoNotDisturbDate> &doNotDisturbDate) const
269 {
270 auto iter = doNotDisturbDate_.find(userId);
271 if (iter != doNotDisturbDate_.end()) {
272 doNotDisturbDate = iter->second;
273 return true;
274 }
275 return false;
276 }
277
SetEnabledAllNotification(const int32_t & userId,const bool & enable)278 void NotificationPreferencesInfo::SetEnabledAllNotification(const int32_t &userId, const bool &enable)
279 {
280 isEnabledAllNotification_.insert_or_assign(userId, enable);
281 }
282
GetEnabledAllNotification(const int32_t & userId,bool & enable) const283 bool NotificationPreferencesInfo::GetEnabledAllNotification(const int32_t &userId, bool &enable) const
284 {
285 auto iter = isEnabledAllNotification_.find(userId);
286 if (iter != isEnabledAllNotification_.end()) {
287 enable = iter->second;
288 return true;
289 }
290 return false;
291 }
292
RemoveNotificationEnable(const int32_t userId)293 void NotificationPreferencesInfo::RemoveNotificationEnable(const int32_t userId)
294 {
295 isEnabledAllNotification_.erase(userId);
296 }
297
RemoveDoNotDisturbDate(const int32_t userId)298 void NotificationPreferencesInfo::RemoveDoNotDisturbDate(const int32_t userId)
299 {
300 doNotDisturbDate_.erase(userId);
301 }
302
SetBundleInfoFromDb(const BundleInfo & info,std::string bundleKey)303 void NotificationPreferencesInfo::SetBundleInfoFromDb(const BundleInfo &info, std::string bundleKey)
304 {
305 infos_.insert_or_assign(bundleKey, info);
306 }
307 } // namespace Notification
308 } // namespace OHOS
309