• 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.ACTIVE) {
67          localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_FORM_ITEM_VISIBLE, null);
68          Log.showInfo(TAG, `lifeCycleEvent change: ${stageEventType}`);
69        }
70      })
71    };
72
73    windowManager.registerWindowEvent();
74    navigationBarCommonEventManager.registerNavigationBarEvent();
75
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      globalThis.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    Log.showInfo(TAG, 'onDestroy success');
159  }
160
161  onRequest(want: Want, startId: number): void {
162    Log.showInfo(TAG,`onRequest, want:${want.abilityName}`);
163    // if app publish card to launcher
164    if(want.action === FormConstants.ACTION_PUBLISH_FORM) {
165      PageDesktopViewModel.getInstance().publishCardToDesktop(want.parameters);
166    }
167    if (startId !== 1) {
168      windowManager.minimizeAllApps();
169    }
170    windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME);
171    localEventManager.sendLocalEventSticky(EventConstants.EVENT_OPEN_FOLDER_TO_CLOSE, null);
172  }
173
174  onConfigurationUpdate(config): void {
175    Log.showInfo(TAG, 'onConfigurationUpdated, config:' + JSON.stringify(config));
176    const systemLanguage = AppStorage.get('systemLanguage');
177    if(systemLanguage !== config.language) {
178      this.clearCacheWhenLanguageChange();
179    }
180    AppStorage.setOrCreate('systemLanguage', config.language);
181  }
182
183  private clearCacheWhenLanguageChange(): void {
184    FormListInfoCacheManager.getInstance().clearCache();
185    ResourceManager.getInstance().clearAppResourceCache();
186    launcherAbilityManager.cleanAppMapCache();
187    PageDesktopViewModel.getInstance().updateDesktopInfo();
188    PageDesktopViewModel.getInstance().updateForms();
189  }
190}
191