1 2/* 3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import { BusinessError } from 'basic'; 18import common from '@ohos.app.ability.common' 19import Want from '@ohos.application.Want'; 20import Log from '../Log'; 21 22const TAG = 'AbilityManager'; 23 24export default class AbilityManager { 25 static readonly ABILITY_NAME_ENTRY = 'SystemUi_Entry'; 26 static readonly ABILITY_NAME_STATUS_BAR = 'SystemUi_StatusBar'; 27 static readonly ABILITY_NAME_NAVIGATION_BAR = 'SystemUi_NavigationBar'; 28 static readonly ABILITY_NAME_VOLUME_PANEL = 'SystemUi_VolumePanel'; 29 static readonly ABILITY_NAME_NOTIFICATION_MANAGEMENT = 'SystemUi_NotificationManagement'; 30 static readonly ABILITY_NAME_DROPDOWN_PANEL = 'SystemUi_DropdownPanel'; 31 static readonly ABILITY_NAME_NOTIFICATION_PANEL = 'SystemUi_NotificationPanel'; 32 static readonly ABILITY_NAME_CONTROL_PANEL = 'SystemUi_ControlPanel'; 33 static readonly ABILITY_NAME_BANNER_NOTICE = 'SystemUi_BannerNotice'; 34 static readonly ABILITY_NAME_APP_LIST = 'SystemUi_AppList'; 35 static readonly ABILITY_NAME_OWNER_WANT = 'Owner_Want'; 36 37 static setContext(abilityName: string, context): void { 38 Log.showDebug(TAG, `setContext, abilityName: ${abilityName}`); 39 globalThis[abilityName + '_Context'] = context; 40 } 41 42 static getContext(abilityName?: string) { 43 Log.showDebug(TAG, `getContext, abilityName: ${abilityName}`); 44 if (!abilityName) { 45 abilityName = AbilityManager.ABILITY_NAME_ENTRY; 46 } 47 return globalThis[abilityName + '_Context']; 48 } 49 50 static setAbilityContext(abilityName: string, context: common.UIAbilityContext): void { 51 Log.showDebug(TAG, `setContext, abilityName: ${abilityName}`); 52 globalThis[abilityName + '_Context'] = context; 53 } 54 55 static getAbilityContext(abilityName?: string): common.UIAbilityContext { 56 Log.showDebug(TAG, `getContext, abilityName: ${abilityName}`); 57 if (!abilityName) { 58 abilityName = AbilityManager.ABILITY_NAME_ENTRY; 59 } 60 return globalThis[abilityName + '_Context']; 61 } 62 63 static setAbilityData(abilityName: string, key: string, data: any): void { 64 Log.showDebug(TAG, `setAbilityData, abilityName: ${abilityName} key: ${key} data: ${JSON.stringify(data)}`); 65 globalThis[abilityName + '_data_' + key] = data; 66 } 67 68 static getAbilityData(abilityName: string, key: string): any { 69 Log.showDebug(TAG, `getAbilityData, abilityName: ${abilityName} key: ${key} `); 70 return globalThis[abilityName + '_data_' + key]; 71 } 72 73 static setContextName(abilityName: string, contextName: string): void { 74 Log.showDebug(TAG, `setContextName, abilityName: ${abilityName}`); 75 globalThis[abilityName + '_ContextName'] = contextName; 76 } 77 78 static getContextName(abilityName?: string): string { 79 Log.showDebug(TAG, `setContextName, abilityName: ${abilityName}`); 80 if (!abilityName) { 81 abilityName = AbilityManager.ABILITY_NAME_ENTRY; 82 } 83 return globalThis[abilityName + '_ContextName']; 84 } 85 86 static startAbility(context: any, want: Want, callback?: (error?: BusinessError) => void): void { 87 Log.showDebug(TAG, `startAbility, want: ${JSON.stringify(want)}`); 88 if (context == null) { 89 context = AbilityManager.getContext(); 90 } 91 context.startAbility(want).then(() => { 92 Log.showInfo(TAG, 'startAbility, then'); 93 if (callback) { 94 callback(); 95 } 96 }).catch((error: BusinessError) => { 97 Log.showError(TAG, `startAbility, error: ${JSON.stringify(error)}`); 98 if (callback) { 99 callback(error); 100 } 101 }); 102 } 103 104 static startServiceExtensionAbility(context: any, want: Want, callback?: (error?: BusinessError) => void): void { 105 Log.showDebug(TAG, `startServiceExtensionAbility, want: ${JSON.stringify(want)}`); 106 if (context == null) { 107 context = AbilityManager.getContext(); 108 } 109 context.startServiceExtensionAbility(want).then(() => { 110 Log.showInfo(TAG, 'startServiceExtensionAbility, then'); 111 if (callback) { 112 callback(); 113 } 114 }).catch((error: BusinessError) => { 115 Log.showError(TAG, `startServiceExtensionAbility, error: ${JSON.stringify(error)}`); 116 if (callback) { 117 callback(error); 118 } 119 }); 120 } 121} 122