diff --git a/frameworks/ans/native/src/reminder_request.cpp b/frameworks/ans/native/src/reminder_request.cpp index a1d134f8dbcad17ca8b5f85383d092925b0d1166..f6ab680f167cd3b4f934980a0b7e7056a4dc967c 100644 --- a/frameworks/ans/native/src/reminder_request.cpp +++ b/frameworks/ans/native/src/reminder_request.cpp @@ -1536,6 +1536,10 @@ int32_t ReminderRequest::GetUid(const int &userId, const std::string &bundleName AppExecFwk::ApplicationInfo info; sptr systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + ANSR_LOGE("Failed to get uid due to get systemAbilityManager is null."); + return -1; + } sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); sptr bundleMgr = iface_cast(remoteObject); bundleMgr->GetApplicationInfo(bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, @@ -1639,4 +1643,4 @@ void ReminderRequest::AddColumn( } } } -} +} \ 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 e8776359a1d388ecf77362d3b2bcc3d199f0cd26..50ce312e24ba7c6aaf0851521645c188a2ede4d2 100644 --- a/frameworks/ans/native/src/reminder_request_alarm.cpp +++ b/frameworks/ans/native/src/reminder_request_alarm.cpp @@ -160,7 +160,12 @@ int8_t ReminderRequestAlarm::GetNextAlarm(const time_t now, const time_t target) if (repeatDays_ == 0) { return INVALID_INT_VALUE; } - int today = GetActualTime(TimeTransferType::WEEK, gmtime(&now)->tm_wday); + tm *nowTime = gmtime(&now); + if (nowTime == nullptr) { + ANSR_LOGE("Failed to get next alarm due to gmtime return null."); + return 0; + } + int today = GetActualTime(TimeTransferType::WEEK, nowTime->tm_wday); int dayCount = now >= target ? 1 : 0; for (; dayCount <= DAYS_PER_WEEK; dayCount++) { int day = (today + dayCount) % DAYS_PER_WEEK; diff --git a/frameworks/ans/native/src/reminder_request_timer.cpp b/frameworks/ans/native/src/reminder_request_timer.cpp index 8e108f5e5436a6f3b9d6b6ab60c206e1ea3cfcf9..4b5c878143304caca833b1dd6382091f202236b7 100644 --- a/frameworks/ans/native/src/reminder_request_timer.cpp +++ b/frameworks/ans/native/src/reminder_request_timer.cpp @@ -33,11 +33,15 @@ ReminderRequestTimer::ReminderRequestTimer(uint64_t countDownTimeInSeconds) ReminderRequest::SetTriggerTimeInMilli( ReminderRequest::GetDurationSinceEpochInMilli(now) + countDownTimeInSeconds_ * ReminderRequest::MILLI_SECONDS); sptr timer = MiscServices::TimeServiceClient::GetInstance(); - int64_t bootTimeMs = timer->GetBootTimeMs(); - if (bootTimeMs >= 0) { - firstRealTimeInMilliSeconds_ = static_cast(bootTimeMs); + if (timer == nullptr) { + ANSR_LOGW("Failed to get boot time due to TimeServiceClient is null."); } else { - ANSR_LOGW("Get boot time error."); + int64_t bootTimeMs = timer->GetBootTimeMs(); + if (bootTimeMs >= 0) { + firstRealTimeInMilliSeconds_ = static_cast(bootTimeMs); + } else { + ANSR_LOGW("Get boot time error."); + } } } @@ -95,6 +99,10 @@ void ReminderRequestTimer::UpdateTimeInfo(const std::string &description) (void)time(&now); // unit is seconds. whenToChangeSysTime_ = ReminderRequest::GetDurationSinceEpochInMilli(now); sptr timer = MiscServices::TimeServiceClient::GetInstance(); + if (timer == nullptr) { + ANSR_LOGW("Failed to updateTime info due to TimeServiceClient is null."); + return; + } int64_t bootTime = timer->GetBootTimeMs(); if (bootTime < 0) { ANSR_LOGW("BootTime is illegal"); diff --git a/frameworks/ans/native/src/reminder_store.cpp b/frameworks/ans/native/src/reminder_store.cpp index 7babdc29eef8cf9801656294b8886f1c4b1de352..150c7dfce465170992f78989ea772778567a0300 100644 --- a/frameworks/ans/native/src/reminder_store.cpp +++ b/frameworks/ans/native/src/reminder_store.cpp @@ -87,6 +87,10 @@ int32_t ReminderStore::Init() int32_t ReminderStore::InitData() { ANSR_LOGD("Reminder data init."); + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return STATE_FAIL; + } std::string deleteCondition = ReminderRequest::IS_EXPIRED + " is true"; ReminderStore::Delete(deleteCondition); @@ -193,6 +197,10 @@ int64_t ReminderStore::UpdateOrInsert( int64_t ReminderStore::Insert( const sptr &reminder, const sptr &bundleOption) { + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return STATE_FAIL; + } int64_t rowId = STATE_FAIL; NativeRdb::ValuesBucket values; ReminderStore::GenerateData(reminder, bundleOption, values); @@ -209,6 +217,10 @@ int64_t ReminderStore::Insert( int64_t ReminderStore::Update( const sptr &reminder, const sptr &bundleOption) { + if (rdbStore_ == nullptr) { + ANSR_LOGE("Rdb store is not initialized."); + return STATE_FAIL; + } int32_t changedRows = STATE_FAIL; NativeRdb::ValuesBucket values; ReminderStore::GenerateData(reminder, bundleOption, values); diff --git a/interfaces/kits/napi/ans/src/reminder/reminder_common.cpp b/interfaces/kits/napi/ans/src/reminder/reminder_common.cpp index 76ed09fe0e08236cb8c7d5d2c799dc9c1b78d9f0..47ff49f06de78d1829dc5430341be874a1d6378f 100644 --- a/interfaces/kits/napi/ans/src/reminder/reminder_common.cpp +++ b/interfaces/kits/napi/ans/src/reminder/reminder_common.cpp @@ -528,4 +528,4 @@ napi_value ReminderCommon::ParseInt32Array(const napi_env &env, const napi_value return NotificationNapi::Common::NapiGetNull(env); } } -} +} \ No newline at end of file diff --git a/services/ans/include/reminder_data_manager.h b/services/ans/include/reminder_data_manager.h index bd69270eb4970ed0e6d2dc4458e34accd3595fc1..606d4a3aad4e1ce36f8432db4b6ea93e7d8cebb4 100644 --- a/services/ans/include/reminder_data_manager.h +++ b/services/ans/include/reminder_data_manager.h @@ -86,7 +86,7 @@ public: * @param[out] reminders return the valid reminders. */ void GetValidReminders( - const sptr bundleOption, std::vector> &reminders); + const sptr &bundleOption, std::vector> &reminders); /** * @brief Inits and recovery data from database. @@ -102,7 +102,7 @@ public: * * @param bundleOption Indicates the bundleOption of third party application. */ - void OnProcessDiedLocked(const sptr bundleOption); + void OnProcessDiedLocked(const sptr &bundleOption); /** * Publishs a scheduled reminder. diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index 417848546608be65a671f45915b82210f4abef12..f9b85337ecb82d3b62eab00ece143db4f366c023 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -160,7 +160,7 @@ void ReminderDataManager::CancelAllReminders(const sptr bundleOption, std::vector> &reminders) + const sptr &bundleOption, std::vector> &reminders) { std::lock_guard lock(ReminderDataManager::MUTEX); for (auto it = reminderVector_.begin(); it != reminderVector_.end(); ++it) { @@ -201,7 +201,7 @@ void ReminderDataManager::OnServiceStart() StartRecentReminder(); } -void ReminderDataManager::OnProcessDiedLocked(const sptr bundleOption) +void ReminderDataManager::OnProcessDiedLocked(const sptr &bundleOption) { std::string bundleName = bundleOption->GetBundleName(); int32_t uid = bundleOption->GetUid(); @@ -237,7 +237,7 @@ void ReminderDataManager::OnProcessDiedLocked(const sptr ReminderDataManager::CreateTimerInfo(TimerType type) const { auto sharedTimerInfo = std::make_shared(); - if (sharedTimerInfo->TIMER_TYPE_WAKEUP > UINT8_MAX || sharedTimerInfo->TIMER_TYPE_EXACT > UINT8_MAX) { + if ((sharedTimerInfo->TIMER_TYPE_WAKEUP > UINT8_MAX) || (sharedTimerInfo->TIMER_TYPE_EXACT > UINT8_MAX)) { ANSR_LOGE("Failed to set timer type."); return nullptr; } @@ -442,6 +442,10 @@ void ReminderDataManager::TerminateAlerting(const sptr &reminde } ANSR_LOGD("publish(update) notification.(reminderId=%{public}d)", reminder->GetReminderId()); UpdateNotification(reminder); + if (advancedNotificationService_ == nullptr) { + ANSR_LOGE("Ans instance is null."); + return; + } advancedNotificationService_->PublishPreparedNotification(notificationRequest, bundleOption); store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); } @@ -642,6 +646,10 @@ void ReminderDataManager::SnoozeReminderImpl(sptr &reminder) } ANSR_LOGD("publish(update) notification.(reminderId=%{public}d)", reminder->GetReminderId()); UpdateNotification(reminder); + if (advancedNotificationService_ == nullptr) { + ANSR_LOGE("Ans instance is null"); + return; + } advancedNotificationService_->PublishPreparedNotification(notificationRequest, bundleOption); StartRecentReminder(); } @@ -952,10 +960,14 @@ void ReminderDataManager::PlaySoundAndVibration(const sptr &rem std::string ReminderDataManager::GetSoundUri(const sptr &reminder) { + Uri uri = DEFAULT_NOTIFICATION_SOUND; + if (advancedNotificationService_ == nullptr) { + ANSR_LOGE("Ans instance is null."); + return uri.GetSchemeSpecificPart(); + } sptr bundle = FindNotificationBundleOption(reminder->GetReminderId()); std::vector> slots; ErrCode errCode = advancedNotificationService_->GetSlotsByBundle(bundle, slots); - Uri uri = DEFAULT_NOTIFICATION_SOUND; if (errCode != ERR_OK) { ANSR_LOGW("Get sound uri fail, use default sound instead."); return uri.GetSchemeSpecificPart(); @@ -987,9 +999,13 @@ void ReminderDataManager::StopSoundAndVibration(const sptr &rem return; } ANSR_LOGD("Stop sound and vibration, reminderId=%{public}d", reminder->GetReminderId()); - soundPlayer_->Stop(); - soundPlayer_->Release(); - soundPlayer_ = nullptr; + if (soundPlayer_ == nullptr) { + ANSR_LOGW("Sound player is null"); + } else { + soundPlayer_->Stop(); + soundPlayer_->Release(); + soundPlayer_ = nullptr; + } sptr nullReminder = nullptr; SetAlertingReminder(nullReminder); } @@ -1051,6 +1067,10 @@ void ReminderDataManager::StartTimerLocked(const sptr &reminder void ReminderDataManager::StartTimer(const sptr &reminderRequest, TimerType type) { sptr timer = MiscServices::TimeServiceClient::GetInstance(); + if (timer == nullptr) { + ANS_LOGE("Failed to start timer due to get TimeServiceClient is null."); + return; + } time_t now; (void)time(&now); // unit is seconds. if (now < 0) { @@ -1107,6 +1127,10 @@ void ReminderDataManager::StopTimerLocked(TimerType type) void ReminderDataManager::StopTimer(TimerType type) { sptr timer = MiscServices::TimeServiceClient::GetInstance(); + if (timer == nullptr) { + ANSR_LOGE("Failed to stop timer due to get TimeServiceClient is null."); + return; + } uint64_t timerId = 0; switch (type) { case TimerType::TRIGGER_TIMER: {