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 { WidthPercent } from '../common/util/ConfigData'; 17import HeadComponent from '../common/component/headComponent'; 18import CmFaPresenter from '../presenter/CmFaPresenter'; 19import { GlobalContext } from '../common/GlobalContext'; 20import ComponentConfig from '../common/component/ComponentConfig'; 21import router from '@ohos.router'; 22import { CustomContentDialog } from '@ohos.arkui.advanced.Dialog'; 23import checkUserAuthModel from '../model/CheckUserAuthModel'; 24import { NavEntryKey } from '../common/NavEntryKey'; 25import picker from '@ohos.file.picker'; 26import FileIoModel from '../model/FileIoModel'; 27import CmInstallPresenter from '../presenter/CmInstallPresenter'; 28import { BusinessError } from '@ohos.base'; 29import { RouterFileVo } from '../model/CertManagerVo/RouterInfoVo'; 30import { CredPwdInputParam } from './picker/CredPwdInputPage'; 31 32const COPIES_NUM: number = 12; 33 34const TAG: string = 'CertInstallFromStorage: '; 35 36@Entry 37@Component 38export struct CertInstallFromStorage { 39 @State columnMargin: string = '12vp'; 40 @State mFaPresenter: CmFaPresenter = CmFaPresenter.getInstance(); 41 @State installCertFlag: boolean = false; 42 43 isStartBySheetFirst: boolean = false; 44 isStartBySheet: boolean = false; 45 selected?: (path: string, param?: Object) => void; 46 47 @Styles normalStyle() { 48 .backgroundColor($r('sys.color.ohos_id_color_card_bg')) 49 .borderRadius($r('app.float.user_list_divider_borderRadius_value')) 50 }; 51 @Styles pressedStyle() { 52 .backgroundColor($r('sys.color.ohos_id_color_click_effect')) 53 .borderRadius($r('app.float.user_list_divider_borderRadius_value')) 54 }; 55 56 rootCertificateDialog: CustomDialogController = new CustomDialogController({ 57 alignment: DialogAlignment.Center, 58 builder: CustomContentDialog({ 59 contentBuilder: () => { 60 this.rootCertificateContent(); 61 }, 62 contentAreaPadding: { right: $r('app.float.wh_value_0') }, 63 buttons: [ 64 { 65 value: $r('app.string.root_certificate_cancel'), 66 buttonStyle: ButtonStyleMode.TEXTUAL, 67 action: () => { 68 } 69 }, 70 { 71 value: $r('app.string.root_certificate_continue'), 72 buttonStyle: ButtonStyleMode.TEXTUAL, 73 action: () => { 74 this.installCertFlag = true; 75 this.checkUserAuth(); 76 } 77 } 78 ] 79 }) 80 }) 81 82 @Builder 83 rootCertificateContent(): void { 84 Column() { 85 Text($r('app.string.root_certificate')) 86 .height($r('app.float.wh_value_56')) 87 .fontSize($r('sys.float.ohos_id_text_size_dialog_tittle')) 88 .fontColor($r('sys.color.ohos_id_color_text_primary')) 89 .fontWeight(FontWeight.Medium) 90 .margin({ 91 left: $r('app.float.wh_value_24'), 92 right: $r('app.float.wh_value_24') 93 }) 94 .alignSelf(ItemAlign.Start) 95 96 Text($r('app.string.root_certificate_message')) 97 .fontSize($r('sys.float.ohos_id_text_size_body1')) 98 .fontWeight(FontWeight.Regular) 99 .fontColor($r('sys.color.ohos_id_color_primary')) 100 .margin({ 101 left: $r('app.float.wh_value_24'), 102 right: $r('app.float.wh_value_24') 103 }) 104 .alignSelf(ItemAlign.Start) 105 } 106 .width(WidthPercent.WH_100_100) 107 .borderRadius($r('app.float.user_list_divider_borderRadius_value')) 108 .backgroundColor($r('sys.color.ohos_id_color_dialog_bg')) 109 } 110 111 checkUserAuth() { 112 let titleStr = getContext().resourceManager.getStringSync($r('app.string.Identity_Authentication')); 113 checkUserAuthModel.auth(titleStr, (authResult: boolean) => { 114 if (authResult) { 115 console.info('checkUserAuth success'); 116 if (this.installCertFlag) { 117 if (this.isStartBySheet) { 118 this.startInstallCertBySheet(); 119 } else { 120 this.mFaPresenter.startInstallCert(); 121 } 122 } else { 123 if (this.isStartBySheet) { 124 this.startInstallEvidenceBySheet(); 125 } else { 126 this.mFaPresenter.startInstallEvidence(); 127 } 128 } 129 } 130 }) 131 } 132 133 startInstallCertBySheet(): void { 134 try { 135 let documentSelectOptions = new picker.DocumentSelectOptions(); 136 let documentPicker = new picker.DocumentViewPicker(); 137 console.info(TAG + 'start documentPicker.select'); 138 documentPicker.select(documentSelectOptions).then((documentSelectResult) => { 139 if (documentSelectResult.length >= 1) { 140 this.routeToNextInstallCert(String(documentSelectResult[0])) 141 } else { 142 console.error(TAG + 'documentPicker.select length invalid:' + documentSelectResult.length); 143 } 144 }).catch((err: BusinessError) => { 145 console.error(TAG + 'documentPicker.select failed with err, message: ' + err.message + ', code: ' + err.code); 146 }); 147 } catch (err) { 148 let e: BusinessError = err as BusinessError; 149 console.error(TAG + 'DocumentViewPicker failed with err, message: ' + e.message + ', code: ' + e.code); 150 } 151 } 152 153 routeToNextInstallCert(fileUri: string): void { 154 FileIoModel.getMediaFileSuffix(fileUri, (suffix: string | undefined) => { 155 if (suffix !== undefined) { 156 console.debug(TAG, 'suffix = ', suffix); 157 if ((suffix === 'cer') || (suffix === 'pem')) { 158 CmInstallPresenter.getInstance().installCert(fileUri, '', suffix, false); 159 } else { 160 this.mFaPresenter.unrecognizedFileTips(); 161 } 162 } 163 }) 164 } 165 166 startInstallEvidenceBySheet(): void { 167 try { 168 let documentSelectOptions = new picker.DocumentSelectOptions(); 169 let documentPicker = new picker.DocumentViewPicker(); 170 console.info(TAG + 'start documentPicker.select'); 171 documentPicker.select(documentSelectOptions).then((documentSelectResult) => { 172 if (documentSelectResult.length >= 1) { 173 this.routeToNextInstallEvidence(String(documentSelectResult[0])) 174 } else { 175 console.error(TAG + 'documentPicker.select length invalid:' + documentSelectResult.length); 176 } 177 }).catch((err: BusinessError) => { 178 console.error(TAG + 'documentPicker.select failed with err, message: ' + err.message + ', code: ' + err.code); 179 }); 180 } catch (err) { 181 let e: BusinessError = err as BusinessError; 182 console.error(TAG + 'DocumentViewPicker failed with err, message: ' + e.message + ', code: ' + e.code); 183 } 184 } 185 186 routeToNextInstallEvidence(fileUri: string): void { 187 FileIoModel.getMediaFileSuffix(fileUri, (suffix: string | undefined) => { 188 if (suffix !== undefined) { 189 console.debug(TAG, 'suffix = ', suffix); 190 if ((suffix === 'p12') || (suffix === 'pfx')) { 191 this.selected?.(NavEntryKey.CRED_PWD_INPUT_ENTRY, new CredPwdInputParam(new RouterFileVo(fileUri, suffix))); 192 } else { 193 this.mFaPresenter.unrecognizedFileTips(); 194 } 195 } 196 }) 197 } 198 199 build() { 200 Column() { 201 GridRow({ 202 columns: COPIES_NUM, 203 gutter: vp2px(1) === 2 ? $r('app.float.wh_value_12') : $r('app.float.wh_value_0') 204 }) { 205 GridCol({ span: COPIES_NUM }) { 206 Row({}) { 207 Column({ space: this.columnMargin }) { 208 HeadComponent({ headName: $r('app.string.installInStorageDevice'), isStartBySheet: this.isStartBySheet, 209 icBackIsVisibility: !this.isStartBySheetFirst, 210 onBackClicked: () => { 211 this.selected?.(NavEntryKey.POP); 212 }}) 213 .margin({ 214 top: this.isStartBySheet ? 8 : 0, 215 left: 12, 216 right: 12 217 }) 218 Scroll() { 219 Column() { 220 Row() { 221 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 222 Text($r('app.string.CA_cert')) 223 .fontSize($r('sys.float.ohos_id_text_size_body1')) 224 .fontColor($r('sys.color.ohos_id_color_text_primary')) 225 .fontWeight(FontWeight.Medium) 226 .margin({ left: $r('app.float.wh_value_12') }) 227 .textAlign(TextAlign.Start) 228 } 229 } 230 .stateStyles({ 231 normal: this.normalStyle, 232 pressed: this.pressedStyle 233 }) 234 .constraintSize({ 235 minHeight: $r('app.float.wh_value_48') 236 }) 237 .onClick(() => { 238 this.rootCertificateDialog.open(); 239 }) 240 241 Divider() 242 .color($r('sys.color.ohos_id_color_list_separator')) 243 .margin({ 244 left: $r('app.float.wh_value_12'), 245 right: $r('app.float.wh_value_12') 246 }) 247 248 Row() { 249 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 250 Text($r('app.string.system_credentials')) 251 .fontSize($r('sys.float.ohos_id_text_size_body1')) 252 .fontColor($r('sys.color.ohos_id_color_text_primary')) 253 .fontWeight(FontWeight.Medium) 254 .margin({ left: $r('app.float.wh_value_12') }) 255 .textAlign(TextAlign.Start) 256 } 257 .onClick(() => { 258 this.installCertFlag = false; 259 AppStorage.setOrCreate('installSystemCred', true); 260 AppStorage.setOrCreate('installUserCred',false); 261 if (this.isStartBySheet) { 262 this.startInstallEvidenceBySheet(); 263 } else { 264 this.mFaPresenter.startInstallEvidence(); 265 } 266 }) 267 } 268 .stateStyles({ 269 normal: this.normalStyle, 270 pressed: this.pressedStyle 271 }) 272 .constraintSize({ 273 minHeight: $r('app.float.wh_value_48') 274 }) 275 276 Divider() 277 .color($r('sys.color.ohos_id_color_list_separator')) 278 .margin({ 279 left: $r('app.float.wh_value_12'), 280 right: $r('app.float.wh_value_12') 281 }) 282 283 Row() { 284 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 285 Text($r('app.string.user_certificate_credentials')) 286 .fontSize($r('sys.float.ohos_id_text_size_body1')) 287 .fontColor($r('sys.color.ohos_id_color_text_primary')) 288 .fontWeight(FontWeight.Medium) 289 .margin({ left: $r('app.float.wh_value_12') }) 290 .textAlign(TextAlign.Start) 291 } 292 .onClick(() => { 293 this.installCertFlag = false; 294 AppStorage.setOrCreate('installUserCred', true); 295 AppStorage.setOrCreate('installSystemCred',false); 296 if (this.isStartBySheet) { 297 this.startInstallEvidenceBySheet(); 298 } else { 299 this.mFaPresenter.startInstallEvidence(); 300 } 301 }) 302 } 303 .stateStyles({ 304 normal: this.normalStyle, 305 pressed: this.pressedStyle 306 }) 307 .constraintSize({ 308 minHeight: $r('app.float.wh_value_48') 309 }) 310 } 311 .padding($r('app.float.wh_value_4')) 312 .backgroundColor($r('sys.color.ohos_id_color_card_bg')) 313 .borderRadius($r('app.float.radius_20')) 314 .width(ComponentConfig.WH_100_100) 315 } 316 .align(Alignment.Top) 317 .scrollable(ScrollDirection.Vertical) 318 .scrollBar(BarState.Auto) 319 .width(WidthPercent.WH_100_100) 320 .edgeEffect(EdgeEffect.Spring) 321 .layoutWeight(1) 322 .padding({ 323 left: 16, 324 right: 16 325 }) 326 } 327 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 328 .width(WidthPercent.WH_100_100) 329 .height(WidthPercent.WH_100_100) 330 } 331 .width(WidthPercent.WH_100_100) 332 .height(WidthPercent.WH_100_100); 333 } 334 } 335 .margin(vp2px(1) === 2 ? $r('app.float.item_common_horizontal_margin') : $r('app.float.wh_value_0')) 336 .width(WidthPercent.WH_100_100) 337 .height(WidthPercent.WH_100_100); 338 } 339 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 340 .width(WidthPercent.WH_100_100) 341 .height(WidthPercent.WH_100_100); 342 } 343 344 onPageShow() { 345 let uiExtensionFlag = GlobalContext.getContext().getFlag(); 346 let installType: string = GlobalContext.getContext().getAbilityWant().parameters?.installType as string; 347 let uri = GlobalContext.getContext().getAbilityWant().uri || 348 GlobalContext.getContext().getAbilityWant().parameters?.uri; 349 GlobalContext.getContext().clearAbilityWantUri(); 350 GlobalContext.getContext().clearAbilityWantParamsUri(); 351 if (uri === 'certInstall') { 352 router.pushUrl({ 353 url: 'pages/certInstallFromStorage' 354 }) 355 } else if (uri === 'requestAuthorize') { 356 this.mFaPresenter.startRequestAuth(GlobalContext.getContext().getAbilityWant().parameters?.appUid as string); 357 } else if (uiExtensionFlag && uri === 'systemCredInstall' && installType === 'systemCred') { 358 AppStorage.setOrCreate('installSystemCred', true); 359 AppStorage.setOrCreate('installUserCred', false); 360 this.mFaPresenter.startInstallEvidence(); 361 } else if (uiExtensionFlag && uri === 'specifyInstall') { 362 let fileUri = GlobalContext.getContext().getAbilityWant().parameters?.fileUri as string; 363 this.mFaPresenter.startInstall(installType, fileUri); 364 } else { 365 console.error('The want type is not supported'); 366 } 367 } 368}