• 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 #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         NotifyAutoReboot(upParams.updateMode);
122         return;
123     }
124     GetFacade().ShowLogRes(TR(LABEL_UPD_OK_DONE));
125     GetFacade().ShowSuccessPage();
126     Utils::UsSleep(SUCCESS_DELAY);
127     PostUpdater(true);
128     NotifyReboot("", "Updater sdcard update success reboot");
129 }
130 
DEFINE_ASYN_CALLBACK(OnLabelSDUpdateResEvt)131 DEFINE_ASYN_CALLBACK(OnLabelSDUpdateResEvt)
132 {
133     LOG(INFO) << "On Label SDCard To Reserve Userdata";
134     if (!GetFacade().SetMode(UPDATERMODE_SDCARD)) {
135         return;
136     }
137     Utils::UsSleep(CALLBACK_DELAY);
138     UpdaterParams upParams;
139     upParams.updateMode = SDCARD_UPDATE;
140     UPDATER_UI_INSTANCE.ShowProgressPage();
141     Utils::SetMessageToMisc("boot-updater", 0, "sdcard_intral_update"); // set retain userdata
142     if (!Utils::CheckUpdateMode(Updater::SDCARD_INTRAL_MODE)) {
143         LOG(ERROR) << "sdcard_intral_update write to misc failed";
144         GetFacade().ShowFailedPage();
145         return;
146     }
147     LOG(INFO) << "sdcard_intral_update write to misc success";
148     if (auto res = UpdaterFromSdcard(upParams); res != UPDATE_SUCCESS) {
149         Utils::RemoveUpdateInfoFromMisc("sdcard_update");
150         GetFacade().ShowLogRes(res == UPDATE_CORRUPT ? TR(LOGRES_VERIFY_FAILED) : TR(LOGRES_UPDATE_FAILED));
151         GetFacade().ShowFailedPage();
152         return;
153     }
154     GetFacade().ShowLogRes(TR(LABEL_UPD_OK_DONE));
155     GetFacade().ShowSuccessPage();
156     Utils::UsSleep(SUCCESS_DELAY);
157     PostUpdater(true);
158     NotifyReboot("", "Updater sdcard update success reboot");
159 }
160 
DEFINE_SYNC_CALLBACK(OnLabelCancelEvt)161 DEFINE_SYNC_CALLBACK(OnLabelCancelEvt)
162 {
163     LOG(INFO) << "On Label Cancel";
164     PageManager::GetInstance().GoBack();
165 }
166 
DEFINE_SYNC_CALLBACK(OnReturnToMainEvt)167 DEFINE_SYNC_CALLBACK(OnReturnToMainEvt)
168 {
169     LOG(INFO) << "On Return To Main";
170     PageManager::GetInstance().ShowMainPage();
171 }
172 
DEFINE_ASYN_CALLBACK(OnLabelOkEvt)173 DEFINE_ASYN_CALLBACK(OnLabelOkEvt)
174 {
175     LOG(INFO) << "On Label Ok";
176     Utils::UsSleep(CALLBACK_DELAY);
177     GetFacade().ShowMainpage();
178     GetFacade().ClearText();
179     GetFacade().ShowLog(TR(LOG_WIPE_DATA));
180     if (!GetFacade().SetMode(UPDATERMODE_FACTORYRST)) {
181         return;
182     }
183     GetFacade().ShowProgress(0);
184     GetFacade().ShowProgressPage();
185     DoProgress();
186     if (FactoryReset(USER_WIPE_DATA, "/data") == 0) {
187         GetFacade().ShowLog(TR(LOG_WIPE_DONE));
188         GetFacade().ShowSuccessPage();
189     } else {
190         GetFacade().ShowLog(TR(LOG_WIPE_FAIL));
191         GetFacade().ShowFailedPage();
192     }
193 }
194 
DEFINE_ASYN_CALLBACK(OnConfirmRstEvt)195 DEFINE_ASYN_CALLBACK(OnConfirmRstEvt)
196 {
197     LOG(INFO) << "On Label Ok";
198     if (!GetFacade().SetMode(UPDATERMODE_FACTORYRST)) {
199         return;
200     }
201     Utils::AddUpdateInfoToMisc("user_wipe_data", std::nullopt);
202     GetFacade().ShowUpdInfo(TR(LABEL_RESET_PROGRESS_INFO));
203     GetFacade().ShowProgressPage();
204     DoProgress();
205     if (FactoryReset(USER_WIPE_DATA, "/data") != 0) {
206         Utils::RemoveUpdateInfoFromMisc("user_wipe_data");
207         GetFacade().ShowLogRes(TR(LOG_WIPE_FAIL));
208         GetFacade().ShowFailedPage();
209     } else {
210         GetFacade().ShowSuccessPage();
211         PostUpdater(true);
212         Utils::UsSleep(SUCCESS_DELAY);
213         NotifyReboot("", "Updater factory reset success");
214     }
215 }
216 
DEFINE_ASYN_CALLBACK(OnMenuShutdownEvt)217 DEFINE_ASYN_CALLBACK(OnMenuShutdownEvt)
218 {
219     LOG(DEBUG) << "shutdown";
220     GraphicEngine::GetInstance().StopEngine();
221     Utils::DoShutdown("Updater shutdown btn event");
222 }
223 
DEFINE_ASYN_CALLBACK(OnMenuClearCacheEvt)224 DEFINE_ASYN_CALLBACK(OnMenuClearCacheEvt)
225 {
226     LOG(INFO) << "On clear cache";
227     GetFacade().ClearText();
228     if (!GetFacade().SetMode(UPDATERMODE_FACTORYRST)) {
229         return;
230     }
231     Utils::UsSleep(CALLBACK_DELAY);
232     GetFacade().ShowUpdInfo(TR(LOG_CLEAR_CAHCE));
233     GetFacade().ShowProgressPage();
234     ClearMisc();
235     DoProgress();
236     GetFacade().ShowMainpage();
237 }
238 } // namespace Updater
239