• 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 "power_mode_module.h"
17 
18 #include "power_log.h"
19 #include "power_mode_policy.h"
20 #include "power_mgr_service.h"
21 #include "setting_helper.h"
22 
23 #include "singleton.h"
24 
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::AAFwk;
28 using namespace OHOS::EventFwk;
29 
30 namespace OHOS {
31 namespace PowerMgr {
PowerModeModule()32 PowerModeModule::PowerModeModule()
33     : mode_(PowerMode::NORMAL_MODE), lastMode_(LAST_MODE_FLAG), started_(false)
34 {
35     POWER_HILOGI(FEATURE_POWER_MODE, "Instance create");
36     callbackMgr_ = new CallbackManager();
37     auto policy = DelayedSingleton<PowerModePolicy>::GetInstance();
38     PowerModePolicy::ModeAction displayOffTimeAction = [&](bool isInit) { SetDisplayOffTime(isInit); };
39     policy->AddAction(PowerModePolicy::ServiceType::DISPLAY_OFFTIME, displayOffTimeAction);
40     PowerModePolicy::ModeAction sleepTimeAction = [&](bool isInit) { SetSleepTime(isInit); };
41     policy->AddAction(PowerModePolicy::ServiceType::SLEEPTIME, sleepTimeAction);
42     PowerModePolicy::ModeAction autoAdjustBrightnessAction = [&](bool isInit) { SetAutoAdjustBrightness(isInit); };
43     policy->AddAction(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS, autoAdjustBrightnessAction);
44     PowerModePolicy::ModeAction lcdBrightnessAction = [&](bool isInit) { SetLcdBrightness(isInit); };
45     policy->AddAction(PowerModePolicy::ServiceType::SMART_BACKLIGHT, lcdBrightnessAction);
46     PowerModePolicy::ModeAction vibrationAction = [&](bool isInit) { SetVibration(isInit); };
47     policy->AddAction(PowerModePolicy::ServiceType::VIBRATORS_STATE, vibrationAction);
48     PowerModePolicy::ModeAction onOffRotationAction = [&](bool isInit) { SetWindowRotation(isInit); };
49     policy->AddAction(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION, onOffRotationAction);
50 }
51 
SetModeItem(PowerMode mode)52 void PowerModeModule::SetModeItem(PowerMode mode)
53 {
54     POWER_HILOGI(FEATURE_POWER_MODE, "mode_: %{public}u, mode: %{public}u", mode_, mode);
55 
56     /* Same as the previous mode */
57     if (this->mode_ == mode) {
58         return;
59     }
60 
61     /* If it's a valid mode */
62     if (mode < PowerMode::POWER_MODE_MIN || mode > PowerMode::POWER_MODE_MAX) {
63         POWER_HILOGW(FEATURE_POWER_MODE, "Invalid mode %{public}d", mode);
64         return;
65     }
66 
67     /* start set mode thread */
68     EnableMode(mode);
69 }
70 
GetModeItem()71 PowerMode PowerModeModule::GetModeItem()
72 {
73     POWER_HILOGD(FEATURE_POWER_MODE, "mode_: %{public}u", mode_);
74     /* get power mode */
75     return mode_;
76 }
77 
EnableMode(PowerMode mode,bool isBoot)78 void PowerModeModule::EnableMode(PowerMode mode, bool isBoot)
79 {
80     if (started_) {
81         POWER_HILOGW(FEATURE_POWER_MODE, "Power Mode is already running");
82         return;
83     }
84 
85     started_ = true;
86     mode_ = mode;
87 
88     /* Update power mode policy */
89     UpdateModepolicy();
90 
91     /* Send state change */
92     Prepare();
93 
94     /* Set action */
95     RunAction(isBoot);
96 
97     this->lastMode_ = static_cast<uint32_t>(mode);
98     started_ = false;
99 }
100 
UpdateModepolicy()101 void PowerModeModule::UpdateModepolicy()
102 {
103     /* update policy */
104     DelayedSingleton<PowerModePolicy>::GetInstance()->SetPowerModePolicy(static_cast<uint32_t>(this->mode_),
105         this->lastMode_);
106 }
107 
AddPowerModeCallback(const sptr<IPowerModeCallback> & callback)108 void PowerModeModule::AddPowerModeCallback(const sptr<IPowerModeCallback>& callback)
109 {
110     if (callbackMgr_) {
111         callbackMgr_->AddCallback(callback);
112     }
113 }
114 
DelPowerModeCallback(const sptr<IPowerModeCallback> & callback)115 void PowerModeModule::DelPowerModeCallback(const sptr<IPowerModeCallback>& callback)
116 {
117     if (callbackMgr_) {
118         callbackMgr_->RemoveCallback(callback);
119     }
120 }
121 
Prepare()122 void PowerModeModule::Prepare()
123 {
124     PublishPowerModeEvent();
125     if (callbackMgr_) {
126         callbackMgr_->WaitingCallback();
127     }
128 }
129 
AddCallback(const sptr<IPowerModeCallback> & callback)130 void PowerModeModule::CallbackManager::AddCallback(const sptr<IPowerModeCallback>& callback)
131 {
132     unique_lock<mutex> lock(mutex_);
133     RETURN_IF((callback == nullptr) || (callback->AsObject() == nullptr));
134     auto object = callback->AsObject();
135     auto retIt = callbacks_.insert(object);
136     if (retIt.second) {
137         object->AddDeathRecipient(this);
138     }
139     POWER_HILOGD(FEATURE_POWER_MODE, "callbacks.size = %{public}zu, insertOk = %{public}d",
140         callbacks_.size(), retIt.second);
141 }
142 
RemoveCallback(const sptr<IPowerModeCallback> & callback)143 void PowerModeModule::CallbackManager::RemoveCallback(const sptr<IPowerModeCallback>& callback)
144 {
145     unique_lock<mutex> lock(mutex_);
146     RETURN_IF((callback == nullptr) || (callback->AsObject() == nullptr));
147     auto object = callback->AsObject();
148     auto it = find(callbacks_.begin(), callbacks_.end(), object);
149     if (it != callbacks_.end()) {
150         callbacks_.erase(it);
151         object->RemoveDeathRecipient(this);
152     }
153     POWER_HILOGD(FEATURE_POWER_MODE, "callbacks.size = %{public}zu", callbacks_.size());
154 }
155 
OnRemoteDied(const wptr<IRemoteObject> & remote)156 void PowerModeModule::CallbackManager::OnRemoteDied(const wptr<IRemoteObject>& remote)
157 {
158     POWER_HILOGW(FEATURE_POWER_MODE, "On remote died");
159     RETURN_IF(remote.promote() == nullptr);
160     RemoveCallback(iface_cast<IPowerModeCallback>(remote.promote()));
161 }
162 
WaitingCallback()163 void PowerModeModule::CallbackManager::WaitingCallback()
164 {
165     POWER_HILOGD(FEATURE_POWER_MODE, "Mode callback started");
166     unique_lock<mutex> lock(mutex_);
167     for (auto& obj: callbacks_) {
168         sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
169         if (callback != nullptr) {
170             POWER_HILOGD(FEATURE_POWER_MODE, "Call IPowerModeCallback");
171             PowerMode mode = PowerMode::NORMAL_MODE;
172             callback->OnPowerModeChanged(mode);
173         }
174     }
175 }
176 
PublishPowerModeEvent()177 void PowerModeModule::PublishPowerModeEvent()
178 {
179     POWER_HILOGD(FEATURE_POWER_MODE, "Publish power mode module event");
180     /* send event */
181     CommonEventPublishInfo publishInfo;
182     publishInfo.SetOrdered(false);
183     std::string action;
184     uint32_t code;
185     std::string data;
186     switch (mode_) {
187         case PowerMode::PERFORMANCE_MODE:
188             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
189             code = static_cast<uint32_t>(PowerMode::PERFORMANCE_MODE);
190             data = ToString(static_cast<uint32_t>(PowerMode::PERFORMANCE_MODE));
191             break;
192         case PowerMode::NORMAL_MODE:
193             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
194             code = static_cast<uint32_t>(PowerMode::NORMAL_MODE);
195             data = ToString(static_cast<uint32_t>(PowerMode::NORMAL_MODE));
196             break;
197         case PowerMode::POWER_SAVE_MODE:
198             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
199             code = static_cast<uint32_t>(PowerMode::POWER_SAVE_MODE);
200             data = ToString(static_cast<uint32_t>(PowerMode::POWER_SAVE_MODE));
201             break;
202         case PowerMode::EXTREME_POWER_SAVE_MODE:
203             action = CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED;
204             code = static_cast<uint32_t>(PowerMode::EXTREME_POWER_SAVE_MODE);
205             data = ToString(static_cast<uint32_t>(PowerMode::EXTREME_POWER_SAVE_MODE));
206             break;
207         default:
208             POWER_HILOGW(FEATURE_POWER_MODE, "Unknown mode");
209             return;
210     }
211     IntentWant setModeWant;
212     setModeWant.SetAction(action);
213     CommonEventData event(setModeWant);
214     event.SetCode(code);
215     event.SetData(data);
216     if (!CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr)) {
217         POWER_HILOGE(FEATURE_POWER_MODE, "Failed to publish the mode event");
218         return;
219     }
220     POWER_HILOGD(FEATURE_POWER_MODE, "Publish power mode module event end");
221 }
222 
RunAction(bool isBoot)223 void PowerModeModule::RunAction(bool isBoot)
224 {
225     POWER_HILOGD(FEATURE_POWER_MODE, "Run action");
226     auto policy = DelayedSingleton<PowerModePolicy>::GetInstance();
227     policy->TriggerAllActions(isBoot);
228 }
229 
SetDisplayOffTime(bool isBoot)230 void PowerModeModule::SetDisplayOffTime(bool isBoot)
231 {
232     if (isBoot && SettingHelper::IsDisplayOffTimeSettingValid()) {
233         return;
234     }
235     int32_t time = DelayedSingleton<PowerModePolicy>::GetInstance()
236         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::DISPLAY_OFFTIME);
237     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
238     POWER_HILOGD(FEATURE_POWER_MODE, "Set display off timeout: %{public}d", time);
239     pms->GetPowerStateMachine()->SetDisplayOffTime(static_cast<int64_t>(time));
240 }
241 
SetSleepTime(bool isBoot)242 void PowerModeModule::SetSleepTime([[maybe_unused]] bool isBoot)
243 {
244     int32_t time = DelayedSingleton<PowerModePolicy>::GetInstance()
245         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::SLEEPTIME);
246     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
247     POWER_HILOGD(FEATURE_POWER_MODE, "Set sleep timeout: %{public}d", time);
248     pms->GetPowerStateMachine()->SetSleepTime(static_cast<int64_t>(time));
249 }
250 
SetAutoAdjustBrightness(bool isBoot)251 void PowerModeModule::SetAutoAdjustBrightness(bool isBoot)
252 {
253     if (isBoot && SettingHelper::IsAutoAdjustBrightnessSettingValid()) {
254         return;
255     }
256     int32_t value = DelayedSingleton<PowerModePolicy>::GetInstance()
257         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_ADJUST_BRIGHTNESS);
258     auto status = static_cast<SettingHelper::SwitchStatus>(value);
259     POWER_HILOGI(FEATURE_POWER_MODE, "status: %{public}d", status);
260     SettingHelper::SetSettingAutoAdjustBrightness(status);
261 }
262 
SetLcdBrightness(bool isBoot)263 void PowerModeModule::SetLcdBrightness(bool isBoot)
264 {
265     if (isBoot && SettingHelper::IsBrightnessSettingValid()) {
266         return;
267     }
268     int32_t lcdBrightness = DelayedSingleton<PowerModePolicy>::GetInstance()
269         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::SMART_BACKLIGHT);
270     POWER_HILOGD(FEATURE_POWER_MODE, "lcdBrightness: %{public}d", lcdBrightness);
271     SettingHelper::SetSettingBrightness(lcdBrightness);
272 }
273 
SetVibration(bool isBoot)274 void PowerModeModule::SetVibration(bool isBoot)
275 {
276     if (isBoot && SettingHelper::IsVibrationSettingValid()) {
277         return;
278     }
279     int32_t vibration = DelayedSingleton<PowerModePolicy>::GetInstance()
280         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::VIBRATORS_STATE);
281     POWER_HILOGD(FEATURE_POWER_MODE, "GetPowerModeValuePolicy vibrate=%{public}d", vibration);
282     SettingHelper::SetSettingVibration(static_cast<SettingHelper::SwitchStatus>(vibration));
283 }
284 
SetWindowRotation(bool isBoot)285 void PowerModeModule::SetWindowRotation(bool isBoot)
286 {
287     if (isBoot && SettingHelper::IsWindowRotationSettingValid()) {
288         return;
289     }
290     int32_t rotation = DelayedSingleton<PowerModePolicy>::GetInstance()
291         ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::AUTO_WINDOWN_RORATION);
292     POWER_HILOGD(FEATURE_POWER_MODE, "GetPowerModeValuePolicy rotation=%{public}d", rotation);
293     SettingHelper::SetSettingWindowRotation(static_cast<SettingHelper::SwitchStatus>(rotation));
294 }
295 } // namespace PowerMgr
296 } // namespace OHOS
297