From a6288b75691ff2c692263ee2d67291e7118d32d0 Mon Sep 17 00:00:00 2001 From: derek Date: Wed, 2 Mar 2022 15:39:12 +0800 Subject: [PATCH 1/8] add database Signed-off-by: derek Change-Id: Ie6eb086e9deb7b29d87340646442a75beb07a3f1 --- frameworks/ans/core/BUILD.gn | 2 + frameworks/ans/native/BUILD.gn | 1 + .../ans/native/src/reminder_request.cpp | 367 +++++++++++++++++- .../ans/native/src/reminder_request_alarm.cpp | 60 +++ .../native/src/reminder_request_calendar.cpp | 116 ++++++ frameworks/ans/native/test/unittest/BUILD.gn | 1 + .../ans/native/include/reminder_request.h | 97 +++++ .../native/include/reminder_request_alarm.h | 25 ++ .../include/reminder_request_calendar.h | 38 ++ .../native/include/reminder_request_timer.h | 8 + interfaces/kits/napi/ans/BUILD.gn | 1 + .../kits/napi/ans/src/reminder/BUILD.gn | 1 + .../advanced_notification_service_ability.h | 2 + services/ans/include/reminder_data_manager.h | 37 +- services/ans/include/reminder_event_manager.h | 11 + .../ans/src/advanced_notification_service.cpp | 28 +- .../advanced_notification_service_ability.cpp | 4 + services/ans/src/reminder_data_manager.cpp | 120 +++++- services/ans/src/reminder_event_manager.cpp | 39 ++ tools/dump/BUILD.gn | 1 + 20 files changed, 925 insertions(+), 34 deletions(-) diff --git a/frameworks/ans/core/BUILD.gn b/frameworks/ans/core/BUILD.gn index fdca50b6d..3294e9ac2 100644 --- a/frameworks/ans/core/BUILD.gn +++ b/frameworks/ans/core/BUILD.gn @@ -78,6 +78,7 @@ ohos_shared_library("ans_core") { "${frameworks_path}/ans/native/src/reminder_request_alarm.cpp", "${frameworks_path}/ans/native/src/reminder_request_calendar.cpp", "${frameworks_path}/ans/native/src/reminder_request_timer.cpp", + "${frameworks_path}/ans/native/src/reminder_store.cpp", ] configs = [ ":private_config" ] @@ -98,6 +99,7 @@ ohos_shared_library("ans_core") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", "os_account_standard:os_account_innerkits", "samgr_standard:samgr_proxy", "time_native:time_service", diff --git a/frameworks/ans/native/BUILD.gn b/frameworks/ans/native/BUILD.gn index a0aa9d6cc..74f56a311 100644 --- a/frameworks/ans/native/BUILD.gn +++ b/frameworks/ans/native/BUILD.gn @@ -90,6 +90,7 @@ ohos_shared_library("ans_innerkits") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", "samgr_standard:samgr_proxy", ] diff --git a/frameworks/ans/native/src/reminder_request.cpp b/frameworks/ans/native/src/reminder_request.cpp index 7bc0780e0..5e8899ae5 100644 --- a/frameworks/ans/native/src/reminder_request.cpp +++ b/frameworks/ans/native/src/reminder_request.cpp @@ -20,6 +20,7 @@ #include "if_system_ability_manager.h" #include "iservice_registry.h" #include "os_account_manager.h" +#include "reminder_store.h" #include "system_ability_definition.h" #include "want_agent_helper.h" @@ -37,6 +38,7 @@ const uint8_t ReminderRequest::REMINDER_STATUS_ACTIVE = 1; const uint8_t ReminderRequest::REMINDER_STATUS_ALERTING = 2; const uint8_t ReminderRequest::REMINDER_STATUS_SHOWING = 4; const uint8_t ReminderRequest::REMINDER_STATUS_SNOOZE = 8; +const int ReminderRequest::BASE_YEAR = 1900; const std::string ReminderRequest::NOTIFICATION_LABEL = "REMINDER_AGENT"; const std::string ReminderRequest::REMINDER_EVENT_ALARM_ALERT = "ohos.event.notification.reminder.ALARM_ALERT"; const std::string ReminderRequest::REMINDER_EVENT_CLOSE_ALERT = "ohos.event.notification.reminder.CLOSE_ALERT"; @@ -45,13 +47,13 @@ const std::string ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT = "ohos.event.no const std::string ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION = "ohos.event.notification.reminder.REMOVE_NOTIFICATION"; const std::string ReminderRequest::PARAM_REMINDER_ID = "REMINDER_ID"; -const int ReminderRequest::BASE_YEAR = 1900; +const std::string ReminderRequest::SEP_BUTTON_SINGLE = ""; +const std::string ReminderRequest::SEP_BUTTON_MULTI = ""; +const std::string ReminderRequest::SEP_WANT_AGENT = ""; ReminderRequest::ReminderRequest() { - wantAgentInfo_ = wantAgentInfo_ == nullptr ? std::make_shared() : wantAgentInfo_; - maxScreenWantAgentInfo_ = - maxScreenWantAgentInfo_ == nullptr ? std::make_shared() : maxScreenWantAgentInfo_; + InitServerObj(); } ReminderRequest::ReminderRequest(const ReminderRequest &other) @@ -79,12 +81,16 @@ ReminderRequest::ReminderRequest(const ReminderRequest &other) this->actionButtonMap_ = other.actionButtonMap_; } +ReminderRequest::ReminderRequest(int32_t reminderId) +{ + reminderId_ = reminderId; + InitServerObj(); +} + ReminderRequest::ReminderRequest(ReminderType reminderType) { reminderType_ = reminderType; - wantAgentInfo_ = wantAgentInfo_ == nullptr ? std::make_shared() : wantAgentInfo_; - maxScreenWantAgentInfo_ = - maxScreenWantAgentInfo_ == nullptr ? std::make_shared() : maxScreenWantAgentInfo_; + InitServerObj(); } bool ReminderRequest::CanRemove() const @@ -169,6 +175,11 @@ void ReminderRequest::InitUserId(const int &userId) userId_ = userId; } +void ReminderRequest::InitUid(const int32_t &uid) +{ + uid_ = uid; +} + bool ReminderRequest::IsExpired() const { return isExpired_; @@ -372,6 +383,165 @@ bool ReminderRequest::OnTimeZoneChange() triggerTimeInMilli_, GetDurationSinceEpochInMilli(newZoneTriggerTime), nextTriggerTime); } +int64_t ReminderRequest::RecoveryInt64FromDb(const std::shared_ptr &resultSet, + const std::string &columnName, const DbRecoveryType &columnType) +{ + if (columnType == DbRecoveryType::INT) { + int value; + resultSet->GetInt(ReminderStore::GetColumnIndex(columnName), value); + return static_cast(value); + } + if (columnType == DbRecoveryType::LONG) { + int64_t value; + resultSet->GetLong(ReminderStore::GetColumnIndex(columnName), value); + return value; + } + ANSR_LOGE("Recovery data error"); + return 0; +} + +void ReminderRequest::RecoveryFromDb(const std::shared_ptr &resultSet) +{ + // reminderId + resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::REMINDER_ID), reminderId_); + + // userId + resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::USER_ID), userId_); + + // bundleName + resultSet->GetString(ReminderStore::GetColumnIndex(Instance::PKG_NAME), bundleName_); + + // uid + resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::UID), uid_); + + // reminderType + int reminderType; + resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::REMINDER_TYPE), reminderType); + reminderType_ = ReminderType(reminderType); + + // reminderTime + reminderTimeInMilli_ = + static_cast(RecoveryInt64FromDb(resultSet, Instance::REMINDER_TIME, DbRecoveryType::LONG)); + + // triggerTime + triggerTimeInMilli_ = + static_cast(RecoveryInt64FromDb(resultSet, Instance::TRIGGER_TIME, DbRecoveryType::LONG)); + + // timeInterval + uint64_t timeIntervalInSecond = + static_cast(RecoveryInt64FromDb(resultSet, Instance::TIME_INTERVAL, DbRecoveryType::LONG)); + SetTimeInterval(timeIntervalInSecond); + + // snoozeTimes + snoozeTimes_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::SNOOZE_TIMES, DbRecoveryType::INT)); + + // dynamicSnoozeTimes + snoozeTimesDynamic_ = + static_cast(RecoveryInt64FromDb(resultSet, Instance::DYNAMIC_SNOOZE_TIMES, DbRecoveryType::INT)); + + // ringDuration + uint64_t ringDurationInSecond = + static_cast(RecoveryInt64FromDb(resultSet, Instance::RING_DURATION, DbRecoveryType::LONG)); + SetRingDuration(ringDurationInSecond); + + // isExpired + std::string isExpired; + resultSet->GetString(ReminderStore::GetColumnIndex(Instance::IS_EXPIRED), isExpired); + isExpired_ = isExpired == "true" ? true : false; + + // state + // RecoveryUint8FromDb(resultSet, Instance::STATE, DbRecoveryType::INT, state_); + state_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::STATE, DbRecoveryType::INT)); + + // action buttons + RecoveryActionButton(resultSet); + + // slotType + int slotType; + resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::SLOT_ID), slotType); + slotType_ = NotificationConstant::SlotType(slotType); + + // notification id + resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::NOTIFICATION_ID), notificationId_); + + // title + resultSet->GetString(ReminderStore::GetColumnIndex(Instance::TITLE), title_); + + // content + resultSet->GetString(ReminderStore::GetColumnIndex(Instance::CONTENT), content_); + + // snoozeContent + resultSet->GetString(ReminderStore::GetColumnIndex(Instance::SNOOZE_CONTENT), snoozeContent_); + + // expiredContent + resultSet->GetString(ReminderStore::GetColumnIndex(Instance::EXPIRED_CONTENT), expiredContent_); + + InitNotificationRequest(); // must set before wantAgent & maxScreenWantAgent + + // wantAgent + std::string wantAgent; + resultSet->GetString(ReminderStore::GetColumnIndex(Instance::AGENT), wantAgent); + RecoveryWantAgent(wantAgent, 0); + + // maxScreenWantAgent + std::string maxScreenWantAgent; + resultSet->GetString(ReminderStore::GetColumnIndex(Instance::MAX_SCREEN_AGENT), maxScreenWantAgent); + RecoveryWantAgent(wantAgent, 1); +} + +void ReminderRequest::RecoveryActionButton(const std::shared_ptr &resultSet) +{ + std::string actionButtonInfo; + resultSet->GetString(ReminderStore::GetColumnIndex(Instance::ACTION_BUTTON_INFO), actionButtonInfo); + std::vector multiButton = StringSplit(actionButtonInfo, SEP_BUTTON_MULTI); + for (auto it = multiButton.begin(); it != multiButton.end(); ++it) { + std::vector singleButton = StringSplit(*it, SEP_BUTTON_SINGLE); + SetActionButton(singleButton.at(1), ActionButtonType(std::stoi(singleButton.at(0), nullptr))); + } +} + +std::vector ReminderRequest::StringSplit(std::string source, const std::string &split) const +{ + size_t pos = 0; + std::vector result; + while ((pos = source.find(split)) != std::string::npos) { + std::string token = source.substr(0, pos); + if (!token.empty()) { + result.push_back(token); + } + source.erase(0, pos + split.length()); + } + if (!source.empty()) { + result.push_back(source); + } + return result; +} + +void ReminderRequest::RecoveryWantAgent(std::string wantAgentInfo, const uint8_t &type) +{ + std::vector info = StringSplit(wantAgentInfo, ReminderRequest::SEP_WANT_AGENT); + uint8_t minLen = 2; + if (info.size() < minLen) { + ANSR_LOGW("RecoveryWantAgent fail"); + return; + } + // AppExecFwk::ElementName wantAgent("", info.at(0), info.at(1)); + ANSR_LOGD("pkg=%{public}s, ability=%{public}s", info.at(0).c_str(), info.at(1).c_str()); + if (type == 0) { + auto wantAgentInfo = std::make_shared(); + wantAgentInfo->pkgName = info.at(0); + wantAgentInfo->abilityName = info.at(1); + SetWantAgentInfo(wantAgentInfo); + } else if (type == 1) { + auto maxScreenWantAgentInfo = std::make_shared(); + maxScreenWantAgentInfo->pkgName = info.at(0); + maxScreenWantAgentInfo->abilityName = info.at(1); + SetMaxScreenWantAgentInfo(maxScreenWantAgentInfo); + } else { + ANSR_LOGW("RecoveryWantAgent type not support"); + } +} + ReminderRequest& ReminderRequest::SetMaxScreenWantAgentInfo( const std::shared_ptr &maxScreenWantAgentInfo) { @@ -458,6 +628,11 @@ bool ReminderRequest::ShouldShowImmediately() const return true; } +uint8_t ReminderRequest::GetConstStateInactive() +{ + return REMINDER_STATUS_INACTIVE; +} + std::map ReminderRequest::GetActionButtons( ) const { @@ -560,6 +735,16 @@ uint64_t ReminderRequest::GetTriggerTimeInMilli() const return triggerTimeInMilli_; } +int ReminderRequest::GetUserId() const +{ + return userId_; +} + +int32_t ReminderRequest::GetUid() const +{ + return uid_; +} + std::shared_ptr ReminderRequest::GetWantAgentInfo() const { return wantAgentInfo_; @@ -606,6 +791,11 @@ void ReminderRequest::UpdateNotificationRequest(UpdateNotificationType type, std SetMaxScreenWantAgent(maxScreenWantAgent); break; } + case UpdateNotificationType::BUNDLE_INFO: { + ANSR_LOGI("UpdateNotification hap information"); + UpdateNotificationBundleInfo(); + break; + } case UpdateNotificationType::CONTENT: { break; } @@ -870,6 +1060,13 @@ bool ReminderRequest::InitNotificationRequest() return true; } +void ReminderRequest::InitServerObj() +{ + wantAgentInfo_ = wantAgentInfo_ == nullptr ? std::make_shared() : wantAgentInfo_; + maxScreenWantAgentInfo_ = + maxScreenWantAgentInfo_ == nullptr ? std::make_shared() : maxScreenWantAgentInfo_; +} + bool ReminderRequest::IsAlerting() const { return (state_ & REMINDER_STATUS_ALERTING) != 0; @@ -892,6 +1089,21 @@ std::string ReminderRequest::GetDateTimeInfo(const time_t &timeInSecond) const return GetTimeInfoInner(timeInSecond, TimeFormat::YMDHMS); } +std::string ReminderRequest::GetButtonInfo() const +{ + std::string info = ""; + bool isFirst = true; + for (auto it = actionButtonMap_.begin(); it != actionButtonMap_.end(); ++it) { + if (!isFirst) { + info += SEP_BUTTON_MULTI; + } + ActionButtonInfo buttonInfo = it->second; + info += std::to_string(static_cast(it->first)) + SEP_BUTTON_SINGLE + buttonInfo.title; + isFirst = false; + } + return info; +} + uint64_t ReminderRequest::GetNowInstantMilli() const { time_t now; @@ -1121,7 +1333,7 @@ void ReminderRequest::UpdateNotificationCommon() { time_t now; (void)time(&now); // unit is seconds. - notificationRequest_->SetDeliveryTime(static_cast(now) * MILLI_SECONDS); + notificationRequest_->SetDeliveryTime(GetDurationSinceEpochInMilli(now)); notificationRequest_->SetLabel(NOTIFICATION_LABEL); notificationRequest_->SetShowDeliveryTime(true); notificationRequest_->SetTapDismissed(true); @@ -1141,6 +1353,24 @@ void ReminderRequest::UpdateNotificationCommon() notificationRequest_->SetFlags(flags); } +void ReminderRequest::UpdateNotificationBundleInfo() +{ + ANSR_LOGD("~~~~Bundle name=%{public}s, bundleName_=%{public}s", + notificationRequest_->GetOwnerBundleName().c_str(), bundleName_.c_str()); + if (!(notificationRequest_->GetOwnerBundleName()).empty()) { + return; + } + + notificationRequest_->SetOwnerBundleName(bundleName_); + notificationRequest_->SetCreatorBundleName(bundleName_); + + notificationRequest_->SetCreatorUid(uid_); + // notificationRequest_->SetCreatorPid(pid); + + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid_, userId_); + notificationRequest_->SetCreatorUserId(userId_); +} + void ReminderRequest::UpdateNotificationContent(const bool &setSnooze) { if (notificationRequest_ == nullptr) { @@ -1153,6 +1383,7 @@ void ReminderRequest::UpdateNotificationContent(const bool &setSnooze) // snooze the reminder by manual extendContent = GetShowTime(triggerTimeInMilli_) + snoozeContent_ == "" ? "" : (" (" + snoozeContent_ + ")"); + notificationRequest_->SetTapDismissed(false); } else { // the reminder is expired now, when timeInterval is 0 extendContent = GetShowTime(reminderTimeInMilli_) + @@ -1247,5 +1478,125 @@ int ReminderRequest::GetUserId(const int &uid) AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId); return userId; } + +void ReminderRequest::AppendValuesBucket(const sptr &reminder, + const sptr &bundleOption, NativeRdb::ValuesBucket &values) +{ + values.PutInt(Instance::REMINDER_ID, reminder->GetReminderId()); + values.PutString(Instance::PKG_NAME, bundleOption->GetBundleName()); + values.PutInt(Instance::USER_ID, reminder->GetUserId()); + values.PutInt(Instance::UID, reminder->GetUid()); + values.PutString(Instance::APP_LABEL, ""); // no use, compatible with old version. + values.PutInt(Instance::REMINDER_TYPE, static_cast(reminder->GetReminderType())); + values.PutLong(Instance::REMINDER_TIME, reminder->GetReminderTimeInMilli()); + values.PutLong(Instance::TRIGGER_TIME, reminder->GetTriggerTimeInMilli()); + values.PutLong( + Instance::RTC_TRIGGER_TIME, reminder->GetTriggerTimeInMilli()); // no use, compatible with old version. + values.PutLong(Instance::TIME_INTERVAL, reminder->GetTimeInterval()); + values.PutInt(Instance::SNOOZE_TIMES, reminder->GetSnoozeTimes()); + values.PutInt(Instance::DYNAMIC_SNOOZE_TIMES, reminder->GetSnoozeTimesDynamic()); + values.PutLong(Instance::RING_DURATION, reminder->GetRingDuration()); + values.PutString(Instance::IS_EXPIRED, reminder->IsExpired() ? "true" : "false"); + values.PutString(Instance::IS_ACTIVE, ""); // no use, compatible with old version. + values.PutInt(Instance::STATE, reminder->GetState()); + values.PutString(Instance::ZONE_ID, ""); // no use, compatible with old version. + values.PutString(Instance::HAS_SCHEDULED_TIMEOUT, ""); // no use, compatible with old version. + values.PutString(Instance::ACTION_BUTTON_INFO, reminder->GetButtonInfo()); + values.PutInt(Instance::SLOT_ID, reminder->GetSlotType()); + values.PutInt(Instance::NOTIFICATION_ID, reminder->GetNotificationId()); + values.PutString(Instance::TITLE, reminder->GetTitle()); + values.PutString(Instance::CONTENT, reminder->GetContent()); + values.PutString(Instance::SNOOZE_CONTENT, reminder->GetSnoozeContent()); + values.PutString(Instance::EXPIRED_CONTENT, reminder->GetExpiredContent()); + auto wantAgentInfo = reminder->GetWantAgentInfo(); + if (wantAgentInfo == nullptr) { + std::string info = "null" + ReminderRequest::SEP_WANT_AGENT + "null"; + values.PutString(Instance::AGENT, info); + } else { + values.PutString(Instance::AGENT, wantAgentInfo->pkgName + + ReminderRequest::SEP_WANT_AGENT + wantAgentInfo->abilityName); + } + auto maxScreenWantAgentInfo = reminder->GetMaxScreenWantAgentInfo(); + if (maxScreenWantAgentInfo == nullptr) { + std::string info = "null" + ReminderRequest::SEP_WANT_AGENT + "null"; + values.PutString(Instance::MAX_SCREEN_AGENT, info); + } else { + values.PutString(Instance::MAX_SCREEN_AGENT, maxScreenWantAgentInfo->pkgName + + ReminderRequest::SEP_WANT_AGENT + maxScreenWantAgentInfo->abilityName); + } +} + +const std::string ReminderRequest::Instance::REMINDER_ID = "reminder_id"; +const std::string ReminderRequest::Instance::PKG_NAME = "package_name"; +const std::string ReminderRequest::Instance::USER_ID = "user_id"; +const std::string ReminderRequest::Instance::UID = "uid"; +const std::string ReminderRequest::Instance::APP_LABEL = "app_label"; +const std::string ReminderRequest::Instance::REMINDER_TYPE = "reminder_type"; +const std::string ReminderRequest::Instance::REMINDER_TIME = "reminder_time"; +const std::string ReminderRequest::Instance::TRIGGER_TIME = "trigger_time"; +const std::string ReminderRequest::Instance::RTC_TRIGGER_TIME = "rtc_trigger_time"; +const std::string ReminderRequest::Instance::TIME_INTERVAL = "time_interval"; +const std::string ReminderRequest::Instance::SNOOZE_TIMES = "snooze_times"; +const std::string ReminderRequest::Instance::DYNAMIC_SNOOZE_TIMES = "dynamic_snooze_times"; +const std::string ReminderRequest::Instance::RING_DURATION = "ring_duration"; +const std::string ReminderRequest::Instance::IS_EXPIRED = "is_expired"; +const std::string ReminderRequest::Instance::IS_ACTIVE = "is_active"; +const std::string ReminderRequest::Instance::STATE = "state"; +const std::string ReminderRequest::Instance::ZONE_ID = "zone_id"; +const std::string ReminderRequest::Instance::HAS_SCHEDULED_TIMEOUT = "has_ScheduledTimeout"; +const std::string ReminderRequest::Instance::ACTION_BUTTON_INFO = "button_info"; +const std::string ReminderRequest::Instance::SLOT_ID = "slot_id"; +const std::string ReminderRequest::Instance::NOTIFICATION_ID = "notification_id"; +const std::string ReminderRequest::Instance::TITLE = "title"; +const std::string ReminderRequest::Instance::CONTENT = "content"; +const std::string ReminderRequest::Instance::SNOOZE_CONTENT = "snooze_content"; +const std::string ReminderRequest::Instance::EXPIRED_CONTENT = "expired_content"; +const std::string ReminderRequest::Instance::AGENT = "agent"; +const std::string ReminderRequest::Instance::MAX_SCREEN_AGENT = "maxScreen_agent"; + +std::string ReminderRequest::Instance::SQL_ADD_COLUMNS = ""; +std::vector ReminderRequest::Instance::COLUMNS; + +void ReminderRequest::Instance::Init() +{ + AddColumn(REMINDER_ID, "INTEGER PRIMARY KEY", false); + AddColumn(PKG_NAME, "TEXT NOT NULL", false); + AddColumn(USER_ID, "INT NOT NULL", false); + AddColumn(UID, "INT NOT NULL", false); + AddColumn(APP_LABEL, "TEXT", false); + AddColumn(REMINDER_TYPE, "INT NOT NULL", false); + AddColumn(REMINDER_TIME, "BIGINT NOT NULL", false); + AddColumn(TRIGGER_TIME, "BIGINT NOT NULL", false); + AddColumn(RTC_TRIGGER_TIME, "BIGINT NOT NULL", false); + AddColumn(TIME_INTERVAL, "BIGINT NOT NULL", false); + AddColumn(SNOOZE_TIMES, "INT NOT NULL", false); + AddColumn(DYNAMIC_SNOOZE_TIMES, "INT NOT NULL", false); + AddColumn(RING_DURATION, "BIGINT NOT NULL", false); + AddColumn(IS_EXPIRED, "TEXT NOT NULL", false); + AddColumn(IS_ACTIVE, "TEXT NOT NULL", false); + AddColumn(STATE, "INT NOT NULL", false); + AddColumn(ZONE_ID, "TEXT", false); + AddColumn(HAS_SCHEDULED_TIMEOUT, "TEXT", false); + AddColumn(ACTION_BUTTON_INFO, "TEXT", false); + AddColumn(SLOT_ID, "INT", false); + AddColumn(NOTIFICATION_ID, "INT NOT NULL", false); + AddColumn(TITLE, "TEXT", false); + AddColumn(CONTENT, "TEXT", false); + AddColumn(SNOOZE_CONTENT, "TEXT", false); + AddColumn(EXPIRED_CONTENT, "TEXT", false); + AddColumn(AGENT, "TEXT", false); + AddColumn(MAX_SCREEN_AGENT, "TEXT", false); +} + +void ReminderRequest::Instance::AddColumn( + const std::string &name, const std::string &type, const bool &isEnd) +{ + COLUMNS.push_back(name); + if (!isEnd) { + SQL_ADD_COLUMNS += name + " " + type + ", "; + } else { + SQL_ADD_COLUMNS += name + " " + type; + } +} } } \ No newline at end of file diff --git a/frameworks/ans/native/src/reminder_request_alarm.cpp b/frameworks/ans/native/src/reminder_request_alarm.cpp index b1b44f7a6..988ef8459 100644 --- a/frameworks/ans/native/src/reminder_request_alarm.cpp +++ b/frameworks/ans/native/src/reminder_request_alarm.cpp @@ -16,6 +16,7 @@ #include "reminder_request_alarm.h" #include "ans_log_wrapper.h" +#include "reminder_store.h" namespace OHOS { namespace Notification { @@ -296,5 +297,64 @@ bool ReminderRequestAlarm::ReadFromParcel(Parcel &parcel) ANSR_LOGD("hour_=%{public}d, minute_=%{public}d, repeatDays_=%{public}d", hour_, minute_, repeatDays_); return true; } + +void ReminderRequestAlarm::RecoveryFromDb(const std::shared_ptr &resultSet) +{ + ReminderRequest::RecoveryFromDb(resultSet); + + // repeatDays + repeatDays_ = + static_cast(RecoveryInt64FromDb(resultSet, Instance::REPEAT_DAYS_OF_WEEK, DbRecoveryType::INT)); + + // hour + hour_ = + static_cast(RecoveryInt64FromDb(resultSet, Instance::ALARM_HOUR, DbRecoveryType::INT)); + + // minute + minute_ = + static_cast(RecoveryInt64FromDb(resultSet, Instance::ALARM_MINUTE, DbRecoveryType::INT)); +} + +void ReminderRequestAlarm::AppendValuesBucket(const sptr &reminder, + const sptr &bundleOption, NativeRdb::ValuesBucket &values) +{ + uint8_t repeatDays = 0; + uint8_t hour = 0; + uint8_t minute = 0; + if (reminder->GetReminderType() == ReminderRequest::ReminderType::ALARM) { + ReminderRequestAlarm* alarm = static_cast(reminder.GetRefPtr()); + repeatDays = alarm->GetRepeatDay(); + hour = alarm->GetHour(); + minute = alarm->GetMinute(); + } + values.PutInt(Instance::REPEAT_DAYS_OF_WEEK, repeatDays); + values.PutInt(Instance::ALARM_HOUR, hour); + values.PutInt(Instance::ALARM_MINUTE, minute); +} + +const std::string ReminderRequestAlarm::Instance::REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; +const std::string ReminderRequestAlarm::Instance::ALARM_HOUR = "alarm_hour"; +const std::string ReminderRequestAlarm::Instance::ALARM_MINUTE = "alarm_minute"; + +std::string ReminderRequestAlarm::Instance::SQL_ADD_COLUMNS = ""; +std::vector ReminderRequestAlarm::Instance::COLUMNS; + +void ReminderRequestAlarm::Instance::Init() +{ + AddColumn(REPEAT_DAYS_OF_WEEK, "INT", false); + AddColumn(ALARM_HOUR, "INT", false); + AddColumn(ALARM_MINUTE, "INT", true); +} + +void ReminderRequestAlarm::Instance::AddColumn( + const std::string &name, const std::string &type, const bool &isEnd) +{ + COLUMNS.push_back(name); + if (!isEnd) { + SQL_ADD_COLUMNS += name + " " + type + ", "; + } else { + SQL_ADD_COLUMNS += name + " " + type; + } +} } } \ No newline at end of file diff --git a/frameworks/ans/native/src/reminder_request_calendar.cpp b/frameworks/ans/native/src/reminder_request_calendar.cpp index aa180640d..4f61f745b 100644 --- a/frameworks/ans/native/src/reminder_request_calendar.cpp +++ b/frameworks/ans/native/src/reminder_request_calendar.cpp @@ -498,5 +498,121 @@ bool ReminderRequestCalendar::ReadFromParcel(Parcel &parcel) } return true; } + +void ReminderRequestCalendar::RecoveryFromDb(const std::shared_ptr &resultSet) +{ + ReminderRequest::RecoveryFromDb(resultSet); + + // repeatDay + repeatDay_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::REPEAT_DAYS, DbRecoveryType::INT)); + + // repeatMonth + repeatMonth_ = + static_cast(RecoveryInt64FromDb(resultSet, Instance::REPEAT_MONTHS, DbRecoveryType::INT)); + + // firstDesignateYear + firstDesignateYear_ = + static_cast(RecoveryInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_YEAR, DbRecoveryType::INT)); + + // firstDesignateMonth + firstDesignateMonth_ = + static_cast(RecoveryInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_MONTH, DbRecoveryType::INT)); + + // firstDesignateDay + firstDesignateDay_ = + static_cast(RecoveryInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_DAY, DbRecoveryType::INT)); + + // year + year_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::CALENDAR_YEAR, DbRecoveryType::INT)); + + // month + month_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::CALENDAR_MONTH, DbRecoveryType::INT)); + + // day + day_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::CALENDAR_DAY, DbRecoveryType::INT)); + + // hour + hour_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::CALENDAR_HOUR, DbRecoveryType::INT)); + + // minute + minute_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::CALENDAR_MINUTE, DbRecoveryType::INT)); +} + +void ReminderRequestCalendar::AppendValuesBucket(const sptr &reminder, + const sptr &bundleOption, NativeRdb::ValuesBucket &values) +{ + uint32_t repeatDay = 0; + uint16_t repeatMonth = 0; + uint16_t firstDesignateYear = 0; + uint8_t firstDesignateMonth = 0; + uint8_t firstDesignateDay = 0; + uint16_t year = 0; + uint8_t month = 0; + uint8_t day = 0; + uint8_t hour = 0; + uint8_t minute = 0; + if (reminder->GetReminderType() == ReminderRequest::ReminderType::CALENDAR) { + ReminderRequestCalendar* calendar = static_cast(reminder.GetRefPtr()); + repeatDay = calendar->GetRepeatDay(); + repeatMonth = calendar->GetRepeatMonth(); + firstDesignateYear = calendar->GetFirstDesignateYear(); + firstDesignateMonth = calendar->GetFirstDesignageMonth(); + firstDesignateDay = calendar->GetFirstDesignateDay(); + year = calendar->GetYear(); + month = calendar->GetMonth(); + day = calendar->GetDay(); + hour = calendar->GetHour(); + minute = calendar->GetMinute(); + } + values.PutInt(Instance::REPEAT_DAYS, repeatDay); + values.PutInt(Instance::REPEAT_MONTHS, repeatMonth); + values.PutInt(Instance::FIRST_DESIGNATE_YEAR, firstDesignateYear); + values.PutInt(Instance::FIRST_DESIGNATE_MONTH, firstDesignateMonth); + values.PutInt(Instance::FIRST_DESIGNATE_DAY, firstDesignateDay); + values.PutInt(Instance::CALENDAR_YEAR, year); + values.PutInt(Instance::CALENDAR_MONTH, month); + values.PutInt(Instance::CALENDAR_DAY, day); + values.PutInt(Instance::CALENDAR_HOUR, hour); + values.PutInt(Instance::CALENDAR_MINUTE, minute); +} + +const std::string ReminderRequestCalendar::Instance::REPEAT_DAYS = "repeat_days"; +const std::string ReminderRequestCalendar::Instance::REPEAT_MONTHS = "repeat_months"; +const std::string ReminderRequestCalendar::Instance::FIRST_DESIGNATE_YEAR = "first_designate_year"; +const std::string ReminderRequestCalendar::Instance::FIRST_DESIGNATE_MONTH = "first_designate_month"; +const std::string ReminderRequestCalendar::Instance::FIRST_DESIGNATE_DAY = "first_designate_day"; +const std::string ReminderRequestCalendar::Instance::CALENDAR_YEAR = "calendar_year"; +const std::string ReminderRequestCalendar::Instance::CALENDAR_MONTH = "calendar_month"; +const std::string ReminderRequestCalendar::Instance::CALENDAR_DAY = "calendar_day"; +const std::string ReminderRequestCalendar::Instance::CALENDAR_HOUR = "calendar_hour"; +const std::string ReminderRequestCalendar::Instance::CALENDAR_MINUTE = "calendar_minute"; + +std::string ReminderRequestCalendar::Instance::SQL_ADD_COLUMNS = ""; +std::vector ReminderRequestCalendar::Instance::COLUMNS; + +void ReminderRequestCalendar::Instance::Init() +{ + AddColumn(REPEAT_DAYS, "INT", false); + AddColumn(REPEAT_MONTHS, "INT", false); + AddColumn(FIRST_DESIGNATE_YEAR, "INT", false); + AddColumn(FIRST_DESIGNATE_MONTH, "INT", false); + AddColumn(FIRST_DESIGNATE_DAY, "INT", false); + AddColumn(CALENDAR_YEAR, "INT", false); + AddColumn(CALENDAR_MONTH, "INT", false); + AddColumn(CALENDAR_DAY, "INT", false); + AddColumn(CALENDAR_HOUR, "INT", false); + AddColumn(CALENDAR_MINUTE, "INT", false); +} + +void ReminderRequestCalendar::Instance::AddColumn( + const std::string &name, const std::string &type, const bool &isEnd) +{ + COLUMNS.push_back(name); + if (!isEnd) { + SQL_ADD_COLUMNS += name + " " + type + ", "; + } else { + SQL_ADD_COLUMNS += name + " " + type; + } +} } } \ No newline at end of file diff --git a/frameworks/ans/native/test/unittest/BUILD.gn b/frameworks/ans/native/test/unittest/BUILD.gn index c0eedd516..15eecb3a8 100644 --- a/frameworks/ans/native/test/unittest/BUILD.gn +++ b/frameworks/ans/native/test/unittest/BUILD.gn @@ -58,6 +58,7 @@ ohos_unittest("ans_reminder_unit_test") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", ] diff --git a/interfaces/innerkits/ans/native/include/reminder_request.h b/interfaces/innerkits/ans/native/include/reminder_request.h index 1ab0b90b0..d6125345e 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request.h +++ b/interfaces/innerkits/ans/native/include/reminder_request.h @@ -19,8 +19,11 @@ #include #include +#include "abs_shared_result_set.h" +#include "notification_bundle_option.h" #include "notification_constant.h" #include "notification_request.h" +#include "values_bucket.h" namespace OHOS { namespace Notification { @@ -75,6 +78,7 @@ public: REMOVAL_WANT_AGENT, WANT_AGENT, MAX_SCREEN_WANT_AGENT, + BUNDLE_INFO, CONTENT }; @@ -129,6 +133,14 @@ public: * @param Indicates the exist reminder. */ explicit ReminderRequest(const ReminderRequest &other); + + /** + * @brief This constructor should only be used in background proxy service process + * when reminder instance recovery from database. + * + * @param reminderId Indicates reminder id. + */ + explicit ReminderRequest(int32_t reminderId); ReminderRequest& operator = (const ReminderRequest &other); virtual ~ReminderRequest() override {}; @@ -256,6 +268,9 @@ public: */ uint64_t GetTriggerTimeInMilli() const; + int GetUserId() const; + int32_t GetUid() const; + /** * @brief Obtains want agent information. * @@ -280,6 +295,16 @@ public: */ void InitUserId(const int &userId); + /** + * @brief Inites reminder uid when publish reminder success. + * + * When system reboot and recovery from database, we cannot get the uid according user id as BMS has not be + * ready. So we need to record the uid in order to create correct bundleOption. + * + * @param uid Indicates the uid which the reminder belong to. + */ + void InitUid(const int32_t &uid); + /** * @brief Check the reminder is alerting or not. * @@ -378,6 +403,15 @@ public: */ virtual bool OnTimeZoneChange(); + /** + * @brief Recovery reminder instance from database record. + * + * @param resultSet Indicates the resultSet with pointer to the row of record data. + */ + virtual void RecoveryFromDb(const std::shared_ptr &resultSet); + void RecoveryActionButton(const std::shared_ptr &resultSet); + void RecoveryWantAgent(std::string wantAgentInfo, const uint8_t &type); + /** * @brief Sets action button. * @@ -525,6 +559,8 @@ public: */ void UpdateNotificationRequest(UpdateNotificationType type, std::string extra); + static uint8_t GetConstStateInactive(); + static int32_t GLOBAL_ID; static const uint64_t INVALID_LONG_LONG_VALUE; static const uint16_t INVALID_U16_VALUE; @@ -563,8 +599,52 @@ public: static uint64_t GetDurationSinceEpochInMilli(const time_t target); static int32_t GetUid(const int &userId, const std::string &bundleName); static int GetUserId(const int &uid); + static void AppendValuesBucket(const sptr &reminder, + const sptr &bundleOption, NativeRdb::ValuesBucket &values); + +class Instance { +public: + const static std::string REMINDER_ID; + const static std::string PKG_NAME; + const static std::string USER_ID; + const static std::string UID; + const static std::string APP_LABEL; + const static std::string REMINDER_TYPE; + const static std::string REMINDER_TIME; + const static std::string TRIGGER_TIME; + const static std::string RTC_TRIGGER_TIME; + const static std::string TIME_INTERVAL; + const static std::string SNOOZE_TIMES; + const static std::string DYNAMIC_SNOOZE_TIMES; + const static std::string RING_DURATION; + const static std::string IS_EXPIRED; + const static std::string IS_ACTIVE; + const static std::string STATE; + const static std::string ZONE_ID; + const static std::string HAS_SCHEDULED_TIMEOUT; + const static std::string ACTION_BUTTON_INFO; + const static std::string SLOT_ID; + const static std::string NOTIFICATION_ID; + const static std::string TITLE; + const static std::string CONTENT; + const static std::string SNOOZE_CONTENT; + const static std::string EXPIRED_CONTENT; + const static std::string AGENT; + const static std::string MAX_SCREEN_AGENT; + + static std::string SQL_ADD_COLUMNS; + static std::vector COLUMNS; + static void Init(); + +private: + static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); +}; protected: + enum class DbRecoveryType : uint8_t { + INT, + LONG + }; ReminderRequest(); explicit ReminderRequest(ReminderType reminderType); std::string GetDateTimeInfo(const time_t &timeInSecond) const; @@ -572,12 +652,15 @@ protected: { return INVALID_LONG_LONG_VALUE; } + int64_t RecoveryInt64FromDb(const std::shared_ptr &resultSet, + const std::string &columnName, const DbRecoveryType &columnType); static const int BASE_YEAR; private: void AddActionButtons(const bool includeSnooze); void AddRemovalWantAgent(); std::shared_ptr CreateWantAgent(AppExecFwk::ElementName &element) const; + std::string GetButtonInfo() const; uint64_t GetNowInstantMilli() const; std::string GetShowTime(const uint64_t showTime) const; std::string GetTimeInfoInner(const time_t &timeInSecond, const TimeFormat &format) const; @@ -585,14 +668,23 @@ private: bool HandleSysTimeChange(uint64_t oriTriggerTime, uint64_t optTriggerTime); bool HandleTimeZoneChange(uint64_t oldZoneTriggerTime, uint64_t newZoneTriggerTime, uint64_t optTriggerTime); bool InitNotificationRequest(); + void InitServerObj(); void SetMaxScreenWantAgent(AppExecFwk::ElementName &element); void SetState(bool deSet, const uint8_t newState, std::string function); void SetWantAgent(AppExecFwk::ElementName &element); + std::vector StringSplit(std::string source, const std::string &split) const; void UpdateActionButtons(const bool &setSnooze); bool UpdateNextReminder(const bool &force); void UpdateNotificationContent(const bool &setSnooze); void UpdateNotificationCommon(); + /** + * @brief Used for reminder recovery from database. + * + * @param bundleName Indicates the third part bundle name. + */ + void UpdateNotificationBundleInfo(); + /** * @brief Update the notification, which will be shown for the "Alerting" reminder. * 1. Update the notification label/content. @@ -613,12 +705,16 @@ private: static const uint8_t REMINDER_STATUS_SHOWING; static const uint8_t REMINDER_STATUS_SNOOZE; static const uint32_t MIN_TIME_INTERVAL_IN_MILLI; + static const std::string SEP_BUTTON_SINGLE; + static const std::string SEP_BUTTON_MULTI; + static const std::string SEP_WANT_AGENT; std::string content_ {}; std::string expiredContent_ {}; std::string snoozeContent_ {}; std::string displayContent_ {}; std::string title_ {}; + std::string bundleName_ {}; bool isExpired_ {false}; uint8_t snoozeTimes_ {0}; uint8_t snoozeTimesDynamic_ {0}; @@ -626,6 +722,7 @@ private: int32_t notificationId_ {0}; int32_t reminderId_ {-1}; int userId_ {-1}; + int32_t uid_ {-1}; // Indicates the reminder has been shown in the past time. // When the reminder has been created but not showed, it is equals to 0. diff --git a/interfaces/innerkits/ans/native/include/reminder_request_alarm.h b/interfaces/innerkits/ans/native/include/reminder_request_alarm.h index 6c943699e..659d798dc 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_alarm.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_alarm.h @@ -40,6 +40,14 @@ public: */ ReminderRequestAlarm(uint8_t hour, uint8_t minute, std::vector daysOfWeek); + /** + * @brief This constructor should only be used in background proxy service process + * when reminder instance recovery from database. + * + * @param reminderId Indicates reminder id. + */ + ReminderRequestAlarm(int32_t reminderId) : ReminderRequest(reminderId) {}; + /** * @brief Copy construct from an exist reminder. * @@ -96,6 +104,23 @@ public: * @return true if read parcel success. */ bool ReadFromParcel(Parcel &parcel) override; + virtual void RecoveryFromDb(const std::shared_ptr &resultSet) override; + static void AppendValuesBucket(const sptr &reminder, + const sptr &bundleOption, NativeRdb::ValuesBucket &values); + +class Instance { +public: + const static std::string REPEAT_DAYS_OF_WEEK; + const static std::string ALARM_HOUR; + const static std::string ALARM_MINUTE; + + static std::string SQL_ADD_COLUMNS; + static std::vector COLUMNS; + static void Init(); + +private: + static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); +}; protected: virtual uint64_t PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const override; diff --git a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h index 33a35feb6..f93882a5a 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h @@ -52,6 +52,14 @@ public: ReminderRequestCalendar(const tm &dateTime, const std::vector &repeatMonths, const std::vector &repeatDays); + /** + * @brief This constructor should only be used in background proxy service process + * when reminder instance recovery from database. + * + * @param reminderId Indicates reminder id. + */ + ReminderRequestCalendar(int32_t reminderId) : ReminderRequest(reminderId) {}; + explicit ReminderRequestCalendar(const ReminderRequestCalendar &other); ReminderRequestCalendar& operator = (const ReminderRequestCalendar &other); ~ReminderRequestCalendar() override {} @@ -133,8 +141,32 @@ public: static const uint8_t MAX_MONTHS_OF_YEAR; static const uint8_t MAX_DAYS_OF_MONTH; + virtual void RecoveryFromDb(const std::shared_ptr &resultSet) override; + static void AppendValuesBucket(const sptr &reminder, + const sptr &bundleOption, NativeRdb::ValuesBucket &values); static uint8_t GetDaysOfMonth(const uint16_t &year, const uint8_t &month); +class Instance { +public: + const static std::string REPEAT_DAYS; + const static std::string REPEAT_MONTHS; + const static std::string FIRST_DESIGNATE_YEAR; + const static std::string FIRST_DESIGNATE_MONTH; + const static std::string FIRST_DESIGNATE_DAY; + const static std::string CALENDAR_YEAR; + const static std::string CALENDAR_MONTH; + const static std::string CALENDAR_DAY; + const static std::string CALENDAR_HOUR; + const static std::string CALENDAR_MINUTE; + + static std::string SQL_ADD_COLUMNS; + static std::vector COLUMNS; + static void Init(); + +private: + static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); +}; + protected: virtual uint64_t PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const override; @@ -144,6 +176,12 @@ private: uint8_t GetNextDay(const uint16_t &settedYear, const uint8_t &settedMonth, const tm &now, const tm &target) const; uint64_t GetNextTriggerTime() const; uint64_t GetNextTriggerTimeAsRepeatReminder(const tm &nowTime, const tm &tarTime) const; + uint32_t GetRepeatDay() const { + return repeatDay_; + } + uint16_t GetRepeatMonth() const { + return repeatMonth_; + } uint64_t GetTimeInstantMilli( uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) const; diff --git a/interfaces/innerkits/ans/native/include/reminder_request_timer.h b/interfaces/innerkits/ans/native/include/reminder_request_timer.h index 6853191db..46ef053f2 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_timer.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_timer.h @@ -33,6 +33,14 @@ public: */ ReminderRequestTimer(uint64_t countDownTimeInSeconds); + /** + * @brief This constructor should only be used in background proxy service process + * when reminder instance recovery from database. + * + * @param reminderId Indicates reminder id. + */ + ReminderRequestTimer(int32_t reminderId) : ReminderRequest(reminderId) {}; + /** * @brief Copy construct from an exist reminder. * diff --git a/interfaces/kits/napi/ans/BUILD.gn b/interfaces/kits/napi/ans/BUILD.gn index 74ee9b535..eed0bf82c 100644 --- a/interfaces/kits/napi/ans/BUILD.gn +++ b/interfaces/kits/napi/ans/BUILD.gn @@ -83,6 +83,7 @@ ohos_shared_library("notification") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "napi:ace_napi", + "native_appdatamgr:native_rdb", ] relative_install_dir = "module" diff --git a/interfaces/kits/napi/ans/src/reminder/BUILD.gn b/interfaces/kits/napi/ans/src/reminder/BUILD.gn index 0cabca867..a8654b15f 100644 --- a/interfaces/kits/napi/ans/src/reminder/BUILD.gn +++ b/interfaces/kits/napi/ans/src/reminder/BUILD.gn @@ -71,6 +71,7 @@ ohos_shared_library("reminderagent") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "napi:ace_napi", + "native_appdatamgr:native_rdb", ] relative_install_dir = "module" diff --git a/services/ans/include/advanced_notification_service_ability.h b/services/ans/include/advanced_notification_service_ability.h index ec5c866d8..236cdeb0e 100644 --- a/services/ans/include/advanced_notification_service_ability.h +++ b/services/ans/include/advanced_notification_service_ability.h @@ -19,6 +19,7 @@ #include "system_ability.h" #include "advanced_notification_service.h" +#include "reminder_data_manager.h" #include "system_ability_definition.h" namespace OHOS { @@ -36,6 +37,7 @@ private: private: sptr service_; + std::shared_ptr reminderAgent_; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/reminder_data_manager.h b/services/ans/include/reminder_data_manager.h index ab90a999e..941600bde 100644 --- a/services/ans/include/reminder_data_manager.h +++ b/services/ans/include/reminder_data_manager.h @@ -22,13 +22,16 @@ #include "advanced_notification_service.h" #include "player.h" #include "reminder_request.h" +#include "reminder_store.h" #include "reminder_timer_info.h" namespace OHOS { namespace Notification { class ReminderDataManager final { public: - ReminderDataManager() {}; + ReminderDataManager() { + Init(false); + }; ~ReminderDataManager() {}; ReminderDataManager(ReminderDataManager &other) = delete; @@ -72,6 +75,8 @@ public: * @return Single instance of ReminderDataManager. */ static std::shared_ptr GetInstance(); + static std::shared_ptr InitInstance( + sptr &advancedNotificationService); /** * Obtains all the valid reminders (which are not expired) relative to the bundle option. @@ -82,6 +87,15 @@ public: void GetValidReminders( const sptr bundleOption, std::vector> &reminders); + /** + * @brief Inits and recovery data from database. + * + * @param isFromBootComplete Indicates the init is called when boot completed. + */ + void Init(bool isFromBootComplete); + + void OnServiceStart(); + /** * @brief Triggered when third party application died. * @@ -129,6 +143,11 @@ public: */ void SnoozeReminder(const OHOS::EventFwk::Want &want); + /** + * Starts the recent reminder timing. + */ + void StartRecentReminder(); + /** * @brief Terminate the alerting reminder. * @@ -188,6 +207,8 @@ private: */ std::shared_ptr CreateTimerInfo(TimerType type) const; + void GetImmediatelyShowRemindersLocked(std::vector> &reminders) const; + std::string GetSoundUri(const sptr &reminder); /** @@ -254,6 +275,8 @@ private: bool HandleSysTimeChange(const sptr reminder) const; + bool IsReminderAgentReady() const; + /** * Judge the two reminders is belong to the same application or not. * @@ -265,6 +288,8 @@ private: bool IsBelongToSameApp( const sptr reminder, const std::string otherPkgName, const int otherUserId); + void LoadReminderFromDb(); + void PlaySoundAndVibrationLocked(const sptr &reminder); void PlaySoundAndVibration(const sptr &reminder); void StopSoundAndVibrationLocked(const sptr &reminder); @@ -321,11 +346,6 @@ private: void SnoozeReminderImpl(sptr &reminder); - /** - * Starts the recent reminder timing. - */ - void StartRecentReminder(); - /** * Starts timing actually. * @@ -403,6 +423,8 @@ private: */ static const int16_t MAX_NUM_REMINDER_LIMIT_APP; + bool isReminderAgentReady_ = false; + /** * Vector used to record all the reminders in system. */ @@ -446,7 +468,8 @@ private: * Indicates the total count of reminders in system. */ int16_t totalCount_ {0}; - AdvancedNotificationService *advancedNotificationService_; + sptr advancedNotificationService_ = nullptr; + std::shared_ptr store_ = nullptr; }; } // namespace OHOS } // namespace Nofitifcation diff --git a/services/ans/include/reminder_event_manager.h b/services/ans/include/reminder_event_manager.h index 0fb8b6556..54f900a48 100644 --- a/services/ans/include/reminder_event_manager.h +++ b/services/ans/include/reminder_event_manager.h @@ -18,6 +18,7 @@ #include "common_event_subscriber.h" #include "reminder_data_manager.h" +#include "system_ability_status_change_stub.h" #include @@ -45,6 +46,16 @@ private: void HandleProcessDied(OHOS::EventFwk::Want &want) const; std::shared_ptr reminderDataManager_ = nullptr; }; + +class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { +public: + SystemAbilityStatusChangeListener(std::shared_ptr &reminderDataManager); + ~SystemAbilityStatusChangeListener() {}; + virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; +private: + std::shared_ptr reminderDataManager_ = nullptr; +}; }; } // namespace OHOS } // namespace Notification diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 5cce71c4c..ab0d0fb28 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1703,7 +1703,6 @@ ErrCode AdvancedNotificationService::PublishReminder(sptr &remi return result; } - ReminderDataManager::GetInstance()->SetService(this); sptr notificationRequest = reminder->GetNotificationRequest(); sptr bundleOption = nullptr; result = PrepareNotificationInfo(notificationRequest, bundleOption); @@ -1711,46 +1710,59 @@ ErrCode AdvancedNotificationService::PublishReminder(sptr &remi ANSR_LOGW("PrepareNotificationInfo fail"); return result; } - ReminderDataManager::GetInstance()->PublishReminder(reminder, bundleOption); + auto rdm = ReminderDataManager::GetInstance(); + if (rdm == nullptr) { + return ERR_NO_INIT; + } + rdm->PublishReminder(reminder, bundleOption); return ERR_OK; } ErrCode AdvancedNotificationService::CancelReminder(const int32_t reminderId) { ANSR_LOGI("Cancel Reminder"); - ReminderDataManager::GetInstance()->SetService(this); sptr bundleOption = GenerateBundleOption(); if (bundleOption == nullptr) { return ERR_ANS_INVALID_BUNDLE; } - ReminderDataManager::GetInstance()->CancelReminder(reminderId, bundleOption); + auto rdm = ReminderDataManager::GetInstance(); + if (rdm == nullptr) { + return ERR_NO_INIT; + } + rdm->CancelReminder(reminderId, bundleOption); return ERR_OK; } ErrCode AdvancedNotificationService::CancelAllReminders() { ANSR_LOGI("Cancel all reminders"); - ReminderDataManager::GetInstance()->SetService(this); sptr bundleOption = GenerateBundleOption(); if (bundleOption == nullptr) { return ERR_ANS_INVALID_BUNDLE; } int userId = -1; AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId); - ReminderDataManager::GetInstance()->CancelAllReminders(bundleOption, userId); + auto rdm = ReminderDataManager::GetInstance(); + if (rdm == nullptr) { + return ERR_NO_INIT; + } + rdm->CancelAllReminders(bundleOption, userId); return ERR_OK; } ErrCode AdvancedNotificationService::GetValidReminders(std::vector> &reminders) { ANSR_LOGI("GetValidReminders"); - ReminderDataManager::GetInstance()->SetService(this); reminders.clear(); sptr bundleOption = GenerateBundleOption(); if (bundleOption == nullptr) { return ERR_ANS_INVALID_BUNDLE; } - ReminderDataManager::GetInstance()->GetValidReminders(bundleOption, reminders); + auto rdm = ReminderDataManager::GetInstance(); + if (rdm == nullptr) { + return ERR_NO_INIT; + } + rdm->GetValidReminders(bundleOption, reminders); ANSR_LOGD("Valid reminders size=%{public}zu", reminders.size()); return ERR_OK; } diff --git a/services/ans/src/advanced_notification_service_ability.cpp b/services/ans/src/advanced_notification_service_ability.cpp index 56f052fb8..f56917cc1 100644 --- a/services/ans/src/advanced_notification_service_ability.cpp +++ b/services/ans/src/advanced_notification_service_ability.cpp @@ -38,11 +38,15 @@ void AdvancedNotificationServiceAbility::OnStart() if (!Publish(service_)) { return; } + ANSR_LOGD("~~~~derek AdvancedNotificationServiceAbility onStart"); + reminderAgent_ = ReminderDataManager::InitInstance(service_); + // reminderAgent_->SetService(static_cast(service_.GetRefPtr())); } void AdvancedNotificationServiceAbility::OnStop() { service_ = nullptr; + reminderAgent_ = nullptr; } } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index cde3cb48f..5bdd05e5c 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -149,6 +149,7 @@ void ReminderDataManager::CancelAllReminders(const sptrDelete(reminderId); continue; } ++vit; @@ -190,6 +191,15 @@ void ReminderDataManager::AddToShowedReminders(const sptr &remi showedReminderVector_.push_back(reminder); } +void ReminderDataManager::OnServiceStart() +{ + std::vector> immediatelyShowReminders; + GetImmediatelyShowRemindersLocked(immediatelyShowReminders); + ANSR_LOGD("immediatelyShowReminders size=%{public}d", immediatelyShowReminders.size()); + HandleImmediatelyShow(immediatelyShowReminders, false); + StartRecentReminder(); +} + void ReminderDataManager::OnProcessDiedLocked(const sptr bundleOption) { std::string bundleName = bundleOption->GetBundleName(); @@ -219,6 +229,7 @@ void ReminderDataManager::OnProcessDiedLocked(const sptrUpdateOrInsert((*it), bundleOption); } } @@ -340,15 +351,23 @@ void ReminderDataManager::CloseReminder(const sptr &reminder, b } reminder->OnClose(true); RemoveFromShowedReminders(reminder); + store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); if (cancelNotification) { CancelNotification(reminder); } } std::shared_ptr ReminderDataManager::GetInstance() +{ + return REMINDER_DATA_MANAGER; +} + +std::shared_ptr ReminderDataManager::InitInstance( + sptr &advancedNotificationService) { if (REMINDER_DATA_MANAGER == nullptr) { REMINDER_DATA_MANAGER = std::make_shared(); + REMINDER_DATA_MANAGER->advancedNotificationService_ = advancedNotificationService; ReminderEventManager reminderEventManager(REMINDER_DATA_MANAGER); } return REMINDER_DATA_MANAGER; @@ -411,6 +430,7 @@ void ReminderDataManager::TerminateAlerting(const sptr &reminde ANSR_LOGD("publish(update) notification.(reminderId=%{public}d)", reminder->GetReminderId()); UpdateNotification(reminder); advancedNotificationService_->PublishPreparedNotification(notificationRequest, bundleOption); + store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); } void ReminderDataManager::UpdateAndSaveReminderLocked( @@ -419,6 +439,7 @@ void ReminderDataManager::UpdateAndSaveReminderLocked( std::lock_guard lock(ReminderDataManager::MUTEX); reminder->InitReminderId(); reminder->InitUserId(ReminderRequest::GetUserId(bundleOption->GetUid())); + reminder->InitUid(bundleOption->GetUid()); int32_t reminderId = reminder->GetReminderId(); ANSR_LOGD("Containers(map) add. reminderId=%{public}d", reminderId); auto ret = notificationBundleOptionMap_.insert( @@ -430,11 +451,17 @@ void ReminderDataManager::UpdateAndSaveReminderLocked( ANSR_LOGD("Containers(vector) add. reminderId=%{public}d", reminderId); reminderVector_.push_back(reminder); totalCount_++; + store_->UpdateOrInsert(reminder, bundleOption); } void ReminderDataManager::SetService(AdvancedNotificationService *advancedNotificationService) { advancedNotificationService_ = advancedNotificationService; + if (advancedNotificationService_ == nullptr) { + ANSR_LOGD("~~~~advancedNotificationService_ is null"); + } else { + ANSR_LOGD("~~~~advancedNotificationService_ is not null"); + } } void ReminderDataManager::ShowActiveReminder() @@ -531,17 +558,7 @@ void ReminderDataManager::ShowReminder(const sptr &reminder, co reminder->OnShow(false, isSysTimeChanged, false); return; } - if (isNeedToPlaySound) { - PlaySoundAndVibration(reminder); // play sound and vibration - reminder->OnShow(true, isSysTimeChanged, true); - if (needScheduleTimeout) { - StartTimer(reminder, TimerType::ALERTING_TIMER); - } else { - TerminateAlerting(1, reminder); - } - } else { - reminder->OnShow(false, isSysTimeChanged, true); - } + reminder->OnShow(isNeedToPlaySound, isSysTimeChanged, true); AddToShowedReminders(reminder); UpdateNotification(reminder); // this should be called after OnShow ANSR_LOGD("publish notification.(reminderId=%{public}d)", reminder->GetReminderId()); @@ -550,8 +567,18 @@ void ReminderDataManager::ShowReminder(const sptr &reminder, co reminder->OnShowFail(); RemoveFromShowedReminders(reminder); } else { + if (isNeedToPlaySound) { + PlaySoundAndVibration(reminder); // play sound and vibration + if (needScheduleTimeout) { + StartTimer(reminder, TimerType::ALERTING_TIMER); + } else { + TerminateAlerting(1, reminder); + } + } HandleSameNotificationIdShowing(reminder); } + store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); + if (isNeedToStartNext) { StartRecentReminder(); } @@ -563,6 +590,7 @@ void ReminderDataManager::UpdateNotification(const sptr &remind reminder->UpdateNotificationRequest(ReminderRequest::UpdateNotificationType::REMOVAL_WANT_AGENT, ""); reminder->UpdateNotificationRequest(ReminderRequest::UpdateNotificationType::WANT_AGENT, ""); reminder->UpdateNotificationRequest(ReminderRequest::UpdateNotificationType::MAX_SCREEN_WANT_AGENT, ""); + reminder->UpdateNotificationRequest(ReminderRequest::UpdateNotificationType::BUNDLE_INFO, ""); } void ReminderDataManager::SnoozeReminder(const OHOS::EventFwk::Want &want) @@ -592,6 +620,7 @@ void ReminderDataManager::SnoozeReminderImpl(sptr &reminder) StopTimerLocked(TimerType::ALERTING_TIMER); } reminder->OnSnooze(); + store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); // 2) Show the notification dialog in the systemUI sptr bundleOption = FindNotificationBundleOption(reminderId); @@ -622,11 +651,13 @@ void ReminderDataManager::StartRecentReminder() } if (activeReminderId_ != -1) { activeReminder_->OnStop(); + store_->UpdateOrInsert(activeReminder_, FindNotificationBundleOption(activeReminderId_)); StopTimerLocked(TimerType::TRIGGER_TIMER); } ANSR_LOGI("Start recent reminder"); StartTimerLocked(reminder, TimerType::TRIGGER_TIMER); reminder->OnStart(); + store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); SetActiveReminder(reminder); } @@ -727,6 +758,7 @@ sptr ReminderDataManager::GetRecentReminderLocked() } it = reminderVector_.erase(it); totalCount_--; + store_->Delete(reminderId); } return nullptr; } @@ -779,8 +811,10 @@ sptr ReminderDataManager::HandleRefreshReminder(uint8_t &type, if (triggerTimeBefore != triggerTimeAfter || reminder->GetReminderId() == alertingReminderId_) { CloseReminder(reminder, true); } + store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); return nullptr; } + store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); return reminder; } @@ -810,10 +844,49 @@ void ReminderDataManager::HandleSameNotificationIdShowing(const sptrOnSameNotificationIdCovered(); RemoveFromShowedReminders(*it); + store_->UpdateOrInsert((*it), FindNotificationBundleOption((*it)->GetReminderId())); + } + } +} + +void ReminderDataManager::Init(bool isFromBootComplete) +{ + ANSR_LOGD("ReminderDataManager Init, isFromBootComplete:%{public}d", isFromBootComplete); + if (IsReminderAgentReady()) { + return; + } + if (store_ == nullptr) { + store_ = std::make_shared(); + } + if (store_->Init() != ReminderStore::STATE_OK) { + ANSR_LOGW("Db init fail."); + return; + } + LoadReminderFromDb(); + isReminderAgentReady_ = true; + ANSR_LOGD("ReminderAgent is ready."); +} + +void ReminderDataManager::GetImmediatelyShowRemindersLocked(std::vector> &reminders) const +{ + std::lock_guard lock(ReminderDataManager::MUTEX); + for (auto it = reminderVector_.begin(); it != reminderVector_.end(); ++it) { + if ((*it)->ShouldShowImmediately()) { + if ((*it)->GetReminderType() != ReminderRequest::ReminderType::TIMER) { + (*it)->SetSnoozeTimesDynamic(0); + } + reminders.push_back(*it); + } else { + break; } } } +bool ReminderDataManager::IsReminderAgentReady() const +{ + return isReminderAgentReady_; +} + bool ReminderDataManager::IsBelongToSameApp( const sptr reminder, const std::string otherPkgName, const int otherUserId) { @@ -830,6 +903,30 @@ bool ReminderDataManager::IsBelongToSameApp( return false; } +void ReminderDataManager::LoadReminderFromDb() +{ + std::lock_guard lock(ReminderDataManager::MUTEX); + std::vector> existReminders = store_->GetAllValidReminders(); + reminderVector_ = existReminders; + ANSR_LOGD("LoadReminderFromDb, reminder size=%{public}d", reminderVector_.size()); + for (auto it = reminderVector_.begin(); it != reminderVector_.end(); ++it) { + sptr bundleOption = new NotificationBundleOption(); + int32_t reminderId = (*it)->GetReminderId(); + if (!(store_->GetBundleOption(reminderId, bundleOption))) { + ANSR_LOGE("Get bundle option fail, reminderId=%{public}d", reminderId); + continue; + } + auto ret = notificationBundleOptionMap_.insert( + std::pair>(reminderId, bundleOption)); + if (!ret.second) { + ANSR_LOGE("Containers add to map error"); + continue; + } + } + totalCount_ = reminderVector_.size(); + ReminderRequest::GLOBAL_ID = store_->GetMaxId() + 1; +} + void ReminderDataManager::PlaySoundAndVibrationLocked(const sptr &reminder) { std::lock_guard lock(ReminderDataManager::ALERT_MUTEX); @@ -935,6 +1032,7 @@ void ReminderDataManager::RemoveReminderLocked(const int32_t &reminderId) ANSR_LOGD("Containers(vector) remove. reminderId=%{public}d", reminderId); it = reminderVector_.erase(it); totalCount_--; + store_->Delete(reminderId); break; } else { ++it; diff --git a/services/ans/src/reminder_event_manager.cpp b/services/ans/src/reminder_event_manager.cpp index 7d1faf0ba..36215051b 100644 --- a/services/ans/src/reminder_event_manager.cpp +++ b/services/ans/src/reminder_event_manager.cpp @@ -21,7 +21,10 @@ #include "bundle_mgr_interface.h" #include "common_event_manager.h" #include "common_event_support.h" +#include "if_system_ability_manager.h" #include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" using namespace OHOS::EventFwk; namespace OHOS { @@ -38,6 +41,7 @@ void ReminderEventManager::init(std::shared_ptr &reminderDa matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); @@ -53,6 +57,18 @@ void ReminderEventManager::init(std::shared_ptr &reminderDa ANSR_LOGD("SubscribeCommonEvent fail"); } IPCSkeleton::SetCallingIdentity(identity); + + sptr statusChangeListener + = new SystemAbilityStatusChangeListener(reminderDataManager); + sptr samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + ANSR_LOGD("samgrProxy is null"); + return; + } + int32_t ret = samgrProxy->SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, statusChangeListener); + if (ret != ERR_OK) { + ANSR_LOGE("subscribe system ability id: %{public}d failed", BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + } } ReminderEventManager::ReminderEventSubscriber::ReminderEventSubscriber( @@ -87,6 +103,10 @@ void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFw reminderDataManager_->CloseReminder(want, false); return; } + if (action == CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) { + reminderDataManager_->Init(true); + return; + } if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { HandlePackageRemove(want); return; @@ -146,5 +166,24 @@ sptr ReminderEventManager::ReminderEventSubscriber::Ge } return bundleOption; } + +ReminderEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener( + std::shared_ptr &reminderDataManager) +{ + reminderDataManager_ = reminderDataManager; +} + +void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility( + int32_t systemAbilityId, const std::string& deviceId) +{ + ANSR_LOGD("OnAddSystemAbilityInner"); + reminderDataManager_->OnServiceStart(); +} + +void ReminderEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( + int32_t systemAbilityId, const std::string& deviceId) +{ + ANSR_LOGD("OnRemoveSystemAbilityInner"); +} } // namespace OHOS } // namespace Notification diff --git a/tools/dump/BUILD.gn b/tools/dump/BUILD.gn index e89422237..04e604041 100644 --- a/tools/dump/BUILD.gn +++ b/tools/dump/BUILD.gn @@ -50,6 +50,7 @@ ohos_executable("anm") { external_deps = [ "hiviewdfx_hilog_native:libhilog", "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", ] external_deps += ans_standard_external_deps -- Gitee From acc4ec71e3ec5cc559f77fd237447e817a1946bd Mon Sep 17 00:00:00 2001 From: linyunxuan Date: Wed, 2 Mar 2022 16:22:34 +0800 Subject: [PATCH 2/8] add database feature Signed-off-by: linyunxuan --- frameworks/ans/native/src/reminder_store.cpp | 434 ++++++++++++++++++ .../ans/native/include/reminder_store.h | 93 ++++ 2 files changed, 527 insertions(+) create mode 100644 frameworks/ans/native/src/reminder_store.cpp create mode 100644 interfaces/innerkits/ans/native/include/reminder_store.h diff --git a/frameworks/ans/native/src/reminder_store.cpp b/frameworks/ans/native/src/reminder_store.cpp new file mode 100644 index 000000000..51da2bebe --- /dev/null +++ b/frameworks/ans/native/src/reminder_store.cpp @@ -0,0 +1,434 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "reminder_store.h" + +#include +#include +#include +#include + +#include "ability_context.h" +#include "ans_log_wrapper.h" +#include "reminder_request_alarm.h" +#include "reminder_request_calendar.h" +#include "reminder_request_timer.h" + +namespace OHOS { +namespace Notification { +const std::string ReminderStore::REMINDER_DB_DIR = "/data/system_ce/ans_standard/"; +const std::string ReminderStore::REMINDER_DB_NAME = "reminder.db"; +const std::string ReminderStore::REMINDER_DB_TABLE = "reminder"; +const uint32_t ReminderStore::REMINDER_RDB_VERSION = 1; +const int32_t ReminderStore::STATE_FAIL = -1; +const int32_t ReminderStore::STATE_OK = 0; +const uint16_t ReminderStore::TIME_INTERVAL_FOR_DELETE = 1800; +const uint16_t ReminderStore::MILLI_SECONDS = 1000; +std::vector ReminderStore::COLUMNS; + +int32_t ReminderStore::ReminderStoreDataCallBack::OnCreate(NativeRdb::RdbStore &store) +{ + ANSR_LOGD("Create table."); + std::string CREATE_REMINDER_TABLE = "CREATE TABLE IF NOT EXISTS " + REMINDER_DB_TABLE + " (" + + ReminderRequest::Instance::SQL_ADD_COLUMNS + + ReminderRequestCalendar::Instance::SQL_ADD_COLUMNS + + ReminderRequestAlarm::Instance::SQL_ADD_COLUMNS + ")"; + ANSR_LOGD("createTable:%{public}s", CREATE_REMINDER_TABLE.c_str()); + return store.ExecuteSql(CREATE_REMINDER_TABLE); +} + +int32_t ReminderStore::ReminderStoreDataCallBack::OnUpgrade(NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) +{ + return NativeRdb::E_OK; +} + +int32_t ReminderStore::Init() +{ + ANSR_LOGD("Reminder store init."); + int32_t errCode(STATE_FAIL); + + if (access(REMINDER_DB_DIR.c_str(), F_OK) != 0) { + int createDir = mkdir(REMINDER_DB_DIR.c_str(), S_IRWXU); + if (createDir != 0) { + ANSR_LOGE("Failed to create directory %{public}s", REMINDER_DB_DIR.c_str()); + return errCode; + } + } + + ReminderRequest::Instance::Init(); + ReminderRequestCalendar::Instance::Init(); + ReminderRequestAlarm::Instance::Init(); + ReminderStore::COLUMNS.insert(ReminderStore::COLUMNS.begin(), + ReminderRequestAlarm::Instance::COLUMNS.begin(), ReminderRequestAlarm::Instance::COLUMNS.end()); + ReminderStore::COLUMNS.insert(ReminderStore::COLUMNS.begin(), + ReminderRequestCalendar::Instance::COLUMNS.begin(), ReminderRequestCalendar::Instance::COLUMNS.end()); + ReminderStore::COLUMNS.insert(ReminderStore::COLUMNS.begin(), + ReminderRequest::Instance::COLUMNS.begin(), ReminderRequest::Instance::COLUMNS.end()); + + std::string dbConfig = ReminderStore::REMINDER_DB_DIR + ReminderStore::REMINDER_DB_NAME; + NativeRdb::RdbStoreConfig config_(dbConfig); + ReminderStoreDataCallBack rdbDataCallBack_; + rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(config_, REMINDER_RDB_VERSION, rdbDataCallBack_, errCode); + if (rdbStore_ == nullptr) { + ANSR_LOGE("ReminderStore init fail, errCode %{public}d.", errCode); + return errCode; + } + return ReminderStore::InitData(); +} + +int32_t ReminderStore::InitData() +{ + ANSR_LOGD("Reminder data init."); + std::string deleteCondition = ReminderRequest::Instance::IS_EXPIRED + " is true"; + ReminderStore::Delete(deleteCondition); + + int32_t statusChangedRows = STATE_FAIL; + NativeRdb::ValuesBucket statusValues; + statusValues.PutInt(ReminderRequest::Instance::STATE, ReminderRequest::GetConstStateInactive()); + int32_t statusResult = rdbStore_->Update(statusChangedRows, REMINDER_DB_TABLE, statusValues); + ANSR_LOGD("Change status to inactive, changed rows: %{public}d.", statusChangedRows); + + int32_t activeChangedRows = STATE_FAIL; + NativeRdb::ValuesBucket activeValues; + activeValues.PutString(ReminderRequest::Instance::IS_ACTIVE, "false"); + std::string activeUpdateCondition = ReminderRequest::Instance::IS_ACTIVE + " is true"; + std::vector activeWhereArgs; + int32_t activeResult = rdbStore_->Update( + activeChangedRows, REMINDER_DB_TABLE, activeValues, activeUpdateCondition, activeWhereArgs); + ANSR_LOGD("Change status isActive to false, changed rows: %{public}d.", activeChangedRows); + + int32_t scheduledChangedRows = STATE_FAIL; + NativeRdb::ValuesBucket scheduledValues; + scheduledValues.PutString(ReminderRequest::Instance::HAS_SCHEDULED_TIMEOUT, "false"); + std::string scheduledUpdateCondition = ReminderRequest::Instance::HAS_SCHEDULED_TIMEOUT + " is true"; + std::vector scheduledWhereArgs; + int32_t scheduledResult = rdbStore_->Update( + scheduledChangedRows, REMINDER_DB_TABLE, scheduledValues, scheduledUpdateCondition, scheduledWhereArgs); + ANSR_LOGD("Change status has_ScheduledTimeout to false, changed rows: %{public}d.", scheduledChangedRows); + + if (statusResult != NativeRdb::E_OK || activeResult != NativeRdb::E_OK + || scheduledResult != NativeRdb::E_OK) { + ANSR_LOGE("Init data failed."); + return STATE_FAIL; + } + return STATE_OK; +} + +int32_t ReminderStore::Delete(int32_t reminderId) +{ + std::string deleteCondition = ReminderRequest::Instance::REMINDER_ID + + " = " + std::to_string(reminderId); + return ReminderStore::Delete(deleteCondition); +} + +int32_t ReminderStore::DeleteUser(int32_t userId) +{ + std::string deleteCondition = ReminderRequest::Instance::USER_ID + " = " + std::to_string(userId); + return ReminderStore::Delete(deleteCondition); +} + +int32_t ReminderStore::Delete(const std::string &pkg, int32_t userId) +{ + std::string deleteCondition = ReminderRequest::Instance::PKG_NAME + " = " + pkg + " and " + + ReminderRequest::Instance::USER_ID + " = " + std::to_string(userId); + return ReminderStore::Delete(deleteCondition); +} + +int32_t ReminderStore::Delete(const std::string &deleteCondition) +{ + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return STATE_FAIL; + } + int32_t deletedRows = STATE_FAIL; + std::vector whereArgs; + int32_t result = rdbStore_->Delete(deletedRows, REMINDER_DB_TABLE, deleteCondition, whereArgs); + if (result != NativeRdb::E_OK) { + ANSR_LOGE("Delete operation failed, deleteConditon: %{public}s," \ + "result: %{public}d.", deleteCondition.c_str(), result); + } + ANSR_LOGD("Delete operation done, deleteConditon: %{public}s," \ + "deleted rows: %{public}d.", deleteCondition.c_str(), deletedRows); + return deletedRows; +} + +int64_t ReminderStore::UpdateOrInsert( + const sptr &reminder, const sptr &bundleOption) +{ + if (reminder->GetReminderType() == ReminderRequest::ReminderType::TIMER) { + ANSR_LOGI("Countdown not support persist."); + return STATE_FAIL; + } + int64_t isSuccess = STATE_FAIL; + if (IsReminderExist(reminder)) { + isSuccess = ReminderStore::Update(reminder, bundleOption); + } else { + isSuccess = ReminderStore::Insert(reminder, bundleOption); + } + return isSuccess; +} + +int64_t ReminderStore::Insert( + const sptr &reminder, const sptr &bundleOption) +{ + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return STATE_FAIL; + } + if (bundleOption == nullptr) { + ANSR_LOGE("BundleOption is null."); + return STATE_FAIL; + } + int64_t rowId = STATE_FAIL; + NativeRdb::ValuesBucket values; + ReminderStore::GenerateData(reminder, bundleOption, values); + int32_t result = rdbStore_->Insert(rowId, REMINDER_DB_TABLE, values); + if (result != NativeRdb::E_OK) { + ANSR_LOGE("Insert operation failed, result: %{public}d, reminderId=%{public}d." + , result, reminder->GetReminderId()); + return result; + } + ANSR_LOGD("Insert successfully, reminderId=%{public}d.", reminder->GetReminderId()); + return result; +} + +int64_t ReminderStore::Update( + const sptr &reminder, const sptr &bundleOption) +{ + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return STATE_FAIL; + } + int32_t changedRows = STATE_FAIL; + NativeRdb::ValuesBucket values; + ReminderStore::GenerateData(reminder, bundleOption, values); + std::string updateCondition = ReminderRequest::Instance::REMINDER_ID + + " = " + std::to_string(reminder->GetReminderId()); + std::vector whereArgs; + int32_t result = rdbStore_->Update(changedRows, REMINDER_DB_TABLE, values, updateCondition, whereArgs); + if ((result != NativeRdb::E_OK) || (changedRows <= 0)) { + ANSR_LOGE("Update operation failed, result: %{public}d, updated rows: %{public}d, reminderId=%{public}d." + , result, changedRows, reminder->GetReminderId()); + return result; + } + ANSR_LOGD("Update successfully, updated rows: %{public}d, reminderId=%{public}d." + , changedRows, reminder->GetReminderId()); + return result; +} + +bool ReminderStore::IsReminderExist(const sptr &reminder) +{ + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return false; + } + std::string queryCondition = "select " + ReminderRequest::Instance::REMINDER_ID + + " from " + REMINDER_DB_TABLE + " where " + + ReminderRequest::Instance::REMINDER_ID + " = " + std::to_string(reminder->GetReminderId()); + std::vector whereArgs; + std::unique_ptr queryResultSet = rdbStore_->QuerySql(queryCondition, whereArgs); + if (queryResultSet == nullptr) { + ANSR_LOGE("QueryResultSet is null."); + return false; + } + int32_t resultNum; + queryResultSet->GetRowCount(resultNum); + if (resultNum == 0) { + return false; + } + return true; +} + +std::shared_ptr ReminderStore::Query(const std::string &queryCondition) const +{ + std::unique_ptr queryResultSet; + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return queryResultSet; + } + std::vector whereArgs; + queryResultSet = rdbStore_->QuerySql(queryCondition, whereArgs); + return queryResultSet; +} + +std::vector ReminderStore::GetRepeatInfo(int64_t repeatData, int32_t maxRepeatVal) +{ + std::vector repeatInfo; + for (int i = 1; i <= maxRepeatVal; i++) { + if ((repeatData & (1 << (i - 1))) > 0) { + repeatInfo.push_back(i); + } + } + return repeatInfo; +} + +uint8_t ReminderStore::GetColumnIndex(const std::string& name) +{ + uint8_t index = 0; + for (auto it = ReminderStore::COLUMNS.begin(); it != ReminderStore::COLUMNS.end(); ++it) { + if (name == (*it)) { + break; + } + index++; + } + return index; +} + +int32_t ReminderStore::GetMaxId() +{ + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return STATE_FAIL; + } + std::string queryCondition = "select " + ReminderRequest::Instance::REMINDER_ID + + " from " + REMINDER_DB_TABLE + " order by " + + ReminderRequest::Instance::REMINDER_ID + " desc"; + std::shared_ptr queryResultSet = ReminderStore::Query(queryCondition); + if (queryResultSet == nullptr) { + ANSR_LOGE("QueryResultSet is null."); + return STATE_FAIL; + } + int32_t resultNum; + queryResultSet->GetRowCount(resultNum); + if (resultNum == 0) { + ANSR_LOGI("QueryResultSet is zero."); + return STATE_FAIL; + } + queryResultSet->GoToNextRow(); + int32_t maxId = STATE_FAIL; + int32_t result = queryResultSet->GetInt(0, maxId); + if (result != NativeRdb::E_OK) { + ANSR_LOGE("Query operation failed, result %{public}d.", result); + } + ANSR_LOGD("MaxId: %{public}d.", maxId); + return maxId; +} + +std::vector> ReminderStore::GetAllValidReminders() +{ + std::string queryCondition = "select * from " + REMINDER_DB_TABLE + " where " + + ReminderRequest::Instance::IS_EXPIRED + " is false order by " + + ReminderRequest::Instance::TRIGGER_TIME + " asc"; + ANSR_LOGD("Get all reminders"); + return GetReminders(queryCondition); +} + +std::vector> ReminderStore::GetReminders(const std::string &queryCondition) +{ + std::vector> reminders; + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return reminders; + } + std::shared_ptr queryResultSet = Query(queryCondition); + if (queryResultSet == nullptr) { + return reminders; + } + bool isAtLastRow = false; + queryResultSet->IsAtLastRow(isAtLastRow); + while(!isAtLastRow) { + queryResultSet->GoToNextRow(); + sptr reminder; + reminder = BuildReminder(queryResultSet); + reminders.push_back(reminder); + queryResultSet->IsAtLastRow(isAtLastRow); + } + ANSR_LOGD("Size=%{public}d", reminders.size()); + return reminders; +} + +sptr ReminderStore::BuildReminder(const std::shared_ptr &resultSet) +{ + int32_t reminderType; + int32_t reminderId; + resultSet->GetInt(ReminderStore::GetColumnIndex(ReminderRequest::Instance::REMINDER_TYPE), reminderType); + resultSet->GetInt(ReminderStore::GetColumnIndex(ReminderRequest::Instance::REMINDER_ID), reminderId); + + sptr reminder = nullptr; + switch (reminderType) { + case (static_cast(ReminderRequest::ReminderType::TIMER)): { + reminder = new ReminderRequestTimer(reminderId); + break; + } + case (static_cast(ReminderRequest::ReminderType::CALENDAR)): { + reminder = new ReminderRequestCalendar(reminderId); + break; + } + case (static_cast(ReminderRequest::ReminderType::ALARM)): { + reminder = new ReminderRequestAlarm(reminderId); + break; + } + default: { + ANSR_LOGE("ReminderType from database is error, reminderType %{public}d.", reminderType); + } + } + if (reminder != nullptr) { + reminder->RecoveryFromDb(resultSet); + ANSR_LOGI("BuildReminder success."); + } else { + ANSR_LOGW("BuildReminder fail."); + } + return reminder; +} + +bool ReminderStore::GetBundleOption(const int32_t &reminderId, sptr &bundleOption) const +{ + std::string queryCondition = "select " + ReminderRequest::Instance::PKG_NAME + ", " + + ReminderRequest::Instance::UID + " from " + REMINDER_DB_TABLE + " where " + + ReminderRequest::Instance::REMINDER_ID + "=" + std::to_string(reminderId); + std::shared_ptr queryResultSet = Query(queryCondition); + if (queryResultSet == nullptr) { + return false; + } + bool isAtLastRow = false; + queryResultSet->IsAtLastRow(isAtLastRow); + if (isAtLastRow) { + return false; + } + queryResultSet->GoToNextRow(); + std::string pkgName; + GetStringVal(queryResultSet, ReminderRequest::Instance::PKG_NAME, pkgName); + int32_t uid; + GetInt32Val(queryResultSet, ReminderRequest::Instance::UID, uid); + bundleOption->SetBundleName(pkgName); + bundleOption->SetUid(uid); + return true; +} + +void ReminderStore::GetInt32Val(std::shared_ptr &resultSet + , const std::string &name, int32_t &value) const +{ + int32_t columnIndex; + resultSet->GetColumnIndex(name, columnIndex); + resultSet->GetInt(columnIndex, value); +} + +void ReminderStore::GetStringVal(std::shared_ptr &resultSet + , const std::string &name, std::string &value) const +{ + int32_t columnIndex; + resultSet->GetColumnIndex(name, columnIndex); + resultSet->GetString(columnIndex, value); +} + +void ReminderStore::GenerateData(const sptr &reminder, + const sptr &bundleOption, NativeRdb::ValuesBucket &values) const +{ + ReminderRequest::AppendValuesBucket(reminder, bundleOption, values); + ReminderRequestCalendar::AppendValuesBucket(reminder, bundleOption, values); + ReminderRequestAlarm::AppendValuesBucket(reminder, bundleOption, values); +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/ans/native/include/reminder_store.h b/interfaces/innerkits/ans/native/include/reminder_store.h new file mode 100644 index 000000000..130e53e1b --- /dev/null +++ b/interfaces/innerkits/ans/native/include/reminder_store.h @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_INTERFACES_INNERKITS_ANS_NATIVE_INCLUDE_REMINDER_STORE_H +#define BASE_NOTIFICATION_ANS_STANDARD_INTERFACES_INNERKITS_ANS_NATIVE_INCLUDE_REMINDER_STORE_H + +#include + +#include "notification_bundle_option.h" +#include "reminder_request.h" +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_open_callback.h" +#include "rdb_store_config.h" + +namespace OHOS { +namespace Notification { +class ReminderStore { +public: + ReminderStore() {}; + virtual ~ReminderStore() {}; + int32_t Init(); + int32_t Delete(int32_t reminderId); + int32_t Delete(const std::string &pkg, int32_t userId); + int32_t DeleteUser(int32_t userId); + std::vector> GetAllValidReminders(); + bool GetBundleOption(const int32_t &reminderId, sptr &bundleOption) const; + int32_t GetMaxId(); + int64_t UpdateOrInsert(const sptr &reminder, const sptr &bundleOption); + static std::vector GetRepeatInfo(int64_t repeatData, int32_t maxRepeatVal); + static uint8_t GetColumnIndex(const std::string& name); + + static std::vector COLUMNS; + static const int32_t STATE_FAIL; + static const int32_t STATE_OK; + +private: + /** + * @brief Inits the data in database when system boot on or proxy process reboot on. + * + * 1. Deletes all the reminders which IS_EXPIRED is true. + * 2. Sets all the value of STATE to ReminderRequest::REMINDER_STATUS_INACTIVE + * 3. Sets all the value of IS_ACTIVE to false. + * 4. Sets all the value of HAS_SCHEDULED_TIMEOUT to false. + * + * @return int32_t result code. + */ + int32_t InitData(); + sptr BuildReminder(const std::shared_ptr &resultSet); + int32_t Delete(const std::string &deleteCondition); + void GetInt32Val(std::shared_ptr &resultSet + , const std::string &name, int32_t &value) const; + void GetStringVal(std::shared_ptr &resultSet + , const std::string &name, std::string &value) const; + std::vector> GetReminders(const std::string &queryCondition); + void GenerateData(const sptr &remindert, + const sptr &bundleOption, NativeRdb::ValuesBucket &values) const; + bool IsReminderExist(const sptr &reminder); + int64_t Insert(const sptr &reminder, const sptr &bundleOption); + std::shared_ptr Query(const std::string &queryCondition) const; + int64_t Update(const sptr &reminder, const sptr &bundleOption); + + static const std::string REMINDER_DB_DIR; + static const std::string REMINDER_DB_NAME; + static const std::string REMINDER_DB_TABLE; + static const uint32_t REMINDER_RDB_VERSION; + static const uint16_t TIME_INTERVAL_FOR_DELETE; + static const uint16_t MILLI_SECONDS; + +class ReminderStoreDataCallBack : public NativeRdb::RdbOpenCallback { +public: + int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; + int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; +}; + +private: + std::shared_ptr rdbStore_ = nullptr; +}; +} // namespace Notification +} // namespace OHOS +#endif // BASE_NOTIFICATION_ANS_STANDARD_INTERFACES_INNERKITS_ANS_NATIVE_INCLUDE_REMINDER_STORE_H \ No newline at end of file -- Gitee From f076943869c46113f2287b4ea0d33014970fae2b Mon Sep 17 00:00:00 2001 From: linyunxuan Date: Wed, 2 Mar 2022 17:39:20 +0800 Subject: [PATCH 3/8] add database feature Signed-off-by: linyunxuan --- frameworks/ans/core/BUILD.gn | 2 +- frameworks/ans/native/BUILD.gn | 2 +- .../ans/native/src/reminder_request.cpp | 4 +-- frameworks/ans/native/src/reminder_store.cpp | 29 ++++++++++--------- frameworks/ans/native/test/unittest/BUILD.gn | 2 +- frameworks/ans/test/moduletest/BUILD.gn | 1 + .../include/reminder_request_calendar.h | 6 ++-- .../ans/native/include/reminder_store.h | 10 +++---- interfaces/kits/napi/ans/BUILD.gn | 2 +- services/ans/include/reminder_data_manager.h | 3 +- .../advanced_notification_service_ability.cpp | 2 -- services/ans/src/reminder_data_manager.cpp | 2 +- tools/dump/BUILD.gn | 2 +- 13 files changed, 34 insertions(+), 33 deletions(-) diff --git a/frameworks/ans/core/BUILD.gn b/frameworks/ans/core/BUILD.gn index 3294e9ac2..e18fab859 100644 --- a/frameworks/ans/core/BUILD.gn +++ b/frameworks/ans/core/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at diff --git a/frameworks/ans/native/BUILD.gn b/frameworks/ans/native/BUILD.gn index 74f56a311..9ef3af8b6 100644 --- a/frameworks/ans/native/BUILD.gn +++ b/frameworks/ans/native/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at diff --git a/frameworks/ans/native/src/reminder_request.cpp b/frameworks/ans/native/src/reminder_request.cpp index 5e8899ae5..9884ae350 100644 --- a/frameworks/ans/native/src/reminder_request.cpp +++ b/frameworks/ans/native/src/reminder_request.cpp @@ -450,7 +450,6 @@ void ReminderRequest::RecoveryFromDb(const std::shared_ptr(RecoveryInt64FromDb(resultSet, Instance::STATE, DbRecoveryType::INT)); // action buttons @@ -1355,7 +1354,7 @@ void ReminderRequest::UpdateNotificationCommon() void ReminderRequest::UpdateNotificationBundleInfo() { - ANSR_LOGD("~~~~Bundle name=%{public}s, bundleName_=%{public}s", + ANSR_LOGD("Bundle name=%{public}s, bundleName_=%{public}s", notificationRequest_->GetOwnerBundleName().c_str(), bundleName_.c_str()); if (!(notificationRequest_->GetOwnerBundleName()).empty()) { return; @@ -1365,7 +1364,6 @@ void ReminderRequest::UpdateNotificationBundleInfo() notificationRequest_->SetCreatorBundleName(bundleName_); notificationRequest_->SetCreatorUid(uid_); - // notificationRequest_->SetCreatorPid(pid); OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid_, userId_); notificationRequest_->SetCreatorUserId(userId_); diff --git a/frameworks/ans/native/src/reminder_store.cpp b/frameworks/ans/native/src/reminder_store.cpp index 51da2bebe..57b3a9ea0 100644 --- a/frameworks/ans/native/src/reminder_store.cpp +++ b/frameworks/ans/native/src/reminder_store.cpp @@ -49,7 +49,8 @@ int32_t ReminderStore::ReminderStoreDataCallBack::OnCreate(NativeRdb::RdbStore & return store.ExecuteSql(CREATE_REMINDER_TABLE); } -int32_t ReminderStore::ReminderStoreDataCallBack::OnUpgrade(NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) +int32_t ReminderStore::ReminderStoreDataCallBack::OnUpgrade( + NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) { return NativeRdb::E_OK; } @@ -196,8 +197,8 @@ int64_t ReminderStore::Insert( ReminderStore::GenerateData(reminder, bundleOption, values); int32_t result = rdbStore_->Insert(rowId, REMINDER_DB_TABLE, values); if (result != NativeRdb::E_OK) { - ANSR_LOGE("Insert operation failed, result: %{public}d, reminderId=%{public}d." - , result, reminder->GetReminderId()); + ANSR_LOGE("Insert operation failed, result: %{public}d, reminderId=%{public}d.", + result, reminder->GetReminderId()); return result; } ANSR_LOGD("Insert successfully, reminderId=%{public}d.", reminder->GetReminderId()); @@ -219,12 +220,12 @@ int64_t ReminderStore::Update( std::vector whereArgs; int32_t result = rdbStore_->Update(changedRows, REMINDER_DB_TABLE, values, updateCondition, whereArgs); if ((result != NativeRdb::E_OK) || (changedRows <= 0)) { - ANSR_LOGE("Update operation failed, result: %{public}d, updated rows: %{public}d, reminderId=%{public}d." - , result, changedRows, reminder->GetReminderId()); + ANSR_LOGE("Update operation failed, result: %{public}d, updated rows: %{public}d, reminderId=%{public}d.", + result, changedRows, reminder->GetReminderId()); return result; } - ANSR_LOGD("Update successfully, updated rows: %{public}d, reminderId=%{public}d." - , changedRows, reminder->GetReminderId()); + ANSR_LOGD("Update successfully, updated rows: %{public}d, reminderId=%{public}d.", + changedRows, reminder->GetReminderId()); return result; } @@ -329,8 +330,8 @@ std::vector> ReminderStore::GetReminders(const std::string { std::vector> reminders; if (rdbStore_ == nullptr) { - ANSR_LOGE("Rdb store is not initialized."); - return reminders; + ANSR_LOGE("Rdb store is not initialized."); + return reminders; } std::shared_ptr queryResultSet = Query(queryCondition); if (queryResultSet == nullptr) { @@ -338,7 +339,7 @@ std::vector> ReminderStore::GetReminders(const std::string } bool isAtLastRow = false; queryResultSet->IsAtLastRow(isAtLastRow); - while(!isAtLastRow) { + while (!isAtLastRow) { queryResultSet->GoToNextRow(); sptr reminder; reminder = BuildReminder(queryResultSet); @@ -407,16 +408,16 @@ bool ReminderStore::GetBundleOption(const int32_t &reminderId, sptr &resultSet - , const std::string &name, int32_t &value) const +void ReminderStore::GetInt32Val(std::shared_ptr &resultSet, + const std::string &name, int32_t &value) const { int32_t columnIndex; resultSet->GetColumnIndex(name, columnIndex); resultSet->GetInt(columnIndex, value); } -void ReminderStore::GetStringVal(std::shared_ptr &resultSet - , const std::string &name, std::string &value) const +void ReminderStore::GetStringVal(std::shared_ptr &resultSet, + const std::string &name, std::string &value) const { int32_t columnIndex; resultSet->GetColumnIndex(name, columnIndex); diff --git a/frameworks/ans/native/test/unittest/BUILD.gn b/frameworks/ans/native/test/unittest/BUILD.gn index 15eecb3a8..0318720c8 100644 --- a/frameworks/ans/native/test/unittest/BUILD.gn +++ b/frameworks/ans/native/test/unittest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at diff --git a/frameworks/ans/test/moduletest/BUILD.gn b/frameworks/ans/test/moduletest/BUILD.gn index 102cfea0d..8eaa9e509 100644 --- a/frameworks/ans/test/moduletest/BUILD.gn +++ b/frameworks/ans/test/moduletest/BUILD.gn @@ -167,6 +167,7 @@ ohos_moduletest("ans_innerkits_module_publish_test") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", ] diff --git a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h index f93882a5a..0f5bcde24 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h @@ -176,10 +176,12 @@ private: uint8_t GetNextDay(const uint16_t &settedYear, const uint8_t &settedMonth, const tm &now, const tm &target) const; uint64_t GetNextTriggerTime() const; uint64_t GetNextTriggerTimeAsRepeatReminder(const tm &nowTime, const tm &tarTime) const; - uint32_t GetRepeatDay() const { + uint32_t GetRepeatDay() const + { return repeatDay_; } - uint16_t GetRepeatMonth() const { + uint16_t GetRepeatMonth() const + { return repeatMonth_; } uint64_t GetTimeInstantMilli( diff --git a/interfaces/innerkits/ans/native/include/reminder_store.h b/interfaces/innerkits/ans/native/include/reminder_store.h index 130e53e1b..48a68a15d 100644 --- a/interfaces/innerkits/ans/native/include/reminder_store.h +++ b/interfaces/innerkits/ans/native/include/reminder_store.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -60,10 +60,10 @@ private: int32_t InitData(); sptr BuildReminder(const std::shared_ptr &resultSet); int32_t Delete(const std::string &deleteCondition); - void GetInt32Val(std::shared_ptr &resultSet - , const std::string &name, int32_t &value) const; - void GetStringVal(std::shared_ptr &resultSet - , const std::string &name, std::string &value) const; + void GetInt32Val(std::shared_ptr &resultSet, + const std::string &name, int32_t &value) const; + void GetStringVal(std::shared_ptr &resultSet, + const std::string &name, std::string &value) const; std::vector> GetReminders(const std::string &queryCondition); void GenerateData(const sptr &remindert, const sptr &bundleOption, NativeRdb::ValuesBucket &values) const; diff --git a/interfaces/kits/napi/ans/BUILD.gn b/interfaces/kits/napi/ans/BUILD.gn index eed0bf82c..521d6ce72 100644 --- a/interfaces/kits/napi/ans/BUILD.gn +++ b/interfaces/kits/napi/ans/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at diff --git a/services/ans/include/reminder_data_manager.h b/services/ans/include/reminder_data_manager.h index 941600bde..723dba78a 100644 --- a/services/ans/include/reminder_data_manager.h +++ b/services/ans/include/reminder_data_manager.h @@ -29,7 +29,8 @@ namespace OHOS { namespace Notification { class ReminderDataManager final { public: - ReminderDataManager() { + ReminderDataManager() + { Init(false); }; ~ReminderDataManager() {}; diff --git a/services/ans/src/advanced_notification_service_ability.cpp b/services/ans/src/advanced_notification_service_ability.cpp index f56917cc1..2fb8f0911 100644 --- a/services/ans/src/advanced_notification_service_ability.cpp +++ b/services/ans/src/advanced_notification_service_ability.cpp @@ -38,9 +38,7 @@ void AdvancedNotificationServiceAbility::OnStart() if (!Publish(service_)) { return; } - ANSR_LOGD("~~~~derek AdvancedNotificationServiceAbility onStart"); reminderAgent_ = ReminderDataManager::InitInstance(service_); - // reminderAgent_->SetService(static_cast(service_.GetRefPtr())); } void AdvancedNotificationServiceAbility::OnStop() diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index 5bdd05e5c..a5443765d 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -917,7 +917,7 @@ void ReminderDataManager::LoadReminderFromDb() continue; } auto ret = notificationBundleOptionMap_.insert( - std::pair>(reminderId, bundleOption)); + std::pair>(reminderId, bundleOption)); if (!ret.second) { ANSR_LOGE("Containers add to map error"); continue; diff --git a/tools/dump/BUILD.gn b/tools/dump/BUILD.gn index 04e604041..0d583b0bd 100644 --- a/tools/dump/BUILD.gn +++ b/tools/dump/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -- Gitee From 7a504facb61d966810c54478c9bbb660b1f3ea8f Mon Sep 17 00:00:00 2001 From: linyunxuan Date: Thu, 3 Mar 2022 12:47:23 +0800 Subject: [PATCH 4/8] add database feature Signed-off-by: linyunxuan --- .../ans/native/src/reminder_request.cpp | 144 ++++++++++-------- .../ans/native/src/reminder_request_alarm.cpp | 20 +-- .../native/src/reminder_request_calendar.cpp | 34 ++--- frameworks/ans/test/moduletest/BUILD.gn | 3 + .../ans/native/include/reminder_request.h | 37 +++-- .../native/include/reminder_request_alarm.h | 6 +- .../include/reminder_request_calendar.h | 6 +- services/ans/include/reminder_event_manager.h | 2 +- services/ans/src/reminder_data_manager.cpp | 13 +- 9 files changed, 144 insertions(+), 121 deletions(-) diff --git a/frameworks/ans/native/src/reminder_request.cpp b/frameworks/ans/native/src/reminder_request.cpp index 9884ae350..5f84ee059 100644 --- a/frameworks/ans/native/src/reminder_request.cpp +++ b/frameworks/ans/native/src/reminder_request.cpp @@ -383,25 +383,36 @@ bool ReminderRequest::OnTimeZoneChange() triggerTimeInMilli_, GetDurationSinceEpochInMilli(newZoneTriggerTime), nextTriggerTime); } -int64_t ReminderRequest::RecoveryInt64FromDb(const std::shared_ptr &resultSet, +int64_t ReminderRequest::RecoverInt64FromDb(const std::shared_ptr &resultSet, const std::string &columnName, const DbRecoveryType &columnType) { - if (columnType == DbRecoveryType::INT) { - int value; - resultSet->GetInt(ReminderStore::GetColumnIndex(columnName), value); - return static_cast(value); + if (resultSet == nullptr) { + ANSR_LOGE("ResultSet is null"); + return 0; } - if (columnType == DbRecoveryType::LONG) { - int64_t value; - resultSet->GetLong(ReminderStore::GetColumnIndex(columnName), value); - return value; + switch (columnType) { + case (DbRecoveryType::INT): { + int value; + resultSet->GetInt(ReminderStore::GetColumnIndex(columnName), value); + return static_cast(value); + } + case (DbRecoveryType::LONG): { + int64_t value; + resultSet->GetLong(ReminderStore::GetColumnIndex(columnName), value); + return value; + } } - ANSR_LOGE("Recovery data error"); + ANSR_LOGE("Recover data error"); return 0; } -void ReminderRequest::RecoveryFromDb(const std::shared_ptr &resultSet) +void ReminderRequest::RecoverFromDb(const std::shared_ptr &resultSet) { + if (resultSet == nullptr) { + ANSR_LOGE("ResultSet is null"); + return; + } + // reminderId resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::REMINDER_ID), reminderId_); @@ -421,27 +432,27 @@ void ReminderRequest::RecoveryFromDb(const std::shared_ptr(RecoveryInt64FromDb(resultSet, Instance::REMINDER_TIME, DbRecoveryType::LONG)); + static_cast(RecoverInt64FromDb(resultSet, Instance::REMINDER_TIME, DbRecoveryType::LONG)); // triggerTime triggerTimeInMilli_ = - static_cast(RecoveryInt64FromDb(resultSet, Instance::TRIGGER_TIME, DbRecoveryType::LONG)); + static_cast(RecoverInt64FromDb(resultSet, Instance::TRIGGER_TIME, DbRecoveryType::LONG)); // timeInterval uint64_t timeIntervalInSecond = - static_cast(RecoveryInt64FromDb(resultSet, Instance::TIME_INTERVAL, DbRecoveryType::LONG)); + static_cast(RecoverInt64FromDb(resultSet, Instance::TIME_INTERVAL, DbRecoveryType::LONG)); SetTimeInterval(timeIntervalInSecond); // snoozeTimes - snoozeTimes_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::SNOOZE_TIMES, DbRecoveryType::INT)); + snoozeTimes_ = static_cast(RecoverInt64FromDb(resultSet, Instance::SNOOZE_TIMES, DbRecoveryType::INT)); // dynamicSnoozeTimes snoozeTimesDynamic_ = - static_cast(RecoveryInt64FromDb(resultSet, Instance::DYNAMIC_SNOOZE_TIMES, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, Instance::DYNAMIC_SNOOZE_TIMES, DbRecoveryType::INT)); // ringDuration uint64_t ringDurationInSecond = - static_cast(RecoveryInt64FromDb(resultSet, Instance::RING_DURATION, DbRecoveryType::LONG)); + static_cast(RecoverInt64FromDb(resultSet, Instance::RING_DURATION, DbRecoveryType::LONG)); SetRingDuration(ringDurationInSecond); // isExpired @@ -450,10 +461,10 @@ void ReminderRequest::RecoveryFromDb(const std::shared_ptr(RecoveryInt64FromDb(resultSet, Instance::STATE, DbRecoveryType::INT)); + state_ = static_cast(RecoverInt64FromDb(resultSet, Instance::STATE, DbRecoveryType::INT)); // action buttons - RecoveryActionButton(resultSet); + RecoverActionButton(resultSet); // slotType int slotType; @@ -480,29 +491,36 @@ void ReminderRequest::RecoveryFromDb(const std::shared_ptrGetString(ReminderStore::GetColumnIndex(Instance::AGENT), wantAgent); - RecoveryWantAgent(wantAgent, 0); + RecoverWantAgent(wantAgent, 0); // maxScreenWantAgent std::string maxScreenWantAgent; resultSet->GetString(ReminderStore::GetColumnIndex(Instance::MAX_SCREEN_AGENT), maxScreenWantAgent); - RecoveryWantAgent(wantAgent, 1); + RecoverWantAgent(wantAgent, 1); } -void ReminderRequest::RecoveryActionButton(const std::shared_ptr &resultSet) +void ReminderRequest::RecoverActionButton(const std::shared_ptr &resultSet) { + if (resultSet == nullptr) { + ANSR_LOGE("ResultSet is null"); + return; + } std::string actionButtonInfo; resultSet->GetString(ReminderStore::GetColumnIndex(Instance::ACTION_BUTTON_INFO), actionButtonInfo); std::vector multiButton = StringSplit(actionButtonInfo, SEP_BUTTON_MULTI); - for (auto it = multiButton.begin(); it != multiButton.end(); ++it) { - std::vector singleButton = StringSplit(*it, SEP_BUTTON_SINGLE); + for (auto button : multiButton) { + std::vector singleButton = StringSplit(button, SEP_BUTTON_SINGLE); SetActionButton(singleButton.at(1), ActionButtonType(std::stoi(singleButton.at(0), nullptr))); } } std::vector ReminderRequest::StringSplit(std::string source, const std::string &split) const { - size_t pos = 0; std::vector result; + if (source.empty()) { + return result; + } + size_t pos = 0; while ((pos = source.find(split)) != std::string::npos) { std::string token = source.substr(0, pos); if (!token.empty()) { @@ -516,28 +534,34 @@ std::vector ReminderRequest::StringSplit(std::string source, const return result; } -void ReminderRequest::RecoveryWantAgent(std::string wantAgentInfo, const uint8_t &type) +void ReminderRequest::RecoverWantAgent(std::string wantAgentInfo, const uint8_t &type) { std::vector info = StringSplit(wantAgentInfo, ReminderRequest::SEP_WANT_AGENT); uint8_t minLen = 2; if (info.size() < minLen) { - ANSR_LOGW("RecoveryWantAgent fail"); + ANSR_LOGW("RecoverWantAgent fail"); return; } - // AppExecFwk::ElementName wantAgent("", info.at(0), info.at(1)); ANSR_LOGD("pkg=%{public}s, ability=%{public}s", info.at(0).c_str(), info.at(1).c_str()); - if (type == 0) { - auto wantAgentInfo = std::make_shared(); - wantAgentInfo->pkgName = info.at(0); - wantAgentInfo->abilityName = info.at(1); - SetWantAgentInfo(wantAgentInfo); - } else if (type == 1) { - auto maxScreenWantAgentInfo = std::make_shared(); - maxScreenWantAgentInfo->pkgName = info.at(0); - maxScreenWantAgentInfo->abilityName = info.at(1); - SetMaxScreenWantAgentInfo(maxScreenWantAgentInfo); - } else { - ANSR_LOGW("RecoveryWantAgent type not support"); + switch (type) { + case 0: { + auto wantAgentInfo = std::make_shared(); + wantAgentInfo->pkgName = info.at(0); + wantAgentInfo->abilityName = info.at(1); + SetWantAgentInfo(wantAgentInfo); + break; + } + case 1: { + auto maxScreenWantAgentInfo = std::make_shared(); + maxScreenWantAgentInfo->pkgName = info.at(0); + maxScreenWantAgentInfo->abilityName = info.at(1); + SetMaxScreenWantAgentInfo(maxScreenWantAgentInfo); + break; + } + default: { + ANSR_LOGW("RecoverWantAgent type not support"); + break; + } } } @@ -627,11 +651,6 @@ bool ReminderRequest::ShouldShowImmediately() const return true; } -uint8_t ReminderRequest::GetConstStateInactive() -{ - return REMINDER_STATUS_INACTIVE; -} - std::map ReminderRequest::GetActionButtons( ) const { @@ -899,12 +918,12 @@ bool ReminderRequest::Marshalling(Parcel &parcel) const ANSR_LOGE("Failed to write action button size"); return false; } - for (auto it = actionButtonMap_.begin(); it != actionButtonMap_.end(); ++it) { - if (!parcel.WriteUint8(static_cast(it->first))) { + for (auto button : actionButtonMap_) { + if (!parcel.WriteUint8(static_cast(button.first))) { ANSR_LOGE("Failed to write action button type"); return false; } - if (!parcel.WriteString(static_cast(it->second.title))) { + if (!parcel.WriteString(static_cast(button.second.title))) { ANSR_LOGE("Failed to write action button title"); return false; } @@ -1092,12 +1111,12 @@ std::string ReminderRequest::GetButtonInfo() const { std::string info = ""; bool isFirst = true; - for (auto it = actionButtonMap_.begin(); it != actionButtonMap_.end(); ++it) { + for (auto button : actionButtonMap_) { if (!isFirst) { info += SEP_BUTTON_MULTI; } - ActionButtonInfo buttonInfo = it->second; - info += std::to_string(static_cast(it->first)) + SEP_BUTTON_SINGLE + buttonInfo.title; + ActionButtonInfo buttonInfo = button.second; + info += std::to_string(static_cast(button.first)) + SEP_BUTTON_SINGLE + buttonInfo.title; isFirst = false; } return info; @@ -1189,9 +1208,9 @@ void ReminderRequest::AddActionButtons(const bool includeSnooze) int requestCode = 10; std::vector flags; flags.push_back(AbilityRuntime::WantAgent::WantAgentConstant::Flags::UPDATE_PRESENT_FLAG); - for (auto it = actionButtonMap_.begin(); it != actionButtonMap_.end(); ++it) { + for (auto button : actionButtonMap_) { auto want = std::make_shared(); - auto type = it->first; + auto type = button.first; if (type == ActionButtonType::CLOSE) { want->SetAction(REMINDER_EVENT_CLOSE_ALERT); ANSR_LOGD("Add action button, type is close"); @@ -1208,7 +1227,7 @@ void ReminderRequest::AddActionButtons(const bool includeSnooze) want->SetParam(PARAM_REMINDER_ID, reminderId_); std::vector> wants; wants.push_back(want); - auto title = static_cast(it->second.title); + auto title = static_cast(button.second.title); AbilityRuntime::WantAgent::WantAgentInfo buttonWantAgentInfo( requestCode, AbilityRuntime::WantAgent::WantAgentConstant::OperationType::SEND_COMMON_EVENT, @@ -1359,13 +1378,16 @@ void ReminderRequest::UpdateNotificationBundleInfo() if (!(notificationRequest_->GetOwnerBundleName()).empty()) { return; } - notificationRequest_->SetOwnerBundleName(bundleName_); notificationRequest_->SetCreatorBundleName(bundleName_); notificationRequest_->SetCreatorUid(uid_); - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid_, userId_); + ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid_, userId_); + if (errCode != ERR_OK) { + ANSR_LOGE("GetOsAccountLocalIdFromUid fail."); + return; + } notificationRequest_->SetCreatorUserId(userId_); } @@ -1552,8 +1574,8 @@ const std::string ReminderRequest::Instance::EXPIRED_CONTENT = "expired_content" const std::string ReminderRequest::Instance::AGENT = "agent"; const std::string ReminderRequest::Instance::MAX_SCREEN_AGENT = "maxScreen_agent"; -std::string ReminderRequest::Instance::SQL_ADD_COLUMNS = ""; -std::vector ReminderRequest::Instance::COLUMNS; +std::string ReminderRequest::Instance::sqlOfAddColumns = ""; +std::vector ReminderRequest::Instance::columns; void ReminderRequest::Instance::Init() { @@ -1589,11 +1611,11 @@ void ReminderRequest::Instance::Init() void ReminderRequest::Instance::AddColumn( const std::string &name, const std::string &type, const bool &isEnd) { - COLUMNS.push_back(name); + columns.push_back(name); if (!isEnd) { - SQL_ADD_COLUMNS += name + " " + type + ", "; + sqlOfAddColumns += name + " " + type + ", "; } else { - SQL_ADD_COLUMNS += name + " " + type; + sqlOfAddColumns += name + " " + type; } } } diff --git a/frameworks/ans/native/src/reminder_request_alarm.cpp b/frameworks/ans/native/src/reminder_request_alarm.cpp index 988ef8459..55f5a9275 100644 --- a/frameworks/ans/native/src/reminder_request_alarm.cpp +++ b/frameworks/ans/native/src/reminder_request_alarm.cpp @@ -298,21 +298,21 @@ bool ReminderRequestAlarm::ReadFromParcel(Parcel &parcel) return true; } -void ReminderRequestAlarm::RecoveryFromDb(const std::shared_ptr &resultSet) +void ReminderRequestAlarm::RecoverFromDb(const std::shared_ptr &resultSet) { - ReminderRequest::RecoveryFromDb(resultSet); + ReminderRequest::RecoverFromDb(resultSet); // repeatDays repeatDays_ = - static_cast(RecoveryInt64FromDb(resultSet, Instance::REPEAT_DAYS_OF_WEEK, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, Instance::REPEAT_DAYS_OF_WEEK, DbRecoveryType::INT)); // hour hour_ = - static_cast(RecoveryInt64FromDb(resultSet, Instance::ALARM_HOUR, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, Instance::ALARM_HOUR, DbRecoveryType::INT)); // minute minute_ = - static_cast(RecoveryInt64FromDb(resultSet, Instance::ALARM_MINUTE, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, Instance::ALARM_MINUTE, DbRecoveryType::INT)); } void ReminderRequestAlarm::AppendValuesBucket(const sptr &reminder, @@ -336,8 +336,8 @@ const std::string ReminderRequestAlarm::Instance::REPEAT_DAYS_OF_WEEK = "repeat_ const std::string ReminderRequestAlarm::Instance::ALARM_HOUR = "alarm_hour"; const std::string ReminderRequestAlarm::Instance::ALARM_MINUTE = "alarm_minute"; -std::string ReminderRequestAlarm::Instance::SQL_ADD_COLUMNS = ""; -std::vector ReminderRequestAlarm::Instance::COLUMNS; +std::string ReminderRequestAlarm::Instance::sqlOfAddColumns = ""; +std::vector ReminderRequestAlarm::Instance::columns; void ReminderRequestAlarm::Instance::Init() { @@ -349,11 +349,11 @@ void ReminderRequestAlarm::Instance::Init() void ReminderRequestAlarm::Instance::AddColumn( const std::string &name, const std::string &type, const bool &isEnd) { - COLUMNS.push_back(name); + columns.push_back(name); if (!isEnd) { - SQL_ADD_COLUMNS += name + " " + type + ", "; + sqlOfAddColumns += name + " " + type + ", "; } else { - SQL_ADD_COLUMNS += name + " " + type; + sqlOfAddColumns += name + " " + type; } } } diff --git a/frameworks/ans/native/src/reminder_request_calendar.cpp b/frameworks/ans/native/src/reminder_request_calendar.cpp index 4f61f745b..020ac45ea 100644 --- a/frameworks/ans/native/src/reminder_request_calendar.cpp +++ b/frameworks/ans/native/src/reminder_request_calendar.cpp @@ -499,43 +499,43 @@ bool ReminderRequestCalendar::ReadFromParcel(Parcel &parcel) return true; } -void ReminderRequestCalendar::RecoveryFromDb(const std::shared_ptr &resultSet) +void ReminderRequestCalendar::RecoverFromDb(const std::shared_ptr &resultSet) { - ReminderRequest::RecoveryFromDb(resultSet); + ReminderRequest::RecoverFromDb(resultSet); // repeatDay - repeatDay_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::REPEAT_DAYS, DbRecoveryType::INT)); + repeatDay_ = static_cast(RecoverInt64FromDb(resultSet, Instance::REPEAT_DAYS, DbRecoveryType::INT)); // repeatMonth repeatMonth_ = - static_cast(RecoveryInt64FromDb(resultSet, Instance::REPEAT_MONTHS, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, Instance::REPEAT_MONTHS, DbRecoveryType::INT)); // firstDesignateYear firstDesignateYear_ = - static_cast(RecoveryInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_YEAR, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_YEAR, DbRecoveryType::INT)); // firstDesignateMonth firstDesignateMonth_ = - static_cast(RecoveryInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_MONTH, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_MONTH, DbRecoveryType::INT)); // firstDesignateDay firstDesignateDay_ = - static_cast(RecoveryInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_DAY, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_DAY, DbRecoveryType::INT)); // year - year_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::CALENDAR_YEAR, DbRecoveryType::INT)); + year_ = static_cast(RecoverInt64FromDb(resultSet, Instance::CALENDAR_YEAR, DbRecoveryType::INT)); // month - month_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::CALENDAR_MONTH, DbRecoveryType::INT)); + month_ = static_cast(RecoverInt64FromDb(resultSet, Instance::CALENDAR_MONTH, DbRecoveryType::INT)); // day - day_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::CALENDAR_DAY, DbRecoveryType::INT)); + day_ = static_cast(RecoverInt64FromDb(resultSet, Instance::CALENDAR_DAY, DbRecoveryType::INT)); // hour - hour_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::CALENDAR_HOUR, DbRecoveryType::INT)); + hour_ = static_cast(RecoverInt64FromDb(resultSet, Instance::CALENDAR_HOUR, DbRecoveryType::INT)); // minute - minute_ = static_cast(RecoveryInt64FromDb(resultSet, Instance::CALENDAR_MINUTE, DbRecoveryType::INT)); + minute_ = static_cast(RecoverInt64FromDb(resultSet, Instance::CALENDAR_MINUTE, DbRecoveryType::INT)); } void ReminderRequestCalendar::AppendValuesBucket(const sptr &reminder, @@ -587,8 +587,8 @@ const std::string ReminderRequestCalendar::Instance::CALENDAR_DAY = "calendar_da const std::string ReminderRequestCalendar::Instance::CALENDAR_HOUR = "calendar_hour"; const std::string ReminderRequestCalendar::Instance::CALENDAR_MINUTE = "calendar_minute"; -std::string ReminderRequestCalendar::Instance::SQL_ADD_COLUMNS = ""; -std::vector ReminderRequestCalendar::Instance::COLUMNS; +std::string ReminderRequestCalendar::Instance::sqlOfAddColumns = ""; +std::vector ReminderRequestCalendar::Instance::columns; void ReminderRequestCalendar::Instance::Init() { @@ -607,11 +607,11 @@ void ReminderRequestCalendar::Instance::Init() void ReminderRequestCalendar::Instance::AddColumn( const std::string &name, const std::string &type, const bool &isEnd) { - COLUMNS.push_back(name); + columns.push_back(name); if (!isEnd) { - SQL_ADD_COLUMNS += name + " " + type + ", "; + sqlOfAddColumns += name + " " + type + ", "; } else { - SQL_ADD_COLUMNS += name + " " + type; + sqlOfAddColumns += name + " " + type; } } } diff --git a/frameworks/ans/test/moduletest/BUILD.gn b/frameworks/ans/test/moduletest/BUILD.gn index 8eaa9e509..5312f6073 100644 --- a/frameworks/ans/test/moduletest/BUILD.gn +++ b/frameworks/ans/test/moduletest/BUILD.gn @@ -91,6 +91,7 @@ ohos_moduletest("ans_fw_module_test") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", ] @@ -241,6 +242,7 @@ ohos_moduletest("ans_innerkits_module_slot_test") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", ] @@ -314,6 +316,7 @@ ohos_moduletest("ans_innerkits_module_setting_test") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", ] diff --git a/interfaces/innerkits/ans/native/include/reminder_request.h b/interfaces/innerkits/ans/native/include/reminder_request.h index d6125345e..3852bafb0 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request.h +++ b/interfaces/innerkits/ans/native/include/reminder_request.h @@ -408,9 +408,9 @@ public: * * @param resultSet Indicates the resultSet with pointer to the row of record data. */ - virtual void RecoveryFromDb(const std::shared_ptr &resultSet); - void RecoveryActionButton(const std::shared_ptr &resultSet); - void RecoveryWantAgent(std::string wantAgentInfo, const uint8_t &type); + virtual void RecoverFromDb(const std::shared_ptr &resultSet); + void RecoverActionButton(const std::shared_ptr &resultSet); + void RecoverWantAgent(std::string wantAgentInfo, const uint8_t &type); /** * @brief Sets action button. @@ -559,7 +559,13 @@ public: */ void UpdateNotificationRequest(UpdateNotificationType type, std::string extra); - static uint8_t GetConstStateInactive(); + static int GetActualTime(const TimeTransferType &type, int cTime); + static int GetCTime(const TimeTransferType &type, int actualTime); + static uint64_t GetDurationSinceEpochInMilli(const time_t target); + static int32_t GetUid(const int &userId, const std::string &bundleName); + static int GetUserId(const int &uid); + static void AppendValuesBucket(const sptr &reminder, + const sptr &bundleOption, NativeRdb::ValuesBucket &values); static int32_t GLOBAL_ID; static const uint64_t INVALID_LONG_LONG_VALUE; @@ -594,13 +600,11 @@ public: */ static const std::string REMINDER_EVENT_REMOVE_NOTIFICATION; static const std::string PARAM_REMINDER_ID; - static int GetActualTime(const TimeTransferType &type, int cTime); - static int GetCTime(const TimeTransferType &type, int actualTime); - static uint64_t GetDurationSinceEpochInMilli(const time_t target); - static int32_t GetUid(const int &userId, const std::string &bundleName); - static int GetUserId(const int &uid); - static void AppendValuesBucket(const sptr &reminder, - const sptr &bundleOption, NativeRdb::ValuesBucket &values); + static const uint8_t REMINDER_STATUS_INACTIVE; + static const uint8_t REMINDER_STATUS_ACTIVE; + static const uint8_t REMINDER_STATUS_ALERTING; + static const uint8_t REMINDER_STATUS_SHOWING; + static const uint8_t REMINDER_STATUS_SNOOZE; class Instance { public: @@ -632,8 +636,8 @@ public: const static std::string AGENT; const static std::string MAX_SCREEN_AGENT; - static std::string SQL_ADD_COLUMNS; - static std::vector COLUMNS; + static std::string sqlOfAddColumns; + static std::vector columns; static void Init(); private: @@ -652,7 +656,7 @@ protected: { return INVALID_LONG_LONG_VALUE; } - int64_t RecoveryInt64FromDb(const std::shared_ptr &resultSet, + int64_t RecoverInt64FromDb(const std::shared_ptr &resultSet, const std::string &columnName, const DbRecoveryType &columnType); static const int BASE_YEAR; @@ -699,11 +703,6 @@ private: */ void UpdateNotificationStateForSnooze(); - static const uint8_t REMINDER_STATUS_INACTIVE; - static const uint8_t REMINDER_STATUS_ACTIVE; - static const uint8_t REMINDER_STATUS_ALERTING; - static const uint8_t REMINDER_STATUS_SHOWING; - static const uint8_t REMINDER_STATUS_SNOOZE; static const uint32_t MIN_TIME_INTERVAL_IN_MILLI; static const std::string SEP_BUTTON_SINGLE; static const std::string SEP_BUTTON_MULTI; diff --git a/interfaces/innerkits/ans/native/include/reminder_request_alarm.h b/interfaces/innerkits/ans/native/include/reminder_request_alarm.h index 659d798dc..5277bb4a6 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_alarm.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_alarm.h @@ -104,7 +104,7 @@ public: * @return true if read parcel success. */ bool ReadFromParcel(Parcel &parcel) override; - virtual void RecoveryFromDb(const std::shared_ptr &resultSet) override; + virtual void RecoverFromDb(const std::shared_ptr &resultSet) override; static void AppendValuesBucket(const sptr &reminder, const sptr &bundleOption, NativeRdb::ValuesBucket &values); @@ -114,8 +114,8 @@ public: const static std::string ALARM_HOUR; const static std::string ALARM_MINUTE; - static std::string SQL_ADD_COLUMNS; - static std::vector COLUMNS; + static std::string sqlOfAddColumns; + static std::vector columns; static void Init(); private: diff --git a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h index 0f5bcde24..0e4576883 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h @@ -141,7 +141,7 @@ public: static const uint8_t MAX_MONTHS_OF_YEAR; static const uint8_t MAX_DAYS_OF_MONTH; - virtual void RecoveryFromDb(const std::shared_ptr &resultSet) override; + virtual void RecoverFromDb(const std::shared_ptr &resultSet) override; static void AppendValuesBucket(const sptr &reminder, const sptr &bundleOption, NativeRdb::ValuesBucket &values); static uint8_t GetDaysOfMonth(const uint16_t &year, const uint8_t &month); @@ -159,8 +159,8 @@ public: const static std::string CALENDAR_HOUR; const static std::string CALENDAR_MINUTE; - static std::string SQL_ADD_COLUMNS; - static std::vector COLUMNS; + static std::string sqlOfAddColumns; + static std::vector columns; static void Init(); private: diff --git a/services/ans/include/reminder_event_manager.h b/services/ans/include/reminder_event_manager.h index 54f900a48..50aacfd84 100644 --- a/services/ans/include/reminder_event_manager.h +++ b/services/ans/include/reminder_event_manager.h @@ -49,7 +49,7 @@ private: class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { public: - SystemAbilityStatusChangeListener(std::shared_ptr &reminderDataManager); + explicit SystemAbilityStatusChangeListener(std::shared_ptr &reminderDataManager); ~SystemAbilityStatusChangeListener() {}; virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index a5443765d..18e18bfed 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -870,15 +870,14 @@ void ReminderDataManager::Init(bool isFromBootComplete) void ReminderDataManager::GetImmediatelyShowRemindersLocked(std::vector> &reminders) const { std::lock_guard lock(ReminderDataManager::MUTEX); - for (auto it = reminderVector_.begin(); it != reminderVector_.end(); ++it) { - if ((*it)->ShouldShowImmediately()) { - if ((*it)->GetReminderType() != ReminderRequest::ReminderType::TIMER) { - (*it)->SetSnoozeTimesDynamic(0); - } - reminders.push_back(*it); - } else { + for (auto reminderSptr : reminderVector_) { + if (!(reminderSptr->ShouldShowImmediately())) { break; } + if (reminderSptr->GetReminderType() != ReminderRequest::ReminderType::TIMER) { + reminderSptr->SetSnoozeTimesDynamic(0); + } + reminders.push_back(reminderSptr); } } -- Gitee From 634f45834bd27f00d7200a7617a22081c31fa8ae Mon Sep 17 00:00:00 2001 From: linyunxuan Date: Thu, 3 Mar 2022 15:08:32 +0800 Subject: [PATCH 5/8] add database feature Signed-off-by: linyunxuan --- frameworks/ans/native/src/reminder_store.cpp | 60 ++++++++++--------- .../ans/native/include/reminder_store.h | 2 +- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/frameworks/ans/native/src/reminder_store.cpp b/frameworks/ans/native/src/reminder_store.cpp index 57b3a9ea0..77954217b 100644 --- a/frameworks/ans/native/src/reminder_store.cpp +++ b/frameworks/ans/native/src/reminder_store.cpp @@ -36,16 +36,16 @@ const int32_t ReminderStore::STATE_FAIL = -1; const int32_t ReminderStore::STATE_OK = 0; const uint16_t ReminderStore::TIME_INTERVAL_FOR_DELETE = 1800; const uint16_t ReminderStore::MILLI_SECONDS = 1000; -std::vector ReminderStore::COLUMNS; +std::vector ReminderStore::columns; int32_t ReminderStore::ReminderStoreDataCallBack::OnCreate(NativeRdb::RdbStore &store) { ANSR_LOGD("Create table."); std::string CREATE_REMINDER_TABLE = "CREATE TABLE IF NOT EXISTS " + REMINDER_DB_TABLE + " (" - + ReminderRequest::Instance::SQL_ADD_COLUMNS - + ReminderRequestCalendar::Instance::SQL_ADD_COLUMNS - + ReminderRequestAlarm::Instance::SQL_ADD_COLUMNS + ")"; - ANSR_LOGD("createTable:%{public}s", CREATE_REMINDER_TABLE.c_str()); + + ReminderRequest::Instance::sqlOfAddColumns + + ReminderRequestCalendar::Instance::sqlOfAddColumns + + ReminderRequestAlarm::Instance::sqlOfAddColumns + ")"; + ANSR_LOGD("CreateTable:%{public}s", CREATE_REMINDER_TABLE.c_str()); return store.ExecuteSql(CREATE_REMINDER_TABLE); } @@ -71,12 +71,12 @@ int32_t ReminderStore::Init() ReminderRequest::Instance::Init(); ReminderRequestCalendar::Instance::Init(); ReminderRequestAlarm::Instance::Init(); - ReminderStore::COLUMNS.insert(ReminderStore::COLUMNS.begin(), - ReminderRequestAlarm::Instance::COLUMNS.begin(), ReminderRequestAlarm::Instance::COLUMNS.end()); - ReminderStore::COLUMNS.insert(ReminderStore::COLUMNS.begin(), - ReminderRequestCalendar::Instance::COLUMNS.begin(), ReminderRequestCalendar::Instance::COLUMNS.end()); - ReminderStore::COLUMNS.insert(ReminderStore::COLUMNS.begin(), - ReminderRequest::Instance::COLUMNS.begin(), ReminderRequest::Instance::COLUMNS.end()); + ReminderStore::columns.insert(ReminderStore::columns.begin(), + ReminderRequestAlarm::Instance::columns.begin(), ReminderRequestAlarm::Instance::columns.end()); + ReminderStore::columns.insert(ReminderStore::columns.begin(), + ReminderRequestCalendar::Instance::columns.begin(), ReminderRequestCalendar::Instance::columns.end()); + ReminderStore::columns.insert(ReminderStore::columns.begin(), + ReminderRequest::Instance::columns.begin(), ReminderRequest::Instance::columns.end()); std::string dbConfig = ReminderStore::REMINDER_DB_DIR + ReminderStore::REMINDER_DB_NAME; NativeRdb::RdbStoreConfig config_(dbConfig); @@ -97,9 +97,13 @@ int32_t ReminderStore::InitData() int32_t statusChangedRows = STATE_FAIL; NativeRdb::ValuesBucket statusValues; - statusValues.PutInt(ReminderRequest::Instance::STATE, ReminderRequest::GetConstStateInactive()); + statusValues.PutInt(ReminderRequest::Instance::STATE, ReminderRequest::REMINDER_STATUS_INACTIVE); int32_t statusResult = rdbStore_->Update(statusChangedRows, REMINDER_DB_TABLE, statusValues); ANSR_LOGD("Change status to inactive, changed rows: %{public}d.", statusChangedRows); + if (statusResult != NativeRdb::E_OK) { + ANSR_LOGE("Init data failed."); + return STATE_FAIL; + } int32_t activeChangedRows = STATE_FAIL; NativeRdb::ValuesBucket activeValues; @@ -109,6 +113,10 @@ int32_t ReminderStore::InitData() int32_t activeResult = rdbStore_->Update( activeChangedRows, REMINDER_DB_TABLE, activeValues, activeUpdateCondition, activeWhereArgs); ANSR_LOGD("Change status isActive to false, changed rows: %{public}d.", activeChangedRows); + if (activeResult != NativeRdb::E_OK) { + ANSR_LOGE("Init data failed."); + return STATE_FAIL; + } int32_t scheduledChangedRows = STATE_FAIL; NativeRdb::ValuesBucket scheduledValues; @@ -118,9 +126,7 @@ int32_t ReminderStore::InitData() int32_t scheduledResult = rdbStore_->Update( scheduledChangedRows, REMINDER_DB_TABLE, scheduledValues, scheduledUpdateCondition, scheduledWhereArgs); ANSR_LOGD("Change status has_ScheduledTimeout to false, changed rows: %{public}d.", scheduledChangedRows); - - if (statusResult != NativeRdb::E_OK || activeResult != NativeRdb::E_OK - || scheduledResult != NativeRdb::E_OK) { + if (scheduledResult != NativeRdb::E_OK) { ANSR_LOGE("Init data failed."); return STATE_FAIL; } @@ -173,10 +179,14 @@ int64_t ReminderStore::UpdateOrInsert( return STATE_FAIL; } int64_t isSuccess = STATE_FAIL; + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return false; + } if (IsReminderExist(reminder)) { - isSuccess = ReminderStore::Update(reminder, bundleOption); + isSuccess = Update(reminder, bundleOption); } else { - isSuccess = ReminderStore::Insert(reminder, bundleOption); + isSuccess = Insert(reminder, bundleOption); } return isSuccess; } @@ -184,10 +194,6 @@ int64_t ReminderStore::UpdateOrInsert( int64_t ReminderStore::Insert( const sptr &reminder, const sptr &bundleOption) { - if (rdbStore_ == nullptr) { - ANSR_LOGE("Rdb store is not initialized."); - return STATE_FAIL; - } if (bundleOption == nullptr) { ANSR_LOGE("BundleOption is null."); return STATE_FAIL; @@ -208,8 +214,8 @@ int64_t ReminderStore::Insert( int64_t ReminderStore::Update( const sptr &reminder, const sptr &bundleOption) { - if (rdbStore_ == nullptr) { - ANSR_LOGE("Rdb store is not initialized."); + if (bundleOption == nullptr) { + ANSR_LOGE("BundleOption is null."); return STATE_FAIL; } int32_t changedRows = STATE_FAIL; @@ -231,10 +237,6 @@ int64_t ReminderStore::Update( bool ReminderStore::IsReminderExist(const sptr &reminder) { - if (rdbStore_ == nullptr) { - ANSR_LOGE("Rdb store is not initialized."); - return false; - } std::string queryCondition = "select " + ReminderRequest::Instance::REMINDER_ID + " from " + REMINDER_DB_TABLE + " where " + ReminderRequest::Instance::REMINDER_ID + " = " + std::to_string(reminder->GetReminderId()); @@ -278,7 +280,7 @@ std::vector ReminderStore::GetRepeatInfo(int64_t repeatData, int32_t ma uint8_t ReminderStore::GetColumnIndex(const std::string& name) { uint8_t index = 0; - for (auto it = ReminderStore::COLUMNS.begin(); it != ReminderStore::COLUMNS.end(); ++it) { + for (auto it = ReminderStore::columns.begin(); it != ReminderStore::columns.end(); ++it) { if (name == (*it)) { break; } @@ -376,7 +378,7 @@ sptr ReminderStore::BuildReminder(const std::shared_ptrRecoveryFromDb(resultSet); + reminder->RecoverFromDb(resultSet); ANSR_LOGI("BuildReminder success."); } else { ANSR_LOGW("BuildReminder fail."); diff --git a/interfaces/innerkits/ans/native/include/reminder_store.h b/interfaces/innerkits/ans/native/include/reminder_store.h index 48a68a15d..ca6dca05d 100644 --- a/interfaces/innerkits/ans/native/include/reminder_store.h +++ b/interfaces/innerkits/ans/native/include/reminder_store.h @@ -42,7 +42,7 @@ public: static std::vector GetRepeatInfo(int64_t repeatData, int32_t maxRepeatVal); static uint8_t GetColumnIndex(const std::string& name); - static std::vector COLUMNS; + static std::vector columns; static const int32_t STATE_FAIL; static const int32_t STATE_OK; -- Gitee From 2805f9dc2f9afddd5a3ff1122469a6a0e5fe3471 Mon Sep 17 00:00:00 2001 From: linyunxuan Date: Thu, 3 Mar 2022 17:23:44 +0800 Subject: [PATCH 6/8] add database feature Signed-off-by: linyunxuan --- .../ans/native/src/reminder_request.cpp | 177 +++++++++--------- .../ans/native/src/reminder_request_alarm.cpp | 31 ++- .../native/src/reminder_request_calendar.cpp | 75 ++++---- frameworks/ans/native/src/reminder_store.cpp | 76 ++++---- .../ans/native/include/reminder_request.h | 73 ++++---- .../native/include/reminder_request_alarm.h | 19 +- .../include/reminder_request_calendar.h | 34 ++-- .../ans/native/include/reminder_store.h | 2 +- 8 files changed, 232 insertions(+), 255 deletions(-) diff --git a/frameworks/ans/native/src/reminder_request.cpp b/frameworks/ans/native/src/reminder_request.cpp index 5f84ee059..c2c128691 100644 --- a/frameworks/ans/native/src/reminder_request.cpp +++ b/frameworks/ans/native/src/reminder_request.cpp @@ -414,88 +414,88 @@ void ReminderRequest::RecoverFromDb(const std::shared_ptrGetInt(ReminderStore::GetColumnIndex(Instance::REMINDER_ID), reminderId_); + resultSet->GetInt(ReminderStore::GetColumnIndex(REMINDER_ID), reminderId_); // userId - resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::USER_ID), userId_); + resultSet->GetInt(ReminderStore::GetColumnIndex(USER_ID), userId_); // bundleName - resultSet->GetString(ReminderStore::GetColumnIndex(Instance::PKG_NAME), bundleName_); + resultSet->GetString(ReminderStore::GetColumnIndex(PKG_NAME), bundleName_); // uid - resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::UID), uid_); + resultSet->GetInt(ReminderStore::GetColumnIndex(UID), uid_); // reminderType int reminderType; - resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::REMINDER_TYPE), reminderType); + resultSet->GetInt(ReminderStore::GetColumnIndex(REMINDER_TYPE), reminderType); reminderType_ = ReminderType(reminderType); // reminderTime reminderTimeInMilli_ = - static_cast(RecoverInt64FromDb(resultSet, Instance::REMINDER_TIME, DbRecoveryType::LONG)); + static_cast(RecoverInt64FromDb(resultSet, REMINDER_TIME, DbRecoveryType::LONG)); // triggerTime triggerTimeInMilli_ = - static_cast(RecoverInt64FromDb(resultSet, Instance::TRIGGER_TIME, DbRecoveryType::LONG)); + static_cast(RecoverInt64FromDb(resultSet, TRIGGER_TIME, DbRecoveryType::LONG)); // timeInterval uint64_t timeIntervalInSecond = - static_cast(RecoverInt64FromDb(resultSet, Instance::TIME_INTERVAL, DbRecoveryType::LONG)); + static_cast(RecoverInt64FromDb(resultSet, TIME_INTERVAL, DbRecoveryType::LONG)); SetTimeInterval(timeIntervalInSecond); // snoozeTimes - snoozeTimes_ = static_cast(RecoverInt64FromDb(resultSet, Instance::SNOOZE_TIMES, DbRecoveryType::INT)); + snoozeTimes_ = static_cast(RecoverInt64FromDb(resultSet, SNOOZE_TIMES, DbRecoveryType::INT)); // dynamicSnoozeTimes snoozeTimesDynamic_ = - static_cast(RecoverInt64FromDb(resultSet, Instance::DYNAMIC_SNOOZE_TIMES, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, DYNAMIC_SNOOZE_TIMES, DbRecoveryType::INT)); // ringDuration uint64_t ringDurationInSecond = - static_cast(RecoverInt64FromDb(resultSet, Instance::RING_DURATION, DbRecoveryType::LONG)); + static_cast(RecoverInt64FromDb(resultSet, RING_DURATION, DbRecoveryType::LONG)); SetRingDuration(ringDurationInSecond); // isExpired std::string isExpired; - resultSet->GetString(ReminderStore::GetColumnIndex(Instance::IS_EXPIRED), isExpired); + resultSet->GetString(ReminderStore::GetColumnIndex(IS_EXPIRED), isExpired); isExpired_ = isExpired == "true" ? true : false; // state - state_ = static_cast(RecoverInt64FromDb(resultSet, Instance::STATE, DbRecoveryType::INT)); + state_ = static_cast(RecoverInt64FromDb(resultSet, STATE, DbRecoveryType::INT)); // action buttons RecoverActionButton(resultSet); // slotType int slotType; - resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::SLOT_ID), slotType); + resultSet->GetInt(ReminderStore::GetColumnIndex(SLOT_ID), slotType); slotType_ = NotificationConstant::SlotType(slotType); // notification id - resultSet->GetInt(ReminderStore::GetColumnIndex(Instance::NOTIFICATION_ID), notificationId_); + resultSet->GetInt(ReminderStore::GetColumnIndex(NOTIFICATION_ID), notificationId_); // title - resultSet->GetString(ReminderStore::GetColumnIndex(Instance::TITLE), title_); + resultSet->GetString(ReminderStore::GetColumnIndex(TITLE), title_); // content - resultSet->GetString(ReminderStore::GetColumnIndex(Instance::CONTENT), content_); + resultSet->GetString(ReminderStore::GetColumnIndex(CONTENT), content_); // snoozeContent - resultSet->GetString(ReminderStore::GetColumnIndex(Instance::SNOOZE_CONTENT), snoozeContent_); + resultSet->GetString(ReminderStore::GetColumnIndex(SNOOZE_CONTENT), snoozeContent_); // expiredContent - resultSet->GetString(ReminderStore::GetColumnIndex(Instance::EXPIRED_CONTENT), expiredContent_); + resultSet->GetString(ReminderStore::GetColumnIndex(EXPIRED_CONTENT), expiredContent_); InitNotificationRequest(); // must set before wantAgent & maxScreenWantAgent // wantAgent std::string wantAgent; - resultSet->GetString(ReminderStore::GetColumnIndex(Instance::AGENT), wantAgent); + resultSet->GetString(ReminderStore::GetColumnIndex(AGENT), wantAgent); RecoverWantAgent(wantAgent, 0); // maxScreenWantAgent std::string maxScreenWantAgent; - resultSet->GetString(ReminderStore::GetColumnIndex(Instance::MAX_SCREEN_AGENT), maxScreenWantAgent); + resultSet->GetString(ReminderStore::GetColumnIndex(MAX_SCREEN_AGENT), maxScreenWantAgent); RecoverWantAgent(wantAgent, 1); } @@ -506,7 +506,7 @@ void ReminderRequest::RecoverActionButton(const std::shared_ptrGetString(ReminderStore::GetColumnIndex(Instance::ACTION_BUTTON_INFO), actionButtonInfo); + resultSet->GetString(ReminderStore::GetColumnIndex(ACTION_BUTTON_INFO), actionButtonInfo); std::vector multiButton = StringSplit(actionButtonInfo, SEP_BUTTON_MULTI); for (auto button : multiButton) { std::vector singleButton = StringSplit(button, SEP_BUTTON_SINGLE); @@ -1373,16 +1373,15 @@ void ReminderRequest::UpdateNotificationCommon() void ReminderRequest::UpdateNotificationBundleInfo() { - ANSR_LOGD("Bundle name=%{public}s, bundleName_=%{public}s", - notificationRequest_->GetOwnerBundleName().c_str(), bundleName_.c_str()); - if (!(notificationRequest_->GetOwnerBundleName()).empty()) { + std::string ownerBundleName = notificationRequest_->GetOwnerBundleName(); + if (!(ownerBundleName.empty())) { return; } + ANSR_LOGD("ownerBundleName=%{public}s, bundleName_=%{public}s", + ownerBundleName.c_str(), bundleName_.c_str()); notificationRequest_->SetOwnerBundleName(bundleName_); notificationRequest_->SetCreatorBundleName(bundleName_); - notificationRequest_->SetCreatorUid(uid_); - ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid_, userId_); if (errCode != ERR_OK) { ANSR_LOGE("GetOsAccountLocalIdFromUid fail."); @@ -1502,82 +1501,82 @@ int ReminderRequest::GetUserId(const int &uid) void ReminderRequest::AppendValuesBucket(const sptr &reminder, const sptr &bundleOption, NativeRdb::ValuesBucket &values) { - values.PutInt(Instance::REMINDER_ID, reminder->GetReminderId()); - values.PutString(Instance::PKG_NAME, bundleOption->GetBundleName()); - values.PutInt(Instance::USER_ID, reminder->GetUserId()); - values.PutInt(Instance::UID, reminder->GetUid()); - values.PutString(Instance::APP_LABEL, ""); // no use, compatible with old version. - values.PutInt(Instance::REMINDER_TYPE, static_cast(reminder->GetReminderType())); - values.PutLong(Instance::REMINDER_TIME, reminder->GetReminderTimeInMilli()); - values.PutLong(Instance::TRIGGER_TIME, reminder->GetTriggerTimeInMilli()); + values.PutInt(REMINDER_ID, reminder->GetReminderId()); + values.PutString(PKG_NAME, bundleOption->GetBundleName()); + values.PutInt(USER_ID, reminder->GetUserId()); + values.PutInt(UID, reminder->GetUid()); + values.PutString(APP_LABEL, ""); // no use, compatible with old version. + values.PutInt(REMINDER_TYPE, static_cast(reminder->GetReminderType())); + values.PutLong(REMINDER_TIME, reminder->GetReminderTimeInMilli()); + values.PutLong(TRIGGER_TIME, reminder->GetTriggerTimeInMilli()); values.PutLong( - Instance::RTC_TRIGGER_TIME, reminder->GetTriggerTimeInMilli()); // no use, compatible with old version. - values.PutLong(Instance::TIME_INTERVAL, reminder->GetTimeInterval()); - values.PutInt(Instance::SNOOZE_TIMES, reminder->GetSnoozeTimes()); - values.PutInt(Instance::DYNAMIC_SNOOZE_TIMES, reminder->GetSnoozeTimesDynamic()); - values.PutLong(Instance::RING_DURATION, reminder->GetRingDuration()); - values.PutString(Instance::IS_EXPIRED, reminder->IsExpired() ? "true" : "false"); - values.PutString(Instance::IS_ACTIVE, ""); // no use, compatible with old version. - values.PutInt(Instance::STATE, reminder->GetState()); - values.PutString(Instance::ZONE_ID, ""); // no use, compatible with old version. - values.PutString(Instance::HAS_SCHEDULED_TIMEOUT, ""); // no use, compatible with old version. - values.PutString(Instance::ACTION_BUTTON_INFO, reminder->GetButtonInfo()); - values.PutInt(Instance::SLOT_ID, reminder->GetSlotType()); - values.PutInt(Instance::NOTIFICATION_ID, reminder->GetNotificationId()); - values.PutString(Instance::TITLE, reminder->GetTitle()); - values.PutString(Instance::CONTENT, reminder->GetContent()); - values.PutString(Instance::SNOOZE_CONTENT, reminder->GetSnoozeContent()); - values.PutString(Instance::EXPIRED_CONTENT, reminder->GetExpiredContent()); + RTC_TRIGGER_TIME, reminder->GetTriggerTimeInMilli()); // no use, compatible with old version. + values.PutLong(TIME_INTERVAL, reminder->GetTimeInterval()); + values.PutInt(SNOOZE_TIMES, reminder->GetSnoozeTimes()); + values.PutInt(DYNAMIC_SNOOZE_TIMES, reminder->GetSnoozeTimesDynamic()); + values.PutLong(RING_DURATION, reminder->GetRingDuration()); + values.PutString(IS_EXPIRED, reminder->IsExpired() ? "true" : "false"); + values.PutString(IS_ACTIVE, ""); // no use, compatible with old version. + values.PutInt(STATE, reminder->GetState()); + values.PutString(ZONE_ID, ""); // no use, compatible with old version. + values.PutString(HAS_SCHEDULED_TIMEOUT, ""); // no use, compatible with old version. + values.PutString(ACTION_BUTTON_INFO, reminder->GetButtonInfo()); + values.PutInt(SLOT_ID, reminder->GetSlotType()); + values.PutInt(NOTIFICATION_ID, reminder->GetNotificationId()); + values.PutString(TITLE, reminder->GetTitle()); + values.PutString(CONTENT, reminder->GetContent()); + values.PutString(SNOOZE_CONTENT, reminder->GetSnoozeContent()); + values.PutString(EXPIRED_CONTENT, reminder->GetExpiredContent()); auto wantAgentInfo = reminder->GetWantAgentInfo(); if (wantAgentInfo == nullptr) { std::string info = "null" + ReminderRequest::SEP_WANT_AGENT + "null"; - values.PutString(Instance::AGENT, info); + values.PutString(AGENT, info); } else { - values.PutString(Instance::AGENT, wantAgentInfo->pkgName + values.PutString(AGENT, wantAgentInfo->pkgName + ReminderRequest::SEP_WANT_AGENT + wantAgentInfo->abilityName); } auto maxScreenWantAgentInfo = reminder->GetMaxScreenWantAgentInfo(); if (maxScreenWantAgentInfo == nullptr) { std::string info = "null" + ReminderRequest::SEP_WANT_AGENT + "null"; - values.PutString(Instance::MAX_SCREEN_AGENT, info); + values.PutString(MAX_SCREEN_AGENT, info); } else { - values.PutString(Instance::MAX_SCREEN_AGENT, maxScreenWantAgentInfo->pkgName + values.PutString(MAX_SCREEN_AGENT, maxScreenWantAgentInfo->pkgName + ReminderRequest::SEP_WANT_AGENT + maxScreenWantAgentInfo->abilityName); } } -const std::string ReminderRequest::Instance::REMINDER_ID = "reminder_id"; -const std::string ReminderRequest::Instance::PKG_NAME = "package_name"; -const std::string ReminderRequest::Instance::USER_ID = "user_id"; -const std::string ReminderRequest::Instance::UID = "uid"; -const std::string ReminderRequest::Instance::APP_LABEL = "app_label"; -const std::string ReminderRequest::Instance::REMINDER_TYPE = "reminder_type"; -const std::string ReminderRequest::Instance::REMINDER_TIME = "reminder_time"; -const std::string ReminderRequest::Instance::TRIGGER_TIME = "trigger_time"; -const std::string ReminderRequest::Instance::RTC_TRIGGER_TIME = "rtc_trigger_time"; -const std::string ReminderRequest::Instance::TIME_INTERVAL = "time_interval"; -const std::string ReminderRequest::Instance::SNOOZE_TIMES = "snooze_times"; -const std::string ReminderRequest::Instance::DYNAMIC_SNOOZE_TIMES = "dynamic_snooze_times"; -const std::string ReminderRequest::Instance::RING_DURATION = "ring_duration"; -const std::string ReminderRequest::Instance::IS_EXPIRED = "is_expired"; -const std::string ReminderRequest::Instance::IS_ACTIVE = "is_active"; -const std::string ReminderRequest::Instance::STATE = "state"; -const std::string ReminderRequest::Instance::ZONE_ID = "zone_id"; -const std::string ReminderRequest::Instance::HAS_SCHEDULED_TIMEOUT = "has_ScheduledTimeout"; -const std::string ReminderRequest::Instance::ACTION_BUTTON_INFO = "button_info"; -const std::string ReminderRequest::Instance::SLOT_ID = "slot_id"; -const std::string ReminderRequest::Instance::NOTIFICATION_ID = "notification_id"; -const std::string ReminderRequest::Instance::TITLE = "title"; -const std::string ReminderRequest::Instance::CONTENT = "content"; -const std::string ReminderRequest::Instance::SNOOZE_CONTENT = "snooze_content"; -const std::string ReminderRequest::Instance::EXPIRED_CONTENT = "expired_content"; -const std::string ReminderRequest::Instance::AGENT = "agent"; -const std::string ReminderRequest::Instance::MAX_SCREEN_AGENT = "maxScreen_agent"; - -std::string ReminderRequest::Instance::sqlOfAddColumns = ""; -std::vector ReminderRequest::Instance::columns; - -void ReminderRequest::Instance::Init() +const std::string ReminderRequest::REMINDER_ID = "reminder_id"; +const std::string ReminderRequest::PKG_NAME = "package_name"; +const std::string ReminderRequest::USER_ID = "user_id"; +const std::string ReminderRequest::UID = "uid"; +const std::string ReminderRequest::APP_LABEL = "app_label"; +const std::string ReminderRequest::REMINDER_TYPE = "reminder_type"; +const std::string ReminderRequest::REMINDER_TIME = "reminder_time"; +const std::string ReminderRequest::TRIGGER_TIME = "trigger_time"; +const std::string ReminderRequest::RTC_TRIGGER_TIME = "rtc_trigger_time"; +const std::string ReminderRequest::TIME_INTERVAL = "time_interval"; +const std::string ReminderRequest::SNOOZE_TIMES = "snooze_times"; +const std::string ReminderRequest::DYNAMIC_SNOOZE_TIMES = "dynamic_snooze_times"; +const std::string ReminderRequest::RING_DURATION = "ring_duration"; +const std::string ReminderRequest::IS_EXPIRED = "is_expired"; +const std::string ReminderRequest::IS_ACTIVE = "is_active"; +const std::string ReminderRequest::STATE = "state"; +const std::string ReminderRequest::ZONE_ID = "zone_id"; +const std::string ReminderRequest::HAS_SCHEDULED_TIMEOUT = "has_ScheduledTimeout"; +const std::string ReminderRequest::ACTION_BUTTON_INFO = "button_info"; +const std::string ReminderRequest::SLOT_ID = "slot_id"; +const std::string ReminderRequest::NOTIFICATION_ID = "notification_id"; +const std::string ReminderRequest::TITLE = "title"; +const std::string ReminderRequest::CONTENT = "content"; +const std::string ReminderRequest::SNOOZE_CONTENT = "snooze_content"; +const std::string ReminderRequest::EXPIRED_CONTENT = "expired_content"; +const std::string ReminderRequest::AGENT = "agent"; +const std::string ReminderRequest::MAX_SCREEN_AGENT = "maxScreen_agent"; + +std::string ReminderRequest::sqlOfAddColumns = ""; +std::vector ReminderRequest::columns; + +void ReminderRequest::Init() { AddColumn(REMINDER_ID, "INTEGER PRIMARY KEY", false); AddColumn(PKG_NAME, "TEXT NOT NULL", false); @@ -1608,7 +1607,7 @@ void ReminderRequest::Instance::Init() AddColumn(MAX_SCREEN_AGENT, "TEXT", false); } -void ReminderRequest::Instance::AddColumn( +void ReminderRequest::AddColumn( const std::string &name, const std::string &type, const bool &isEnd) { columns.push_back(name); diff --git a/frameworks/ans/native/src/reminder_request_alarm.cpp b/frameworks/ans/native/src/reminder_request_alarm.cpp index 55f5a9275..8a9621478 100644 --- a/frameworks/ans/native/src/reminder_request_alarm.cpp +++ b/frameworks/ans/native/src/reminder_request_alarm.cpp @@ -304,15 +304,15 @@ void ReminderRequestAlarm::RecoverFromDb(const std::shared_ptr(RecoverInt64FromDb(resultSet, Instance::REPEAT_DAYS_OF_WEEK, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, REPEAT_DAYS_OF_WEEK, DbRecoveryType::INT)); // hour hour_ = - static_cast(RecoverInt64FromDb(resultSet, Instance::ALARM_HOUR, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, ALARM_HOUR, DbRecoveryType::INT)); // minute minute_ = - static_cast(RecoverInt64FromDb(resultSet, Instance::ALARM_MINUTE, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, ALARM_MINUTE, DbRecoveryType::INT)); } void ReminderRequestAlarm::AppendValuesBucket(const sptr &reminder, @@ -327,33 +327,30 @@ void ReminderRequestAlarm::AppendValuesBucket(const sptr &remin hour = alarm->GetHour(); minute = alarm->GetMinute(); } - values.PutInt(Instance::REPEAT_DAYS_OF_WEEK, repeatDays); - values.PutInt(Instance::ALARM_HOUR, hour); - values.PutInt(Instance::ALARM_MINUTE, minute); + values.PutInt(REPEAT_DAYS_OF_WEEK, repeatDays); + values.PutInt(ALARM_HOUR, hour); + values.PutInt(ALARM_MINUTE, minute); } -const std::string ReminderRequestAlarm::Instance::REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; -const std::string ReminderRequestAlarm::Instance::ALARM_HOUR = "alarm_hour"; -const std::string ReminderRequestAlarm::Instance::ALARM_MINUTE = "alarm_minute"; +const std::string ReminderRequestAlarm::REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; +const std::string ReminderRequestAlarm::ALARM_HOUR = "alarm_hour"; +const std::string ReminderRequestAlarm::ALARM_MINUTE = "alarm_minute"; -std::string ReminderRequestAlarm::Instance::sqlOfAddColumns = ""; -std::vector ReminderRequestAlarm::Instance::columns; - -void ReminderRequestAlarm::Instance::Init() +void ReminderRequestAlarm::Init() { AddColumn(REPEAT_DAYS_OF_WEEK, "INT", false); AddColumn(ALARM_HOUR, "INT", false); AddColumn(ALARM_MINUTE, "INT", true); } -void ReminderRequestAlarm::Instance::AddColumn( +void ReminderRequestAlarm::AddColumn( const std::string &name, const std::string &type, const bool &isEnd) { - columns.push_back(name); + ReminderRequest::columns.push_back(name); if (!isEnd) { - sqlOfAddColumns += name + " " + type + ", "; + ReminderRequest::sqlOfAddColumns += name + " " + type + ", "; } else { - sqlOfAddColumns += name + " " + type; + ReminderRequest::sqlOfAddColumns += name + " " + type; } } } diff --git a/frameworks/ans/native/src/reminder_request_calendar.cpp b/frameworks/ans/native/src/reminder_request_calendar.cpp index 020ac45ea..a53a5271a 100644 --- a/frameworks/ans/native/src/reminder_request_calendar.cpp +++ b/frameworks/ans/native/src/reminder_request_calendar.cpp @@ -504,38 +504,38 @@ void ReminderRequestCalendar::RecoverFromDb(const std::shared_ptr(RecoverInt64FromDb(resultSet, Instance::REPEAT_DAYS, DbRecoveryType::INT)); + repeatDay_ = static_cast(RecoverInt64FromDb(resultSet, REPEAT_DAYS, DbRecoveryType::INT)); // repeatMonth repeatMonth_ = - static_cast(RecoverInt64FromDb(resultSet, Instance::REPEAT_MONTHS, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, REPEAT_MONTHS, DbRecoveryType::INT)); // firstDesignateYear firstDesignateYear_ = - static_cast(RecoverInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_YEAR, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, FIRST_DESIGNATE_YEAR, DbRecoveryType::INT)); // firstDesignateMonth firstDesignateMonth_ = - static_cast(RecoverInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_MONTH, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, FIRST_DESIGNATE_MONTH, DbRecoveryType::INT)); // firstDesignateDay firstDesignateDay_ = - static_cast(RecoverInt64FromDb(resultSet, Instance::FIRST_DESIGNATE_DAY, DbRecoveryType::INT)); + static_cast(RecoverInt64FromDb(resultSet, FIRST_DESIGNATE_DAY, DbRecoveryType::INT)); // year - year_ = static_cast(RecoverInt64FromDb(resultSet, Instance::CALENDAR_YEAR, DbRecoveryType::INT)); + year_ = static_cast(RecoverInt64FromDb(resultSet, CALENDAR_YEAR, DbRecoveryType::INT)); // month - month_ = static_cast(RecoverInt64FromDb(resultSet, Instance::CALENDAR_MONTH, DbRecoveryType::INT)); + month_ = static_cast(RecoverInt64FromDb(resultSet, CALENDAR_MONTH, DbRecoveryType::INT)); // day - day_ = static_cast(RecoverInt64FromDb(resultSet, Instance::CALENDAR_DAY, DbRecoveryType::INT)); + day_ = static_cast(RecoverInt64FromDb(resultSet, CALENDAR_DAY, DbRecoveryType::INT)); // hour - hour_ = static_cast(RecoverInt64FromDb(resultSet, Instance::CALENDAR_HOUR, DbRecoveryType::INT)); + hour_ = static_cast(RecoverInt64FromDb(resultSet, CALENDAR_HOUR, DbRecoveryType::INT)); // minute - minute_ = static_cast(RecoverInt64FromDb(resultSet, Instance::CALENDAR_MINUTE, DbRecoveryType::INT)); + minute_ = static_cast(RecoverInt64FromDb(resultSet, CALENDAR_MINUTE, DbRecoveryType::INT)); } void ReminderRequestCalendar::AppendValuesBucket(const sptr &reminder, @@ -564,33 +564,30 @@ void ReminderRequestCalendar::AppendValuesBucket(const sptr &re hour = calendar->GetHour(); minute = calendar->GetMinute(); } - values.PutInt(Instance::REPEAT_DAYS, repeatDay); - values.PutInt(Instance::REPEAT_MONTHS, repeatMonth); - values.PutInt(Instance::FIRST_DESIGNATE_YEAR, firstDesignateYear); - values.PutInt(Instance::FIRST_DESIGNATE_MONTH, firstDesignateMonth); - values.PutInt(Instance::FIRST_DESIGNATE_DAY, firstDesignateDay); - values.PutInt(Instance::CALENDAR_YEAR, year); - values.PutInt(Instance::CALENDAR_MONTH, month); - values.PutInt(Instance::CALENDAR_DAY, day); - values.PutInt(Instance::CALENDAR_HOUR, hour); - values.PutInt(Instance::CALENDAR_MINUTE, minute); + values.PutInt(REPEAT_DAYS, repeatDay); + values.PutInt(REPEAT_MONTHS, repeatMonth); + values.PutInt(FIRST_DESIGNATE_YEAR, firstDesignateYear); + values.PutInt(FIRST_DESIGNATE_MONTH, firstDesignateMonth); + values.PutInt(FIRST_DESIGNATE_DAY, firstDesignateDay); + values.PutInt(CALENDAR_YEAR, year); + values.PutInt(CALENDAR_MONTH, month); + values.PutInt(CALENDAR_DAY, day); + values.PutInt(CALENDAR_HOUR, hour); + values.PutInt(CALENDAR_MINUTE, minute); } -const std::string ReminderRequestCalendar::Instance::REPEAT_DAYS = "repeat_days"; -const std::string ReminderRequestCalendar::Instance::REPEAT_MONTHS = "repeat_months"; -const std::string ReminderRequestCalendar::Instance::FIRST_DESIGNATE_YEAR = "first_designate_year"; -const std::string ReminderRequestCalendar::Instance::FIRST_DESIGNATE_MONTH = "first_designate_month"; -const std::string ReminderRequestCalendar::Instance::FIRST_DESIGNATE_DAY = "first_designate_day"; -const std::string ReminderRequestCalendar::Instance::CALENDAR_YEAR = "calendar_year"; -const std::string ReminderRequestCalendar::Instance::CALENDAR_MONTH = "calendar_month"; -const std::string ReminderRequestCalendar::Instance::CALENDAR_DAY = "calendar_day"; -const std::string ReminderRequestCalendar::Instance::CALENDAR_HOUR = "calendar_hour"; -const std::string ReminderRequestCalendar::Instance::CALENDAR_MINUTE = "calendar_minute"; - -std::string ReminderRequestCalendar::Instance::sqlOfAddColumns = ""; -std::vector ReminderRequestCalendar::Instance::columns; - -void ReminderRequestCalendar::Instance::Init() +const std::string ReminderRequestCalendar::REPEAT_DAYS = "repeat_days"; +const std::string ReminderRequestCalendar::REPEAT_MONTHS = "repeat_months"; +const std::string ReminderRequestCalendar::FIRST_DESIGNATE_YEAR = "first_designate_year"; +const std::string ReminderRequestCalendar::FIRST_DESIGNATE_MONTH = "first_designate_month"; +const std::string ReminderRequestCalendar::FIRST_DESIGNATE_DAY = "first_designate_day"; +const std::string ReminderRequestCalendar::CALENDAR_YEAR = "calendar_year"; +const std::string ReminderRequestCalendar::CALENDAR_MONTH = "calendar_month"; +const std::string ReminderRequestCalendar::CALENDAR_DAY = "calendar_day"; +const std::string ReminderRequestCalendar::CALENDAR_HOUR = "calendar_hour"; +const std::string ReminderRequestCalendar::CALENDAR_MINUTE = "calendar_minute"; + +void ReminderRequestCalendar::Init() { AddColumn(REPEAT_DAYS, "INT", false); AddColumn(REPEAT_MONTHS, "INT", false); @@ -604,14 +601,14 @@ void ReminderRequestCalendar::Instance::Init() AddColumn(CALENDAR_MINUTE, "INT", false); } -void ReminderRequestCalendar::Instance::AddColumn( +void ReminderRequestCalendar::AddColumn( const std::string &name, const std::string &type, const bool &isEnd) { - columns.push_back(name); + ReminderRequest::columns.push_back(name); if (!isEnd) { - sqlOfAddColumns += name + " " + type + ", "; + ReminderRequest::sqlOfAddColumns += name + " " + type + ", "; } else { - sqlOfAddColumns += name + " " + type; + ReminderRequest::sqlOfAddColumns += name + " " + type; } } } diff --git a/frameworks/ans/native/src/reminder_store.cpp b/frameworks/ans/native/src/reminder_store.cpp index 77954217b..50442291b 100644 --- a/frameworks/ans/native/src/reminder_store.cpp +++ b/frameworks/ans/native/src/reminder_store.cpp @@ -42,9 +42,7 @@ int32_t ReminderStore::ReminderStoreDataCallBack::OnCreate(NativeRdb::RdbStore & { ANSR_LOGD("Create table."); std::string CREATE_REMINDER_TABLE = "CREATE TABLE IF NOT EXISTS " + REMINDER_DB_TABLE + " (" - + ReminderRequest::Instance::sqlOfAddColumns - + ReminderRequestCalendar::Instance::sqlOfAddColumns - + ReminderRequestAlarm::Instance::sqlOfAddColumns + ")"; + + ReminderRequest::sqlOfAddColumns + ")"; ANSR_LOGD("CreateTable:%{public}s", CREATE_REMINDER_TABLE.c_str()); return store.ExecuteSql(CREATE_REMINDER_TABLE); } @@ -68,15 +66,11 @@ int32_t ReminderStore::Init() } } - ReminderRequest::Instance::Init(); - ReminderRequestCalendar::Instance::Init(); - ReminderRequestAlarm::Instance::Init(); + ReminderRequest::Init(); + ReminderRequestCalendar::Init(); + ReminderRequestAlarm::Init(); ReminderStore::columns.insert(ReminderStore::columns.begin(), - ReminderRequestAlarm::Instance::columns.begin(), ReminderRequestAlarm::Instance::columns.end()); - ReminderStore::columns.insert(ReminderStore::columns.begin(), - ReminderRequestCalendar::Instance::columns.begin(), ReminderRequestCalendar::Instance::columns.end()); - ReminderStore::columns.insert(ReminderStore::columns.begin(), - ReminderRequest::Instance::columns.begin(), ReminderRequest::Instance::columns.end()); + ReminderRequest::columns.begin(), ReminderRequest::columns.end()); std::string dbConfig = ReminderStore::REMINDER_DB_DIR + ReminderStore::REMINDER_DB_NAME; NativeRdb::RdbStoreConfig config_(dbConfig); @@ -92,12 +86,12 @@ int32_t ReminderStore::Init() int32_t ReminderStore::InitData() { ANSR_LOGD("Reminder data init."); - std::string deleteCondition = ReminderRequest::Instance::IS_EXPIRED + " is true"; + std::string deleteCondition = ReminderRequest::IS_EXPIRED + " is true"; ReminderStore::Delete(deleteCondition); int32_t statusChangedRows = STATE_FAIL; NativeRdb::ValuesBucket statusValues; - statusValues.PutInt(ReminderRequest::Instance::STATE, ReminderRequest::REMINDER_STATUS_INACTIVE); + statusValues.PutInt(ReminderRequest::STATE, ReminderRequest::REMINDER_STATUS_INACTIVE); int32_t statusResult = rdbStore_->Update(statusChangedRows, REMINDER_DB_TABLE, statusValues); ANSR_LOGD("Change status to inactive, changed rows: %{public}d.", statusChangedRows); if (statusResult != NativeRdb::E_OK) { @@ -107,8 +101,8 @@ int32_t ReminderStore::InitData() int32_t activeChangedRows = STATE_FAIL; NativeRdb::ValuesBucket activeValues; - activeValues.PutString(ReminderRequest::Instance::IS_ACTIVE, "false"); - std::string activeUpdateCondition = ReminderRequest::Instance::IS_ACTIVE + " is true"; + activeValues.PutString(ReminderRequest::IS_ACTIVE, "false"); + std::string activeUpdateCondition = ReminderRequest::IS_ACTIVE + " is true"; std::vector activeWhereArgs; int32_t activeResult = rdbStore_->Update( activeChangedRows, REMINDER_DB_TABLE, activeValues, activeUpdateCondition, activeWhereArgs); @@ -120,8 +114,8 @@ int32_t ReminderStore::InitData() int32_t scheduledChangedRows = STATE_FAIL; NativeRdb::ValuesBucket scheduledValues; - scheduledValues.PutString(ReminderRequest::Instance::HAS_SCHEDULED_TIMEOUT, "false"); - std::string scheduledUpdateCondition = ReminderRequest::Instance::HAS_SCHEDULED_TIMEOUT + " is true"; + scheduledValues.PutString(ReminderRequest::HAS_SCHEDULED_TIMEOUT, "false"); + std::string scheduledUpdateCondition = ReminderRequest::HAS_SCHEDULED_TIMEOUT + " is true"; std::vector scheduledWhereArgs; int32_t scheduledResult = rdbStore_->Update( scheduledChangedRows, REMINDER_DB_TABLE, scheduledValues, scheduledUpdateCondition, scheduledWhereArgs); @@ -135,21 +129,21 @@ int32_t ReminderStore::InitData() int32_t ReminderStore::Delete(int32_t reminderId) { - std::string deleteCondition = ReminderRequest::Instance::REMINDER_ID + std::string deleteCondition = ReminderRequest::REMINDER_ID + " = " + std::to_string(reminderId); return ReminderStore::Delete(deleteCondition); } int32_t ReminderStore::DeleteUser(int32_t userId) { - std::string deleteCondition = ReminderRequest::Instance::USER_ID + " = " + std::to_string(userId); + std::string deleteCondition = ReminderRequest::USER_ID + " = " + std::to_string(userId); return ReminderStore::Delete(deleteCondition); } int32_t ReminderStore::Delete(const std::string &pkg, int32_t userId) { - std::string deleteCondition = ReminderRequest::Instance::PKG_NAME + " = " + pkg + " and " - + ReminderRequest::Instance::USER_ID + " = " + std::to_string(userId); + std::string deleteCondition = ReminderRequest::PKG_NAME + " = " + pkg + " and " + + ReminderRequest::USER_ID + " = " + std::to_string(userId); return ReminderStore::Delete(deleteCondition); } @@ -183,6 +177,10 @@ int64_t ReminderStore::UpdateOrInsert( ANSR_LOGE("Rdb store is not initialized."); return false; } + if (bundleOption == nullptr) { + ANSR_LOGE("BundleOption is null."); + return STATE_FAIL; + } if (IsReminderExist(reminder)) { isSuccess = Update(reminder, bundleOption); } else { @@ -194,10 +192,6 @@ int64_t ReminderStore::UpdateOrInsert( int64_t ReminderStore::Insert( const sptr &reminder, const sptr &bundleOption) { - if (bundleOption == nullptr) { - ANSR_LOGE("BundleOption is null."); - return STATE_FAIL; - } int64_t rowId = STATE_FAIL; NativeRdb::ValuesBucket values; ReminderStore::GenerateData(reminder, bundleOption, values); @@ -214,14 +208,10 @@ int64_t ReminderStore::Insert( int64_t ReminderStore::Update( const sptr &reminder, const sptr &bundleOption) { - if (bundleOption == nullptr) { - ANSR_LOGE("BundleOption is null."); - return STATE_FAIL; - } int32_t changedRows = STATE_FAIL; NativeRdb::ValuesBucket values; ReminderStore::GenerateData(reminder, bundleOption, values); - std::string updateCondition = ReminderRequest::Instance::REMINDER_ID + std::string updateCondition = ReminderRequest::REMINDER_ID + " = " + std::to_string(reminder->GetReminderId()); std::vector whereArgs; int32_t result = rdbStore_->Update(changedRows, REMINDER_DB_TABLE, values, updateCondition, whereArgs); @@ -237,9 +227,9 @@ int64_t ReminderStore::Update( bool ReminderStore::IsReminderExist(const sptr &reminder) { - std::string queryCondition = "select " + ReminderRequest::Instance::REMINDER_ID + std::string queryCondition = "select " + ReminderRequest::REMINDER_ID + " from " + REMINDER_DB_TABLE + " where " - + ReminderRequest::Instance::REMINDER_ID + " = " + std::to_string(reminder->GetReminderId()); + + ReminderRequest::REMINDER_ID + " = " + std::to_string(reminder->GetReminderId()); std::vector whereArgs; std::unique_ptr queryResultSet = rdbStore_->QuerySql(queryCondition, whereArgs); if (queryResultSet == nullptr) { @@ -295,9 +285,9 @@ int32_t ReminderStore::GetMaxId() ANSR_LOGE("Rdb store is not initialized."); return STATE_FAIL; } - std::string queryCondition = "select " + ReminderRequest::Instance::REMINDER_ID + std::string queryCondition = "select " + ReminderRequest::REMINDER_ID + " from " + REMINDER_DB_TABLE + " order by " - + ReminderRequest::Instance::REMINDER_ID + " desc"; + + ReminderRequest::REMINDER_ID + " desc"; std::shared_ptr queryResultSet = ReminderStore::Query(queryCondition); if (queryResultSet == nullptr) { ANSR_LOGE("QueryResultSet is null."); @@ -322,8 +312,8 @@ int32_t ReminderStore::GetMaxId() std::vector> ReminderStore::GetAllValidReminders() { std::string queryCondition = "select * from " + REMINDER_DB_TABLE + " where " - + ReminderRequest::Instance::IS_EXPIRED + " is false order by " - + ReminderRequest::Instance::TRIGGER_TIME + " asc"; + + ReminderRequest::IS_EXPIRED + " is false order by " + + ReminderRequest::TRIGGER_TIME + " asc"; ANSR_LOGD("Get all reminders"); return GetReminders(queryCondition); } @@ -356,8 +346,8 @@ sptr ReminderStore::BuildReminder(const std::shared_ptrGetInt(ReminderStore::GetColumnIndex(ReminderRequest::Instance::REMINDER_TYPE), reminderType); - resultSet->GetInt(ReminderStore::GetColumnIndex(ReminderRequest::Instance::REMINDER_ID), reminderId); + resultSet->GetInt(ReminderStore::GetColumnIndex(ReminderRequest::REMINDER_TYPE), reminderType); + resultSet->GetInt(ReminderStore::GetColumnIndex(ReminderRequest::REMINDER_ID), reminderId); sptr reminder = nullptr; switch (reminderType) { @@ -388,9 +378,9 @@ sptr ReminderStore::BuildReminder(const std::shared_ptr &bundleOption) const { - std::string queryCondition = "select " + ReminderRequest::Instance::PKG_NAME + ", " - + ReminderRequest::Instance::UID + " from " + REMINDER_DB_TABLE + " where " - + ReminderRequest::Instance::REMINDER_ID + "=" + std::to_string(reminderId); + std::string queryCondition = "select " + ReminderRequest::PKG_NAME + ", " + + ReminderRequest::UID + " from " + REMINDER_DB_TABLE + " where " + + ReminderRequest::REMINDER_ID + "=" + std::to_string(reminderId); std::shared_ptr queryResultSet = Query(queryCondition); if (queryResultSet == nullptr) { return false; @@ -402,9 +392,9 @@ bool ReminderStore::GetBundleOption(const int32_t &reminderId, sptrGoToNextRow(); std::string pkgName; - GetStringVal(queryResultSet, ReminderRequest::Instance::PKG_NAME, pkgName); + GetStringVal(queryResultSet, ReminderRequest::PKG_NAME, pkgName); int32_t uid; - GetInt32Val(queryResultSet, ReminderRequest::Instance::UID, uid); + GetInt32Val(queryResultSet, ReminderRequest::UID, uid); bundleOption->SetBundleName(pkgName); bundleOption->SetUid(uid); return true; diff --git a/interfaces/innerkits/ans/native/include/reminder_request.h b/interfaces/innerkits/ans/native/include/reminder_request.h index 3852bafb0..c105a0692 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request.h +++ b/interfaces/innerkits/ans/native/include/reminder_request.h @@ -606,43 +606,37 @@ public: static const uint8_t REMINDER_STATUS_SHOWING; static const uint8_t REMINDER_STATUS_SNOOZE; -class Instance { -public: - const static std::string REMINDER_ID; - const static std::string PKG_NAME; - const static std::string USER_ID; - const static std::string UID; - const static std::string APP_LABEL; - const static std::string REMINDER_TYPE; - const static std::string REMINDER_TIME; - const static std::string TRIGGER_TIME; - const static std::string RTC_TRIGGER_TIME; - const static std::string TIME_INTERVAL; - const static std::string SNOOZE_TIMES; - const static std::string DYNAMIC_SNOOZE_TIMES; - const static std::string RING_DURATION; - const static std::string IS_EXPIRED; - const static std::string IS_ACTIVE; - const static std::string STATE; - const static std::string ZONE_ID; - const static std::string HAS_SCHEDULED_TIMEOUT; - const static std::string ACTION_BUTTON_INFO; - const static std::string SLOT_ID; - const static std::string NOTIFICATION_ID; - const static std::string TITLE; - const static std::string CONTENT; - const static std::string SNOOZE_CONTENT; - const static std::string EXPIRED_CONTENT; - const static std::string AGENT; - const static std::string MAX_SCREEN_AGENT; - + // For database recovery. + static void Init(); + static const std::string REMINDER_ID; + static const std::string PKG_NAME; + static const std::string USER_ID; + static const std::string UID; + static const std::string APP_LABEL; + static const std::string REMINDER_TYPE; + static const std::string REMINDER_TIME; + static const std::string TRIGGER_TIME; + static const std::string RTC_TRIGGER_TIME; + static const std::string TIME_INTERVAL; + static const std::string SNOOZE_TIMES; + static const std::string DYNAMIC_SNOOZE_TIMES; + static const std::string RING_DURATION; + static const std::string IS_EXPIRED; + static const std::string IS_ACTIVE; + static const std::string STATE; + static const std::string ZONE_ID; + static const std::string HAS_SCHEDULED_TIMEOUT; + static const std::string ACTION_BUTTON_INFO; + static const std::string SLOT_ID; + static const std::string NOTIFICATION_ID; + static const std::string TITLE; + static const std::string CONTENT; + static const std::string SNOOZE_CONTENT; + static const std::string EXPIRED_CONTENT; + static const std::string AGENT; + static const std::string MAX_SCREEN_AGENT; static std::string sqlOfAddColumns; static std::vector columns; - static void Init(); - -private: - static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); -}; protected: enum class DbRecoveryType : uint8_t { @@ -703,6 +697,15 @@ private: */ void UpdateNotificationStateForSnooze(); + /** + * @brief Add column to create table of database. + * + * @param name Indicates the column name. + * @param type Indicates the type of the column. + * @param isEnd Indicates whether it is the last column. + */ + static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); + static const uint32_t MIN_TIME_INTERVAL_IN_MILLI; static const std::string SEP_BUTTON_SINGLE; static const std::string SEP_BUTTON_MULTI; diff --git a/interfaces/innerkits/ans/native/include/reminder_request_alarm.h b/interfaces/innerkits/ans/native/include/reminder_request_alarm.h index 5277bb4a6..38d0e531c 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_alarm.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_alarm.h @@ -108,20 +108,9 @@ public: static void AppendValuesBucket(const sptr &reminder, const sptr &bundleOption, NativeRdb::ValuesBucket &values); -class Instance { -public: - const static std::string REPEAT_DAYS_OF_WEEK; - const static std::string ALARM_HOUR; - const static std::string ALARM_MINUTE; - - static std::string sqlOfAddColumns; - static std::vector columns; + // For database recovery. static void Init(); -private: - static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); -}; - protected: virtual uint64_t PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const override; @@ -173,6 +162,12 @@ private: uint8_t hour_ = {0}; uint8_t minute_ = {0}; uint8_t repeatDays_ = {0}; + + // For database recovery. + static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); + static const std::string REPEAT_DAYS_OF_WEEK; + static const std::string ALARM_HOUR; + static const std::string ALARM_MINUTE; }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h index 0e4576883..7277e825d 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h @@ -146,27 +146,9 @@ public: const sptr &bundleOption, NativeRdb::ValuesBucket &values); static uint8_t GetDaysOfMonth(const uint16_t &year, const uint8_t &month); -class Instance { -public: - const static std::string REPEAT_DAYS; - const static std::string REPEAT_MONTHS; - const static std::string FIRST_DESIGNATE_YEAR; - const static std::string FIRST_DESIGNATE_MONTH; - const static std::string FIRST_DESIGNATE_DAY; - const static std::string CALENDAR_YEAR; - const static std::string CALENDAR_MONTH; - const static std::string CALENDAR_DAY; - const static std::string CALENDAR_HOUR; - const static std::string CALENDAR_MINUTE; - - static std::string sqlOfAddColumns; - static std::vector columns; + // For database recovery. static void Init(); -private: - static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); -}; - protected: virtual uint64_t PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const override; @@ -216,6 +198,20 @@ private: uint8_t second_ {0}; uint16_t repeatMonth_ {0}; uint32_t repeatDay_ {0}; + + // For database recovery. + static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); + + static const std::string REPEAT_DAYS; + static const std::string REPEAT_MONTHS; + static const std::string FIRST_DESIGNATE_YEAR; + static const std::string FIRST_DESIGNATE_MONTH; + static const std::string FIRST_DESIGNATE_DAY; + static const std::string CALENDAR_YEAR; + static const std::string CALENDAR_MONTH; + static const std::string CALENDAR_DAY; + static const std::string CALENDAR_HOUR; + static const std::string CALENDAR_MINUTE; }; } } diff --git a/interfaces/innerkits/ans/native/include/reminder_store.h b/interfaces/innerkits/ans/native/include/reminder_store.h index ca6dca05d..62585c0e1 100644 --- a/interfaces/innerkits/ans/native/include/reminder_store.h +++ b/interfaces/innerkits/ans/native/include/reminder_store.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at -- Gitee From bf533395128c73e8a71ca2b5e24687efffbe41f7 Mon Sep 17 00:00:00 2001 From: linyunxuan Date: Thu, 3 Mar 2022 21:36:26 +0800 Subject: [PATCH 7/8] add database feature Signed-off-by: linyunxuan --- .../ans/native/src/reminder_request.cpp | 4 +++ .../ans/native/src/reminder_request_alarm.cpp | 17 ++-------- .../native/src/reminder_request_calendar.cpp | 31 ++++++------------- frameworks/ans/native/src/reminder_store.cpp | 16 ++-------- .../ans/native/include/reminder_request.h | 21 +++++++------ .../native/include/reminder_request_alarm.h | 3 +- .../include/reminder_request_calendar.h | 4 +-- .../native/include/reminder_request_timer.h | 4 +-- .../ans/native/include/reminder_store.h | 1 - 9 files changed, 36 insertions(+), 65 deletions(-) diff --git a/frameworks/ans/native/src/reminder_request.cpp b/frameworks/ans/native/src/reminder_request.cpp index c2c128691..5adbeee6c 100644 --- a/frameworks/ans/native/src/reminder_request.cpp +++ b/frameworks/ans/native/src/reminder_request.cpp @@ -401,6 +401,10 @@ int64_t ReminderRequest::RecoverInt64FromDb(const std::shared_ptrGetLong(ReminderStore::GetColumnIndex(columnName), value); return value; } + default: { + ANSR_LOGD("ColumnType not support."); + break; + } } ANSR_LOGE("Recover data error"); return 0; diff --git a/frameworks/ans/native/src/reminder_request_alarm.cpp b/frameworks/ans/native/src/reminder_request_alarm.cpp index 8a9621478..532202d0c 100644 --- a/frameworks/ans/native/src/reminder_request_alarm.cpp +++ b/frameworks/ans/native/src/reminder_request_alarm.cpp @@ -338,20 +338,9 @@ const std::string ReminderRequestAlarm::ALARM_MINUTE = "alarm_minute"; void ReminderRequestAlarm::Init() { - AddColumn(REPEAT_DAYS_OF_WEEK, "INT", false); - AddColumn(ALARM_HOUR, "INT", false); - AddColumn(ALARM_MINUTE, "INT", true); -} - -void ReminderRequestAlarm::AddColumn( - const std::string &name, const std::string &type, const bool &isEnd) -{ - ReminderRequest::columns.push_back(name); - if (!isEnd) { - ReminderRequest::sqlOfAddColumns += name + " " + type + ", "; - } else { - ReminderRequest::sqlOfAddColumns += name + " " + type; - } + ReminderRequest::AddColumn(REPEAT_DAYS_OF_WEEK, "INT", false); + ReminderRequest::AddColumn(ALARM_HOUR, "INT", false); + ReminderRequest::AddColumn(ALARM_MINUTE, "INT", true); } } } \ No newline at end of file diff --git a/frameworks/ans/native/src/reminder_request_calendar.cpp b/frameworks/ans/native/src/reminder_request_calendar.cpp index a53a5271a..746c2adcd 100644 --- a/frameworks/ans/native/src/reminder_request_calendar.cpp +++ b/frameworks/ans/native/src/reminder_request_calendar.cpp @@ -589,27 +589,16 @@ const std::string ReminderRequestCalendar::CALENDAR_MINUTE = "calendar_minute"; void ReminderRequestCalendar::Init() { - AddColumn(REPEAT_DAYS, "INT", false); - AddColumn(REPEAT_MONTHS, "INT", false); - AddColumn(FIRST_DESIGNATE_YEAR, "INT", false); - AddColumn(FIRST_DESIGNATE_MONTH, "INT", false); - AddColumn(FIRST_DESIGNATE_DAY, "INT", false); - AddColumn(CALENDAR_YEAR, "INT", false); - AddColumn(CALENDAR_MONTH, "INT", false); - AddColumn(CALENDAR_DAY, "INT", false); - AddColumn(CALENDAR_HOUR, "INT", false); - AddColumn(CALENDAR_MINUTE, "INT", false); -} - -void ReminderRequestCalendar::AddColumn( - const std::string &name, const std::string &type, const bool &isEnd) -{ - ReminderRequest::columns.push_back(name); - if (!isEnd) { - ReminderRequest::sqlOfAddColumns += name + " " + type + ", "; - } else { - ReminderRequest::sqlOfAddColumns += name + " " + type; - } + ReminderRequest::AddColumn(REPEAT_DAYS, "INT", false); + ReminderRequest::AddColumn(REPEAT_MONTHS, "INT", false); + ReminderRequest::AddColumn(FIRST_DESIGNATE_YEAR, "INT", false); + ReminderRequest::AddColumn(FIRST_DESIGNATE_MONTH, "INT", false); + ReminderRequest::AddColumn(FIRST_DESIGNATE_DAY, "INT", false); + ReminderRequest::AddColumn(CALENDAR_YEAR, "INT", false); + ReminderRequest::AddColumn(CALENDAR_MONTH, "INT", false); + ReminderRequest::AddColumn(CALENDAR_DAY, "INT", false); + ReminderRequest::AddColumn(CALENDAR_HOUR, "INT", false); + ReminderRequest::AddColumn(CALENDAR_MINUTE, "INT", false); } } } \ No newline at end of file diff --git a/frameworks/ans/native/src/reminder_store.cpp b/frameworks/ans/native/src/reminder_store.cpp index 50442291b..b63bcaea3 100644 --- a/frameworks/ans/native/src/reminder_store.cpp +++ b/frameworks/ans/native/src/reminder_store.cpp @@ -175,11 +175,11 @@ int64_t ReminderStore::UpdateOrInsert( int64_t isSuccess = STATE_FAIL; if (rdbStore_ == nullptr) { ANSR_LOGE("Rdb store is not initialized."); - return false; + return isSuccess; } if (bundleOption == nullptr) { ANSR_LOGE("BundleOption is null."); - return STATE_FAIL; + return isSuccess; } if (IsReminderExist(reminder)) { isSuccess = Update(reminder, bundleOption); @@ -256,17 +256,6 @@ std::shared_ptr ReminderStore::Query(const std::s return queryResultSet; } -std::vector ReminderStore::GetRepeatInfo(int64_t repeatData, int32_t maxRepeatVal) -{ - std::vector repeatInfo; - for (int i = 1; i <= maxRepeatVal; i++) { - if ((repeatData & (1 << (i - 1))) > 0) { - repeatInfo.push_back(i); - } - } - return repeatInfo; -} - uint8_t ReminderStore::GetColumnIndex(const std::string& name) { uint8_t index = 0; @@ -365,6 +354,7 @@ sptr ReminderStore::BuildReminder(const std::shared_ptr &resultSet, const std::string &columnName, const DbRecoveryType &columnType); + + /** + * @brief For database recovery. + * + * Add column to create table of database. + * + * @param name Indicates the column name. + * @param type Indicates the type of the column. + * @param isEnd Indicates whether it is the last column. + */ + static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); + static const int BASE_YEAR; private: @@ -697,15 +709,6 @@ private: */ void UpdateNotificationStateForSnooze(); - /** - * @brief Add column to create table of database. - * - * @param name Indicates the column name. - * @param type Indicates the type of the column. - * @param isEnd Indicates whether it is the last column. - */ - static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); - static const uint32_t MIN_TIME_INTERVAL_IN_MILLI; static const std::string SEP_BUTTON_SINGLE; static const std::string SEP_BUTTON_MULTI; diff --git a/interfaces/innerkits/ans/native/include/reminder_request_alarm.h b/interfaces/innerkits/ans/native/include/reminder_request_alarm.h index 38d0e531c..b0f562507 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_alarm.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_alarm.h @@ -46,7 +46,7 @@ public: * * @param reminderId Indicates reminder id. */ - ReminderRequestAlarm(int32_t reminderId) : ReminderRequest(reminderId) {}; + explicit ReminderRequestAlarm(int32_t reminderId) : ReminderRequest(reminderId) {}; /** * @brief Copy construct from an exist reminder. @@ -164,7 +164,6 @@ private: uint8_t repeatDays_ = {0}; // For database recovery. - static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); static const std::string REPEAT_DAYS_OF_WEEK; static const std::string ALARM_HOUR; static const std::string ALARM_MINUTE; diff --git a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h index 7277e825d..9d022dc7d 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h @@ -58,7 +58,7 @@ public: * * @param reminderId Indicates reminder id. */ - ReminderRequestCalendar(int32_t reminderId) : ReminderRequest(reminderId) {}; + explicit ReminderRequestCalendar(int32_t reminderId) : ReminderRequest(reminderId) {}; explicit ReminderRequestCalendar(const ReminderRequestCalendar &other); ReminderRequestCalendar& operator = (const ReminderRequestCalendar &other); @@ -200,8 +200,6 @@ private: uint32_t repeatDay_ {0}; // For database recovery. - static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); - static const std::string REPEAT_DAYS; static const std::string REPEAT_MONTHS; static const std::string FIRST_DESIGNATE_YEAR; diff --git a/interfaces/innerkits/ans/native/include/reminder_request_timer.h b/interfaces/innerkits/ans/native/include/reminder_request_timer.h index 46ef053f2..8b9aae986 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_timer.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_timer.h @@ -31,7 +31,7 @@ public: * * @param countDownTimeInSeconds Indicates the duration after which this timer reminder will be triggered. */ - ReminderRequestTimer(uint64_t countDownTimeInSeconds); + explicit ReminderRequestTimer(uint64_t countDownTimeInSeconds); /** * @brief This constructor should only be used in background proxy service process @@ -39,7 +39,7 @@ public: * * @param reminderId Indicates reminder id. */ - ReminderRequestTimer(int32_t reminderId) : ReminderRequest(reminderId) {}; + explicit ReminderRequestTimer(int32_t reminderId) : ReminderRequest(reminderId) {}; /** * @brief Copy construct from an exist reminder. diff --git a/interfaces/innerkits/ans/native/include/reminder_store.h b/interfaces/innerkits/ans/native/include/reminder_store.h index 62585c0e1..8e49fded8 100644 --- a/interfaces/innerkits/ans/native/include/reminder_store.h +++ b/interfaces/innerkits/ans/native/include/reminder_store.h @@ -39,7 +39,6 @@ public: bool GetBundleOption(const int32_t &reminderId, sptr &bundleOption) const; int32_t GetMaxId(); int64_t UpdateOrInsert(const sptr &reminder, const sptr &bundleOption); - static std::vector GetRepeatInfo(int64_t repeatData, int32_t maxRepeatVal); static uint8_t GetColumnIndex(const std::string& name); static std::vector columns; -- Gitee From af53f4b28e2339089abb995cfebc7080e284dee4 Mon Sep 17 00:00:00 2001 From: linyunxuan Date: Thu, 3 Mar 2022 21:39:01 +0800 Subject: [PATCH 8/8] add database feature Signed-off-by: linyunxuan --- services/ans/src/reminder_data_manager.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index 18e18bfed..238814284 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -457,11 +457,6 @@ void ReminderDataManager::UpdateAndSaveReminderLocked( void ReminderDataManager::SetService(AdvancedNotificationService *advancedNotificationService) { advancedNotificationService_ = advancedNotificationService; - if (advancedNotificationService_ == nullptr) { - ANSR_LOGD("~~~~advancedNotificationService_ is null"); - } else { - ANSR_LOGD("~~~~advancedNotificationService_ is not null"); - } } void ReminderDataManager::ShowActiveReminder() -- Gitee