• 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';
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    let onKeyCodeHome = {
91      preKeys: [],
92      finalKey: KeyCode.KEYCODE_HOME,
93      finalKeyDownDuration: 0,
94      isFinalKeyDown: true
95    }
96    // register/unregister HOME inputConsumer
97    inputConsumer.on('key', onKeyCodeHome, () => {
98      Log.showInfo(TAG, 'HOME inputConsumer homeEvent start');
99      globalThis.desktopContext.startAbility({
100        bundleName: CommonConstants.LAUNCHER_BUNDLE,
101        abilityName: CommonConstants.LAUNCHER_ABILITY
102      })
103        .then(() => {
104          Log.showDebug(TAG, 'HOME inputConsumer startAbility Promise in service successful.');
105        })
106        .catch(() => {
107          Log.showDebug(TAG, 'HOME inputConsumer startAbility Promise in service failed.');
108        });
109    });
110    let onKeyCodeFunction = {
111      preKeys: [],
112      finalKey: KeyCode.KEYCODE_FUNCTION,
113      finalKeyDownDuration: 0,
114      isFinalKeyDown: true
115    }
116    // register/unregister RECENT inputConsumer
117    inputConsumer.on('key', onKeyCodeFunction, () => {
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    let offKeyCodeHome = {
125      preKeys: [],
126      finalKey: KeyCode.KEYCODE_HOME,
127      finalKeyDownDuration: 0,
128      isFinalKeyDown: true
129    }
130    // unregister HOME inputConsumer
131    inputConsumer.off('key', offKeyCodeHome);
132    let offKeyCodeFunction = {
133      preKeys: [],
134      finalKey: KeyCode.KEYCODE_FUNCTION,
135      finalKeyDownDuration: 0,
136      isFinalKeyDown: true
137    }
138    // unregister RECENT inputConsumer
139    inputConsumer.off('key', offKeyCodeFunction);
140  }
141
142  private initGlobalConst(): void {
143    // init create window global function
144    globalThis.createWindowWithName = ((windowName: string, windowRank: number): void => {
145      Log.showInfo(TAG, `createWindowWithName begin windowName: ${windowName}`);
146      if (windowName === windowManager.RECENT_WINDOW_NAME) {
147        windowManager.createRecentWindow();
148      } else {
149        windowManager.createWindowIfAbsent(globalThis.desktopContext, windowName, windowRank, 'pages/' + windowName);
150      }
151    });
152  }
153
154  private startGestureNavigation(): void {
155    const gestureNavigationManage = GestureNavigationManager.getInstance();
156    let dis: display.Display = display.getDefaultDisplaySync();
157    dis && gestureNavigationManage.initWindowSize(dis);
158  }
159
160  onDestroy(): void {
161    windowManager.unregisterWindowEvent();
162    this.unregisterInputConsumer();
163    navigationBarCommonEventManager.unregisterNavigationBarEvent();
164    windowManager.destroyWindow(windowManager.DESKTOP_WINDOW_NAME);
165    windowManager.destroyRecentWindow();
166    windowManager.destroyWindow(windowManager.APP_CENTER_WINDOW_NAME);
167    this.displayManager?.destroySubDisplayWindow();
168    Log.showInfo(TAG, 'onDestroy success');
169  }
170
171  onRequest(want: Want, startId: number): void {
172    Log.showInfo(TAG,`onRequest, want: ${want.abilityName}`);
173    // if app publish card to launcher
174    if(want.action === FormConstants.ACTION_PUBLISH_FORM) {
175      PageDesktopViewModel.getInstance().publishCardToDesktop(want.parameters);
176    }
177    if (startId !== 1) {
178      windowManager.minimizeAllApps();
179    }
180    windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME);
181    windowManager.destroyWindow(windowManager.APP_CENTER_WINDOW_NAME);
182    this.closeFolder();
183    this.closeRecentDockPopup();
184  }
185
186  private closeFolder(): void {
187    AppStorage.setOrCreate('openFolderPageIndex', StyleConstants.DEFAULT_NUMBER_0);
188    AppStorage.setOrCreate('openFolderStatus', StyleConstants.DEFAULT_NUMBER_0);
189  }
190
191  private closeRecentDockPopup(): void {
192    let num: number = AppStorage.get('sysUiRecentOnClickEvent');
193    AppStorage.setOrCreate('sysUiRecentOnClickEvent', ++num);
194  }
195
196  async onConfigurationUpdated(config) {
197    Log.showInfo(TAG, 'onConfigurationUpdated, config:' + JSON.stringify(config));
198    const systemLanguage = AppStorage.get('systemLanguage');
199    if(systemLanguage !== config.language) {
200      this.clearCacheWhenLanguageChange();
201    }
202    AppStorage.setOrCreate("systemLanguage", config.language);
203  }
204
205  private clearCacheWhenLanguageChange() {
206    FormListInfoCacheManager.getInstance().clearCache();
207    ResourceManager.getInstance().clearAppResourceCache();
208    launcherAbilityManager.cleanAppMapCache();
209    PageDesktopViewModel.getInstance().updateDesktopInfo();
210  }
211}