• 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_ {UpdaterMode::MODEMAX}
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(UpdaterMode mode)43 [[nodiscard]] bool UpdaterUiFacade::SetMode(UpdaterMode mode)
44 {
45     if (mode < UpdaterMode::SDCARD || mode >= UpdaterMode::MODEMAX) {
46         LOG(ERROR) << "updater mode invalid";
47         return false;
48     }
49     if (mode == mode_) {
50         return true;
51     }
52     mode_ = mode;
53     SetLogoProgress();
54     return true;
55 }
56 
GetMode() const57 UpdaterMode UpdaterUiFacade::GetMode() const
58 {
59     return mode_;
60 }
61 
CheckMode() const62 std::pair<bool, UpdaterUiFacade::StrategyMap::const_iterator> UpdaterUiFacade::CheckMode() const
63 {
64     if (mode_ < UpdaterMode::SDCARD || mode_ >= UpdaterMode::MODEMAX) {
65         LOG(ERROR) << "mode invalid";
66         return {false, strategies_.cend()};
67     }
68     auto it = strategies_.find(mode_);
69     if (it == strategies_.end()) {
70         LOG(ERROR) << "mode has not a strategy for it";
71         return {false, strategies_.cend()};
72     }
73     return {true, it};
74 }
75 
ShowLog(const std::string & tag,bool isClear) const76 void UpdaterUiFacade::ShowLog(const std::string &tag, bool isClear) const
77 {
78     if (auto [res, it] = CheckMode(); res) {
79         ShowMsg(it->second.labelLogId, tag, isClear);
80     }
81 }
82 
ShowLogRes(const std::string & tag,bool isClear) const83 void UpdaterUiFacade::ShowLogRes(const std::string &tag, bool isClear) const
84 {
85     if (auto [res, it] = CheckMode(); res) {
86         ShowMsg(it->second.labelLogResId, tag, isClear);
87     }
88 }
89 
ShowUpdInfo(const std::string & tag,bool isClear) const90 void UpdaterUiFacade::ShowUpdInfo(const std::string &tag, bool isClear) const
91 {
92     if (auto [res, it] = CheckMode(); res) {
93         ShowMsg(it->second.labelUpdId, tag, isClear);
94     }
95 }
96 
ShowProgress(float value) const97 void UpdaterUiFacade::ShowProgress(float value) const
98 {
99     if (!CheckMode().first) {
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         it->second->ShowProgress(value);
109         return;
110     }
111     LOG(ERROR) << "progress is null, can't show progress";
112 }
113 
IsInProgress() const114 bool UpdaterUiFacade::IsInProgress() const
115 {
116     if (auto [res, it] = CheckMode(); res) {
117         return pgMgr_[it->second.progressPage.progressPageId].IsVisible();
118     }
119     return false;
120 }
121 
SetLogoVisible(bool isVisible) const122 void UpdaterUiFacade::SetLogoVisible(bool isVisible) const
123 {
124     if (!CheckMode().first) {
125         return;
126     }
127     if (auto it = logoMap_.find(mode_); it->second != nullptr) {
128         isVisible ? it->second->Show() : it->second->Hide();
129         return;
130     }
131     LOG(ERROR) << "logo is null, can't show logo";
132 }
133 
SetProgressVisible(bool isVisible) const134 void UpdaterUiFacade::SetProgressVisible(bool isVisible) const
135 {
136     if (!CheckMode().first) {
137         return;
138     }
139     if (auto it = progressMap_.find(mode_); it->second != nullptr) {
140         isVisible ? it->second->Show() : it->second->Hide();
141         return;
142     }
143     LOG(ERROR) << "progress is null, can't show progress";
144 }
145 
ShowProgressWarning(bool isShow) const146 void UpdaterUiFacade::ShowProgressWarning(bool isShow) const
147 {
148     if (auto [res, it] = CheckMode(); res) {
149         auto &progressPg = it->second.progressPage;
150         pgMgr_[progressPg.progressPageId][progressPg.warningComId]->SetVisible(isShow);
151     }
152 }
153 
ShowProgressPage() const154 void UpdaterUiFacade::ShowProgressPage() const
155 {
156     auto [res, it] = CheckMode();
157     if (IsInProgress() || !res) {
158         return;
159     }
160     SetProgressVisible(true);
161     SetLogoVisible(true);
162     ShowProgress(0);
163     pgMgr_.ShowPage(it->second.progressPage.progressPageId);
164     ShowProgressWarning(false);
165 }
166 
ShowSuccessPage() const167 void UpdaterUiFacade::ShowSuccessPage() const
168 {
169     auto [res, it] = CheckMode();
170     if (!res) {
171         return;
172     }
173     LOG(DEBUG) << "show success page";
174     SetProgressVisible(false);
175     SetLogoVisible(false);
176     ShowProgressWarning(false);
177     pgMgr_.ShowPage(it->second.resPage.successPageId);
178 }
179 
ShowFailedPage() const180 void UpdaterUiFacade::ShowFailedPage() const
181 {
182     auto [res, it] = CheckMode();
183     if (!res) {
184         return;
185     }
186     LOG(DEBUG) << "show failed page";
187     SetProgressVisible(false);
188     SetLogoVisible(false);
189     ShowProgressWarning(false);
190     pgMgr_.ShowPage(it->second.resPage.failPageId);
191 }
192 
ShowFactoryConfirmPage()193 void UpdaterUiFacade::ShowFactoryConfirmPage()
194 {
195     auto [res, it] = CheckMode();
196     if (!res) {
197         return;
198     }
199     LOG(DEBUG) << "show confirm page";
200     ClearLog();
201     pgMgr_.ShowPage(it->second.confirmPageId);
202 }
203 
ShowMainpage() const204 void UpdaterUiFacade::ShowMainpage() const
205 {
206     pgMgr_.ShowMainPage();
207 }
208 
ClearText() const209 void UpdaterUiFacade::ClearText() const
210 {
211     auto [res, it] = CheckMode();
212     if (!res) {
213         return;
214     }
215     ClearLog();
216     ShowMsg(it->second.labelUpdId, "");
217 }
218 
ClearLog() const219 void UpdaterUiFacade::ClearLog() const
220 {
221     if (auto [res, it] = CheckMode(); res) {
222         ShowMsg(it->second.labelLogId, "");
223         ShowMsg(it->second.labelLogResId, "");
224     }
225 }
226 
ShowMsg(const ComInfo & id,const std::string & tag,bool isClear) const227 void UpdaterUiFacade::ShowMsg(const ComInfo &id, const std::string &tag, bool isClear) const
228 {
229     if (isClear) {
230         LOG(INFO) << "clear all log label's text";
231         ClearText();
232     }
233     pgMgr_[id.pageId][id.comId].As<TextLabelAdapter>()->SetText(tag);
234 }
235 
ShowMsg(const ComInfo & id,const std::string & tag) const236 void UpdaterUiFacade::ShowMsg(const ComInfo &id, const std::string &tag) const
237 {
238     pgMgr_[id.pageId][id.comId].As<TextLabelAdapter>()->SetText(tag);
239 }
240 
SetLogoProgress()241 void UpdaterUiFacade::SetLogoProgress()
242 {
243     auto [res, it] = CheckMode();
244     if (!res) {
245         return;
246     }
247     const ProgressPage &progressPage { it->second.progressPage };
248     if (progressMap_.find(mode_) == progressMap_.end()) {
249         progressMap_[mode_] = ProgressStrategy::Factory(progressPage.progressType, {
250             progressPage.progressPageId, progressPage.progressComId
251         });
252     }
253     if (logoMap_.find(mode_) == logoMap_.end()) {
254         logoMap_[mode_] = LogoStrategy::Factory(progressPage.logoType, {
255             progressPage.progressPageId, progressPage.logoComId
256         });
257     }
258 }
259 
Sleep(int ms) const260 void UpdaterUiFacade::Sleep(int ms) const
261 {
262     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
263 }
264 
SaveScreen() const265 void UpdaterUiFacade::SaveScreen() const
266 {
267     UpdaterUiTools::SaveUxBuffToFile("/tmp/mainpage.png");
268 }
269 
OnKeyUpEvent()270 void OnKeyUpEvent()
271 {
272     UpdaterUiFacade::GetInstance().ShowProgressWarning(false);
273 }
274 
OnKeyDownEvent()275 void OnKeyDownEvent()
276 {
277     UpdaterUiFacade::GetInstance().ShowProgressWarning(true);
278 }
279 } // namespace Updater