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 Prompt from '@ohos.promptAction';
176e80583aSopenharmony_ciimport { Log } from '../utils/Log';
186e80583aSopenharmony_ciimport { AppModel } from '../model/AppModel';
196e80583aSopenharmony_ciimport { ResourceManager } from '../manager/ResourceManager';
206e80583aSopenharmony_ciimport { CommonConstants } from '../constants/CommonConstants';
216e80583aSopenharmony_ciimport { launcherAbilityManager } from '../manager/LauncherAbilityManager';
226e80583aSopenharmony_ci
236e80583aSopenharmony_ciconst TAG = 'BaseViewModel';
246e80583aSopenharmony_ci
256e80583aSopenharmony_ci
266e80583aSopenharmony_ci/**
276e80583aSopenharmony_ci * Base class for view models.
286e80583aSopenharmony_ci */
296e80583aSopenharmony_ciexport class BaseViewModel {
306e80583aSopenharmony_ci  protected mAppModel: AppModel;
316e80583aSopenharmony_ci  protected mResourceManager: ResourceManager;
326e80583aSopenharmony_ci  private readonly listener;
336e80583aSopenharmony_ci  private KEY_NAME = 'name';
346e80583aSopenharmony_ci
356e80583aSopenharmony_ci  protected constructor() {
366e80583aSopenharmony_ci    this.mAppModel = AppModel.getInstance();
376e80583aSopenharmony_ci    this.mResourceManager = ResourceManager.getInstance();
386e80583aSopenharmony_ci    this.listener = this.appListChangeListener.bind(this);
396e80583aSopenharmony_ci  }
406e80583aSopenharmony_ci
416e80583aSopenharmony_ci
426e80583aSopenharmony_ci  /**
436e80583aSopenharmony_ci   * Start target ability
446e80583aSopenharmony_ci   *
456e80583aSopenharmony_ci   * @param bundleName target bundle name
466e80583aSopenharmony_ci   * @param abilityName target ability name
476e80583aSopenharmony_ci   */
486e80583aSopenharmony_ci  jumpTo(abilityName: string, bundleName: string, moduleName: string): void {
496e80583aSopenharmony_ci    launcherAbilityManager.startLauncherAbility(abilityName, bundleName, moduleName);
506e80583aSopenharmony_ci  }
516e80583aSopenharmony_ci
526e80583aSopenharmony_ci  /**
536e80583aSopenharmony_ci   * start form config ability.
546e80583aSopenharmony_ci   *
556e80583aSopenharmony_ci   * @param bundleName
566e80583aSopenharmony_ci   * @param abilityName
576e80583aSopenharmony_ci   */
586e80583aSopenharmony_ci  jumpToForm(abilityName: string, bundleName: string, moduleName: string, cardId: number): void {
596e80583aSopenharmony_ci    launcherAbilityManager.startAbilityFormEdit(abilityName, bundleName, moduleName, cardId);
606e80583aSopenharmony_ci  }
616e80583aSopenharmony_ci
626e80583aSopenharmony_ci  /**
636e80583aSopenharmony_ci   * Start launcher settings page.
646e80583aSopenharmony_ci   */
656e80583aSopenharmony_ci  jumpToSetting(): void {
666e80583aSopenharmony_ci    this.jumpTo(CommonConstants.SETTING_ABILITY, CommonConstants.LAUNCHER_BUNDLE, CommonConstants.SETTING_MODULE);
676e80583aSopenharmony_ci  }
686e80583aSopenharmony_ci
696e80583aSopenharmony_ci  private uninstallAppCallback = (resultCode: number): void => {
706e80583aSopenharmony_ci    this.informUninstallResult(resultCode);
716e80583aSopenharmony_ci  }
726e80583aSopenharmony_ci
736e80583aSopenharmony_ci  /**
746e80583aSopenharmony_ci   * Uninstall target app by bundle name.
756e80583aSopenharmony_ci   *
766e80583aSopenharmony_ci   * @param uninstallBundleName bundle name to uninstall
776e80583aSopenharmony_ci   * @param isUninstallable true if target app is uninstallable.
786e80583aSopenharmony_ci   */
796e80583aSopenharmony_ci  uninstallApp(uninstallBundleName: string, isUninstallable: boolean): void {
806e80583aSopenharmony_ci    if (!isUninstallable) {
816e80583aSopenharmony_ci      this.informUninstallResult(CommonConstants.UNINSTALL_FORBID);
826e80583aSopenharmony_ci    } else {
836e80583aSopenharmony_ci      void launcherAbilityManager.uninstallLauncherAbility(uninstallBundleName, this.uninstallAppCallback);
846e80583aSopenharmony_ci    }
856e80583aSopenharmony_ci  }
866e80583aSopenharmony_ci
876e80583aSopenharmony_ci  registerAppListChangeCallback(): void {
886e80583aSopenharmony_ci    this.mAppModel.registerStateChangeListener(this.listener);
896e80583aSopenharmony_ci  }
906e80583aSopenharmony_ci
916e80583aSopenharmony_ci  unregisterAppListChangeCallback(): void {
926e80583aSopenharmony_ci    Log.showInfo(TAG, 'unregisterAppListChangeCallback');
936e80583aSopenharmony_ci    this.mAppModel.unregisterAppStateChangeListener(this.listener);
946e80583aSopenharmony_ci  }
956e80583aSopenharmony_ci
966e80583aSopenharmony_ci  appListChangeListener(appList: []): void {
976e80583aSopenharmony_ci    this.regroupDataAppListChange(appList);
986e80583aSopenharmony_ci  }
996e80583aSopenharmony_ci
1006e80583aSopenharmony_ci  regroupDataAppListChange(callbackList: []): void {
1016e80583aSopenharmony_ci  }
1026e80583aSopenharmony_ci
1036e80583aSopenharmony_ci  informUninstallResult(resultCode: number): void {
1046e80583aSopenharmony_ci    Log.showDebug(TAG, `Launcher AppListView getUninstallApp uninstallationResult: ${resultCode}`);
1056e80583aSopenharmony_ci    if (resultCode === CommonConstants.UNINSTALL_FORBID) {
1066e80583aSopenharmony_ci      Prompt.showToast({
1076e80583aSopenharmony_ci        message: $r("app.string.disable_uninstall")
1086e80583aSopenharmony_ci      });
1096e80583aSopenharmony_ci    } else if (resultCode === CommonConstants.UNINSTALL_SUCCESS) {
1106e80583aSopenharmony_ci      Prompt.showToast({
1116e80583aSopenharmony_ci        message: $r("app.string.uninstall_success")
1126e80583aSopenharmony_ci      });
1136e80583aSopenharmony_ci    } else {
1146e80583aSopenharmony_ci      Prompt.showToast({
1156e80583aSopenharmony_ci        message: $r("app.string.uninstall_failed")
1166e80583aSopenharmony_ci      });
1176e80583aSopenharmony_ci    }
1186e80583aSopenharmony_ci  }
1196e80583aSopenharmony_ci
1206e80583aSopenharmony_ci  getAppName(cacheKey: string): string {
1216e80583aSopenharmony_ci    return this.mResourceManager.getAppResourceCache(cacheKey, this.KEY_NAME);
1226e80583aSopenharmony_ci  }
1236e80583aSopenharmony_ci}
124