• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 sim from '@ohos.telephony.sim'
17import Logger from '../model/Logger'
18
19const TAG = '[ShowView]'
20
21@CustomDialog
22export struct ShowView {
23  @State simState: Resource = undefined
24  @State results: Array<unknown> = []
25  @State simTitle: Resource = undefined
26  private slotId: number
27  controller: CustomDialogController
28
29  async simResult() {
30    Logger.info(TAG, `getResult this.slotId ${this.slotId}`)
31    this.simTitle = this.slotId === 0 ? $r('app.string.sim1_state') : $r('app.string.sim2_state')
32    let result = await sim.isSimActive(this.slotId)
33    this.simState = result ? $r('app.string.sim_activation') : $r('app.string.sim_inactivated')
34  }
35
36  async getSimData() {
37    let data: Array<string | Resource> = new Array(3).fill('')
38    Logger.info(TAG, `data = ${JSON.stringify(data)}`)
39    try {
40      data[0] = await sim.getSimSpn(this.slotId)
41      Logger.info(TAG, `data = ${JSON.stringify(data[0])}`)
42    } catch (err) {
43      data[0] = $r('app.string.err')
44      Logger.info(TAG, `data = ${JSON.stringify(data[0])} err = ${JSON.stringify(err)}`)
45    }
46    try {
47      data[1] = await sim.getISOCountryCodeForSim(this.slotId)
48      Logger.info(TAG, `data = ${JSON.stringify(data[1])}`)
49    } catch (err) {
50      data[1] = $r('app.string.err')
51      Logger.info(TAG, `data = ${JSON.stringify(data[1])} err = ${JSON.stringify(err)}`)
52    }
53    try {
54      data[2] = await sim.getSimOperatorNumeric(this.slotId)
55      Logger.info(TAG, `data = ${JSON.stringify(data[2])}`)
56    } catch (err) {
57      data[2] = $r('app.string.err')
58      Logger.info(TAG, `data = ${JSON.stringify(data[2])} err = ${JSON.stringify(err)}`)
59    }
60    Logger.info(TAG, `data is ${JSON.stringify(data)}`)
61    return data
62  }
63
64  async aboutToAppear() {
65    await this.simResult()
66    let result = await this.getSimData()
67    Logger.info(TAG, `result = ${JSON.stringify(result)}`)
68    this.results = [
69      { title: $r('app.string.spn'), value: result[0] }, { title: $r('app.string.iso'), value: result[1] },
70      { title: $r('app.string.plmn'), value: result[2] }
71    ]
72    Logger.info(TAG, `results = ${JSON.stringify(this.results)}`)
73  }
74
75  build() {
76    Column() {
77      Text(this.simTitle)
78        .fontSize(18)
79        .margin({ left: 5, right: 5, top: 5, bottom: 10 })
80
81      Text($r('app.string.active'))
82        .margin(5)
83        .fontSize(18)
84        .fontColor(Color.Gray)
85
86      Text(this.simState)
87        .margin(5)
88        .fontSize(18)
89
90      ForEach(this.results, item => {
91        Text(item.title)
92          .margin(5)
93          .fontSize(18)
94          .fontColor(Color.Gray)
95
96        Text(item.value)
97          .margin(5)
98          .fontSize(18)
99      }, item => JSON.stringify(item))
100    }
101    .margin(10)
102    .padding(5)
103    .width('100%')
104    .borderRadius(10)
105    .alignItems(HorizontalAlign.Start)
106    .onClick(() => {
107      this.controller.close()
108      Logger.info(TAG, ` CustomDialog close`)
109    })
110  }
111}
112