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