1/**
2 * Copyright (c) 2022-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, ConfigValue } from '../common/util/ConfigData';
18import router from '@ohos.router';
19import CmFaPresenter from '../presenter/CmFaPresenter';
20import { GlobalContext } from '../common/GlobalContext';
21import { RouterFileVo } from '../model/CertManagerVo/RouterInfoVo';
22import CmInstallPresenter from '../presenter/CmInstallPresenter';
23import { CMModelErrorCode } from '../model/CertMangerModel';
24import ComponentConfig from '../common/component/ComponentConfig';
25import PreventScreenshotsPresenter from '../model/PreventScreenshotsModel';
26
27@Entry
28@Component
29struct CertPwdInput {
30  @State certPwd: string = '';
31  @State mFaPresenter: CmFaPresenter = CmFaPresenter.getInstance();
32  @State isPasswordError: boolean = false;
33  @State passWordWarn: number = 0;
34  @State mAppCredAuthPresenter: CmInstallPresenter = CmInstallPresenter.getInstance();
35  @State mPreventScreenshotsPresenter: PreventScreenshotsPresenter = PreventScreenshotsPresenter.getInstance();
36
37  onPageShow() {
38    this.mPreventScreenshotsPresenter.PreventScreenshots(true, GlobalContext.getContext().getSession());
39    let uri = GlobalContext.getContext().getAbilityWant().uri;
40    GlobalContext.getContext().clearAbilityWantUri();
41
42    if (uri === 'certInstall') {
43      router.pushUrl({
44        url: 'pages/certInstallFromStorage'
45      })
46    } else if (uri === 'requestAuthorize') {
47      this.mFaPresenter.startRequestAuth(GlobalContext.getContext().getAbilityWant().parameters?.appUid as string);
48    } else {
49      console.error('The want type is not supported');
50    }
51  }
52
53  onPageHide() {
54    this.mPreventScreenshotsPresenter.PreventScreenshots(false, GlobalContext.getContext().getSession());
55  }
56
57  build() {
58    Column() {
59      Column() {
60        HeadComponent({ headName: $r('app.string.certificatePwdTab') });
61        Text($r('app.string.certificatePwdInfo'))
62          .fontColor($r('sys.color.ohos_id_color_text_primary'))
63          .fontSize($r('sys.float.ohos_id_text_size_body1'))
64          .fontWeight(FontWeight.Medium)
65          .margin({
66            top: $r('app.float.distance_24'),
67            left: $r('app.float.distance_24')
68          })
69          .alignSelf(ItemAlign.Start);
70
71        TextInput({ text: this.certPwd })
72          .type(InputType.Password)
73          .focusable(true)
74          .border(this.isPasswordError ? {
75            width: $r('app.float.wh_value_1'),
76            color: $r('sys.color.ohos_id_color_warning')
77          } : { width: $r('app.float.wh_value_0') })
78          .maxLength(ConfigValue.PWD_MAX_LENGTH)
79          .margin({
80            top: $r('app.float.distance_16'),
81            left: $r('app.float.distance_24'),
82            right: $r('app.float.distance_24')
83          })
84          .height($r('app.float.wh_value_40'))
85          .onChange((value: string) => {
86            this.certPwd = value;
87          })
88
89        Row() {
90          Text($r('app.string.Password_Message'))
91            .fontFamily('HarmonyHeiTi')
92            .fontSize($r('app.float.distance_14'))
93            .fontWeight(FontWeight.Regular)
94            .lineHeight($r('app.float.distance_19'))
95            .width(ComponentConfig.WH_100_100)
96            .textAlign(TextAlign.Center)
97            .fontColor($r('sys.color.ohos_id_color_warning'))
98            .visibility(this.passWordWarn === CMModelErrorCode.CM_MODEL_ERROR_PASSWORD_ERR
99              ? Visibility.Visible : Visibility.None)
100        }
101      }
102      .width(WidthPercent.WH_100_100)
103      .height(WidthPercent.WH_75_100);
104
105      Column() {
106        Row() {
107          Button() {
108            Text($r('app.string.installPwdInputCancel'))
109              .fontSize($r('sys.float.ohos_id_text_size_body1'))
110              .fontWeight(FontWeight.Medium)
111              .fontColor($r('sys.color.ohos_id_color_text_hyperlink'))
112          }
113          .type(ButtonType.Capsule)
114          .backgroundColor($r('app.color.install_cancel_bt_bg_color'))
115          .width($r('app.float.component_button_width_phone'))
116          .height($r('app.float.application_button_height'))
117          .margin({
118            left: $r('app.float.distance_24'),
119          })
120          .onClick(() => {
121            router.back();
122          })
123
124          Button() {
125            Text($r('app.string.installPwdInputConfirm'))
126              .fontSize($r('sys.float.ohos_id_text_size_body1'))
127              .fontWeight(FontWeight.Medium)
128              .fontColor($r('app.color.install_confirm_bt_font_color'))
129          }
130          .type(ButtonType.Capsule)
131          .backgroundColor($r('app.color.install_confirm_bt_bg_color'))
132          .width($r('app.float.component_button_width_phone'))
133          .height($r('app.float.application_button_height'))
134          .margin({
135            left: $r('app.float.distance_16'),
136            right: $r('app.float.distance_24'),
137          })
138          .enabled(this.certPwd !== undefined && this.certPwd.length > 0)
139          .onClick(() => {
140            let fileInfo: RouterFileVo = router.getParams() as RouterFileVo;
141            this.isPasswordError = false;
142            GlobalContext.getContext().getPwdStore().setCertPwd(this.certPwd);
143            CmInstallPresenter.getInstance().installCert(fileInfo.uri, '', fileInfo.suffix, true).then((resultCode) => {
144              this.passWordWarn = resultCode;
145              if (resultCode === CMModelErrorCode.CM_MODEL_ERROR_PASSWORD_ERR) {
146                this.isPasswordError = true;
147              }
148            });
149          })
150        }
151        .margin({ bottom: $r('app.float.wh_value_24') })
152      }
153      .justifyContent(FlexAlign.End)
154      .height(WidthPercent.WH_25_100);
155    }
156    .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
157    .width(WidthPercent.WH_100_100)
158    .height(WidthPercent.WH_100_100);
159  }
160}
161