• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import { AsyncCallback, BusinessError } from './@ohos.base';
17
18/**
19 * Provides a mechanism to prevent the system from hibernating so that the applications can run in the background or
20 * when the screen is off.
21 * <p>{@link create} can be called to obtain a {@link RunningLock}.
22 * <p>{@link hold} can be called to set the lock duration, during which the system will not hibernate. After the
23 * lock duration times out, the lock is automatically released and the system hibernates if no other
24 * {@link RunningLock} is set.
25 *
26 * @namespace runningLock
27 * @syscap SystemCapability.PowerManager.PowerManager.Core
28 * @since 7
29 */
30declare namespace runningLock {
31  class RunningLock {
32    /**
33     * Prevents the system from hibernating and sets the lock duration.
34     * This method requires the ohos.permission.RUNNING_LOCK permission.
35     *
36     * @permission ohos.permission.RUNNING_LOCK
37     * @param { number } timeout Indicates the lock duration (ms). After the lock duration times out, the lock is automatically
38     * released and the system hibernates if no other {@link RunningLock} is set.
39     * @syscap SystemCapability.PowerManager.PowerManager.Core
40     * @since 7
41     * @deprecated since 9
42     * @useinstead RunningLock#hold
43     */
44    lock(timeout: number): void;
45
46    /**
47     * Prevents the system from hibernating and sets the lock duration.
48     * This method requires the ohos.permission.RUNNING_LOCK permission.
49     *
50     * @permission ohos.permission.RUNNING_LOCK
51     * @param { number } timeout Indicates the lock duration (ms). After the lock duration times out,
52     * the lock is automatically released and the system hibernates if no other {@link RunningLock} is set.
53     * @throws { BusinessError } 401 - If the timeout is not valid.
54     * @throws { BusinessError } 4900101 - If connecting to the service failed.
55     * @syscap SystemCapability.PowerManager.PowerManager.Core
56     * @since 9
57     */
58    hold(timeout: number): void;
59
60    /**
61     * Checks whether a lock is held or in use.
62     *
63     * @returns { boolean } Returns true if the lock is held or in use; returns false if the lock has been released.
64     * @syscap SystemCapability.PowerManager.PowerManager.Core
65     * @since 7
66     * @deprecated since 9
67     * @useinstead RunningLock#isHolding
68     */
69    isUsed(): boolean;
70
71    /**
72     * Checks whether a lock is held or in use.
73     *
74     * @returns { boolean } Returns true if the lock is held or in use; returns false if the lock has been released.
75     * @throws { BusinessError } 4900101 - If connecting to the service failed.
76     * @syscap SystemCapability.PowerManager.PowerManager.Core
77     * @since 9
78     */
79    isHolding(): boolean;
80
81    /**
82     * Release the {@link RunningLock} that prevents the system from hibernating.
83     * This method requires the ohos.permission.RUNNING_LOCK permission.
84     *
85     * @permission ohos.permission.RUNNING_LOCK
86     * @syscap SystemCapability.PowerManager.PowerManager.Core
87     * @since 7
88     * @deprecated since 9
89     * @useinstead RunningLock#unhold
90     */
91    unlock(): void;
92
93    /**
94     * Release the {@link RunningLock} that prevents the system from hibernating.
95     * This method requires the ohos.permission.RUNNING_LOCK permission.
96     *
97     * @permission ohos.permission.RUNNING_LOCK
98     * @throws { BusinessError } 4900101 - If connecting to the service failed.
99     * @syscap SystemCapability.PowerManager.PowerManager.Core
100     * @since 9
101     */
102    unhold(): void;
103  }
104
105  /**
106   * Enumerates the {@link RunningLock} types.
107   * <p>Two {@link RunningLock} types are available: {@link BACKGROUND}, and {@link PROXIMITY_SCREEN_CONTROL}.
108   * {@link BACKGROUND} ensures that applications can run in the background.
109   * {@link PROXIMITY_SCREEN_CONTROL} determines whether to turn on or off the screen based on the proximity sensor.
110   *
111   * @enum { number }
112   * @syscap SystemCapability.PowerManager.PowerManager.Core
113   * @since 7
114   */
115  export enum RunningLockType {
116    /**
117     * Indicates the lock that prevents the system from hibernating.
118     *
119     * @syscap SystemCapability.PowerManager.PowerManager.Core
120     * @since 7
121     * @deprecated since 10
122     */
123    BACKGROUND = 1,
124    /**
125     * Indicates the lock that determines whether to turn on or off the screen based on the proximity sensor.
126     * For example, during a call, if the proximity sensor detects that the device is moving close to
127     * the user's ear, the screen turns off; if the proximity sensor detects that the device is moving away
128     * from the user's ear, the screen turns on.
129     *
130     * @syscap SystemCapability.PowerManager.PowerManager.Core
131     * @since 7
132     */
133    PROXIMITY_SCREEN_CONTROL
134  }
135
136  /**
137   * Checks whether the specified {@link RunningLockType} is supported.
138   *
139   * @param { RunningLockType } type Indicates the specified {@link RunningLockType}.
140   * @param { AsyncCallback<boolean> } callback Indicates the callback function contains the result whether the specified
141   * {@link RunningLockType} is supported.
142   * @syscap SystemCapability.PowerManager.PowerManager.Core
143   * @since 7
144   * @deprecated since 9
145   * @useinstead RunningLock#isSupported
146   */
147  function isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback<boolean>): void;
148
149  /**
150   * Checks whether the specified {@link RunningLockType} is supported.
151   *
152   * @param { RunningLockType } type Indicates the specified {@link RunningLockType}.
153   * @returns { Promise<boolean> } Returns true if the specified {@link RunningLockType} is supported;
154   * returns false otherwise.
155   * @syscap SystemCapability.PowerManager.PowerManager.Core
156   * @since 7
157   * @deprecated since 9
158   * @useinstead RunningLock#isSupported
159   */
160  function isRunningLockTypeSupported(type: RunningLockType): Promise<boolean>;
161
162  /**
163   * Checks whether the specified {@link RunningLockType} is supported.
164   *
165   * @param { RunningLockType } type Indicates the specified {@link RunningLockType}.
166   * @returns { boolean } Whether the specified {@link RunningLockType} is supported.
167   * @throws { BusinessError } 401 - If the type is not valid.
168   * @throws { BusinessError } 4900101 - If connecting to the service failed.
169   * @syscap SystemCapability.PowerManager.PowerManager.Core
170   * @since 9
171   */
172  function isSupported(type: RunningLockType): boolean;
173
174  /**
175   * Creates a {@link RunningLock} object.
176   * <p>This method requires the ohos.permission.RUNNING_LOCK permission.
177   * <p>The {@link RunningLock} object can be used to perform a lock operation to prevent the system from hibernating.
178   *
179   * @permission ohos.permission.RUNNING_LOCK
180   * @param { string } name Indicates the {@link RunningLock} name. A recommended name consists of the package or class name and
181   * a suffix.
182   * @param { RunningLockType } type Indicates the {@link RunningLockType}.
183   * @param { AsyncCallback<RunningLock> } callback Indicates the callback contains the {@link RunningLock} object.
184   * @syscap SystemCapability.PowerManager.PowerManager.Core
185   * @since 7
186   * @deprecated since 9
187   * @useinstead RunningLock#create
188   */
189  function createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void;
190
191  /**
192   * Creates a {@link RunningLock} object.
193   * <p>This method requires the ohos.permission.RUNNING_LOCK permission.
194   * <p>The {@link RunningLock} object can be used to perform a lock operation to prevent the system from hibernating.
195   *
196   * @permission ohos.permission.RUNNING_LOCK
197   * @param { string } name Indicates the {@link RunningLock} name. A recommended name consists of the package or class name and
198   * a suffix.
199   * @param { RunningLockType } type Indicates the {@link RunningLockType}.
200   * @returns { Promise<RunningLock> } Returns the {@link RunningLock} object.
201   * @syscap SystemCapability.PowerManager.PowerManager.Core
202   * @since 7
203   * @deprecated since 9
204   * @useinstead RunningLock#create
205   */
206  function createRunningLock(name: string, type: RunningLockType): Promise<RunningLock>;
207
208  /**
209   * Creates a {@link RunningLock} object.
210   * <p>This method requires the ohos.permission.RUNNING_LOCK permission.
211   * <p>The {@link RunningLock} object can be used to perform a lock operation to prevent the system from hibernating.
212   *
213   * @permission ohos.permission.RUNNING_LOCK
214   * @param { string } name Indicates the {@link RunningLock} name. A recommended name consists of the package or
215   * class name and a suffix.
216   * @param { RunningLockType } type Indicates the {@link RunningLockType}.
217   * @param { AsyncCallback<RunningLock> } callback Indicates the callback of {@link RunningLock} object.
218   * @throws { BusinessError } 401 - If the name, type or callback is not valid.
219   * @syscap SystemCapability.PowerManager.PowerManager.Core
220   * @since 9
221   */
222  function create(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void;
223
224  /**
225   * Creates a {@link RunningLock} object.
226   * <p>This method requires the ohos.permission.RUNNING_LOCK permission.
227   * <p>The {@link RunningLock} object can be used to perform a lock operation to prevent the system from hibernating.
228   *
229   * @permission ohos.permission.RUNNING_LOCK
230   * @param { string } name Indicates the {@link RunningLock} name. A recommended name consists of the package or
231   * class name and a suffix.
232   * @param { RunningLockType } type Indicates the {@link RunningLockType}.
233   * @returns { Promise<RunningLock> } The {@link RunningLock} object.
234   * @throws { BusinessError } 401 - If the name or type is not valid.
235   * @syscap SystemCapability.PowerManager.PowerManager.Core
236   * @since 9
237   */
238  function create(name: string, type: RunningLockType): Promise<RunningLock>;
239}
240export default runningLock;
241