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