From ac0aaaa65ae962ee19214fa90bfe729e485c2a08 Mon Sep 17 00:00:00 2001 From: wangsen1994 Date: Thu, 28 Nov 2024 16:57:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8C=81=E4=B9=85=E5=8C=96?= =?UTF-8?q?=E5=85=8B=E9=9A=86=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangsen1994 --- .../include/advanced_notification_service.h | 14 +- .../notification_clone_bundle_service.h | 4 +- .../notification_clone_disturb_service.h | 4 +- .../ans/include/notification_clone_manager.h | 2 + .../ans/include/notification_clone_template.h | 2 + .../ans/include/notification_preferences.h | 13 ++ .../notification_preferences_database.h | 15 ++ .../advanced_notification_publish_service.cpp | 2 +- .../ans/src/advanced_notification_utils.cpp | 127 +++++++++++++- .../notification_clone_bundle_service.cpp | 37 ++-- .../notification_clone_disturb_service.cpp | 40 ++--- .../src/clone/notification_clone_manager.cpp | 40 ++++- services/ans/src/notification_preferences.cpp | 79 ++++++++- .../src/notification_preferences_database.cpp | 163 ++++++++++++++++++ services/ans/src/system_event_observer.cpp | 3 + 15 files changed, 490 insertions(+), 55 deletions(-) diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 4e7890bf7..1d10526bc 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -48,6 +48,7 @@ #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED #include "reminder_swing_decision_center.h" #endif +#include "notification_clone_bundle_info.h" namespace OHOS { namespace Notification { @@ -561,7 +562,7 @@ public: */ ErrCode CanPopEnableNotificationDialog(const sptr &callback, bool &canPop, std::string &bundleName) override; - + /** * @brief remove enable notification dialog. * @@ -1163,6 +1164,8 @@ public: int32_t OnBackup(MessageParcel& data, MessageParcel& reply); int32_t OnRestore(MessageParcel& data, MessageParcel& reply); + + void UpdateCloneBundleInfo(const NotificationCloneBundleInfo cloneBundleInfo); protected: /** * @brief Query whether there is a agent relationship between the two apps. @@ -1382,7 +1385,7 @@ private: const std::vector extraInfoKeys, sptr &newRequest); ErrCode IsAllowedRemoveSlot(const sptr &bundleOption, const NotificationConstant::SlotType &slotType); - void HandleBadgeEnabledChanged(const sptr &bundleOption, bool &enabled); + void HandleBadgeEnabledChanged(const sptr &bundleOption, bool enabled); ErrCode CheckBundleOptionValid(sptr &bundleOption); bool IsNeedNotifyConsumed(const sptr &request); ErrCode AddRecordToMemory(const std::shared_ptr &record, @@ -1431,6 +1434,13 @@ private: bool IsSystemUser(int32_t userId); ErrCode UpdateFlowCtrl(const std::shared_ptr &record); ErrCode PublishFlowControlInner(const std::shared_ptr &record); + ErrCode SetEnabledForBundleSlotInner(const sptr &bundleOption, + const sptr &bundle, + const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl); + ErrCode AddSlotThenPublishEvent( + const sptr &slot, + const sptr &bundle, + bool enabled, bool isForceControl); private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/include/notification_clone_bundle_service.h b/services/ans/include/notification_clone_bundle_service.h index 7e30c4664..f1a6b6ab8 100644 --- a/services/ans/include/notification_clone_bundle_service.h +++ b/services/ans/include/notification_clone_bundle_service.h @@ -30,10 +30,8 @@ public: ~NotificationCloneBundle() override; static std::shared_ptr GetInstance(); ErrCode OnBackup(nlohmann::json &jsonObject) override; + void OnRestoreStart(const std::string bundleName, int32_t appIndex, int32_t userId, int32_t uid) override; void OnRestore(const nlohmann::json &jsonObject) override; - void OnBundleDataAdd(const sptr &bundleOption); - void OnBundleDataUpdate(const sptr &bundleOption); - void HandleBundleEvent(const std::string bundleName, int32_t appIndex, int32_t uid); void OnUserSwitch(int32_t userId) override; private: std::vector bundlesInfo_; diff --git a/services/ans/include/notification_clone_disturb_service.h b/services/ans/include/notification_clone_disturb_service.h index ab8a3939c..47dfa98a3 100644 --- a/services/ans/include/notification_clone_disturb_service.h +++ b/services/ans/include/notification_clone_disturb_service.h @@ -31,9 +31,7 @@ public: static std::shared_ptr GetInstance(); ErrCode OnBackup(nlohmann::json &jsonObject) override; void OnRestore(const nlohmann::json &jsonObject) override; - void OnBundleDataAdd(const sptr &bundleOption); - void OnBundleDataUpdate(const sptr &bundleOption); - void HandlerBundleEvent(const std::string bundleName, int32_t appIndex, int32_t uid); + void OnRestoreStart(const std::string bundleName, int32_t appIndex, int32_t userId, int32_t uid) override; int32_t GetBundleUid(const std::string bundleName, int32_t userId, int32_t appIndex); void GetProfileUid(int32_t userId, std::map& uidMap, std::vector trustList, std::vector& exitBunldleList, diff --git a/services/ans/include/notification_clone_manager.h b/services/ans/include/notification_clone_manager.h index 1328e8fd9..a93a9846f 100644 --- a/services/ans/include/notification_clone_manager.h +++ b/services/ans/include/notification_clone_manager.h @@ -21,6 +21,7 @@ #include "unique_fd.h" #include "iremote_proxy.h" +#include "common_event_data.h" #include "notification_clone_template.h" namespace OHOS { namespace Notification { @@ -32,6 +33,7 @@ public: int32_t OnBackup(MessageParcel& data, MessageParcel& reply); int32_t OnRestore(MessageParcel& data, MessageParcel& reply); void OnUserSwitch(int32_t userId); + void OnRestoreStart(EventFwk::Want want); private: NotificationCloneManager(); diff --git a/services/ans/include/notification_clone_template.h b/services/ans/include/notification_clone_template.h index b9baa7472..02a486acd 100644 --- a/services/ans/include/notification_clone_template.h +++ b/services/ans/include/notification_clone_template.h @@ -29,6 +29,8 @@ public: virtual ErrCode OnBackup(nlohmann::json &jsonObject) = 0; virtual void OnRestore(const nlohmann::json &jsonObject) = 0; virtual void OnUserSwitch(int32_t userId) = 0; + virtual void OnRestoreStart(const std::string bundleName, int32_t appIndex, + int32_t userId, int32_t uid) = 0; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 9665a5885..793a50030 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -386,6 +386,19 @@ public: void GetAllCLoneBundlesInfo(int32_t userId, std::vector &cloneBundles); void UpdateCloneBundleInfo(int32_t userId, const NotificationCloneBundleInfo& cloneBundleInfo); bool IsNotificationSlotFlagsExists(const sptr &bundleOption); + bool DelCloneProfileInfo(const int32_t &userId, const sptr& info); + bool UpdateBatchCloneProfileInfo(const int32_t &userId, + const std::vector>& profileInfo); + void GetAllCloneProfileInfo(const int32_t &userId, + std::vector>& profilesInfo); + void GetAllCloneBundleInfo(const int32_t &userId, std::vector& cloneBundleInfo); + bool UpdateBatchCloneBundleInfo(const int32_t &userId, + const std::vector& cloneBundleInfo); + bool DelCloneBundleInfo(const int32_t &userId, const NotificationCloneBundleInfo& cloneBundleInfo); + bool DelBatchCloneBundleInfo(const int32_t &userId, + const std::vector& cloneBundleInfo); + bool DelBatchCloneProfileInfo(const int32_t &userId, + const std::vector>& profileInfo); private: bool GetBundleInfo(NotificationPreferencesInfo &preferencesInfo, diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 00b49d035..b6c6eeca1 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -247,6 +247,19 @@ public: bool UpdateBundleSlotToDisturbeDB(int32_t userId, const std::string &bundleName, const int32_t &bundleUid, const std::vector> &slots); bool IsNotificationSlotFlagsExists(const sptr &bundleOption); + bool DelCloneProfileInfo(const int32_t &userId, const sptr& info); + bool UpdateBatchCloneProfileInfo(const int32_t &userId, + const std::vector>& profileInfo); + void GetAllCloneProfileInfo(const int32_t &userId, + std::vector>& profilesInfo); + void GetAllCloneBundleInfo(const int32_t &userId, std::vector& cloneBundleInfo); + bool UpdateBatchCloneBundleInfo(const int32_t &userId, + const std::vector& cloneBundleInfo); + bool DelCloneBundleInfo(const int32_t &userId, const NotificationCloneBundleInfo& cloneBundleInfo); + bool DelBatchCloneProfileInfo(const int32_t &userId, + const std::vector>& profileInfo); + bool DelBatchCloneBundleInfo(const int32_t &userId, + const std::vector& cloneBundleInfo); private: bool CheckRdbStore(); @@ -320,6 +333,8 @@ private: void ParseSlotAuthorizedStatus(sptr &slot, const std::string &value) const; void ParseSlotAuthHitnCnt(sptr &slot, const std::string &value) const; void ParseSlotReminderMode(sptr &slot, const std::string &value) const; + bool UpdateCloneToDisturbeDB(const int32_t &userId, + const std::unordered_map values); std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo) const; std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo, diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index 35e5ea9ca..cc0298c08 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -781,7 +781,7 @@ ErrCode AdvancedNotificationService::SetShowBadgeEnabledForBundle( } void AdvancedNotificationService::HandleBadgeEnabledChanged( - const sptr &bundleOption, bool &enabled) + const sptr &bundleOption, bool enabled) { sptr enabledData = new (std::nothrow) EnabledNotificationCallbackData(bundleOption->GetBundleName(), bundleOption->GetUid(), enabled); diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 22f669fc1..a71dbdb17 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -48,8 +48,6 @@ #include "advanced_notification_inline.cpp" #include "notification_analytics_util.h" -#include "notification_clone_disturb_service.h" -#include "notification_clone_bundle_service.h" #define CHECK_BUNDLE_OPTION_IS_INVALID(option) \ if (option == nullptr || option->GetBundleName().empty()) { \ @@ -590,10 +588,8 @@ void AdvancedNotificationService::OnBundleDataAdd(const sptrOnBundleDataAdd(bundleOption); }; - NotificationCloneDisturb::GetInstance()->OnBundleDataAdd(bundleOption); notificationSvrQueue_ != nullptr ? notificationSvrQueue_->submit(bundleInstall) : bundleInstall(); } @@ -621,7 +617,6 @@ void AdvancedNotificationService::OnBundleDataUpdate(const sptrOnBundleDataUpdate(bundleOption); notificationSvrQueue_ != nullptr ? notificationSvrQueue_->submit(bundleUpdate) : bundleUpdate(); } @@ -2082,5 +2077,127 @@ ErrCode AdvancedNotificationService::OnRecoverLiveView( BatchCancelTimer(timerIds); return ERR_OK; } + +ErrCode AdvancedNotificationService::SetEnabledForBundleSlotInner( + const sptr &bundleOption, + const sptr &bundle, + const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) +{ + sptr slot; + ErrCode result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundle, slotType, slot); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST || + result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + slot = new (std::nothrow) NotificationSlot(slotType); + if (slot == nullptr) { + ANS_LOGE("Failed to create NotificationSlot ptr."); + return ERR_ANS_NO_MEMORY; + } + GenerateSlotReminderMode(slot, bundleOption); + return AddSlotThenPublishEvent(slot, bundle, enabled, isForceControl); + } else if ((result == ERR_OK) && (slot != nullptr)) { + if (slot->GetEnable() == enabled && slot->GetForceControl() == isForceControl) { + slot->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED); + std::vector> slots; + slots.push_back(slot); + return NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots); + } + NotificationPreferences::GetInstance()->RemoveNotificationSlot(bundle, slotType); + return AddSlotThenPublishEvent(slot, bundle, enabled, isForceControl); + } + ANS_LOGE("Set enable slot: GetNotificationSlot failed"); + return result; +} + +ErrCode AdvancedNotificationService::AddSlotThenPublishEvent( + const sptr &slot, + const sptr &bundle, + bool enabled, bool isForceControl) +{ + bool allowed = false; + ErrCode result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundle, allowed); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + allowed = CheckApiCompatibility(bundle); + SetDefaultNotificationEnabled(bundle, allowed); + } + + slot->SetEnable(enabled); + slot->SetForceControl(isForceControl); + slot->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED); + std::vector> slots; + slots.push_back(slot); + result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots); + if (result != ERR_OK) { + ANS_LOGE("Set enable slot: AddNotificationSlot failed"); + return result; + } + + if (!slot->GetEnable()) { + RemoveNotificationBySlot(bundle, slot, NotificationConstant::DISABLE_SLOT_REASON_DELETE); + } else { + if (!slot->GetForceControl() && !allowed) { + RemoveNotificationBySlot(bundle, slot, NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE); + } + } + + PublishSlotChangeCommonEvent(bundle); + return result; +} + +void AdvancedNotificationService::UpdateCloneBundleInfo(const NotificationCloneBundleInfo cloneBundleInfo) +{ + ANS_LOGI("Event bundle update %{public}s.", cloneBundleInfo.Dump().c_str()); + if (notificationSvrQueue_ == nullptr) { + return; + } + + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&, cloneBundleInfo]() { + sptr bundle = new (std::nothrow) NotificationBundleOption( + cloneBundleInfo.GetBundleName(), cloneBundleInfo.GetUid()); + if (bundle == nullptr) { + return; + } + bundle->SetAppIndex(cloneBundleInfo.GetAppIndex()); + if (NotificationPreferences::GetInstance()->SetNotificationsEnabledForBundle(bundle, + cloneBundleInfo.GetEnableNotification()) == ERR_OK) { + SetSlotFlagsTrustlistsAsBundle(bundle); + sptr bundleData = new (std::nothrow) EnabledNotificationCallbackData( + bundle->GetBundleName(), bundle->GetUid(), cloneBundleInfo.GetEnableNotification()); + if (bundleData == nullptr) { + return; + } + NotificationSubscriberManager::GetInstance()->NotifyEnabledNotificationChanged(bundleData); + } else { + ANS_LOGW("Set notification enable failed."); + return; + } + + if (cloneBundleInfo.GetSlotInfo().empty()) { + PublishSlotChangeCommonEvent(bundle); + } + if (NotificationPreferences::GetInstance()->SetNotificationSlotFlagsForBundle(bundle, + cloneBundleInfo.GetSlotFlags()) != ERR_OK) { + ANS_LOGW("Set notification slot failed."); + return; + } + if (UpdateSlotReminderModeBySlotFlags(bundle, cloneBundleInfo.GetSlotFlags()) != ERR_OK) { + ANS_LOGW("Set notification reminder slot failed."); + return; + } + + if (NotificationPreferences::GetInstance()->SetShowBadge(bundle, cloneBundleInfo.GetIsShowBadge()) == ERR_OK) { + HandleBadgeEnabledChanged(bundle, cloneBundleInfo.GetIsShowBadge()); + } else { + ANS_LOGW("Set notification badge failed."); + } + + for (auto& cloneSlot : cloneBundleInfo.GetSlotInfo()) { + if (SetEnabledForBundleSlotInner(bundle, bundle, cloneSlot.slotType_, cloneSlot.enable_, + cloneSlot.isForceControl_) != ERR_OK) { + ANS_LOGW("Set notification slots failed %{public}s.", cloneSlot.Dump().c_str()); + } + } + })); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/clone/notification_clone_bundle_service.cpp b/services/ans/src/clone/notification_clone_bundle_service.cpp index 3511ce3a0..4dae3d501 100644 --- a/services/ans/src/clone/notification_clone_bundle_service.cpp +++ b/services/ans/src/clone/notification_clone_bundle_service.cpp @@ -20,6 +20,7 @@ #include "notification_clone_util.h" #include "notification_clone_bundle_info.h" #include "os_account_manager_helper.h" +#include "advanced_notification_service.h" namespace OHOS { namespace Notification { @@ -75,7 +76,10 @@ void NotificationCloneBundle::OnRestore(const nlohmann::json &jsonObject) int32_t userId = NotificationCloneUtil::GetActiveUserId(); std::unique_lock lock(lock_); - bundlesInfo_.clear(); + if (!bundlesInfo_.empty()) { + NotificationPreferences::GetInstance()->DelBatchCloneBundleInfo(userId, bundlesInfo_); + bundlesInfo_.clear(); + } for (const auto &profile : jsonObject) { NotificationCloneBundleInfo cloneBundleInfo;; cloneBundleInfo.FromJson(profile); @@ -95,41 +99,32 @@ void NotificationCloneBundle::OnRestore(const nlohmann::json &jsonObject) continue; } bundle->SetUid(uid); - NotificationPreferences::GetInstance()->UpdateCloneBundleInfo(userId, *bundle); + AdvancedNotificationService::GetInstance()->UpdateCloneBundleInfo(*bundle); bundle = bundlesInfo_.erase(bundle); } + NotificationPreferences::GetInstance()->UpdateBatchCloneBundleInfo(userId, bundlesInfo_); for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end(); bundle++) { ANS_LOGI("Event bundle left %{public}s.", bundle->Dump().c_str()); } ANS_LOGI("Notification bundle list on restore end."); } -void NotificationCloneBundle::OnBundleDataAdd(const sptr &bundleOption) -{ - HandleBundleEvent(bundleOption->GetBundleName(), bundleOption->GetAppIndex(), bundleOption->GetUid()); -} - -void NotificationCloneBundle::OnBundleDataUpdate(const sptr &bundleOption) +void NotificationCloneBundle::OnRestoreStart(const std::string bundleName, int32_t appIndex, + int32_t userId, int32_t uid) { - HandleBundleEvent(bundleOption->GetBundleName(), bundleOption->GetAppIndex(), bundleOption->GetUid()); -} - -void NotificationCloneBundle::HandleBundleEvent(const std::string bundleName, int32_t appIndex, int32_t uid) -{ - ANS_LOGI("Handle bundle event %{public}s %{public}d %{public}d %{public}zu.", - bundleName.c_str(), appIndex, uid, bundlesInfo_.size()); + ANS_LOGI("Handle bundle event %{public}s %{public}d %{public}d %{public}d %{public}zu.", + bundleName.c_str(), appIndex, userId, uid, bundlesInfo_.size()); if (bundlesInfo_.empty()) { return; } - int32_t userId = -1; - OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(uid, userId); std::unique_lock lcck(lock_); for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end();) { if (bundle->GetBundleName() == bundleName && bundle->GetAppIndex() == appIndex) { bundle->SetUid(uid); - NotificationPreferences::GetInstance()->UpdateCloneBundleInfo(userId, *bundle); + AdvancedNotificationService::GetInstance()->UpdateCloneBundleInfo(*bundle); + NotificationPreferences::GetInstance()->DelCloneBundleInfo(userId, *bundle); bundle = bundlesInfo_.erase(bundle); break; } @@ -145,9 +140,13 @@ void NotificationCloneBundle::OnUserSwitch(int32_t userId) ANS_LOGW("Clone bundle queue is null."); return; } - cloneBundleQueue_->submit_h(std::bind([&]() { + cloneBundleQueue_->submit_h(std::bind([&, userId]() { std::unique_lock lock(lock_); bundlesInfo_.clear(); + NotificationPreferences::GetInstance()->GetAllCloneBundleInfo(userId, bundlesInfo_); + for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end(); bundle++) { + ANS_LOGI("Event bundle OnUserSwitch %{public}s.", bundle->Dump().c_str()); + } })); } diff --git a/services/ans/src/clone/notification_clone_disturb_service.cpp b/services/ans/src/clone/notification_clone_disturb_service.cpp index 6ec7c8466..95939dd84 100644 --- a/services/ans/src/clone/notification_clone_disturb_service.cpp +++ b/services/ans/src/clone/notification_clone_disturb_service.cpp @@ -69,7 +69,11 @@ void NotificationCloneDisturb::OnRestore(const nlohmann::json &jsonObject) return; } - profiles_.clear(); + int32_t userId = NotificationCloneUtil::GetActiveUserId(); + if (!profiles_.empty()) { + NotificationPreferences::GetInstance()->DelBatchCloneProfileInfo(userId, profiles_); + profiles_.clear(); + } for (const auto &profile : jsonObject) { sptr item = new (std::nothrow) NotificationDoNotDisturbProfile(); item->FromJson(profile.dump()); @@ -81,9 +85,7 @@ void NotificationCloneDisturb::OnRestore(const nlohmann::json &jsonObject) return; } - int32_t userId = NotificationCloneUtil::GetActiveUserId(); cloneDisturbQueue_->submit_h(std::bind([&, userId]() { - ANS_LOGI("Notification disturb profile ffrt start %{public}zu.", profiles_.size()); int32_t profileId = -1; std::string name; std::map uidMap; @@ -92,8 +94,7 @@ void NotificationCloneDisturb::OnRestore(const nlohmann::json &jsonObject) std::vector notExitBunldleList; name = (*profile)->GetProfileName(); profileId = (*profile)->GetProfileId(); - ANS_LOGI("Notification disturb profile %{public}d, %{public}zu.", profileId, - (*profile)->GetProfileTrustList().size()); + ANS_LOGI("Disturb %{public}d, %{public}zu.", profileId, (*profile)->GetProfileTrustList().size()); GetProfileUid(userId, uidMap, (*profile)->GetProfileTrustList(), exitBunldleList, notExitBunldleList); NotificationPreferences::GetInstance()->UpdateDoNotDisturbProfiles(userId, profileId, name, exitBunldleList); @@ -107,6 +108,7 @@ void NotificationCloneDisturb::OnRestore(const nlohmann::json &jsonObject) profileId = -1; } + NotificationPreferences::GetInstance()->UpdateBatchCloneProfileInfo(userId, profiles_); for (auto profile = profiles_.begin(); profile != profiles_.end(); profile++) { ANS_LOGI("Clone queue left %{public}d %{public}zu.", (*profile)->GetProfileId(), (*profile)->GetProfileTrustList().size()); @@ -139,16 +141,6 @@ void NotificationCloneDisturb::GetProfileUid(int32_t userId, std::map &bundleOption) -{ - HandlerBundleEvent(bundleOption->GetBundleName(), bundleOption->GetAppIndex(), bundleOption->GetUid()); -} - -void NotificationCloneDisturb::OnBundleDataUpdate(const sptr &bundleOption) -{ - HandlerBundleEvent(bundleOption->GetBundleName(), bundleOption->GetAppIndex(), bundleOption->GetUid()); -} - void NotificationCloneDisturb::OnUserSwitch(int32_t userId) { ANS_LOGI("Handler user switch %{public}d", userId); @@ -156,20 +148,26 @@ void NotificationCloneDisturb::OnUserSwitch(int32_t userId) ANS_LOGW("Clone disturb queue is null."); return; } - cloneDisturbQueue_->submit_h(std::bind([&]() { + cloneDisturbQueue_->submit_h(std::bind([&, userId]() { profiles_.clear(); + NotificationPreferences::GetInstance()->GetAllCloneProfileInfo(userId, profiles_); + for (auto profile = profiles_.begin(); profile != profiles_.end(); profile++) { + ANS_LOGI("Clone queue left %{public}d %{public}zu.", (*profile)->GetProfileId(), + (*profile)->GetProfileTrustList().size()); + } + ANS_LOGI("Notification disturb profile list on recover end."); })); } -void NotificationCloneDisturb::HandlerBundleEvent(const std::string bundleName, int32_t appIndex, int32_t uid) +void NotificationCloneDisturb::OnRestoreStart(const std::string bundleName, int32_t appIndex, + int32_t userId, int32_t uid) { - ANS_LOGI("Handler bundle event %{public}s %{public}d %{public}d %{public}zu.", - bundleName.c_str(), appIndex, uid, profiles_.size()); + ANS_LOGI("Handler bundle event %{public}s %{public}d %{public}d %{public}d %{public}zu.", + bundleName.c_str(), appIndex, userId, uid, profiles_.size()); if (profiles_.empty()) { return; } - int32_t userId = NotificationCloneUtil::GetActiveUserId(); NotificationBundleOption bundle(bundleName, uid); bundle.SetAppIndex(appIndex); if (cloneDisturbQueue_ == nullptr) { @@ -188,6 +186,7 @@ void NotificationCloneDisturb::HandlerBundleEvent(const std::string bundleName, NotificationPreferences::GetInstance()->UpdateDoNotDisturbProfiles(userId, profileId, name, bundleList); if (trustList.empty()) { + NotificationPreferences::GetInstance()->DelCloneProfileInfo(userId, *profile); profile = profiles_.erase(profile); } else { (*profile)->SetProfileTrustList(trustList); @@ -196,6 +195,7 @@ void NotificationCloneDisturb::HandlerBundleEvent(const std::string bundleName, name.clear(); profileId = -1; } + NotificationPreferences::GetInstance()->UpdateBatchCloneProfileInfo(userId, profiles_); for (auto profile = profiles_.begin(); profile != profiles_.end(); profile++) { ANS_LOGI("Event queue left %{public}d %{public}zu.", (*profile)->GetProfileId(), (*profile)->GetProfileTrustList().size()); diff --git a/services/ans/src/clone/notification_clone_manager.cpp b/services/ans/src/clone/notification_clone_manager.cpp index ae65acf2b..2b5b88903 100644 --- a/services/ans/src/clone/notification_clone_manager.cpp +++ b/services/ans/src/clone/notification_clone_manager.cpp @@ -26,6 +26,7 @@ #include "nlohmann/json.hpp" #include "notification_clone_disturb_service.h" #include "notification_clone_bundle_service.h" +#include "notification_clone_util.h" namespace OHOS { namespace Notification { @@ -41,6 +42,22 @@ NotificationCloneManager& NotificationCloneManager::GetInstance() return notificationCloneManager; } +static std::string SetBackUpReply() +{ + nlohmann::json reply; + nlohmann::json resultInfo = nlohmann::json::array(); + nlohmann::json errorInfo; + + errorInfo["type"] = "ErrorInfo"; + errorInfo["errorCode"] = std::to_string(ERR_OK); + errorInfo["errorInfo"] = ""; + + resultInfo.emplace_back(errorInfo); + reply["resultInfo"] = resultInfo; + + return reply.dump(); +} + int32_t NotificationCloneManager::OnBackup(MessageParcel& data, MessageParcel& reply) { if (cloneTemplates.empty()) { @@ -86,6 +103,7 @@ int32_t NotificationCloneManager::OnBackup(MessageParcel& data, MessageParcel& r int32_t NotificationCloneManager::OnRestore(MessageParcel& data, MessageParcel& reply) { + reply.WriteString(SetBackUpReply()); std::string storeMessage; UniqueFd fd(data.ReadFileDescriptor()); if (LoadConfig(fd, storeMessage) != ERR_OK) { @@ -182,7 +200,27 @@ void NotificationCloneManager::RemoveBackUpFile() void NotificationCloneManager::OnUserSwitch(int32_t userId) { for (auto iter = cloneTemplates.begin(); iter != cloneTemplates.end(); ++iter) { - iter->second->OnUserSwitch(userId); + if (iter->second != nullptr) { + iter->second->OnUserSwitch(userId); + } + } +} + +void NotificationCloneManager::OnRestoreStart(EventFwk::Want want) +{ + int32_t appIndex = want.GetIntParam("index", -1); + std::string bundleName = want.GetStringParam("bundleName"); + int32_t userId = NotificationCloneUtil::GetActiveUserId(); + if (appIndex == -1 || bundleName.empty()) { + ANS_LOGW("Invalid restore data %{public}d %{public}d %{public}s", + appIndex, userId, bundleName.c_str()); + return; + } + int32_t uid = NotificationCloneUtil::GetBundleUid(bundleName, userId, appIndex); + for (auto iter = cloneTemplates.begin(); iter != cloneTemplates.end(); ++iter) { + if (iter->second != nullptr) { + iter->second->OnRestoreStart(bundleName, appIndex, userId, uid); + } } } } diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index fbd23fde0..790532eec 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -689,7 +689,6 @@ void NotificationPreferences::UpdateCloneBundleInfo(int32_t userId, } /* after clone, override these witch */ - bundleInfo.SetSlotFlags(cloneBundleInfo.GetSlotFlags()); bundleInfo.SetIsShowBadge(cloneBundleInfo.GetIsShowBadge()); bundleInfo.SetEnableNotification(cloneBundleInfo.GetEnableNotification()); /* update property to db */ @@ -697,6 +696,12 @@ void NotificationPreferences::UpdateCloneBundleInfo(int32_t userId, ANS_LOGW("Clone bundle info failed %{public}s.", cloneBundleInfo.Dump().c_str()); return; } + + if (SaveBundleProperty(bundleInfo, bundleOption, + BundleType::BUNDLE_SLOTFLGS_TYPE, cloneBundleInfo.GetSlotFlags()) != ERR_OK) { + ANS_LOGW("Clone bundle slot info %{public}s.", cloneBundleInfo.Dump().c_str()); + return; + } preferencesInfo.SetBundleInfo(bundleInfo); /* update slot info */ @@ -1256,5 +1261,77 @@ std::string NotificationPreferences::GetAdditionalConfig(const std::string &key) } return preferncesDB_->GetAdditionalConfig(key); } + +bool NotificationPreferences::DelCloneProfileInfo(const int32_t &userId, + const sptr& info) +{ + if (preferncesDB_ == nullptr) { + return false; + } + return preferncesDB_->DelCloneProfileInfo(userId, info); +} + +bool NotificationPreferences::UpdateBatchCloneProfileInfo(const int32_t &userId, + const std::vector>& profileInfo) +{ + if (preferncesDB_ == nullptr) { + return false; + } + return preferncesDB_->UpdateBatchCloneProfileInfo(userId, profileInfo); +} + +void NotificationPreferences::GetAllCloneProfileInfo(const int32_t &userId, + std::vector>& profilesInfo) +{ + if (preferncesDB_ == nullptr) { + return; + } + return preferncesDB_->GetAllCloneProfileInfo(userId, profilesInfo); +} + +void NotificationPreferences::GetAllCloneBundleInfo(const int32_t &userId, + std::vector& cloneBundleInfo) +{ + if (preferncesDB_ == nullptr) { + return; + } + return preferncesDB_->GetAllCloneBundleInfo(userId, cloneBundleInfo); +} + +bool NotificationPreferences::UpdateBatchCloneBundleInfo(const int32_t &userId, + const std::vector& cloneBundleInfo) +{ + if (preferncesDB_ == nullptr) { + return false; + } + return preferncesDB_->UpdateBatchCloneBundleInfo(userId, cloneBundleInfo); +} + +bool NotificationPreferences::DelCloneBundleInfo(const int32_t &userId, + const NotificationCloneBundleInfo& cloneBundleInfo) +{ + if (preferncesDB_ == nullptr) { + return false; + } + return preferncesDB_->DelCloneBundleInfo(userId, cloneBundleInfo); +} + +bool NotificationPreferences::DelBatchCloneProfileInfo(const int32_t &userId, + const std::vector>& profileInfo) +{ + if (preferncesDB_ == nullptr) { + return false; + } + return preferncesDB_->DelBatchCloneProfileInfo(userId, profileInfo); +} + +bool NotificationPreferences::DelBatchCloneBundleInfo(const int32_t &userId, + const std::vector& cloneBundleInfo) +{ + if (preferncesDB_ == nullptr) { + return false; + } + return preferncesDB_->DelBatchCloneBundleInfo(userId, cloneBundleInfo); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 3e1cdcb4d..7d2b325a1 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -223,6 +223,10 @@ const static std::string KEY_REMINDER_MODE = "reminderMode"; constexpr char RELATIONSHIP_JSON_KEY_SERVICE[] = "service"; constexpr char RELATIONSHIP_JSON_KEY_APP[] = "app"; +const static std::string KEY_CLONE_LABEL = "label_ans_clone_"; +const static std::string CLONE_BUNDLE = "bundle_"; +const static std::string CLONE_PROFILE = "profile_"; + NotificationPreferencesDatabase::NotificationPreferencesDatabase() { NotificationRdbConfig notificationRdbConfig; @@ -2119,5 +2123,164 @@ bool NotificationPreferencesDatabase::UpdateBundleSlotToDisturbeDB(int32_t userI int32_t result = rdbDataManager_->InsertBatchData(values, userId); return (result == NativeRdb::E_OK); } + +bool NotificationPreferencesDatabase::DelCloneProfileInfo(const int32_t &userId, + const sptr& info) +{ + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + + std::string key = KEY_CLONE_LABEL + CLONE_PROFILE + std::to_string(info->GetProfileId()); + int32_t result = rdbDataManager_->DeleteData(key, userId); + if (result != NativeRdb::E_OK) { + ANS_LOGE("delete clone profile Info failed."); + return false; + } + return true; +} + +bool NotificationPreferencesDatabase::DelBatchCloneProfileInfo(const int32_t &userId, + const std::vector>& profileInfo) +{ + std::string cloneProfile = KEY_CLONE_LABEL + CLONE_PROFILE; + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + + std::vector keys; + for (auto info : profileInfo) { + std::string key = cloneProfile + std::to_string(info->GetProfileId()); + keys.emplace_back(key); + } + + int32_t result = rdbDataManager_->DeleteBathchData(keys, userId); + if (result != NativeRdb::E_OK) { + ANS_LOGE("delete clone bundle Info failed."); + return false; + } + return true; +} + +bool NotificationPreferencesDatabase::UpdateBatchCloneProfileInfo(const int32_t &userId, + const std::vector>& profileInfo) +{ + std::string cloneProfile = KEY_CLONE_LABEL + CLONE_PROFILE; + std::unordered_map values; + for (auto& info : profileInfo) { + std::string key = cloneProfile + std::to_string(info->GetProfileId()); + std::string jsonString = info->ToJson(); + values.emplace(key, jsonString); + } + return UpdateCloneToDisturbeDB(userId, values); +} + +void NotificationPreferencesDatabase::GetAllCloneProfileInfo(const int32_t &userId, + std::vector>& profilesInfo) +{ + std::string cloneProfile = KEY_CLONE_LABEL + CLONE_PROFILE; + std::unordered_map values; + if (GetBatchKvsFromDb(cloneProfile, values, userId) != ERR_OK) { + ANS_LOGW("Get clone bundle map info failed %{public}d.", userId); + return; + } + + for (auto item : values) { + sptr profile = new (std::nothrow) NotificationDoNotDisturbProfile(); + profile->FromJson(item.second); + profilesInfo.push_back(profile); + } +} + +void NotificationPreferencesDatabase::GetAllCloneBundleInfo(const int32_t &userId, + std::vector& cloneBundleInfo) +{ + std::unordered_map values; + if (GetBatchKvsFromDb(KEY_CLONE_LABEL + CLONE_BUNDLE, values, userId) != ERR_OK) { + ANS_LOGW("Get clone bundle map info failed %{public}d.", userId); + return; + } + + for (auto item : values) { + NotificationCloneBundleInfo bundleInfo; + nlohmann::json jsonObject = nlohmann::json::parse(item.second, nullptr, false); + if (jsonObject.is_null() || !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + continue; + } + bundleInfo.FromJson(jsonObject); + cloneBundleInfo.emplace_back(bundleInfo); + } +} + +bool NotificationPreferencesDatabase::DelBatchCloneBundleInfo(const int32_t &userId, + const std::vector& cloneBundleInfo) +{ + std::string cloneBundle = KEY_CLONE_LABEL + CLONE_BUNDLE; + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + + std::vector keys; + for (auto bundleInfo : cloneBundleInfo) { + std::string key = cloneBundle + bundleInfo.GetBundleName() + + std::to_string(bundleInfo.GetAppIndex()); + keys.emplace_back(key); + } + int32_t result = rdbDataManager_->DeleteBathchData(keys, userId); + if (result != NativeRdb::E_OK) { + ANS_LOGE("delete clone bundle Info failed."); + return false; + } + return true; +} + +bool NotificationPreferencesDatabase::UpdateBatchCloneBundleInfo(const int32_t &userId, + const std::vector& cloneBundleInfo) +{ + std::string cloneBundle = KEY_CLONE_LABEL + CLONE_BUNDLE; + std::unordered_map values; + for (auto& info : cloneBundleInfo) { + nlohmann::json jsonNode; + std::string key = cloneBundle + info.GetBundleName() + std::to_string(info.GetAppIndex()); + info.ToJson(jsonNode); + values.emplace(key, jsonNode.dump()); + } + return UpdateCloneToDisturbeDB(userId, values); +} + +bool NotificationPreferencesDatabase::DelCloneBundleInfo(const int32_t &userId, + const NotificationCloneBundleInfo& cloneBundleInfo) +{ + std::string cloneBundle = KEY_CLONE_LABEL + CLONE_BUNDLE; + std::string key = cloneBundle + cloneBundleInfo.GetBundleName() + + std::to_string(cloneBundleInfo.GetAppIndex()); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + + int32_t result = rdbDataManager_->DeleteData(key, userId); + if (result != NativeRdb::E_OK) { + ANS_LOGE("delete clone bundle Info failed."); + return false; + } + return true; +} + +bool NotificationPreferencesDatabase::UpdateCloneToDisturbeDB(const int32_t &userId, + const std::unordered_map values) +{ + if (values.empty() || !CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + + int32_t result = rdbDataManager_->InsertBatchData(values, userId); + return (result == NativeRdb::E_OK); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index 2fab63ba9..2ca95752e 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -43,6 +43,7 @@ SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callba matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_RESTORE_START); EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills); commonEventSubscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON); @@ -119,6 +120,8 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) callbacks_.onBundleDataCleared(bundleOption); } } + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_RESTORE_START) { + NotificationCloneManager::GetInstance().OnRestoreStart(want); } else { OnReceiveEventInner(data); } -- Gitee