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_ = 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 updateCallback_->OnUpgradeProgress(updateStatus_, percent_, "");
42 LOG(INFO) << "reset progress when reset callback " << percent_ << " " << static_cast<int>(updateStatus_);
43 return 0;
44 }
45
GetUpdateStatus()46 int StatusManager::GetUpdateStatus()
47 {
48 return static_cast<int32_t>(updateStatus_);
49 }
50
UpdateCallback(UpdateStatus updateStatus,int percent,const std::string & resultMsg)51 void StatusManager::UpdateCallback(UpdateStatus updateStatus, int percent, const std::string &resultMsg)
52 {
53 std::lock_guard<std::mutex> lock(updateCbMutex_);
54 if (updateCallback_ == nullptr) {
55 LOG(ERROR) << "updateCallback_ null";
56 return;
57 }
58
59 if (updateStatus > UpdateStatus::UPDATE_STATE_MAX) {
60 LOG(INFO) << "status error:" << static_cast<int32_t>(updateStatus);
61 return;
62 }
63 if (updateStatus_ == updateStatus && percent == percent_) {
64 return;
65 }
66 if (updateStatus == UpdateStatus::UPDATE_STATE_SUCCESSFUL || updateStatus == UpdateStatus::UPDATE_STATE_FAILED) {
67 percent_ = 100; // 100 : max percent
68 } else if (percent >= 0 && percent <= 100 && percent > percent_) { // 100 : max percent
69 percent_ = percent;
70 }
71
72 updateStatus_ = updateStatus;
73 LOG(INFO) << "status:" << static_cast<int32_t>(updateStatus_) << " percent:" << percent_ << " msg:" << resultMsg;
74 updateCallback_->OnUpgradeProgress(updateStatus_, percent_, resultMsg);
75 }
76
CallbackWithoutCheck(UpdateStatus updateStatus,int percent,const std::string & resultMsg)77 void StatusManager::CallbackWithoutCheck(UpdateStatus updateStatus, int percent, const std::string &resultMsg)
78 {
79 std::lock_guard<std::mutex> lock(updateCbMutex_);
80 if (updateCallback_ == nullptr) {
81 LOG(ERROR) << "updateCallback_ null";
82 return;
83 }
84 if (updateStatus_ == updateStatus && percent == percent_ && resultMsg_ == resultMsg) {
85 return;
86 }
87 if (updateStatus > UpdateStatus::UPDATE_STATE_MAX) {
88 LOG(INFO) << "status error:" << static_cast<int32_t>(updateStatus);
89 return;
90 }
91 resultMsg_ = resultMsg;
92 updateStatus_ = updateStatus;
93 LOG(INFO) << "status:" << static_cast<int32_t>(updateStatus_) << " percent:" << percent_ << " msg:" << resultMsg;
94 updateCallback_->OnUpgradeProgress(updateStatus_, percent_, resultMsg);
95 }
96
SetUpdatePercent(int percent)97 void StatusManager::SetUpdatePercent(int percent)
98 {
99 UpdateCallback(updateStatus_, percent, "");
100 }
101
GetUpdateProgress()102 float StatusManager::GetUpdateProgress()
103 {
104 return percent_ / 100.0; // 100.0 : max percent
105 }
106
UpdateFeatureStatus(const FeatureStatus & statusInfo)107 void StatusManager::UpdateFeatureStatus(const FeatureStatus &statusInfo)
108 {
109 std::lock_guard<std::mutex> lock(updateCbMutex_);
110 if (updateCallback_ == nullptr) {
111 LOG(ERROR) << "updateCallback_ null";
112 return;
113 }
114
115 updateCallback_->OnUpgradeFeatureStatus(statusInfo);
116 }
117 } // namespace SysInstaller
118 } // namespace OHOS
119