/* * Copyright (c) 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_CLIENT_H #define POWERMGR_POWER_MGR_CLIENT_H #include #include #include "power_state_machine_info.h" #include "running_lock.h" namespace OHOS { namespace PowerMgr { class PowerMgrClient final { private: PowerMgrClient(); DISALLOW_COPY_AND_MOVE(PowerMgrClient); public: static PowerMgrClient& GetInstance(); virtual ~PowerMgrClient(); static const uint32_t CONNECT_RETRY_COUNT = 5; static const uint32_t CONNECT_RETRY_MS = 800000; /** * Reboot the device. * * @param reason The reason for rebooting the device. e.g.updater */ PowerErrors RebootDevice(const std::string& reason); PowerErrors RebootDeviceForDeprecated(const std::string& reason); /** * Shut down the device. * * @param reason The reason for shutting down the device. * */ PowerErrors ShutDownDevice(const std::string& reason); /** * @brief Set suspend tag before suspend. * The special sleep mode supported by the kernel and hardware is triggered by setting a special * suspend tag and then triggering suspend. If the suspend tag is not set, the standard S3 sleep * mode is triggered when suspend. * * @param tag Suspend tag. */ PowerErrors SetSuspendTag(const std::string& tag); /** * Suspend device and set screen off. * * @param reason The reason why will you suspend the device, such as timeout/powerkey/forcesuspend and so on. */ PowerErrors SuspendDevice(SuspendDeviceType reason = SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, bool suspendImmed = false); /** * Wake up the device and set the screen on. * * @param reason The reason for waking up the device, such as powerkey/plugin/application. */ PowerErrors WakeupDevice(WakeupDeviceType reason = WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, const std::string& detail = std::string("app call")); /** * Wake up the device and set the screen on async. * * @param reason The reason for waking up the device, such as powerkey/plugin/application. */ void WakeupDeviceAsync(WakeupDeviceType reason = WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, const std::string& detail = std::string("app call")); /** * Refresh the screentimeout time, and keep the screen on. RefreshActivity works only when the screen is on. * * @param type The RefreshActivity type, such as touch/button/accessibility and so on. * @param needChangeBacklight Whether to change the backlight state, for example, from DIM to BRIGHT. * Set it to false if you don't want to change the backlight state. */ bool RefreshActivity(UserActivityType type = UserActivityType::USER_ACTIVITY_TYPE_OTHER); /** * Windows overwrite timeout * @param timeout Specifies the timeout duration. */ PowerErrors OverrideScreenOffTime(int64_t timeout); /** * Windows restores timeout */ PowerErrors RestoreScreenOffTime(); /** * Check whether the device screen is on. The result may be true or false, depending on the system state. */ bool IsScreenOn(bool needPrintLog = true); /** * Check whether the fold device screen is on. The result may be true or false, depending on the system state. */ bool IsFoldScreenOn(); /** * Check whether the collaboration device screen is on. * The result may be true or false, depending on the system state. */ bool IsCollaborationScreenOn(); /** * Get Power state. The result is PowerState type. */ PowerState GetState(); /** * Forcibly suspend the device into deepsleep, and return the suspend result. */ PowerErrors ForceSuspendDevice(); /** * Check whether the type of running lock is supported */ bool IsRunningLockTypeSupported(RunningLockType type); /** * Enable/disable display suspend state */ bool SetDisplaySuspend(bool enable); /** * Hibernate the device. * @param clearMemory Indicates whether to clear the memory before the device hibernates. */ PowerErrors Hibernate(bool clearMemory); /* Set the device mode. * * @param set The mode the device. */ PowerErrors SetDeviceMode(const PowerMode mode); /** * Get the device mode. * * @param Get The mode the device. */ PowerMode GetDeviceMode(); /** * Check if the device has entered standby mode. */ PowerErrors IsStandby(bool& isStandby); /** * Query the list of lock information. */ bool QueryRunningLockLists(std::map& runningLockLists); PowerErrors SetForceTimingOut(bool enabled); PowerErrors LockScreenAfterTimingOut(bool enabledLockScreen, bool checkLock, bool sendScreenOffEvent = true); std::shared_ptr CreateRunningLock(const std::string& name, RunningLockType type); bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid); bool ProxyRunningLocks(bool isProxied, const std::vector>& processInfos); bool ResetRunningLocks(); bool RegisterPowerStateCallback(const sptr& callback, bool isSync = true); bool UnRegisterPowerStateCallback(const sptr& callback); bool RegisterSyncHibernateCallback(const sptr& callback); bool UnRegisterSyncHibernateCallback(const sptr& callback); bool RegisterSyncSleepCallback(const sptr& callback, SleepPriority priority); bool UnRegisterSyncSleepCallback(const sptr& callback); bool RegisterPowerModeCallback(const sptr& callback); bool UnRegisterPowerModeCallback(const sptr& callback); void RecoverRunningLocks(); bool RegisterScreenStateCallback(int32_t remainTime, const sptr& callback); bool UnRegisterScreenStateCallback(const sptr& callback); bool RegisterRunningLockCallback(const sptr& callback); bool UnRegisterRunningLockCallback(const sptr& callback); std::string Dump(const std::vector& args); PowerErrors GetError(); #ifndef POWERMGR_SERVICE_DEATH_UT private: #endif class PowerMgrDeathRecipient : public IRemoteObject::DeathRecipient { public: explicit PowerMgrDeathRecipient(PowerMgrClient& client) : client_(client) {} ~PowerMgrDeathRecipient() = default; void OnRemoteDied(const wptr& remote); private: DISALLOW_COPY_AND_MOVE(PowerMgrDeathRecipient); PowerMgrClient& client_; }; ErrCode Connect(); void ResetProxy(const wptr& remote); sptr proxy_ {nullptr}; sptr deathRecipient_ {nullptr}; std::mutex mutex_; static std::vector> runningLocks_; static std::mutex runningLocksMutex_; sptr token_ {nullptr}; PowerErrors error_ = PowerErrors::ERR_OK; }; } // namespace PowerMgr } // namespace OHOS #endif // POWERMGR_POWER_MGR_CLIENT_H