• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
17 #include "smart_reminder_center.h"
18 
19 #include "ans_log_wrapper.h"
20 #include "ipc_skeleton.h"
21 #include "notification_bundle_option.h"
22 #include "notification_local_live_view_content.h"
23 #include "notification_preferences.h"
24 #include "os_account_manager.h"
25 #include "screenlock_manager.h"
26 #include "string_utils.h"
27 
28 namespace OHOS {
29 namespace Notification {
30 using namespace std;
31 namespace {
32     const std::string ANS_VOIP = "ANS_VOIP";
33 }
34 constexpr int32_t CONTROL_BY_SMART_REMINDER = 1 << 15;
SmartReminderCenter()35 SmartReminderCenter::SmartReminderCenter()
36 {
37     if (!DelayedSingleton<NotificationConfigParse>::GetInstance()->GetCurrentSlotReminder(currentReminderMethods_)) {
38         return;
39     }
40     GetMultiDeviceReminder();
41 }
42 
GetMultiDeviceReminder()43 void SmartReminderCenter::GetMultiDeviceReminder()
44 {
45     nlohmann::json root;
46     string multiJsonPoint = "/";
47     multiJsonPoint.append(NotificationConfigParse::CFG_KEY_NOTIFICATION_SERVICE);
48     multiJsonPoint.append("/");
49     multiJsonPoint.append(MULTI_DEVICE_REMINDER);
50     if (!DelayedSingleton<NotificationConfigParse>::GetInstance()->GetConfigJson(multiJsonPoint, root)) {
51         ANS_LOGW("Failed to get multiDeviceReminder CCM config file.");
52         return;
53     }
54 
55     if (root.find(NotificationConfigParse::CFG_KEY_NOTIFICATION_SERVICE) == root.end()) {
56         ANS_LOGW("GetMultiDeviceReminder failed as can not find notificationService.");
57         return;
58     }
59 
60     nlohmann::json multiDeviceRemindJson =
61         root[NotificationConfigParse::CFG_KEY_NOTIFICATION_SERVICE][MULTI_DEVICE_REMINDER];
62     if (multiDeviceRemindJson.is_null() || !multiDeviceRemindJson.is_array() || multiDeviceRemindJson.empty()) {
63         ANS_LOGW("GetMultiDeviceReminder failed as invalid multiDeviceReminder json.");
64         return;
65     }
66 
67     reminderMethods_.clear();
68     for (auto &singleDeviceRemindJson : multiDeviceRemindJson) {
69         if (singleDeviceRemindJson.is_null() || !singleDeviceRemindJson.is_object()) {
70             continue;
71         }
72 
73         if (singleDeviceRemindJson.find(ReminderAffected::DEVICE_TYPE) == singleDeviceRemindJson.end() ||
74             singleDeviceRemindJson[ReminderAffected::DEVICE_TYPE].is_null() ||
75             !singleDeviceRemindJson[ReminderAffected::DEVICE_TYPE].is_string()) {
76             continue;
77         }
78 
79         if (singleDeviceRemindJson.find(REMINDER_FILTER_DEVICE) == singleDeviceRemindJson.end() ||
80             singleDeviceRemindJson[REMINDER_FILTER_DEVICE].is_null() ||
81             !singleDeviceRemindJson[REMINDER_FILTER_DEVICE].is_array() ||
82             singleDeviceRemindJson[REMINDER_FILTER_DEVICE].empty()) {
83             continue;
84         }
85         ParseReminderFilterDevice(singleDeviceRemindJson[REMINDER_FILTER_DEVICE],
86             singleDeviceRemindJson[ReminderAffected::DEVICE_TYPE].get<string>());
87     }
88 
89     if (reminderMethods_.size() <= 0) {
90         ANS_LOGW("GetMultiDeviceReminder failed as Invalid reminderMethods size.");
91     }
92 }
93 
ParseReminderFilterDevice(const nlohmann::json & root,const string & deviceType)94 void SmartReminderCenter::ParseReminderFilterDevice(const nlohmann::json &root, const string &deviceType)
95 {
96     map<string, vector<shared_ptr<ReminderAffected>>> reminderFilterDevice;
97     for (auto &reminderFilterDeviceJson : root) {
98         NotificationConstant::SlotType slotType;
99         if (reminderFilterDeviceJson.find(SLOT_TYPE) == reminderFilterDeviceJson.end() ||
100             reminderFilterDeviceJson[SLOT_TYPE].is_null() ||
101             !reminderFilterDeviceJson[SLOT_TYPE].is_string()) {
102             continue;
103         }
104 
105         if (reminderFilterDeviceJson.find(REMINDER_FILTER_SLOT) == reminderFilterDeviceJson.end() ||
106             reminderFilterDeviceJson[REMINDER_FILTER_SLOT].is_null() ||
107             !reminderFilterDeviceJson[REMINDER_FILTER_SLOT].is_array() ||
108             reminderFilterDeviceJson[REMINDER_FILTER_SLOT].empty()) {
109             continue;
110         }
111 
112         std::string slotTypes = reminderFilterDeviceJson[SLOT_TYPE].get<std::string>();
113         std::vector<std::string> slotTypeVector;
114         StringUtils::Split(slotTypes, SPLIT_FLAG, slotTypeVector);
115 
116         for (std::string slotTypeStr : slotTypeVector) {
117             if (!NotificationSlot::GetSlotTypeByString(slotTypeStr, slotType)) {
118                 continue;
119             }
120             ParseReminderFilterSlot(reminderFilterDeviceJson[REMINDER_FILTER_SLOT],
121                 to_string(static_cast<int32_t>(slotType)), reminderFilterDevice);
122         }
123     }
124     if (reminderFilterDevice.size() > 0) {
125         reminderMethods_[deviceType] = move(reminderFilterDevice);
126     } else {
127         ANS_LOGI("ParseReminderFilterDevice failed as Invalid reminderFilterDevice size. deviceType = %{public}s.",
128             deviceType.c_str());
129     }
130 }
131 
ParseReminderFilterSlot(const nlohmann::json & root,const string & notificationType,map<string,vector<shared_ptr<ReminderAffected>>> & reminderFilterDevice) const132 void SmartReminderCenter::ParseReminderFilterSlot(
133     const nlohmann::json &root,
134     const string &notificationType,
135     map<string, vector<shared_ptr<ReminderAffected>>> &reminderFilterDevice) const
136 {
137     vector<shared_ptr<ReminderAffected>> reminderFilterSlot;
138     for (auto &reminderFilterSlotJson : root) {
139         NotificationContent::Type contentType;
140         bool validContentType = true;
141 
142         if (reminderFilterSlotJson.find(CONTENT_TYPE) == reminderFilterSlotJson.end() ||
143             reminderFilterSlotJson[CONTENT_TYPE].is_null() ||
144             !reminderFilterSlotJson[CONTENT_TYPE].is_string() ||
145             !NotificationContent::GetContentTypeByString(
146                 reminderFilterSlotJson[CONTENT_TYPE].get<std::string>(), contentType)) {
147             validContentType = false;
148         }
149 
150         if (reminderFilterSlotJson.find(REMINDER_FILTER_CONTENT) == reminderFilterSlotJson.end() ||
151             reminderFilterSlotJson[REMINDER_FILTER_CONTENT].is_null() ||
152             !reminderFilterSlotJson[REMINDER_FILTER_CONTENT].is_array() ||
153             reminderFilterSlotJson[REMINDER_FILTER_CONTENT].empty()) {
154             validContentType = false;
155         }
156 
157         if (validContentType) {
158             string localNotificationType = notificationType;
159             localNotificationType.append("#");
160             localNotificationType.append(to_string(static_cast<int32_t>(contentType)));
161             ParseReminderFilterContent(
162                 reminderFilterSlotJson[REMINDER_FILTER_CONTENT], localNotificationType, reminderFilterDevice);
163             continue;
164         }
165         shared_ptr<ReminderAffected> reminderAffected = make_shared<ReminderAffected>();
166         if (reminderAffected->FromJson(reminderFilterSlotJson)) {
167             reminderFilterSlot.push_back(reminderAffected);
168         }
169     }
170     if (reminderFilterSlot.size() > 0) {
171         reminderFilterDevice[notificationType] = move(reminderFilterSlot);
172     }
173 }
174 
ParseReminderFilterContent(const nlohmann::json & root,const string & notificationType,map<string,vector<shared_ptr<ReminderAffected>>> & reminderFilterDevice) const175 void SmartReminderCenter::ParseReminderFilterContent(
176     const nlohmann::json &root,
177     const string &notificationType,
178     map<string, vector<shared_ptr<ReminderAffected>>> &reminderFilterDevice) const
179 {
180     vector<shared_ptr<ReminderAffected>> reminderFilterContent;
181     for (auto &reminderFilterContentJson : root) {
182         bool validTypeCode = true;
183         if (reminderFilterContentJson.find(TYPE_CODE) == reminderFilterContentJson.end() ||
184             reminderFilterContentJson[TYPE_CODE].is_null() ||
185             !reminderFilterContentJson[TYPE_CODE].is_number()) {
186             validTypeCode = false;
187         }
188 
189         if (reminderFilterContentJson.find(REMINDER_FILTER_CODE) == reminderFilterContentJson.end() ||
190             reminderFilterContentJson[REMINDER_FILTER_CODE].is_null() ||
191             !reminderFilterContentJson[REMINDER_FILTER_CODE].is_array() ||
192             reminderFilterContentJson[REMINDER_FILTER_CODE].empty()) {
193             validTypeCode = false;
194         }
195 
196         if (validTypeCode) {
197             int32_t typeCode = reminderFilterContentJson[TYPE_CODE].get<int32_t>();
198             string localNotificationType = notificationType;
199             localNotificationType.append("#");
200             localNotificationType.append(to_string(typeCode));
201             ParseReminderFilterCode(
202                 reminderFilterContentJson[REMINDER_FILTER_CODE], localNotificationType, reminderFilterDevice);
203             continue;
204         }
205         shared_ptr<ReminderAffected> reminderAffected = make_shared<ReminderAffected>();
206         if (reminderAffected->FromJson(reminderFilterContentJson)) {
207             reminderFilterContent.push_back(reminderAffected);
208         }
209     }
210     if (reminderFilterContent.size() > 0) {
211         reminderFilterDevice[notificationType] = move(reminderFilterContent);
212     }
213 }
214 
ParseReminderFilterCode(const nlohmann::json & root,const string & notificationType,map<string,vector<shared_ptr<ReminderAffected>>> & reminderFilterDevice) const215 void SmartReminderCenter::ParseReminderFilterCode(
216     const nlohmann::json &root,
217     const string &notificationType,
218     map<string, vector<shared_ptr<ReminderAffected>>> &reminderFilterDevice) const
219 {
220     vector<shared_ptr<ReminderAffected>> reminderFilterCode;
221     for (auto &reminderFilterCodeJson : root) {
222         shared_ptr<ReminderAffected> reminderAffected = make_shared<ReminderAffected>();
223         if (reminderAffected->FromJson(reminderFilterCodeJson)) {
224             reminderFilterCode.push_back(reminderAffected);
225         }
226     }
227     if (reminderFilterCode.size() > 0) {
228         reminderFilterDevice[notificationType] = move(reminderFilterCode);
229     }
230 }
231 
ReminderDecisionProcess(const sptr<NotificationRequest> & request) const232 void SmartReminderCenter::ReminderDecisionProcess(const sptr<NotificationRequest> &request) const
233 {
234     shared_ptr<map<string, shared_ptr<NotificationFlags>>> notificationFlagsOfDevices =
235         make_shared<map<string, shared_ptr<NotificationFlags>>>();
236     NotificationConstant::SlotType slotType = request->GetSlotType();
237     auto iter = currentReminderMethods_.find(slotType);
238     if (iter != currentReminderMethods_.end()) {
239         // Only config file can set reminder open now. Otherwise, change iter->second to 11111
240         (*notificationFlagsOfDevices)[NotificationConstant::CURRENT_DEVICE_TYPE] = iter->second;
241     }
242 
243     map<string, vector<shared_ptr<ReminderAffected>>> currentReminderMethod;
244     set<string> validDevices;
245     for (auto &reminderMethod : reminderMethods_) {
246         if (reminderMethod.first.compare(NotificationConstant::CURRENT_DEVICE_TYPE) == 0) {
247             currentReminderMethod = reminderMethod.second;
248             continue;
249         }
250         HandleReminderMethods(
251             reminderMethod.first, reminderMethod.second, request, validDevices, notificationFlagsOfDevices);
252     }
253     if (currentReminderMethod.size() > 0 && validDevices.size() > 0) {
254         HandleReminderMethods(NotificationConstant::CURRENT_DEVICE_TYPE,
255             currentReminderMethod, request, validDevices, notificationFlagsOfDevices);
256     }
257 
258     request->SetDeviceFlags(notificationFlagsOfDevices);
259 }
260 
HandleReminderMethods(const string & deviceType,const map<string,vector<shared_ptr<ReminderAffected>>> & reminderFilterDevice,const sptr<NotificationRequest> & request,set<string> & validDevices,shared_ptr<map<string,shared_ptr<NotificationFlags>>> notificationFlagsOfDevices) const261 void SmartReminderCenter::HandleReminderMethods(
262     const string &deviceType,
263     const map<string, vector<shared_ptr<ReminderAffected>>> &reminderFilterDevice,
264     const sptr<NotificationRequest> &request,
265     set<string> &validDevices,
266     shared_ptr<map<string, shared_ptr<NotificationFlags>>> notificationFlagsOfDevices) const
267 {
268     if (deviceType.compare(NotificationConstant::CURRENT_DEVICE_TYPE) == 0 &&
269         (request->GetClassification() == ANS_VOIP)) {
270         ANS_LOGI("VOIP or CALL is not affected with SmartReminder");
271         return;
272     }
273     vector<shared_ptr<ReminderAffected>> reminderAffecteds;
274     GetReminderAffecteds(reminderFilterDevice, request, reminderAffecteds);
275     if (reminderAffecteds.size() <= 0) {
276         return;
277     }
278     bitset<DistributedDeviceStatus::STATUS_SIZE> bitStatus;
279     GetDeviceStatusByType(deviceType, bitStatus);
280     request->AdddeviceStatu(deviceType, bitStatus.bitset<DistributedDeviceStatus::STATUS_SIZE>::to_string());
281     bool enabledAffectedBy = true;
282     auto notificationControlFlags = request->GetNotificationControlFlags();
283     if (deviceType.compare(NotificationConstant::CURRENT_DEVICE_TYPE) != 0) {
284         if (IsNeedSynergy(deviceType, request->GetOwnerBundleName(), request->GetOwnerUid())) {
285             validDevices.insert(deviceType);
286             request->SetNotificationControlFlags(notificationControlFlags | CONTROL_BY_SMART_REMINDER);
287         } else {
288             enabledAffectedBy = false;
289         }
290     }
291     if (!NotificationSubscriberManager::GetInstance()->GetIsEnableEffectedRemind()) {
292         enabledAffectedBy = false;
293     }
294 
295     for (auto &reminderAffected : reminderAffecteds) {
296         if (!CompareStatus(reminderAffected->status_, bitStatus)) {
297             continue;
298         }
299         if (reminderAffected->affectedBy_.size() <= 0) {
300             (*notificationFlagsOfDevices)[deviceType] = reminderAffected->reminderFlags_;
301             continue;
302         }
303         if (enabledAffectedBy &&
304             HandleAffectedReminder(deviceType, reminderAffected, validDevices, notificationFlagsOfDevices)) {
305             break;
306         }
307     }
308 }
309 
IsNeedSynergy(const string & deviceType,const string & ownerBundleName,int32_t ownerUid) const310 bool SmartReminderCenter::IsNeedSynergy(const string &deviceType, const string &ownerBundleName, int32_t ownerUid) const
311 {
312     bool isEnable = true;
313     if (NotificationPreferences::GetInstance()->IsSmartReminderEnabled(deviceType, isEnable) != ERR_OK || !isEnable) {
314         return false;
315     }
316 
317     sptr<NotificationBundleOption> bundleOption =
318         new (std::nothrow) NotificationBundleOption(ownerBundleName, ownerUid);
319     if (NotificationPreferences::GetInstance()->IsDistributedEnabledByBundle(
320         bundleOption, deviceType, isEnable) != ERR_OK || !isEnable) {
321         return false;
322     }
323     return true;
324 }
325 
HandleAffectedReminder(const string & deviceType,const shared_ptr<ReminderAffected> & reminderAffected,const set<string> & validDevices,shared_ptr<map<string,shared_ptr<NotificationFlags>>> notificationFlagsOfDevices) const326 bool SmartReminderCenter::HandleAffectedReminder(
327     const string &deviceType,
328     const shared_ptr<ReminderAffected> &reminderAffected,
329     const set<string> &validDevices,
330     shared_ptr<map<string, shared_ptr<NotificationFlags>>> notificationFlagsOfDevices) const
331 {
332     bool ret = true;
333     for (auto &affectedBy : reminderAffected->affectedBy_) {
334         if (deviceType.compare(NotificationConstant::CURRENT_DEVICE_TYPE) == 0 &&
335             validDevices.find(affectedBy.first) == validDevices.end()) {
336             ret = false;
337             break;
338         }
339         bitset<DistributedDeviceStatus::STATUS_SIZE> bitStatus;
340         GetDeviceStatusByType(affectedBy.first, bitStatus);
341         if (!CompareStatus(affectedBy.second, bitStatus)) {
342             ret = false;
343             break;
344         }
345     }
346     if (ret) {
347         (*notificationFlagsOfDevices)[deviceType] = reminderAffected->reminderFlags_;
348     }
349     return ret;
350 }
351 
CompareStatus(const string & status,const bitset<DistributedDeviceStatus::STATUS_SIZE> & bitStatus) const352 bool SmartReminderCenter::CompareStatus(
353     const string &status, const bitset<DistributedDeviceStatus::STATUS_SIZE> &bitStatus) const
354 {
355     if (status.size() <= 0) {
356         return true;
357     }
358     // bitset.to_string() and config is reverse, bit[0] is behind
359     string localStatus = status;
360     reverse(localStatus.begin(), localStatus.end());
361     for (int32_t seq = 0; seq < DistributedDeviceStatus::STATUS_SIZE; seq++) {
362         if (localStatus[seq] != ReminderAffected::STATUS_DEFAULT && bitStatus[seq] != localStatus[seq] - '0') {
363             return false;
364         }
365     }
366     return true;
367 }
368 
GetReminderAffecteds(const map<string,vector<shared_ptr<ReminderAffected>>> & reminderFilterDevice,const sptr<NotificationRequest> & request,vector<shared_ptr<ReminderAffected>> & reminderAffecteds) const369 __attribute__((no_sanitize("cfi"))) void SmartReminderCenter::GetReminderAffecteds(
370     const map<string, vector<shared_ptr<ReminderAffected>>> &reminderFilterDevice,
371     const sptr<NotificationRequest> &request,
372     vector<shared_ptr<ReminderAffected>> &reminderAffecteds) const
373 {
374     string strSlotType = to_string(static_cast<int32_t>(request->GetSlotType()));
375     string contentTypeCombination = strSlotType;
376     contentTypeCombination.append("#");
377     if (request->GetContent() != nullptr) {
378         contentTypeCombination.append(to_string(static_cast<int32_t>(request->GetContent()->GetContentType())));
379     }
380     string typeCodeCombination = contentTypeCombination;
381     typeCodeCombination.append("#");
382     if (request->GetContent() != nullptr && request->GetContent()->GetNotificationContent() != nullptr) {
383         NotificationLocalLiveViewContent *localLiveView =
384             static_cast<NotificationLocalLiveViewContent *>(&(*(request->GetContent()->GetNotificationContent())));
385         typeCodeCombination.append(to_string(localLiveView->GetType()));
386     }
387     auto iter = reminderFilterDevice.find(typeCodeCombination);
388     if (iter != reminderFilterDevice.end()) {
389         reminderAffecteds = iter->second;
390         return;
391     }
392     iter = reminderFilterDevice.find(contentTypeCombination);
393     if (iter != reminderFilterDevice.end()) {
394         reminderAffecteds = iter->second;
395         return;
396     }
397     iter = reminderFilterDevice.find(strSlotType);
398     if (iter != reminderFilterDevice.end()) {
399         reminderAffecteds = iter->second;
400         return;
401     }
402     ANS_LOGD("GetReminderAffecteds fail as wrong notification_config.json possibly. TypeCombination = %{public}s.",
403         typeCodeCombination.c_str());
404 }
405 
GetDeviceStatusByType(const string & deviceType,bitset<DistributedDeviceStatus::STATUS_SIZE> & bitStatus) const406 void SmartReminderCenter::GetDeviceStatusByType(
407     const string &deviceType, bitset<DistributedDeviceStatus::STATUS_SIZE> &bitStatus) const
408 {
409     u_int32_t status = DelayedSingleton<DistributedDeviceStatus>::GetInstance()->GetDeviceStatus(deviceType);
410     bitStatus = bitset<DistributedDeviceStatus::STATUS_SIZE>(status);
411     if (deviceType.compare(NotificationConstant::CURRENT_DEVICE_TYPE) == 0) {
412         bool screenLocked = true;
413         screenLocked = ScreenLock::ScreenLockManager::GetInstance()->IsScreenLocked();
414         bitStatus.set(DistributedDeviceStatus::LOCK_FLAG, !screenLocked);
415     }
416     ANS_LOGD("GetDeviceStatusByType deviceType: %{public}s, bitStatus: %{public}s.",
417         deviceType.c_str(), bitStatus.to_string().c_str());
418 }
419 }  // namespace Notification
420 }  // namespace OHOS
421 #endif
422