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