1/* 2 * Copyright (c) 2023 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 UIAbility from '@ohos.app.ability.UIAbility'; 17import Want from '@ohos.app.ability.Want'; 18import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 19import window from '@ohos.window'; 20import { BusinessError } from '@ohos.base'; 21import display from '@ohos.display'; 22import GlobalContext from '../common/GlobalContext'; 23import Constants from '../common/constant'; 24import { HiLog } from '../common/HiLog'; 25 26const TAG = 'Alert'; 27export default class AlertAbility extends UIAbility { 28 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 29 HiLog.info(TAG, `onCreate`); 30 GlobalContext.store('alertWant', want); 31 } 32 33 onDestroy(): void { 34 HiLog.info(TAG, `onDestroy`); 35 } 36 37 onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { 38 HiLog.info(TAG, `onNewWant start`); 39 this.getNewWantPage(); 40 } 41 42 getNewWantPage(): void { 43 HiLog.info(TAG, `getNewWantPage start`); 44 let windowStage: window.WindowStage = GlobalContext.load('windowStage') as window.WindowStage; 45 let dis = display.getDefaultDisplaySync(); 46 try { 47 windowStage.loadContent('pages/alert'); 48 windowStage.disableWindowDecor(); 49 } catch (exception) { 50 HiLog.error(TAG, `Failed to set the background color. Cause: ${JSON.stringify(exception)}`); 51 } 52 try { 53 let mainWindow: window.Window | undefined = undefined; 54 55 windowStage.getMainWindow((err, data) => { 56 if (err && err.code) { 57 HiLog.error(TAG, `Failed to obtain the main window. Cause: ${JSON.stringify(err)}`); 58 return; 59 } 60 mainWindow = data; 61 try { 62 mainWindow.setWindowBackgroundColor(Constants.TRANSPARENT_BACKGROUND_COLOR); 63 } catch (exception) { 64 HiLog.error(TAG, `Failed to set the background color. Cause: ${JSON.stringify(exception)}`); 65 } 66 try { 67 let windowLimits = mainWindow.getWindowLimits(); 68 mainWindow.resize(windowLimits.minWidth, windowLimits.minHeight); 69 let xNumber = 70 windowLimits.minWidth ? windowLimits.minWidth : Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_WIDTH; 71 let yNumber = windowLimits.minHeight ? windowLimits.minHeight : 72 Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_HEIGHT; 73 mainWindow.moveWindowTo( 74 Math.floor((dis.width - xNumber) * Constants.RATIO_HALF), 75 Math.floor((dis.height - yNumber) * Constants.RATIO_HALF) 76 ); 77 } catch (exception) { 78 HiLog.error(TAG, `Failed to obtain the window limits of window. Cause: ${JSON.stringify(exception)}`); 79 } 80 mainWindow.setResizeByDragEnabled(false, (err: BusinessError) => { 81 if (err && err.code) { 82 HiLog.error(TAG, `Failed to set the function of disabling the resize by dragg window. Cause: ${err.code}`); 83 return; 84 } 85 HiLog.info(TAG, `Succeeded in setting the function of disabling the resize by dragg window.`); 86 }); 87 }) 88 } catch (exception) { 89 HiLog.error(TAG, `Failed to obtain the main window. Cause: ${JSON.stringify(exception)}`); 90 }; 91 } 92 93 onWindowStageCreate(windowStage: window.WindowStage): void { 94 // Main window is created, set main page for this ability 95 HiLog.info(TAG, `onWindowStageCreate`); 96 GlobalContext.store('windowStage', windowStage); 97 this.getNewWantPage() 98 } 99 100 onWindowStageDestroy(): void { 101 // Main window is destroyed, release UI related resources 102 HiLog.info(TAG, `onWindowStageDestroy`); 103 } 104 105 onForeground(): void { 106 // Ability has brought to foreground 107 HiLog.info(TAG, `onForeground`); 108 } 109 110 onBackground(): void { 111 // Ability has back to background 112 HiLog.info(TAG, `onBackground`); 113 } 114}; 115