From 21ae18cc894f2c9c062cb9e0e45f13e8c665c5d2 Mon Sep 17 00:00:00 2001 From: xdongs Date: Sun, 6 Apr 2025 10:47:43 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=B8=9C=E6=B9=96clone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xdongs --- .../core/common/include/ans_const_define.h | 1 + frameworks/core/src/ans_notification.cpp | 3 + services/ans/BUILD.gn | 1 + .../dh_notification_clone_bundle_service.h | 43 ++++++ .../ans/include/notification_analytics_util.h | 1 + .../ans/include/notification_clone_manager.h | 19 ++- .../ans/include/notification_clone_template.h | 4 + .../notification_preferences_database.h | 12 +- .../advanced_notification_publish_service.cpp | 16 +- .../dh_notification_clone_bundle_service.cpp | 146 ++++++++++++++++++ .../notification_clone_bundle_service.cpp | 4 +- .../src/clone/notification_clone_manager.cpp | 89 ++++++++++- services/ans/src/notification_dialog.cpp | 6 +- services/ans/src/notification_preferences.cpp | 1 + 14 files changed, 329 insertions(+), 17 deletions(-) create mode 100644 services/ans/include/dh_notification_clone_bundle_service.h create mode 100644 services/ans/src/clone/dh_notification_clone_bundle_service.cpp diff --git a/frameworks/core/common/include/ans_const_define.h b/frameworks/core/common/include/ans_const_define.h index abc37ced4..6b80bf344 100644 --- a/frameworks/core/common/include/ans_const_define.h +++ b/frameworks/core/common/include/ans_const_define.h @@ -50,6 +50,7 @@ constexpr int64_t INVALID_PROFILE_ID = -1; constexpr int32_t SUBSCRIBE_USER_INIT = -1; constexpr int32_t SUBSCRIBE_USER_ALL = -2; constexpr int32_t DEFAULT_USER_ID = 100; +constexpr int32_t ZERO_USERID = 0; constexpr int32_t SUBSCRIBE_USER_SYSTEM_BEGIN = 0; constexpr int32_t SUBSCRIBE_USER_SYSTEM_END = 99; constexpr int32_t NOTIFICATION_MIN_COUNT = 0; diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 1c356b790..daa7e35e3 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -1707,6 +1707,9 @@ ErrCode AnsNotification::SetBadgeNumberByBundle(const NotificationBundleOption & return ERR_ANS_INVALID_PARAM; } + ANS_LOGI("SetBadgeNumberForDhByBundle, info:%{public}s %{public}d %{public}d", + bundleOption.GetBundleName().c_str(), bundleOption.GetUid(), badgeNumber); + sptr proxy = GetAnsManagerProxy(); if (!proxy) { ANS_LOGE("Unable to connect to ANS service."); diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index d8b860a9c..d0071feac 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -84,6 +84,7 @@ ohos_source_set("ans_service_sources") { "src/ans_manager_stub.cpp", "src/ans_manager_stub_invalid.cpp", "src/bundle_manager_helper.cpp", + "src/clone/dh_notification_clone_bundle_service.cpp", "src/clone/notification_clone_bundle_info.cpp", "src/clone/notification_clone_bundle_service.cpp", "src/clone/notification_clone_disturb_service.cpp", diff --git a/services/ans/include/dh_notification_clone_bundle_service.h b/services/ans/include/dh_notification_clone_bundle_service.h new file mode 100644 index 000000000..efd0ce692 --- /dev/null +++ b/services/ans/include/dh_notification_clone_bundle_service.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_DH_NOTIFICATION_CLONE_BUNDLE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_DH_NOTIFICATION_CLONE_BUNDLE_H + +#include "notification_clone_template.h" +#include "ffrt.h" +#include "notification_bundle_option.h" +#include "notification_clone_bundle_info.h" + +namespace OHOS { +namespace Notification { +class DhNotificationCloneBundle final : public NotificationCloneTemplate { +public: + DhNotificationCloneBundle(); + ~DhNotificationCloneBundle() 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 OnUserSwitch(int32_t userId) override; + bool isDhSource() override; +private: + std::vector bundlesInfo_; + std::shared_ptr dhCloneBundleQueue_ = nullptr; + ffrt::mutex lock_; +}; +} // namespace Notification +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_DH_NOTIFICATION_CLONE_BUNDLE_H \ No newline at end of file diff --git a/services/ans/include/notification_analytics_util.h b/services/ans/include/notification_analytics_util.h index ee131ce42..4fcb7c461 100644 --- a/services/ans/include/notification_analytics_util.h +++ b/services/ans/include/notification_analytics_util.h @@ -38,6 +38,7 @@ enum EventSceneId { SCENE_11 = 11, SCENE_20 = 20, SCENE_21 = 21, + SCENE_22 = 22, }; enum EventBranchId { diff --git a/services/ans/include/notification_clone_manager.h b/services/ans/include/notification_clone_manager.h index a93a9846f..268327107 100644 --- a/services/ans/include/notification_clone_manager.h +++ b/services/ans/include/notification_clone_manager.h @@ -23,9 +23,24 @@ #include "iremote_proxy.h" #include "common_event_data.h" #include "notification_clone_template.h" +#include "common_event_data.h" +#include "common_event_subscriber.h" namespace OHOS { namespace Notification { +class AncoRestoreStartEventSubscriber : public EventFwk::CommonEventSubscriber { +public: + DISALLOW_COPY_AND_MOVE(AncoRestoreStartEventSubscriber); + explicit AncoRestoreStartEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo); + ~AncoRestoreStartEventSubscriber() override; + void OnReceiveEvent(const EventFwk::CommonEventData& data) override; + static std::shared_ptr create(); + +private: + inline static const std::string EVENT_NAME = "usual.event.ANCO_RESTORE_START"; + inline static const std::string EVENT_PUBLISHER_PERMISSION = "ohos.permission.RECEIVE_BMS_BROKER_MESSAGES"; +}; + class NotificationCloneManager { public: static NotificationCloneManager& GetInstance(); @@ -34,6 +49,7 @@ public: int32_t OnRestore(MessageParcel& data, MessageParcel& reply); void OnUserSwitch(int32_t userId); void OnRestoreStart(EventFwk::Want want); + void OnDhRestoreStart(const std::string bundleName, const int32_t uid); private: NotificationCloneManager(); @@ -42,7 +58,8 @@ private: void RemoveBackUpFile(); ErrCode LoadConfig(UniqueFd &fd, std::string& config); ErrCode SaveConfig(const std::string& config); - std::map> cloneTemplates; + std::vector>> cloneTemplates; + std::shared_ptr restoreStartEventSubscriber_ = nullptr; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/notification_clone_template.h b/services/ans/include/notification_clone_template.h index 02a486acd..b5b696086 100644 --- a/services/ans/include/notification_clone_template.h +++ b/services/ans/include/notification_clone_template.h @@ -31,6 +31,10 @@ public: virtual void OnUserSwitch(int32_t userId) = 0; virtual void OnRestoreStart(const std::string bundleName, int32_t appIndex, int32_t userId, int32_t uid) = 0; + virtual bool isDhSource() + { + return false; + }; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index a675c0d4d..e6ea8df60 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -315,6 +315,16 @@ public: bool GetBundleRemoveFlag(const sptr &bundleOption, const NotificationConstant::SlotType &slotType, int32_t sourceType); + /** + * @brief ParseBundleFromDistureDB + * @param info bundle info. + * @param entries bundle keys. + * @param userId userId. + * @return void + */ + void ParseBundleFromDistureDB(NotificationPreferencesInfo &info, + const std::unordered_map &entries, const int32_t &userId); + private: bool CheckRdbStore(); @@ -354,8 +364,6 @@ private: const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const; std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const; - void ParseBundleFromDistureDB(NotificationPreferencesInfo &info, - const std::unordered_map &entries, const int32_t &userId); void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, const std::pair &entry, const int32_t &userId); void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index c18ee2146..b108876ba 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -1102,7 +1102,9 @@ ErrCode AdvancedNotificationService::CommonRequestEnableNotification(const std:: ANS_LOGI("%{public}s_%{public}d, deviceId: %{public}s, Request enable notification dailog result: %{public}d", bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), deviceId.c_str(), result); message.ErrorCode(result); - NotificationAnalyticsUtil::ReportModifyEvent(message); + if (!innerLake || result == ERR_ANS_DIALOG_POP_SUCCEEDED) { + NotificationAnalyticsUtil::ReportModifyEvent(message); + } return result; } @@ -2664,9 +2666,15 @@ ErrCode AdvancedNotificationService::SetBadgeNumber(int32_t badgeNumber, const s ErrCode AdvancedNotificationService::SetBadgeNumberForDhByBundle( const sptr &bundleOption, int32_t badgeNumber) { - if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || - bundleOption->GetUid() <= DEFAULT_UID) { - ANS_LOGE("SetBadgeNumberForDhByBundle invalid bundleOption"); + if (bundleOption == nullptr) { + ANS_LOGE("SetBadgeNumberForDhByBundle bundleOption is null"); + } + if (bundleOption->GetBundleName().empty()) { + ANS_LOGE("SetBadgeNumberForDhByBundle Invalid bundle name."); + return ERR_ANS_INVALID_PARAM; + } + if (bundleOption->GetUid() <= DEFAULT_UID) { + ANS_LOGE("SetBadgeNumberForDhByBundle invalid uid"); return ERR_ANS_INVALID_PARAM; } if (badgeNumber < BADGE_NUM_LIMIT) { diff --git a/services/ans/src/clone/dh_notification_clone_bundle_service.cpp b/services/ans/src/clone/dh_notification_clone_bundle_service.cpp new file mode 100644 index 000000000..0e93e2a5f --- /dev/null +++ b/services/ans/src/clone/dh_notification_clone_bundle_service.cpp @@ -0,0 +1,146 @@ +/* +* Copyright (c) 2024-2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "dh_notification_clone_bundle_service.h" + +#include "ans_log_wrapper.h" +#include "notification_preferences.h" +#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 { + +std::shared_ptr DhNotificationCloneBundle::GetInstance() +{ + static std::shared_ptr instance = + std::make_shared(); + return instance; +} + +DhNotificationCloneBundle::DhNotificationCloneBundle() +{ + dhCloneBundleQueue_ = std::make_shared("DhNotificationCloneBundleQueue"); + if (!dhCloneBundleQueue_) { + ANS_LOGE("ffrt create failed!"); + return; + } +} + +DhNotificationCloneBundle::~DhNotificationCloneBundle() +{ +} + +ErrCode DhNotificationCloneBundle::OnBackup(nlohmann::json &jsonObject) +{ + ANS_LOGI("dh Notification bundle backup."); + std::vector cloneBundles; + NotificationPreferences::GetInstance()->GetAllCLoneBundlesInfo(ZERO_USERID, cloneBundles); + + if (cloneBundles.empty()) { + ANS_LOGI("dh Notification bundle list is empty."); + return ERR_OK; + } + jsonObject = nlohmann::json::array(); + for (size_t index = 0; index < cloneBundles.size(); index++) { + nlohmann::json jsonNode; + cloneBundles[index].ToJson(jsonNode); + jsonObject.emplace_back(jsonNode); + ANS_LOGD("Event dh bundle backup %{public}s.", cloneBundles[index].Dump().c_str()); + } + ANS_LOGD("dh Notification bundle list %{public}s", jsonObject.dump().c_str()); + return ERR_OK; +} + +void DhNotificationCloneBundle::OnRestore(const nlohmann::json &jsonObject) +{ + ANS_LOGI("dh Notification bundle list on restore."); + if (jsonObject.is_null() || !jsonObject.is_array()) { + ANS_LOGI("dh Notification bundle list is null or not array."); + return; + } + + std::unique_lock lock(lock_); + if (!bundlesInfo_.empty()) { + NotificationPreferences::GetInstance()->DelBatchCloneBundleInfo(ZERO_USERID, bundlesInfo_); + bundlesInfo_.clear(); + } + for (const auto &profile : jsonObject) { + NotificationCloneBundleInfo cloneBundleInfo; + cloneBundleInfo.FromJson(profile); + bundlesInfo_.emplace_back(cloneBundleInfo); + } + ANS_LOGI("dh Notification bundle list size %{public}zu.", bundlesInfo_.size()); + if (dhCloneBundleQueue_ == nullptr || bundlesInfo_.empty()) { + ANS_LOGE("Clone dh bundle is invalidated or empty."); + return; + } + + NotificationPreferences::GetInstance()->UpdateBatchCloneBundleInfo(ZERO_USERID, bundlesInfo_); + for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end(); bundle++) { + ANS_LOGI("Event dh bundle left %{public}s.", bundle->Dump().c_str()); + } + ANS_LOGI("dh Notification bundle list on restore end."); +} + +void DhNotificationCloneBundle::OnRestoreStart(const std::string bundleName, int32_t appIndex, + int32_t userId, int32_t uid) +{ + ANS_LOGI("Handle dh bundle event %{public}s %{public}d %{public}zu.", + bundleName.c_str(), uid, bundlesInfo_.size()); + std::unique_lock lock(lock_); + if (bundlesInfo_.empty()) { + return; + } + + for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end();) { + if (bundle->GetBundleName() == bundleName) { + bundle->SetUid(uid); + AdvancedNotificationService::GetInstance()->UpdateCloneBundleInfo(*bundle); + NotificationPreferences::GetInstance()->DelCloneBundleInfo(ZERO_USERID, *bundle); + bundle = bundlesInfo_.erase(bundle); + break; + } + bundle++; + } + ANS_LOGI("Event dh bundle left %{public}zu.", bundlesInfo_.size()); +} + +void DhNotificationCloneBundle::OnUserSwitch(int32_t userId) +{ + ANS_LOGI("Handler user switch %{public}d", userId); + if (dhCloneBundleQueue_ == nullptr) { + ANS_LOGW("Clone bundle queue is null."); + return; + } + dhCloneBundleQueue_->submit_h(std::bind([&, userId]() { + std::unique_lock lock(lock_); + bundlesInfo_.clear(); + NotificationPreferences::GetInstance()->GetAllCloneBundleInfo(ZERO_USERID, bundlesInfo_); + for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end(); bundle++) { + ANS_LOGI("Event dh bundle OnUserSwitch %{public}s.", bundle->Dump().c_str()); + } + })); +} + +bool DhNotificationCloneBundle::isDhSource() +{ + return true; +} + +} +} \ No newline at end of file diff --git a/services/ans/src/clone/notification_clone_bundle_service.cpp b/services/ans/src/clone/notification_clone_bundle_service.cpp index 4dae3d501..9bdf7f60b 100644 --- a/services/ans/src/clone/notification_clone_bundle_service.cpp +++ b/services/ans/src/clone/notification_clone_bundle_service.cpp @@ -81,7 +81,7 @@ void NotificationCloneBundle::OnRestore(const nlohmann::json &jsonObject) bundlesInfo_.clear(); } for (const auto &profile : jsonObject) { - NotificationCloneBundleInfo cloneBundleInfo;; + NotificationCloneBundleInfo cloneBundleInfo; cloneBundleInfo.FromJson(profile); bundlesInfo_.emplace_back(cloneBundleInfo); } @@ -115,11 +115,11 @@ void NotificationCloneBundle::OnRestoreStart(const std::string bundleName, int32 { ANS_LOGI("Handle bundle event %{public}s %{public}d %{public}d %{public}d %{public}zu.", bundleName.c_str(), appIndex, userId, uid, bundlesInfo_.size()); + std::unique_lock lock(lock_); if (bundlesInfo_.empty()) { return; } - std::unique_lock lcck(lock_); for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end();) { if (bundle->GetBundleName() == bundleName && bundle->GetAppIndex() == appIndex) { bundle->SetUid(uid); diff --git a/services/ans/src/clone/notification_clone_manager.cpp b/services/ans/src/clone/notification_clone_manager.cpp index 1f4d4392a..9d054ac5a 100644 --- a/services/ans/src/clone/notification_clone_manager.cpp +++ b/services/ans/src/clone/notification_clone_manager.cpp @@ -27,15 +27,58 @@ #include "notification_clone_disturb_service.h" #include "notification_clone_bundle_service.h" #include "notification_clone_util.h" +#include "dh_notification_clone_bundle_service.h" +#include "common_event_manager.h" +#include "notification_analytics_util.h" namespace OHOS { namespace Notification { const int ANS_CLONE_ERROR = -1; +const int32_t DEFAULT_APP_INDEX = -1; constexpr const char *CLONE_ITEM_BUNDLE_INFO = "notificationBundle"; +constexpr const char *DH_CLONE_ITEM_BUNDLE_INFO = "dhNotificationBundle"; constexpr const char *CLONE_ITEM_DISTURB = "notificationDisturb"; constexpr const char *BACKUP_CONFIG_FILE_PATH = "/data/service/el1/public/notification/backup_config.conf"; +std::shared_ptr AncoRestoreStartEventSubscriber::create() +{ + ANS_LOGI("AncoRestoreStartEventSubscriber create"); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(AncoRestoreStartEventSubscriber::EVENT_NAME); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + subscriberInfo.SetPermission(AncoRestoreStartEventSubscriber::EVENT_PUBLISHER_PERMISSION); + return std::make_shared(subscriberInfo); + ANS_LOGI("AncoRestoreStartEventSubscriber create end"); +} + +AncoRestoreStartEventSubscriber::AncoRestoreStartEventSubscriber( + const EventFwk::CommonEventSubscribeInfo &subscribeInfo) + : EventFwk::CommonEventSubscriber(subscribeInfo) +{ +} + +AncoRestoreStartEventSubscriber::~AncoRestoreStartEventSubscriber() +{ + ANS_LOGI("AncoRestoreStartEventSubscriber release"); +} + +void AncoRestoreStartEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data) +{ + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_22, EventBranchId::BRANCH_4); + std::string bundleName = data.GetWant().GetStringParam("bundleName"); + int32_t uid = data.GetWant().GetIntParam("uid", 0); + ANS_LOGI("AncoRestoreStartEventSubscriber Get Data %{public}s %{public}d", + bundleName.c_str(), uid); + if (uid <= 0) { + message.Message("dh restoreStart uid error" + bundleName); + NotificationAnalyticsUtil::ReportModifyEvent(message); + ANS_LOGE("AncoRestoreStartEventSubscriber uid error"); + return; + } + NotificationCloneManager::GetInstance().OnDhRestoreStart(bundleName, uid); +} + NotificationCloneManager& NotificationCloneManager::GetInstance() { static NotificationCloneManager notificationCloneManager; @@ -60,6 +103,8 @@ static std::string SetBackUpReply() int32_t NotificationCloneManager::OnBackup(MessageParcel& data, MessageParcel& reply) { + ANS_LOGI("OnBackup start"); + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_22, EventBranchId::BRANCH_1); if (cloneTemplates.empty()) { ANS_LOGI("Notification no need Backup."); return ERR_OK; @@ -81,15 +126,19 @@ int32_t NotificationCloneManager::OnBackup(MessageParcel& data, MessageParcel& r } if (SaveConfig(jsonObject.dump()) != ERR_OK) { + message.Message("SaveConfig failed."); + NotificationAnalyticsUtil::ReportModifyEvent(message); return ANS_CLONE_ERROR; } UniqueFd fd = UniqueFd(open(BACKUP_CONFIG_FILE_PATH, O_RDONLY)); if (fd.Get() < 0) { - ANS_LOGW("Notification open file failed."); + ANS_LOGE("Notification open file failed."); + message.Message("Notification open file failed."); + NotificationAnalyticsUtil::ReportModifyEvent(message); return ANS_CLONE_ERROR; } - + if (reply.WriteFileDescriptor(fd) == false) { close(fd.Release()); ANS_LOGW("Notification write file descriptor failed!"); @@ -103,23 +152,31 @@ int32_t NotificationCloneManager::OnBackup(MessageParcel& data, MessageParcel& r int32_t NotificationCloneManager::OnRestore(MessageParcel& data, MessageParcel& reply) { + ANS_LOGI("OnRestore start"); + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_22, EventBranchId::BRANCH_2); reply.WriteString(SetBackUpReply()); std::string storeMessage; UniqueFd fd(data.ReadFileDescriptor()); if (LoadConfig(fd, storeMessage) != ERR_OK) { close(fd.Release()); RemoveBackUpFile(); + message.Message("LoadConfig failed!"); + NotificationAnalyticsUtil::ReportModifyEvent(message); return ANS_CLONE_ERROR; } RemoveBackUpFile(); if (storeMessage.empty() || !nlohmann::json::accept(storeMessage)) { ANS_LOGE("Invalid JSON"); + message.Message("Invalid JSON"); + NotificationAnalyticsUtil::ReportModifyEvent(message); return ANS_CLONE_ERROR; } nlohmann::json jsonObject = nlohmann::json::parse(storeMessage, nullptr, false); if (jsonObject.is_null() || !jsonObject.is_object()) { ANS_LOGE("Invalid JSON object"); + message.Message("Invalid JSON object"); + NotificationAnalyticsUtil::ReportModifyEvent(message); return ANS_CLONE_ERROR; } for (auto iter = cloneTemplates.begin(); iter != cloneTemplates.end(); ++iter) { @@ -127,14 +184,24 @@ int32_t NotificationCloneManager::OnRestore(MessageParcel& data, MessageParcel& iter->second->OnRestore(jsonObject.at(iter->first)); } } + ANS_LOGI("OnRestore end"); return ERR_OK; } NotificationCloneManager::NotificationCloneManager() { ANS_LOGI("Notification clone manager init."); - cloneTemplates.insert_or_assign(CLONE_ITEM_BUNDLE_INFO, NotificationCloneBundle::GetInstance()); - cloneTemplates.insert_or_assign(CLONE_ITEM_DISTURB, NotificationCloneDisturb::GetInstance()); + // not change push sequence, ensure [clone item] before [dh clone item] + cloneTemplates.push_back(std::make_pair(CLONE_ITEM_BUNDLE_INFO, NotificationCloneBundle::GetInstance())); + cloneTemplates.push_back(std::make_pair(DH_CLONE_ITEM_BUNDLE_INFO, DhNotificationCloneBundle::GetInstance())); + cloneTemplates.push_back(std::make_pair(CLONE_ITEM_DISTURB, NotificationCloneDisturb::GetInstance())); + + restoreStartEventSubscriber_ = AncoRestoreStartEventSubscriber::create(); + if (!EventFwk::CommonEventManager::SubscribeCommonEvent(restoreStartEventSubscriber_)) { + ANS_LOGE("Subscribe AncoRestoreStartEventSubscriber Failed."); + restoreStartEventSubscriber_ = nullptr; + } + ANS_LOGI("Subscribe AncoRestoreStartEventSubscriber success."); } NotificationCloneManager::~NotificationCloneManager() @@ -212,20 +279,32 @@ void NotificationCloneManager::OnUserSwitch(int32_t userId) void NotificationCloneManager::OnRestoreStart(EventFwk::Want want) { + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_22, EventBranchId::BRANCH_3); 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()); + message.Message("Invalid restore data"); + NotificationAnalyticsUtil::ReportModifyEvent(message); return; } int32_t uid = NotificationCloneUtil::GetBundleUid(bundleName, userId, appIndex); for (auto iter = cloneTemplates.begin(); iter != cloneTemplates.end(); ++iter) { - if (iter->second != nullptr) { + if (iter->second != nullptr && !iter->second->isDhSource()) { iter->second->OnRestoreStart(bundleName, appIndex, userId, uid); } } } + +void NotificationCloneManager::OnDhRestoreStart(const std::string bundleName, const int32_t uid) +{ + for (auto iter = cloneTemplates.begin(); iter != cloneTemplates.end(); ++iter) { + if (iter->second != nullptr && iter->second->isDhSource()) { + iter->second->OnRestoreStart(bundleName, DEFAULT_APP_INDEX, ZERO_USERID, uid); + } + } +} } } diff --git a/services/ans/src/notification_dialog.cpp b/services/ans/src/notification_dialog.cpp index ac53a9545..5698fabc4 100644 --- a/services/ans/src/notification_dialog.cpp +++ b/services/ans/src/notification_dialog.cpp @@ -51,16 +51,16 @@ ErrCode NotificationDialog::StartEnableNotificationDialogAbility( auto topBundleName = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName()); if (topBundleName != appBundleName) { - ANS_LOGE("Current application isn't in foreground, top is %{public}s.", topBundleName.c_str()); + ANS_LOGW("Current application isn't in foreground, top is %{public}s.", topBundleName.c_str()); if (!innerLake) { return ERR_ANS_INVALID_BUNDLE; } else { - ANS_LOGE("get top ability again"); + ANS_LOGW("get top ability again"); std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME)); topBundleName = IN_PROCESS_CALL( AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName()); if (topBundleName != appBundleName) { - ANS_LOGE("get top ability again failed"); + ANS_LOGW("get top ability again failed"); return ERR_ANS_INVALID_BUNDLE; } } diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 619458945..d6b33778d 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -732,6 +732,7 @@ void NotificationPreferences::GetAllCLoneBundlesInfo(int32_t userId, ANS_LOGE("Get bundle map info failed."); return; } + preferncesDB_->ParseBundleFromDistureDB(preferencesInfo, bundlesMap, userId); preferencesInfo.GetAllCLoneBundlesInfo(userId, bundlesMap, cloneBundles); preferencesInfo_ = preferencesInfo; } -- Gitee From b5f053911ef42272fd8f4b3e967d3bf3e0f1f730 Mon Sep 17 00:00:00 2001 From: xdongs2009 Date: Sun, 27 Apr 2025 12:38:13 +0000 Subject: [PATCH 2/3] update services/ans/src/advanced_notification_publish_service.cpp. Signed-off-by: xdongs2009 --- services/ans/src/advanced_notification_publish_service.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index b108876ba..af79cac70 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -2668,6 +2668,7 @@ ErrCode AdvancedNotificationService::SetBadgeNumberForDhByBundle( { if (bundleOption == nullptr) { ANS_LOGE("SetBadgeNumberForDhByBundle bundleOption is null"); + return ERR_ANS_INVALID_PARAM; } if (bundleOption->GetBundleName().empty()) { ANS_LOGE("SetBadgeNumberForDhByBundle Invalid bundle name."); -- Gitee From 382667303471e52a3880e827c619714bec55aea3 Mon Sep 17 00:00:00 2001 From: xdongs2009 Date: Sun, 27 Apr 2025 12:48:24 +0000 Subject: [PATCH 3/3] update services/ans/src/clone/notification_clone_manager.cpp. Signed-off-by: xdongs2009 --- services/ans/src/clone/notification_clone_manager.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/services/ans/src/clone/notification_clone_manager.cpp b/services/ans/src/clone/notification_clone_manager.cpp index 9d054ac5a..95533d846 100644 --- a/services/ans/src/clone/notification_clone_manager.cpp +++ b/services/ans/src/clone/notification_clone_manager.cpp @@ -138,10 +138,12 @@ int32_t NotificationCloneManager::OnBackup(MessageParcel& data, MessageParcel& r NotificationAnalyticsUtil::ReportModifyEvent(message); return ANS_CLONE_ERROR; } - + if (reply.WriteFileDescriptor(fd) == false) { close(fd.Release()); - ANS_LOGW("Notification write file descriptor failed!"); + ANS_LOGE("Notification write file descriptor failed!"); + message.Message("Notification write file descriptor failed!"); + NotificationAnalyticsUtil::ReportModifyEvent(message); return ANS_CLONE_ERROR; } -- Gitee