• 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 { ConfigValue, WidthPercent } from '../../common/util/ConfigData';
18import { BusinessError } from '@ohos.base';
19import { CMModelErrorCode } from '../../model/CertMangerModel';
20import { RouterFileVo } from '../../model/CertManagerVo/RouterInfoVo';
21import { GlobalContext } from '../../common/GlobalContext';
22import CmInstallPresenter from '../../presenter/CmInstallPresenter';
23
24import PreventScreenshotsPresenter from '../../model/PreventScreenshotsModel';
25import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
26
27const TAG: string = 'CredPwdInputPage: ';
28
29export class CredPwdInputParam {
30  public credFile: RouterFileVo;
31
32  constructor(file: RouterFileVo) {
33    this.credFile = file;
34  }
35}
36
37@Component
38export struct CredPwdInputPage {
39  private stack?: NavPathStack;
40
41  private credFile?: RouterFileVo;
42
43  @State private bottomRectHeight: Length = $r('sys.float.padding_level8');
44
45  @State certPwd: string = '';
46  @State isPasswordError: boolean = false;
47  @State passWordWarn: number = 0;
48
49  @State mPreventScreenshotsPresenter: PreventScreenshotsPresenter = PreventScreenshotsPresenter.getInstance();
50
51  private sheetSession?: UIExtensionContentSession;
52
53  onPageShow(): void {
54    this.mPreventScreenshotsPresenter.PreventScreenshots(true, this.sheetSession);
55  }
56
57  onPageHide(): void {
58    this.mPreventScreenshotsPresenter.PreventScreenshots(false, this.sheetSession);
59  }
60
61  build() {
62    NavDestination() {
63      Stack({ alignContent: Alignment.Bottom }) {
64        Column() {
65          HeadComponent({ headName: $r('app.string.certificatePwdTab'), isStartBySheet: true, onBackClicked: () => {
66            this.stack?.pop();
67          } })
68            .margin({
69              left: $r('app.float.wh_value_12'),
70              top: 8
71            })
72
73          Scroll() {
74            this.buildContent()
75          }
76          .align(Alignment.Top)
77          .scrollable(ScrollDirection.Vertical)
78          .scrollBar(BarState.Auto)
79          .edgeEffect(EdgeEffect.Spring)
80          .width(WidthPercent.WH_100_100)
81          .layoutWeight(1)
82        }
83        .width(WidthPercent.WH_100_100)
84        .height(WidthPercent.WH_100_100)
85        .padding({
86          bottom: this.bottomRectHeight
87        })
88
89        Column() {
90          Button() {
91            Text($r('app.string.installPwdInputConfirm'))
92              .fontSize($r('sys.float.ohos_id_text_size_body1'))
93              .fontWeight(FontWeight.Medium)
94              .fontColor($r('app.color.install_confirm_bt_font_color'))
95          }
96          .type(ButtonType.Capsule)
97          .backgroundColor($r('app.color.install_confirm_bt_bg_color'))
98          .width(WidthPercent.WH_100_100)
99          .height($r('app.float.wh_value_40'))
100          .margin({
101            top: $r('app.float.distance_16'),
102            bottom: $r('app.float.distance_24')
103          })
104          .enabled(this.certPwd !== undefined && this.certPwd.length > 0)
105          .onClick(() => {
106            if (this.credFile === undefined || this.credFile === null) {
107              console.error(TAG + 'Cred file is undefined');
108              return;
109            }
110            this.isPasswordError = false;
111            GlobalContext.getContext().getPwdStore().setCertPwd(this.certPwd);
112            CmInstallPresenter.getInstance().installCert(this.credFile.uri, '', this.credFile.suffix, false).then((resultCode) => {
113              this.passWordWarn = resultCode;
114              console.log(TAG + 'install cred result code = ' + resultCode);
115              if (resultCode === CMModelErrorCode.CM_MODEL_ERROR_PASSWORD_ERR) {
116                this.isPasswordError = true;
117              } else {
118                this.stack?.pop();
119              }
120            });
121          })
122        }.onAreaChange((oldArea: Area, newArea: Area) => {
123          this.bottomRectHeight = newArea.height as number;
124        })
125        .padding({
126          left: 16, right: 16
127        })
128        .expandSafeArea([SafeAreaType.KEYBOARD])
129      }
130      .width(WidthPercent.WH_100_100)
131      .height(WidthPercent.WH_100_100)
132      .expandSafeArea([SafeAreaType.KEYBOARD])
133    }
134    .hideTitleBar(true)
135    .width(WidthPercent.WH_100_100)
136    .height(WidthPercent.WH_100_100)
137    .backgroundColor($r('sys.color.background_secondary'))
138    .onReady((ctx: NavDestinationContext) => {
139      this.stack = ctx.pathStack;
140      try {
141        this.credFile = (ctx.pathInfo.param as CredPwdInputParam).credFile;
142      } catch (err) {
143        let error = err as BusinessError;
144        console.error(TAG + 'Get cred file failed: ' + error?.code + ', message: ' + error?.message);
145      }
146    })
147  }
148
149  @Builder
150  private buildContent() {
151    Column() {
152      Text($r('app.string.certificatePwdInfo'))
153        .fontColor($r('sys.color.ohos_id_color_text_primary'))
154        .fontSize($r('sys.float.ohos_id_text_size_body1'))
155        .fontWeight(FontWeight.Medium)
156        .margin({
157          top: $r('app.float.distance_24'),
158          left: $r('app.float.distance_24')
159        })
160        .alignSelf(ItemAlign.Start);
161
162      TextInput({ text: this.certPwd })
163        .type(InputType.Password)
164        .focusable(true)
165        .border(this.isPasswordError ? {
166          width: $r('app.float.wh_value_1'),
167          color: $r('sys.color.ohos_id_color_warning')
168        } : { width: $r('app.float.wh_value_0') })
169        .maxLength(ConfigValue.PWD_MAX_LENGTH)
170        .margin({
171          top: $r('app.float.distance_16'),
172          left: $r('app.float.distance_24'),
173          right: $r('app.float.distance_24')
174        })
175        .height($r('app.float.wh_value_40'))
176        .onChange((value: string) => {
177          this.certPwd = value;
178        })
179
180      Row() {
181        Text($r('app.string.Password_Message'))
182          .fontFamily('HarmonyHeiTi')
183          .fontSize($r('app.float.distance_14'))
184          .fontWeight(FontWeight.Regular)
185          .lineHeight($r('app.float.distance_19'))
186          .width(WidthPercent.WH_100_100)
187          .textAlign(TextAlign.Center)
188          .fontColor($r('sys.color.ohos_id_color_warning'))
189          .visibility(this.passWordWarn === CMModelErrorCode.CM_MODEL_ERROR_PASSWORD_ERR
190            ? Visibility.Visible : Visibility.None)
191      }
192    }
193    .width(WidthPercent.WH_100_100)
194    .alignItems(HorizontalAlign.Start)
195  }
196}