• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 common from '@ohos.app.ability.common';
17import { BusinessError } from '@ohos.base';
18import { LogUtil } from './LogUtil';
19
20const TAG: string = 'Note_GlobalResourceManager';
21let context = getContext(this) as common.UIAbilityContext;
22
23export class GlobalResourceManager {
24  public async getStringByResource(res: Resource): Promise<string> {
25    let json: Resource = JSON.parse(JSON.stringify(res));
26    let id: number = json.id;
27    LogUtil.info(TAG, "ResourceID = " + id)
28    return await this.getStringById(id);
29  }
30
31  public getStringById(id: number): Promise<string> {
32    let promise = new Promise<string>(resolve => {
33      let resourceMgr = context.resourceManager;
34      resourceMgr.getStringValue(id)
35        .then((resource) => {
36          resolve(resource);
37          LogUtil.info(TAG, 'getStringById resolve(resource) : ' + resolve(resource));
38          LogUtil.info(TAG, 'getStringById resource : ' + resource);
39          LogUtil.info(TAG, 'getStringById resource2 : ' + JSON.stringify(resource));
40        })
41        .catch((err: BusinessError) => {
42          LogUtil.error(TAG, 'getStringById err : ' + JSON.stringify(err));
43        });
44    });
45    LogUtil.info(TAG, 'getStringById promise: ' + JSON.stringify(promise));
46    return promise;
47  }
48}
49
50let mGlobalResourceManager = new GlobalResourceManager();
51
52export default mGlobalResourceManager as GlobalResourceManager;
53