1import { ApnInfo } from '../common/constant/apnInfo'; 2import router from '@ohos.router'; 3import { ApnDataStorage } from '../model/apnDataStorage'; 4import common from '@ohos.app.ability.common'; 5import sim from '@ohos.telephony.sim'; 6import promptAction from '@ohos.promptAction'; 7import { ApnItemInfo } from '../common/constant/apnItemInfo'; 8import { ApnDetailDataConst } from '../common/constant/apnData'; 9 10 11export class ApnProxy { 12 proxy: string = '' 13 port: string = '' 14} 15 16@CustomDialog 17@Component 18struct EditDialog { 19 @Link textValue: Resource 20 @Link inputValue: string 21 @State value: string = '' 22 controller?: CustomDialogController 23 cancel: () => void = () => { 24 } 25 confirm: () => void = () => { 26 } 27 28 build() { 29 Column() { 30 Text(this.textValue).fontSize(20).margin({ top: 10, bottom: 10 }) 31 TextInput({ placeholder: this.inputValue, text: this.inputValue }) 32 .height(60) 33 .width('90%') 34 .showUnderline(false) 35 .borderRadius(0) 36 .onChange((value: string) => { 37 this.value = value 38 }) 39 Flex({ justifyContent: FlexAlign.SpaceAround }) { 40 Button($r('app.string.apn_cancel')).onClick(() => { 41 if (this.controller != undefined) { 42 this.controller.close() 43 this.cancel() 44 } 45 }).backgroundColor(0xffffff).fontColor(Color.Black) 46 Button($r('app.string.ok')) 47 .onClick(() => { 48 if (this.controller != undefined) { 49 this.inputValue = this.value 50 this.controller.close() 51 this.confirm() 52 } 53 }).backgroundColor(0xffffff).fontColor(Color.Black) 54 }.margin({ bottom: 10 }) 55 }.backgroundColor(Color.White) 56 .width('100%') 57 .borderRadius(10) 58 } 59} 60 61 62@CustomDialog 63@Component 64struct SingleSelectDialog { 65 @Link textValue: Resource 66 @Link inputValue: string 67 @Link selectList: string[] 68 tmpValue: string = this.inputValue 69 controller?: CustomDialogController 70 cancel: () => void = () => { 71 } 72 confirm: () => void = () => { 73 } 74 75 build() { 76 Column() { 77 Text(this.textValue).fontSize(20).margin({ top: 10, bottom: 10 }) 78 List() { 79 ForEach(this.selectList, (item: string) => { 80 ListItem() { 81 Row() { 82 Text(item) 83 Radio({ value: item, group: 'radioGroup' }) 84 .checked(this.inputValue == item) 85 .onChange((isChecked: boolean) => { 86 if (isChecked) { 87 this.tmpValue = item 88 } else { 89 this.tmpValue = this.inputValue 90 } 91 }) 92 }.width('95%') 93 .margin({ left: 20, right: 20 }) 94 .justifyContent(FlexAlign.SpaceBetween) 95 } 96 }) 97 98 } 99 100 Flex({ justifyContent: FlexAlign.SpaceAround }) { 101 Button($r('app.string.apn_cancel')) 102 .onClick(() => { 103 if (this.controller != undefined) { 104 this.controller.close() 105 this.cancel() 106 } 107 }).backgroundColor(0xffffff).fontColor(Color.Black) 108 Button($r('app.string.ok')) 109 .onClick(() => { 110 if (this.controller != undefined) { 111 this.inputValue = this.tmpValue 112 this.controller.close() 113 this.confirm() 114 } 115 }) 116 .backgroundColor(0xffffff).fontColor(Color.Black) 117 }.margin({ bottom: 10 }) 118 } 119 .backgroundColor(Color.White) 120 .width('100%') 121 .borderRadius(10) 122 } 123} 124 125 126@CustomDialog 127@Component 128struct MultipleSelectDialog { 129 @Link textValue: Resource 130 @Link inputValue: string 131 @Link selectList: string[] 132 tmpList: string[] = [] 133 controller?: CustomDialogController 134 cancel: () => void = () => { 135 } 136 confirm: () => void = () => { 137 } 138 139 build() { 140 Column() { 141 Text(this.textValue).fontSize(20).margin({ top: 10, bottom: 10 }) 142 List() { 143 ForEach(this.selectList, (item: string) => { 144 ListItem() { 145 Row() { 146 Text(item) 147 Checkbox({ name: item, group: 'checkboxGroup' }) 148 .select(this.inputValue === item) 149 .selectedColor(Color.Blue) 150 .onChange((isChecked: boolean) => { 151 if (isChecked) { 152 this.tmpList.push(item) 153 } else { 154 let index = this.tmpList.indexOf(item) 155 this.tmpList.slice(index, 1) 156 } 157 }) 158 }.width('95%') 159 .margin({ left: 20, right: 20 }) 160 .justifyContent(FlexAlign.SpaceBetween) 161 } 162 }) 163 164 }.width('100%') 165 .layoutWeight(1) 166 167 Flex({ justifyContent: FlexAlign.SpaceAround }) { 168 Button($r('app.string.apn_cancel')) 169 .onClick(() => { 170 if (this.controller != undefined) { 171 this.controller.close() 172 this.cancel() 173 } 174 }).backgroundColor(0xffffff).fontColor(Color.Black) 175 Button($r('app.string.ok')) 176 .onClick(() => { 177 if (this.controller != undefined) { 178 if (this.tmpList.length > 0) { 179 this.inputValue = this.tmpList.join(','); 180 } 181 this.controller.close() 182 this.confirm() 183 } 184 }) 185 .backgroundColor(0xffffff).fontColor(Color.Black) 186 }.margin({ bottom: 10 }) 187 }.backgroundColor(Color.White) 188 .width('100%') 189 .height('50%') 190 .borderRadius(10) 191 } 192} 193 194 195@Entry 196@Component 197struct ApnDetail { 198 199 private storage: ApnDataStorage = new ApnDataStorage() 200 @State name: string = '' 201 @State textValue: Resource | null = null 202 @State inputValue: string = '' 203 @State list: Array<ApnItemInfo> = [] 204 @State apnInfo: ApnInfo = router.getParams() as ApnInfo 205 @State selectList: string[] = [] 206 @State title: string = '' 207 @State edited: number = 0 //0不可编辑 1 可编辑 2 新增 208 @State profileID: number = 0 209 @StorageLink('ApnStatus') apnStatus: boolean = false; 210 dialogController: CustomDialogController | null = new CustomDialogController({ 211 builder: EditDialog({ 212 cancel: () => { 213 this.onCancel() 214 }, 215 confirm: () => { 216 this.onAccept() 217 }, 218 textValue: $textValue, 219 inputValue: $inputValue 220 }), 221 cancel: this.exitApp, 222 autoCancel: true, 223 alignment: DialogAlignment.Center, 224 offset: { dx: 0, dy: -20 }, 225 gridCount: 4, 226 customStyle: false, 227 cornerRadius: 10, 228 }) 229 singleSelectDialogController: CustomDialogController | null = new CustomDialogController({ 230 builder: SingleSelectDialog({ 231 cancel: () => { 232 this.onCancel() 233 }, 234 confirm: () => { 235 this.onAccept() 236 }, 237 selectList: $selectList, 238 textValue: $textValue, 239 inputValue: $inputValue 240 }), 241 cancel: this.exitApp, 242 autoCancel: true, 243 alignment: DialogAlignment.Center, 244 offset: { dx: 0, dy: -20 }, 245 gridCount: 4, 246 customStyle: false, 247 cornerRadius: 10, 248 }) 249 multipleSelectDialogController: CustomDialogController | null = new CustomDialogController({ 250 builder: MultipleSelectDialog({ 251 cancel: () => { 252 this.onCancel() 253 }, 254 confirm: () => { 255 this.onAccept() 256 }, 257 selectList: $selectList, 258 textValue: $textValue, 259 inputValue: $inputValue 260 }), 261 cancel: this.exitApp, 262 autoCancel: true, 263 alignment: DialogAlignment.Center, 264 offset: { dx: 0, dy: -20 }, 265 gridCount: 4, 266 customStyle: false, 267 cornerRadius: 10, 268 }) 269 270 aboutToAppear(): void { 271 this.storage.initData(getContext() as common.UIAbilityContext) 272 this.getDataList() 273 } 274 275 aboutToDisappear() { 276 this.dialogController = null 277 this.singleSelectDialogController = null 278 this.multipleSelectDialogController = null 279 } 280 281 onCancel() { 282 } 283 284 onAccept() { 285 for (let i = 0; i < this.list.length; i++) { 286 if (this.list[i].key.id == this.textValue.id) { 287 let apnInfo = new ApnItemInfo() 288 apnInfo.id = this.list[i].id 289 apnInfo.key = this.textValue 290 apnInfo.value = this.inputValue 291 this.list[i] = apnInfo 292 } 293 } 294 } 295 296 exitApp() { 297 console.info('Click the callback in the blank area') 298 } 299 300 getProxyPort(pp: string[]): ApnProxy { 301 let length = pp.length 302 let resproxy: string = '' 303 let resport: string = '' 304 pp.forEach((value, index) => { 305 if (index < length - 1) { 306 if (resproxy === '') { 307 resproxy += value 308 } else { 309 resproxy += ':' + value 310 } 311 } else { 312 resport = value 313 } 314 }) 315 let res: ApnProxy = { 316 proxy: resproxy, 317 port: resport 318 } 319 return res; 320 } 321 322 getDataList() { 323 let simOpKey: string = sim.getSimOperatorNumericSync(0); 324 if (this.apnInfo) { 325 this.title = this.apnInfo.profile_name 326 this.edited = this.apnInfo.edited 327 this.profileID = this.apnInfo.profile_id 328 } else { 329 this.edited = 2 330 this.apnInfo = new ApnInfo() 331 } 332 333 //名称 334 let name: ApnItemInfo = new ApnItemInfo() 335 name.id = ApnDetailDataConst.PROFILE_NAME 336 name.key = $r('app.string.apn_name') 337 name.value = this.isNoConfig(this.apnInfo.profile_name) 338 this.list.push(name) 339 //apn 340 let apn: ApnItemInfo = new ApnItemInfo() 341 apn.id = ApnDetailDataConst.APN 342 apn.key = $r('app.string.apn_apn') 343 apn.value = this.isNoConfig(this.apnInfo.apn) 344 this.list.push(apn) 345 //代理 346 let apnProxy: ApnItemInfo = new ApnItemInfo() 347 apnProxy.id = ApnDetailDataConst.PROXY_IP_ADDRESS 348 apnProxy.key = $r('app.string.apn_proxy') 349 350 //代理端口 351 let apnPort: ApnItemInfo = new ApnItemInfo() 352 apnPort.id = ApnDetailDataConst.APNPORT 353 apnPort.key = $r('app.string.apn_port') 354 let proxyIPAddressList = this.apnInfo.proxy_ip_address.split(':'); 355 if (proxyIPAddressList.length >= 2) { 356 let obj = this.getProxyPort(proxyIPAddressList) 357 apnProxy.value = obj.proxy 358 apnPort.value = obj.port 359 } else { 360 apnProxy.value = this.isNoConfig(this.apnInfo.proxy_ip_address) 361 apnPort.value = this.ResourceToString($r('app.string.apn_not_configured')) 362 } 363 this.list.push(apnProxy) 364 this.list.push(apnPort) 365 366 //用户名 367 let authName: ApnItemInfo = new ApnItemInfo() 368 authName.id = ApnDetailDataConst.AUTH_USER 369 authName.key = $r('app.string.apn_auth_user') 370 authName.value = this.isNoConfig(this.apnInfo.auth_user) 371 this.list.push(authName) 372 //密码 373 let authPwd: ApnItemInfo = new ApnItemInfo() 374 authPwd.id = ApnDetailDataConst.AUTH_PWD 375 authPwd.key = $r('app.string.apn_auth_pwd') 376 authPwd.value = this.isNoConfig(this.apnInfo.auth_pwd) 377 this.list.push(authPwd) 378 //服务器 379 let server: ApnItemInfo = new ApnItemInfo() 380 server.id = ApnDetailDataConst.SERVER 381 server.key = $r('app.string.apn_server') 382 server.value = this.isNoConfig(this.apnInfo.server) 383 this.list.push(server) 384 //mmsc 385 let mmsc: ApnItemInfo = new ApnItemInfo() 386 mmsc.id = ApnDetailDataConst.HOME_URL 387 mmsc.key = $r('app.string.apn_mmsc') 388 mmsc.value = this.isNoConfig(this.apnInfo.home_url) 389 this.list.push(mmsc) 390 //彩信代理 391 let mmsProxy: ApnItemInfo = new ApnItemInfo() 392 mmsProxy.id = ApnDetailDataConst.MMS_IP_ADDRESS 393 mmsProxy.key = $r('app.string.apn_mms_proxy') 394 //彩信端口 395 let mmsPort: ApnItemInfo = new ApnItemInfo() 396 mmsPort.id = ApnDetailDataConst.MMSPORT 397 mmsPort.key = $r('app.string.apn_mms_port') 398 399 let mmsProxyIPAddressList = this.apnInfo.mms_ip_address.split(':'); 400 if (mmsProxyIPAddressList.length >= 2) { 401 let obj = this.getProxyPort(mmsProxyIPAddressList) 402 mmsProxy.value = obj.proxy 403 mmsPort.value = obj.port 404 } else { 405 mmsProxy.value = this.isNoConfig(this.apnInfo.mms_ip_address) 406 mmsPort.value = this.ResourceToString($r('app.string.apn_not_configured')) 407 } 408 this.list.push(mmsProxy) 409 this.list.push(mmsPort) 410 411 //mcc 412 let mcc: ApnItemInfo = new ApnItemInfo() 413 mcc.id = ApnDetailDataConst.MCC 414 mcc.key = $r('app.string.apn_mcc') 415 mcc.value = (this.apnInfo.mcc === '' ? simOpKey.substring(0, 3) : this.apnInfo.mcc) 416 this.list.push(mcc) 417 //mnc 418 let mnc: ApnItemInfo = new ApnItemInfo() 419 mnc.id = ApnDetailDataConst.MNC 420 mnc.key = $r('app.string.apn_mnc') 421 mnc.value = (this.apnInfo.mnc === '' ? simOpKey.substring(3, 5) : this.apnInfo.mnc) 422 this.list.push(mnc) 423 //身份验证类型 424 let authType: ApnItemInfo = new ApnItemInfo() 425 authType.id = ApnDetailDataConst.AUTH_TYPE 426 authType.key = $r('app.string.apn_auth_type') 427 authType.value = this.isNullDefaultTxt(this.apnInfo.auth_type) 428 this.list.push(authType) 429 //APN类型 430 let apnType: ApnItemInfo = new ApnItemInfo() 431 apnType.id = ApnDetailDataConst.APN_TYPES 432 apnType.key = $r('app.string.apn_apn_types') 433 apnType.value = (this.apnInfo.apn_types === '' ? 'default' : this.apnInfo.apn_types) 434 this.list.push(apnType) 435 //APN协议 436 let apnProtocol: ApnItemInfo = new ApnItemInfo() 437 apnProtocol.id = ApnDetailDataConst.APN_PROTOCOL 438 apnProtocol.key = $r('app.string.apn_apn_protocol') 439 apnProtocol.value = (this.apnInfo.apn_protocol === '' ? 'IPv4' : this.apnInfo.apn_protocol) 440 this.list.push(apnProtocol) 441 //APN漫游协议 442 let apnRoamProtocol: ApnItemInfo = new ApnItemInfo() 443 apnRoamProtocol.id = ApnDetailDataConst.APN_ROAM_PROTOCOL 444 apnRoamProtocol.key = $r('app.string.apn_apn_roam_protocol') 445 apnRoamProtocol.value = (this.apnInfo.apn_roam_protocol === '' ? 'IPv4' : this.apnInfo.apn_roam_protocol) 446 this.list.push(apnRoamProtocol) 447 //APN承载系统 448 let apnBearingSystemType: ApnItemInfo = new ApnItemInfo() 449 apnBearingSystemType.id = ApnDetailDataConst.BEARING_SYSTEM_TYPE 450 apnBearingSystemType.key = $r('app.string.apn_bearing_system_type') 451 apnBearingSystemType.value = this.isNullDefaultTxt(this.apnInfo.bearing_system_type) 452 this.list.push(apnBearingSystemType) 453 //mvno类型 454 let mvnoType: ApnItemInfo = new ApnItemInfo() 455 mvnoType.id = ApnDetailDataConst.MVNO_TYPE 456 mvnoType.key = $r('app.string.apn_mvno_type') 457 mvnoType.value = this.isNullDefaultTxt(this.apnInfo.mvno_type) 458 this.list.push(mvnoType) 459 //mvno值 460 let mvnoData: ApnItemInfo = new ApnItemInfo() 461 mvnoData.id = ApnDetailDataConst.MVNO_MATCH_DATA 462 mvnoData.key = $r('app.string.apn_mvno_match_data') 463 mvnoData.value = this.isNullDefaultTxt(this.apnInfo.mvno_match_data) 464 this.list.push(mvnoData) 465 } 466 467 isNullDefaultTxt(value: string | number): string { 468 return (value === '' || value === 0) ? this.ResourceToString($r('app.string.apn_null')) : value.toString() 469 } 470 471 isNoConfig(value: string | number): string { 472 return (value === '' || value === 0) ? this.ResourceToString($r('app.string.apn_not_configured')) : value.toString() 473 } 474 475 ResourceToString(resource: Resource): string { 476 return getContext(this).resourceManager.getStringSync(resource) 477 } 478 479 build() { 480 Column() { 481 Row() { 482 Row() { 483 Stack({ alignContent: Alignment.Center }) { 484 Image($r('app.media.ic_back')) 485 .width(24) 486 .height(24) 487 .fillColor($r('sys.color.ohos_id_color_primary')) 488 } 489 .margin({ right: 10 }) 490 .backgroundColor($r('app.color.color_00000000_transparent')) 491 .onClick(() => { 492 router.back() 493 }) 494 495 Text(this.title === '' ? $r('app.string.apn_add') : this.title) 496 .fontSize(20) 497 .fontFamily('HarmonyHeiTi-Bold') 498 .fontWeight(FontWeight.Medium) 499 .fontColor($r('sys.color.ohos_id_color_text_primary')) 500 .maxLines(1) 501 .textOverflow({ overflow: TextOverflow.Ellipsis }) 502 .textAlign(TextAlign.Start) 503 .margin({ top: 15, bottom: 15 }); 504 } 505 506 Image($r('app.media.ic_apn_save')) 507 .width(28) 508 .height(28) 509 .visibility(this.edited === 0 ? Visibility.None : Visibility.Visible) 510 .fillColor($r('sys.color.ohos_id_color_primary')) 511 .onClick((event: ClickEvent) => { 512 let name = this.list[0].value 513 if (name === '' || name === this.ResourceToString($r('app.string.apn_not_configured'))) { 514 promptAction.showToast({ 515 message: this.ResourceToString($r('app.string.apn_tips_name_null')), 516 duration: 2000 517 }); 518 return 519 } 520 let apn = this.list[1].value 521 if (apn === '' || apn === this.ResourceToString($r('app.string.apn_not_configured'))) { 522 promptAction.showToast({ 523 message: this.ResourceToString($r('app.string.apn_tips_apn_null')), 524 duration: 2000 525 }); 526 return 527 } 528 if (this.edited == 2) { //新增 529 this.storage.dataInsert(this.storage.listToApnInfo(this.list, 2, this.profileID)) 530 .then((success) => { 531 if (success) { 532 AppStorage.setOrCreate<boolean>('ApnStatus',!this.apnStatus) 533 router.back() 534 } 535 }) 536 } else { //编辑 537 this.storage.dataUpdate(this.storage.listToApnInfo(this.list, 1, this.profileID)) 538 .then((success) => { 539 if (success) { 540 AppStorage.setOrCreate<boolean>('ApnStatus',!this.apnStatus) 541 router.back() 542 } 543 }) 544 } 545 }) 546 } 547 .justifyContent(FlexAlign.SpaceBetween) 548 .width('100%') 549 .padding({ left: 12, right: 12 }) 550 .height(56) 551 .alignItems(VerticalAlign.Center) 552 .align(Alignment.Start) 553 554 List() { 555 ForEach(this.list, (item: ApnItemInfo) => { 556 ListItem() { 557 Row() { 558 Text(item.key) 559 .fontFamily('HarmonyHeiTi') 560 .fontSize($r('sys.float.ohos_id_text_size_body1')) 561 .fontWeight(FontWeight.Medium) 562 .fontColor(this.edited == 0 ? '#666666' : $r('sys.color.ohos_id_color_text_primary')) 563 .letterSpacing(1) 564 .lineHeight(22) 565 .textAlign(TextAlign.Start) 566 567 Text(item.value.toString()) 568 .fontFamily('HarmonyHeiTi') 569 .fontSize($r('sys.float.ohos_id_text_size_body1')) 570 .fontWeight(FontWeight.Medium) 571 .fontColor(this.edited == 0 ? '#666666' : $r('sys.color.ohos_id_color_text_primary')) 572 .textAlign(TextAlign.Start) 573 } 574 .height(50) 575 .width('96%') 576 .justifyContent(FlexAlign.SpaceBetween) 577 .borderRadius(10) 578 .backgroundColor(Color.White) 579 .margin({ top: 5, left: 10, right: 10, bottom: 5 }) 580 .padding({ left: 10, right: 10 }) 581 582 } 583 .enabled(this.edited == 0 ? false : true) 584 .onClick((event: ClickEvent) => { 585 this.textValue = item.key 586 this.inputValue = (item.value.toString() === this.ResourceToString($r('app.string.apn_not_configured'))) ? '' : item.value.toString() 587 if (item.id == 'auth_type') { 588 this.selectList = [this.ResourceToString($r('app.string.apn_null')), 'PAP', 'CHAP', 'PAP/CHAP'] 589 if (this.singleSelectDialogController != null){ 590 this.singleSelectDialogController.open() 591 } 592 } else if (item.id == 'apn_protocol') { 593 this.selectList = ['IPv4', 'IPv6', 'IPv4/IPv6'] 594 if (this.singleSelectDialogController != null){ 595 this.singleSelectDialogController.open() 596 } 597 } else if (item.id == 'apn_roam_protocol') { 598 this.selectList = ['IPv4', 'IPv6', 'IPv4/IPv6'] 599 if (this.singleSelectDialogController != null){ 600 this.singleSelectDialogController.open() 601 } 602 } else if (item.id == 'bearing_system_type') { 603 this.selectList = [this.ResourceToString($r('app.string.apn_null')), 'LTE', 'HSPAP', 'HSPA', 'HSUPA', 'HSDPA', 'UMTS', 'EDGE', 'GPRS', 'eHRPD', 'EVDO_B', 'EVDO_A', 'EVDO_O', '1xRTT', 'IS95B', 'IS95A'] 604 if (this.multipleSelectDialogController != null){ 605 this.multipleSelectDialogController.open() 606 } 607 } else if (item.id == 'mvno_type') { 608 this.selectList = [this.ResourceToString($r('app.string.apn_null')), 'SPN', 'IMSI', 'GID'] 609 if (this.singleSelectDialogController != null){ 610 this.singleSelectDialogController.open() 611 } 612 } else { 613 this.dialogController.open() 614 } 615 }) 616 }) 617 }.width('100%') 618 .layoutWeight(1) 619 620 Row() { 621 Image($r('app.media.ic_apn_delete')).width(25).height(25).onClick((even: ClickEvent) => { 622 this.storage.dataDelete(this.profileID) 623 .then((success) => { 624 if (success) { 625 AppStorage.setOrCreate<boolean>('ApnStatus',!this.apnStatus) 626 router.back() 627 } 628 }) 629 }) 630 } 631 .width('100%') 632 .height(40) 633 .backgroundColor(Color.White) 634 .justifyContent(FlexAlign.Center) 635 .visibility(this.edited === 1 ? Visibility.Visible : Visibility.None) 636 }.backgroundColor($r('sys.color.ohos_id_color_sub_background')) 637 .height('100%') 638 .width('100%') 639 640 } 641}