• 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 Prompt from '@ohos.promptAction';
17import missionManager from '@ohos.app.ability.missionManager';
18import { CloseAppManager, LauncherDragItemInfo, windowManager } from '@ohos/common';
19import { Log } from '@ohos/common';
20import { CheckEmptyUtils } from '@ohos/common';
21import { EventConstants } from '@ohos/common';
22import { StyleConstants } from '@ohos/common';
23import { CommonConstants } from '@ohos/common';
24import { AppModel } from '@ohos/common';
25import { AppItemInfo } from '@ohos/common';
26import { DockItemInfo } from '@ohos/common';
27import { MissionInfo } from '@ohos/common';
28import { RecentBundleMissionInfo } from '@ohos/common';
29import { ResourceManager } from '@ohos/common';
30import { amsMissionManager } from '@ohos/common';
31import { localEventManager } from '@ohos/common';
32import { layoutConfigManager } from '@ohos/common';
33import { launcherAbilityManager } from '@ohos/common';
34import SmartDockCloseAppHandler from '../common/SmartDockCloseAppHandler';
35import { SmartDockStyleConfig } from '../config/SmartDockStyleConfig';
36import { SmartDockLayoutConfig } from '../config/SmartDockLayoutConfig';
37import SmartDockConstants from '../common/constants/SmartDockConstants';
38import { RecentMissionInfo } from '@ohos/common';
39import launcherBundleManager from '@ohos.bundle.launcherBundleManager';
40
41const TAG = 'SmartDockModel';
42
43/**
44 * SmartDock Model
45 */
46export default class SmartDockModel {
47  private readonly mSmartDockLayoutConfig: SmartDockLayoutConfig;
48  private readonly mSmartDockCloseAppHandler: SmartDockCloseAppHandler;
49  private readonly mSmartDockStyleConfig: SmartDockStyleConfig;
50  private mResidentList: DockItemInfo[] = new Array<DockItemInfo>();
51  private mRecentDataList: RecentBundleMissionInfo[] = new Array<RecentBundleMissionInfo>();
52  private readonly mCloseAppManager: CloseAppManager;
53  private readonly mDevice = CommonConstants.DEFAULT_DEVICE_TYPE;
54  private readonly mResourceManager: ResourceManager;
55  protected mAppModel: AppModel;
56  private KEY_NAME: string = 'name';
57
58  private constructor() {
59    this.mSmartDockLayoutConfig = layoutConfigManager.getFunctionConfig(SmartDockLayoutConfig.SMART_DOCK_LAYOUT_INFO);
60    this.mSmartDockStyleConfig = layoutConfigManager.getStyleConfig(SmartDockStyleConfig.APP_LIST_STYLE_CONFIG, SmartDockConstants.FEATURE_NAME);
61    this.mCloseAppManager = CloseAppManager.getInstance();
62    this.mSmartDockCloseAppHandler = new SmartDockCloseAppHandler();
63    this.mCloseAppManager.registerCloseAppHandler(this.mSmartDockCloseAppHandler);
64    this.mAppModel = AppModel.getInstance();
65    this.mResourceManager = ResourceManager.getInstance();
66    this.registerDockListener();
67    this.mDevice = AppStorage.get('deviceType');
68    Log.showDebug(TAG, `dockDevice: ${this.mDevice}`);
69    this.getResidentList().then(() => {}, () => {});
70    if (this.mDevice === CommonConstants.PAD_DEVICE_TYPE) {
71      this.getRecentDataList().then(() => {}, () => {});
72    }
73    this.registerMissionListener();
74    Log.showInfo(TAG, 'constructor!');
75  }
76
77  static getInstance(): SmartDockModel {
78    if (globalThis.SmartDockModel == null) {
79      globalThis.SmartDockModel = new SmartDockModel();
80      Log.showInfo(TAG, 'getInstance!');
81    }
82    return globalThis.SmartDockModel;
83  }
84
85  /**
86   * get resident dock list
87   */
88  async getResidentList(): Promise<void> {
89    let residentList = new Array<DockItemInfo>();
90
91    // query rdb data
92    let rdbResidentList: DockItemInfo[] = [];
93    rdbResidentList = await globalThis.RdbStoreManagerInstance.querySmartDock();
94    let dockDataList = rdbResidentList;
95    if (CheckEmptyUtils.isEmptyArr(rdbResidentList) && !this.mSmartDockLayoutConfig.isConfigExist()) {
96      // init preset dock data
97      dockDataList = this.mSmartDockLayoutConfig.getDockLayoutInfo();
98      Log.showDebug(TAG, `getResidentList from config length: ${dockDataList.length}`);
99    } else {
100      if (CheckEmptyUtils.isEmptyArr(rdbResidentList)) {
101        dockDataList = this.mSmartDockLayoutConfig.getDockLayoutInfo();
102      }
103      residentList = rdbResidentList;
104      Log.showDebug(TAG, 'getResidentList from rdb!');
105    }
106    for (let i = 0; i < dockDataList.length; i++) {
107      if (dockDataList[i].itemType == CommonConstants.TYPE_APP) {
108        Log.showDebug(TAG, `getResidentList dockDataList[i].bundleName: ${dockDataList[i].bundleName}`);
109        const appData = await launcherAbilityManager.getAppInfoByBundleName(dockDataList[i].bundleName);
110        if (appData == undefined) {
111          continue;
112        }
113        const dockItemInfo = new DockItemInfo();
114        dockItemInfo.itemType = dockDataList[i].itemType;
115        dockItemInfo.editable = dockDataList[i].editable;
116        dockItemInfo.appName = typeof (appData) === 'undefined' ? dockDataList[i].appName : appData.appName;
117        dockItemInfo.bundleName = typeof (appData) === 'undefined' ? dockDataList[i].bundleName : appData.bundleName;
118        dockItemInfo.moduleName = typeof (appData) === 'undefined' ? dockDataList[i].bundleName : appData.moduleName;
119        dockItemInfo.abilityName = typeof (appData) === 'undefined' ? dockItemInfo.abilityName : appData.abilityName;
120        dockItemInfo.keyName = `${dockItemInfo.bundleName}${dockItemInfo.abilityName}${dockItemInfo.moduleName}`;
121        dockItemInfo.appIconId = typeof (appData) === 'undefined' ? dockItemInfo.appIconId : appData.appIconId;
122        dockItemInfo.appLabelId = typeof (appData) === 'undefined' ? dockItemInfo.appLabelId : appData.appLabelId;
123        dockItemInfo.isSystemApp = typeof (appData) === 'undefined' ? dockItemInfo.isSystemApp : appData.isSystemApp;
124        dockItemInfo.isUninstallAble = typeof (appData) === 'undefined' ? dockItemInfo.isUninstallAble : appData.isUninstallAble;
125        dockItemInfo.installTime = typeof (appData) === 'undefined' ? dockItemInfo.installTime : appData.installTime;
126        dockItemInfo.badgeNumber = typeof (appData) === 'undefined' ? dockItemInfo.badgeNumber : appData.badgeNumber;
127        residentList.push(dockItemInfo);
128      } else if (dockDataList[i].itemType == CommonConstants.TYPE_CARD) {
129      } else {
130        const dockItemInfo = new DockItemInfo();
131        dockItemInfo.itemType = dockDataList[i].itemType;
132        dockItemInfo.editable = dockDataList[i].editable;
133        dockItemInfo.bundleName = dockDataList[i].bundleName;
134        dockItemInfo.abilityName = dockDataList[i].abilityName;
135        dockItemInfo.moduleName = dockDataList[i].moduleName;
136        dockItemInfo.keyName = `${dockItemInfo.bundleName}${dockItemInfo.abilityName}${dockItemInfo.moduleName}`;
137        dockItemInfo.appIconId = dockDataList[i].appIconId;
138        dockItemInfo.appLabelId = dockDataList[i].appLabelId;
139        dockItemInfo.isSystemApp = typeof (dockDataList[i].isSystemApp) === 'undefined' ? true : dockDataList[i].isSystemApp;
140        dockItemInfo.isUninstallAble = typeof (dockDataList[i].isUninstallAble) === 'undefined' ? true : dockDataList[i].isUninstallAble;
141        dockItemInfo.badgeNumber = typeof (dockDataList[i].badgeNumber) === 'undefined' ?
142        CommonConstants.BADGE_DISPLAY_HIDE : dockDataList[i].badgeNumber;
143        const loadAppName = await this.mResourceManager
144          .getAppNameSync(dockItemInfo.appLabelId, dockItemInfo.bundleName, dockItemInfo.moduleName, '');
145        dockItemInfo.appName = loadAppName;
146        residentList.push(dockItemInfo);
147      }
148    }
149    // update persistent data
150    globalThis.RdbStoreManagerInstance.insertIntoSmartdock(residentList);
151    this.mSmartDockLayoutConfig.updateDockLayoutInfo(residentList);
152
153    // trigger component update
154    AppStorage.setOrCreate('residentList', residentList);
155    if (this.mDevice) {
156      localEventManager.sendLocalEventSticky(EventConstants.EVENT_SMARTDOCK_INIT_FINISHED, residentList);
157    }
158    Log.showDebug(TAG, `getResidentList end residentList.length: ${residentList.length}`);
159  }
160
161  /**
162   * get recent dock list
163   */
164  async getRecentDataList(): Promise<void> {
165    Log.showDebug(TAG, 'getRecentDataList start!');
166    if (this.mDevice === CommonConstants.DEFAULT_DEVICE_TYPE) {
167      return;
168    }
169    const recentList = await amsMissionManager.getRecentBundleMissionsList();
170    if (CheckEmptyUtils.isEmptyArr(recentList)) {
171      Log.showDebug(TAG, 'getRecentDataList empty');
172      AppStorage.setOrCreate('recentList', recentList);
173      return;
174    }
175    let recents: RecentBundleMissionInfo[] = [];
176    let missionInfos: {
177      appName: string,
178      bundleName: string,
179      missionInfoList: MissionInfo[]
180    }[] = [];
181    recentList.forEach(item => {
182      missionInfos.push({
183        appName: item.appName,
184        bundleName: item.bundleName,
185        missionInfoList: item.missionInfoList
186      });
187      item.missionInfoList = [];
188      recents.push(item);
189    });
190    if (recents.length > this.mSmartDockStyleConfig.mMaxRecentNum) {
191      recents = recents.slice(0, this.mSmartDockStyleConfig.mMaxRecentNum);
192    }
193    AppStorage.setOrCreate('recentList', recents);
194
195    missionInfos = missionInfos.slice(0,20);
196
197    AppStorage.setOrCreate('missionInfoList', missionInfos);
198    Log.showDebug(TAG, `getRecentDataList end, recentList.length: ${recents.length}`);
199  }
200
201  /**
202   * delete app from smartdock
203   * @param dockItem
204   * @param dockType
205   */
206  deleteDockItem(dockItem: DockItemInfo, dockType: number): boolean {
207    if (SmartDockConstants.RESIDENT_DOCK_TYPE === dockType) {
208      return this.deleteResistDockItem(dockItem);
209    }
210    else if (SmartDockConstants.RECENT_DOCK_TYPE === dockType) {
211      return this.deleteRecentDockItem(dockItem);
212    }
213  }
214
215  /**
216   * add appItem to smartdock
217   *
218   * @param appInfo
219   * @param index
220   */
221  addToSmartdock(appInfo: LauncherDragItemInfo, index?: number): boolean {
222    if (appInfo.typeId != CommonConstants.TYPE_APP) {
223      return false;
224    }
225    this.mResidentList = AppStorage.get('residentList');
226    const dockItemCount = this.mResidentList.length;
227    if (this.checkDockNum(dockItemCount)) {
228      return false;
229    }
230    const flag = this.idDuplicate(this.mResidentList, appInfo as AppItemInfo);
231    if (flag) {
232      const dockItemInfo = new DockItemInfo();
233      dockItemInfo.itemType = CommonConstants.TYPE_APP;
234      dockItemInfo.editable = true;
235      dockItemInfo.appId = appInfo.appId;
236      dockItemInfo.appName = appInfo.appName;
237      dockItemInfo.bundleName = appInfo.bundleName;
238      dockItemInfo.abilityName = appInfo.abilityName;
239      dockItemInfo.moduleName = appInfo.moduleName;
240      dockItemInfo.keyName = appInfo.keyName;
241      dockItemInfo.appIconId = appInfo.appIconId;
242      dockItemInfo.appLabelId = appInfo.appLabelId;
243      dockItemInfo.isSystemApp = appInfo.isSystemApp;
244      dockItemInfo.isUninstallAble = appInfo.isUninstallAble;
245      dockItemInfo.badgeNumber = appInfo.badgeNumber;
246      if (dockItemCount == 0 || index == undefined || index >= dockItemCount || index < 0) {
247        this.mResidentList.push(dockItemInfo);
248      } else {
249        this.mResidentList.splice(index, 0, dockItemInfo);
250      }
251      AppStorage.setOrCreate('residentList', this.mResidentList);
252      globalThis.RdbStoreManagerInstance.insertIntoSmartdock(this.mResidentList);
253      Log.showDebug(TAG, `addToSmartdock appInfo: ${appInfo.keyName}`);
254      return true;
255    }
256    return false;
257  }
258
259  /**
260   * check docklist over max num or not
261   * @param dockItemCount
262   * @return true: over max
263   * @return false: editable
264   */
265  private checkDockNum(dockItemCount: number): boolean {
266    if (dockItemCount >= this.mSmartDockStyleConfig.mMaxDockNum) {
267      Prompt.showToast({
268        message: $r('app.string.no_space_for_add')
269      });
270      return true;
271    }
272    return false;
273  }
274
275  /**
276   * check app exist in list
277   * @param list
278   * @param appInfo
279   * @return true: not exit, editable
280   * @return false: exited
281   */
282  private idDuplicate(list: AppItemInfo[], appInfo: AppItemInfo): boolean {
283    for (let i = 0; i < list.length; i++) {
284      if (list[i].keyName === appInfo.keyName) {
285        Prompt.showToast({
286          message: $r('app.string.duplicate_add')
287        });
288        return false;
289      }
290    }
291    return true;
292  }
293
294  /**
295   * send requset to add appItem to pageDesktop
296   * @param appInfo
297   */
298  addToPageDesk(appInfo: DockItemInfo): void {
299    if (appInfo.itemType == CommonConstants.TYPE_APP) {
300      localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_PAGEDESK_ITEM_ADD, appInfo).then(()=>{}, ()=>{});
301    } else {
302      Prompt.showToast({
303        message: $r('app.string.disable_add_to_dock')
304      });
305    }
306  }
307
308  /**
309   * move appItem from itemIndex to insertIndex
310   * @param insertIndex
311   * @param itemIndex
312   */
313  insertItemToIndex(insertIndex: number, itemIndex: number): void {
314    Log.showInfo(TAG, `insertItemToIndex insertIndex: ${insertIndex}, itemIndex: ${itemIndex}`);
315    if ((insertIndex == 0 || insertIndex == 1 || itemIndex == 0 || itemIndex == 1) && this.mDevice === CommonConstants.PAD_DEVICE_TYPE) {
316      Prompt.showToast({
317        message: $r('app.string.disable_to_move')
318      });
319      return;
320    }
321    this.mResidentList = AppStorage.get('residentList');
322    if (itemIndex < insertIndex) {
323      const selectItem = this.mResidentList[itemIndex];
324      this.mResidentList.splice(insertIndex, 0, selectItem);
325      this.mResidentList.splice(itemIndex, 1);
326    }
327    if (itemIndex > insertIndex) {
328      const selectItem = this.mResidentList[itemIndex];
329      this.mResidentList.splice(itemIndex, 1);
330      this.mResidentList.splice(insertIndex, 0, selectItem);
331    }
332    AppStorage.setOrCreate('residentList', this.mResidentList);
333    globalThis.RdbStoreManagerInstance.insertIntoSmartdock(this.mResidentList);
334  }
335
336  /**
337   * register residentList dock ADD ITEM Listener
338   * local listener for model to model send and receive msg
339   */
340  registerDockListener(): void {
341    localEventManager.registerEventListener(this.mAddToDockListener, [
342      EventConstants.EVENT_REQUEST_DOCK_ITEM_ADD,
343      EventConstants.EVENT_REQUEST_RESIDENT_DOCK_ITEM_DELETE,
344      EventConstants.EVENT_REQUEST_RECENT_DOCK_ITEM_DELETE,
345      EventConstants.EVENT_REQUEST_RESIDENT_DOCK_ITEM_UPDATE,
346      EventConstants.EVENT_BADGE_UPDATE
347    ]);
348    Log.showDebug(TAG, 'local listener on create');
349  }
350
351  /**
352   * unregister residentList dock ADD ITEM Listener
353   */
354  unregisterDockListener(): void {
355    localEventManager.unregisterEventListener(this.mAddToDockListener);
356    Log.showDebug(TAG, 'local listener on destroy');
357  }
358
359  /**
360   * resident local Listener
361   */
362  private readonly mAddToDockListener = {
363    onReceiveEvent: (event: string, params: any) => {
364      Log.showDebug(TAG, `receive event: ${event}, params: ${JSON.stringify(params)}`);
365      if (event === EventConstants.EVENT_REQUEST_DOCK_ITEM_ADD) {
366        this.addToSmartdock(params);
367      } else if (event === EventConstants.EVENT_REQUEST_RESIDENT_DOCK_ITEM_DELETE) {
368        this.deleteDockItem(params, SmartDockConstants.RESIDENT_DOCK_TYPE);
369      } else if (event === EventConstants.EVENT_REQUEST_RECENT_DOCK_ITEM_DELETE) {
370        this.deleteDockItem(params, SmartDockConstants.RECENT_DOCK_TYPE);
371      } else if (event === EventConstants.EVENT_REQUEST_RESIDENT_DOCK_ITEM_UPDATE) {
372        this.updateResistDockItem(params);
373      } else if (event === EventConstants.EVENT_BADGE_UPDATE) {
374        this.updateBadgeNum(params);
375      }
376    }
377  };
378
379  private updateBadgeNum(badgeInfo) {
380    Log.showInfo(TAG, `updateBadgeNum badgeInfo is ${JSON.stringify(badgeInfo)}`);
381    let residentListTemp: DockItemInfo[] = AppStorage.get('residentList');
382    if (!CheckEmptyUtils.isEmptyArr(residentListTemp)) {
383      for (var i = 0; i < residentListTemp.length; i++) {
384        if (badgeInfo.bundleName === residentListTemp[i].bundleName) {
385          let dockItemInfo = new DockItemInfo();
386          dockItemInfo.itemType = CommonConstants.TYPE_APP;
387          dockItemInfo.editable = true;
388          dockItemInfo.appId = residentListTemp[i].appId;
389          dockItemInfo.appName = residentListTemp[i].appName;
390          dockItemInfo.bundleName = residentListTemp[i].bundleName;
391          dockItemInfo.abilityName = residentListTemp[i].abilityName;
392          dockItemInfo.moduleName = residentListTemp[i].moduleName;
393          dockItemInfo.keyName = residentListTemp[i].keyName;
394          dockItemInfo.appIconId = residentListTemp[i].appIconId;
395          dockItemInfo.appLabelId = residentListTemp[i].appLabelId;
396          dockItemInfo.installTime = residentListTemp[i].installTime;
397          dockItemInfo.isSystemApp = residentListTemp[i].isSystemApp;
398          dockItemInfo.isUninstallAble = residentListTemp[i].isUninstallAble;
399          dockItemInfo.badgeNumber = badgeInfo.badgeNumber;
400          residentListTemp[i] = dockItemInfo;
401          Log.showDebug(TAG, `updateBadgeNum dockItemInfo is ${JSON.stringify(dockItemInfo)}`);
402          AppStorage.setOrCreate('residentList', residentListTemp);
403        }
404      }
405    }
406
407    if (this.mDevice === CommonConstants.PAD_DEVICE_TYPE) {
408      this.mRecentDataList = AppStorage.get('recentList');
409      Log.showDebug(TAG, 'updateBadgeNum recent ');
410      if (!CheckEmptyUtils.isEmptyArr(this.mRecentDataList)) {
411        for (var i = 0; i < this.mRecentDataList.length; i++) {
412          let curRecentData: RecentBundleMissionInfo = this.mRecentDataList[i];
413          Log.showDebug(TAG, `updateBadgeNum curRecentData is ${JSON.stringify(curRecentData)}`);
414          if (curRecentData.bundleName === badgeInfo.bundleName) {
415            let recentBundleMission: RecentBundleMissionInfo = new RecentBundleMissionInfo();
416            recentBundleMission.appId = curRecentData.appId;
417            recentBundleMission.appName = curRecentData.appName;
418            recentBundleMission.bundleName = curRecentData.bundleName;
419            recentBundleMission.abilityName = curRecentData.abilityName;
420            recentBundleMission.moduleName = curRecentData.moduleName;
421            recentBundleMission.keyName = curRecentData.keyName;
422            recentBundleMission.appIconId = curRecentData.appIconId;
423            recentBundleMission.appLabelId = curRecentData.appLabelId;
424            recentBundleMission.installTime = curRecentData.installTime;
425            recentBundleMission.isSystemApp = curRecentData.isSystemApp;
426            recentBundleMission.isUninstallAble = curRecentData.isUninstallAble;
427            recentBundleMission.badgeNumber = badgeInfo.badgeNumber;
428            this.mRecentDataList[i] = recentBundleMission;
429            Log.showDebug(TAG, `updateBadgeNum dockItemInfo is ${JSON.stringify(recentBundleMission)}`);
430            AppStorage.setOrCreate('recentList', this.mRecentDataList);
431          }
432        }
433      }
434    }
435  }
436
437  private registerMissionListener(): void {
438    Log.showDebug(TAG, 'registerMissionListener');
439    const listener: missionManager.MissionListener = {
440      onMissionCreated: this.onMissionCreatedCallback.bind(this),
441      onMissionDestroyed: this.onMissionDestroyedCallback.bind(this),
442      onMissionSnapshotChanged: this.onMissionSnapshotChangedCallback.bind(this),
443      onMissionMovedToFront: this.onMissionMovedToFrontCallback.bind(this),
444      onMissionIconUpdated: this.onMissionIconUpdatedCallback.bind(this),
445      onMissionClosed: this.onMissionClosedCallback.bind(this),
446      onMissionLabelUpdated: this.onMissionLabelUpdatedCallback.bind(this)
447    };
448    missionManager.on('mission', listener);
449  }
450
451  /**
452   * get recent view list
453   */
454  async getRecentViewDataList(missionId: number): Promise<void> {
455    let mRecentMissionsList = await amsMissionManager.getRecentMissionsList();
456    Log.showDebug(TAG, `getRecentMissionsList length: ${mRecentMissionsList.length}`);
457    const snapShotTime = new Date().toString();
458
459    let recentMissionInfoIndex = mRecentMissionsList.findIndex(item => {
460      return item.missionId === missionId;
461    })
462    if (recentMissionInfoIndex != -1) {
463      let recentMissionInfo: RecentMissionInfo = {
464        missionId: mRecentMissionsList[recentMissionInfoIndex].missionId,
465        appIconId: mRecentMissionsList[recentMissionInfoIndex].appIconId,
466        appLabelId: mRecentMissionsList[recentMissionInfoIndex].appLabelId,
467        appName: mRecentMissionsList[recentMissionInfoIndex].appName,
468        bundleName: mRecentMissionsList[recentMissionInfoIndex].bundleName,
469        moduleName: mRecentMissionsList[recentMissionInfoIndex].moduleName,
470        abilityName: mRecentMissionsList[recentMissionInfoIndex].abilityName,
471        lockedState: mRecentMissionsList[recentMissionInfoIndex].lockedState,
472        snapShotTime: snapShotTime
473      }
474      mRecentMissionsList[recentMissionInfoIndex] = recentMissionInfo;
475    }
476    if (globalThis.recentMode && windowManager.isSplitWindowMode(globalThis.recentMode)) {
477      mRecentMissionsList.forEach((item, index) => {
478        if (item.missionId == globalThis.splitMissionId) {
479          mRecentMissionsList.splice(index, 1);
480          return;
481        }
482      });
483    }
484    AppStorage.setOrCreate('recentMissionsList', mRecentMissionsList);
485  }
486
487  onMissionCreatedCallback(missionId: number): void {
488    Log.showInfo(TAG, 'onMissionCreatedCallback, missionId=' + missionId);
489  }
490
491  onMissionDestroyedCallback(missionId: number): void {
492    Log.showInfo(TAG, 'onMissionDestroyedCallback, missionId=' + missionId);
493    this.getRecentDataList().then(() => {}, ( )=> {});
494    this.getRecentViewDataList(missionId).then(() => {}, () => {});
495  }
496
497  onMissionSnapshotChangedCallback(missionId: number): void {
498    Log.showInfo(TAG, 'onMissionSnapshotChangedCallback, missionId=' + missionId);
499    this.getRecentDataList().then(() => {}, () => {});
500    this.getRecentViewDataList(missionId).then(() => {}, () => {});
501  }
502
503  onMissionMovedToFrontCallback(missionId: number): void {
504    Log.showInfo(TAG, 'onMissionMovedToFrontCallback, missionId=' + missionId);
505    this.getRecentDataList().then(() => {}, () => { });
506    this.getRecentViewDataList(missionId).then(() => {}, () => {});
507  }
508
509  onMissionIconUpdatedCallback(missionId: number): void {
510    Log.showInfo(TAG, 'onMissionIconUpdatedCallback, missionId=' + missionId);
511    this.getRecentDataList().then(() => {}, () => {});
512    this.getRecentViewDataList(missionId).then(() => {}, () => {});
513  }
514
515  onMissionClosedCallback(missionId: number): void {
516    Log.showInfo(TAG, 'onMissionClosedCallback, missionId=' + missionId);
517    this.getRecentDataList().then(() => {}, () => {});
518    this.getRecentViewDataList(missionId).then(() => {}, () => {});
519  }
520
521  onMissionLabelUpdatedCallback(missionId: number): void {
522    Log.showInfo(TAG, 'onMissionLabelUpdatedCallback, missionId=' + missionId);
523    this.getRecentDataList().then(() => {}, () => {});
524    this.getRecentViewDataList(missionId).then(() => {}, () => {});
525  }
526
527  /**
528   * get ShortcutInfo by bundleName
529   * @param bundleName
530   */
531  getShortcutInfo(bundleName: string): launcherBundleManager.ShortcutInfo[] | undefined {
532    return this.mAppModel.getShortcutInfo(bundleName);
533  }
534
535  /**
536   * @param cacheKey
537   *
538   * @return cache
539   */
540  getAppName(cacheKey: string): string {
541    return this.mResourceManager.getAppResourceCache(cacheKey, this.KEY_NAME);
542  }
543
544  async getSnapshot(missionIds: MissionInfo[], name: string): Promise<any> {
545    const snapshotList: {
546      name: string,
547      image: any,
548      missionId: number,
549      boxSize: number,
550      bundleName: string,
551      left?: number,
552      right?: number,
553    }[] = [];
554    if(this.mDevice === CommonConstants.PAD_DEVICE_TYPE){
555      let snapShotWidth = 0;
556      for (const item of missionIds) {
557        let pixelMap: {
558          name: string,
559          image: any,
560          missionId: number,
561          boxSize: number,
562          bundleName: string,
563          left?: number,
564          right?: number,
565        } = {
566          name: '',
567          left: StyleConstants.DEFAULT_12,
568          missionId: -1,
569          image: $r('app.media.icon'),
570          boxSize: StyleConstants.DEFAULT_SMART_DOCK_MISSION_IMAGE_HEIGHT,
571          bundleName: ''
572        };
573        const snapshotMap = await missionManager.getMissionSnapShot('', item.missionId);
574        pixelMap.image = snapshotMap.snapshot;
575        pixelMap.missionId = item.missionId;
576        pixelMap.bundleName = snapshotMap.ability.bundleName;
577        const imageInfo = await snapshotMap.snapshot.getImageInfo();
578        pixelMap.boxSize = Math.ceil(StyleConstants.DEFAULT_SMART_DOCK_MISSION_IMAGE_HEIGHT / imageInfo.size.height * imageInfo.size.width);
579        pixelMap.name = name;
580        pixelMap.left = StyleConstants.DEFAULT_12;
581        snapshotList.push(pixelMap);
582        snapShotWidth += pixelMap.boxSize + pixelMap.left;
583      }
584      AppStorage.setOrCreate('snapshotList', snapshotList);
585      AppStorage.setOrCreate('snapShotWidth', snapShotWidth);
586    }
587    Log.showDebug(TAG, 'getSnapshot update snapshotList');
588    return snapshotList;
589  }
590
591  private deleteResistDockItem(dockItem: {bundleName: string | undefined, keyName: string | undefined}): boolean {
592    this.mResidentList = AppStorage.get('residentList');
593    Log.showError(TAG, `deleteResistDockItem residentList length ${this.mResidentList.length}`);
594    if (!CheckEmptyUtils.isEmptyArr(this.mResidentList)) {
595      const findResidentList = this.mResidentList.find(item => {
596        return dockItem.bundleName == item.bundleName || dockItem.keyName == item.keyName;
597      })
598      // check right to delete
599      if (!findResidentList.editable) {
600        Prompt.showToast({
601          message: $r('app.string.disable_add_to_delete')
602        });
603        return false;
604      }
605      this.mResidentList = this.mResidentList.filter(item => {
606        if (dockItem.bundleName) {
607          return dockItem.bundleName != item.bundleName;
608        } else if (dockItem.keyName) {
609          return dockItem.keyName != item.keyName;
610        }
611      })
612      AppStorage.setOrCreate('residentList', this.mResidentList);
613      globalThis.RdbStoreManagerInstance.insertIntoSmartdock(this.mResidentList);
614      Log.showDebug(TAG, `deleteResistDockItem resist dockItem: ${JSON.stringify(dockItem)}`);
615    }
616    return true;
617  }
618
619  private deleteRecentDockItem(dockItem: {bundleName: string | undefined, keyName: string | undefined}): boolean {
620    let res = false;
621    this.mRecentDataList = AppStorage.get('recentList');
622    Log.showDebug(TAG, `deleteRecentDockItem recent dockItem: ${JSON.stringify(dockItem)}`);
623    if (!CheckEmptyUtils.isEmptyArr(this.mRecentDataList)) {
624      this.mRecentDataList = this.mRecentDataList.filter(item => {
625        if (dockItem.bundleName) {
626          return dockItem.bundleName != item.bundleName;
627        } else if (dockItem.keyName) {
628          return dockItem.keyName != item.keyName;
629        }
630      })
631      AppStorage.setOrCreate('recentList', this.mRecentDataList);
632      res = true;
633    }
634    return res;
635  }
636
637  updateResistDockItem(appInfo: AppItemInfo): void{
638    Log.showDebug(TAG, `updateResistDockItem appInfo: ${JSON.stringify(appInfo)}`);
639    let resistDockItem: DockItemInfo[] = AppStorage.get('residentList');
640    if (!CheckEmptyUtils.isEmptyArr(resistDockItem)) {
641      for (let i = 0; i < resistDockItem.length; i++) {
642        if (appInfo.bundleName === resistDockItem[i].bundleName) {
643          let dockItemInfo = new DockItemInfo();
644          dockItemInfo.itemType = CommonConstants.TYPE_APP;
645          dockItemInfo.editable = true;
646          dockItemInfo.appId = appInfo.appId;
647          dockItemInfo.appName = appInfo.appName;
648          dockItemInfo.bundleName = appInfo.bundleName;
649          dockItemInfo.abilityName = appInfo.abilityName;
650          dockItemInfo.moduleName = appInfo.moduleName;
651          dockItemInfo.keyName = appInfo.keyName;
652          dockItemInfo.appIconId = appInfo.appIconId;
653          dockItemInfo.appLabelId = appInfo.appLabelId;
654          dockItemInfo.installTime = appInfo.installTime;
655          dockItemInfo.isSystemApp = appInfo.isSystemApp;
656          dockItemInfo.isUninstallAble = appInfo.isUninstallAble;
657          dockItemInfo.badgeNumber = appInfo.badgeNumber;
658          resistDockItem[i] = dockItemInfo;
659          AppStorage.setOrCreate('residentList', resistDockItem);
660        }
661      }
662    }
663  }
664}