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 */ 15import deviceManager from '@ohos.distributedHardware.deviceManager'; 16import { BusinessError } from '@ohos.base'; 17import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 18import deviceInfo from '@ohos.deviceInfo'; 19import Constant from '../common/constant'; 20import i18n from '@ohos.i18n'; 21import { KeyCode } from '@ohos.multimodalInput.keyCode'; 22 23let dmClass: deviceManager.DeviceManager | null; 24let TAG = '[DeviceManagerUI:PinDialog]==>'; 25const ACTION_CANCEL_PINCODE_DISPLAY: number = 3; 26const MSG_CANCEL_PIN_CODE_SHOW: number = 2; 27 28@CustomDialog 29struct PinCustomDialog { 30 @State pinCode: string = ''; 31 @State pinCodeArr: Array<string> = []; 32 @State btnColor: ResourceColor = Color.Transparent; 33 @State isPC: boolean = false; 34 controller?: CustomDialogController 35 36 cancel() { 37 console.log(TAG + 'destruction()'); 38 try { 39 console.log(TAG + 'pin dialog terminateSelf'); 40 let session = AppStorage.get<UIExtensionContentSession>('pinSession'); 41 if (session) { 42 session.terminateSelf(); 43 } 44 } catch (err) { 45 console.log(TAG + 'dialog cancel failed: ' + JSON.stringify(err)); 46 } 47 } 48 49 aboutToAppear() { 50 console.log(TAG + 'aboutToAppear execute PinCustomDialog'); 51 this.isPC = Constant.isPC(); 52 // 获取pinCode 53 this.pinCode = AppStorage.get('pinCode') as string; 54 this.pinCodeArr = this.pinCode.split(''); 55 console.log(TAG + 'this.pinCodeArr' + this.pinCodeArr); 56 } 57 58 setUserOperation(operation: number) { 59 console.log(TAG + 'setUserOperation: ' + operation); 60 if (dmClass == null) { 61 console.log(TAG + 'setUserOperation: ' + 'dmClass null'); 62 return; 63 } 64 try { 65 dmClass.setUserOperation(operation, 'extra'); 66 } catch (error) { 67 console.log(TAG + 'dmClass setUserOperation failed'); 68 } 69 } 70 71 private isTibetanLanguages(): boolean { 72 console.info(`${TAG} isTibetanLanguages in`); 73 let locale = new Intl.Locale(i18n.System.getSystemLanguage()).toString(); 74 console.info(`${TAG} isTibetanLanguages: ${locale}`); 75 return Constant.TIBETAN_LANGUAGES.includes(locale); 76 } 77 78 build() { 79 GridRow({ 80 columns: { xs: 4, sm: 8, md: this.isPC ? 24 : 12 }, 81 gutter: { x: 4 }, 82 breakpoints: { value: ['600vp', '840vp'] } 83 }) { 84 GridCol({ span: { xs: 4, sm: 4, md: this.isPC ? 6 : 4 }, offset: { sm: 2, md: this.isPC ? 9 : 4 } }) { 85 Column() { 86 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 87 Text($r('app.string.dm_connect_code')) 88 .fontSize($r('sys.float.ohos_id_text_size_dialog_tittle')) 89 .fontColor($r('sys.color.ohos_id_color_text_primary')) 90 .fontWeight(FontWeight.Bold) 91 .lineHeight(this.isTibetanLanguages() ? 32.5 : 0) 92 .margin({ 93 left: 24, 94 right: 24 95 }) 96 } 97 .constraintSize({ minHeight: 56 }) 98 .margin({ bottom: this.isPC ? 16 : 8 }) 99 100 Row() { 101 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 102 ForEach(this.pinCodeArr, (item: string) => { 103 Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 104 Text(item) 105 .fontSize($r('sys.float.ohos_id_text_size_headline7')) 106 .fontColor($r('sys.color.ohos_id_color_text_primary')) 107 .fontWeight(FontWeight.Medium) 108 }.width('10%') 109 .height('100%') 110 }) 111 } 112 .height(48) 113 .accessibilityText('[n1]'+this.pinCode+'[n0]') 114 } 115 .margin({ bottom: this.isPC ? 16 : 8 }) 116 117 Flex({ justifyContent: FlexAlign.Center }) { 118 Button($r('app.string.dm_cancel')) 119 .fontSize($r('sys.float.ohos_id_text_size_button1')) 120 .fontColor($r('sys.color.ohos_id_color_text_primary_activated')) 121 .constraintSize({ minHeight: 40 }) 122 .width('100%') 123 .height(57) 124 .backgroundColor(this.btnColor) 125 .defaultFocus(true) 126 .onKeyEvent((event?: KeyEvent) => { 127 if (event && event?.keyCode === KeyCode.KEYCODE_HOME && event?.type === KeyType.Down) { 128 console.log(TAG + 'onKeyEvent eventType: ' + event?.type) 129 return; 130 } 131 if (event && event?.keyCode === KeyCode.KEYCODE_HOME && event?.type === KeyType.Up) { 132 console.log(TAG + 'onKeyEvent eventType: ' + event?.type) 133 if (this.controller) { 134 this.controller.close(); 135 } 136 this.cancel(); 137 this.setUserOperation(ACTION_CANCEL_PINCODE_DISPLAY); 138 } 139 }) 140 .onClick(() => { 141 if (this.controller) { 142 this.controller.close(); 143 } 144 this.cancel(); 145 this.setUserOperation(ACTION_CANCEL_PINCODE_DISPLAY); 146 }) 147 .onHover((isHover?: boolean, event?: HoverEvent): void => { 148 if (isHover) { 149 this.btnColor = $r('sys.color.ohos_id_color_hover'); 150 } else { 151 this.btnColor = this.isPC ? $r('sys.color.ohos_id_color_button_normal') : Color.Transparent; 152 } 153 }) 154 .stateStyles({ 155 pressed: { 156 .backgroundColor($r('sys.color.ohos_id_color_click_effect')) 157 }, 158 normal: { 159 .backgroundColor(this.isPC ? $r('sys.color.ohos_id_color_button_normal') : Color.Transparent) 160 } 161 }) 162 }.margin({ 163 left: 16, 164 right: 16, 165 bottom: this.isPC ? 24 : 16 }) 166 } 167 .constraintSize({ maxHeight: `${300}` }) 168 .borderRadius($r('sys.float.ohos_id_corner_radius_dialog')) 169 .backgroundBlurStyle(BlurStyle.COMPONENT_ULTRA_THICK) 170 .margin({ left: $r('sys.float.ohos_id_dialog_margin_start'), right: $r('sys.float.ohos_id_dialog_margin_end') }) 171 } 172 } 173 } 174} 175 176@Entry 177@Component 178struct dialogPlusPage { 179 dialogController: CustomDialogController = new CustomDialogController({ 180 builder: PinCustomDialog(), 181 cancel: this.onCancel, 182 autoCancel: false, 183 onWillDismiss: ()=>{ 184 this.onWillDismiss() 185 }, 186 alignment: DialogAlignment.Center, 187 offset: { dx: 0, dy: -20 }, 188 customStyle: true, 189 maskColor: $r('sys.color.ohos_id_color_mask_thin') 190 }); 191 192 onCancel() { 193 this.destruction(); 194 } 195 196 onWillDismiss() { 197 console.log(TAG + 'onWillDismiss: ' + ACTION_CANCEL_PINCODE_DISPLAY) 198 this.setUserOperation(ACTION_CANCEL_PINCODE_DISPLAY); 199 this.destruction(); 200 } 201 202 aboutToAppear() { 203 this.initStatue(); 204 console.log(TAG + 'aboutToAppear execute') 205 } 206 207 aboutToDisappear() { 208 console.log(TAG + 'aboutToDisappear executed') 209 if (dmClass != null) { 210 try { 211 dmClass.off('uiStateChange') 212 try { 213 dmClass.release(); 214 } catch (err) { 215 let e: BusinessError = err as BusinessError; 216 console.error(TAG + 'release device manager errCode:' + e.code + ',errMessage:' + e.message); 217 } 218 } catch (error) { 219 console.log(TAG + 'dmClass release failed') 220 } 221 dmClass = null 222 } 223 } 224 225 initStatue() { 226 if (dmClass) { 227 console.log(TAG + 'deviceManager exist'); 228 return; 229 } 230 deviceManager.createDeviceManager('com.ohos.devicemanagerui.pin', 231 (err: Error, dm: deviceManager.DeviceManager) => { 232 if (err) { 233 console.log('createDeviceManager err:' + JSON.stringify(err) + ' --fail:' + JSON.stringify(dm)) 234 return 235 } 236 dmClass = dm 237 dmClass.on('uiStateChange', (data: Record<string, string>) => { 238 console.log('uiStateChange executed, dialog closed' + JSON.stringify(data)) 239 let tmpStr: Record<string, number> = JSON.parse(data.param) 240 let msg: number = tmpStr.uiStateMsg as number 241 if (msg === MSG_CANCEL_PIN_CODE_SHOW) { 242 this.destruction() 243 } 244 }) 245 }); 246 } 247 248 setUserOperation(operation: number) { 249 console.log(TAG + 'setUserOperation: ' + operation) 250 if (dmClass == null) { 251 console.log(TAG + 'setUserOperation: ' + 'dmClass null') 252 return; 253 } 254 try { 255 dmClass.setUserOperation(operation, 'extra'); 256 } catch (error) { 257 console.log(TAG + 'dmClass setUserOperation failed') 258 } 259 } 260 261 destruction() { 262 console.log(TAG + 'destruction()'); 263 try { 264 console.log(TAG + 'pin dialog terminateSelf'); 265 let session = AppStorage.get<UIExtensionContentSession>('pinSession'); 266 if (session) { 267 session.terminateSelf(); 268 } 269 } catch (err) { 270 console.log(TAG + 'dialog cancel failed: ' + JSON.stringify(err)); 271 } 272 } 273 274 build() { 275 Column(this.dialogController.open()) 276 } 277}