From: xue <2144439972@qq.com> Date: Fri, 5 Jun 2026 07:16:19 +0000 (+0800) Subject: 新增语音对话slice X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=refs%2Fchanges%2F3438%2Fhead;p=mirrors%2FFD230.git 新增语音对话slice --- diff --git a/code/sdk/application/wearable/n9/nativeapp/nativelauncher/include/AppViewIDs.h b/code/sdk/application/wearable/n9/nativeapp/nativelauncher/include/AppViewIDs.h index b08c0c9db..acd419ef4 100644 --- a/code/sdk/application/wearable/n9/nativeapp/nativelauncher/include/AppViewIDs.h +++ b/code/sdk/application/wearable/n9/nativeapp/nativelauncher/include/AppViewIDs.h @@ -170,6 +170,7 @@ typedef enum : uint16_t #endif VIEW_HEART_CHART, // 心率曲线图检测 VIEW_PERSONAL_CENTER, // 个人中心(蓝牙/车控等) + VIEW_SIMPLE_VOICE, // 简化语音对话(仅点击说话/聆听/播放界面) VIEW_ADD_NEW_ID = 250, /* -------------------------------------------------------------------------- */ VIEW_RECENT_APP, diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoiceListeningView.h b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoiceListeningView.h new file mode 100644 index 000000000..a7a724a8b --- /dev/null +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoiceListeningView.h @@ -0,0 +1,70 @@ +#ifndef SIMPLE_VOICE_LISTENING_VIEW_H +#define SIMPLE_VOICE_LISTENING_VIEW_H + +#include "components/ui_scroll_view.h" +#include "components/ui_label_ext.h" +#include "components/ui_button.h" +#include "components/ui_image_animator.h" + +namespace OHOS +{ + static constexpr const char *SIMPLE_VOICE_FINISH_SPEAKING_BUTTON_VIEW_ID = "SimpleVoiceFinishSpeakingButton"; + + static constexpr int16_t SIMPLE_VOICE_LISTENING_ANIM_X = 113; + static constexpr int16_t SIMPLE_VOICE_LISTENING_ANIM_Y = 178; + static constexpr int16_t SIMPLE_VOICE_LISTENING_ANIM_W = 240; + static constexpr int16_t SIMPLE_VOICE_LISTENING_ANIM_H = 60; + static constexpr int16_t SIMPLE_VOICE_LISTENING_ANIM_NUM_FRAMES = 40; + static constexpr int16_t SIMPLE_VOICE_LISTENING_ANIM_DELAY = 70; + + class SimpleVoiceListeningView + { + public: + struct LabelConfig + { + int16_t left; + int16_t top; + int16_t width; + int16_t height; + int16_t fontSize; + int16_t lineHeigh; + uint16_t textId; + uint8_t lineBreakMode; + }; + + struct ButtonConfig + { + int16_t x; + int16_t y; + int16_t w; + int16_t h; + const char *ViewId; + }; + + SimpleVoiceListeningView(); + ~SimpleVoiceListeningView(); + + UIScrollView *CreateListeningView(UIView::OnDragListener *dragListener, UIView::OnTouchListener *touchListener); + UILabelExt *GetTimeoutLabel(void); + void StartOrStopAnimation(bool start); + + private: + void ClearAll(void); + UILabelExt *InitLabel(LabelConfig &labelconfig); + UIButton *InitButton(ButtonConfig &buttonconfig); + bool CreateScrollView(UIView::OnDragListener *dragListener); + bool CreateLabels(void); + bool CreateButton(UIView::OnTouchListener *touchListener); + bool CreateImageAnimator(void); + + UIScrollView *scrollView_{nullptr}; + UILabelExt *title_{nullptr}; + UILabelExt *finishSpeakLabel_{nullptr}; + // UILabelExt *timeoutLabel_{nullptr}; + UIButton *finishSpeakButton_{nullptr}; + UIImageAnimatorView *animatorView_{nullptr}; + ImageAnimatorInfo animInfo_[SIMPLE_VOICE_LISTENING_ANIM_NUM_FRAMES]; + }; +} + +#endif diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoicePlayView.h b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoicePlayView.h new file mode 100644 index 000000000..c3509a53b --- /dev/null +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoicePlayView.h @@ -0,0 +1,67 @@ +#ifndef SIMPLE_VOICE_PLAY_VIEW_H +#define SIMPLE_VOICE_PLAY_VIEW_H + +#include +#include "components/ui_scroll_view.h" +#include "components/ui_label_ext.h" +#include "components/ui_button.h" +#include "components/ui_list.h" +#include "aivoice/include/AivoiceChatContentListAdapter.h" +#include "aivoice/include/ChatContentItem.h" + +namespace OHOS +{ + static constexpr const char *SIMPLE_VOICE_RETURN_BUTTON_VIEW_ID = "SimpleVoiceReturnButton"; + static constexpr const char *SIMPLE_VOICE_CONTINUE_CHAT_BUTTON_VIEW_ID = "SimpleVoiceContinueChatButton"; + + class SimpleVoicePlayView + { + public: + struct LabelConfig + { + int16_t left; + int16_t top; + int16_t width; + int16_t height; + int16_t fontSize; + int16_t lineHeight; + uint16_t textId; + uint8_t lineBreakMode; + }; + + struct ButtonConfig + { + int16_t x; + int16_t y; + int16_t w; + int16_t h; + const char *ViewId; + }; + + SimpleVoicePlayView(); + ~SimpleVoicePlayView(); + + UIScrollView *CreatePlayView(UIView::OnDragListener *dragListener, UIView::OnTouchListener *touchListener, + UIView::OnClickListener *clickListener); + void SetChatContent(const std::string &userText, const std::string &aiText); + + private: + void ClearAll(void); + UILabelExt *InitLabel(LabelConfig &labelconfig); + UIButton *InitButton(ButtonConfig &buttonconfig); + bool CreateScrollView(UIView::OnDragListener *dragListener); + bool CreateLabels(void); + bool CreateChatList(UIView::OnClickListener *clickListener); + bool CreateButtons(UIView::OnTouchListener *touchListener); + + UIScrollView *scrollView_{nullptr}; + UILabelExt *title_{nullptr}; + UILabelExt *continueChatLabel_{nullptr}; + UIButton *returnButton_{nullptr}; + UIButton *continueChatButton_{nullptr}; + AivoiceChatContentListAdapter *chatAdapter_{nullptr}; + UIList *chatList_{nullptr}; + }; +} + +#endif diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoicePresenter.h b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoicePresenter.h new file mode 100644 index 000000000..44fa9e85b --- /dev/null +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoicePresenter.h @@ -0,0 +1,36 @@ +#ifndef SIMPLE_VOICE_PRESENTER_H +#define SIMPLE_VOICE_PRESENTER_H + +#include "Presenter.h" +#include "View.h" + +namespace OHOS +{ + class SimpleVoiceView; + + class SimpleVoicePresenter : public Presenter, + public UIView::OnClickListener, + public UIView::OnDragListener, + public UIView::OnTouchListener + { + public: + SimpleVoicePresenter(); + virtual ~SimpleVoicePresenter(); + static SimpleVoicePresenter *GetInstance(); + + void OnStart() override; + void OnStop() override; + void ChangeView(int16_t hideView, int16_t showView); + + private: + bool OnClick(UIView &view, const ClickEvent &event) override; + bool OnPress(UIView &view, const PressEvent &event) override; + 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 xDragFlag_{false}; + }; +} + +#endif diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoiceStartView.h b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoiceStartView.h new file mode 100644 index 000000000..212df575f --- /dev/null +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoiceStartView.h @@ -0,0 +1,68 @@ +#ifndef SIMPLE_VOICE_START_VIEW_H +#define SIMPLE_VOICE_START_VIEW_H + +#include "components/ui_scroll_view.h" +#include "components/ui_label_ext.h" +#include "components/ui_button.h" +#include "components/ui_image_view.h" + +namespace OHOS +{ + static constexpr const char *SIMPLE_VOICE_START_CHAT_BUTTON_VIEW_ID = "simple_voice_start_chat_button"; + + class SimpleVoiceStartView + { + public: + struct LabelConfig + { + int16_t left; + int16_t top; + int16_t width; + int16_t height; + int16_t fontSize; + int16_t lineHeigh; + uint16_t textId; + uint8_t lineBreakMode; + }; + + struct ImageConfig + { + int16_t x; + int16_t y; + int16_t w; + int16_t h; + const char *src; + }; + + struct ButtonConfig + { + int16_t x; + int16_t y; + int16_t w; + int16_t h; + const char *ViewId; + }; + + SimpleVoiceStartView(); + ~SimpleVoiceStartView(); + UIScrollView *CreateStartView(UIView::OnDragListener *dragListener, UIView::OnTouchListener *touchListener); + + private: + void ClearAll(void); + UILabelExt *InitLabel(LabelConfig &labelconfig); + UIImageView *InitImage(ImageConfig &imageconfig); + UIButton *InitButton(ButtonConfig &buttonconfig, UIView::OnTouchListener *touchListener); + bool CreateScrollView(UIView::OnDragListener *dragListener); + bool CreateLabels(void); + bool CreateImages(void); + bool CreateButtons(UIView::OnTouchListener *touchListener); + + UIScrollView *scrollView{nullptr}; + UILabelExt *title{nullptr}; + UILabelExt *startChatLabel{nullptr}; + UIButton *startChatButton{nullptr}; + UIImageView *buttonBgImage{nullptr}; + }; +} + +#endif diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoiceView.h b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoiceView.h new file mode 100644 index 000000000..931f25741 --- /dev/null +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/include/SimpleVoiceView.h @@ -0,0 +1,58 @@ +#ifndef SIMPLE_VOICE_VIEW_H +#define SIMPLE_VOICE_VIEW_H + +#include +#include "View.h" +#include "components/ui_scroll_view.h" +#include "simplevoice/include/SimpleVoiceStartView.h" +#include "simplevoice/include/SimpleVoiceListeningView.h" +#include "simplevoice/include/SimpleVoicePlayView.h" + +namespace OHOS +{ + typedef enum + { + SIMPLE_VOICE_START_VIEWID = 0, + SIMPLE_VOICE_LISTENING_VIEWID, + SIMPLE_VOICE_PLAY_VIEWID, + MAX_SIMPLE_VOICE_VIEWID, + } SIMPLEVOICEVIEWID; + + class SimpleVoicePresenter; + class SimpleVoiceView : public View + { + public: + SimpleVoiceView(); + virtual ~SimpleVoiceView(); + static SimpleVoiceView *GetInstance(void); + + void OnStart() override; + void OnStop() override; + void ChangeView(int16_t hideView, int16_t showView); + UIScrollView *GetScrollView(int16_t index); + int16_t GetCurrentViewId(void) const { return currentViewId_; } + void SetPlayContent(const std::string &userText, const std::string &aiText); + + bool ChangeStartView(int16_t thisView); + bool ChangeListeningView(int16_t thisView); + bool ChangePlayView(int16_t thisView); + + private: + bool CheckViewParam(int16_t hideView, int16_t showView); + bool ClickFunc(int16_t interface); + void StopListeningAnimation(void); + void DestroyTempView(int16_t hideView); + void ClearAll(void); + + std::string userQuestion_; + std::string aiAnswer_; + + UIScrollView *scrollViews_[MAX_SIMPLE_VOICE_VIEWID]{nullptr}; + SimpleVoiceStartView *startView_{nullptr}; + SimpleVoiceListeningView *listeningView_{nullptr}; + SimpleVoicePlayView *playView_{nullptr}; + int16_t currentViewId_{SIMPLE_VOICE_START_VIEWID}; + }; +} + +#endif diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoiceListeningView.cpp b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoiceListeningView.cpp new file mode 100644 index 000000000..5bc1483a8 --- /dev/null +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoiceListeningView.cpp @@ -0,0 +1,282 @@ +#include "simplevoice/include/SimpleVoiceListeningView.h" +#include "wearable_log.h" +#include "ui_resource_string.h" +#include "UiConfig.h" +#include "uicomponents/UIComponents.h" + +namespace OHOS +{ + static const char *kListeningFrames[] = { + "/user/res/AIVoice/AIVoiceListening/1.bin", + "/user/res/AIVoice/AIVoiceListening/2.bin", + "/user/res/AIVoice/AIVoiceListening/3.bin", + "/user/res/AIVoice/AIVoiceListening/4.bin", + "/user/res/AIVoice/AIVoiceListening/5.bin", + "/user/res/AIVoice/AIVoiceListening/6.bin", + "/user/res/AIVoice/AIVoiceListening/7.bin", + "/user/res/AIVoice/AIVoiceListening/8.bin", + "/user/res/AIVoice/AIVoiceListening/9.bin", + "/user/res/AIVoice/AIVoiceListening/10.bin", + "/user/res/AIVoice/AIVoiceListening/11.bin", + "/user/res/AIVoice/AIVoiceListening/12.bin", + "/user/res/AIVoice/AIVoiceListening/13.bin", + "/user/res/AIVoice/AIVoiceListening/14.bin", + "/user/res/AIVoice/AIVoiceListening/15.bin", + "/user/res/AIVoice/AIVoiceListening/16.bin", + "/user/res/AIVoice/AIVoiceListening/17.bin", + "/user/res/AIVoice/AIVoiceListening/18.bin", + "/user/res/AIVoice/AIVoiceListening/19.bin", + "/user/res/AIVoice/AIVoiceListening/20.bin", + "/user/res/AIVoice/AIVoiceListening/21.bin", + "/user/res/AIVoice/AIVoiceListening/22.bin", + "/user/res/AIVoice/AIVoiceListening/23.bin", + "/user/res/AIVoice/AIVoiceListening/24.bin", + "/user/res/AIVoice/AIVoiceListening/25.bin", + "/user/res/AIVoice/AIVoiceListening/26.bin", + "/user/res/AIVoice/AIVoiceListening/27.bin", + "/user/res/AIVoice/AIVoiceListening/28.bin", + "/user/res/AIVoice/AIVoiceListening/29.bin", + "/user/res/AIVoice/AIVoiceListening/30.bin", + "/user/res/AIVoice/AIVoiceListening/31.bin", + "/user/res/AIVoice/AIVoiceListening/32.bin", + "/user/res/AIVoice/AIVoiceListening/33.bin", + "/user/res/AIVoice/AIVoiceListening/34.bin", + "/user/res/AIVoice/AIVoiceListening/35.bin", + "/user/res/AIVoice/AIVoiceListening/36.bin", + "/user/res/AIVoice/AIVoiceListening/37.bin", + "/user/res/AIVoice/AIVoiceListening/38.bin", + "/user/res/AIVoice/AIVoiceListening/39.bin", + "/user/res/AIVoice/AIVoiceListening/40.bin", + }; + + SimpleVoiceListeningView::SimpleVoiceListeningView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoiceListeningView::SimpleVoiceListeningView"); + } + + SimpleVoiceListeningView::~SimpleVoiceListeningView() + { + ClearAll(); + } + + void SimpleVoiceListeningView::ClearAll(void) + { + if (scrollView_ == nullptr) + { + return; + } + scrollView_->RemoveAll(); + if (animatorView_ != nullptr) + { + animatorView_->Stop(); + delete animatorView_; + animatorView_ = nullptr; + for (int i = 0; i < SIMPLE_VOICE_LISTENING_ANIM_NUM_FRAMES; i++) + { + if (animInfo_[i].imageInfo != nullptr) + { + ImageCacheManager::GetInstance().UnloadSingleRes(kListeningFrames[i]); + animInfo_[i].imageInfo = nullptr; + } + } + } + delete title_; + title_ = nullptr; + delete finishSpeakLabel_; + finishSpeakLabel_ = nullptr; + // delete timeoutLabel_; + // timeoutLabel_ = nullptr; + delete finishSpeakButton_; + finishSpeakButton_ = nullptr; + delete scrollView_; + scrollView_ = nullptr; + } + + UILabelExt *SimpleVoiceListeningView::InitLabel(LabelConfig &labelconfig) + { + UILabelExt *label = new UILabelExt(); + if (label == nullptr) + { + return nullptr; + } + label->SetPosition(labelconfig.left, labelconfig.top, labelconfig.width, labelconfig.height); + label->SetStyle(STYLE_TEXT_FONT, labelconfig.fontSize); + label->SetStyle(STYLE_LINE_HEIGHT, labelconfig.lineHeigh); + label->SetTextId(labelconfig.textId); + label->SetLineBreakMode(labelconfig.lineBreakMode); + label->SetRollSpeed(23); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, labelconfig.fontSize); + return label; + } + + UIButton *SimpleVoiceListeningView::InitButton(ButtonConfig &buttonconfig) + { + UIButton *button = new UIButton(); + if (button == nullptr) + { + return nullptr; + } + button->SetPosition(buttonconfig.x, buttonconfig.y, buttonconfig.w, buttonconfig.h); + button->SetViewId(buttonconfig.ViewId); + button->SetStyle(STYLE_BACKGROUND_COLOR, 0xff191919); + button->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + button->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xff191919, UIButton::PRESSED); + return button; + } + + bool SimpleVoiceListeningView::CreateScrollView(UIView::OnDragListener *dragListener) + { + scrollView_ = new UIScrollView(); + if (scrollView_ == nullptr) + { + return false; + } + scrollView_->SetPosition(0, 0, 466, 466); + scrollView_->SetTouchable(true); + scrollView_->SetDraggable(true); + if (dragListener != nullptr) + { + scrollView_->SetOnDragListener(dragListener); + } + return true; + } + + bool SimpleVoiceListeningView::CreateLabels(void) + { + LabelConfig labelConfig = { + .left = 83, + .top = 50, + .width = 300, + .height = 35, + .fontSize = 30, + .lineHeigh = 35, + .textId = STR_LISTENING, + .lineBreakMode = UILabel::LINE_BREAK_OSCILLATION, + }; + title_ = InitLabel(labelConfig); + if (title_ == nullptr) + { + return false; + } + + labelConfig = { + .left = 133, + .top = 368, + .width = 200, + .height = 35, + .fontSize = 30, + .lineHeigh = 35, + .textId = STR_FINISH_SPEAKING, + .lineBreakMode = UILabel::LINE_BREAK_OSCILLATION, + }; + finishSpeakLabel_ = InitLabel(labelConfig); + if (finishSpeakLabel_ == nullptr) + { + return false; + } + + // timeoutLabel_ = new UILabelExt(); + // if (timeoutLabel_ == nullptr) + // { + // return false; + // } + // timeoutLabel_->SetPosition(60, 258, 240, 60); + // timeoutLabel_->SetStyle(STYLE_TEXT_FONT, 50); + // timeoutLabel_->SetStyle(STYLE_LINE_HEIGHT, 60); + // timeoutLabel_->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + // timeoutLabel_->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + // timeoutLabel_->SetFont(DEFAULT_VECTOR_FONT_FILENAME_EN, 50); + // timeoutLabel_->SetText("10"); + return true; + } + + bool SimpleVoiceListeningView::CreateButton(UIView::OnTouchListener *touchListener) + { + ButtonConfig buttonConfig = { + .x = 93, + .y = 340, + .w = 280, + .h = 90, + .ViewId = SIMPLE_VOICE_FINISH_SPEAKING_BUTTON_VIEW_ID, + }; + finishSpeakButton_ = InitButton(buttonConfig); + if (finishSpeakButton_ == nullptr || touchListener == nullptr) + { + return false; + } + finishSpeakButton_->SetOnTouchListener(touchListener); + return true; + } + + bool SimpleVoiceListeningView::CreateImageAnimator(void) + { + animatorView_ = new UIImageAnimatorView(); + if (animatorView_ == nullptr) + { + return false; + } + for (uint8_t i = 0; i < SIMPLE_VOICE_LISTENING_ANIM_NUM_FRAMES; i++) + { + animInfo_[i].imageInfo = ImageCacheManager::GetInstance().LoadSingleRes(kListeningFrames[i]); + animInfo_[i].imageType = IMG_SRC_IMAGE_INFO; + animInfo_[i].pos.x = SIMPLE_VOICE_LISTENING_ANIM_X; + animInfo_[i].pos.y = SIMPLE_VOICE_LISTENING_ANIM_Y; + animInfo_[i].width = SIMPLE_VOICE_LISTENING_ANIM_W; + animInfo_[i].height = SIMPLE_VOICE_LISTENING_ANIM_H; + } + animatorView_->SetPosition(SIMPLE_VOICE_LISTENING_ANIM_X, SIMPLE_VOICE_LISTENING_ANIM_Y, + SIMPLE_VOICE_LISTENING_ANIM_W, SIMPLE_VOICE_LISTENING_ANIM_H); + animatorView_->SetImageAnimatorSrc(animInfo_, SIMPLE_VOICE_LISTENING_ANIM_NUM_FRAMES, + SIMPLE_VOICE_LISTENING_ANIM_DELAY); + animatorView_->SetRepeat(true); + animatorView_->SetAutoEnable(false); + animatorView_->SetResizeMode(UIImageView::FILL); + return true; + } + + void SimpleVoiceListeningView::StartOrStopAnimation(bool start) + { + if (animatorView_ == nullptr) + { + return; + } + if (start) + { + animatorView_->Start(); + } + else + { + animatorView_->Stop(); + } + } + + // UILabelExt *SimpleVoiceListeningView::GetTimeoutLabel(void) + // { + // return timeoutLabel_; + // } + + UIScrollView *SimpleVoiceListeningView::CreateListeningView(UIView::OnDragListener *dragListener, + UIView::OnTouchListener *touchListener) + { + if (scrollView_ != nullptr) + { + return scrollView_; + } + if (!CreateScrollView(dragListener) || !CreateLabels() || !CreateButton(touchListener) || !CreateImageAnimator()) + { + ClearAll(); + return nullptr; + } + scrollView_->Add(title_); + scrollView_->Add(animatorView_); + // if (timeoutLabel_ != nullptr) + // { + // timeoutLabel_->SetX((scrollView_->GetWidth() - timeoutLabel_->GetWidth()) / 2); + // scrollView_->Add(timeoutLabel_); + // } + scrollView_->Add(finishSpeakButton_); + scrollView_->Add(finishSpeakLabel_); + return scrollView_; + } +} diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoicePlayView.cpp b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoicePlayView.cpp new file mode 100644 index 000000000..c99b7611c --- /dev/null +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoicePlayView.cpp @@ -0,0 +1,261 @@ +#include "simplevoice/include/SimpleVoicePlayView.h" +#include "wearable_log.h" +#include "ui_resource_string.h" +#include "UiConfig.h" + +namespace OHOS +{ + static constexpr const int16_t CHAT_LIST_X = 53; + static constexpr const int16_t CHAT_LIST_Y = 100; + static constexpr const int16_t CHAT_LIST_W = 360; + static constexpr const int16_t CHAT_LIST_H = 200; + static constexpr const int16_t MAX_SCR_DIS = 1000; + + static constexpr const int16_t RETURN_BUTTON_X = 73; + static constexpr const int16_t RETURN_BUTTON_Y = CHAT_LIST_Y + CHAT_LIST_H + 10; + static constexpr const int16_t RETURN_BUTTON_W = 100; + static constexpr const int16_t RETURN_BUTTON_H = 100; + static constexpr const char *RETURN_BUTTON_PATH = "/user/res/AIVoice/AI_Union.bin"; + + static constexpr const int16_t CONTINUE_BUTTON_X = 183; + static constexpr const int16_t CONTINUE_BUTTON_Y = CHAT_LIST_Y + CHAT_LIST_H + 20; + static constexpr const int16_t CONTINUE_BUTTON_W = 210; + static constexpr const int16_t CONTINUE_BUTTON_H = 100; + + static constexpr const int16_t CONTINUE_LABEL_X = CONTINUE_BUTTON_X + 45; + static constexpr const int16_t CONTINUE_LABEL_Y = CONTINUE_BUTTON_Y + 33; + static constexpr const int16_t CONTINUE_LABEL_W = 120; + static constexpr const int16_t CONTINUE_LABEL_H = 35; + + SimpleVoicePlayView::SimpleVoicePlayView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoicePlayView::SimpleVoicePlayView"); + } + + SimpleVoicePlayView::~SimpleVoicePlayView() + { + ClearAll(); + } + + void SimpleVoicePlayView::ClearAll(void) + { + if (scrollView_ == nullptr) + { + return; + } + scrollView_->RemoveAll(); + delete title_; + title_ = nullptr; + delete continueChatLabel_; + continueChatLabel_ = nullptr; + delete returnButton_; + returnButton_ = nullptr; + delete continueChatButton_; + continueChatButton_ = nullptr; + delete chatAdapter_; + chatAdapter_ = nullptr; + delete chatList_; + chatList_ = nullptr; + delete scrollView_; + scrollView_ = nullptr; + } + + UILabelExt *SimpleVoicePlayView::InitLabel(LabelConfig &labelconfig) + { + UILabelExt *label = new UILabelExt(); + if (label == nullptr) + { + return nullptr; + } + label->SetPosition(labelconfig.left, labelconfig.top, labelconfig.width, labelconfig.height); + label->SetStyle(STYLE_TEXT_FONT, labelconfig.fontSize); + label->SetStyle(STYLE_LINE_HEIGHT, labelconfig.lineHeight); + label->SetTextId(labelconfig.textId); + label->SetLineBreakMode(labelconfig.lineBreakMode); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, labelconfig.fontSize); + label->SetRollSpeed(70); + return label; + } + + UIButton *SimpleVoicePlayView::InitButton(ButtonConfig &buttonconfig) + { + UIButton *button = new UIButton(); + if (button == nullptr) + { + return nullptr; + } + button->SetPosition(buttonconfig.x, buttonconfig.y, buttonconfig.w, buttonconfig.h); + button->SetViewId(buttonconfig.ViewId); + button->SetStyle(STYLE_BACKGROUND_COLOR, 0xff191919); + button->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + button->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xff383838, UIButton::PRESSED); + return button; + } + + bool SimpleVoicePlayView::CreateScrollView(UIView::OnDragListener *dragListener) + { + scrollView_ = new UIScrollView(); + if (scrollView_ == nullptr) + { + return false; + } + scrollView_->SetPosition(0, 0, 466, 466); + scrollView_->SetTouchable(true); + scrollView_->SetDraggable(true); + scrollView_->SetDirection(UIScrollView::VERTICAL); + if (dragListener != nullptr) + { + scrollView_->SetOnDragListener(dragListener); + } + return true; + } + + bool SimpleVoicePlayView::CreateLabels(void) + { + LabelConfig labelConfig = { + .left = 83, + .top = 50, + .width = 300, + .height = 35, + .fontSize = 30, + .lineHeight = 35, + .textId = STR_AI_VOICE, + .lineBreakMode = UILabel::LINE_BREAK_OSCILLATION, + }; + title_ = InitLabel(labelConfig); + if (title_ == nullptr) + { + return false; + } + title_->SetText("AI对话"); + + labelConfig = { + .left = CONTINUE_LABEL_X, + .top = CONTINUE_LABEL_Y, + .width = CONTINUE_LABEL_W, + .height = CONTINUE_LABEL_H, + .fontSize = 30, + .lineHeight = 35, + .textId = STR_CONTINUE_CHAT, + .lineBreakMode = UILabel::LINE_BREAK_OSCILLATION, + }; + continueChatLabel_ = InitLabel(labelConfig); + return continueChatLabel_ != nullptr; + } + + bool SimpleVoicePlayView::CreateChatList(UIView::OnClickListener *clickListener) + { + chatAdapter_ = new AivoiceChatContentListAdapter(); + if (chatAdapter_ == nullptr) + { + return false; + } + if (clickListener != nullptr) + { + chatAdapter_->SetItemClickListener(clickListener); + } + + chatList_ = new UIList(UIList::VERTICAL); + if (chatList_ == nullptr) + { + return false; + } + chatList_->SetPosition(CHAT_LIST_X, CHAT_LIST_Y, CHAT_LIST_W, CHAT_LIST_H); + chatList_->SetStyle(STYLE_BACKGROUND_COLOR, 0xff000000); + chatList_->EnableAutoAlign(true); + chatList_->SetTouchable(true); + chatList_->SetDraggable(true); + chatList_->SetThrowDrag(true); + chatList_->SetMaxScrollDistance(MAX_SCR_DIS); + chatList_->SetSwipeACCLevel(200); + chatList_->SetDragACCLevel(5); + chatList_->SetAdapter(chatAdapter_); + chatList_->RefreshList(); + return true; + } + + bool SimpleVoicePlayView::CreateButtons(UIView::OnTouchListener *touchListener) + { + if (touchListener == nullptr) + { + return false; + } + + ButtonConfig buttonConfig = { + .x = RETURN_BUTTON_X, + .y = RETURN_BUTTON_Y, + .w = RETURN_BUTTON_W, + .h = RETURN_BUTTON_H, + .ViewId = SIMPLE_VOICE_RETURN_BUTTON_VIEW_ID, + }; + returnButton_ = InitButton(buttonConfig); + if (returnButton_ == nullptr) + { + return false; + } + returnButton_->SetImagePosition(39, 39); + returnButton_->SetImageSrc(RETURN_BUTTON_PATH, RETURN_BUTTON_PATH); + returnButton_->SetOnTouchListener(touchListener); + + buttonConfig = { + .x = CONTINUE_BUTTON_X, + .y = CONTINUE_BUTTON_Y, + .w = CONTINUE_BUTTON_W, + .h = CONTINUE_BUTTON_H, + .ViewId = SIMPLE_VOICE_CONTINUE_CHAT_BUTTON_VIEW_ID, + }; + continueChatButton_ = InitButton(buttonConfig); + if (continueChatButton_ == nullptr) + { + return false; + } + continueChatButton_->SetOnTouchListener(touchListener); + return true; + } + + void SimpleVoicePlayView::SetChatContent(const std::string &userText, const std::string &aiText) + { + if (chatAdapter_ == nullptr) + { + return; + } + chatAdapter_->ClearData(); + if (!userText.empty()) + { + ChatContentItem userItem = {0, userText, false}; + chatAdapter_->AddListItem(userItem); + } + if (!aiText.empty()) + { + ChatContentItem aiItem = {1, aiText, true}; + chatAdapter_->AddListItem(aiItem); + } + if (chatList_ != nullptr) + { + chatList_->RefreshList(); + } + } + + UIScrollView *SimpleVoicePlayView::CreatePlayView(UIView::OnDragListener *dragListener, + UIView::OnTouchListener *touchListener, + UIView::OnClickListener *clickListener) + { + if (scrollView_ != nullptr) + { + return scrollView_; + } + if (!CreateScrollView(dragListener) || !CreateLabels() || !CreateChatList(clickListener) || + !CreateButtons(touchListener)) + { + ClearAll(); + return nullptr; + } + scrollView_->Add(title_); + scrollView_->Add(chatList_); + scrollView_->Add(returnButton_); + scrollView_->Add(continueChatButton_); + scrollView_->Add(continueChatLabel_); + return scrollView_; + } +} diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoicePresenter.cpp b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoicePresenter.cpp new file mode 100644 index 000000000..f9b414c34 --- /dev/null +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoicePresenter.cpp @@ -0,0 +1,154 @@ +#include "AppViewIDs.h" +#include "NativeAbility.h" +#include "NativeRegisterManager.h" +#include "UiConfig.h" +#include "simplevoice/include/SimpleVoicePresenter.h" +#include "simplevoice/include/SimpleVoiceView.h" +#include "simplevoice/include/SimpleVoiceStartView.h" +#include "simplevoice/include/SimpleVoiceListeningView.h" +#include "simplevoice/include/SimpleVoicePlayView.h" +#include "wearable_log.h" +#include "ui_resource_string.h" +#include +#include +#include "ct_ai_api.h" + +namespace OHOS +{ + REGIST_MENU_PRO(VIEW_SIMPLE_VOICE, SimpleVoiceView, SimpleVoicePresenter, + PNG_APPLIST_AIVOICE_IMAGE_BIG, PNG_APPLIST_AIVOICE_IMAGE, PNG_APPLIST_AIVOICE_IMAGE, + "语音对话", STR_AI_VOICE); + + static SimpleVoicePresenter *g_pSimpleVoicePresenter = nullptr; + + SimpleVoicePresenter::SimpleVoicePresenter() + { + g_pSimpleVoicePresenter = this; + } + + SimpleVoicePresenter::~SimpleVoicePresenter() + { + g_pSimpleVoicePresenter = nullptr; + } + + SimpleVoicePresenter *SimpleVoicePresenter::GetInstance() + { + return g_pSimpleVoicePresenter; + } + + void SimpleVoicePresenter::OnStart() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoicePresenter::OnStart"); + ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON); + // ct_ai_ws_audio_init(false); + } + + void SimpleVoicePresenter::OnStop() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoicePresenter::OnStop"); + // if (view_ != nullptr && view_->GetCurrentViewId() == SIMPLE_VOICE_LISTENING_VIEWID) + // { + // ct_ai_ws_voice_stream_end(); + // } + // ct_ai_ws_audio_deinit(); + ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON); + } + + void SimpleVoicePresenter::ChangeView(int16_t hideView, int16_t showView) + { + if (hideView == showView) + { + return; + } + if (view_ != nullptr) + { + view_->ChangeView(hideView, showView); + } + } + + bool SimpleVoicePresenter::OnClick(UIView &view, const ClickEvent &event) + { + UNUSED(view); + UNUSED(event); + return true; + } + + bool SimpleVoicePresenter::OnPress(UIView &view, const PressEvent &event) + { + UNUSED(event); + std::string viewId = view.GetViewId(); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoicePresenter::OnPress %s", viewId.c_str()); + + if (viewId == SIMPLE_VOICE_START_CHAT_BUTTON_VIEW_ID) + { + ChangeView(SIMPLE_VOICE_START_VIEWID, SIMPLE_VOICE_LISTENING_VIEWID); + return true; + } + if (viewId == SIMPLE_VOICE_FINISH_SPEAKING_BUTTON_VIEW_ID) + { + if (view_ != nullptr) + { + view_->SetPlayContent("你好", "你好,有什么可以帮助你的吗?"); + ChangeView(SIMPLE_VOICE_LISTENING_VIEWID, SIMPLE_VOICE_PLAY_VIEWID); + } + return true; + } + if (viewId == SIMPLE_VOICE_RETURN_BUTTON_VIEW_ID) + { + ChangeView(SIMPLE_VOICE_PLAY_VIEWID, SIMPLE_VOICE_START_VIEWID); + return true; + } + if (viewId == SIMPLE_VOICE_CONTINUE_CHAT_BUTTON_VIEW_ID) + { + ChangeView(SIMPLE_VOICE_PLAY_VIEWID, SIMPLE_VOICE_LISTENING_VIEWID); + return true; + } + return true; + } + + bool SimpleVoicePresenter::OnRelease(UIView &view, const ReleaseEvent &event) + { + UNUSED(view); + UNUSED(event); + return true; + } + + bool SimpleVoicePresenter::OnDrag(UIView &view, const DragEvent &event) + { + UNUSED(view); + if ((event.GetDeltaX() > X_DRAG_OFFSET) && (event.GetDeltaX() > (DOUBLES * MATH_ABS(event.GetDeltaY())))) + { + xDragFlag_ = true; + } + return true; + } + + bool SimpleVoicePresenter::OnDragEnd(UIView &view, const DragEvent &event) + { + UNUSED(view); + if (event.GetDragDirection() != DragEvent::DIRECTION_LEFT_TO_RIGHT || !xDragFlag_) + { + xDragFlag_ = false; + return true; + } + + UIScrollView *scrollView = dynamic_cast(&view); + if (scrollView == nullptr || view_ == nullptr) + { + xDragFlag_ = false; + return false; + } + + if (scrollView == view_->GetScrollView(SIMPLE_VOICE_START_VIEWID)) + { + NativeAbility::GetInstance().Back(); + } + else if (scrollView == view_->GetScrollView(SIMPLE_VOICE_PLAY_VIEWID)) + { + ChangeView(SIMPLE_VOICE_PLAY_VIEWID, SIMPLE_VOICE_START_VIEWID); + } + + xDragFlag_ = false; + return true; + } +} diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoiceStartView.cpp b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoiceStartView.cpp new file mode 100644 index 000000000..c7556d015 --- /dev/null +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoiceStartView.cpp @@ -0,0 +1,188 @@ +#include "simplevoice/include/SimpleVoiceStartView.h" +#include "wearable_log.h" +#include "ui_resource_string.h" +#include "UiConfig.h" + +namespace OHOS +{ + SimpleVoiceStartView::SimpleVoiceStartView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoiceStartView::SimpleVoiceStartView"); + } + + SimpleVoiceStartView::~SimpleVoiceStartView() + { + ClearAll(); + } + + void SimpleVoiceStartView::ClearAll(void) + { + if (scrollView == nullptr) + { + return; + } + scrollView->RemoveAll(); + delete title; + title = nullptr; + delete startChatLabel; + startChatLabel = nullptr; + delete startChatButton; + startChatButton = nullptr; + delete buttonBgImage; + buttonBgImage = nullptr; + delete scrollView; + scrollView = nullptr; + } + + UILabelExt *SimpleVoiceStartView::InitLabel(LabelConfig &labelconfig) + { + UILabelExt *label = new UILabelExt(); + if (label == nullptr) + { + return nullptr; + } + label->SetPosition(labelconfig.left, labelconfig.top, labelconfig.width, labelconfig.height); + label->SetStyle(STYLE_TEXT_FONT, labelconfig.fontSize); + label->SetStyle(STYLE_LINE_HEIGHT, labelconfig.lineHeigh); + label->SetTextId(labelconfig.textId); + label->SetLineBreakMode(labelconfig.lineBreakMode); + label->SetRollSpeed(23); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, labelconfig.fontSize); + return label; + } + + UIImageView *SimpleVoiceStartView::InitImage(ImageConfig &imageconfig) + { + UIImageView *image = new UIImageView(); + if (image == nullptr) + { + return nullptr; + } + image->SetPosition(imageconfig.x, imageconfig.y, imageconfig.w, imageconfig.h); + image->SetSrc(imageconfig.src); + return image; + } + + UIButton *SimpleVoiceStartView::InitButton(ButtonConfig &buttonconfig, UIView::OnTouchListener *touchListener) + { + UIButton *button = new UIButton(); + if (button == nullptr) + { + return nullptr; + } + button->SetPosition(buttonconfig.x, buttonconfig.y, buttonconfig.w, buttonconfig.h); + button->SetViewId(buttonconfig.ViewId); + button->SetStyle(STYLE_BACKGROUND_COLOR, 0xff4fc34c); + button->SetStyle(STYLE_BACKGROUND_OPA, 0x00); + button->SetStyleForState(STYLE_BACKGROUND_COLOR, 0x00, UIButton::PRESSED); + if (touchListener != nullptr) + { + button->SetOnTouchListener(touchListener); + } + return button; + } + + bool SimpleVoiceStartView::CreateScrollView(UIView::OnDragListener *dragListener) + { + scrollView = new UIScrollView(); + if (scrollView == nullptr) + { + return false; + } + scrollView->SetPosition(0, 0, 466, 466); + scrollView->SetHorizontalScrollState(false); + scrollView->SetVerticalScrollState(false); + scrollView->SetTouchable(true); + scrollView->SetDraggable(true); + if (dragListener != nullptr) + { + scrollView->SetOnDragListener(dragListener); + } + return true; + } + + bool SimpleVoiceStartView::CreateLabels(void) + { + LabelConfig labelConfig = { + .left = 83, + .top = 50, + .width = 300, + .height = 35, + .fontSize = 30, + .lineHeigh = 35, + .textId = STR_AI_VOICE, + .lineBreakMode = UILabel::LINE_BREAK_OSCILLATION, + }; + title = InitLabel(labelConfig); + if (title == nullptr) + { + return false; + } + title->SetText("AI对话"); + + labelConfig = { + .left = 133, + .top = 348, + .width = 200, + .height = 35, + .fontSize = 30, + .lineHeigh = 35, + .textId = STR_START_CHAT, + .lineBreakMode = UILabel::LINE_BREAK_OSCILLATION, + }; + startChatLabel = InitLabel(labelConfig); + return startChatLabel != nullptr; + } + + bool SimpleVoiceStartView::CreateImages(void) + { + ImageConfig imageConfig = { + .x = 208, + .y = 340, + .w = 48, + .h = 56, + .src = "/user/res/AIVoice/ai_chart_icon.bin", + }; + buttonBgImage = InitImage(imageConfig); + return buttonBgImage != nullptr; + } + + bool SimpleVoiceStartView::CreateButtons(UIView::OnTouchListener *touchListener) + { + ButtonConfig buttonConfig = { + .x = 43, + .y = 328, + .w = 380, + .h = 80, + .ViewId = SIMPLE_VOICE_START_CHAT_BUTTON_VIEW_ID, + }; + startChatButton = InitButton(buttonConfig, touchListener); + if (startChatButton == nullptr) + { + return false; + } + const char *image = "/user/res/AIVoice/ai_chart_icon_bg.bin"; + startChatButton->SetImageSrc(image, image); + return true; + } + + UIScrollView *SimpleVoiceStartView::CreateStartView(UIView::OnDragListener *dragListener, + UIView::OnTouchListener *touchListener) + { + if (scrollView != nullptr) + { + return scrollView; + } + if (!CreateScrollView(dragListener) || !CreateLabels() || !CreateImages() || !CreateButtons(touchListener)) + { + ClearAll(); + return nullptr; + } + scrollView->Add(title); + scrollView->Add(startChatButton); + scrollView->Add(startChatLabel); + scrollView->Add(buttonBgImage); + return scrollView; + } +} diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoiceView.cpp b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoiceView.cpp new file mode 100644 index 000000000..e2e01daaf --- /dev/null +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/simplevoice/src/SimpleVoiceView.cpp @@ -0,0 +1,264 @@ +#include "simplevoice/include/SimpleVoiceView.h" +#include "simplevoice/include/SimpleVoicePresenter.h" +#include "wearable_log.h" + +namespace OHOS +{ + static SimpleVoiceView *g_pSimpleVoiceView{nullptr}; + + SimpleVoiceView::SimpleVoiceView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoiceView::SimpleVoiceView"); + g_pSimpleVoiceView = this; + } + + SimpleVoiceView::~SimpleVoiceView() + { + ClearAll(); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoiceView::~SimpleVoiceView"); + g_pSimpleVoiceView = nullptr; + } + + SimpleVoiceView *SimpleVoiceView::GetInstance(void) + { + return g_pSimpleVoiceView; + } + + void SimpleVoiceView::ClearAll(void) + { + StopListeningAnimation(); + + if (listeningView_ != nullptr) + { + delete listeningView_; + listeningView_ = nullptr; + } + if (playView_ != nullptr) + { + delete playView_; + playView_ = nullptr; + } + if (startView_ != nullptr) + { + delete startView_; + startView_ = nullptr; + } + for (int16_t i = 0; i < MAX_SIMPLE_VOICE_VIEWID; i++) + { + scrollViews_[i] = nullptr; + } + } + + void SimpleVoiceView::OnStop() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoiceView::OnStop"); + StopListeningAnimation(); + } + + void SimpleVoiceView::OnStart() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoiceView::OnStart"); + ChangeView(MAX_SIMPLE_VOICE_VIEWID, SIMPLE_VOICE_START_VIEWID); + } + + bool SimpleVoiceView::CheckViewParam(int16_t hideView, int16_t showView) + { + if (showView < 0 || showView >= MAX_SIMPLE_VOICE_VIEWID) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SimpleVoiceView::CheckViewParam showView err."); + return false; + } + if (hideView < 0 || hideView > MAX_SIMPLE_VOICE_VIEWID) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SimpleVoiceView::CheckViewParam hideView err."); + return false; + } + return true; + } + + bool SimpleVoiceView::ChangeStartView(int16_t thisView) + { + if (scrollViews_[thisView] == nullptr) + { + startView_ = new SimpleVoiceStartView(); + if (startView_ == nullptr) + { + return false; + } + UIScrollView *scrollview = startView_->CreateStartView(presenter_, presenter_); + if (scrollview == nullptr) + { + delete startView_; + startView_ = nullptr; + return false; + } + scrollViews_[thisView] = scrollview; + AddViewToRootContainer(scrollViews_[thisView]); + } + return true; + } + + bool SimpleVoiceView::ChangeListeningView(int16_t thisView) + { + if (scrollViews_[thisView] == nullptr) + { + listeningView_ = new SimpleVoiceListeningView(); + if (listeningView_ == nullptr) + { + return false; + } + UIScrollView *scrollview = listeningView_->CreateListeningView(presenter_, presenter_); + if (scrollview == nullptr) + { + delete listeningView_; + listeningView_ = nullptr; + return false; + } + scrollViews_[thisView] = scrollview; + AddViewToRootContainer(scrollViews_[thisView]); + } + if (listeningView_ != nullptr) + { + listeningView_->StartOrStopAnimation(true); + // if (listeningView_->GetTimeoutLabel() != nullptr) + // { + // listeningView_->GetTimeoutLabel()->SetText("10"); + // listeningView_->GetTimeoutLabel()->Invalidate(); + // } + } + return true; + } + + bool SimpleVoiceView::ChangePlayView(int16_t thisView) + { + if (scrollViews_[thisView] == nullptr) + { + playView_ = new SimpleVoicePlayView(); + if (playView_ == nullptr) + { + return false; + } + UIScrollView *scrollview = playView_->CreatePlayView(presenter_, presenter_, presenter_); + if (scrollview == nullptr) + { + delete playView_; + playView_ = nullptr; + return false; + } + scrollViews_[thisView] = scrollview; + AddViewToRootContainer(scrollViews_[thisView]); + } + if (playView_ != nullptr) + { + playView_->SetChatContent(userQuestion_, aiAnswer_); + } + return true; + } + + void SimpleVoiceView::SetPlayContent(const std::string &userText, const std::string &aiText) + { + userQuestion_ = userText; + aiAnswer_ = aiText; + } + + void SimpleVoiceView::StopListeningAnimation(void) + { + if (listeningView_ != nullptr) + { + listeningView_->StartOrStopAnimation(false); + } + } + + struct SimpleVoiceViewMapper + { + const int16_t viewId; + bool (SimpleVoiceView::*func)(int16_t thisView); + }; + + static const SimpleVoiceViewMapper kViewMapper[] = { + {SIMPLE_VOICE_START_VIEWID, &SimpleVoiceView::ChangeStartView}, + {SIMPLE_VOICE_LISTENING_VIEWID, &SimpleVoiceView::ChangeListeningView}, + {SIMPLE_VOICE_PLAY_VIEWID, &SimpleVoiceView::ChangePlayView}, + }; + + bool SimpleVoiceView::ClickFunc(int16_t interface) + { + int funcSize = sizeof(kViewMapper) / sizeof(kViewMapper[0]); + for (int i = 0; i < funcSize; i++) + { + if (interface == kViewMapper[i].viewId) + { + return (this->*(kViewMapper[i].func))(interface); + } + } + return false; + } + + void SimpleVoiceView::DestroyTempView(int16_t hideView) + { + UIViewGroup *root = GetRootContainer(); + switch (hideView) + { + case SIMPLE_VOICE_LISTENING_VIEWID: + StopListeningAnimation(); + if (root != nullptr && scrollViews_[hideView] != nullptr) + { + root->Remove(scrollViews_[hideView]); + } + scrollViews_[hideView] = nullptr; + delete listeningView_; + listeningView_ = nullptr; + break; + case SIMPLE_VOICE_PLAY_VIEWID: + if (root != nullptr && scrollViews_[hideView] != nullptr) + { + root->Remove(scrollViews_[hideView]); + } + scrollViews_[hideView] = nullptr; + delete playView_; + playView_ = nullptr; + break; + default: + break; + } + } + + void SimpleVoiceView::ChangeView(int16_t hideView, int16_t showView) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SimpleVoiceView::ChangeView hideView:%d, showView:%d", hideView, showView); + if (!CheckViewParam(hideView, showView)) + { + return; + } + if (hideView != MAX_SIMPLE_VOICE_VIEWID) + { + if (scrollViews_[hideView] == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SimpleVoiceView scrollViews_[%d] is nullptr", hideView); + return; + } + scrollViews_[hideView]->SetVisible(false); + } + if (!ClickFunc(showView)) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SimpleVoiceView::ClickFunc failed for view:%d", showView); + return; + } + if (scrollViews_[showView] != nullptr) + { + scrollViews_[showView]->SetVisible(true); + } + Draw(); + DestroyTempView(hideView); + currentViewId_ = showView; + } + + UIScrollView *SimpleVoiceView::GetScrollView(int16_t index) + { + if (index >= 0 && index < MAX_SIMPLE_VOICE_VIEWID) + { + return scrollViews_[index]; + } + return nullptr; + } +}