• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2024-2024 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 HeadComponent from '../../common/component/headComponent';
17import { WidthPercent } from '../../common/util/ConfigData';
18import CmShowUserCaPresenter from '../../presenter/CmShowUserCaPresenter';
19import {
20  DialogDateComponent,
21  DialogFingerPrintComponent,
22  DialogIssuerComponent,
23  DialogSubjectComponent
24} from '../trustedCa';
25import { AlertDialog } from '@ohos.arkui.advanced.Dialog';
26import { BusinessError } from '@ohos.base';
27
28const TAG: string = 'CaUserDetailPage: ';
29
30export class CaUserDetailParam {
31  public showUserCaPresenter: CmShowUserCaPresenter;
32
33  constructor(presenter: CmShowUserCaPresenter) {
34    this.showUserCaPresenter = presenter;
35  }
36}
37
38@Component
39export struct CaUserDetailPage {
40  private showUserCaPresenter: CmShowUserCaPresenter = CmShowUserCaPresenter.getInstance();
41
42  private stack?: NavPathStack;
43
44  @State private bottomRectHeight: Length = $r('sys.float.padding_level8');
45
46  @State toggleStatus: boolean = false;
47
48  rootCertificateDialog: CustomDialogController = new CustomDialogController({
49    alignment: DialogAlignment.Center,
50    cancel: () => {
51      this.toggleStatus = false;
52    },
53    builder: AlertDialog({
54      primaryTitle: $r('app.string.root_certificate'),
55      content: $r('app.string.root_certificate_message'),
56      primaryButton: {
57        value: $r('app.string.root_certificate_cancel'),
58        buttonStyle: ButtonStyleMode.TEXTUAL,
59        action: () => {
60          this.toggleStatus = false;
61          this.showUserCaPresenter.updateUserTrustedCertificateList();
62        }
63      },
64      secondaryButton: {
65        value: $r('app.string.root_certificate_continue'),
66        buttonStyle: ButtonStyleMode.TEXTUAL,
67        action: () => {
68          this.showUserCaPresenter.setUserCertificateStatus(this.showUserCaPresenter.certInfo.uri, true)
69            .then(result => {
70              if (result) {
71                this.showUserCaPresenter.updateUserTrustedCertificateList();
72              } else {
73                this.toggleStatus = false;
74              }
75          });
76        }
77      }
78    })
79  })
80
81  deleteCertWarnDialog: CustomDialogController = new CustomDialogController({
82    builder: AlertDialog({
83      primaryTitle: $r('app.string.CustomDialogExampleUser_warning_Button_title_text'),
84      content: $r('app.string.CustomDialogExampleUser_warning_Button_message_text'),
85      primaryButton: {
86        value: $r('app.string.deleteAllCredCancel'),
87        buttonStyle: ButtonStyleMode.TEXTUAL,
88        action: () => {
89        }
90      },
91      secondaryButton: {
92        value: $r('app.string.deleteAllCredDelete'),
93        buttonStyle: ButtonStyleMode.TEXTUAL,
94        action: () => {
95          this.showUserCaPresenter.deleteUserCertificate(this.showUserCaPresenter.certInfo.uri, () => {
96            this.showUserCaPresenter.updateUserTrustedCertificateList();
97          })
98          this.stack?.pop();
99        },
100        role: ButtonRole.ERROR
101      }
102    })
103  })
104
105  build() {
106    NavDestination() {
107      Stack({ alignContent: Alignment.Bottom }) {
108        Column() {
109          HeadComponent({ headName: $r('app.string.CustomDialogExample_firText'), isStartBySheet: true, onBackClicked: () => {
110            this.stack?.pop();
111          } })
112            .margin({
113              left: $r('app.float.wh_value_12'),
114              top: 8
115            })
116
117          Scroll() {
118            this.buildContent()
119          }
120          .padding({
121            left: 16, right: 16
122          })
123          .align(Alignment.Top)
124          .scrollable(ScrollDirection.Vertical)
125          .scrollBar(BarState.Auto)
126          .edgeEffect(EdgeEffect.Spring)
127          .width(WidthPercent.WH_100_100)
128          .layoutWeight(1)
129        }
130        .width(WidthPercent.WH_100_100)
131        .height(WidthPercent.WH_100_100)
132        .padding({
133          bottom: this.bottomRectHeight
134        })
135
136        Column() {
137          Button($r('app.string.CustomDialogExampleUser_warning_Button_title_text'))
138            .onClick(() => {
139              this.deleteCertWarnDialog.open();
140            })
141            .role(ButtonRole.ERROR)
142            .buttonStyle(ButtonStyleMode.NORMAL)
143            .margin({
144              top: $r('app.float.distance_16'),
145              bottom: $r('app.float.distance_24')
146            })
147            .width(WidthPercent.WH_100_100)
148        }.onAreaChange((oldArea: Area, newArea: Area) => {
149          this.bottomRectHeight = newArea.height as number;
150        })
151        .padding({
152          left: 16, right: 16
153        })
154      }
155    }
156    .hideTitleBar(true)
157    .width(WidthPercent.WH_100_100)
158    .height(WidthPercent.WH_100_100)
159    .backgroundColor($r('sys.color.background_secondary'))
160    .onReady((ctx: NavDestinationContext) => {
161      this.stack = ctx.pathStack;
162      try {
163        this.showUserCaPresenter = (ctx.pathInfo.param as CaUserDetailParam).showUserCaPresenter;
164        this.toggleStatus = this.showUserCaPresenter.certInfo.status;
165      } catch (err) {
166        let error = err as BusinessError;
167        console.error(TAG + 'Get presenter failed: ' + error?.code + ', message: ' + error?.message);
168      }
169    })
170  }
171
172  @Builder
173  private buildContent() {
174    Column() {
175      Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
176        Column() {
177          Text(this.showUserCaPresenter.certInfo.certAlias)
178            .fontSize($r('sys.float.ohos_id_text_size_body1'))
179            .fontColor($r('sys.color.ohos_id_color_text_primary'))
180            .fontWeight(FontWeight.Medium)
181        }.alignItems(HorizontalAlign.Start);
182
183        Toggle({ type: ToggleType.Switch, isOn: $$this.toggleStatus })
184          .margin({
185            left: $r('app.float.wh_value_5')
186          })
187          .flexShrink(0)
188          .onChange((isOn: boolean) => {
189            if (isOn) {
190              this.rootCertificateDialog.open();
191            } else {
192              this.showUserCaPresenter.setUserCertificateStatus(this.showUserCaPresenter.certInfo.uri, false);
193            }
194          })
195      }.margin({ top: 8 })
196
197      Text($r('app.string.CustomDialogExample_firListItem_text'))
198        .fontSize($r('sys.float.ohos_id_text_size_body1'))
199        .fontColor($r('sys.color.ohos_id_color_text_primary'))
200        .fontWeight(FontWeight.Medium)
201        .margin({
202          top: $r('app.float.wh_value_24')
203        })
204
205      DialogSubjectComponent({ map: this.showUserCaPresenter.certInfo.subjectNameMap })
206
207      Text($r('app.string.CustomDialogExample_secListItem_text'))
208        .fontSize($r('sys.float.ohos_id_text_size_body1'))
209        .fontColor($r('sys.color.ohos_id_color_text_primary'))
210        .fontWeight(FontWeight.Medium)
211        .margin({
212          top: $r('app.float.wh_value_24')
213        })
214
215      DialogIssuerComponent({ map: this.showUserCaPresenter.certInfo.issuerNameMap })
216
217      Text($r('app.string.CustomDialogExample_thdListItem_text'))
218        .fontSize($r('sys.float.ohos_id_text_size_body1'))
219        .fontColor($r('sys.color.ohos_id_color_text_primary'))
220        .fontWeight(FontWeight.Medium)
221        .margin({
222          top: $r('app.float.wh_value_24')
223        })
224
225      DialogDateComponent({ map: this.showUserCaPresenter.certInfo.dateMap })
226
227      Text($r('app.string.CustomDialogExample_fouListItem_text'))
228        .fontSize($r('sys.float.ohos_id_text_size_body1'))
229        .fontColor($r('sys.color.ohos_id_color_text_primary'))
230        .fontWeight(FontWeight.Medium)
231        .margin({
232          top: $r('app.float.wh_value_24')
233        })
234
235      DialogFingerPrintComponent({
236        fingerprintSha256: this.showUserCaPresenter.certInfo.fingerprintSha256
237      })
238    }
239    .width(WidthPercent.WH_100_100)
240    .alignItems(HorizontalAlign.Start)
241  }
242}