1/* 2 * Copyright (c) 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'; 18 19let dmClass: deviceManager.DeviceManager | null; 20let TAG = '[DeviceManagerUI:PinDialog]==>'; 21const ACTION_CANCEL_PINCODE_DISPLAY: number = 3; 22const MSG_CANCEL_PIN_CODE_SHOW: number = 2; 23 24@Entry 25@Component 26struct PinDialog { 27 @State pinCode: string = ''; 28 @State pinCodeArr: Array<string> = []; 29 @State btnColor: ResourceColor = Color.Transparent; 30 @State isUserOperate: boolean = false; 31 32 aboutToAppear() { 33 console.log(TAG + 'aboutToAppear execute PinCustomDialog'); 34 // 获取pinCode 35 this.pinCode = AppStorage.get('pinCode') as string; 36 this.pinCodeArr = this.pinCode.split(''); 37 console.log(TAG + 'this.pinCodeArr' + this.pinCodeArr); 38 this.initStatue(); 39 } 40 41 aboutToDisappear() { 42 console.log(TAG + 'aboutToDisappear executed'); 43 if (dmClass != null) { 44 try { 45 dmClass.off('uiStateChange'); 46 try { 47 dmClass.release(); 48 } catch (err) { 49 let e: BusinessError = err as BusinessError; 50 console.error(TAG + 'release device manager errCode:' + e.code + ',errMessage:' + e.message); 51 } 52 } catch (error) { 53 console.log(TAG + 'dmClass release failed'); 54 } 55 dmClass = null; 56 } 57 } 58 59 cancel() { 60 console.log(TAG + 'destruction()'); 61 try { 62 console.log(TAG + 'pin dialog terminateSelf'); 63 let session = AppStorage.get<UIExtensionContentSession>('pinSession'); 64 if (session) { 65 session.terminateSelf(); 66 } 67 } catch (err) { 68 console.log(TAG + 'dialog cancel failed: ' + JSON.stringify(err)); 69 } 70 } 71 72 onPageHide() { 73 console.log('onPageHide'); 74 if (this.isUserOperate) { 75 console.log('user operate'); 76 return; 77 } 78 this.cancel(); 79 } 80 81 initStatue() { 82 if (dmClass) { 83 console.log(TAG + 'deviceManager exist'); 84 return; 85 } 86 deviceManager.createDeviceManager('com.ohos.devicemanagerui.pin', 87 (err: Error, dm: deviceManager.DeviceManager) => { 88 if (err) { 89 console.log('createDeviceManager err:' + JSON.stringify(err) + ' --fail:' + JSON.stringify(dm)); 90 return; 91 } 92 dmClass = dm; 93 dmClass.on('uiStateChange', (data: Record<string, string>) => { 94 console.log('uiStateChange executed, dialog closed' + JSON.stringify(data)); 95 let tmpStr: Record<string, number> = JSON.parse(data.param); 96 let msg: number = tmpStr.uiStateMsg as number; 97 if (msg === MSG_CANCEL_PIN_CODE_SHOW) { 98 this.destruction(); 99 } 100 }) 101 }); 102 } 103 104 setUserOperation(operation: number) { 105 console.log(TAG + 'setUserOperation: ' + operation); 106 if (dmClass === null) { 107 console.log(TAG + 'setUserOperation: ' + 'dmClass null'); 108 return; 109 } 110 try { 111 this.isUserOperate = true; 112 dmClass.setUserOperation(operation, 'extra'); 113 } catch (error) { 114 console.log(TAG + 'dmClass setUserOperation failed'); 115 } 116 } 117 118 destruction() { 119 console.log(TAG + 'destruction()'); 120 try { 121 console.log(TAG + 'pin dialog terminateSelf'); 122 let session = AppStorage.get<UIExtensionContentSession>('pinSession'); 123 if (session) { 124 session.terminateSelf(); 125 } 126 } catch (err) { 127 console.log(TAG + 'dialog cancel failed: ' + JSON.stringify(err)); 128 } 129 } 130 131 @Builder 132 ConnectionCode() { 133 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 134 Text($r('app.string.dm_connect_code')) 135 .fontSize($r('sys.float.ohos_id_text_size_sub_title1')) 136 .fontColor('#FFFFFF') 137 .fontWeight(FontWeight.Bold) 138 .margin({ 139 left: 24, 140 right: 24 141 }) 142 .offset({ y: 10}) 143 } 144 .height(45) 145 .margin({ bottom: 40 }) 146 } 147 148 @Builder 149 PinCode() { 150 Row() { 151 Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 152 ForEach(this.pinCodeArr, (item: string) => { 153 Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 154 Text(item) 155 .fontSize($r('sys.float.ohos_id_text_size_headline5')) 156 .fontColor('#FFFFFF') 157 .fontWeight(FontWeight.Medium) 158 }.width('13%') 159 .height('100%') 160 }) 161 }.height(48) 162 } 163 .margin({ bottom: 34 }) 164 } 165 166 @Builder 167 ButtonCancel() { 168 Flex({ justifyContent: FlexAlign.Center }) { 169 Shape() { 170 Ellipse() 171 .width('100%') 172 .height(60) 173 .fill('#1F71FF') 174 Column() { 175 Button($r('app.string.dm_cancel')) 176 .fontSize($r('sys.float.ohos_id_text_size_sub_title1')) 177 .fontColor('#FFFFFF') 178 .backgroundColor(Color.Transparent) 179 .onClick(() => { 180 this.setUserOperation(ACTION_CANCEL_PINCODE_DISPLAY); 181 this.destruction(); 182 }) 183 .offset({ y: -5 }) 184 } 185 } 186 .align(Alignment.Center) 187 .offset({ y: 10}) 188 } 189 } 190 191 build() { 192 Column() { 193 this.ConnectionCode(); 194 this.PinCode(); 195 this.ButtonCancel(); 196 } 197 .backgroundColor(Color.Black) 198 .width('100%') 199 .height('100%') 200 } 201} 202