• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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} from './basic.d.ts';
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 createRunningLock} can be called to obtain a {@link RunningLock}.
23 * <p>{@link lock} 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 {@link
25  * RunningLock} is set.
26 *
27 * @SysCap SystemCapability.PowerMgr.PowerManager
28 * @devices phone, tablet, tv, wearable
29 * @since 7
30 */
31declare namespace runningLock {
32    class RunningLock {
33        /**
34         * Prevents the system from hibernating and sets the lock duration.
35         *
36         * @param timeout Indicates the lock duration (ms). After the lock duration times out, the lock is automatically
37         * released and the system hibernates if no other {@link RunningLock} is set.
38         * @since 7
39         */
40        function lock(timeout: number): void;
41
42        /**
43         * Checks whether a lock is held or in use.
44         *
45         * @return Returns true if the lock is held or in use; returns false if the lock has been released.
46         * @since 7
47         */
48        function isUsed(): boolean;
49
50        /**
51         * Release the {@link RunningLock} that prevents the system from hibernating.
52         *
53         * @since 7
54         */
55        function unlock(): void;
56    }
57
58    /**
59     * Enumerates the {@link RunningLock} types.
60     *
61     * <p>Two {@link RunningLock} types are available: {@link BACKGROUND}, and {@link PROXIMITY_SCREEN_CONTROL}.
62     * {@link BACKGROUND} ensures that applications can run in the background.
63     * {@link PROXIMITY_SCREEN_CONTROL} determines whether to turn on or off the screen based on the proximity sensor.
64     *
65     * @since 7
66     */
67    export enum RunningLockType {
68        /**
69         * Indicates the lock that prevents the system from hibernating.
70         */
71        BACKGROUND = 1,
72        /**
73         * Indicates the lock that determines whether to turn on or off the screen based on the proximity sensor.
74         * For example, during a call, if the proximity sensor detects that the device is moving close to
75         * the user's ear, the screen turns off; if the proximity sensor detects that the device is moving away
76         * from the user's ear, the screen turns on.
77         */
78        PROXIMITY_SCREEN_CONTROL
79    }
80
81    /**
82     * Checks whether the specified {@link RunningLockType} is supported.
83     *
84     * @param type Indicates the specified {@link RunningLockType}.
85     * @return Returns true if the specified {@link RunningLockType} is supported;
86     * returns false otherwise.
87     * @since 7
88     */
89    function isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback<boolean>): void;
90    function isRunningLockTypeSupported(type: RunningLockType): Promise<boolean>;
91    /**
92     * Creates a {@link RunningLock} object.
93     *
94     * <p>This method requires the ohos.permission.RUNNING_LOCK permission.
95     *
96     * <p>The {@link RunningLock} object can be used to perform a lock operation to prevent the system from hibernating.
97     *
98     * @param name Indicates the {@link RunningLock} name. A recommended name consists of the package or class name and
99     * a suffix.
100     * @param type Indicates the {@link RunningLockType}.
101     * @return Returns the {@link RunningLock} object.
102     * @since 7
103     */
104    function createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void;
105    function createRunningLock(name: string, type: RunningLockType): Promise<RunningLock>;
106}
107export default runningLock;
108