• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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(RunningLockType 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     /**
124      * Check if the device has entered standby mode.
125      */
126     PowerErrors IsStandby(bool& isStandby);
127 
128     std::shared_ptr<RunningLock> CreateRunningLock(const std::string& name, RunningLockType type);
129     bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid);
130     bool ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos);
131     bool ResetRunningLocks();
132     bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
133     bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
134     bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback);
135     bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback);
136     std::string Dump(const std::vector<std::string>& args);
137     PowerErrors GetError();
138 
139 #ifndef POWERMGR_SERVICE_DEATH_UT
140 private:
141 #endif
142     class PowerMgrDeathRecipient : public IRemoteObject::DeathRecipient {
143     public:
144         PowerMgrDeathRecipient() = default;
145         ~PowerMgrDeathRecipient() = default;
146         void OnRemoteDied(const wptr<IRemoteObject>& remote);
147 
148     private:
149         DISALLOW_COPY_AND_MOVE(PowerMgrDeathRecipient);
150     };
151 
152     ErrCode Connect();
153     sptr<IPowerMgr> proxy_ {nullptr};
154     sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
155     void ResetProxy(const wptr<IRemoteObject>& remote);
156     std::mutex mutex_;
157     PowerErrors error_ = PowerErrors::ERR_OK;
158 };
159 } // namespace PowerMgr
160 } // namespace OHOS
161 #endif // POWERMGR_POWER_MGR_CLIENT_H
162