• 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 #ifndef POWERMGR_POWER_MGR_SERVICE_H
17 #define POWERMGR_POWER_MGR_SERVICE_H
18 
19 #include <common_event_subscriber.h>
20 #include <iremote_object.h>
21 #ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT
22 #include <screen_manager_lite.h>
23 #endif
24 #include <system_ability.h>
25 
26 #include "actions/idevice_power_action.h"
27 #include "ffrt_utils.h"
28 #include "ipower_mgr.h"
29 #include "power_mgr_notify.h"
30 #include "power_mgr_service_ipc_adapter.h"
31 #include "power_mgr_stub.h"
32 #include "power_mode_module.h"
33 #include "power_save_mode.h"
34 #include "power_state_machine.h"
35 #include "running_lock_mgr.h"
36 #include "screen_off_pre_controller.h"
37 #include "shutdown_controller.h"
38 #include "shutdown_dialog.h"
39 #include "sp_singleton.h"
40 #include "suspend_controller.h"
41 #include "wakeup_controller.h"
42 #ifdef POWER_MANAGER_WAKEUP_ACTION
43 #include "wakeup_action_controller.h"
44 #endif
45 
46 namespace OHOS {
47 namespace PowerMgr {
48 #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
49 enum class PowerConnectStatus : int32_t {
50     POWER_CONNECT_INVALID = -1,
51     POWER_CONNECT_DC = 0, // DC for Direct Current, means battery supply
52     POWER_CONNECT_AC = 1, // AC for Alternating Current, means charing supply
53 };
54 #endif
55 
56 class RunningLockMgr;
57 class PowerMgrService final : public SystemAbility, public PowerMgrServiceAdapter {
58     DECLARE_SYSTEM_ABILITY(PowerMgrService)
59     DECLARE_DELAYED_SP_SINGLETON(PowerMgrService);
60 
61 public:
62     static void RegisterSettingWakeupPickupGestureObserver();
63     static void WakeupPickupGestureSettingUpdateFunc(const std::string& key);
64     static void RegisterSettingWakeupDoubleClickObservers();
65     static void WakeupDoubleClickSettingUpdateFunc(const std::string& key);
66     static bool GetSettingWakeupDoubleClick(const std::string& key = SETTING_POWER_WAKEUP_DOUBLE_KEY);
67     static void RegisterSettingPowerModeObservers();
68     static void PowerModeSettingUpdateFunc(const std::string& key);
69     static void RegisterSettingWakeUpLidObserver();
70     static void WakeupLidSettingUpdateFunc(const std::string& key);
71     static void RegisterSettingDuringCallObservers();
72     static void DuringCallSettingUpdateFunc(const std::string& key);
73 
74     virtual void OnStart() override;
75     virtual void OnStop() override;
76     virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
77     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
78     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
79     virtual PowerErrors RebootDevice(const std::string& reason) override;
80     virtual PowerErrors RebootDeviceForDeprecated(const std::string& reason) override;
81     virtual PowerErrors ShutDownDevice(const std::string& reason) override;
82     virtual PowerErrors SetSuspendTag(const std::string& tag) override;
83     virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason,
84         bool suspendImmed, const std::string& apiVersion = "-1") override;
85     virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason,
86         const std::string& details, const std::string& apiVersion = "-1") override;
WakeupDeviceAsync(int64_t callTimeMs,WakeupDeviceType reason,const std::string & details)87     virtual void WakeupDeviceAsync(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override {};
88     virtual bool RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) override;
89     bool RefreshActivityInner(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight);
90     virtual PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion = "-1") override;
91     virtual PowerErrors RestoreScreenOffTime(const std::string& apiVersion = "-1") override;
92     virtual PowerState GetState() override;
93     virtual bool IsScreenOn(bool needPrintLog = true) override;
94     virtual bool IsFoldScreenOn() override;
95     virtual bool IsCollaborationScreenOn() override;
96     virtual bool IsForceSleeping() override;
97     virtual PowerErrors ForceSuspendDevice(int64_t callTimeMs, const std::string& apiVersion = "-1") override;
98     virtual PowerErrors Hibernate(
99         bool clearMemory, const std::string& reason = "", const std::string& apiVersion = "-1") override;
100     virtual PowerErrors CreateRunningLock(
101         const sptr<IRemoteObject>& remoteObj, const RunningLockInfo& runningLockInfo) override;
102     virtual bool ReleaseRunningLock(const sptr<IRemoteObject>& remoteObj, const std::string& name = "") override;
103     virtual bool IsRunningLockTypeSupported(RunningLockType type) override;
104     virtual bool UpdateWorkSource(const sptr<IRemoteObject>& remoteObj,
105         const std::vector<int32_t>& workSources) override;
106     virtual PowerErrors Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMs = -1) override;
107     virtual PowerErrors UnLock(const sptr<IRemoteObject>& remoteObj, const std::string& name = "") override;
108     virtual bool QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists) override;
109     virtual void ForceUnLock(const sptr<IRemoteObject>& remoteObj);
110     virtual bool IsUsed(const sptr<IRemoteObject>& remoteObj) override;
111     virtual bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid) override;
112     virtual bool ProxyRunningLocks(bool isProxied,
113         const std::vector<std::pair<pid_t, pid_t>>& processInfos) override;
114     virtual bool ResetRunningLocks() override;
115     virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync = true) override;
116     virtual bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
117 
118     virtual bool RegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority) override;
119     virtual bool UnRegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback) override;
120     virtual bool RegisterSuspendTakeoverCallback(const sptr<ITakeOverSuspendCallback>& callback,
121         TakeOverSuspendPriority priority) override;
122     virtual bool UnRegisterSuspendTakeoverCallback(const sptr<ITakeOverSuspendCallback>& callback) override;
123     virtual bool RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback) override;
124     virtual bool UnRegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback) override;
125 
126     virtual bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override;
127     virtual bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override;
128 
129     virtual bool RegisterScreenStateCallback(int32_t remainTime, const sptr<IScreenOffPreCallback>& callback) override;
130     virtual bool UnRegisterScreenStateCallback(const sptr<IScreenOffPreCallback>& callback) override;
131 
132     virtual bool RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback) override;
133     virtual bool UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback) override;
134     virtual bool SetDisplaySuspend(bool enable) override;
135     virtual PowerErrors SetDeviceMode(const PowerMode& mode) override;
136     virtual PowerMode GetDeviceMode() override;
137     virtual std::string ShellDump(const std::vector<std::string>& args, uint32_t argc) override;
138     virtual PowerErrors IsStandby(bool& isStandby) override;
139     virtual PowerErrors SetForceTimingOut(bool enabled, const sptr<IRemoteObject>& token) override;
140     virtual PowerErrors LockScreenAfterTimingOut(
141         bool enabledLockScreen, bool checkLock, bool sendScreenOffEvent, const sptr<IRemoteObject>& token) override;
142     virtual PowerErrors IsRunningLockEnabled(const RunningLockType type, bool& result) override;
143     virtual PowerErrors RefreshActivity(
144         int64_t callTimeMs, UserActivityType type, const std::string& refreshReason) override;
145 
146     void SetEnableDoze(bool enable);
147     void RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback, ShutdownPriority priority) override;
148     void UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback) override;
149 
150     void RegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback, ShutdownPriority priority) override;
151     void UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback) override;
152     void RegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback, ShutdownPriority priority) override;
153     void UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback) override;
154 
155 #ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT
156     void ExternalScreenInit();
157     void RegisterExternalScreenListener();
158     void UnRegisterExternalScreenListener();
159 #endif
160     void HandleKeyEvent(int32_t keyCode);
161     void HandlePointEvent(int32_t type);
162     void KeyMonitorCancel();
163     void SwitchSubscriberInit();
164     void SwitchSubscriberCancel();
165     void HallSensorSubscriberInit();
166     void HallSensorSubscriberCancel();
167     bool CheckDialogFlag();
168     void InputMonitorInit();
169     void InputMonitorCancel();
170     bool CheckDialogAndShuttingDown();
171     void SuspendControllerInit();
172     void WakeupControllerInit();
173 #ifdef POWER_MANAGER_POWER_ENABLE_S4
174     void HibernateControllerInit();
175 #endif
176     bool IsCollaborationState();
177     void QueryRunningLockListsInner(std::map<std::string, RunningLockInfo>& runningLockLists);
178     bool IsExistAudioStream(pid_t uid);
179 #ifdef POWER_MANAGER_WAKEUP_ACTION
180     void WakeupActionControllerInit();
181 #endif
182     void VibratorInit();
183     void Reset();
184     void KeepScreenOnInit();
185     void KeepScreenOn(bool isOpenOn);
186     void UnregisterAllSettingObserver();
187     void RegisterAllSettingObserver();
188     int64_t GetSettingDisplayOffTime(int64_t defaultTime);
189 #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
GetPowerConnectStatus()190     PowerConnectStatus GetPowerConnectStatus() const
191     {
192         return powerConnectStatus_;
193     }
SetPowerConnectStatus(PowerConnectStatus status)194     void SetPowerConnectStatus(PowerConnectStatus status)
195     {
196         powerConnectStatus_ = status;
197     }
198     void PowerConnectStatusInit();
199     bool IsPowerConnected();
200     void UpdateSettingInvalidDisplayOffTime();
201 #endif
202     void OnChargeStateChanged();
203 
GetRunningLockMgr()204     std::shared_ptr<RunningLockMgr> GetRunningLockMgr() const
205     {
206         return runningLockMgr_;
207     }
208 
GetPowerStateMachine()209     std::shared_ptr<PowerStateMachine> GetPowerStateMachine() const
210     {
211         return powerStateMachine_;
212     }
213 
GetPowerMgrNotify()214     std::shared_ptr<PowerMgrNotify> GetPowerMgrNotify() const
215     {
216         return powerMgrNotify_;
217     }
218 
GetSuspendController()219     std::shared_ptr<SuspendController> GetSuspendController() const
220     {
221         return suspendController_;
222     }
223 #ifdef POWER_MANAGER_POWER_ENABLE_S4
GetHibernateController()224     std::shared_ptr<HibernateController> GetHibernateController() const
225     {
226         return hibernateController_;
227     }
228 #endif
GetWakeupController()229     std::shared_ptr<WakeupController> GetWakeupController() const
230     {
231         return wakeupController_;
232     }
GetScreenOffPreController()233     std::shared_ptr<ScreenOffPreController> GetScreenOffPreController() const
234     {
235         return screenOffPreController_;
236     }
237 #ifdef POWER_MANAGER_WAKEUP_ACTION
GetWakeupActionController()238     std::shared_ptr<WakeupActionController> GetWakeupActionController() const
239     {
240         return wakeupActionController_;
241     }
242 #endif
GetShutdownDialog()243     ShutdownDialog& GetShutdownDialog()
244     {
245         return shutdownDialog_;
246     }
IsServiceReady()247     bool IsServiceReady() const
248     {
249         return ready_;
250     }
SetDisplayOffTime(int64_t time)251     void SetDisplayOffTime(int64_t time)
252     {
253         powerStateMachine_->SetDisplayOffTime(time);
254     }
SetSleepTime(int64_t time)255     void SetSleepTime(int64_t time)
256     {
257         powerStateMachine_->SetSleepTime(time);
258     }
259 
EnableMock(IDeviceStateAction * powerState,IDeviceStateAction * shutdownState,IDevicePowerAction * powerAction,IRunningLockAction * lockAction)260     void EnableMock(IDeviceStateAction* powerState, IDeviceStateAction* shutdownState, IDevicePowerAction* powerAction,
261         IRunningLockAction* lockAction)
262     {
263         POWER_HILOGI(LABEL_TEST, "Service EnableMock:%{public}d", mockCount_++);
264         runningLockMgr_->EnableMock(lockAction);
265         powerStateMachine_->EnableMock(powerState);
266         shutdownController_->EnableMock(powerAction, shutdownState);
267     }
268 
EnableShutdownMock(ShutdownController * shutdownAction)269     void EnableShutdownMock(ShutdownController* shutdownAction)
270     {
271         POWER_HILOGI(LABEL_TEST, "need to mock shutdownController");
272         std::unique_ptr<ShutdownController> mock(shutdownAction);
273         shutdownController_.reset();
274         shutdownController_ = std::move(mock);
275     }
276 
MockProximity(uint32_t status)277     void MockProximity(uint32_t status)
278     {
279         POWER_HILOGI(LABEL_TEST, "MockProximity: fun is start");
280 #ifdef HAS_SENSORS_SENSOR_PART
281         runningLockMgr_->SetProximity(status);
282 #endif
283         POWER_HILOGI(LABEL_TEST, "MockProximity: fun is end");
284     }
MockSystemWakeup()285     void MockSystemWakeup()
286     {
287         PowerStateMachine::onWakeup();
288     }
GetShutdownController()289     std::shared_ptr<ShutdownController> GetShutdownController()
290     {
291         return shutdownController_;
292     }
IsDuringCallStateEnable()293     bool IsDuringCallStateEnable()
294     {
295         return isDuringCallStateEnable_;
296     }
297 #ifdef HAS_SENSORS_SENSOR_PART
298     static std::atomic_bool isInLidMode_;
299 #endif
300 
301 private:
302     static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000;
303     static constexpr int32_t INIT_KEY_MONITOR_DELAY_MS = 1000;
304     static constexpr int32_t HALL_REPORT_INTERVAL = 0;
305     static constexpr uint32_t HALL_SAMPLING_RATE = 100000000;
306     static constexpr const char* SETTING_POWER_WAKEUP_DOUBLE_KEY {"settings.power.wakeup_double_click"};
307     static std::atomic_bool isBootCompleted_;
308     static std::atomic_bool isNeedReInit_ ;
309     static std::atomic_bool displayManagerServiceCrash_;
310     static void RegisterBootCompletedCallback();
311     static void PowerExternalAbilityInit();
312     static bool IsDeveloperMode();
313 #ifdef HAS_SENSORS_SENSOR_PART
314     static void HallSensorCallback(SensorEvent* event);
315 #endif
316 
317 #ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT
318     class ExternalScreenListener : public Rosen::ScreenManagerLite::IScreenListener {
319     public:
320         virtual void OnConnect(uint64_t screenId) override;
321         virtual void OnDisconnect(uint64_t screenId) override;
OnChange(uint64_t screenId)322         virtual void OnChange(uint64_t screenId) override {}
323     };
324     class AbnormalExternalScreenConnectListener
325         : public Rosen::ScreenManagerLite::IAbnormalScreenConnectChangeListener {
326     public:
327         virtual void NotifyAbnormalScreenConnectChange(uint64_t screenId) override;
328     };
329 #endif
330     class BackgroundRunningLock {
331     public:
332         BackgroundRunningLock(std::string name, int32_t timeOutMs);
333         ~BackgroundRunningLock();
334     private:
335         sptr<IRemoteObject> token_ {nullptr};
336     };
337 
338     class InvokerDeathRecipient : public DeathRecipient {
339         using CallbackType = std::function<void(const sptr<PowerMgrService>&)>;
340 
341     public:
InvokerDeathRecipient(std::string interfaceName,CallbackType callback)342         InvokerDeathRecipient(std::string interfaceName, CallbackType callback)
343             : interfaceName_(interfaceName),
344               callback_(callback) {}
345         virtual ~InvokerDeathRecipient() = default;
346         virtual void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
347 
348     private:
349         std::string interfaceName_;
350         CallbackType callback_;
351     };
352 
GetPowerModeModule()353     inline PowerModeModule& GetPowerModeModule()
354     {
355         return powerModeModule_;
356     }
357 
358     bool Init();
359     bool PowerStateMachineInit();
360     std::string GetBundleNameByUid(const int32_t uid);
361     RunningLockParam FillRunningLockParam(const RunningLockInfo& info, const uint64_t lockid, int32_t timeOutMS = -1);
362     void SubscribeCommonEvent();
363 #ifdef MSDP_MOVEMENT_ENABLE
364     void RegisterMovementCallback();
365     void UnRegisterMovementCallback();
366     void ResetMovementState();
367 #endif
368 #ifdef HAS_SENSORS_SENSOR_PART
369     bool IsSupportSensor(SensorTypeId);
370 #endif
371     void RegisterExternalCallback();
372     void UnregisterExternalCallback();
373 
374     bool ready_ {false};
375     bool isDuringCallStateEnable_ {false};
376     std::mutex wakeupMutex_;
377     std::mutex suspendMutex_;
378 #ifdef POWER_MANAGER_POWER_ENABLE_S4
379     std::mutex hibernateMutex_;
380 #endif
381     std::mutex stateMutex_;
382     std::mutex shutdownMutex_;
383     std::mutex modeMutex_;
384     std::mutex screenOffPreMutex_;
385     std::mutex screenMutex_;
386     std::mutex dumpMutex_;
387     std::mutex lockMutex_;
388     std::shared_ptr<RunningLockMgr> runningLockMgr_ {nullptr};
389     std::shared_ptr<PowerStateMachine> powerStateMachine_ {nullptr};
390     std::shared_ptr<PowerMgrNotify> powerMgrNotify_ {nullptr};
391     std::shared_ptr<ShutdownController> shutdownController_ {nullptr};
392     std::shared_ptr<FFRTTimer> ffrtTimer_ {nullptr};
393     std::shared_ptr<EventFwk::CommonEventSubscriber> subscriberPtr_ {nullptr};
394     std::shared_ptr<SuspendController> suspendController_ {nullptr};
395     std::shared_ptr<WakeupController> wakeupController_ {nullptr};
396     sptr<IRemoteObject> ptoken_ {nullptr};
397 #ifdef POWER_MANAGER_POWER_ENABLE_S4
398     std::shared_ptr<HibernateController> hibernateController_ {nullptr};
399 #endif
400     std::shared_ptr<ScreenOffPreController> screenOffPreController_ {nullptr};
401 #ifdef POWER_MANAGER_WAKEUP_ACTION
402     std::shared_ptr<WakeupActionController> wakeupActionController_ {nullptr};
403 #endif
404 #ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT
405     sptr<Rosen::ScreenManagerLite::IScreenListener> externalScreenListener_ {nullptr};
406     sptr<Rosen::ScreenManagerLite::IAbnormalScreenConnectChangeListener> abnormalExScreenListener_ {nullptr};
407 #endif
408     PowerModeModule powerModeModule_;
409     ShutdownDialog shutdownDialog_;
410     uint32_t mockCount_ {0};
411     int32_t switchId_ {0};
412     int32_t doubleClickId_ {0};
413     int32_t monitorId_ {0};
414     int32_t inputMonitorId_ {-1};
415 #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
416     PowerConnectStatus powerConnectStatus_ {PowerConnectStatus::POWER_CONNECT_INVALID};
417 #endif
418 #ifdef HAS_SENSORS_SENSOR_PART
419     SensorUser sensorUser_{};
420 #endif
421 };
422 
423 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
424 class PowerMgrInputMonitor : public IInputEventConsumer {
425 public:
426     virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const;
427     virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const;
428     virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const;
429 };
430 #endif
431 
432 class PowerCommonEventSubscriber : public EventFwk::CommonEventSubscriber {
433 public:
PowerCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)434     explicit PowerCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscribeInfo)
435         : EventFwk::CommonEventSubscriber(subscribeInfo) {}
~PowerCommonEventSubscriber()436     virtual ~PowerCommonEventSubscriber() {}
437     void OnReceiveEvent(const EventFwk::CommonEventData &data) override;
438 private:
439 #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
440     void OnPowerConnectStatusChanged(PowerConnectStatus status);
441 #endif
442 };
443 
444 } // namespace PowerMgr
445 } // namespace OHOS
446 #endif // POWERMGR_POWER_MGR_SERVICE_H
447