16e80583aSopenharmony_ci/**
26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46e80583aSopenharmony_ci * you may not use this file except in compliance with the License.
56e80583aSopenharmony_ci * You may obtain a copy of the License at
66e80583aSopenharmony_ci *
76e80583aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86e80583aSopenharmony_ci *
96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126e80583aSopenharmony_ci * See the License for the specific language governing permissions and
136e80583aSopenharmony_ci * limitations under the License.
146e80583aSopenharmony_ci */
156e80583aSopenharmony_ci
166e80583aSopenharmony_ciimport installer from '@ohos.bundle.installer';
176e80583aSopenharmony_ciimport bundleMonitor from '@ohos.bundle.bundleMonitor';
186e80583aSopenharmony_ciimport osAccount from '@ohos.account.osAccount';
196e80583aSopenharmony_ciimport hiSysEvent from '@ohos.hiSysEvent';
206e80583aSopenharmony_ciimport launcherBundleManager from '@ohos.bundle.launcherBundleManager';
216e80583aSopenharmony_ciimport { Log } from '../utils/Log';
226e80583aSopenharmony_ciimport { CheckEmptyUtils } from '../utils/CheckEmptyUtils';
236e80583aSopenharmony_ciimport { AppItemInfo } from '../bean/AppItemInfo';
246e80583aSopenharmony_ciimport { CommonConstants } from '../constants/CommonConstants';
256e80583aSopenharmony_ciimport { ResourceManager } from './ResourceManager';
266e80583aSopenharmony_ciimport { EventConstants } from '../constants/EventConstants';
276e80583aSopenharmony_ciimport { BadgeManager } from '../manager/BadgeManager';
286e80583aSopenharmony_ciimport { PreferencesHelper } from './PreferencesHelper';
296e80583aSopenharmony_ciimport { BusinessError } from '@ohos.base';
306e80583aSopenharmony_ciimport performanceMonitor from '@ohos.arkui.performanceMonitor';
316e80583aSopenharmony_ci
326e80583aSopenharmony_ciconst TAG = 'LauncherAbilityManager';
336e80583aSopenharmony_ci
346e80583aSopenharmony_ciinterface BundleStatusCallback {
356e80583aSopenharmony_ci  add: (bundleName: string, userId: number) => void;
366e80583aSopenharmony_ci  remove: (bundleName: string, userId: number) => void;
376e80583aSopenharmony_ci  update: (bundleName: string, userId: number) => void;
386e80583aSopenharmony_ci}
396e80583aSopenharmony_ci
406e80583aSopenharmony_ci/**
416e80583aSopenharmony_ci * Wrapper class for innerBundleManager and formManager interfaces.
426e80583aSopenharmony_ci */
436e80583aSopenharmony_ciclass LauncherAbilityManager {
446e80583aSopenharmony_ci  private static readonly CURRENT_USER_ID = -2;
456e80583aSopenharmony_ci  private readonly mAppMap = new Map<string, AppItemInfo>();
466e80583aSopenharmony_ci  private mUserId: number = 100;
476e80583aSopenharmony_ci  private mIsNeedRegisterBundleCallback = false;
486e80583aSopenharmony_ci  private readonly mBundleStatusCallback: BundleStatusCallback = {
496e80583aSopenharmony_ci    add: (bundleName, userId) => {
506e80583aSopenharmony_ci      Log.showDebug(TAG, `PACKAGE_ADDED bundleName: ${bundleName}, userId: ${userId}, mUserId ${this.mUserId}`);
516e80583aSopenharmony_ci      this.mUserId === userId && this.notifyLauncherAbilityChange(EventConstants.EVENT_PACKAGE_ADDED, bundleName, userId);
526e80583aSopenharmony_ci    },
536e80583aSopenharmony_ci    remove: (bundleName, userId) => {
546e80583aSopenharmony_ci      Log.showDebug(TAG, `PACKAGE_REMOVED bundleName: ${bundleName}, userId: ${userId}, mUserId ${this.mUserId}`);
556e80583aSopenharmony_ci      this.mUserId === userId && this.notifyLauncherAbilityChange(EventConstants.EVENT_PACKAGE_REMOVED, bundleName, userId);
566e80583aSopenharmony_ci    },
576e80583aSopenharmony_ci    update: (bundleName, userId) => {
586e80583aSopenharmony_ci      Log.showDebug(TAG, `PACKAGE_CHANGED bundleName: ${bundleName}, userId: ${userId}, mUserId ${this.mUserId}`);
596e80583aSopenharmony_ci      this.mUserId === userId && this.notifyLauncherAbilityChange(EventConstants.EVENT_PACKAGE_CHANGED, bundleName, userId);
606e80583aSopenharmony_ci    }
616e80583aSopenharmony_ci  };
626e80583aSopenharmony_ci
636e80583aSopenharmony_ci  private readonly mLauncherAbilityChangeListeners: any[] = [];
646e80583aSopenharmony_ci
656e80583aSopenharmony_ci  /**
666e80583aSopenharmony_ci   * Get desktop application information management object
676e80583aSopenharmony_ci   *
686e80583aSopenharmony_ci   * @return Desktop application information management object instance
696e80583aSopenharmony_ci   */
706e80583aSopenharmony_ci  static getInstance(): LauncherAbilityManager {
716e80583aSopenharmony_ci    if (globalThis.LauncherAbilityManagerInstance == null) {
726e80583aSopenharmony_ci      globalThis.LauncherAbilityManagerInstance = new LauncherAbilityManager();
736e80583aSopenharmony_ci    }
746e80583aSopenharmony_ci    return globalThis.LauncherAbilityManagerInstance;
756e80583aSopenharmony_ci  }
766e80583aSopenharmony_ci
776e80583aSopenharmony_ci  private constructor() {
786e80583aSopenharmony_ci    const osAccountManager = osAccount.getAccountManager();
796e80583aSopenharmony_ci    osAccountManager.getOsAccountLocalId((err, localId) => {
806e80583aSopenharmony_ci      Log.showDebug(TAG, `getOsAccountLocalId localId ${localId}`);
816e80583aSopenharmony_ci      this.mUserId = localId;
826e80583aSopenharmony_ci    });
836e80583aSopenharmony_ci  }
846e80583aSopenharmony_ci
856e80583aSopenharmony_ci  getUserId(): number {
866e80583aSopenharmony_ci    return this.mUserId;
876e80583aSopenharmony_ci  }
886e80583aSopenharmony_ci
896e80583aSopenharmony_ci  checkBundleMonitor(): void {
906e80583aSopenharmony_ci    if (!this.mIsNeedRegisterBundleCallback) {
916e80583aSopenharmony_ci      return;
926e80583aSopenharmony_ci    }
936e80583aSopenharmony_ci    Log.showInfo(TAG, 'checkBundleMonitor need to register bundle callback');
946e80583aSopenharmony_ci    this.bundleMonitorOn();
956e80583aSopenharmony_ci  }
966e80583aSopenharmony_ci
976e80583aSopenharmony_ci  bundleMonitorOn(): void  {
986e80583aSopenharmony_ci    this.mIsNeedRegisterBundleCallback = false;
996e80583aSopenharmony_ci    bundleMonitor.on('add', (bundleChangeInfo) => {
1006e80583aSopenharmony_ci      Log.showInfo(TAG, `add bundleName: ${bundleChangeInfo.bundleName} userId: ${bundleChangeInfo.userId}`);
1016e80583aSopenharmony_ci      this.mBundleStatusCallback.add(bundleChangeInfo.bundleName, bundleChangeInfo.userId);
1026e80583aSopenharmony_ci    });
1036e80583aSopenharmony_ci    bundleMonitor.on('update', (bundleChangeInfo) => {
1046e80583aSopenharmony_ci      Log.showInfo(TAG, `update bundleName: ${bundleChangeInfo.bundleName} userId: ${bundleChangeInfo.userId}`);
1056e80583aSopenharmony_ci      this.mBundleStatusCallback.update(bundleChangeInfo.bundleName, bundleChangeInfo.userId);
1066e80583aSopenharmony_ci    });
1076e80583aSopenharmony_ci    bundleMonitor.on('remove', (bundleChangeInfo) => {
1086e80583aSopenharmony_ci      Log.showInfo(TAG, `remove bundleName: ${bundleChangeInfo.bundleName} userId: ${bundleChangeInfo.userId}`);
1096e80583aSopenharmony_ci      this.mBundleStatusCallback.remove(bundleChangeInfo.bundleName, bundleChangeInfo.userId);
1106e80583aSopenharmony_ci    });
1116e80583aSopenharmony_ci    Log.showInfo(TAG, 'bundleMonitorOn register bundle callback finished');
1126e80583aSopenharmony_ci  }
1136e80583aSopenharmony_ci
1146e80583aSopenharmony_ci  /**
1156e80583aSopenharmony_ci   * Monitor system application status.
1166e80583aSopenharmony_ci   *
1176e80583aSopenharmony_ci   * @params listener: listening object
1186e80583aSopenharmony_ci   */
1196e80583aSopenharmony_ci  async registerLauncherAbilityChangeListener(listener: any):Promise<void> {
1206e80583aSopenharmony_ci    if (listener != null) {
1216e80583aSopenharmony_ci      if (this.mLauncherAbilityChangeListeners.length == 0) {
1226e80583aSopenharmony_ci        try {
1236e80583aSopenharmony_ci          let isBmsHasInit = await PreferencesHelper.getInstance().get('isBmsHasInit', false);
1246e80583aSopenharmony_ci          Log.showInfo(TAG, `registerCallback success, isBmsHasInit:${isBmsHasInit}`);
1256e80583aSopenharmony_ci          if (isBmsHasInit) {
1266e80583aSopenharmony_ci            this.bundleMonitorOn();
1276e80583aSopenharmony_ci          } else {
1286e80583aSopenharmony_ci            PreferencesHelper.getInstance().put('isBmsHasInit', true);
1296e80583aSopenharmony_ci            this.mIsNeedRegisterBundleCallback = true;
1306e80583aSopenharmony_ci          }
1316e80583aSopenharmony_ci        } catch (errData) {
1326e80583aSopenharmony_ci          let message = (errData as BusinessError).message;
1336e80583aSopenharmony_ci          let errCode = (errData as BusinessError).code;
1346e80583aSopenharmony_ci          Log.showError(TAG, `registerCallback fail errCode:${errCode}, message:${message}`);
1356e80583aSopenharmony_ci        }
1366e80583aSopenharmony_ci      }
1376e80583aSopenharmony_ci      const index = this.mLauncherAbilityChangeListeners.indexOf(listener);
1386e80583aSopenharmony_ci      if (index == CommonConstants.INVALID_VALUE) {
1396e80583aSopenharmony_ci        this.mLauncherAbilityChangeListeners.push(listener);
1406e80583aSopenharmony_ci      }
1416e80583aSopenharmony_ci    }
1426e80583aSopenharmony_ci  }
1436e80583aSopenharmony_ci
1446e80583aSopenharmony_ci  /**
1456e80583aSopenharmony_ci   * Cancel monitoring system application status.
1466e80583aSopenharmony_ci   *
1476e80583aSopenharmony_ci   * @params listener: listening object
1486e80583aSopenharmony_ci   */
1496e80583aSopenharmony_ci  unregisterLauncherAbilityChangeListener(listener: any): void {
1506e80583aSopenharmony_ci    if (listener != null) {
1516e80583aSopenharmony_ci      const index = this.mLauncherAbilityChangeListeners.indexOf(listener);
1526e80583aSopenharmony_ci      if (index != CommonConstants.INVALID_VALUE) {
1536e80583aSopenharmony_ci        this.mLauncherAbilityChangeListeners.splice(index, 1);
1546e80583aSopenharmony_ci      }
1556e80583aSopenharmony_ci      if (this.mLauncherAbilityChangeListeners.length == 0) {
1566e80583aSopenharmony_ci        try {
1576e80583aSopenharmony_ci          bundleMonitor.off('add');
1586e80583aSopenharmony_ci          bundleMonitor.off('update');
1596e80583aSopenharmony_ci          bundleMonitor.off('remove');
1606e80583aSopenharmony_ci          Log.showInfo(TAG, 'unregisterCallback success');
1616e80583aSopenharmony_ci        } catch (errData) {
1626e80583aSopenharmony_ci          let message = (errData as BusinessError).message;
1636e80583aSopenharmony_ci          let errCode = (errData as BusinessError).code;
1646e80583aSopenharmony_ci          Log.showError(TAG, `unregisterCallback fail errCode:${errCode}, message:${message}`);
1656e80583aSopenharmony_ci        }
1666e80583aSopenharmony_ci      }
1676e80583aSopenharmony_ci    }
1686e80583aSopenharmony_ci  }
1696e80583aSopenharmony_ci
1706e80583aSopenharmony_ci  private notifyLauncherAbilityChange(event, bundleName: string, userId): void {
1716e80583aSopenharmony_ci    for (let index = 0; index < this.mLauncherAbilityChangeListeners.length; index++) {
1726e80583aSopenharmony_ci      this.mLauncherAbilityChangeListeners[index](event, bundleName, userId);
1736e80583aSopenharmony_ci    }
1746e80583aSopenharmony_ci  }
1756e80583aSopenharmony_ci
1766e80583aSopenharmony_ci  /**
1776e80583aSopenharmony_ci   * get all app List info from BMS
1786e80583aSopenharmony_ci   */
1796e80583aSopenharmony_ci  async getLauncherAbilityList(): Promise<AppItemInfo[]> {
1806e80583aSopenharmony_ci    let abilityList = null;
1816e80583aSopenharmony_ci    await launcherBundleManager.getAllLauncherAbilityInfo(LauncherAbilityManager.CURRENT_USER_ID)
1826e80583aSopenharmony_ci      .then((res) => {
1836e80583aSopenharmony_ci        abilityList = res;
1846e80583aSopenharmony_ci      })
1856e80583aSopenharmony_ci      .catch((err) => {
1866e80583aSopenharmony_ci        Log.showError(TAG, `getLauncherAbilityList error: ${JSON.stringify(err)}`);
1876e80583aSopenharmony_ci      });
1886e80583aSopenharmony_ci    const appItemInfoList = new Array<AppItemInfo>();
1896e80583aSopenharmony_ci    if (CheckEmptyUtils.isEmpty(abilityList)) {
1906e80583aSopenharmony_ci      Log.showDebug(TAG, 'getLauncherAbilityList Empty');
1916e80583aSopenharmony_ci      return appItemInfoList;
1926e80583aSopenharmony_ci    }
1936e80583aSopenharmony_ci    for (let i = 0; i < abilityList.length; i++) {
1946e80583aSopenharmony_ci      let appItem = await this.convertToAppItemInfo(abilityList[i]);
1956e80583aSopenharmony_ci      appItemInfoList.push(appItem);
1966e80583aSopenharmony_ci    }
1976e80583aSopenharmony_ci    return appItemInfoList;
1986e80583aSopenharmony_ci  }
1996e80583aSopenharmony_ci
2006e80583aSopenharmony_ci  /**
2016e80583aSopenharmony_ci   * get AbilityInfos by bundleName from BMS
2026e80583aSopenharmony_ci   *
2036e80583aSopenharmony_ci   * @params bundleName Application package name
2046e80583aSopenharmony_ci   * @return List of entry capabilities information of the target application
2056e80583aSopenharmony_ci   */
2066e80583aSopenharmony_ci  async getLauncherAbilityInfo(bundleName: string): Promise<AppItemInfo[]> {
2076e80583aSopenharmony_ci    let abilityInfos: launcherBundleManager.LauncherAbilityInfo[];
2086e80583aSopenharmony_ci    await launcherBundleManager.getLauncherAbilityInfo(bundleName, this.mUserId)
2096e80583aSopenharmony_ci      .then((res) => {
2106e80583aSopenharmony_ci        abilityInfos = res;
2116e80583aSopenharmony_ci      })
2126e80583aSopenharmony_ci      .catch((err) => {
2136e80583aSopenharmony_ci        Log.showError(TAG, `getLauncherAbilityInfo error: ${JSON.stringify(err)}`);
2146e80583aSopenharmony_ci      });
2156e80583aSopenharmony_ci    const appItemInfoList = new Array<AppItemInfo>();
2166e80583aSopenharmony_ci    if (CheckEmptyUtils.isEmpty(abilityInfos) || abilityInfos.length === 0) {
2176e80583aSopenharmony_ci      Log.showDebug(TAG, 'getLauncherAbilityInfo Empty');
2186e80583aSopenharmony_ci      return appItemInfoList;
2196e80583aSopenharmony_ci    }
2206e80583aSopenharmony_ci    for (let i = 0; i < abilityInfos.length; i++) {
2216e80583aSopenharmony_ci      let appItem = await this.convertToAppItemInfo(abilityInfos[i]);
2226e80583aSopenharmony_ci      appItemInfoList.push(appItem);
2236e80583aSopenharmony_ci    }
2246e80583aSopenharmony_ci    return appItemInfoList;
2256e80583aSopenharmony_ci  }
2266e80583aSopenharmony_ci
2276e80583aSopenharmony_ci  /**
2286e80583aSopenharmony_ci   * get AppItemInfo from BMS with bundleName
2296e80583aSopenharmony_ci   * @params bundleName
2306e80583aSopenharmony_ci   * @return AppItemInfo
2316e80583aSopenharmony_ci   */
2326e80583aSopenharmony_ci  async getAppInfoByBundleName(bundleName: string, abilityName?: string): Promise<AppItemInfo | undefined> {
2336e80583aSopenharmony_ci    let appItemInfo: AppItemInfo | undefined = undefined;
2346e80583aSopenharmony_ci    // get from cache
2356e80583aSopenharmony_ci    if (this.mAppMap && this.mAppMap.has(bundleName)) {
2366e80583aSopenharmony_ci      appItemInfo = this.mAppMap.get(bundleName);
2376e80583aSopenharmony_ci    }
2386e80583aSopenharmony_ci    if (appItemInfo && abilityName && appItemInfo.abilityName === abilityName) {
2396e80583aSopenharmony_ci      return appItemInfo;
2406e80583aSopenharmony_ci    }
2416e80583aSopenharmony_ci    // get from system
2426e80583aSopenharmony_ci    let abilityInfos = new Array<launcherBundleManager.LauncherAbilityInfo>();
2436e80583aSopenharmony_ci    await launcherBundleManager.getLauncherAbilityInfo(bundleName, LauncherAbilityManager.CURRENT_USER_ID)
2446e80583aSopenharmony_ci      .then((res)=>{
2456e80583aSopenharmony_ci        if (res && res.length) {
2466e80583aSopenharmony_ci          abilityInfos = res;
2476e80583aSopenharmony_ci        }
2486e80583aSopenharmony_ci      })
2496e80583aSopenharmony_ci      .catch((err)=>{
2506e80583aSopenharmony_ci        Log.showError(TAG, `getAppInfoByBundleName getLauncherAbilityInfo error: ${JSON.stringify(err)}`);
2516e80583aSopenharmony_ci      });
2526e80583aSopenharmony_ci    if (!abilityInfos || abilityInfos.length === 0) {
2536e80583aSopenharmony_ci      Log.showDebug(TAG, `${bundleName} has no launcher ability`);
2546e80583aSopenharmony_ci      return undefined;
2556e80583aSopenharmony_ci    }
2566e80583aSopenharmony_ci    let appInfo = abilityInfos[0];
2576e80583aSopenharmony_ci    if (abilityName) {
2586e80583aSopenharmony_ci      appInfo = abilityInfos.find(item => {
2596e80583aSopenharmony_ci        return item.elementName.abilityName === abilityName;
2606e80583aSopenharmony_ci      });
2616e80583aSopenharmony_ci    }
2626e80583aSopenharmony_ci    if (!appInfo) {
2636e80583aSopenharmony_ci      appInfo = abilityInfos[0];
2646e80583aSopenharmony_ci    }
2656e80583aSopenharmony_ci    const data = await this.convertToAppItemInfo(appInfo);
2666e80583aSopenharmony_ci    return data;
2676e80583aSopenharmony_ci  }
2686e80583aSopenharmony_ci
2696e80583aSopenharmony_ci  private async convertToAppItemInfo(info): Promise<AppItemInfo> {
2706e80583aSopenharmony_ci    const appItemInfo = new AppItemInfo();
2716e80583aSopenharmony_ci    appItemInfo.appName = await ResourceManager.getInstance().getAppNameSync(
2726e80583aSopenharmony_ci      info.labelId, info.elementName.bundleName, info.elementName.moduleName, info.applicationInfo.label
2736e80583aSopenharmony_ci    );
2746e80583aSopenharmony_ci    appItemInfo.isSystemApp = info.applicationInfo.systemApp;
2756e80583aSopenharmony_ci    appItemInfo.isUninstallAble = info.applicationInfo.removable;
2766e80583aSopenharmony_ci    appItemInfo.appIconId = info.iconId;
2776e80583aSopenharmony_ci    appItemInfo.appLabelId = info.labelId;
2786e80583aSopenharmony_ci    appItemInfo.bundleName = info.elementName.bundleName;
2796e80583aSopenharmony_ci    appItemInfo.abilityName = info.elementName.abilityName;
2806e80583aSopenharmony_ci    appItemInfo.moduleName = info.elementName.moduleName;
2816e80583aSopenharmony_ci    appItemInfo.keyName = info.elementName.bundleName + info.elementName.abilityName + info.elementName.moduleName;
2826e80583aSopenharmony_ci    appItemInfo.typeId = CommonConstants.TYPE_APP;
2836e80583aSopenharmony_ci    appItemInfo.installTime = String(new Date());
2846e80583aSopenharmony_ci    appItemInfo.badgeNumber = await BadgeManager.getInstance().getBadgeByBundleSync(info.elementName.bundleName);
2856e80583aSopenharmony_ci    await ResourceManager.getInstance().updateIconCache(appItemInfo.appIconId, appItemInfo.bundleName, appItemInfo.moduleName);
2866e80583aSopenharmony_ci    this.mAppMap.set(appItemInfo.bundleName, appItemInfo);
2876e80583aSopenharmony_ci    return appItemInfo;
2886e80583aSopenharmony_ci  }
2896e80583aSopenharmony_ci
2906e80583aSopenharmony_ci  /**
2916e80583aSopenharmony_ci   * uninstall application, notice the userId need to be the login user
2926e80583aSopenharmony_ci   *
2936e80583aSopenharmony_ci   * @params bundleName application bundleName
2946e80583aSopenharmony_ci   * @params callback to get result
2956e80583aSopenharmony_ci   */
2966e80583aSopenharmony_ci  async uninstallLauncherAbility(bundleName: string, callback: (resultCode: number) => void): Promise<void> {
2976e80583aSopenharmony_ci    Log.showInfo(TAG, `uninstallLauncherAbility bundleName: ${bundleName}`);
2986e80583aSopenharmony_ci    try {
2996e80583aSopenharmony_ci      const bundlerInstaller = await installer.getBundleInstaller();
3006e80583aSopenharmony_ci      bundlerInstaller.uninstall(bundleName, {
3016e80583aSopenharmony_ci        userId: this.mUserId,
3026e80583aSopenharmony_ci        installFlag: 0,
3036e80583aSopenharmony_ci        isKeepData: false
3046e80583aSopenharmony_ci      }, (err: BusinessError) => {
3056e80583aSopenharmony_ci        if (err) {
3066e80583aSopenharmony_ci          callback(CommonConstants.INVALID_VALUE);
3076e80583aSopenharmony_ci          Log.showError(TAG, `uninstallLauncherAbility failed: ${JSON.stringify(err)}`);
3086e80583aSopenharmony_ci        } else {
3096e80583aSopenharmony_ci          callback(CommonConstants.UNINSTALL_SUCCESS);
3106e80583aSopenharmony_ci          Log.showDebug(TAG, `uninstallLauncherAbility successfully: ${JSON.stringify(err)}`);
3116e80583aSopenharmony_ci        }
3126e80583aSopenharmony_ci      });
3136e80583aSopenharmony_ci    } catch (err) {
3146e80583aSopenharmony_ci      let errCode = (err as BusinessError).code;
3156e80583aSopenharmony_ci      let errMsg = (err as BusinessError).message;
3166e80583aSopenharmony_ci      Log.showError(TAG, `uninstallLauncherAbility errCode: ${errCode}, errMsg: ${errMsg}`);
3176e80583aSopenharmony_ci    }
3186e80583aSopenharmony_ci  }
3196e80583aSopenharmony_ci
3206e80583aSopenharmony_ci  /**
3216e80583aSopenharmony_ci   * start the app
3226e80583aSopenharmony_ci   *
3236e80583aSopenharmony_ci   * @params paramAbilityName: Ability name
3246e80583aSopenharmony_ci   * @params paramBundleName: Application package name
3256e80583aSopenharmony_ci   */
3266e80583aSopenharmony_ci  startLauncherAbility(paramAbilityName: string, paramBundleName: string, paramModuleName: string) {
3276e80583aSopenharmony_ci    Log.showDebug(TAG, `startApplication abilityName: ${paramAbilityName}, bundleName: ${paramBundleName}, moduleName ${paramModuleName}`);
3286e80583aSopenharmony_ci    globalThis.desktopContext.startAbility({
3296e80583aSopenharmony_ci      bundleName: paramBundleName,
3306e80583aSopenharmony_ci      abilityName: paramAbilityName,
3316e80583aSopenharmony_ci      moduleName: paramModuleName
3326e80583aSopenharmony_ci    }).then(() => {
3336e80583aSopenharmony_ci      Log.showDebug(TAG, 'startApplication promise success');
3346e80583aSopenharmony_ci      performanceMonitor.end('LAUNCHER_APP_LAUNCH_FROM_ICON');
3356e80583aSopenharmony_ci      Log.showDebug(TAG, 'performanceMonitor end');
3366e80583aSopenharmony_ci    }, (err) => {
3376e80583aSopenharmony_ci      Log.showError(TAG, `startApplication promise error: ${JSON.stringify(err)}`);
3386e80583aSopenharmony_ci    });
3396e80583aSopenharmony_ci
3406e80583aSopenharmony_ci    const sysEventInfo = {
3416e80583aSopenharmony_ci      domain: 'LAUNCHER_APP',
3426e80583aSopenharmony_ci      name: 'START_ABILITY',
3436e80583aSopenharmony_ci      eventType: hiSysEvent.EventType.BEHAVIOR,
3446e80583aSopenharmony_ci      params: {
3456e80583aSopenharmony_ci        'BUNDLE_NAME': paramBundleName,
3466e80583aSopenharmony_ci        'ABILITY_NAME': paramAbilityName,
3476e80583aSopenharmony_ci        'MODULE_NAME': paramModuleName
3486e80583aSopenharmony_ci      }
3496e80583aSopenharmony_ci    };
3506e80583aSopenharmony_ci    hiSysEvent.write(sysEventInfo,
3516e80583aSopenharmony_ci      (err, value) => {
3526e80583aSopenharmony_ci        if (err) {
3536e80583aSopenharmony_ci          Log.showError(TAG, `startApplication hiSysEvent write error: ${err.code}`);
3546e80583aSopenharmony_ci        } else {
3556e80583aSopenharmony_ci          Log.showDebug(TAG, `startApplication hiSysEvent write success: ${value}`);
3566e80583aSopenharmony_ci        }
3576e80583aSopenharmony_ci      });
3586e80583aSopenharmony_ci    Log.showDebug(TAG, 'performanceMonitor begin');
3596e80583aSopenharmony_ci    performanceMonitor.begin('LAUNCHER_APP_LAUNCH_FROM_ICON', performanceMonitor.ActionType.LAST_UP,
3606e80583aSopenharmony_ci      paramBundleName);
3616e80583aSopenharmony_ci  }
3626e80583aSopenharmony_ci
3636e80583aSopenharmony_ci  /**
3646e80583aSopenharmony_ci   * start form config ability
3656e80583aSopenharmony_ci   *
3666e80583aSopenharmony_ci   * @params paramAbilityName
3676e80583aSopenharmony_ci   * @params paramBundleName
3686e80583aSopenharmony_ci   */
3696e80583aSopenharmony_ci  startAbilityFormEdit(paramAbilityName: string, paramBundleName: string, paramModuleName: string, paramCardId: number) {
3706e80583aSopenharmony_ci    Log.showDebug(TAG, `startAbility abilityName: ${paramAbilityName},bundleName: ${paramBundleName}, moduleName: ${paramModuleName} ,paramCardId: ${paramCardId}`);
3716e80583aSopenharmony_ci    globalThis.desktopContext.startAbility({
3726e80583aSopenharmony_ci      bundleName: paramBundleName,
3736e80583aSopenharmony_ci      abilityName: paramAbilityName,
3746e80583aSopenharmony_ci      moduleName: paramModuleName,
3756e80583aSopenharmony_ci      parameters:
3766e80583aSopenharmony_ci      {
3776e80583aSopenharmony_ci        formId: paramCardId.toString()
3786e80583aSopenharmony_ci      }
3796e80583aSopenharmony_ci    }).then((ret) => {
3806e80583aSopenharmony_ci      Log.showDebug(TAG, `startAbility ret: ${JSON.stringify(ret)}`);
3816e80583aSopenharmony_ci    }, (err) => {
3826e80583aSopenharmony_ci      Log.showError(TAG, `startAbility catch error: ${JSON.stringify(err)}`);
3836e80583aSopenharmony_ci    });
3846e80583aSopenharmony_ci  }
3856e80583aSopenharmony_ci
3866e80583aSopenharmony_ci  async getShortcutInfo(paramBundleName: string, callback) {
3876e80583aSopenharmony_ci    Log.showDebug(TAG, `getShortcutInfo bundleName: ${paramBundleName}`);
3886e80583aSopenharmony_ci    await launcherBundleManager.getShortcutInfo(paramBundleName)
3896e80583aSopenharmony_ci      .then(shortcutInfo => {
3906e80583aSopenharmony_ci        callback(paramBundleName, shortcutInfo);
3916e80583aSopenharmony_ci      })
3926e80583aSopenharmony_ci      .catch(err => {
3936e80583aSopenharmony_ci      });
3946e80583aSopenharmony_ci  }
3956e80583aSopenharmony_ci
3966e80583aSopenharmony_ci  /**
3976e80583aSopenharmony_ci   * start application by uri
3986e80583aSopenharmony_ci   *
3996e80583aSopenharmony_ci   * @params paramBundleName application bundle name
4006e80583aSopenharmony_ci   * @params paramAbilityName application abilit uri
4016e80583aSopenharmony_ci   */
4026e80583aSopenharmony_ci  startLauncherAbilityByUri(paramBundleName: string, abilityUri) {
4036e80583aSopenharmony_ci    Log.showInfo(TAG, `startLauncherAbilityByUri bundleName:${paramBundleName} abilityUri:${abilityUri}`);
4046e80583aSopenharmony_ci    const result = globalThis.desktopContext.startAbility({
4056e80583aSopenharmony_ci      bundleName: paramBundleName,
4066e80583aSopenharmony_ci      uri: abilityUri
4076e80583aSopenharmony_ci    }).then(() => {
4086e80583aSopenharmony_ci      Log.showDebug(TAG, 'startLauncherAbilityByUri promise success');
4096e80583aSopenharmony_ci    }, (err) => {
4106e80583aSopenharmony_ci      Log.showError(TAG, `startLauncherAbilityByUri promise error: ${JSON.stringify(err)}`);
4116e80583aSopenharmony_ci    });
4126e80583aSopenharmony_ci    Log.showDebug(TAG, `startLauncherAbilityByUri AceApplication : startAbility : ${result}`);
4136e80583aSopenharmony_ci  }
4146e80583aSopenharmony_ci
4156e80583aSopenharmony_ci  cleanAppMapCache() {
4166e80583aSopenharmony_ci    this.mAppMap.clear();
4176e80583aSopenharmony_ci  }
4186e80583aSopenharmony_ci}
4196e80583aSopenharmony_ci
4206e80583aSopenharmony_ciexport const launcherAbilityManager = LauncherAbilityManager.getInstance();
421