• 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 { Log } from '../utils/Log';
18import { AppModel } from '../model/AppModel';
19import { ResourceManager } from '../manager/ResourceManager';
20import { CommonConstants } from '../constants/CommonConstants';
21import { launcherAbilityManager } from '../manager/LauncherAbilityManager';
22import { AsyncCallback } from '@ohos.base';
23
24const TAG = 'BaseViewModel';
25
26const KEY_NAME = 'name';
27
28/**
29 * Base class for view models.
30 */
31export class BaseViewModel {
32  protected mAppModel: AppModel;
33  protected mResourceManager: ResourceManager;
34  private readonly listener;
35
36  protected constructor() {
37    this.mAppModel = AppModel.getInstance();
38    this.mResourceManager = ResourceManager.getInstance();
39    this.listener = this.appListChangeListener.bind(this);
40  }
41
42
43  /**
44   * Start target ability
45   *
46   * @param bundleName target bundle name
47   * @param abilityName target ability name
48   */
49  jumpTo(abilityName: string, bundleName: string, moduleName: string): void {
50    launcherAbilityManager.startLauncherAbility(abilityName, bundleName, moduleName);
51  }
52
53  /**
54   * start form config ability.
55   *
56   * @param bundleName
57   * @param abilityName
58   */
59  jumpToForm(abilityName: string, bundleName: string, moduleName: string, cardId: number): void {
60    launcherAbilityManager.startAbilityFormEdit(abilityName, bundleName, moduleName, cardId);
61  }
62
63  /**
64   * Start launcher settings page.
65   */
66  jumpToSetting(): void {
67    this.jumpTo(CommonConstants.SETTING_ABILITY, CommonConstants.LAUNCHER_BUNDLE, CommonConstants.SETTING_MODULE);
68  }
69
70  private uninstallAppCallback = (resultCode: number): void => {
71    this.informUninstallResult(resultCode);
72  }
73
74  /**
75   * Uninstall target app by bundle name.
76   *
77   * @param uninstallBundleName bundle name to uninstall
78   * @param isUninstallable true if target app is uninstallable.
79   */
80  uninstallApp(uninstallBundleName: string, isUninstallable: boolean): void {
81    if (!isUninstallable) {
82      this.informUninstallResult(CommonConstants.UNINSTALL_FORBID);
83    } else {
84      void launcherAbilityManager.uninstallLauncherAbility(uninstallBundleName, this.uninstallAppCallback);
85    }
86  }
87
88  registerAppListChangeCallback(): void {
89    this.mAppModel.registerStateChangeListener(this.listener);
90  }
91
92  unregisterAppListChangeCallback(): void {
93    Log.showInfo(TAG, 'unregisterAppListChangeCallback');
94    this.mAppModel.unregisterAppStateChangeListener(this.listener);
95  }
96
97  appListChangeListener(appList: []): void {
98    this.regroupDataAppListChange(appList);
99  }
100
101  regroupDataAppListChange(callbackList: []): void {
102  }
103
104  informUninstallResult(resultCode: number): void {
105    Log.showDebug(TAG, `Launcher AppListView getUninstallApp uninstallationResult: ${resultCode}`);
106    if (resultCode === CommonConstants.UNINSTALL_FORBID) {
107      Prompt.showToast({
108        message: $r("app.string.disable_uninstall")
109      });
110    } else if (resultCode === CommonConstants.UNINSTALL_SUCCESS) {
111      Prompt.showToast({
112        message: $r("app.string.uninstall_success")
113      });
114    } else {
115      Prompt.showToast({
116        message: $r("app.string.uninstall_failed")
117      });
118    }
119  }
120
121  getAppName(cacheKey: string): string {
122    return this.mResourceManager.getAppResourceCache(cacheKey, KEY_NAME);
123  }
124}
125