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 type AbilityConstant from '@ohos.app.ability.AbilityConstant'; 17import type common from '@ohos.app.ability.common'; 18import UIAbility from '@ohos.app.ability.UIAbility'; 19import type Want from '@ohos.app.ability.Want'; 20import type { BusinessError } from '@ohos.base'; 21import image from '@ohos.multimedia.image'; 22import type resourceManager from '@ohos.resourceManager'; 23import type window from '@ohos.window'; 24import hilog from '@ohos.hilog'; 25import commonEventManager from '@ohos.commonEventManager'; 26 27const DOMAIN_NUMBER: number = 0xFF00; 28const TAG: string = 'EntryAbility'; 29 30export default class EntryAbility extends UIAbility { 31 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 32 let context: common.UIAbilityContext = this.context; // UIAbilityContext 33 // 设置任务快照的名称 34 context.setMissionLabel('test').then(() => { 35 hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in seting mission label.'); 36 }).catch((err: BusinessError) => { 37 hilog.info(DOMAIN_NUMBER, TAG, `Failed to set mission label. Code is ${err.code}, message is ${err.message}`); 38 }); 39 40 // 获取resourceManager资源管理 41 const resourceMgr: resourceManager.ResourceManager = this.context.resourceManager; 42 resourceMgr.getRawFileContent('test.jpg').then((data) => { 43 hilog.info(DOMAIN_NUMBER, TAG, 'data.length = ' + data.byteLength); 44 // 获取图片的ArrayBuffer 45 const imageSource: image.ImageSource = image.createImageSource(data.buffer); 46 imageSource.createPixelMap().then((pixelMap) => { 47 // 设置任务快照的图标 48 context.setMissionIcon(pixelMap, (err) => { 49 if (err.code) { 50 hilog.error(DOMAIN_NUMBER, TAG, `Failed to set mission icon. Code is ${err.code}, message is ${err.message}`); 51 } else { 52 hilog.info(DOMAIN_NUMBER, TAG, 'Success to set mission icon.'); 53 } 54 }) 55 pixelMap.release(); 56 }).catch((error: BusinessError) => { 57 hilog.error(DOMAIN_NUMBER, TAG, 'setMissionIcon failed: ' + JSON.stringify(error)); 58 }); 59 }).catch((error: BusinessError) => { 60 hilog.error(DOMAIN_NUMBER, TAG, 'getRawFileContent failed: ' + JSON.stringify(error)); 61 }); 62 hilog.info(DOMAIN_NUMBER, TAG, 'Ability onCreate'); 63 } 64 65 // onDestroy():void { 66 // commonEventManager.removeStickyCommonEvent('usual.event.SCREEN_OFF', (err: Base.BusinessError) => { 67 // // sticky_event粘性公共事件名 68 // if (err) { 69 // hilog.error(DOMAIN_NUMBER, TAG, `Failed to remove sticky common event. Code is ${err.code}, message is ${err.message}`); 70 // return; 71 // } 72 // hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in removeing sticky event.`); 73 // }); 74 // } 75 76 onWindowStageCreate(windowStage: window.WindowStage): void { 77 // Main window is created, set main page for this ability 78 hilog.info(DOMAIN_NUMBER, TAG, 'Ability onWindowStageCreate'); 79 windowStage.loadContent('pages/Index', (err, data) => { 80 if (err.code) { 81 hilog.error(DOMAIN_NUMBER, TAG, 'Failed to load the content. Cause:', JSON.stringify(err) ?? ''); 82 return; 83 } 84 hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in loading the content. Data:', JSON.stringify(data) ?? ''); 85 }); 86 } 87} 88