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 BundleMgr from "@ohos.bundle.bundleManager"; 17import Log from "../Log"; 18import SwitchUserManager from "../SwitchUserManager"; 19import AbilityManager from "./abilityManager"; 20 21const TAG = "BRManager"; 22 23interface Resource { 24 bundleName: string; 25 moduleName: string; 26 id: number; 27} 28 29export default class BundleManager { 30 static readonly RESOURCE_MANAGER = 'SystemUi_Resource_Manager'; 31 32 static getResourceManager() { 33 if(!globalThis[BundleManager.RESOURCE_MANAGER]){ 34 Log.showInfo(TAG, 'init resourceManager'); 35 globalThis[BundleManager.RESOURCE_MANAGER] = AbilityManager.getContext().resourceManager; 36 } 37 return globalThis[BundleManager.RESOURCE_MANAGER]; 38 } 39 40 static getString(resource: Resource, callback?: Function){ 41 Log.showDebug(TAG, `getString, resource: ${JSON.stringify(resource)}`); 42 if(callback){ 43 BundleManager.getResourceManager().getString(resource).then((value) => { 44 Log.showDebug(TAG, `getString, callback excute`); 45 callback(value) ; 46 }) 47 return; 48 } else { 49 return BundleManager.getResourceManager().getString(resource); 50 } 51 } 52 53 static getMediaBase64(resource: Resource, callback?: Function){ 54 Log.showDebug(TAG, `getMediaBase64, resource: ${JSON.stringify(resource)}`); 55 if(callback){ 56 BundleManager.getResourceManager().getMediaBase64(resource).then((value) => { 57 Log.showDebug(TAG, `getMediaBase64, callback excute`); 58 callback(value) ; 59 }) 60 return; 61 } else { 62 return BundleManager.getResourceManager().getMediaBase64(resource); 63 } 64 } 65 66 static async getBundleInfo(tag: string, bundleName: string, getInfo?: any, requestId?: number) { 67 getInfo = getInfo ?? BundleMgr.BundleFlag.GET_BUNDLE_INFO_DEFAULT; 68 let userInfo = { 69 userId: requestId ?? (await SwitchUserManager.getInstance().getCurrentUserInfo()).userId, 70 }; 71 Log.showDebug(TAG, `getBundleInfo from: ${tag}, userId: ${userInfo.userId}`); 72 return await BundleMgr.getBundleInfo(bundleName, getInfo, userInfo.userId); 73 } 74}