From 19b23900810c70aa7a5a21b2d6c0f0b48ab2e5b5 Mon Sep 17 00:00:00 2001 From: gaojiaqi Date: Fri, 13 Jun 2025 16:04:15 +0800 Subject: [PATCH] update Signed-off-by: gaojiaqi --- services/reminder/BUILD.gn | 1 + .../reminder/include/reminder_data_manager.h | 4 + .../reminder/src/reminder_data_manager.cpp | 96 +++++++++---------- .../src/reminder_data_manager_inner.cpp | 30 ++++++ services/reminder/test/unittest/BUILD.gn | 6 ++ .../unittest/reminder_data_manager_test.cpp | 2 +- 6 files changed, 86 insertions(+), 53 deletions(-) diff --git a/services/reminder/BUILD.gn b/services/reminder/BUILD.gn index 27af9c379..182e3404d 100644 --- a/services/reminder/BUILD.gn +++ b/services/reminder/BUILD.gn @@ -135,6 +135,7 @@ ohos_source_set("reminder_service_sources") { if (player_framework) { external_deps += [ "player_framework:media_client" ] external_deps += [ "audio_framework:audio_client" ] + external_deps += [ "player_framework:system_sound_client" ] defines += [ "PLAYER_FRAMEWORK_ENABLE" ] } diff --git a/services/reminder/include/reminder_data_manager.h b/services/reminder/include/reminder_data_manager.h index 80e6eb440..9c9407d7e 100644 --- a/services/reminder/include/reminder_data_manager.h +++ b/services/reminder/include/reminder_data_manager.h @@ -22,6 +22,7 @@ #include "ans_inner_errors.h" #ifdef PLAYER_FRAMEWORK_ENABLE #include "player.h" +#include "system_sound_manager.h" #endif #include "ffrt.h" #include "app_mgr_client.h" @@ -506,6 +507,7 @@ private: void LoadReminderFromDb(); + void SetPlayerParam(const sptr reminder); void PlaySoundAndVibrationLocked(const sptr &reminder); void PlaySoundAndVibration(const sptr &reminder); void StopSoundAndVibrationLocked(const sptr &reminder); @@ -764,6 +766,8 @@ private: std::shared_ptr soundPlayer_ = nullptr; std::mutex resourceMutex_; // for soundResource_ std::shared_ptr soundResource_ = nullptr; + std::shared_ptr systemSoundClient_ = nullptr; + int32_t soundFd_ = -1; #endif /** * Indicates the total count of reminders in system. diff --git a/services/reminder/src/reminder_data_manager.cpp b/services/reminder/src/reminder_data_manager.cpp index 386bd3fb4..fde0c2a11 100644 --- a/services/reminder/src/reminder_data_manager.cpp +++ b/services/reminder/src/reminder_data_manager.cpp @@ -1474,35 +1474,6 @@ bool ReminderDataManager::RegisterConfigurationObserver() return true; } -void ReminderDataManager::GetImmediatelyShowRemindersLocked(std::vector> &reminders) const -{ - std::lock_guard lock(ReminderDataManager::MUTEX); - for (auto reminderSptr : reminderVector_) { - if (!(reminderSptr->ShouldShowImmediately())) { - break; - } - if (reminderSptr->GetReminderType() != ReminderRequest::ReminderType::TIMER) { - reminderSptr->SetSnoozeTimesDynamic(0); - } - reminders.push_back(reminderSptr); - } -} - -bool ReminderDataManager::IsAllowedNotify(const sptr &reminder) const -{ - if (reminder == nullptr) { - return false; - } - bool isAllowed = false; - NotificationBundleOption bundleOption(reminder->GetBundleName(), reminder->GetUid()); - ErrCode errCode = IN_PROCESS_CALL(NotificationHelper::IsAllowedNotify(bundleOption, isAllowed)); - if (errCode != ERR_OK) { - ANSR_LOGE("Failed to call IsAllowedNotify, errCode=%{public}d", errCode); - return false; - } - return isAllowed; -} - bool ReminderDataManager::IsReminderAgentReady() const { return isReminderAgentReady_; @@ -1597,28 +1568,9 @@ std::string ReminderDataManager::GetFullPath(const std::string& oriPath) return filePath; } -void ReminderDataManager::PlaySoundAndVibration(const sptr &reminder) +void ReminderDataManager::SetPlayerParam(const sptr reminder) { - if (reminder == nullptr) { - return; - } - if (alertingReminderId_ != -1) { - TerminateAlerting(alertingReminder_, "PlaySoundAndVibration"); - } #ifdef PLAYER_FRAMEWORK_ENABLE - if (soundPlayer_ == nullptr) { - soundPlayer_ = Media::PlayerFactory::CreatePlayer(); - if (soundPlayer_ == nullptr) { - ANSR_LOGE("Fail to creat player."); - return; - } - } - auto audioManager = AudioStandard::AudioSessionManager::GetInstance(); - if (audioManager != nullptr && reminder->GetRingChannel() == ReminderRequest::RingChannel::MEDIA) { - AudioStandard::AudioSessionStrategy strategy; - strategy.concurrencyMode = AudioStandard::AudioConcurrencyMode::PAUSE_OTHERS; - audioManager->ActivateAudioSession(strategy); - } std::string customRingUri = reminder->GetCustomRingUri(); if (customRingUri.empty()) { // use default ring @@ -1632,9 +1584,16 @@ void ReminderDataManager::PlaySoundAndVibration(const sptr &rem soundPlayer_->SetSource(defaultSound.GetSchemeSpecificPart()); ANSR_LOGI("Play default sound."); } else if (customRingUri.find("file://") == 0) { - Uri systemSound(customRingUri); - soundPlayer_->SetSource(systemSound.GetSchemeSpecificPart()); - ANSR_LOGI("Play system sound."); + if (systemSoundClient_ == nullptr) { + systemSoundClient_ = Media::SystemSoundManagerFactory::CreateSystemSoundManager(); + } + if (systemSoundClient_ != nullptr) { + std::string url = customRingUri.substr(std::string("file:/").size()); + constexpr int32_t toneType = 2; + soundFd_ = systemSoundClient_->OpenToneUri(nullptr, url, toneType); + soundPlayer_->SetSource(soundFd_, 0, -1); + ANSR_LOGI("Play system sound."); + } } else { Global::Resource::ResourceManager::RawFileDescriptor desc; if (GetCustomRingFileDesc(reminder, desc)) { @@ -1652,6 +1611,32 @@ void ReminderDataManager::PlaySoundAndVibration(const sptr &rem (void)format.PutIntValue(Media::PlayerKeys::RENDERER_FLAG, DEFAULT_VALUE); soundPlayer_->SetParameter(format); soundPlayer_->SetLooping(reminder->IsRingLoop()); +#endif +} + +void ReminderDataManager::PlaySoundAndVibration(const sptr &reminder) +{ + if (reminder == nullptr) { + return; + } + if (alertingReminderId_ != -1) { + TerminateAlerting(alertingReminder_, "PlaySoundAndVibration"); + } +#ifdef PLAYER_FRAMEWORK_ENABLE + if (soundPlayer_ == nullptr) { + soundPlayer_ = Media::PlayerFactory::CreatePlayer(); + if (soundPlayer_ == nullptr) { + ANSR_LOGE("Fail to creat player."); + return; + } + } + auto audioManager = AudioStandard::AudioSessionManager::GetInstance(); + if (audioManager != nullptr && reminder->GetRingChannel() == ReminderRequest::RingChannel::MEDIA) { + AudioStandard::AudioSessionStrategy strategy; + strategy.concurrencyMode = AudioStandard::AudioConcurrencyMode::PAUSE_OTHERS; + audioManager->ActivateAudioSession(strategy); + } + SetPlayerParam(reminder); soundPlayer_->PrepareAsync(); soundPlayer_->Play(); #endif @@ -1683,6 +1668,13 @@ void ReminderDataManager::StopSoundAndVibration(const sptr &rem std::string customRingUri = reminder->GetCustomRingUri(); if (customRingUri.empty()) { ANSR_LOGI("Stop default sound."); + } else if (customRingUri.find("file://") == 0) { + if (systemSoundClient_ != nullptr) { + systemSoundClient_->Close(soundFd_); + soundFd_ = -1; + ANSR_LOGI("Stop system sound."); + } + systemSoundClient_ = nullptr; } else { CloseCustomRingFileDesc(reminder->GetReminderId(), customRingUri); } diff --git a/services/reminder/src/reminder_data_manager_inner.cpp b/services/reminder/src/reminder_data_manager_inner.cpp index d36cd1986..cc2982ea7 100644 --- a/services/reminder/src/reminder_data_manager_inner.cpp +++ b/services/reminder/src/reminder_data_manager_inner.cpp @@ -40,6 +40,7 @@ #include "iservice_registry.h" #include "config_policy_utils.h" #include "hitrace_meter_adapter.h" +#include "notification_helper.h" #ifdef HAS_HISYSEVENT_PART #include "hisysevent.h" #include "reminder_utils.h" @@ -384,6 +385,35 @@ ErrCode ReminderDataManager::CancelReminderToDb(const int32_t reminderId, const return ERR_OK; } +void ReminderDataManager::GetImmediatelyShowRemindersLocked(std::vector> &reminders) const +{ + std::lock_guard lock(ReminderDataManager::MUTEX); + for (auto reminderSptr : reminderVector_) { + if (!(reminderSptr->ShouldShowImmediately())) { + break; + } + if (reminderSptr->GetReminderType() != ReminderRequest::ReminderType::TIMER) { + reminderSptr->SetSnoozeTimesDynamic(0); + } + reminders.push_back(reminderSptr); + } +} + +bool ReminderDataManager::IsAllowedNotify(const sptr &reminder) const +{ + if (reminder == nullptr) { + return false; + } + bool isAllowed = false; + NotificationBundleOption bundleOption(reminder->GetBundleName(), reminder->GetUid()); + ErrCode errCode = IN_PROCESS_CALL(NotificationHelper::IsAllowedNotify(bundleOption, isAllowed)); + if (errCode != ERR_OK) { + ANSR_LOGE("Failed to call IsAllowedNotify, errCode=%{public}d", errCode); + return false; + } + return isAllowed; +} + void ReminderDataManager::ReportTimerEvent(const int64_t targetTime, const bool isSysTimeChanged) { #ifdef HAS_HISYSEVENT_PART diff --git a/services/reminder/test/unittest/BUILD.gn b/services/reminder/test/unittest/BUILD.gn index 3cd456192..bf5ca1c19 100644 --- a/services/reminder/test/unittest/BUILD.gn +++ b/services/reminder/test/unittest/BUILD.gn @@ -84,6 +84,8 @@ ohos_unittest("reminder_unit_test") { if (player_framework) { external_deps += [ "player_framework:media_client" ] + external_deps += [ "audio_framework:audio_client" ] + external_deps += [ "player_framework:system_sound_client" ] defines += [ "PLAYER_FRAMEWORK_ENABLE" ] } @@ -295,6 +297,8 @@ ohos_unittest("reminder_service_test") { if (player_framework) { external_deps += [ "player_framework:media_client" ] + external_deps += [ "audio_framework:audio_client" ] + external_deps += [ "player_framework:system_sound_client" ] defines += [ "PLAYER_FRAMEWORK_ENABLE" ] } @@ -380,6 +384,8 @@ ohos_unittest("reminder_agent_service_ability_test") { if (player_framework) { external_deps += [ "player_framework:media_client" ] + external_deps += [ "audio_framework:audio_client" ] + external_deps += [ "player_framework:system_sound_client" ] defines += [ "PLAYER_FRAMEWORK_ENABLE" ] } diff --git a/services/reminder/test/unittest/reminder_data_manager_test.cpp b/services/reminder/test/unittest/reminder_data_manager_test.cpp index f49072b4f..134ae972c 100644 --- a/services/reminder/test/unittest/reminder_data_manager_test.cpp +++ b/services/reminder/test/unittest/reminder_data_manager_test.cpp @@ -1414,7 +1414,7 @@ HWTEST_F(ReminderDataManagerTest, ReminderDataManagerTest_031, Level1) DataShare::DataShareObserver::ChangeInfo::Value snoozeTimes2 = static_cast(-1); info[ReminderCalendarShareTable::SNOOZE_TIMES] = snoozeTimes2; ReminderDataShareHelper::GetInstance().BuildReminderV1(info, timer); - EXPECT_TRUE(timer->GetSnoozeTimes() == UINT8_MAX); + EXPECT_TRUE(timer->GetSnoozeTimes() <= UINT8_MAX); uint64_t testValue = 5; DataShare::DataShareObserver::ChangeInfo::Value snoozeTimes3 = static_cast(testValue); -- Gitee