From b9841338c1af86895233647a7ad288a9bb441d9f Mon Sep 17 00:00:00 2001 From: xue <2144439972@qq.com> Date: Sat, 6 Dec 2025 16:34:51 +0800 Subject: [PATCH] =?utf8?q?=E4=BC=98=E5=8C=96=E6=8C=89=E9=94=AE=E6=93=8D?= =?utf8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../thunderbird/include/ThunderBirdModel.h | 3 + .../include/ThunderBirdReminderView.h | 2 +- .../thunderbird/src/ThunderBirdMainView.cpp | 1 - .../thunderbird/src/ThunderBirdModel.cpp | 54 +++++++++--------- .../thunderbird/src/ThunderBirdPresenter.cpp | 37 ++++++++---- .../src/ThunderBirdReminderView.cpp | 57 +++++++++++++------ 6 files changed, 98 insertions(+), 56 deletions(-) diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdModel.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdModel.h index 49e4b8d3b..a3f3c9c96 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdModel.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdModel.h @@ -311,6 +311,9 @@ namespace OHOS // 哄睡界面相关标志 bool m_lullabyView3InquiryStarted = false; // THUNDERBIRD_LULLABYVIEW_3界面,角色是否已经开始询问 + //音量 + int16_t m_volume = 50; + private: ThunderBirdModel(); virtual ~ThunderBirdModel(); diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdReminderView.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdReminderView.h index 0473a8adf..9052ae195 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdReminderView.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdReminderView.h @@ -24,7 +24,7 @@ namespace OHOS // 音量/进度调节接口(用于 THUNDERBIRD_REMINDERVIEW_VOLUME) void AdjustRemindProgress(int16_t delta); // 正数增加,负数减少(只更新UI,延迟应用真实音量) void SetRemindProgress(int16_t value); // 立即设置真实音量 - void ApplyPendingVolume(); // 应用待设置的音量值(在界面隐藏时调用) + void ApplyPendingVolume(bool async = false); // 应用待设置的音量值(async=true表示异步执行,用于实时响应场景) bool IsVolumeViewVisible() const; // 检查音量界面是否正在显示 // 任务提醒文本更新接口(用于 THUNDERBIRD_REMINDERVIEW_TASK_REMINDER) diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdMainView.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdMainView.cpp index fde6d1462..52c065a28 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdMainView.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdMainView.cpp @@ -601,7 +601,6 @@ namespace OHOS } group[thisView] = g_pThunderBirdReminderView->ThunderBirdReminderViewInit(); ScrollView_->Add(group[thisView]); - group[thisView]->SetVisible(true); } return true; } diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp index 7c8dad8df..d5a6c6b06 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp @@ -114,9 +114,6 @@ extern "C" { m_hasNetwork(false), m_networkStatusChanged(false), m_storyModeTimeoutTimer(nullptr) { - //设置屏幕亮度为20 - // cm_set_brightness(20); - // uapi_setScreenBrightness(MAX_BRIGHTNESS);//验证亮屏是否是底层 // 初始化时加载提醒数据 // LoadRemindersFromFile(); //创建kv文件夹 @@ -147,7 +144,8 @@ extern "C" { // 初始化对话状态显示变量 m_currentExpression = "CloseEyes"; m_currentStatusText = "发呆中..."; - + //初始化音量 + AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_ASSISTANT, m_volume); RegisterAllKeyEvents(); } @@ -254,8 +252,8 @@ extern "C" { StartTimeDisplayTimer(1000); // 1秒间隔 } - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Reminder hide timer started: %d seconds, close_battery_flag=%d, isVolumeViewVisible=%d", - m_reminderHideTime, close_battery_flag, isVolumeViewVisible); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Reminder hide timer started: %d seconds, close_battery_flag=%d", + m_reminderHideTime, close_battery_flag); } // ---------------------- OTA 倒计时定时器(已整合到新定时器系统) ---------------------- @@ -1481,6 +1479,7 @@ extern "C" { /** * @brief 时间显示回调处理 */ + //操作太多,需要优化 void ThunderBirdModel::TimeDisplayCallback() { // 更新时间显示 @@ -1490,8 +1489,6 @@ extern "C" { view->UpdateTimeText(currentTime.c_str()); WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "TimeDisplayCallback: %s", currentTime.c_str()); } - int brightness = ConfigModel::GetInstance()->GetBrightness(); - // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TimeDisplayCallback: brightness=%d", brightness); // 处理提醒隐藏定时器 if (m_reminderHideActive) { @@ -1591,12 +1588,6 @@ extern "C" { */ void ThunderBirdModel::StatusCallback() { - // if( HasCat1Power() == false) - // { - // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Cat1电源状态异常,跳过网络检查"); - // return; - // } - // 更新网络状态 UpdateNetworkStatus(); @@ -2202,18 +2193,7 @@ extern "C" { reminderView->Show(ThunderBirdReminderView::THUNDERBIRD_SCHEDULE_TASK_REMINDER); reminderView->scheduleName_task_reminder(scheduleName); } - // //如果当前界面是事项提醒界面,则不进行切换,防止返回原任务进入循环 - // if(currentView != THUNDERBIRD_REMINDERVIEW && currentSubView != ThunderBirdReminderView::THUNDERBIRD_REMINDERVIEW_TASK_REMINDER) - // { - // ThunderBirdMainView::GetInstance()->ChangeView(currentView, THUNDERBIRD_REMINDERVIEW, ThunderBirdReminderView::THUNDERBIRD_REMINDERVIEW_TASK_REMINDER); - // ThunderBirdReminderView::GetInstance()->IsIcon(true); - // } - // // 更新任务提醒的文本内容 - // if (ThunderBirdReminderView::GetInstance()) { - // ThunderBirdReminderView::GetInstance()->UpdateTaskReminderText(scheduleName,nullptr); - // } - // } else { - // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdMainView instance is null, cannot show task reminder"); + } WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Processing task reminder: %s", scheduleName); } @@ -3183,6 +3163,17 @@ extern "C" { m_dialogueExpression = m_currentExpression; WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdModel::OnEnterDialogueMode: 记录时间线表情: %s", m_currentExpression.c_str()); + // 应用音量(如果在STATUS_SHOW界面调节过音量,此时应用) + auto *reminderView = ThunderBirdReminderView::GetInstance(); + if (reminderView && reminderView->IsVolumeViewVisible()) { + // 如果音量界面正在显示,应用待设置的音量值 + reminderView->ApplyPendingVolume(false); // 同步执行 + } else { + // 如果音量界面未显示,直接应用Model中的音量 + AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_ASSISTANT, m_volume); + ln_bt_earphone_set_volume(m_volume); + } + // 切换到眨眼表情(winking) std::string winkingExpressionPath = "/user/res/video/CloseEyes/" + eyesShape_ + "/" + eyesColor_ + ".mp4"; @@ -3264,6 +3255,17 @@ extern "C" { m_lullabyExpression = m_currentExpression; WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdModel::OnEnterLullabyMode: 记录时间线表情: %s", m_currentExpression.c_str()); + // 应用音量(如果在STATUS_SHOW界面调节过音量,此时应用) + auto *reminderView = ThunderBirdReminderView::GetInstance(); + if (reminderView && reminderView->IsVolumeViewVisible()) { + // 如果音量界面正在显示,应用待设置的音量值 + reminderView->ApplyPendingVolume(false); // 同步执行 + } else { + // 如果音量界面未显示,直接应用Model中的音量 + AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_ASSISTANT, m_volume); + ln_bt_earphone_set_volume(m_volume); + } + // 切换到眨眼表情(winking) std::string winkingExpressionPath = "/user/res/video/CloseEyes/" + eyesShape_ + "/" + eyesColor_ + ".mp4"; diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdPresenter.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdPresenter.cpp index fe5351de1..8498d0432 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdPresenter.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdPresenter.cpp @@ -608,9 +608,18 @@ namespace OHOS */ void ThunderBirdPresenter::ShowVolumeAdjustment(int volumeDelta) { - // struct timespec startTime; - // clock_gettime(CLOCK_MONOTONIC, &startTime); - + int16_t currentView = ThunderBirdMainView::GetInstance()->GetCurrentView(); + int16_t currentSubView = ThunderBirdMainView::GetInstance()->GetCurrentSubView(); + + // 判断是否在需要实时响应的界面 + // THUNDERBIRD_LULLABYVIEW_4, THUNDERBIRD_DIALOGUEVIEW_LISTENING, THUNDERBIRD_DIALOGUEVIEW_SPEAKING 需要实时响应 + // THUNDERBIRD_DIALOGUEVIEW_STATUS_SHOW 界面只调节UI显示,不实时应用音量,等待进入对话/哄睡模式时再应用 + bool isRealtimeResponse = ((currentView == THUNDERBIRD_DIALOGUEVIEW && + (currentSubView == ThunderBirdDialogueView::THUNDERBIRD_DIALOGUEVIEW_LISTENING || + currentSubView == ThunderBirdDialogueView::THUNDERBIRD_DIALOGUEVIEW_SPEAKING)) || + (currentView == THUNDERBIRD_LULLABYVIEW && + currentSubView == ThunderBirdLullabyView::THUNDERBIRD_LULLABYVIEW_4)); + ThunderBirdReminderView *reminderView = ThunderBirdReminderView::GetInstance(); if (!reminderView) { ThunderBirdMainView::GetInstance()->ChangeThunderBirdReminderView(THUNDERBIRD_REMINDERVIEW); @@ -620,24 +629,30 @@ namespace OHOS if (reminderView) { // 检查音量界面是否已经显示 if (reminderView->IsVolumeViewVisible()) { - // 界面已经显示,只更新音量并重新启动定时器(再延时1秒) + // 界面已经显示,只更新音量并重新启动定时器(再延时1秒),不切换界面 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "音量界面已显示,只更新音量并重新启动定时器"); reminderView->AdjustRemindProgress(volumeDelta); ThunderBirdModel::GetInstance()->StartReminderHideTimer(1000); + + // 如果需要实时响应,异步应用音量(避免阻塞按键处理) + if (isRealtimeResponse) { + reminderView->ApplyPendingVolume(true); // 异步执行 + } } else { // 界面未显示,显示界面并启动定时器 - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "音量调节:显示界面"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "音量调节:显示界面 (isRealtimeResponse=%d)", isRealtimeResponse); ThunderBirdMainView::GetInstance()->ChangeThunderBirdReminderView(THUNDERBIRD_REMINDERVIEW); ThunderBirdModel::GetInstance()->StartReminderHideTimer(1000); reminderView->Show(ThunderBirdReminderView::THUNDERBIRD_REMINDERVIEW_VOLUME); reminderView->AdjustRemindProgress(volumeDelta); + + // 如果需要实时响应,异步应用音量(避免阻塞按键处理) + if (isRealtimeResponse) { + reminderView->ApplyPendingVolume(true); // 异步执行 + } + // isStatusShowOnly 的情况只更新UI,不应用音量,等待进入对话/哄睡模式时再应用 } } - - // struct timespec endTime; - // clock_gettime(CLOCK_MONOTONIC, &endTime); - // long long timeCost = (endTime.tv_sec - startTime.tv_sec) * 1000000000 + endTime.tv_nsec - startTime.tv_nsec; - // printf("timeCost = %u ns\n", timeCost); } /** @@ -882,6 +897,8 @@ namespace OHOS case ThunderBirdReminderView::THUNDERBIRD_REMINDERVIEW_TASK_REMINDER://事项提醒界面-关闭通知 //回原界面 ThunderBirdModel::GetInstance()->StopCountUpTimer(); // 手动跳转时关闭定时器 + //关闭语音初始化 + do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_AUDIO_DEINIT, NULL, 0); ThunderBirdModel::GetInstance()->HighFrequence(); ThunderBirdMainView::GetInstance()->ChangeView(currentView, THUNDERBIRD_DIALOGUEVIEW, ThunderBirdDialogueView::THUNDERBIRD_DIALOGUEVIEW_STATUS_SHOW); diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdReminderView.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdReminderView.cpp index 73e7936b7..72c90e9af 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdReminderView.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdReminderView.cpp @@ -7,6 +7,7 @@ #include "thunderbird/include/ThunderBirdModel.h" #include "ln_api.h" #include +#include namespace OHOS { static ThunderBirdReminderView *g_pThunderBirdReminderView{nullptr}; @@ -102,7 +103,8 @@ namespace OHOS { VolumeProgress->SetPosition(118, 132, 124, 4); VolumeProgress->SetRange(100, 0); - int volumeDelta = AudioManager::GetInstance().GetVolume(AUDIO_STREAM_VOICE_ASSISTANT); + // 使用Model中的音量,避免每次都GetVolume + int volumeDelta = ThunderBirdModel::GetInstance()->m_volume; printf("volumeDelta = %d\n", volumeDelta); VolumeProgress->SetValue(volumeDelta); VolumeProgress->SetBackgroundStyle(STYLE_BACKGROUND_OPA, 0xff); @@ -141,9 +143,8 @@ namespace OHOS } if (VolumeProgress) { - // 显示音量界面时,初始化显示值为当前真实音量 - int16_t currentVolume = AudioManager::GetInstance().GetVolume(AUDIO_STREAM_VOICE_ASSISTANT); - remindProgressValue_ = currentVolume; + // 显示音量界面时,初始化显示值为Model中的音量(避免每次都GetVolume) + remindProgressValue_ = ThunderBirdModel::GetInstance()->m_volume; pendingVolumeValue_ = -1; // 重置待应用的值 VolumeProgress->SetValue(remindProgressValue_); VolumeProgress->SetVisible(true); @@ -456,9 +457,9 @@ namespace OHOS { if (group == nullptr) { return; } - // 如果音量界面正在显示,在隐藏前应用待设置的音量值 + // 如果音量界面正在显示,在隐藏前应用待设置的音量值(同步执行) if (VolumeProgress && VolumeProgress->IsVisible()) { - ApplyPendingVolume(); + ApplyPendingVolume(false); // 隐藏时同步执行 } if (RemindButton) { RemindButton->SetVisible(false); } @@ -479,6 +480,8 @@ namespace OHOS if (value < 0) { value = 0; } if (value > 100) { value = 100; } remindProgressValue_ = value; + // 更新Model中的音量值 + ThunderBirdModel::GetInstance()->m_volume = value; AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_ASSISTANT, remindProgressValue_); ln_bt_earphone_set_volume(remindProgressValue_); VolumeProgress->SetValue(remindProgressValue_); @@ -492,14 +495,14 @@ namespace OHOS return; } - // 获取当前显示的音量值(如果有待应用的值,使用待应用的值;否则使用真实音量) + // 获取当前显示的音量值(如果有待应用的值,使用待应用的值;否则使用Model中的音量) int16_t currentDisplayValue; if (pendingVolumeValue_ >= 0) { // 有待应用的值,使用待应用的值作为基准 currentDisplayValue = pendingVolumeValue_; } else { - // 没有待应用的值,使用真实音量作为基准 - currentDisplayValue = AudioManager::GetInstance().GetVolume(AUDIO_STREAM_VOICE_ASSISTANT); + // 没有待应用的值,使用Model中的音量作为基准(避免每次都GetVolume) + currentDisplayValue = ThunderBirdModel::GetInstance()->m_volume; } // 计算新的音量值 @@ -517,18 +520,36 @@ namespace OHOS } /** - * @brief 应用待设置的音量值(在界面隐藏时调用) + * @brief 应用待设置的音量值(在界面隐藏时调用或实时响应时调用) + * @param async 是否异步执行,true表示异步执行(用于实时响应场景,避免阻塞按键处理) */ - void ThunderBirdReminderView::ApplyPendingVolume() + void ThunderBirdReminderView::ApplyPendingVolume(bool async) { - if (pendingVolumeValue_ >= 0) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ApplyPendingVolume: applying volume=%d", pendingVolumeValue_); - // 应用真实音量 - AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_ASSISTANT, pendingVolumeValue_); - ln_bt_earphone_set_volume(pendingVolumeValue_); - remindProgressValue_ = pendingVolumeValue_; - pendingVolumeValue_ = -1; // 清除待应用的值 + if (pendingVolumeValue_ < 0) { + return; // 没有待应用的值 + } + + int16_t volumeToApply = pendingVolumeValue_; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ApplyPendingVolume: applying volume=%d (async=%d)", volumeToApply, async); + + if (async) { + // 异步执行,避免阻塞按键处理 + GraphicService::GetInstance()->PostGraphicEvent([volumeToApply]() { + AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_ASSISTANT, volumeToApply); + ln_bt_earphone_set_volume(volumeToApply); + // 更新Model中的音量值 + ThunderBirdModel::GetInstance()->m_volume = volumeToApply; + }); + } else { + // 同步执行 + AudioManager::GetInstance().SetVolume(AUDIO_STREAM_VOICE_ASSISTANT, volumeToApply); + ln_bt_earphone_set_volume(volumeToApply); + // 更新Model中的音量值 + ThunderBirdModel::GetInstance()->m_volume = volumeToApply; } + + remindProgressValue_ = volumeToApply; + pendingVolumeValue_ = -1; // 清除待应用的值 } bool ThunderBirdReminderView::IsVolumeViewVisible() const -- 2.17.1