• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2023 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 */
15
16import extension from '@ohos.app.ability.ServiceExtensionAbility';
17import window from '@ohos.window';
18import display from '@ohos.display';
19import deviceInfo from '@ohos.deviceInfo';
20import Constant from '../common/constant';
21import type Want from '@ohos.app.ability.Want';
22
23const TAG = '[DeviceManagerUI:BluetoothDialog]==>';
24const dmCallerUid: number = 3062;
25
26export default class ServiceExtAbility extends extension {
27  isCreatingWindow: boolean = false;
28  onCreate(want: Want): void {
29    AppStorage.SetOrCreate('bluetoothDialogContext', this.context);
30    AppStorage.SetOrCreate('bluetoothDialogWindowNum', 0);
31    this.getShareStyle();
32  }
33
34  onRequest(want: Want, startId: number): void {
35    if (want.parameters['ohos.aafwk.param.callerUid'] as number !== dmCallerUid) {
36      console.log(TAG + 'device manager callerUid ' + want.parameters['ohos.aafwk.param.callerUid']);
37      return;
38    }
39    console.log(TAG + 'onRequest execute' + JSON.stringify(want.parameters));
40    let bluetoothDialogWindowNum: number = AppStorage.get('bluetoothDialogWindowNum');
41    if (bluetoothDialogWindowNum !== 0 || this.isCreatingWindow) {
42      console.log(TAG + 'onRequest window number is not zero or creating window.');
43      return;
44    }
45    this.isCreatingWindow = true;
46    AppStorage.SetOrCreate('abilityWant', want);
47    let globalWant: Want = AppStorage.get('abilityWant') as Want;
48    console.log(TAG + 'onRequest execute' + JSON.stringify(globalWant.parameters));
49
50    display.getDefaultDisplay().then((dis: display.Display) => {
51      let density: number = dis.densityPixels;
52      let dialogRect: { left: number; top: number; width: number; height: number; } = {
53        left: (dis.width - (globalThis.style.shareWidth * density)) * Constant.HALF,
54        top: (dis.height - (globalThis.style.shareHeight * density)) * Constant.HALF -
55          (globalThis.style.shareHeight * density) / Constant.TOP_OFFSET_PROPORTION,
56        width: globalThis.style.shareWidth * density,
57        height: globalThis.style.shareHeight * density * Constant.DIALOG_HEIGHT_PROPORTION
58      };
59      this.createWindow('picker Dialog:' + startId, window.WindowType.TYPE_FLOAT, dialogRect);
60    });
61  }
62
63  getShareStyle(): void {
64    globalThis.style = {};
65    if (deviceInfo.deviceType === 'phone' || deviceInfo.deviceType === 'default') {
66      globalThis.style.shareWidth = Constant.SHARE_WIDTH_PHONE;
67      globalThis.style.shareHeight = Constant.SHARE_HEIGHT_PHONE;
68    } else {
69      globalThis.style.shareWidth = Constant.SHARE_WIDTH_PAD;
70      globalThis.style.shareHeight = Constant.SHARE_HEIGHT_PAD;
71    }
72  }
73
74  onDestroy(): void {
75    console.log(TAG + 'ServiceExtAbility destroyed');
76  }
77
78  private async createWindow(name: string, windowType: window.WindowType,
79    rect: { left: number; top: number; width: number; height: number; }): Promise<void> {
80    console.log(TAG + 'createWindow execute');
81    try {
82      const win: window.Window = await window.create(this.context, name, windowType);
83      AppStorage.SetOrCreate('bluetoothDialogWin', win);
84      await win.moveTo(rect.left, rect.top);
85      await win.resetSize(rect.width, rect.height);
86      await win.loadContent('pages/BluetoothDialog');
87      await win.show();
88      let windowNum: number = AppStorage.get('bluetoothDialogWindowNum') as number;
89      windowNum++;
90      AppStorage.SetOrCreate('bluetoothDialogWindowNum', windowNum);
91      this.isCreatingWindow = false;
92      console.log(TAG + 'window create successfully');
93    } catch {
94      this.isCreatingWindow = false;
95      console.info(TAG + 'window create failed');
96    }
97  }
98};