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