1/** 2 * Copyright (c) 2024 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 16import window from '@ohos.window'; 17import { BusinessError } from '@ohos.base'; 18import { GlobalContext } from '../common/GlobalContext'; 19import type UIAbilityContext from 'application/UIAbilityContext'; 20import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 21 22const TAG: string = 'PreventScreenshotsModel'; 23 24export default class PreventScreenshotsModel { 25 private static sInstance: PreventScreenshotsModel; 26 27 public static getInstance(): PreventScreenshotsModel { 28 if (PreventScreenshotsModel.sInstance == null) { 29 PreventScreenshotsModel.sInstance = new PreventScreenshotsModel(); 30 } 31 return PreventScreenshotsModel.sInstance; 32 } 33 34 PreventScreenshots(flag: boolean, session: UIExtensionContentSession | undefined) { 35 let isPrivacyMode: boolean = flag; 36 if (session !== undefined) { 37 session.setWindowPrivacyMode(isPrivacyMode); 38 return; 39 } 40 41 let windowClass: window.Window | undefined = undefined; 42 let context: UIAbilityContext = GlobalContext.getContext().getCmContext(); 43 if (context === undefined) { 44 console.error(TAG, 'context is undefined!'); 45 return; 46 } 47 48 window.getLastWindow(context).then((window) => { 49 windowClass = window; 50 console.info(TAG,'Success in obtaining the top window data'); 51 windowClass.setWindowPrivacyMode(isPrivacyMode).catch((err: BusinessError) => { 52 console.error(TAG, 'setWindowPrivacyMode failed: ' + JSON.stringify(err)); 53 }) 54 }).catch((err: BusinessError) => { 55 console.info(TAG + 'getLastWindow failed: ' + JSON.stringify(err)); 56 }) 57 } 58}