From 4cae678f5f0d091a6f4ea8b7adde1fea77e7c008 Mon Sep 17 00:00:00 2001 From: mubaoyu Date: Wed, 25 Oct 2023 10:30:30 +0800 Subject: [PATCH 1/2] test --- README_zh.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README_zh.md b/README_zh.md index d5d49c4c9..4d5f88ad6 100644 --- a/README_zh.md +++ b/README_zh.md @@ -1,5 +1,5 @@ # 通知子系统 - +# tes ## 简介 OpenHarmony通过ANS(Advanced Notification Service,通知系统服务)对通知类型的消息进行管理,支持多种通知类型,包括文本,长文本,多文本,图片,社交,媒体等。所有系统服务以及应用都可以通过通知接口发送通知消息,用户可以通过SystemUI查看所有通知消息。 @@ -61,4 +61,4 @@ ANS支持4种类型的通道,分别为: [SystemUI系统应用](https://gitee.com/openharmony/applications_systemui/blob/master/README_zh.md) -[元能力子系统](https://gitee.com/openharmony/ability_ability_runtime/blob/master/README_zh.md) \ No newline at end of file +[元能力子系统](https://gitee.com/openharmony/ability_ability_runtime/blob/master/README_zh.md) -- Gitee From fa93f19f9117864f5bd361a97ac9e29ccc5242bf Mon Sep 17 00:00:00 2001 From: mubaoyu Date: Wed, 25 Oct 2023 16:13:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?add=20group=20id=20interface=20Signed-off-b?= =?UTF-8?q?y:=20=E5=BF=AB=E4=B9=90=E7=9A=84pro=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README_zh.md | 1 - frameworks/ans/src/reminder_request.cpp | 28 +++++++++ frameworks/ans/src/reminder_store.cpp | 53 ++++++++++++++++- .../napi/include/reminder/reminder_common.h | 1 + frameworks/js/napi/src/reminder/publish.cpp | 4 ++ .../js/napi/src/reminder/reminder_common.cpp | 7 +++ interfaces/inner_api/reminder_request.h | 22 +++++++ services/ans/include/reminder_data_manager.h | 21 +++++++ services/ans/src/reminder_data_manager.cpp | 57 +++++++++++++++++++ 9 files changed, 191 insertions(+), 3 deletions(-) diff --git a/README_zh.md b/README_zh.md index 4d5f88ad6..32f891a56 100644 --- a/README_zh.md +++ b/README_zh.md @@ -1,5 +1,4 @@ # 通知子系统 -# tes ## 简介 OpenHarmony通过ANS(Advanced Notification Service,通知系统服务)对通知类型的消息进行管理,支持多种通知类型,包括文本,长文本,多文本,图片,社交,媒体等。所有系统服务以及应用都可以通过通知接口发送通知消息,用户可以通过SystemUI查看所有通知消息。 diff --git a/frameworks/ans/src/reminder_request.cpp b/frameworks/ans/src/reminder_request.cpp index d5f1af06d..a32209cbe 100644 --- a/frameworks/ans/src/reminder_request.cpp +++ b/frameworks/ans/src/reminder_request.cpp @@ -104,6 +104,7 @@ const std::string ReminderRequest::AGENT = "agent"; const std::string ReminderRequest::MAX_SCREEN_AGENT = "maxScreen_agent"; const std::string ReminderRequest::TAP_DISMISSED = "tapDismissed"; const std::string ReminderRequest::AUTO_DELETED_TIME = "autoDeletedTime"; +const std::string ReminderRequest::GROUP_ID = "groupId"; std::string ReminderRequest::sqlOfAddColumns = ""; std::vector ReminderRequest::columns; @@ -140,6 +141,7 @@ ReminderRequest::ReminderRequest(const ReminderRequest &other) this->tapDismissed_= other.tapDismissed_; this->autoDeletedTime_ = other.autoDeletedTime_; this->customButtonUri_ = other.customButtonUri_; + this->groupId_ = other.groupId_; } ReminderRequest::ReminderRequest(int32_t reminderId) @@ -715,6 +717,12 @@ ReminderRequest& ReminderRequest::SetNotificationId(int32_t notificationId) return *this; } +ReminderRequest& ReminderRequest::SetGroupId(int32_t groupId) +{ + groupId_ = groupId; + return *this; +} + ReminderRequest& ReminderRequest::SetSlotType(const NotificationConstant::SlotType &slotType) { slotType_ = slotType; @@ -814,6 +822,11 @@ int32_t ReminderRequest::GetNotificationId() const return notificationId_; } +int32_t ReminderRequest::GetGroupId() const +{ + return groupId_; +} + sptr ReminderRequest::GetNotificationRequest() const { return notificationRequest_; @@ -1075,6 +1088,10 @@ bool ReminderRequest::Marshalling(Parcel &parcel) const ANSR_LOGE("Failed to write notificationId"); return false; } + if (!parcel.WriteInt32(groupId_)) { + ANSR_LOGE("Failed to write groupId"); + return false; + } if (!parcel.WriteUint64(triggerTimeInMilli_)) { ANSR_LOGE("Failed to write triggerTimeInMilli"); return false; @@ -1248,6 +1265,10 @@ bool ReminderRequest::ReadFromParcel(Parcel &parcel) ANSR_LOGE("Failed to read notificationId"); return false; } + if (!parcel.ReadInt32(groupId_)) { + ANSR_LOGE("Failed to read groupId"); + return false; + } if (!parcel.ReadUint64(triggerTimeInMilli_)) { ANSR_LOGE("Failed to read triggerTimeInMilli"); return false; @@ -1620,6 +1641,11 @@ void ReminderRequest::SetState(bool deSet, const uint8_t newState, std::string f reminderId_, GetState(oldState).c_str(), GetState(state_).c_str(), function.c_str()); } +void ReminderRequest::SetStateToInActive() +{ + SetState(false, (REMINDER_STATUS_SHOWING | REMINDER_STATUS_ALERTING | REMINDER_STATUS_ACTIVE), "SetStateToInActive"); +} + void ReminderRequest::UpdateActionButtons(const bool &setSnooze) { if (notificationRequest_ == nullptr) { @@ -1844,6 +1870,7 @@ void ReminderRequest::AppendValuesBucket(const sptr &reminder, values.PutString(CONTENT, reminder->GetContent()); values.PutString(SNOOZE_CONTENT, reminder->GetSnoozeContent()); values.PutString(EXPIRED_CONTENT, reminder->GetExpiredContent()); + values.PutInt(GROUP_ID, reminder->GetGroupId()); auto wantAgentInfo = reminder->GetWantAgentInfo(); if (wantAgentInfo == nullptr) { std::string info = "null" + ReminderRequest::SEP_WANT_AGENT + "null" + ReminderRequest::SEP_WANT_AGENT + "null"; @@ -1898,6 +1925,7 @@ void ReminderRequest::InitDbColumns() AddColumn(MAX_SCREEN_AGENT, "TEXT", false); AddColumn(TAP_DISMISSED, "TEXT", false); AddColumn(AUTO_DELETED_TIME, "BIGINT", false); + AddColumn(GROUP_ID, "INT", false); } void ReminderRequest::AddColumn( diff --git a/frameworks/ans/src/reminder_store.cpp b/frameworks/ans/src/reminder_store.cpp index 74da38d7b..aaceb0c17 100644 --- a/frameworks/ans/src/reminder_store.cpp +++ b/frameworks/ans/src/reminder_store.cpp @@ -32,7 +32,7 @@ namespace { const std::string REMINDER_DB_DIR = "/data/service/el1/public/notification/"; const std::string REMINDER_DB_NAME = "notification.db"; const std::string REMINDER_DB_TABLE = "reminder"; -const uint32_t REMINDER_RDB_VERSION = 1; +const uint32_t REMINDER_RDB_VERSION = 2; const int32_t STATE_FAIL = -1; std::vector columns; } @@ -51,6 +51,11 @@ int32_t ReminderStore::ReminderStoreDataCallBack::OnCreate(NativeRdb::RdbStore & int32_t ReminderStore::ReminderStoreDataCallBack::OnUpgrade( NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) { + ANSR_LOGI("OnUpgrade oldVersion is %{public}d, newVersion is %{public}d", oldVersion, newVersion); + if (oldVersion < newVersion && newVersion == REMINDER_RDB_VERSION) { + store.ExecuteSql("ALTER TABLE " + REMINDER_DB_TABLE + " ADD groupId INT DEFAULT -1;"); + } + store.SetVersion(newVersion); return NativeRdb::E_OK; } @@ -310,7 +315,51 @@ int32_t ReminderStore::GetMaxId() std::vector> ReminderStore::GetAllValidReminders() { - std::string queryCondition = "select * from " + REMINDER_DB_TABLE + " where " + std::string queryCondition = "select reminder_id,\ + package_name,\ + user_id,\ + uid,\ + system_app,\ + app_label,\ + reminder_type,\ + reminder_time,\ + trigger_time,\ + rtc_trigger_time,\ + time_interval,\ + snooze_times,\ + dynamic_snooze_times,\ + ring_duration,\ + is_expired,\ + is_active,\ + state,\ + zone_id,\ + has_ScheduledTimeout,\ + button_info,\ + custom_button_uri,\ + slot_id,\ + notification_id,\ + title,\ + content,\ + snooze_content,\ + expired_content,\ + agent,\ + maxScreen_agent,\ + tapDismissed,\ + autoDeletedTime,\ + groupId,\ + repeat_days,\ + repeat_months,\ + first_designate_year,\ + first_designate_month,\ + first_designate_day,\ + calendar_year,\ + calendar_month,\ + calendar_day,\ + calendar_hour,\ + calendar_minute,\ + repeat_days_of_week,\ + alarm_hour,\ + alarm_minute from " + REMINDER_DB_TABLE + " where " + ReminderRequest::IS_EXPIRED + " is false order by " + ReminderRequest::TRIGGER_TIME + " asc"; ANSR_LOGD("Get all reminders"); diff --git a/frameworks/js/napi/include/reminder/reminder_common.h b/frameworks/js/napi/include/reminder/reminder_common.h index 5d0270706..90a69d78b 100644 --- a/frameworks/js/napi/include/reminder/reminder_common.h +++ b/frameworks/js/napi/include/reminder/reminder_common.h @@ -70,6 +70,7 @@ const char* BUTTON_DATA_SHARE_UPDATE_EQUALTO = "equalTo"; const char* BUTTON_DATA_SHARE_UPDATE_VALUE = "value"; const char* TAPDISMISSED = "tapDismissed"; const char* AUTODELETEDTIME = "autoDeletedTime"; +const char* GROUP_ID = "groupId"; const int INDEX_KEY = 0; const int INDEX_TYPE = 1; const int INDEX_VALUE = 2; diff --git a/frameworks/js/napi/src/reminder/publish.cpp b/frameworks/js/napi/src/reminder/publish.cpp index 5814cc93e..9a28f4754 100644 --- a/frameworks/js/napi/src/reminder/publish.cpp +++ b/frameworks/js/napi/src/reminder/publish.cpp @@ -756,6 +756,10 @@ napi_value SetValidReminder(const napi_env &env, ReminderRequest &reminder, napi // actionButtons ParseActionButtons(env, reminder, result); return NotificationNapi::Common::NapiGetBoolean(env, true); + + // group id + napi_create_int32(env, reminder.GetGroupId(), &value); + napi_set_named_property(env, result, GROUP_ID, value); } void GetValidRemindersInner(napi_env env, const std::vector>& validReminders, napi_value& arr) diff --git a/frameworks/js/napi/src/reminder/reminder_common.cpp b/frameworks/js/napi/src/reminder/reminder_common.cpp index 755e4ddff..ed558acb0 100644 --- a/frameworks/js/napi/src/reminder/reminder_common.cpp +++ b/frameworks/js/napi/src/reminder/reminder_common.cpp @@ -520,6 +520,13 @@ napi_value ReminderCommon::GenReminder( if (!GenActionButtons(env, value, reminder, isSysApp)) { return nullptr; } + + // group id + int32_t groupId = -1; + if (GetInt32(env, value, ReminderAgentNapi::GROUP_ID, groupId, false)) { + reminder->SetGroupId(groupId); + } + return NotificationNapi::Common::NapiGetNull(env); } diff --git a/interfaces/inner_api/reminder_request.h b/interfaces/inner_api/reminder_request.h index 3da5a5639..c6707cc5d 100644 --- a/interfaces/inner_api/reminder_request.h +++ b/interfaces/inner_api/reminder_request.h @@ -231,6 +231,13 @@ public: */ int32_t GetNotificationId() const; + /** + * @brief Obtains group id. + * + * @return group id. + */ + int32_t GetGroupId() const; + /** * @brief Obtains notification request. * @@ -388,6 +395,11 @@ public: */ void OnSameNotificationIdCovered(); + /** + * Set the reminder state is InActive, so that it will be removed when expired + */ + void SetStateToInActive(); + /** * @brief Shows the reminder on panel. TriggerTime will be updated to next. * @@ -495,6 +507,14 @@ public: */ ReminderRequest& SetNotificationId(int32_t notificationId); + /** + * @brief Sets group id. + * + * @param notificationId Indicates group id. + * @return Current reminder self. + */ + ReminderRequest& SetGroupId(int32_t groupId); + /** * @brief Sets reminder id. * @@ -728,6 +748,7 @@ public: static const std::string MAX_SCREEN_AGENT; static const std::string TAP_DISMISSED; static const std::string AUTO_DELETED_TIME; + static const std::string GROUP_ID; static std::string sqlOfAddColumns; static std::vector columns; @@ -816,6 +837,7 @@ private: uint8_t snoozeTimesDynamic_ {0}; uint8_t state_ {0}; int32_t notificationId_ {0}; + int32_t groupId_ {-1}; int32_t reminderId_ {-1}; int32_t userId_ {-1}; int32_t uid_ {-1}; diff --git a/services/ans/include/reminder_data_manager.h b/services/ans/include/reminder_data_manager.h index 52fbdd48a..625d4d749 100644 --- a/services/ans/include/reminder_data_manager.h +++ b/services/ans/include/reminder_data_manager.h @@ -251,6 +251,16 @@ private: */ void CancelRemindersImplLocked(const std::string &packageName, const int32_t &userId); + /** + * @brief Close reminders with the same group id. + * + * @param oldReminderId Indicates the reminderId that are currently bing showed. + * @param packageName Indicates the packageName need to cancel. + * @param groupId Indicates the group id to cancel. + */ + void CloseRemindersByGroupId(const int32_t &oldReminderId, const std::string &packageName, + const int32_t &groupId); + /** * Cancels the notification relative to the reminder. * @@ -360,6 +370,17 @@ private: */ bool IsMatched(const sptr &reminder, const std::string &packageName, const int32_t &userId) const; + /** + * @brief Judges whether the reminder is matched with the packageName or groupId. + * + * @param reminder Indicates the target reminder. + * @param packageName Indicates the package name. + * @param groupId Indicates the group id. + * @return true If the reminder is matched with the packageName and groupId. + */ + bool IsMatchedForGroupIdAndPkgName(const sptr &reminder, const std::string &packageName, + const int32_t &groupId) const; + bool IsAllowedNotify(const sptr &reminder) const; bool IsReminderAgentReady() const; diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index 514a7db31..8c9078342 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -172,6 +172,20 @@ void ReminderDataManager::CancelRemindersImplLocked(const std::string &packageNa StartRecentReminder(); } +bool ReminderDataManager::IsMatchedForGroupIdAndPkgName(const sptr &reminder, const std::string &packageName, + const int32_t &groupId) const +{ + sptr notification = reminder->GetNotificationRequest(); + if (notification == nullptr) { + ANSR_LOGW("IsMatchedForGroupIdAndPkgName not find the notification"); + return; + } + if (notification->GetCreatorBundleName() == packageName && reminder->GetGroupId() == groupId) { + return true; + } + return false; +} + bool ReminderDataManager::IsMatched(const sptr &reminder, const std::string &packageName, const int32_t &userId) const { @@ -480,10 +494,53 @@ void ReminderDataManager::CloseReminder(const OHOS::EventFwk::Want &want, bool c ANSR_LOGW("Invalid reminder id: %{public}d", reminderId); return; } + sptr notificationRequest = reminder->GetNotificationRequest(); + if (notificationRequest == nullptr) { + ANSR_LOGW("notificationRequest is not find"); + return; + } + std::string bundleName = notificationRequest->GetCreatorBundleName(); + int32_t groupId = reminder->GetGroupId(); + if (groupId == -1) { + ANSR_LOGD("default close reminder, the group id is not set."); + CloseReminder(reminder, cancelNotification); + StartRecentReminder(); + return; + } + CloseRemindersByGroupId(reminderId, bundleName, groupId); CloseReminder(reminder, cancelNotification); StartRecentReminder(); } +void ReminderDataManager::CloseRemindersByGroupId(const int32_t &oldReminderId, const std::string &packageName, + const int32_t &groupId) +{ + if (packageName == "") { + ANSR_LOGD("packageName is empty"); + return; + } + for (auto vit = reminderVector_.begin(); vit != reminderVector_.end();) { + sptr reminder = *vit; + if (reminder == nullptr) { + ANSR_LOGD("reminder is null"); + ++vit; + continue; + } + int32_t reminderId = reminder->GetReminderId(); + if (reminderId == oldReminderId) { + ANSR_LOGD("The old and new reminder are the same"); + ++vit; + continue; + } + if (IsMatchedForGroupIdAndPkgName(reminder, packageName, groupId)) { + reminder->SetExpired(true); + reminder->SetStateToInActive(); + store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); + ANSR_LOGD("Cancel reminders by groupid, reminder is %{public}s", reminder->Dump(),c_str()); + } + } +} + void ReminderDataManager::CloseReminder(const sptr &reminder, bool cancelNotification) { int32_t reminderId = reminder->GetReminderId(); -- Gitee