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