This feature enables apps to protect user privacy based on the peeping status. For example, 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 not used by the device owner. The system intelligently identifies the person who consistently unlocks the phone using facial recognition as the device owner for screen privacy protection. If screen privacy protection is not enabled, display a settings pop-up or prompt the user to go to the settings screen to enable it.
Facial recognition has been enabled on the device.
The global screen privacy switch has been enabled under Settings > Privacy & security > Screen Privacy on the device. Then the screen privacy function has been enabled for the specific app after successful facial recognition.
All of the following conditions must be met:
This feature requires that the Screen Privacy option be available on the device. You can check it by going to Settings > Privacy & security > Screen Privacy on the device.
The HarmonyOS version must be HarmonyOS 6.0.0 Beta1 or later.
The DevEco Studio version must be DevEco Studio 6.0.0 Beta1 or later.
The HarmonyOS SDK version must be HarmonyOS 6.0.0 Beta1 SDK or later.
The screen privacy function uses intelligent detection to assess your surrounding environment via sensors and provide risk alerts. Key factors include whether your face is within a certain distance from the device, whether your face is blocked, and whether the ambient lighting is sufficient. If the distance is too close or too far, the face is blocked, or the environment is dimly lit, recognition errors may occur, potentially resulting in missed or false alerts. If you suspect the intelligent detection is inaccurate, you can try adjusting your position and lighting, re-unlock the phone using facial recognition, and then re-enable the feature to help safeguard your privacy.

