From 40c1c2ad4d9eeef79c66e20dcc87e61352ffd560 Mon Sep 17 00:00:00 2001 From: wangsen1994 Date: Thu, 3 Apr 2025 17:42:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=B8=85=E6=A5=9A=E9=95=BF?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangsen1994 --- .../include/advanced_notification_service.h | 1 + .../ans/include/notification_preferences.h | 6 ++ .../notification_preferences_database.h | 6 ++ .../advanced_notification_publish_service.cpp | 40 +++++++++ .../ans/src/advanced_notification_service.cpp | 4 + .../advanced_notification_slot_service.cpp | 11 ++- services/ans/src/notification_preferences.cpp | 20 ++++- .../src/notification_preferences_database.cpp | 83 +++++++++++++++++++ services/ans/src/permission_filter.cpp | 5 ++ 9 files changed, 172 insertions(+), 4 deletions(-) diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index f40a118e1..697c470a4 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1474,6 +1474,7 @@ private: const sptr &callerToken, const sptr bundleOption, const bool innerLake); + void ClearSlotTypeData(const sptr &request, int32_t callingUid, int32_t sourceType); private: static sptr instance_; diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 0fe91457d..97eed85dc 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -448,6 +448,12 @@ public: */ uint32_t GetHashCodeRule(const int32_t uid); + bool GetBundleRemoveFlag(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, int32_t sourceType); + + bool SetBundleRemoveFlag(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, int32_t sourceType); + private: bool GetBundleInfo(NotificationPreferencesInfo &preferencesInfo, const sptr &bundleOption, NotificationPreferencesInfo::BundleInfo &info) const; diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 5e7f06230..a675c0d4d 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -309,6 +309,12 @@ public: */ uint32_t GetHashCodeRule(const int32_t uid); + bool SetBundleRemoveFlag(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, int32_t sourceType); + + bool GetBundleRemoveFlag(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, int32_t sourceType); + private: bool CheckRdbStore(); diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index df00fb015..c18ee2146 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -78,6 +78,8 @@ const static std::string BUNDLE_NAME_ZYT = "com.zhuoyi.appstore.lite"; const static std::string BUNDLE_NAME_ABROAD = "com.easy.transfer.abroad"; const static std::string INSTALL_SOURCE_EASYABROAD = "com.easy.abroad"; constexpr int32_t BADGE_NUM_LIMIT = 0; +constexpr int32_t CLEAR_SLOT_FROM_AVSEESAION = 1; +constexpr int32_t CLEAR_SLOT_FROM_RSS = 2; ErrCode AdvancedNotificationService::SetDefaultNotificationEnabled( const sptr &bundleOption, bool enabled) @@ -2431,6 +2433,43 @@ void AdvancedNotificationService::UpdateUnifiedGroupInfo(const std::string &key, }); } +void AdvancedNotificationService::ClearSlotTypeData(const sptr &request, int32_t callingUid, + int32_t sourceType) +{ + if (request == nullptr || (sourceType != CLEAR_SLOT_FROM_AVSEESAION && sourceType != CLEAR_SLOT_FROM_RSS)) { + return; + } + + if (sourceType == CLEAR_SLOT_FROM_AVSEESAION) { + if (callingUid != AVSEESAION_PID || + request->GetSlotType() != NotificationConstant::SlotType::LIVE_VIEW) { + return; + } + } + if (sourceType == CLEAR_SLOT_FROM_RSS) { + if (request->GetCreatorUid() != RSS_PID || !request->IsSystemLiveView()) { + return; + } + } + + int32_t uid = request->GetOwnerUid(); + std::string bundleName = BundleManagerHelper::GetInstance()->GetBundleNameByUid(uid); + sptr bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid); + if (bundleOption == nullptr) { + ANS_LOGW("Notification get bundle failed %{public}d", uid); + return; + } + + if (NotificationPreferences::GetInstance()->GetBundleRemoveFlag(bundleOption, + NotificationConstant::SlotType::LIVE_VIEW, sourceType)) { + return; + } + NotificationPreferences::GetInstance()->RemoveNotificationSlot(bundleOption, + NotificationConstant::SlotType::LIVE_VIEW); + NotificationPreferences::GetInstance()->SetBundleRemoveFlag(bundleOption, + NotificationConstant::SlotType::LIVE_VIEW, sourceType); +} + ErrCode AdvancedNotificationService::PublishNotificationBySa(const sptr &request) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -2551,6 +2590,7 @@ ErrCode AdvancedNotificationService::PublishNotificationBySa(const sptrrequest, ipcUid, CLEAR_SLOT_FROM_RSS); return; } bool isNotificationExists = IsNotificationExists(record->notification->GetKey()); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index c8d6900e9..460edc41a 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -914,6 +914,10 @@ ErrCode AdvancedNotificationService::UpdateSlotAuthInfo(const std::shared_ptrSetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED); } } + if (record->request->IsSystemLiveView()) { + ANS_LOGI("System live view no need add sloty."); + return ERR_OK; + } std::vector> slots; slots.push_back(slot); result = NotificationPreferences::GetInstance()->AddNotificationSlots(record->bundleOption, slots); diff --git a/services/ans/src/advanced_notification_slot_service.cpp b/services/ans/src/advanced_notification_slot_service.cpp index 61e2eafbd..e2584bd19 100644 --- a/services/ans/src/advanced_notification_slot_service.cpp +++ b/services/ans/src/advanced_notification_slot_service.cpp @@ -477,9 +477,14 @@ ErrCode AdvancedNotificationService::AssignValidNotificationSlot(const std::shar } GenerateSlotReminderMode(slot, bundleOption); - std::vector> slots; - slots.push_back(slot); - result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundleOption, slots); + if (record->request->IsSystemLiveView()) { + ANS_LOGI("System live view no need add sloty."); + result = ERR_OK; + } else { + std::vector> slots; + slots.push_back(slot); + result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundleOption, slots); + } } if (result == ERR_OK) { std::string bundleName = bundleOption->GetBundleName(); diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index cbd846d8b..619458945 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -1480,7 +1480,7 @@ void NotificationPreferences::SetDistributedEnabledForBundle(const NotificationP ErrCode NotificationPreferences::SetHashCodeRule(const int32_t uid, const uint32_t type) { ANS_LOGD("%{public}s", __FUNCTION__); - + std::lock_guard lock(preferenceMutex_); bool storeDBResult = true; storeDBResult = preferncesDB_->SetHashCodeRule(uid, type); @@ -1496,5 +1496,23 @@ uint32_t NotificationPreferences::GetHashCodeRule(const int32_t uid) ANS_LOGI("GetHashCodeRule uid = %{public}d result = %{public}d", uid, result); return result; } + +bool NotificationPreferences::GetBundleRemoveFlag(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, int32_t sourceType) +{ + if (preferncesDB_ == nullptr) { + return true; + } + return preferncesDB_->GetBundleRemoveFlag(bundleOption, slotType, sourceType); +} + +bool NotificationPreferences::SetBundleRemoveFlag(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, int32_t sourceType) +{ + if (preferncesDB_ == nullptr) { + return false; + } + return preferncesDB_->SetBundleRemoveFlag(bundleOption, slotType, sourceType); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index adc49ece9..9cc57161b 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -245,6 +245,10 @@ const static std::string KEY_SUBSCRIBER_EXISTED_FLAG = "existFlag"; const static int32_t DISTRIBUTED_KEY_NUM = 4; const static int32_t DISTRIBUTED_KEY_BUNDLE_INDEX = 1; const static int32_t DISTRIBUTED_KEY_UID_INDEX = 2; +constexpr int32_t CLEAR_SLOT_FROM_AVSEESAION = 1; +const static std::string KEY_REMOVE_SLOT_FLAG = "label_ans_remove_"; +const static std::string KEY_REMOVED_FLAG = "1"; +const static std::string KEY_SECOND_REMOVED_FLAG = "2"; NotificationPreferencesDatabase::NotificationPreferencesDatabase() { @@ -2627,6 +2631,85 @@ uint32_t NotificationPreferencesDatabase::GetHashCodeRule(const int32_t uid) return result; } +static std::string GetBundleRemoveFlagKey(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, int32_t sourceType) +{ + std::string key; + if (sourceType == CLEAR_SLOT_FROM_AVSEESAION) { + key = KEY_REMOVE_SLOT_FLAG + bundleOption->GetBundleName() + std::to_string(bundleOption->GetUid()) + + KEY_UNDER_LINE + std::to_string(slotType); + } else { + key = KEY_REMOVE_SLOT_FLAG + std::to_string(sourceType) + KEY_UNDER_LINE + bundleOption->GetBundleName() + + std::to_string(bundleOption->GetUid()) + KEY_UNDER_LINE + std::to_string(slotType); + } + return key; +} + +bool NotificationPreferencesDatabase::SetBundleRemoveFlag(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, int32_t sourceType) +{ + if (bundleOption == nullptr) { + ANS_LOGW("Current bundle option is null"); + return false; + } + + int32_t userId = SUBSCRIBE_USER_INIT; + OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId); + if (userId == SUBSCRIBE_USER_INIT) { + ANS_LOGE("Current user acquisition failed"); + return false; + } + + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + + std::string key = GetBundleRemoveFlagKey(bundleOption, slotType, sourceType); + int32_t result = rdbDataManager_->InsertData(key, KEY_SECOND_REMOVED_FLAG, userId); + return (result == NativeRdb::E_OK); +} + +bool NotificationPreferencesDatabase::GetBundleRemoveFlag(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, int32_t sourceType) +{ + if (bundleOption == nullptr) { + ANS_LOGW("Current bundle option is null"); + return true; + } + + int32_t userId = SUBSCRIBE_USER_INIT; + OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId); + if (userId == SUBSCRIBE_USER_INIT) { + ANS_LOGW("Current user acquisition failed"); + return true; + } + + std::string key = GetBundleRemoveFlagKey(bundleOption, slotType, sourceType); + bool existFlag = true; + std::string result; + GetValueFromDisturbeDB(key, userId, [&](const int32_t& status, std::string& value) { + switch (status) { + case NativeRdb::E_EMPTY_VALUES_BUCKET: { + existFlag = false; + break; + } + case NativeRdb::E_OK: { + result = value; + break; + } + default: + break; + } + }); + + ANS_LOGI("Get current remove flag %{public}s,%{public}s,%{public}d", key.c_str(), result.c_str(), existFlag); + if (!existFlag || result == KEY_REMOVED_FLAG) { + return false; + } + return true; +} + std::string NotificationPreferencesDatabase::GenerateHashCodeGenerate(const int32_t uid) { return std::string(KEY_HASH_CODE_RULE).append(KEY_MIDDLE_LINE).append(std::to_string(uid)); diff --git a/services/ans/src/permission_filter.cpp b/services/ans/src/permission_filter.cpp index cc7525aae..4b6485c3d 100644 --- a/services/ans/src/permission_filter.cpp +++ b/services/ans/src/permission_filter.cpp @@ -44,6 +44,11 @@ ErrCode PermissionFilter::OnPublish(const std::shared_ptr &r } } + if (record->request->IsSystemLiveView()) { + ANS_LOGI("System live view no need check switch."); + return ERR_OK; + } + sptr slot; NotificationConstant::SlotType slotType = record->request->GetSlotType(); message.SlotType(slotType); -- Gitee