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