• 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 */
15
16import prompt from '@ohos.promptAction'
17import Logger from '../model/Logger'
18import { PswDialog } from '../component/PswDialog'
19import { WifiModel } from '../model/WifiModel'
20import { WifiView } from '../component/WifiView'
21import WifiDataSource from '../component/BasicDataSource'
22import wifi from '@ohos.wifiManager'
23
24const TAG = 'AvailableWiFi'
25let self = null
26
27@Component
28export struct AvailableWifi {
29  private wifiModel = new WifiModel()
30  private linkedInfo: wifi.WifiLinkedInfo = null
31  @StorageLink('wifiList') @Watch('wifiListRefresh') wifiList: Array<wifi.WifiScanInfo> = []
32  @State wifiDataResource: WifiDataSource = new WifiDataSource(this.wifiList)
33  @State scanInfo: wifi.WifiScanInfo = undefined
34  private pswDialogController: CustomDialogController = new CustomDialogController({
35    builder: PswDialog({ scanInfo: $scanInfo, action: this.onAccept }),
36    autoCancel: true
37  })
38
39  build() {
40    List() {
41      ListItem() {
42        Row() {
43          Text($r('app.string.available_wlan'))
44            .fontSize(22)
45            .layoutWeight(1)
46        }
47        .id('validWlan')
48        .width('100%')
49      }
50
51      LazyForEach(this.wifiDataResource, (item, index) => {
52        ListItem() {
53          WifiView({ wifi: item })
54        }
55        .id(`Wifi${index}`)
56        .onClick(() => {
57          Logger.info(TAG, 'wifi click')
58          this.scanInfo = item
59          if (this.linkedInfo !== null && item.ssid === this.linkedInfo.ssid) {
60            prompt.showToast({ message: 'this wifi is connected' })
61            return
62          }
63          if (item.securityType === 0 || item.securityType === 1) {
64            this.wifiModel.connectNetwork(item, '')
65            return
66          }
67          this.pswDialogController.open()
68        })
69      }, item => JSON.stringify(item))
70    }
71    .width('100%')
72    .height('100%')
73    .padding({ left: 16, right: 16 })
74    .layoutWeight(1)
75    .divider({ strokeWidth: 1, color: Color.Gray, startMargin: 10, endMargin: 10 })
76    .margin({ top: 10 })
77  }
78
79  onAccept(scanInfo, psw) {
80    Logger.info(TAG, 'connect wifi')
81    self.wifiModel.connectNetwork(scanInfo, psw)
82  }
83
84  aboutToAppear() {
85    self = this
86  }
87
88  wifiListRefresh() {
89    this.wifiDataResource['dataArray'] = this.wifiList
90    this.wifiDataResource.notifyDataReload()
91  }
92}