Process description:
A user enables the screen privacy function for the current app in Settings > Privacy & security > Screen Privacy, or an app provides a setting entry. After a user taps the entry, the app calls the requestAntiPeepOptions API to display the setting dialog box.
The app calls the isDlpAntiPeepSwitchOn API to query the status of the screen privacy function for the current app.
The app calls the on API to subscribe to the screen privacy notification. The status is returned based on the detection result: If only the owner is looking at the screen, the non-peeping state is returned. If the owner and a non-owner are looking at the screen at the same time, the peeping state is returned. If the owner is not using or sharing the screen, the non-peeping state is returned.
The app calls the getDlpAntiPeepInfo API to query the peeping status.
The app calls the setAntiPeepMaskLayer API to display the system-level overlay.
The app calls the passDlpAntiPeepInfo API to set the peeping status to non-peeping. Then the non-peeping state will be returned until the phone is locked or the app exits.
The app calls the off API to unsubscribe from the screen privacy notification.
The following table lists the APIs related to screen privacy. For more information about the APIs, please refer to API Reference.
| API | Description |
|---|---|
| isDlpAntiPeepSwitchOn(): Promise<boolean> | Checks whether screen privacy has been enabled. |
| on(type: 'dlpAntiPeep', callback: Callback<DlpAntiPeepStatus>): void | Subscribes to the screen privacy notification. |
| off(type: 'dlpAntiPeep', callback?: Callback<DlpAntiPeepStatus>): void | Unsubscribes from the screen privacy notification. |
| getDlpAntiPeepInfo(): DlpAntiPeepStatus | Obtains the peeping status of the current app. |
| passDlpAntiPeepInfo(): void | Sets the peeping status to non-peeping. Then the non-peeping state will be returned until the phone is locked or the app exits. |
| setAntiPeepMaskLayer(windowId: number): Promise<void> | Displays a system-level overlay to mask the window. |
| requestAntiPeepOptions(context: Context): Promise<AntiPeepOptionsResult> | Displays a settings dialog box to request the user to enable the screen privacy function. |
| publishAntiPeepInformation(): Promise<void> | Publishes a screen privacy notification. |
When preparing for the development, you need to apply for the ohos.permission.DLP_GET_HIDE_STATUS permission to obtain the screen peeping status of the current app. For details about how to apply for a restricted permission, please refer to Requesting Restricted Permissions.
You need to inform users about the purpose, methods, and scope of data usage.
Import the Screen Privacy module and related public modules.
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
import { JSON } from '@kit.ArkTS';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit'; Encapsulate the Screen Privacy API.
/**
* Callback for screen privacy status changes. The status parameter indicates the screen privacy status.
* */
export interface AntiPeepCallback {
onStatusChanged: (status: dlpAntiPeep.DlpAntiPeepStatus) => Promise<void>;
}
/**
* Check whether the current device supports the screen privacy API.
* */
export function canUseAntiPeep(): boolean {
return canIUse('SystemCapability.Security.DlpAntiPeep');
}
/**
* Check whether screen privacy has been enabled for the current app.
* */
export async function isAntiPeepOn(): Promise<boolean> {
try {
let result: boolean = await dlpAntiPeep.isDlpAntiPeepSwitchOn();
hilog.info(0x0000, 'AntiPeepUtils', `[isAntiPeepOn] isDlpAntiPeepSwitchOn success. ${result}`);
return result;
} catch (err) {
hilog.error(0x0000, 'AntiPeepUtils', `[isAntiPeepOn] isDlpAntiPeepSwitchOn failed.${JSON.stringify(err)}`);
return false;
}
}
/**
* get current screen privacy status
* */
export function getAntiPeepInfo(): dlpAntiPeep.DlpAntiPeepStatus {
try {
// Obtain the screen privacy status.
let dlpAntiPeepStatus = dlpAntiPeep.getDlpAntiPeepInfo();
hilog.info(0x0000, 'AntiPeepUtils', `getDlpHideInfo success. ${JSON.stringify(dlpAntiPeepStatus)}`);
return dlpAntiPeepStatus;
} catch (err) {
hilog.info(0x0000, 'AntiPeepUtils', `getDlpHideInfo failed. ${JSON.stringify(err)}`);
return -1;
}
}
/**
* Subscribe to the screen privacy notification.
* */
export function listenOnAntiPeepStatus(antiPeepCB: AntiPeepCallback): boolean {
try {
hilog.info(0x0000, 'AntiPeepUtils', `start on('dlpAntiPeep')`);
dlpAntiPeep.on('dlpAntiPeep', (dlpAntiPeepStatus: dlpAntiPeep.DlpAntiPeepStatus) => {
hilog.info(0x0000, 'AntiPeepUtils', `dlpAntiPeep callback: ${JSON.stringify(dlpAntiPeepStatus)}`);
if (antiPeepCB) {
antiPeepCB.onStatusChanged(dlpAntiPeepStatus);
} else {
hilog.warn(0x0000, 'AntiPeepUtils', `antiPeepCB is empty`);
}
});
hilog.info(0x0000, 'AntiPeepUtils', `on('dlpAntiPeep') ok`);
return true;
} catch (err) {
hilog.error(0x0000, 'AntiPeepUtils', `dlpAntiPeep.on failed. ${JSON.stringify(err)}`);
return false;
}
}
/**
* Unsubscribe from the screen privacy notification.
* */
export function listenOffAntiPeepStatus() {
try {
hilog.info(0x0000, 'AntiPeepUtils', `start off('dlpAntiPeep')`);
dlpAntiPeep.off('dlpAntiPeep');
hilog.info(0x0000, 'AntiPeepUtils', `off('dlpAntiPeep') ok`);
} catch (err) {
hilog.error(0x0000, 'AntiPeepUtils', `dlpAntiPeep.off failed. ${JSON.stringify(err)}`);
}
}
/**
* Display the system overlay.
* */
export function showSystemMaskLayer(windowId: number): Promise<boolean> {
return new Promise((resolve) => {
try {
dlpAntiPeep.setAntiPeepMaskLayer(windowId).catch((err: BusinessError) => {
hilog.error(0x0000, 'AntiPeepUtils', `Execute setAntiPeepMaskLayer failed. error code:${err.code},
error message:${err.message}`);
resolve(false);
}).then(() => {
hilog.info(0x0000, 'AntiPeepUtils', `setAntiPeepMaskLayer success`);
resolve(true);
})
} catch (err) {
hilog.error(0x0000, 'AntiPeepUtils', `Call setAntiPeepMaskLayer failed. ${JSON.stringify(err)}`);
resolve(false);
}
});
}
/**
* Instruct the user to enable screen privacy for the current app.
* */
export async function requestAntiPeepOptions(
context: common.UIAbilityContext): Promise<dlpAntiPeep.AntiPeepOptionsResult> {
try {
const result = await dlpAntiPeep.requestAntiPeepOptions(context);
hilog.info(0x0000, 'AntiPeepUtils', `[requestAntiPeepOptions] requestAntiPeepOptions success. ${result}`);
return result;
} catch (err) {
hilog.error(0x0000,
'AntiPeepUtils', `[requestAntiPeepOptions] requestAntiPeepOptions failed.${JSON.stringify(err)}`);
return dlpAntiPeep.AntiPeepOptionsResult.FAIL;
}
} Import the DlpAntiPeep encapsulation module and related public modules.
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
// ...
import { hilog } from '@kit.PerformanceAnalysisKit';
// ...
import { JSON } from '@kit.ArkTS';
import { window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
// ...
import {
AntiPeepCallback,
canUseAntiPeep,
getAntiPeepInfo,
isAntiPeepOn,
listenOffAntiPeepStatus,
listenOnAntiPeepStatus,
showSystemMaskLayer,
requestAntiPeepOptions,
} from '../common/utils/AntiPeepUtils'; Call the query API to check whether screen privacy is enabled for the current app. If screen privacy is enabled, call the screen privacy notification subscription API and obtain the peeping status.
@Entry
@Component
struct Index {
// ...
private isSystemLayerTriggered: boolean = false;
// ...
private isListenOn: boolean = false;
private antiPeepCB: AntiPeepCallback = {
onStatusChanged: async (status: dlpAntiPeep.DlpAntiPeepStatus): Promise<void> => {
await this.handleAntiPeepStatus(status);
}
};
// ...
/**
* Process the screen privacy status.
* */
private async handleAntiPeepStatus(status: dlpAntiPeep.DlpAntiPeepStatus) {
hilog.info(0x0000, 'IndexPage', `[handleAntiPeepStatus] ${status}`);
switch (status) {
// No one is peeping at the screen.
case dlpAntiPeep.DlpAntiPeepStatus.PASS:
// ...
break;
// Someone is peeping at the screen. The app can perform privacy protection operations.
case dlpAntiPeep.DlpAntiPeepStatus.HIDE:
// ...
const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
const windowClass = await window.getLastWindow(context);
const windowId = windowClass.getWindowProperties().id;
// Display the system overlay.
this.isSystemLayerTriggered = await showSystemMaskLayer(windowId);
// ...
break;
default:
// ...
break;
}
}
// Check whether screen privacy is enabled for the current app. If not, instruct the user to enable it. After it is enabled, obtain the screen privacy status and subscribe to the screen privacy status listener.
private initAntiPeepStatus() {
if (canUseAntiPeep()) {
isAntiPeepOn().then((opened) => {
if (opened) {
let info = getAntiPeepInfo();
this.handleAntiPeepStatus(info);
// Subscribe to the screen privacy status listener.
this.isListenOn = listenOnAntiPeepStatus(this.antiPeepCB);
} else {
// If the function is not enabled, instruct the user to enable it.
const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
requestAntiPeepOptions(context).then((result) => {
if (result === dlpAntiPeep.AntiPeepOptionsResult.SUCCESS ||
result === dlpAntiPeep.AntiPeepOptionsResult.ALREADY_ON) { // Indicates that the screen privacy function is enabled successfully or has been enabled.
let info = getAntiPeepInfo();
this.handleAntiPeepStatus(info);
this.isListenOn = listenOnAntiPeepStatus(this.antiPeepCB);
} else {
try {
this.getUIContext().getPromptAction().showToast({
message: $r('app.string.anti_peep_not_enable')
});
} catch (error) {
hilog.info(0x0000, 'IndexPage', `show toast error:${JSON.parse(error)}}`);
}
}
})
}
})
} else {
try {
this.getUIContext().getPromptAction().showToast({
message: $r('app.string.device_not_supported')
});
} catch (error) {
hilog.info(0x0000, 'IndexPage', `show toast error:${JSON.parse(error)}}`);
}
}
}
// ...
// Initialize the screen privacy status.
private initData() {
// ...
this.initAntiPeepStatus();
// ...
}
onPageShow(): void {
this.initData();
}
onPageHide(): void {
this.isSystemLayerTriggered = false;
// ...
}
// Unsubscribe from the screen privacy notification.
aboutToDisappear(): void {
if (this.isListenOn) {
listenOffAntiPeepStatus();
this.isListenOn = false;
}
}
build() {
Scroll() {
// ...
}
.height('100%')
.width('100%')
.backgroundColor($r('app.color.bg_main'))
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
}
}