1df226684Sopenharmony_ci/* 2df226684Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3df226684Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4df226684Sopenharmony_ci * you may not use this file except in compliance with the License. 5df226684Sopenharmony_ci * You may obtain a copy of the License at 6df226684Sopenharmony_ci * 7df226684Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8df226684Sopenharmony_ci * 9df226684Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10df226684Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11df226684Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12df226684Sopenharmony_ci * See the License for the specific language governing permissions and 13df226684Sopenharmony_ci * limitations under the License. 14df226684Sopenharmony_ci */ 15df226684Sopenharmony_ci 16df226684Sopenharmony_ciimport UIAbility from '@ohos.app.ability.UIAbility'; 17df226684Sopenharmony_ciimport Want from '@ohos.app.ability.Want'; 18df226684Sopenharmony_ciimport AbilityConstant from '@ohos.app.ability.AbilityConstant'; 19df226684Sopenharmony_ciimport window from '@ohos.window'; 20df226684Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 21df226684Sopenharmony_ciimport display from '@ohos.display'; 22df226684Sopenharmony_ciimport GlobalContext from '../common/GlobalContext'; 23df226684Sopenharmony_ciimport Constants from '../common/constant'; 24df226684Sopenharmony_ciimport { HiLog } from '../common/HiLog'; 25df226684Sopenharmony_ci 26df226684Sopenharmony_ciconst TAG = 'Alert'; 27df226684Sopenharmony_ciexport default class AlertAbility extends UIAbility { 28df226684Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 29df226684Sopenharmony_ci HiLog.info(TAG, `onCreate`); 30df226684Sopenharmony_ci GlobalContext.store('alertWant', want); 31df226684Sopenharmony_ci } 32df226684Sopenharmony_ci 33df226684Sopenharmony_ci onDestroy(): void { 34df226684Sopenharmony_ci HiLog.info(TAG, `onDestroy`); 35df226684Sopenharmony_ci } 36df226684Sopenharmony_ci 37df226684Sopenharmony_ci onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { 38df226684Sopenharmony_ci HiLog.info(TAG, `onNewWant start`); 39df226684Sopenharmony_ci this.getNewWantPage(); 40df226684Sopenharmony_ci } 41df226684Sopenharmony_ci 42df226684Sopenharmony_ci getNewWantPage(): void { 43df226684Sopenharmony_ci HiLog.info(TAG, `getNewWantPage start`); 44df226684Sopenharmony_ci let windowStage: window.WindowStage = GlobalContext.load('windowStage') as window.WindowStage; 45df226684Sopenharmony_ci let dis = display.getDefaultDisplaySync(); 46df226684Sopenharmony_ci try { 47df226684Sopenharmony_ci windowStage.loadContent('pages/alert'); 48df226684Sopenharmony_ci windowStage.disableWindowDecor(); 49df226684Sopenharmony_ci } catch (exception) { 50df226684Sopenharmony_ci HiLog.error(TAG, `Failed to set the background color. Cause: ${JSON.stringify(exception)}`); 51df226684Sopenharmony_ci } 52df226684Sopenharmony_ci try { 53df226684Sopenharmony_ci let mainWindow: window.Window | undefined = undefined; 54df226684Sopenharmony_ci 55df226684Sopenharmony_ci windowStage.getMainWindow((err, data) => { 56df226684Sopenharmony_ci if (err && err.code) { 57df226684Sopenharmony_ci HiLog.error(TAG, `Failed to obtain the main window. Cause: ${JSON.stringify(err)}`); 58df226684Sopenharmony_ci return; 59df226684Sopenharmony_ci } 60df226684Sopenharmony_ci mainWindow = data; 61df226684Sopenharmony_ci try { 62df226684Sopenharmony_ci mainWindow.setWindowBackgroundColor(Constants.TRANSPARENT_BACKGROUND_COLOR); 63df226684Sopenharmony_ci } catch (exception) { 64df226684Sopenharmony_ci HiLog.error(TAG, `Failed to set the background color. Cause: ${JSON.stringify(exception)}`); 65df226684Sopenharmony_ci } 66df226684Sopenharmony_ci try { 67df226684Sopenharmony_ci let windowLimits = mainWindow.getWindowLimits(); 68df226684Sopenharmony_ci mainWindow.resize(windowLimits.minWidth, windowLimits.minHeight); 69df226684Sopenharmony_ci let xNumber = 70df226684Sopenharmony_ci windowLimits.minWidth ? windowLimits.minWidth : Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_WIDTH; 71df226684Sopenharmony_ci let yNumber = windowLimits.minHeight ? windowLimits.minHeight : 72df226684Sopenharmony_ci Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_HEIGHT; 73df226684Sopenharmony_ci mainWindow.moveWindowTo( 74df226684Sopenharmony_ci Math.floor((dis.width - xNumber) * Constants.RATIO_HALF), 75df226684Sopenharmony_ci Math.floor((dis.height - yNumber) * Constants.RATIO_HALF) 76df226684Sopenharmony_ci ); 77df226684Sopenharmony_ci } catch (exception) { 78df226684Sopenharmony_ci HiLog.error(TAG, `Failed to obtain the window limits of window. Cause: ${JSON.stringify(exception)}`); 79df226684Sopenharmony_ci } 80df226684Sopenharmony_ci mainWindow.setResizeByDragEnabled(false, (err: BusinessError) => { 81df226684Sopenharmony_ci if (err && err.code) { 82df226684Sopenharmony_ci HiLog.error(TAG, `Failed to set the function of disabling the resize by dragg window. Cause: ${err.code}`); 83df226684Sopenharmony_ci return; 84df226684Sopenharmony_ci } 85df226684Sopenharmony_ci HiLog.info(TAG, `Succeeded in setting the function of disabling the resize by dragg window.`); 86df226684Sopenharmony_ci }); 87df226684Sopenharmony_ci }) 88df226684Sopenharmony_ci } catch (exception) { 89df226684Sopenharmony_ci HiLog.error(TAG, `Failed to obtain the main window. Cause: ${JSON.stringify(exception)}`); 90df226684Sopenharmony_ci }; 91df226684Sopenharmony_ci } 92df226684Sopenharmony_ci 93df226684Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage): void { 94df226684Sopenharmony_ci // Main window is created, set main page for this ability 95df226684Sopenharmony_ci HiLog.info(TAG, `onWindowStageCreate`); 96df226684Sopenharmony_ci GlobalContext.store('windowStage', windowStage); 97df226684Sopenharmony_ci this.getNewWantPage() 98df226684Sopenharmony_ci } 99df226684Sopenharmony_ci 100df226684Sopenharmony_ci onWindowStageDestroy(): void { 101df226684Sopenharmony_ci // Main window is destroyed, release UI related resources 102df226684Sopenharmony_ci HiLog.info(TAG, `onWindowStageDestroy`); 103df226684Sopenharmony_ci } 104df226684Sopenharmony_ci 105df226684Sopenharmony_ci onForeground(): void { 106df226684Sopenharmony_ci // Ability has brought to foreground 107df226684Sopenharmony_ci HiLog.info(TAG, `onForeground`); 108df226684Sopenharmony_ci } 109df226684Sopenharmony_ci 110df226684Sopenharmony_ci onBackground(): void { 111df226684Sopenharmony_ci // Ability has back to background 112df226684Sopenharmony_ci HiLog.info(TAG, `onBackground`); 113df226684Sopenharmony_ci } 114df226684Sopenharmony_ci}; 115