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