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} 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 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.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 * @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 * @permission ohos.permission.RUNNING_LOCK 39 * @since 7 40 */ 41 lock(timeout: number): void; 42 43 /** 44 * Checks whether a lock is held or in use. 45 * 46 * @return Returns true if the lock is held or in use; returns false if the lock has been released. 47 * @since 7 48 */ 49 isUsed(): boolean; 50 51 /** 52 * Release the {@link RunningLock} that prevents the system from hibernating. 53 * This method requires the ohos.permission.RUNNING_LOCK permission. 54 * 55 * @since 7 56 * @permission ohos.permission.RUNNING_LOCK 57 */ 58 unlock(): void; 59 } 60 61 /** 62 * Enumerates the {@link RunningLock} types. 63 * 64 * <p>Two {@link RunningLock} types are available: {@link BACKGROUND}, and {@link PROXIMITY_SCREEN_CONTROL}. 65 * {@link BACKGROUND} ensures that applications can run in the background. 66 * {@link PROXIMITY_SCREEN_CONTROL} determines whether to turn on or off the screen based on the proximity sensor. 67 * 68 * @since 7 69 */ 70 export enum RunningLockType { 71 /** 72 * Indicates the lock that prevents the system from hibernating. 73 */ 74 BACKGROUND = 1, 75 /** 76 * Indicates the lock that determines whether to turn on or off the screen based on the proximity sensor. 77 * For example, during a call, if the proximity sensor detects that the device is moving close to 78 * the user's ear, the screen turns off; if the proximity sensor detects that the device is moving away 79 * from the user's ear, the screen turns on. 80 */ 81 PROXIMITY_SCREEN_CONTROL 82 } 83 84 /** 85 * Checks whether the specified {@link RunningLockType} is supported. 86 * 87 * @param type Indicates the specified {@link RunningLockType}. 88 * @return Returns true if the specified {@link RunningLockType} is supported; 89 * returns false otherwise. 90 * @since 7 91 */ 92 function isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback<boolean>): void; 93 function isRunningLockTypeSupported(type: RunningLockType): Promise<boolean>; 94 /** 95 * Creates a {@link RunningLock} object. 96 * 97 * <p>This method requires the ohos.permission.RUNNING_LOCK permission. 98 * 99 * <p>The {@link RunningLock} object can be used to perform a lock operation to prevent the system from hibernating. 100 * 101 * @param name Indicates the {@link RunningLock} name. A recommended name consists of the package or class name and 102 * a suffix. 103 * @param type Indicates the {@link RunningLockType}. 104 * @return Returns the {@link RunningLock} object. 105 * @permission ohos.permission.RUNNING_LOCK 106 * @since 7 107 */ 108 function createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void; 109 function createRunningLock(name: string, type: RunningLockType): Promise<RunningLock>; 110} 111export default runningLock; 112