1/* 2 * Copyright (c) 2021-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 i18n from '@ohos.i18n'; 18import { permissionGroups, groups } from "../model/permissionGroup"; 19import Constants from '../utils/constant'; 20import { BundleInfoUtils } from './bundleInfoUtils'; 21import { CommonUtils } from './commonUtils'; 22import { GroupInfo, appInfo, ApplicationObj } from './typedef'; 23 24/** 25 * verify permission 26 * @param {Number} accessTokenId 27 * @param {String} permission permission name 28 */ 29export function verifyAccessToken(accessTokenId: number, permission: Permissions) { 30 return new Promise<number>((resolve) => { 31 let atManager = abilityAccessCtrl.createAtManager(); 32 let data = atManager.verifyAccessTokenSync(accessTokenId, permission); 33 if (data == abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) { 34 resolve(data); 35 } else { 36 try { 37 atManager.getPermissionFlags(accessTokenId, permission).then((flag: number) => { 38 if (flag == Constants.PERMISSION_COMPONENT_SET) { 39 resolve(abilityAccessCtrl.GrantStatus.PERMISSION_DENIED); 40 } else { 41 resolve(data); 42 } 43 }) 44 } 45 catch(err) { 46 console.log(TAG + 'getPermissionFlags error: ' + JSON.stringify(err)); 47 resolve(data); 48 } 49 } 50 }) 51} 52 53/** 54 * Omit display when application name is too long 55 * @param {String} Application name 56 */ 57export function titleTrim(title: string): string { 58 const length: number = title.length; 59 if (length > Constants.MAXIMUM_HEADER_LENGTH) { 60 let str = ''; 61 str = title.substring(0, Constants.MAXIMUM_HEADER_LENGTH) + '...'; 62 return str; 63 } else { 64 return title; 65 } 66} 67 68export const indexValue: string[] = [ 69 '#', 70 'A', 71 'B', 72 'C', 73 'D', 74 'E', 75 'F', 76 'G', 77 'H', 78 'I', 79 'J', 80 'K', 81 'L', 82 'M', 83 'N', 84 'O', 85 'P', 86 'Q', 87 'R', 88 'S', 89 'T', 90 'U', 91 'V', 92 'W', 93 'X', 94 'Y', 95 'Z' 96]; // Array of alphabetically indexed names 97 98export function addLocalTag(info: appInfo) { 99 let isZh = i18n.System.getSystemLanguage().indexOf('zh') >= 0; 100 let appName: string = info.label; 101 let upperCase = CommonUtils.isEmpty(appName) ? '' : appName[0].toLocaleUpperCase(); 102 let regexEn: RegExp = new RegExp("[A-Z]"); 103 let regexNm: RegExp = new RegExp("[0-9]"); 104 105 if (isZh) { 106 if (upperCase.match(regexEn)) { 107 info.zhTag = BundleInfoUtils.getStringZh(appName); 108 info.indexTag = upperCase; 109 info.language = 'EN'; 110 } else { 111 info.zhTag = appName; 112 info.language = 'CN'; 113 if (upperCase.match(regexNm)) { 114 info.indexTag = '#'; 115 } else { 116 info.indexTag = BundleInfoUtils.findZhIndex(upperCase); 117 } 118 } 119 } else { 120 if (upperCase.match(regexEn)) { 121 info.zhTag = appName; 122 info.indexTag = upperCase; 123 info.language = 'EN'; 124 } else { 125 info.zhTag = appName; 126 info.indexTag = '#'; 127 info.language = 'CN'; 128 } 129 } 130} 131 132let enComparator = new Intl.Collator('en'); 133let zhComparator = new Intl.Collator('zh-Hans-CN'); 134 135export function sortByName(appArray: Array<appInfo | ApplicationObj>): Array<appInfo | ApplicationObj> { 136 return appArray.sort((item1: appInfo | ApplicationObj, item2: appInfo | ApplicationObj) => { 137 if (item1.indexTag !== item2.indexTag) { 138 return enComparator.compare(item1.indexTag, item2.indexTag); 139 } 140 141 let isEn1 = item1.language === 'EN'; 142 let isEn2 = item2.language === 'EN'; 143 144 if (isEn1 && isEn2) { 145 return enComparator.compare(item1.label, item2.label); 146 } else if (isEn1 && !isEn2) { 147 return 1; 148 } else if (!isEn1 && isEn2) { 149 return -1; 150 } else { 151 return zhComparator.compare(item1.zhTag, item2.zhTag); 152 } 153 }) 154} 155 156/** 157 * Get permission label 158 * @param {String} permission name 159 */ 160export function getPermissionLabel(permission: string): ResourceStr { 161 for (let i = 0; i < permissionGroups.length; i++) { 162 if (permissionGroups[i].permissionName == permission) { 163 return permissionGroups[i].label 164 } 165 } 166 return ''; 167} 168 169/** 170 * Get the corresponding permission group id according to the permission 171 * @param {String} permission app name id 172 * @return {GroupInfo} group 173 */ 174export function getPermissionGroup(permission: string): GroupInfo { 175 for (let i = 0; i < permissionGroups.length; i++) { 176 if (permissionGroups[i].permissionName == permission) { 177 return groups[permissionGroups[i].groupId]; 178 } 179 } 180 return groups[0]; 181} 182 183/** 184 * Obtain a permission group by its name 185 * @param {String} group name 186 * @return {GroupInfo} group 187 */ 188export function getPermissionGroupByName(name: string): GroupInfo { 189 for (let i = 0; i < groups.length; i++) { 190 if (groups[i].name === name) { 191 return groups[i]; 192 } 193 } 194 return groups[0]; 195} 196 197/** 198 * Obtain the permission group ID by permission name 199 * @param {String} permission name 200 * @return {number} groupId 201 */ 202export function getGroupIdByPermission(permission: string): number { 203 for (let i = 0; i < permissionGroups.length; i++) { 204 if (permissionGroups[i].permissionName === permission) { 205 return permissionGroups[i].groupId; 206 } 207 } 208 return 0; 209} 210 211const TAG = 'PermissionManager_Log'; 212 213export class Log { 214 static info(log: string) { 215 console.info(`Info: ${TAG} ${log}`); 216 } 217 218 static error(log: string) { 219 console.error(`Error: ${TAG} ${log}`); 220 } 221}