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 "ability_manager_client.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(std::string value)56 void ActionPopup::AddActionValue(std::string value)
57 {
58     if (value.empty()) {
59         return;
60     }
61     valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
62 }
63 
Execute()64 void ActionPopup::Execute()
65 {
66     auto tms = ThermalService::GetInstance();
67     THERMAL_RETURN_IF (tms == nullptr);
68     uint32_t value = GetActionValue();
69     if (value != lastValue_) {
70         HandlePopupEvent(value);
71         WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
72         tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
73         lastValue_ = value;
74         THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
75     }
76     valueList_.clear();
77 }
78 
GetActionValue()79 uint32_t ActionPopup::GetActionValue()
80 {
81     uint32_t value = FALLBACK_VALUE_UINT_ZERO;
82     if (!valueList_.empty()) {
83         if (isStrict_) {
84             value = *min_element(valueList_.begin(), valueList_.end());
85         } else {
86             value = *max_element(valueList_.begin(), valueList_.end());
87         }
88     }
89     return value;
90 }
91 
HandlePopupEvent(const int32_t value)92 void ActionPopup::HandlePopupEvent(const int32_t value)
93 {
94     switch (value) {
95         case LOWER_TEMP:
96             ShowThermalDialog(ActionPopup::TempStatus::LOWER_TEMP);
97             g_powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
98             break;
99         case HIGHER_TEMP:
100             ShowThermalDialog(ActionPopup::TempStatus::HIGHER_TEMP);
101             g_powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
102             break;
103         default:
104             break;
105     }
106 }
107 
ShowThermalDialog(TempStatus value)108 bool ActionPopup::ShowThermalDialog(TempStatus value)
109 {
110     auto client = AbilityManagerClient::GetInstance();
111     if (client == nullptr) {
112         return false;
113     }
114     AAFwk::Want want;
115     if (value == TempStatus::LOWER_TEMP) {
116         want.SetElementName("com.ohos.powerdialog", "ThermalServiceExtAbility_low");
117     } else {
118         want.SetElementName("com.ohos.powerdialog", "ThermalServiceExtAbility_high");
119     }
120     int32_t result = client->StartAbility(want);
121     if (result != 0) {
122         THERMAL_HILOGE(COMP_SVC, "ShowThermalDialog failed, result = %{public}d", result);
123         return false;
124     }
125     THERMAL_HILOGD(COMP_SVC, "ShowThermalDialog success");
126     return true;
127 }
128 } // namespace PowerMgr
129 } // namespace OHOS
130