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 { NetworkMobile } from '../model/NetworkMobile' 17 18@Component 19export struct NetWork { 20 @State slotId: number = -1 21 @State cellularData: boolean = false 22 @State cellularDataRoaming: boolean = false 23 @State flowState: Resource = $r('app.string.none') 24 @State mobileDataState: Resource = $r('app.string.unknown') 25 private netWorkMobile: NetworkMobile = new NetworkMobile() 26 27 async aboutToAppear() { 28 this.slotId = await this.netWorkMobile.getDefaultCellularDataSlotId() 29 this.cellularData = await this.netWorkMobile.isCellularDataEnabled() 30 this.cellularDataRoaming = await this.netWorkMobile.isCellularDataRoamingEnabled(this.slotId) 31 this.flowState = await this.netWorkMobile.getCellularDataFlowType() 32 this.mobileDataState = await this.netWorkMobile.getCellularDataState() 33 } 34 35 @Builder dataState($$: { 36 key: string, 37 text: Resource, 38 flag: boolean 39 }) { 40 Row() { 41 Text($$.text) 42 .margin({ left: 10 }) 43 .fontSize(20) 44 .width('55%') 45 .textAlign(TextAlign.Start) 46 47 Text($$.flag === true ? $r('app.string.start') : $r('app.string.end')) 48 .id($$.key) 49 .fontSize(20) 50 .margin({ left: 10, right: 6 }) 51 .textAlign(TextAlign.Center) 52 .layoutWeight(1) 53 } 54 .margin(10) 55 .height('8%') 56 } 57 58 @Builder netWorkState($$: { 59 key: string, 60 info: Resource, 61 result: Resource 62 }) { 63 Column() { 64 Row() { 65 Text($$.info) 66 .margin({ left: 10 }) 67 .fontSize(20) 68 .width('55%') 69 .textAlign(TextAlign.Start) 70 .layoutWeight(1) 71 72 Blank() 73 } 74 75 Row() { 76 Text($$.result) 77 .id($$.key) 78 .margin({ left: 10 }) 79 .fontSize(18) 80 .width('55%') 81 .textAlign(TextAlign.Start) 82 .layoutWeight(1) 83 84 Blank() 85 } 86 } 87 .margin(10) 88 .height('8%') 89 } 90 91 build() { 92 Column() { 93 Row() { 94 Text($r('app.string.sim')) 95 .margin({ left: 10 }) 96 .fontSize(20) 97 .width('60%') 98 .textAlign(TextAlign.Start) 99 Row() { 100 Text($r('app.string.simOne')) 101 .id('simOne') 102 .margin(5) 103 .fontSize(20) 104 .width('40%') 105 .borderRadius(10) 106 .textAlign(TextAlign.Center) 107 .backgroundColor(this.slotId === 0 ? '#0D9FFB' : undefined) 108 109 Text($r('app.string.simTwo')) 110 .id('simTwo') 111 .margin(5) 112 .fontSize(20) 113 .width('40%') 114 .borderRadius(10) 115 .textAlign(TextAlign.Center) 116 .backgroundColor(this.slotId === 1 ? '#0D9FFB' : undefined) 117 } 118 .width('35%') 119 .borderRadius(10) 120 .backgroundColor('#fff5f5f5') 121 } 122 .height('8%') 123 .margin({ right: 10, bottom: 10 }) 124 125 Divider() 126 127 this.dataState({ key: 'mobileData', text: $r('app.string.mobile'), flag: this.cellularData }) 128 129 Divider() 130 131 this.dataState({ key: 'roaming', text: $r('app.string.roaming'), flag: this.cellularDataRoaming }) 132 133 Divider() 134 135 this.netWorkState({ key: 'dataFlowType', info: $r('app.string.flow'), result: this.flowState }) 136 137 Divider() 138 139 this.netWorkState({ key: 'connectState', info: $r('app.string.connectData'), result: this.mobileDataState }) 140 } 141 .width('100%') 142 .height('100%') 143 } 144}