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 abilityAccessCtrl ,{Permissions}from '@ohos.abilityAccessCtrl' 17import promptAction from '@ohos.promptAction' 18import common from '@ohos.app.ability.common' 19import bundleManager from '@ohos.bundle.bundleManager' 20import Logger from '../../model/Logger' 21 22const TAG = 'VerifyIndex' 23 24@Entry 25@Component 26struct Index { 27 @StorageProp('bundleName') bundleName: string = '' 28 @StorageProp('permission') permission: string = '' 29 @State tokenID: number = 0 30 private cancel: string = '' 31 private confirm: string = '' 32 private ctx: common.UIAbilityContext = getContext(this) as common.UIAbilityContext 33 34 async aboutToAppear() { 35 let resource = await this.ctx.resourceManager 36 this.cancel = resource.getStringSync($r('app.string.cancel').id) 37 this.confirm = resource.getStringSync($r('app.string.confirm').id) 38 let bundleFlag = 0 39 let appInfo = await bundleManager.getApplicationInfo(this.bundleName, bundleFlag) 40 this.tokenID = appInfo.accessTokenId 41 this.grantUserPermissions(this.permission as Permissions) 42 } 43 44 async grantUserPermissions(grantPermissions: Permissions) { 45 let atManager = abilityAccessCtrl.createAtManager() 46 let result = await promptAction.showDialog({ 47 title: $r('app.string.title'), 48 message: grantPermissions, 49 buttons: [ 50 { 51 text: this.cancel, 52 color: $r('app.color.cancel'), 53 }, 54 { 55 text: this.confirm, 56 color: $r('app.color.confirm'), 57 } 58 ] 59 }) 60 if (result.index === 0) { 61 this.ctx.terminateSelf() 62 } else { 63 let permissionFlag = 2 64 try { 65 await atManager.grantUserGrantedPermission(this.tokenID, grantPermissions, permissionFlag) 66 this.ctx.terminateSelf() 67 } catch (err) { 68 Logger.info(`err ${JSON.stringify(err)}`) 69 } 70 } 71 } 72 73 build() { 74 Row() { 75 Column() { 76 } 77 .width('100%') 78 } 79 .height('100%') 80 } 81}