1bea4f105Sopenharmony_ci/*
2bea4f105Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
3bea4f105Sopenharmony_ci */
4bea4f105Sopenharmony_ciimport wantConstant from '@ohos.app.ability.wantConstant';
5bea4f105Sopenharmony_ciimport fileShare from '@ohos.fileshare';
6bea4f105Sopenharmony_ciimport { BusinessError } from '@ohos.base';
7bea4f105Sopenharmony_ci
8bea4f105Sopenharmony_ciimport { Logger } from '../common/util/HiLogger'
9bea4f105Sopenharmony_ciimport { LocalAudioFile } from './localaudio/LocalAudioFile'
10bea4f105Sopenharmony_ciimport { LocalAudioManager } from './localaudio/LocalAudioManager'
11bea4f105Sopenharmony_ciimport { Constants } from '../constant/Constants';
12bea4f105Sopenharmony_ci
13bea4f105Sopenharmony_ciconst logger: Logger = new Logger('LocalResourceManager')
14bea4f105Sopenharmony_ci
15bea4f105Sopenharmony_ciexport class LocalResourceManager {
16bea4f105Sopenharmony_ci
17bea4f105Sopenharmony_ci  /**
18bea4f105Sopenharmony_ci   * 本地歌曲资源管理类 todo 改全局注册
19bea4f105Sopenharmony_ci   */
20bea4f105Sopenharmony_ci  public localAudioManager: LocalAudioManager = new LocalAudioManager()
21bea4f105Sopenharmony_ci
22bea4f105Sopenharmony_ci  public getLocalAudioResource(offset: number, limit: number, context: Context): Promise<Array<LocalAudioFile>> {
23bea4f105Sopenharmony_ci    return this.localAudioManager.getLocalAudio(offset, limit, context)
24bea4f105Sopenharmony_ci  }
25bea4f105Sopenharmony_ci
26bea4f105Sopenharmony_ci  /**
27bea4f105Sopenharmony_ci   * 用户选择文件后,返回结果URI列表并赋予临时权限
28bea4f105Sopenharmony_ci   */
29bea4f105Sopenharmony_ci  public terminateSelfWithResult(localAudioFile: Array<LocalAudioFile>): Promise<Array<string>> {
30bea4f105Sopenharmony_ci    //赋予权限就放在audiopicker模块,直接调系统接口
31bea4f105Sopenharmony_ci    return new Promise(async (resolve) => {
32bea4f105Sopenharmony_ci      let uriList: string[] = []
33bea4f105Sopenharmony_ci      for(let item of localAudioFile) {
34bea4f105Sopenharmony_ci        try {
35bea4f105Sopenharmony_ci          await fileShare.grantUriPermission(item.uri, Constants.AUDIO_PICKER_BUNDLE, wantConstant
36bea4f105Sopenharmony_ci            .Flags.FLAG_AUTH_READ_URI_PERMISSION | wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION)
37bea4f105Sopenharmony_ci          uriList.push(item.uri)
38bea4f105Sopenharmony_ci          logger.info('push uriList length: ' + uriList.length)
39bea4f105Sopenharmony_ci        } catch (err) {
40bea4f105Sopenharmony_ci          let error: BusinessError = err as BusinessError;
41bea4f105Sopenharmony_ci          logger.error('grantUriPermission failed with error:' + JSON.stringify(error));
42bea4f105Sopenharmony_ci          resolve([])
43bea4f105Sopenharmony_ci          return
44bea4f105Sopenharmony_ci        }
45bea4f105Sopenharmony_ci      }
46bea4f105Sopenharmony_ci      logger.info('Permission uriList length: ' + uriList.length)
47bea4f105Sopenharmony_ci      resolve(uriList)
48bea4f105Sopenharmony_ci    })
49bea4f105Sopenharmony_ci  }
50bea4f105Sopenharmony_ci}
51