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 "power_mode_policy.h"
17
18 #include "power_log.h"
19 #include "power_save_mode.h"
20 #include "singleton.h"
21 #include "setting_helper.h"
22 #include "json/json.h"
23 using namespace std;
24
25 namespace OHOS {
26 namespace PowerMgr {
GetPowerModeValuePolicy(uint32_t type)27 int32_t PowerModePolicy::GetPowerModeValuePolicy(uint32_t type)
28 {
29 int32_t ret = INIT_VALUE_FALSE;
30 if (IsValidType(type)) {
31 ret = GetPolicyFromMap(type);
32 }
33
34 return ret;
35 }
36
GetPolicyFromMap(uint32_t type)37 int32_t PowerModePolicy::GetPolicyFromMap(uint32_t type)
38 {
39 int32_t ret = INIT_VALUE_FALSE;
40 std::lock_guard<std::mutex> lock(policyMutex_);
41 auto iter = switchMap_.find(type);
42 if (iter != switchMap_.end()) {
43 ret = iter->second;
44 }
45 return ret;
46 }
47
UpdatePowerModePolicy(uint32_t mode)48 void PowerModePolicy::UpdatePowerModePolicy(uint32_t mode)
49 {
50 POWER_HILOGD(FEATURE_POWER_MODE, "update mode policy, mode=%{public}d", mode);
51 ReadPowerModePolicy(mode);
52 ComparePowerModePolicy();
53 }
54
ComparePowerModePolicy()55 void PowerModePolicy::ComparePowerModePolicy()
56 {
57 std::lock_guard<std::mutex> lock(policyMutex_);
58 for (auto [id, value] : recoverMap_) {
59 if (switchMap_.find(id) != switchMap_.end()) {
60 backupMap_[id] = value;
61 }
62 switchMap_.emplace(id, value);
63 }
64 recoverMap_ = backupMap_;
65 SavePowerModeRecoverMap();
66 }
67
InitRecoverMap()68 bool PowerModePolicy::InitRecoverMap()
69 {
70 std::string jsonStr = SettingHelper::ReadPowerModeRecoverMap();
71 Json::Value recoverJson;
72 Json::Reader reader;
73 if (!reader.parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), recoverJson)) {
74 POWER_HILOGW(FEATURE_POWER_MODE, "parse recover json str error");
75 return false;
76 }
77 for (const auto &member : recoverJson.getMemberNames()) {
78 int32_t key = std::stoi(member);
79 if (!recoverJson[member].isInt()) {
80 continue;
81 }
82 int32_t value = recoverJson[member].asInt();
83 recoverMap_[key] = value;
84 }
85 POWER_HILOGI(FEATURE_POWER_MODE, "init recover map succeed");
86 return true;
87 }
88
ReadPowerModePolicy(uint32_t mode)89 void PowerModePolicy::ReadPowerModePolicy(uint32_t mode)
90 {
91 auto policyCache = DelayedSpSingleton<PowerSaveMode>::GetInstance()->GetPolicyCache();
92 if (policyCache.empty()) {
93 POWER_HILOGD(FEATURE_POWER_MODE, "config policy cache is empty");
94 return;
95 }
96
97 switchMap_.clear();
98 backupMap_.clear();
99 for (auto [id, value, flag] : policyCache[mode]) {
100 switchMap_[id] = value;
101 POWER_HILOGD(FEATURE_POWER_MODE, "read switch id: %{public}d, value: %{public}d", id, value);
102 if (flag == ValueProp::recover) {
103 GetSettingSwitchState(id, backupMap_[id]);
104 }
105 }
106 }
107
GetSettingSwitchState(uint32_t & switchId,int32_t & value)108 void PowerModePolicy::GetSettingSwitchState(uint32_t& switchId, int32_t& value)
109 {
110 int32_t defaultVal = INIT_VALUE_FALSE;
111 switch (switchId) {
112 case PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS:
113 defaultVal = SettingHelper::GetSettingAutoAdjustBrightness(defaultVal);
114 break;
115 case PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION:
116 defaultVal = SettingHelper::GetSettingWindowRotation(defaultVal);
117 break;
118 case PowerModePolicy::ServiceType::VIBRATORS_STATE:
119 defaultVal = SettingHelper::GetSettingVibration(defaultVal);
120 break;
121 case PowerModePolicy::ServiceType::INTELL_VOICE:
122 defaultVal = SettingHelper::GetSettingIntellVoice(defaultVal);
123 break;
124 case PowerModePolicy::ServiceType::DISPLAY_OFFTIME: {
125 int64_t displayOfftime = INIT_VALUE_FALSE;
126 displayOfftime = SettingHelper::GetSettingDisplayOffTime(INIT_VALUE_FALSE);
127 defaultVal = static_cast<int32_t>(displayOfftime);
128 break;
129 }
130 default:
131 break;
132 }
133
134 if (defaultVal == INIT_VALUE_FALSE) {
135 POWER_HILOGW(FEATURE_POWER_MODE, "get setting state invalid, switch id: %{public}d", switchId);
136 return;
137 }
138 value = defaultVal;
139 POWER_HILOGD(FEATURE_POWER_MODE, "read switch id: %{public}d, switch value: %{public}d", switchId, value);
140 }
141
AddAction(uint32_t type,ModeAction & action)142 void PowerModePolicy::AddAction(uint32_t type, ModeAction& action)
143 {
144 POWER_HILOGD(FEATURE_POWER_MODE, "add action, type=%{public}d", type);
145 std::lock_guard<std::mutex> lock(actionMapMutex_);
146 actionMap_.emplace(type, action);
147 }
148
TriggerAllActions(bool isBoot)149 void PowerModePolicy::TriggerAllActions(bool isBoot)
150 {
151 std::vector<ModeAction> allActions;
152 {
153 std::lock_guard<std::mutex> lock(actionMapMutex_);
154 for (auto iterator = actionMap_.begin(); iterator != actionMap_.end(); iterator++) {
155 POWER_HILOGD(FEATURE_POWER_MODE, "trigger action, type=%{public}d", iterator->first);
156 allActions.emplace_back(iterator->second);
157 }
158 }
159 for (const auto &actions : allActions) {
160 actions(isBoot);
161 }
162 }
163
IsValidType(uint32_t type)164 bool PowerModePolicy::IsValidType(uint32_t type)
165 {
166 std::lock_guard<std::mutex> lock(actionMapMutex_);
167 auto iterator = actionMap_.find(type);
168 if (iterator == actionMap_.end()) {
169 POWER_HILOGW(FEATURE_POWER_MODE, "Invalid type: %{public}d", type);
170 return false;
171 }
172 return true;
173 }
174
RemoveBackupMapSettingSwitch(uint32_t switchId)175 void PowerModePolicy::RemoveBackupMapSettingSwitch(uint32_t switchId)
176 {
177 auto iter = recoverMap_.find(switchId);
178 if (iter != recoverMap_.end()) {
179 recoverMap_.erase(iter);
180 SavePowerModeRecoverMap();
181 POWER_HILOGW(FEATURE_POWER_MODE, "remove backup switch: %{public}d", switchId);
182 }
183 }
184
SavePowerModeRecoverMap()185 void PowerModePolicy::SavePowerModeRecoverMap()
186 {
187 Json::Value recoverJson;
188 for (const auto& pair : recoverMap_) {
189 recoverJson[to_string(pair.first)] = pair.second;
190 }
191 SettingHelper::SavePowerModeRecoverMap(recoverJson.toStyledString());
192 }
193 } // namespace PowerMgr
194 } // namespace OHOS
195