1 /* 2 * Copyright (c) 2021 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 <iremote_object.h> 20 #include <system_ability.h> 21 22 #include "actions/idevice_power_action.h" 23 #include "devicestatus_agent.h" 24 #include "devicestatus_data_utils.h" 25 #include "ipower_mgr.h" 26 #include "powerms_event_handler.h" 27 #include "power_mgr_notify.h" 28 #include "power_mgr_stub.h" 29 #include "power_state_machine.h" 30 #include "running_lock_mgr.h" 31 #include "shutdown_service.h" 32 #include "sp_singleton.h" 33 #include "power_mode_module.h" 34 #include "power_save_mode.h" 35 36 #define APP_FIRST_UID_VALUE 10000 37 38 namespace OHOS { 39 namespace PowerMgr { 40 class RunningLockMgr; 41 class PowerMgrService final : public SystemAbility, public PowerMgrStub { 42 DECLARE_SYSTEM_ABILITY(PowerMgrService) 43 DECLARE_DELAYED_SP_SINGLETON(PowerMgrService); 44 45 public: 46 virtual void OnStart() override; 47 virtual void OnStop() override; 48 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 49 virtual void RebootDevice(const std::string& reason) override; 50 virtual void ShutDownDevice(const std::string& reason) override; 51 virtual void SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, 52 bool suspendImmed) override; 53 virtual void WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, 54 const std::string& details) override; 55 virtual void RefreshActivity(int64_t callTimeMs, UserActivityType type, 56 bool needChangeBacklight) override; 57 virtual PowerState GetState() override; 58 virtual bool IsScreenOn() override; 59 virtual bool ForceSuspendDevice(int64_t callTimeMs) override; 60 virtual void CreateRunningLock(const sptr<IRemoteObject>& token, 61 const RunningLockInfo& runningLockInfo) override; 62 virtual void ReleaseRunningLock(const sptr<IRemoteObject>& token) override; 63 virtual bool IsRunningLockTypeSupported(uint32_t type) override; 64 virtual void Lock(const sptr<IRemoteObject>& token, 65 const RunningLockInfo& runningLockInfo, uint32_t timeOutMS) override; 66 virtual void UnLock(const sptr<IRemoteObject>& token) override; 67 virtual void ForceUnLock(const sptr<IRemoteObject>& token); 68 virtual bool IsUsed(const sptr<IRemoteObject>& token) override; 69 virtual void SetWorkTriggerList(const sptr<IRemoteObject>& token, 70 const WorkTriggerList& workTriggerList) override; 71 virtual void ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) override; 72 virtual void RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override; 73 virtual void UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override; 74 virtual void RegisterShutdownCallback(IShutdownCallback::ShutdownPriority priority, 75 const sptr<IShutdownCallback>& callback) override; 76 virtual void UnRegisterShutdownCallback(const sptr<IShutdownCallback>& callback) override; 77 virtual void RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override; 78 virtual void UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override; 79 virtual void SetDisplaySuspend(bool enable) override; 80 virtual void SetDeviceMode(const uint32_t& mode) override; 81 virtual uint32_t GetDeviceMode() override; 82 virtual std::string ShellDump(const std::vector<std::string>& args, uint32_t argc) override; 83 84 void HandleShutdownRequest(); 85 void HandleKeyEvent(int32_t keyCode); 86 void HandlePointEvent(int32_t type); 87 void NotifyDisplayActionDone(uint32_t event); 88 void KeyMonitorInit(); 89 void KeyMonitorCancel(); 90 void DeviceStatusMonitorInit(); GetHandler()91 std::shared_ptr<PowermsEventHandler> GetHandler() const 92 { 93 return handler_; 94 } 95 GetRunningLockMgr()96 std::shared_ptr<RunningLockMgr> GetRunningLockMgr() const 97 { 98 return runningLockMgr_; 99 } 100 GetPowerStateMachine()101 std::shared_ptr<PowerStateMachine> GetPowerStateMachine() const 102 { 103 return powerStateMachine_; 104 } 105 GetPowerMgrNotify()106 std::shared_ptr<PowerMgrNotify> GetPowerMgrNotify() const 107 { 108 return powerMgrNotify_; 109 } 110 IsServiceReady()111 bool IsServiceReady() const 112 { 113 return ready_; 114 } SetDisplayOffTime(int64_t time)115 void SetDisplayOffTime(int64_t time) 116 { 117 powerStateMachine_->SetDisplayOffTime(time); 118 } SetSleepTime(int64_t time)119 void SetSleepTime(int64_t time) 120 { 121 powerStateMachine_->SetSleepTime(time); 122 } 123 void HandlePowerKeyTimeout(); 124 EnableMock(IDeviceStateAction * stateAction,IDevicePowerAction * powerAction,IRunningLockAction * lockAction)125 void EnableMock(IDeviceStateAction* stateAction, IDevicePowerAction* powerAction, 126 IRunningLockAction* lockAction) 127 { 128 POWER_HILOGE(MODULE_SERVICE, "Service EnableMock:%{public}d", mockCount_++); 129 runningLockMgr_->EnableMock(lockAction); 130 powerStateMachine_->EnableMock(stateAction); 131 shutdownService_.EnableMock(powerAction); 132 } MockProximity(uint32_t status)133 void MockProximity(uint32_t status) 134 { 135 POWER_HILOGE(MODULE_SERVICE, "MockProximity: fun is start"); 136 runningLockMgr_->SetProximity(status); 137 POWER_HILOGE(MODULE_SERVICE, "MockProximity: fun is end"); 138 } MockSystemWakeup()139 void MockSystemWakeup() 140 { 141 PowerStateMachine::onWakeup(); 142 } 143 private: 144 static constexpr int32_t LONG_PRESS_DELAY_MS = 3000; 145 static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000; 146 static constexpr int32_t INIT_KEY_MONITOR_DELAY_MS = 1000; 147 static constexpr int32_t WATCH_DOG_DELAY_MS = 10000; 148 bool Init(); 149 bool PowerStateMachineInit(); 150 void HandlePowerKeyUp(); 151 void NotifyRunningLockChanged(bool isUnLock); 152 void FillUserIPCInfo(UserIPCInfo &userIPCinfo); 153 void GetDisplayPosition(int32_t& offsetX, int32_t& offsetY, int32_t& width, int32_t& height, bool& wideScreen); 154 bool ready_ {false}; 155 std::mutex mutex_; 156 std::mutex lockMutex_; 157 std::shared_ptr<RunningLockMgr> runningLockMgr_; 158 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_; 159 std::shared_ptr<PowermsEventHandler> handler_; 160 std::shared_ptr<PowerStateMachine> powerStateMachine_; 161 std::shared_ptr<PowerMgrNotify> powerMgrNotify_; 162 std::shared_ptr<Msdp::DeviceStatusAgent> deviceStatusAgent_; 163 ShutdownService shutdownService_; 164 PowerModeModule powerModeModule_; 165 bool powerkeyPressed_ {false}; 166 uint32_t mockCount_ {0}; 167 int32_t dialogId_ {-1}; 168 int32_t powerkeyLongPressId_ {0}; 169 int32_t powerkeyShortPressId_ {0}; 170 int32_t powerkeyReleaseId_ {0}; 171 int32_t doubleClickId_ {0}; 172 int32_t monitorId_ {0}; 173 }; 174 } // namespace PowerMgr 175 } // namespace OHOS 176 #endif // POWERMGR_POWER_MGR_SERVICE_H 177