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 UIAbility from '@ohos.app.ability.UIAbility' 17import window from '@ohos.window' 18import AbilityCommonUtil from '../base/utils/AbilityCommonUtil' 19import Logger from '../base/log/Logger' 20import { FileUtil } from '../base/utils/FileUtil' 21 22const TAG = 'MainAbility' 23 24export default class MainAbility extends UIAbility { 25 onCreate(want, launchParam) { 26 Logger.i(TAG, 'onCreate') 27 globalThis.action = want.action; 28 globalThis.abilityContext = this.context; 29 globalThis.startMode = want.parameters.startMode; 30 globalThis.saveFile = want.parameters.saveFile ? [want.parameters.saveFile] : []; 31 Logger.i(TAG, 'globalThis.startMode: ' + globalThis.startMode + ', ' + (globalThis.startMode == 'choose')); 32 const parameters = want.parameters 33 if (globalThis.action == 'ohos.want.action.OPEN_FILE' || globalThis.startMode == 'choose') { 34 // 文件选择器 35 // 选择文件的类型列表 36 globalThis.keyPickTypeList = AbilityCommonUtil.getKeyPickTypeList(parameters.key_pick_type, parameters.key_pick_type_list); 37 // 选择文件个数 38 globalThis.filePickNum = AbilityCommonUtil.getPickFileNum(parameters.key_pick_num); 39 // 文件选择范围:0-本地 1-云盘 不传则默认展示全部路径(未实现) 40 globalThis.filePickLocation = parameters.key_pick_location; 41 // 选择指定目录下的文件 42 globalThis.keyFileDefaultPickPath = FileUtil.getUriPath(parameters.key_pick_dir_path); 43 // 选择文件模式,默认只支持文件选择 44 globalThis.keySelectMode = AbilityCommonUtil.getKeySelectMode(parameters.key_select_mode); 45 // 指定文件后缀,string[] 46 if (parameters.key_file_suffix_filter instanceof Array) { 47 globalThis.keyFileSuffixFilter = AbilityCommonUtil.getKeyFileSuffixFilter(parameters.key_file_suffix_filter); 48 } 49 globalThis.pickerCallerUid = parameters[AbilityCommonUtil.CALLER_UID]; 50 globalThis.pickerCallerBundleName = parameters[AbilityCommonUtil.CALLER_BUNDLE_NAME] 51 globalThis.filePickerViewFlag = true; 52 } else { 53 // 路径选择器 54 globalThis.pathAbilityContext = this.context 55 // 保存文件时的文件名列表 56 globalThis.keyPickFileName = parameters.key_pick_file_name || globalThis.saveFile || [] 57 // 保存位置,[本地、云盘](未实现) 58 globalThis.keyPickFileLocation = parameters.key_pick_file_location 59 // 保存云盘文件到本地时云盘文件的uri列表(未实现) 60 globalThis.keyPickFilePaths = parameters.key_pick_file_paths 61 globalThis.keyPathDefaultPickDir = FileUtil.getUriPath(parameters.key_pick_dir_path); 62 // 指定文件后缀,只获取第一个有效的后缀 63 if (parameters.key_file_suffix_choices instanceof Array) { 64 globalThis.keyFileSuffixChoices = AbilityCommonUtil.getKeyFileSuffixChoices(parameters.key_file_suffix_choices); 65 } 66 globalThis.pathCallerUid = parameters[AbilityCommonUtil.CALLER_UID] 67 globalThis.pathCallerBundleName = parameters[AbilityCommonUtil.CALLER_BUNDLE_NAME] 68 } 69 Logger.i(TAG, ' onCreate, parameters: ' + JSON.stringify(want.parameters)) 70 } 71 72 onDestroy() { 73 Logger.i(TAG, 'onDestroy') 74 AbilityCommonUtil.releaseMediaLibrary() 75 } 76 77 onWindowStageCreate(windowStage: window.WindowStage) { 78 // Main window is created, set main page for this ability 79 Logger.i(TAG, 'onWindowStageCreate') 80 AbilityCommonUtil.init().then(() => { 81 globalThis.windowClass = windowStage.getMainWindowSync(); 82 if (globalThis.action == 'ohos.want.action.OPEN_FILE' || globalThis.startMode == 'choose') { 83 // 文件选择器 84 windowStage.loadContent('pages/browser/storage/MyPhone', (err, data) => { 85 if (err.code) { 86 Logger.e(TAG, 'Failed to load the content: ' + JSON.stringify(err)); 87 return 88 } 89 Logger.i(TAG, 'data: ' + JSON.stringify(data)); 90 }) 91 } else { 92 // 路径选择器 93 windowStage.loadContent('pages/PathPicker', (err, data) => { 94 if (err.code) { 95 Logger.e(TAG, 'Failed to load the content. Cause: ' + JSON.stringify(err)) 96 return; 97 } 98 Logger.i(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data)) 99 }) 100 } 101 }) 102 103 } 104 105 onWindowStageDestroy() { 106 // Main window is destroyed, release UI related resources 107 Logger.i(TAG, 'onWindowStageDestroy') 108 } 109 110 onForeground() { 111 // Ability has brought to foreground 112 Logger.i(TAG, 'onForeground') 113 } 114 115 onBackground() { 116 // Ability has back to background 117 Logger.i(TAG, 'onBackground') 118 } 119} 120