1/* 2 * Copyright (c) 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 Logger from '../model/Logger' 17import UIAbility from '@ohos.app.ability.UIAbility' 18import Window from '@ohos.window' 19import prompt from '@ohos.promptAction' 20import abilityAccessCtrl from '@ohos.abilityAccessCtrl' 21import type { Permissions } from '@ohos.abilityAccessCtrl' 22 23const TAG: string = '[Index]' 24const PERMISSIONS: Array<Permissions> = ['ohos.permission.MICROPHONE', 'ohos.permission.WRITE_MEDIA', 'ohos.permission.READ_MEDIA'] 25 26export default class EntryAbility extends UIAbility { 27 async onCreate(want, launchParam) { 28 } 29 30 onDestroy() { 31 Logger.info(TAG, `Ability onDestroy`) 32 } 33 34 async onWindowStageCreate(windowStage: Window.WindowStage): Promise<void> { 35 // Main window is created, set main page for this ability 36 Logger.info(TAG, 'Ability onWindowStageCreate'); 37 let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 38 let authResults = await atManager.requestPermissionsFromUser(this.context, PERMISSIONS); 39 if (authResults.authResults.includes(-1)) { 40 return; 41 } 42 prompt.showToast({ 43 message: 'requestPermissionsFromUser success' 44 }); 45 windowStage.loadContent('pages/Index', (err, data) => { 46 if (err.code) { 47 Logger.error(TAG, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 48 return 49 } 50 Logger.info(TAG, 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 51 }) 52 } 53 54 onWindowStageDestroy() { 55 // Main window is destroyed, release UI related resources 56 Logger.info(TAG, 'Ability onWindowStageDestroy') 57 } 58 59 onForeground() { 60 // Ability has brought to foreground 61 Logger.info(TAG, 'Ability onForeground') 62 } 63 64 onBackground() { 65 // Ability has back to background 66 Logger.info(TAG, 'Ability onBackground') 67 } 68} 69