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