1/*
2 * Copyright (c) 2021-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 */
15
16import mWifiManger from '../wifiModel'
17import Log from '../../../../../../../common/src/main/ets/default/Log'
18import EventManager from "../../../../../../../common/src/main/ets/default/event/EventManager"
19import {obtainStartAbility} from "../../../../../../../common/src/main/ets/default/event/EventUtil"
20import iconTitleBase from '../../../../../../../common/src/main/ets/template/iconTitleBase'
21import Constants from '../common/constants'
22
23const TAG = 'Control-wifiComponent'
24
25@Component
26export default struct WifiComponent {
27  private IconInfo: any[] = [
28    $r('app.media.wifi_d'),
29    $r('app.media.wifi'),
30  ]
31  @StorageLink('wifiName') wifiName: string = Constants.DEFAULT_WIFI_NAME
32  @StorageLink('wifiOpenStatus') wifiOpenStatus: boolean = Constants.DEFAULT_WIFI_OPEN_STATUS
33
34  mClickEvent() {
35    Log.showInfo(TAG, `mClickEvent, wifiOpenStatus: ${this.wifiOpenStatus}`);
36
37    if (this.wifiOpenStatus == false) {
38      Log.showInfo(TAG, `WLAN status is closing:${this.wifiOpenStatus} “no”, set WLAN status enableWifi`);
39      mWifiManger.enableWifi()
40    } else {
41      Log.showInfo(TAG, `WLAN status is opening:${this.wifiOpenStatus} “yes”,se WLAN status disableWifi`);
42      mWifiManger.disableWifi()
43    }
44  }
45
46  mLongClickEvent() {
47    Log.showInfo(TAG, `mLongClickEvent, wifiOpenStatus: ${this.wifiOpenStatus}`);
48    EventManager.publish(obtainStartAbility('com.ohos.settings', 'com.ohos.settings.WifiAbility'))
49  }
50
51  aboutToAppear() {
52    Log.showDebug(TAG, 'aboutToAppear');
53    mWifiManger.initWifiModel();
54  }
55
56  aboutToDisappear() {
57    Log.showDebug(TAG, 'aboutToDisappear');
58  }
59
60  build() {
61    Column() {
62      iconTitleBase({
63        iconOff: this.IconInfo[0],
64        iconOn: this.IconInfo[1],
65        mTitle: $wifiName,
66        changeSwitch: $wifiOpenStatus,
67        mClickEvent: () => this.mClickEvent(),
68        mLongClickEvent: () => this.mLongClickEvent()
69      })
70    }.width('100%')
71    .height('100%')
72  }
73}