• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18@Entry
19@Component
20struct dialogPlusPage {
21  @State message: string = '是否允许对端连接本机'
22  @State allow: string = '允许'
23  @State seconds: number = 60
24  @State cancel: string = '取消({{seconds}}秒)'
25  @State times: number = 0
26  @State win: any = ""
27  @State ACTION_ALLOW_AUTH: number = 0
28  @State ACTION_CANCEL_AUTH: number = 1
29  @State ACTION_AUTH_CONFIRM_TIMEOUT: number = 2
30
31  initStatue() {
32    console.log('initStatue' + "windowNum:" + globalThis.windowNum)
33    if (dmClass) {
34      console.log('deviceManager exist')
35      return
36    }
37    deviceManager.createDeviceManager('com.ohos.devicemanagerui', (err, dm) => {
38      if (err) {
39        console.log("createDeviceManager err:" + JSON.stringify(err) + '  --fail:' + JSON.stringify(dm))
40        return
41      }
42      dmClass = dm
43    })
44  }
45
46  onAllow() {
47    console.log("aalow")
48    if (dmClass) {
49      console.log('deviceManager exist')
50    } else {
51      console.log('createDeviceManager is null')
52      return
53    }
54    console.log("onAllow execute" + JSON.stringify(globalThis.abilityWant.parameters))
55    console.log("aalow" + this.ACTION_ALLOW_AUTH)
56    this.setUserOperation(this.ACTION_ALLOW_AUTH)
57    this.destruction()
58  }
59
60  onCancel() {
61    console.log("cancle")
62    if (dmClass) {
63      console.log('deviceManager exist')
64    } else {
65      console.log('createDeviceManager is null')
66      return
67    }
68    console.log("cancle" + this.ACTION_CANCEL_AUTH)
69    this.setUserOperation(this.ACTION_CANCEL_AUTH);
70    this.destruction()
71  }
72
73  setUserOperation(operation) {
74    console.log('setUserOperation: ' + operation)
75    if(dmClass == null) {
76      console.log('setUserOperation: ' + 'dmClass null')
77      return;
78    }
79    dmClass.setUserOperation(operation, "extra")
80  }
81
82  run() {
83      console.info('devicemanagerui confirmdialog run seconds:' + that.seconds )
84      that.seconds--;
85      if (that.seconds == 0) {
86          clearInterval(that.times);
87          that.times = 0;
88          that.setUserOperation(that.ACTION_AUTH_CONFIRM_TIMEOUT)
89          that.destruction();
90          console.info('click cancel times run out');
91      }
92  }
93
94  onPageShow(){
95    this.initStatue()
96    this.win = globalThis.extensionWin
97    that = this
98    if(this.times)
99       return;
100    this.times = setInterval(this.run,1000)
101  }
102
103  destruction() {
104    if (dmClass != null) {
105      dmClass.release()
106      dmClass = null
107    }
108    this.win.destroy()
109    globalThis.windowNum --
110    console.info("windowNum:" + globalThis.windowNum)
111    if(globalThis.windowNum == 0) {
112      globalThis.extensionContext.terminateSelf()
113    }
114  }
115
116
117  build() {
118    Row() {
119        Column() {
120            Text(this.message)
121              .fontSize(35)
122              .fontWeight(FontWeight.Bold)
123            Row() {}.height('2%')
124            Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
125              Row() {
126                Button("取消" + "(" + this.seconds + "秒)")
127                  .fontSize(25)
128                  .backgroundColor(0xffffff)
129                  .fontColor(0x0000ff)
130                  .margin({left: '5%'})
131                  .height(50)
132                  .border({ width: 1.5, color: (0xe7e7e7), radius: 50 })
133                  .fontWeight(FontWeight.Normal)
134                  .onClick(() => {
135                    this.onCancel()
136                  }).width('55%')
137                Button(this.allow)
138                  .fontSize(25)
139                  .height(50)
140                  .margin({left: '5%'})
141                  .border({ width: 1.5, color: (0xe7e7e7), radius: 50 })
142                  .fontWeight(FontWeight.Normal)
143                  .onClick(() => {
144                    this.onAllow()
145                  }).width('30%')
146              }.width('100%')
147            }.width('100%')
148        }.width('100%')
149      }.width('100%')
150    .height('100%')
151  }
152}