• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 userAuth from '@ohos.userIAM.userAuth';
17import window from '@ohos.window';
18import { DialogType } from '../../../../main/ets/common/module/DialogType';
19import FuncUtils from '../../../../main/ets/common/utils/FuncUtils';
20import LogUtils from '../../../../main/ets/common/utils/LogUtils';
21import Constants, { CmdType, WantParams, WidgetCommand } from '../../../../main/ets/common/vm/Constants';
22import CustomPassword from '../../../../main/ets/pages/components/CustomPassword';
23import FaceAuth from '../../../../main/ets/pages/components/FaceAuth';
24import FingerprintAuth from '../../../../main/ets/pages/components/FingerprintAuth';
25import PasswordAuth from '../../../../main/ets/pages/components/PasswordAuth';
26import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
27import { DEFAULT } from '@ohos/hypium';
28
29const TAG = 'Index';
30const defaultSize = 72;
31let userAuthWidgetMgr: userAuth.UserAuthWidgetMgr;
32
33@Entry
34@Component
35struct Index {
36  @State authType: Array<userAuth.UserAuthType> = [userAuth.UserAuthType.PIN];
37  @State type: string[] = [];
38  @State topHeight: number = defaultSize;
39  @State bottomHeight: number = defaultSize;
40  @State pinSubType: string = Constants.pinNumber;
41  @State dialogType: DialogType = DialogType.ALL;
42  @State windowModeType: string = 'DIALOG_BOX';
43  @State cmdData: Array<CmdType> = [];
44  @Provide isLandscape: boolean = false;
45  @Provide underFingerPrint: boolean = false;
46
47  onCancel(): void {
48    LogUtils.debug(TAG, 'Callback when the first button is clicked');
49    (AppStorage.get("session") as UIExtensionContentSession)?.terminateSelf();
50  }
51
52  onAccept(): void {
53    LogUtils.debug(TAG, 'Callback when the second button is clicked');
54  }
55
56  existApp(): void {
57    LogUtils.debug(TAG, 'Click the callback in the blank area');
58    (AppStorage.get("session") as UIExtensionContentSession)?.terminateSelf();
59  }
60
61  handleAuthStart(): void {
62    try {
63      userAuthWidgetMgr = userAuth.getUserAuthWidgetMgr(Constants.userAuthWidgetMgrVersion);
64      LogUtils.info(TAG, 'getUserAuthWidgetMgr success');
65      let that = this;
66      userAuthWidgetMgr.on('command', {
67        sendCommand (result) {
68          LogUtils.info(TAG, 'sendCommand result: ' + result);
69          const cmdDataObj: WidgetCommand = JSON.parse(result || '{}');
70          if (cmdDataObj?.cmd?.[0]?.payload?.result === Constants.userAuthWidgetMgrSuccess) {
71            that.onCancel();
72          } else {
73            that.cmdData = cmdDataObj?.cmd || [];
74            that.pinSubType = cmdDataObj?.pinSubType;
75          }
76        }
77      });
78    } catch (error) {
79      LogUtils.error(TAG, 'getUserAuthWidgetMgr catch error: ' + error?.code);
80    }
81  }
82
83  aboutToAppear(): void {
84    LogUtils.debug(TAG, 'aboutToAppear test');
85    if (AppStorage.get("wantParams") as WantParams) {
86      LogUtils.debug(TAG, 'wantParams: ' + JSON.stringify(AppStorage.get("wantParams") as WantParams));
87      this.getParams(AppStorage.get("wantParams") as WantParams);
88    } else {
89      LogUtils.error(TAG, 'aboutToAppear wantParams null');
90    }
91    this.getWindowHeight();
92  }
93
94  aboutToDisappear(): void {
95    LogUtils.info(TAG, 'aboutToDisappear');
96    if (userAuthWidgetMgr) {
97      userAuthWidgetMgr.off('command', {
98        sendCommand (result) {
99          LogUtils.info(TAG, 'aboutToDisappear userAuthWidgetMgr offCommand result: ' + JSON.stringify(result));
100        }
101      });
102    }
103  }
104
105  onPageShow(): void {
106    LogUtils.info(TAG, 'onPageShow');
107  }
108
109  onPageHide(): void {
110    LogUtils.info(TAG, 'onPageHide');
111  }
112
113  onBackPress(): void {
114    LogUtils.info(TAG, 'onBackPress');
115  }
116
117  getParams(result: WantParams): void {
118    LogUtils.info(TAG, 'getParams');
119    const resultInfo: WantParams = result;
120    this.pinSubType = resultInfo?.pinSubType;
121    const newType = resultInfo?.type && resultInfo?.type.map(item => {
122      if (item === Constants.noticeTypePin) {
123        return userAuth.UserAuthType.PIN;
124      } else if (item === Constants.noticeTypeFinger) {
125        return userAuth.UserAuthType.FINGERPRINT;
126      } else if (item === Constants.noticeTypeFace) {
127        return userAuth.UserAuthType.FACE;
128      } else {
129        return userAuth.UserAuthType.PIN;
130      }
131    })
132    AppStorage.setOrCreate("widgetContextId", resultInfo.widgetContextId as number);
133    this.authType = newType;
134    this.type = resultInfo.type;
135    this.windowModeType = resultInfo.windowModeType;
136    this.dialogType = FuncUtils.getDialogType(newType);
137    this.cmdData = resultInfo.cmd || [];
138  }
139
140  getWindowHeight(): void {
141    LogUtils.info(TAG, 'getWindowHeight');
142    try {
143      window.on('systemBarTintChange', (data) => {
144        LogUtils.debug(TAG, 'Succeeded in enabling the listener for window stage event changes. Data: ' +
145        JSON.stringify(data));
146        for (let i = 0; i < data.regionTint.length; i++) {
147          let regionData = data.regionTint[i];
148          if (regionData.region === undefined) {
149            continue;
150          } else if (regionData.type === window.WindowType.TYPE_STATUS_BAR) {
151            this.topHeight = px2vp(regionData.region.height);
152            continue;
153          } else if (regionData.type === window.WindowType.TYPE_NAVIGATION_BAR) {
154            this.bottomHeight = px2vp(regionData.region.top);
155            continue;
156          }
157        }
158      });
159    } catch (error) {
160      LogUtils.error(TAG, 'Failed to enable the listener for window stage event changes. error: ' + error?.code);
161    }
162  }
163
164  build() {
165    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) {
166      if ('DIALOG_BOX' === this.windowModeType) {
167        if (this.authType.includes(userAuth.UserAuthType.FACE)) {
168          FaceAuth({
169            type: $windowModeType,
170            pinSubType: $pinSubType,
171            dialogType: $dialogType,
172            cmdData: $cmdData,
173          })
174        } else if (this.authType.includes(userAuth.UserAuthType.FINGERPRINT)) {
175          FingerprintAuth({
176            type: $windowModeType,
177            pinSubType: $pinSubType,
178            dialogType: $dialogType,
179            cmdData: $cmdData,
180          })
181        } else {
182          PasswordAuth({
183            pinSubType: $pinSubType,
184            cmdData: $cmdData
185          })
186        }
187
188      } else {
189        // full screen PIN
190        CustomPassword({
191          authType: $authType,
192          pinSubType: $pinSubType,
193          cmdData: $cmdData
194        })
195      }
196    }
197    .backgroundColor(Color.Transparent)
198    .position({ x: 0, y: this.topHeight })
199    .height((this.bottomHeight - this.topHeight))
200  }
201}