• 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 insightIntentDriver from '@ohos.app.ability.insightIntentDriver';
17import insightIntent from '@ohos.app.ability.insightIntent';
18import { logger } from '../util/Logger';
19import { BusinessError } from '@ohos.base';
20
21const TAG: string = '[PlayMusicIntentDriver]';
22
23class PlayMusicIntentDriver {
24  public executeResult: insightIntent.ExecuteResult = { code: 0 };
25  executeUIAbilityForeground = (): void => {
26    let intentParam: Record<string, Object> = {
27      'songName': 'City Of Stars',
28    };
29    let param: insightIntentDriver.ExecuteParam = {
30      bundleName: 'com.samples.intentexecutor',
31      moduleName: 'entry',
32      abilityName: 'EntryAbility',
33      insightIntentName: 'PlayMusic',
34      insightIntentParam: intentParam,
35      executeMode: insightIntent.ExecuteMode.UI_ABILITY_FOREGROUND,
36    };
37
38    try {
39      insightIntentDriver.execute(param, (error: BusinessError, data: insightIntent.ExecuteResult) => {
40        if (error) {
41          logger.info(TAG, `executeUIAbilityForeground failed with ${JSON.stringify(error)}`);
42        } else {
43          logger.info(TAG, 'executeUIAbilityForeground succeed');
44        }
45        logger.info(TAG, `executeUIAbilityForeground result ${JSON.stringify(data)}`);
46        this.executeResult = data;
47      });
48    } catch (error) {
49      logger.info(TAG, `executeUIAbilityForeground error caught ${JSON.stringify(error)}`);
50      this.executeResult = {
51        code: -1,
52        result: { 'error': JSON.stringify(error) }
53      }
54    }
55  }
56  executeServiceExtension = (): void => {
57    let intentParam: Record<string, Object> = {
58      'songName': 'City Of Stars',
59    };
60    let param: insightIntentDriver.ExecuteParam = {
61      bundleName: 'com.samples.intentdriver',
62      moduleName: 'entry',
63      abilityName: 'ServiceExtAbility',
64      insightIntentName: 'PlayMusic',
65      insightIntentParam: intentParam,
66      executeMode: insightIntent.ExecuteMode.SERVICE_EXTENSION_ABILITY,
67    };
68
69    try {
70      insightIntentDriver.execute(param, (error: BusinessError, data: insightIntent.ExecuteResult) => {
71        if (error) {
72          logger.info(TAG, `executeServiceExtension failed with ${JSON.stringify(error)}`);
73        } else {
74          logger.info(TAG, 'executeServiceExtension succeed');
75        }
76        logger.info(TAG, `executeServiceExtension result ${JSON.stringify(data)}`);
77        this.executeResult = data;
78      })
79    } catch (error) {
80      logger.info(TAG, `executeServiceExtension error caught ${JSON.stringify(error)}`);
81      this.executeResult = {
82        code: -1,
83        result: { 'error': JSON.stringify(error) }
84      }
85    }
86  }
87}
88
89export let playMusicIntentDriver = new PlayMusicIntentDriver();