• 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 */
15
16import extension from '@ohos.app.ability.ServiceExtensionAbility';
17import window from '@ohos.window';
18import display from '@ohos.display';
19import deviceInfo from '@ohos.deviceInfo';
20
21var TAG = "PermissionManager_Log:";
22const BG_COLOR = '#00000000'
23let bottomPopoverTypes = ['default', 'phone']
24
25export default class GlobalExtensionAbility extends extension {
26    /**
27     * Lifecycle function, called back when a service extension is started for initialization.
28     */
29    onCreate(want) {
30        console.info(TAG + "ServiceExtensionAbility onCreate, ability name is " + want.abilityName);
31
32        globalThis.globalContext = this.context;
33        globalThis.globalState = want.parameters['ohos.sensitive.resource']
34        console.info(TAG + "want: " + JSON.stringify(want))
35
36        try {
37            let dis = display.getDefaultDisplaySync();
38            let navigationBarRect = {
39                left: 0,
40                top: 0,
41                width: dis.width,
42                height: dis.height
43            }
44            let isVertical = dis.width > dis.height ? false : true
45            globalThis.isBottomPopover = bottomPopoverTypes.includes(deviceInfo.deviceType) && isVertical
46            this.createWindow("globalDialog", window.WindowType.TYPE_KEYGUARD, navigationBarRect)
47        } catch (exception) {
48            console.error(TAG + 'Failed to obtain the default display object. Code: ' + JSON.stringify(exception));
49        };
50    }
51
52    /**
53     * Lifecycle function, called back when a service extension is started or recall.
54     */
55    onRequest(want, startId) {
56        console.info(TAG + "ServiceExtensionAbility onRequest. start id is " + startId);
57    }
58
59    /**
60     * Lifecycle function, called back before a service extension is destroyed.
61     */
62    onDestroy() {
63        console.info(TAG + "ServiceExtensionAbility onDestroy.");
64        globalThis.globalWin.destroy()
65    }
66
67    private async createWindow(name: string, windowType: number, rect) {
68        console.info(TAG + "create window")
69        try {
70            const win = await window.createWindow({ ctx: globalThis.globalContext, name, windowType })
71            globalThis.globalWin = win
72            await win.moveWindowTo(rect.left, rect.top)
73            await win.resize(rect.width, rect.height)
74            await win.setUIContent('pages/globalSwitch')
75            await win.setWindowBackgroundColor(BG_COLOR)
76            await win.showWindow()
77        } catch {
78            console.info(TAG + "window create failed!")
79        }
80    }
81};