1/* 2 * Copyright (c) 2021 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 { Callback } from 'basic'; 18import { logInfo, logError } from '../../../../../../common/src/main/ets/components/Utils/LogUtils'; 19 20const TAG: string = 'MainAbility' 21 22export default class MainAbility extends UIAbility { 23 onCreate(want, launchParam) { 24 logInfo(TAG, "onCreate") 25 globalThis.abilityWant = want; 26 27 globalThis.startMode = want.parameters.startMode 28 globalThis.saveFile = want.parameters.saveFile 29 globalThis.debugMode = want.parameters.debugMode 30 logInfo(TAG, `parameters ${JSON.stringify(want.parameters)}`) 31 } 32 33 onDestroy() { 34 logInfo(TAG, "onDestroy") 35 } 36 37 onWindowStageCreate(windowStage) { 38 // Main window is created, set main page for this ability 39 logInfo(TAG, "onWindowStageCreate") 40 41 globalThis.context = this.context 42 this.requestPermissions(() => this.displayWindow(windowStage)) 43 } 44 45 onWindowStageDestroy() { 46 // Main window is destroyed, release UI related resources 47 logInfo(TAG, "onWindowStageDestroy") 48 } 49 50 onForeground() { 51 // Ability has brought to foreground 52 logInfo(TAG, "onForeground") 53 } 54 55 onBackground() { 56 // Ability has back to background 57 logInfo(TAG, "onBackground") 58 } 59 60 private requestPermissions(callback: Callback<void>) { 61 let permissionList: Array<string> = [ 62 "ohos.permission.MEDIA_LOCATION", 63 "ohos.permission.READ_MEDIA", 64 "ohos.permission.WRITE_MEDIA" 65 ] 66 globalThis.context.requestPermissionsFromUser(permissionList).then(function (data) { 67 logInfo(TAG, `request permission data result = ${data.authResults}`) 68 callback() 69 }, (error) => { 70 logError(TAG, `fail to request permission error code = ${error.code}`) 71 }) 72 } 73 74 private displayWindow(windowStage) { 75 logInfo(TAG, "displayWindow()") 76 windowStage.setUIContent(this.context, "pages/index", null) 77 windowStage.getMainWindow().then(win => { 78 logInfo(TAG, "windowStage.getMainWindow()") 79 win.resetSize(vp2px(752), vp2px(450)) 80 }) 81 } 82}; 83