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_CLIENT_H 17 #define POWERMGR_POWER_MGR_CLIENT_H 18 19 #include <string> 20 21 #include <singleton.h> 22 23 #include "power_state_machine_info.h" 24 #include "running_lock.h" 25 26 namespace OHOS { 27 namespace PowerMgr { 28 class PowerMgrClient final : public DelayedRefSingleton<PowerMgrClient> { 29 DECLARE_DELAYED_REF_SINGLETON(PowerMgrClient) 30 31 public: 32 DISALLOW_COPY_AND_MOVE(PowerMgrClient); 33 /** 34 * Reboot the device. 35 * 36 * @param reason The reason for rebooting the device. e.g.updater 37 */ 38 PowerErrors RebootDevice(const std::string& reason); 39 PowerErrors RebootDeviceForDeprecated(const std::string& reason); 40 41 /** 42 * Shut down the device. 43 * 44 * @param reason The reason for shutting down the device. 45 * 46 */ 47 PowerErrors ShutDownDevice(const std::string& reason); 48 49 /** 50 * Suspend device and set screen off. 51 * 52 * @param reason The reason why will you suspend the device, such as timeout/powerkey/forcesuspend and so on. 53 */ 54 PowerErrors SuspendDevice(SuspendDeviceType reason = SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, 55 bool suspendImmed = false); 56 57 /** 58 * Wake up the device and set the screen on. 59 * 60 * @param reason The reason for waking up the device, such as powerkey/plugin/application. 61 */ 62 PowerErrors WakeupDevice(WakeupDeviceType reason = WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, 63 const std::string& detail = std::string("app call")); 64 65 /** 66 * Refresh the screentimeout time, and keep the screen on. RefreshActivity works only when the screen is on. 67 * 68 * @param type The RefreshActivity type, such as touch/button/accessibility and so on. 69 * @param needChangeBacklight Whether to change the backlight state, for example, from DIM to BRIGHT. 70 * Set it to false if you don't want to change the backlight state. 71 */ 72 bool RefreshActivity(UserActivityType type = UserActivityType::USER_ACTIVITY_TYPE_OTHER); 73 74 /** 75 * Windows overwrite timeout 76 * @param timeout Specifies the timeout duration. 77 */ 78 bool OverrideScreenOffTime(int64_t timeout); 79 80 /** 81 * Windows restores timeout 82 */ 83 bool RestoreScreenOffTime(); 84 85 /** 86 * Check whether the device screen is on. The result may be true or false, depending on the system state. 87 */ 88 bool IsScreenOn(); 89 90 /** 91 * Get Power state. The result is PowerState type. 92 */ 93 PowerState GetState(); 94 95 /** 96 * Forcibly suspend the device into deepsleep, and return the suspend result. 97 */ 98 bool ForceSuspendDevice(); 99 100 /** 101 * Check whether the type of running lock is supported 102 */ 103 bool IsRunningLockTypeSupported(uint32_t type); 104 105 /** 106 * Enable/disable display suspend state 107 */ 108 bool SetDisplaySuspend(bool enable); 109 110 /* Set the device mode. 111 * 112 * @param set The mode the device. 113 */ 114 PowerErrors SetDeviceMode(const PowerMode mode); 115 116 /** 117 * Get the device mode. 118 * 119 * @param Get The mode the device. 120 */ 121 PowerMode GetDeviceMode(); 122 123 std::shared_ptr<RunningLock> CreateRunningLock(const std::string& name, RunningLockType type); 124 bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback); 125 bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback); 126 bool RegisterShutdownCallback(const sptr<IShutdownCallback>& callback, 127 IShutdownCallback::ShutdownPriority priority 128 = IShutdownCallback::ShutdownPriority::POWER_SHUTDOWN_PRIORITY_DEFAULT); 129 bool UnRegisterShutdownCallback(const sptr<IShutdownCallback>& callback); 130 bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback); 131 bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback); 132 std::string Dump(const std::vector<std::string>& args); 133 PowerErrors GetError(); 134 135 #ifndef POWERMGR_SERVICE_DEATH_UT 136 private: 137 #endif 138 class PowerMgrDeathRecipient : public IRemoteObject::DeathRecipient { 139 public: 140 PowerMgrDeathRecipient() = default; 141 ~PowerMgrDeathRecipient() = default; 142 void OnRemoteDied(const wptr<IRemoteObject>& remote); 143 144 private: 145 DISALLOW_COPY_AND_MOVE(PowerMgrDeathRecipient); 146 }; 147 148 ErrCode Connect(); 149 sptr<IPowerMgr> proxy_ {nullptr}; 150 sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr}; 151 void ResetProxy(const wptr<IRemoteObject>& remote); 152 std::mutex mutex_; 153 PowerErrors error_ = PowerErrors::ERR_OK; 154 }; 155 } // namespace PowerMgr 156 } // namespace OHOS 157 #endif // POWERMGR_POWER_MGR_CLIENT_H 158