1/** 2 * Copyright (c) 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 16/** 17 * @file: Mobile Network Home page 18 */ 19import { addAirPlaneModeListener, removeAirPlaneModeListener, queryAirPlaneMode } from '../common/utils/AirplaneMode'; 20import publiccontent from '../common/components/publiccontent'; 21import item from '../common/components/listItem/listItem'; 22import call from '@ohos.telephony.call'; 23import { registerSimStateChange, getMaxSimCount } from '../model/registerSimStateApi'; 24import { getSimStateCardOne, getSimStateCardTwo } from '../common/model/getSimStateApi'; 25import { getSimCardOnePhoneNumber, getSimCardTwoPhoneNumber } from '../common/model/getSimTelephoneNumberApi'; 26import { 27 isCellularDataRoamingEnabledCardOne, 28 isCellularDataRoamingEnabledCardTwo, 29 getSupportNetwork 30} from '../common/model/getCellularDataRoamingEnabledApi'; 31import { setPreferredNetwork } from '../common/model/setPreferredNetworkApi'; 32import { getPreferredNetwork } from '../common/model/getPreferredNetworkModeApi'; 33import { 34 isCellularDataEnabled, 35 enableCellularData, 36 disableCellularData, 37 getOperatorName 38} from '../model/mobileDataStatus'; 39import HeadComponent from '../common/components/headComponent'; 40import { SubHeader } from '../common/components/subHeader'; 41import LogUtils from '../common/utils/LogUtils'; 42 43const TAG = "Index"; 44 45@Entry 46@Component 47struct Index { 48 scroller: Scroller = new Scroller(); 49 @State byValueWLMS: Resource = publiccontent.strings.fourAuto; 50 @State publicheader: Resource = publiccontent.strings.mobileData; 51 @State isLoading: boolean = true; 52 @State moisBtn: boolean = true; 53 @State isBtn: boolean = true; 54 @State gqBtn: boolean = true; 55 @State moislastone: boolean = true; 56 @State isSupport: boolean = true; 57 @State mobileDataDisabled: boolean = false; 58 @State mobileDataDisabled2: boolean = false; 59 // Mobile data switch 60 @State isDataEnable: boolean = true; 61 // Mobile AirPlane Mode 62 @State isAirPlaneMode: boolean = false; 63 // Data roaming switch 64 @State dataRoamSwitchCardOne: boolean = false; 65 @State dataRoamSwitchCardTwo: boolean = false; 66 // HD call switch 67 @State volteSwitch: boolean = false; 68 // Card I status 69 @State simStateStatusCardOne: boolean = true; 70 // Card 2 status 71 @State simStateStatusCardTwo: boolean = false; 72 @State ControlswitchWLMS: boolean = false; 73 @State slotId: number = 0; 74 @State dialogTitle: Resource = publiccontent.strings.preferredNetworkMode; 75 setCache_cun_name = 'DISTRIBUTEDDATA_CUN_NAME'; 76 setCache_wl_name = 'SETCACHE_WL_NAME'; 77 @State enable5g: boolean = false; 78 @StorageLink("imsOne")enableISM: boolean = false; 79 @State enableISM2: boolean = false; 80 @State TelephoneNumber: string = ''; 81 @State TelephoneNumber2?: string = ''; 82 @State ceshi: boolean = false; 83 @State cardOne: number = 0; 84 @State cardTwo: number = 1; 85 86 /** 87 * Get network mode 88 * 89 * @param {Object} slotId - call slotId 90 */ 91 async getPreferredNetwork(slotId) { 92 const res = await getPreferredNetwork(this.slotId); 93 if (res == 0) { 94 this.byValueWLMS = $r('app.string.mobile_data_5g'); 95 } else if (res == 5) { 96 this.byValueWLMS = $r("app.string.mobile_data_4g"); 97 } else if (res == 6) { 98 this.byValueWLMS = $r("app.string.mobile_data_3g"); 99 } else if (res == 1) { 100 this.byValueWLMS = $r("app.string.mobile_data_2g"); 101 } 102 } 103 104 /** 105 * This interface is used to obtain sim cardOne status 106 */ 107 getSimStateDataCardOne() { 108 getSimStateCardOne().then((res: number) => { 109 LogUtils.i(TAG, "getSimStateData Card1 :success " + JSON.stringify(res)) 110 const simState = { 111 SIM_STATE_UNKNOWN: 0, 112 SIM_STATE_NOT_PRESENT: 1, 113 SIM_STATE_LOCKED: 2, 114 SIM_STATE_NOT_READY: 3, 115 SIM_STATE_READY: 4, 116 SIM_STATE_LOADED: 5 117 }; 118 if (res == simState.SIM_STATE_LOADED || res == simState.SIM_STATE_READY) { 119 this.simStateStatusCardOne = true; 120 } else { 121 this.simStateStatusCardOne = false; 122 } 123 }).catch((err) => { 124 this.simStateStatusCardOne = false; 125 LogUtils.i(TAG, 'getSimStateData Card1 :console.error() ' + JSON.stringify(err.message)); 126 }); 127 } 128 129 /** 130 * This interface is used to obtain sim cardTwo status 131 */ 132 getSimStateDataCardTwo() { 133 getSimStateCardTwo().then((res: number) => { 134 LogUtils.i(TAG, "getSimState Card2 :success then" + JSON.stringify(res)); 135 const simState = { 136 SIM_STATE_UNKNOWN: 0, 137 SIM_STATE_NOT_PRESENT: 1, 138 SIM_STATE_LOCKED: 2, 139 SIM_STATE_NOT_READY: 3, 140 SIM_STATE_READY: 4, 141 SIM_STATE_LOADED: 5 142 }; 143 if (res == simState.SIM_STATE_LOADED || res == simState.SIM_STATE_READY) { 144 this.simStateStatusCardTwo = true; 145 } else { 146 this.simStateStatusCardTwo = false; 147 } 148 }).catch((err) => { 149 this.simStateStatusCardTwo = false; 150 LogUtils.i(TAG, 'getSimState Card2 :console.error() ' + JSON.stringify(err.message)); 151 }); 152 } 153 154 getSupportNetwork(slotId) { 155 getSupportNetwork(slotId).then((res) => { 156 if (res) { 157 this.isSupport = true; 158 } else { 159 this.isSupport = false; 160 } 161 }).catch((err) => { 162 }); 163 } 164 165 /** 166 * Get mobile phone number 167 */ 168 getSimCardOnePhoneNumber() { 169 getSimCardOnePhoneNumber().then((res) => { 170 LogUtils.i(TAG, "getSimTelephoneNumber card1 other success" + JSON.stringify(res)); 171 this.TelephoneNumber = res; 172 }).catch((err) => { 173 LogUtils.i(TAG, "getSimTelephoneNumber card1 other catch" + JSON.stringify(err)); 174 }); 175 } 176 177 /** 178 * Get mobile phone number 179 */ 180 getSimCardTwoPhoneNumber() { 181 getSimCardTwoPhoneNumber().then((res) => { 182 LogUtils.i(TAG, "getSimTelephoneNumber card2 other success" + JSON.stringify(res)); 183 this.TelephoneNumber2 = res; 184 }).catch((err) => { 185 LogUtils.i(TAG, "getSimTelephoneNumber card2 other catch" + JSON.stringify(err)); 186 }); 187 } 188 189 /** 190 * Get mobile data on or off 191 */ 192 getCellularDataState() { 193 isCellularDataEnabled().then((data) => { 194 LogUtils.i(TAG, "isCellularDataEnabled success: isON:" + JSON.stringify(data)); 195 this.isDataEnable = data; 196 }).catch((error) => { 197 LogUtils.i(TAG, "isCellularDataEnabled error catch" + JSON.stringify(error)); 198 }); 199 } 200 201 // init AirPlane Mode 202 initAirPlaneMode() { 203 LogUtils.i(TAG, 'initAirPlaneMode'); 204 try { 205 addAirPlaneModeListener((data) => { 206 LogUtils.i(TAG, 'initAirPlaneMode callback'); 207 this.isAirPlaneMode = data == 1 ? true : false; 208 }); 209 } catch(err) { 210 LogUtils.e(TAG,`initAirPlaneMode err = ${JSON.stringify(err)}`); 211 } 212 } 213 214 // query AirPlane Mode 215 getAirPlaneMode() { 216 LogUtils.i(TAG, 'getAirPlaneMode'); 217 try { 218 queryAirPlaneMode((data) => { 219 LogUtils.i(TAG, 'getAirPlaneMode callback'); 220 this.isAirPlaneMode = data == 1 ? true : false; 221 }); 222 } catch(err) { 223 LogUtils.e(TAG,`getAirPlaneMode err = ${JSON.stringify(err)}`); 224 } 225 } 226 227 addRegisterSimStateChange(slotId) { 228 registerSimStateChange(slotId, async () => { 229 this.getCellularDataRoamingEnabled(); 230 this.getSimStateDataCardOne(); 231 this.getSimStateDataCardTwo(); 232 this.getPreferredNetwork(slotId); 233 this.getSupportNetwork(slotId); 234 this.getSimCardOnePhoneNumber(); 235 this.getSimCardTwoPhoneNumber(); 236 this.getCellularDataState(); 237 this.isImsSwitchEnabled(0); 238 if (getMaxSimCount() === 2) { 239 this.isImsSwitchEnabled(1); 240 } 241 }) 242 } 243 244 isImsSwitchEnabled(slotId) { 245 call.isImsSwitchEnabled(slotId).then((res: boolean) => { 246 if (slotId) { 247 this.enableISM2 = res; 248 LogUtils.i(TAG, "isImsSwitchEnable enable 1:" + JSON.stringify(this.enableISM2)); 249 } else { 250 this.enableISM = res; 251 LogUtils.i(TAG, "isImsSwitchEnable enable 0:" + JSON.stringify(this.enableISM)); 252 } 253 }).catch((err) => { 254 if (slotId) { 255 this.enableISM2 = true; 256 LogUtils.i(TAG, "isImsSwitchEnable err 1:" + JSON.stringify(this.enableISM2)); 257 } else { 258 this.enableISM = true; 259 LogUtils.i(TAG, "isImsSwitchEnable err 0:" + JSON.stringify(this.enableISM)); 260 } 261 LogUtils.i(TAG, "isImsSwitchEnabled card catch:" + JSON.stringify(err)); 262 }); 263 } 264 265 aboutToAppear() { 266 LogUtils.i(TAG, "aboutToAppear") 267 this.initAirPlaneMode(); 268 if (getMaxSimCount() === 2) { 269 this.addRegisterSimStateChange(1); 270 this.isImsSwitchEnabled(1); 271 this.getSimStateDataCardTwo(); 272 } 273 this.addRegisterSimStateChange(0); 274 //Get data roaming status 275 this.getCellularDataRoamingEnabled(); 276 //Get HD call status 277 this.isImsSwitchEnabled(0); 278 this.getSimStateDataCardOne(); 279 //Get network mode 280 this.getPreferredNetwork(this.slotId); 281 getOperatorName().then((res) => { 282 }) 283 this.getSupportNetwork(this.slotId); 284 //card number acquire 285 this.getSimCardOnePhoneNumber(); 286 this.getSimCardTwoPhoneNumber(); 287 this.getCellularDataState(); 288 } 289 290 onPageShow() { 291 this.isImsSwitchEnabled(0); 292 this.getAirPlaneMode(); 293 } 294 295 onPageHide() { 296 removeAirPlaneModeListener(); 297 } 298 299 /** 300 * Set network mode 301 */ 302 radioChange(slotId, v) { 303 const res = setPreferredNetwork(slotId, v); 304 LogUtils.i(TAG, "radioChange res:" + JSON.stringify(res)); 305 } 306 307 getCellularDataRoamingEnabled() { 308 isCellularDataRoamingEnabledCardOne().then((res) => { 309 LogUtils.i(TAG,"getCellularDataRoamingEnabled card one success then:" + JSON.stringify(res)); 310 this.dataRoamSwitchCardOne = res; 311 }).catch((err) => { 312 this.dataRoamSwitchCardOne = false; 313 LogUtils.i(TAG, "enableCellularDataRoaming card one catch:" + JSON.stringify(err)); 314 }); 315 if (getMaxSimCount() === 2) { 316 isCellularDataRoamingEnabledCardTwo().then((res) => { 317 LogUtils.i(TAG,"getCellularDataRoamingEnabled card two success then:" + JSON.stringify(res)); 318 this.dataRoamSwitchCardTwo = res; 319 }).catch((err) => { 320 this.dataRoamSwitchCardTwo = false; 321 LogUtils.i(TAG, "enableCellularDataRoaming card two catch:" + JSON.stringify(err)); 322 }); 323 } 324 } 325 326 build() { 327 GridRow({ columns: { sm: 4, md: 8, lg: 12 }, gutter: { x: 12 } }) { 328 GridCol({ span: { sm: 4, md: 8, lg: 8 }, offset: { sm: 0, md: 1, lg: 2 } }) { 329 Column() { 330 Column() { 331 332 HeadComponent(); 333 334 Stack({ alignContent: Alignment.TopStart }) { 335 Scroll(this.scroller) { 336 Column() { 337 SubHeader({ titleContent: $r('app.string.mobile_data_general'), subTitleContent: '' }); 338 339 Flex({ 340 direction: FlexDirection.Row, 341 justifyContent: FlexAlign.SpaceBetween, 342 alignItems: ItemAlign.Center 343 }) { 344 Column() { 345 Row() { 346 Text($r('app.string.mobile_data')) 347 .fontSize(16) 348 .fontWeight(FontWeight.Medium) 349 .fontFamily('HarmonyHeiTi') 350 .lineHeight(22) 351 .fontColor($r('app.color.font_color_182431')) 352 } 353 354 Row() { 355 Text($r('app.string.mobile_data_charges')) 356 .fontSize(14) 357 .fontWeight(FontWeight.Regular) 358 .fontFamily('HarmonyHeiTi') 359 .fontColor('#555') 360 .opacity(0.6) 361 .lineHeight(19) 362 .maxLines(2) 363 } 364 .margin({ top: 2, right: 26 }) 365 } 366 .alignItems(HorizontalAlign.Start) 367 368 Toggle({ type: ToggleType.Switch, isOn: this.isDataEnable }) 369 .width(36) 370 .height(20) 371 .enabled((this.simStateStatusCardOne || this.simStateStatusCardTwo) && !this.isAirPlaneMode) 372 .onChange((isOn: boolean) => { 373 this.isDataEnable = !this.isDataEnable; 374 LogUtils.i(TAG, "mobile data switch changes enable:" + JSON.stringify(this.isDataEnable)); 375 if (this.isDataEnable == false) { 376 disableCellularData().then((data) => { 377 LogUtils.i(TAG, "disableCellularData: success then" + JSON.stringify(data)); 378 }).catch((error) => { 379 LogUtils.i(TAG, "disableCellularData: error catch" + JSON.stringify(error)); 380 }); 381 } else { 382 enableCellularData().then((data) => { 383 LogUtils.i(TAG, "enableCellularData: success then" + JSON.stringify(data)); 384 }).catch((error) => { 385 LogUtils.i(TAG, "enableCellularData: error catch" + JSON.stringify(error)); 386 }); 387 } 388 }) 389 } 390 .padding({ 391 left: 12, 392 right: 12, 393 top: 4, 394 bottom: 4 395 }) 396 .height(90) 397 .width("100%") 398 .borderRadius(16) 399 .backgroundColor($r("sys.color.ohos_id_color_foreground_contrary")) 400 .opacity((this.simStateStatusCardOne || this.simStateStatusCardTwo) && !this.isAirPlaneMode ? 1 : 0.4) 401 402 SubHeader({ 403 titleContent: $r('app.string.mobile_data_card1'), 404 subTitleContent: this.TelephoneNumber 405 }); 406 407 Column() { 408 item({ 409 isBtn: this.moisBtn, 410 cardType: this.cardOne, 411 controlSwitch: $dataRoamSwitchCardOne, 412 title: $r('app.string.mobile_data_dataRoaming'), 413 describe: $r('app.string.mobile_data_enableDataWhileRoaming'), 414 isDisabled: this.simStateStatusCardOne && !this.isAirPlaneMode, 415 isCard: true, 416 isSupport: false, 417 isCon: 0, 418 }) 419 420 Divider() 421 .strokeWidth(0.5) 422 .color("#E3E3E3") 423 .lineCap(LineCapStyle.Round) 424 .margin({ right: '2%' }) 425 426 item({ 427 isBtn: this.gqBtn, 428 cardType: this.cardOne, 429 controlSwitch: $enableISM, 430 title: $r('app.string.mobile_data_volte'), 431 describe: $r('app.string.mobile_data_confirmation_function'), 432 isDisabled: this.simStateStatusCardOne && !this.isAirPlaneMode, 433 isSupport: false, 434 isCard: true, 435 isCon: 22, 436 }) 437 } 438 .padding({ 439 left: 12, 440 right: 12, 441 top: 4, 442 bottom: 4 443 }) 444 .width("100%") 445 .borderRadius(16) 446 .backgroundColor($r("sys.color.ohos_id_color_foreground_contrary")) 447 .opacity(this.simStateStatusCardOne && !this.isAirPlaneMode ? 1 : 0.4) 448 449 SubHeader({ 450 titleContent: $r('app.string.mobile_data_card2'), 451 subTitleContent: this.TelephoneNumber2 452 }) 453 .visibility(getMaxSimCount() === 2 ? Visibility.Visible : Visibility.Hidden) 454 455 Column() { 456 item({ 457 isBtn: this.moisBtn, 458 cardType: this.cardTwo, 459 controlSwitch: $mobileDataDisabled2, 460 title: $r('app.string.mobile_data_dataRoaming'), 461 describe: $r('app.string.mobile_data_enableDataWhileRoaming'), 462 isDisabled: this.simStateStatusCardTwo && !this.isAirPlaneMode, 463 isSupport: false, 464 isCard: true, 465 isCon: 0, 466 }) 467 Divider() 468 .strokeWidth(0.5) 469 .color("#E3E3E3") 470 .lineCap(LineCapStyle.Round) 471 .margin({ right: '2%' }) 472 item({ 473 isBtn: this.gqBtn, 474 cardType: this.cardTwo, 475 controlSwitch: $enableISM2, 476 title: $r('app.string.mobile_data_volte'), 477 describe: $r('app.string.mobile_data_confirmation_function'), 478 isDisabled: this.simStateStatusCardTwo && !this.isAirPlaneMode, 479 isSupport: false, 480 isCard: true, 481 isCon: 22, 482 }) 483 } 484 .padding({ 485 left: 12, 486 right: 12, 487 top: 4, 488 bottom: 4 489 }) 490 .padding({ 491 left: 12, 492 right: 12, 493 top: 4, 494 bottom: 4 495 }) 496 .width("100%") 497 .borderRadius(16) 498 .backgroundColor($r("sys.color.ohos_id_color_foreground_contrary")) 499 .opacity(this.simStateStatusCardTwo && !this.isAirPlaneMode ? 1 : 0.4) 500 .visibility(getMaxSimCount() === 2 ? Visibility.Visible : Visibility.Hidden) 501 } 502 .alignItems(HorizontalAlign.Start) 503 } 504 } 505 } 506 .useSizeType({ 507 sm: { span: 4, offset: 0 }, 508 md: { span: 6, offset: 1 }, 509 lg: { span: 8, offset: 2 } 510 }) 511 } 512 .visibility(this.isLoading ? Visibility.Visible : Visibility.Hidden) 513 } 514 } 515 .padding({ left: 12, right: 12 }) 516 .width('100%') 517 .height('100%') 518 .backgroundColor($r("sys.color.ohos_id_color_sub_background")) 519 } 520}