diff --git a/frameworks/ans/core/src/ans_manager_proxy.cpp b/frameworks/ans/core/src/ans_manager_proxy.cpp index ae493cd9da4650a7e9b8d16dc5557db6d359d2ce..b7ac0f7d019a35e00b50d959efe43faedba3d69a 100644 --- a/frameworks/ans/core/src/ans_manager_proxy.cpp +++ b/frameworks/ans/core/src/ans_manager_proxy.cpp @@ -354,10 +354,12 @@ ErrCode AnsManagerProxy::GetSlotByType(const NotificationConstant::SlotType &slo return ERR_ANS_PARCELABLE_FAILED; } - slot = reply.ReadParcelable(); - if (slot == nullptr) { - ANS_LOGW("[GetSlotByType] fail: read slot failed"); - return ERR_ANS_PARCELABLE_FAILED; + if (result == ERR_OK) { + slot = reply.ReadParcelable(); + if (slot == nullptr) { + ANS_LOGW("[GetSlotByType] fail: read slot failed"); + return ERR_ANS_PARCELABLE_FAILED; + } } return result; @@ -418,10 +420,12 @@ ErrCode AnsManagerProxy::GetSlotGroup(const std::string &groupId, sptr(); - if (group == nullptr) { - ANS_LOGW("[GetSlotGroup] fail: read group failed"); - return ERR_ANS_PARCELABLE_FAILED; + if (result == ERR_OK) { + group = reply.ReadParcelable(); + if (group == nullptr) { + ANS_LOGW("[GetSlotGroup] fail: read group failed"); + return ERR_ANS_PARCELABLE_FAILED; + } } return result; diff --git a/frameworks/ans/native/src/notification_sorting.cpp b/frameworks/ans/native/src/notification_sorting.cpp index 0ff27d780b079bb00ea7cfa9404d36a7b35d8109..fc390cfc0224fd99c11a29435392d421d2b3950d 100644 --- a/frameworks/ans/native/src/notification_sorting.cpp +++ b/frameworks/ans/native/src/notification_sorting.cpp @@ -39,6 +39,10 @@ NotificationSorting::NotificationSorting(const NotificationSorting &sorting) void NotificationSorting::SetSlot(const sptr &slot) { + if (slot == nullptr) { + slot_ = new NotificationSlot(NotificationConstant::SlotType::OTHER); + return; + } slot_ = slot; } diff --git a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp index e82c4e47e9cad6620a46a02d2401f375d3e88c71..a1ba734cdf57aff72cc350ba28e1de89c3c47787 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -58,8 +58,8 @@ const int32_t CASE_FIFTEEN = 15; const int32_t CASE_SIXTEEN = 16; const int32_t CALLING_UID = 9999; -const int32_t PIXEL_MAP_TEST_WIDTH = 1024; -const int32_t PIXEL_MAP_TEST_HEIGHT = 1024; +const int32_t PIXEL_MAP_TEST_WIDTH = 32; +const int32_t PIXEL_MAP_TEST_HEIGHT = 32; std::mutex g_subscribe_mtx; std::mutex g_consumed_mtx; @@ -190,9 +190,9 @@ private: EXPECT_EQ(outImageInfo.pixelFormat, Media::PixelFormat::ALPHA_8); EXPECT_EQ(outImageInfo.colorSpace, Media::ColorSpace::SRGB); - EXPECT_EQ(nullptr, notificationRequest.GetBigIcon()); - EXPECT_EQ(nullptr, notificationRequest.GetLittleIcon()); - EXPECT_EQ(nullptr, notificationRequest.GetLittleIcon()); + EXPECT_NE(nullptr, notificationRequest.GetBigIcon()); + EXPECT_NE(nullptr, notificationRequest.GetLittleIcon()); + EXPECT_NE(nullptr, notificationRequest.GetLittleIcon()); std::vector> messageUser = notificationRequest.GetMessageUsers(); for (auto user : messageUser) { if (user != nullptr) { diff --git a/interfaces/innerkits/ans/native/include/notification_sorting.h b/interfaces/innerkits/ans/native/include/notification_sorting.h index 4be3ba5b49bffb0943175e61a489ee84780aacfd..1efa272e597a02f54a3512e40fa39b93aa63bb85 100644 --- a/interfaces/innerkits/ans/native/include/notification_sorting.h +++ b/interfaces/innerkits/ans/native/include/notification_sorting.h @@ -175,7 +175,7 @@ private: bool isHiddenNotification_ {}; std::string groupKeyOverride_ {}; int32_t visiblenessOverride_ {}; - sptr slot_ {nullptr}; + sptr slot_ = new NotificationSlot(NotificationConstant::SlotType::OTHER); friend class AdvancedNotificationService; }; diff --git a/interfaces/kits/napi/ans/include/common.h b/interfaces/kits/napi/ans/include/common.h index ace3fefc313e264d2d6b7f4a59d386602b4de402..8cd32ef3aa549a898d1ddc18cc1e553604ba6f27 100644 --- a/interfaces/kits/napi/ans/include/common.h +++ b/interfaces/kits/napi/ans/include/common.h @@ -132,7 +132,8 @@ public: static void SetCallback( const napi_env &env, const napi_ref &callbackIn, const int &errorCode, const napi_value &result); - static void SetPromise(const napi_env &env, const napi_deferred &deferred, const napi_value &result); + static void SetPromise( + const napi_env &env, const napi_deferred &deferred, const int &errorCode, const napi_value &result); static napi_value JSParaError(const napi_env &env, const napi_ref &callback); diff --git a/interfaces/kits/napi/ans/src/cancel.cpp b/interfaces/kits/napi/ans/src/cancel.cpp index 7bc7871ffd069955e3264be9a8e70767b247e7d7..71ea420be9f92fd8b9f260852d8a32438d8626bf 100644 --- a/interfaces/kits/napi/ans/src/cancel.cpp +++ b/interfaces/kits/napi/ans/src/cancel.cpp @@ -102,7 +102,7 @@ napi_value Cancel(napi_env env, napi_callback_info info) ParametersInfoCancel paras; if (ParseParameters(env, info, paras) == nullptr) { - return Common::JSParaError(env, paras.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoCancel *asynccallbackinfo = new (std::nothrow) @@ -160,7 +160,7 @@ napi_value CancelAll(napi_env env, napi_callback_info info) napi_ref callback = nullptr; if (ParseParametersByCancelAll(env, info, callback) == nullptr) { - return Common::JSParaError(env, callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoCancel *asynccallbackinfo = diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index 45f69f2e8a33e14495e5cf8ced977de093cee4c2..650e0935ea1ba36fca49c9a41a71473d03abed49 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -138,7 +138,7 @@ void Common::ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInf if (info.isCallback) { SetCallback(env, info.callback, info.errorCode, result); } else { - SetPromise(env, info.deferred, result); + SetPromise(env, info.deferred, info.errorCode, result); } ANS_LOGI("ReturnCallbackPromise end"); } @@ -160,10 +160,15 @@ void Common::SetCallback( ANS_LOGI("end"); } -void Common::SetPromise(const napi_env &env, const napi_deferred &deferred, const napi_value &result) +void Common::SetPromise( + const napi_env &env, const napi_deferred &deferred, const int &errorCode, const napi_value &result) { ANS_LOGI("enter"); - napi_resolve_deferred(env, deferred, result); + if (errorCode == ERR_OK) { + napi_resolve_deferred(env, deferred, result); + } else { + napi_reject_deferred(env, deferred, GetCallbackErrorValue(env, errorCode)); + } ANS_LOGI("end"); } @@ -175,7 +180,7 @@ napi_value Common::JSParaError(const napi_env &env, const napi_ref &callback) napi_value promise = nullptr; napi_deferred deferred = nullptr; napi_create_promise(env, &deferred, &promise); - SetPromise(env, deferred, Common::NapiGetNull(env)); + SetPromise(env, deferred, ERROR, Common::NapiGetNull(env)); return promise; } } diff --git a/interfaces/kits/napi/ans/src/display_badge.cpp b/interfaces/kits/napi/ans/src/display_badge.cpp index ea42c40b0ceabdd8351341aee1defa767656dcd5..889fd7842deba7f8cb35af9e9a515133c523a7f1 100644 --- a/interfaces/kits/napi/ans/src/display_badge.cpp +++ b/interfaces/kits/napi/ans/src/display_badge.cpp @@ -129,7 +129,7 @@ napi_value DisplayBadge(napi_env env, napi_callback_info info) EnableBadgeParams params {}; if (ParseParameters(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoEnableBadge *asynccallbackinfo = @@ -194,7 +194,7 @@ napi_value IsBadgeDisplayed(napi_env env, napi_callback_info info) IsDisplayBadgeParams params {}; if (ParseParameters(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoIsDisplayBadge *asynccallbackinfo = diff --git a/interfaces/kits/napi/ans/src/disturb_mode.cpp b/interfaces/kits/napi/ans/src/disturb_mode.cpp index d2b88c530288ae23d1cb4f59247c58ccdfc90f13..5bb64c12cba66ee9c987c6c6b9c1077e79cb71b8 100644 --- a/interfaces/kits/napi/ans/src/disturb_mode.cpp +++ b/interfaces/kits/napi/ans/src/disturb_mode.cpp @@ -70,7 +70,7 @@ napi_value SetDoNotDisturbMode(napi_env env, napi_callback_info info) DisturbModeParams params {}; if (ParseParameters(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoDisturbMode *asynccallbackinfo = diff --git a/interfaces/kits/napi/ans/src/enable_notification.cpp b/interfaces/kits/napi/ans/src/enable_notification.cpp index aa8c2cdcd2350b067d49f544a3f67203c64b414b..b350df6c0a310bb2e048af081579f416bc7c0d4a 100644 --- a/interfaces/kits/napi/ans/src/enable_notification.cpp +++ b/interfaces/kits/napi/ans/src/enable_notification.cpp @@ -129,7 +129,7 @@ napi_value EnableNotification(napi_env env, napi_callback_info info) EnableParams params {}; if (ParseParameters(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoEnable *asynccallbackinfo = @@ -191,7 +191,7 @@ napi_value IsNotificationEnabled(napi_env env, napi_callback_info info) IsEnableParams params {}; if (ParseParameters(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoIsEnable *asynccallbackinfo = diff --git a/interfaces/kits/napi/ans/src/get_active.cpp b/interfaces/kits/napi/ans/src/get_active.cpp index 0d6ba8c4222b07703359bb523051a6e7e096650d..c424913327036a505782518472a41873d6b89c78 100644 --- a/interfaces/kits/napi/ans/src/get_active.cpp +++ b/interfaces/kits/napi/ans/src/get_active.cpp @@ -23,8 +23,10 @@ const int ACTIVE_OR_NUMS_MAX_PARA = 1; struct AsyncCallbackInfoActive { napi_env env = nullptr; napi_async_work asyncWork = nullptr; - napi_value result = nullptr; CallbackPromiseInfo info; + std::vector> notifications; + std::vector> requests; + int32_t num = 0; }; napi_value ParseParametersByAllActive(const napi_env &env, const napi_callback_info &info, napi_ref &callback) @@ -67,13 +69,65 @@ napi_value ParseParametersByGetActive(const napi_env &env, const napi_callback_i return Common::NapiGetNull(env); } +void AsyncCompleteCallbackGetAllActiveNotifications(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetAllActiveNotifications napi_create_async_work end"); + + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + AsyncCallbackInfoActive *asynccallbackinfo = (AsyncCallbackInfoActive *)data; + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + int count = 0; + napi_create_array(env, &arr); + for (auto vec : asynccallbackinfo->notifications) { + if (!vec) { + ANS_LOGW("Invalid Notification object ptr"); + continue; + } + napi_value notificationResult = nullptr; + napi_create_object(env, ¬ificationResult); + if (!Common::SetNotification(env, vec.GetRefPtr(), notificationResult)) { + ANS_LOGW("Set Notification object failed"); + continue; + } + napi_set_element(env, arr, count, notificationResult); + count++; + } + ANS_LOGI("GetAllActiveNotifications count = %{public}d", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->notifications.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result); + + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + if (asynccallbackinfo) { + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } +} + napi_value GetAllActiveNotifications(napi_env env, napi_callback_info info) { ANS_LOGI("enter"); napi_ref callback = nullptr; if (ParseParametersByAllActive(env, info, callback) == nullptr) { - return Common::JSParaError(env, callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoActive *asynccallbackinfo = @@ -95,52 +149,10 @@ napi_value GetAllActiveNotifications(napi_env env, napi_callback_info info) ANS_LOGI("GetAllActiveNotifications napi_create_async_work start"); AsyncCallbackInfoActive *asynccallbackinfo = (AsyncCallbackInfoActive *)data; - std::vector> notifications; - asynccallbackinfo->info.errorCode = NotificationHelper::GetAllActiveNotifications(notifications); - if (asynccallbackinfo->info.errorCode != ERR_OK) { - asynccallbackinfo->result = Common::NapiGetNull(env); - return; - } - napi_value arr = nullptr; - int count = 0; - napi_create_array(env, &arr); - for (auto vec : notifications) { - if (!vec) { - ANS_LOGW("Invalid Notification object ptr"); - continue; - } - napi_value result = nullptr; - napi_create_object(env, &result); - if (!Common::SetNotification(env, vec.GetRefPtr(), result)) { - ANS_LOGW("Set Notification object failed"); - continue; - } - napi_set_element(env, arr, count, result); - count++; - } - ANS_LOGI("GetAllActiveNotifications count = %{public}d", count); - asynccallbackinfo->result = arr; - if ((count == 0) && (notifications.size() > 0)) { - asynccallbackinfo->info.errorCode = ERROR; - asynccallbackinfo->result = Common::NapiGetNull(env); - } - }, - [](napi_env env, napi_status status, void *data) { - ANS_LOGI("GetAllActiveNotifications napi_create_async_work end"); - AsyncCallbackInfoActive *asynccallbackinfo = (AsyncCallbackInfoActive *)data; - - Common::ReturnCallbackPromise(env, asynccallbackinfo->info, asynccallbackinfo->result); - - if (asynccallbackinfo->info.callback != nullptr) { - napi_delete_reference(env, asynccallbackinfo->info.callback); - } - - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; - } + asynccallbackinfo->info.errorCode = + NotificationHelper::GetAllActiveNotifications(asynccallbackinfo->notifications); }, + AsyncCompleteCallbackGetAllActiveNotifications, (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); @@ -153,13 +165,64 @@ napi_value GetAllActiveNotifications(napi_env env, napi_callback_info info) } } +void AsyncCompleteCallbackGetActiveNotifications(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetActiveNotifications napi_create_async_work end"); + + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + AsyncCallbackInfoActive *asynccallbackinfo = (AsyncCallbackInfoActive *)data; + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + int count = 0; + napi_create_array(env, &arr); + for (auto vec : asynccallbackinfo->requests) { + if (!vec) { + ANS_LOGW("Invalid NotificationRequest object ptr"); + continue; + } + napi_value requestResult = nullptr; + napi_create_object(env, &requestResult); + if (!Common::SetNotificationRequest(env, vec.GetRefPtr(), requestResult)) { + ANS_LOGW("Set NotificationRequest object failed"); + continue; + } + napi_set_element(env, arr, count, requestResult); + count++; + } + ANS_LOGI("GetActiveNotifications count = %{public}d", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->requests.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result); + + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + if (asynccallbackinfo) { + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } +} + napi_value GetActiveNotifications(napi_env env, napi_callback_info info) { ANS_LOGI("enter"); napi_ref callback = nullptr; if (ParseParametersByGetActive(env, info, callback) == nullptr) { - return Common::JSParaError(env, callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoActive *asynccallbackinfo = @@ -181,52 +244,10 @@ napi_value GetActiveNotifications(napi_env env, napi_callback_info info) ANS_LOGI("GetActiveNotifications napi_create_async_work start"); AsyncCallbackInfoActive *asynccallbackinfo = (AsyncCallbackInfoActive *)data; - std::vector> requests; - asynccallbackinfo->info.errorCode = NotificationHelper::GetActiveNotifications(requests); - if (asynccallbackinfo->info.errorCode != ERR_OK) { - asynccallbackinfo->result = Common::NapiGetNull(env); - return; - } - napi_value arr = nullptr; - int count = 0; - napi_create_array(env, &arr); - for (auto vec : requests) { - if (!vec) { - ANS_LOGW("Invalid NotificationRequest object ptr"); - continue; - } - napi_value result = nullptr; - napi_create_object(env, &result); - if (!Common::SetNotificationRequest(env, vec.GetRefPtr(), result)) { - ANS_LOGW("Set NotificationRequest object failed"); - continue; - } - napi_set_element(env, arr, count, result); - count++; - } - ANS_LOGI("GetActiveNotifications count = %{public}d", count); - asynccallbackinfo->result = arr; - if ((count == 0) && (requests.size() > 0)) { - asynccallbackinfo->info.errorCode = ERROR; - asynccallbackinfo->result = Common::NapiGetNull(env); - } - }, - [](napi_env env, napi_status status, void *data) { - ANS_LOGI("GetActiveNotifications napi_create_async_work end"); - AsyncCallbackInfoActive *asynccallbackinfo = (AsyncCallbackInfoActive *)data; - - Common::ReturnCallbackPromise(env, asynccallbackinfo->info, asynccallbackinfo->result); - - if (asynccallbackinfo->info.callback != nullptr) { - napi_delete_reference(env, asynccallbackinfo->info.callback); - } - - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; - } + asynccallbackinfo->info.errorCode = + NotificationHelper::GetActiveNotifications(asynccallbackinfo->requests); }, + AsyncCompleteCallbackGetActiveNotifications, (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); @@ -239,13 +260,43 @@ napi_value GetActiveNotifications(napi_env env, napi_callback_info info) } } +void AsyncCompleteCallbackGetActiveNotificationCount(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetActiveNotificationCount napi_create_async_work end"); + + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + AsyncCallbackInfoActive *asynccallbackinfo = (AsyncCallbackInfoActive *)data; + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_create_int32(env, asynccallbackinfo->num, &result); + } + + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result); + + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + if (asynccallbackinfo) { + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } +} + napi_value GetActiveNotificationCount(napi_env env, napi_callback_info info) { ANS_LOGI("enter"); napi_ref callback = nullptr; if (ParseParametersByGetActive(env, info, callback) == nullptr) { - return Common::JSParaError(env, callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoActive *asynccallbackinfo = @@ -267,31 +318,10 @@ napi_value GetActiveNotificationCount(napi_env env, napi_callback_info info) ANS_LOGI("GetActiveNotificationCount napi_create_async_work start"); AsyncCallbackInfoActive *asynccallbackinfo = (AsyncCallbackInfoActive *)data; - int32_t num = 0; - asynccallbackinfo->info.errorCode = NotificationHelper::GetActiveNotificationNums(num); - ANS_LOGI("GetActiveNotificationCount count = %{public}d", num); - if (asynccallbackinfo->info.errorCode != ERR_OK) { - asynccallbackinfo->result = Common::NapiGetNull(env); - return; - } - napi_create_int32(env, num, &asynccallbackinfo->result); - }, - [](napi_env env, napi_status status, void *data) { - ANS_LOGI("GetActiveNotificationCount napi_create_async_work end"); - AsyncCallbackInfoActive *asynccallbackinfo = (AsyncCallbackInfoActive *)data; - - Common::ReturnCallbackPromise(env, asynccallbackinfo->info, asynccallbackinfo->result); - - if (asynccallbackinfo->info.callback != nullptr) { - napi_delete_reference(env, asynccallbackinfo->info.callback); - } - - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; - } + asynccallbackinfo->info.errorCode = NotificationHelper::GetActiveNotificationNums(asynccallbackinfo->num); + ANS_LOGI("GetActiveNotificationCount count = %{public}d", asynccallbackinfo->num); }, + AsyncCompleteCallbackGetActiveNotificationCount, (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); diff --git a/interfaces/kits/napi/ans/src/publish.cpp b/interfaces/kits/napi/ans/src/publish.cpp index 3c860cab93386e6461945300a1dadb939456d6a0..7bcaf88c2d01a5bad50f3d926230e419fcf475e5 100644 --- a/interfaces/kits/napi/ans/src/publish.cpp +++ b/interfaces/kits/napi/ans/src/publish.cpp @@ -83,7 +83,7 @@ napi_value Publish(napi_env env, napi_callback_info info) ParametersInfoPublish params; if (ParseParameters(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } napi_value promise = nullptr; diff --git a/interfaces/kits/napi/ans/src/remove.cpp b/interfaces/kits/napi/ans/src/remove.cpp index 59b9a3dae34736a55b4e0f660549ae5170ede2d6..90c71f2f2cb521612b05eb5fb290c2cdfc75d9c5 100644 --- a/interfaces/kits/napi/ans/src/remove.cpp +++ b/interfaces/kits/napi/ans/src/remove.cpp @@ -151,7 +151,7 @@ napi_value Remove(napi_env env, napi_callback_info info) RemoveParams params {}; if (ParseParameters(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoRemove *asynccallbackinfo = @@ -217,7 +217,7 @@ napi_value RemoveAll(napi_env env, napi_callback_info info) RemoveParams params {}; if (ParseParametersByRemoveAll(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoRemove *asynccallbackinfo = diff --git a/interfaces/kits/napi/ans/src/slot.cpp b/interfaces/kits/napi/ans/src/slot.cpp index 3764f5380b0a30d434eef2789dac27f79c1ff48f..4590c92ee27caa823d333eaee768b71750995188 100644 --- a/interfaces/kits/napi/ans/src/slot.cpp +++ b/interfaces/kits/napi/ans/src/slot.cpp @@ -77,8 +77,8 @@ struct AsyncCallbackInfoGetSlot { napi_env env = nullptr; napi_async_work asyncWork = nullptr; enum NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER; - napi_value result = nullptr; CallbackPromiseInfo info; + sptr slot = nullptr; }; struct ParametersInfoGetSlotNumByBundle { @@ -89,16 +89,16 @@ struct ParametersInfoGetSlotNumByBundle { struct AsyncCallbackInfoGetSlotNumByBundle { napi_env env = nullptr; napi_async_work asyncWork = nullptr; - napi_value result = nullptr; ParametersInfoGetSlotNumByBundle params; CallbackPromiseInfo info; + int num = 0; }; struct AsyncCallbackInfoGetSlots { napi_env env = nullptr; napi_async_work asyncWork = nullptr; - napi_value result = nullptr; CallbackPromiseInfo info; + std::vector> slots; }; struct ParametersInfoGetSlotsByBundle { @@ -109,9 +109,9 @@ struct ParametersInfoGetSlotsByBundle { struct AsyncCallbackInfoGetSlotsByBundle { napi_env env = nullptr; napi_async_work asyncWork = nullptr; - napi_value result = nullptr; ParametersInfoGetSlotsByBundle params; CallbackPromiseInfo info; + std::vector> slots; }; struct ParametersInfoRemoveSlot { @@ -418,7 +418,7 @@ napi_value AddSlot(napi_env env, napi_callback_info info) ParametersInfoAddSlot paras; if (ParseParametersByAddSlot(env, info, paras) == nullptr) { - return Common::JSParaError(env, paras.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoAddSlot *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoAddSlot { @@ -483,7 +483,7 @@ napi_value AddSlots(napi_env env, napi_callback_info info) ParametersInfoAddSlots paras; if (ParseParametersByAddSlots(env, info, paras) == nullptr) { - return Common::JSParaError(env, paras.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoAddSlots *asynccallbackinfo = @@ -539,7 +539,7 @@ napi_value SetSlotByBundle(napi_env env, napi_callback_info info) ParametersInfoSetSlotByBundle params {}; if (ParseParametersSetSlotByBundle(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoSetSlotByBundle *asynccallbackinfo = @@ -594,13 +594,49 @@ napi_value SetSlotByBundle(napi_env env, napi_callback_info info) } } + +void AsyncCompleteCallbackGetSlot(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetSlot napi_create_async_work end"); + + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + AsyncCallbackInfoGetSlot *asynccallbackinfo = (AsyncCallbackInfoGetSlot *)data; + napi_value result = Common::NapiGetNull(env); + if (asynccallbackinfo->info.errorCode == ERR_OK) { + if (asynccallbackinfo->slot == nullptr) { + asynccallbackinfo->info.errorCode = ERROR; + } else { + napi_create_object(env, &result); + if (!Common::SetNotificationSlot(env, *asynccallbackinfo->slot, result)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + } + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result); + + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + if (asynccallbackinfo) { + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } +} + napi_value GetSlot(napi_env env, napi_callback_info info) { ANS_LOGI("enter"); ParametersInfoGetSlot paras; if (ParseParametersByGetSlot(env, info, paras) == nullptr) { - return Common::JSParaError(env, paras.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoGetSlot *asynccallbackinfo = @@ -621,39 +657,10 @@ napi_value GetSlot(napi_env env, napi_callback_info info) ANS_LOGI("GetSlot napi_create_async_work start"); AsyncCallbackInfoGetSlot *asynccallbackinfo = (AsyncCallbackInfoGetSlot *)data; - sptr slot = nullptr; asynccallbackinfo->info.errorCode = - NotificationHelper::GetNotificationSlot(asynccallbackinfo->outType, slot); - asynccallbackinfo->result = Common::NapiGetNull(env); - if (asynccallbackinfo->info.errorCode != ERR_OK) { - return; - } - if (slot == nullptr) { - asynccallbackinfo->info.errorCode = ERROR; - return; - } - napi_create_object(env, &asynccallbackinfo->result); - if (!Common::SetNotificationSlot(env, *slot, asynccallbackinfo->result)) { - asynccallbackinfo->info.errorCode = ERROR; - asynccallbackinfo->result = Common::NapiGetNull(env); - } - }, - [](napi_env env, napi_status status, void *data) { - ANS_LOGI("GetSlot napi_create_async_work end"); - AsyncCallbackInfoGetSlot *asynccallbackinfo = (AsyncCallbackInfoGetSlot *)data; - - Common::ReturnCallbackPromise(env, asynccallbackinfo->info, asynccallbackinfo->result); - - if (asynccallbackinfo->info.callback != nullptr) { - napi_delete_reference(env, asynccallbackinfo->info.callback); - } - - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; - } + NotificationHelper::GetNotificationSlot(asynccallbackinfo->outType, asynccallbackinfo->slot); }, + AsyncCompleteCallbackGetSlot, (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); @@ -672,7 +679,7 @@ napi_value GetSlotNumByBundle(napi_env env, napi_callback_info info) ParametersInfoGetSlotNumByBundle params {}; if (ParseParametersGetSlotNumByBundle(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoGetSlotNumByBundle *asynccallbackinfo = @@ -696,15 +703,16 @@ napi_value GetSlotNumByBundle(napi_env env, napi_callback_info info) NotificationBundleOption bundleOption; bundleOption.SetBundleName(asynccallbackinfo->params.option.bundle); bundleOption.SetUid(asynccallbackinfo->params.option.uid); - int num = 0; - asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSlotNumAsBundle(bundleOption, num); - napi_create_int32(env, num, &asynccallbackinfo->result); + asynccallbackinfo->info.errorCode = + NotificationHelper::GetNotificationSlotNumAsBundle(bundleOption, asynccallbackinfo->num); + }, [](napi_env env, napi_status status, void *data) { ANS_LOGI("GetSlotNumByBundle napi_create_async_work end"); AsyncCallbackInfoGetSlotNumByBundle *asynccallbackinfo = (AsyncCallbackInfoGetSlotNumByBundle *)data; - - Common::ReturnCallbackPromise(env, asynccallbackinfo->info, asynccallbackinfo->result); + napi_value result = nullptr; + napi_create_int32(env, asynccallbackinfo->num, &result); + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result); if (asynccallbackinfo->info.callback != nullptr) { napi_delete_reference(env, asynccallbackinfo->info.callback); @@ -734,7 +742,7 @@ napi_value GetSlots(napi_env env, napi_callback_info info) napi_ref callback = nullptr; if (ParseParametersByGetSlots(env, info, callback) == nullptr) { - return Common::JSParaError(env, callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoGetSlots *asynccallbackinfo = @@ -754,42 +762,39 @@ napi_value GetSlots(napi_env env, napi_callback_info info) [](napi_env env, void *data) { ANS_LOGI("GetSlots napi_create_async_work start"); AsyncCallbackInfoGetSlots *asynccallbackinfo = (AsyncCallbackInfoGetSlots *)data; - - std::vector> slots; - asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSlots(slots); - if (asynccallbackinfo->info.errorCode != ERR_OK) { - asynccallbackinfo->result = Common::NapiGetNull(env); - return; - } - - napi_value arr = nullptr; - napi_create_array(env, &arr); - size_t count = 0; - for (auto vec : slots) { - if (!vec) { - ANS_LOGW("Invalid NotificationSlot object ptr"); - continue; - } - napi_value nSlot = nullptr; - napi_create_object(env, &nSlot); - if (!Common::SetNotificationSlot(env, *vec, nSlot)) { - continue; - } - napi_set_element(env, arr, count, nSlot); - count++; - } - ANS_LOGI("getSlots count = %{public}zu", count); - asynccallbackinfo->result = arr; - if ((count == 0) && (slots.size() > 0)) { - asynccallbackinfo->info.errorCode = ERROR; - asynccallbackinfo->result = Common::NapiGetNull(env); - } + asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSlots(asynccallbackinfo->slots); }, [](napi_env env, napi_status status, void *data) { ANS_LOGI("GetSlots napi_create_async_work end"); AsyncCallbackInfoGetSlots *asynccallbackinfo = (AsyncCallbackInfoGetSlots *)data; - - Common::ReturnCallbackPromise(env, asynccallbackinfo->info, asynccallbackinfo->result); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + napi_create_array(env, &arr); + size_t count = 0; + for (auto vec : asynccallbackinfo->slots) { + if (!vec) { + ANS_LOGW("Invalid NotificationSlot object ptr"); + continue; + } + napi_value nSlot = nullptr; + napi_create_object(env, &nSlot); + if (!Common::SetNotificationSlot(env, *vec, nSlot)) { + continue; + } + napi_set_element(env, arr, count, nSlot); + count++; + } + ANS_LOGI("getSlots count = %{public}zu", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->slots.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result); if (asynccallbackinfo->info.callback != nullptr) { napi_delete_reference(env, asynccallbackinfo->info.callback); @@ -819,7 +824,7 @@ napi_value GetSlotsByBundle(napi_env env, napi_callback_info info) ParametersInfoGetSlotsByBundle params {}; if (ParseParametersGetSlotsByBundle(env, info, params) == nullptr) { - return Common::JSParaError(env, params.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoGetSlotsByBundle *asynccallbackinfo = @@ -843,42 +848,41 @@ napi_value GetSlotsByBundle(napi_env env, napi_callback_info info) NotificationBundleOption bundleOption; bundleOption.SetBundleName(asynccallbackinfo->params.option.bundle); bundleOption.SetUid(asynccallbackinfo->params.option.uid); - - std::vector> slots; - asynccallbackinfo->info.errorCode = NotificationHelper::GetNotificationSlotsForBundle(bundleOption, slots); - if (asynccallbackinfo->info.errorCode != ERR_OK) { - asynccallbackinfo->result = Common::NapiGetNull(env); - return; - } - - napi_value arr = nullptr; - napi_create_array(env, &arr); - size_t count = 0; - for (auto vec : slots) { - if (!vec) { - ANS_LOGW("Invalid NotificationSlot object ptr"); - continue; - } - napi_value nSlot = nullptr; - napi_create_object(env, &nSlot); - if (!Common::SetNotificationSlot(env, *vec, nSlot)) { - continue; - } - napi_set_element(env, arr, count, nSlot); - count++; - } - ANS_LOGI("getSlots count = %{public}zu", count); - asynccallbackinfo->result = arr; - if ((count == 0) && (slots.size() > 0)) { - asynccallbackinfo->info.errorCode = ERROR; - asynccallbackinfo->result = Common::NapiGetNull(env); - } + asynccallbackinfo->info.errorCode = + NotificationHelper::GetNotificationSlotsForBundle(bundleOption, asynccallbackinfo->slots); }, [](napi_env env, napi_status status, void *data) { ANS_LOGI("GetSlotsByBundle napi_create_async_work end"); AsyncCallbackInfoGetSlotsByBundle *asynccallbackinfo = (AsyncCallbackInfoGetSlotsByBundle *)data; - Common::ReturnCallbackPromise(env, asynccallbackinfo->info, asynccallbackinfo->result); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + napi_create_array(env, &arr); + size_t count = 0; + for (auto vec : asynccallbackinfo->slots) { + if (!vec) { + ANS_LOGW("Invalid NotificationSlot object ptr"); + continue; + } + napi_value nSlot = nullptr; + napi_create_object(env, &nSlot); + if (!Common::SetNotificationSlot(env, *vec, nSlot)) { + continue; + } + napi_set_element(env, arr, count, nSlot); + count++; + } + ANS_LOGI("getSlots count = %{public}zu", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->slots.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result); if (asynccallbackinfo->info.callback != nullptr) { napi_delete_reference(env, asynccallbackinfo->info.callback); @@ -908,7 +912,7 @@ napi_value RemoveSlot(napi_env env, napi_callback_info info) ParametersInfoRemoveSlot paras; if (ParseParametersByRemoveSlot(env, info, paras) == nullptr) { - return Common::JSParaError(env, paras.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoRemoveSlot *asynccallbackinfo = @@ -964,7 +968,7 @@ napi_value RemoveAllSlots(napi_env env, napi_callback_info info) napi_ref callback = nullptr; if (ParseParametersByRemoveAllSlots(env, info, callback) == nullptr) { - return Common::JSParaError(env, callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoRemoveAllSlots *asynccallbackinfo = diff --git a/interfaces/kits/napi/ans/src/subscribe.cpp b/interfaces/kits/napi/ans/src/subscribe.cpp index 74d6671e8008b8f0f0b6e3a4e0cfe58da3d5783a..4edb90d995aef25473cef75ca58a3dbe77b345e4 100644 --- a/interfaces/kits/napi/ans/src/subscribe.cpp +++ b/interfaces/kits/napi/ans/src/subscribe.cpp @@ -927,7 +927,7 @@ napi_value Subscribe(napi_env env, napi_callback_info info) SubscriberInstance *objectInfo = nullptr; NotificationSubscribeInfo subscriberInfo; if (ParseParameters(env, info, subscriberInfo, objectInfo, callback) == nullptr) { - return Common::JSParaError(env, callback); + return Common::NapiGetUndefined(env); } ANS_LOGI("Subscribe objectInfo = %{public}p", objectInfo); diff --git a/interfaces/kits/napi/ans/src/unsubscribe.cpp b/interfaces/kits/napi/ans/src/unsubscribe.cpp index 7e2b9bc384a268e090fd9b8930accd1db5dc4766..a8b188eeb6b89721c214b3d63a916ee5b17339e3 100644 --- a/interfaces/kits/napi/ans/src/unsubscribe.cpp +++ b/interfaces/kits/napi/ans/src/unsubscribe.cpp @@ -16,6 +16,8 @@ #include "unsubscribe.h" #include "subscribe.h" +#include "ans_inner_errors.h" + namespace OHOS { namespace NotificationNapi { const int UNSUBSCRIBE_MAX_PARA = 2; @@ -48,8 +50,9 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, SubscriberInstancesInfo subscriberInstancesInfo; if (!HasNotificationSubscriber(env, argv[0], subscriberInstancesInfo)) { - return nullptr; + ANS_LOGW("Subscriber not found"); } + paras.objectInfo = subscriberInstancesInfo.subscriber; ANS_LOGI("ObjectInfo = %{public}p start", paras.objectInfo); @@ -69,7 +72,7 @@ napi_value Unsubscribe(napi_env env, napi_callback_info info) ParametersInfoUnsubscribe paras; if (ParseParameters(env, info, paras) == nullptr) { - return Common::JSParaError(env, paras.callback); + return Common::NapiGetUndefined(env); } AsyncCallbackInfoUnsubscribe *asynccallbackinfo = new (std::nothrow) @@ -91,6 +94,12 @@ napi_value Unsubscribe(napi_env env, napi_callback_info info) ANS_LOGI("Unsubscribe napi_create_async_work start"); AsyncCallbackInfoUnsubscribe *asynccallbackinfo = (AsyncCallbackInfoUnsubscribe *)data; + if (asynccallbackinfo->objectInfo == nullptr) { + ANS_LOGE("invalid object info"); + asynccallbackinfo->info.errorCode = ERR_ANS_INVALID_PARAM; + return; + } + asynccallbackinfo->info.errorCode = NotificationHelper::UnSubscribeNotification(*(asynccallbackinfo->objectInfo)); }, diff --git a/interfaces/kits/napi/wantagent/napi_want_agent.cpp b/interfaces/kits/napi/wantagent/napi_want_agent.cpp index aeb4f5fc151ddcd2a100662b8d94f1ae8a0f31e0..bce26d68216dcb7befb72aa310ff2ebf1abbedab 100644 --- a/interfaces/kits/napi/wantagent/napi_want_agent.cpp +++ b/interfaces/kits/napi/wantagent/napi_want_agent.cpp @@ -592,11 +592,11 @@ napi_value NAPI_GetWant(napi_env env, napi_callback_info info) void DeleteRecordByCode(const int32_t code) { std::lock_guard guard(g_mutex); - for (const auto &item : g_WantAgentMap) { + for (const auto &item : *g_WantAgentMap) { auto code_ = item.second; auto record = item.first; if (code_ == code) { - g_WantAgentMap.erase(record); + g_WantAgentMap->erase(record); if (record != nullptr) { delete record; record = nullptr; @@ -608,9 +608,7 @@ void DeleteRecordByCode(const int32_t code) auto NAPI_CancelWrapExecuteCallBack = [](napi_env env, void *data) { HILOG_INFO("Cancel called(CallBack Mode)..."); AsyncCancelCallbackInfo *asyncCallbackInfo = static_cast(data); - int32_t code = WantAgentHelper::GetHashCode(asyncCallbackInfo->wantAgent); WantAgentHelper::Cancel(asyncCallbackInfo->wantAgent); - DeleteRecordByCode(code); }; auto NAPI_CancelWrapCompleteCallBack = [](napi_env env, napi_status status, void *data) { @@ -1033,7 +1031,7 @@ auto NAPI_GetWantAgentWrapExecuteCallBack = [](napi_env env, void *data) { } int32_t code = Notification::WantAgent::WantAgentHelper::GetHashCode(asyncCallbackInfo->wantAgent); std::lock_guard guard(g_mutex); - g_WantAgentMap.emplace(asyncCallbackInfo, code); + g_WantAgentMap->emplace(asyncCallbackInfo, code); }; auto NAPI_GetWantAgentWrapCompleteCallBack = [](napi_env env, napi_status status, void *data) { diff --git a/interfaces/kits/napi/wantagent/napi_want_agent.h b/interfaces/kits/napi/wantagent/napi_want_agent.h index 9b92bc02b16d7f83fdea0e1f6974549afaa7deae..e79f2e93d5edde266d6865783350ec782ddee7ca 100644 --- a/interfaces/kits/napi/wantagent/napi_want_agent.h +++ b/interfaces/kits/napi/wantagent/napi_want_agent.h @@ -179,7 +179,21 @@ napi_value GetCallbackErrorResult(napi_env env, int errCode); napi_value NapiGetNull(napi_env env); void DeleteRecordByCode(const int32_t code); -static std::map g_WantAgentMap; +static std::unique_ptr, + std::function *)>> + g_WantAgentMap(new std::map, + [](std::map *map) { + if (map == nullptr) { + return; + } + for (auto &item : *map) { + if (item.first != nullptr) { + delete item.first; + } + } + map->clear(); + delete map; + }); static std::recursive_mutex g_mutex; } // namespace OHOS #endif // NAPI_WANT_AGENT_H diff --git a/sa_profile/3203.xml b/sa_profile/3203.xml index 7f45c1fc08c584b6ff62c2b12119baacbd64c0e1..ccaa7c4fbe92e073c425e04ecd56b914ac888b9a 100755 --- a/sa_profile/3203.xml +++ b/sa_profile/3203.xml @@ -17,6 +17,8 @@ 3203 /system/lib/libans.z.so + 3299 + 5000 true false 1 diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 875112f77e009f61afabe4f21fd5427ad87944db..153e6bc7247116b5c482d596162835dc5a583dd3 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -502,8 +502,13 @@ ErrCode AdvancedNotificationService::GetSlots(std::vector } ErrCode result = ERR_OK; - handler_->PostSyncTask(std::bind( - [&]() { result = NotificationPreferences::GetInstance().GetNotificationAllSlots(bundleOption, slots); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().GetNotificationAllSlots(bundleOption, slots); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + slots.clear(); + } + })); return result; } @@ -519,6 +524,9 @@ ErrCode AdvancedNotificationService::GetSlotGroup(const std::string &groupId, sp ErrCode result = ERR_OK; handler_->PostSyncTask(std::bind([&]() { result = NotificationPreferences::GetInstance().GetNotificationSlotGroup(bundleOption, groupId, group); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_NOT_EXIST; + } })); return result; } @@ -533,8 +541,13 @@ ErrCode AdvancedNotificationService::GetSlotGroups(std::vectorPostSyncTask(std::bind( - [&]() { result = NotificationPreferences::GetInstance().GetNotificationAllSlotGroups(bundleOption, groups); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().GetNotificationAllSlotGroups(bundleOption, groups); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + groups.clear(); + } + })); return result; } @@ -548,6 +561,9 @@ ErrCode AdvancedNotificationService::RemoveSlotGroups(const std::vectorPostSyncTask(std::bind([&]() { result = NotificationPreferences::GetInstance().RemoveNotificationSlotGroups(bundleOption, groupIds); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_ID_INVALID; + } })); return result; } @@ -709,6 +725,10 @@ ErrCode AdvancedNotificationService::GetPrivateNotificationsAllowed(bool &allow) ErrCode result = ERR_OK; handler_->PostSyncTask(std::bind([&]() { result = NotificationPreferences::GetInstance().GetPrivateNotificationsAllowed(bundleOption, allow); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + allow = false; + } })); return result; } @@ -843,8 +863,13 @@ ErrCode AdvancedNotificationService::GetSlotsByBundle( } ErrCode result = ERR_OK; - handler_->PostSyncTask( - std::bind([&]() { result = NotificationPreferences::GetInstance().GetNotificationAllSlots(bundle, slots); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().GetNotificationAllSlots(bundle, slots); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + slots.clear(); + } + })); return result; } @@ -863,8 +888,12 @@ ErrCode AdvancedNotificationService::UpdateSlots( } ErrCode result = ERR_OK; - handler_->PostSyncTask( - std::bind([&]() { result = NotificationPreferences::GetInstance().UpdateNotificationSlots(bundle, slots); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().UpdateNotificationSlots(bundle, slots); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST; + } + })); return result; } @@ -883,8 +912,12 @@ ErrCode AdvancedNotificationService::UpdateSlotGroups( } ErrCode result = ERR_OK; - handler_->PostSyncTask(std::bind( - [&]() { result = NotificationPreferences::GetInstance().UpdateNotificationSlotGroups(bundle, groups); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().UpdateNotificationSlotGroups(bundle, groups); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_NOT_EXIST; + } + })); return result; } @@ -943,8 +976,13 @@ ErrCode AdvancedNotificationService::GetShowBadgeEnabled(bool &enabled) } ErrCode result = ERR_OK; - handler_->PostSyncTask( - std::bind([&]() { result = NotificationPreferences::GetInstance().IsShowBadge(bundleOption, enabled); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().IsShowBadge(bundleOption, enabled); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + enabled = false; + } + })); return result; } @@ -1029,8 +1067,12 @@ ErrCode AdvancedNotificationService::GetSlotByType( } ErrCode result = ERR_OK; - handler_->PostSyncTask(std::bind( - [&]() { result = NotificationPreferences::GetInstance().GetNotificationSlot(bundleOption, slotType, slot); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().GetNotificationSlot(bundleOption, slotType, slot); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST; + } + })); return result; } @@ -1044,8 +1086,12 @@ ErrCode AdvancedNotificationService::RemoveSlotByType(const NotificationConstant } ErrCode result = ERR_OK; - handler_->PostSyncTask(std::bind( - [&]() { result = NotificationPreferences::GetInstance().RemoveNotificationSlot(bundleOption, slotType); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().RemoveNotificationSlot(bundleOption, slotType); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST; + } + })); return result; } @@ -1200,6 +1246,10 @@ ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( result = NotificationPreferences::GetInstance().GetNotificationsEnabled(allowed); if (result == ERR_OK && allowed) { result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(targetBundle, allowed); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + allowed = true; + } } })); return result; @@ -1403,10 +1453,10 @@ void AdvancedNotificationService::OnBundleRemoved(const sptrPostTask(std::bind([&]() { + handler_->PostTask(std::bind([bundleOption]() { ErrCode result = NotificationPreferences::GetInstance().RemoveNotificationForBundle(bundleOption); if (result != ERR_OK) { - ANS_LOGE("NotificationPreferences::RemoveNotificationForBundle failed: %{public}d", result); + ANS_LOGW("NotificationPreferences::RemoveNotificationForBundle failed: %{public}d", result); } })); } @@ -1428,8 +1478,12 @@ ErrCode AdvancedNotificationService::RemoveAllSlots() } ErrCode result = ERR_OK; - handler_->PostSyncTask( - std::bind([&]() { result = NotificationPreferences::GetInstance().RemoveNotificationAllSlots(bundleOption); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().RemoveNotificationAllSlots(bundleOption); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + } + })); return result; } @@ -1555,8 +1609,13 @@ ErrCode AdvancedNotificationService::GetSlotNumAsBundle(const sptrPostSyncTask(std::bind( - [&]() { result = NotificationPreferences::GetInstance().GetNotificationSlotsNumForBundle(bundle, num); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().GetNotificationSlotsNumForBundle(bundle, num); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + num = 0; + } + })); return result; } diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index dc3ebcb9b7e32ff62def86d7468aab6312d15eff..a95b1fcc5771c4cd458655b148c9382d9426fab2 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -295,7 +295,7 @@ ErrCode NotificationPreferences::GetNotificationAllSlots( if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) { bundleInfo.GetAllSlots(slots); } else { - ANS_LOGE("Notification bundle does not exsit."); + ANS_LOGW("Notification bundle does not exsit."); result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST; } @@ -333,7 +333,7 @@ ErrCode NotificationPreferences::GetNotificationSlotGroup( result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_NOT_EXIST; } } else { - ANS_LOGE("Notification bundle does not exsit."); + ANS_LOGW("Notification bundle does not exsit."); result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST; } return result; @@ -351,7 +351,7 @@ ErrCode NotificationPreferences::GetNotificationAllSlotGroups( if (preferencesInfo_.GetBundleInfo(bundleOption, bundleInfo)) { bundleInfo.GetAllGroups(groups); } else { - ANS_LOGE("Notification bundle does not exsit."); + ANS_LOGW("Notification bundle does not exsit."); result = ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST; } return result; @@ -369,7 +369,7 @@ ErrCode NotificationPreferences::GetNotificationAllSlotInSlotGroup(const sptr> &slots) { + slots.clear(); std::for_each(slots_.begin(), slots_.end(), [&slots](std::map>::reference iter) { diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index f79dc6a38fbb6d3ca24a017f98a34403f8eb23e4..b54d4b57f5205b39505d2ce2827c520cd2020e6e 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -1251,7 +1251,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_09800, bool allowed = true; EXPECT_EQ((int)advancedNotificationService_->IsSpecialBundleAllowedNotify( new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), allowed), - (int)ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST); + (int)ERR_OK); } /** @@ -1264,7 +1264,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_09900, std::vector> slots; EXPECT_EQ((int)advancedNotificationService_->GetSlotsByBundle( new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID), slots), - (int)ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST); + (int)ERR_OK); } inline std::shared_ptr MakePixelMap(int32_t width, int32_t height) diff --git a/services/test/moduletest/ans_module_test.cpp b/services/test/moduletest/ans_module_test.cpp index 3ad7baf332ca2bc922f983e884500ff1b2278476..52d333eb065fdc448089b30c0b68af9398b64d14 100644 --- a/services/test/moduletest/ans_module_test.cpp +++ b/services/test/moduletest/ans_module_test.cpp @@ -96,7 +96,7 @@ void AnsModuleTest::SetUpTestCase() void AnsModuleTest::TearDownTestCase() { passed = false; - g_advancedNotificationService = new AdvancedNotificationService(); + g_advancedNotificationService = nullptr; NotificationPreferences::GetInstance().ClearNotificationInRestoreFactorySettings(); } @@ -109,7 +109,7 @@ void AnsModuleTest::SetUp() void AnsModuleTest::TearDown() { - sptr g_advancedNotificationService = nullptr; + g_advancedNotificationService = nullptr; NotificationPreferences::GetInstance().ClearNotificationInRestoreFactorySettings(); passed = false; } @@ -746,14 +746,14 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0033, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); } /** * @tc.number : AnsModuleTest_0034 * @tc.name : AMS_ANS_GetActiveNotifications_03400 - * @tc.desc : Test publish notification when slot type is CONTENT_INFORMATION. + * @tc.desc : Test publish notification when slot type is SOCIAL_COMMUNICATION. */ HWTEST_F(AnsModuleTest, AnsModuleTest_0034, Function | SmallTest | Level1) { @@ -773,6 +773,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0034, Function | SmallTest | Level1) std::vector> slots; sptr slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); slot->SetEnableVibration(true); + slot->SetSound(Uri("/sound/test.mp3")); slots.push_back(slot); g_advancedNotificationService->AddSlots(slots); @@ -1116,14 +1117,14 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0051, Function | SmallTest | Level1) std::vector> slotsRef{}; g_advancedNotificationService->GetSlots(slotsRef); - EXPECT_EQ(5, static_cast(slotsRef.size())); + EXPECT_EQ(1, static_cast(slotsRef.size())); std::vector slotsId{}; for (const auto &i : slotsRef) { slotsId.push_back(i->GetId()); } g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); g_advancedNotificationService->GetSlots(slotsRef); - EXPECT_EQ(9, static_cast(slotsRef.size())); + EXPECT_EQ(0, static_cast(slotsRef.size())); } /** @@ -1148,7 +1149,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0052, Function | SmallTest | Level1) g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); g_advancedNotificationService->RemoveSlotByType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); g_advancedNotificationService->GetSlots(slotsRef); - EXPECT_EQ(9, static_cast(slotsRef.size())); + EXPECT_EQ(0, static_cast(slotsRef.size())); } /** @@ -1258,8 +1259,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0058, Function | SmallTest | Level1) req->SetBadgeNumber(1); // SetShowBadgeEnabledForBundle true - sptr bundleOption; - bundleOption->SetBundleName("bundleName"); + sptr bundleOption = new NotificationBundleOption("bundleName", 0); g_advancedNotificationService->SetShowBadgeEnabledForBundle(bundleOption, true); g_advancedNotificationService->Publish(label, req); @@ -1288,7 +1288,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0060, Function | SmallTest | Level1) sptr slot = new NotificationSlot(); slot->EnableBadge(true); slots.push_back(slot); - EXPECT_EQ(g_advancedNotificationService->AddSlots(slots), 0); + EXPECT_EQ(g_advancedNotificationService->AddSlots(slots), ERR_OK); // create request std::string label = "testLabel"; @@ -1297,15 +1297,20 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0060, Function | SmallTest | Level1) req->SetStatusBarText("text"); req->SetBadgeNumber(1); + sptr bundleOption = new NotificationBundleOption("bundleName", 1); + ASSERT_NE(bundleOption, nullptr); + // not allow publish notification - g_advancedNotificationService->SetNotificationsEnabledForBundle("bundleName", false); + g_advancedNotificationService->SetNotificationsEnabledForSpecialBundle("", bundleOption, false); g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(false, passed); + EXPECT_FALSE(passed); + passed = false; // allow publish - g_advancedNotificationService->SetNotificationsEnabledForBundle("bundleName", true); + g_advancedNotificationService->SetNotificationsEnabledForSpecialBundle("", bundleOption, true); g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(true, passed); + EXPECT_TRUE(passed); + g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); } @@ -1339,16 +1344,20 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0061, Function | SmallTest | Level1) req->SetStatusBarText("text"); req->SetBadgeNumber(1); + sptr bundleOption = new NotificationBundleOption("bundleName", 1); + ASSERT_NE(bundleOption, nullptr); + // allow publish notification - g_advancedNotificationService->SetNotificationsEnabledForBundle("bundleName", true); + g_advancedNotificationService->SetNotificationsEnabledForSpecialBundle("", bundleOption, true); g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(true, passed); + EXPECT_TRUE(passed); // not allow publish passed = false; - g_advancedNotificationService->SetNotificationsEnabledForBundle("bundleName", false); + g_advancedNotificationService->SetNotificationsEnabledForSpecialBundle("", bundleOption, false); g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(false, passed); + EXPECT_FALSE(passed); + g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); } @@ -1395,7 +1404,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0062, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); g_advancedNotificationService->Publish(label, req1); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); } @@ -1440,7 +1449,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0063, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); g_advancedNotificationService->Publish(label, req1); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); } @@ -1500,7 +1509,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0066, Function | SmallTest | Level1) // remove request g_advancedNotificationService->Delete("1_testLabel_0"); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); } @@ -1543,7 +1552,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0100, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(passed, false); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); } @@ -1574,7 +1583,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0101, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(passed, false); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); } @@ -1605,7 +1614,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0102, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(passed, false); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); } @@ -1731,7 +1740,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0106, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(passed, false); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); } @@ -1977,7 +1986,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0113, Function | SmallTest | Level1) g_advancedNotificationService->Publish(label, req); g_advancedNotificationService->Publish(label, req1); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); } /** @@ -2036,7 +2045,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0114, Function | SmallTest | Level1) g_advancedNotificationService->Publish(label, req); g_advancedNotificationService->Publish(label, req1); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); } /** @@ -2095,7 +2104,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0116, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); g_advancedNotificationService->Publish(label, req1); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); } @@ -2155,7 +2164,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0117, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); g_advancedNotificationService->Publish(label, req1); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); } @@ -2357,18 +2366,18 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0123, Function | SmallTest | Level1) req4->SetSlotType(NotificationConstant::SlotType::CUSTOM); g_advancedNotificationService->Publish("testLabel", req); - EXPECT_EQ(ret, 0); + EXPECT_EQ(ret, 1); g_advancedNotificationService->Publish("testLabel", req1); - EXPECT_EQ(ret, 0); + EXPECT_EQ(ret, 2); g_advancedNotificationService->Publish("testLabel", req2); - EXPECT_EQ(ret, 0); + EXPECT_EQ(ret, 3); g_advancedNotificationService->Publish("testLabel", req3); - EXPECT_EQ(ret, 0); + EXPECT_EQ(ret, 4); g_advancedNotificationService->Publish("testLabel", req4); - EXPECT_EQ(ret, 0); + EXPECT_EQ(ret, 5); g_advancedNotificationService->DeleteAll(); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); } /** @@ -2406,7 +2415,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0124, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); } /** @@ -2439,7 +2448,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0125, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); } /** @@ -2477,7 +2486,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0126, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); - EXPECT_FALSE(passed); + EXPECT_TRUE(passed); } /**