• 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 "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 {
44     (void)params;
45 }
46 
SetStrict(bool enable)47 void ActionPopup::SetStrict(bool enable)
48 {
49     isStrict_ = enable;
50 }
51 
SetEnableEvent(bool enable)52 void ActionPopup::SetEnableEvent(bool enable)
53 {
54     enableEvent_ = enable;
55 }
56 
AddActionValue(std::string value)57 void ActionPopup::AddActionValue(std::string value)
58 {
59     if (value.empty()) {
60         return;
61     }
62     valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
63 }
64 
Execute()65 void ActionPopup::Execute()
66 {
67     THERMAL_RETURN_IF (g_service == nullptr);
68     uint32_t value = GetActionValue();
69     if (value != lastValue_) {
70         HandlePopupEvent(value);
71         WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
72         g_service->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     std::string scene = g_service->GetScene();
82     auto iter = g_sceneMap.find(scene);
83     if (iter != g_sceneMap.end()) {
84         return static_cast<uint32_t>(strtol(iter->second.c_str(), nullptr, STRTOL_FORMART_DEC));
85     }
86     uint32_t value = FALLBACK_VALUE_UINT_ZERO;
87     if (!valueList_.empty()) {
88         if (isStrict_) {
89             value = *min_element(valueList_.begin(), valueList_.end());
90         } else {
91             value = *max_element(valueList_.begin(), valueList_.end());
92         }
93     }
94     return value;
95 }
96 
HandlePopupEvent(const int32_t value)97 void ActionPopup::HandlePopupEvent(const int32_t value)
98 {
99     switch (value) {
100         case LOWER_TEMP:
101             ShowThermalDialog(ActionPopup::TempStatus::LOWER_TEMP);
102             g_powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
103             break;
104         case HIGHER_TEMP:
105             ShowThermalDialog(ActionPopup::TempStatus::HIGHER_TEMP);
106             g_powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
107             break;
108         default:
109             break;
110     }
111 }
112 
ShowThermalDialog(TempStatus value)113 bool ActionPopup::ShowThermalDialog(TempStatus value)
114 {
115     auto client = AbilityManagerClient::GetInstance();
116     if (client == nullptr) {
117         return false;
118     }
119     AAFwk::Want want;
120     if (value == TempStatus::LOWER_TEMP) {
121         want.SetElementName("com.ohos.powerdialog", "ThermalServiceExtAbility_low");
122     } else {
123         want.SetElementName("com.ohos.powerdialog", "ThermalServiceExtAbility_high");
124     }
125     int32_t result = client->StartAbility(want);
126     if (result != 0) {
127         THERMAL_HILOGE(COMP_SVC, "ShowThermalDialog failed, result = %{public}d", result);
128         return false;
129     }
130     THERMAL_HILOGD(COMP_SVC, "ShowThermalDialog success");
131     return true;
132 }
133 } // namespace PowerMgr
134 } // namespace OHOS
135