diff --git a/frameworks/ans/IAnsManager.idl b/frameworks/ans/IAnsManager.idl index c8203b8843b0fed2999a98c9832a45fbc2683d34..deb8d179dc9a4a32cf47429e6a2f83156ac80bab 100644 --- a/frameworks/ans/IAnsManager.idl +++ b/frameworks/ans/IAnsManager.idl @@ -246,6 +246,8 @@ interface OHOS.Notification.IAnsManager { void SetSlotFlagsAsBundle([in] sptr bundleOption, [in] unsigned int slotFlags); + void GetNotificationSettings([out] unsigned int slotFlags); + void GetAllNotificationEnabledBundles([out] NotificationBundleOption[] bundleOption); void GetAllLiveViewEnabledBundles([out] NotificationBundleOption[] bundleOption); diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 0277e776700f5472022092366e5160c1d8fc2ba2..b02bfe9c0e2a75bd524ecddca6e062e0258e7d30 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -67,6 +67,11 @@ ErrCode NotificationHelper::GetNotificationSlotFlagsAsBundle(const NotificationB return DelayedSingleton::GetInstance()->GetNotificationSlotFlagsAsBundle(bundleOption, slotFlags); } +ErrCode NotificationHelper::GetNotificationSettings(uint32_t &slotFlags) +{ + return DelayedSingleton::GetInstance()->GetNotificationSettings(slotFlags); +} + ErrCode NotificationHelper::SetNotificationSlotFlagsAsBundle(const NotificationBundleOption &bundleOption, uint32_t slotFlags) { diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index 10d86a05afa49c51994793d2274b6e5b456c0cf0..94d429f52e264afa2417004bbbf987d04fa3736e 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -116,6 +116,13 @@ public: */ ErrCode GetNotificationSlotFlagsAsBundle(const NotificationBundleOption &bundleOption, uint32_t &slotFlags); + /** + * @brief Obtains slotFlags of bundle. + * + * @param slotFlags Indicates slotFlags of bundle. + * @return Returns get slotflags by bundle result. + */ + ErrCode GetNotificationSettings(uint32_t &slotFlags); /** * @brief Set slotFlags of bundle. * diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index 36d02911b985557da4b5e340dbb62d33d89f6606..1738c062cb69c5e410a8cf32299fb418ce5d8b22 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -162,6 +162,7 @@ namespace Notification { GET_ALL_NOTIFICATIONS_BY_SLOTTYPE, REQUEST_ENABLE_NOTIFICATION_BY_BUNDLE, REPLY_DISTRIBUTE_OPERATION, + GET_NOTIFICATION_SETTING, }; } } diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 10024f68205b82b6d8504fef932847dea39d7765..e36445c2d237f36518b67d3e628cd2d05442b6ae 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -174,6 +174,17 @@ ErrCode AnsNotification::GetNotificationSlotFlagsAsBundle(const NotificationBund return proxy->GetSlotFlagsAsBundle(bo, slotFlags); } +ErrCode AnsNotification::GetNotificationSettings(uint32_t &slotFlags) +{ + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Fail to GetAnsManagerProxy."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->GetNotificationSettings(slotFlags); +} + ErrCode AnsNotification::SetNotificationSlotFlagsAsBundle(const NotificationBundleOption &bundleOption, uint32_t slotFlags) { diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 1e4d6353f8468f7928e5ceb7936ab6060b475075..981bebe5121e92d4a27f56efe7377e5bd9437fd1 100644 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -525,6 +525,11 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode GetNotificationSettings(uint32_t &slotFlags) override + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode RegisterPushCallback(const sptr &pushCallback, const sptr ¬ificationCheckRequest) override { diff --git a/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp b/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp index 856f6899c5e74796add7cf273e3c423b7b796105..1b9b7958c28c70162ae90126f504072e367cd666 100644 --- a/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp @@ -1620,5 +1620,23 @@ HWTEST_F(AnsNotificationUnitTest, PublishNotificationForIndirectProxy_0100, Func ErrCode res = ans_->PublishNotificationForIndirectProxy(request); EXPECT_EQ(res, ERR_ANS_INVALID_PARAM); } + +/* + * @tc.name: GetNotificationSettings_0100 + * @tc.desc: test GetNotificationSetting. + * @tc.type: FUNC + */ +HWTEST_F(AnsNotificationUnitTest, GetNotificationSettings_0100, Function | MediumTest | Level1) +{ + MockWriteInterfaceToken(false); + sptr iremoteObjects = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObjects); + std::shared_ptr proxy = std::make_shared(iremoteObjects); + ASSERT_NE(nullptr, proxy); + ans_->GetAnsManagerProxy(); + uint32_t slotFlags = 0; + ErrCode result = ans_->GetNotificationSettings(slotFlags); + EXPECT_EQ(result, ERR_ANS_SERVICE_NOT_CONNECTED); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/js/napi/include/manager/napi_slot.h b/frameworks/js/napi/include/manager/napi_slot.h index 56b03ddf29ea74d272fcef573b89e17910aef9e9..b13a42a26a9f7307459c7d71a6af3f6c15e19e36 100644 --- a/frameworks/js/napi/include/manager/napi_slot.h +++ b/frameworks/js/napi/include/manager/napi_slot.h @@ -195,6 +195,13 @@ struct AsyncCallbackInfoGetSlotFlagsByBundle { uint32_t slotFlags = 0; }; +struct AsyncCallbackInfoGetNotificationSettings { + napi_env env; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + uint32_t slotFlags = 0; +}; + napi_value NapiAddSlot(napi_env env, napi_callback_info info); napi_value NapiAddSlots(napi_env env, napi_callback_info info); napi_value NapiSetSlotByBundle(napi_env env, napi_callback_info info); @@ -209,6 +216,7 @@ napi_value NapiEnableNotificationSlot(napi_env env, napi_callback_info info); napi_value NapiIsEnableNotificationSlot(napi_env env, napi_callback_info info); napi_value NapiSetSlotFlagsByBundle(napi_env env, napi_callback_info info); napi_value NapiGetSlotFlagsByBundle(napi_env env, napi_callback_info info); +napi_value NapiGetNotificationSettings(napi_env env, napi_callback_info info); napi_value ParseParametersByAddSlot(const napi_env &env, const napi_callback_info &info, ParametersInfoAddSlot ¶s); napi_value ParseParametersByAddSlots( diff --git a/frameworks/js/napi/src/manager/init_module.cpp b/frameworks/js/napi/src/manager/init_module.cpp index 864d9a7506cd70212195f3d70a8bdd92e2d774a6..57478fc46228c1da3ef39a4132b22f0c3a254258 100644 --- a/frameworks/js/napi/src/manager/init_module.cpp +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -99,6 +99,7 @@ napi_value NotificationManagerInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("disableNotificationFeature", NapiDisableNotificationFeature), DECLARE_NAPI_FUNCTION("setTargetDeviceStatus", NapiSetTargetDeviceStatus), DECLARE_NAPI_FUNCTION("requestEnableNotification", NapiRequestEnableNotification), + DECLARE_NAPI_FUNCTION("getNotificationSetting", NapiGetNotificationSettings), #ifdef ANS_FEATURE_BADGE_MANAGER DECLARE_NAPI_FUNCTION("displayBadge", NapiDisplayBadge), diff --git a/frameworks/js/napi/src/manager/napi_slot.cpp b/frameworks/js/napi/src/manager/napi_slot.cpp index df9d4aa0d48c7f9ac7d8aed65cb277644f64eb88..7b17b998a223a7fc3b26295e0d4e4ab1e68caed3 100644 --- a/frameworks/js/napi/src/manager/napi_slot.cpp +++ b/frameworks/js/napi/src/manager/napi_slot.cpp @@ -20,6 +20,7 @@ namespace OHOS { namespace NotificationNapi { +uint32_t NOTIFICATION_SETTING_VIBRATION_ENABLE_BIT = 4; napi_value NapiAddSlot(napi_env env, napi_callback_info info) { ANS_LOGD("enter"); @@ -988,5 +989,71 @@ napi_value NapiGetSlotFlagsByBundle(napi_env env, napi_callback_info info) } } +void AsyncCompleteCallbackNapiGetNotificationSettings(napi_env env, napi_status status, void *data) +{ + ANS_LOGD("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + auto asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + napi_value result = Common::NapiGetNull(env); + napi_create_object(env, &result); + bool soundEnabled = (asynccallbackinfo->slotFlags >> 0) & 1; + bool vibrationEnabled = (asynccallbackinfo->slotFlags >> NOTIFICATION_SETTING_VIBRATION_ENABLE_BIT) & 1; + napi_value vibrationValue; + napi_value soundValue; + napi_get_boolean(env, vibrationEnabled, &vibrationValue); + napi_get_boolean(env, soundEnabled, &soundValue); + + napi_set_named_property(env, result, "vibrationEnabled", vibrationValue); + napi_set_named_property(env, result, "soundEnabled", soundValue); + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete NapiGetNotificationSettings callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGD("NapiGetNotificationSettings work complete end."); +} + +napi_value NapiGetNotificationSettings(napi_env env, napi_callback_info info) +{ + ANS_LOGD("enter"); + AsyncCallbackInfoGetNotificationSettings *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetNotificationSettings {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getNotificationSetting", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("getNotificationSettings work excute."); + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSettings( + asynccallbackinfo->slotFlags); + } + }, + AsyncCompleteCallbackNapiGetNotificationSettings, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return asynccallbackinfo->info.isCallback ? Common::NapiGetNull(env) : promise; +} + } // namespace NotificationNapi } // namespace OHOS diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 0ba6ff992bfe3283d5085ef5044cf6ab012e1cdc..49a84036a507e11c57782f38f0a3ca5a584b48bf 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -182,6 +182,14 @@ public: */ static ErrCode GetNotificationSlotFlagsAsBundle(const NotificationBundleOption &bundleOption, uint32_t &slotFlags); + /** + * @brief Obtains slotFlags of bundle. + * + * @param slotFlags Indicates slotFlags of bundle. + * @return Returns get slotflags by bundle result. + */ + static ErrCode GetNotificationSettings(uint32_t &slotFlags); + /** * @brief set slotflags of bundle. * diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 95628b1234f4d7235d9d0dd53cde1a1e3d00f6df..b892f841ea48212d478f67b92dbcc76eaa500000 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -869,6 +869,14 @@ public: virtual ErrCode GetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t &slotFlags) override; + /** + * @brief Obtains the number of slotFlags. + * + * @param slotFlags Indicates the slogFlags of slot. + * @return Returns ERR_OK on success, others on failure. + */ + virtual ErrCode GetNotificationSettings(uint32_t &slotFlags) override; + /** * @brief Set the slotFlags of slot. * diff --git a/services/ans/src/advanced_notification_slot_service.cpp b/services/ans/src/advanced_notification_slot_service.cpp index 9d666a5c01070d0d990d3a00f23a42c9755c8ce7..a688ee38f3565a42339c17f7f0cad0a9afb08667 100644 --- a/services/ans/src/advanced_notification_slot_service.cpp +++ b/services/ans/src/advanced_notification_slot_service.cpp @@ -45,6 +45,7 @@ namespace { constexpr char KEY_NAME[] = "AGGREGATE_CONFIG"; constexpr char CTRL_LIST_KEY_NAME[] = "NOTIFICATION_CTL_LIST_PKG"; constexpr char CALL_UI_BUNDLE[] = "com.ohos.callui"; + constexpr uint32_t NOTIFICATION_SETTING_FLAG_BASE = 0x11; } ErrCode AdvancedNotificationService::AddSlots(const std::vector> &slots) @@ -418,6 +419,34 @@ ErrCode AdvancedNotificationService::GetSlotFlagsAsBundle(const sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + ANS_LOGD("Failed to generateBundleOption."); + return ERR_ANS_INVALID_BUNDLE; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + result = NotificationPreferences::GetInstance()->GetNotificationSlotFlagsForBundle(bundleOption, slotFlags); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + slotFlags = DEFAULT_SLOT_FLAGS; + } + slotFlags = slotFlags & NOTIFICATION_SETTING_FLAG_BASE; + })); + notificationSvrQueue_->wait(handler); + + return result; +} + ErrCode AdvancedNotificationService::SetSlotFlagsAsBundle(const sptr &bundleOption, uint32_t slotFlags) { diff --git a/services/ans/test/unittest/advanced_notification_slot_service_test.cpp b/services/ans/test/unittest/advanced_notification_slot_service_test.cpp index eb5ed733703e99fe024f3b7b8b7c317a731a34f1..94470e822ac034eb0b4fe318103833d56e819ba0 100644 --- a/services/ans/test/unittest/advanced_notification_slot_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_slot_service_test.cpp @@ -546,5 +546,47 @@ HWTEST_F(AnsSlotServiceTest, GetConfigSlotReminderModeByType_00001, Function | S DelayedSingleton::GetInstance()->GetConfigSlotReminderModeByType(slotType); ASSERT_EQ(reminderMode, (int)0b111111); } + +/** + * @tc.name: GetNotificationSettings_00001 + * @tc.desc: Test GetNotificationSettings + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsSlotServiceTest, GetNotificationSettings_00001, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(false); + + uint32_t flag = 0xff; + auto ret = advancedNotificationService_->GetNotificationSettings(flag); + ASSERT_EQ(ret, ERR_OK); + // invalid outside of the 0th and 4th position + uint32_t result = flag & 0xee; + EXPECT_EQ(result, 0); +} + +/** + * @tc.name: GetNotificationSettings_00002 + * @tc.desc: Test GetNotificationSettings + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsSlotServiceTest, GetNotificationSettings_00002, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(true); + MockIsVerfyPermisson(true); + sptr bundleOption = advancedNotificationService_->GenerateBundleOption(); + ASSERT_NE(bundleOption, nullptr); + + uint32_t flag = 0xff; + auto ret = advancedNotificationService_->SetSlotFlagsAsBundle(bundleOption, flag); + ASSERT_EQ(ret, ERR_OK); + + ret = advancedNotificationService_->GetNotificationSettings(flag); + ASSERT_EQ(ret, ERR_OK); + EXPECT_EQ(flag , 0x11); +} } // namespace Notification } // namespace OHOS diff --git a/tools/test/mock/mock_ans_manager_stub.h b/tools/test/mock/mock_ans_manager_stub.h index c180dbf15206c2bf23cee5aef356b316f7e0fb02..4552c1b49af14627b95a4521138fef814908fd84 100644 --- a/tools/test/mock/mock_ans_manager_stub.h +++ b/tools/test/mock/mock_ans_manager_stub.h @@ -507,6 +507,11 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode GetNotificationSettings(uint32_t& slotFlags) override + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode RegisterPushCallback(const sptr& pushCallback, const sptr& notificationCheckRequest) override {