1/** 2 * Copyright (c) 2021-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 ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility'; 17import display from '@ohos.display'; 18import Want from '@ohos.application.Want'; 19import { Log } from '@ohos/common'; 20import { windowManager } from '@ohos/common'; 21import { RdbStoreManager } from '@ohos/common'; 22import { FormConstants } from '@ohos/common'; 23import { GestureNavigationManager } from '@ohos/gesturenavigation'; 24import StyleConstants from '../common/constants/StyleConstants'; 25import { navigationBarCommonEventManager } from '@ohos/common'; 26import PageDesktopViewModel from '../../../../../../feature/pagedesktop/src/main/ets/default/viewmodel/PageDesktopViewModel'; 27import { localEventManager } from '@ohos/common'; 28import { EventConstants } from '@ohos/common'; 29import Window from '@ohos.window'; 30 31const TAG = 'LauncherMainAbility'; 32 33export default class MainAbility extends ServiceExtension { 34 onCreate(want: Want): void { 35 Log.showInfo(TAG,'onCreate start'); 36 this.context.area = 0; 37 this.initLauncher(); 38 } 39 40 async initLauncher(): Promise<void> { 41 // init Launcher context 42 globalThis.desktopContext = this.context; 43 44 // init global const 45 this.initGlobalConst(); 46 47 // init Gesture navigation 48 this.startGestureNavigation(); 49 50 // init rdb 51 let dbStore = RdbStoreManager.getInstance(); 52 await dbStore.initRdbConfig(); 53 await dbStore.createTable(); 54 55 let registerWinEvent = (win) => { 56 win.on('lifeCycleEvent', (stageEventType) => { 57 // 桌面获焦或失焦时,通知桌面的卡片变为可见状态 58 if (stageEventType === Window.WindowStageEventType.INACTIVE 59 || stageEventType === Window.WindowStageEventType.ACTIVE) { 60 localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_FORM_ITEM_VISIBLE, null); 61 Log.showInfo(TAG, `lifeCycleEvent change: ${stageEventType}`); 62 } 63 }) 64 }; 65 66 windowManager.registerWindowEvent(); 67 navigationBarCommonEventManager.registerNavigationBarEvent(); 68 69 // create Launcher entry view 70 windowManager.createWindow(globalThis.desktopContext, windowManager.DESKTOP_WINDOW_NAME, 71 windowManager.DESKTOP_RANK, 'pages/' + windowManager.DESKTOP_WINDOW_NAME, true, registerWinEvent); 72 73 // load recent 74 windowManager.createRecentWindow(); 75 } 76 77 private initGlobalConst(): void { 78 // init create window global function 79 globalThis.createWindowWithName = ((windowName: string, windowRank: number): void => { 80 Log.showInfo(TAG, `createWindowWithName begin windowName: ${windowName}`); 81 if (windowName === windowManager.RECENT_WINDOW_NAME) { 82 windowManager.createRecentWindow(); 83 } else { 84 windowManager.createWindowIfAbsent(globalThis.desktopContext, windowName, windowRank, 'pages/' + windowName); 85 } 86 }); 87 } 88 89 private startGestureNavigation(): void { 90 const gestureNavigationManage = GestureNavigationManager.getInstance(); 91 display.getDefaultDisplay() 92 .then((dis: { id: number, width: number, height: number, refreshRate: number }): void => { 93 gestureNavigationManage.initWindowSize(dis); 94 }); 95 } 96 97 onDestroy(): void { 98 windowManager.unregisterWindowEvent(); 99 navigationBarCommonEventManager.unregisterNavigationBarEvent(); 100 windowManager.destroyWindow(windowManager.DESKTOP_WINDOW_NAME); 101 windowManager.destroyRecentWindow(); 102 Log.showInfo(TAG, 'onDestroy success'); 103 } 104 105 onRequest(want: Want, startId: number): void { 106 Log.showInfo(TAG,`onRequest, want:${want.abilityName}`); 107 // if app publish card to launcher 108 if(want.action === FormConstants.ACTION_PUBLISH_FORM) { 109 PageDesktopViewModel.getInstance().publishCardToDesktop(want.parameters); 110 } 111 if (startId !== 1) { 112 windowManager.minimizeAllApps(); 113 } 114 windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME); 115 this.closeFolder(); 116 } 117 118 private closeFolder(): void { 119 AppStorage.SetOrCreate('openFolderPageIndex', StyleConstants.DEFAULT_NUMBER_0); 120 AppStorage.SetOrCreate('openFolderStatus', StyleConstants.DEFAULT_NUMBER_0); 121 } 122} 123