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