161847f8eSopenharmony_ci/*
261847f8eSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
361847f8eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
461847f8eSopenharmony_ci * you may not use this file except in compliance with the License.
561847f8eSopenharmony_ci * You may obtain a copy of the License at
661847f8eSopenharmony_ci *
761847f8eSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
861847f8eSopenharmony_ci *
961847f8eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1061847f8eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1161847f8eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1261847f8eSopenharmony_ci * See the License for the specific language governing permissions and
1361847f8eSopenharmony_ci * limitations under the License.
1461847f8eSopenharmony_ci */
1561847f8eSopenharmony_ci
1661847f8eSopenharmony_ci/**
1761847f8eSopenharmony_ci * @file
1861847f8eSopenharmony_ci * @kit BasicServicesKit
1961847f8eSopenharmony_ci */
2061847f8eSopenharmony_ci
2161847f8eSopenharmony_ciimport { AsyncCallback, BusinessError } from './@ohos.base';
2261847f8eSopenharmony_ci
2361847f8eSopenharmony_ci/**
2461847f8eSopenharmony_ci * Provides a mechanism to prevent the system from hibernating so that the applications can run in the background or
2561847f8eSopenharmony_ci * when the screen is off.
2661847f8eSopenharmony_ci * <p>{@link create} can be called to obtain a {@link RunningLock}.
2761847f8eSopenharmony_ci * <p>{@link hold} can be called to set the lock duration, during which the system will not hibernate. After the
2861847f8eSopenharmony_ci * lock duration times out, the lock is automatically released and the system hibernates if no other
2961847f8eSopenharmony_ci * {@link RunningLock} is set.
3061847f8eSopenharmony_ci *
3161847f8eSopenharmony_ci * @namespace runningLock
3261847f8eSopenharmony_ci * @syscap SystemCapability.PowerManager.PowerManager.Core
3361847f8eSopenharmony_ci * @since 7
3461847f8eSopenharmony_ci */
3561847f8eSopenharmony_cideclare namespace runningLock {
3661847f8eSopenharmony_ci
3761847f8eSopenharmony_ci  /**
3861847f8eSopenharmony_ci   * Provides a mechanism to prevent the system from hibernating so that the applications can run in the background or
3961847f8eSopenharmony_ci   * when the screen is off.
4061847f8eSopenharmony_ci   * @syscap SystemCapability.PowerManager.PowerManager.Core
4161847f8eSopenharmony_ci   * @since 7
4261847f8eSopenharmony_ci   */
4361847f8eSopenharmony_ci  class RunningLock {
4461847f8eSopenharmony_ci    /**
4561847f8eSopenharmony_ci     * Prevents the system from hibernating and sets the lock duration.
4661847f8eSopenharmony_ci     * This method requires the ohos.permission.RUNNING_LOCK permission.
4761847f8eSopenharmony_ci     *
4861847f8eSopenharmony_ci     * @permission ohos.permission.RUNNING_LOCK
4961847f8eSopenharmony_ci     * @param { number } timeout Indicates the lock duration (ms). After the lock duration times out, the lock is automatically
5061847f8eSopenharmony_ci     * released and the system hibernates if no other {@link RunningLock} is set.
5161847f8eSopenharmony_ci     * @syscap SystemCapability.PowerManager.PowerManager.Core
5261847f8eSopenharmony_ci     * @since 7
5361847f8eSopenharmony_ci     * @deprecated since 9
5461847f8eSopenharmony_ci     * @useinstead RunningLock#hold
5561847f8eSopenharmony_ci     */
5661847f8eSopenharmony_ci    lock(timeout: number): void;
5761847f8eSopenharmony_ci
5861847f8eSopenharmony_ci    /**
5961847f8eSopenharmony_ci     * Prevents the system from hibernating and sets the lock duration.
6061847f8eSopenharmony_ci     * This method requires the ohos.permission.RUNNING_LOCK permission.
6161847f8eSopenharmony_ci     *
6261847f8eSopenharmony_ci     * @permission ohos.permission.RUNNING_LOCK
6361847f8eSopenharmony_ci     * @param { number } timeout Indicates the lock duration (ms). After the lock duration times out,
6461847f8eSopenharmony_ci     * the lock is automatically released and the system hibernates if no other {@link RunningLock} is set.
6561847f8eSopenharmony_ci     * timeout parameter must be of type number.
6661847f8eSopenharmony_ci     * @throws { BusinessError } 201 – If the permission is denied.
6761847f8eSopenharmony_ci     * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Incorrect parameter types;
6861847f8eSopenharmony_ci     * @throws { BusinessError } 4900101 - Failed to connect to the service.
6961847f8eSopenharmony_ci     * @syscap SystemCapability.PowerManager.PowerManager.Core
7061847f8eSopenharmony_ci     * @since 9
7161847f8eSopenharmony_ci     */
7261847f8eSopenharmony_ci    hold(timeout: number): void;
7361847f8eSopenharmony_ci
7461847f8eSopenharmony_ci    /**
7561847f8eSopenharmony_ci     * Checks whether a lock is held or in use.
7661847f8eSopenharmony_ci     *
7761847f8eSopenharmony_ci     * @returns { boolean } Returns true if the lock is held or in use; returns false if the lock has been released.
7861847f8eSopenharmony_ci     * @syscap SystemCapability.PowerManager.PowerManager.Core
7961847f8eSopenharmony_ci     * @since 7
8061847f8eSopenharmony_ci     * @deprecated since 9
8161847f8eSopenharmony_ci     * @useinstead RunningLock#isHolding
8261847f8eSopenharmony_ci     */
8361847f8eSopenharmony_ci    isUsed(): boolean;
8461847f8eSopenharmony_ci
8561847f8eSopenharmony_ci    /**
8661847f8eSopenharmony_ci     * Checks whether a lock is held or in use.
8761847f8eSopenharmony_ci     *
8861847f8eSopenharmony_ci     * @returns { boolean } Returns true if the lock is held or in use; returns false if the lock has been released.
8961847f8eSopenharmony_ci     * @throws { BusinessError } 4900101 - Failed to connect to the service.
9061847f8eSopenharmony_ci     * @syscap SystemCapability.PowerManager.PowerManager.Core
9161847f8eSopenharmony_ci     * @since 9
9261847f8eSopenharmony_ci     */
9361847f8eSopenharmony_ci    isHolding(): boolean;
9461847f8eSopenharmony_ci
9561847f8eSopenharmony_ci    /**
9661847f8eSopenharmony_ci     * Release the {@link RunningLock} that prevents the system from hibernating.
9761847f8eSopenharmony_ci     * This method requires the ohos.permission.RUNNING_LOCK permission.
9861847f8eSopenharmony_ci     *
9961847f8eSopenharmony_ci     * @permission ohos.permission.RUNNING_LOCK
10061847f8eSopenharmony_ci     * @syscap SystemCapability.PowerManager.PowerManager.Core
10161847f8eSopenharmony_ci     * @since 7
10261847f8eSopenharmony_ci     * @deprecated since 9
10361847f8eSopenharmony_ci     * @useinstead RunningLock#unhold
10461847f8eSopenharmony_ci     */
10561847f8eSopenharmony_ci    unlock(): void;
10661847f8eSopenharmony_ci
10761847f8eSopenharmony_ci    /**
10861847f8eSopenharmony_ci     * Release the {@link RunningLock} that prevents the system from hibernating.
10961847f8eSopenharmony_ci     * This method requires the ohos.permission.RUNNING_LOCK permission.
11061847f8eSopenharmony_ci     *
11161847f8eSopenharmony_ci     * @permission ohos.permission.RUNNING_LOCK
11261847f8eSopenharmony_ci     * @throws { BusinessError } 201 – If the permission is denied.
11361847f8eSopenharmony_ci     * @throws { BusinessError } 4900101 - Failed to connect to the service.
11461847f8eSopenharmony_ci     * @syscap SystemCapability.PowerManager.PowerManager.Core
11561847f8eSopenharmony_ci     * @since 9
11661847f8eSopenharmony_ci     */
11761847f8eSopenharmony_ci    unhold(): void;
11861847f8eSopenharmony_ci  }
11961847f8eSopenharmony_ci
12061847f8eSopenharmony_ci  /**
12161847f8eSopenharmony_ci   * Enumerates the {@link RunningLock} types.
12261847f8eSopenharmony_ci   * <p>Two {@link RunningLock} types are available: {@link BACKGROUND}, and {@link PROXIMITY_SCREEN_CONTROL}.
12361847f8eSopenharmony_ci   * {@link BACKGROUND} ensures that applications can run in the background.
12461847f8eSopenharmony_ci   * {@link PROXIMITY_SCREEN_CONTROL} determines whether to turn on or off the screen based on the proximity sensor.
12561847f8eSopenharmony_ci   *
12661847f8eSopenharmony_ci   * @enum { number }
12761847f8eSopenharmony_ci   * @syscap SystemCapability.PowerManager.PowerManager.Core
12861847f8eSopenharmony_ci   * @since 7
12961847f8eSopenharmony_ci   */
13061847f8eSopenharmony_ci  export enum RunningLockType {
13161847f8eSopenharmony_ci    /**
13261847f8eSopenharmony_ci     * Indicates the lock that prevents the system from hibernating.
13361847f8eSopenharmony_ci     *
13461847f8eSopenharmony_ci     * @syscap SystemCapability.PowerManager.PowerManager.Core
13561847f8eSopenharmony_ci     * @since 7
13661847f8eSopenharmony_ci     * @deprecated since 10
13761847f8eSopenharmony_ci     */
13861847f8eSopenharmony_ci    BACKGROUND = 1,
13961847f8eSopenharmony_ci    /**
14061847f8eSopenharmony_ci     * Indicates the lock that determines whether to turn on or off the screen based on the proximity sensor.
14161847f8eSopenharmony_ci     * For example, during a call, if the proximity sensor detects that the device is moving close to
14261847f8eSopenharmony_ci     * the user's ear, the screen turns off; if the proximity sensor detects that the device is moving away
14361847f8eSopenharmony_ci     * from the user's ear, the screen turns on.
14461847f8eSopenharmony_ci     *
14561847f8eSopenharmony_ci     * @syscap SystemCapability.PowerManager.PowerManager.Core
14661847f8eSopenharmony_ci     * @since 7
14761847f8eSopenharmony_ci     */
14861847f8eSopenharmony_ci    PROXIMITY_SCREEN_CONTROL
14961847f8eSopenharmony_ci  }
15061847f8eSopenharmony_ci
15161847f8eSopenharmony_ci  /**
15261847f8eSopenharmony_ci   * Checks whether the specified {@link RunningLockType} is supported.
15361847f8eSopenharmony_ci   *
15461847f8eSopenharmony_ci   * @param { RunningLockType } type Indicates the specified {@link RunningLockType}.
15561847f8eSopenharmony_ci   * @param { AsyncCallback<boolean> } callback Indicates the callback function contains the result whether the specified
15661847f8eSopenharmony_ci   * {@link RunningLockType} is supported.
15761847f8eSopenharmony_ci   * @syscap SystemCapability.PowerManager.PowerManager.Core
15861847f8eSopenharmony_ci   * @since 7
15961847f8eSopenharmony_ci   * @deprecated since 9
16061847f8eSopenharmony_ci   * @useinstead RunningLock#isSupported
16161847f8eSopenharmony_ci   */
16261847f8eSopenharmony_ci  function isRunningLockTypeSupported(type: RunningLockType, callback: AsyncCallback<boolean>): void;
16361847f8eSopenharmony_ci
16461847f8eSopenharmony_ci  /**
16561847f8eSopenharmony_ci   * Checks whether the specified {@link RunningLockType} is supported.
16661847f8eSopenharmony_ci   *
16761847f8eSopenharmony_ci   * @param { RunningLockType } type Indicates the specified {@link RunningLockType}.
16861847f8eSopenharmony_ci   * @returns { Promise<boolean> } Returns true if the specified {@link RunningLockType} is supported;
16961847f8eSopenharmony_ci   * returns false otherwise.
17061847f8eSopenharmony_ci   * @syscap SystemCapability.PowerManager.PowerManager.Core
17161847f8eSopenharmony_ci   * @since 7
17261847f8eSopenharmony_ci   * @deprecated since 9
17361847f8eSopenharmony_ci   * @useinstead RunningLock#isSupported
17461847f8eSopenharmony_ci   */
17561847f8eSopenharmony_ci  function isRunningLockTypeSupported(type: RunningLockType): Promise<boolean>;
17661847f8eSopenharmony_ci
17761847f8eSopenharmony_ci  /**
17861847f8eSopenharmony_ci   * Checks whether the specified {@link RunningLockType} is supported.
17961847f8eSopenharmony_ci   *
18061847f8eSopenharmony_ci   * @param { RunningLockType } type Indicates the specified {@link RunningLockType}.
18161847f8eSopenharmony_ci   * the RunningLockType type is an enumeration class.
18261847f8eSopenharmony_ci   * @returns { boolean } Whether the specified {@link RunningLockType} is supported.
18361847f8eSopenharmony_ci   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Incorrect parameter types;
18461847f8eSopenharmony_ci   * 2. Parameter verification failed.
18561847f8eSopenharmony_ci   * @throws { BusinessError } 4900101 - Failed to connect to the service.
18661847f8eSopenharmony_ci   * @syscap SystemCapability.PowerManager.PowerManager.Core
18761847f8eSopenharmony_ci   * @since 9
18861847f8eSopenharmony_ci   */
18961847f8eSopenharmony_ci  function isSupported(type: RunningLockType): boolean;
19061847f8eSopenharmony_ci
19161847f8eSopenharmony_ci  /**
19261847f8eSopenharmony_ci   * Creates a {@link RunningLock} object.
19361847f8eSopenharmony_ci   * <p>This method requires the ohos.permission.RUNNING_LOCK permission.
19461847f8eSopenharmony_ci   * <p>The {@link RunningLock} object can be used to perform a lock operation to prevent the system from hibernating.
19561847f8eSopenharmony_ci   *
19661847f8eSopenharmony_ci   * @permission ohos.permission.RUNNING_LOCK
19761847f8eSopenharmony_ci   * @param { string } name Indicates the {@link RunningLock} name. A recommended name consists of the package or class name and
19861847f8eSopenharmony_ci   * a suffix.
19961847f8eSopenharmony_ci   * @param { RunningLockType } type Indicates the {@link RunningLockType}.
20061847f8eSopenharmony_ci   * @param { AsyncCallback<RunningLock> } callback Indicates the callback contains the {@link RunningLock} object.
20161847f8eSopenharmony_ci   * @syscap SystemCapability.PowerManager.PowerManager.Core
20261847f8eSopenharmony_ci   * @since 7
20361847f8eSopenharmony_ci   * @deprecated since 9
20461847f8eSopenharmony_ci   * @useinstead RunningLock#create
20561847f8eSopenharmony_ci   */
20661847f8eSopenharmony_ci  function createRunningLock(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void;
20761847f8eSopenharmony_ci
20861847f8eSopenharmony_ci  /**
20961847f8eSopenharmony_ci   * Creates a {@link RunningLock} object.
21061847f8eSopenharmony_ci   * <p>This method requires the ohos.permission.RUNNING_LOCK permission.
21161847f8eSopenharmony_ci   * <p>The {@link RunningLock} object can be used to perform a lock operation to prevent the system from hibernating.
21261847f8eSopenharmony_ci   *
21361847f8eSopenharmony_ci   * @permission ohos.permission.RUNNING_LOCK
21461847f8eSopenharmony_ci   * @param { string } name Indicates the {@link RunningLock} name. A recommended name consists of the package or class name and
21561847f8eSopenharmony_ci   * a suffix.
21661847f8eSopenharmony_ci   * @param { RunningLockType } type Indicates the {@link RunningLockType}.
21761847f8eSopenharmony_ci   * @returns { Promise<RunningLock> } Returns the {@link RunningLock} object.
21861847f8eSopenharmony_ci   * @syscap SystemCapability.PowerManager.PowerManager.Core
21961847f8eSopenharmony_ci   * @since 7
22061847f8eSopenharmony_ci   * @deprecated since 9
22161847f8eSopenharmony_ci   * @useinstead RunningLock#create
22261847f8eSopenharmony_ci   */
22361847f8eSopenharmony_ci  function createRunningLock(name: string, type: RunningLockType): Promise<RunningLock>;
22461847f8eSopenharmony_ci
22561847f8eSopenharmony_ci  /**
22661847f8eSopenharmony_ci   * Creates a {@link RunningLock} object.
22761847f8eSopenharmony_ci   * <p>This method requires the ohos.permission.RUNNING_LOCK permission.
22861847f8eSopenharmony_ci   * <p>The {@link RunningLock} object can be used to perform a lock operation to prevent the system from hibernating.
22961847f8eSopenharmony_ci   *
23061847f8eSopenharmony_ci   * @permission ohos.permission.RUNNING_LOCK
23161847f8eSopenharmony_ci   * @param { string } name Indicates the {@link RunningLock} name. A recommended name consists of the package or
23261847f8eSopenharmony_ci   * class name and a suffix.
23361847f8eSopenharmony_ci   * name parameter must be of type string.
23461847f8eSopenharmony_ci   * @param { RunningLockType } type Indicates the {@link RunningLockType}.
23561847f8eSopenharmony_ci   * the RunningLockType type is an enumeration class.
23661847f8eSopenharmony_ci   * @param { AsyncCallback<RunningLock> } callback Indicates the callback of {@link RunningLock} object.
23761847f8eSopenharmony_ci   * AsyncCallback encapsulates a class of RunningLock type
23861847f8eSopenharmony_ci   * @throws { BusinessError } 201 – If the permission is denied.
23961847f8eSopenharmony_ci   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Parameter verification failed.
24061847f8eSopenharmony_ci   * @syscap SystemCapability.PowerManager.PowerManager.Core
24161847f8eSopenharmony_ci   * @since 9
24261847f8eSopenharmony_ci   */
24361847f8eSopenharmony_ci  function create(name: string, type: RunningLockType, callback: AsyncCallback<RunningLock>): void;
24461847f8eSopenharmony_ci
24561847f8eSopenharmony_ci  /**
24661847f8eSopenharmony_ci   * Creates a {@link RunningLock} object.
24761847f8eSopenharmony_ci   * <p>This method requires the ohos.permission.RUNNING_LOCK permission.
24861847f8eSopenharmony_ci   * <p>The {@link RunningLock} object can be used to perform a lock operation to prevent the system from hibernating.
24961847f8eSopenharmony_ci   *
25061847f8eSopenharmony_ci   * @permission ohos.permission.RUNNING_LOCK
25161847f8eSopenharmony_ci   * @param { string } name Indicates the {@link RunningLock} name. A recommended name consists of the package or
25261847f8eSopenharmony_ci   * class name and a suffix.
25361847f8eSopenharmony_ci   * name parameter must be of type string.
25461847f8eSopenharmony_ci   * @param { RunningLockType } type Indicates the {@link RunningLockType}.
25561847f8eSopenharmony_ci   * the RunningLockType type is an enumeration class.
25661847f8eSopenharmony_ci   * @returns { Promise<RunningLock> } The {@link RunningLock} object.
25761847f8eSopenharmony_ci   * @throws { BusinessError } 201 – If the permission is denied.
25861847f8eSopenharmony_ci   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Parameter verification failed.
25961847f8eSopenharmony_ci   * @syscap SystemCapability.PowerManager.PowerManager.Core
26061847f8eSopenharmony_ci   * @since 9
26161847f8eSopenharmony_ci   */
26261847f8eSopenharmony_ci  function create(name: string, type: RunningLockType): Promise<RunningLock>;
26361847f8eSopenharmony_ci}
26461847f8eSopenharmony_ciexport default runningLock;
265