/* * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef POWERMGR_POWER_MGR_SERVICE_H #define POWERMGR_POWER_MGR_SERVICE_H #include #include #include "actions/idevice_power_action.h" #include "ipower_mgr.h" #include "power_mgr_notify.h" #include "power_mgr_stub.h" #include "power_mode_module.h" #include "power_save_mode.h" #include "power_state_machine.h" #include "running_lock_mgr.h" #include "screen_off_pre_controller.h" #include "shutdown_controller.h" #include "shutdown_dialog.h" #include "sp_singleton.h" #include "suspend_controller.h" #include "wakeup_controller.h" #include "common_event_subscriber.h" #ifdef POWER_MANAGER_WAKEUP_ACTION #include "wakeup_action_controller.h" #endif namespace OHOS { namespace PowerMgr { #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING typedef enum { POWER_CONNECT_INVALID = -1, POWER_CONNECT_DC = 0, // DC for Direct Current, means battery supply POWER_CONNECT_AC = 1, // AC for Alternating Current, means charing supply } PowerConnectStatus; #endif class RunningLockMgr; class PowerMgrService final : public SystemAbility, public PowerMgrStub { DECLARE_SYSTEM_ABILITY(PowerMgrService) DECLARE_DELAYED_SP_SINGLETON(PowerMgrService); public: virtual void OnStart() override; virtual void OnStop() override; virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; int32_t Dump(int32_t fd, const std::vector& args) override; virtual PowerErrors RebootDevice(const std::string& reason) override; virtual PowerErrors RebootDeviceForDeprecated(const std::string& reason) override; virtual PowerErrors ShutDownDevice(const std::string& reason) override; virtual PowerErrors SetSuspendTag(const std::string& tag) override; virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) override; virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override; virtual void WakeupDeviceAsync(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override {}; virtual bool RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) override; bool RefreshActivityInner(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight); virtual PowerErrors OverrideScreenOffTime(int64_t timeout) override; virtual PowerErrors RestoreScreenOffTime() override; virtual PowerState GetState() override; virtual bool IsScreenOn(bool needPrintLog = true) override; virtual bool IsFoldScreenOn() override; virtual bool IsCollaborationScreenOn() override; virtual PowerErrors ForceSuspendDevice(int64_t callTimeMs) override; virtual PowerErrors Hibernate(bool clearMemory) override; virtual PowerErrors CreateRunningLock( const sptr& remoteObj, const RunningLockInfo& runningLockInfo) override; virtual bool ReleaseRunningLock(const sptr& remoteObj, const std::string& name = "") override; virtual bool IsRunningLockTypeSupported(RunningLockType type) override; virtual bool UpdateWorkSource(const sptr& remoteObj, const std::map& workSources) override; virtual PowerErrors Lock(const sptr& remoteObj, int32_t timeOutMs = -1) override; virtual PowerErrors UnLock(const sptr& remoteObj, const std::string& name = "") override; virtual bool QueryRunningLockLists(std::map& runningLockLists) override; virtual void ForceUnLock(const sptr& remoteObj); virtual bool IsUsed(const sptr& remoteObj) override; virtual bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid) override; virtual bool ProxyRunningLocks(bool isProxied, const std::vector>& processInfos) override; virtual bool ResetRunningLocks() override; virtual bool RegisterPowerStateCallback(const sptr& callback, bool isSync = true) override; virtual bool UnRegisterPowerStateCallback(const sptr& callback) override; virtual bool RegisterSyncSleepCallback(const sptr& callback, SleepPriority priority) override; virtual bool UnRegisterSyncSleepCallback(const sptr& callback) override; virtual bool RegisterSyncHibernateCallback(const sptr& callback) override; virtual bool UnRegisterSyncHibernateCallback(const sptr& callback) override; virtual bool RegisterPowerModeCallback(const sptr& callback) override; virtual bool UnRegisterPowerModeCallback(const sptr& callback) override; virtual bool RegisterScreenStateCallback(int32_t remainTime, const sptr& callback) override; virtual bool UnRegisterScreenStateCallback(const sptr& callback) override; virtual bool RegisterRunningLockCallback(const sptr& callback) override; virtual bool UnRegisterRunningLockCallback(const sptr& callback) override; virtual bool SetDisplaySuspend(bool enable) override; virtual PowerErrors SetDeviceMode(const PowerMode& mode) override; virtual PowerMode GetDeviceMode() override; virtual std::string ShellDump(const std::vector& args, uint32_t argc) override; virtual PowerErrors IsStandby(bool& isStandby) override; virtual PowerErrors SetForceTimingOut(bool enabled, const sptr& token) override; virtual PowerErrors LockScreenAfterTimingOut( bool enabledLockScreen, bool checkLock, bool sendScreenOffEvent, const sptr& token) override; void RegisterShutdownCallback(const sptr& callback, ShutdownPriority priority) override; void UnRegisterShutdownCallback(const sptr& callback) override; void RegisterShutdownCallback(const sptr& callback, ShutdownPriority priority) override; void UnRegisterShutdownCallback(const sptr& callback) override; void RegisterShutdownCallback(const sptr& callback, ShutdownPriority priority) override; void UnRegisterShutdownCallback(const sptr& callback) override; void HandleKeyEvent(int32_t keyCode); void HandlePointEvent(int32_t type); void KeyMonitorCancel(); void SwitchSubscriberInit(); void SwitchSubscriberCancel(); void HallSensorSubscriberInit(); void HallSensorSubscriberCancel(); bool CheckDialogFlag(); void InputMonitorInit(); void InputMonitorCancel(); bool CheckDialogAndShuttingDown(); void SuspendControllerInit(); void WakeupControllerInit(); #ifdef POWER_MANAGER_POWER_ENABLE_S4 void HibernateControllerInit(); #endif bool IsCollaborationState(); void QueryRunningLockListsInner(std::map& runningLockLists); static void RegisterSettingWakeupPickupGestureObserver(); static void WakeupPickupGestureSettingUpdateFunc(const std::string& key); static void RegisterSettingWakeupDoubleClickObservers(); static void WakeupDoubleClickSettingUpdateFunc(const std::string& key); static bool GetSettingWakeupDoubleClick(const std::string& key = SETTING_POWER_WAKEUP_DOUBLE_KEY); static void RegisterSettingPowerModeObservers(); static void PowerModeSettingUpdateFunc(const std::string& key); static void RegisterSettingWakeUpLidObserver(); static void WakeupLidSettingUpdateFunc(const std::string& key); #ifdef POWER_MANAGER_WAKEUP_ACTION void WakeupActionControllerInit(); #endif void VibratorInit(); void Reset(); void KeepScreenOnInit(); void KeepScreenOn(bool isOpenOn); void UnregisterAllSettingObserver(); void RegisterAllSettingObserver(); int64_t GetSettingDisplayOffTime(int64_t defaultTime); #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING PowerConnectStatus GetPowerConnectStatus() const { return powerConnectStatus_; } void SetPowerConnectStatus(PowerConnectStatus status) { powerConnectStatus_ = status; } void PowerConnectStatusInit(); bool IsPowerConnected(); void UpdateSettingInvalidDisplayOffTime(); #endif std::shared_ptr GetRunningLockMgr() const { return runningLockMgr_; } std::shared_ptr GetPowerStateMachine() const { return powerStateMachine_; } std::shared_ptr GetPowerMgrNotify() const { return powerMgrNotify_; } std::shared_ptr GetSuspendController() const { return suspendController_; } #ifdef POWER_MANAGER_POWER_ENABLE_S4 std::shared_ptr GetHibernateController() const { return hibernateController_; } #endif std::shared_ptr GetWakeupController() const { return wakeupController_; } std::shared_ptr GetScreenOffPreController() const { return screenOffPreController_; } #ifdef POWER_MANAGER_WAKEUP_ACTION std::shared_ptr GetWakeupActionController() const { return wakeupActionController_; } #endif ShutdownDialog& GetShutdownDialog() { return shutdownDialog_; } bool IsServiceReady() const { return ready_; } void SetDisplayOffTime(int64_t time) { powerStateMachine_->SetDisplayOffTime(time); } void SetSleepTime(int64_t time) { powerStateMachine_->SetSleepTime(time); } void EnableMock(IDeviceStateAction* powerState, IDeviceStateAction* shutdownState, IDevicePowerAction* powerAction, IRunningLockAction* lockAction) { POWER_HILOGI(LABEL_TEST, "Service EnableMock:%{public}d", mockCount_++); runningLockMgr_->EnableMock(lockAction); powerStateMachine_->EnableMock(powerState); shutdownController_->EnableMock(powerAction, shutdownState); } void EnableShutdownMock(ShutdownController* shutdownAction) { POWER_HILOGI(LABEL_TEST, "need to mock shutdownController"); std::unique_ptr mock(shutdownAction); shutdownController_.reset(); shutdownController_ = std::move(mock); } void MockProximity(uint32_t status) { POWER_HILOGI(LABEL_TEST, "MockProximity: fun is start"); runningLockMgr_->SetProximity(status); POWER_HILOGI(LABEL_TEST, "MockProximity: fun is end"); } void MockSystemWakeup() { PowerStateMachine::onWakeup(); } std::shared_ptr GetShutdownController() { return shutdownController_; } std::shared_ptr suspendController_ = nullptr; std::shared_ptr wakeupController_ = nullptr; #ifdef POWER_MANAGER_POWER_ENABLE_S4 std::shared_ptr hibernateController_ = nullptr; #endif std::shared_ptr screenOffPreController_ = nullptr; #ifdef POWER_MANAGER_WAKEUP_ACTION std::shared_ptr wakeupActionController_ = nullptr; #endif #ifdef HAS_SENSORS_SENSOR_PART static bool isInLidMode_; #endif private: class WakeupRunningLock { public: WakeupRunningLock(); ~WakeupRunningLock(); private: sptr token_; }; class InvokerDeathRecipient : public DeathRecipient { using CallbackType = std::function&)>; public: InvokerDeathRecipient(std::string interfaceName, CallbackType callback) : interfaceName_(interfaceName), callback_(callback) {}; virtual ~InvokerDeathRecipient() = default; virtual void OnRemoteDied(const wptr& remote) override; private: std::string interfaceName_; CallbackType callback_; }; static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000; static constexpr int32_t INIT_KEY_MONITOR_DELAY_MS = 1000; static constexpr int32_t HALL_REPORT_INTERVAL = 0; static constexpr uint32_t HALL_SAMPLING_RATE = 100000000; static constexpr const char* SETTING_POWER_WAKEUP_DOUBLE_KEY {"settings.power.wakeup_double_click"}; bool Init(); bool PowerStateMachineInit(); std::string GetBundleNameByUid(const int32_t uid); RunningLockParam FillRunningLockParam(const RunningLockInfo& info, const uint64_t lockid, int32_t timeOutMS = -1); static void RegisterBootCompletedCallback(); static bool IsDeveloperMode(); #ifdef MSDP_MOVEMENT_ENABLE void RegisterMovementCallback(); void UnRegisterMovementCallback(); void ResetMovementState(); #endif inline PowerModeModule& GetPowerModeModule() { return powerModeModule_; } #ifdef HAS_SENSORS_SENSOR_PART bool IsSupportSensor(SensorTypeId); static void HallSensorCallback(SensorEvent* event); SensorUser sensorUser_{}; #endif bool ready_ {false}; static std::atomic_bool isBootCompleted_; std::mutex wakeupMutex_; std::mutex suspendMutex_; #ifdef POWER_MANAGER_POWER_ENABLE_S4 std::mutex hibernateMutex_; #endif std::mutex stateMutex_; std::mutex shutdownMutex_; std::mutex modeMutex_; std::mutex screenOffPreMutex_; std::mutex screenMutex_; std::mutex dumpMutex_; std::mutex lockMutex_; std::shared_ptr runningLockMgr_; std::shared_ptr powerStateMachine_; std::shared_ptr powerMgrNotify_; std::shared_ptr shutdownController_; PowerModeModule powerModeModule_; ShutdownDialog shutdownDialog_; uint32_t mockCount_ {0}; int32_t switchId_ {0}; int32_t doubleClickId_ {0}; int32_t monitorId_ {0}; int32_t inputMonitorId_ {-1}; sptr ptoken_; #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING PowerConnectStatus powerConnectStatus_ {PowerConnectStatus::POWER_CONNECT_INVALID}; #endif void SubscribeCommonEvent(); std::shared_ptr subscriberPtr_; }; #ifdef HAS_MULTIMODALINPUT_INPUT_PART class PowerMgrInputMonitor : public IInputEventConsumer { public: virtual void OnInputEvent(std::shared_ptr keyEvent) const; virtual void OnInputEvent(std::shared_ptr pointerEvent) const; virtual void OnInputEvent(std::shared_ptr axisEvent) const; }; #endif class PowerCommonEventSubscriber : public EventFwk::CommonEventSubscriber { public: explicit PowerCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscribeInfo) : EventFwk::CommonEventSubscriber(subscribeInfo) {} virtual ~PowerCommonEventSubscriber() {} void OnReceiveEvent(const EventFwk::CommonEventData &data) override; private: #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING void OnPowerConnectStatusChanged(PowerConnectStatus status); #endif }; } // namespace PowerMgr } // namespace OHOS #endif // POWERMGR_POWER_MGR_SERVICE_H