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 #include "power_mgr_service.h"
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 
GetSettingDisplayOffTime(int64_t defaultVal)108 int64_t PowerModePolicy::GetSettingDisplayOffTime(int64_t defaultVal)
109 {
110     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
111     if (pms == nullptr) {
112         POWER_HILOGE(FEATURE_POWER_MODE, "get PowerMgrService fail");
113         return defaultVal;
114     }
115     return pms->GetSettingDisplayOffTime(defaultVal);
116 }
117 
GetSettingSwitchState(uint32_t & switchId,int32_t & value)118 void PowerModePolicy::GetSettingSwitchState(uint32_t& switchId, int32_t& value)
119 {
120     int32_t defaultVal = INIT_VALUE_FALSE;
121     switch (switchId) {
122         case PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS:
123             defaultVal = SettingHelper::GetSettingAutoAdjustBrightness(defaultVal);
124             break;
125         case PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION:
126             defaultVal = SettingHelper::GetSettingWindowRotation(defaultVal);
127             break;
128         case PowerModePolicy::ServiceType::VIBRATORS_STATE:
129             defaultVal = SettingHelper::GetSettingVibration(defaultVal);
130             break;
131         case PowerModePolicy::ServiceType::INTELL_VOICE:
132             defaultVal = SettingHelper::GetSettingIntellVoice(defaultVal);
133             break;
134         case PowerModePolicy::ServiceType::DISPLAY_OFFTIME: {
135             int64_t displayOfftime = GetSettingDisplayOffTime(static_cast<int64_t>(defaultVal));
136             defaultVal = static_cast<int32_t>(displayOfftime);
137             break;
138         }
139         default:
140             break;
141     }
142 
143     if (defaultVal == INIT_VALUE_FALSE) {
144         POWER_HILOGW(FEATURE_POWER_MODE, "get setting state invalid, switch id: %{public}d", switchId);
145         return;
146     }
147     value = defaultVal;
148     POWER_HILOGD(FEATURE_POWER_MODE, "read switch id: %{public}d, switch value: %{public}d", switchId, value);
149 }
150 
AddAction(uint32_t type,ModeAction & action)151 void PowerModePolicy::AddAction(uint32_t type, ModeAction& action)
152 {
153     POWER_HILOGD(FEATURE_POWER_MODE, "add action, type=%{public}d", type);
154     std::lock_guard<std::mutex> lock(actionMapMutex_);
155     actionMap_.emplace(type, action);
156 }
157 
TriggerAllActions(bool isBoot)158 void PowerModePolicy::TriggerAllActions(bool isBoot)
159 {
160     std::vector<ModeAction> allActions;
161     {
162         std::lock_guard<std::mutex> lock(actionMapMutex_);
163         for (auto iterator = actionMap_.begin(); iterator != actionMap_.end(); iterator++) {
164             POWER_HILOGD(FEATURE_POWER_MODE, "trigger action, type=%{public}d", iterator->first);
165             allActions.emplace_back(iterator->second);
166         }
167     }
168     for (const auto &actions : allActions) {
169         actions(isBoot);
170     }
171 }
172 
IsValidType(uint32_t type)173 bool PowerModePolicy::IsValidType(uint32_t type)
174 {
175     std::lock_guard<std::mutex> lock(actionMapMutex_);
176     auto iterator = actionMap_.find(type);
177     if (iterator == actionMap_.end()) {
178         POWER_HILOGW(FEATURE_POWER_MODE, "Invalid type: %{public}d", type);
179         return false;
180     }
181     return true;
182 }
183 
RemoveBackupMapSettingSwitch(uint32_t switchId)184 void PowerModePolicy::RemoveBackupMapSettingSwitch(uint32_t switchId)
185 {
186     auto iter = recoverMap_.find(switchId);
187     if (iter != recoverMap_.end()) {
188         recoverMap_.erase(iter);
189         SavePowerModeRecoverMap();
190         POWER_HILOGW(FEATURE_POWER_MODE, "remove backup switch: %{public}d", switchId);
191     }
192 }
193 
SavePowerModeRecoverMap()194 void PowerModePolicy::SavePowerModeRecoverMap()
195 {
196     Json::Value recoverJson;
197     for (const auto& pair : recoverMap_) {
198         recoverJson[to_string(pair.first)] = pair.second;
199     }
200     SettingHelper::SavePowerModeRecoverMap(recoverJson.toStyledString());
201 }
202 } // namespace PowerMgr
203 } // namespace OHOS
204