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