• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "action_popup.h"
17 
18 #include <map>
19 #include <ipc_skeleton.h>
20 #include <dlfcn.h>
21 #include "constants.h"
22 #include "thermal_common.h"
23 #include "thermal_hisysevent.h"
24 #include "thermal_service.h"
25 #include "power_mgr_client.h"
26 
27 using namespace OHOS::AppExecFwk;
28 using namespace OHOS::AAFwk;
29 
30 namespace OHOS {
31 namespace PowerMgr {
32 namespace {
33 static PowerMgrClient& g_powerMgrClient = PowerMgrClient::GetInstance();
34 }
35 
ActionPopup(const std::string & actionName)36 ActionPopup::ActionPopup(const std::string& actionName)
37 {
38     actionName_ = actionName;
39 }
40 
InitParams(const std::string & params)41 void ActionPopup::InitParams(const std::string& params)
42 {
43     (void)params;
44 }
45 
SetStrict(bool enable)46 void ActionPopup::SetStrict(bool enable)
47 {
48     isStrict_ = enable;
49 }
50 
SetEnableEvent(bool enable)51 void ActionPopup::SetEnableEvent(bool enable)
52 {
53     enableEvent_ = enable;
54 }
55 
AddActionValue(uint32_t actionId,std::string value)56 void ActionPopup::AddActionValue(uint32_t actionId, std::string value)
57 {
58     if (value.empty()) {
59         return;
60     }
61     if (actionId > 0) {
62         auto iter = policyActionMap_.find(actionId);
63         if (iter != policyActionMap_.end()) {
64             iter->second.uintDelayValue = static_cast<uint32_t>(static_cast<uint32_t>(strtol(value.c_str(),
65                 nullptr, STRTOL_FORMART_DEC)));
66         }
67     } else {
68         valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
69     }
70 }
71 
ExecuteInner()72 void ActionPopup::ExecuteInner()
73 {
74     auto tms = ThermalService::GetInstance();
75     THERMAL_RETURN_IF (tms == nullptr);
76     for (auto &policyAction : policyActionMap_) {
77         if (policyAction.second.isCompleted) {
78             valueList_.push_back(policyAction.second.uintDelayValue);
79         }
80     }
81 
82     uint32_t value = GetActionValue();
83     if (value != lastValue_) {
84         HandlePopupEvent(value);
85         WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
86         tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
87         lastValue_ = value;
88         THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
89     }
90     valueList_.clear();
91 }
92 
ResetActionValue()93 void ActionPopup::ResetActionValue()
94 {
95     lastValue_ = 0;
96 }
97 
GetActionValue()98 uint32_t ActionPopup::GetActionValue()
99 {
100     uint32_t value = FALLBACK_VALUE_UINT_ZERO;
101     if (!valueList_.empty()) {
102         if (isStrict_) {
103             value = *min_element(valueList_.begin(), valueList_.end());
104         } else {
105             value = *max_element(valueList_.begin(), valueList_.end());
106         }
107     }
108     return value;
109 }
110 
HandlePopupEvent(const int32_t value)111 void ActionPopup::HandlePopupEvent(const int32_t value)
112 {
113     switch (value) {
114         case LOWER_TEMP:
115             ShowThermalDialog(ActionPopup::TempStatus::LOWER_TEMP);
116             g_powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
117             break;
118         case HIGHER_TEMP:
119             ShowThermalDialog(ActionPopup::TempStatus::HIGHER_TEMP);
120             g_powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
121             break;
122         default:
123             break;
124     }
125 }
126 
ShowThermalDialog(TempStatus value)127 bool ActionPopup::ShowThermalDialog(TempStatus value)
128 {
129     void *handler = dlopen("libpower_ability.z.so", RTLD_NOW | RTLD_NODELETE);
130     if (handler == nullptr) {
131         THERMAL_HILOGE(COMP_SVC, "dlopen libpower_ability.z.so failed, reason : %{public}s", dlerror());
132         return false;
133     }
134     auto PowerStartAbility = reinterpret_cast<void (*)(const Want&)>(dlsym(handler, "PowerStartAbility"));
135     if (PowerStartAbility == nullptr) {
136         THERMAL_HILOGE(COMP_SVC, "find PowerStartAbility function failed, reason : %{public}s", dlerror());
137 #ifndef FUZZ_TEST
138         dlclose(handler);
139 #endif
140         handler = nullptr;
141         return false;
142     }
143     AAFwk::Want want;
144     if (value == TempStatus::LOWER_TEMP) {
145         want.SetElementName("com.ohos.powerdialog", "ThermalServiceExtAbility_low");
146     } else {
147         want.SetElementName("com.ohos.powerdialog", "ThermalServiceExtAbility_high");
148     }
149     PowerStartAbility(want);
150 #ifndef FUZZ_TEST
151     dlclose(handler);
152 #endif
153     handler = nullptr;
154     THERMAL_HILOGD(COMP_SVC, "ShowThermalDialog success");
155     return true;
156 }
157 } // namespace PowerMgr
158 } // namespace OHOS
159