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 static readonly 200 201 private storage: ApnDataStorage = new ApnDataStorage() 202 @State name: string = '' 203 @State textValue: Resource = null 204 @State inputValue: string = '' 205 @State list: Array<ApnItemInfo> = [] 206 @State apnInfo: ApnInfo = router.getParams() as ApnInfo 207 @State selectList: string[] = [] 208 @State title: string = '' 209 @State edited: number = 0 //0不可编辑 1 可编辑 2 新增 210 @State profileID: number = 0 211 @StorageLink('ApnStatus') apnStatus: boolean = false; 212 dialogController: CustomDialogController | null = new CustomDialogController({ 213 builder: EditDialog({ 214 cancel: () => { 215 this.onCancel() 216 }, 217 confirm: () => { 218 this.onAccept() 219 }, 220 textValue: $textValue, 221 inputValue: $inputValue 222 }), 223 cancel: this.exitApp, 224 autoCancel: true, 225 alignment: DialogAlignment.Center, 226 offset: { dx: 0, dy: -20 }, 227 gridCount: 4, 228 customStyle: false, 229 cornerRadius: 10, 230 }) 231 singleSelectDialogController: CustomDialogController | null = new CustomDialogController({ 232 builder: SingleSelectDialog({ 233 cancel: () => { 234 this.onCancel() 235 }, 236 confirm: () => { 237 this.onAccept() 238 }, 239 selectList: $selectList, 240 textValue: $textValue, 241 inputValue: $inputValue 242 }), 243 cancel: this.exitApp, 244 autoCancel: true, 245 alignment: DialogAlignment.Center, 246 offset: { dx: 0, dy: -20 }, 247 gridCount: 4, 248 customStyle: false, 249 cornerRadius: 10, 250 }) 251 multipleSelectDialogController: CustomDialogController | null = new CustomDialogController({ 252 builder: MultipleSelectDialog({ 253 cancel: () => { 254 this.onCancel() 255 }, 256 confirm: () => { 257 this.onAccept() 258 }, 259 selectList: $selectList, 260 textValue: $textValue, 261 inputValue: $inputValue 262 }), 263 cancel: this.exitApp, 264 autoCancel: true, 265 alignment: DialogAlignment.Center, 266 offset: { dx: 0, dy: -20 }, 267 gridCount: 4, 268 customStyle: false, 269 cornerRadius: 10, 270 }) 271 272 aboutToAppear(): void { 273 this.storage.initData(getContext() as common.UIAbilityContext) 274 this.getDataList() 275 } 276 277 aboutToDisappear() { 278 this.dialogController = null 279 this.singleSelectDialogController = null 280 this.multipleSelectDialogController = null 281 } 282 283 onCancel() { 284 } 285 286 onAccept() { 287 for (let i = 0; i < this.list.length; i++) { 288 if (this.list[i].key.id == this.textValue.id) { 289 let apnInfo = new ApnItemInfo() 290 apnInfo.id = this.list[i].id 291 apnInfo.key = this.textValue 292 apnInfo.value = this.inputValue 293 this.list[i] = apnInfo 294 } 295 } 296 } 297 298 exitApp() { 299 console.info('Click the callback in the blank area') 300 } 301 302 getProxyPort(pp: string[]): ApnProxy { 303 let length = pp.length 304 let resproxy: string = '' 305 let resport: string = '' 306 pp.forEach((value, index) => { 307 if (index < length - 1) { 308 if (resproxy === '') { 309 resproxy += value 310 } else { 311 resproxy += ':' + value 312 } 313 } else { 314 resport = value 315 } 316 }) 317 let res: ApnProxy = { 318 proxy: resproxy, 319 port: resport 320 } 321 return res; 322 } 323 324 getDataList() { 325 let simOpKey: string = sim.getSimOperatorNumericSync(0); 326 if (this.apnInfo) { 327 this.title = this.apnInfo.profile_name 328 this.edited = this.apnInfo.edited 329 this.profileID = this.apnInfo.profile_id 330 } else { 331 this.edited = 2 332 this.apnInfo = new ApnInfo() 333 } 334 335 //名称 336 let name: ApnItemInfo = new ApnItemInfo() 337 name.id = ApnDetailDataConst.PROFILE_NAME 338 name.key = $r('app.string.apn_name') 339 name.value = this.isNoConfig(this.apnInfo.profile_name) 340 this.list.push(name) 341 //apn 342 let apn: ApnItemInfo = new ApnItemInfo() 343 apn.id = ApnDetailDataConst.APN 344 apn.key = $r('app.string.apn_apn') 345 apn.value = this.isNoConfig(this.apnInfo.apn) 346 this.list.push(apn) 347 //代理 348 let apnProxy: ApnItemInfo = new ApnItemInfo() 349 apnProxy.id = ApnDetailDataConst.PROXY_IP_ADDRESS 350 apnProxy.key = $r('app.string.apn_proxy') 351 352 //代理端口 353 let apnPort: ApnItemInfo = new ApnItemInfo() 354 apnPort.id = ApnDetailDataConst.APNPORT 355 apnPort.key = $r('app.string.apn_port') 356 let proxyIPAddressList = this.apnInfo.proxy_ip_address.split(':'); 357 if (proxyIPAddressList.length >= 2) { 358 let obj = this.getProxyPort(proxyIPAddressList) 359 apnProxy.value = obj.proxy 360 apnPort.value = obj.port 361 } else { 362 apnProxy.value = this.isNoConfig(this.apnInfo.proxy_ip_address) 363 apnPort.value = this.ResourceToString($r('app.string.apn_not_configured')) 364 } 365 this.list.push(apnProxy) 366 this.list.push(apnPort) 367 368 //用户名 369 let authName: ApnItemInfo = new ApnItemInfo() 370 authName.id = ApnDetailDataConst.AUTH_USER 371 authName.key = $r('app.string.apn_auth_user') 372 authName.value = this.isNoConfig(this.apnInfo.auth_user) 373 this.list.push(authName) 374 //密码 375 let authPwd: ApnItemInfo = new ApnItemInfo() 376 authPwd.id = ApnDetailDataConst.AUTH_PWD 377 authPwd.key = $r('app.string.apn_auth_pwd') 378 authPwd.value = this.isNoConfig(this.apnInfo.auth_pwd) 379 this.list.push(authPwd) 380 //服务器 381 let server: ApnItemInfo = new ApnItemInfo() 382 server.id = ApnDetailDataConst.SERVER 383 server.key = $r('app.string.apn_server') 384 server.value = this.isNoConfig(this.apnInfo.server) 385 this.list.push(server) 386 //mmsc 387 let mmsc: ApnItemInfo = new ApnItemInfo() 388 mmsc.id = ApnDetailDataConst.HOME_URL 389 mmsc.key = $r('app.string.apn_mmsc') 390 mmsc.value = this.isNoConfig(this.apnInfo.home_url) 391 this.list.push(mmsc) 392 // //彩信代理 393 let mmsProxy: ApnItemInfo = new ApnItemInfo() 394 mmsProxy.id = ApnDetailDataConst.MMS_IP_ADDRESS 395 mmsProxy.key = $r('app.string.apn_mms_proxy') 396 //彩信端口 397 let mmsPort: ApnItemInfo = new ApnItemInfo() 398 mmsPort.id = ApnDetailDataConst.MMSPORT 399 mmsPort.key = $r('app.string.apn_mms_port') 400 401 let mmsProxyIPAddressList = this.apnInfo.mms_ip_address.split(':'); 402 if (mmsProxyIPAddressList.length >= 2) { 403 let obj = this.getProxyPort(mmsProxyIPAddressList) 404 mmsProxy.value = obj.proxy 405 mmsPort.value = obj.port 406 } else { 407 mmsProxy.value = this.isNoConfig(this.apnInfo.mms_ip_address) 408 mmsPort.value = this.ResourceToString($r('app.string.apn_not_configured')) 409 } 410 this.list.push(mmsProxy) 411 this.list.push(mmsPort) 412 413 //mcc 414 let mcc: ApnItemInfo = new ApnItemInfo() 415 mcc.id = ApnDetailDataConst.MCC 416 mcc.key = $r('app.string.apn_mcc') 417 mcc.value = (this.apnInfo.mcc === '' ? simOpKey.substring(0, 3) : this.apnInfo.mcc) 418 this.list.push(mcc) 419 //mnc 420 let mnc: ApnItemInfo = new ApnItemInfo() 421 mnc.id = ApnDetailDataConst.MNC 422 mnc.key = $r('app.string.apn_mnc') 423 mnc.value = (this.apnInfo.mnc === '' ? simOpKey.substring(3, 5) : this.apnInfo.mnc) 424 this.list.push(mnc) 425 //身份验证类型 426 let authType: ApnItemInfo = new ApnItemInfo() 427 authType.id = ApnDetailDataConst.AUTH_TYPE 428 authType.key = $r('app.string.apn_auth_type') 429 authType.value = this.isNullDefaultTxt(this.apnInfo.auth_type) 430 this.list.push(authType) 431 //APN类型 432 let apnType: ApnItemInfo = new ApnItemInfo() 433 apnType.id = ApnDetailDataConst.APN_TYPES 434 apnType.key = $r('app.string.apn_apn_types') 435 apnType.value = (this.apnInfo.apn_types === '' ? 'default' : this.apnInfo.apn_types) 436 this.list.push(apnType) 437 //APN协议 438 let apnProtocol: ApnItemInfo = new ApnItemInfo() 439 apnProtocol.id = ApnDetailDataConst.APN_PROTOCOL 440 apnProtocol.key = $r('app.string.apn_apn_protocol') 441 apnProtocol.value = (this.apnInfo.apn_protocol === '' ? 'IPv4' : this.apnInfo.apn_protocol) 442 this.list.push(apnProtocol) 443 //APN漫游协议 444 let apnRoamProtocol: ApnItemInfo = new ApnItemInfo() 445 apnRoamProtocol.id = ApnDetailDataConst.APN_PROTOCOL 446 apnRoamProtocol.key = $r('app.string.apn_apn_roam_protocol') 447 apnRoamProtocol.value = (this.apnInfo.apn_roam_protocol === '' ? 'IPv4' : this.apnInfo.apn_roam_protocol) 448 this.list.push(apnRoamProtocol) 449 //APN承载系统 450 let apnBearingSystemType: ApnItemInfo = new ApnItemInfo() 451 apnBearingSystemType.id = ApnDetailDataConst.BEARING_SYSTEM_TYPE 452 apnBearingSystemType.key = $r('app.string.apn_bearing_system_type') 453 apnBearingSystemType.value = this.isNullDefaultTxt(this.apnInfo.bearing_system_type) 454 this.list.push(apnBearingSystemType) 455 //mvno类型 456 let mvnoType: ApnItemInfo = new ApnItemInfo() 457 mvnoType.id = ApnDetailDataConst.MVNO_TYPE 458 mvnoType.key = $r('app.string.apn_mvno_type') 459 mvnoType.value = this.isNullDefaultTxt(this.apnInfo.mvno_type) 460 this.list.push(mvnoType) 461 //mvno值 462 let mvnoData: ApnItemInfo = new ApnItemInfo() 463 mvnoData.id = ApnDetailDataConst.MVNO_MATCH_DATA 464 mvnoData.key = $r('app.string.apn_mvno_match_data') 465 mvnoData.value = this.isNullDefaultTxt(this.apnInfo.mvno_match_data) 466 this.list.push(mvnoData) 467 } 468 469 isNullDefaultTxt(value: string | number): string { 470 return (value === '' || value === 0) ? this.ResourceToString($r('app.string.apn_null')) : value.toString() 471 } 472 473 isNoConfig(value: string | number): string { 474 return (value === '' || value === 0) ? this.ResourceToString($r('app.string.apn_not_configured')) : value.toString() 475 } 476 477 ResourceToString(resource: Resource): string { 478 return getContext(this).resourceManager.getStringSync(resource) 479 } 480 481 build() { 482 Column() { 483 Row() { 484 Row() { 485 Stack({ alignContent: Alignment.Center }) { 486 Image($r('app.media.ic_back')) 487 .width(24) 488 .height(24) 489 .fillColor($r('sys.color.ohos_id_color_primary')) 490 } 491 .margin({ right: 10 }) 492 .backgroundColor($r('app.color.color_00000000_transparent')) 493 .onClick(() => { 494 router.back() 495 }) 496 497 Text(this.title === '' ? $r('app.string.apn_add') : this.title) 498 .fontSize(20) 499 .fontFamily('HarmonyHeiTi-Bold') 500 .fontWeight(FontWeight.Medium) 501 .fontColor($r('sys.color.ohos_id_color_text_primary')) 502 .maxLines(1) 503 .textOverflow({ overflow: TextOverflow.Ellipsis }) 504 .textAlign(TextAlign.Start) 505 .margin({ top: 15, bottom: 15 }); 506 } 507 508 Image($r('app.media.ic_apn_save')) 509 .width(28) 510 .height(28) 511 .visibility(this.edited === 0 ? Visibility.None : Visibility.Visible) 512 .fillColor($r('sys.color.ohos_id_color_primary')) 513 .onClick((event: ClickEvent) => { 514 let name = this.list[0].value 515 if (name === '' || name === this.ResourceToString($r('app.string.apn_not_configured'))) { 516 promptAction.showToast({ 517 message: this.ResourceToString($r('app.string.apn_tips_name_null')), 518 duration: 2000 519 }); 520 return 521 } 522 let apn = this.list[1].value 523 if (apn === '' || apn === this.ResourceToString($r('app.string.apn_not_configured'))) { 524 promptAction.showToast({ 525 message: this.ResourceToString($r('app.string.apn_tips_apn_null')), 526 duration: 2000 527 }); 528 return 529 } 530 if (this.edited == 2) { //新增 531 this.storage.dataInsert(this.storage.listToApnInfo(this.list, 2, this.profileID)) 532 .then((success) => { 533 if (success) { 534 AppStorage.setOrCreate<boolean>('ApnStatus',!this.apnStatus) 535 router.back() 536 } 537 }) 538 } else { //编辑 539 this.storage.dataUpdate(this.storage.listToApnInfo(this.list, 1, this.profileID)) 540 .then((success) => { 541 if (success) { 542 AppStorage.setOrCreate<boolean>('ApnStatus',!this.apnStatus) 543 router.back() 544 } 545 }) 546 } 547 }) 548 } 549 .justifyContent(FlexAlign.SpaceBetween) 550 .width('100%') 551 .padding({ left: 12, right: 12 }) 552 .height(56) 553 .alignItems(VerticalAlign.Center) 554 .align(Alignment.Start) 555 556 List() { 557 ForEach(this.list, (item: ApnItemInfo) => { 558 ListItem() { 559 Row() { 560 Text(item.key) 561 .fontFamily('HarmonyHeiTi') 562 .fontSize($r('sys.float.ohos_id_text_size_body1')) 563 .fontWeight(FontWeight.Medium) 564 .fontColor(this.edited == 0 ? '#666666' : $r('sys.color.ohos_id_color_text_primary')) 565 .letterSpacing(1) 566 .lineHeight(22) 567 .textAlign(TextAlign.Start) 568 569 Text(item.value.toString()) 570 .fontFamily('HarmonyHeiTi') 571 .fontSize($r('sys.float.ohos_id_text_size_body1')) 572 .fontWeight(FontWeight.Medium) 573 .fontColor(this.edited == 0 ? '#666666' : $r('sys.color.ohos_id_color_text_primary')) 574 .textAlign(TextAlign.Start) 575 } 576 .height(50) 577 .width('96%') 578 .justifyContent(FlexAlign.SpaceBetween) 579 .borderRadius(10) 580 .backgroundColor(Color.White) 581 .margin({ top: 5, left: 10, right: 10, bottom: 5 }) 582 .padding({ left: 10, right: 10 }) 583 584 } 585 .enabled(this.edited == 0 ? false : true) 586 .onClick((event: ClickEvent) => { 587 this.textValue = item.key 588 this.inputValue = (item.value.toString() === this.ResourceToString($r('app.string.apn_not_configured'))) ? '' : item.value.toString() 589 if (item.id == 'auth_type') { 590 this.selectList = [this.ResourceToString($r('app.string.apn_null')), 'PAP', 'CHAP', 'PAP/CHAP'] 591 this.singleSelectDialogController.open() 592 } else if (item.id == 'apn_protocol' || item.id == 'apn_roam_protocol') { 593 this.selectList = ['IPv4', 'IPv6', 'IPv4/IPv6'] 594 this.singleSelectDialogController.open() 595 } else if (item.id == 'bearing_system_type') { 596 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'] 597 this.multipleSelectDialogController.open() 598 } else if (item.id == 'mvno_type') { 599 this.selectList = [this.ResourceToString($r('app.string.apn_null')), 'SPN', 'IMSI', 'GID'] 600 this.singleSelectDialogController.open() 601 } else { 602 this.dialogController.open() 603 } 604 }) 605 }) 606 }.width('100%') 607 .layoutWeight(1) 608 609 Row() { 610 Image($r('app.media.ic_apn_delete')).width(25).height(25).onClick((even: ClickEvent) => { 611 this.storage.dataDelete(this.profileID) 612 .then((success) => { 613 if (success) { 614 AppStorage.setOrCreate<boolean>('ApnStatus',!this.apnStatus) 615 router.back() 616 } 617 }) 618 }) 619 } 620 .width('100%') 621 .height(40) 622 .backgroundColor(Color.White) 623 .justifyContent(FlexAlign.Center) 624 .visibility(this.edited === 1 ? Visibility.Visible : Visibility.None) 625 }.backgroundColor($r('sys.color.ohos_id_color_sub_background')) 626 .height('100%') 627 .width('100%') 628 629 } 630}