1/* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 UIAbility from '@ohos.app.ability.UIAbility'; 17import window from '@ohos.window'; 18import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; 19import type { Permissions } from '@ohos.abilityAccessCtrl'; 20 21import type Want from '@ohos.application.Want'; 22import MediaFileUri from '../media/MediaFileUri'; 23import Logger from '../common/Logger'; 24 25const TAG = 'EntryAbility: '; 26 27let wantInfo = null; 28 29export default class EntryAbility extends UIAbility { 30 private mediaFileUri: MediaFileUri = new MediaFileUri(); 31 32 private fileName: string = ''; 33 private size: number = 0; 34 private content: string = ''; 35 private showFlag: Boolean = false; 36 private windowStage: window.WindowStage; 37 38 private loadFlag: Boolean = false; 39 private loadFileSize: number = 0; 40 private loadFileName: string = ''; 41 private loadFileContent: string = ''; 42 private loadUri: string = ''; 43 44 private storage: LocalStorage = new LocalStorage(); 45 46 onCreate(want, launchParam): void { 47 Logger.info(TAG, 'Ability onCreate'); 48 this.getPermission(want); 49 wantInfo = want; 50 } 51 52 getWantInfo(want: Want): void { 53 if (want === null || want === undefined) { 54 Logger.info(TAG, 'getWantInfo want is null'); 55 return; 56 } 57 58 let srcUri = want.uri; 59 if (srcUri === null || srcUri === undefined) { 60 Logger.info(TAG, 'getWantInfo srcUri is null'); 61 return; 62 } 63 64 let parameters = want.parameters; 65 if (parameters === null || parameters === undefined) { 66 Logger.info(TAG, 'getWantInfo parameters is null'); 67 return; 68 } 69 70 let keyFd = want.parameters.keyFd; 71 if (keyFd === null || keyFd === undefined) { 72 Logger.info(TAG, 'getWantInfo keyFd is null'); 73 return; 74 } 75 76 let size = want.parameters.keyFd.size; 77 if (size === null || size === undefined) { 78 Logger.info(TAG, 'getWantInfo size is null'); 79 return; 80 } 81 82 let name = want.parameters.keyFd.name; 83 if (name === null || name === undefined) { 84 Logger.info(TAG, 'getWantInfo name is null'); 85 return; 86 } 87 88 let content = want.parameters.keyFd.content; 89 if (content === null || content === undefined) { 90 Logger.info(TAG, 'getWantInfo content is null'); 91 return; 92 } 93 94 let fd = want.parameters.keyFd.value; 95 if (fd === null || fd === undefined) { 96 Logger.info(TAG, 'getWantInfo fd is null'); 97 return; 98 } 99 100 this.loadFlag = true; 101 this.loadFileName = want.parameters.keyFd.name; 102 this.loadFileSize = want.parameters.keyFd.size; 103 this.loadFileContent = want.parameters.keyFd.content; 104 this.loadUri = want.uri; 105 106 this.storage.setOrCreate('loadFlag', this.loadFlag); 107 this.storage.setOrCreate('loadUri', this.loadUri); 108 this.storage.setOrCreate('loadFileName', this.loadFileName); 109 this.storage.setOrCreate('loadFileSize', this.loadFileSize); 110 this.storage.setOrCreate('loadFileContent', this.loadFileContent); 111 this.storage.setOrCreate('fd', fd); 112 113 Logger.info(TAG, 'getWantInfo Ability loadFlag: ' + this.loadFlag); 114 Logger.info(TAG, 'getWantInfo Ability loadUri: ' + want.uri); 115 Logger.info(TAG, 'getWantInfo Ability loadFileName: ' + this.loadFileName); 116 Logger.info(TAG, 'getWantInfo Ability loadFileSize: ' + this.loadFileSize); 117 Logger.info(TAG, 'getWantInfo Ability loadFileContent: ' + this.loadFileContent); 118 119 this.loadEditFilePage(); 120 } 121 122 onNewWant(want, launchParam): void { 123 Logger.info(TAG, 'Ability onNewWant'); 124 wantInfo = want; 125 this.getWantInfo(want); 126 } 127 128 onDestroy(): void { 129 Logger.info(TAG, 'Ability onDestroy'); 130 } 131 132 onWindowStageCreate(windowStage: window.WindowStage): void { 133 this.windowStage = windowStage; 134 135 // Main window is created, set main page for this ability 136 Logger.info(TAG, 'Ability onWindowStageCreate'); 137 windowStage.loadContent('pages/Index', (err, data) => { 138 if (err.code) { 139 Logger.error(TAG, 'Failed to load the content. Cause: ' + JSON.stringify(err) ?? ''); 140 return; 141 } 142 Logger.info(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data) ?? ''); 143 }); 144 } 145 146 onWindowStageDestroy(): void { 147 // Main window is destroyed, release UI related resources 148 Logger.info(TAG, 'Ability onWindowStageDestroy'); 149 } 150 151 onForeground(): void { 152 // Ability has brought to foreground 153 Logger.info(TAG, 'Ability onForeground'); 154 } 155 156 onBackground(): void { 157 // Ability has back to background 158 Logger.info(TAG, 'Ability onBackground'); 159 } 160 161 loadEditFilePage(): void { 162 this.windowStage.loadContent('pages/EditFile', this.storage, (err, data) => { 163 if (err.code) { 164 Logger.error(TAG, 'Failed to load the content. Cause: ' + JSON.stringify(err) ?? ''); 165 return; 166 } 167 Logger.info(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data) ?? ''); 168 }); 169 } 170 171 getPermission(want: Want): void { 172 let array: Array<Permissions> = [ 173 'ohos.permission.READ_MEDIA', 174 'ohos.permission.WRITE_MEDIA', 175 'ohos.permission.MEDIA_LOCATION', 176 'ohos.permission.READ_IMAGEVIDEO', 177 'ohos.permission.WRITE_IMAGEVIDEO', 178 ]; 179 let context = this.context; 180 let atManager = abilityAccessCtrl.createAtManager(); 181 // requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗 182 atManager.requestPermissionsFromUser(context, array).then((data) => { 183 Logger.info(TAG, 'data type:' + typeof (data)); 184 Logger.info(TAG, 'data:' + data); 185 Logger.info(TAG, 'data permissions:' + data.permissions); 186 Logger.info(TAG, 'data result:' + data.authResults); 187 188 this.mediaFileUri.getAllFiles(context); 189 Logger.info(TAG, 'getPermission getWantInfo'); 190 this.getWantInfo(want); 191 }).catch((err) => { 192 Logger.error(TAG, 'Failed to start ability' + err.code); 193 }); 194 } 195} 196