• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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 */
15import deviceManager from '@ohos.distributedHardware.deviceManager';
16import Want from '@ohos.app.ability.Want';
17import window from '@ohos.window';
18import Context from '@ohos.app.ability.common';
19
20let that: dialogPlusPage;
21let dmClass: deviceManager.DeviceManager | null;
22let TAG = '[DeviceManagerUI:InputPinDialog]==>'
23const ACTION_CANCEL_PINCODE_INPUT: number = 4
24const ACTION_DONE_PINCODE_INPUT: number = 5
25const MSG_PIN_CODE_ERROR: number = 0
26const MSG_CANCEL_PIN_CODE_INPUT: number = 3
27const MSG_DOING_AUTH: number = 4
28@Entry
29@Component
30struct dialogPlusPage {
31  @State messageTitle: string = 'PIN码连接'
32  @State messageTips: string = '请输入另一个设备显示的PIN码进行验证'
33  @State isTimes: number = 3
34  @State isShow: boolean = true
35  @State errorTips: string = 'PIN码输入错误,请重新输入(3次:还有' + this.isTimes + '次机会)'
36  @State errorTipsVisible: number = Visibility.Hidden
37  @State inputPinCode: string = ''
38  @State ok: string = '确定'
39  @State cancel: string = '取消'
40
41  aboutToAppear() {
42    console.log(TAG + 'aboutToAppear aboutToAppear')
43    that = this
44    this.initStatue()
45  }
46
47  aboutToDisappear() {
48    console.log(TAG + 'aboutToDisappear aboutToDisappear')
49    if (dmClass != null) {
50      try {
51        dmClass.off('uiStateChange')
52        dmClass.release()
53      } catch (error) {
54        console.log('dmClass release failed')
55      }
56      dmClass = null
57    }
58  }
59
60  initStatue() {
61    console.log('initStatue')
62    if (dmClass) {
63      console.log('deviceManager exist')
64      return
65    }
66    deviceManager.createDeviceManager('com.ohos.devicemanagerui.input',
67      (err: Error, dm: deviceManager.DeviceManager) => {
68      if (err) {
69        console.log('createDeviceManager err:' + JSON.stringify(err) + '  --fail:' + '${dm}')
70        return
71      }
72      dmClass = dm
73      dmClass.on('uiStateChange', (data: Record<string, string>) => {
74        console.log('uiStateChange executed, dialog closed' + JSON.stringify(data))
75        let tmpStr: Record<string, number> = JSON.parse(data.param)
76        let msg: number = tmpStr.uiStateMsg as number
77        if (msg === MSG_DOING_AUTH) {
78          this.errorTips = '正在认证中,请稍候……'
79          this.errorTipsVisible = Visibility.Visible
80          return
81        }
82        if (msg === MSG_CANCEL_PIN_CODE_INPUT) {
83          this.destruction()
84          return
85        }
86        if (msg === MSG_PIN_CODE_ERROR) {
87          this.isTimes--
88          this.errorTips = 'PIN码输入错误,请重新输入(3次:还有' + this.isTimes + '次机会)'
89          this.inputPinCode = ''
90          this.errorTipsVisible = Visibility.Visible
91        }
92    })
93    });
94  }
95
96  onDone() {
97    console.log('Done')
98    if(this.inputPinCode == null || this.inputPinCode == '') {
99      return;
100    }
101    if (dmClass) {
102      console.log('deviceManager exist')
103    } else {
104      console.log('createDeviceManager is null')
105      return
106    }
107    console.log('Done' + JSON.stringify(ACTION_DONE_PINCODE_INPUT))
108    this.setUserOperation(ACTION_DONE_PINCODE_INPUT, this.inputPinCode)
109  }
110
111  onCancel() {
112    console.log('cancle')
113    if (dmClass) {
114      console.log('deviceManager exist')
115    } else {
116      console.log('createDeviceManager is null')
117      return
118    }
119    console.log('cancle' + ACTION_CANCEL_PINCODE_INPUT)
120    this.setUserOperation(ACTION_CANCEL_PINCODE_INPUT, 'extra')
121    this.destruction()
122  }
123
124  setUserOperation(operation: number, extra: string) {
125    console.log('setUserOperation: ' + operation + 'inputPinCode'+ extra)
126    if(dmClass == null) {
127      console.log('setUserOperation: ' + 'dmClass null')
128      return;
129    }
130    try {
131      dmClass.setUserOperation(operation, extra)
132    } catch (error) {
133      console.log('dmClass setUserOperation failed')
134    }
135  }
136
137  destruction() {
138    let temporaryWindow: window.Window = AppStorage.get('inputWin') as window.Window
139    temporaryWindow.destroy()
140    let context: Context.UIAbilityContext = AppStorage.get('inputContext') as Context.UIAbilityContext
141    context.terminateSelf()
142  }
143
144  build() {
145    Row() {
146      Column() {
147          Text(this.messageTitle)
148            .fontSize(35)
149            .fontWeight(FontWeight.Bold)
150            .height('25%')
151          Text(this.messageTips)
152            .fontSize(25)
153            .fontWeight(FontWeight.Medium)
154            .height('20%')
155          Row() {}.height('2%')
156          TextInput({text: this.inputPinCode})
157            .fontSize(25)
158            .fontWeight(FontWeight.Bold)
159            .maxLength(6)
160            .height('20%')
161            .type(InputType.Number)
162            .onChange((value) => {
163              this.inputPinCode = value
164              console.info(this.inputPinCode)
165            })
166          Text(this.errorTips)
167            .fontSize(15)
168            .fontWeight(FontWeight.Medium)
169            .fontColor(0xff0000)
170            .visibility(this.errorTipsVisible)
171            .height('10%')
172          Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
173            Row() {
174              Button(this.cancel)
175                .fontSize(25)
176                .backgroundColor(0xffffff)
177                .fontColor(0x0000ff)
178                .height(50)
179                .margin({left: '10%'})
180                .border({ width: 1.5, color: (0xe7e7e7), radius: 50 })
181                .fontWeight(FontWeight.Normal)
182                .onClick(() => {
183                  this.onCancel()
184                }).width('30%')
185              Button(this.ok)
186                .fontSize(25)
187                .fontColor(0xffffff)
188                .height(50)
189                .margin({left: '20%'})
190                .border({ width: 1.5, color: (0xe7e7e7), radius: 50 })
191                .fontWeight(FontWeight.Normal)
192                .onClick(() => {
193                  this.onDone()
194                }).width('30%')
195            }.width('100%')
196          }.width('100%').height('20%')
197          Row() {}.height('3%')
198        }.width('100%')
199      }.width('100%')
200      .height('100%')
201  }
202}