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