1/*
2 * Copyright (c) 2024 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 */
15import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
16import { hilog } from '@kit.PerformanceAnalysisKit';
17import { window } from '@kit.ArkUI';
18import { BusinessError } from '@ohos.base';
19
20export default class EntryAbility extends UIAbility {
21  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam){
22    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
23    globalThis.abilityWant = want;
24  }
25
26  onDestroy(){
27    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
28  }
29  
30  onWindowStageCreate(windowStage: window.WindowStage):void{
31    // Main window is created, set main page for this ability
32    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
33
34    globalThis.windowStage = windowStage
35    globalThis.abilityContext = this.context
36    windowStage.loadContent('MainAbility/pages/index/index', (err: BusinessError, data) => {
37      if (err.code) {
38        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
39        return;
40      }
41      let windowClass: window.Window | undefined = undefined;
42      windowStage.getMainWindow((err: BusinessError, data) => {
43        let errCode: number = err.code;
44        if (errCode) {
45          console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
46          return;
47        }
48        windowClass = data;
49        hilog.info(0x0000, 'testTag', 'Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
50        try {
51          let properties : window.WindowProperties = windowClass.getWindowProperties();
52          let wRect : window.Rect = properties.windowRect;
53          globalThis.winLeft = wRect.left;
54          globalThis.winTop = wRect.top;
55          hilog.info(0x0000, 'testTag', 'Succeeded get winLeft:' + globalThis.winLeft + ',winTop:' + globalThis.winTop );
56        } catch (exception) {
57          hilog.error(0x0000, 'testTag', 'Failed to obtain the window properties. Cause: ' + JSON.stringify(exception));
58        }
59        // Obtain a UIContext instance.
60        globalThis.uiContent = windowClass.getUIContext();
61      })
62      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
63    });
64  }
65
66  onWindowStageDestroy():void{
67    // Main window is destroyed, release UI related resources
68    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
69  }
70
71  onForeground():void{
72    // Ability has brought to foreground
73    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
74  }
75
76  onBackground():void{
77    // Ability has back to background
78    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
79  }
80}