• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Logger from '../utils/Logger';
25
26const TAG: string = 'EntryAbility';
27
28export default class EntryAbility extends UIAbility {
29  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
30    let context: common.UIAbilityContext = this.context; // UIAbilityContext
31    // 设置任务快照的名称
32    context.setMissionLabel('test').then(() => {
33      Logger.info(TAG, 'Succeeded in seting mission label.');
34    }).catch((err: BusinessError) => {
35      Logger.error(TAG, `Failed to set mission label. Code is ${err.code}, message is ${err.message}`);
36    });
37
38    // 获取resourceManager资源管理
39    const resourceMgr: resourceManager.ResourceManager = this.context.resourceManager;
40    resourceMgr.getRawFileContent('test.jpg').then((data) => {
41      Logger.info(TAG, 'data.length = ' + data.byteLength);
42      // 获取图片的ArrayBuffer
43      const imageSource: image.ImageSource = image.createImageSource(data.buffer);
44      imageSource.createPixelMap().then((pixelMap) => {
45        // 设置任务快照的图标
46        context.setMissionIcon(pixelMap, (err) => {
47          if (err.code) {
48            Logger.error(TAG, `Failed to set mission icon. Code is ${err.code}, message is ${err.message}`);
49          } else {
50            Logger.info(TAG, 'Success to set mission icon.');
51          }
52        })
53        pixelMap.release();
54      }).catch((error: BusinessError) => {
55        Logger.error(TAG, 'setMissionIcon failed: ' + JSON.stringify(error));
56      });
57    }).catch((error: BusinessError) => {
58      Logger.error(TAG, 'getRawFileContent failed: ' + JSON.stringify(error));
59    });
60    Logger.info(TAG, 'Ability onCreate');
61  }
62
63  onWindowStageCreate(windowStage: window.WindowStage): void {
64    // Main window is created, set main page for this ability
65    Logger.info(TAG, 'Ability onWindowStageCreate');
66    windowStage.loadContent('pages/Index', (err, data) => {
67      if (err.code) {
68        Logger.error(TAG, 'Failed to load the content. Cause:', JSON.stringify(err) ?? '');
69        return;
70      }
71      Logger.info(TAG, 'Succeeded in loading the content. Data:', JSON.stringify(data) ?? '');
72    });
73  }
74}
75