1/* 2 * Copyright (c) 2023 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 resourceManager from '@ohos.resourceManager'; 17import hilog from '@ohos.hilog'; 18import { BusinessError } from '@ohos.base'; 19 20const TAG = '[Sample_ResourceManager]'; 21const DOMAIN = 0xFF00; 22const SPECIFIED_NUM = 2; 23let resMgr = getContext().resourceManager; 24 25async function getString(resId: number): Promise<string | undefined> { 26 try { 27 let value = await resMgr.getStringValue(resId); 28 return value; 29 } catch (error) { 30 let code = (error as BusinessError).code; 31 let message = (error as BusinessError).message; 32 hilog.error(DOMAIN, TAG, `getStringValue failed, error code: ${code}, message: ${message}.`); 33 return; 34 } 35} 36 37async function getStringArray(resource: resourceManager.Resource): Promise<Array<string> | undefined> { 38 try { 39 let value = await resMgr.getStringArrayValue(resource); 40 return value; 41 } catch (error) { 42 let code = (error as BusinessError).code; 43 let message = (error as BusinessError).message; 44 hilog.error(DOMAIN, TAG, `getStringArrayValue failed, error code: ${code}, message: ${message}.`); 45 return; 46 } 47} 48 49async function getPluralString(resId: number, num: number): Promise<string | undefined> { 50 try { 51 let value = await resMgr.getPluralStringValue(resId, num); 52 return value; 53 } catch (error) { 54 let code = (error as BusinessError).code; 55 let message = (error as BusinessError).message; 56 hilog.error(DOMAIN, TAG, `getPluralStringValue failed, error code: ${code}, message: ${message}.`); 57 return; 58 } 59} 60 61async function getDeviceCapability(): Promise<resourceManager.DeviceCapability | undefined> { 62 try { 63 let value = await resMgr.getDeviceCapability(); 64 return value; 65 } catch (error) { 66 let code = (error as BusinessError).code; 67 let message = (error as BusinessError).message; 68 hilog.error(DOMAIN, TAG, `getDeviceCapability failed, error code: ${code}, message: ${message}.`); 69 return; 70 } 71} 72 73async function getConfiguration(): Promise<resourceManager.Configuration | undefined> { 74 try { 75 let value = await resMgr.getConfiguration(); 76 return value; 77 } catch (error) { 78 let code = (error as BusinessError).code; 79 let message = (error as BusinessError).message; 80 hilog.error(DOMAIN, TAG, `getConfiguration failed, error code: ${code}, message: ${message}.`); 81 return; 82 } 83} 84 85async function getMedia(resId: number): Promise<Uint8Array | undefined> { 86 try { 87 let value = await resMgr.getMediaContent(resId); 88 return value; 89 } catch (error) { 90 let code = (error as BusinessError).code; 91 let message = (error as BusinessError).message; 92 hilog.error(DOMAIN, TAG, `getMediaContent failed, error code: ${code}, message: ${message}.`); 93 return; 94 } 95} 96 97async function getMediaBase64(resId: number): Promise<string | undefined> { 98 try { 99 let value = await resMgr.getMediaContentBase64(resId); 100 return value; 101 } catch (error) { 102 let code = (error as BusinessError).code; 103 let message = (error as BusinessError).message; 104 hilog.error(DOMAIN, TAG, `getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 105 return; 106 } 107} 108 109function getFormatString(resId: number, world: string): string | undefined { 110 try { 111 let value = resMgr.getStringSync(resId, world); 112 return value; 113 } catch (error) { 114 let code = (error as BusinessError).code; 115 let message = (error as BusinessError).message; 116 hilog.error(DOMAIN, TAG, `getStringSync failed, error code: ${code}, message: ${message}.`); 117 return; 118 } 119} 120 121async function getDensityMediaBase64(resId: number, density: number): Promise<string | undefined> { 122 try { 123 let value = await resMgr.getMediaContentBase64(resId, density); 124 return value; 125 } catch (error) { 126 let code = (error as BusinessError).code; 127 let message = (error as BusinessError).message; 128 hilog.error(DOMAIN, TAG, `getDensityMediaBase64 failed, error code: ${code}, message: ${message}.`); 129 return; 130 } 131} 132 133async function getSystemMediaBase64(resId: number): Promise<string | undefined> { 134 // 获取仅系统资源管理对象 135 let sysMgr = resourceManager.getSystemResourceManager(); 136 try { 137 let value = await sysMgr.getMediaContentBase64(resId); 138 return value; 139 } catch (error) { 140 let code = (error as BusinessError).code; 141 let message = (error as BusinessError).message; 142 hilog.error(DOMAIN, TAG, `getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 143 return; 144 } 145} 146 147@Entry 148@Component 149struct Index { 150 @State string_str: string = 'string' 151 @State strArray: string = 'stringArray' 152 @State plural: string = 'plural' 153 @State configuration: string = 'configuration' 154 @State capability: string = 'capability' 155 @State media: string = 'media' 156 @State mediaBase: string = 'mediaBase' 157 @State formatStr: string = 'Format String' 158 @State densityMedia: string = 'Density Media' 159 @State systemRes: string = 'System Res' 160 161 async aboutToAppear() { 162 this.string_str = await getString($r('app.string.string_str').id) as string; 163 let resource: resourceManager.Resource = { 164 bundleName: "ohos.samples.resourcemanager", 165 moduleName: "entry", 166 id: $r('app.strarray.str_array').id 167 } 168 this.strArray = JSON.stringify(await getStringArray(resource) as Array<string>); 169 this.plural = await getPluralString($r('app.plural.eat_apple').id, SPECIFIED_NUM) as string; 170 this.configuration = JSON.stringify(await getConfiguration() as resourceManager.Configuration); 171 this.capability = JSON.stringify(await getDeviceCapability() as resourceManager.DeviceCapability); 172 this.media = JSON.stringify(((await getMedia($r('app.media.app_icon').id)) as Uint8Array).length); 173 this.mediaBase = JSON.stringify(((await getMediaBase64($r('app.media.app_icon').id)) as string).length); 174 this.formatStr = getFormatString($r('app.string.formatStr').id, 175 await getString($r('app.string.world').id) as string) as string; 176 this.densityMedia = await getDensityMediaBase64($r('app.media.density').id, 640) as string; 177 this.systemRes = await getSystemMediaBase64($r('sys.media.ohos_app_icon').id) as string; 178 } 179 180 build() { 181 Column() { 182 Text($r('app.string.title')) 183 .width('100%') 184 .height(50) 185 .backgroundColor($r('app.color.text_color')) 186 .fontColor(Color.White) 187 .fontSize(20) 188 .padding({ left: 15 }) 189 Scroll() { 190 Column() { 191 Text($r('app.string.stringDesc')) 192 .fontSize(25) 193 194 Text(this.string_str) 195 .fontSize(25) 196 .fontColor('#ffff0000') 197 .fontWeight(FontWeight.Bold) 198 199 Text($r('app.string.stringArrayDesc')) 200 .fontSize(25) 201 202 Text(this.strArray) 203 .fontSize(25) 204 .fontColor('#ffff0000') 205 .fontWeight(FontWeight.Bold) 206 207 Text($r('app.string.pluralStringDesc')) 208 .fontSize(25) 209 210 Text(this.plural) 211 .fontSize(25) 212 .fontColor('#ffff0000') 213 .fontWeight(FontWeight.Bold) 214 215 Text($r('app.string.configurationDesc')) 216 .fontSize(25) 217 218 Text(this.configuration) 219 .fontSize(25) 220 .fontColor('#ffff0000') 221 .fontWeight(FontWeight.Bold) 222 223 Text($r('app.string.capabilityDesc')) 224 .fontSize(25) 225 226 Text(this.capability) 227 .fontSize(25) 228 .fontColor('#ffff0000') 229 .fontWeight(FontWeight.Bold) 230 231 Text($r('app.string.mediaDesc')) 232 .fontSize(25) 233 234 Text(this.media) 235 .fontSize(25) 236 .fontColor('#ffff0000') 237 .fontWeight(FontWeight.Bold) 238 239 Text($r('app.string.mediaBase64Desc')) 240 .fontSize(25) 241 242 Text(this.mediaBase) 243 .fontSize(25) 244 .fontColor('#ffff0000') 245 .fontWeight(FontWeight.Bold) 246 247 Text($r('app.string.formatStrDesc')) 248 .fontSize(25) 249 250 Text(this.formatStr) 251 .fontSize(25) 252 .fontColor('#ffff0000') 253 .fontWeight(FontWeight.Bold) 254 255 Text($r('app.string.densityMediaDesc')) 256 .fontSize(25) 257 258 Image(this.densityMedia) 259 .id('getDensityMedia') 260 .height('10%') 261 262 Text($r('app.string.systemResDesc')) 263 .fontSize(25) 264 265 Image(this.systemRes) 266 .id('getSystemMedia') 267 .height('10%') 268 } 269 .width('100%') 270 .padding(10) 271 .alignItems(HorizontalAlign.Start) 272 } 273 } 274 .width('100%') 275 .height('100%') 276 } 277}