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
16 #include "status_manager.h"
17
18 #include "sys_installer_common.h"
19 #include "log/log.h"
20 #include "utils.h"
21
22 namespace OHOS {
23 namespace SysInstaller {
24 using namespace Updater;
25
Init()26 void StatusManager::Init()
27 {
28 updateStatus_ = UPDATE_STATE_INIT;
29 percent_ = 0;
30 }
31
SetUpdateCallback(const sptr<ISysInstallerCallback> & updateCallback)32 int StatusManager::SetUpdateCallback(const sptr<ISysInstallerCallback> &updateCallback)
33 {
34 std::lock_guard<std::mutex> lock(updateCbMutex_);
35 if (updateCallback == nullptr) {
36 LOG(ERROR) << "para error";
37 return -1;
38 }
39
40 updateCallback_ = updateCallback;
41 return 0;
42 }
43
GetUpdateStatus()44 int StatusManager::GetUpdateStatus()
45 {
46 return updateStatus_;
47 }
48
UpdateCallback(UpdateStatus updateStatus,int percent,const std::string & resultMsg)49 void StatusManager::UpdateCallback(UpdateStatus updateStatus, int percent, const std::string &resultMsg)
50 {
51 std::lock_guard<std::mutex> lock(updateCbMutex_);
52 if (updateCallback_ == nullptr) {
53 LOG(ERROR) << "updateCallback_ null";
54 return;
55 }
56
57 if (updateStatus > UPDATE_STATE_MAX) {
58 LOG(INFO) << "status error:" << updateStatus;
59 return;
60 }
61 if (updateStatus == UPDATE_STATE_SUCCESSFUL || updateStatus == UPDATE_STATE_FAILED) {
62 percent_ = 100; // 100 : max percent
63 } else if (percent >= 0 && percent <= 100 && percent > percent_) { // 100 : max percent
64 percent_ = percent;
65 }
66
67 updateStatus_ = updateStatus;
68 LOG(INFO) << "status:" << updateStatus_ << " percent:" << percent_ << " msg:" << resultMsg;
69 updateCallback_->OnUpgradeProgress(updateStatus_, percent_, resultMsg);
70 }
71
SetUpdatePercent(int percent)72 void StatusManager::SetUpdatePercent(int percent)
73 {
74 UpdateCallback(updateStatus_, percent, "");
75 }
76
GetUpdateProgress()77 float StatusManager::GetUpdateProgress()
78 {
79 return percent_ / 100.0; // 100.0 : max percent
80 }
81 } // namespace SysInstaller
82 } // namespace OHOS
83