已关闭
feat: PhotoViewPicker 启用防截屏/防录屏保护 #687
2401_83369328创建于 8月11日关闭于 9月11日
feat: PhotoViewPicker 启用防截屏/防录屏保护 #687
已关闭
2401_83369328创建于 8月11日关闭于 9月11日
共 1 个文件变更+29-0
@@ -33,6 +33,7 @@ import lazy { getScreen } from '@ohos/common/src/main/ets/default/model/common/S
33import lazy { MediaObserver } from '@ohos/common/src/main/ets/default/model/browser/dataObserver/MediaObserver';33import lazy { MediaObserver } from '@ohos/common/src/main/ets/default/model/browser/dataObserver/MediaObserver';
34import lazy { UserFileManagerAccess } from '@ohos/common/src/main/ets/default/access/UserFileManagerAccess';34import lazy { UserFileManagerAccess } from '@ohos/common/src/main/ets/default/access/UserFileManagerAccess';
35import lazy UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';35import lazy UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
36+import lazy window from '@ohos.window';
36import lazy { CommonUtil } from '@ohos/common/src/main/ets/default/utils/CommonUtil';37import lazy { CommonUtil } from '@ohos/common/src/main/ets/default/utils/CommonUtil';
37import lazy { CustomColors } from '@ohos.arkui.theme';38import lazy { CustomColors } from '@ohos.arkui.theme';
38import lazy Bundles from '@ohos/tools/src/main/utils/Bundle';39import lazy Bundles from '@ohos/tools/src/main/utils/Bundle';
@@ -82,6 +83,17 @@ export default class CommonPickerUIExtensionAbility extends UIExtensionAbility {
82 PickerForegroundManager.getInstance().addForegroundInstance();83 PickerForegroundManager.getInstance().addForegroundInstance();
83 this.context?.eventHub?.emit(Constants.PHOTO_PICKER_ON_FOREGROUND, true);84 this.context?.eventHub?.emit(Constants.PHOTO_PICKER_ON_FOREGROUND, true);
84 MaskLayerUtils.getInstance().setPickerProtectedState(this.storage, false);85 MaskLayerUtils.getInstance().setPickerProtectedState(this.storage, false);
86+ // 启用防截屏/防录屏保护,防止后台 App 通过录屏 API 窃取用户在 Picker 中的相册内容
87+ try {
88+ window.getLastWindow(this.context).then((win: window.Window): void => {
89+ win.setWindowPrivacyMode(true);
90+ Log.info(TAG, 'setWindowPrivacyMode true for picker privacy');
91+ }).catch((err: Error): void => {
92+ Log.error(TAG, `setWindowPrivacyMode true failed: ${err.message}`);
93+ });
94+ } catch (e) {
95+ Log.error(TAG, `getLastWindow failed: ${JSON.stringify(e)}`);
96+ }
85 }97 }
86 98 
87 onBackground() {99 onBackground() {
@@ -92,6 +104,17 @@ export default class CommonPickerUIExtensionAbility extends UIExtensionAbility {
92 }104 }
93 this.context?.eventHub?.emit(Constants.PHOTO_PICKER_ON_FOREGROUND, false);105 this.context?.eventHub?.emit(Constants.PHOTO_PICKER_ON_FOREGROUND, false);
94 MaskLayerUtils.getInstance().setPickerProtectedState(this.storage, true);106 MaskLayerUtils.getInstance().setPickerProtectedState(this.storage, true);
107+ // Picker 退后台时取消防截屏保护
108+ try {
109+ window.getLastWindow(this.context).then((win: window.Window): void => {
110+ win.setWindowPrivacyMode(false);
111+ Log.info(TAG, 'setWindowPrivacyMode false, picker in background');
112+ }).catch((err: Error): void => {
113+ Log.error(TAG, `setWindowPrivacyMode false failed: ${err.message}`);
114+ });
115+ } catch (e) {
116+ Log.error(TAG, `getLastWindow in background failed: ${JSON.stringify(e)}`);
117+ }
95 }118 }
96 119 
97 onDestroy() {120 onDestroy() {
@@ -303,6 +326,12 @@ export default class CommonPickerUIExtensionAbility extends UIExtensionAbility {
303 }326 }
304 onSessionDestroy(session: UIExtensionContentSession) {327 onSessionDestroy(session: UIExtensionContentSession) {
305 Log.info(TAG, 'onSessionDestroy');328 Log.info(TAG, 'onSessionDestroy');
329+ // ensure anti-screen-capture protection is released
330+ try {
331+ window.getLastWindow(this.context).then((win: window.Window): void => {
332+ win.setWindowPrivacyMode(false);
333+ });
334+ } catch (e) {}
306 this.destroyPickerSubWindow();335 this.destroyPickerSubWindow();
307 let param = this.storage?.get<SelectParams>(Constants.PHOTO_PICKER_PARAMS_KEY) as SelectParams;336 let param = this.storage?.get<SelectParams>(Constants.PHOTO_PICKER_PARAMS_KEY) as SelectParams;
308 this.deleteCallerBundleName(param);337 this.deleteCallerBundleName(param);