• 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 UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
17import common from '@ohos.app.ability.common';
18
19let dmClass: deviceManager.DeviceManager | null;
20let TAG = '[DeviceManagerUI:ConfirmDialog]==>';
21const ACTION_ALLOW_AUTH_ONCE: number = 0;
22const ACTION_CANCEL_AUTH: number = 1;
23const ACTION_AUTH_CONFIRM_TIMEOUT: number = 2;
24const ACTION_ALLOW_AUTH_ALWAYS: number = 6;
25const MSG_CANCEL_CONFIRM_SHOW: number = 5;
26const DEVICE_TYPE_2IN1: number = 0xA2F;
27const DEVICE_TYPE_PC: number = 0x0C;
28const CAST_PKG_NAME: string = 'CastEngineService';
29
30@Entry
31@Component
32struct Index {
33  @State peerAppOperation: string = '';
34  @State peerCustomDescription: string = '';
35  @State peerDeviceName: string = '';
36  @State peerDeviceType: number = 0;
37  @State secondsNum: number = 30;
38  @State times: number = 0;
39  @State isAvailableType: boolean = false;
40  @State title: string = '';
41  @State isUserOperate: boolean = false;
42
43  initStatue() {
44    if (dmClass) {
45      console.log(TAG + 'deviceManager exist');
46      return;
47    }
48    deviceManager.createDeviceManager('com.ohos.devicemanagerui.confirm',
49      (err: Error, dm: deviceManager.DeviceManager) => {
50        if (err) {
51          console.log('createDeviceManager err:' + JSON.stringify(err) + ' --fail:' + JSON.stringify(dm));
52          return;
53        }
54        dmClass = dm;
55        dmClass.on('uiStateChange', (data: Record<string, string>) => {
56          console.log('uiStateChange executed, dialog closed' + JSON.stringify(data));
57          let tmpStr: Record<string, number> = JSON.parse(data.param);
58          let msg: number = tmpStr.uiStateMsg as number;
59          if (msg === MSG_CANCEL_CONFIRM_SHOW) {
60            this.destruction();
61            return;
62          }
63        });
64      });
65  }
66
67  getImages(peerdeviceType: number): Resource {
68    console.info('peerdeviceType is ' + peerdeviceType);
69    if (peerdeviceType === deviceManager.DeviceType.SPEAKER) {
70      this.isAvailableType = true;
71      return $r('sys.symbol.soundai_fill');
72    } else if (peerdeviceType === deviceManager.DeviceType.PHONE) {
73      this.isAvailableType = true;
74      return $r('sys.symbol.phone_fill_1');
75    } else if (peerdeviceType === deviceManager.DeviceType.TABLET) {
76      this.isAvailableType = true;
77      return $r('sys.symbol.pad_fill');
78    } else if (peerdeviceType === deviceManager.DeviceType.WEARABLE) {
79      this.isAvailableType = true;
80      return $r('sys.symbol.earphone_case_16896');
81    } else if (peerdeviceType === deviceManager.DeviceType.CAR) {
82      this.isAvailableType = true;
83      return $r('sys.symbol.car_fill');
84    } else if (peerdeviceType === deviceManager.DeviceType.TV) {
85      this.isAvailableType = true;
86      return $r('sys.symbol.smartscreen_fill');
87    } else if (peerdeviceType === DEVICE_TYPE_PC) {
88      this.isAvailableType = true;
89      return $r('sys.symbol.matebook_fill');
90    } else if (peerdeviceType === DEVICE_TYPE_2IN1) {
91      this.isAvailableType = true;
92      return $r('sys.symbol.matebook_fill');
93    } else {
94      this.isAvailableType = false;
95      return $r('sys.symbol.unknown_device_fill');
96    }
97  }
98
99  onPageShow() {
100    console.log('onPageShow');
101    this.initStatue();
102  }
103
104  onPageHide() {
105    console.log('onPageHide');
106    if (this.isUserOperate) {
107      console.log('user operate');
108      return;
109    }
110    this.onCancel();
111  }
112
113  destruction() {
114    if (dmClass != null) {
115      try {
116        dmClass.release();
117        dmClass = null;
118      } catch (error) {
119        console.log('dmClass release failed');
120      }
121    }
122    let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession');
123    if (session) {
124      session.terminateSelf();
125    }
126  }
127
128  setUserOperation(operation: number) {
129    console.log(TAG + 'setUserOperation: ' + operation);
130    if (dmClass === null) {
131      console.log(TAG + 'setUserOperation: ' + 'dmClass null');
132      return;
133    }
134    try {
135      this.isUserOperate = true;
136      dmClass.setUserOperation(operation, 'extra');
137    } catch (error) {
138      console.log(TAG + 'dmClass setUserOperation failed');
139    }
140  }
141
142  aboutToAppear() {
143    console.log(TAG + 'aboutToAppear execute PinCustomDialog');
144    let context = getContext() as common.UIAbilityContext;
145
146    if (AppStorage.get('deviceName') != null) {
147      this.peerDeviceName = AppStorage.get('deviceName') as string;
148    }
149    let hostPkgLabel: string = AppStorage.get('hostPkgLabel') as string;
150    if (hostPkgLabel === CAST_PKG_NAME) {
151      this.title =
152        context.resourceManager.getStringSync($r('app.string.dm_confirm_title_cast').id, this.peerDeviceName);
153    } else if (hostPkgLabel != null) {
154      this.title = context.resourceManager.getStringSync($r('app.string.dm_confirm_title_hap').id, hostPkgLabel,
155        this.peerDeviceName);
156      this.peerCustomDescription = context.resourceManager.getStringSync($r('app.string.dm_confirm_intention').id);
157    } else {
158      let titleFirst: string =
159        context.resourceManager.getStringSync($r('app.string.dm_connect_device').id, this.peerDeviceName);
160      this.title =
161        context.resourceManager.getStringSync($r('app.string.dm_is_trust_device').id, titleFirst);
162      this.peerCustomDescription = context.resourceManager.getStringSync($r('app.string.dm_confirm_intention').id);
163    }
164
165    if (AppStorage.get('deviceType') != null) {
166      this.peerDeviceType = AppStorage.get('deviceType') as number;
167      console.log('peerDeviceType is ' + this.peerDeviceType);
168    }
169
170    this.times = setInterval(() => {
171      console.info('devicemanagerui confirm dialog run seconds:' + this.secondsNum);
172      this.secondsNum--;
173      if (this.secondsNum === 0) {
174        clearInterval(this.times);
175        this.times = 0;
176        this.setUserOperation(ACTION_AUTH_CONFIRM_TIMEOUT);
177        this.destruction();
178        console.info('click cancel times run out');
179      }
180    }, 1000);
181  }
182
183  onAllowOnce() {
184    console.log('allow once');
185    if (dmClass === null) {
186      console.log('createDeviceManager is null');
187      return;
188    }
189
190    console.log('allow once' + ACTION_ALLOW_AUTH_ONCE);
191    this.setUserOperation(ACTION_ALLOW_AUTH_ONCE);
192    this.destruction();
193  }
194
195  onAllowAlways() {
196    console.log('allow always');
197    if (dmClass === null) {
198      console.log('createDeviceManager is null');
199      return;
200    }
201
202    console.log('allow always' + ACTION_ALLOW_AUTH_ALWAYS);
203    this.setUserOperation(ACTION_ALLOW_AUTH_ALWAYS);
204    this.destruction();
205  }
206
207  onCancel() {
208    console.log('cancel');
209    if (dmClass === null) {
210      console.log('createDeviceManager is null');
211      return;
212    }
213
214    console.log('cancel' + ACTION_CANCEL_AUTH);
215    this.setUserOperation(ACTION_CANCEL_AUTH);
216    this.destruction();
217  }
218
219  @Builder
220  Symbol() {
221    Shape() {
222      Circle()
223        .width(52)
224        .height(52)
225        .fill($r('sys.color.ohos_id_color_activated'))
226      Column() {
227        SymbolGlyph(this.getImages(this.peerDeviceType))
228          .fontSize('36vp')
229          .renderingStrategy(SymbolRenderingStrategy.MULTIPLE_OPACITY)
230          .fontColor([$r('sys.color.ohos_id_color_primary_contrary')])
231          .offset({ x: 8, y: 8 })
232      }
233    }
234    .visibility(this.isAvailableType ? Visibility.Visible : Visibility.None)
235    .margin({ bottom: 12, top: 6 })
236  }
237
238  @Builder
239  Title() {
240    Column() {
241      Text(this.title)
242        .textAlign(TextAlign.Center)
243        .fontSize($r('sys.float.ohos_id_text_size_body2'))
244        .fontWeight(FontWeight.Regular)
245        .fontColor('#FFFFFF')
246        .width('auto')
247      Text(this.peerCustomDescription)
248        .textAlign(TextAlign.Center)
249        .fontColor('#99FFFFFF')
250        .fontWeight(FontWeight.Regular)
251        .fontSize($r('sys.float.ohos_id_text_size_body3'))
252        .width('auto')
253        .margin({ top: 2 })
254        .visibility(this.peerCustomDescription === '' ? Visibility.None : Visibility.Visible)
255    }.margin({
256      top: this.isAvailableType ? 0 : 6,
257      bottom: 12, left: 26, right: 26 })
258  }
259
260  @Builder
261  Buttons() {
262    Column() {
263      Button($r('app.string.dm_allow_always'))
264        .margin({ bottom: 12 })
265        .onClick(() => {
266          this.onAllowAlways();
267        })
268        .fontColor('#FFFFFF')
269        .fontSize($r('sys.float.ohos_id_text_size_button2'))
270        .height(40)
271        .width('100%')
272        .backgroundColor('#1F71FF')
273      Button($r('app.string.dm_allow_this_time'))
274        .margin({ bottom: 12 })
275        .onClick(() => {
276          this.onAllowOnce();
277        })
278        .fontColor('#5EA1FF')
279        .fontSize($r('sys.float.ohos_id_text_size_button2'))
280        .height(40)
281        .width('100%')
282        .backgroundColor('#405ea1ff')
283      Button($r('app.plural.dm_not_allow', this.secondsNum, this.secondsNum))
284        .onClick(() => {
285          this.onCancel();
286        })
287        .fontColor('#5EA1FF')
288        .fontSize($r('sys.float.ohos_id_text_size_button2'))
289        .height(40)
290        .width('100%')
291        .backgroundColor('#405ea1ff')
292    }
293    .margin({ left: 50, right: 50, bottom: 36})
294  }
295
296  build() {
297    Scroll() {
298      Column() {
299        this.Symbol();
300        this.Title();
301        this.Buttons();
302      }
303    }
304    .backgroundColor(Color.Black)
305    .height('100%')
306   .width('100%')
307  }
308}
309