From e4c2db34c24e72b2463a7eb39a9f8f02b9f94317 Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Wed, 26 Mar 2025 10:59:22 +0800 Subject: [PATCH] =?utf8?q?=E8=AE=A1=E6=97=B6=E5=99=A8slice=E5=90=88?= =?utf8?q?=E5=B9=B6=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../countdown/include/CountDownModel.h | 157 ++++ .../include/CountDownTimeConfigView.h | 44 ++ .../include/CountDownTimeNotNullView.h | 41 + .../include/MainCountDownPresenter.h | 65 ++ .../countdown/include/MainCountDownView.h | 80 ++ .../countdown/include/StartCountDownView.h | 67 ++ .../nativeui/countdown/src/CountDownModel.cpp | 738 ++++++++++++++++++ .../countdown/src/CountDownTimeConfigView.cpp | 171 ++++ .../src/CountDownTimeNotNullView.cpp | 144 ++++ .../countdown/src/MainCountDownPresenter.cpp | 308 ++++++++ .../countdown/src/MainCountDownView.cpp | 435 +++++++++++ .../countdown/src/StartCountDownView.cpp | 302 +++++++ 12 files changed, 2552 insertions(+) create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownModel.h create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownTimeConfigView.h create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownTimeNotNullView.h create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/MainCountDownPresenter.h create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/MainCountDownView.h create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/StartCountDownView.h create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownModel.cpp create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownTimeConfigView.cpp create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownTimeNotNullView.cpp create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/MainCountDownPresenter.cpp create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/MainCountDownView.cpp create mode 100644 code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/StartCountDownView.cpp diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownModel.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownModel.h new file mode 100644 index 000000000..41c1aa3d6 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownModel.h @@ -0,0 +1,157 @@ +/* + * @Author: luozheng 1732036298@qq.com + * @Date: 2024-04-07 18:15:53 + * @LastEditors: luozheng 1732036298@qq.com + * @LastEditTime: 2024-11-21 16:06:32 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/countdown/include/CountDownModel.h + */ +#ifndef COUNTDOWN_MODEL_H +#define COUNTDOWN_MODEL_H + +#include +#include "source.h" +#include "audio_manager.h" +#include "../../../../../../middleware/services/media/foundation/player/interfaces/kits/player.h" +namespace OHOS +{ + using OHOS::Media::Player; + using OHOS::Media::Source; + using namespace OHOS::Media; + using namespace Audio; + using Audio::AudioManager; + + // 计时器铃声播放监听类 + class CountDownRingPlayerListener : public InterruptListener + { + public: + CountDownRingPlayerListener() {} + ~CountDownRingPlayerListener() {} + + void SetOwner(std::shared_ptr player); + void RestFlag(); + bool IsPaused(); + bool IsDelayed(); + bool IsResumed(); + bool IsBeginStopped(); + bool IsEndStopped(); + void StopCountDownRing(); + void StartCountDownRing(); + void OnInterrupt(int32_t type, int32_t hint) override; + + private: + std::shared_ptr player_{nullptr}; + bool isPaused_{false}; + bool isDelayed_{false}; + bool isResumed_{false}; + bool isBeginStopped_{false}; + bool isEndStopped_{false}; + }; + + /*处理计时器铃声媒体播放事件的类*/ + class CountDownRingPlayerCallbackImpl : public OHOS::Media::PlayerCallback + { + public: + CountDownRingPlayerCallbackImpl() {} + ~CountDownRingPlayerCallbackImpl() {} + /*媒体播放完毕时被调用*/ + void OnPlaybackComplete(); + /*当播放过程中发生错误时,此方法将被调用,提供了错误类型和错误代码以供分析和处理。*/ + void OnError(int32_t errorType, int32_t errorCode); + /*在播放过程中,为处理某些信息而调用该方法,信息类型和额外数据通过参数提供。*/ + void OnInfo(int type, int extra); + /*当跳转操作完成时(例如快进到某个特定位置),该方法被调用*/ + void OnRewindToComplete(); + }; + + class CountDownModel + { + public: + static CountDownModel *GetInstance(); // shared_ptr:当没有引用指向这个实例时会自动释放内存 + + bool CountDownTimerIsEnd(); // 计时器是否结束 + bool GetChanageToStartCountDownView(); + void SetChanageToStartCountDownView(bool val); + bool CountDownViewIsExit(); // 计时器界面是否退出 + void HandCountDownEndOperator(void); + bool GetCountDownTimerIsPause(); + void SetCountDownTimerIsPause(bool val); + + void SetCountDownTime(const std::string &countdowntime); // 设置倒计时时间 + std::string GetCountDownTime(void); // 获取倒计时时间 + std::string GetBackupCountDownTime(void); + void SetRingTime(const std::string &ringtime); // 设置响铃时间 + std::string GetRingTime(void); // 获取响铃时间 + void ParseCountDownTime(const char *countdowntime); + std::string ParseRingTime(const std::string &ringtime); + const char *DisplayCountDownDuration(); + void CountDown_StartTimer(uint32_t interval); + void CountDown_StopTimer(void); + void CountDown_PauseTimer(void); + void CountDown_ResumeTimer(uint32_t interval); + + /*播放器相关函数*/ + int CountDownRingPlayerInit(const char *RingMusicName); + int InitializeAndPlayRingtone(void); + int StartRingtonePlayback(void); + int StopRingtonePlaybackAndReleaseResources(void); + void RequestStartRingtone(void); + void RequestStopRingtone(void); + int ResetRingtonePlayback(void); + + void countdownstart(); + void countdownpause(); + void countdownresume(); + void countdownstop(); + + void SetisRunning(bool val) { + isRunning = val; + } + bool GetisRunning(void) const { + return isRunning; + } + void SetisPaused(bool val) { + isPaused = val; + } + bool GetisPaused(void) const { + return isPaused; + } + uint32_t get_current_time();//获取当前系统时间戳 + public: + static int g_CountDownTimes; + static int totalSeconds; //要到计时总秒数 + static uint32_t startTime; //开始时间戳 + static uint32_t pauseTime; //暂停时间戳 + static uint32_t endTime; //结束时间戳 + + private: + CountDownModel() {} + ~CountDownModel() {} + CountDownModel(const CountDownModel &) = delete; + CountDownModel &operator=(const CountDownModel &) = delete; + + std::string uri; + std::shared_ptr playerCallback{nullptr}; + std::shared_ptr playerCtr{nullptr}; + std::map header; + std::shared_ptr source{nullptr}; + std::shared_ptr playerInterruptListener{nullptr}; + AudioSession sessionId; + AudioInterrupt interrupt; + // const char *RingMusicPath = "/user/music/alarm.mp3"; // 铃声路径 +#ifdef SINGLE_USER_PARTITION + const char *RingMusicPath = "/user/music/ring/alarm.mp3"; // 铃声路径 +#else + const char *RingMusicPath = "/music/ring/alarm.mp3"; // 铃声路径 +#endif + std::string countdownDuration{nullptr}; + std::string RingTime{"00:00:00"}; // 响铃时间 + std::string CountTime{"00:00:00"}; // 倒计时时间 + std::string CountTimeBackup{"00:00:00"}; + bool IsChanageToStartCountDownViewFlag{false};//是否需要切换到开始倒计时界面标志 + bool CountDownTimerIsPauseFlag{false}; + + bool isRunning{false};//记录倒计时是否正在运行 + bool isPaused{false}; //记录倒计时是否暂停 + }; +} +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownTimeConfigView.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownTimeConfigView.h new file mode 100644 index 000000000..4850b2b40 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownTimeConfigView.h @@ -0,0 +1,44 @@ +/* + * @Author: luozheng 1732036298@qq.com + * @Date: 2024-04-07 18:15:53 + * @LastEditors: luozheng 1732036298@qq.com + * @LastEditTime: 2025-02-21 15:22:25 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/countdown/include/CountDownTimeConfigView.h + */ +#ifndef COUNTDOWN_TIME_CONFIG_VIEW_H +#define COUNTDOWN_TIME_CONFIG_VIEW_H + +#include "common/screen.h" +#include "View.h" +#include "components/ui_view_group.h" +#include "components/ui_scroll_view.h" +#include "components/ui_label_ext.h" +#include "components/ui_time_picker.h" +#include "components/ui_image_view.h" +#include "components/ui_button.h" +/*倒计时时间配置*/ +namespace OHOS +{ + static constexpr const char *COLONEIMAGESPATH = "/user/res/countdown/COUNTDOWN_COLONE.bin"; // 冒号图片 + static constexpr const char *START_COUNTDOWN_BUTTON_IMAGEPATH = "/user/res/countdown/COUNTDOWM_PAUSE.bin"; // 开始倒计时按钮图片 + static constexpr const char *STARTCOUNTDOWN_BUTTON = "startCountdownButton"; // 开始倒计时按钮视图id + static constexpr const char *COUNTDOWN_TIMECONFIG_VIEW = "CountDownTimeConfigView"; + class CountDownTimeConfigView + { + public: + CountDownTimeConfigView(); + virtual ~CountDownTimeConfigView(); + static CountDownTimeConfigView *GetInstance(void); + UIScrollView *CountDownTimeConfigViewInit(void); + void ClearAll(void); + UITimePicker * GetTimePicker(void); + + private: + UIScrollView *scrollView = nullptr; + UILabelExt *Title = nullptr; + UITimePicker *timePicker = nullptr; + UIImageView *Img[2]{nullptr}; + UIButton *startCountdownButton{nullptr}; + }; +} +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownTimeNotNullView.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownTimeNotNullView.h new file mode 100644 index 000000000..c216368e4 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/CountDownTimeNotNullView.h @@ -0,0 +1,41 @@ +/* + * @Author: luozheng 1732036298@qq.com + * @Date: 2024-04-07 18:15:53 + * @LastEditors: luozheng 1732036298@qq.com + * @LastEditTime: 2024-04-08 14:52:37 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/countdown/include/CountDownTimeNotNullView.h + */ +#ifndef COUNTDOWN_TIME_NOT_NULL_VIEW_H +#define COUNTDOWN_TIME_NOT_NULL_VIEW_H + + +#include "View.h" +#include "components/ui_scroll_view.h" +#include "components/ui_label_ext.h" +#include "components/ui_image_view.h" +#include "components/ui_label_button_ext.h" + +/*倒计时时间不能为0*/ +namespace OHOS +{ + static constexpr const char *COUNTDOWNTIMENOTNULLVIEWRETURNBUTTON = "ReturnButton"; // 点击该按钮后回到倒计时设置界面 + static constexpr const char *COUNTDOWNTIMENOTNULLVIEW = "CountDownTimeNotNullView"; + class CountDownTimeNotNullView + { + + public: + CountDownTimeNotNullView(); + virtual ~CountDownTimeNotNullView(); + static CountDownTimeNotNullView *GetInstance(void); + UIScrollView * CountDownTimeNotNullViewInit(void); + void ClearAll(void); + + private: + UIScrollView *scrollView = nullptr; + UILabelExt *Title = nullptr; + UIImageView *Attention = nullptr; + UILabelExt *AttentionLabel = nullptr; + UILabelButtonExt *button = nullptr; + }; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/MainCountDownPresenter.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/MainCountDownPresenter.h new file mode 100644 index 000000000..e16f7feef --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/MainCountDownPresenter.h @@ -0,0 +1,65 @@ +/* + * @Author: luozheng 1732036298@qq.com + * @Date: 2024-04-07 18:15:53 + * @LastEditors: luozheng 1732036298@qq.com + * @LastEditTime: 2024-10-19 17:07:09 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/countdown/include/MainCountDownPresenter.h + */ +#ifndef MAIN_COUNTDOWN_PRESENTER_H +#define MAIN_COUNTDOWN_PRESENTER_H + +#include "View.h" +#include "Presenter.h" +#include "MainCountDownView.h" +#include "ui_resource_string.h" +#include "ModelListenerSample.h" +/*倒计时的主界面*/ +namespace OHOS +{ + typedef enum + { + MAIN_COUNTDOWN = 0, // 倒计时主视图 + TIMECONFIG_COUNTDOWN, // 自定义倒计时 + START_COUNTDOWN, // 开始倒计时 + COUNTDOWN_TIMENOTNULL, // 倒计时时间不能为空 + COUNTDOWN_MAX, + } CountDownViewId; + + static constexpr const int COUNTDOWN_TIMER_DURATION = 1000; // 计时器的定时器周期 + class MainCountDownView; + class MainCountDownPresenter : public Presenter, + public UIView::OnClickListener, + public UIView::OnDragListener, + UITimePicker::SelectedListener, + public UICheckBox::OnChangeListener, + public RootView::OnKeyActListener, + public ModelListenerSample + { + public: + MainCountDownPresenter(); + virtual ~MainCountDownPresenter(); + + void OnStart() override; + void OnPause() override; + void OnResume() override; + void OnStop() override; + + static MainCountDownPresenter *GetInstance(); + + private: + // 因为继承了OnClickListener,所以有OnClick属性,至于参数,原函数也这样直接继承; + bool OnClick(UIView &view, const ClickEvent &event) override; + bool OnDrag(UIView &view, const DragEvent &event) override; + bool OnDragEnd(UIView &view, const DragEvent &event) override; + bool OnKeyAct(UIView &view, const KeyEvent &event) override; // 增加按键事件 + bool OnChange(UICheckBox::UICheckBoxState state) override; + void OnTimePickerStoped(UITimePicker &picker) override; + void Notify() override; + + std::string countdowntime{0}; + std::string ringtime{0}; + bool X_DRAG_FLAG{false}; + }; +} + +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/MainCountDownView.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/MainCountDownView.h new file mode 100644 index 000000000..27d9fee0e --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/MainCountDownView.h @@ -0,0 +1,80 @@ +/* + * @Author: luozheng 1732036298@qq.com + * @Date: 2024-04-07 18:15:53 + * @LastEditors: luozheng 1732036298@qq.com + * @LastEditTime: 2025-02-24 18:39:35 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/countdown/include/MainCountDownView.h + */ +#ifndef MAIN_COUNTDOWN_VIEW_H +#define MAIN_COUNTDOWN_VIEW_H + + +#include "View.h" +#include "components/ui_view_group.h" +#include "components/ui_scroll_view.h" +#include "components/ui_label_ext.h" +#include "components/ui_label_button_ext.h" +#include "countdown/include/CountDownTimeConfigView.h" +#include "countdown/include/CountDownTimeNotNullView.h" +#include "countdown/include/StartCountDownView.h" +/*倒计时的主界面*/ +namespace OHOS +{ +#define CountDownImageSrc "/user/res/countdown/" + + // 图片路径 + static const char *COUNTDOWM_TIMER_ICON = CountDownImageSrc "COUNTDOWM_TIMER_ICON.bin"; + + // 视图ID + static constexpr const char *COUNTDOWN_LABELBUTTON_VIEWID = "COUNTDOWN_LABELBUTTON"; // 自定义时间ID + static constexpr const char *COUNTDOWN_ONE_MINUTE_BUTTON_VIEWID = "COUNTDOWN_ONE_MINUTE_BUTTON"; // 1分钟 + static constexpr const char *COUNTDOWN_FIVE_MINUTE_BUTTON_VIEWID = "COUNTDOWN_FIVE_MINUTE_BUTTON"; // 5分钟 + static constexpr const char *COUNTDOWN_TEN_MINUTE_BUTTON_VIEWID = "COUNTDOWN_TEN_MINUTE_BUTTON"; // 10分钟 + static constexpr const char *COUNTDOWN_THIRTY_MINUTE_BUTTON_VIEWID = "COUNTDOWN_THIRTY_MINUTE_BUTTON"; // 30 分钟 + static constexpr const char *MAINCOUNTDOWNVIEW = "MainCountDownView"; +} +namespace OHOS +{ +#define COUNTDOWN_MAX_VIEW 4 + + class MainCountDownPresenter; + class MainCountDownView : public View + { + + public: + MainCountDownView(); + virtual ~MainCountDownView(); + static MainCountDownView *GetInstance(void); + bool ClickFunc(int16_t interface); + bool CheckViewParam(int16_t hideView, int16_t showView); + void ChangeView(int16_t hideView, int16_t showView); + bool ChangeMainCountDown(int16_t thisView); + bool ChangeTimeConfigCountdown(int16_t thisView); + bool ChangeCountdownTimeNotNull(int16_t thisView); + bool ChangeStartCountDown(int16_t thisView); + StartCountDownView *GetStartCountDownView(void); + + public: + void OnStart() override; + + private: + CountDownTimeConfigView *p_CountDownTimeConfigView{nullptr}; + CountDownTimeNotNullView *p_CountDownTimeNotNullView{nullptr}; + StartCountDownView *p_StartCountDownView{nullptr}; + + private: + UIScrollView * MainCountDownViewInit(void); + void ClearAll(void); + UILabelButtonExt *SetUILabelButton(int x, int y, int w, int h, const char *id, const char *text,int textId); + UIScrollView *scrollView[COUNTDOWN_MAX_VIEW]{nullptr}; + UILabelExt *Title{nullptr}; + UILabelExt *QuickStart{nullptr}; + UILabelButtonExt *CUSTOM_COUNTDOWN{nullptr}; // 自定义倒计时 + UILabelButtonExt *COUNTDOWN_ONE_MINUTE_BUTTON = nullptr; + UILabelButtonExt *COUNTDOWN_FIVE_MINUTE_BUTTON = nullptr; + UILabelButtonExt *COUNTDOWN_TEN_MINUTE_BUTTON = nullptr; + UILabelButtonExt *COUNTDOWN_THIRTY_MINUTE_BUTTON = nullptr; + bool viewInitStatus{false}; + }; +} // namespace OHOS +#endif // MainCountDown_PRESENTER_H \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/StartCountDownView.h b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/StartCountDownView.h new file mode 100644 index 000000000..1c721cbf9 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/include/StartCountDownView.h @@ -0,0 +1,67 @@ +/*** + * @Date: 2024-08-06 18:11:47 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-08-27 18:27:41 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/countdown/include/StartCountDownView.h + */ +#ifndef START_COUNTDOWN_VIEW_H +#define START_COUNTDOWN_VIEW_H + + +#include "View.h" + +#include "components/ui_scroll_view.h" +#include "components/ui_label_ext.h" +#include "components/ui_image_view.h" +#include "components/ui_toggle_button.h" +#include "components/ui_button.h" +#include "components/ui_checkbox.h" + +/*开始倒计时*/ +namespace OHOS +{ + static constexpr const char *EndButton_ImagePath = "/user/res/countdown/COUNTDOWM_END.bin"; // 结束返回按钮图片 + static constexpr const char *CountDown_Play_ImagePath = "/user/res/countdown/COUNTDOWM_PLAY.bin"; // 开始倒计时image + static constexpr const char *CountDown_PAUSE_ImagePath = "/user/res/countdown/COUNTDOWM_PAUSE.bin"; // 暂停倒计时image + static constexpr const char *RepeatButtonImagePath = "/user/res/countdown/repeat_button.bin"; + static constexpr const char *VectorImagesPath = "/user/res/countdown/COUNTDOWM_VECTOR.bin"; + static constexpr const char *VectorBigImagesPath = "/user/res/countdown/Vector_Big.bin"; + static constexpr const char *PlayOrPauseToggleButton_ViewID = "PlayOrPauseToggleButton"; // 暂停和开始toggleButton视图id + static constexpr const char *STARTCOUNTDOWNVIEWRETURNBUTTON = "StartCountDownViewReturnButton"; + static constexpr const char *STARTCOUNTDOWNVIEW = "StartCountDownView"; + static constexpr const char *RepeatButtonViewID = "RepeatButtonViewID"; + class StartCountDownView + { + + public: + StartCountDownView(); + virtual ~StartCountDownView(); + static StartCountDownView *GetInstance(void); + UIScrollView *StartCountDownViewInit(void); + void ClearAll(void); + UILabelExt *GetShowCountDownTimelabel(void); // 获取显示倒计时时间标签 + UILabelExt *GetShowTheRingTimelabel(void); // 获取显示响铃时间标签 + UIToggleButton *GetPlayOrPauseToggleButton(void); + UIButton *GetRepeatButton(void); + + void SetEndView(); + void SetImageAndLabel(bool state); + void ShowEndView(bool state); + void UpdateimageAndLabelPosition(void); + private: + UIScrollView *scrollView = nullptr; + UILabelExt *Title = nullptr; + UILabelExt *showCountDownTimelabel = nullptr; + UILabelExt *showTheRingTime = nullptr; + UIImageView *vectorImage = nullptr; + + UILabelExt *showTheEndRingTime = nullptr; + UIImageView *vectorbigImage = nullptr; + + UIToggleButton *play_or_pause_toggleButton = nullptr; + UIButton *endbutton = nullptr; + UIButton *RepeatButton = nullptr; + bool viewInitStatus{false}; + }; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownModel.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownModel.cpp new file mode 100644 index 000000000..f32602644 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownModel.cpp @@ -0,0 +1,738 @@ +/* + * @Author: luozheng 1732036298@qq.com + * @Date: 2024-04-07 18:15:53 + * @LastEditors: luozheng 1732036298@qq.com + * @LastEditTime: 2024-12-18 17:42:19 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/countdown/src/CountDownModel.cpp + */ +#include "countdown/include/CountDownModel.h" +#include +#include +#include +#include "wearable_log.h" +#include "countdown/include/MainCountDownView.h" +#include +#include "ConfigModel.h" +#include "ui_resource_string.h" +#include "custom_runtime.h" +#include "uiservice/include/ui_service.h" +#include "AppViewIDs.h" +#include "NativeAbility.h" +#include +#include +#include +namespace OHOS +{ + /* -------------------------------------------------------------------------- */ + // 声明定时器变量 + static osTimerId_t g_timer = nullptr; + int CountDownModel::g_CountDownTimes = {0}; + int CountDownModel::totalSeconds = 0; //总的倒计时时间 + uint32_t CountDownModel::startTime = 0; + uint32_t CountDownModel::pauseTime = 0; + uint32_t CountDownModel::endTime = 0; + static constexpr int32 NUM_TEN = 10; // 主要用于格式化输出,当小时、分钟或秒小于10时,在其前面添加一个’0’以保持常规时间格式,例如“09:05:03”。 + static constexpr int16 NUM_TIME_MIN = 60; + static constexpr int16 NUM_TIME_HOUR = 3600; + static constexpr int32 NUM_TIME_DAY = 86400; + + /* -------------------------------------------------------------------------- */ + // 定义了 GetInstance() 方法来获取 CountDownModel 的实例,确保只有一个全局实例存在。 + CountDownModel *CountDownModel::GetInstance() + { + static CountDownModel instance; + return &instance; + } + + // 函数用于解析"HH:MM:SS"格式的时间字符串,并更新g_CountDownTimes + void CountDownModel::ParseCountDownTime(const char *countdowntime) + { + int hours, minutes, seconds; + + // 使用sscanf从字符串中读取格式化数据 + // "%2d:%2d:%2d"用于检查两个数字,后面跟一个冒号,这样的模式重复两次,从而捕获三个整数 + if (sscanf(countdowntime, "%2d:%2d:%2d", &hours, &minutes, &seconds) == 3) + { + // 解析成功,现在转换为总秒数 + g_CountDownTimes = hours * NUM_TIME_HOUR + minutes * NUM_TIME_MIN + seconds; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "g_CountDownTimes = %d", g_CountDownTimes); + } + else + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownModel::ParseCountDownTime Parsing failure"); + } + } + + std::string CountDownModel::ParseRingTime(const std::string &timeStr) + { + int hours, minutes, seconds; + std::string formattedTime; + // 使用 sscanf 从字符串中读取时间数据 + if (sscanf(timeStr.c_str(), "%2d:%2d:%2d", &hours, &minutes, &seconds) == 3) + { + // 如果小时部分为0,则格式化去掉小时部分 + if (hours == 0) + { + formattedTime = (minutes < NUM_TEN ? "0" : "") + std::to_string(minutes) + ":" + (seconds < NUM_TEN ? "0" : "") + std::to_string(seconds); + } + else + { + // 如果小时部分不为0,保持原有格式 + formattedTime = timeStr; + } + } + else + { + // 解析失败逻辑处理:可以选择记录错误、返回原字符串或其他合适的处理方式 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TrimTimeFormat Parsing failure: Invalid format"); + formattedTime = timeStr; // 例子中选择返回原字符串 + } + return formattedTime; + } + + // 设置倒计时时间 + void CountDownModel::SetCountDownTime(const std::string &countdowntime) + { + CountTime = countdowntime; + CountTimeBackup = countdowntime; + ParseCountDownTime(CountTime.c_str()); + } + + // 获取倒计时时间 + std::string CountDownModel::GetCountDownTime(void) + { + return CountTime; + } + + std::string CountDownModel::GetBackupCountDownTime(void) + { + return CountTimeBackup; + } + + // 设置响铃时间 + void CountDownModel::SetRingTime(const std::string &ringtime) + { + RingTime = ParseRingTime(ringtime); + } + + // 获取响铃时间 + std::string CountDownModel::GetRingTime(void) + { + return RingTime; + } + + /* + * @brief 初始化音乐播放器以播放一个指定的铃声文件 + * @param {char*} RingMusicName 铃声文件 + */ + int CountDownModel::CountDownRingPlayerInit(const char *RingMusicName) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDownRingPlayerInit"); + /* ------------------------------- 1.播放文件路径设置 ------------------------------- */ + uri = RingMusicName; + /* -------------------------------- 2.播放器对象创建 ------------------------------- */ + if (playerCtr.get() == nullptr) + { + playerCtr = std::make_shared(); + if (playerCtr.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] playerCtr new fail"); + return OHOS_FAILURE; + } + } + /* ------------------------------- 3.播放器回调对象创建 ------------------------------ */ + if (playerCallback.get() == nullptr) + { + playerCallback = std::make_shared(); + if (playerCallback.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] playerCallback new fail"); + return OHOS_FAILURE; + } + } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[CountDownModel] PlayerInit success!"); + return OHOS_SUCCESS; + } + + /* + * @brief 对铃声播放器进行设置并开始播放铃声。 + */ + int CountDownModel::InitializeAndPlayRingtone(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownModel::InitializeAndPlayRingtone"); + /* -------------------------------- 1.检查播放器实例 ------------------------------- */ + int32_t ret; + int32_t state; + if (playerCtr.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] playerCtr.get() == nullptr"); + return OHOS_FAILURE; + } + playerCtr->GetPlayerState(state); + /* -------------------------------- 2.设置播放器回调 ------------------------------- */ + playerCtr->SetPlayerCallback(playerCallback); + /* -------------------------------- 3.创建并设置音频源 -------------------------------- */ + source = std::make_shared(uri, header); + if (source.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] source.get() == fail"); + return OHOS_FAILURE; + } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[CountDownModel] Player url:%s", uri.c_str()); + playerCtr->SetSource(*source); + /* --------------------------------- 4.配置播放器 -------------------------------- */ + playerCtr->SetAudioStreamType(AUDIO_STREAM_NOTIFICATION_SYSTEM); // 设置播放器的音频流类型 + playerCtr->Prepare(); // 准备播放器对象以播放音频。 + /* -------------------------------- 5.请求音频焦点 -------------------------------- */ + AudioManager &amIntance = AudioManager::GetInstance(); + sessionId = amIntance.MakeSessionId(); + if (sessionId == AUDIO_SESSION_ID_NONE) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] sessionId invalid!!"); + return OHOS_FAILURE; + } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[CountDownModel] MakeSessionId = %d", sessionId); + playerInterruptListener = std::make_shared(); // 创建音频中断监听器 + if (playerInterruptListener.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] playerInterruptListener playerInterruptListener.get() == nullptr"); + return OHOS_FAILURE; + } + playerInterruptListener->SetOwner(playerCtr); + interrupt = {AUDIO_STREAM_NOTIFICATION_SYSTEM, sessionId, playerInterruptListener}; + // 激活音频中断 5s超时 + if (amIntance.ActivateAudioInterrupt(interrupt) == INTERRUPT_FAILED) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ActivateAudioInterrupt faild"); + return OHOS_FAILURE; + } + // playerCtr->SetVolume(50, 50); // 将左声道和右声道音量都设置为50 + playerCtr->SetAudioSessionId(sessionId); + // 播放 + ret = playerCtr->Play(); + if(ret == 0) + { + //如果倒计时铃声播放成功,记录该事件 + uapi_save_runtime_log(RUNTIME_LOG_RING_PLAY_START_COUNTDOWN);//TOOD* + } + playerCtr->EnableSingleLooping(true); // 设置是否循环播放 + return OHOS_SUCCESS; + } + + /* + * @brief 开始铃声的播放 + */ + int CountDownModel::StartRingtonePlayback(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownModel::StartRingtonePlayback"); + if (CountDownRingPlayerInit(RingMusicPath) != OHOS_SUCCESS) + { + // 错误处理,例如记录日志或者返回错误状态码 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDownRingPlayerInit error!!"); + return OHOS_FAILURE; + } + + if (InitializeAndPlayRingtone() != OHOS_SUCCESS) + { + // 错误处理,例如记录日志或者返回错误状态码 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownModel::InitializeAndPlayRingtone error!!"); + return OHOS_FAILURE; + } + return OHOS_SUCCESS; + } + + /* + * @brief 停止播放铃声,重置并释放播放器资源,并去激活之前设置的音频中断。 + */ + int CountDownModel::StopRingtonePlaybackAndReleaseResources(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownModel::StopRingtonePlaybackAndReleaseResources"); + int32_t ret = 0; + int32_t state = 0; + AudioManager &amIntance = AudioManager::GetInstance(); + if (playerCtr.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] playerCtr.get() == nullptr"); + return OHOS_FAILURE; + } + playerCtr->GetPlayerState(state); + if (state == PlayerStates::PLAYER_STARTED || state == PlayerStates::PLAYER_PAUSED) + { + ret = playerCtr->Stop(); + if (ret != 0) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] Failed to stop the player"); + return OHOS_FAILURE; + } + //如果倒计时铃声停止播放成功,记录该事件 + uapi_save_runtime_log(RUNTIME_LOG_RING_PLAY_STOP_COUNTDOWN);//TOOD* + } + ret = playerCtr->Reset(); + if (ret != 0) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] Failed to reset the player"); + return OHOS_FAILURE; + } + ret = playerCtr->Release(); + playerCtr.reset(); + if (ret != 0) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] Failed to release the player"); + return OHOS_FAILURE; + } + if (amIntance.DeactivateAudioInterrupt(interrupt) != 0) + { + return OHOS_FAILURE; + } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Reset, ret:%d", ret); + return OHOS_SUCCESS; + } + + int CountDownModel::ResetRingtonePlayback(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownModel::ResetRingtonePlayback"); + int32_t ret = 0; + int32_t state = 0; + if (playerCtr.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] playerCtr is null"); + return OHOS_FAILURE; + } + playerCtr->GetPlayerState(state); + if (state == PlayerStates::PLAYER_STARTED || state == PlayerStates::PLAYER_PAUSED) + { + ret = playerCtr->Stop(); + if (ret != 0) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] Failed to stop the player"); + return OHOS_FAILURE; + } + } + ret = playerCtr->Reset(); + if (ret != 0) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDownModel] Failed to reset the player"); + return OHOS_FAILURE; + } + return OHOS_SUCCESS; + } + + template + static void PostRingtoneEvent(Func function) + { + GraphicService::GetInstance()->PostGraphicEvent( + std::bind(function, CountDownModel::GetInstance())); + } + + /*触发铃声的开始*/ + void CountDownModel::RequestStartRingtone(void) + { + if (uapi_sleep_mode_flag_get()) + { + return; + } + if (true != ConfigModel::GetInstance()->GetDnotdisturb()) + { + PostRingtoneEvent(&CountDownModel::StartRingtonePlayback); + } + } + + /*触发铃声的停止*/ + void CountDownModel::RequestStopRingtone(void) + { + PostRingtoneEvent(&CountDownModel::StopRingtonePlaybackAndReleaseResources); + } + + // 判断计时器是否结束 + bool CountDownModel::CountDownTimerIsEnd() // 计时器是否结束 + { + if (g_timer == nullptr) + { + return true; + } + return false; + } + // 获取是否需要跳转到开始到计时界面 + bool CountDownModel::GetChanageToStartCountDownView() + { + return IsChanageToStartCountDownViewFlag; + } + + // 设置是否需要跳转到开始到计时界面 + void CountDownModel::SetChanageToStartCountDownView(bool val) + { + IsChanageToStartCountDownViewFlag = val; + } + + bool CountDownModel::CountDownViewIsExit() // 计时器界面是否退出 + { + MainCountDownView *g_CountDownView = MainCountDownView::GetInstance(); + if (g_CountDownView == nullptr) + { + return true; + } + return false; + } + + // 处理倒计时到期时的一些操作 + void CountDownModel::HandCountDownEndOperator(void) + { + StartCountDownView *p_StartCountDownView = StartCountDownView::GetInstance(); + if (p_StartCountDownView) + { + CountDown_StopTimer(); // 停止定时器 + uapi_set_screen_on_api(); // 点亮屏幕 + CountDownModel::GetInstance()->RequestStartRingtone(); // 播放铃声 + + StartCountDownView::GetInstance()->GetPlayOrPauseToggleButton()->SetVisible(false); + p_StartCountDownView->GetRepeatButton()->SetVisible(true); + /*倒计时结束 显示大图标 */ + p_StartCountDownView->ShowEndView(true); + } + } + + bool CountDownModel::GetCountDownTimerIsPause() + { + return CountDownTimerIsPauseFlag; + } + void CountDownModel::SetCountDownTimerIsPause(bool val) + { + CountDownTimerIsPauseFlag = val; + } + /* ----------------------------------- 定时器 ---------------------------------- */ + + /* + * @brief 显示计时器(时、分、秒) + */ + const char *CountDownModel::DisplayCountDownDuration() + { + countdownDuration.clear(); // 更正为clear(),更加表明意图,与erase()等价 + std::string hours; + std::string minute; + std::string seconds; + + if (g_CountDownTimes >= NUM_TIME_DAY) + { + g_CountDownTimes = 0; + } + + int hour = g_CountDownTimes / NUM_TIME_HOUR; + int min = (g_CountDownTimes % NUM_TIME_HOUR) / NUM_TIME_MIN; + int sec = (g_CountDownTimes % NUM_TIME_HOUR) % NUM_TIME_MIN; + + hours = (hour < NUM_TEN) ? "0" + std::to_string(hour) : std::to_string(hour); + minute = (min < NUM_TEN) ? "0" + std::to_string(min) : std::to_string(min); + seconds = (sec < NUM_TEN) ? "0" + std::to_string(sec) : std::to_string(sec); + countdownDuration = hours + ":" + minute + ":" + seconds; + + return countdownDuration.c_str(); // 返回 std::string 的 c_str(),但需要注意生命周期 + } + /* + * @brief 在这里添加定时器到期后的处理逻辑 + */ + static void CountDown_TimerCallbackProc(int data) + { + if (CountDownModel::GetInstance()->CountDownViewIsExit()) // 如果已经退出了界面定时器还在跑,就先跳转Slice + { + CountDownModel::GetInstance()->CountDown_StopTimer(); // 停止定时器 + CountDownModel::GetInstance()->SetChanageToStartCountDownView(true); + NativeAbility::GetInstance().EnterSlice(VIEW_MAIN_COUNTDOWN); + } + else + { + StartCountDownView *p_StartCountDownView = StartCountDownView::GetInstance(); + if (p_StartCountDownView) + { + CountDownModel::GetInstance()->HandCountDownEndOperator(); + } + } + return; + } + + /* + * @brief 计时器回调函数 + */ + static void CountDown_TimerCallback(int data) + { + + /*拿系统时间实现倒计时功能*/ + bool isRunning = CountDownModel::GetInstance()->GetisRunning(); + bool isPaused = CountDownModel::GetInstance()->GetisPaused(); + + if (isRunning &&!isPaused) + { + uint32_t currentTime = CountDownModel::GetInstance()->get_current_time(); + CountDownModel::g_CountDownTimes = CountDownModel::totalSeconds - (currentTime - CountDownModel::startTime); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "当前时间戳:%u,倒计时剩余时间:%d", currentTime ,CountDownModel::g_CountDownTimes); + if (CountDownModel::g_CountDownTimes <= 0 && currentTime == CountDownModel::endTime) { + // 这里可以添加倒计时结束后的操作,比如发出提示音等 + GraphicService::GetInstance()->PostGraphicEvent(std::bind(CountDown_TimerCallbackProc, data)); + } + } + } + + uint32_t CountDownModel::get_current_time() + { + uint32_t time; + if (uapi_get_timestampe_ss(&time) != ERRCODE_SUCC) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to get timestamp"); + return 0; + } + return time; + } + + void CountDownModel::countdownstart() + { + isRunning = true; + isPaused = false; + CountDownModel::totalSeconds = CountDownModel::g_CountDownTimes; + CountDownModel::startTime = get_current_time(); //获取开始时间戳 + CountDownModel::endTime = CountDownModel::startTime + g_CountDownTimes; //获取结束时间戳 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "startTime:%u,endTime:%u",CountDownModel::startTime,CountDownModel::endTime); + + } + + void CountDownModel::countdownpause() + { + if (isRunning &&!isPaused) { + isPaused = true; + CountDownModel::pauseTime = get_current_time(); + } + } + void CountDownModel::countdownresume() + { + if (isRunning && isPaused) { + isPaused = false; + uint32_t currentTime = get_current_time(); + uint32_t pauseDuration = currentTime - CountDownModel::pauseTime; + CountDownModel::endTime = CountDownModel::endTime + pauseDuration; + CountDownModel::totalSeconds = CountDownModel::totalSeconds + pauseDuration; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "startTime:%u,pauseDuration:%u,endTime:%u",CountDownModel::startTime,pauseDuration,CountDownModel::endTime); + } + } + + void CountDownModel::countdownstop() + { + isRunning = false; + isPaused = false; + } + + /* + * @brief 开启定时器函数 + * @param interval 时间间隔,单位ms + */ + void CountDownModel::CountDown_StartTimer(uint32_t interval) + { + /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */ + if (g_timer != nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDown_StartTimer Timer already exists!!"); + return; + } + + countdownstart(); + + /* --------------------------------- 2.创建定时器 -------------------------------- */ + g_timer = osTimerNew((osTimerFunc_t)CountDown_TimerCallback, osTimerPeriodic, nullptr, nullptr); + // 检查定时器创建是否成功 + if (g_timer == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownModel::CountDown_StartTimer Timer create failed!!"); + return; + } + + /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 ---------------------------- */ + int tick = osMs2Tick(interval); + + /* --------------------------------- 4.启动定时器 -------------------------------- */ + // g_timerStartTimestamp = GetCurrentTimestamp(); // 假设GetCurrentTimestamp()是获取当前时间戳的函数 + osStatus_t ret = osTimerStart(g_timer, tick); + /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 nullptr -------------------- */ + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[CountDown_StartTimer] osTimerDelete failed, ret = %lx!!", ret); + if (g_timer != nullptr) + { + ret = osTimerDelete(g_timer); + } + g_timer = nullptr; + return; + } + return; + } + + /* + * @brief 停止定时器 + */ + void CountDownModel::CountDown_StopTimer(void) + { + // 检查定时器是否存在 + if (g_timer != nullptr) + { + countdownstop(); + + osTimerStop(g_timer); + // 删除定时器并置为 nullptr + osStatus_t ret = osTimerDelete(g_timer); + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ScanTimerCallback osTimerDelete failed, ret = %lx!!", ret); + } + g_timer = nullptr; + g_CountDownTimes = 0; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "stop timer success!!"); + } + } + /* + * @brief 暂停定时器 + */ + void CountDownModel::CountDown_PauseTimer(void) + { + // 检查定时器是否存在 + if (g_timer != nullptr) + { + countdownpause(); + + // 暂停定时器,但不删除它 + osStatus_t ret = osTimerStop(g_timer); + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDown_PauseTimer osTimerStop failed, ret = %lx!!", ret); + } + } + } + + /* + * @brief 恢复定时器 + */ + void CountDownModel::CountDown_ResumeTimer(uint32_t interval) + { + // 检查定时器是否存在 + if (g_timer != nullptr) + { + countdownresume(); + + int tick = osMs2Tick(interval); + // 恢复定时器 + osStatus_t ret = osTimerStart(g_timer, tick); + if (ret != osOK) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDown_ResumeTimer osTimerStart failed, ret = %lx!!", ret); + } + } + } + + /* ----------------------- CountDownRingPlayerListener ---------------------- */ + + void CountDownRingPlayerListener::SetOwner(std::shared_ptr player) + { + player_ = player; + } + + /*重置标志位*/ + void CountDownRingPlayerListener::RestFlag() + { + isPaused_ = false; + isDelayed_ = false; + isResumed_ = false; + isBeginStopped_ = false; + isEndStopped_ = false; + } + + bool CountDownRingPlayerListener::IsPaused() + { + return isPaused_; + } + + bool CountDownRingPlayerListener::IsDelayed() + { + return isDelayed_; + } + + bool CountDownRingPlayerListener::IsResumed() + { + return isResumed_; + } + + bool CountDownRingPlayerListener::IsBeginStopped() + { + return isBeginStopped_; + } + + bool CountDownRingPlayerListener::IsEndStopped() + { + return isEndStopped_; + } + + static void Start(void) + { + CountDownModel::GetInstance()->RequestStartRingtone(); + } + + static void Stop(void) + { + CountDownModel::GetInstance()->RequestStopRingtone(); + } + + /*开始计时器铃声播放*/ + void CountDownRingPlayerListener::StartCountDownRing(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[CountDownRingPlayerListener::StartCountDownRing] start"); + GraphicService::GetInstance()->PostGraphicEvent(Start); + } + + /*停止计时器铃声播放*/ + void CountDownRingPlayerListener::StopCountDownRing(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[CountDownRingPlayerListener::StopCountDownRing] start"); + GraphicService::GetInstance()->PostGraphicEvent(Stop); + } + + /*响应外部的音频中断事件*/ + void CountDownRingPlayerListener::OnInterrupt(int32_t type, int32_t hint) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnInterrupt go in, type %d, hint %d.", type, hint); + if (type == INTERRUPT_TYPE_BEGIN && hint == INTERRUPT_HINT_PAUSE) + { + isDelayed_ = true; + } + + if (type == INTERRUPT_TYPE_BEGIN && hint == INTERRUPT_HINT_STOP) + { + StopCountDownRing(); + } + else if (type == INTERRUPT_TYPE_END && isDelayed_ == true) + { + // 重新开始计时器的铃声播放 + StartCountDownRing(); + isDelayed_ = false; + } + return; + } + + /* --------------------- CountDownRingPlayerCallbackImpl -------------------- */ + void CountDownRingPlayerCallbackImpl::OnPlaybackComplete(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnPlaybackComplete"); + // CountDownModel::GetInstance()->RequestStopRingtone(); + // CountDownModel::GetInstance()->RequestStartRingtone(); + } + + void CountDownRingPlayerCallbackImpl::OnError(int32_t errorType, int32_t errorCode) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnError test, errorType %d, errorCode %d", errorType, errorCode); + // 此处实现代码处理错误事件 + } + + void CountDownRingPlayerCallbackImpl::OnInfo(int type, int extra) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnInfo test, type %d, extra %d.", type, extra); + // 此处实现代码处理普通事件 + } + + void CountDownRingPlayerCallbackImpl::OnRewindToComplete(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OnRewindToComplete test"); + // 此处实现代码处理进度控制完成的事件 + } +} diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownTimeConfigView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownTimeConfigView.cpp new file mode 100644 index 000000000..3aa166151 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownTimeConfigView.cpp @@ -0,0 +1,171 @@ +#include "countdown/include/CountDownTimeConfigView.h" +#include "countdown/include/MainCountDownPresenter.h" +#include "countdown/include/CountDownModel.h" +#include "UiConfig.h" +#include "wearable_log.h" +/*倒计时时间配置*/ +namespace OHOS +{ + + static CountDownTimeConfigView *g_pCountDownTimeConfigView{nullptr}; + + CountDownTimeConfigView::CountDownTimeConfigView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownTimeConfigView::CountDownTimeConfigView"); + g_pCountDownTimeConfigView = this; + } + + CountDownTimeConfigView::~CountDownTimeConfigView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownTimeConfigView::~CountDownTimeConfigView"); + ClearAll(); + g_pCountDownTimeConfigView = nullptr; + } + + CountDownTimeConfigView *CountDownTimeConfigView::GetInstance() + { + return g_pCountDownTimeConfigView; + } + + void CountDownTimeConfigView::ClearAll() + { + scrollView->RemoveAll(); + + if (Title != nullptr) + { + delete Title; + Title = nullptr; + } + + if (timePicker != nullptr) + { + delete timePicker; + timePicker = nullptr; + } + + for (int i = 0; i < 2; i++) + { + delete Img[i]; + Img[i] = nullptr; + } + + if (startCountdownButton != nullptr) + { + delete startCountdownButton; + startCountdownButton = nullptr; + } + + if (scrollView != nullptr) + { + delete scrollView; + scrollView = nullptr; + } + } + + UIScrollView *CountDownTimeConfigView::CountDownTimeConfigViewInit() + { + if (scrollView == nullptr) + { + scrollView = new UIScrollView(); + if (scrollView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeConfigView::scrollView is new fail!!"); + return nullptr; + } + scrollView->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + scrollView->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + UIView::OnDragListener *dragListener = static_cast(MainCountDownPresenter::GetInstance()); + if (dragListener == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "dragListener is nullptr!!"); + return nullptr; + } + scrollView->SetViewId(COUNTDOWN_TIMECONFIG_VIEW); + scrollView->SetOnDragListener(dragListener); + scrollView->SetHorizontalScrollState(false); + scrollView->SetVerticalScrollState(false); + /* -------------------------------------------------------------------------- */ + Title = new UILabelExt(); + if (Title == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeConfigView::Title is new fail!!"); + return nullptr; + } + Title->SetPosition(83, 50, 300, 35); + Title->SetStyle(STYLE_TEXT_FONT, 30); + Title->SetStyle(STYLE_LINE_HEIGHT, 35); + Title->SetTextId(STR_COUNT_DOWN); // 倒计时 + Title->SetLineBreakMode(UILabel::LINE_BREAK_OSCILLATION); + Title->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + Title->SetRollSpeed(23); + Title->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); + /* -------------------------------------------------------------------------- */ + timePicker = new UITimePicker(); + if (timePicker == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeConfigView::timePicker is new fail!!"); + return nullptr; + } + timePicker->SetPosition(74, 121, 320, 180); + timePicker->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + timePicker->SetStyle(STYLE_BORDER_RADIUS, 51); + timePicker->SetItemHeight(60); // 设置时间选择器中每个项目的高度。 + timePicker->EnableSecond(true); // 设置在时间选择器中启用秒。 + timePicker->SetSelected(CountDownModel::GetInstance()->GetCountDownTime().c_str()); // 设置时间选择器中当前选择的时间。 + timePicker->SetTextColor(Color::GetColorFromRGBA(0x80, 0x80, 0x80, 0xff), Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0xff)); + // timePicker->SetTextColor(Color::GetColorFromRGBA(0xcc, 0xcc, 0xcc, 0xff), Color::GetColorFromRGBA(0xff, 0xa8, 0x00, 0xff)); // 设置时间选择器中的文本颜色。 + timePicker->SetLoopState(UITimePicker::PickerType::PICKER_HOUR, true); // 设置是否开启循环 :时 + timePicker->SetLoopState(UITimePicker::PickerType::PICKER_MIN, true); // 设置是否开启循环:分 + timePicker->SetLoopState(UITimePicker::PickerType::PICKER_SEC, true); // 设置是否开启循环:秒 + timePicker->SetHighlightFont(GILROY_BOLD_FONT, 55); // 设置突出显示文本的字体名称和大小。 + timePicker->SetBackgroundFont(DEFAULT_VECTOR_FONT_FILENAME, 50); // 设置背景文本的字体名称和大小。 + UITimePicker::SelectedListener *selectTimeListener = + (UITimePicker::SelectedListener *)OHOS::MainCountDownPresenter::GetInstance(); + timePicker->RegisterSelectedListener(selectTimeListener); + /* -------------------------------------------------------------------------- */ + for (int i = 0; i < 2; i++) + { + // 计算每个图片的x位置,这里第一张图片的x坐标是 175,第二张图片的x坐标是 280 + int xpos = (i == 0) ? 175 : 280; + Img[i] = new UIImageView(); + if (Img[i] == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeConfigView::Img[%d] is new fail!!", i); + return nullptr; + } + + Img[i]->SetPosition(xpos, 200, 18, 79); + Img[i]->SetSrc(COLONEIMAGESPATH); + } + /* -------------------------------------------------------------------------- */ + startCountdownButton = new UIButton(); + if (startCountdownButton == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeConfigView::startCountdownButton is new fail!!"); + return nullptr; + } + startCountdownButton->SetPosition(183, 321, 100, 100); + startCountdownButton->SetViewId(STARTCOUNTDOWN_BUTTON); + startCountdownButton->SetStyle(STYLE_BORDER_RADIUS, 52); + startCountdownButton->SetImageSrc(START_COUNTDOWN_BUTTON_IMAGEPATH, START_COUNTDOWN_BUTTON_IMAGEPATH); + startCountdownButton->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xffffa800, UIButton::PRESSED); + startCountdownButton->SetStyleForState(STYLE_BORDER_COLOR, 0xff0b1928, UIButton::PRESSED); + startCountdownButton->SetStyleForState(STYLE_BORDER_RADIUS, 52, UIButton::PRESSED); + UIView::OnClickListener *start_countdown_button_Click = static_cast(MainCountDownPresenter::GetInstance()); + startCountdownButton->SetOnClickListener(start_countdown_button_Click); + + /* ----------------------------------- Add ---------------------------------- */ + scrollView->Add(Title); + scrollView->Add(timePicker); + scrollView->Add(Img[0]); + scrollView->Add(Img[1]); + scrollView->Add(startCountdownButton); + } + return scrollView; + } + + UITimePicker *CountDownTimeConfigView::GetTimePicker(void) + { + return timePicker; + } +} \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownTimeNotNullView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownTimeNotNullView.cpp new file mode 100644 index 000000000..679d66a12 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/CountDownTimeNotNullView.cpp @@ -0,0 +1,144 @@ +#include "countdown/include/CountDownTimeNotNullView.h" +#include "countdown/include/MainCountDownPresenter.h" +#include "UiConfig.h" +#include "wearable_log.h" +/*倒计时时间不能为0*/ +namespace OHOS +{ + + static CountDownTimeNotNullView *g_pCountDownTimeNotNullView{nullptr}; + + CountDownTimeNotNullView::CountDownTimeNotNullView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownTimeNotNullView::CountDownTimeNotNullView"); + + g_pCountDownTimeNotNullView = this; + } + + CountDownTimeNotNullView::~CountDownTimeNotNullView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "CountDownTimeNotNullView::~CountDownTimeNotNullView"); + ClearAll(); + g_pCountDownTimeNotNullView = nullptr; + } + + CountDownTimeNotNullView *CountDownTimeNotNullView::GetInstance() + { + + return g_pCountDownTimeNotNullView; + } + + void CountDownTimeNotNullView::ClearAll() + { + scrollView->RemoveAll(); + if (Title != nullptr) + { + delete Title; + Title = nullptr; + } + if (Attention != nullptr) + { + delete Attention; + Attention = nullptr; + } + if (AttentionLabel != nullptr) + { + delete AttentionLabel; + AttentionLabel = nullptr; + } + if (button != nullptr) + { + delete button; + button = nullptr; + } + if (scrollView != nullptr) + { + delete scrollView; + scrollView = nullptr; + } + } + + UIScrollView *CountDownTimeNotNullView::CountDownTimeNotNullViewInit() + { + if (scrollView == nullptr) + { + scrollView = new UIScrollView(); + scrollView->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + scrollView->SetViewId(COUNTDOWNTIMENOTNULLVIEW); + scrollView->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + /* -------------------------------------------------------------------------- */ + Title = new UILabelExt(); + if (Title == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeNotNullView::Title is new fail!!"); + return nullptr; + } + Title->SetPosition(83, 50, 300, 35); + Title->SetStyle(STYLE_TEXT_FONT, 30); + Title->SetStyle(STYLE_LINE_HEIGHT, 35); + Title->SetTextId(STR_COUNT_DOWN); // 倒计时 + Title->SetLineBreakMode(UILabel::LINE_BREAK_OSCILLATION); + Title->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + Title->SetRollSpeed(23); + Title->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); + /* -------------------------------------------------------------------------- */ + Attention = new UIImageView(); + if (Attention == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeNotNullView::Attention is new fail!!"); + return nullptr; + } + Attention->SetPosition(193, 121, 80, 80); + Attention->SetStyle(STYLE_BORDER_RADIUS, 100); + #ifdef FD230 + Attention->SetSrc(PNG_ATTENTION_IMAGE); + #endif + /* -------------------------------------------------------------------------- */ + AttentionLabel = new UILabelExt(); + if (AttentionLabel == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeNotNullView::AttentionLabel is new fail!!"); + return nullptr; + } + AttentionLabel->SetPosition(53, 253, 360, 35); + AttentionLabel->SetStyle(STYLE_TEXT_FONT, 30); + AttentionLabel->SetStyle(STYLE_LINE_HEIGHT, 35); + AttentionLabel->SetTextId(STR_SET_COUNT_DOWN_NOTE); // 请先设置好倒计时间 + AttentionLabel->SetLineBreakMode(UILabel::LINE_BREAK_OSCILLATION); + AttentionLabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + AttentionLabel->SetRollSpeed(23); + AttentionLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); + /* -------------------------------------------------------------------------- */ + button = new UILabelButtonExt(); + if (button == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeNotNullView::button is new fail!!"); + return nullptr; + } + button->SetPosition(0, 366, 466, 100); + button->SetViewId(COUNTDOWNTIMENOTNULLVIEWRETURNBUTTON); + #ifdef FD230 + button->SetImageSrc(PNG_466x100BUTTON_IMAGE, PNG_466x100BUTTON_IMAGE); + #endif + button->SetTextId(STR_KONW); // 知道了 + button->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); + button->SetStyle(STYLE_LINE_HEIGHT, 35); + button->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xff000000, UIButton::PRESSED); + UIView::OnClickListener *buttonClick = static_cast(MainCountDownPresenter::GetInstance()); + if (buttonClick == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeNotNullView::buttonClick is new fail!!"); + return nullptr; + } + button->SetOnClickListener(buttonClick); + /* ----------------------------------- Add ---------------------------------- */ + scrollView->Add(Title); + scrollView->Add(Attention); + scrollView->Add(AttentionLabel); + scrollView->Add(button); + } + + return scrollView; + } + +} \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/MainCountDownPresenter.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/MainCountDownPresenter.cpp new file mode 100644 index 000000000..39be26534 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/MainCountDownPresenter.cpp @@ -0,0 +1,308 @@ +#include "AppViewIDs.h" +#include "NativeAbility.h" +#include "UiConfig.h" +#include "countdown/include/MainCountDownPresenter.h" +#include "countdown/include/CountDownModel.h" +#include "countdown/include/StartCountDownView.h" +#include "common/key_code.h" +#include "custom_utils.h" +#include "NativeRegisterManager.h" + +namespace OHOS +{ + REGIST_MENU_PRO(VIEW_MAIN_COUNTDOWN, MainCountDownView, MainCountDownPresenter, + PNG_APPLIST_COUNTDOWN_IMAGE_BIG, PNG_APPLIST_COUNTDOWN_IMAGE,PNG_APPLIST_COUNTDOWN_IMAGE, "计时器", STR_TIMER); + + static constexpr const char *OneMinuteTime = "00:01:00"; // 1分钟 + static constexpr const char *FiveMinuteTime = "00:05:00"; // 5分钟 + static constexpr const char *TenMinuteTime = "00:10:00"; // 10分钟 + static constexpr const char *ThirtyMinuteTime = "00:30:00"; // 30分钟 + + // 当前Presenter属性实例化的内存指针 + static MainCountDownPresenter *g_pMainCountDownPresenter = nullptr; + MainCountDownPresenter::MainCountDownPresenter() + { + g_pMainCountDownPresenter = this; + } + + MainCountDownPresenter::~MainCountDownPresenter() + { + g_pMainCountDownPresenter = nullptr; + } + + MainCountDownPresenter *MainCountDownPresenter::GetInstance() + { + + return g_pMainCountDownPresenter; + } + + void MainCountDownPresenter::OnStart() + { + // ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON); // 长亮屏 + } + + void MainCountDownPresenter::OnPause(void) + { + + Presenter::OnPause(); + } + + void MainCountDownPresenter::OnResume(void) + { + + Presenter::OnResume(); + } + + void MainCountDownPresenter::OnStop(void) + { + // ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON); // 取消长亮屏 + CountDownModel::GetInstance()->StopRingtonePlaybackAndReleaseResources(); // 清理播放器资源 + } + + bool MainCountDownPresenter::OnDrag(UIView &view, const DragEvent &event) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownPresenter::OnDrag"); + UNUSED(view); + if ((event.GetDeltaX() > X_DRAG_OFFSET) && (event.GetDeltaX() > (DOUBLES * MATH_ABS(event.GetDeltaY())))) // 拖动过程中判断是否是水平滑动 + { + MainCountDownPresenter::X_DRAG_FLAG = true; + } + return true; + } + + bool MainCountDownPresenter::OnDragEnd(UIView &view, const DragEvent &event) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownPresenter::OnDragEnd"); + if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT && MainCountDownPresenter::X_DRAG_FLAG) + { + if (strcmp(view.GetViewId(), MAINCOUNTDOWNVIEW) == 0) + { + NativeAbility::GetInstance().Back(TransitionType::TRANSITION_BACK_TO_HEXAGONS); + } + else if (strcmp(view.GetViewId(), COUNTDOWN_TIMECONFIG_VIEW) == 0) + { + MainCountDownView::GetInstance()->ChangeView(TIMECONFIG_COUNTDOWN, MAIN_COUNTDOWN); + } + MainCountDownPresenter::X_DRAG_FLAG = false; + } + return true; + } + + static bool SetCountDownAndChangeView(MainCountDownView *p_MainCountDownView, const std::string &timeValue) + { + CountDownModel *model = CountDownModel::GetInstance(); + model->SetCountDownTime(timeValue); + model->SetRingTime(timeValue); + + // Update label texts if necessary + if (p_MainCountDownView->GetStartCountDownView() != nullptr) + { + p_MainCountDownView->GetStartCountDownView()->GetShowCountDownTimelabel()->SetText(CountDownModel::GetInstance()->GetCountDownTime().c_str()); + p_MainCountDownView->GetStartCountDownView()->GetShowTheRingTimelabel()->SetText(CountDownModel::GetInstance()->GetRingTime().c_str()); + p_MainCountDownView->GetStartCountDownView()->GetPlayOrPauseToggleButton()->SetVisible(true); + p_MainCountDownView->GetStartCountDownView()->GetRepeatButton()->SetVisible(false); + } + + p_MainCountDownView->ChangeView(MAIN_COUNTDOWN, START_COUNTDOWN); + return true; + } + + bool MainCountDownPresenter::OnClick(UIView &view, const ClickEvent &event) + { + UNUSED(event); + + MainCountDownView *p_MainCountDownView = MainCountDownView::GetInstance(); + StartCountDownView *p_StartCountDownView = StartCountDownView::GetInstance(); + + if (p_MainCountDownView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainCountDownView::GetInstance fail!!"); + return false; + } + + if (strcmp(view.GetViewId(), COUNTDOWN_LABELBUTTON_VIEWID) == 0) + { + p_MainCountDownView->ChangeView(MAIN_COUNTDOWN, TIMECONFIG_COUNTDOWN); + return true; + } + + /*四个快速开始倒计时按钮*/ + if (strcmp(view.GetViewId(), COUNTDOWN_ONE_MINUTE_BUTTON_VIEWID) == 0) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "COUNTDOWN_ONE_MINUTE_BUTTON_VIEWID"); + return SetCountDownAndChangeView(p_MainCountDownView, OneMinuteTime); + } + else if (strcmp(view.GetViewId(), COUNTDOWN_FIVE_MINUTE_BUTTON_VIEWID) == 0) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "COUNTDOWN_FIVE_MINUTE_BUTTON_VIEWID"); + return SetCountDownAndChangeView(p_MainCountDownView, FiveMinuteTime); + } + else if (strcmp(view.GetViewId(), COUNTDOWN_TEN_MINUTE_BUTTON_VIEWID) == 0) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "COUNTDOWN_TEN_MINUTE_BUTTON_VIEWID"); + return SetCountDownAndChangeView(p_MainCountDownView, TenMinuteTime); + } + else if (strcmp(view.GetViewId(), COUNTDOWN_THIRTY_MINUTE_BUTTON_VIEWID) == 0) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "COUNTDOWN_THIRTY_MINUTE_BUTTON_VIEWID"); + return SetCountDownAndChangeView(p_MainCountDownView, ThirtyMinuteTime); + } + + if (strcmp(view.GetViewId(), STARTCOUNTDOWN_BUTTON) == 0) // 在自定义倒计时界面点击开始按钮跳转到开始倒计时界面 + { + UITimePicker *timePicker = CountDownTimeConfigView::GetInstance()->GetTimePicker(); + + std::string timeValue = timePicker->GetSelectValue(); + if (nullptr != timePicker && ("00:00:00" != timeValue)) + { + CountDownModel::GetInstance()->SetCountDownTime(timePicker->GetSelectValue()); // 设置倒计时时间 + CountDownModel::GetInstance()->SetRingTime(timePicker->GetSelectValue()); // 设置响铃时间 + } + + if (strcmp(CountDownModel::GetInstance()->GetCountDownTime().c_str(), "00:00:00") == 0) // 如果自定义时间设置为0 + { + p_MainCountDownView->ChangeView(TIMECONFIG_COUNTDOWN, COUNTDOWN_TIMENOTNULL); + return true; + } + else + { + p_MainCountDownView->ChangeView(TIMECONFIG_COUNTDOWN, START_COUNTDOWN); + if (p_StartCountDownView != nullptr) + { + std::string CountDownTime = CountDownModel::GetInstance()->GetCountDownTime(); + std::string RingTime = CountDownModel::GetInstance()->GetRingTime(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTime = %s RingTime = %s", CountDownTime.c_str(), RingTime.c_str()); + p_StartCountDownView->GetShowCountDownTimelabel()->SetText(CountDownTime.c_str()); + p_StartCountDownView->GetShowTheRingTimelabel()->SetText(RingTime.c_str()); + p_StartCountDownView->GetPlayOrPauseToggleButton()->SetVisible(true); + p_StartCountDownView->GetRepeatButton()->SetVisible(false); + } + + return true; + } + } + + if (strcmp(view.GetViewId(), COUNTDOWNTIMENOTNULLVIEWRETURNBUTTON) == 0) // 从倒计时不为空视图退到自定义倒计时界面 + { + p_MainCountDownView->ChangeView(COUNTDOWN_TIMENOTNULL, TIMECONFIG_COUNTDOWN); + /*结束视图恢复*/ + if (p_StartCountDownView == nullptr) + return false; + p_StartCountDownView->ShowEndView(false); + return true; + } + + if (strcmp(view.GetViewId(), STARTCOUNTDOWNVIEWRETURNBUTTON) == 0) // 从开始倒计时视图退到倒计时主视图 + { + CountDownModel::GetInstance()->SetCountDownTime("00:00:00"); + CountDownModel::GetInstance()->SetRingTime("00:00:00"); + CountDownModel::GetInstance()->CountDown_StopTimer(); + CountDownModel::GetInstance()->StopRingtonePlaybackAndReleaseResources(); + p_StartCountDownView->GetPlayOrPauseToggleButton()->SetTouchable(true); + p_StartCountDownView->GetPlayOrPauseToggleButton()->SetState(false); + p_MainCountDownView->ChangeView(START_COUNTDOWN, MAIN_COUNTDOWN); + p_StartCountDownView->ShowEndView(false); + + return true; + } + + // repeat button cb + if (0 == strcmp(view.GetViewId(), RepeatButtonViewID)) + { + // close prompt + CountDownModel::GetInstance()->StopRingtonePlaybackAndReleaseResources(); + + /*结束视图恢复*/ + if (p_StartCountDownView == nullptr) + return false; + p_StartCountDownView->ShowEndView(false); + + // set param & change view + CountDownModel *model = CountDownModel::GetInstance(); + const std::string timeValue = model->GetBackupCountDownTime(); + model->SetCountDownTime(timeValue); + model->SetRingTime(timeValue); + + if (nullptr != p_MainCountDownView->GetStartCountDownView()) + { + + p_MainCountDownView->GetStartCountDownView()->GetShowCountDownTimelabel()->SetText(CountDownModel::GetInstance()->GetCountDownTime().c_str()); + p_MainCountDownView->GetStartCountDownView()->GetShowTheRingTimelabel()->SetText(CountDownModel::GetInstance()->GetRingTime().c_str()); + + p_MainCountDownView->GetStartCountDownView()->GetPlayOrPauseToggleButton()->SetVisible(true); + p_MainCountDownView->GetStartCountDownView()->GetRepeatButton()->SetVisible(false); + } + + // start timer + CountDownModel::GetInstance()->CountDown_StartTimer(COUNTDOWN_TIMER_DURATION); + + return true; + } + + return true; + } + + // 增加按键事件 + bool MainCountDownPresenter::OnKeyAct(UIView &view, const KeyEvent &event) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownPresenter::OnKeyAct"); + uint16_t keyID = event.GetKeyId(); + uint16_t keyState = event.GetState(); + // 如果是power键就返回上一级 + if (keyID == static_cast(ZliteKeyCode::ZLITE_KEY_POWER) && keyState == 1 && uapi_get_screen_state() == 0) + { + NativeAbility::GetInstance().Back(TransitionType::TRANSITION_BACK_TO_HEXAGONS); + } + return true; + } + + bool MainCountDownPresenter::OnChange(UICheckBox::UICheckBoxState state) + { + CountDownModel *p_CountDownModel = CountDownModel::GetInstance(); + StartCountDownView *p_StartCountDownView = StartCountDownView::GetInstance(); + if (p_CountDownModel == nullptr || p_StartCountDownView == nullptr) + return false; + + switch (state) + { + case UICheckBox::SELECTED: + p_CountDownModel->SetCountDownTimerIsPause(false); + p_CountDownModel->CountDown_StartTimer(COUNTDOWN_TIMER_DURATION); + p_CountDownModel->CountDown_ResumeTimer(COUNTDOWN_TIMER_DURATION); + /*视图恢复*/ + p_StartCountDownView->SetImageAndLabel(true); + p_StartCountDownView->ShowEndView(false); + break; + case UICheckBox::UNSELECTED: + p_CountDownModel->SetCountDownTimerIsPause(true); + p_CountDownModel->CountDown_PauseTimer(); + /*进行视图隐藏*/ + p_StartCountDownView->SetImageAndLabel(false); + break; + default: + break; + } + return true; + } + + void MainCountDownPresenter::OnTimePickerStoped(UITimePicker &picker) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "picker.GetSelectValue = %s", picker.GetSelectValue()); + CountDownModel::GetInstance()->SetCountDownTime(picker.GetSelectValue()); // 设置倒计时时间 + CountDownModel::GetInstance()->SetRingTime(picker.GetSelectValue()); // 设置响铃时间 + } + void MainCountDownPresenter::Notify() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownPresenter::Notify"); + if (CountDownModel::GetInstance()->CountDownTimerIsEnd()) // 倒计时timer已经停止直接退出不刷新 + { + return; + } + StartCountDownView *p_StartCountDownView = StartCountDownView::GetInstance(); + if (p_StartCountDownView && p_StartCountDownView->GetShowCountDownTimelabel()) + { + // 更新显示 + p_StartCountDownView->GetShowCountDownTimelabel()->SetText(CountDownModel::GetInstance()->DisplayCountDownDuration()); + } + } +} \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/MainCountDownView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/MainCountDownView.cpp new file mode 100644 index 000000000..42617ac30 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/MainCountDownView.cpp @@ -0,0 +1,435 @@ +#include "countdown/include/MainCountDownView.h" +#include "countdown/include/MainCountDownPresenter.h" +#include "wearable_log.h" +#include "common/image_cache_manager.h" +#include "countdown/include/CountDownModel.h" +/*倒计时的主界面*/ +namespace OHOS +{ +#define VISIBLE_XBAR(visible) + + static MainCountDownView *g_pMainCountDownView{nullptr}; + + MainCountDownView::MainCountDownView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownView::MainCountDownView"); + g_pMainCountDownView = this; + } + + MainCountDownView::~MainCountDownView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownView::~MainCountDownView"); + ClearAll(); + g_pMainCountDownView = nullptr; + } + + MainCountDownView *MainCountDownView::GetInstance() + { + return g_pMainCountDownView; + } + + void MainCountDownView::ClearAll() + { + if (scrollView[MAIN_COUNTDOWN] != nullptr) + { + scrollView[MAIN_COUNTDOWN]->RemoveAll(); + if (Title != nullptr) + { + delete Title; + Title = nullptr; + } + if (QuickStart != nullptr) + { + delete QuickStart; + QuickStart = nullptr; + } + ImageCacheManager::GetInstance().UnloadSingleRes(COUNTDOWM_TIMER_ICON); + if (CUSTOM_COUNTDOWN != nullptr) + { + delete CUSTOM_COUNTDOWN; + CUSTOM_COUNTDOWN = nullptr; + } + if (COUNTDOWN_ONE_MINUTE_BUTTON != nullptr) + { + delete COUNTDOWN_ONE_MINUTE_BUTTON; + COUNTDOWN_ONE_MINUTE_BUTTON = nullptr; + } + if (COUNTDOWN_FIVE_MINUTE_BUTTON != nullptr) + { + delete COUNTDOWN_FIVE_MINUTE_BUTTON; + COUNTDOWN_FIVE_MINUTE_BUTTON = nullptr; + } + if (COUNTDOWN_TEN_MINUTE_BUTTON != nullptr) + { + delete COUNTDOWN_TEN_MINUTE_BUTTON; + COUNTDOWN_TEN_MINUTE_BUTTON = nullptr; + } + if (COUNTDOWN_THIRTY_MINUTE_BUTTON != nullptr) + { + delete COUNTDOWN_THIRTY_MINUTE_BUTTON; + COUNTDOWN_THIRTY_MINUTE_BUTTON = nullptr; + } + delete scrollView[MAIN_COUNTDOWN]; + scrollView[MAIN_COUNTDOWN] = nullptr; + } + if (p_CountDownTimeConfigView != nullptr) + { + delete p_CountDownTimeConfigView; + p_CountDownTimeConfigView = nullptr; + } + if (p_CountDownTimeNotNullView != nullptr) + { + delete p_CountDownTimeNotNullView; + p_CountDownTimeNotNullView = nullptr; + } + if (p_StartCountDownView != nullptr) + { + delete p_StartCountDownView; + p_StartCountDownView = nullptr; + } + } + + UIScrollView *MainCountDownView::MainCountDownViewInit() + { + if (scrollView[MAIN_COUNTDOWN] == nullptr) + { + /* ----------------------- scrollView[MAIN_COUNTDOWN] ----------------------- */ + scrollView[MAIN_COUNTDOWN] = new UIScrollView(); + if (scrollView[MAIN_COUNTDOWN] == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "scrollView[MAIN_COUNTDOWN] is new fail!!"); + return nullptr; + } + scrollView[MAIN_COUNTDOWN]->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + scrollView[MAIN_COUNTDOWN]->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + scrollView[MAIN_COUNTDOWN]->SetViewId(MAINCOUNTDOWNVIEW); + UIView::OnDragListener *draglistener = static_cast(MainCountDownPresenter::GetInstance()); + if (draglistener == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "draglistener is nullptr!!"); + return nullptr; + } + scrollView[MAIN_COUNTDOWN]->SetOnDragListener(draglistener); + scrollView[MAIN_COUNTDOWN]->SetReboundSize(50); // 设置回弹大小 + scrollView[MAIN_COUNTDOWN]->SetHorizontalScrollState(false); // 设置横向不可以滚动 + scrollView[MAIN_COUNTDOWN]->SetScrollBlankSize(75); + scrollView[MAIN_COUNTDOWN]->SetThrowDrag(true); + scrollView[MAIN_COUNTDOWN]->SetMaxScrollDistance(1000); + scrollView[MAIN_COUNTDOWN]->SetSwipeACCLevel(200); // 800: compensation distance + scrollView[MAIN_COUNTDOWN]->SetDragACCLevel(5); // 5: drag acceleration + scrollView[MAIN_COUNTDOWN]->SetRotateFactor(2); + scrollView[MAIN_COUNTDOWN]->RequestFocus(); + /* ---------------------------------- Title --------------------------------- */ + Title = new UILabelExt(); + if (Title == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Title is new fail!!"); + return nullptr; + } + Title->SetPosition(83, 50, 300, 35); + Title->SetStyle(STYLE_TEXT_FONT, 30); + Title->SetStyle(STYLE_LINE_HEIGHT, 35); + Title->SetTextId(STR_COUNT_DOWN); // 倒计时 + Title->SetLineBreakMode(UILabel::LINE_BREAK_OSCILLATION); + Title->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + Title->SetRollSpeed(23); + Title->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); + /* -------------------------- CUSTOM_COUNTDOWN ------------------------- */ + CUSTOM_COUNTDOWN = new UILabelButtonExt(); + if (CUSTOM_COUNTDOWN == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CUSTOM_COUNTDOWN is new fail!!"); + return nullptr; + } + CUSTOM_COUNTDOWN->SetPosition(43, 130, 380, 100); + CUSTOM_COUNTDOWN->SetViewId(COUNTDOWN_LABELBUTTON_VIEWID); + CUSTOM_COUNTDOWN->SetStyle(STYLE_BACKGROUND_COLOR, 0xffffa800); + CUSTOM_COUNTDOWN->SetStyle(STYLE_BORDER_RADIUS, 100); + CUSTOM_COUNTDOWN->SetTextId(STR_CUSTOM_TIME); // 自定义时间 + CUSTOM_COUNTDOWN->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); + CUSTOM_COUNTDOWN->SetStyle(STYLE_LINE_HEIGHT, 35); + CUSTOM_COUNTDOWN->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xffffa800, UIButton::PRESSED); + /* ------------------------------- QuickStart ------------------------------- */ + QuickStart = new UILabelExt(); + if (QuickStart == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "QuickStart is new fail!!"); + return nullptr; + } + QuickStart->SetPosition(83, 270, 300, 35); + QuickStart->SetStyle(STYLE_TEXT_FONT, 30); + QuickStart->SetStyle(STYLE_LINE_HEIGHT, 35); + QuickStart->SetTextId(STR_QUICK_START); + QuickStart->SetLineBreakMode(UILabel::LINE_BREAK_OSCILLATION); + QuickStart->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + QuickStart->SetRollSpeed(23); + QuickStart->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); + /* ----------------------- COUNTDOWN_ONE_MINUTE_BUTTON ---------------------- */ + COUNTDOWN_ONE_MINUTE_BUTTON = SetUILabelButton(43, 345, 180, 150, COUNTDOWN_ONE_MINUTE_BUTTON_VIEWID, "1分钟", STR_1_MIN); + if (COUNTDOWN_ONE_MINUTE_BUTTON == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "COUNTDOWN_ONE_MINUTE_BUTTON is new fail!!"); + return nullptr; + } + /* ---------------------- COUNTDOWN_FIVE_MINUTE_BUTTON ---------------------- */ + COUNTDOWN_FIVE_MINUTE_BUTTON = SetUILabelButton(243, 345, 180, 150, COUNTDOWN_FIVE_MINUTE_BUTTON_VIEWID, "5分钟", STR_5_MIN); + if (COUNTDOWN_FIVE_MINUTE_BUTTON == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "COUNTDOWN_FIVE_MINUTE_BUTTON is new fail!!"); + return nullptr; + } + /* ----------------------- COUNTDOWN_TEN_MINUTE_BUTTON ---------------------- */ + COUNTDOWN_TEN_MINUTE_BUTTON = SetUILabelButton(43, 515, 180, 150, COUNTDOWN_TEN_MINUTE_BUTTON_VIEWID, "10分钟", STR_10_MIN); + if (COUNTDOWN_TEN_MINUTE_BUTTON == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "COUNTDOWN_TEN_MINUTE_BUTTON is new fail!!"); + return nullptr; + } + /* --------------------- COUNTDOWN_THIRTY_MINUTE_BUTTON --------------------- */ + COUNTDOWN_THIRTY_MINUTE_BUTTON = SetUILabelButton(243, 515, 180, 150, COUNTDOWN_THIRTY_MINUTE_BUTTON_VIEWID, "30分钟", STR_30_MIN); + if (COUNTDOWN_THIRTY_MINUTE_BUTTON == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "COUNTDOWN_THIRTY_MINUTE_BUTTON is new fail!!"); + return nullptr; + } + /* -------------------------------------------------------------------------- */ + UIView::OnClickListener *ButtonOnClick = static_cast(MainCountDownPresenter::GetInstance()); + CUSTOM_COUNTDOWN->SetOnClickListener(ButtonOnClick); + COUNTDOWN_ONE_MINUTE_BUTTON->SetOnClickListener(ButtonOnClick); + COUNTDOWN_FIVE_MINUTE_BUTTON->SetOnClickListener(ButtonOnClick); + COUNTDOWN_TEN_MINUTE_BUTTON->SetOnClickListener(ButtonOnClick); + COUNTDOWN_THIRTY_MINUTE_BUTTON->SetOnClickListener(ButtonOnClick); + /* -------------------------------------------------------------------------- */ + scrollView[MAIN_COUNTDOWN]->Add(Title); + scrollView[MAIN_COUNTDOWN]->Add(CUSTOM_COUNTDOWN); + scrollView[MAIN_COUNTDOWN]->Add(QuickStart); + scrollView[MAIN_COUNTDOWN]->Add(COUNTDOWN_ONE_MINUTE_BUTTON); + scrollView[MAIN_COUNTDOWN]->Add(COUNTDOWN_FIVE_MINUTE_BUTTON); + scrollView[MAIN_COUNTDOWN]->Add(COUNTDOWN_TEN_MINUTE_BUTTON); + scrollView[MAIN_COUNTDOWN]->Add(COUNTDOWN_THIRTY_MINUTE_BUTTON); + } + return scrollView[MAIN_COUNTDOWN]; + } + + UILabelButtonExt *MainCountDownView::SetUILabelButton(int x, int y, int w, int h, const char *id, const char *text, int textId) + { + UILabelButtonExt *button = new UILabelButtonExt(); + if (button == nullptr) + { + return nullptr; + } + button->SetPosition(x, y, w, h); + button->SetViewId(id); + button->SetStyle(STYLE_BACKGROUND_COLOR, 0xff191919); + button->SetStyle(STYLE_BORDER_RADIUS, 50); + button->SetImagePosition(72, 30); + ImageInfo *img = ImageCacheManager::GetInstance().LoadSingleRes(COUNTDOWM_TIMER_ICON); + button->SetImageSrc(img, img); + // button->SetText(text); + button->SetTextId(textId); + button->SetAlign(TEXT_ALIGNMENT_CENTER); + button->SetLabelPosition(0, 61); + button->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); + button->SetStyle(STYLE_LINE_HEIGHT, 35); + button->SetStyleForState(STYLE_BACKGROUND_OPA, 0x00, UIButton::PRESSED); + button->SetStyleForState(STYLE_BORDER_RADIUS, 50, UIButton::PRESSED); + return button; + } + + // 获取开始倒计时视图 + StartCountDownView *MainCountDownView::GetStartCountDownView(void) + { + return p_StartCountDownView; + } + + /*1.切换到主倒计时视图*/ + bool MainCountDownView::ChangeMainCountDown(int16_t thisView) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownView::ChangeMainCountDown"); + if (scrollView[thisView] == nullptr) + { + UIScrollView *view = MainCountDownViewInit(); + if (view == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "view nullptr fail"); + return false; + } + scrollView[thisView] = view; + AddViewToRootContainer(scrollView[thisView]); + } + return true; + } + + /*2.切换到自定义倒计时界面*/ + bool MainCountDownView::ChangeTimeConfigCountdown(int16_t thisView) + { + if (scrollView[thisView] == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainCountDownView::ChangeTimeConfigCountdown"); + p_CountDownTimeConfigView = new CountDownTimeConfigView(); + if (p_CountDownTimeConfigView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainCountDownView:: new scrollView[%d] fail", thisView); + return false; + } + UIScrollView *view = p_CountDownTimeConfigView->CountDownTimeConfigViewInit(); + if (view == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "view nullptr fail"); + return false; + } + scrollView[thisView] = view; + AddViewToRootContainer(scrollView[thisView]); + } + return true; + } + + /*3.开始倒计时*/ + bool MainCountDownView::ChangeStartCountDown(int16_t thisView) + { + if (scrollView[thisView] == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownView::ChangeCountdownTimeNotNull"); + p_StartCountDownView = new StartCountDownView(); + if (p_StartCountDownView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainCountDownView:: new scrollView[%d] fail", thisView); + return false; + } + UIScrollView *view = p_StartCountDownView->StartCountDownViewInit(); + if (view == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "view nullptr fail"); + return false; + } + scrollView[thisView] = view; + AddViewToRootContainer(scrollView[thisView]); + } + if (p_StartCountDownView) + { + p_StartCountDownView->UpdateimageAndLabelPosition(); + if(CountDownModel::GetInstance()->GetChanageToStartCountDownView() == true) + { + CountDownModel::GetInstance()->SetChanageToStartCountDownView(false); + CountDownModel::GetInstance()->HandCountDownEndOperator(); + } + } + return true; + } + + /*4.切换到倒计时不能为空界面*/ + bool MainCountDownView::ChangeCountdownTimeNotNull(int16_t thisView) + { + if (scrollView[thisView] == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownView::ChangeCountdownTimeNotNull"); + p_CountDownTimeNotNullView = new CountDownTimeNotNullView(); + if (p_CountDownTimeNotNullView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainCountDownView:: new scrollView[%d] fail", thisView); + return false; + } + UIScrollView *view = p_CountDownTimeNotNullView->CountDownTimeNotNullViewInit(); + if (view == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "view nullptr fail"); + return false; + } + scrollView[thisView] = view; + AddViewToRootContainer(scrollView[thisView]); + } + return true; + } + + /* -------------------------------------------------------------------------- */ + // 1.MainCountDownViewMapper:结构体定义了一个将视图ID映射到特定成员函数指针的映射表。 + struct MainCountDownViewMapper + { + const int16_t viewId; + bool (MainCountDownView::*func)(int16_t thisView); + }; + + // 2.ViewMapper[] 是一个静态的视图映射数组,将视图ID与MainCountDownView类中的特定成员函数指针进行了绑定。 + static const MainCountDownViewMapper ViewMapper[] = { + {MAIN_COUNTDOWN, &MainCountDownView::ChangeMainCountDown}, + {TIMECONFIG_COUNTDOWN, &MainCountDownView::ChangeTimeConfigCountdown}, + {START_COUNTDOWN, &MainCountDownView::ChangeStartCountDown}, + {COUNTDOWN_TIMENOTNULL, &MainCountDownView::ChangeCountdownTimeNotNull}, + }; + + // 3.根据传入的interface参数执行相应视图ID关联的成员函数。 + bool MainCountDownView::ClickFunc(int16_t interface) + { + int funcSize = sizeof(ViewMapper) / sizeof(ViewMapper[0]); + for (int i = 0; i < funcSize; i++) + { + if (interface == ViewMapper[i].viewId) + { + bool jug = (this->*(ViewMapper[i].func))(interface); + if (jug == false) + { + return false; + } + return true; + } + } + return false; + } + + bool MainCountDownView::CheckViewParam(int16_t hideView, int16_t showView) + { + if (showView < 0 || showView >= COUNTDOWN_MAX) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RebootView::CheckViewParam showView err."); + return false; + } + + if (hideView < 0 || hideView > COUNTDOWN_MAX) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RebootView::CheckViewParam hideView err."); + return false; + } + + return true; + } + + // 4.切换视图 + void MainCountDownView::ChangeView(int16_t hideView, int16_t showView) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownView::ChangeView %d, %d", hideView, showView); + if (!CheckViewParam(hideView, showView)) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainCountDownView::CheckViewParam failed."); + return; + } + if (hideView != COUNTDOWN_MAX) + { + if (scrollView[hideView] == nullptr) + { + return; + } + scrollView[hideView]->SetVisible(false); + } + ClickFunc(showView); + scrollView[showView]->SetVisible(true); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainCountDownView:: ChangeView end!!!%d", showView); + Draw(); + } + /* -------------------------------------------------------------------------- */ + + void MainCountDownView::OnStart() + { + /* -------------------------------------------------------------------------- */ + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d,%d",CountDownModel::GetInstance()->CountDownTimerIsEnd(),CountDownModel::GetInstance()->GetChanageToStartCountDownView()); + if (CountDownModel::GetInstance()->CountDownTimerIsEnd() == false || + CountDownModel::GetInstance()->GetChanageToStartCountDownView() == true) + { + ChangeView(COUNTDOWN_MAX, START_COUNTDOWN); // 如果倒计时的timer没有结束或需要跳转到开始倒计时界面,则跳转到开始倒计时界面 + } + else + { + ChangeView(COUNTDOWN_MAX, MAIN_COUNTDOWN); // 如果倒计时的timer已经结束,则跳转到主界面 + } + /* -------------------------------------------------------------------------- */ + } +} \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/StartCountDownView.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/StartCountDownView.cpp new file mode 100644 index 000000000..af12408a2 --- /dev/null +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/countdown/src/StartCountDownView.cpp @@ -0,0 +1,302 @@ +#include "countdown/include/StartCountDownView.h" +#include "countdown/include/MainCountDownPresenter.h" +#include "countdown/include/CountDownModel.h" +#include "UiConfig.h" +#include "wearable_log.h" +#include "uicomponents/UIComponents.h" + +/*开始倒计时*/ +namespace OHOS +{ + + static StartCountDownView *g_pStartCountDownView{nullptr}; + + StartCountDownView::StartCountDownView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "StartCountDownView::StartCountDownView"); + g_pStartCountDownView = this; + } + + StartCountDownView::~StartCountDownView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "StartCountDownView::~StartCountDownView"); + ClearAll(); + g_pStartCountDownView = nullptr; + } + + StartCountDownView *StartCountDownView::GetInstance() + { + return g_pStartCountDownView; + } + + void StartCountDownView::ClearAll() + { + scrollView->RemoveAll(); + + if (Title != nullptr) + { + delete Title; + Title = nullptr; + } + + if (showCountDownTimelabel != nullptr) + { + delete showCountDownTimelabel; + showCountDownTimelabel = nullptr; + } + + if (vectorImage != nullptr) + { + delete vectorImage; + vectorImage = nullptr; + } + if (showTheRingTime != nullptr) + { + delete showTheRingTime; + showTheRingTime = nullptr; + } + + if (play_or_pause_toggleButton != nullptr) + { + delete play_or_pause_toggleButton; + play_or_pause_toggleButton = nullptr; + } + + if (endbutton != nullptr) + { + delete endbutton; + endbutton = nullptr; + } + + if (nullptr != RepeatButton) + { + delete RepeatButton; + RepeatButton = nullptr; + } + + if (nullptr != showTheEndRingTime) + { + delete showTheEndRingTime; + showTheEndRingTime = nullptr; + } + if (nullptr != vectorbigImage) + { + delete vectorbigImage; + vectorbigImage = nullptr; + } + if (scrollView != nullptr) + { + delete scrollView; + scrollView = nullptr; + } + } + void StartCountDownView::SetEndView() + { + /*最后显示图片*/ + InitImageView(vectorbigImage, 183, 120, 100, 100, "image", VectorBigImagesPath); + + /*显示时间字符串*/ + std::string EndTimeStr = CountDownModel::GetInstance()->GetCountDownTime(); + InitLabel(showTheEndRingTime, 83, 240, 300, 47, "title", EndTimeStr.c_str(), GILROY_BOLD_FONT, 40); + + /*结束前先隐藏*/ + vectorbigImage->SetVisible(false); + showTheEndRingTime->SetVisible(false); + } + UIScrollView *StartCountDownView::StartCountDownViewInit() + { + if (scrollView == nullptr) + { + scrollView = new UIScrollView(); + scrollView->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight()); + scrollView->SetViewId(STARTCOUNTDOWNVIEW); + scrollView->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + /* -------------------------------------------------------------------------- */ + Title = new UILabelExt(); + if (Title == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CountDownTimeNotNullView::Title is is new fail!!"); + return nullptr; + } + Title->SetPosition(83, 50, 300, 35); + Title->SetStyle(STYLE_TEXT_FONT, 30); + Title->SetStyle(STYLE_LINE_HEIGHT, 35); + Title->SetTextId(STR_COUNT_DOWN); // 倒计时 + Title->SetLineBreakMode(UILabel::LINE_BREAK_OSCILLATION); + Title->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + Title->SetRollSpeed(23); + Title->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); + /* -------------------------------------------------------------------------- */ + showCountDownTimelabel = new UILabelExt(); + if (showCountDownTimelabel == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "showCountDownTimelabel is new fail!!"); + return nullptr; + } + showCountDownTimelabel->SetPosition(33, 138, 400, 94); + showCountDownTimelabel->SetStyle(STYLE_TEXT_FONT, 80); + showCountDownTimelabel->SetStyle(STYLE_LINE_HEIGHT, 94); + if (CountDownModel::GetInstance()->CountDownTimerIsEnd()) // 如果定时器是停的,显示设置的倒计时时间 + showCountDownTimelabel->SetText(CountDownModel::GetInstance()->GetCountDownTime().c_str()); + else + showCountDownTimelabel->SetText(CountDownModel::GetInstance()->DisplayCountDownDuration()); + showCountDownTimelabel->SetLineBreakMode(UILabel::LINE_BREAK_OSCILLATION); + showCountDownTimelabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + showCountDownTimelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME_EN, 80); + showCountDownTimelabel->SetRollSpeed(23); + /* -------------------------------------------------------------------------- */ + vectorImage = new UIImageView(); + if (vectorImage == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "vectorImage is new fail!!"); + return nullptr; + } + vectorImage->SetPosition(174, 247, 28, 28); + vectorImage->SetSrc(VectorImagesPath); + /* -------------------------------------------------------------------------- */ + showTheRingTime = new UILabelExt(); + if (showTheRingTime == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "showTheRingTime is new fail!!"); + return nullptr; + } + showTheRingTime->SetPosition(209, 241, 81, 41); + showTheRingTime->SetStyle(STYLE_TEXT_FONT, 35); + showTheRingTime->SetStyle(STYLE_LINE_HEIGHT, 41); + showTheRingTime->SetText(CountDownModel::GetInstance()->GetRingTime().c_str()); + showTheRingTime->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); + showTheRingTime->SetTextColor(Color::GetColorFromRGBA(0x66, 0x66, 0x66, 0xff)); + showTheRingTime->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + showTheRingTime->SetFont(DEFAULT_VECTOR_FONT_FILENAME_EN, 35); + /* -------------------------------------------------------------------------- */ + play_or_pause_toggleButton = new UIToggleButton(); + if (play_or_pause_toggleButton == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "play_or_pause_toggleButton is new fail!!"); + return nullptr; + } + play_or_pause_toggleButton->SetPosition(248, 321, 100, 100); + play_or_pause_toggleButton->SetViewId(PlayOrPauseToggleButton_ViewID); + play_or_pause_toggleButton->SetStyle(STYLE_BACKGROUND_OPA, 0x00); + play_or_pause_toggleButton->SetStyle(STYLE_BORDER_RADIUS, 100); + if (CountDownModel::GetInstance()->CountDownTimerIsEnd() || CountDownModel::GetInstance()->GetCountDownTimerIsPause()) // 如果定时器是停的 + play_or_pause_toggleButton->SetState(false); + else + play_or_pause_toggleButton->SetState(true); + play_or_pause_toggleButton->SetImages(CountDown_Play_ImagePath, CountDown_PAUSE_ImagePath); + UICheckBox::OnChangeListener *ToggleButtonChange = static_cast(MainCountDownPresenter::GetInstance()); + play_or_pause_toggleButton->SetOnChangeListener(ToggleButtonChange); + play_or_pause_toggleButton->SetVisible(true); + + /* -------------------------------------------------------------------------- */ + endbutton = new UIButton(); + if (endbutton == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "endbutton is new fail!!"); + return nullptr; + } + endbutton->SetPosition(118, 321, 100, 100); + endbutton->SetViewId(STARTCOUNTDOWNVIEWRETURNBUTTON); + endbutton->SetStyle(STYLE_BACKGROUND_COLOR, 0xffffffff); + endbutton->SetStyle(STYLE_BACKGROUND_OPA, 0x00); + endbutton->SetStyle(STYLE_BORDER_RADIUS, 100); + endbutton->SetImageSrc(EndButton_ImagePath, EndButton_ImagePath); + endbutton->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xffffa800, UIButton::PRESSED); + endbutton->SetStyleForState(STYLE_BORDER_RADIUS, 100, UIButton::PRESSED); + UIView::OnClickListener *buttonClick = static_cast(MainCountDownPresenter::GetInstance()); + if (buttonClick == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "buttonClick is new fail!!"); + return nullptr; + } + endbutton->SetOnClickListener(buttonClick); + RepeatButton = new UIButton(); + if (nullptr == RepeatButton) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "endbutton is new fail!!"); + return nullptr; + } + RepeatButton->SetPosition(248, 321, 100, 100); + RepeatButton->SetViewId(RepeatButtonViewID); + RepeatButton->SetStyle(STYLE_BACKGROUND_COLOR, 0x00); + RepeatButton->SetStyle(STYLE_BACKGROUND_OPA, 0x00); + RepeatButton->SetStyle(STYLE_BORDER_RADIUS, 100); + RepeatButton->SetImageSrc(RepeatButtonImagePath, RepeatButtonImagePath); + RepeatButton->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xffffa800, UIButton::PRESSED); + RepeatButton->SetStyleForState(STYLE_BORDER_RADIUS, 100, UIButton::PRESSED); + UIView::OnClickListener *RepeatButtonClick = static_cast(MainCountDownPresenter::GetInstance()); + RepeatButton->SetOnClickListener(RepeatButtonClick); + RepeatButton->SetVisible(false); + + SetEndView(); + + /* ----------------------------------- Add ---------------------------------- */ + scrollView->Add(Title); + scrollView->Add(showCountDownTimelabel); + scrollView->Add(vectorImage); + scrollView->Add(showTheRingTime); + scrollView->Add(endbutton); + scrollView->Add(play_or_pause_toggleButton); + scrollView->Add(RepeatButton); + scrollView->Add(vectorbigImage); + scrollView->Add(showTheEndRingTime); + } + + return scrollView; + } + + // 获取显示倒计时时间标签 + UILabelExt *StartCountDownView::GetShowCountDownTimelabel(void) + { + return showCountDownTimelabel; + } + + // 获取显示响铃时间标签 + UILabelExt *StartCountDownView::GetShowTheRingTimelabel(void) + { + return showTheRingTime; + } + + UIToggleButton *StartCountDownView::GetPlayOrPauseToggleButton(void) + { + return play_or_pause_toggleButton; + } + + UIButton *StartCountDownView::GetRepeatButton(void) + { + return RepeatButton; + } + + void StartCountDownView::SetImageAndLabel(bool state) + { + if (vectorImage == nullptr || showTheRingTime == nullptr) + return; + + vectorImage->SetVisible(state); + showTheRingTime->SetVisible(state); + } + /*结束状态更新*/ + void StartCountDownView::ShowEndView(bool state) + { + if (vectorImage == nullptr || showTheRingTime == nullptr || showCountDownTimelabel == nullptr) + return; + + vectorImage->SetVisible(!state); + showTheRingTime->SetVisible(!state); + showCountDownTimelabel->SetVisible(!state); + + vectorbigImage->SetVisible(state); + showTheEndRingTime->SetVisible(state); + showTheEndRingTime->SetText(showTheRingTime->GetText()); + } + void StartCountDownView::UpdateimageAndLabelPosition(void) + { + if (vectorImage == nullptr || showTheRingTime == nullptr) + return; + int w = vectorImage->GetWidth() + showTheRingTime->GetTextWidth() + 7; + vectorImage->SetX((Screen::GetInstance().GetWidth() - w) / 2); + showTheRingTime->SetX(vectorImage->GetX() + vectorImage->GetWidth() + 7); + vectorImage->Invalidate(); + showTheRingTime->Invalidate(); + } +} \ No newline at end of file -- 2.17.1