HarmonyOS —— 仿 XPermission 鸿蒙权限申请工具类

仿 XPermission 写一个简单的鸿蒙权限申请工具类。

TypeScript 复制代码
import { abilityAccessCtrl, bundleManager, Context, Permissions } from '@kit.AbilityKit'
import { ArrayList } from '@kit.ArkTS'

export class PermissionUtil {
  private static readonly sUtil: PermissionUtil = new PermissionUtil()
  private mAtManager: abilityAccessCtrl.AtManager
  private mTokenId: number
  private mContext?: Context
  private mPermissions: ArrayList<Permissions> = new ArrayList()

  constructor() {
    this.mAtManager = abilityAccessCtrl.createAtManager()
    let bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)
    this.mTokenId = bundleInfo.appInfo.accessTokenId
  }

  public static init(context: Context): PermissionUtil {
    PermissionUtil.sUtil.mContext = context
    return PermissionUtil.sUtil
  }

  public addPermissions(permission: Permissions): PermissionUtil {
    this.mPermissions.add(permission)
    return PermissionUtil.sUtil
  }


  /**
   * 检测并申请权限
   * @returns 
   */
  async request() {
    return new Promise<RequestResult>((resolve, reject) => {
      //如果开发者没有添加权限则抛出异常
      if (this.mPermissions.length === 0) {
        reject(Error("请添加权限"))
        return
      }
      //克隆开发添加的权限
      let temp = this.mPermissions.clone()
      let grantedList: ArrayList<Permissions> = new ArrayList()
      let deniedList: ArrayList<Permissions> = new ArrayList()
      //遍历开发者添加的权限是否已经授权,如果已经授权则移出列表
      this.mPermissions.forEach((permission) => {
        let result = this.checkPermission(permission)
        if (result) {
          //从临时列表里面移出
          temp.remove(permission)
          //添加到已授权权限列表
          this.push(permission, grantedList)
        }
      })
      //如果列表长度为 0 则表示权限已经全部授权,直接返回.否则对未授权的权限进行权限申请
      if (temp.length === 0) {
        resolve({ allGranted: true, grantedList, deniedList })
      } else {
        this.mAtManager.requestPermissionsFromUser(this.mContext, temp.convertToArray(), (error, requestResult) => {
          if (error) {
            //申请权限出错,抛出异常
            reject(error)
          } else {
            //对授权结果进行分类
            requestResult.authResults.forEach((authResult, index) => {
              if (authResult === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
                this.push(requestResult.permissions[index] as Permissions, grantedList)
              } else {
                this.push(requestResult.permissions[index] as Permissions, deniedList)
              }
              resolve({ allGranted: deniedList.length === 0, grantedList, deniedList })
            })
          }
        })
      }
    })
  }

  /**
   * add 数据前查重
   * @param element
   * @param arrayList
   */
  private push<T>(element: T, arrayList: ArrayList<T>) {
    if (!arrayList.has(element)) {
      arrayList.add(element)
    }
  }

  private checkPermission(permission: Permissions): boolean {
    let result = this.mAtManager.checkAccessTokenSync(this.mTokenId, permission)
    return result === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED
  }
}

interface RequestResult {
  allGranted: boolean
  grantedList: ArrayList<Permissions>
  deniedList: ArrayList<Permissions>
}
相关推荐
Li_Ning2110 小时前
vue3+uniapp开发鸿蒙初体验
华为·uni-app·harmonyos
特立独行的猫a10 小时前
HarmonyOS NEXT边学边玩:从零实现一个影视App(七、今日票房页面的设计与实现)
华为·harmonyos
liukuang11013 小时前
华为小米vivo向上,苹果荣耀OPPO向下
华为
李洋-蛟龙腾飞公司13 小时前
华为支付-(可选)特定场景配置操作
华为·harmonyos
李洋-蛟龙腾飞公司13 小时前
华为支付接入规范
华为·harmonyos
luoganttcc13 小时前
华为升腾算子开发(一) helloword
java·前端·华为
程序猿阿伟14 小时前
《探秘鸿蒙Next:非结构化数据处理与模型轻量化的完美适配》
华为·harmonyos
没有猫饼15 小时前
《鸿蒙HarmonyOS 5.0开发教程》基础篇11:父子组件通信
harmonyos·arkts
HarmonyOS_SDK16 小时前
巧用多目标识别能力,帮助应用实现智能化图片解析
harmonyos
TS_forever00719 小时前
【华为路由的arp配置】
网络·华为