• 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 ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
17import window from '@ohos.window';
18import display from '@ohos.display';
19
20const BG_COLOR = '#00000000';
21var g_thermalLowWindowFirst = undefined;
22
23export default class ThermalLowDialogAbility extends ServiceExtensionAbility {
24    /**
25     * Lifecycle function, called back when a service extension is started for initialization.
26     */
27    onCreate(want) {
28        console.log('ThermalLowDialogAbility onCreate' + want.abilityName);
29        globalThis.extensionContext = this.context;
30        globalThis.g_thermalLowWindowFirst = g_thermalLowWindowFirst;
31    }
32
33    /**
34     * Lifecycle function, called back when a service extension is started or recall.
35     */
36    onRequest(want, startId) {
37        globalThis.abilityWant = want;
38        console.log('ThermalLowDialogAbility onRequest. start id is ' + startId);
39        console.log( 'want: ' + JSON.stringify(want));
40        display.getDefaultDisplay().then(dis => {
41            let navigationBarRect = {
42                left: 0,
43                top: 0,
44                width: dis.width,
45                height: dis.height
46            };
47            this.createWindow("Thermal_low Dialog" + startId, window.WindowType.TYPE_FLOAT, navigationBarRect);
48        })
49    }
50
51    /**
52     * Lifecycle function, called back before a service extension is destroyed.
53     */
54    onDestroy() {
55        console.log('ThermalServiceExtAbility_low onDestroy.');
56    }
57
58    private async createWindow(name: string, windowType: number, rect) {
59        try {
60            if (globalThis.g_thermalLowWindowFirst != undefined) {
61                console.log('destroy first thermal low window');
62                globalThis.g_thermalLowWindowFirst.destroy();
63                globalThis.g_thermalLowWindowFirst = undefined;
64            }
65            const thermalLowWin = await window.create(globalThis.extensionContext, name, windowType);
66            if (globalThis.g_thermalLowWindowFirst == undefined) {
67                g_thermalLowWindowFirst = thermalLowWin;
68                globalThis.g_thermalLowWindowFirst = g_thermalLowWindowFirst;
69            }
70            globalThis.thermalLowWindow = thermalLowWin;
71            await thermalLowWin.moveTo(rect.left, rect.top);
72            await thermalLowWin.resetSize(rect.width, rect.height);
73            await thermalLowWin.loadContent('pages/thermalLowDialog');
74            await thermalLowWin.setBackgroundColor(BG_COLOR);
75            await thermalLowWin.show();
76            console.log('Thermal_low window create success');
77        } catch {
78            console.log('Thermal_low window create failed');
79        }
80    }
81}