• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_ui_config.h"
20 #include "updater_ui_env.h"
21 #include "updater_ui_tools.h"
22 
23 namespace Updater {
UpdaterUiFacade()24 UpdaterUiFacade::UpdaterUiFacade()
25     : strategies_ {UpdaterUiConfig::GetStrategy()}, pgMgr_ {PageManager::GetInstance()}, mode_ {UpdaterMode::MODEMAX}
26 {
27 }
28 
GetInstance()29 UpdaterUiFacade &UpdaterUiFacade::GetInstance()
30 {
31     static UpdaterUiFacade instance;
32     return instance;
33 }
34 
InitEnv() const35 void UpdaterUiFacade::InitEnv() const
36 {
37     UpdaterUiEnv::Init();
38 }
39 
SetMode(UpdaterMode mode)40 [[nodiscard]] bool UpdaterUiFacade::SetMode(UpdaterMode mode)
41 {
42     if (mode < UpdaterMode::SDCARD || mode >= UpdaterMode::MODEMAX) {
43         LOG(ERROR) << "updater mode invalid";
44         return false;
45     }
46     if (mode == mode_) {
47         return true;
48     }
49     mode_ = mode;
50     SetLogoProgress();
51     return true;
52 }
53 
GetMode() const54 UpdaterMode UpdaterUiFacade::GetMode() const
55 {
56     return mode_;
57 }
58 
CheckMode() const59 std::pair<bool, UpdaterUiFacade::StrategyMap::const_iterator> UpdaterUiFacade::CheckMode() const
60 {
61     if (mode_ < UpdaterMode::SDCARD || mode_ >= UpdaterMode::MODEMAX) {
62         LOG(ERROR) << "mode invalid";
63         return {false, strategies_.cend()};
64     }
65     auto it = strategies_.find(mode_);
66     if (it == strategies_.end()) {
67         LOG(ERROR) << "mode has not a strategy for it";
68         return {false, strategies_.cend()};
69     }
70     return {true, it};
71 }
72 
ShowLog(const std::string & tag,bool isClear) const73 void UpdaterUiFacade::ShowLog(const std::string &tag, bool isClear) const
74 {
75     if (auto [res, it] = CheckMode(); res) {
76         ShowMsg(it->second.labelLogId, tag, isClear);
77     }
78 }
79 
ShowLogRes(const std::string & tag,bool isClear) const80 void UpdaterUiFacade::ShowLogRes(const std::string &tag, bool isClear) const
81 {
82     if (auto [res, it] = CheckMode(); res) {
83         ShowMsg(it->second.labelLogResId, tag, isClear);
84     }
85 }
86 
ShowUpdInfo(const std::string & tag,bool isClear) const87 void UpdaterUiFacade::ShowUpdInfo(const std::string &tag, bool isClear) const
88 {
89     if (auto [res, it] = CheckMode(); res) {
90         ShowMsg(it->second.labelUpdId, tag, isClear);
91     }
92 }
93 
ShowProgress(float value) const94 void UpdaterUiFacade::ShowProgress(float value) const
95 {
96     if (!CheckMode().first) {
97         return;
98     }
99     static float lastValue = 0.0;
100     if (abs(value - lastValue) > 0.01) { // 0.01 : The progress bar changes by more than 0.01
101         LOG(INFO) << "current progress " << value;
102         lastValue = value;
103     }
104     if (auto it = progressMap_.find(mode_); it->second != nullptr) {
105         it->second->ShowProgress(value);
106         return;
107     }
108     LOG(ERROR) << "progress is null, can't show progress";
109 }
110 
IsInProgress() const111 bool UpdaterUiFacade::IsInProgress() const
112 {
113     if (auto [res, it] = CheckMode(); res) {
114         return pgMgr_[it->second.progressPage.progressPageId].IsVisible();
115     }
116     return false;
117 }
118 
SetLogoVisible(bool isVisible) const119 void UpdaterUiFacade::SetLogoVisible(bool isVisible) const
120 {
121     if (!CheckMode().first) {
122         return;
123     }
124     if (auto it = logoMap_.find(mode_); it->second != nullptr) {
125         isVisible ? it->second->Show() : it->second->Hide();
126         return;
127     }
128     LOG(ERROR) << "logo is null, can't show logo";
129 }
130 
SetProgressVisible(bool isVisible) const131 void UpdaterUiFacade::SetProgressVisible(bool isVisible) const
132 {
133     if (!CheckMode().first) {
134         return;
135     }
136     if (auto it = progressMap_.find(mode_); it->second != nullptr) {
137         isVisible ? it->second->Show() : it->second->Hide();
138         return;
139     }
140     LOG(ERROR) << "progress is null, can't show progress";
141 }
142 
ShowProgressWarning(bool isShow) const143 void UpdaterUiFacade::ShowProgressWarning(bool isShow) const
144 {
145     if (auto [res, it] = CheckMode(); res) {
146         const auto &progressPg = it->second.progressPage;
147         pgMgr_[progressPg.progressPageId][progressPg.warningComId]->SetVisible(isShow);
148     }
149 }
150 
ShowProgressPage() const151 void UpdaterUiFacade::ShowProgressPage() const
152 {
153     auto [res, it] = CheckMode();
154     if (IsInProgress() || !res) {
155         return;
156     }
157     SetProgressVisible(true);
158     SetLogoVisible(true);
159     ShowProgress(0);
160     pgMgr_.ShowPage(it->second.progressPage.progressPageId);
161     ShowProgressWarning(false);
162 }
163 
ShowSuccessPage() const164 void UpdaterUiFacade::ShowSuccessPage() const
165 {
166     auto [res, it] = CheckMode();
167     if (!res) {
168         return;
169     }
170     LOG(DEBUG) << "show success page";
171     SetProgressVisible(false);
172     SetLogoVisible(false);
173     StopLongPressTimer();
174     pgMgr_.ShowPage(it->second.resPage.successPageId);
175 }
176 
ShowFailedPage() const177 void UpdaterUiFacade::ShowFailedPage() const
178 {
179     auto [res, it] = CheckMode();
180     if (!res) {
181         return;
182     }
183     LOG(DEBUG) << "show failed page";
184     SetProgressVisible(false);
185     SetLogoVisible(false);
186     StopLongPressTimer();
187     pgMgr_.ShowPage(it->second.resPage.failPageId);
188 }
189 
ShowFactoryConfirmPage()190 void UpdaterUiFacade::ShowFactoryConfirmPage()
191 {
192     auto [res, it] = CheckMode();
193     if (!res) {
194         return;
195     }
196     LOG(DEBUG) << "show confirm page";
197     ClearLog();
198     pgMgr_.ShowPage(it->second.confirmPageId);
199 }
200 
ShowMainpage() const201 void UpdaterUiFacade::ShowMainpage() const
202 {
203     pgMgr_.ShowMainPage();
204 }
205 
ClearText() const206 void UpdaterUiFacade::ClearText() const
207 {
208     auto [res, it] = CheckMode();
209     if (!res) {
210         return;
211     }
212     ClearLog();
213     ShowMsg(it->second.labelUpdId, "");
214 }
215 
ClearLog() const216 void UpdaterUiFacade::ClearLog() const
217 {
218     if (auto [res, it] = CheckMode(); res) {
219         ShowMsg(it->second.labelLogId, "");
220         ShowMsg(it->second.labelLogResId, "");
221     }
222 }
223 
ShowMsg(const ComInfo & id,const std::string & tag,bool isClear) const224 void UpdaterUiFacade::ShowMsg(const ComInfo &id, const std::string &tag, bool isClear) const
225 {
226     if (isClear) {
227         LOG(INFO) << "clear all log label's text";
228         ClearText();
229     }
230     pgMgr_[id.pageId][id.comId].As<TextLabelAdapter>()->SetText(tag);
231 }
232 
ShowMsg(const ComInfo & id,const std::string & tag) const233 void UpdaterUiFacade::ShowMsg(const ComInfo &id, const std::string &tag) const
234 {
235     pgMgr_[id.pageId][id.comId].As<TextLabelAdapter>()->SetText(tag);
236 }
237 
SetLogoProgress()238 void UpdaterUiFacade::SetLogoProgress()
239 {
240     auto [res, it] = CheckMode();
241     if (!res) {
242         return;
243     }
244     const ProgressPage &progressPage { it->second.progressPage };
245     if (progressMap_.find(mode_) == progressMap_.end()) {
246         progressMap_[mode_] = ProgressStrategy::Factory(progressPage.progressType, {
247             progressPage.progressPageId, progressPage.progressComId
248         });
249     }
250     if (logoMap_.find(mode_) == logoMap_.end()) {
251         logoMap_[mode_] = LogoStrategy::Factory(progressPage.logoType, {
252             progressPage.progressPageId, progressPage.logoComId
253         });
254     }
255 }
256 
Sleep(int ms) const257 void UpdaterUiFacade::Sleep(int ms) const
258 {
259     std::this_thread::sleep_for(std::chrono::milliseconds(ms));
260 }
261 
SaveScreen() const262 void UpdaterUiFacade::SaveScreen() const
263 {
264     UpdaterUiTools::SaveUxBuffToFile("/tmp/mainpage.png");
265 }
266 } // namespace Updater