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