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