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 NfcModel from '../model/moreConnectionsImpl/NfcModel'; 17import LogUtil from '../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil'; 18import ConfigData from '../../../../../../common/utils/src/main/ets/default/baseUtil/ConfigData'; 19import HeadComponent from '../../../../../../common/component/src/main/ets/default/headComponent'; 20 21const TAG = ConfigData.TAG + 'Nfc: '; 22 23@Entry 24@Component 25struct Nfc { 26 @StorageLink('isNfcEnabled') isNfcEnabled: boolean = false; 27 @StorageLink('nfcImageWidth') nfcImageWidth: number = 0; 28 @StorageLink('nfcImageWidth') nfcImageHeight: number = 0; 29 private switchDebounceFlag: number | undefined = undefined; 30 31 build() { 32 Column() { 33 GridContainer({ gutter: ConfigData.GRID_CONTAINER_GUTTER_24, margin: ConfigData.GRID_CONTAINER_MARGIN_24 }) { 34 Column() { 35 HeadComponent({ headName: $r('app.string.NFC'), isActive: true }); 36 37 Image($r("app.media.ic_nfc")) 38 .width(this.nfcImageWidth + "px") 39 .height(this.nfcImageHeight + "px") 40 41 Text($r("app.string.nfcTips")) 42 .fontFamily('HarmonyHeiTi') 43 .fontWeight(FontWeight.Regular) 44 .fontSize($r("app.float.font_14")) 45 .lineHeight($r("app.float.lineHeight_19")) 46 .fontColor($r("app.color.font_color_182431")) 47 .margin({ 48 bottom: $r("app.float.distance_36"), 49 top: $r("app.float.distance_24"), 50 left: $r("app.float.distance_12"), 51 right: $r("app.float.distance_12") 52 }) 53 .opacity($r("app.float.opacity_100_60")) 54 .textAlign(TextAlign.Center) 55 56 Row() { 57 Text($r('app.string.NFC')) 58 .fontSize($r('sys.float.ohos_id_text_size_body1')) 59 .fontColor($r('sys.color.ohos_id_color_text_primary')) 60 .fontWeight(FontWeight.Medium) 61 62 Blank() 63 64 Toggle({ type: ToggleType.Switch, isOn: this.isNfcEnabled }) 65 .width('36vp') 66 .height('20vp') 67 .selectedColor($r("app.color.toggle_selected_color_007DFF")) 68 .margin({ left: $r('app.float.wh_value_6') }) 69 .onChange((isOn: boolean) => { 70 this.switchNfcActiveStatus(isOn); 71 }); 72 } 73 .width(ConfigData.WH_100_100) 74 .height($r('app.float.wh_value_56')) 75 .backgroundColor($r("app.color.white_bg_color")) 76 .padding({ left: $r('app.float.wh_value_12'), right: $r('app.float.wh_value_6') }) 77 .alignItems(VerticalAlign.Center) 78 .borderRadius($r('app.float.radius_24')) 79 } 80 .useSizeType({ 81 sm: { span: 4, offset: 0 }, 82 md: { span: 6, offset: 1 }, 83 lg: { span: 8, offset: 2 } 84 }) 85 } 86 .width(ConfigData.WH_100_100) 87 .height(ConfigData.WH_100_100) 88 } 89 .backgroundColor($r("sys.color.ohos_id_color_sub_background")) 90 .width(ConfigData.WH_100_100) 91 .height(ConfigData.WH_100_100) 92 } 93 94 switchNfcActiveStatus(isOn: boolean) { 95 // make the ui change quickly 96 // this.isNfcEnabled = !this.isNfcEnabled; 97 this.isNfcEnabled = isOn; 98 LogUtil.info(TAG + 'current enable status: ' + this.isNfcEnabled); 99 100 // delay the nfc status change event 101 if (this.switchDebounceFlag) { 102 clearTimeout(this.switchDebounceFlag); 103 } 104 105 this.switchDebounceFlag = setTimeout(() => { 106 if (this.isNfcEnabled) { 107 let enableNFC = NfcModel.openNfc(); 108 LogUtil.info(TAG + 'enable nfc: ' + enableNFC); 109 } else { 110 let disableNFC = NfcModel.closeNfc(); 111 LogUtil.info(TAG + 'disable nfc: ' + disableNFC); 112 } 113 this.switchDebounceFlag = undefined; 114 }, 1500); 115 } 116 117 aboutToAppear() { 118 LogUtil.info(TAG + 'aboutToAppear in'); 119 if (!this.switchDebounceFlag) { 120 NfcModel.registerNfcStatusObserver((code: boolean) => { 121 LogUtil.info(TAG + 'NFC status code: ' + code); 122 }) 123 } 124 125 // init wifi active status 126 this.isNfcEnabled = NfcModel.isNfcOpen(); 127 LogUtil.info(TAG + 'aboutToAppear out'); 128 } 129}