• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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 fileShare from '@ohos.fileshare';
17import wantConstant from '@ohos.app.ability.wantConstant';
18import { Log } from '../../../utils/Log';
19
20const TAG: string = 'common_SelectUtil';
21
22export class SelectUtil {
23  static getUriArray(selectedPhotos: Set<string>): Array<string> {
24    let uriArray = new Array<string>();
25    if (selectedPhotos == undefined) {
26      return uriArray;
27    }
28    selectedPhotos.forEach((uri) => {
29      uriArray.push(uri);
30    })
31    return uriArray;
32  }
33
34  static async grantPermissionForUris(uris: Array<string>, bundleName: string): Promise<void> {
35    Log.info(TAG, `start uris grant. bundleName: ${bundleName}`);
36    let promises: Array<Promise<void>> = [];
37    for (let uri of uris) {
38      promises.push(SelectUtil.grantPermissionForUri(uri, bundleName));
39    }
40    await Promise.all(promises);
41  }
42
43  private static async grantPermissionForUri(uri: string, bundleName: string): Promise<void> {
44    Log.debug(TAG, `start uri grant. uri: ${uri}`);
45    try {
46      await fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION);
47    } catch (err) {
48      Log.error(TAG, `grant permission error: ${JSON.stringify(err)}`);
49    }
50  }
51}