• 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 "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 
27 namespace Updater {
28 namespace {
29 constexpr uint32_t DISPLAY_TIME = 1 * 1000 * 1000; /* 1s */
30 constexpr uint32_t FAIL_DELAY = 5 * 1000 * 1000;
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.sdcardUpdate = true;
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";
104     if (!GetFacade().SetMode(UpdaterMode::SDCARD)) {
105         return;
106     }
107     Utils::UsSleep(CALLBACK_DELAY);
108     UpdaterParams upParams;
109     upParams.sdcardUpdate = true;
110     if (auto res = UpdaterFromSdcard(upParams); res != UPDATE_SUCCESS) {
111         GetFacade().ShowLogRes(res == UPDATE_CORRUPT ? TR(LOGRES_VERIFY_FAILED) : TR(LOGRES_UPDATE_FAILED));
112         GetFacade().ShowFailedPage();
113         Utils::UsSleep(FAIL_DELAY);
114         GetFacade().ShowMainpage();
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_ASYN_CALLBACK(OnLabelOkEvt)130 DEFINE_ASYN_CALLBACK(OnLabelOkEvt)
131 {
132     LOG(INFO) << "On Label Ok";
133     Utils::UsSleep(CALLBACK_DELAY);
134     GetFacade().ShowMainpage();
135     GetFacade().ClearText();
136     GetFacade().ShowLog(TR(LOG_WIPE_DATA));
137     if (!GetFacade().SetMode(UpdaterMode::FACTORYRST)) {
138         return;
139     }
140     GetFacade().ShowProgress(0);
141     GetFacade().ShowProgressPage();
142     DoProgress();
143     if (FactoryReset(USER_WIPE_DATA, "/data") == 0) {
144         GetFacade().ShowLog(TR(LOG_WIPE_DONE));
145         GetFacade().ShowSuccessPage();
146     } else {
147         GetFacade().ShowLog(TR(LOG_WIPE_FAIL));
148         GetFacade().ShowFailedPage();
149     }
150 }
151 
DEFINE_ASYN_CALLBACK(OnConfirmRstEvt)152 DEFINE_ASYN_CALLBACK(OnConfirmRstEvt)
153 {
154     LOG(INFO) << "On Label Ok";
155     if (!GetFacade().SetMode(UpdaterMode::FACTORYRST)) {
156         return;
157     }
158     GetFacade().ShowUpdInfo(TR(LABEL_RESET_PROGRESS_INFO));
159     GetFacade().ShowProgressPage();
160     DoProgress();
161     if (FactoryReset(USER_WIPE_DATA, "/data") != 0) {
162         GetFacade().ShowLogRes(TR(LOG_WIPE_FAIL));
163         GetFacade().ShowFailedPage();
164         Utils::UsSleep(FAIL_DELAY);
165         GetFacade().ShowMainpage();
166     } else {
167         GetFacade().ShowUpdInfo(TR(LOGRES_WIPE_FINISH));
168         Utils::UsSleep(DISPLAY_TIME);
169         GetFacade().ShowSuccessPage();
170     }
171 }
172 
DEFINE_ASYN_CALLBACK(OnMenuShutdownEvt)173 DEFINE_ASYN_CALLBACK(OnMenuShutdownEvt)
174 {
175     LOG(DEBUG) << "shutdown";
176     Utils::DoShutdown();
177 }
178 
DEFINE_ASYN_CALLBACK(OnMenuClearCacheEvt)179 DEFINE_ASYN_CALLBACK(OnMenuClearCacheEvt)
180 {
181     LOG(INFO) << "On clear cache";
182     GetFacade().ClearText();
183     if (!GetFacade().SetMode(UpdaterMode::FACTORYRST)) {
184         return;
185     }
186     Utils::UsSleep(CALLBACK_DELAY);
187     GetFacade().ShowUpdInfo(TR(LOG_CLEAR_CAHCE));
188     GetFacade().ShowProgressPage();
189     ClearMisc();
190     DoProgress();
191     GetFacade().ShowMainpage();
192 }
193 } // namespace Updater
194