1/**
2 * Copyright (c) 2023 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 util from '@ohos.util';
17import userAuth from '@ohos.userIAM.userAuth';
18import { DialogType } from '../module/DialogType';
19import LogUtils from './LogUtils';
20import window from '@ohos.window';
21import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
22
23const TAG = 'FuncUtils';
24
25export class FuncUtils {
26  getUint8PW(value: string): Uint8Array {
27    let textEncoder = new util.TextEncoder();
28    return textEncoder.encode(value);
29  }
30
31  getDialogType(type: Array<userAuth.UserAuthType>): DialogType {
32    if (type) {
33      if (type.includes(userAuth.UserAuthType.PIN)) {
34        if (type.includes(userAuth.UserAuthType.FACE)) {
35          if (type.includes(userAuth.UserAuthType.FINGERPRINT)) {
36            return DialogType.ALL;
37          }
38          return DialogType.PIN_FACE;
39        }
40        if (type.includes(userAuth.UserAuthType.FINGERPRINT)) {
41          return DialogType.PIN_FINGER;
42        }
43        return DialogType.PIN;
44      }
45      if (type.includes(userAuth.UserAuthType.FINGERPRINT)) {
46        return DialogType.FINGER;
47      }
48      if (type.includes(userAuth.UserAuthType.FACE)) {
49        return DialogType.FACE;
50      }
51    }
52    return DialogType.PIN;
53  }
54
55  getWindowHeight(): void {
56    LogUtils.info(TAG, 'getWindowHeight');
57    try {
58      window.on('systemBarTintChange', (data) => {
59        LogUtils.debug(TAG, 'Succeeded in enabling the listener for window stage event changes. Data: ' +
60        JSON.stringify(data));
61        for (let i = 0; i < data.regionTint.length; i++) {
62          let regionData = data.regionTint[i];
63          if (regionData.region == undefined) {
64            continue;
65          } else if (regionData.type === window.WindowType.TYPE_STATUS_BAR) {
66            AppStorage.SetOrCreate('SYSTEM_STATUS_BAR_HEIGHT', px2vp(regionData.region.height));
67            continue;
68          } else if (regionData.type === window.WindowType.TYPE_NAVIGATION_BAR) {
69            AppStorage.SetOrCreate('SYSTEM_NAVIGATION_BAR_HEIGHT', px2vp(regionData.region.height));
70            continue;
71          }
72        }
73      });
74    } catch (error) {
75      LogUtils.error(TAG, 'Failed to enable the listener for window stage event changes. error: ' + error?.code);
76    }
77  }
78
79  judgmentOverflow(value: number): void {
80    if (value === Number.POSITIVE_INFINITY || value === Number.NEGATIVE_INFINITY) {
81      LogUtils.error(TAG, 'judgmentOverflow spill code value: ' + value);
82      (AppStorage.get("session") as UIExtensionContentSession)?.terminateSelf();
83    }
84  }
85}
86
87let funcUtils = new FuncUtils();
88
89export default funcUtils as FuncUtils;
90