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.app.ability.Want'; 19import { 20 Log, 21 CommonConstants, 22 windowManager, 23 RdbStoreManager, 24 FormConstants, 25 FormListInfoCacheManager, 26 ResourceManager, 27 launcherAbilityManager, 28 navigationBarCommonEventManager, 29 localEventManager, 30 EventConstants, 31 DisplayManager 32} from '@ohos/common'; 33import { GestureNavigationManager } from '@ohos/gesturenavigation'; 34import StyleConstants from '../common/constants/StyleConstants'; 35import { PageDesktopViewModel } from '@ohos/pagedesktop'; 36import Window from '@ohos.window'; 37import inputConsumer from '@ohos.multimodalInput.inputConsumer'; 38import { KeyCode } from '@ohos.multimodalInput.keyCode'; 39 40const TAG = 'LauncherMainAbility'; 41 42export default class MainAbility extends ServiceExtension { 43 private displayManager: DisplayManager = undefined 44 45 onCreate(want: Want): void { 46 Log.showInfo(TAG,'onCreate start'); 47 this.context.area = 0; 48 this.initLauncher(); 49 } 50 51 async initLauncher(): Promise<void> { 52 // init Launcher context 53 globalThis.desktopContext = this.context; 54 55 // init global const 56 this.initGlobalConst(); 57 58 // init Gesture navigation 59 this.startGestureNavigation(); 60 61 // init rdb 62 let dbStore = RdbStoreManager.getInstance(); 63 await dbStore.initRdbConfig(); 64 await dbStore.createTable(); 65 66 let registerWinEvent = (win) => { 67 win.on('lifeCycleEvent', (stageEventType) => { 68 // 桌面获焦或失焦时,通知桌面的卡片变为可见状态 69 if (stageEventType === Window.WindowStageEventType.INACTIVE 70 || stageEventType === Window.WindowStageEventType.ACTIVE) { 71 localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_FORM_ITEM_VISIBLE, null); 72 Log.showInfo(TAG, `lifeCycleEvent change: ${stageEventType}`); 73 } 74 }) 75 }; 76 77 windowManager.registerWindowEvent(); 78 navigationBarCommonEventManager.registerNavigationBarEvent(); 79 // create Launcher entry view 80 windowManager.createWindow(globalThis.desktopContext, windowManager.DESKTOP_WINDOW_NAME, 81 windowManager.DESKTOP_RANK, 'pages/' + windowManager.DESKTOP_WINDOW_NAME, true, registerWinEvent); 82 83 // load recent 84 windowManager.createRecentWindow(); 85 this.registerInputConsumer(); 86 this.displayManager = DisplayManager.getInstance(); 87 } 88 89 private registerInputConsumer(): void { 90 // register/unregister HOME inputConsumer 91 inputConsumer.on('key', { 92 preKeys: [], 93 finalKey: KeyCode.KEYCODE_HOME, 94 finalKeyDownDuration: 0, 95 isFinalKeyDown: true 96 }, () => { 97 Log.showInfo(TAG, 'HOME inputConsumer homeEvent start'); 98 globalThis.desktopContext.startAbility({ 99 bundleName: CommonConstants.LAUNCHER_BUNDLE, 100 abilityName: CommonConstants.LAUNCHER_ABILITY 101 }) 102 .then(() => { 103 Log.showDebug(TAG, 'HOME inputConsumer startAbility Promise in service successful.'); 104 }) 105 .catch(() => { 106 Log.showDebug(TAG, 'HOME inputConsumer startAbility Promise in service failed.'); 107 }); 108 }); 109 // register/unregister RECENT inputConsumer 110 inputConsumer.on('key', { 111 preKeys: [], 112 finalKey: KeyCode.KEYCODE_FUNCTION, 113 finalKeyDownDuration: 0, 114 isFinalKeyDown: true 115 }, () => { 116 Log.showInfo(TAG, 'RECENT inputConsumer recentEvent start'); 117 windowManager.createWindowWithName(windowManager.RECENT_WINDOW_NAME, windowManager.RECENT_RANK); 118 }); 119 } 120 121 private unregisterInputConsumer(): void { 122 // unregister HOME inputConsumer 123 inputConsumer.off('key', { 124 preKeys: [], 125 finalKey: KeyCode.KEYCODE_HOME, 126 finalKeyDownDuration: 0, 127 isFinalKeyDown: true 128 }); 129 // unregister RECENT inputConsumer 130 inputConsumer.off('key', { 131 preKeys: [], 132 finalKey: KeyCode.KEYCODE_FUNCTION, 133 finalKeyDownDuration: 0, 134 isFinalKeyDown: true 135 }); 136 } 137 138 private initGlobalConst(): void { 139 // init create window global function 140 globalThis.createWindowWithName = ((windowName: string, windowRank: number): void => { 141 Log.showInfo(TAG, `createWindowWithName begin windowName: ${windowName}`); 142 if (windowName === windowManager.RECENT_WINDOW_NAME) { 143 windowManager.createRecentWindow(); 144 } else { 145 windowManager.createWindowIfAbsent(globalThis.desktopContext, windowName, windowRank, 'pages/' + windowName); 146 } 147 }); 148 } 149 150 private startGestureNavigation(): void { 151 const gestureNavigationManage = GestureNavigationManager.getInstance(); 152 let dis: display.Display = display.getDefaultDisplaySync(); 153 dis && gestureNavigationManage.initWindowSize(dis); 154 } 155 156 onDestroy(): void { 157 windowManager.unregisterWindowEvent(); 158 this.unregisterInputConsumer(); 159 navigationBarCommonEventManager.unregisterNavigationBarEvent(); 160 windowManager.destroyWindow(windowManager.DESKTOP_WINDOW_NAME); 161 windowManager.destroyRecentWindow(); 162 windowManager.destroyWindow(windowManager.APP_CENTER_WINDOW_NAME); 163 this.displayManager?.destroySubDisplayWindow(); 164 Log.showInfo(TAG, 'onDestroy success'); 165 } 166 167 onRequest(want: Want, startId: number): void { 168 Log.showInfo(TAG,`onRequest, want: ${want.abilityName}`); 169 // if app publish card to launcher 170 if(want.action === FormConstants.ACTION_PUBLISH_FORM) { 171 PageDesktopViewModel.getInstance().publishCardToDesktop(want.parameters); 172 } 173 if (startId !== 1) { 174 windowManager.minimizeAllApps(); 175 } 176 windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME); 177 windowManager.destroyWindow(windowManager.APP_CENTER_WINDOW_NAME); 178 this.closeFolder(); 179 this.closeRecentDockPopup(); 180 } 181 182 private closeFolder(): void { 183 AppStorage.setOrCreate('openFolderPageIndex', StyleConstants.DEFAULT_NUMBER_0); 184 AppStorage.setOrCreate('openFolderStatus', StyleConstants.DEFAULT_NUMBER_0); 185 } 186 187 private closeRecentDockPopup(): void { 188 let num: number = AppStorage.get('sysUiRecentOnClickEvent'); 189 AppStorage.setOrCreate('sysUiRecentOnClickEvent', ++num); 190 } 191 192 async onConfigurationUpdated(config) { 193 Log.showInfo(TAG, 'onConfigurationUpdated, config:' + JSON.stringify(config)); 194 const systemLanguage = AppStorage.get('systemLanguage'); 195 if(systemLanguage !== config.language) { 196 this.clearCacheWhenLanguageChange(); 197 } 198 AppStorage.setOrCreate("systemLanguage", config.language); 199 } 200 201 private clearCacheWhenLanguageChange() { 202 FormListInfoCacheManager.getInstance().clearCache(); 203 ResourceManager.getInstance().clearAppResourceCache(); 204 launcherAbilityManager.cleanAppMapCache(); 205 PageDesktopViewModel.getInstance().updateDesktopInfo(); 206 } 207}