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