index.uts 2.9 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
import { display } from '@kit.ArkUI';
import { window } from '@kit.ArkUI';
import { Callback, BusinessError } from '@kit.BasicServicesKit';

import { getAbilityContext, UTSHarmony } from '@dcloudio/uni-runtime';

import {
  OnUserCaptureScreen, UserCaptureScreenCallback,
  OffUserCaptureScreen,
  SetUserCaptureScreen, SetUserCaptureScreenOptions, SetUserCaptureScreenSuccess
} from '../interface.uts';

const onUserCaptureScreenCallbacks: Function[] = []

const harmonyCaptureStatusChange: Callback<boolean> = (captureStatus: boolean) => {
  if (captureStatus) {
    onUserCaptureScreenCallbacks.forEach(cb => {
      typeof cb === 'function' && cb()
    })
  }
}

display.on('captureStatusChange', harmonyCaptureStatusChange)

export const onUserCaptureScreen: OnUserCaptureScreen = function (callback: UserCaptureScreenCallback | null) {
  if (callback) {
    onUserCaptureScreenCallbacks.push(callback)
  }
}

export const offUserCaptureScreen: OffUserCaptureScreen = function (callback: UserCaptureScreenCallback | null) {
  if (callback) {
    const index = onUserCaptureScreenCallbacks.indexOf(callback)
    if (index > -1) {
      onUserCaptureScreenCallbacks.splice(index, 1)
    }
  }
}

export const setUserCaptureScreen: SetUserCaptureScreen = function (options: SetUserCaptureScreenOptions) {
  const errSubject = 'uni-usercapturescreen'
  const setUserCaptureScreenSuccess: SetUserCaptureScreenSuccess = {}
  window.getLastWindow(getAbilityContext()!, (err, window) => {
    const errCode: number = err.code;
    if (errCode) {
      options.fail?.({
        errCode: (err as BusinessError).code,
        errSubject,
        errMsg: `setUserCaptureScreen:fail ${(err as BusinessError).message}`
      } as IUniError)
      options.complete?.(setUserCaptureScreenSuccess);
      return;
    } else {
      try {
        UTSHarmony.requestSystemPermission(['ohos.permission.PRIVACY_WINDOW'], (allRight: boolean) => {
          if (allRight) {
            window.setWindowPrivacyMode(!options.enable, (err: BusinessError) => {
              const errCode: number = err.code;
              if (errCode) {
                options.fail?.({
                  errCode: err.code,
                  errSubject,
                  errMsg: `setUserCaptureScreen:fail ${err.message}`
                } as IUniError)
                options.complete?.(setUserCaptureScreenSuccess);
                return;
              }
              options.success?.(setUserCaptureScreenSuccess);
              options.complete?.(setUserCaptureScreenSuccess);
            });
          } else {
            throw new Error('permission denied')
          }
        }, () => {
          throw new Error('permission denied')
        })
      } catch (err) {
        options.fail?.({
          errCode: (err as BusinessError).code,
          errSubject,
          errMsg: `setUserCaptureScreen:fail ${(err as BusinessError).message}`
        } as IUniError)
        options.complete?.(setUserCaptureScreenSuccess);
      }
    }
  })
}