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 abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'; 17import Log from './Log'; 18 19const permissions: Array<Permissions> = ['ohos.permission.READ_AUDIO']; 20 21export default class PermissionUtils { 22 public static init(context: Context, callback: Function) { 23 Log.info('start init permission'); 24 let atManager = abilityAccessCtrl.createAtManager(); 25 // requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗 26 27 atManager.requestPermissionsFromUser(context, permissions).then((data) => { 28 let grantStatus: Array<number> = data.authResults; 29 let length: number = grantStatus.length; 30 for (let i = 0; i < length; i ++) { 31 if(grantStatus[i] !== 0) { 32 Log.info(`User reject permission: ${grantStatus[i]}`); 33 callback(false); 34 return; 35 } 36 } 37 Log.info(`get permission succeed`); 38 callback(true); 39 }).catch((err: Error) => { 40 Log.info(`Failed to request permissions from user. err : ${JSON.stringify(err)}`); 41 }) 42 } 43}