diff --git a/services/ans/include/bundle_manager_helper.h b/services/ans/include/bundle_manager_helper.h index 1ddaf79187f5f1425ad4ea59f700b659af2da745..2e5fbd57a5bda70b19a4ec9f1dfff34f13023fe2 100644 --- a/services/ans/include/bundle_manager_helper.h +++ b/services/ans/include/bundle_manager_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -23,6 +23,7 @@ #include "bundle_mgr_interface.h" #include "ipc_skeleton.h" #include "iremote_object.h" +#include "notification_bundle_option.h" #include "refbase.h" #include "remote_death_recipient.h" #include "singleton.h" @@ -33,6 +34,7 @@ class BundleManagerHelper : public DelayedSingleton { public: std::string GetBundleNameByUid(int uid); bool IsSystemApp(int uid); + bool CheckApiCompatibility(const sptr &bundleOption); int GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId); bool GetBundleInfoByBundleName(const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 63d6053779f2f7c6f528cc246e7ab9c024c3b85a..df5bf790160d7eed63cc3b54f84b018f2333f5ce 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -101,6 +101,7 @@ private: ErrCode GetBundleProperty( const sptr &bundleOption, const BundleType &type, T &value) const; std::string GenerateBundleKey(const sptr &bundleOption) const; + bool CheckApiCompatibility(const sptr &bundleOption) const; private: NotificationPreferencesInfo preferencesInfo_ {}; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 2cb610c118d2fcf4d545194453122a31e92812e8..9678c5f8d976537a894e0e8335325a07051770d3 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1591,6 +1591,7 @@ ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; allowed = CheckApiCompatibility(targetBundle); + SetNotificationsEnabledForSpecialBundle("", bundleOption, allowed); } } })); @@ -3225,24 +3226,12 @@ ErrCode AdvancedNotificationService::GetHasPoppedDialog( bool AdvancedNotificationService::CheckApiCompatibility(const sptr &bundleOption) { - AppExecFwk::BundleInfo bundleInfo; + ANS_LOGD("%{public}s", __FUNCTION__); std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); - int32_t callingUserId; - AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), callingUserId); - if (bundleManager != nullptr) { - if (!bundleManager->GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { - ANS_LOGW("Failed to GetBundleInfoByBundleName, bundlename = %{public}s", - bundleOption->GetBundleName().c_str()); - return false; - } - } - - for (auto abilityInfo : bundleInfo.abilityInfos) { - if (abilityInfo.isStageBasedModel) { - return false; - } + if (bundleManager == nullptr) { + return false; } - return true; + return bundleManager->CheckApiCompatibility(bundleOption); } void AdvancedNotificationService::OnResourceRemove(int32_t userId) diff --git a/services/ans/src/bundle_manager_helper.cpp b/services/ans/src/bundle_manager_helper.cpp index 9e965edcb73798f9fe07f06e0f98ee0b2983315e..037d4bb0a81caca32294dafa7b190f38e75d0d1d 100644 --- a/services/ans/src/bundle_manager_helper.cpp +++ b/services/ans/src/bundle_manager_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -17,6 +17,7 @@ #include "if_system_ability_manager.h" #include "iservice_registry.h" +#include "os_account_manager.h" #include "system_ability_definition.h" #include "ans_const_define.h" @@ -71,6 +72,25 @@ bool BundleManagerHelper::IsSystemApp(int uid) return isSystemApp; } +bool BundleManagerHelper::CheckApiCompatibility(const sptr &bundleOption) +{ + AppExecFwk::BundleInfo bundleInfo; + int32_t callingUserId; + AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), callingUserId); + if (!GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { + ANS_LOGW("Failed to GetBundleInfoByBundleName, bundlename = %{public}s", + bundleOption->GetBundleName().c_str()); + return false; + } + + for (auto abilityInfo : bundleInfo.abilityInfos) { + if (abilityInfo.isStageBasedModel) { + return false; + } + } + return true; +} + bool BundleManagerHelper::GetBundleInfoByBundleName( const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo) { diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index a0613efa31e5215b6499e5aa46671110dc2b96f0..f3367710e3ee3d125aa2d219aa116e7a0c32edf5 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -20,6 +20,7 @@ #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" +#include "bundle_manager_helper.h" #include "nlohmann/json.hpp" #include "os_account_manager.h" @@ -611,6 +612,7 @@ ErrCode NotificationPreferences::CheckSlotForCreateSlot(const sptrGetBundleName()); bundleInfo.SetBundleUid(bundleOption->GetUid()); + bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption)); } bundleInfo.SetSlot(slot); preferencesInfo.SetBundleInfo(bundleInfo); @@ -635,6 +637,7 @@ ErrCode NotificationPreferences::CheckGroupForCreateSlotGroup(const sptrGetBundleName()); bundleInfo.SetBundleUid(bundleOption->GetUid()); + bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption)); } else { if (bundleInfo.GetGroupSize() >= MAX_SLOT_GROUP_NUM) { return ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_EXCEED_MAX_NUM; @@ -745,6 +748,7 @@ ErrCode NotificationPreferences::SetBundleProperty(NotificationPreferencesInfo & if (!preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) { bundleInfo.SetBundleName(bundleOption->GetBundleName()); bundleInfo.SetBundleUid(bundleOption->GetUid()); + bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleOption)); } result = SaveBundleProperty(bundleInfo, bundleOption, type, value); @@ -890,5 +894,15 @@ void NotificationPreferences::RemoveSettings(int32_t userId) preferncesDB_->RemoveDoNotDisturbDate(userId); } } + +bool NotificationPreferences::CheckApiCompatibility(const sptr &bundleOption) const +{ + ANS_LOGD("%{public}s", __FUNCTION__); + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager == nullptr) { + return false; + } + return bundleManager->CheckApiCompatibility(bundleOption); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/permission_filter.cpp b/services/ans/src/permission_filter.cpp index 9c4a86870cd4a44666ca1c5e5c6b6239bd639618..70ef64d60a4c49d480c317a250363211e335ce83 100644 --- a/services/ans/src/permission_filter.cpp +++ b/services/ans/src/permission_filter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -17,6 +17,7 @@ #include "ans_inner_errors.h" #include "ans_log_wrapper.h" +#include "bundle_manager_helper.h" #include "notification_preferences.h" namespace OHOS { @@ -34,7 +35,10 @@ ErrCode PermissionFilter::OnPublish(const std::shared_ptr &r NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(record->bundleOption, enable); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; - enable = true; + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager == nullptr) { + enable = bundleManager->CheckApiCompatibility(record->bundleOption); + } } if (result == ERR_OK) { if (!enable) { diff --git a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp index 6eba48ba6f8b9a28b9b77136e946362d3b88ca52..fa0d3eb61358b4024540942ee05530f1f1b724ac 100644 --- a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp +++ b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -46,6 +46,11 @@ bool BundleManagerHelper::IsSystemApp(int uid) return (uid == SYSTEM_APP_UID); } +bool BundleManagerHelper::CheckApiCompatibility(const sptr &bundleOption) +{ + return true; +} + bool BundleManagerHelper::GetBundleInfoByBundleName( const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo) { diff --git a/services/test/moduletest/mock/mock_bundle_manager_helper.cpp b/services/test/moduletest/mock/mock_bundle_manager_helper.cpp index fdeac6723e6820bb7a97931b9baff0972d3ce38b..38de76c01e2f8df937aac4cf356a268635654aa0 100644 --- a/services/test/moduletest/mock/mock_bundle_manager_helper.cpp +++ b/services/test/moduletest/mock/mock_bundle_manager_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -40,6 +40,11 @@ bool BundleManagerHelper::IsSystemApp(int uid) return true; } +bool BundleManagerHelper::CheckApiCompatibility(const sptr &bundleOption) +{ + return true; +} + int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId) { return 1000;