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