• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "reminder_request.h"
17 
18 #include "ans_const_define.h"
19 #include "ans_log_wrapper.h"
20 #include "bundle_mgr_interface.h"
21 #include "bundle_mgr_proxy.h"
22 #include "if_system_ability_manager.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "locale_config.h"
26 #include "os_account_manager.h"
27 #include "reminder_store.h"
28 #include "system_ability_definition.h"
29 #include "want_agent_helper.h"
30 
31 namespace OHOS {
32 namespace Notification {
33 namespace {
34 const int32_t BASE_YEAR = 1900;
35 const int32_t SINGLE_BUTTON_MIN_LEN = 2;
36 const int32_t SINGLE_BUTTON_MAX_LEN = 4;
37 const int32_t BUTTON_TYPE_INDEX = 0;
38 const int32_t BUTTON_TITLE_INDEX = 1;
39 const int32_t BUTTON_PKG_INDEX = 2;
40 const int32_t BUTTON_ABILITY_INDEX = 3;
41 }
42 
43 int32_t ReminderRequest::GLOBAL_ID = 0;
44 const uint64_t ReminderRequest::INVALID_LONG_LONG_VALUE = 0;
45 const uint16_t ReminderRequest::INVALID_U16_VALUE = 0;
46 const uint16_t ReminderRequest::MILLI_SECONDS = 1000;
47 const uint16_t ReminderRequest::SAME_TIME_DISTINGUISH_MILLISECONDS = 1000;
48 const uint32_t ReminderRequest::MIN_TIME_INTERVAL_IN_MILLI = 5 * 60 * 1000;
49 const uint8_t ReminderRequest::INVALID_U8_VALUE = 0;
50 const uint8_t ReminderRequest::REMINDER_STATUS_INACTIVE = 0;
51 const uint8_t ReminderRequest::REMINDER_STATUS_ACTIVE = 1;
52 const uint8_t ReminderRequest::REMINDER_STATUS_ALERTING = 2;
53 const uint8_t ReminderRequest::REMINDER_STATUS_SHOWING = 4;
54 const uint8_t ReminderRequest::REMINDER_STATUS_SNOOZE = 8;
55 const uint8_t ReminderRequest::TIME_HOUR_OFFSET = 12;
56 const std::string ReminderRequest::NOTIFICATION_LABEL = "REMINDER_AGENT";
57 const std::string ReminderRequest::REMINDER_EVENT_ALARM_ALERT = "ohos.event.notification.reminder.ALARM_ALERT";
58 const std::string ReminderRequest::REMINDER_EVENT_CLOSE_ALERT = "ohos.event.notification.reminder.CLOSE_ALERT";
59 const std::string ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT = "ohos.event.notification.reminder.SNOOZE_ALERT";
60 const std::string ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT = "ohos.event.notification.reminder.COSTUM_ALERT";
61 const std::string ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT = "ohos.event.notification.reminder.ALERT_TIMEOUT";
62 const std::string ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION =
63     "ohos.event.notification.reminder.REMOVE_NOTIFICATION";
64 const std::string ReminderRequest::PARAM_REMINDER_ID = "REMINDER_ID";
65 const std::string ReminderRequest::SEP_BUTTON_SINGLE = "<SEP,/>";
66 const std::string ReminderRequest::SEP_BUTTON_MULTI = "<SEP#/>";
67 const std::string ReminderRequest::SEP_WANT_AGENT = "<SEP#/>";
68 const int32_t ReminderRequest::SUNDAY = 7;
69 
70 // For database recovery.
71 const std::string ReminderRequest::REMINDER_ID = "reminder_id";
72 const std::string ReminderRequest::PKG_NAME = "package_name";
73 const std::string ReminderRequest::USER_ID = "user_id";
74 const std::string ReminderRequest::UID = "uid";
75 const std::string ReminderRequest::SYS_APP = "system_app";
76 const std::string ReminderRequest::APP_LABEL = "app_label";
77 const std::string ReminderRequest::REMINDER_TYPE = "reminder_type";
78 const std::string ReminderRequest::REMINDER_TIME = "reminder_time";
79 const std::string ReminderRequest::TRIGGER_TIME = "trigger_time";
80 const std::string ReminderRequest::RTC_TRIGGER_TIME = "rtc_trigger_time";
81 const std::string ReminderRequest::TIME_INTERVAL = "time_interval";
82 const std::string ReminderRequest::SNOOZE_TIMES = "snooze_times";
83 const std::string ReminderRequest::DYNAMIC_SNOOZE_TIMES = "dynamic_snooze_times";
84 const std::string ReminderRequest::RING_DURATION = "ring_duration";
85 const std::string ReminderRequest::IS_EXPIRED = "is_expired";
86 const std::string ReminderRequest::IS_ACTIVE = "is_active";
87 const std::string ReminderRequest::STATE = "state";
88 const std::string ReminderRequest::ZONE_ID = "zone_id";
89 const std::string ReminderRequest::HAS_SCHEDULED_TIMEOUT = "has_ScheduledTimeout";
90 const std::string ReminderRequest::ACTION_BUTTON_INFO = "button_info";
91 const std::string ReminderRequest::CUSTOM_BUTTON_URI = "custom_button_uri";
92 const std::string ReminderRequest::SLOT_ID = "slot_id";
93 const std::string ReminderRequest::NOTIFICATION_ID = "notification_id";
94 const std::string ReminderRequest::TITLE = "title";
95 const std::string ReminderRequest::CONTENT = "content";
96 const std::string ReminderRequest::SNOOZE_CONTENT = "snooze_content";
97 const std::string ReminderRequest::EXPIRED_CONTENT = "expired_content";
98 const std::string ReminderRequest::AGENT = "agent";
99 const std::string ReminderRequest::MAX_SCREEN_AGENT = "maxScreen_agent";
100 const std::string ReminderRequest::TAP_DISMISSED = "tapDismissed";
101 const std::string ReminderRequest::AUTO_DELETED_TIME = "autoDeletedTime";
102 
103 std::string ReminderRequest::sqlOfAddColumns = "";
104 std::vector<std::string> ReminderRequest::columns;
105 
ReminderRequest()106 ReminderRequest::ReminderRequest()
107 {
108     InitServerObj();
109 }
110 
ReminderRequest(const ReminderRequest & other)111 ReminderRequest::ReminderRequest(const ReminderRequest &other)
112 {
113     this->content_ = other.content_;
114     this->expiredContent_ = other.expiredContent_;
115     this->snoozeContent_ = other.snoozeContent_;
116     this->displayContent_ = other.displayContent_;
117     this->title_ = other.title_;
118     this->isExpired_ = other.isExpired_;
119     this->isSystemApp_ = other.isSystemApp_;
120     this->snoozeTimes_ = other.snoozeTimes_;
121     this->snoozeTimesDynamic_ = other.snoozeTimesDynamic_;
122     this->state_ = other.state_;
123     this->notificationId_ = other.notificationId_;
124     this->reminderId_ = other.reminderId_;
125     this->reminderTimeInMilli_ = other.reminderTimeInMilli_;
126     this->ringDurationInMilli_ = other.ringDurationInMilli_;
127     this->triggerTimeInMilli_ = other.triggerTimeInMilli_;
128     this->timeIntervalInMilli_ = other.timeIntervalInMilli_;
129     this->reminderType_ = other.reminderType_;
130     this->slotType_ = other.slotType_;
131     this->notificationRequest_ = other.notificationRequest_;
132     this->wantAgentInfo_ = other.wantAgentInfo_;
133     this->maxScreenWantAgentInfo_ = other.maxScreenWantAgentInfo_;
134     this->actionButtonMap_ = other.actionButtonMap_;
135     this->tapDismissed_= other.tapDismissed_;
136     this->autoDeletedTime_ = other.autoDeletedTime_;
137     this->customButtonUri_ = other.customButtonUri_;
138 }
139 
ReminderRequest(int32_t reminderId)140 ReminderRequest::ReminderRequest(int32_t reminderId)
141 {
142     reminderId_ = reminderId;
143     InitServerObj();
144 }
145 
ReminderRequest(ReminderType reminderType)146 ReminderRequest::ReminderRequest(ReminderType reminderType)
147 {
148     reminderType_ = reminderType;
149     InitServerObj();
150 }
151 
CanRemove() const152 bool ReminderRequest::CanRemove() const
153 {
154     if ((state_ & (REMINDER_STATUS_SHOWING | REMINDER_STATUS_ALERTING | REMINDER_STATUS_ACTIVE)) == 0) {
155         return true;
156     }
157     return false;
158 }
159 
CanShow() const160 bool ReminderRequest::CanShow() const
161 {
162     // when system time change by user manually, and the reminde is to show immediately,
163     // the show reminder just need to be triggered by ReminderDataManager#RefreshRemindersLocked(uint8_t).
164     // we need to make the REMINDER_EVENT_ALARM_ALERT do nothing.
165     uint64_t nowInstantMilli = GetNowInstantMilli();
166     if (nowInstantMilli == 0) {
167         return false;
168     }
169     if ((nowInstantMilli - GetReminderTimeInMilli()) < MIN_TIME_INTERVAL_IN_MILLI) {
170         return false;
171     }
172     return true;
173 }
174 
Dump() const175 std::string ReminderRequest::Dump() const
176 {
177     const time_t nextTriggerTime = static_cast<time_t>(triggerTimeInMilli_ / MILLI_SECONDS);
178     std::string dateTimeInfo = GetTimeInfoInner(nextTriggerTime, TimeFormat::YMDHMS, true);
179     return "Reminder["
180            "reminderId=" + std::to_string(reminderId_) +
181            ", type=" + std::to_string(static_cast<uint8_t>(reminderType_)) +
182            ", state=" + GetState(state_) +
183            ", nextTriggerTime=" + dateTimeInfo.c_str() +
184            "]";
185 }
186 
SetActionButton(const std::string & title,const ActionButtonType & type,const std::shared_ptr<ButtonWantAgent> & buttonWantAgent)187 ReminderRequest& ReminderRequest::SetActionButton(const std::string &title, const ActionButtonType &type,
188     const std::shared_ptr<ButtonWantAgent> &buttonWantAgent)
189 {
190     if ((type != ActionButtonType::CLOSE) && (type != ActionButtonType::SNOOZE) && (type != ActionButtonType::CUSTOM)) {
191         ANSR_LOGI("Button type is not support: %{public}d.", static_cast<uint8_t>(type));
192         return *this;
193     }
194     ActionButtonInfo actionButtonInfo;
195     actionButtonInfo.type = type;
196     actionButtonInfo.title = title;
197     actionButtonInfo.wantAgent = buttonWantAgent;
198 
199     actionButtonMap_.insert(std::pair<ActionButtonType, ActionButtonInfo>(type, actionButtonInfo));
200     return *this;
201 }
202 
SetContent(const std::string & content)203 ReminderRequest& ReminderRequest::SetContent(const std::string &content)
204 {
205     content_ = content;
206     return *this;
207 }
208 
SetExpiredContent(const std::string & expiredContent)209 ReminderRequest& ReminderRequest::SetExpiredContent(const std::string &expiredContent)
210 {
211     expiredContent_ = expiredContent;
212     return *this;
213 }
214 
SetExpired(bool isExpired)215 void ReminderRequest::SetExpired(bool isExpired)
216 {
217     isExpired_ = isExpired;
218 }
219 
InitReminderId()220 void ReminderRequest::InitReminderId()
221 {
222     std::lock_guard<std::mutex> lock(std::mutex);
223     if (GLOBAL_ID < 0) {
224         ANSR_LOGW("GLOBAL_ID overdule");
225         GLOBAL_ID = 0;
226     }
227     reminderId_ = ++GLOBAL_ID;
228     ANSR_LOGI("reminderId_=%{public}d", reminderId_);
229 }
230 
InitUserId(const int32_t & userId)231 void ReminderRequest::InitUserId(const int32_t &userId)
232 {
233     userId_ = userId;
234 }
235 
InitUid(const int32_t & uid)236 void ReminderRequest::InitUid(const int32_t &uid)
237 {
238     uid_ = uid;
239 }
240 
IsExpired() const241 bool ReminderRequest::IsExpired() const
242 {
243     return isExpired_;
244 }
245 
IsShowing() const246 bool ReminderRequest::IsShowing() const
247 {
248     if ((state_ & REMINDER_STATUS_SHOWING) != 0) {
249         return true;
250     }
251     return false;
252 }
253 
OnClose(bool updateNext)254 void ReminderRequest::OnClose(bool updateNext)
255 {
256     if ((state_ & REMINDER_STATUS_SHOWING) == 0) {
257         ANSR_LOGE("onClose, the state of reminder is incorrect, state:%{public}s", GetState(state_).c_str());
258         return;
259     }
260     SetState(false, REMINDER_STATUS_SHOWING | REMINDER_STATUS_SNOOZE, "onClose()");
261     if ((state_ & REMINDER_STATUS_ALERTING) != 0) {
262         SetState(false, REMINDER_STATUS_ALERTING, "onClose");
263     }
264     if (updateNext) {
265         uint64_t nextTriggerTime = PreGetNextTriggerTimeIgnoreSnooze(true, false);
266         if (nextTriggerTime == INVALID_LONG_LONG_VALUE) {
267             isExpired_ = true;
268         } else {
269             SetTriggerTimeInMilli(nextTriggerTime);
270             snoozeTimesDynamic_ = snoozeTimes_;
271         }
272     }
273 }
274 
OnDateTimeChange()275 bool ReminderRequest::OnDateTimeChange()
276 {
277     uint64_t nextTriggerTime = PreGetNextTriggerTimeIgnoreSnooze(true, false);
278     return HandleSysTimeChange(triggerTimeInMilli_, nextTriggerTime);
279 }
280 
HandleSysTimeChange(uint64_t oriTriggerTime,uint64_t optTriggerTime)281 bool ReminderRequest::HandleSysTimeChange(uint64_t oriTriggerTime, uint64_t optTriggerTime)
282 {
283     if (isExpired_) {
284         return false;
285     }
286     uint64_t now = GetNowInstantMilli();
287     if (now == 0) {
288         ANSR_LOGE("get now time failed.");
289         return false;
290     }
291     if (oriTriggerTime == 0 && optTriggerTime < now) {
292         ANSR_LOGW("trigger time is less than now time.");
293         return false;
294     }
295     bool showImmediately = false;
296     if (optTriggerTime != INVALID_LONG_LONG_VALUE && (optTriggerTime <= oriTriggerTime || oriTriggerTime == 0)) {
297         // case1. switch to a previous time
298         SetTriggerTimeInMilli(optTriggerTime);
299         snoozeTimesDynamic_ = snoozeTimes_;
300     } else {
301         if (oriTriggerTime <= now) {
302             // case2. switch to a future time, trigger time is less than now time.
303             // when the reminder show immediately, trigger time will update in onShow function.
304             snoozeTimesDynamic_ = 0;
305             showImmediately = true;
306         } else {
307             // case3. switch to a future time, trigger time is larger than now time.
308             showImmediately = false;
309         }
310     }
311     return showImmediately;
312 }
313 
HandleTimeZoneChange(uint64_t oldZoneTriggerTime,uint64_t newZoneTriggerTime,uint64_t optTriggerTime)314 bool ReminderRequest::HandleTimeZoneChange(
315     uint64_t oldZoneTriggerTime, uint64_t newZoneTriggerTime, uint64_t optTriggerTime)
316 {
317     if (isExpired_) {
318         return false;
319     }
320     ANSR_LOGD("Handle timezone change, oldZoneTriggerTime:%{public}" PRIu64 "\
321         , newZoneTriggerTime:%{public}" PRIu64 "", oldZoneTriggerTime, newZoneTriggerTime);
322     if (oldZoneTriggerTime == newZoneTriggerTime) {
323         return false;
324     }
325     bool showImmediately = false;
326     if (optTriggerTime != INVALID_LONG_LONG_VALUE && oldZoneTriggerTime < newZoneTriggerTime) {
327         // case1. timezone change to smaller
328         SetTriggerTimeInMilli(optTriggerTime);
329         snoozeTimesDynamic_ = snoozeTimes_;
330     } else {
331         // case2. timezone change to larger
332         time_t now;
333         (void)time(&now);  // unit is seconds.
334         if (static_cast<int64_t>(now) < 0) {
335             ANSR_LOGE("Get now time error");
336             return false;
337         }
338         if (newZoneTriggerTime <= GetDurationSinceEpochInMilli(now)) {
339             snoozeTimesDynamic_ = 0;
340             showImmediately = true;
341         } else {
342             SetTriggerTimeInMilli(newZoneTriggerTime);
343             showImmediately = false;
344         }
345     }
346     return showImmediately;
347 }
348 
OnSameNotificationIdCovered()349 void ReminderRequest::OnSameNotificationIdCovered()
350 {
351     SetState(false, REMINDER_STATUS_ALERTING | REMINDER_STATUS_SHOWING | REMINDER_STATUS_SNOOZE,
352         "OnSameNotificationIdCovered");
353 }
354 
OnShow(bool isPlaySoundOrVibration,bool isSysTimeChanged,bool allowToNotify)355 void ReminderRequest::OnShow(bool isPlaySoundOrVibration, bool isSysTimeChanged, bool allowToNotify)
356 {
357     if ((state_ & (REMINDER_STATUS_ACTIVE | REMINDER_STATUS_SNOOZE)) != 0) {
358         SetState(false, REMINDER_STATUS_ACTIVE | REMINDER_STATUS_SNOOZE, "onShow()");
359     }
360     if (isSysTimeChanged) {
361         uint64_t nowInstantMilli = GetNowInstantMilli();
362         if (nowInstantMilli == 0) {
363             ANSR_LOGW("Onshow, get now time error");
364         }
365         reminderTimeInMilli_ = nowInstantMilli;
366     } else {
367         reminderTimeInMilli_ = triggerTimeInMilli_;
368     }
369     UpdateNextReminder(false);
370     if (allowToNotify) {
371         SetState(true, REMINDER_STATUS_SHOWING, "OnShow");
372         if (isPlaySoundOrVibration) {
373             SetState(true, REMINDER_STATUS_ALERTING, "OnShow");
374         }
375         UpdateNotificationStateForAlert();
376     }
377 }
378 
OnShowFail()379 void ReminderRequest::OnShowFail()
380 {
381     SetState(false, REMINDER_STATUS_SHOWING, "OnShowFailed()");
382 }
383 
OnSnooze()384 bool ReminderRequest::OnSnooze()
385 {
386     if ((state_ & REMINDER_STATUS_SNOOZE) != 0) {
387         ANSR_LOGW("onSnooze, the state of reminder is incorrect, state: %{public}s", (GetState(state_)).c_str());
388         return false;
389     }
390     if ((state_ & REMINDER_STATUS_ALERTING) != 0) {
391         SetState(false, REMINDER_STATUS_ALERTING, "onSnooze()");
392     }
393     SetSnoozeTimesDynamic(GetSnoozeTimes());
394     if (!UpdateNextReminder(true)) {
395         return false;
396     }
397     UpdateNotificationStateForSnooze();
398     if (timeIntervalInMilli_ > 0) {
399         SetState(true, REMINDER_STATUS_SNOOZE, "onSnooze()");
400     }
401     return true;
402 }
403 
OnStart()404 void ReminderRequest::OnStart()
405 {
406     if ((state_ & REMINDER_STATUS_ACTIVE) != 0) {
407         ANSR_LOGE(
408             "start failed, the state of reminder is incorrect, state: %{public}s", (GetState(state_)).c_str());
409         return;
410     }
411     if (isExpired_) {
412         ANSR_LOGE("start failed, the reminder is expired");
413         return;
414     }
415     SetState(true, REMINDER_STATUS_ACTIVE, "OnStart()");
416 }
417 
OnStop()418 void ReminderRequest::OnStop()
419 {
420     ANSR_LOGI("Stop the previous active reminder, %{public}s", this->Dump().c_str());
421     if ((state_ & REMINDER_STATUS_ACTIVE) == 0) {
422         ANSR_LOGW("onStop, the state of reminder is incorrect, state: %{public}s", (GetState(state_)).c_str());
423         return;
424     }
425     SetState(false, REMINDER_STATUS_ACTIVE, "OnStop");
426 }
427 
OnTerminate()428 bool ReminderRequest::OnTerminate()
429 {
430     if ((state_ & REMINDER_STATUS_ALERTING) == 0) {
431         ANSR_LOGW("onTerminate, the state of reminder is %{public}s", (GetState(state_)).c_str());
432         return false;
433     }
434     SetState(false, REMINDER_STATUS_ALERTING, "onTerminate");
435     UpdateNotificationStateForAlert();
436     return true;
437 }
438 
OnTimeZoneChange()439 bool ReminderRequest::OnTimeZoneChange()
440 {
441     time_t oldZoneTriggerTime = static_cast<time_t>(triggerTimeInMilli_ / MILLI_SECONDS);
442     struct tm oriTime;
443     (void)gmtime_r(&oldZoneTriggerTime, &oriTime);
444     time_t newZoneTriggerTime = mktime(&oriTime);
445     uint64_t nextTriggerTime = PreGetNextTriggerTimeIgnoreSnooze(true, false);
446     return HandleTimeZoneChange(
447         triggerTimeInMilli_, GetDurationSinceEpochInMilli(newZoneTriggerTime), nextTriggerTime);
448 }
449 
RecoverInt64FromDb(const std::shared_ptr<NativeRdb::ResultSet> & resultSet,const std::string & columnName,const DbRecoveryType & columnType)450 int64_t ReminderRequest::RecoverInt64FromDb(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
451     const std::string &columnName, const DbRecoveryType &columnType)
452 {
453     if (resultSet == nullptr) {
454         ANSR_LOGE("ResultSet is null");
455         return 0;
456     }
457     switch (columnType) {
458         case (DbRecoveryType::INT): {
459             int32_t value;
460             resultSet->GetInt(ReminderStore::GetColumnIndex(columnName), value);
461             return static_cast<int64_t>(value);
462         }
463         case (DbRecoveryType::LONG): {
464             int64_t value;
465             resultSet->GetLong(ReminderStore::GetColumnIndex(columnName), value);
466             return value;
467         }
468         default: {
469             ANSR_LOGD("ColumnType not support.");
470             break;
471         }
472     }
473     ANSR_LOGE("Recover data error");
474     return 0;
475 }
476 
RecoverFromDb(const std::shared_ptr<NativeRdb::ResultSet> & resultSet)477 void ReminderRequest::RecoverFromDb(const std::shared_ptr<NativeRdb::ResultSet> &resultSet)
478 {
479     if (resultSet == nullptr) {
480         ANSR_LOGE("ResultSet is null");
481         return;
482     }
483 
484     // reminderId
485     resultSet->GetInt(ReminderStore::GetColumnIndex(REMINDER_ID), reminderId_);
486 
487     // userId
488     resultSet->GetInt(ReminderStore::GetColumnIndex(USER_ID), userId_);
489 
490     // bundleName
491     resultSet->GetString(ReminderStore::GetColumnIndex(PKG_NAME), bundleName_);
492 
493     // uid
494     resultSet->GetInt(ReminderStore::GetColumnIndex(UID), uid_);
495 
496     // isSystemApp
497     std::string isSysApp;
498     resultSet->GetString(ReminderStore::GetColumnIndex(SYS_APP), isSysApp);
499     isSystemApp_ = isSysApp == "true" ? true : false;
500 
501     // reminderType
502     int32_t reminderType;
503     resultSet->GetInt(ReminderStore::GetColumnIndex(REMINDER_TYPE), reminderType);
504     reminderType_ = ReminderType(reminderType);
505 
506     // reminderTime
507     reminderTimeInMilli_ =
508         static_cast<uint64_t>(RecoverInt64FromDb(resultSet, REMINDER_TIME, DbRecoveryType::LONG));
509 
510     // triggerTime
511     triggerTimeInMilli_ =
512         static_cast<uint64_t>(RecoverInt64FromDb(resultSet, TRIGGER_TIME, DbRecoveryType::LONG));
513 
514     // timeInterval
515     uint64_t timeIntervalInSecond =
516         static_cast<uint64_t>(RecoverInt64FromDb(resultSet, TIME_INTERVAL, DbRecoveryType::LONG));
517     SetTimeInterval(timeIntervalInSecond);
518 
519     // snoozeTimes
520     snoozeTimes_ = static_cast<uint8_t>(RecoverInt64FromDb(resultSet, SNOOZE_TIMES, DbRecoveryType::INT));
521 
522     // dynamicSnoozeTimes
523     snoozeTimesDynamic_ =
524         static_cast<uint8_t>(RecoverInt64FromDb(resultSet, DYNAMIC_SNOOZE_TIMES, DbRecoveryType::INT));
525 
526     // ringDuration
527     uint64_t ringDurationInSecond =
528         static_cast<uint64_t>(RecoverInt64FromDb(resultSet, RING_DURATION, DbRecoveryType::LONG));
529     SetRingDuration(ringDurationInSecond);
530 
531     // isExpired
532     std::string isExpired;
533     resultSet->GetString(ReminderStore::GetColumnIndex(IS_EXPIRED), isExpired);
534     isExpired_ = isExpired == "true" ? true : false;
535 
536     // state
537     state_ = static_cast<uint8_t>(RecoverInt64FromDb(resultSet, STATE, DbRecoveryType::INT));
538 
539     // action buttons
540     RecoverActionButton(resultSet);
541 
542     // slotType
543     int32_t slotType;
544     resultSet->GetInt(ReminderStore::GetColumnIndex(SLOT_ID), slotType);
545     slotType_ = NotificationConstant::SlotType(slotType);
546 
547     // notification id
548     resultSet->GetInt(ReminderStore::GetColumnIndex(NOTIFICATION_ID), notificationId_);
549 
550     // title
551     resultSet->GetString(ReminderStore::GetColumnIndex(TITLE), title_);
552 
553     // content
554     resultSet->GetString(ReminderStore::GetColumnIndex(CONTENT), content_);
555 
556     // snoozeContent
557     resultSet->GetString(ReminderStore::GetColumnIndex(SNOOZE_CONTENT), snoozeContent_);
558 
559     // expiredContent
560     resultSet->GetString(ReminderStore::GetColumnIndex(EXPIRED_CONTENT), expiredContent_);
561 
562     InitNotificationRequest();  // must set before wantAgent & maxScreenWantAgent
563 
564     // wantAgent
565     std::string wantAgent;
566     resultSet->GetString(ReminderStore::GetColumnIndex(AGENT), wantAgent);
567     RecoverWantAgent(wantAgent, 0);
568 
569     // maxScreenWantAgent
570     std::string maxScreenWantAgent;
571     resultSet->GetString(ReminderStore::GetColumnIndex(MAX_SCREEN_AGENT), maxScreenWantAgent);
572     RecoverWantAgent(maxScreenWantAgent, 1);
573 
574     // tapDismissed
575     std::string tapDismissed;
576     resultSet->GetString(ReminderStore::GetColumnIndex(TAP_DISMISSED), tapDismissed);
577     tapDismissed_ = tapDismissed == "true" ? true : false;
578 
579     // autoDeletedTime
580     autoDeletedTime_ =
581         static_cast<int64_t>(RecoverInt64FromDb(resultSet, AUTO_DELETED_TIME, DbRecoveryType::LONG));
582 
583     // customButtonUri
584     resultSet->GetString(ReminderStore::GetColumnIndex(CUSTOM_BUTTON_URI), customButtonUri_);
585 }
586 
RecoverActionButton(const std::shared_ptr<NativeRdb::ResultSet> & resultSet)587 void ReminderRequest::RecoverActionButton(const std::shared_ptr<NativeRdb::ResultSet> &resultSet)
588 {
589     if (resultSet == nullptr) {
590         ANSR_LOGE("ResultSet is null");
591         return;
592     }
593     std::string actionButtonInfo;
594     resultSet->GetString(ReminderStore::GetColumnIndex(ACTION_BUTTON_INFO), actionButtonInfo);
595     std::vector<std::string> multiButton = StringSplit(actionButtonInfo, SEP_BUTTON_MULTI);
596     for (auto button : multiButton) {
597         std::vector<std::string> singleButton = StringSplit(button, SEP_BUTTON_SINGLE);
598         if (singleButton.size() < SINGLE_BUTTON_MIN_LEN) {
599             ANSR_LOGW("RecoverButton fail");
600             return;
601         }
602         auto buttonWantAgent = std::make_shared<ReminderRequest::ButtonWantAgent>();
603         if (singleButton.size() == SINGLE_BUTTON_MAX_LEN) {
604             buttonWantAgent->pkgName = singleButton.at(BUTTON_PKG_INDEX);
605             buttonWantAgent->abilityName = singleButton.at(BUTTON_ABILITY_INDEX);
606         }
607         SetActionButton(singleButton.at(BUTTON_TITLE_INDEX),
608             ActionButtonType(std::stoi(singleButton.at(BUTTON_TYPE_INDEX), nullptr)), buttonWantAgent);
609         ANSR_LOGI("RecoverButton title:%{public}s, pkgName:%{public}s, abilityName:%{public}s",
610             singleButton.at(BUTTON_TITLE_INDEX).c_str(), buttonWantAgent->pkgName.c_str(),
611             buttonWantAgent->abilityName.c_str());
612     }
613 }
614 
StringSplit(std::string source,const std::string & split) const615 std::vector<std::string> ReminderRequest::StringSplit(std::string source, const std::string &split) const
616 {
617     std::vector<std::string> result;
618     if (source.empty()) {
619         return result;
620     }
621     size_t pos = 0;
622     while ((pos = source.find(split)) != std::string::npos) {
623         std::string token = source.substr(0, pos);
624         if (!token.empty()) {
625             result.push_back(token);
626         }
627         source.erase(0, pos + split.length());
628     }
629     if (!source.empty()) {
630         result.push_back(source);
631     }
632     return result;
633 }
634 
RecoverWantAgent(const std::string & wantAgentInfo,const uint8_t & type)635 void ReminderRequest::RecoverWantAgent(const std::string &wantAgentInfo, const uint8_t &type)
636 {
637     std::vector<std::string> info = StringSplit(wantAgentInfo, ReminderRequest::SEP_WANT_AGENT);
638     uint8_t minLen = 2;
639     if (info.size() < minLen) {
640         ANSR_LOGW("RecoverWantAgent fail");
641         return;
642     }
643     ANSR_LOGD("pkg=%{public}s, ability=%{public}s", info.at(0).c_str(), info.at(1).c_str());
644     switch (type) {
645         case 0: {
646             auto wai = std::make_shared<ReminderRequest::WantAgentInfo>();
647             wai->pkgName = info.at(0);
648             wai->abilityName = info.at(1);
649             SetWantAgentInfo(wai);
650             break;
651         }
652         case 1: {
653             auto maxScreenWantAgentInfo = std::make_shared<ReminderRequest::MaxScreenAgentInfo>();
654             maxScreenWantAgentInfo->pkgName = info.at(0);
655             maxScreenWantAgentInfo->abilityName = info.at(1);
656             SetMaxScreenWantAgentInfo(maxScreenWantAgentInfo);
657             break;
658         }
659         default: {
660             ANSR_LOGW("RecoverWantAgent type not support");
661             break;
662         }
663     }
664 }
665 
SetMaxScreenWantAgentInfo(const std::shared_ptr<MaxScreenAgentInfo> & maxScreenWantAgentInfo)666 ReminderRequest& ReminderRequest::SetMaxScreenWantAgentInfo(
667     const std::shared_ptr<MaxScreenAgentInfo> &maxScreenWantAgentInfo)
668 {
669     maxScreenWantAgentInfo_ = maxScreenWantAgentInfo;
670     return *this;
671 }
672 
SetNotificationId(int32_t notificationId)673 ReminderRequest& ReminderRequest::SetNotificationId(int32_t notificationId)
674 {
675     notificationId_ = notificationId;
676     return *this;
677 }
678 
SetSlotType(const NotificationConstant::SlotType & slotType)679 ReminderRequest& ReminderRequest::SetSlotType(const NotificationConstant::SlotType &slotType)
680 {
681     slotType_ = slotType;
682     return *this;
683 }
684 
SetSnoozeContent(const std::string & snoozeContent)685 ReminderRequest& ReminderRequest::SetSnoozeContent(const std::string &snoozeContent)
686 {
687     snoozeContent_ = snoozeContent;
688     return *this;
689 }
690 
SetSnoozeTimes(const uint8_t snoozeTimes)691 ReminderRequest& ReminderRequest::SetSnoozeTimes(const uint8_t snoozeTimes)
692 {
693     snoozeTimes_ = snoozeTimes;
694     SetSnoozeTimesDynamic(snoozeTimes);
695     return *this;
696 }
697 
SetSnoozeTimesDynamic(const uint8_t snooziTimes)698 ReminderRequest& ReminderRequest::SetSnoozeTimesDynamic(const uint8_t snooziTimes)
699 {
700     snoozeTimesDynamic_ = snooziTimes;
701     return *this;
702 }
703 
SetTimeInterval(const uint64_t timeIntervalInSeconds)704 ReminderRequest& ReminderRequest::SetTimeInterval(const uint64_t timeIntervalInSeconds)
705 {
706     if (timeIntervalInSeconds > (UINT64_MAX / MILLI_SECONDS)) {
707         ANSR_LOGW("SetTimeInterval, replace to set (0s), for the given is out of legal range");
708         timeIntervalInMilli_ = 0;
709     } else {
710         uint64_t timeIntervalInMilli = timeIntervalInSeconds * MILLI_SECONDS;
711         if (timeIntervalInMilli > 0 && timeIntervalInMilli < MIN_TIME_INTERVAL_IN_MILLI) {
712             ANSR_LOGW("SetTimeInterval, replace to set %{public}u, for the given is 0<%{public}" PRIu64 "<%{public}u",
713                 MIN_TIME_INTERVAL_IN_MILLI / MILLI_SECONDS, timeIntervalInSeconds,
714                 MIN_TIME_INTERVAL_IN_MILLI / MILLI_SECONDS);
715             timeIntervalInMilli_ = MIN_TIME_INTERVAL_IN_MILLI;
716         } else {
717             timeIntervalInMilli_ = timeIntervalInMilli;
718         }
719     }
720     return *this;
721 }
722 
SetTitle(const std::string & title)723 ReminderRequest& ReminderRequest::SetTitle(const std::string &title)
724 {
725     title_ = title;
726     return *this;
727 }
728 
SetTriggerTimeInMilli(uint64_t triggerTimeInMilli)729 void ReminderRequest::SetTriggerTimeInMilli(uint64_t triggerTimeInMilli)
730 {
731     triggerTimeInMilli_ = triggerTimeInMilli;
732 }
733 
SetWantAgentInfo(const std::shared_ptr<WantAgentInfo> & wantAgentInfo)734 ReminderRequest& ReminderRequest::SetWantAgentInfo(const std::shared_ptr<WantAgentInfo> &wantAgentInfo)
735 {
736     wantAgentInfo_ = wantAgentInfo;
737     return *this;
738 }
739 
ShouldShowImmediately() const740 bool ReminderRequest::ShouldShowImmediately() const
741 {
742     uint64_t nowInstantMilli = GetNowInstantMilli();
743     if (nowInstantMilli == 0) {
744         return false;
745     }
746     if (triggerTimeInMilli_ > nowInstantMilli) {
747         return false;
748     }
749     return true;
750 }
751 
GetActionButtons() const752 std::map<ReminderRequest::ActionButtonType, ReminderRequest::ActionButtonInfo> ReminderRequest::GetActionButtons(
753     ) const
754 {
755     return actionButtonMap_;
756 }
757 
GetContent() const758 std::string ReminderRequest::GetContent() const
759 {
760     return content_;
761 }
762 
GetExpiredContent() const763 std::string ReminderRequest::GetExpiredContent() const
764 {
765     return expiredContent_;
766 }
767 
GetMaxScreenWantAgentInfo() const768 std::shared_ptr<ReminderRequest::MaxScreenAgentInfo> ReminderRequest::GetMaxScreenWantAgentInfo() const
769 {
770     return maxScreenWantAgentInfo_;
771 }
772 
GetNotificationId() const773 int32_t ReminderRequest::GetNotificationId() const
774 {
775     return notificationId_;
776 }
777 
GetNotificationRequest() const778 sptr<NotificationRequest> ReminderRequest::GetNotificationRequest() const
779 {
780     return notificationRequest_;
781 }
782 
GetReminderId() const783 int32_t ReminderRequest::GetReminderId() const
784 {
785     return reminderId_;
786 }
787 
GetReminderTimeInMilli() const788 uint64_t ReminderRequest::GetReminderTimeInMilli() const
789 {
790     return reminderTimeInMilli_;
791 }
792 
SetReminderId(int32_t reminderId)793 void ReminderRequest::SetReminderId(int32_t reminderId)
794 {
795     reminderId_ = reminderId;
796 }
797 
SetReminderTimeInMilli(const uint64_t reminderTimeInMilli)798 void ReminderRequest::SetReminderTimeInMilli(const uint64_t reminderTimeInMilli)
799 {
800     reminderTimeInMilli_ = reminderTimeInMilli;
801 }
802 
SetRingDuration(const uint64_t ringDurationInSeconds)803 ReminderRequest& ReminderRequest::SetRingDuration(const uint64_t ringDurationInSeconds)
804 {
805     if ((ringDurationInSeconds == 0) || (ringDurationInSeconds > (UINT64_MAX / MILLI_SECONDS))) {
806         ANSR_LOGW("setRingDuration, replace to set (1s), for the given is out of legal range");
807         ringDurationInMilli_ = MILLI_SECONDS;
808     } else {
809         ringDurationInMilli_ = ringDurationInSeconds * MILLI_SECONDS;
810     }
811     return *this;
812 }
813 
GetSlotType() const814 NotificationConstant::SlotType ReminderRequest::GetSlotType() const
815 {
816     return slotType_;
817 }
818 
GetSnoozeContent() const819 std::string ReminderRequest::GetSnoozeContent() const
820 {
821     return snoozeContent_;
822 }
823 
GetSnoozeTimes() const824 uint8_t ReminderRequest::GetSnoozeTimes() const
825 {
826     return snoozeTimes_;
827 }
828 
GetSnoozeTimesDynamic() const829 uint8_t ReminderRequest::GetSnoozeTimesDynamic() const
830 {
831     return snoozeTimesDynamic_;
832 }
833 
GetState() const834 uint8_t ReminderRequest::GetState() const
835 {
836     return state_;
837 }
838 
GetTimeInterval() const839 uint64_t ReminderRequest::GetTimeInterval() const
840 {
841     return timeIntervalInMilli_ / MILLI_SECONDS;
842 }
843 
GetTitle() const844 std::string ReminderRequest::GetTitle() const
845 {
846     return title_;
847 }
848 
GetTriggerTimeInMilli() const849 uint64_t ReminderRequest::GetTriggerTimeInMilli() const
850 {
851     return triggerTimeInMilli_;
852 }
853 
GetUserId() const854 int32_t ReminderRequest::GetUserId() const
855 {
856     return userId_;
857 }
858 
GetUid() const859 int32_t ReminderRequest::GetUid() const
860 {
861     return uid_;
862 }
863 
SetSystemApp(bool isSystem)864 void ReminderRequest::SetSystemApp(bool isSystem)
865 {
866     isSystemApp_ = isSystem;
867 }
868 
IsSystemApp() const869 bool ReminderRequest::IsSystemApp() const
870 {
871     return isSystemApp_;
872 }
873 
SetTapDismissed(bool tapDismissed)874 void ReminderRequest::SetTapDismissed(bool tapDismissed)
875 {
876     tapDismissed_ = tapDismissed;
877 }
878 
IsTapDismissed() const879 bool ReminderRequest::IsTapDismissed() const
880 {
881     return tapDismissed_;
882 }
883 
SetAutoDeletedTime(int64_t autoDeletedTime)884 void ReminderRequest::SetAutoDeletedTime(int64_t autoDeletedTime)
885 {
886     autoDeletedTime_ = autoDeletedTime;
887 }
888 
GetAutoDeletedTime() const889 int64_t ReminderRequest::GetAutoDeletedTime() const
890 {
891     return autoDeletedTime_;
892 }
893 
SetCustomButtonUri(const std::string & uri)894 void ReminderRequest::SetCustomButtonUri(const std::string &uri)
895 {
896     customButtonUri_ = uri;
897 }
898 
GetCustomButtonUri() const899 std::string ReminderRequest::GetCustomButtonUri() const
900 {
901     return customButtonUri_;
902 }
903 
GetWantAgentInfo() const904 std::shared_ptr<ReminderRequest::WantAgentInfo> ReminderRequest::GetWantAgentInfo() const
905 {
906     return wantAgentInfo_;
907 }
908 
GetReminderType() const909 ReminderRequest::ReminderType ReminderRequest::GetReminderType() const
910 {
911     return reminderType_;
912 }
913 
GetRingDuration() const914 uint16_t ReminderRequest::GetRingDuration() const
915 {
916     return ringDurationInMilli_ / MILLI_SECONDS;
917 }
918 
UpdateNextReminder()919 bool ReminderRequest::UpdateNextReminder()
920 {
921     return false;
922 }
923 
SetNextTriggerTime()924 bool ReminderRequest::SetNextTriggerTime()
925 {
926     return false;
927 }
928 
UpdateNotificationRequest(UpdateNotificationType type,std::string extra)929 void ReminderRequest::UpdateNotificationRequest(UpdateNotificationType type, std::string extra)
930 {
931     switch (type) {
932         case UpdateNotificationType::COMMON: {
933             ANSR_LOGI("UpdateNotification common information");
934             UpdateNotificationCommon();
935             break;
936         }
937         case UpdateNotificationType::REMOVAL_WANT_AGENT: {
938             ANSR_LOGI("UpdateNotification removal_want_agent");
939             AddRemovalWantAgent();
940             break;
941         }
942         case UpdateNotificationType::WANT_AGENT: {
943             ANSR_LOGI("UpdateNotification want_agent");
944             AppExecFwk::ElementName wantAgent("", wantAgentInfo_->pkgName, wantAgentInfo_->abilityName);
945             SetWantAgent(wantAgent);
946             break;
947         }
948         case UpdateNotificationType::MAX_SCREEN_WANT_AGENT: {
949             ANSR_LOGI("UpdateNotification max_screen_want_agent");
950             AppExecFwk::ElementName maxScreenWantAgent(
951                 "", maxScreenWantAgentInfo_->pkgName, maxScreenWantAgentInfo_->abilityName);
952             SetMaxScreenWantAgent(maxScreenWantAgent);
953             break;
954         }
955         case UpdateNotificationType::BUNDLE_INFO: {
956             ANSR_LOGI("UpdateNotification hap information");
957             UpdateNotificationBundleInfo();
958             break;
959         }
960         case UpdateNotificationType::CONTENT: {
961             break;
962         }
963         default:
964             break;
965     }
966 }
967 
Marshalling(Parcel & parcel) const968 bool ReminderRequest::Marshalling(Parcel &parcel) const
969 {
970     // write string
971     if (!parcel.WriteString(content_)) {
972         ANSR_LOGE("Failed to write content");
973         return false;
974     }
975     if (!parcel.WriteString(expiredContent_)) {
976         ANSR_LOGE("Failed to write expiredContent");
977         return false;
978     }
979     if (!parcel.WriteString(snoozeContent_)) {
980         ANSR_LOGE("Failed to write snoozeContent");
981         return false;
982     }
983     if (!parcel.WriteString(title_)) {
984         ANSR_LOGE("Failed to write title");
985         return false;
986     }
987     if (!parcel.WriteString(wantAgentInfo_->abilityName)) {
988         ANSR_LOGE("Failed to write wantAgentInfo`s abilityName");
989         return false;
990     }
991     if (!parcel.WriteString(wantAgentInfo_->pkgName)) {
992         ANSR_LOGE("Failed to write wantAgentInfo`s pkgName");
993         return false;
994     }
995     if (!parcel.WriteString(maxScreenWantAgentInfo_->abilityName)) {
996         ANSR_LOGE("Failed to write maxScreenWantAgentInfo`s abilityName");
997         return false;
998     }
999     if (!parcel.WriteString(maxScreenWantAgentInfo_->pkgName)) {
1000         ANSR_LOGE("Failed to write maxScreenWantAgentInfo`s pkgName");
1001         return false;
1002     }
1003     if (!parcel.WriteString(customButtonUri_)) {
1004         ANSR_LOGE("Failed to write customButtonUri");
1005         return false;
1006     }
1007 
1008     // write bool
1009     if (!parcel.WriteBool(isExpired_)) {
1010         ANSR_LOGE("Failed to write isExpired");
1011         return false;
1012     }
1013     if (!parcel.WriteBool(isSystemApp_)) {
1014         ANSR_LOGE("Failed to write isSystemApp");
1015         return false;
1016     }
1017     if (!parcel.WriteBool(tapDismissed_)) {
1018         ANSR_LOGE("Failed to write tapDismissed");
1019         return false;
1020     }
1021 
1022     // write int
1023     if (!parcel.WriteInt64(autoDeletedTime_)) {
1024         ANSR_LOGE("Failed to write autoDeletedTime");
1025         return false;
1026     }
1027     if (!parcel.WriteInt32(reminderId_)) {
1028         ANSR_LOGE("Failed to write reminderId");
1029         return false;
1030     }
1031     if (!parcel.WriteInt32(notificationId_)) {
1032         ANSR_LOGE("Failed to write notificationId");
1033         return false;
1034     }
1035     if (!parcel.WriteUint64(triggerTimeInMilli_)) {
1036         ANSR_LOGE("Failed to write triggerTimeInMilli");
1037         return false;
1038     }
1039     if (!parcel.WriteUint64(timeIntervalInMilli_)) {
1040         ANSR_LOGE("Failed to write timeIntervalInMilli");
1041         return false;
1042     }
1043     if (!parcel.WriteUint64(ringDurationInMilli_)) {
1044         ANSR_LOGE("Failed to write ringDurationInMilli");
1045         return false;
1046     }
1047     if (!parcel.WriteUint64(reminderTimeInMilli_)) {
1048         ANSR_LOGE("Failed to write reminderTimeInMilli");
1049         return false;
1050     }
1051     if (!parcel.WriteUint8(snoozeTimes_)) {
1052         ANSR_LOGE("Failed to write snoozeTimes");
1053         return false;
1054     }
1055     if (!parcel.WriteUint8(snoozeTimesDynamic_)) {
1056         ANSR_LOGE("Failed to write snoozeTimesDynamic");
1057         return false;
1058     }
1059     if (!parcel.WriteUint8(state_)) {
1060         ANSR_LOGE("Failed to write state");
1061         return false;
1062     }
1063 
1064     // write enum
1065     if (!parcel.WriteUint8(static_cast<uint8_t>(reminderType_))) {
1066         ANSR_LOGE("Failed to write reminder type");
1067         return false;
1068     }
1069     if (!parcel.WriteInt32(static_cast<int32_t>(slotType_))) {
1070         ANSR_LOGE("Failed to write slot type");
1071         return false;
1072     }
1073 
1074     // write map
1075     if (!parcel.WriteUint64(static_cast<uint64_t>(actionButtonMap_.size()))) {
1076         ANSR_LOGE("Failed to write action button size");
1077         return false;
1078     }
1079     for (auto button : actionButtonMap_) {
1080         if (!parcel.WriteUint8(static_cast<uint8_t>(button.first))) {
1081             ANSR_LOGE("Failed to write action button type");
1082             return false;
1083         }
1084         if (!parcel.WriteString(static_cast<std::string>(button.second.title))) {
1085             ANSR_LOGE("Failed to write action button title");
1086             return false;
1087         }
1088         if (button.second.wantAgent == nullptr) {
1089             ANSR_LOGE("button wantAgent is null");
1090             return false;
1091         }
1092         if (!parcel.WriteString(button.second.wantAgent->pkgName)) {
1093             ANSR_LOGE("Failed to write action button pkgName");
1094             return false;
1095         }
1096         if (!parcel.WriteString(button.second.wantAgent->abilityName)) {
1097             ANSR_LOGE("Failed to write action button abilityName");
1098             return false;
1099         }
1100     }
1101     return true;
1102 }
1103 
Unmarshalling(Parcel & parcel)1104 ReminderRequest *ReminderRequest::Unmarshalling(Parcel &parcel)
1105 {
1106     auto objptr = new (std::nothrow) ReminderRequest();
1107     if (objptr == nullptr) {
1108         ANSR_LOGE("Failed to create reminder due to no memory.");
1109         return objptr;
1110     }
1111     if (!objptr->ReadFromParcel(parcel)) {
1112         delete objptr;
1113         objptr = nullptr;
1114     }
1115     return objptr;
1116 }
1117 
ReadFromParcel(Parcel & parcel)1118 bool ReminderRequest::ReadFromParcel(Parcel &parcel)
1119 {
1120     // read string
1121     if (!parcel.ReadString(content_)) {
1122         ANSR_LOGE("Failed to read content");
1123         return false;
1124     }
1125     if (!parcel.ReadString(expiredContent_)) {
1126         ANSR_LOGE("to read expiredContent");
1127         return false;
1128     }
1129     if (!parcel.ReadString(snoozeContent_)) {
1130         ANSR_LOGE("to read snoozeContent");
1131         return false;
1132     }
1133     if (!parcel.ReadString(title_)) {
1134         ANSR_LOGE("Failed to read title");
1135         return false;
1136     }
1137     if (!parcel.ReadString(wantAgentInfo_->abilityName)) {
1138         ANSR_LOGE("Failed to read wantAgentInfo`s abilityName");
1139         return false;
1140     }
1141     if (!parcel.ReadString(wantAgentInfo_->pkgName)) {
1142         ANSR_LOGE("Failed to read wantAgentInfo`s pkgName");
1143         return false;
1144     }
1145     if (!parcel.ReadString(maxScreenWantAgentInfo_->abilityName)) {
1146         ANSR_LOGE("Failed to read maxScreenWantAgentInfo`s abilityName");
1147         return false;
1148     }
1149     if (!parcel.ReadString(maxScreenWantAgentInfo_->pkgName)) {
1150         ANSR_LOGE("Failed to read maxScreenWantAgentInfo`s pkgName");
1151         return false;
1152     }
1153     if (!parcel.ReadString(customButtonUri_)) {
1154         ANSR_LOGE("Failed to read customButtonUri");
1155         return false;
1156     }
1157 
1158     // read bool
1159     if (!parcel.ReadBool(isExpired_)) {
1160         ANSR_LOGE("Failed to read isExpired");
1161         return false;
1162     }
1163     if (!parcel.ReadBool(isSystemApp_)) {
1164         ANSR_LOGE("Failed to read isSystemApp");
1165         return false;
1166     }
1167     if (!parcel.ReadBool(tapDismissed_)) {
1168         ANSR_LOGE("Failed to read tapDismissed");
1169         return false;
1170     }
1171 
1172     // read int
1173     if (!parcel.ReadInt64(autoDeletedTime_)) {
1174         ANSR_LOGE("Failed to read autoDeletedTime");
1175         return false;
1176     }
1177     int32_t tempReminderId = -1;
1178     if (!parcel.ReadInt32(tempReminderId)) {
1179         ANSR_LOGE("Failed to read tempReminderId");
1180         return false;
1181     }
1182     reminderId_ = (tempReminderId == -1) ? reminderId_ : tempReminderId;
1183 
1184     if (!parcel.ReadInt32(notificationId_)) {
1185         ANSR_LOGE("Failed to read notificationId");
1186         return false;
1187     }
1188     if (!parcel.ReadUint64(triggerTimeInMilli_)) {
1189         ANSR_LOGE("Failed to read triggerTimeInMilli");
1190         return false;
1191     }
1192     if (!parcel.ReadUint64(timeIntervalInMilli_)) {
1193         ANSR_LOGE("Failed to read timeIntervalInMilli");
1194         return false;
1195     }
1196     if (!parcel.ReadUint64(ringDurationInMilli_)) {
1197         ANSR_LOGE("Failed to read ringDurationInMilli");
1198         return false;
1199     }
1200     if (!parcel.ReadUint64(reminderTimeInMilli_)) {
1201         ANSR_LOGE("Failed to read reminderTimeInMilli");
1202         return false;
1203     }
1204     if (!parcel.ReadUint8(snoozeTimes_)) {
1205         ANSR_LOGE("Failed to read snoozeTimes");
1206         return false;
1207     }
1208     if (!parcel.ReadUint8(snoozeTimesDynamic_)) {
1209         ANSR_LOGE("Failed to read snoozeTimesDynamic");
1210         return false;
1211     }
1212     if (!parcel.ReadUint8(state_)) {
1213         ANSR_LOGE("Failed to read state");
1214         return false;
1215     }
1216 
1217     // read enum
1218     uint8_t reminderType = static_cast<uint8_t>(ReminderType::INVALID);
1219     if (!parcel.ReadUint8(reminderType)) {
1220         ANSR_LOGE("Failed to read reminderType");
1221         return false;
1222     }
1223     reminderType_ = static_cast<ReminderType>(reminderType);
1224 
1225     int32_t slotType = static_cast<int32_t>(NotificationConstant::SlotType::OTHER);
1226     if (!parcel.ReadInt32(slotType)) {
1227         ANSR_LOGE("Failed to read slotType");
1228         return false;
1229     }
1230     slotType_ = static_cast<NotificationConstant::SlotType>(slotType);
1231 
1232     // read map
1233     uint64_t buttonMapSize = 0;
1234     if (!parcel.ReadUint64(buttonMapSize)) {
1235         ANSR_LOGE("Failed to read buttonMapSize");
1236         return false;
1237     }
1238 
1239     buttonMapSize = (buttonMapSize < MAX_ACTION_BUTTON_NUM) ? buttonMapSize : MAX_ACTION_BUTTON_NUM;
1240     for (uint64_t i = 0; i < buttonMapSize; i++) {
1241         uint8_t buttonType = static_cast<uint8_t>(ActionButtonType::INVALID);
1242         if (!parcel.ReadUint8(buttonType)) {
1243             ANSR_LOGE("Failed to read buttonType");
1244             return false;
1245         }
1246         ActionButtonType type = static_cast<ActionButtonType>(buttonType);
1247         std::string title = parcel.ReadString();
1248         std::string pkgName = parcel.ReadString();
1249         std::string abilityName = parcel.ReadString();
1250         ActionButtonInfo info;
1251         info.type = type;
1252         info.title = title;
1253         info.wantAgent = std::make_shared<ButtonWantAgent>();
1254         info.wantAgent->pkgName = pkgName;
1255         info.wantAgent->abilityName = abilityName;
1256         actionButtonMap_.insert(std::pair<ActionButtonType, ActionButtonInfo>(type, info));
1257     }
1258     if (!InitNotificationRequest()) {
1259         return false;
1260     }
1261     return true;
1262 }
1263 
InitNotificationRequest()1264 bool ReminderRequest::InitNotificationRequest()
1265 {
1266     ANSR_LOGI("Init notification");
1267     notificationRequest_ = new (std::nothrow) NotificationRequest(notificationId_);
1268     if (notificationRequest_ == nullptr) {
1269         ANSR_LOGE("Failed to create notification.");
1270         return false;
1271     }
1272     displayContent_ = content_;
1273     AddActionButtons(true);
1274     return true;
1275 }
1276 
InitServerObj()1277 void ReminderRequest::InitServerObj()
1278 {
1279     wantAgentInfo_ = wantAgentInfo_ == nullptr ? std::make_shared<WantAgentInfo>() : wantAgentInfo_;
1280     maxScreenWantAgentInfo_ =
1281         maxScreenWantAgentInfo_ == nullptr ? std::make_shared<MaxScreenAgentInfo>() : maxScreenWantAgentInfo_;
1282 }
1283 
IsAlerting() const1284 bool ReminderRequest::IsAlerting() const
1285 {
1286     return (state_ & REMINDER_STATUS_ALERTING) != 0;
1287 }
1288 
GetDurationSinceEpochInMilli(const time_t target)1289 uint64_t ReminderRequest::GetDurationSinceEpochInMilli(const time_t target)
1290 {
1291     auto tarEndTimePoint = std::chrono::system_clock::from_time_t(target);
1292     auto tarDuration = std::chrono::duration_cast<std::chrono::milliseconds>(tarEndTimePoint.time_since_epoch());
1293     int64_t tarDate = tarDuration.count();
1294     if (tarDate < 0) {
1295         ANSR_LOGW("tarDuration is less than 0.");
1296         return INVALID_LONG_LONG_VALUE;
1297     }
1298     return static_cast<uint64_t>(tarDate);
1299 }
1300 
GetDateTimeInfo(const time_t & timeInSecond) const1301 std::string ReminderRequest::GetDateTimeInfo(const time_t &timeInSecond) const
1302 {
1303     return GetTimeInfoInner(timeInSecond, TimeFormat::YMDHMS, true);
1304 }
1305 
GetButtonInfo() const1306 std::string ReminderRequest::GetButtonInfo() const
1307 {
1308     std::string info = "";
1309     bool isFirst = true;
1310     for (auto button : actionButtonMap_) {
1311         if (!isFirst) {
1312             info += SEP_BUTTON_MULTI;
1313         }
1314         ActionButtonInfo buttonInfo = button.second;
1315         info += std::to_string(static_cast<uint8_t>(button.first)) + SEP_BUTTON_SINGLE + buttonInfo.title;
1316         if (buttonInfo.wantAgent == nullptr) {
1317             continue;
1318         }
1319         info += (SEP_BUTTON_SINGLE + buttonInfo.wantAgent->pkgName + SEP_BUTTON_SINGLE
1320             + buttonInfo.wantAgent->abilityName);
1321         isFirst = false;
1322     }
1323     return info;
1324 }
1325 
GetNowInstantMilli() const1326 uint64_t ReminderRequest::GetNowInstantMilli() const
1327 {
1328     time_t now;
1329     (void)time(&now);  // unit is seconds.
1330     if (static_cast<int64_t>(now) < 0) {
1331         ANSR_LOGE("Get now time error");
1332         return 0;
1333     }
1334     return GetDurationSinceEpochInMilli(now);
1335 }
1336 
GetShowTime(const uint64_t showTime) const1337 std::string ReminderRequest::GetShowTime(const uint64_t showTime) const
1338 {
1339     if (reminderType_ == ReminderType::TIMER) {
1340         return "";
1341     }
1342     return GetTimeInfoInner(static_cast<time_t>(showTime / MILLI_SECONDS), TimeFormat::HM, false);
1343 }
1344 
GetTimeInfoInner(const time_t & timeInSecond,const TimeFormat & format,bool keep24Hour) const1345 std::string ReminderRequest::GetTimeInfoInner(const time_t &timeInSecond, const TimeFormat &format,
1346     bool keep24Hour) const
1347 {
1348     const uint8_t dateTimeLen = 80;
1349     char dateTimeBuffer[dateTimeLen];
1350     struct tm timeInfo;
1351     (void)localtime_r(&timeInSecond, &timeInfo);
1352     bool is24HourClock = OHOS::Global::I18n::LocaleConfig::Is24HourClock();
1353     if (!is24HourClock && timeInfo.tm_hour > TIME_HOUR_OFFSET && !keep24Hour) {
1354         timeInfo.tm_hour -= TIME_HOUR_OFFSET;
1355     }
1356     switch (format) {
1357         case TimeFormat::YMDHMS: {
1358             (void)strftime(dateTimeBuffer, dateTimeLen, "%Y-%m-%d %H:%M:%S", &timeInfo);
1359             break;
1360         }
1361         case TimeFormat::HM: {
1362             (void)strftime(dateTimeBuffer, dateTimeLen, "%H:%M", &timeInfo);
1363             break;
1364         }
1365         default: {
1366             ANSR_LOGW("Time format not support.");
1367             break;
1368         }
1369     }
1370     std::string dateTimeInfo(dateTimeBuffer);
1371     return dateTimeInfo;
1372 }
1373 
GetState(const uint8_t state) const1374 std::string ReminderRequest::GetState(const uint8_t state) const
1375 {
1376     std::string stateInfo = "'";
1377     if (state == REMINDER_STATUS_INACTIVE) {
1378         stateInfo += "Inactive";
1379     } else {
1380         bool hasSeparator = false;
1381         if ((state & REMINDER_STATUS_ACTIVE) != 0) {
1382             stateInfo += "Active";
1383             hasSeparator = true;
1384         }
1385         if ((state & REMINDER_STATUS_ALERTING) != 0) {
1386             if (hasSeparator) {
1387                 stateInfo += ",";
1388             }
1389             stateInfo += "Alerting";
1390             hasSeparator = true;
1391         }
1392         if ((state & REMINDER_STATUS_SHOWING) != 0) {
1393             if (hasSeparator) {
1394                 stateInfo += ",";
1395             }
1396             stateInfo += "Showing";
1397             hasSeparator = true;
1398         }
1399         if ((state & REMINDER_STATUS_SNOOZE) != 0) {
1400             if (hasSeparator) {
1401                 stateInfo += ",";
1402             }
1403             stateInfo += "Snooze";
1404         }
1405     }
1406     stateInfo += "'";
1407     return stateInfo;
1408 }
1409 
AddActionButtons(const bool includeSnooze)1410 void ReminderRequest::AddActionButtons(const bool includeSnooze)
1411 {
1412     int32_t requestCode = 10;
1413     std::vector<AbilityRuntime::WantAgent::WantAgentConstant::Flags> flags;
1414     flags.push_back(AbilityRuntime::WantAgent::WantAgentConstant::Flags::UPDATE_PRESENT_FLAG);
1415     for (auto button : actionButtonMap_) {
1416         auto want = std::make_shared<OHOS::AAFwk::Want>();
1417         auto type = button.first;
1418         switch (type) {
1419             case ActionButtonType::CLOSE:
1420                 want->SetAction(REMINDER_EVENT_CLOSE_ALERT);
1421                 break;
1422             case ActionButtonType::SNOOZE:
1423                 if (includeSnooze) {
1424                     want->SetAction(REMINDER_EVENT_SNOOZE_ALERT);
1425                 } else {
1426                     ANSR_LOGD("Not add action button, type is snooze, as includeSnooze is false");
1427                     continue;
1428                 }
1429                 break;
1430             case ActionButtonType::CUSTOM:
1431                 want->SetAction(REMINDER_EVENT_CUSTOM_ALERT);
1432                 if (button.second.wantAgent == nullptr) {
1433                     return;
1434                 }
1435                 want->SetParam("PkgName", button.second.wantAgent->pkgName);
1436                 want->SetParam("AbilityName", button.second.wantAgent->abilityName);
1437                 break;
1438             default:
1439                 break;
1440         }
1441         want->SetParam(PARAM_REMINDER_ID, reminderId_);
1442         std::vector<std::shared_ptr<AAFwk::Want>> wants;
1443         wants.push_back(want);
1444         auto title = static_cast<std::string>(button.second.title);
1445         AbilityRuntime::WantAgent::WantAgentInfo buttonWantAgentInfo(
1446             requestCode,
1447             AbilityRuntime::WantAgent::WantAgentConstant::OperationType::SEND_COMMON_EVENT,
1448             flags,
1449             wants,
1450             nullptr
1451         );
1452 
1453         std::string identity = IPCSkeleton::ResetCallingIdentity();
1454         std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> buttonWantAgent =
1455             AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(buttonWantAgentInfo, userId_);
1456         IPCSkeleton::SetCallingIdentity(identity);
1457 
1458         std::shared_ptr<NotificationActionButton> actionButton
1459             = NotificationActionButton::Create(nullptr, title, buttonWantAgent);
1460         notificationRequest_->AddActionButton(actionButton);
1461     }
1462 }
1463 
AddRemovalWantAgent()1464 void ReminderRequest::AddRemovalWantAgent()
1465 {
1466     int32_t requestCode = 10;
1467     std::vector<AbilityRuntime::WantAgent::WantAgentConstant::Flags> flags;
1468     flags.push_back(AbilityRuntime::WantAgent::WantAgentConstant::Flags::UPDATE_PRESENT_FLAG);
1469     auto want = std::make_shared<OHOS::AAFwk::Want>();
1470     want->SetAction(REMINDER_EVENT_REMOVE_NOTIFICATION);
1471     want->SetParam(PARAM_REMINDER_ID, reminderId_);
1472     std::vector<std::shared_ptr<AAFwk::Want>> wants;
1473     wants.push_back(want);
1474     AbilityRuntime::WantAgent::WantAgentInfo wantAgentInfo(
1475         requestCode,
1476         AbilityRuntime::WantAgent::WantAgentConstant::OperationType::SEND_COMMON_EVENT,
1477         flags,
1478         wants,
1479         nullptr
1480     );
1481 
1482     std::string identity = IPCSkeleton::ResetCallingIdentity();
1483     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent =
1484         AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(wantAgentInfo, userId_);
1485     IPCSkeleton::SetCallingIdentity(identity);
1486 
1487     notificationRequest_->SetRemovalWantAgent(wantAgent);
1488 }
1489 
CreateWantAgent(AppExecFwk::ElementName & element) const1490 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> ReminderRequest::CreateWantAgent(
1491     AppExecFwk::ElementName &element) const
1492 {
1493     int32_t requestCode = 10;
1494     std::vector<AbilityRuntime::WantAgent::WantAgentConstant::Flags> flags;
1495     flags.push_back(AbilityRuntime::WantAgent::WantAgentConstant::Flags::UPDATE_PRESENT_FLAG);
1496     auto want = std::make_shared<OHOS::AAFwk::Want>();
1497     want->SetElement(element);
1498     std::vector<std::shared_ptr<AAFwk::Want>> wants;
1499     wants.push_back(want);
1500     AbilityRuntime::WantAgent::WantAgentInfo wantAgentInfo(
1501         requestCode,
1502         AbilityRuntime::WantAgent::WantAgentConstant::OperationType::START_ABILITY,
1503         flags,
1504         wants,
1505         nullptr
1506     );
1507     std::string identity = IPCSkeleton::ResetCallingIdentity();
1508     auto wantAgent = AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(wantAgentInfo, userId_);
1509     IPCSkeleton::SetCallingIdentity(identity);
1510     return wantAgent;
1511 }
1512 
SetMaxScreenWantAgent(AppExecFwk::ElementName & element)1513 void ReminderRequest::SetMaxScreenWantAgent(AppExecFwk::ElementName &element)
1514 {
1515     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent = CreateWantAgent(element);
1516     notificationRequest_->SetMaxScreenWantAgent(wantAgent);
1517 }
1518 
SetWantAgent(AppExecFwk::ElementName & element)1519 void ReminderRequest::SetWantAgent(AppExecFwk::ElementName &element)
1520 {
1521     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent = CreateWantAgent(element);
1522     notificationRequest_->SetWantAgent(wantAgent);
1523 }
1524 
SetState(bool deSet,const uint8_t newState,std::string function)1525 void ReminderRequest::SetState(bool deSet, const uint8_t newState, std::string function)
1526 {
1527     uint8_t oldState = state_;
1528     if (deSet) {
1529         state_ |= newState;
1530     } else {
1531         state_ &= static_cast<uint8_t>(~newState);
1532     }
1533     ANSR_LOGI("Switch the reminder(reminderId=%{public}d) state, from %{public}s to %{public}s, called by %{public}s",
1534         reminderId_, GetState(oldState).c_str(), GetState(state_).c_str(), function.c_str());
1535 }
1536 
UpdateActionButtons(const bool & setSnooze)1537 void ReminderRequest::UpdateActionButtons(const bool &setSnooze)
1538 {
1539     if (notificationRequest_ == nullptr) {
1540         ANSR_LOGE("updateActionButtons failed, the notificationRequest is null");
1541         return;
1542     }
1543     notificationRequest_->ClearActionButtons();
1544     if (setSnooze) {
1545         AddActionButtons(false);
1546     } else {
1547         AddActionButtons(true);
1548     }
1549 }
1550 
UpdateNextReminder(const bool & force)1551 bool ReminderRequest::UpdateNextReminder(const bool &force)
1552 {
1553     bool result = true;
1554     if (force) {
1555         uint64_t nowInstantMilli = GetNowInstantMilli();
1556         if (nowInstantMilli == 0) {
1557             result = false;
1558         } else {
1559             triggerTimeInMilli_ = nowInstantMilli + timeIntervalInMilli_;
1560             snoozeTimesDynamic_ = snoozeTimes_;
1561             if (timeIntervalInMilli_ != 0) {
1562                 isExpired_ = false;
1563             }
1564         }
1565     } else {
1566         result = UpdateNextReminder();
1567     }
1568     std::string info = result ? "success" : "no next";
1569     ANSR_LOGI("updateNextReminder(id=%{public}d, %{public}s): force=%{public}d, trigger time is: %{public}s",
1570         reminderId_, info.c_str(), force,
1571         GetDateTimeInfo(static_cast<time_t>(triggerTimeInMilli_ / MILLI_SECONDS)).c_str());
1572     return result;
1573 }
1574 
UpdateNotificationCommon()1575 void ReminderRequest::UpdateNotificationCommon()
1576 {
1577     time_t now;
1578     (void)time(&now);  // unit is seconds.
1579     notificationRequest_->SetDeliveryTime(GetDurationSinceEpochInMilli(now));
1580     notificationRequest_->SetLabel(NOTIFICATION_LABEL);
1581     notificationRequest_->SetShowDeliveryTime(true);
1582     notificationRequest_->SetSlotType(slotType_);
1583     notificationRequest_->SetTapDismissed(tapDismissed_);
1584     notificationRequest_->SetAutoDeletedTime(autoDeletedTime_);
1585     auto notificationNormalContent = std::make_shared<NotificationNormalContent>();
1586     notificationNormalContent->SetText(displayContent_);
1587     notificationNormalContent->SetTitle(title_);
1588     auto notificationContent = std::make_shared<NotificationContent>(notificationNormalContent);
1589     notificationRequest_->SetContent(notificationContent);
1590     if ((reminderType_ == ReminderRequest::ReminderType::TIMER) ||
1591         (reminderType_ == ReminderRequest::ReminderType::ALARM)) {
1592         notificationRequest_->SetUnremovable(true);
1593     }
1594     auto flags = std::make_shared<NotificationFlags>();
1595     flags->SetSoundEnabled(NotificationConstant::FlagStatus::CLOSE);
1596     flags->SetVibrationEnabled(NotificationConstant::FlagStatus::CLOSE);
1597     notificationRequest_->SetFlags(flags);
1598 }
1599 
UpdateNotificationBundleInfo()1600 void ReminderRequest::UpdateNotificationBundleInfo()
1601 {
1602     std::string ownerBundleName = notificationRequest_->GetOwnerBundleName();
1603     if (!(ownerBundleName.empty())) {
1604         return;
1605     }
1606     ANSR_LOGD("ownerBundleName=%{public}s, bundleName_=%{public}s",
1607         ownerBundleName.c_str(), bundleName_.c_str());
1608     notificationRequest_->SetOwnerBundleName(bundleName_);
1609     notificationRequest_->SetCreatorBundleName(bundleName_);
1610     notificationRequest_->SetCreatorUid(uid_);
1611     ErrCode errCode = AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid_, userId_);
1612     if (errCode != ERR_OK) {
1613         ANSR_LOGE("GetOsAccountLocalIdFromUid fail.");
1614         return;
1615     }
1616     notificationRequest_->SetCreatorUserId(userId_);
1617 }
1618 
UpdateNotificationContent(const bool & setSnooze)1619 void ReminderRequest::UpdateNotificationContent(const bool &setSnooze)
1620 {
1621     if (notificationRequest_ == nullptr) {
1622         ANSR_LOGE("updateNotificationContent failed, the notificationRequest is null");
1623         return;
1624     }
1625     std::string extendContent = "";
1626     if (setSnooze) {
1627         if (timeIntervalInMilli_ != 0) {
1628             // snooze the reminder by manual
1629             extendContent = GetShowTime(triggerTimeInMilli_) +
1630                 (snoozeContent_ == "" ? "" : (" (" + snoozeContent_ + ")"));
1631             notificationRequest_->SetTapDismissed(false);
1632         } else {
1633             // the reminder is expired now, when timeInterval is 0
1634             extendContent = GetShowTime(reminderTimeInMilli_) +
1635                 (expiredContent_ == "" ? "" : (" (" + expiredContent_ + ")"));
1636         }
1637     } else if (IsAlerting()) {
1638         // the reminder is alerting, or ring duration is 0
1639         extendContent = GetShowTime(reminderTimeInMilli_);
1640     } else if (snoozeTimesDynamic_ != snoozeTimes_) {
1641         // the reminder is snoozing by period artithmetic, when the ring duration is over.
1642         extendContent = GetShowTime(triggerTimeInMilli_) +
1643             (snoozeContent_ == "" ? "" : (" (" + snoozeContent_ + ")"));
1644         notificationRequest_->SetTapDismissed(false);
1645     } else {
1646         // the reminder has already snoozed by period arithmetic, when the ring duration is over.
1647         extendContent = GetShowTime(reminderTimeInMilli_) +
1648             (expiredContent_ == "" ? "" : (" (" + expiredContent_ + ")"));
1649     }
1650     displayContent_ = content_ + " " + extendContent;
1651     ANSR_LOGD("Display content=%{public}s", displayContent_.c_str());
1652 }
1653 
UpdateNotificationStateForAlert()1654 void ReminderRequest::UpdateNotificationStateForAlert()
1655 {
1656     ANSR_LOGD("UpdateNotification content and buttons");
1657     UpdateNotificationContent(false);
1658     UpdateActionButtons(false);
1659 }
1660 
UpdateNotificationStateForSnooze()1661 void ReminderRequest::UpdateNotificationStateForSnooze()
1662 {
1663     ANSR_LOGD("UpdateNotification content and buttons");
1664     UpdateNotificationContent(true);
1665     UpdateActionButtons(true);
1666 }
1667 
GetActualTime(const TimeTransferType & type,int32_t cTime)1668 int32_t ReminderRequest::GetActualTime(const TimeTransferType &type, int32_t cTime)
1669 {
1670     switch (type) {
1671         case (TimeTransferType::YEAR):  // year
1672             return BASE_YEAR + cTime;
1673         case (TimeTransferType::MONTH):  // month
1674             return 1 + cTime;
1675         case (TimeTransferType::WEEK): {  // week
1676             return cTime == 0 ? SUNDAY : cTime;
1677         }
1678         default:
1679             return -1;
1680     }
1681 }
1682 
GetCTime(const TimeTransferType & type,int32_t actualTime)1683 int32_t ReminderRequest::GetCTime(const TimeTransferType &type, int32_t actualTime)
1684 {
1685     switch (type) {
1686         case (TimeTransferType::YEAR):  // year
1687             return actualTime - BASE_YEAR;
1688         case (TimeTransferType::MONTH):  // month
1689             return actualTime - 1;
1690         case (TimeTransferType::WEEK): {  // week
1691             return actualTime == SUNDAY ? 0 : actualTime;
1692         }
1693         default:
1694             return -1;
1695     }
1696 }
1697 
GetUid(const int32_t & userId,const std::string & bundleName)1698 int32_t ReminderRequest::GetUid(const int32_t &userId, const std::string &bundleName)
1699 {
1700     AppExecFwk::ApplicationInfo info;
1701     sptr<ISystemAbilityManager> systemAbilityManager
1702         = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1703     if (systemAbilityManager == nullptr) {
1704         ANSR_LOGE("Failed to get uid due to get systemAbilityManager is null.");
1705         return -1;
1706     }
1707     sptr<IRemoteObject> remoteObject  = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
1708     if (remoteObject == nullptr) {
1709         ANSR_LOGE("Fail to get bundle manager proxy");
1710         return -1;
1711     }
1712     sptr<AppExecFwk::IBundleMgr> bundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
1713     if (bundleMgr == nullptr) {
1714         ANSR_LOGE("Bundle mgr proxy is nullptr");
1715         return -1;
1716     }
1717     bundleMgr->GetApplicationInfo(bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, info);
1718     ANSR_LOGD("uid=%{public}d", info.uid);
1719     return static_cast<int32_t>(info.uid);
1720 }
1721 
GetUserId(const int32_t & uid)1722 int32_t ReminderRequest::GetUserId(const int32_t &uid)
1723 {
1724     int32_t userId = -1;
1725     AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
1726     ANSR_LOGD("userId=%{public}d", userId);
1727     return userId;
1728 }
1729 
AppendValuesBucket(const sptr<ReminderRequest> & reminder,const sptr<NotificationBundleOption> & bundleOption,NativeRdb::ValuesBucket & values)1730 void ReminderRequest::AppendValuesBucket(const sptr<ReminderRequest> &reminder,
1731     const sptr<NotificationBundleOption> &bundleOption, NativeRdb::ValuesBucket &values)
1732 {
1733     values.PutInt(REMINDER_ID, reminder->GetReminderId());
1734     values.PutString(PKG_NAME, bundleOption->GetBundleName());
1735     values.PutInt(USER_ID, reminder->GetUserId());
1736     values.PutInt(UID, reminder->GetUid());
1737     values.PutString(SYS_APP, reminder->IsSystemApp() ? "true" : "false");
1738     values.PutString(APP_LABEL, "");  // no use, compatible with old version.
1739     values.PutInt(REMINDER_TYPE, static_cast<int32_t>(reminder->GetReminderType()));
1740     values.PutLong(REMINDER_TIME, reminder->GetReminderTimeInMilli());
1741     values.PutLong(TRIGGER_TIME, reminder->GetTriggerTimeInMilli());
1742     values.PutLong(
1743         RTC_TRIGGER_TIME, reminder->GetTriggerTimeInMilli());  // no use, compatible with old version.
1744     values.PutLong(TIME_INTERVAL, reminder->GetTimeInterval());
1745     values.PutInt(SNOOZE_TIMES, reminder->GetSnoozeTimes());
1746     values.PutInt(DYNAMIC_SNOOZE_TIMES, reminder->GetSnoozeTimesDynamic());
1747     values.PutLong(RING_DURATION, reminder->GetRingDuration());
1748     values.PutString(IS_EXPIRED, reminder->IsExpired() ? "true" : "false");
1749     values.PutString(IS_ACTIVE, "");  // no use, compatible with old version.
1750     values.PutInt(STATE, reminder->GetState());
1751     values.PutString(ZONE_ID, "");  // no use, compatible with old version.
1752     values.PutString(HAS_SCHEDULED_TIMEOUT, "");  // no use, compatible with old version.
1753     values.PutString(ACTION_BUTTON_INFO, reminder->GetButtonInfo());
1754     values.PutString(CUSTOM_BUTTON_URI, reminder->GetCustomButtonUri());
1755     values.PutInt(SLOT_ID, reminder->GetSlotType());
1756     values.PutInt(NOTIFICATION_ID, reminder->GetNotificationId());
1757     values.PutString(TITLE, reminder->GetTitle());
1758     values.PutString(CONTENT, reminder->GetContent());
1759     values.PutString(SNOOZE_CONTENT, reminder->GetSnoozeContent());
1760     values.PutString(EXPIRED_CONTENT, reminder->GetExpiredContent());
1761     auto wantAgentInfo = reminder->GetWantAgentInfo();
1762     if (wantAgentInfo == nullptr) {
1763         std::string info = "null" + ReminderRequest::SEP_WANT_AGENT + "null";
1764         values.PutString(AGENT, info);
1765     } else {
1766         values.PutString(AGENT, wantAgentInfo->pkgName
1767             + ReminderRequest::SEP_WANT_AGENT + wantAgentInfo->abilityName);
1768     }
1769     auto maxScreenWantAgentInfo = reminder->GetMaxScreenWantAgentInfo();
1770     if (maxScreenWantAgentInfo == nullptr) {
1771         std::string info = "null" + ReminderRequest::SEP_WANT_AGENT + "null";
1772         values.PutString(MAX_SCREEN_AGENT, info);
1773     } else {
1774         values.PutString(MAX_SCREEN_AGENT, maxScreenWantAgentInfo->pkgName
1775             + ReminderRequest::SEP_WANT_AGENT + maxScreenWantAgentInfo->abilityName);
1776     }
1777     values.PutString(TAP_DISMISSED, reminder->IsTapDismissed() ? "true" : "false");
1778     values.PutLong(AUTO_DELETED_TIME, reminder->GetAutoDeletedTime());
1779 }
1780 
InitDbColumns()1781 void ReminderRequest::InitDbColumns()
1782 {
1783     AddColumn(REMINDER_ID, "INTEGER PRIMARY KEY", false);
1784     AddColumn(PKG_NAME, "TEXT NOT NULL", false);
1785     AddColumn(USER_ID, "INT NOT NULL", false);
1786     AddColumn(UID, "INT NOT NULL", false);
1787     AddColumn(SYS_APP, "TEXT NOT NULL", false);
1788     AddColumn(APP_LABEL, "TEXT", false);
1789     AddColumn(REMINDER_TYPE, "INT NOT NULL", false);
1790     AddColumn(REMINDER_TIME, "BIGINT NOT NULL", false);
1791     AddColumn(TRIGGER_TIME, "BIGINT NOT NULL", false);
1792     AddColumn(RTC_TRIGGER_TIME, "BIGINT NOT NULL", false);
1793     AddColumn(TIME_INTERVAL, "BIGINT NOT NULL", false);
1794     AddColumn(SNOOZE_TIMES, "INT NOT NULL", false);
1795     AddColumn(DYNAMIC_SNOOZE_TIMES, "INT NOT NULL", false);
1796     AddColumn(RING_DURATION, "BIGINT NOT NULL", false);
1797     AddColumn(IS_EXPIRED, "TEXT NOT NULL", false);
1798     AddColumn(IS_ACTIVE, "TEXT NOT NULL", false);
1799     AddColumn(STATE, "INT NOT NULL", false);
1800     AddColumn(ZONE_ID, "TEXT", false);
1801     AddColumn(HAS_SCHEDULED_TIMEOUT, "TEXT", false);
1802     AddColumn(ACTION_BUTTON_INFO, "TEXT", false);
1803     AddColumn(CUSTOM_BUTTON_URI, "TEXT", false);
1804     AddColumn(SLOT_ID, "INT", false);
1805     AddColumn(NOTIFICATION_ID, "INT NOT NULL", false);
1806     AddColumn(TITLE, "TEXT", false);
1807     AddColumn(CONTENT, "TEXT", false);
1808     AddColumn(SNOOZE_CONTENT, "TEXT", false);
1809     AddColumn(EXPIRED_CONTENT, "TEXT", false);
1810     AddColumn(AGENT, "TEXT", false);
1811     AddColumn(MAX_SCREEN_AGENT, "TEXT", false);
1812     AddColumn(TAP_DISMISSED, "TEXT", false);
1813     AddColumn(AUTO_DELETED_TIME, "BIGINT", false);
1814 }
1815 
AddColumn(const std::string & name,const std::string & type,const bool & isEnd)1816 void ReminderRequest::AddColumn(
1817     const std::string &name, const std::string &type, const bool &isEnd)
1818 {
1819     columns.push_back(name);
1820     if (!isEnd) {
1821         sqlOfAddColumns += name + " " + type + ", ";
1822     } else {
1823         sqlOfAddColumns += name + " " + type;
1824     }
1825 }
1826 }
1827 }