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