• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
UpdaterUiFacade()25 UpdaterUiFacade::UpdaterUiFacade()
26     : strategies_ {UpdaterUiConfig::GetStrategy()}, pgMgr_ {PageManager::GetInstance()}, mode_ {""}
27 {
28 }
29 
GetInstance()30 UpdaterUiFacade &UpdaterUiFacade::GetInstance()
31 {
32     static UpdaterUiFacade instance;
33     return instance;
34 }
35 
InitEnv() const36 void UpdaterUiFacade::InitEnv() const
37 {
38     UpdaterUiEnv::Init();
39     UpdaterEvent::Subscribe(UPDATER_POWER_VOLUME_UP_EVENT, OnKeyUpEvent);
40     UpdaterEvent::Subscribe(UPDATER_POWER_VOLUME_DOWN_EVENT, OnKeyDownEvent);
41 }
42 
SetMode(std::string mode)43 [[nodiscard]] bool UpdaterUiFacade::SetMode(std::string mode)
44 {
45     if (mode == mode_) {
46         return true;
47     }
48     mode_ = mode;
49     SetLogoProgress();
50     return true;
51 }
52 
GetMode() const53 std::string UpdaterUiFacade::GetMode() const
54 {
55     return mode_;
56 }
57 
CheckMode() const58 std::pair<bool, UpdaterUiFacade::StrategyMap::const_iterator> UpdaterUiFacade::CheckMode() const
59 {
60     auto it = strategies_.find(mode_);
61     if (it == strategies_.end()) {
62         LOG(ERROR) << "mode has not a strategy for it " << mode_;
63         return {false, strategies_.cend()};
64     }
65     return {true, it};
66 }
67 
ShowLog(const std::string & tag,bool isClear) const68 void UpdaterUiFacade::ShowLog(const std::string &tag, bool isClear) const
69 {
70     if (auto [res, it] = CheckMode(); res) {
71         ShowMsg(it->second.labelLogId, tag, isClear);
72     }
73 }
74 
ShowLogRes(const std::string & tag,bool isClear) const75 void UpdaterUiFacade::ShowLogRes(const std::string &tag, bool isClear) const
76 {
77     if (auto [res, it] = CheckMode(); res) {
78         ShowMsg(it->second.labelLogResId, tag, isClear);
79     }
80 }
81 
ShowUpdInfo(const std::string & tag,bool isClear) const82 void UpdaterUiFacade::ShowUpdInfo(const std::string &tag, bool isClear) const
83 {
84     if (auto [res, it] = CheckMode(); res) {
85         ShowMsg(it->second.labelUpdId, tag, isClear);
86     }
87 }
88 
ShowProgress(float value) const89 void UpdaterUiFacade::ShowProgress(float value) const
90 {
91     if (!CheckMode().first) {
92         return;
93     }
94     static float lastValue = 0.0;
95     if (abs(value - lastValue) > 0.01) { // 0.01 : The progress bar changes by more than 0.01
96         LOG(INFO) << "current progress " << value;
97         lastValue = value;
98     }
99     if (auto it = progressMap_.find(mode_); it->second != nullptr) {
100         it->second->ShowProgress(value);
101         return;
102     }
103     LOG(ERROR) << "progress is null, can't show progress";
104 }
105 
IsInProgress() const106 bool UpdaterUiFacade::IsInProgress() const
107 {
108     if (auto [res, it] = CheckMode(); res) {
109         return pgMgr_[it->second.progressPage.progressPageId].IsVisible();
110     }
111     return false;
112 }
113 
SetLogoVisible(bool isVisible) const114 void UpdaterUiFacade::SetLogoVisible(bool isVisible) const
115 {
116     if (!CheckMode().first) {
117         return;
118     }
119     if (auto it = logoMap_.find(mode_); it->second != nullptr) {
120         isVisible ? it->second->Show() : it->second->Hide();
121         return;
122     }
123     LOG(ERROR) << "logo is null, can't show logo";
124 }
125 
SetProgressVisible(bool isVisible) const126 void UpdaterUiFacade::SetProgressVisible(bool isVisible) const
127 {
128     if (!CheckMode().first) {
129         return;
130     }
131     if (auto it = progressMap_.find(mode_); it->second != nullptr) {
132         isVisible ? it->second->Show() : it->second->Hide();
133         return;
134     }
135     LOG(ERROR) << "progress is null, can't show progress";
136 }
137 
ShowProgressWarning(bool isShow) const138 void UpdaterUiFacade::ShowProgressWarning(bool isShow) const
139 {
140     if (auto [res, it] = CheckMode(); res) {
141         auto &progressPg = it->second.progressPage;
142         pgMgr_[progressPg.progressPageId][progressPg.warningComId]->SetVisible(isShow);
143     }
144 }
145 
ShowProgressPage() const146 void UpdaterUiFacade::ShowProgressPage() const
147 {
148     auto [res, it] = CheckMode();
149     if (IsInProgress() || !res) {
150         return;
151     }
152     SetProgressVisible(true);
153     SetLogoVisible(true);
154     ShowProgress(0);
155     pgMgr_.ShowPage(it->second.progressPage.progressPageId);
156     ShowProgressWarning(false);
157 }
158 
ShowSuccessPage() const159 void UpdaterUiFacade::ShowSuccessPage() const
160 {
161     auto [res, it] = CheckMode();
162     if (!res) {
163         return;
164     }
165     LOG(DEBUG) << "show success page";
166     SetProgressVisible(false);
167     SetLogoVisible(false);
168     ShowProgressWarning(false);
169     pgMgr_.ShowPage(it->second.resPage.successPageId);
170 }
171 
ShowFailedPage() const172 void UpdaterUiFacade::ShowFailedPage() const
173 {
174     auto [res, it] = CheckMode();
175     if (!res) {
176         return;
177     }
178     LOG(DEBUG) << "show failed page";
179     SetProgressVisible(false);
180     SetLogoVisible(false);
181     ShowProgressWarning(false);
182     pgMgr_.ShowPage(it->second.resPage.failPageId);
183 }
184 
ShowFactoryConfirmPage()185 void UpdaterUiFacade::ShowFactoryConfirmPage()
186 {
187     auto [res, it] = CheckMode();
188     if (!res) {
189         return;
190     }
191     LOG(DEBUG) << "show confirm page";
192     ClearLog();
193     pgMgr_.ShowPage(it->second.confirmPageId);
194 }
195 
ShowMainpage() const196 void UpdaterUiFacade::ShowMainpage() const
197 {
198     pgMgr_.ShowMainPage();
199 }
200 
ClearText() const201 void UpdaterUiFacade::ClearText() const
202 {
203     auto [res, it] = CheckMode();
204     if (!res) {
205         return;
206     }
207     ClearLog();
208     ShowMsg(it->second.labelUpdId, "");
209 }
210 
ClearLog() const211 void UpdaterUiFacade::ClearLog() const
212 {
213     if (auto [res, it] = CheckMode(); res) {
214         ShowMsg(it->second.labelLogId, "");
215         ShowMsg(it->second.labelLogResId, "");
216     }
217 }
218 
ShowMsg(const ComInfo & id,const std::string & tag,bool isClear) const219 void UpdaterUiFacade::ShowMsg(const ComInfo &id, const std::string &tag, bool isClear) const
220 {
221     if (isClear) {
222         LOG(INFO) << "clear all log label's text";
223         ClearText();
224     }
225     pgMgr_[id.pageId][id.comId].As<TextLabelAdapter>()->SetText(tag);
226 }
227 
ShowMsg(const ComInfo & id,const std::string & tag) const228 void UpdaterUiFacade::ShowMsg(const ComInfo &id, const std::string &tag) const
229 {
230     pgMgr_[id.pageId][id.comId].As<TextLabelAdapter>()->SetText(tag);
231 }
232 
SetLogoProgress()233 void UpdaterUiFacade::SetLogoProgress()
234 {
235     auto [res, it] = CheckMode();
236     if (!res) {
237         return;
238     }
239     const ProgressPage &progressPage { it->second.progressPage };
240     if (progressMap_.find(mode_) == progressMap_.end()) {
241         progressMap_[mode_] = ProgressStrategy::Factory(progressPage.progressType, {
242             progressPage.progressPageId, progressPage.progressComId
243         });
244     }
245     if (logoMap_.find(mode_) == logoMap_.end()) {
246         logoMap_[mode_] = LogoStrategy::Factory(progressPage.logoType, {
247             progressPage.progressPageId, progressPage.logoComId
248         });
249     }
250 }
251 
Sleep(int ms) const252 void UpdaterUiFacade::Sleep(int ms) const
253 {
254     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
255 }
256 
SaveScreen() const257 void UpdaterUiFacade::SaveScreen() const
258 {
259     UpdaterUiTools::SaveUxBuffToFile("/tmp/mainpage.png");
260 }
261 
OnKeyUpEvent()262 void OnKeyUpEvent()
263 {
264     UpdaterUiFacade::GetInstance().ShowProgressWarning(false);
265 }
266 
OnKeyDownEvent()267 void OnKeyDownEvent()
268 {
269     UpdaterUiFacade::GetInstance().ShowProgressWarning(true);
270 }
271 } // namespace Updater