1/* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved. 3 */ 4import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 5import { ability, wantConstant } from '@kit.AbilityKit'; 6import bundleManager from '@ohos.bundle.bundleManager'; 7import { DownloadDialog } from './component/dialog/DownloadDialog'; 8import { StartModeOptions } from '../base/model/StartModeOptions'; 9import { FilePickerUtil } from '../base/utils/FilePickerUtil'; 10import Logger from '../base/log/Logger'; 11import { VirtualUri } from '../base/constants/FolderRecord'; 12import { FsUtil } from '../base/utils/FsUtil'; 13import { FileUtil } from '../base/utils/FileUtil'; 14import { BusinessError } from '@kit.BasicServicesKit'; 15import uriPermissionManager from '@ohos.application.uriPermissionManager'; 16import AbilityCommonUtil from '../base/utils/AbilityCommonUtil'; 17 18 19const TAG = 'DownloadAuth'; 20const DOWNLOAD_PATH = '/storage/Users/currentUser/Download'; 21let storage = LocalStorage.getShared(); 22 23@Component 24@Entry(storage) 25struct DownloadAuth { 26 private downloadNewUri: string = ''; 27 private startModeOptions: StartModeOptions = FilePickerUtil.getStartOptionsFromStorage(); 28 private session: UIExtensionContentSession = this.startModeOptions.session; 29 // 存储当前从bms查询到的所有应用的bundle信息 30 public bundleArray: Array<bundleManager.BundleInfo> = []; 31 private appName: string | undefined = storage.get<string>('appName'); 32 private appIcon: string | undefined = storage.get<string>('appIcon'); 33 private confirm: Function = () => { 34 }; 35 // 弹窗关联取消 36 private cancel: Function = () => { 37 }; 38 39 aboutToAppear(): void { 40 Logger.i(TAG, 'DownloadAuth aboutToAppear'); 41 } 42 43 onPageShow() { 44 Logger.i(TAG, 'DownloadAuth onPageShow appName: ' + this.appName + ', appIcon: ' + this.appIcon); 45 this.downloadDialogOpen(); 46 } 47 48 downloadDialogOpen(): void { 49 this.confirm = async () => { 50 this.downloadDialogConfirm(); 51 this.externalDownloadDialog.close(); 52 }; 53 this.cancel = () => { 54 this.externalDownloadDialog.close(); 55 if (this.session != undefined) { 56 this.session.terminateSelf(); 57 } 58 }; 59 this.externalDownloadDialog.open(); 60 } 61 62 async downloadDialogConfirm(): Promise<void> { 63 const bundleName: string = this.startModeOptions.callerBundleName; 64 Logger.i(TAG, 'Download Dialog confirm.'); 65 this.downloadNewUri = VirtualUri.DOWNLOAD + '/' + bundleName; 66 let isExist: boolean = FsUtil.accessSync(DOWNLOAD_PATH + '/' + bundleName); 67 if (!isExist) { 68 this.downloadNewUri = FileUtil.createFolderByFs(VirtualUri.DOWNLOAD, bundleName); 69 } 70 Logger.i(TAG, 'Download Dialog return uri is: ' + this.downloadNewUri); 71 AbilityCommonUtil.grantUriPermission([this.downloadNewUri], bundleName); 72 this.externalDownloadDialog.close(); 73 if (this.session === undefined) { 74 Logger.i(TAG, `this.session is undefined`) 75 return; 76 } 77 Logger.i(TAG, 'Download Dialog exist close.'); 78 let abilityResult: ability.AbilityResult = { 79 resultCode: (this.downloadNewUri === undefined) ? -1 : 0, 80 want: { 81 parameters: { 82 'downloadNewUri': this.downloadNewUri 83 } 84 } 85 }; 86 this.session.terminateSelfWithResult(abilityResult, (error) => { 87 Logger.i(TAG, 'terminateSelfWithResult is called = ' + error?.code); 88 }); 89 } 90 91 build() { 92 } 93 94 //外部调用下载弹窗 95 externalDownloadDialog: CustomDialogController = new CustomDialogController({ 96 builder: DownloadDialog({ 97 appName: this.appName, 98 appIcon: this.appIcon, 99 confirm: this.confirm, 100 cancel: this.cancel 101 }), 102 autoCancel: false, 103 customStyle: true, 104 alignment: DialogAlignment.Center, // 可设置dialog的对齐方式,设定显示在底部或中间等,默认为底部显示 105 }) 106}