From f0154354ad3448914b1aca324e2d9495aecf6c5a Mon Sep 17 00:00:00 2001 From: wjh Date: Mon, 4 Dec 2023 21:08:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E9=86=92=E4=BB=A3=E7=90=86=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E6=8C=89=E9=92=AE=E6=94=AF=E6=8C=81=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=8C=E9=80=82=E9=85=8D=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=8C=89=E9=92=AE=20Signed-off-by:=20wjh=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../napi/include/reminder/reminder_common.h | 4 +- frameworks/js/napi/src/reminder/publish.cpp | 3 +- .../js/napi/src/reminder/reminder_common.cpp | 53 ++++++++++--------- services/ans/include/reminder_data_manager.h | 12 +++++ services/ans/src/reminder_data_manager.cpp | 41 +++++++++----- .../unittest/reminder_data_manager_test.cpp | 5 ++ 6 files changed, 76 insertions(+), 42 deletions(-) diff --git a/frameworks/js/napi/include/reminder/reminder_common.h b/frameworks/js/napi/include/reminder/reminder_common.h index eb5971666..30ef12716 100644 --- a/frameworks/js/napi/include/reminder/reminder_common.h +++ b/frameworks/js/napi/include/reminder/reminder_common.h @@ -158,6 +158,9 @@ private: static napi_value GenReminder( const napi_env &env, const napi_value &value, std::shared_ptr& reminder); + static ReminderCommon::HandleActionButtonTitle(const napi_env &env, const napi_value &actionButton, + std::shared_ptr& reminder, const char* str, int32_t buttonType); + static napi_value ParseInt32Array(const napi_env &env, const napi_value &value, const char* propertyName, std::vector &propertyVal, uint8_t maxLen); @@ -168,7 +171,6 @@ private: static void GetButtonWantAgent(const napi_env &env, const napi_value &value, std::shared_ptr& reminder, std::shared_ptr& wantAgent); static void GetButtonDataShareUpdate(const napi_env &env, const napi_value &value, - std::shared_ptr& reminder, std::shared_ptr& buttonDataShareUpdate); static bool GetValueBucketObject(std::string &ValueBucketString, const napi_env &env, const napi_value &arg); static std::string GetStringFromJS(const napi_env &env, const napi_value ¶m, diff --git a/frameworks/js/napi/src/reminder/publish.cpp b/frameworks/js/napi/src/reminder/publish.cpp index b8f831166..7c925534d 100644 --- a/frameworks/js/napi/src/reminder/publish.cpp +++ b/frameworks/js/napi/src/reminder/publish.cpp @@ -659,8 +659,7 @@ void ParseActionButtons(const napi_env &env, ReminderRequest &reminder, napi_val napi_set_named_property(env, wantAgentInfo, BUTTON_WANT_AGENT_URI, info); } // Parse ButtonDataShareUpdate - if (it->second.type == ReminderRequest::ActionButtonType::CLOSE || - it->second.type == ReminderRequest::ActionButtonType::SNOOZE) { + if (it->second.type != ReminderRequest::ActionButtonType::INVALID) { ParseButtonDataShareUpdate(env, it->second.dataShareUpdate, actionButton); } // add obj to array diff --git a/frameworks/js/napi/src/reminder/reminder_common.cpp b/frameworks/js/napi/src/reminder/reminder_common.cpp index 94fd3aae2..5592b3b9a 100644 --- a/frameworks/js/napi/src/reminder/reminder_common.cpp +++ b/frameworks/js/napi/src/reminder/reminder_common.cpp @@ -50,7 +50,6 @@ bool ReminderCommon::GenActionButtons( const napi_env &env, const napi_value &value, std::shared_ptr& reminder, bool isSysApp) { char str[NotificationNapi::STR_MAX_SIZE] = {0}; - char res[NotificationNapi::STR_MAX_SIZE] = {0}; napi_valuetype valuetype = napi_undefined; napi_value actionButtons = nullptr; if (!GetObject(env, value, ReminderAgentNapi::ACTION_BUTTON, actionButtons)) { @@ -85,28 +84,7 @@ bool ReminderCommon::GenActionButtons( ANSR_LOGW("Wrong argument type:%{public}s. buttonType not support.", ACTION_BUTTON); return false; } - - std::string resource = ""; - if (GetStringUtf8(env, actionButton, ReminderAgentNapi::ACTION_BUTTON_RESOURCE, res, - NotificationNapi::STR_MAX_SIZE)) { - resource = std::string(res); - } - - std::string title(str); - auto buttonWantAgent = std::make_shared(); - if (ReminderRequest::ActionButtonType(buttonType) == ReminderRequest::ActionButtonType::CUSTOM) { - GetButtonWantAgent(env, actionButton, reminder, buttonWantAgent); - } - // gen buttonDataShareUpdate - auto buttonDataShareUpdate = std::make_shared(); - if (ReminderRequest::ActionButtonType(buttonType) == ReminderRequest::ActionButtonType::CLOSE || - ReminderRequest::ActionButtonType(buttonType) == ReminderRequest::ActionButtonType::SNOOZE) { - GetButtonDataShareUpdate(env, actionButton, reminder, buttonDataShareUpdate); - } - reminder->SetActionButton(title, static_cast(buttonType), - resource, buttonWantAgent, buttonDataShareUpdate); - ANSR_LOGD("button title=%{public}s, type=%{public}d, resource=%{public}s", - title.c_str(), buttonType, resource.c_str()); + HandleActionButtonTitle(env, actionButton, reminder, str, buttonType); } else { ANSR_LOGW("Parse action button error."); return false; @@ -115,6 +93,32 @@ bool ReminderCommon::GenActionButtons( return true; } +void ReminderCommon::HandleActionButtonTitle(const napi_env &env, const napi_value &actionButton, + std::shared_ptr& reminder, const char* str, int32_t buttonType) +{ + char res[NotificationNapi::STR_MAX_SIZE] = {0}; + std::string resource = ""; + if (GetStringUtf8(env, actionButton, ReminderAgentNapi::ACTION_BUTTON_RESOURCE, res, + NotificationNapi::STR_MAX_SIZE)) { + resource = std::string(res); + } + + std::string title(str); + auto buttonWantAgent = std::make_shared(); + if (ReminderRequest::ActionButtonType(buttonType) == ReminderRequest::ActionButtonType::CUSTOM) { + GetButtonWantAgent(env, actionButton, reminder, buttonWantAgent); + } + // gen buttonDataShareUpdate + auto buttonDataShareUpdate = std::make_shared(); + if (ReminderRequest::ActionButtonType(buttonType) != ReminderRequest::ActionButtonType::INVALID) { + GetButtonDataShareUpdate(env, actionButton, buttonDataShareUpdate); + } + reminder->SetActionButton(title, static_cast(buttonType), + resource, buttonWantAgent, buttonDataShareUpdate); + ANSR_LOGD("button title=%{public}s, type=%{public}d, resource=%{public}s", + title.c_str(), buttonType, resource.c_str()); +} + bool ReminderCommon::IsSelfSystemApp(std::shared_ptr& reminder) { auto selfToken = IPCSkeleton::GetSelfTokenID(); @@ -149,7 +153,6 @@ void ReminderCommon::GetButtonWantAgent(const napi_env &env, const napi_value &v // uri:string equalTo{key:value} valuesBucket{key:value} void ReminderCommon::GetButtonDataShareUpdate(const napi_env &env, const napi_value &value, - std::shared_ptr& reminder, std::shared_ptr& buttonDataShareUpdate) { napi_value dataShare = nullptr; @@ -228,12 +231,12 @@ std::string ReminderCommon::GetStringFromJS(const napi_env &env, const napi_valu if (napi_get_value_string_utf8(env, param, nullptr, 0, &size) != napi_ok) { return defaultValue; } - std::string value(""); if (size == 0) { return defaultValue; } char *buf = new (std::nothrow) char[size + 1]; + std::string value(""); if (buf == nullptr) { return value; } diff --git a/services/ans/include/reminder_data_manager.h b/services/ans/include/reminder_data_manager.h index ba3790ebc..ca55d308f 100644 --- a/services/ans/include/reminder_data_manager.h +++ b/services/ans/include/reminder_data_manager.h @@ -240,6 +240,18 @@ private: void CancelAllReminders(const int32_t &userId); + /** + * @brief Check the update conditions. + * + * @param reminder Indicates the showed reminder. + * @param actionButtonType Button type of the button. + * @param actionButtonMap Button map. + * @return True if check successful. + */ + bool ReminderDataManager::CheckUpdateConditions(const sptr &reminder, + const ReminderRequest::ActionButtonType &actionButtonType, + const std::map actionButtonMap) + /** * @brief update app database. * diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index b321471ee..71a7e3125 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -515,6 +515,7 @@ void ReminderDataManager::CloseReminder(const OHOS::EventFwk::Want &want, bool c CloseRemindersByGroupId(reminderId, bundleName, groupId); } CloseReminder(reminder, cancelNotification); + UpdateAppDatabase(reminder, ReminderRequest::ActionButtonType::CLOSE); StartRecentReminder(); CheckNeedNotifyStatus(reminder, ReminderRequest::ActionButtonType::CLOSE); } @@ -562,7 +563,6 @@ void ReminderDataManager::CloseReminder(const sptr &reminder, b } reminder->OnClose(true); RemoveFromShowedReminders(reminder); - UpdateAppDatabase(reminder, ReminderRequest::ActionButtonType::CLOSE); store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); if (cancelNotification) { CancelNotification(reminder); @@ -584,28 +584,40 @@ std::shared_ptr ReminderDataManager::InitInstance( } return REMINDER_DATA_MANAGER; } - -void ReminderDataManager::UpdateAppDatabase(const sptr &reminder, - const ReminderRequest::ActionButtonType &actionButtonType) +bool ReminderDataManager::CheckUpdateConditions(const sptr &reminder, + const ReminderRequest::ActionButtonType &actionButtonType, + const std::map& actionButtonMap) { if (!reminder->IsSystemApp()) { ANSR_LOGI("UpdateAppDatabase faild, is not SystemApp"); - return; + return false; } - if (actionButtonType == ReminderRequest::ActionButtonType::CUSTOM || - actionButtonType == ReminderRequest::ActionButtonType::INVALID) { - ANSR_LOGI("actionButtonType is CUSTOM or INVALID"); - return; + if (actionButtonType == ReminderRequest::ActionButtonType::INVALID) { + ANSR_LOGI("actionButtonType is NVALID"); + return false; } - auto actionButtonMap = reminder->GetActionButtons(); - if (actionButtonMap.count(actionButtonType) && actionButtonMap.at(actionButtonType).dataShareUpdate == nullptr) { + if (!actionButtonMap.count(actionButtonType)) { + ANSR_LOGI("actionButtonType does not exist"); + return false; + } + if (actionButtonMap.at(actionButtonType).dataShareUpdate == nullptr) { ANSR_LOGI("dataShareUpdate is null"); - return; + return false; } if (actionButtonMap.at(actionButtonType).dataShareUpdate->uri == "" || actionButtonMap.at(actionButtonType).dataShareUpdate->equalTo == "" || actionButtonMap.at(actionButtonType).dataShareUpdate->valuesBucket == "") { - ANSR_LOGI("not have uri"); + ANSR_LOGI("datashare parameter is invalid"); + return false; + } + return true; +} + +void ReminderDataManager::UpdateAppDatabase(const sptr &reminder, + const ReminderRequest::ActionButtonType &actionButtonType) +{ + auto actionButtonMap = reminder->GetActionButtons(); + if (!CheckUpdateConditions(reminder, actionButtonType, actionButtonMap)) { return; } // init default dstBundleName @@ -1038,6 +1050,7 @@ void ReminderDataManager::SnoozeReminder(const OHOS::EventFwk::Want &want) return; } SnoozeReminderImpl(reminder); + UpdateAppDatabase(reminder, ReminderRequest::ActionButtonType::SNOOZE); CheckNeedNotifyStatus(reminder, ReminderRequest::ActionButtonType::SNOOZE); } @@ -1057,7 +1070,6 @@ void ReminderDataManager::SnoozeReminderImpl(sptr &reminder) StopTimerLocked(TimerType::ALERTING_TIMER); } reminder->OnSnooze(); - UpdateAppDatabase(reminder, ReminderRequest::ActionButtonType::SNOOZE); store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); // 2) Show the notification dialog in the systemUI @@ -1683,6 +1695,7 @@ void ReminderDataManager::HandleCustomButtonClick(const OHOS::EventFwk::Want &wa return; } CloseReminder(reminder, false); + UpdateAppDatabase(reminder, ReminderRequest::ActionButtonType::CUSTOM); std::string buttonPkgName = want.GetStringParam("PkgName"); std::string buttonAbilityName = want.GetStringParam("AbilityName"); diff --git a/services/ans/test/unittest/reminder_data_manager_test.cpp b/services/ans/test/unittest/reminder_data_manager_test.cpp index e22cd54c1..f3fa1a633 100644 --- a/services/ans/test/unittest/reminder_data_manager_test.cpp +++ b/services/ans/test/unittest/reminder_data_manager_test.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include #include #include @@ -387,6 +388,10 @@ HWTEST_F(ReminderDataManagerTest, ReminderDataManagerTest_016, Level1) buttonWantAgent, buttonDataShareUpdate); manager->UpdateAppDatabase(reminder, ReminderRequest::ActionButtonType::INVALID); + // actionButtonType does not exist + std::map actionButtonMap; + manager->CheckUpdateConditions(reminder, ReminderRequest::ActionButtonType::CLOSE, actionButtonMap); + // null ButtonDataShareUpdate reminder->SetActionButton("稍后提醒", ReminderRequest::ActionButtonType::SNOOZE, "", buttonWantAgent); manager->UpdateAppDatabase(reminder, ReminderRequest::ActionButtonType::SNOOZE); -- Gitee