• 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.application.Want';
19import {
20  Log,
21  FormListInfoCacheManager,
22  ResourceManager,
23  launcherAbilityManager,
24  windowManager,
25  RdbStoreManager,
26  FormConstants,
27  navigationBarCommonEventManager,
28  localEventManager,
29  EventConstants
30} from '@ohos/common';
31import { GestureNavigationManager } from '@ohos/gesturenavigation';
32import StyleConstants from '../common/constants/StyleConstants';
33import PageDesktopViewModel from '../../../../../../feature/pagedesktop/src/main/ets/default/viewmodel/PageDesktopViewModel';
34import Window from '@ohos.window';
35import type { Configuration } from '@ohos.app.ability.Configuration';
36
37const TAG = 'LauncherMainAbility';
38
39export default class MainAbility extends ServiceExtension {
40  onCreate(want: Want): void {
41    Log.showInfo(TAG,'onCreate start');
42    this.context.area = 0;
43    this.initLauncher();
44  }
45
46  async initLauncher(): Promise<void> {
47    // init Launcher context
48    globalThis.desktopContext = this.context;
49
50    // init global const
51    this.initGlobalConst();
52
53    // init Gesture navigation
54    this.startGestureNavigation();
55
56    // init rdb
57    let dbStore = RdbStoreManager.getInstance();
58    await dbStore.initRdbConfig();
59    await dbStore.createTable();
60
61    let registerWinEvent = (win) => {
62      win.on('lifeCycleEvent', (stageEventType) => {
63        // 桌面获焦或失焦时,通知桌面的卡片变为可见状态
64        if (stageEventType === Window.WindowStageEventType.INACTIVE
65        || stageEventType === Window.WindowStageEventType.ACTIVE) {
66          localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_FORM_ITEM_VISIBLE, null);
67          Log.showInfo(TAG, `lifeCycleEvent change: ${stageEventType}`);
68        }
69      })
70    };
71
72    windowManager.registerWindowEvent();
73    navigationBarCommonEventManager.registerNavigationBarEvent();
74
75    // create Launcher entry view
76    windowManager.createWindow(globalThis.desktopContext, windowManager.DESKTOP_WINDOW_NAME,
77      windowManager.DESKTOP_RANK, 'pages/' + windowManager.DESKTOP_WINDOW_NAME, true, registerWinEvent);
78
79    // load recent
80    windowManager.createRecentWindow();
81  }
82
83  private initGlobalConst(): void {
84    // init create window global function
85    globalThis.createWindowWithName = ((windowName: string, windowRank: number): void => {
86      Log.showInfo(TAG, `createWindowWithName begin windowName: ${windowName}`);
87      if (windowName === windowManager.RECENT_WINDOW_NAME) {
88        windowManager.createRecentWindow();
89      } else {
90        windowManager.createWindowIfAbsent(globalThis.desktopContext, windowName, windowRank, 'pages/' + windowName);
91      }
92    });
93  }
94
95  private startGestureNavigation(): void {
96    const gestureNavigationManage = GestureNavigationManager.getInstance();
97    display.getDefaultDisplay()
98      .then((dis: { id: number, width: number, height: number, refreshRate: number }): void => {
99        gestureNavigationManage.initWindowSize(dis);
100      });
101  }
102
103  onDestroy(): void {
104    windowManager.unregisterWindowEvent();
105    navigationBarCommonEventManager.unregisterNavigationBarEvent();
106    windowManager.destroyWindow(windowManager.DESKTOP_WINDOW_NAME);
107    windowManager.destroyRecentWindow();
108    Log.showInfo(TAG, 'onDestroy success');
109  }
110
111  onRequest(want: Want, startId: number): void {
112    Log.showInfo(TAG,`onRequest, want:${want.abilityName}`);
113    // if app publish card to launcher
114    if(want.action === FormConstants.ACTION_PUBLISH_FORM) {
115      PageDesktopViewModel.getInstance().publishCardToDesktop(want.parameters);
116    }
117    if (startId !== 1) {
118      windowManager.minimizeAllApps();
119    }
120    windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME);
121    this.closeFolder();
122  }
123
124  private closeFolder(): void {
125    AppStorage.SetOrCreate('openFolderPageIndex', StyleConstants.DEFAULT_NUMBER_0);
126    AppStorage.SetOrCreate('openFolderStatus', StyleConstants.DEFAULT_NUMBER_0);
127  }
128
129  onConfigurationUpdate(config: Configuration): void {
130    Log.showInfo(TAG, 'onConfigurationUpdated, config:' + JSON.stringify(config));
131    const systemLanguage = AppStorage.Get('systemLanguage');
132    if (systemLanguage !== config.language) {
133      this.clearCacheWhenLanguageChange();
134    }
135    AppStorage.SetOrCreate('systemLanguage', config.language);
136  }
137
138  private clearCacheWhenLanguageChange(): void {
139    FormListInfoCacheManager.getInstance().clearCache();
140    ResourceManager.getInstance().clearAppResourceCache();
141    launcherAbilityManager.cleanAppMapCache();
142    PageDesktopViewModel.getInstance().updateDesktopInfo();
143  }
144}
145