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 Ability from '@ohos.app.ability.UIAbility' 17import Window from '@ohos.window' 18import WorkFactory, { WorkerType } from "../workers/WorkFactory"; 19import { HiLog } from '../../../../../common/src/main/ets/util/HiLog'; 20import Want from '@ohos.app.ability.Want'; 21import SimManager from '../feature/sim/SimManager'; 22import { missedCallManager } from '../feature/missedCall/MissedCallManager'; 23import PresenterManager from '../presenter/PresenterManager'; 24 25const TAG = 'MainAbility '; 26 27export default class MainAbility extends Ability { 28 storage: LocalStorage; 29 simManager: SimManager; 30 mDataWorker = WorkFactory.getWorker(WorkerType.DataWorker); 31 32 updateBreakpoint(windowWidth: number) { 33 let windowWidthVp: number = px2vp(windowWidth); 34 let breakpoint: string; 35 if (windowWidthVp < 520) { 36 breakpoint = 'sm'; 37 } else if (windowWidthVp < 840) { 38 breakpoint = 'md'; 39 } else { 40 breakpoint = 'lg'; 41 } 42 this.storage.setOrCreate('breakpoint', breakpoint); 43 } 44 45 onRequest(want: Want, isOnCreate: boolean) { 46 if (!want || !want.parameters) { 47 return; 48 } 49 const data: any = want.parameters["missedCallData"]; 50 const action = want.parameters["action"]; 51 HiLog.i(TAG, `onRequest action: ${action}`); 52 if (action != undefined && data != undefined) { 53 missedCallManager.requestMissedCallAction(action, data); 54 } 55 } 56 57 onCreate(want, launchParam) { 58 HiLog.i(TAG, 'Application onCreate start'); 59 globalThis.isFromOnCreate = true; 60 globalThis.context = this.context; 61 globalThis.abilityWant = want; 62 this.storage = new LocalStorage() 63 this.onRequest(want, true); 64 this.simManager = new SimManager(); 65 globalThis.DataWorker = this.mDataWorker; 66 globalThis.presenterManager = new PresenterManager(this.context, this.mDataWorker); 67 globalThis.presenterManager.onCreate(); 68 } 69 70 onNewWant(want, launchParam) { 71 HiLog.i(TAG, 'Application onNewWant'); 72 globalThis.isFromOnCreate = false; 73 globalThis.abilityWant = want; 74 this.onRequest(want, false); 75 } 76 77 onDestroy() { 78 HiLog.i(TAG, 'Ability onDestroy'); 79 globalThis.presenterManager.onDestroy(); 80 this.mDataWorker.close(); 81 } 82 83 onWindowStageCreate(windowStage: Window.WindowStage) { 84 // Main window is created, set main page for this ability 85 HiLog.i(TAG, 'Ability onWindowStageCreate'); 86 87 Window.getTopWindow(this.context).then((windowObj) => { 88 windowObj.getProperties().then((windowProperties) => { 89 this.updateBreakpoint(windowProperties.windowRect.width); 90 }) 91 windowObj.on('windowSizeChange', (data) => { 92 this.updateBreakpoint(data.width); 93 }); 94 }) 95 96 windowStage.loadContent('pages/index', this.storage, (err, data) => { 97 if (err.code) { 98 HiLog.e(TAG, 'Failed to load the content. Cause: ' + JSON.stringify(err) ?? ''); 99 return; 100 } 101 HiLog.i(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data) ?? ''); 102 }); 103 } 104 105 onWindowStageDestroy() { 106 // Main window is destroyed, release UI related resources 107 HiLog.i(TAG, 'Ability onWindowStageDestroy'); 108 } 109 110 onForeground() { 111 // Ability has brought to foreground 112 HiLog.i(TAG, 'Ability onForeground'); 113 this.simManager.init(); 114 } 115 116 onBackground() { 117 // Ability has back to background 118 HiLog.i(TAG, 'Ability onBackground'); 119 this.simManager.recycle(); 120 } 121}