diff --git a/frameworks/js/napi/src/reminder/publish.cpp b/frameworks/js/napi/src/reminder/publish.cpp index d2fb6ea6d3a4d729831ec1c4e38c5a7fae871477..a6e847a29672a33645b2f6054d79acf7017762fd 100644 --- a/frameworks/js/napi/src/reminder/publish.cpp +++ b/frameworks/js/napi/src/reminder/publish.cpp @@ -641,6 +641,8 @@ void ParseActionButtons(const napi_env &env, ReminderRequest &reminder, napi_val napi_set_named_property(env, actionButton, ACTION_BUTTON_TYPE, buttonInfo); napi_create_string_utf8(env, (it->second.title).c_str(), NAPI_AUTO_LENGTH, &buttonInfo); napi_set_named_property(env, actionButton, ACTION_BUTTON_TITLE, buttonInfo); + napi_create_string_utf8(env, (it->second.resource).c_str(), NAPI_AUTO_LENGTH, &buttonInfo); + napi_set_named_property(env, actionButton, ACTION_BUTTON_RESOURCE, buttonInfo); // create obj napi_value wantAgentInfo = nullptr; diff --git a/services/ans/include/reminder_data_manager.h b/services/ans/include/reminder_data_manager.h index 5fae70f7cc650833a951f1cabda01de7c697cdcf..0c08c0462ad41416bc382769b1f06fca67a4acc0 100644 --- a/services/ans/include/reminder_data_manager.h +++ b/services/ans/include/reminder_data_manager.h @@ -31,6 +31,7 @@ #include "reminder_config_change_observer.h" #include "datashare_predicates.h" #include "datashare_values_bucket.h" +#include "app_mgr_interface.h" namespace OHOS { namespace Notification { @@ -209,6 +210,11 @@ public: */ void OnConfigurationChanged(const AppExecFwk::Configuration &configuration); + /** + * @brief When OnRemoveSystemAbility occurs. + */ + void OnRemoveAppMgr(); + static const uint8_t TIME_ZONE_CHANGE; static const uint8_t DATE_TIME_CHANGE; @@ -522,6 +528,22 @@ private: static bool cmp(sptr &reminderRequest, sptr &other); + /** + * @brief Connect App Manager to get the current foreground application. + */ + bool ConnectAppMgr(); + + /** + * @brief Check need to notify the application, if the current foreground application + * is the creator of the reminder, notify the application of the reminder status + * change; otherwise, do not noitfy. + * + * @param reminder Indicates a reminder. + * @param buttonType The type of button clicked by the user. + */ + void CheckNeedNotifyStatus(const sptr &reminder, + const ReminderRequest::ActionButtonType buttonType); + /** * Single instance. */ @@ -603,6 +625,12 @@ private: * Indicates config change observer for language */ sptr configChangeObserver_ = nullptr; + + /** + * Indicates app mananger for get foreground application + */ + std::mutex appMgrMutex_; + sptr appMgrProxy_ = nullptr; }; } // namespace OHOS } // namespace Notification diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index e2c25de693504424cc5fabcdcf7edcd48b0816fc..b91ea9b2d20e0832917f24a05a2fb88974669853 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -20,6 +20,7 @@ #include "ans_log_wrapper.h" #include "ans_const_define.h" #include "common_event_support.h" +#include "common_event_manager.h" #ifdef DEVICE_STANDBY_ENABLE #include "standby_service_client.h" #include "allow_type.h" @@ -36,6 +37,9 @@ #include "datashare_value_object.h" #include "datashare_helper.h" #include "datashare_template.h" +#include "system_ability_definition.h" +#include "app_mgr_constants.h" +#include "iservice_registry.h" namespace OHOS { namespace Notification { @@ -501,19 +505,18 @@ void ReminderDataManager::CloseReminder(const OHOS::EventFwk::Want &want, bool c ANSR_LOGW("notificationRequest is not find, this reminder can`t close by groupId"); CloseReminder(reminder, cancelNotification); StartRecentReminder(); + CheckNeedNotifyStatus(reminder, ReminderRequest::ActionButtonType::CLOSE); return; } std::string bundleName = notificationRequest->GetCreatorBundleName(); std::string groupId = reminder->GetGroupId(); - if (groupId.empty()) { - ANSR_LOGD("default close reminder, the group id is not set."); - CloseReminder(reminder, cancelNotification); - StartRecentReminder(); - return; + if (!groupId.empty()) { + ANSR_LOGD("close reminder, the group id is set."); + CloseRemindersByGroupId(reminderId, bundleName, groupId); } - CloseRemindersByGroupId(reminderId, bundleName, groupId); CloseReminder(reminder, cancelNotification); StartRecentReminder(); + CheckNeedNotifyStatus(reminder, ReminderRequest::ActionButtonType::CLOSE); } void ReminderDataManager::CloseRemindersByGroupId(const int32_t &oldReminderId, const std::string &packageName, @@ -1031,6 +1034,7 @@ void ReminderDataManager::SnoozeReminder(const OHOS::EventFwk::Want &want) return; } SnoozeReminderImpl(reminder); + CheckNeedNotifyStatus(reminder, ReminderRequest::ActionButtonType::SNOOZE); } void ReminderDataManager::SnoozeReminderImpl(sptr &reminder) @@ -1741,5 +1745,83 @@ void ReminderDataManager::OnConfigurationChanged(const AppExecFwk::Configuration UpdateReminderLanguage(*it); } } + +void ReminderDataManager::OnRemoveAppMgr() +{ + std::lock_guard lock(appMgrMutex_); + appMgrProxy_ = nullptr; +} + +bool ReminderDataManager::ConnectAppMgr() +{ + if (appMgrProxy_ != nullptr) { + return true; + } + + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + ANSR_LOGE("get SystemAbilityManager failed"); + return false; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID); + if (remoteObject == nullptr) { + ANSR_LOGE("get app manager service failed"); + return false; + } + + appMgrProxy_ = iface_cast(remoteObject); + if (!appMgrProxy_ || !appMgrProxy_->AsObject()) { + ANSR_LOGE("get app mgr proxy failed!"); + return false; + } + return true; +} + +void ReminderDataManager::CheckNeedNotifyStatus(const sptr &reminder, + const ReminderRequest::ActionButtonType buttonType) +{ + const std::string bundleName = reminder->GetBundleName(); + if (bundleName.empty()) { + return; + } + ANS_LOGI("notify bundleName is: %{public}s", bundleName.c_str()); + // get foreground application + std::vector apps; + { + std::lock_guard lock(appMgrMutex_); + if (!ConnectAppMgr()) { + return; + } + if (appMgrProxy_->GetForegroundApplications(apps) != ERR_OK) { + ANS_LOGW("get foreground application failed"); + return; + } + } + // notify application + for (auto &eachApp : apps) { + if (eachApp.bundleName != bundleName) { + continue; + } + + EventFwk::Want want; + // common event not add COMMON_EVENT_REMINDER_STATUS_CHANGE, Temporary use of string + want.SetAction("usual.event.REMINDER_STATUS_CHANGE"); + EventFwk::CommonEventData eventData(want); + + std::string data; + data.append(std::to_string(static_cast(buttonType))).append(","); + data.append(std::to_string(reminder->GetReminderId())); + eventData.SetData(data); + + EventFwk::CommonEventPublishInfo info; + info.SetBundleName(bundleName); + if (EventFwk::CommonEventManager::PublishCommonEvent(eventData, info)) { + ANSR_LOGI("notify reminder status change %{public}s", bundleName.c_str()); + } + break; + } +} } } diff --git a/services/ans/src/reminder_event_manager.cpp b/services/ans/src/reminder_event_manager.cpp index 8a8e6ebd0d7b9fb9335109f38e7fcb309cbf761f..05988601abccf696c088d1a0725c09918e9fd2f8 100644 --- a/services/ans/src/reminder_event_manager.cpp +++ b/services/ans/src/reminder_event_manager.cpp @@ -75,6 +75,14 @@ void ReminderEventManager::init(std::shared_ptr &reminderDa ANSR_LOGE("Failed to create statusChangeListener due to no memory."); return; } + // app mgr + sptr appMgrStatusChangeListener + = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); + if (appMgrStatusChangeListener == nullptr) { + ANSR_LOGE("Failed to create appMgrStatusChangeListener due to no memory."); + return; + } + sptr samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgrProxy == nullptr) { ANSR_LOGD("samgrProxy is null"); @@ -84,6 +92,10 @@ void ReminderEventManager::init(std::shared_ptr &reminderDa if (ret != ERR_OK) { ANSR_LOGE("subscribe system ability id: %{public}d failed", BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); } + ret = samgrProxy->SubscribeSystemAbility(APP_MGR_SERVICE_ID, appMgrStatusChangeListener); + if (ret != ERR_OK) { + ANSR_LOGE("subscribe system ability id: %{public}d failed", APP_MGR_SERVICE_ID); + } } ReminderEventManager::ReminderEventSubscriber::ReminderEventSubscriber( @@ -213,13 +225,34 @@ void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility int32_t systemAbilityId, const std::string& deviceId) { ANSR_LOGD("OnAddSystemAbilityInner"); - reminderDataManager_->OnServiceStart(); + switch (systemAbilityId) { + case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID: + ANSR_LOGD("OnAddSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY"); + reminderDataManager_->OnServiceStart(); + break; + case APP_MGR_SERVICE_ID: + ANSR_LOGD("OnAddSystemAbilityInner: APP_MGR_SERVICE"); + break; + default: + break; + } } void ReminderEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( int32_t systemAbilityId, const std::string& deviceId) { ANSR_LOGD("OnRemoveSystemAbilityInner"); + switch (systemAbilityId) { + case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID: + ANSR_LOGD("OnRemoveSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY"); + break; + case APP_MGR_SERVICE_ID: + ANSR_LOGD("OnRemoveSystemAbilityInner: APP_MGR_SERVICE"); + reminderDataManager_->OnRemoveAppMgr(); + break; + default: + break; + } } } // namespace OHOS } // namespace Notification