diff --git a/frameworks/ans/core/BUILD.gn b/frameworks/ans/core/BUILD.gn
index fdca50b6dcef7a63f0c9e2ecbc7ee8d1b73bbace..e18fab85957bc58796c6fb0138f77ffc438ac7dc 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
@@ -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..9ef3af8b6fc682952256bd4ccf47871d7103fd0d 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
@@ -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 7bc0780e09fd8d601d1efb411d4d929d723d8e5f..5adbeee6ce012ad980c68c0de62d65253e8be3f1 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,192 @@ bool ReminderRequest::OnTimeZoneChange()
triggerTimeInMilli_, GetDurationSinceEpochInMilli(newZoneTriggerTime), nextTriggerTime);
}
+int64_t ReminderRequest::RecoverInt64FromDb(const std::shared_ptr &resultSet,
+ const std::string &columnName, const DbRecoveryType &columnType)
+{
+ if (resultSet == nullptr) {
+ ANSR_LOGE("ResultSet is null");
+ return 0;
+ }
+ 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;
+ }
+ default: {
+ ANSR_LOGD("ColumnType not support.");
+ break;
+ }
+ }
+ ANSR_LOGE("Recover data error");
+ return 0;
+}
+
+void ReminderRequest::RecoverFromDb(const std::shared_ptr &resultSet)
+{
+ if (resultSet == nullptr) {
+ ANSR_LOGE("ResultSet is null");
+ return;
+ }
+
+ // reminderId
+ resultSet->GetInt(ReminderStore::GetColumnIndex(REMINDER_ID), reminderId_);
+
+ // userId
+ resultSet->GetInt(ReminderStore::GetColumnIndex(USER_ID), userId_);
+
+ // bundleName
+ resultSet->GetString(ReminderStore::GetColumnIndex(PKG_NAME), bundleName_);
+
+ // uid
+ resultSet->GetInt(ReminderStore::GetColumnIndex(UID), uid_);
+
+ // reminderType
+ int reminderType;
+ resultSet->GetInt(ReminderStore::GetColumnIndex(REMINDER_TYPE), reminderType);
+ reminderType_ = ReminderType(reminderType);
+
+ // reminderTime
+ reminderTimeInMilli_ =
+ static_cast(RecoverInt64FromDb(resultSet, REMINDER_TIME, DbRecoveryType::LONG));
+
+ // triggerTime
+ triggerTimeInMilli_ =
+ static_cast(RecoverInt64FromDb(resultSet, TRIGGER_TIME, DbRecoveryType::LONG));
+
+ // timeInterval
+ uint64_t timeIntervalInSecond =
+ static_cast(RecoverInt64FromDb(resultSet, TIME_INTERVAL, DbRecoveryType::LONG));
+ SetTimeInterval(timeIntervalInSecond);
+
+ // snoozeTimes
+ snoozeTimes_ = static_cast(RecoverInt64FromDb(resultSet, SNOOZE_TIMES, DbRecoveryType::INT));
+
+ // dynamicSnoozeTimes
+ snoozeTimesDynamic_ =
+ static_cast(RecoverInt64FromDb(resultSet, DYNAMIC_SNOOZE_TIMES, DbRecoveryType::INT));
+
+ // ringDuration
+ uint64_t ringDurationInSecond =
+ static_cast(RecoverInt64FromDb(resultSet, RING_DURATION, DbRecoveryType::LONG));
+ SetRingDuration(ringDurationInSecond);
+
+ // isExpired
+ std::string isExpired;
+ resultSet->GetString(ReminderStore::GetColumnIndex(IS_EXPIRED), isExpired);
+ isExpired_ = isExpired == "true" ? true : false;
+
+ // state
+ state_ = static_cast(RecoverInt64FromDb(resultSet, STATE, DbRecoveryType::INT));
+
+ // action buttons
+ RecoverActionButton(resultSet);
+
+ // slotType
+ int slotType;
+ resultSet->GetInt(ReminderStore::GetColumnIndex(SLOT_ID), slotType);
+ slotType_ = NotificationConstant::SlotType(slotType);
+
+ // notification id
+ resultSet->GetInt(ReminderStore::GetColumnIndex(NOTIFICATION_ID), notificationId_);
+
+ // title
+ resultSet->GetString(ReminderStore::GetColumnIndex(TITLE), title_);
+
+ // content
+ resultSet->GetString(ReminderStore::GetColumnIndex(CONTENT), content_);
+
+ // snoozeContent
+ resultSet->GetString(ReminderStore::GetColumnIndex(SNOOZE_CONTENT), snoozeContent_);
+
+ // expiredContent
+ resultSet->GetString(ReminderStore::GetColumnIndex(EXPIRED_CONTENT), expiredContent_);
+
+ InitNotificationRequest(); // must set before wantAgent & maxScreenWantAgent
+
+ // wantAgent
+ std::string wantAgent;
+ resultSet->GetString(ReminderStore::GetColumnIndex(AGENT), wantAgent);
+ RecoverWantAgent(wantAgent, 0);
+
+ // maxScreenWantAgent
+ std::string maxScreenWantAgent;
+ resultSet->GetString(ReminderStore::GetColumnIndex(MAX_SCREEN_AGENT), maxScreenWantAgent);
+ RecoverWantAgent(wantAgent, 1);
+}
+
+void ReminderRequest::RecoverActionButton(const std::shared_ptr &resultSet)
+{
+ if (resultSet == nullptr) {
+ ANSR_LOGE("ResultSet is null");
+ return;
+ }
+ std::string 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);
+ SetActionButton(singleButton.at(1), ActionButtonType(std::stoi(singleButton.at(0), nullptr)));
+ }
+}
+
+std::vector ReminderRequest::StringSplit(std::string source, const std::string &split) const
+{
+ 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()) {
+ result.push_back(token);
+ }
+ source.erase(0, pos + split.length());
+ }
+ if (!source.empty()) {
+ result.push_back(source);
+ }
+ return result;
+}
+
+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("RecoverWantAgent fail");
+ return;
+ }
+ ANSR_LOGD("pkg=%{public}s, ability=%{public}s", info.at(0).c_str(), info.at(1).c_str());
+ 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;
+ }
+ }
+}
+
ReminderRequest& ReminderRequest::SetMaxScreenWantAgentInfo(
const std::shared_ptr &maxScreenWantAgentInfo)
{
@@ -560,6 +757,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 +813,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;
}
@@ -710,12 +922,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;
}
@@ -870,6 +1082,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 +1111,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 button : actionButtonMap_) {
+ if (!isFirst) {
+ info += SEP_BUTTON_MULTI;
+ }
+ ActionButtonInfo buttonInfo = button.second;
+ info += std::to_string(static_cast(button.first)) + SEP_BUTTON_SINGLE + buttonInfo.title;
+ isFirst = false;
+ }
+ return info;
+}
+
uint64_t ReminderRequest::GetNowInstantMilli() const
{
time_t now;
@@ -978,9 +1212,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");
@@ -997,7 +1231,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,
@@ -1121,7 +1355,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 +1375,25 @@ void ReminderRequest::UpdateNotificationCommon()
notificationRequest_->SetFlags(flags);
}
+void ReminderRequest::UpdateNotificationBundleInfo()
+{
+ 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.");
+ return;
+ }
+ notificationRequest_->SetCreatorUserId(userId_);
+}
+
void ReminderRequest::UpdateNotificationContent(const bool &setSnooze)
{
if (notificationRequest_ == nullptr) {
@@ -1153,6 +1406,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 +1501,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(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(
+ 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(AGENT, info);
+ } else {
+ 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(MAX_SCREEN_AGENT, info);
+ } else {
+ values.PutString(MAX_SCREEN_AGENT, maxScreenWantAgentInfo->pkgName
+ + ReminderRequest::SEP_WANT_AGENT + maxScreenWantAgentInfo->abilityName);
+ }
+}
+
+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);
+ 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::AddColumn(
+ const std::string &name, const std::string &type, const bool &isEnd)
+{
+ columns.push_back(name);
+ if (!isEnd) {
+ sqlOfAddColumns += name + " " + type + ", ";
+ } else {
+ sqlOfAddColumns += 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 b1b44f7a6a3be96845da98bcf3fb59043f30d1bb..532202d0c16ec1303648c7cb19319e21a6164c5a 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,50 @@ bool ReminderRequestAlarm::ReadFromParcel(Parcel &parcel)
ANSR_LOGD("hour_=%{public}d, minute_=%{public}d, repeatDays_=%{public}d", hour_, minute_, repeatDays_);
return true;
}
+
+void ReminderRequestAlarm::RecoverFromDb(const std::shared_ptr &resultSet)
+{
+ ReminderRequest::RecoverFromDb(resultSet);
+
+ // repeatDays
+ repeatDays_ =
+ static_cast(RecoverInt64FromDb(resultSet, REPEAT_DAYS_OF_WEEK, DbRecoveryType::INT));
+
+ // hour
+ hour_ =
+ static_cast(RecoverInt64FromDb(resultSet, ALARM_HOUR, DbRecoveryType::INT));
+
+ // minute
+ minute_ =
+ static_cast(RecoverInt64FromDb(resultSet, 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(REPEAT_DAYS_OF_WEEK, repeatDays);
+ values.PutInt(ALARM_HOUR, hour);
+ values.PutInt(ALARM_MINUTE, 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";
+
+void ReminderRequestAlarm::Init()
+{
+ 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 aa180640d47a749c67d227bdcc8a0db2d5c17cf9..746c2adcd41a1106367610d0f0c7d2e6a010a9f3 100644
--- a/frameworks/ans/native/src/reminder_request_calendar.cpp
+++ b/frameworks/ans/native/src/reminder_request_calendar.cpp
@@ -498,5 +498,107 @@ bool ReminderRequestCalendar::ReadFromParcel(Parcel &parcel)
}
return true;
}
+
+void ReminderRequestCalendar::RecoverFromDb(const std::shared_ptr &resultSet)
+{
+ ReminderRequest::RecoverFromDb(resultSet);
+
+ // repeatDay
+ repeatDay_ = static_cast(RecoverInt64FromDb(resultSet, REPEAT_DAYS, DbRecoveryType::INT));
+
+ // repeatMonth
+ repeatMonth_ =
+ static_cast(RecoverInt64FromDb(resultSet, REPEAT_MONTHS, DbRecoveryType::INT));
+
+ // firstDesignateYear
+ firstDesignateYear_ =
+ static_cast(RecoverInt64FromDb(resultSet, FIRST_DESIGNATE_YEAR, DbRecoveryType::INT));
+
+ // firstDesignateMonth
+ firstDesignateMonth_ =
+ static_cast(RecoverInt64FromDb(resultSet, FIRST_DESIGNATE_MONTH, DbRecoveryType::INT));
+
+ // firstDesignateDay
+ firstDesignateDay_ =
+ static_cast(RecoverInt64FromDb(resultSet, FIRST_DESIGNATE_DAY, DbRecoveryType::INT));
+
+ // year
+ year_ = static_cast(RecoverInt64FromDb(resultSet, CALENDAR_YEAR, DbRecoveryType::INT));
+
+ // month
+ month_ = static_cast(RecoverInt64FromDb(resultSet, CALENDAR_MONTH, DbRecoveryType::INT));
+
+ // day
+ day_ = static_cast(RecoverInt64FromDb(resultSet, CALENDAR_DAY, DbRecoveryType::INT));
+
+ // hour
+ hour_ = static_cast(RecoverInt64FromDb(resultSet, CALENDAR_HOUR, DbRecoveryType::INT));
+
+ // minute
+ minute_ = static_cast(RecoverInt64FromDb(resultSet, 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(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::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()
+{
+ 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
new file mode 100644
index 0000000000000000000000000000000000000000..b63bcaea32a1504d5901cd6107a3aea6d5fa9925
--- /dev/null
+++ b/frameworks/ans/native/src/reminder_store.cpp
@@ -0,0 +1,417 @@
+/*
+ * 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::sqlOfAddColumns + ")";
+ 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::Init();
+ ReminderRequestCalendar::Init();
+ ReminderRequestAlarm::Init();
+ ReminderStore::columns.insert(ReminderStore::columns.begin(),
+ ReminderRequest::columns.begin(), ReminderRequest::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::IS_EXPIRED + " is true";
+ ReminderStore::Delete(deleteCondition);
+
+ int32_t statusChangedRows = STATE_FAIL;
+ NativeRdb::ValuesBucket statusValues;
+ 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) {
+ ANSR_LOGE("Init data failed.");
+ return STATE_FAIL;
+ }
+
+ int32_t activeChangedRows = STATE_FAIL;
+ NativeRdb::ValuesBucket activeValues;
+ 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);
+ 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;
+ 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);
+ ANSR_LOGD("Change status has_ScheduledTimeout to false, changed rows: %{public}d.", scheduledChangedRows);
+ if (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::REMINDER_ID
+ + " = " + std::to_string(reminderId);
+ return ReminderStore::Delete(deleteCondition);
+}
+
+int32_t ReminderStore::DeleteUser(int32_t 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::PKG_NAME + " = " + pkg + " and "
+ + ReminderRequest::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 (rdbStore_ == nullptr) {
+ ANSR_LOGE("Rdb store is not initialized.");
+ return isSuccess;
+ }
+ if (bundleOption == nullptr) {
+ ANSR_LOGE("BundleOption is null.");
+ return isSuccess;
+ }
+ if (IsReminderExist(reminder)) {
+ isSuccess = Update(reminder, bundleOption);
+ } else {
+ isSuccess = Insert(reminder, bundleOption);
+ }
+ return isSuccess;
+}
+
+int64_t ReminderStore::Insert(
+ const sptr &reminder, const sptr &bundleOption)
+{
+ 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)
+{
+ int32_t changedRows = STATE_FAIL;
+ NativeRdb::ValuesBucket values;
+ ReminderStore::GenerateData(reminder, bundleOption, values);
+ 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);
+ 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)
+{
+ std::string queryCondition = "select " + ReminderRequest::REMINDER_ID
+ + " from " + REMINDER_DB_TABLE + " where "
+ + ReminderRequest::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;
+}
+
+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::REMINDER_ID
+ + " from " + REMINDER_DB_TABLE + " order by "
+ + ReminderRequest::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::IS_EXPIRED + " is false order by "
+ + ReminderRequest::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::REMINDER_TYPE), reminderType);
+ resultSet->GetInt(ReminderStore::GetColumnIndex(ReminderRequest::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);
+ break;
+ }
+ }
+ if (reminder != nullptr) {
+ reminder->RecoverFromDb(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::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;
+ }
+ bool isAtLastRow = false;
+ queryResultSet->IsAtLastRow(isAtLastRow);
+ if (isAtLastRow) {
+ return false;
+ }
+ queryResultSet->GoToNextRow();
+ std::string pkgName;
+ GetStringVal(queryResultSet, ReminderRequest::PKG_NAME, pkgName);
+ int32_t uid;
+ GetInt32Val(queryResultSet, ReminderRequest::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/frameworks/ans/native/test/unittest/BUILD.gn b/frameworks/ans/native/test/unittest/BUILD.gn
index c0eedd516b7d36560a3a35c0d866c4a358597716..0318720c8a1b2bb6f06db03702c6a7df137067ff 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
@@ -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/frameworks/ans/test/moduletest/BUILD.gn b/frameworks/ans/test/moduletest/BUILD.gn
index 102cfea0db97e195424d4b6d4e90e0871d7c2cbb..5312f60735febd61eb21dc87ba93a9f26ee91f09 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",
]
@@ -167,6 +168,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",
]
@@ -240,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",
]
@@ -313,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 1ab0b90b00e4bcdc0fdf5195d4cf9ffb0429bf78..f4585d1deb5efb3c9e2cf5341fcdecf86afcf70d 100644
--- a/interfaces/innerkits/ans/native/include/reminder_request.h
+++ b/interfaces/innerkits/ans/native/include/reminder_request.h
@@ -19,8 +19,11 @@
#include