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