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