diff --git a/frameworks/ans/core/BUILD.gn b/frameworks/ans/core/BUILD.gn
index fdca50b6dcef7a63f0c9e2ecbc7ee8d1b73bbace..3294e9ac236b6fe951f437cb6339716a77500363 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 a0aa9d6ccf3c7a5039278afb806ca60395040791..74f56a311ab614192bc646600f61b1460c80fd17 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 8c71b7f82560abea9e70fe49f1820d167f11aa48..addf79e15b68c6c15918b1a5b13f5ce18a26c465 100644
--- a/frameworks/ans/native/src/reminder_request.cpp
+++ b/frameworks/ans/native/src/reminder_request.cpp
@@ -22,6 +22,7 @@
#include "want_agent_helper.h"
#include "reminder_request.h"
+#include "reminder_store.h"
namespace OHOS {
namespace Notification {
@@ -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,10 @@ 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 +382,163 @@ 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_);
+
+ // 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));
+ ANSR_LOGD("~~~~triggerTimeInMilli_=%{public}llu", triggerTimeInMilli_);
+
+ // 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 +625,11 @@ bool ReminderRequest::ShouldShowImmediately() const
return true;
}
+uint8_t ReminderRequest::GetConstStateInactive()
+{
+ return REMINDER_STATUS_INACTIVE;
+}
+
std::map ReminderRequest::GetActionButtons(
) const
{
@@ -560,6 +732,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_;
@@ -866,10 +1048,16 @@ bool ReminderRequest::InitNotificationRequest()
return false;
}
displayContent_ = content_;
- AddActionButtons(true);
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 +1080,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;
@@ -1153,6 +1356,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 +1451,124 @@ 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
+ 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
+ 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
+ values.PutInt(Instance::STATE, reminder->GetState());
+ values.PutString(Instance::ZONE_ID, ""); // no use
+ values.PutString(Instance::HAS_SCHEDULED_TIMEOUT, ""); // no use
+ 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;
+ }
+}
}
}
diff --git a/frameworks/ans/native/src/reminder_request_alarm.cpp b/frameworks/ans/native/src/reminder_request_alarm.cpp
index f5a549729d310340397863571d3a25fe0fd3fc29..da672199abd9c731d194e29518bbc2c0fe899633 100644
--- a/frameworks/ans/native/src/reminder_request_alarm.cpp
+++ b/frameworks/ans/native/src/reminder_request_alarm.cpp
@@ -16,6 +16,7 @@
#include "ans_log_wrapper.h"
#include "reminder_request_alarm.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 0809af1294726c2a50f9e02e688d28fdf0fc4970..7a715ee44882ffb3b4e16e36909e748d0de64857 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/src/reminder_store.cpp b/frameworks/ans/native/src/reminder_store.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..0fc67557d212a072d5c363e1a4b0ad9b9eabb21a
--- /dev/null
+++ b/frameworks/ans/native/src/reminder_store.cpp
@@ -0,0 +1,453 @@
+/*
+ * 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.
+ */
+
+#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"
+
+#include "reminder_store.h"
+
+namespace OHOS {
+namespace Notification {
+const std::string ReminderStore::REMINDER_DB_DIR = "/data/user/0/reminder/"; // todo replace path
+const std::string ReminderStore::REMINDER_DB_NAME = "reminder.db";
+const uint32_t ReminderStore::REMINDER_RDB_VERSION = 1;
+std::vector ReminderStore::COLUMNS;
+
+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;
+const std::string ReminderStore::REMINDER_DB_TABLE = "reminder";
+
+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.");
+
+ /* std::filesystem::path databaseDir(REMINDER_DB_DIR);
+ ANSR_LOGD("ReminderStore::%{public}s databaseDir.", __func__);
+ if (!std::filesystem::exists(databaseDir)) {
+ bool createDir = std::filesystem::create_directories(REMINDER_DB_DIR);
+ ANSR_LOGD("ReminderStore::%{public}s createDir.", __func__);
+ if (!createDir) {
+ ANSR_LOGE("ReminderStore::%{public}s failed to create directory %{public}s", __func__, REMINDER_DB_DIR.c_str());
+ }
+ } */
+
+ 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());
+
+ int32_t errCode(STATE_FAIL);
+ 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;
+ }
+
+ // std::string databaseDir = AppExecFwk::AbilityContext::GetDatabaseDir() + "/" + REMINDER_DB_DIR;
+ // ANSR_LOGD("~~~~databaseDir=%{public}s", databaseDir);
+
+ return ReminderStore::InitData();
+}
+
+int32_t ReminderStore::InitData()
+{
+ ANSR_LOGD("Reminder data init.");
+
+ struct timeb now;
+ ftime(&now);
+ time_t nowTime = now.time;
+ int64_t overDueRtcTime = (static_cast(nowTime) - TIME_INTERVAL_FOR_DELETE) * MILLI_SECONDS;
+ ANSR_LOGD("OverDueRtcTime: %{public}lld.", overDueRtcTime);
+ ReminderStore::DeleteRtcTime(overDueRtcTime); // todo check(cannot delete if it is a repeat reminder)
+
+ 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::DeleteRtcTime(int64_t rtcTime)
+{
+ std::string deleteCondition = ReminderRequest::Instance::RTC_TRIGGER_TIME
+ + " < " + std::to_string(rtcTime); // todo
+ 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 * from " + REMINDER_DB_TABLE + " order by "
+ + ReminderRequest::Instance::REMINDER_ID + " desc"; // todo *
+ 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";
+ 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;
+ }
+ ANSR_LOGD("~~~~1");
+ 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); // todo check outOf size
+}
+
+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/frameworks/ans/native/test/unittest/BUILD.gn b/frameworks/ans/native/test/unittest/BUILD.gn
index c0eedd516b7d36560a3a35c0d866c4a358597716..15eecb3a8ab8fd614c0ab80b6dd06e6e77afc4d5 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 14fb032d6ff2f0af0445dfee2cc70254c92418b0..9c1e935ac5b53635e4806e627fb04d15406174ff 100644
--- a/interfaces/innerkits/ans/native/include/reminder_request.h
+++ b/interfaces/innerkits/ans/native/include/reminder_request.h
@@ -19,8 +19,11 @@
#include