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