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