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