From: luozheng <1732036298@qq.com> Date: Thu, 4 Jul 2024 11:16:52 +0000 (+0800) Subject: 1.ai语音修改 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=18e58db6a4a1c18c992f2a9b270a3537c2d0dd72;p=mirrors%2FFD230.git 1.ai语音修改 2.解决下拉视图信号显示问题 3.喝水提醒修改 --- diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h b/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h index a3601118a..48722f414 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-20 15:14:43 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-07-03 19:20:55 + * @LastEditTime: 2024-07-04 18:31:22 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h */ #ifndef AI_VOICE_MODEL_H @@ -38,6 +38,7 @@ namespace OHOS uint8_t GetChatContentListCount(void); // 更新和清空问题及回答 + void RemoveLastTwoItemsOrClear(); bool UpdateUserQuestionStr(void); bool UpdateAiAnswerStr(void); void ClearQuestionAndAiAnswerStr(void); @@ -78,6 +79,7 @@ namespace OHOS uint8_t ChatContentListCount{0}; // 列表数量 static const uint16_t MaxChatContentListCount = 2; // 对话列表最大数量 ChatContentItem chatTtems[MaxChatContentListCount] = {0, "", false}; // 对话项数组 + bool entryTextViewState{false}; // 进入返回结果界面的标志 }; } diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoicePresenter.h b/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoicePresenter.h index e84c7e831..32bea1455 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoicePresenter.h +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoicePresenter.h @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-20 14:44:22 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-07-02 18:58:24 + * @LastEditTime: 2024-07-03 19:31:46 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoicePresenter.h */ #ifndef AI_VOICE_PRESENTER_H diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp index efb0a05d1..7b9ec787f 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-20 15:14:50 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-07-03 19:16:04 + * @LastEditTime: 2024-07-04 18:30:47 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp */ #include "aivoice/include/AiVoiceModel.h" @@ -48,6 +48,7 @@ namespace OHOS return false; } Aipresenter_->JumpToReturnResultTextView(); + entryTextViewState = false; return true; } @@ -112,9 +113,7 @@ namespace OHOS return false; } - chatTtems[ChatContentListCount].index = item.index; - chatTtems[ChatContentListCount].text = item.text; - chatTtems[ChatContentListCount].isAiAnswer = item.isAiAnswer; + chatTtems[ChatContentListCount] = item; ChatContentListCount++; return true; @@ -184,17 +183,84 @@ namespace OHOS } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Chat content list or adapter is nullptr!!"); return false; } return true; } - /*更新用户问题字符串*/ - bool AiVoiceModel::UpdateUserQuestionStr(void) + // 函数定义:移除列表的最后两项或清空列表 + void AiVoiceModel::RemoveLastTwoItemsOrClear() + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::RemoveLastTwoItemsOrClear"); + AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance(); + if (p_AivoiceReturnResultTextView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!!"); + return; + } + // 获取适配器 + AivoiceChatContentListAdapter *adapter = p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter(); + if (adapter != nullptr) + { + // 获取列表的大小 + size_t listSize = adapter->GetChatContentItem().size(); + + // 移除最后两项或清空列表 + if (listSize >= 2) + { + auto it = adapter->GetChatContentItem().end(); + std::advance(it, -2); // 移动迭代器到倒数第二项的位置 + adapter->GetChatContentItem().erase(it, adapter->GetChatContentItem().end()); + } + else if (listSize == 1) + { + // 如果列表只有一项,直接清空 + adapter->GetChatContentItem().clear(); + } + ChatContentListCount -= (listSize < 2 ? listSize : 2); + } + } + + // 在需要的地方调用该函数 + // 例如: + // RemoveLastTwoItemsOrClear(p_AivoiceReturnResultTextView); + + /* 更新用户问题字符串 */ + bool AiVoiceModel::UpdateUserQuestionStr() { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateUserQuestionStr"); + RemoveLastTwoItemsOrClear(); + + if (!AddUserQuestionToChatItems()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AddUserQuestionToChatItems fail!!"); + return false; + } + + // 更新显示列表 + AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance(); + if (!p_AivoiceReturnResultTextView) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!!"); + return false; + } + + AivoiceChatContentListAdapter *adapter = p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter(); + if (adapter && !adapter->GetChatContentItem().empty()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetChatContentListCount() - 1 = %d,%d", GetChatContentListCount() - 1, __LINE__); + adapter->GetChatContentItem().push_back(chatTtems[GetChatContentListCount() - 1]); + p_AivoiceReturnResultTextView->GetChatContentList()->RefreshList(); + } + else + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Chat content adapter is nullptr!!"); + return false; + } + return true; } @@ -203,7 +269,6 @@ namespace OHOS { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateAiAnswerStr"); - // 获取 ChatContentListCount int index = GetChatContentListCount() - 1; if (index < 0 || index >= MaxChatContentListCount) { @@ -211,14 +276,20 @@ namespace OHOS return false; } - // 更新 chatTtems 中的 AI 回答内容 - chatTtems[index].index = index; - chatTtems[index].text = GetAiAnswerStr(); - chatTtems[index].isAiAnswer = true; + if (chatTtems[index].isAiAnswer) // 处理ai1的多段回答 + { + ChatContentListCount--; + } + + if (!AddAiAnswerToChatItems()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AddAiAnswerToChatItems fail!!"); + return false; + } // 更新显示列表 AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance(); - if (p_AivoiceReturnResultTextView == nullptr) + if (!p_AivoiceReturnResultTextView) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!!"); return false; @@ -233,7 +304,7 @@ namespace OHOS adapter->GetChatContentItem().pop_back(); } // 将更新后的 AI 回答内容添加到列表 - adapter->GetChatContentItem().push_back(chatTtems[index]); + adapter->GetChatContentItem().push_back(chatTtems[GetChatContentListCount() - 1]); p_AivoiceReturnResultTextView->GetChatContentList()->RefreshList(); } @@ -251,7 +322,13 @@ namespace OHOS /*设置用户问题的字符串*/ void AiVoiceModel::SetUserQuestionStr(std::string str) { + /* -------------------------------------------------------------------------- */ userQuestionStr = str; + /* -------------------------------------------------------------------------- */ + if (!UpdateUserQuestionStr()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateUserQuestionStr fail!!"); + } } /*获取Ai回答的字符串*/ @@ -265,8 +342,18 @@ namespace OHOS /*设置Ai回答的字符串*/ void AiVoiceModel::SetAiAnswerStrStr(std::string str) { - SendMsgToUIService(UI_AI_RESPONSE_USERDATA, 0, nullptr, 0); + if (!entryTextViewState) + { + SendMsgToUIService(UI_AI_RESPONSE_USERDATA, 0, nullptr, 0); + entryTextViewState = true; //进入返回结果界面的标志 避免重复发消息 + } + /* -------------------------------------------------------------------------- */ aiAnswerStr += str; + /* -------------------------------------------------------------------------- */ + if (!UpdateAiAnswerStr()) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateAiAnswerStr fail!!"); + } } /*清空上次用户问题和Ai回答字符串*/ @@ -379,7 +466,7 @@ namespace OHOS chatTtems[i].isAiAnswer = false; } ChatContentListCount = 0; - + entryTextViewState = false; entryTimeoutFlag = false; AIVoice_StopTimer(); ClearQuestionAndAiAnswerStr(); diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp index 534419a93..cb1e9c060 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-20 15:11:17 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-07-03 17:39:36 + * @LastEditTime: 2024-07-04 17:20:46 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp */ #include "AppViewIDs.h" @@ -20,6 +20,12 @@ #include // For std::unordered_map #include "bleEvent.h" #include + +#define LEAK_CHECK 0 +#if LEAK_CHECK +#include "gfx_utils/mem_check.h" +#endif + namespace OHOS { @@ -28,12 +34,18 @@ namespace OHOS AiVoicePresenter::AiVoicePresenter() { +#if LEAK_CHECK + MemCheck::GetInstance().EnableLeakCheck(true); +#endif model = &AiVoiceModel::GetInstance(); g_pAiVoicePresenter = this; } AiVoicePresenter::~AiVoicePresenter() { +#if LEAK_CHECK + MemCheck::GetInstance().EnableLeakCheck(false); +#endif g_pAiVoicePresenter = nullptr; } @@ -89,11 +101,6 @@ namespace OHOS else { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoicePresenter::JumpToReturnResultTextView fail"); - // return false; - } - if (!model->UpdateAiAnswerStr()) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UpdateUserQuestionAndAiAnswerStr fail"); return false; } return true; diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/src/PullUpDownView.cpp b/code/sdk/application/wearable/fd230app/nativeui/main/src/PullUpDownView.cpp index f58fa7582..d2e414846 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/main/src/PullUpDownView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/main/src/PullUpDownView.cpp @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-26 18:14:24 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-07-03 18:42:05 + * @LastEditTime: 2024-07-04 18:51:18 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/main/src/PullUpDownView.cpp */ @@ -801,15 +801,23 @@ namespace OHOS const int NETWORK_TYPE_NO_NETWORK = 0; const int NETWORK_TYPE_4G = 4; - bool showNoSignalUI = (sim == 0 || cat1_powerOn == 0 || signal == SIGNAL_NO_SIGNAL); - bool showNoNetworkUI = (network_on == 0 || cat1_network_type == NETWORK_TYPE_NO_NETWORK); - - bool show4GIcon = (cat1_network_type == NETWORK_TYPE_4G); + bool showNoSignal = (sim == 0 || cat1_powerOn == 0 || signal == SIGNAL_NO_SIGNAL); + bool showNo4GIcon = (network_on == 0 || cat1_network_type == NETWORK_TYPE_NO_NETWORK); - UpdateCat1NoSignalisplay(showNoSignalUI); - UpdateCat14GDisplay(show4GIcon); - UpdateSignalBarDisplay(CalculateSignalBars(signal), !(showNoSignalUI || showNoNetworkUI)); + if (showNoSignal) + { + UpdateCat1NoSignalisplay(true); + UpdateCat14GDisplay(false); + UpdateSignalBarDisplay(CalculateSignalBars(signal), false); + } + else + { + UpdateCat1NoSignalisplay(false); + UpdateCat14GDisplay(!showNo4GIcon && cat1_network_type == NETWORK_TYPE_4G); + UpdateSignalBarDisplay(CalculateSignalBars(signal), true); + } } + #endif // 更新信号强度显示 diff --git a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp index a0d491f68..d8787e87c 100644 --- a/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp +++ b/code/sdk/application/wearable/service/ble_service/blemoder/bletest.cpp @@ -72,6 +72,7 @@ extern "C" { #include #include +#include void ble_set_ai_Question_str(char *buf) { @@ -616,7 +617,8 @@ void ble_set_drink_remind(msg_drink_info_t data) printf("频率:%d\r\n",data.freq); p_ConfigModel->set_condigData_Int(OHOS::ConfigModel::CONFIGMODEL_ITEM_DRINK_FREQ,data.freq); } - } + } + CustomEventPublish(TOPIC_EVENT_DRINKREMINDER,NULL,0); return; } diff --git a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c index 413982566..7de38b475 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleEvent.c @@ -4015,7 +4015,7 @@ static void GetAiRecordCmd(uint16_t cmd,uint8_t *data,uint16_t len,uint8_t *tota send_big_data(1,data,read_size,file_size,pack_id,pack,0x34,1); pack_id++; - msleep(10); + msleep(40); } while (read_size==num); free(data); diff --git a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c index 25ed5dd10..5dfcd3fff 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c @@ -985,6 +985,10 @@ errcode_t svr_ble_bt_gatts_noyify(uint8_t *data, uint16_t data_len)//这里是 data_info.value = data; data_info.value_len = data_len; errcode_t ret = gatts_notify_indicate(g_ble_server_id, g_ble_conn_id, &data_info); + if(ret!=ERRCODE_SUCC) + { + gatts_notify_indicate(g_ble_server_id, g_ble_conn_id, &data_info); + } WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "gatts_notify_indicate ret:0x%x. serverid:%u,conn_id:%u,handle:%u.data_len:%d", ret, g_ble_server_id, g_ble_conn_id, g_ble_server_handle,data_len); diff --git a/code/sdk/application/wearable/service/msg_center/include/notify/drinkReminder_notification.h b/code/sdk/application/wearable/service/msg_center/include/notify/drinkReminder_notification.h index 62d495012..c842942a3 100644 --- a/code/sdk/application/wearable/service/msg_center/include/notify/drinkReminder_notification.h +++ b/code/sdk/application/wearable/service/msg_center/include/notify/drinkReminder_notification.h @@ -1,3 +1,10 @@ +/* + * @Author: luozheng 1732036298@qq.com + * @Date: 2024-06-12 11:44:04 + * @LastEditors: luozheng 1732036298@qq.com + * @LastEditTime: 2024-07-04 15:16:56 + * @FilePath: /FD230/code/sdk/application/wearable/service/msg_center/include/notify/drinkReminder_notification.h + */ #ifndef DRINKREMINDER_NOTIFICATION #define DRINKREMINDER_NOTIFICATION @@ -7,13 +14,22 @@ namespace OHOS UIViewGroup *CreateDrinkReminderView(void); void FreeDrinkReminderView(void); - class DrinkReminderViewEventListener : public UIView::OnTouchListener,public UIView::OnDragListener + class DrinkReminderViewEventListener : public UIView::OnTouchListener, public UIView::OnDragListener { public: static DrinkReminderViewEventListener &GetInstance(); bool OnRelease(UIView &view, const ReleaseEvent &event) override; - bool OnDrag(UIView& view, const DragEvent& event) override; - bool OnDragEnd(UIView& view, const DragEvent& event) override; + bool OnDrag(UIView &view, const DragEvent &event) override; + bool OnDragEnd(UIView &view, const DragEvent &event) override; + + void SetDrinkReminderTopic(uint16_t topic); + uint16_t GetDrinkReminderTopic(void); + bool IsDrinkReminderTimerOpen(void); + int DrinkReminderOpenState(void); + uint32_t GetDrinkReminderIntervalTime(void); + bool IsDrinkReminderTime(void); + void DrinkReminder_StartTimer(); + void DrinkReminder_StopTimer(void); private: DrinkReminderViewEventListener(); @@ -21,7 +37,9 @@ namespace OHOS DrinkReminderViewEventListener(const DrinkReminderViewEventListener &) = delete; const DrinkReminderViewEventListener &operator=(const DrinkReminderViewEventListener &) = delete; bool X_DRAG_FLAG{false}; - + int frequency{0}; + uint16_t drinkReminderTopic; + bool timerOpen{false}; // 定时器是否开启 }; } diff --git a/code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp b/code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp index 215de7311..0f6d1ac56 100644 --- a/code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp +++ b/code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp @@ -918,13 +918,39 @@ void MsgCenterNotifyCanCelFindWatchProc(const Topic topic, Request* req) } //喝水提醒通知 -void MsgCenterNotifyDrinkReminderProc(const Topic topic, Request* req) +void MsgCenterNotifyDrinkReminderProc(const Topic topic, Request *req) { unused(topic); unused(req); WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "MsgCenterNotifyDrinkReminderProc !"); - NotificationManager::GetInstance()->SetNotifyPriority(PRIORITY_TWO); - NotificationManager::GetInstance()->ShowNotify(showDrinkReminderNotify, (uint16_t)topic, true); + int state = DrinkReminderViewEventListener::GetInstance().DrinkReminderOpenState(); + switch (state) + { + case 0: + if (DrinkReminderViewEventListener::GetInstance().IsDrinkReminderTimerOpen()) + { + DrinkReminderViewEventListener::GetInstance().DrinkReminder_StopTimer(); + } + + break; + case 1: + if (DrinkReminderViewEventListener::GetInstance().IsDrinkReminderTimerOpen()) + { + DrinkReminderViewEventListener::GetInstance().DrinkReminder_StopTimer(); + } + // uapi_set_screen_on_api(); + // NotificationManager::GetInstance()->ShowNotify(showDrinkReminderNotify, (uint16_t)topic, true); + + break; + case 2: + if (!DrinkReminderViewEventListener::GetInstance().IsDrinkReminderTimerOpen()) + { + DrinkReminderViewEventListener::GetInstance().DrinkReminder_StartTimer(); + } + break; + default: + break; + } } void MsgCenterNotifyActivityProc(const Topic topic, Request* req) diff --git a/code/sdk/application/wearable/service/msg_center/src/notify/drinkReminder_notification.cpp b/code/sdk/application/wearable/service/msg_center/src/notify/drinkReminder_notification.cpp index f9a68766e..be95abf67 100644 --- a/code/sdk/application/wearable/service/msg_center/src/notify/drinkReminder_notification.cpp +++ b/code/sdk/application/wearable/service/msg_center/src/notify/drinkReminder_notification.cpp @@ -6,15 +6,21 @@ #ifdef FD230 #include "language/font/ui_resource_string.h" #endif -#include - +#include +#include +#include +#include +#include "ConfigModel.h" +#include +#include +#include namespace OHOS { UIViewGroup *DrinkReminderGroupView{nullptr}; UILabelExt *DrinkReminderTitleLabel{nullptr}; // 标题: 喝水提醒 UIImageView *DrinkReminderImage{nullptr}; - UILabel *DrinkReminderNoteLabel{nullptr}; // 喝水提醒note + UILabelExt *DrinkReminderNoteLabel{nullptr}; // 喝水提醒note UILabelExt *DrinkReminderLabel{nullptr}; UIButton *DrinkReminderButton{nullptr}; @@ -24,6 +30,13 @@ namespace OHOS static constexpr const char *DRINKREMINDERGROUPVIEWID = "DrinkReminderGroupview"; static constexpr const char *DRINKREMINDERBUTTONVIEWID = "DrinkReminderButton"; + // 声明定时器变量 + static osTimerId_t g_timer = NULL; + static constexpr const uint32_t HALF_HOUR = 30 * 60 * 1000; // 半小时 + static constexpr const uint32_t ONE_HOUR = 1 * 60 * 60 * 1000; // 1小时 + static constexpr const uint32_t TWO_HOUR = 2 * 60 * 60 * 1000; // 2小时 + static constexpr const uint32_t THREE_HOUR = 3 * 60 * 60 * 1000; // 3小时 + DrinkReminderViewEventListener::DrinkReminderViewEventListener() { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DrinkReminderViewEventListener::DrinkReminderViewEventListener"); @@ -79,6 +92,191 @@ namespace OHOS return true; } + void DrinkReminderViewEventListener::SetDrinkReminderTopic(uint16_t topic) + { + drinkReminderTopic = topic; + } + + uint16_t DrinkReminderViewEventListener::GetDrinkReminderTopic(void) + { + return drinkReminderTopic; + } + + /*获取定时器是否开启*/ + bool DrinkReminderViewEventListener::IsDrinkReminderTimerOpen(void) + { + return timerOpen; + } + + /*久坐提醒是否开启,(0:关闭,1:开启 [每天一次]) 2为间隔提醒*/ + int DrinkReminderViewEventListener::DrinkReminderOpenState(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DrinkReminderViewEventListener::DrinkReminderOpenState"); + int state = ConfigModel::GetInstance()->GetDrinkSwitch(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "state = %d", state); + return state; + } + + /*获取喝水提醒的间隔时间*/ + uint32_t DrinkReminderViewEventListener::GetDrinkReminderIntervalTime(void) + { + // 添加日志输出,用于调试 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DrinkReminderViewEventListener::GetDrinkReminderIntervalTime"); + + // 获取频率前先进行错误处理 + ConfigModel *config = ConfigModel::GetInstance(); + if (!config) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ConfigModel::GetInstance() returned nullptr"); + return HALF_HOUR; // 返回一个默认值 + } + + // 获取喝水提醒的频率 + frequency = config->GetDrinkFreq(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "frequency = %d", frequency); + + // 根据频率设置间隔时间 + switch (frequency) + { + case 0: + return HALF_HOUR; + case 1: + return ONE_HOUR; + case 2: + return TWO_HOUR; + case 3: + return THREE_HOUR; + default: + return HALF_HOUR; // 默认情况下返回半小时间隔 + } + } + + /*是否是喝水提醒时间段内,间隔提醒开启后启用*/ + bool DrinkReminderViewEventListener::IsDrinkReminderTime() + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DrinkReminderViewEventListener::IsDrinkReminderTime"); + // 获取当前时间 + time_t now = time(0); + tm *ltm = localtime(&now); + int currentHour = ltm->tm_hour; + int currentMinute = ltm->tm_min; + + // 获取提醒时间段 + int drinkReminderStartTime = ConfigModel::GetInstance()->GetDrinkStart(); // 获取喝水提醒开始时间,int类型 前16位小时,后16位分钟 + int drinkReminderEndTime = ConfigModel::GetInstance()->GetDrinkEnd(); // 获取喝水提醒结束时间, int类型 前16位小时,后16位分钟 + + // 解析开始时间 + int startHour = (drinkReminderStartTime >> 8) & 0xFF; // 前8位是小时 + int startMinute = drinkReminderStartTime & 0xFF; // 后8位是分钟 + + // 解析结束时间 + int endHour = (drinkReminderEndTime >> 8) & 0xFF; // 中前8位是小时 + int endMinute = drinkReminderEndTime & 0xFF; // 后8位是分钟 + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "currentHour = %d,currentMinute = %d", currentHour, currentMinute); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "drinkReminderStartTime = %d,drinkReminderEndTime = %d", drinkReminderStartTime, drinkReminderEndTime); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "startHour = %d,startMinute = %d", startHour, startMinute); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "endHour = %d,endMinute = %d", endHour, endMinute); + + // 判断当前时间是否在提醒时间段内 + if (currentHour > startHour || (currentHour == startHour && currentMinute >= startMinute)) + { + if (currentHour < endHour || (currentHour == endHour && currentMinute < endMinute)) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "It's time to remind the user to drink!"); + return true; + } + } + + return false; + } + + /* -------------------------------------------------------------------------- */ + + /*显示喝水提醒视图*/ + static OHOS::UIViewGroup *showDrinkReminderNotify(void) + { + return OHOS::CreateDrinkReminderView(); + } + + /*定时器到期时的处理*/ + static void DrinkReminder_TimerCallbackProc(int data) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DrinkReminder_TimerCallbackProc"); + if (DrinkReminderViewEventListener::GetInstance().IsDrinkReminderTime()) + { + uapi_set_screen_on_api(); + NotificationManager::GetInstance()->ShowNotify(showDrinkReminderNotify, DrinkReminderViewEventListener::GetInstance().GetDrinkReminderTopic(), true); + } + else + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DrinkReminder Out of time"); + } + } + + /*回调函数*/ + static void DrinkReminder_TimerCallback(int data) + { + GraphicService::GetInstance()->PostGraphicEvent(std::bind(DrinkReminder_TimerCallbackProc, data)); + } + + /*开启定时器*/ + void DrinkReminderViewEventListener::DrinkReminder_StartTimer(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DrinkReminderViewEventListener::DrinkReminder_StartTimer"); + /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */ + if (g_timer != NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer already exists!!"); + return; + } + + /* --------------------------------- 2.创建定时器 -------------------------------- */ + g_timer = osTimerNew((osTimerFunc_t)DrinkReminder_TimerCallback, osTimerPeriodic, NULL, NULL); + // 检查定时器创建是否成功 + if (g_timer == NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer create failed!!"); + return; + } + + /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 ---------------------------- */ + int tick = osMs2Tick(GetDrinkReminderIntervalTime()); + + /* --------------------------------- 4.启动定时器 -------------------------------- */ + osStatus_t ret = osTimerStart(g_timer, tick); + /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 NULL -------------------- */ + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret); + if (g_timer != NULL) + { + ret = osTimerDelete(g_timer); + } + g_timer = NULL; + return; + } + timerOpen = true; + } + + /*停止定时器*/ + void DrinkReminderViewEventListener::DrinkReminder_StopTimer(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DrinkReminderViewEventListener::DrinkReminder_StopTimer"); + // 检查定时器是否存在 + if (g_timer != NULL) + { + // 删除定时器并置为 NULL + osStatus_t ret = osTimerDelete(g_timer); + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret); + } + g_timer = NULL; + timerOpen = false; + } + } + /*创建喝水提醒视图*/ UIViewGroup *CreateDrinkReminderView(void) { @@ -130,7 +328,7 @@ namespace OHOS DrinkReminderImage->SetPosition(193, 130, 80, 80); DrinkReminderImage->SetSrc("/user/res/drinkReminder/DrinkReminder.bin"); /* ------------------------- DrinkReminderNoteLabel ------------------------- */ - DrinkReminderNoteLabel = new UILabel(); + DrinkReminderNoteLabel = new UILabelExt(); if (DrinkReminderNoteLabel == nullptr) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CreateDrinkReminderView] DrinkReminderNoteLabel new fail!!"); @@ -140,7 +338,7 @@ namespace OHOS DrinkReminderNoteLabel->SetStyle(STYLE_TEXT_FONT, 30); DrinkReminderNoteLabel->SetStyle(STYLE_LINE_HEIGHT, 35); #ifdef FD230 - SetLabelId(DrinkReminderNoteLabel, STR_DRINK_WATER_REMINDER_NOTE); + DrinkReminderNoteLabel->SetTextId(STR_DRINK_WATER_REMINDER_NOTE); #endif DrinkReminderNoteLabel->SetLineBreakMode(UILabel::LINE_BREAK_WRAP); DrinkReminderNoteLabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); @@ -201,6 +399,11 @@ namespace OHOS WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "FreeDrinkReminderView(void)"); if (DrinkReminderGroupView != nullptr) { + if (DrinkReminderViewEventListener::GetInstance().IsDrinkReminderTimerOpen()) + { + DrinkReminderViewEventListener::GetInstance().DrinkReminder_StopTimer(); + } + DrinkReminderGroupView->RemoveAll(); delete DrinkReminderTitleLabel; diff --git a/code/sdk/drivers/peripheral/config/src/ConfigModel.cpp b/code/sdk/drivers/peripheral/config/src/ConfigModel.cpp index cbaac6802..7223f9d5b 100644 --- a/code/sdk/drivers/peripheral/config/src/ConfigModel.cpp +++ b/code/sdk/drivers/peripheral/config/src/ConfigModel.cpp @@ -859,23 +859,23 @@ namespace OHOS return get_configData_int(CONFIGMODEL_ITEM_DRINK_GOAL); } - //设置喝水提醒开始时间,32位数字,前16位代表小时,后16位代表分钟 + //设置喝水提醒开始时间, int类型 前16位小时,后16位分钟 void ConfigModel::SetDrinkStart(const int value) { set_condigData_Int(CONFIGMODEL_ITEM_DRINK_START,value); } - //获取喝水提醒开始时间,32位数字,前16位代表小时,后16位代表分钟 + //获取喝水提醒开始时间,int类型 前16位小时,后16位分钟 int ConfigModel::GetDrinkStart() { return get_configData_int(CONFIGMODEL_ITEM_DRINK_START); } - //设置喝水提醒结束时间,32位数字,前16位代表小时,后16位代表分钟 + //设置喝水提醒结束时间 int类型 前16位小时,后16位分钟 void ConfigModel::SetDrinkEnd(const int value) { set_condigData_Int(CONFIGMODEL_ITEM_DRINK_END,value); } - //获取喝水提醒结束时间,32位数字,前16位代表小时,后16位代表分钟 + //获取喝水提醒结束时间, int类型 前16位小时,后16位分钟 int ConfigModel::GetDrinkEnd() { return get_configData_int(CONFIGMODEL_ITEM_DRINK_END);