02cbd14f97a3e5eebb462e7ee55332c7b044eda6
[mirrors/FD230.git] /
1 /*
2  * @Author: luozheng 1732036298@qq.com
3  * @Date: 2024-06-20 15:11:17
4  * @LastEditors: luozheng 1732036298@qq.com
5  * @LastEditTime: 2024-07-27 13:03:52
6  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/RebootAndShutdownPresenter.cpp
7  */
8 #include "AppViewIDs.h"
9 #include "NativeAbility.h"
10 #include "common/key_code.h"
11 #include <custom_utils.h>
12 #include "UiConfig.h"
13 #include "rebootandshutdown/include/RebootAndShutdownPresenter.h"
14 #include "rebootandshutdown/include/RebootAndShutdownView.h"
15 #include "rebootandshutdown/include/RebootAndShutdownModel.h"
16
17 namespace OHOS
18 {
19
20     static constexpr const int SLIDER_VALUE_MAX = 88;
21     static constexpr const int SLIDER_VALUE_MIN = 12;
22     // 当前Presenter属性实例化的内存指针
23     static RebootAndShutdownPresenter *g_pRebootAndShutdownPresenter = nullptr;
24
25     RebootAndShutdownPresenter::RebootAndShutdownPresenter()
26     {
27         model = &RebootAndShutdownModel::GetInstance();
28         g_pRebootAndShutdownPresenter = this;
29     }
30
31     RebootAndShutdownPresenter::~RebootAndShutdownPresenter()
32     {
33         g_pRebootAndShutdownPresenter = nullptr;
34     }
35
36     RebootAndShutdownPresenter *RebootAndShutdownPresenter::GetInstance()
37     {
38         return g_pRebootAndShutdownPresenter;
39     }
40
41     // 启动时执行的逻辑
42     void RebootAndShutdownPresenter::OnStart()
43     {
44         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnStart");
45         Presenter<RebootAndShutdownView>::OnStart();
46     }
47
48     // 页面已经展示后的调用逻辑
49     void RebootAndShutdownPresenter::OnResume(void)
50     {
51         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnResume");
52         Presenter<RebootAndShutdownView>::OnResume();
53         if (view_ == nullptr)
54         {
55             return;
56         }
57     }
58
59     // 页面准备切换到后台前执行的逻辑
60     void RebootAndShutdownPresenter::OnPause(void)
61     {
62         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnPause");
63         Presenter<RebootAndShutdownView>::OnPause();
64     }
65
66     // 页面完全置于后台时的逻辑。
67     void RebootAndShutdownPresenter::OnStop(void)
68     {
69         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnStop");
70         Presenter<RebootAndShutdownView>::OnStop();
71     }
72
73     bool RebootAndShutdownPresenter::OnClick(UIView &view, const ClickEvent &event)
74     {
75         UNUSED(event);
76         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnClick");
77         if (strcmp(view.GetViewId(), FACTORYRESET_LABEL_VIEWID) == 0) // 进入出厂设置界面
78         {
79             RebootAndShutdownView::GetInstance()->ChangeView(REBOOTANDSHUTDOWN, FACTORYRESET);
80             return true;
81         }
82         else if (strcmp(view.GetViewId(), FACTORY_RESET_BUTTON_CHECK_ON) == 0) // 恢复出厂设置确认
83         {
84             do_custom_cmd_service_check_msg(CUSTOM_CMD_MSG_TYPE_RESTORE_FACTORY_SETTING, NULL, 0);
85             return true;
86         }
87         else if (strcmp(view.GetViewId(), FACTORY_RESET_BUTTON_CHECK_OFF) == 0) // 恢复出厂设置取消
88         {
89             RebootAndShutdownView::GetInstance()->ChangeView(FACTORYRESET, REBOOTANDSHUTDOWN);
90         }
91         else if (strcmp(view.GetViewId(), SHUTDOWN_SLIDER_VIEWID) == 0) 
92         {
93             auto shutdownSlider = view_->GetShutdownSlider();
94             if (shutdownSlider != nullptr)
95             {
96                 shutdownSlider->SetValue(SLIDER_VALUE_MIN);
97             }
98         }
99         else if (strcmp(view.GetViewId(), REBOOT_SLIDER_VIEWID) == 0) 
100         {
101             auto rebootSlider = view_->GetRebootSlider();
102             if (rebootSlider != nullptr)
103             {
104                 rebootSlider->SetValue(SLIDER_VALUE_MIN);
105             }
106         }
107         return true;
108     }
109
110     bool RebootAndShutdownPresenter::OnDrag(UIView &view, const DragEvent &event)
111     {
112         UNUSED(view);
113         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnDrag");
114         if ((event.GetDeltaX() > X_DRAG_OFFSET) && (event.GetDeltaX() > (DOUBLES * MATH_ABS(event.GetDeltaY())))) // 拖动过程中判断是否是水平滑动
115         {
116             RebootAndShutdownPresenter::X_DRAG_FLAG = true;
117         }
118         return true;
119     }
120
121     bool RebootAndShutdownPresenter::OnDragEnd(UIView &view, const DragEvent &event)
122     {
123         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnDragEnd");
124         UISlider *slider = dynamic_cast<UISlider *>(&view);
125         if (slider != nullptr)
126         {
127             if (slider == view_->GetRebootSlider() || slider == view_->GetShutdownSlider())
128             {
129                 return true;
130             }
131         }
132         else
133         {
134             if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT && RebootAndShutdownPresenter::X_DRAG_FLAG)
135             {
136                 if (strcmp(view.GetViewId(), REBOOT_AND_SHUTDOWN_GROUPVIEW) == 0)
137                 {
138                     NativeAbility::GetInstance().Back(TransitionType::TRANSITION_BACK_TO_HEXAGONS);
139                 }
140                 else if (strcmp(view.GetViewId(), FACTORY_RESET_GROUPVIEW) == 0)
141                 {
142                     RebootAndShutdownView::GetInstance()->ChangeView(FACTORYRESET, REBOOTANDSHUTDOWN);
143                 }
144                 RebootAndShutdownPresenter::X_DRAG_FLAG = false;
145                 return true;
146             }
147         }
148         return false;
149     }
150
151     void RebootAndShutdownPresenter::OnAnimatorStop(UIView &view)
152     {
153         UNUSED(view);
154         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnAnimatorStop");
155         if (isShutDownOperation == true)
156         {
157             uapi_poweroff_device(); // 关机
158         }
159         else
160         {
161             uapi_reboot_device(); // 重启
162         }
163     }
164
165     void RebootAndShutdownPresenter::OnChange(int32_t value)
166     {
167         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnChange");
168     }
169
170     void RebootAndShutdownPresenter::OnRelease(int32_t value)
171     {
172         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnRelease,value = %d", value);
173         auto shutdownSlider = view_->GetShutdownSlider();
174         auto rebootSlider = view_->GetRebootSlider();
175
176         if (shutdownSlider->GetValue() < SLIDER_VALUE_MAX)
177         {
178             shutdownSlider->SetValue(SLIDER_VALUE_MIN);
179         }
180         else
181         {
182             shutdownSlider->SetValue(SLIDER_VALUE_MAX);
183             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "关机!!");
184
185             RebootAndShutdownView::GetInstance()->ChangeView(REBOOTANDSHUTDOWN, SHUTDOWNANIMATOR);
186
187             start_shutdown_or_reboot();
188             shutdown_reboot_prejobs();
189
190             ShutDownImageAnimatorView::GetInstance()->GeShutDownImageAnimatorView()->Start();
191
192             isShutDownOperation = true;
193         }
194
195         if (rebootSlider->GetValue() < SLIDER_VALUE_MAX)
196         {
197             rebootSlider->SetValue(SLIDER_VALUE_MIN);
198         }
199         else
200         {
201             rebootSlider->SetValue(SLIDER_VALUE_MAX);
202             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "重启!!");
203             RebootAndShutdownView::GetInstance()->ChangeView(REBOOTANDSHUTDOWN, SHUTDOWNANIMATOR);
204             start_shutdown_or_reboot();
205             shutdown_reboot_prejobs();
206
207             ShutDownImageAnimatorView::GetInstance()->GeShutDownImageAnimatorView()->Start();
208
209             isShutDownOperation = false;
210         }
211     }
212
213     bool RebootAndShutdownPresenter::OnKeyAct(UIView &view, const KeyEvent &event) // 增加按键事件
214     {
215         UNUSED(view);
216         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RebootAndShutdownPresenter::OnKeyAct");
217         uint16_t keyID = event.GetKeyId();
218         uint16_t keyState = event.GetState();
219         // 如果是power键,且是亮屏状态,按键释放就返回上一级
220         if (keyID == static_cast<uint16_t>(ZliteKeyCode::ZLITE_KEY_POWER) && keyState == 0 && uapi_get_screen_state() == 0)
221         {
222             NativeAbility::GetInstance().Back(TransitionType::TRANSITION_BACK_TO_HEXAGONS);
223         }
224         return true;
225     }
226 }