1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "updater_ui_facade.h"
17 #include <thread>
18 #include "component/text_label_adapter.h"
19 #include "updater_event.h"
20 #include "updater_ui_config.h"
21 #include "updater_ui_env.h"
22 #include "updater_ui_tools.h"
23
24 namespace Updater {
25 constexpr int FULL_PERCENT_PROGRESS = 100;
26 static float g_currentPercent = 0.0;
27
UpdaterUiFacade()28 UpdaterUiFacade::UpdaterUiFacade()
29 : strategies_ {UpdaterUiConfig::GetStrategy()}, pgMgr_ {PageManager::GetInstance()}, mode_ {""}
30 {
31 }
32
GetInstance()33 UpdaterUiFacade &UpdaterUiFacade::GetInstance()
34 {
35 static UpdaterUiFacade instance;
36 return instance;
37 }
38
InitEnv() const39 void UpdaterUiFacade::InitEnv() const
40 {
41 UpdaterUiEnv::Init();
42 UpdaterEvent::Subscribe(UPDATER_POWER_VOLUME_UP_EVENT, OnKeyUpEvent);
43 UpdaterEvent::Subscribe(UPDATER_POWER_VOLUME_DOWN_EVENT, OnKeyDownEvent);
44 }
45
SetMode(std::string mode)46 [[nodiscard]] bool UpdaterUiFacade::SetMode(std::string mode)
47 {
48 if (mode == mode_) {
49 return true;
50 }
51 mode_ = mode;
52 SetLogoProgress();
53 return true;
54 }
55
GetMode() const56 std::string UpdaterUiFacade::GetMode() const
57 {
58 return mode_;
59 }
60
CheckMode() const61 std::pair<bool, UpdaterUiFacade::StrategyMap::const_iterator> UpdaterUiFacade::CheckMode() const
62 {
63 auto it = strategies_.find(mode_);
64 if (it == strategies_.end()) {
65 LOG(ERROR) << "mode has not a strategy for it " << mode_;
66 return {false, strategies_.cend()};
67 }
68 return {true, it};
69 }
70
ShowLog(const std::string & tag,bool isClear) const71 void UpdaterUiFacade::ShowLog(const std::string &tag, bool isClear) const
72 {
73 if (auto [res, it] = CheckMode(); res) {
74 ShowMsg(it->second.labelLogId, tag, isClear);
75 }
76 }
77
ShowLogRes(const std::string & tag,bool isClear) const78 void UpdaterUiFacade::ShowLogRes(const std::string &tag, bool isClear) const
79 {
80 if (auto [res, it] = CheckMode(); res) {
81 ShowMsg(it->second.labelLogResId, tag, isClear);
82 }
83 }
84
ShowUpdInfo(const std::string & tag,bool isClear) const85 void UpdaterUiFacade::ShowUpdInfo(const std::string &tag, bool isClear) const
86 {
87 if (auto [res, it] = CheckMode(); res) {
88 ShowMsg(it->second.labelUpdId, tag, isClear);
89 }
90 }
91
GetCurrentPercent(void)92 float UpdaterUiFacade::GetCurrentPercent(void)
93 {
94 return g_currentPercent;
95 }
96
ShowProgress(float value) const97 void UpdaterUiFacade::ShowProgress(float value) const
98 {
99 if (!CheckMode().first || (value > FULL_PERCENT_PROGRESS)) {
100 return;
101 }
102 static float lastValue = 0.0;
103 if (abs(value - lastValue) > 0.01) { // 0.01 : The progress bar changes by more than 0.01
104 LOG(INFO) << "current progress " << value;
105 lastValue = value;
106 }
107 if (auto it = progressMap_.find(mode_); it->second != nullptr) {
108 g_currentPercent = value;
109 it->second->ShowProgress(value);
110 return;
111 }
112 LOG(ERROR) << "progress is null, can't show progress";
113 }
114
IsInProgress() const115 bool UpdaterUiFacade::IsInProgress() const
116 {
117 if (auto [res, it] = CheckMode(); res) {
118 return pgMgr_[it->second.progressPage.progressPageId].IsVisible();
119 }
120 return false;
121 }
122
SetLogoVisible(bool isVisible) const123 void UpdaterUiFacade::SetLogoVisible(bool isVisible) const
124 {
125 if (!CheckMode().first) {
126 return;
127 }
128 if (auto it = logoMap_.find(mode_); it->second != nullptr) {
129 isVisible ? it->second->Show() : it->second->Hide();
130 return;
131 }
132 LOG(ERROR) << "logo is null, can't show logo";
133 }
134
SetProgressVisible(bool isVisible) const135 void UpdaterUiFacade::SetProgressVisible(bool isVisible) const
136 {
137 if (!CheckMode().first) {
138 return;
139 }
140 if (auto it = progressMap_.find(mode_); it->second != nullptr) {
141 isVisible ? it->second->Show() : it->second->Hide();
142 return;
143 }
144 LOG(ERROR) << "progress is null, can't show progress";
145 }
146
ShowProgressWarning(bool isShow) const147 void UpdaterUiFacade::ShowProgressWarning(bool isShow) const
148 {
149 if (auto [res, it] = CheckMode(); res) {
150 auto &progressPg = it->second.progressPage;
151 pgMgr_[progressPg.progressPageId][progressPg.warningComId]->SetZIndex(10); // 10: Raise the level
152 pgMgr_[progressPg.progressPageId][progressPg.warningComId]->SetVisible(isShow);
153 }
154 }
155
ShowProgressPage() const156 void UpdaterUiFacade::ShowProgressPage() const
157 {
158 auto [res, it] = CheckMode();
159 if (IsInProgress() || !res) {
160 return;
161 }
162 SetProgressVisible(true);
163 SetLogoVisible(true);
164 ShowProgress(0);
165 pgMgr_.ShowPage(it->second.progressPage.progressPageId);
166 ShowProgressWarning(false);
167 }
168
ShowSuccessPage() const169 void UpdaterUiFacade::ShowSuccessPage() const
170 {
171 auto [res, it] = CheckMode();
172 if (!res) {
173 return;
174 }
175 LOG(DEBUG) << "show success page";
176 SetProgressVisible(false);
177 SetLogoVisible(false);
178 ShowProgressWarning(false);
179 pgMgr_.ShowPage(it->second.resPage.successPageId);
180 }
181
ShowFailedPage() const182 void UpdaterUiFacade::ShowFailedPage() const
183 {
184 auto [res, it] = CheckMode();
185 if (!res) {
186 return;
187 }
188 LOG(DEBUG) << "show failed page";
189 SetProgressVisible(false);
190 SetLogoVisible(false);
191 ShowProgressWarning(false);
192 pgMgr_.ShowPage(it->second.resPage.failPageId);
193 }
194
ShowFactoryConfirmPage()195 void UpdaterUiFacade::ShowFactoryConfirmPage()
196 {
197 auto [res, it] = CheckMode();
198 if (!res) {
199 return;
200 }
201 LOG(DEBUG) << "show confirm page";
202 ClearLog();
203 pgMgr_.ShowPage(it->second.confirmPageId);
204 }
205
ShowMainpage() const206 void UpdaterUiFacade::ShowMainpage() const
207 {
208 pgMgr_.ShowMainPage();
209 }
210
ClearText() const211 void UpdaterUiFacade::ClearText() const
212 {
213 auto [res, it] = CheckMode();
214 if (!res) {
215 return;
216 }
217 ClearLog();
218 ShowMsg(it->second.labelUpdId, "");
219 }
220
ClearLog() const221 void UpdaterUiFacade::ClearLog() const
222 {
223 if (auto [res, it] = CheckMode(); res) {
224 ShowMsg(it->second.labelLogId, "");
225 ShowMsg(it->second.labelLogResId, "");
226 }
227 }
228
ShowMsg(const ComInfo & id,const std::string & tag,bool isClear) const229 void UpdaterUiFacade::ShowMsg(const ComInfo &id, const std::string &tag, bool isClear) const
230 {
231 if (isClear) {
232 LOG(INFO) << "clear all log label's text";
233 ClearText();
234 }
235 pgMgr_[id.pageId][id.comId].As<TextLabelAdapter>()->SetText(tag);
236 }
237
ShowMsg(const ComInfo & id,const std::string & tag) const238 void UpdaterUiFacade::ShowMsg(const ComInfo &id, const std::string &tag) const
239 {
240 pgMgr_[id.pageId][id.comId].As<TextLabelAdapter>()->SetText(tag);
241 }
242
SetLogoProgress()243 void UpdaterUiFacade::SetLogoProgress()
244 {
245 auto [res, it] = CheckMode();
246 if (!res) {
247 return;
248 }
249 const ProgressPage &progressPage { it->second.progressPage };
250 if (progressMap_.find(mode_) == progressMap_.end()) {
251 progressMap_[mode_] = ProgressStrategy::Factory(progressPage.progressType, {
252 progressPage.progressPageId, progressPage.progressComId
253 });
254 }
255 if (logoMap_.find(mode_) == logoMap_.end()) {
256 logoMap_[mode_] = LogoStrategy::Factory(progressPage.logoType, {
257 progressPage.progressPageId, progressPage.logoComId
258 });
259 }
260 }
261
Sleep(int ms) const262 void UpdaterUiFacade::Sleep(int ms) const
263 {
264 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
265 }
266
SaveScreen() const267 void UpdaterUiFacade::SaveScreen() const
268 {
269 Sleep(50); /* wait for page flush 50ms */
270 UpdaterUiTools::SaveUxBuffToFile("/tmp/mainpage.png");
271 }
272
OnKeyUpEvent()273 void OnKeyUpEvent()
274 {
275 UpdaterUiFacade::GetInstance().ShowProgressWarning(false);
276 }
277
OnKeyDownEvent()278 void OnKeyDownEvent()
279 {
280 UpdaterUiFacade::GetInstance().ShowProgressWarning(true);
281 }
282 } // namespace Updater