• 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 Context from '@ohos.app.ability.common';
17import Want from '@ohos.app.ability.Want';
18import window from '@ohos.window';
19
20let dmClass: deviceManager.DeviceManager | null;
21let TAG = '[DeviceManagerUI:PinDialog]==>'
22
23const ACTION_CANCEL_PINCODE_DISPLAY: number = 3
24const MSG_CANCEL_PIN_CODE_SHOW: number = 2
25@Entry
26@Component
27struct dialogPlusPage {
28  @State messageTitle: string = 'PIN码连接'
29  @State messageTips: string = '请在设备端输入连接码进行验证'
30  @State pinCode: string = ''
31  @State allow: string = '允许'
32  @State cancel: string = '取消'
33  @State isShow: boolean = true
34
35  aboutToAppear() {
36    this.initStatue()
37    console.log('aboutToAppear execute')
38    let globalWant: Want = AppStorage.get('abilityWant') as Want
39    if (globalWant != null && globalWant.parameters != undefined) {
40      this.pinCode = globalWant.parameters['PinCode'] as string
41    }
42  }
43
44  aboutToDisappear() {
45    console.log(TAG + 'aboutToDisappear executed')
46    if (dmClass != null) {
47      try {
48        dmClass.off('uiStateChange')
49        dmClass.release()
50      } catch (error) {
51        console.log('dmClass release failed')
52      }
53      dmClass = null
54    }
55  }
56
57  initStatue() {
58    let globalWindowNum: number = AppStorage.get('pinWindowNum') as number
59    console.log('initStatue' + 'pinWindowNum:' + globalWindowNum)
60    if (dmClass) {
61      console.log('deviceManager exist')
62      return
63    }
64    deviceManager.createDeviceManager('com.ohos.devicemanagerui.pin',
65      (err: Error, dm: deviceManager.DeviceManager) => {
66      if (err) {
67        console.log('createDeviceManager err:' + JSON.stringify(err) + '  --fail:' + JSON.stringify(dm))
68        return
69      }
70      dmClass = dm
71      dmClass.on('uiStateChange', (data: Record<string, string>) => {
72        console.log('uiStateChange executed, dialog closed' + JSON.stringify(data))
73        let tmpStr: Record<string, number> = JSON.parse(data.param)
74        let msg: number = tmpStr.uiStateMsg as number
75        if (msg === MSG_CANCEL_PIN_CODE_SHOW) {
76          this.destruction()
77        }
78      })
79    });
80  }
81
82  setUserOperation(operation: number) {
83    console.log('setUserOperation: ' + operation)
84    if(dmClass == null) {
85      console.log('setUserOperation: ' + 'dmClass null')
86      return;
87    }
88    try {
89      dmClass.setUserOperation(operation, 'extra')
90    } catch (error) {
91      console.log('dmClass setUserOperation failed')
92    }
93  }
94
95  destruction() {
96    let temporaryWindow: window.Window = AppStorage.get('pinWin') as window.Window
97    temporaryWindow.destroy()
98    let context: Context.UIAbilityContext = AppStorage.get('pinContext') as Context.UIAbilityContext
99    context.terminateSelf()
100  }
101
102  build() {
103    Row() {
104      Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
105        Column() {
106          Text(this.messageTitle)
107            .fontSize(35)
108            .fontWeight(FontWeight.Bold)
109          Text(this.messageTips)
110            .fontSize(30)
111            .fontWeight(FontWeight.Medium)
112          Row() {}.height('2%')
113          Text(this.pinCode)
114            .fontSize(30)
115            .fontWeight(FontWeight.Bold)
116          Row() {}.height('2%')
117          Button(this.cancel)
118            .fontSize(25)
119            .fontColor(0xffffff)
120            .border({ width: 1.5, color: (0xe7e7e7), radius: 50 })
121            .fontWeight(FontWeight.Normal)
122            .onClick(() => {
123              this.setUserOperation(ACTION_CANCEL_PINCODE_DISPLAY)
124              this.destruction()
125            })
126        }.width('100%')
127      }.width('100%')
128      .height('100%')
129    }.width('100%')
130  }
131}