• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 display from '@ohos.display'
18import { ResponseData } from '../models/ResponseData'
19import { Logger } from './Logger'
20
21// 获取当前窗口
22export function getCurrentWindow(context: Context) {
23  return window.getTopWindow(context)
24}
25
26// 判断隐私窗口
27export function hasPrivate(): ResponseData {
28  let currentDisplay = null
29  try {
30    currentDisplay = display.getDefaultDisplaySync()
31  } catch (exception) {
32    return { status: 'failed', errorMessage: JSON.stringify(exception) }
33  }
34  if (currentDisplay === null) {
35    return { status: 'failed', errorMessage: 'get current display failed' }
36  }
37  let ret = undefined
38  try {
39    ret = display.hasPrivateWindow(currentDisplay.id)
40  } catch (exception) {
41    return { status: 'failed', errorMessage: JSON.stringify(exception) }
42  }
43  if (ret === undefined) {
44    return { status: 'failed', errorMessage: 'ret is undefined' }
45  }
46  return ret ? { status: 'success', errorMessage: '', result: true } :
47    { status: 'success', errorMessage: '', result: false }
48}
49
50
51// 设置隐私窗口
52export function setWindowPrivacyMode(context: Context, windowPrivacyMode: boolean) {
53  let currentWindow = null
54  getCurrentWindow(context)
55    .then(res => {
56      currentWindow = res
57      try {
58        currentWindow.setWindowPrivacyMode(windowPrivacyMode, (err) => {
59          if (err.code) {
60            Logger.error('set window privacy mode failed cause: ' + JSON.stringify(err))
61            return
62          }
63          Logger.info(`set window privacy mode success ${windowPrivacyMode}`)
64        })
65      } catch (exception) {
66        Logger.info('set window mode failed cause: ' + JSON.stringify(exception))
67      }
68    })
69}