This module provides APIs related to the peeping status. Apps can protect user privacy based on the peeping status. For example, they can display a system-level overlay to mask the window, disable personalized recommendations, and hide sensitive information such as browsing records, payment records, and favorites records when the phone is being viewed by someone else other than the owner.
Since: 6.0.0(20)
Enumerates the peeping statuses.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Security.DlpAntiPeep
Since: 6.0.0(20)
| Name | Value | Description |
|---|---|---|
| PASS | 0 | No one is peeping at the screen of the current device. |
| HIDE | 1 | Someone other than the owner is peeping at the screen of the device. |
isDlpAntiPeepSwitchOn(): Promise<boolean>
Checks whether screen privacy has been enabled for the current app. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
Required permission: ohos.permission.DLP_GET_HIDE_STATUS
System capability: SystemCapability.Security.DlpAntiPeep
Since: 6.0.0(20)
Returns:
| Type | Description |
|---|---|
| Promise<boolean> | Promise used to return the result, indicating whether screen privacy has been enabled for the current app. The value true indicates that the function is enabled, and false indicates that the function is disabled. The default value is false. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 801 | Capability not supported. Function isDlpAntiPeepSwitchOn can not work correctly due to limited device capabilities. |
| 1020600001 | Internal error. |
Example:
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
try {
let result = await dlpAntiPeep.isDlpAntiPeepSwitchOn();
console.info('Succeeded in getting switch status.');
} catch (error) {
console.error(`Failed to get switch status. Code: ${error.code}, message: ${error.message}`);
} on(type: 'dlpAntiPeep', callback: Callback<DlpAntiPeepStatus>): void
Subscribes to the screen privacy notification. This API uses an asynchronous callback to return the result.
Call this API after the development prerequisites are met, ensuring that the screen privacy callback can be triggered. The anti-peeping callback is received only after the API is successfully called and the app is visible in the foreground.
Model restriction: This API can be used only in the stage model.
Required permission: ohos.permission.DLP_GET_HIDE_STATUS
System capability: SystemCapability.Security.DlpAntiPeep
Since: 6.0.0(20)
Parameters:
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event to be subscribed to, which has a fixed value of 'dlpAntiPeep'. |
| callback | Callback<DlpAntiPeepStatus> | Yes | Callback used to return the peeping status. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 801 | Capability not supported. Function on can not work correctly due to limited device capabilities. |
| 1020600001 | Internal error. |
Example:
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
try {
dlpAntiPeep.on('dlpAntiPeep', (dlpAntiPeepStatus: dlpAntiPeep.DlpAntiPeepStatus) => {
console.info('Succeeded in subscribing dlpAntiPeep.');
});
} catch (error) {
console.error(`Failed to subscribe dlpAntiPeep. Code: ${error.code}, message: ${error.message}`);
} off(type: 'dlpAntiPeep', callback?: Callback<DlpAntiPeepStatus>): void
Unsubscribes from the screen privacy notification. This API uses an asynchronous callback to return the result.
Model restriction: This API can be used only in the stage model.
Required permission: ohos.permission.DLP_GET_HIDE_STATUS
System capability: SystemCapability.Security.DlpAntiPeep
Since: 6.0.0(20)
Parameters:
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event to be unsubscribed from, which has a fixed value of 'dlpAntiPeep'. |
| callback | Callback<DlpAntiPeepStatus> | No | Callback function for receiving screen privacy notifications. If a callback is passed, the subscription to this callback is canceled. If no callback is passed, subscriptions to all callbacks are canceled. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 801 | Capability not supported. Function off can not work correctly due to limited device capabilities. |
| 1020600001 | Internal error. |
Example:
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
try {
dlpAntiPeep.off('dlpAntiPeep', (dlpAntiPeepStatus: dlpAntiPeep.DlpAntiPeepStatus) => {
console.info('Succeeded in unsubscribing dlpAntiPeep.');
});
} catch (error) {
console.error(`Failed to unsubscribe dlpAntiPeep. Code: ${error.code}, message: ${error.message}`);
} getDlpAntiPeepInfo(): DlpAntiPeepStatus
Obtains the peeping status of the current app.
Model restriction: This API can be used only in the stage model.
Required permission: ohos.permission.DLP_GET_HIDE_STATUS
System capability: SystemCapability.Security.DlpAntiPeep
Since: 6.0.0(20)
Returns:
| Type | Description |
|---|---|
| DlpAntiPeepStatus | Peeping status. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 801 | Capability not supported. Function getDlpAntiPeepInfo can not work correctly due to limited device capabilities. |
| 1020600001 | Internal error. |
Example:
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
try {
let dlpAntiPeepStatus = dlpAntiPeep.getDlpAntiPeepInfo();
console.info('Succeeded in getting DlpAntiPeepStatus.');
} catch (error) {
console.error(`Failed to get DlpAntiPeepStatus. Code: ${error.code}, message: ${error.message}`);
} passDlpAntiPeepInfo(): void
Sets the peeping status of the current app to non-peeping. Then the peeping status returned by the on("dlpAntiPeep") API's callback function and the peeping status obtained by the getDlpAntiPeepInfo API are both non-peeping.
If an app no longer needs to receive the peeping status within its lifecycle, it can directly call the off("dlpAntiPeep") API to unsubscribe from the screen privacy notification. If the app wants to receive the non-peeping state in scenarios where screen privacy protection can be bypassed, it can call the passDlpAntiPeepInfo API to set the peeping status to non-peeping.
Call this API only after the development prerequisites are met and the on("dlpAntiPeep") API is called. This API can be used to set the peeping status to non-peeping through a callback. Then the non-peeping state remains within the app's subscription lifecycle until the app exits or the device screen is locked.
Model restriction: This API can be used only in the stage model.
Required permission: ohos.permission.DLP_GET_HIDE_STATUS
System capability: SystemCapability.Security.DlpAntiPeep
Since: 6.0.0(20)
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 801 | Capability not supported. Function passDlpAntiPeepInfo can not work correctly due to limited device capabilities. |
| 1020600001 | Internal error. |
Example:
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
try {
dlpAntiPeep.passDlpAntiPeepInfo();
console.info('Succeeded in calling passDlpAntiPeepInfo.');
} catch (error) {
console.error(`Failed to call passDlpAntiPeepInfo. Code: ${error.code}, message: ${error.message}`);
} setAntiPeepMaskLayer(windowId: number): Promise<void>
Sets a system-level overlay for a specified window. This API uses a promise to return the result.
Once called, this API will display an overlay to mask the app window. You are advised to call this API only once when the peeking state is detected, as frequent calls may affect user experience.
Model restriction: This API can be used only in the stage model.
Required permission: ohos.permission.DLP_GET_HIDE_STATUS
System capability: SystemCapability.Security.DlpAntiPeep
Since: 6.0.1(21)
Parameters:
| Name | Type | Mandatory | Description |
|---|---|---|---|
| windowId | number | Yes | ID of the window to be masked for privacy protection. The value must be a positive integer. |
Returns:
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 801 | Capability not supported. Function setAntiPeepMaskLayer can not work correctly due to limited device capabilities. |
| 1020600001 | Internal error. |
| 1020600002 | The antipeep function is not enabled. |
| 1020600003 | The protected application is not displayed on the screen. |
Example:
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
import { common } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
try {
const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
const win: window.Window = await window.getLastWindow(context);
const windowId: number = win.getWindowProperties().id;
await dlpAntiPeep.setAntiPeepMaskLayer(windowId);
console.info('Succeeded in setting AntiPeep MaskLayer.');
} catch (error) {
console.error(`Failed to set AntiPeep MaskLayer. Code: ${error.code}, message: ${error.message}`);
} requestAntiPeepOptions(context: Context): Promise<AntiPeepOptionsResult>
Used by UIAbility/UIExtensionAbility to display a settings dialog and request the user to enable the screen privacy function. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
Required permission: ohos.permission.DLP_GET_HIDE_STATUS
System capability: SystemCapability.Security.DlpAntiPeep
Since: 6.1.0(23)
Parameters:
| Name | Type | Mandatory | Description |
|---|---|---|---|
| context | Context | Yes | Context of the UIAbility/UIExtensionAbility that requests the permissions. |
Returns:
| Type | Description |
|---|---|
| Promise<AntiPeepOptionsResult> | Promise used to return whether the screen privacy function is enabled. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 801 | Capability not supported. Function requestAntiPeepOptions can not work correctly due to limited device capabilities. |
| 1020600001 | Internal error. |
| 1020600004 | Facial recognition is not set. |
Example:
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
import { common } from '@kit.AbilityKit';
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
dlpAntiPeep.requestAntiPeepOptions(context).then((data: dlpAntiPeep.AntiPeepOptionsResult) => {
console.info(`Succeeded in calling requestAntiPeepOptions, result: ${data}.`);
}).catch((error: BusinessError) => {
console.error(`Failed to call requestAntiPeepOptions. Code: ${error.code}, message: ${error.message}`);
}) Enumerates the pop-up setting results.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.Security.DlpAntiPeep
Since: 6.1.0(23)
| Name | Value | Description |
|---|---|---|
| SUCCESS | 0 | The screen privacy function is enabled successfully. |
| FAIL | 1 | The screen privacy function fails to be enabled. |
| ALREADY_ON | 2 | The screen privacy function has already been enabled. |
publishAntiPeepInformation(): Promise<void>
Publishes a screen privacy notification. This API uses a promise to return the result.
When the device remains in the peeking risk state, the system proactively sends only one live view notification with a duration of 5 seconds. To send the notification again, you need to call this API.
Model restriction: This API can be used only in the stage model.
Required permission: ohos.permission.DLP_GET_HIDE_STATUS
System capability: SystemCapability.Security.DlpAntiPeep
Since: 6.1.0(23)
Returns:
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 801 | Capability not supported. Function publishAntiPeepInformation can not work correctly due to limited device capabilities. |
| 1020600001 | Internal error. |
| 1020600002 | The anti-peep function is not enabled. |
| 1020600003 | The protected application is not displayed on the screen. |
| 1020600005 | The anti-peep information has been published. |
| 1020600006 | No peeping risk detected. |
Example:
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
dlpAntiPeep.publishAntiPeepInformation().then(() => {
console.info('Succeeded in calling publishAntiPeepInformation.');
}).catch((error: BusinessError) => {
console.error(`Failed to call publishAntiPeepInformation. Code: ${error.code}, message: ${error.message}`);
})