• 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 gThis: dialogPlusPage;
22
23const ACTION_ALLOW_AUTH_ONCE: number = 0
24const ACTION_CANCEL_AUTH: number = 1
25const ACTION_AUTH_CONFIRM_TIMEOUT: number = 2
26const ACTION_ALLOW_AUTH_ALWAYS: number = 6
27
28@Entry
29@Component
30struct dialogPlusPage {
31  @State message: string = '是否信任此设备?'
32  @State allowOnce: string = '仅本次信任'
33  @State allowAlways: string = '始终信任'
34  @State peerAppOperation: string = '想要连接本机。'
35  @State peerCustomDescription: string = ''
36  @State peerDeviceName: string = ''
37  @State peerDeviceType: number = 0
38  @State seconds: number = 60
39  @State times: number = 0
40  @State selectIndex: number = 0
41  @State isAvailableType: boolean = false
42
43  initStatue() {
44    let globalWindowNum: number = AppStorage.get('confirmWindowNum') as number
45    console.log('initStatue' + 'windowNum:' + globalWindowNum)
46    if (dmClass) {
47      console.log('deviceManager exist')
48      return
49    }
50    deviceManager.createDeviceManager('com.ohos.devicemanagerui.confirm',
51      (err: Error, dm: deviceManager.DeviceManager) => {
52      if (err) {
53        console.log('createDeviceManager err:' + JSON.stringify(err) + ' --fail:' + JSON.stringify(dm))
54        return
55      }
56      dmClass = dm
57    })
58  }
59
60  onAllowOnce() {
61    console.log('allow once')
62    if (dmClass == null) {
63      console.log('createDeviceManager is null')
64      return
65    }
66
67    let globalWant: Want = AppStorage.get('abilityWant') as Want
68    console.log('onAllowOnce execute' + JSON.stringify(globalWant.parameters))
69    console.log('allow once' + ACTION_ALLOW_AUTH_ONCE)
70    this.setUserOperation(ACTION_ALLOW_AUTH_ONCE)
71    this.destruction()
72  }
73
74  onAllowAlways() {
75    console.log('allow always')
76    if (dmClass == null) {
77      console.log('createDeviceManager is null')
78      return
79    }
80
81    let globalWant: Want = AppStorage.get('abilityWant') as Want
82    console.log('onAllowAlways execute' + JSON.stringify(globalWant.parameters))
83    console.log('allow always' + ACTION_ALLOW_AUTH_ALWAYS)
84    this.setUserOperation(ACTION_ALLOW_AUTH_ALWAYS)
85    this.destruction()
86  }
87
88  onCancel() {
89    console.log('cancel')
90    if (dmClass == null) {
91      console.log('createDeviceManager is null')
92      return
93    }
94
95    console.log('cancel' + ACTION_CANCEL_AUTH)
96    this.setUserOperation(ACTION_CANCEL_AUTH)
97    this.destruction()
98  }
99
100  setUserOperation(operation: number) {
101    console.log('setUserOperation: ' + operation)
102    if(dmClass == null) {
103      console.log('setUserOperation: ' + 'dmClass null')
104      return;
105    }
106    try {
107      dmClass.setUserOperation(operation, 'extra')
108    } catch (error) {
109      console.log('dmClass setUserOperation failed')
110    }
111  }
112
113  run() {
114    console.info('devicemanagerui confirm dialog run seconds:' + gThis.seconds)
115    gThis.seconds--;
116    if (gThis.seconds == 0) {
117      clearInterval(gThis.times)
118      gThis.times = 0
119      gThis.setUserOperation(ACTION_AUTH_CONFIRM_TIMEOUT)
120      gThis.destruction()
121      console.info('click cancel times run out')
122    }
123  }
124
125  onPageShow() {
126    console.log('onPageShow')
127
128    let globalWant: Want = AppStorage.get('abilityWant') as Want
129    if (globalWant != null && globalWant.parameters != undefined && globalWant.parameters.deviceName != null) {
130      this.peerDeviceName = globalWant.parameters.deviceName as string
131      console.log('peerDeviceName is ' + this.peerDeviceName)
132    }
133
134    if (globalWant != null && globalWant.parameters != undefined && globalWant.parameters.deviceType != null) {
135      this.peerDeviceType = globalWant.parameters.deviceType as number
136      console.log('peerDeviceType is ' + this.peerDeviceType)
137    }
138
139    if (globalWant != null && globalWant.parameters != undefined && globalWant.parameters.appOperation != null) {
140      this.peerAppOperation = globalWant.parameters.appOperation as string
141      console.log('peerAppOperation is ' + this.peerAppOperation)
142    }
143
144    if (globalWant != null && globalWant.parameters != undefined && globalWant.parameters.customDescription != null) {
145      this.peerCustomDescription = globalWant.parameters.customDescription as string
146      console.log('peerCustomDescription is ' + this.peerCustomDescription)
147    }
148
149    this.initStatue()
150    gThis = this
151    if(this.times) {
152      return;
153    }
154    this.times = setInterval(this.run, 1000)
155  }
156
157  destruction() {
158    if (dmClass != null) {
159      try {
160        dmClass.release()
161        dmClass = null
162      } catch (error) {
163        console.log('dmClass release failed')
164      }
165    }
166    let temporaryWindow: window.Window = AppStorage.get('confirmWin') as window.Window
167    temporaryWindow.destroy()
168    let globalWindowNum: number = AppStorage.get('confirmWindowNum') as number
169    globalWindowNum--
170    AppStorage.SetOrCreate('confirmWindowNum', globalWindowNum)
171    console.info('confirmWindowNum:' + globalWindowNum)
172    if(globalWindowNum == 0) {
173      let context: Context.UIAbilityContext = AppStorage.get('confirmContext') as Context.UIAbilityContext
174      context.terminateSelf()
175    }
176  }
177
178  getImages(peerdeviceType: number): Resource {
179    console.info('peerdeviceType is ' + peerdeviceType)
180    if (peerdeviceType == deviceManager.DeviceType.SPEAKER) {
181      this.isAvailableType = true
182      return $r('app.media.ic_device_soundx')
183    } else if (peerdeviceType == deviceManager.DeviceType.PHONE) {
184      this.isAvailableType = true
185      return $r('app.media.ic_public_devices_phone')
186    } else if (peerdeviceType == deviceManager.DeviceType.TABLET) {
187      this.isAvailableType = true
188      return $r('app.media.ic_device_pad')
189    } else if (peerdeviceType == deviceManager.DeviceType.WEARABLE) {
190      this.isAvailableType = true
191      return $r('app.media.ic_device_watch')
192    } else if (peerdeviceType == deviceManager.DeviceType.CAR) {
193      this.isAvailableType = true
194      return $r('app.media.ic_public_car')
195    } else if (peerdeviceType == deviceManager.DeviceType.TV) {
196      this.isAvailableType = true
197      return $r('app.media.ic_device_smartscreen')
198    } else {
199      this.isAvailableType = false
200      return $r('app.media.icon')
201    }
202  }
203
204  build() {
205    Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center, direction: FlexDirection.Column }) {
206      Image(this.getImages(this.peerDeviceType))
207      .width(24)
208      .height(24)
209      .margin({bottom: 16})
210      .visibility(this.isAvailableType == false? Visibility.None : Visibility.Visible)
211      Text(this.peerDeviceName + this.peerAppOperation + this.message)
212        .textAlign(TextAlign.Start)
213        .fontSize(16)
214        .maxLines(3)
215        .textOverflow({ overflow: TextOverflow.Ellipsis })
216        .width('100%')
217        .opacity(0.9)
218      Text(this.peerCustomDescription)
219        .textAlign(TextAlign.Start)
220        .textOverflow({ overflow: TextOverflow.Ellipsis })
221        .fontSize(14)
222        .maxLines(3)
223        .constraintSize({ maxHeight: 45 })
224        .margin({bottom: 16})
225        .width('100%')
226        .opacity(0.6)
227      Button(this.allowAlways)
228        .margin({bottom: 2})
229        .onClick(() => {
230          this.onAllowAlways()
231        })
232        .onTouch((event?: TouchEvent | undefined) => {
233          if (event != undefined && event.type === TouchType.Down) {
234            this.selectIndex = 0
235          }
236        })
237        .backgroundColor(this.selectIndex == 0 ? 0x0000ff : 0xffffff)
238        .fontColor(this.selectIndex == 0 ? 0xffffff : 0x0000ff)
239        .height(30)
240        .width('100%')
241      Button(this.allowOnce)
242        .margin({bottom: 2, left: 16, right: 16})
243        .onClick(() => {
244          this.onAllowOnce()
245        })
246        .onTouch((event?: TouchEvent | undefined) => {
247          if (event != undefined && event.type === TouchType.Down) {
248            this.selectIndex = 1
249          }
250        })
251        .backgroundColor(this.selectIndex == 1 ? 0x0000ff : 0xffffff)
252        .fontColor(this.selectIndex == 1 ? 0xffffff : 0x0000ff)
253        .height(30)
254        .width('100%')
255      Button('不信任(' + this.seconds + '秒)')
256        .margin({left: 16, right: 16})
257        .backgroundColor(this.selectIndex == 2 ? 0x0000ff : 0xffffff)
258        .fontColor(this.selectIndex == 2 ? 0xffffff : 0x0000ff)
259        .onClick(() => {
260          this.onCancel()
261        })
262        .onTouch((event?: TouchEvent | undefined) => {
263          if (event != undefined && event.type === TouchType.Down) {
264            this.selectIndex = 2
265          }
266        })
267        .height(30)
268        .width('100%')
269    }
270    .padding({top:12, bottom:12, left: 16, right: 16})
271    .height('100%')
272    .width('100%')
273    .borderRadius(24)
274  }
275}