1/* 2 * Copyright (c) 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 LruCache from './LruCache' 17import { CheckEmptyUtils } from '../utils/CheckEmptyUtils' 18import { Logger } from '../utils/Logger' 19 20const KEY_ICON = 'icon' 21const KEY_NAME = 'name' 22const TAG: string = 'ResourceManager' 23 24/** 25 * Wrapper class for resourceManager interfaces. 26 */ 27export class ResourceManager { 28 private static resourceManager: ResourceManager = undefined 29 private readonly memoryCache = new LruCache() 30 private context: any = undefined 31 32 constructor(context) { 33 this.context = context 34 } 35 36 /** 37 * Get the application data model object. 38 * 39 * @return {object} application data model singleton 40 */ 41 static getInstance(context): ResourceManager { 42 if (this.resourceManager === null || this.resourceManager === undefined) { 43 this.resourceManager = new ResourceManager(context) 44 } 45 return this.resourceManager 46 } 47 48 private getCacheFromMemory(cacheKey: string, cacheType: string) { 49 const cache = this.memoryCache.getCache(cacheKey) 50 if (cache == undefined || cache == null || cache == '' || cache === -1) { 51 return null 52 } else if (cache[cacheType] == undefined || cache[cacheType] == null || cache[cacheType] == '') { 53 return null 54 } else { 55 return cache[cacheType] 56 } 57 } 58 59 private setCacheToMemory(cacheKey: string, cacheType: string, value: object | string): void { 60 let cache = this.memoryCache.getCache(cacheKey) 61 if (cache == undefined || cache == null || cache == '' || cache === -1) { 62 cache = {} 63 cache[cacheType] = value 64 } else { 65 cache[cacheType] = value 66 } 67 this.memoryCache.putCache(cacheKey, cache) 68 } 69 70 getCachedAppIcon(iconId: number, bundleName: string) { 71 const cacheKey = `${iconId}${bundleName}` 72 return this.getCacheFromMemory(cacheKey, KEY_ICON) 73 } 74 75 setAppResourceCache(cacheKey: string, cacheType: string, value: object | string): void { 76 this.setCacheToMemory(cacheKey, cacheType, value) 77 } 78 79 deleteAppResourceCache(cacheKey: string, cacheType: string): void { 80 this.memoryCache.remove(cacheKey) 81 } 82 83 async updateIconCache(iconId: number, bundleName: string): Promise<void> { 84 try { 85 let cacheKey = `${iconId}${bundleName}` 86 const iconBase64 = this.getCacheFromMemory(cacheKey, KEY_ICON) 87 if (!CheckEmptyUtils.isEmpty(iconBase64)) { 88 return 89 } 90 const bundleContext = this.context.createBundleContext(bundleName) 91 if (bundleContext == null) { 92 return 93 } 94 await bundleContext.resourceManager.getMediaBase64(iconId).then((value) => { 95 if (value != null) { 96 this.setAppResourceCache(cacheKey, KEY_ICON, value) 97 } 98 }) 99 } catch (error) { 100 Logger.error(TAG, `updateIconCache error ${error}`) 101 } 102 } 103 104 getAppIconWithCache(iconId: number, bundleName: string, callback: Function, defaultAppIcon: string) { 105 if (CheckEmptyUtils.isEmpty(iconId) || iconId <= 0) { 106 Logger.info(TAG, 'getAppIconWithCache defaultAppIcon') 107 callback(defaultAppIcon) 108 } else { 109 const cacheKey = iconId + bundleName 110 const iconBase64 = this.getCacheFromMemory(cacheKey, KEY_ICON) 111 if (CheckEmptyUtils.isEmpty(iconBase64)) { 112 if (this.isResourceManagerEmpty()) { 113 Logger.error(TAG, 'getAppIconWithCache resourceManager is empty') 114 callback(defaultAppIcon) 115 return 116 } 117 try { 118 const bundleContext = this.context.createBundleContext(bundleName) 119 bundleContext.resourceManager.getMediaBase64(iconId).then((value: string) => { 120 Logger.info(TAG, `getAppIconWithCache getMediaBase64 success`) 121 if (value != null) { 122 this.setAppResourceCache(cacheKey, KEY_ICON, value) 123 callback(value) 124 } 125 else { 126 callback(defaultAppIcon) 127 } 128 }) 129 } catch (error) { 130 Logger.error(TAG, `getAppIconWithCache error ${error}`) 131 callback(defaultAppIcon) 132 } 133 } else { 134 callback(iconBase64) 135 } 136 } 137 } 138 139 async getAppNameSync(labelId: number, bundleName: string, appName: string) { 140 if (CheckEmptyUtils.isEmpty(labelId) || labelId <= 0 || CheckEmptyUtils.checkStrIsEmpty(bundleName)) { 141 Logger.info(TAG, `getAppNameSync param empty! appName: ${appName}`) 142 return appName 143 } else { 144 const cacheKey = labelId + bundleName 145 Logger.info(TAG, `getAppNameSync getResourceManager cacheKey: ${cacheKey}`) 146 if (this.isResourceManagerEmpty()) { 147 Logger.info(TAG, 'getAppNameSync resourceManager is empty') 148 return appName 149 } 150 const bundleContext = this.context.createBundleContext(bundleName) 151 let resMgrName = await bundleContext.resourceManager.getString(labelId) 152 Logger.info(TAG, `getAppNameSync resMgrName: ${resMgrName}`) 153 if (resMgrName != null) { 154 return resMgrName 155 } else { 156 return appName 157 } 158 } 159 } 160 161 getAppNameWithCache(labelId: number, bundleName: string, appName: string, callback) { 162 if (CheckEmptyUtils.isEmpty(labelId) || labelId <= 0) { 163 Logger.info(TAG, `getAppNameWithCache ResourceManager getAppName callback: ${appName}`) 164 callback(appName) 165 } else { 166 const cacheKey = labelId + bundleName 167 const name = this.getCacheFromMemory(cacheKey, KEY_ICON) 168 if (CheckEmptyUtils.isEmpty(name)) { 169 if (this.isResourceManagerEmpty()) { 170 Logger.info(TAG, 'getAppNameWithCache resourceManager is empty') 171 return appName 172 } 173 try { 174 const bundleContext = this.context.createBundleContext(bundleName) 175 bundleContext.resourceManager.getString(labelId).then((value) => { 176 if (CheckEmptyUtils.checkStrIsEmpty(value)) { 177 Logger.info(TAG, `getAppNameWithCache getAppName getString ERROR! value is empty id ${labelId}`) 178 callback(appName) 179 } else { 180 this.setAppResourceCache(cacheKey, KEY_NAME, value) 181 callback(value) 182 } 183 }) 184 } catch (err) { 185 Logger.error(TAG, `getAppNameWithCache error: ${JSON.stringify(err)}`) 186 callback(appName) 187 } 188 } else { 189 callback(name) 190 } 191 } 192 } 193 194 /** 195 * Get app resource cache. 196 * 197 * @param {string} cacheKey 198 * @param {string} cacheType 199 */ 200 getAppResourceCache(cacheKey, cacheType) { 201 return this.getCacheFromMemory(cacheKey, cacheType) 202 } 203 204 /** 205 * get string by resource.id. 206 * 207 * @param {number} resource.id 208 * @param {function} callback(value) 209 */ 210 getStringById(resId: number, callback: (value: string) => void): void { 211 if (this.isResourceManagerEmpty()) { 212 Logger.info(TAG, 'resourceManager is empty') 213 callback('') 214 return 215 } 216 try { 217 this.context.resourceManager.getString(resId).then((value) => { 218 if (CheckEmptyUtils.checkStrIsEmpty(value)) { 219 Logger.info(TAG, 'getStringById ERROR! value is empty:' + resId) 220 } 221 callback(value) 222 }) 223 } catch (err) { 224 Logger.error(TAG, `getStringById error: ${JSON.stringify(err)}`) 225 callback('') 226 } 227 } 228 229 private isResourceManagerEmpty(): boolean { 230 return CheckEmptyUtils.isEmpty(this.context) 231 || CheckEmptyUtils.isEmpty(this.context.resourceManager) 232 } 233 234 async getStringByResource(res: Resource): Promise<string> { 235 const json = JSON.parse(JSON.stringify(res)) 236 const id = json.id 237 return await this.getStringByIdSync(id) 238 } 239 240 /** 241 * get string by resource.id. 242 * 243 * @param {number} resource.id 244 * @return {string} resource name 245 */ 246 async getStringByIdSync(resId: number): Promise<string> { 247 let resMgrName = '' 248 if (resId <= 0) { 249 Logger.info(TAG, `getStringByIdSync resId: ${resId}`) 250 return resMgrName 251 } else { 252 if (this.isResourceManagerEmpty()) { 253 Logger.info(TAG, 'getStringByIdSync resourceManager is empty') 254 return resMgrName 255 } 256 try { 257 resMgrName = await this.context.resourceManager.getString(resId) 258 } catch (err) { 259 Logger.error(TAG, `getStringByIdSync error: ${JSON.stringify(err)}`) 260 } 261 Logger.info(TAG, `getStringByIdSync resMgrName: ${resMgrName}`) 262 return resMgrName 263 } 264 } 265}