• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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 */
15import rpc from '@ohos.rpc';
16import hilog from '@ohos.hilog';
17import window from '@ohos.window';
18import display from '@ohos.display';
19import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
20import type Want from '@ohos.application.Want';
21
22interface IRect {
23  left: number;
24  top: number;
25  width: number;
26  height: number;
27}
28
29const RECT_NUMBER = 72;
30const TAG = 'DialogExtensionAbility';
31
32class DialogStub extends rpc.RemoteObject {
33  constructor(des: string) {
34    super(des);
35  }
36}
37
38export default class DialogExtensionAbility extends ServiceExtensionAbility {
39  onCreate(want: Want): void {
40    hilog.info(0, TAG, 'onCreate');
41    globalThis.context = this.context;
42  }
43
44  onConnect(want: Want): DialogStub {
45    hilog.info(0, TAG, 'onConnect');
46    globalThis.dialogInfo = {
47      appName: want.parameters.appName,
48      deviceType: want.parameters.deviceType,
49    };
50    display
51      .getDefaultDisplay()
52      .then((display: display.Display) => {
53        const dialogRect = {
54          left: 0,
55          top: 72,
56          width: display.width,
57          height: display.height - RECT_NUMBER,
58        };
59        this.createFloatWindow('PasteboardDialog' + new Date().getTime(), dialogRect);
60      })
61      .catch((err) => {
62        hilog.info(0, TAG, 'getDefaultDisplay err: ' + JSON.stringify(err));
63      });
64    return new DialogStub('PasteboardDialog');
65  }
66
67  onRequest(want: Want, startId: number): void {
68    hilog.info(0, TAG, 'onRequest');
69    this.onConnect(want);
70  }
71
72  onDisconnet(): void {
73    hilog.info(0, TAG, 'onDisconnet');
74    this.onDestroy();
75  }
76
77  onDestroy(): void {
78    hilog.info(0, TAG, 'onDestroy');
79    globalThis.extensionWin.destroyWindow();
80    globalThis.context.terminateSelf();
81  }
82
83  private async createFloatWindow(name: string, rect: IRect): Promise<void> {
84    hilog.info(0, TAG, 'create window begin');
85
86    if (globalThis.windowNum > 0) {
87      globalThis.windowNum = 0;
88      this.onDestroy();
89    }
90    let windowClass = null;
91    let config = {
92      name,
93      windowType: window.WindowType.TYPE_FLOAT,
94      ctx: this.context,
95    };
96    try {
97      window.createWindow(config, (err, data) => {
98        if (err.code) {
99          hilog.error(0, TAG, 'Failed to create the window. Cause: ' + JSON.stringify(err));
100          return;
101        }
102        windowClass = data;
103        globalThis.extensionWin = data;
104        hilog.info(0, TAG, 'Succeeded in creating the window. Data: ' + JSON.stringify(data));
105        try {
106          windowClass.setUIContent('pages/index', (err) => {
107            if (err.code) {
108              hilog.error(0, TAG, 'Failed to load the content. Cause:' + JSON.stringify(err));
109              return;
110            }
111            windowClass.moveWindowTo(rect.left, rect.top);
112            windowClass.resize(rect.width, rect.height);
113            windowClass.setBackgroundColor('#00000000');
114            windowClass.showWindow();
115            globalThis.windowNum++;
116            hilog.info(0, TAG, 'Create window successfully');
117          });
118        } catch (exception) {
119          hilog.error(0, TAG, 'Failed to load the content. Cause:' + JSON.stringify(exception));
120        }
121      });
122    } catch (exception) {
123      hilog.error(0, TAG, 'Failed to create the window. Cause: ' + JSON.stringify(exception));
124    }
125  }
126}
127