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]->SetVisible(isShow);
152     }
153 }
154 
ShowProgressPage() const155 void UpdaterUiFacade::ShowProgressPage() const
156 {
157     auto [res, it] = CheckMode();
158     if (IsInProgress() || !res) {
159         return;
160     }
161     SetProgressVisible(true);
162     SetLogoVisible(true);
163     ShowProgress(0);
164     pgMgr_.ShowPage(it->second.progressPage.progressPageId);
165     ShowProgressWarning(false);
166 }
167 
ShowSuccessPage() const168 void UpdaterUiFacade::ShowSuccessPage() const
169 {
170     auto [res, it] = CheckMode();
171     if (!res) {
172         return;
173     }
174     LOG(DEBUG) << "show success page";
175     SetProgressVisible(false);
176     SetLogoVisible(false);
177     ShowProgressWarning(false);
178     pgMgr_.ShowPage(it->second.resPage.successPageId);
179 }
180 
ShowFailedPage() const181 void UpdaterUiFacade::ShowFailedPage() const
182 {
183     auto [res, it] = CheckMode();
184     if (!res) {
185         return;
186     }
187     LOG(DEBUG) << "show failed page";
188     SetProgressVisible(false);
189     SetLogoVisible(false);
190     ShowProgressWarning(false);
191     pgMgr_.ShowPage(it->second.resPage.failPageId);
192 }
193 
ShowFactoryConfirmPage()194 void UpdaterUiFacade::ShowFactoryConfirmPage()
195 {
196     auto [res, it] = CheckMode();
197     if (!res) {
198         return;
199     }
200     LOG(DEBUG) << "show confirm page";
201     ClearLog();
202     pgMgr_.ShowPage(it->second.confirmPageId);
203 }
204 
ShowMainpage() const205 void UpdaterUiFacade::ShowMainpage() const
206 {
207     pgMgr_.ShowMainPage();
208 }
209 
ClearText() const210 void UpdaterUiFacade::ClearText() const
211 {
212     auto [res, it] = CheckMode();
213     if (!res) {
214         return;
215     }
216     ClearLog();
217     ShowMsg(it->second.labelUpdId, "");
218 }
219 
ClearLog() const220 void UpdaterUiFacade::ClearLog() const
221 {
222     if (auto [res, it] = CheckMode(); res) {
223         ShowMsg(it->second.labelLogId, "");
224         ShowMsg(it->second.labelLogResId, "");
225     }
226 }
227 
ShowMsg(const ComInfo & id,const std::string & tag,bool isClear) const228 void UpdaterUiFacade::ShowMsg(const ComInfo &id, const std::string &tag, bool isClear) const
229 {
230     if (isClear) {
231         LOG(INFO) << "clear all log label's text";
232         ClearText();
233     }
234     pgMgr_[id.pageId][id.comId].As<TextLabelAdapter>()->SetText(tag);
235 }
236 
ShowMsg(const ComInfo & id,const std::string & tag) const237 void UpdaterUiFacade::ShowMsg(const ComInfo &id, const std::string &tag) const
238 {
239     pgMgr_[id.pageId][id.comId].As<TextLabelAdapter>()->SetText(tag);
240 }
241 
SetLogoProgress()242 void UpdaterUiFacade::SetLogoProgress()
243 {
244     auto [res, it] = CheckMode();
245     if (!res) {
246         return;
247     }
248     const ProgressPage &progressPage { it->second.progressPage };
249     if (progressMap_.find(mode_) == progressMap_.end()) {
250         progressMap_[mode_] = ProgressStrategy::Factory(progressPage.progressType, {
251             progressPage.progressPageId, progressPage.progressComId
252         });
253     }
254     if (logoMap_.find(mode_) == logoMap_.end()) {
255         logoMap_[mode_] = LogoStrategy::Factory(progressPage.logoType, {
256             progressPage.progressPageId, progressPage.logoComId
257         });
258     }
259 }
260 
Sleep(int ms) const261 void UpdaterUiFacade::Sleep(int ms) const
262 {
263     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
264 }
265 
SaveScreen() const266 void UpdaterUiFacade::SaveScreen() const
267 {
268     UpdaterUiTools::SaveUxBuffToFile("/tmp/mainpage.png");
269 }
270 
OnKeyUpEvent()271 void OnKeyUpEvent()
272 {
273     UpdaterUiFacade::GetInstance().ShowProgressWarning(false);
274 }
275 
OnKeyDownEvent()276 void OnKeyDownEvent()
277 {
278     UpdaterUiFacade::GetInstance().ShowProgressWarning(true);
279 }
280 } // namespace Updater