1/**
2 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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.prompt'
17import { PswDialog } from '../Component/pswDialog'
18import { WifiModel } from '../entryability/model/wifiModel'
19import { WifiView } from '../Component/wifiView'
20import wifi from '@ohos.wifi'
21import wifiManager from '@ohos.wifiManager';
22import ConfigData from '../Utils/ConfigData'
23
24const TAG = "[availableWiFi]"
25
26/**
27 * available wifi page of WiFi test
28 */
29
30@Component
31export struct AvailableWifi {
32  private wifiModel: WifiModel = new WifiModel()
33  @Link wifiList!: Array<wifi.WifiScanInfo>
34  @Link linkedInfo!: wifi.WifiLinkedInfo
35  @State selectIndex: number = - 1
36  private pswDialogController: CustomDialogController = new CustomDialogController({
37    builder : PswDialog({ scanInfo : this.wifiList[ this.selectIndex ] , action : this.onAccept }) ,
38    autoCancel : true
39  })
40
41  onAccept(scanInfo: wifi.WifiScanInfo , psw: string) {
42    console.log(TAG , 'connect wifi')
43    let connectInfo: wifiManager.WifiDeviceConfig
44    connectInfo = {
45      ssid : scanInfo.ssid ,
46      preSharedKey : psw ,
47      securityType : scanInfo.securityType
48    }
49    this.wifiModel.connectNetwork(connectInfo , psw)
50  }
51
52  aboutToAppear() {
53    console.info(TAG , "wifiList:" + this.wifiList)
54  }
55
56  build() {
57    Column() {
58      Row() {
59        Text($r('app.string.wlan_available'))
60          .fontSize(22)
61          .layoutWeight(1)
62          .align(Alignment.TopStart)
63      }
64      .width(ConfigData.WH_95_100)
65
66      List() {
67        ForEach(this.wifiList , (item: wifi.WifiScanInfo , index) => {
68          ListItem() {
69            WifiView({ wifi : item })
70          }
71          .onClick(() => {
72            console.log(TAG , 'wifi click')
73            this.selectIndex = index
74            if ( this.linkedInfo !== null && item.ssid === this.linkedInfo.ssid ) {
75              prompt.showToast({ message : 'this wifi is connected' })
76              return
77            }
78            if ( item.securityType === 0 || item.securityType === 1 ) {
79              let connectInfo: wifiManager.WifiDeviceConfig
80              connectInfo = {
81                ssid : item.ssid ,
82                preSharedKey : '' ,
83                securityType : item.securityType
84              }
85              this.wifiModel.connectNetwork(connectInfo , index.toString())
86              return
87            }
88            this.pswDialogController.open()
89          })
90        } , (item: wifi.WifiScanInfo) => JSON.stringify(item))
91      }
92      .layoutWeight(1)
93      .divider({ strokeWidth : 1 , color : Color.Gray , startMargin : 10 , endMargin : 10 })
94      .margin(10)
95    }
96    .margin({ top : 15 , bottom : 100 })
97  }
98}