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 bundleManager from '@ohos.bundle.bundleManager'; 17import common from '@ohos.app.ability.common'; 18import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; 19import privacyManager from '@ohos.privacyManager'; 20import { backBar } from '../common/components/backBar'; 21import Constants from '../common/utils/constant'; 22import { Log, getPermissionGroup, getGroupIdByPermission, supportPermission } from '../common/utils/utils'; 23import { 24 StringObj, 25 AppInfoSimple, 26 AppRecordInfo, 27 AppInfo, 28 GroupRecordInfo, 29 AppGroupRecordInfo 30} from '../common/model/typedef'; 31import { GlobalContext } from '../common/utils/globalContext'; 32import { Permission } from '../common/model/definition'; 33 34@Extend(Image)function customizeImage(width: number, height: number) { 35 .objectFit(ImageFit.Contain) 36 .width(width) 37 .height(height) 38 .draggable(false) 39}; 40 41@Entry 42@Component 43struct permissionRecordPage { 44 private context = this.getUIContext().getHostContext() as common.UIAbilityContext; 45 @State isVertical: boolean = GlobalContext.load('isVertical'); 46 @State allBundleInfo: AppInfo[] = GlobalContext.load('allBundleInfo'); 47 @State groups: GroupRecordInfo[] = []; 48 @State applicationInfos: AppRecordInfo[] = []; 49 @State permissionApplications: AppRecordInfo[] = []; 50 @State permissionIndex: number = -1; 51 @State applicationIndex: number = -1; 52 @State strings: StringObj = new StringObj('', ''); 53 @State currentIndex: number = 0; 54 @State show: boolean = false; 55 @State isTouch: string = ''; 56 @State reqUserRecords: privacyManager.PermissionUsedRecord[] = []; 57 @State reqUserPermissions: Permission[] = []; 58 @State permissionGroups: AppGroupRecordInfo[] = []; 59 @State groupNames: ResourceStr[] = []; 60 @State appLastTime: number = 0; 61 @Builder TabBuilder(index: number) { 62 Flex({ 63 alignItems: index ? ItemAlign.Start : ItemAlign.End, 64 justifyContent: FlexAlign.Center, 65 direction: FlexDirection.Column 66 }) { 67 Text(index ? $r('app.string.application') : $r('app.string.permission')) 68 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 69 .fontColor( 70 this.currentIndex == index ? 71 $r('sys.color.ohos_id_color_subtab_text_on') : 72 $r('sys.color.icon_secondary') 73 ) 74 .fontWeight(this.currentIndex == index ? FontWeight.Bold : FontWeight.Regular) 75 .lineHeight(Constants.TEXT_LINE_HEIGHT) 76 .baselineOffset(Constants.TAB_DECORATION_POSITION_Y) 77 if (this.currentIndex == index) { 78 Text(index ? $r('app.string.application') : $r('app.string.permission')) 79 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 80 .fontWeight(FontWeight.Bold) 81 .lineHeight(Constants.TAB_LINE_HEIGHT) 82 .height(Constants.TAB_DECORATION_HEIGHT) 83 .backgroundColor($r('sys.color.comp_background_emphasize')) 84 } 85 }.height(Constants.TAB_HEIGHT) 86 .width(Constants.FULL_WIDTH) 87 .padding({ left: this.isVertical ? Constants.TAB_INNER_PADDING : Constants.DEFAULT_PADDING_START, 88 right: this.isVertical ? Constants.TAB_INNER_PADDING : Constants.DEFAULT_PADDING_END }) 89 } 90 91 @Builder PermissionListItemLayout(item: GroupRecordInfo, index: number) { 92 ListItem() { 93 Column() { 94 Column() { 95 Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) { 96 Row() { 97 Image(item.icon) 98 .fillColor($r('sys.color.icon_secondary')) 99 .customizeImage(Constants.MANAGEMENT_IMAGE_WIDTH, Constants.MANAGEMENT_IMAGE_HEIGHT) 100 .margin({ 101 right: Constants.MANAGEMENT_IMAGE_MARGIN_RIGHT_RECORD, 102 left: Constants.MANAGEMENT_IMAGE_MARGIN_LEFT 103 }) 104 Column() { 105 Text(item.groupName) 106 .width(Constants.MAXIMUM_HEADER_WIDTH) 107 .maxLines(Constants.MAXIMUM_HEADER_LINES) 108 .textOverflow({ overflow: TextOverflow.Ellipsis }) 109 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 110 .fontWeight(FontWeight.Medium) 111 .fontColor($r('sys.color.font_primary')) 112 .lineHeight(Constants.TEXT_LINE_HEIGHT) 113 .margin({ bottom: Constants.TERTIARY_LABEL_MARGIN_BOTTOM }) 114 Text($r('app.string.visits', String(item.sum))) 115 .fontSize(Constants.TEXT_SMALL_FONT_SIZE) 116 .fontColor($r('sys.color.font_secondary')) 117 .lineHeight(Constants.TEXT_SMALL_LINE_HEIGHT) 118 }.flexGrow(Constants.FLEX_GROW) 119 .alignItems(HorizontalAlign.Start) 120 Image(index == this.permissionIndex ? $r('app.media.xiangshangjiantou') : $r('app.media.xiangxiajiantou')) 121 .fillColor($r('sys.color.icon_secondary')) 122 .customizeImage(Constants.IMAGE_WIDTH_RECORD, Constants.IMAGE_HEIGHT_RECORD) 123 } 124 .width(Constants.FULL_WIDTH) 125 .height(Constants.LISTITEM_HEIGHT_PERMISSION) 126 } 127 }.onClick(() => { 128 this.permissionIndex = this.permissionIndex == index ? -1 : index 129 this.permissionApplications = this.applicationInfos.filter((appInfo) => { 130 return appInfo.groupNames.includes(item.groupName); 131 }) 132 }) 133 .padding({ left: $r('sys.float.ohos_id_card_margin_start'), right: $r('sys.float.ohos_id_card_margin_end') }) 134 if (index == this.permissionIndex) { 135 List() { 136 ForEach(this.permissionApplications, (permissionApplication: AppRecordInfo) => { 137 ListItem() { 138 Row() { 139 Image(permissionApplication.icon) 140 .customizeImage(Constants.APPLICATION_IMAGE_WIDTH, Constants.APPLICATION_IMAGE_HEIGHT) 141 .margin({ right: Constants.MANAGEMENT_IMAGE_MARGIN_RIGHT }) 142 Column() { 143 Row().width(Constants.FULL_WIDTH).height(Constants.DIVIDER) 144 .backgroundColor($r('sys.color.comp_divider')) 145 .position({ x: 0, y: 0 }) 146 Text(permissionApplication.groupName) 147 .width(Constants.MAXIMUM_HEADER_WIDTH) 148 .maxLines(Constants.MAXIMUM_HEADER_LINES) 149 .textOverflow({ overflow: TextOverflow.Ellipsis }) 150 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 151 .fontWeight(FontWeight.Medium) 152 .fontColor($r('sys.color.font_primary')) 153 .lineHeight(Constants.TEXT_LINE_HEIGHT) 154 .margin({ bottom: Constants.TERTIARY_LABEL_MARGIN_BOTTOM }) 155 Text( 156 $r('app.string.recent_visit', 157 String(this.getAppRecords(permissionApplication, item.groupName, true)), 158 this.getTime(this.getAppRecords(permissionApplication, item.groupName, false))) 159 ) 160 .padding({ right: Constants.LISTITEM_PADDING_RIGHT_RECORD }) 161 .fontSize(Constants.TEXT_SMALL_FONT_SIZE) 162 .fontColor($r('sys.color.font_secondary')) 163 .lineHeight(Constants.TEXT_SMALL_LINE_HEIGHT) 164 }.alignItems(HorizontalAlign.Start) 165 .height(Constants.FULL_HEIGHT) 166 .width(Constants.FULL_WIDTH) 167 .justifyContent(FlexAlign.Center) 168 } 169 }.height(Constants.LISTITEM_HEIGHT_APPLICATION) 170 .onClick(() => { 171 let info: AppInfoSimple = new AppInfoSimple( 172 permissionApplication.name, 173 permissionApplication.api, 174 permissionApplication.accessTokenId, 175 permissionApplication.icon, 176 permissionApplication.groupName, 177 permissionApplication.reqUserPermissions, 178 permissionApplication.groupIds 179 ) 180 GlobalContext.store('applicationInfo', info); 181 this.getUIContext().getRouter().pushUrl({ url: 'pages/application-secondary' }); 182 }) 183 .padding({ 184 left: $r('sys.float.ohos_id_card_margin_start'), 185 right: $r('sys.float.ohos_id_card_margin_end') 186 }) 187 .borderRadius($r('sys.float.ohos_id_corner_radius_default_l')) 188 .linearGradient((this.isTouch === permissionApplication.name) ? { 189 angle: 90, 190 direction: GradientDirection.Right, 191 colors: [['#DCEAF9', 0.0], ['#FAFAFA', 1.0]] 192 } : { 193 angle: 90, 194 direction: GradientDirection.Right, 195 colors: [ 196 [$r('sys.color.comp_background_list_card'), 1], 197 [$r('sys.color.comp_background_list_card'), 1] 198 ] 199 }) 200 .onTouch(event => { 201 if (event === undefined) { 202 return; 203 } 204 if (event.type === TouchType.Down) { 205 this.isTouch = permissionApplication.name; 206 } 207 if (event.type === TouchType.Up) { 208 this.isTouch = ''; 209 } 210 }) 211 }, (item: AppRecordInfo) => JSON.stringify(item)) 212 } 213 } 214 } 215 }.padding(Constants.LIST_PADDING_TOP) 216 .margin({ bottom: Constants.LISTITEM_MARGIN_BOTTOM }) 217 .backgroundColor($r('sys.color.comp_background_list_card')) 218 .borderRadius($r('sys.float.ohos_id_corner_radius_card')) 219 } 220 221 @Builder ApplicationListItemLayout(item: AppRecordInfo, index: number) { 222 ListItem() { 223 Column() { 224 Column() { 225 Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) { 226 Row() { 227 Image(item.icon) 228 .customizeImage(Constants.APPLICATION_IMAGE_WIDTH, Constants.APPLICATION_IMAGE_HEIGHT) 229 .margin({ right: Constants.MANAGEMENT_IMAGE_MARGIN_RIGHT }) 230 Column() { 231 Text(item.groupName) 232 .width(Constants.MAXIMUM_HEADER_WIDTH) 233 .maxLines(Constants.MAXIMUM_HEADER_LINES) 234 .textOverflow({ overflow: TextOverflow.Ellipsis }) 235 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 236 .fontWeight(FontWeight.Medium) 237 .fontColor($r('sys.color.font_primary')) 238 .lineHeight(Constants.TEXT_LINE_HEIGHT) 239 .margin({ bottom: Constants.TERTIARY_LABEL_MARGIN_BOTTOM }) 240 Row() { 241 ForEach(item.permissions, (permission: AppGroupRecordInfo) => { 242 Image(permission.icon) 243 .fillColor($r('sys.color.icon_secondary')) 244 .customizeImage( 245 Constants.IMAGE_WIDTH_RECORD_APPLICATION, 246 Constants.IMAGE_HEIGHT_RECORD_APPLICATION 247 ) 248 .margin({ right: Constants.APPLICATION_TEXT_MARGIN_RIGHT }) 249 }, (item: AppGroupRecordInfo) => JSON.stringify(item)) 250 } 251 }.flexGrow(Constants.FLEX_GROW) 252 .alignItems(HorizontalAlign.Start) 253 Image( 254 index == this.applicationIndex ? $r('app.media.xiangshangjiantou') : $r('app.media.xiangxiajiantou') 255 ) 256 .fillColor($r('sys.color.icon_secondary')) 257 .customizeImage(Constants.IMAGE_WIDTH_RECORD, Constants.IMAGE_HEIGHT_RECORD) 258 } 259 .width(Constants.FULL_WIDTH) 260 .height(Constants.LISTITEM_HEIGHT_APPLICATION) 261 } 262 }.onClick(() => { 263 this.applicationIndex = this.applicationIndex == index ? -1 : index 264 }) 265 .padding({ left: $r('sys.float.ohos_id_card_margin_start'), right: $r('sys.float.ohos_id_card_margin_end') }) 266 if (index == this.applicationIndex) { 267 List() { 268 ForEach(item.permissions, (permission: AppGroupRecordInfo) => { 269 ListItem() { 270 Row() { 271 Image(permission.icon) 272 .fillColor($r('sys.color.icon_secondary')) 273 .customizeImage(Constants.MANAGEMENT_IMAGE_WIDTH, Constants.MANAGEMENT_IMAGE_HEIGHT) 274 .margin({ 275 right: Constants.MANAGEMENT_IMAGE_MARGIN_RIGHT_RECORD, 276 left: Constants.MANAGEMENT_IMAGE_MARGIN_LEFT 277 }) 278 Column() { 279 Row().width(Constants.FULL_WIDTH).height(Constants.DIVIDER) 280 .backgroundColor($r('sys.color.comp_divider')) 281 .position({ x: 0, y: 0 }) 282 Text(permission.groupName) 283 .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE) 284 .fontWeight(FontWeight.Medium) 285 .fontColor($r('sys.color.font_primary')) 286 .lineHeight(Constants.TEXT_LINE_HEIGHT) 287 .margin({ bottom: Constants.TERTIARY_LABEL_MARGIN_BOTTOM }) 288 Text($r('app.string.recent_visit', String(permission.count), this.getTime(permission.lastTime))) 289 .padding({ right: Constants.LISTITEM_PADDING_RIGHT_RECORD }) 290 .fontSize(Constants.TEXT_SMALL_FONT_SIZE) 291 .fontColor($r('sys.color.font_secondary')) 292 .lineHeight(Constants.TEXT_SMALL_LINE_HEIGHT) 293 }.alignItems(HorizontalAlign.Start) 294 .height(Constants.FULL_HEIGHT) 295 .width(Constants.FULL_WIDTH) 296 .justifyContent(FlexAlign.Center) 297 } 298 }.height(Constants.LISTITEM_HEIGHT_PERMISSION) 299 .onClick(() => { 300 let info: AppInfoSimple = new AppInfoSimple( 301 item.name, 302 item.api, 303 item.accessTokenId, 304 item.icon, 305 item.groupName, 306 item.reqUserPermissions, 307 item.groupIds 308 ); 309 GlobalContext.store('applicationInfo', info); 310 this.getUIContext().getRouter().pushUrl({ url: 'pages/application-secondary' }); 311 }) 312 .padding({ 313 left: $r('sys.float.ohos_id_card_margin_start'), 314 right: $r('sys.float.ohos_id_card_margin_end') 315 }) 316 .borderRadius($r('sys.float.ohos_id_corner_radius_default_l')) 317 .linearGradient((this.isTouch === permission.name) ? { 318 angle: 90, 319 direction: GradientDirection.Right, 320 colors: [['#DCEAF9', 0.0], ['#FAFAFA', 1.0]] 321 } : { 322 angle: 90, 323 direction: GradientDirection.Right, 324 colors: [ 325 [$r('sys.color.comp_background_list_card'), 1], 326 [$r('sys.color.comp_background_list_card'), 1] 327 ] 328 }) 329 .onTouch(event => { 330 if (event === undefined) { 331 return; 332 } 333 if (event.type === TouchType.Down) { 334 this.isTouch = permission.name; 335 } 336 if (event.type === TouchType.Up) { 337 this.isTouch = ''; 338 } 339 }) 340 }, (item: AppGroupRecordInfo) => JSON.stringify(item)) 341 } 342 } 343 } 344 }.padding(Constants.LIST_PADDING_TOP) 345 .margin({ bottom: Constants.LISTITEM_MARGIN_BOTTOM }) 346 .backgroundColor($r('sys.color.comp_background_list_card')) 347 .borderRadius($r('sys.float.ohos_id_corner_radius_card')) 348 } 349 350 build() { 351 GridRow({ gutter: Constants.GUTTER, columns: { 352 xs: Constants.XS_COLUMNS, sm: Constants.SM_COLUMNS, md: Constants.MD_COLUMNS, lg: Constants.LG_COLUMNS } }) { 353 GridCol({ 354 span: { xs: Constants.XS_SPAN, sm: Constants.SM_SPAN, md: Constants.MD_SPAN, lg: Constants.LG_SPAN }, 355 offset: { xs: Constants.XS_OFFSET, sm: Constants.SM_OFFSET, md: Constants.MD_OFFSET, lg: Constants.LG_OFFSET } 356 }) { 357 Row() { 358 Column() { 359 Row() { 360 backBar( { title: JSON.stringify($r('app.string.permission_access_record')), recordable: false }) 361 } 362 Row() { 363 Column() { 364 Column() { 365 Flex({ justifyContent: FlexAlign.Start }) { 366 Text($r('app.string.record_time_limit')) 367 .margin({ left: Constants.DEFAULT_MARGIN_START }) 368 .fontSize(Constants.TEXT_SMALL_FONT_SIZE) 369 .fontColor($r('sys.color.font_secondary')) 370 .lineHeight(Constants.SUBTITLE_LINE_HEIGHT) 371 }.constraintSize({ minHeight: Constants.SUBTITLE_MIN_HEIGHT }) 372 .padding({ top: Constants.SUBTITLE_PADDING_TOP, bottom: Constants.SUBTITLE_PADDING_BOTTOM }) 373 if (this.groups.length) { 374 Stack() { 375 Tabs() { 376 TabContent() { 377 Row() { 378 Column() { 379 Scroll() { 380 Row() { 381 List() { 382 ForEach(this.groups, (item: GroupRecordInfo, index) => { 383 this.PermissionListItemLayout(item, index as number) 384 }, (item: GroupRecordInfo) => JSON.stringify(item)) 385 }.padding({ top: Constants.LIST_PADDING_TOP, bottom: Constants.LIST_PADDING_BOTTOM }) 386 .height(Constants.FULL_HEIGHT) 387 }.padding({ 388 left: Constants.MANAGEMENT_ROW_PADDING_LEFT, 389 right: Constants.MANAGEMENT_ROW_PADDING_RIGHT, 390 top: Constants.MANAGEMENT_ROW_PADDING_TOP, 391 bottom: Constants.MANAGEMENT_ROW_PADDING_BOTTOM 392 }) 393 }.scrollBar(BarState.Off) 394 }.width(Constants.FULL_WIDTH) 395 } 396 }.tabBar(this.TabBuilder(0)) 397 TabContent() { 398 Row() { 399 Column() { 400 Scroll() { 401 Row() { 402 if (this.show) { 403 List() { 404 ForEach(this.applicationInfos, (item: AppRecordInfo, index) => { 405 this.ApplicationListItemLayout(item, index as number) 406 }, (item: AppRecordInfo) => JSON.stringify(item)) 407 } 408 .padding({ top: Constants.LIST_PADDING_TOP, bottom: Constants.LIST_PADDING_BOTTOM }) 409 .height(Constants.FULL_HEIGHT) 410 } 411 }.padding({ 412 left: Constants.MANAGEMENT_ROW_PADDING_LEFT, 413 right: Constants.MANAGEMENT_ROW_PADDING_RIGHT, 414 top: Constants.MANAGEMENT_ROW_PADDING_TOP, 415 bottom: Constants.MANAGEMENT_ROW_PADDING_BOTTOM 416 }) 417 }.scrollBar(BarState.Off) 418 }.width(Constants.FULL_WIDTH) 419 } 420 }.tabBar(this.TabBuilder(1)) 421 } 422 .barMode(BarMode.Fixed) 423 .barWidth(Constants.BAR_WIDTH) 424 .onChange((index) => { 425 this.currentIndex = index; 426 if (!this.show) { 427 this.show = true; 428 } 429 }) 430 }.height(Constants.FULL_HEIGHT) 431 } else { 432 Flex({ 433 justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center, direction: FlexDirection.Column 434 }) { 435 Image($r('app.media.noRecord')) 436 .customizeImage(Constants.NORECORD_IMAGE_WIDTH, Constants.NORECORD_IMAGE_HEIGHT) 437 .margin({ left: Constants.NORECORD_IMAGE_MARGIN_LEFT }) 438 Text($r('app.string.no_record')).margin({ top: Constants.DIALOG_REQ_MARGIN_TOP }) 439 .fontSize(Constants.TEXT_SMALL_FONT_SIZE) 440 .fontColor($r('sys.color.font_secondary')) 441 }.width(Constants.FULL_WIDTH).height(Constants.FULL_HEIGHT) 442 .padding({ bottom: Constants.RECORD_PADDING_BOTTOM }) 443 } 444 } 445 } 446 } 447 .layoutWeight(Constants.LAYOUT_WEIGHT) 448 } 449 } 450 .height(Constants.FULL_HEIGHT) 451 .width(Constants.FULL_WIDTH) 452 .backgroundColor($r('sys.color.background_secondary')) 453 } 454 }.backgroundColor($r('sys.color.background_secondary')) 455 } 456 457 /** 458 * Get time 459 * @param {Number} The time stamp 460 */ 461 getTime(time: number, format = 'MM月DD日 NNHH:mm') { 462 if (this.strings.morning == 'am') { format = 'HH:mm NN MM/DD' } 463 let date = new Date(time); 464 let key = ['MM', 'DD', 'NN', 'HH', 'mm']; 465 let value = [ 466 date.getMonth() + 1, 467 date.getDate(), 468 date.getHours() >= 12 ? this.strings.afternoon : this.strings.morning, 469 date.getHours() >= 12 ? date.getHours() - 12 : date.getHours(), 470 date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes(), 471 ]; 472 473 for (let i = 0; i < key.length; ++i) { 474 format = format.replace(key[i], String(value[i])); 475 } 476 return format; 477 } 478 479 /** 480 * Get application record info 481 * @param {Object} application info 482 * @param {String} groupName 483 * @param {Boolean} true: count, false: lastTime 484 */ 485 getAppRecords(appInfo: AppRecordInfo, groupName: ResourceStr, option: boolean): number { 486 let record = appInfo.permissions.filter(permission => { 487 return permission.groupName == groupName; 488 }) 489 if (record.length > 0) { 490 return option ? record[0].count : record[0].lastTime; 491 } else { 492 return 0; 493 } 494 } 495 496 getStrings() { 497 this.context.resourceManager.getStringValue($r('app.string.morning').id, (err, val) => { 498 this.strings.morning = val; 499 }) 500 this.context.resourceManager.getStringValue($r('app.string.afternoon').id, (err, val) => { 501 this.strings.afternoon = val; 502 }) 503 } 504 505 async getReqUserRecords(record: privacyManager.BundleUsedRecord, info: bundleManager.BundleInfo) { 506 for (let j = 0; j < record.permissionRecords.length; j++) { 507 let permission = record.permissionRecords[j].permissionName as Permission; 508 try { 509 let flag = await abilityAccessCtrl.createAtManager().getPermissionFlags(info.appInfo.accessTokenId, permission); 510 if (flag == Constants.PERMISSION_SYSTEM_FIXED) { 511 continue; 512 } 513 } catch (err) { 514 Log.error('getPermissionFlags error: ' + JSON.stringify(err)); 515 } 516 let supportPermissions = supportPermission(); 517 if (supportPermissions.indexOf(permission) != -1) { 518 this.reqUserRecords.push(record.permissionRecords[j]); 519 } 520 } 521 } 522 523 async getReqUserPermissions(reqPermissions: Permission[], info: bundleManager.BundleInfo) { 524 let acManager = abilityAccessCtrl.createAtManager(); 525 for (let k = 0; k < reqPermissions.length; k++) { 526 let reqPermission = reqPermissions[k]; 527 try { 528 let reqFlag = await acManager.getPermissionFlags(info.appInfo.accessTokenId, reqPermission); 529 if (reqFlag == Constants.PERMISSION_SYSTEM_FIXED) { 530 continue; 531 } 532 } catch (err) { 533 Log.error('getPermissionFlags error: ' + JSON.stringify(err)); 534 } 535 let supportPermissions = supportPermission(); 536 if (supportPermissions.indexOf(reqPermission) != -1) { 537 this.reqUserPermissions.push(reqPermission); 538 } 539 } 540 } 541 542 getPermissionGroups() { 543 this.reqUserRecords.forEach(reqUserRecord => { 544 let group = getPermissionGroup(reqUserRecord.permissionName as Permission); 545 if (!group) { 546 Log.info('permission not find:' + reqUserRecord.permissionName); 547 } else { 548 let existing = this.permissionGroups.find(permissionGroup => permissionGroup.name == group.name); 549 let lastTime = reqUserRecord.lastAccessTime; 550 lastTime > this.appLastTime ? this.appLastTime = lastTime : ''; 551 if (!existing) { 552 let appGroupRecord: AppGroupRecordInfo = new AppGroupRecordInfo( 553 group.name, 554 group.groupName, 555 group.label, 556 group.icon, 557 reqUserRecord.accessCount, 558 lastTime 559 ); 560 this.permissionGroups.push(appGroupRecord); 561 this.groupNames.push(group.groupName); 562 } else { 563 existing.count += reqUserRecord.accessCount; 564 lastTime > existing.lastTime ? existing.lastTime = lastTime : ''; 565 } 566 } 567 }) 568 } 569 570 getInfo(record: privacyManager.BundleUsedRecord, sortFlag: boolean) { 571 bundleManager.getBundleInfo(record.bundleName, bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | 572 bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION).then(async (info): Promise<boolean> => { 573 let appInfo: AppRecordInfo = new AppRecordInfo('', '', '', 0, 0, [], [], [], [], 0); 574 let reqPermissions: Permission[] = []; 575 let groupIds: number[] = []; 576 this.reqUserPermissions = []; 577 this.reqUserRecords = []; 578 this.permissionGroups = []; 579 this.groupNames = []; 580 this.appLastTime = 0; 581 info.reqPermissionDetails.forEach(item => { 582 reqPermissions.push(item.name as Permission); 583 }); 584 await this.getReqUserRecords(record, info); 585 await this.getReqUserPermissions(reqPermissions, info); 586 if (this.reqUserRecords.length == 0) { 587 return false; 588 } 589 this.getPermissionGroups(); 590 for (let i = 0; i < this.reqUserPermissions.length; i++) { 591 let groupId = getGroupIdByPermission(this.reqUserPermissions[i]) 592 if (groupIds.indexOf(groupId) == -1) { 593 groupIds.push(groupId); 594 } 595 } 596 this.allBundleInfo.forEach(bundleInfo => { 597 if (bundleInfo.bundleName === info.name) { 598 appInfo.groupName = bundleInfo.label; 599 appInfo.icon = bundleInfo.icon; 600 } 601 }) 602 appInfo.name = info.name; 603 appInfo.api = info.targetVersion; 604 appInfo.accessTokenId = info.appInfo.accessTokenId; 605 appInfo.reqUserPermissions = this.reqUserPermissions; 606 appInfo.permissions = this.permissionGroups; 607 appInfo.groupNames = this.groupNames; 608 appInfo.groupIds = groupIds; 609 appInfo.appLastTime = this.appLastTime; 610 this.applicationInfos.push(appInfo); 611 if (sortFlag) { 612 let appInfos: AppRecordInfo[] = []; 613 this.applicationInfos.forEach(item => { appInfos.push(item) }); 614 appInfos.sort((a, b) => { return b.appLastTime - a.appLastTime }); 615 this.applicationInfos = appInfos; 616 } 617 return true; 618 }) 619 } 620 621 getAllRecords() { 622 let request: privacyManager.PermissionUsedRequest = { 623 tokenId: 0, 624 isRemote: false, 625 deviceId: '', 626 bundleName: '', 627 permissionNames: [], 628 beginTime: 0, 629 endTime: 0, 630 flag: 1 631 } 632 privacyManager.getPermissionUsedRecord(request).then(records => { 633 this.getRecord(records); 634 }) 635 } 636 637 async getRecord(records: privacyManager.PermissionUsedResponse) { 638 let groupArray: GroupRecordInfo[] = []; 639 let acManager = abilityAccessCtrl.createAtManager(); 640 for (let i = 0; i < records.bundleRecords.length; i++) { 641 let record = records.bundleRecords[i]; 642 Log.info('bundleName: ' + record.bundleName + 'permissionRecords: ' + JSON.stringify(record.permissionRecords)); 643 try { 644 await bundleManager.queryAbilityInfo({ 645 bundleName: record.bundleName, 646 action: 'action.system.home', 647 entities: ['entity.system.home'] 648 }, bundleManager.AbilityFlag.GET_ABILITY_INFO_WITH_APPLICATION); 649 } catch (e) { 650 continue; 651 } 652 this.getInfo(record, (i + 1) == records.bundleRecords.length); 653 for (let j = 0; j < record.permissionRecords.length; j++) { 654 let permissionRecord = record.permissionRecords[j]; 655 try { 656 let reqFlag = await acManager.getPermissionFlags(record.tokenId, permissionRecord.permissionName); 657 if (reqFlag == Constants.PERMISSION_SYSTEM_FIXED) { 658 continue; 659 } 660 } catch (err) { 661 Log.error('getPermissionFlags error: ' + JSON.stringify(err)); 662 } 663 let group = getPermissionGroup(permissionRecord.permissionName as Permission); 664 if (group) { 665 let exist = groupArray.find(permissionGroup => permissionGroup.name == group.name); 666 let lastTime = permissionRecord.lastAccessTime; 667 if (!exist) { 668 let groupRecord: GroupRecordInfo = new GroupRecordInfo( 669 group.name, 670 group.groupName, 671 group.label, 672 group.icon, 673 group.permissions, 674 permissionRecord.accessCount, 675 lastTime); 676 groupArray.push(groupRecord); 677 } else { 678 exist.sum += permissionRecord.accessCount; 679 lastTime > exist.recentVisit ? exist.recentVisit = lastTime : ''; 680 } 681 } 682 } 683 } 684 groupArray.sort((a, b) => { return b.recentVisit - a.recentVisit }); 685 this.groups = groupArray; 686 } 687 688 aboutToAppear() { 689 this.getStrings(); 690 this.getAllRecords(); 691 } 692} 693