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 #include "updater_ui.h"
16 #include <mutex>
17 #include <thread>
18 #include "control/callback_manager.h"
19 #include "language/language_ui.h"
20 #include "driver/graphic_engine.h"
21 #include "log/log.h"
22 #include "page/page_manager.h"
23 #include "scope_guard.h"
24 #include "updater_main.h"
25 #include "updater_ui_facade.h"
26 #include "updater/updater_const.h"
27 #include "utils.h"
28 #include "updater_ui_stub.h"
29
30 namespace Updater {
31 namespace {
32 constexpr uint32_t DISPLAY_TIME = 1 * 1000 * 1000; /* 1s */
33 constexpr uint32_t SUCCESS_DELAY = 3 * 1000 * 1000;
34 constexpr int CALLBACK_DELAY = 20 * 1000; /* 20ms */
35
GetFacade()36 inline auto &GetFacade()
37 {
38 return UpdaterUiFacade::GetInstance();
39 }
40 } // namespace
41
DoProgress()42 void DoProgress()
43 {
44 constexpr int maxSleepMs = 1000 * 1000;
45 constexpr int minSleepMs = 3000;
46 constexpr float ratio = 10.0;
47 // if 100 as fullpercent, then 0.3 per step
48 constexpr int progressValueStep = static_cast<int>(0.3 * ratio);
49 constexpr int maxProgressValue = static_cast<int>(100 * ratio);
50 int progressvalueTmp = 0;
51 if (GetFacade().GetMode() != UPDATERMODE_FACTORYRST && GetFacade().GetMode() != UPDATERMODE_REBOOTFACTORYRST) {
52 return;
53 }
54 GetFacade().ShowProgress(0);
55 while (progressvalueTmp <= maxProgressValue) {
56 progressvalueTmp = progressvalueTmp + progressValueStep;
57 GetFacade().ShowProgress(progressvalueTmp / ratio);
58 Utils::UsSleep(minSleepMs);
59 if (progressvalueTmp >= maxProgressValue) {
60 Utils::UsSleep(maxSleepMs);
61 return;
62 }
63 }
64 }
65
DEFINE_ASYN_CALLBACK(OnRebootEvt)66 DEFINE_ASYN_CALLBACK(OnRebootEvt)
67 {
68 LOG(INFO) << "On Label Reboot";
69 GraphicEngine::GetInstance().StopEngine();
70 PostUpdater(false);
71 Utils::UpdaterDoReboot("", "Updater reboot btn event");
72 }
73
DEFINE_SYNC_CALLBACK(OnLabelResetEvt)74 DEFINE_SYNC_CALLBACK(OnLabelResetEvt)
75 {
76 LOG(INFO) << "On Label Reset";
77 if (!GetFacade().SetMode(UPDATERMODE_FACTORYRST)) {
78 return;
79 }
80 GetFacade().ShowFactoryConfirmPage();
81 }
82
DEFINE_ASYN_CALLBACK(OnLabelSDCardEvt)83 DEFINE_ASYN_CALLBACK(OnLabelSDCardEvt)
84 {
85 LOG(INFO) << "On Label SDCard";
86 if (!GetFacade().SetMode(UPDATERMODE_SDCARD)) {
87 return;
88 }
89 Utils::UsSleep(CALLBACK_DELAY);
90 GetFacade().ClearText();
91 GetFacade().ShowProgress(0);
92 GetFacade().ShowLog(TR(LOG_SDCARD_NOTMOVE));
93 Utils::UsSleep(DISPLAY_TIME);
94 UpdaterParams upParams;
95 upParams.updateMode = SDCARD_UPDATE;
96 if (UpdaterFromSdcard(upParams) != UPDATE_SUCCESS) {
97 GetFacade().ShowMainpage();
98 return;
99 }
100 PostUpdater(true);
101 Utils::UpdaterDoReboot("", "Updater sdcard update success reboot");
102 }
103
DEFINE_ASYN_CALLBACK(OnLabelSDCardNoDelayEvt)104 DEFINE_ASYN_CALLBACK(OnLabelSDCardNoDelayEvt)
105 {
106 LOG(INFO) << "On Label SDCard No Delay";
107 if (!GetFacade().SetMode(UPDATERMODE_SDCARD)) {
108 return;
109 }
110 Utils::UsSleep(CALLBACK_DELAY);
111 UpdaterParams upParams;
112 upParams.updateMode = SDCARD_UPDATE;
113 UPDATER_UI_INSTANCE.ShowProgressPage();
114 if (auto res = UpdaterFromSdcard(upParams); res != UPDATE_SUCCESS) {
115 Utils::RemoveUpdateInfoFromMisc("sdcard_update");
116 GetFacade().ShowLogRes(res == UPDATE_CORRUPT ? TR(LOGRES_VERIFY_FAILED) : TR(LOGRES_UPDATE_FAILED));
117 GetFacade().ShowFailedPage();
118 return;
119 }
120 GetFacade().ShowLogRes(TR(LABEL_UPD_OK_DONE));
121 GetFacade().ShowSuccessPage();
122 Utils::UsSleep(SUCCESS_DELAY);
123 PostUpdater(true);
124 Utils::UpdaterDoReboot("", "Updater sdcard update success reboot");
125 }
126
DEFINE_ASYN_CALLBACK(OnLabelSDUpdateResEvt)127 DEFINE_ASYN_CALLBACK(OnLabelSDUpdateResEvt)
128 {
129 LOG(INFO) << "On Label SDCard To Reserve Userdata";
130 if (!GetFacade().SetMode(UPDATERMODE_SDCARD)) {
131 return;
132 }
133 Utils::UsSleep(CALLBACK_DELAY);
134 UpdaterParams upParams;
135 upParams.updateMode = SDCARD_UPDATE;
136 UPDATER_UI_INSTANCE.ShowProgressPage();
137 Utils::SetMessageToMisc("boot-updater", 0, "sdcard_intral_update"); // set retain userdata
138 if (!Utils::CheckUpdateMode(Updater::SDCARD_INTRAL_MODE)) {
139 LOG(ERROR) << "sdcard_intral_update write to misc failed";
140 GetFacade().ShowFailedPage();
141 return;
142 }
143 LOG(INFO) << "sdcard_intral_update write to misc success";
144 if (auto res = UpdaterFromSdcard(upParams); res != UPDATE_SUCCESS) {
145 Utils::RemoveUpdateInfoFromMisc("sdcard_update");
146 GetFacade().ShowLogRes(res == UPDATE_CORRUPT ? TR(LOGRES_VERIFY_FAILED) : TR(LOGRES_UPDATE_FAILED));
147 GetFacade().ShowFailedPage();
148 return;
149 }
150 GetFacade().ShowLogRes(TR(LABEL_UPD_OK_DONE));
151 GetFacade().ShowSuccessPage();
152 Utils::UsSleep(SUCCESS_DELAY);
153 PostUpdater(true);
154 Utils::UpdaterDoReboot("", "Updater sdcard update success reboot");
155 }
156
DEFINE_SYNC_CALLBACK(OnLabelCancelEvt)157 DEFINE_SYNC_CALLBACK(OnLabelCancelEvt)
158 {
159 LOG(INFO) << "On Label Cancel";
160 PageManager::GetInstance().GoBack();
161 }
162
DEFINE_SYNC_CALLBACK(OnReturnToMainEvt)163 DEFINE_SYNC_CALLBACK(OnReturnToMainEvt)
164 {
165 LOG(INFO) << "On Return To Main";
166 PageManager::GetInstance().ShowMainPage();
167 }
168
DEFINE_ASYN_CALLBACK(OnLabelOkEvt)169 DEFINE_ASYN_CALLBACK(OnLabelOkEvt)
170 {
171 LOG(INFO) << "On Label Ok";
172 Utils::UsSleep(CALLBACK_DELAY);
173 GetFacade().ShowMainpage();
174 GetFacade().ClearText();
175 GetFacade().ShowLog(TR(LOG_WIPE_DATA));
176 if (!GetFacade().SetMode(UPDATERMODE_FACTORYRST)) {
177 return;
178 }
179 GetFacade().ShowProgress(0);
180 GetFacade().ShowProgressPage();
181 DoProgress();
182 if (FactoryReset(USER_WIPE_DATA, "/data") == 0) {
183 GetFacade().ShowLog(TR(LOG_WIPE_DONE));
184 GetFacade().ShowSuccessPage();
185 } else {
186 GetFacade().ShowLog(TR(LOG_WIPE_FAIL));
187 GetFacade().ShowFailedPage();
188 }
189 }
190
DEFINE_ASYN_CALLBACK(OnConfirmRstEvt)191 DEFINE_ASYN_CALLBACK(OnConfirmRstEvt)
192 {
193 LOG(INFO) << "On Label Ok";
194 if (!GetFacade().SetMode(UPDATERMODE_FACTORYRST)) {
195 return;
196 }
197 Utils::AddUpdateInfoToMisc("user_wipe_data", std::nullopt);
198 GetFacade().ShowUpdInfo(TR(LABEL_RESET_PROGRESS_INFO));
199 GetFacade().ShowProgressPage();
200 DoProgress();
201 if (FactoryReset(USER_WIPE_DATA, "/data") != 0) {
202 Utils::RemoveUpdateInfoFromMisc("user_wipe_data");
203 GetFacade().ShowLogRes(TR(LOG_WIPE_FAIL));
204 GetFacade().ShowFailedPage();
205 } else {
206 GetFacade().ShowSuccessPage();
207 PostUpdater(true);
208 Utils::UsSleep(SUCCESS_DELAY);
209 Utils::UpdaterDoReboot("", "Updater factory reset success");
210 }
211 }
212
DEFINE_ASYN_CALLBACK(OnMenuShutdownEvt)213 DEFINE_ASYN_CALLBACK(OnMenuShutdownEvt)
214 {
215 LOG(DEBUG) << "shutdown";
216 GraphicEngine::GetInstance().StopEngine();
217 Utils::DoShutdown("Updater shutdown btn event");
218 }
219
DEFINE_ASYN_CALLBACK(OnMenuClearCacheEvt)220 DEFINE_ASYN_CALLBACK(OnMenuClearCacheEvt)
221 {
222 LOG(INFO) << "On clear cache";
223 GetFacade().ClearText();
224 if (!GetFacade().SetMode(UPDATERMODE_FACTORYRST)) {
225 return;
226 }
227 Utils::UsSleep(CALLBACK_DELAY);
228 GetFacade().ShowUpdInfo(TR(LOG_CLEAR_CAHCE));
229 GetFacade().ShowProgressPage();
230 ClearMisc();
231 DoProgress();
232 GetFacade().ShowMainpage();
233 }
234 } // namespace Updater
235