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 thermalHighWindowFirst = undefined;
23
24export default class ThermalHighDialogAbility extends ServiceExtensionAbility {
25  /**
26   * Lifecycle function, called back when a service extension is started for initialization.
27   */
28  onCreate(want) {
29    console.log('ThermalHighDialogAbility onCreate' + want.abilityName);
30    GlobalContext.getContext().setObject('extensionContext', this.context);
31    GlobalContext.getContext().setObject('g_thermalHighWindowFirst', thermalHighWindowFirst);
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('ThermalHighDialogAbility 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_high 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_high onDestroy.');
57  }
58
59  private async createWindow(name: string, windowType: number, rect) {
60    try {
61      if (globalThis.g_thermalHighWindowFirst !== undefined) {
62        console.log('destroy first thermal high window');
63        globalThis.g_thermalHighWindowFirst.destroy();
64        globalThis.g_thermalHighWindowFirst = undefined;
65      }
66      const thermalHighWin = await window.create(globalThis.extensionContext, name, windowType);
67      if (globalThis.g_thermalHighWindowFirst === undefined) {
68        thermalHighWindowFirst = thermalHighWin;
69        globalThis.g_thermalHighWindowFirst = thermalHighWindowFirst;
70      }
71      GlobalContext.getContext().setObject('thermalHighWindow', thermalHighWin);
72      await thermalHighWin.moveTo(rect.left, rect.top);
73      await thermalHighWin.resetSize(rect.width, rect.height);
74      await thermalHighWin.loadContent('pages/thermalHighDialog');
75      await thermalHighWin.setBackgroundColor(BG_COLOR);
76      await thermalHighWin.show();
77      console.log('Thermal_high window create success');
78    } catch {
79      console.log('Thermal_high window create failed');
80    }
81  }
82}
83