From 2b4918b25ed3bd26eee058108deb345700473c2f Mon Sep 17 00:00:00 2001 From: zero-cyc Date: Mon, 22 Aug 2022 15:47:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=9F=A5=E5=88=A0=E9=99=A4API?= =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zero-cyc Change-Id: Ifcefcb47e3070eee0c874495bddd5b57c8e1f06e --- frameworks/ans/src/notification_helper.cpp | 11 +- .../core/include/ans_manager_interface.h | 8 +- frameworks/core/include/ans_manager_proxy.h | 8 +- frameworks/core/include/ans_manager_stub.h | 8 +- frameworks/core/include/ans_notification.h | 8 +- frameworks/core/src/ans_manager_proxy.cpp | 16 +- frameworks/core/src/ans_manager_stub.cpp | 22 +- frameworks/core/src/ans_notification.cpp | 10 +- frameworks/js/napi/include/common.h | 2 + frameworks/js/napi/src/common.cpp | 10 + frameworks/js/napi/src/remove.cpp | 250 ++++++++++-------- interfaces/inner_api/notification_helper.h | 8 +- .../include/advanced_notification_service.h | 11 +- .../ans/src/advanced_notification_service.cpp | 40 +-- 14 files changed, 248 insertions(+), 164 deletions(-) diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index fb3053653..823d8a89c 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -196,15 +196,16 @@ ErrCode NotificationHelper::UnSubscribeNotification( return DelayedSingleton::GetInstance()->UnSubscribeNotification(subscriber, subscribeInfo); } -ErrCode NotificationHelper::RemoveNotification(const std::string &key) +ErrCode NotificationHelper::RemoveNotification(const std::string &key, int32_t removeReason) { - return DelayedSingleton::GetInstance()->RemoveNotification(key); + return DelayedSingleton::GetInstance()->RemoveNotification(key, removeReason); } -ErrCode NotificationHelper::RemoveNotification( - const NotificationBundleOption &bundleOption, const int32_t notificationId, const std::string &label) +ErrCode NotificationHelper::RemoveNotification(const NotificationBundleOption &bundleOption, + const int32_t notificationId, const std::string &label, int32_t removeReason) { - return DelayedSingleton::GetInstance()->RemoveNotification(bundleOption, notificationId, label); + return DelayedSingleton::GetInstance()->RemoveNotification(bundleOption, + notificationId, label, removeReason); } ErrCode NotificationHelper::RemoveAllNotifications(const NotificationBundleOption &bundleOption) diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index afd8a3fd1..472582a1c 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -279,9 +279,10 @@ public: * @brief Delete notification based on key. * * @param key Indicates the key to delete notification. + * @param removeReason Indicates the reason of remove notification. * @return Returns ERR_OK on success, others on failure. */ - virtual ErrCode Delete(const std::string &key) = 0; + virtual ErrCode Delete(const std::string &key, int32_t removeReason) = 0; /** * @brief Delete notification. @@ -289,10 +290,11 @@ public: * @param bundleOption Indicates the NotificationBundleOption of the notification. * @param notificationId Indicates the id of the notification. * @param label Indicates the label of the notification. + * @param removeReason Indicates the reason of remove notification. * @return Returns ERR_OK on success, others on failure. */ - virtual ErrCode RemoveNotification( - const sptr &bundleOption, int notificationId, const std::string &label) = 0; + virtual ErrCode RemoveNotification(const sptr &bundleOption, int notificationId, + const std::string &label, int32_t removeReason) = 0; /** * @brief Delete all notifications. diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index 59baba2a2..0cc02ac0d 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -269,10 +269,11 @@ public: * @param bundleOption Indicates the NotificationBundleOption of the notification. * @param notificationId Indicates the id of the notification. * @param label Indicates the label of the notification. + * @param removeReason Indicates the reason of remove notification. * @return Returns ERR_OK on success, others on failure. */ - ErrCode RemoveNotification( - const sptr &bundleOption, int32_t notificationId, const std::string &label) override; + ErrCode RemoveNotification(const sptr &bundleOption, int32_t notificationId, + const std::string &label, int32_t removeReason) override; /** * @brief Delete all notifications. @@ -286,9 +287,10 @@ public: * @brief Delete notification based on key. * * @param key Indicates the key to delete notification. + * @param removeReason Indicates the reason of remove notification. * @return Returns ERR_OK on success, others on failure. */ - ErrCode Delete(const std::string &key) override; + ErrCode Delete(const std::string &key, int32_t removeReason) override; /** * @brief Remove notifications based on bundle. diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index ed28f92c2..a20c40d2e 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -285,10 +285,11 @@ public: * @param bundleOption Indicates the NotificationBundleOption of the notification. * @param notificationId Indicates the id of the notification. * @param label Indicates the label of the notification. + * @param removeReason Indicates the reason of remove notification. * @return Returns ERR_OK on success, others on failure. */ - virtual ErrCode RemoveNotification( - const sptr &bundleOption, int32_t notificationId, const std::string &label) override; + virtual ErrCode RemoveNotification(const sptr &bundleOption, int32_t notificationId, + const std::string &label, int32_t removeReason) override; /** * @brief Delete all notifications. @@ -302,9 +303,10 @@ public: * @brief Delete notification based on key. * * @param key Indicates the key to delete notification. + * @param removeReason Indicates the reason of remove notification. * @return Returns ERR_OK on success, others on failure. */ - virtual ErrCode Delete(const std::string &key) override; + virtual ErrCode Delete(const std::string &key, int32_t removeReason) override; /** * @brief Remove notifications based on bundle. diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index cba702560..6c5e718d0 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -382,9 +382,10 @@ public: * @note Your application must have platform signature to use this method. * * @param key Indicates the key of the notification to remove. + * @param removeReason Indicates the reason of remove notification. * @return Returns remove notification result. */ - ErrCode RemoveNotification(const std::string &key); + ErrCode RemoveNotification(const std::string &key, int32_t removeReason); /** * @brief Removes a specified removable notification of other applications. @@ -393,10 +394,11 @@ public: * @param bundleOption Indicates the bundle name and uid of the application whose notifications are to be removed. * @param notificationId Indicates the id of the notification to remove. * @param label Indicates the label of the notification to remove. + * @param removeReason Indicates the reason of remove notification. * @return Returns remove notification result. */ - ErrCode RemoveNotification( - const NotificationBundleOption &bundleOption, const int32_t notificationId, const std::string &label); + ErrCode RemoveNotification(const NotificationBundleOption &bundleOption, const int32_t notificationId, + const std::string &label, int32_t removeReason); /** * @brief Removes a specified removable notification of other applications. diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index a5e654c05..4a0e2b96f 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -836,8 +836,8 @@ ErrCode AnsManagerProxy::GetPrivateNotificationsAllowed(bool &allow) return result; } -ErrCode AnsManagerProxy::RemoveNotification( - const sptr &bundleOption, int32_t notificationId, const std::string &label) +ErrCode AnsManagerProxy::RemoveNotification(const sptr &bundleOption, + int32_t notificationId, const std::string &label, int32_t removeReason) { if (bundleOption == nullptr) { ANS_LOGE("[RemoveNotification] fail: bundle is empty."); @@ -865,6 +865,11 @@ ErrCode AnsManagerProxy::RemoveNotification( return ERR_ANS_PARCELABLE_FAILED; } + if (!data.WriteInt32(removeReason)) { + ANS_LOGE("[RemoveNotification] fail: write removeReason failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; ErrCode result = InnerTransact(REMOVE_NOTIFICATION, option, data, reply); @@ -915,7 +920,7 @@ ErrCode AnsManagerProxy::RemoveAllNotifications(const sptr &bundleOption, int notificationId, const std::string &label) +ErrCode AnsManagerStub::RemoveNotification(const sptr &bundleOption, + int notificationId, const std::string &label, int32_t removeReason) { ANS_LOGE("AnsManagerStub::RemoveNotification called!"); return ERR_INVALID_OPERATION; @@ -1930,7 +1942,7 @@ ErrCode AnsManagerStub::RemoveAllNotifications(const sptrUnsubscribe(subscriberSptr, sptrInfo); } -ErrCode AnsNotification::RemoveNotification(const std::string &key) +ErrCode AnsNotification::RemoveNotification(const std::string &key, int32_t removeReason) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (key.empty()) { @@ -507,11 +507,11 @@ ErrCode AnsNotification::RemoveNotification(const std::string &key) ANS_LOGE("GetAnsManagerProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } - return ansManagerProxy_->Delete(key); + return ansManagerProxy_->Delete(key, removeReason); } -ErrCode AnsNotification::RemoveNotification( - const NotificationBundleOption &bundleOption, const int32_t notificationId, const std::string &label) +ErrCode AnsNotification::RemoveNotification(const NotificationBundleOption &bundleOption, + const int32_t notificationId, const std::string &label, int32_t removeReason) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (bundleOption.GetBundleName().empty()) { @@ -525,7 +525,7 @@ ErrCode AnsNotification::RemoveNotification( } sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); - return ansManagerProxy_->RemoveNotification(bo, notificationId, label); + return ansManagerProxy_->RemoveNotification(bo, notificationId, label, removeReason); } ErrCode AnsNotification::RemoveAllNotifications(const NotificationBundleOption &bundleOption) diff --git a/frameworks/js/napi/include/common.h b/frameworks/js/napi/include/common.h index d5740ecca..8fe476ea0 100644 --- a/frameworks/js/napi/include/common.h +++ b/frameworks/js/napi/include/common.h @@ -1539,6 +1539,8 @@ public: static napi_value GetNotificationBadgeNumber( const napi_env &env, const napi_value &value, NotificationRequest &request); + static bool IsValidRemoveReason(int32_t reasonType); + private: static const int32_t ARGS_ONE = 1; static const int32_t ARGS_TWO = 2; diff --git a/frameworks/js/napi/src/common.cpp b/frameworks/js/napi/src/common.cpp index 6f37fe51d..8c77fc6bf 100644 --- a/frameworks/js/napi/src/common.cpp +++ b/frameworks/js/napi/src/common.cpp @@ -3930,6 +3930,16 @@ bool Common::ReasonCToJS(const int &inType, int &outType) return true; } +bool Common::IsValidRemoveReason(int32_t reasonType) +{ + if (reasonType == NotificationConstant::CLICK_REASON_DELETE || + reasonType == NotificationConstant::CANCEL_REASON_DELETE) { + return true; + } + ANS_LOGE("Reason %{public}d is an invalid value", reasonType); + return false; +} + bool Common::DoNotDisturbTypeJSToC(const DoNotDisturbType &inType, NotificationConstant::DoNotDisturbType &outType) { switch (inType) { diff --git a/frameworks/js/napi/src/remove.cpp b/frameworks/js/napi/src/remove.cpp index e59a699a6..2929fa193 100644 --- a/frameworks/js/napi/src/remove.cpp +++ b/frameworks/js/napi/src/remove.cpp @@ -18,13 +18,13 @@ namespace OHOS { namespace NotificationNapi { -const int REMOVE_MIN_PARA = 1; -const int REMOVE_OR_BUNDLE_MAX_PARA = 2; +const int REMOVE_MIN_PARA = 2; +const int REMOVE_OR_BUNDLE_MAX_PARA = 3; const int REMOVE_ALL_MAX_PARA = 2; -const int REMOVE_BY_BUNDLE_AND_KEY_MIN_PARA = 2; -const int REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA = 3; +const int REMOVE_BY_BUNDLE_AND_KEY_MIN_PARA = 3; +const int REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA = 4; const int REMOVE_GROUP_BY_BUNDLE_MIN_PARA = 2; const int REMOVE_GROUP_BY_BUNDLE_MAX_PARA = 3; @@ -38,6 +38,7 @@ struct RemoveParams { std::optional hashcode {}; std::optional bundleAndKeyInfo {}; int32_t userId = SUBSCRIBE_USER_INIT; + int32_t removeReason = NotificationConstant::CANCEL_REASON_DELETE; bool hasUserId = false; napi_ref callback = nullptr; }; @@ -62,77 +63,105 @@ struct AsyncCallbackInfoRemoveGroupByBundle { CallbackPromiseInfo info; }; -napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, RemoveParams ¶ms) +bool ParseRemoveReason(const napi_env &env, const napi_value &value, RemoveParams ¶ms) { - ANS_LOGI("enter"); + napi_valuetype valueType = napi_undefined; + NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), false); + if (valueType != napi_number) { + ANS_LOGW("RemoveReason valueType unexpected."); + return false; + } + int32_t removeReason = 0; + napi_get_value_int32(env, value, &removeReason); + if (!Common::IsValidRemoveReason(removeReason)) { + ANS_LOGW("RemoveReason value unexpected."); + return false; + } + params.removeReason = removeReason; + return true; +} + +bool ParseCallbackFunc(const napi_env &env, const napi_value &value, + RemoveParams ¶ms) +{ + napi_valuetype valueType = napi_undefined; + NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), false); + if (valueType != napi_function) { + ANS_LOGW("Wrong argument type. Function expected."); + return false; + } + napi_create_reference(env, value, 1, ¶ms.callback); + return true; +} +bool ParseHashcodeTypeParams(const napi_env &env, napi_value* argv, size_t argc, RemoveParams ¶ms) +{ + // argv[0]: hashCode + size_t strLen = 0; + char str[STR_MAX_SIZE] = {0}; + NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, argv[PARAM0], str, STR_MAX_SIZE - 1, &strLen), false); + params.hashcode = str; + // argv[1]:removeReason + if (!ParseRemoveReason(env, argv[PARAM1], params)) { + return false; + } + // argv[2]:callback + if (argc >= REMOVE_OR_BUNDLE_MAX_PARA) { + if (!ParseCallbackFunc(env, argv[PARAM2], params)) { + return false; + } + } + return true; +} + +bool ParseBundleOptionTypeParams(const napi_env &env, napi_value* argv, size_t argc, RemoveParams ¶ms) +{ + if (argc < REMOVE_BY_BUNDLE_AND_KEY_MIN_PARA) { + ANS_LOGW("Wrong number of arguments."); + return false; + } + BundleAndKeyInfo bundleInfo {}; + // argv[0]: BundleOption argv[1]: NotificationKey + if (Common::GetBundleOption(env, argv[PARAM0], bundleInfo.option) == nullptr || + Common::GetNotificationKey(env, argv[PARAM1], bundleInfo.key) == nullptr) { + ANS_LOGE("GetBundleOption failed."); + return false; + } + params.bundleAndKeyInfo = bundleInfo; + // argv[2]:removeReason + if (!ParseRemoveReason(env, argv[PARAM2], params)) { + return false; + } + // argv[3]:callback + if (argc >= REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA) { + if (!ParseCallbackFunc(env, argv[PARAM3], params)) { + return false; + } + } + return true; +} + +bool ParseParameters(const napi_env &env, const napi_callback_info &info, RemoveParams ¶ms) +{ + ANS_LOGI("enter"); size_t argc = REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA; napi_value argv[REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA] = {nullptr}; napi_value thisVar = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL), false); if (argc < REMOVE_MIN_PARA) { ANS_LOGW("Wrong number of arguments."); - return nullptr; + return false; } - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); - if ((valuetype != napi_string) && (valuetype != napi_object)) { + napi_valuetype valueType = napi_undefined; + NAPI_CALL_BASE(env, napi_typeof(env, argv[PARAM0], &valueType), false); + if ((valueType != napi_string) && (valueType != napi_object)) { ANS_LOGW("Wrong argument type. String or object expected."); - return nullptr; + return false; } - - if (valuetype == napi_string) { - // argv[0]: hashCode - size_t strLen = 0; - char str[STR_MAX_SIZE] = {0}; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM0], str, STR_MAX_SIZE - 1, &strLen)); - params.hashcode = str; - - // argv[1]:callback - if (argc >= REMOVE_OR_BUNDLE_MAX_PARA) { - NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); - if (valuetype != napi_function) { - ANS_LOGW("Wrong argument type. Function expected."); - return nullptr; - } - napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); - } - } else { - if (argc < REMOVE_BY_BUNDLE_AND_KEY_MIN_PARA) { - ANS_LOGW("Wrong number of arguments."); - return nullptr; - } - - BundleAndKeyInfo info {}; - // argv[0]: BundleOption - auto retValue = Common::GetBundleOption(env, argv[PARAM0], info.option); - if (retValue == nullptr) { - ANS_LOGE("GetBundleOption failed."); - return nullptr; - } - - // argv[1]: NotificationKey - retValue = Common::GetNotificationKey(env, argv[PARAM1], info.key); - if (retValue == nullptr) { - ANS_LOGE("GetNotificationKey failed."); - return nullptr; - } - - params.bundleAndKeyInfo = info; - - // argv[2]:callback - if (argc >= REMOVE_BY_BUNDLE_AND_KEY_MAX_PARA) { - NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); - if (valuetype != napi_function) { - ANS_LOGW("Wrong argument type. Function expected."); - return nullptr; - } - napi_create_reference(env, argv[PARAM2], 1, ¶ms.callback); - } + if (valueType == napi_string) { + return ParseHashcodeTypeParams(env, argv, argc, params); } - - return Common::NapiGetNull(env); + return ParseBundleOptionTypeParams(env, argv, argc, params); } napi_value ParseParametersByRemoveAll(const napi_env &env, const napi_callback_info &info, RemoveParams ¶ms) @@ -220,7 +249,6 @@ napi_value ParseParameters( size_t strLen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen)); params.groupName = str; - // argv[2]:callback if (argc >= REMOVE_GROUP_BY_BUNDLE_MAX_PARA) { NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); @@ -230,66 +258,72 @@ napi_value ParseParameters( } napi_create_reference(env, argv[PARAM2], 1, ¶ms.callback); } - return Common::NapiGetNull(env); } +void RemoveExecuteCallback(napi_env env, void *data) +{ + ANS_LOGI("Remove napi_create_async_work start"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoRemove *removeInfo = static_cast(data); + if (!removeInfo) { + ANS_LOGE("Invalid async callback data"); + return; + } + if (removeInfo->params.hashcode.has_value()) { + removeInfo->info.errorCode = NotificationHelper::RemoveNotification(removeInfo->params.hashcode.value(), + removeInfo->params.removeReason); + } else if (removeInfo->params.bundleAndKeyInfo.has_value()) { + auto &infos = removeInfo->params.bundleAndKeyInfo.value(); + removeInfo->info.errorCode = NotificationHelper::RemoveNotification(infos.option, + infos.key.id, infos.key.label, removeInfo->params.removeReason); + } +} + +void RemoveCompleteCallback(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("Remove napi_create_async_work end"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoRemove *removeInfo = static_cast(data); + if (removeInfo) { + Common::ReturnCallbackPromise(env, removeInfo->info, Common::NapiGetNull(env)); + if (removeInfo->info.callback != nullptr) { + napi_delete_reference(env, removeInfo->info.callback); + } + napi_delete_async_work(env, removeInfo->asyncWork); + delete removeInfo; + removeInfo = nullptr; + } +} + napi_value Remove(napi_env env, napi_callback_info info) { ANS_LOGI("enter"); - RemoveParams params {}; - if (ParseParameters(env, info, params) == nullptr) { + if (!ParseParameters(env, info, params)) { return Common::NapiGetUndefined(env); } - - AsyncCallbackInfoRemove *asynccallbackinfo = + AsyncCallbackInfoRemove *removeInfo = new (std::nothrow) AsyncCallbackInfoRemove {.env = env, .asyncWork = nullptr, .params = params}; - if (!asynccallbackinfo) { + if (!removeInfo) { return Common::JSParaError(env, params.callback); } napi_value promise = nullptr; - Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + Common::PaddingCallbackPromiseInfo(env, params.callback, removeInfo->info, promise); napi_value resourceName = nullptr; napi_create_string_latin1(env, "remove", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work(env, - nullptr, - resourceName, - [](napi_env env, void *data) { - ANS_LOGI("Remove napi_create_async_work start"); - AsyncCallbackInfoRemove *asynccallbackinfo = static_cast(data); - - if (asynccallbackinfo->params.hashcode.has_value()) { - asynccallbackinfo->info.errorCode = - NotificationHelper::RemoveNotification(asynccallbackinfo->params.hashcode.value()); - } else if (asynccallbackinfo->params.bundleAndKeyInfo.has_value()) { - auto &infos = asynccallbackinfo->params.bundleAndKeyInfo.value(); - - asynccallbackinfo->info.errorCode = - NotificationHelper::RemoveNotification(infos.option, infos.key.id, infos.key.label); - } - }, - [](napi_env env, napi_status status, void *data) { - ANS_LOGI("Remove napi_create_async_work end"); - AsyncCallbackInfoRemove *asynccallbackinfo = static_cast(data); - if (asynccallbackinfo) { - Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env)); - if (asynccallbackinfo->info.callback != nullptr) { - napi_delete_reference(env, asynccallbackinfo->info.callback); - } - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - delete asynccallbackinfo; - asynccallbackinfo = nullptr; - } - }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); - - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); - - if (asynccallbackinfo->info.isCallback) { + napi_create_async_work(env, nullptr, resourceName, RemoveExecuteCallback, RemoveCompleteCallback, + (void *)removeInfo, &removeInfo->asyncWork); + NAPI_CALL(env, napi_queue_async_work(env, removeInfo->asyncWork)); + if (removeInfo->info.isCallback) { return Common::NapiGetNull(env); } else { return promise; diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 13e85e02d..2c7855390 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -384,9 +384,10 @@ public: * @note Your application must have platform signature to use this method. * * @param key Indicates the key of the notification to remove. + * @param removeReason Indicates the reason of remove notification. * @return Returns remove notification result. */ - static ErrCode RemoveNotification(const std::string &key); + static ErrCode RemoveNotification(const std::string &key, int32_t removeReason); /** * @brief Removes a specified removable notification of other applications. @@ -395,10 +396,11 @@ public: * @param bundleOption Indicates the bundle name and uid of the application whose notifications are to be removed. * @param notificationId Indicates the id of the notification to remove. * @param label Indicates the label of the notification to remove. + * @param removeReason Indicates the reason of remove notification. * @return Returns remove notification result. */ - static ErrCode RemoveNotification( - const NotificationBundleOption &bundleOption, const int32_t notificationId, const std::string &label); + static ErrCode RemoveNotification(const NotificationBundleOption &bundleOption, + const int32_t notificationId, const std::string &label, int32_t removeReason); /** * @brief Removes a specified removable notification of other applications. diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 7281f3a1a..0b82dffee 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -281,10 +281,11 @@ public: * @param bundleOption Indicates the NotificationBundleOption of the notification. * @param notificationId Indicates the id of the notification. * @param label Indicates the label of the notification. + * @param removeReason Indicates the reason of remove notification. * @return Returns ERR_OK on success, others on failure. */ - ErrCode RemoveNotification( - const sptr &bundleOption, int32_t notificationId, const std::string &label) override; + ErrCode RemoveNotification(const sptr &bundleOption, int32_t notificationId, + const std::string &label, int32_t removeReason) override; /** * @brief Delete all notifications. @@ -298,9 +299,10 @@ public: * @brief Delete notification based on key. * * @param key Indicates the key to delete notification. + * @param removeReason Indicates the reason of remove notification. * @return Returns ERR_OK on success, others on failure. */ - ErrCode Delete(const std::string &key) override; + ErrCode Delete(const std::string &key, int32_t removeReason) override; /** * @brief Remove notifications based on bundle. @@ -725,7 +727,8 @@ private: ErrCode AssignToNotificationList(const std::shared_ptr &record); ErrCode RemoveFromNotificationList(const sptr &bundleOption, const std::string &label, int32_t notificationId, sptr ¬ification, bool isCancel = false); - ErrCode RemoveFromNotificationList(const std::string &key, sptr ¬ification, bool isCancel = false); + ErrCode RemoveFromNotificationList(const std::string &key, sptr ¬ification, + bool isCancel, int32_t removeReason); ErrCode RemoveFromNotificationListForDeleteAll(const std::string &key, const int32_t &userId, sptr ¬ification); std::vector GetNotificationKeys(const sptr &bundleOption); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 11bbd96de..0023a2f5e 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -659,7 +659,8 @@ ErrCode AdvancedNotificationService::CancelAll() std::string bundleName; GetDistributedInfo(key, deviceId, bundleName); #endif - result = RemoveFromNotificationList(key, notification, true); + result = RemoveFromNotificationList(key, notification, true, + NotificationConstant::APP_CANCEL_ALL_REASON_DELETE); if (result != ERR_OK) { continue; } @@ -908,7 +909,7 @@ ErrCode AdvancedNotificationService::GetPrivateNotificationsAllowed(bool &allow) return result; } -ErrCode AdvancedNotificationService::Delete(const std::string &key) +ErrCode AdvancedNotificationService::Delete(const std::string &key, int32_t removeReason) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -929,16 +930,15 @@ ErrCode AdvancedNotificationService::Delete(const std::string &key) std::string bundleName; GetDistributedInfo(key, deviceId, bundleName); #endif - result = RemoveFromNotificationList(key, notification); + result = RemoveFromNotificationList(key, notification, false, removeReason); if (result != ERR_OK) { return; } if (notification != nullptr) { - int32_t reason = NotificationConstant::CANCEL_REASON_DELETE; - UpdateRecentNotification(notification, true, reason); + UpdateRecentNotification(notification, true, removeReason); sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, removeReason); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); #endif @@ -976,7 +976,7 @@ ErrCode AdvancedNotificationService::DeleteByBundle(const sptr notification = nullptr; - result = RemoveFromNotificationList(key, notification); + result = RemoveFromNotificationList(key, notification, false, NotificationConstant::CANCEL_REASON_DELETE); if (result != ERR_OK) { continue; } @@ -1225,7 +1225,7 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList(const sptr ¬ification, bool isCancel) + const std::string &key, sptr ¬ification, bool isCancel, int32_t removeReason) { for (auto record : notificationList_) { if (record->notification->GetKey() == key) { @@ -1234,7 +1234,7 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList( } notification = record->notification; // delete or delete all, call the function - if (!isCancel) { + if (!isCancel && removeReason != NotificationConstant::CLICK_REASON_DELETE) { TriggerRemoveWantAgent(record->request); } notificationList_.remove(record); @@ -2150,7 +2150,8 @@ void AdvancedNotificationService::OnBundleRemoved(const sptr notification = nullptr; - result = RemoveFromNotificationList(key, notification, true); + result = RemoveFromNotificationList(key, notification, true, + NotificationConstant::PACKAGE_CHANGED_REASON_DELETE); if (result != ERR_OK) { continue; } @@ -2239,8 +2240,8 @@ ErrCode AdvancedNotificationService::AddSlotByType(NotificationConstant::SlotTyp return result; } -ErrCode AdvancedNotificationService::RemoveNotification( - const sptr &bundleOption, int32_t notificationId, const std::string &label) +ErrCode AdvancedNotificationService::RemoveNotification(const sptr &bundleOption, + int32_t notificationId, const std::string &label, int32_t removeReason) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); @@ -2292,16 +2293,16 @@ ErrCode AdvancedNotificationService::RemoveNotification( } if (notification != nullptr) { - int32_t reason = NotificationConstant::CANCEL_REASON_DELETE; - UpdateRecentNotification(notification, true, reason); + UpdateRecentNotification(notification, true, removeReason); sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, removeReason); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); #endif } - - TriggerRemoveWantAgent(notificationRequest); + if (removeReason != NotificationConstant::CLICK_REASON_DELETE) { + TriggerRemoveWantAgent(notificationRequest); + } })); SendRemoveHiSysEvent(notificationId, label, bundleOption, result); @@ -3460,13 +3461,14 @@ void AdvancedNotificationService::OnBundleDataCleared(const sptr notification = nullptr; - ErrCode result = RemoveFromNotificationList(key, notification); + ErrCode result = RemoveFromNotificationList(key, notification, true, + NotificationConstant::PACKAGE_CHANGED_REASON_DELETE); if (result != ERR_OK) { continue; } if (notification != nullptr) { - int32_t reason = NotificationConstant::CANCEL_REASON_DELETE; + int32_t reason = NotificationConstant::PACKAGE_CHANGED_REASON_DELETE; UpdateRecentNotification(notification, true, reason); sptr sortingMap = GenerateSortingMap(); NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); -- Gitee