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 // create Launcher entry view 69 windowManager.createWindow(globalThis.desktopContext, windowManager.DESKTOP_WINDOW_NAME, 70 windowManager.DESKTOP_RANK, 'pages/' + windowManager.DESKTOP_WINDOW_NAME, true, registerWinEvent); 71 72 // load recent 73 windowManager.createRecentWindow(); 74 } 75 76 private initGlobalConst(): void { 77 // init create window global function 78 globalThis.createWindowWithName = ((windowName: string, windowRank: number): void => { 79 Log.showInfo(TAG, `createWindowWithName begin windowName: ${windowName}`); 80 if (windowName === windowManager.RECENT_WINDOW_NAME) { 81 windowManager.createRecentWindow(); 82 } else { 83 windowManager.createWindowIfAbsent(globalThis.desktopContext, windowName, windowRank, 'pages/' + windowName); 84 } 85 }); 86 } 87 88 private startGestureNavigation(): void { 89 const gestureNavigationManage = GestureNavigationManager.getInstance(); 90 display.getDefaultDisplay() 91 .then((dis: { id: number, width: number, height: number, refreshRate: number }): void => { 92 gestureNavigationManage.initWindowSize(dis); 93 }); 94 } 95 96 onDestroy(): void { 97 windowManager.unregisterWindowEvent(); 98 navigationBarCommonEventManager.unregisterNavigationBarEvent(); 99 windowManager.destroyWindow(windowManager.DESKTOP_WINDOW_NAME); 100 windowManager.destroyRecentWindow(); 101 windowManager.destroyWindow(windowManager.APP_CENTER_WINDOW_NAME); 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 windowManager.destroyWindow(windowManager.APP_CENTER_WINDOW_NAME); 116 this.closeFolder(); 117 this.closeRecentDockPopup(); 118 } 119 120 private closeFolder(): void { 121 AppStorage.SetOrCreate('openFolderPageIndex', StyleConstants.DEFAULT_NUMBER_0); 122 AppStorage.SetOrCreate('openFolderStatus', StyleConstants.DEFAULT_NUMBER_0); 123 } 124 125 private closeRecentDockPopup(): void { 126 let num: number = AppStorage.Get('sysUiRecentOnClickEvent'); 127 AppStorage.SetOrCreate('sysUiRecentOnClickEvent', ++num); 128 } 129}