1 /*
2 * Copyright (c) 2021-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 "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 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
34 static PowerMgrClient& g_powerMgrClient = PowerMgrClient::GetInstance();
35 }
36
ActionPopup(const std::string & actionName)37 ActionPopup::ActionPopup(const std::string& actionName)
38 {
39 actionName_ = actionName;
40 }
41
InitParams(const std::string & params)42 void ActionPopup::InitParams(const std::string& params) {}
43
SetStrict(bool flag)44 void ActionPopup::SetStrict(bool flag)
45 {
46 flag_ = flag;
47 }
48
SetEnableEvent(bool enable)49 void ActionPopup::SetEnableEvent(bool enable)
50 {
51 enableEvent_ = enable;
52 }
53
AddActionValue(std::string value)54 void ActionPopup::AddActionValue(std::string value)
55 {
56 THERMAL_HILOGD(COMP_SVC, "value=%{public}s", value.c_str());
57 if (value.empty()) {
58 return;
59 }
60 valueList_.push_back(atoi(value.c_str()));
61 }
62
Execute()63 void ActionPopup::Execute()
64 {
65 THERMAL_HILOGD(COMP_SVC, "Enter");
66 uint32_t value;
67 THERMAL_RETURN_IF(g_service == nullptr);
68 std::string scene = g_service->GetScene();
69 auto iter = g_sceneMap.find(scene);
70 if (iter != g_sceneMap.end()) {
71 value = static_cast<uint32_t>(atoi(iter->second.c_str()));
72 if (value != lastValue_) {
73 HandlePopupEvent(value);
74 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
75 g_service->GetObserver()->SetDecisionValue(actionName_, iter->second);
76 lastValue_ = value;
77 valueList_.clear();
78 }
79 return;
80 }
81
82 if (valueList_.empty()) {
83 value = 0;
84 } else {
85 if (flag_) {
86 value = *max_element(valueList_.begin(), valueList_.end());
87 } else {
88 value = *min_element(valueList_.begin(), valueList_.end());
89 }
90 valueList_.clear();
91 }
92
93 if (value != lastValue_) {
94 HandlePopupEvent(value);
95 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
96 g_service->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
97 lastValue_ = value;
98 }
99 }
100
HandlePopupEvent(const int32_t value)101 void ActionPopup::HandlePopupEvent(const int32_t value)
102 {
103 switch (value) {
104 case LOWER_TEMP:
105 ShowThermalDialog(ActionPopup::TempStatus::LOWER_TEMP);
106 g_powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
107 break;
108 case HIGHER_TEMP:
109 ShowThermalDialog(ActionPopup::TempStatus::HIGHER_TEMP);
110 g_powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
111 break;
112 default:
113 break;
114 }
115 }
116
ShowThermalDialog(TempStatus value)117 bool ActionPopup::ShowThermalDialog(TempStatus value)
118 {
119 THERMAL_HILOGD(COMP_SVC, "ShowThermalDialog start.");
120 auto client = AbilityManagerClient::GetInstance();
121 if (client == nullptr) {
122 return false;
123 }
124 AAFwk::Want want;
125 if (value == TempStatus::LOWER_TEMP) {
126 want.SetElementName("com.ohos.powerdialog", "ThermalServiceExtAbility_low");
127 } else {
128 want.SetElementName("com.ohos.powerdialog", "ThermalServiceExtAbility_high");
129 }
130 int32_t result = client->StartAbility(want);
131 if (result != 0) {
132 THERMAL_HILOGE(COMP_SVC, "ShowThermalDialog failed, result = %{public}d", result);
133 return false;
134 }
135 THERMAL_HILOGD(COMP_SVC, "ShowThermalDialog success.");
136 return true;
137 }
138 } // namespace PowerMgr
139 } // namespace OHOS
140