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