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 */ 15 16import Ability from '@ohos.app.ability.UIAbility'; 17import { hilog } from '@kit.PerformanceAnalysisKit'; 18import { window } from '@kit.ArkUI'; 19import { BusinessError } from '@ohos.base'; 20 21const TAG: string = '[createWindow]' 22 23export default class MainAbility extends Ability { 24 onCreate(want, launchParam) { 25 // Ability is creating, initialize resources for this ability 26 console.log(`${TAG} MainAbility onCreate`); 27 globalThis.abilityWant = want; 28 globalThis.abilityLaunchParam = launchParam; 29 } 30 31 onDestroy() { 32 // Ability is destroying, release resources for this ability 33 console.log(`${TAG} MainAbility onDestroy`); 34 } 35 36 onWindowStageCreate(windowStage) { 37 // Main window is created, set main page for this ability 38 console.log(`${TAG} MainAbility onWindowStageCreate windowStage=` + windowStage); 39 globalThis.windowStage = windowStage; 40 globalThis.context = this.context; 41 windowStage.setUIContent(this.context, 'MainAbility/pages/XTSIndex', null); 42 let windowClass: window.Window | undefined = undefined; 43 windowStage.getMainWindow((err: BusinessError, data) => { 44 let errCode: number = err.code; 45 if (errCode) { 46 console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 47 return; 48 } 49 windowClass = data; 50 hilog.info(0x0000, 'testTag', 'Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 51 try { 52 let properties : window.WindowProperties = windowClass.getWindowProperties(); 53 let wRect : window.Rect = properties.windowRect; 54 globalThis.winLeft = wRect.left; 55 globalThis.winTop = wRect.top; 56 hilog.info(0x0000, 'testTag', 'Succeeded get winLeft:' + globalThis.winLeft + ',winTop:' + globalThis.winTop ); 57 } catch (exception) { 58 hilog.error(0x0000, 'testTag', 'Failed to obtain the window properties. Cause: ' + JSON.stringify(exception)); 59 } 60 }) 61 } 62 63 onWindowStageDestroy() { 64 //Main window is destroyed, release UI related resources 65 console.log(`${TAG} MainAbility onWindowStageDestroy`); 66 } 67 68 onForeground() { 69 // Ability has brought to foreground 70 console.log(`${TAG} MainAbility onForeground`); 71 } 72 73 onBackground() { 74 // Ability has back to background 75 console.log(`${TAG} MainAbility onBackground`); 76 } 77};