• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.InsightIntentEntryExecutor (Base Class for Intent Execution of @InsightIntentEntry)
2
3The module provides the base class for implementing the execution of intents decorated with [@InsightIntentEntry](js-apis-app-ability-InsightIntentDecorator.md#insightintententry). It must be used together with the @InsightIntentEntry decorator.
4
5You are required to implement the [onExecute()](#onexecute) callback for intent execution in the child class that inherits from this base class and use the @InsightIntentEntry decorator to decorate the child class.
6
7> **NOTE**
8>
9> The initial APIs of this module are supported since API version 20. Newly added APIs will be marked with a superscript to indicate their earliest API version.
10>
11> The APIs of this module can be used only in the stage model.
12
13## Modules to Import
14
15```ts
16import { InsightIntentEntryExecutor } from '@kit.AbilityKit';
17```
18
19## InsightIntentEntryExecutor\<T>
20
21### Properties
22
23**System capability**: SystemCapability.Ability.AbilityRuntime.Core
24
25**Atomic service API**: This API can be used in atomic services since API version 20.
26
27| Name| Type| Read-Only| Optional| Description|
28| -------- | -------- | -------- | -------- | -------- |
29| executeMode | [insightIntent.ExecuteMode](./js-apis-app-ability-insightIntent.md#executemode) | No| No| Intent execution mode, that is, execution mode supported when the bound ability is started.|
30| context | [InsightIntentContext](./js-apis-app-ability-insightIntentContext.md) | No| No| Context for intent execution.|
31| windowStage | [window.WindowStage](../apis-arkui/arkts-apis-window-WindowStage.md) | No| Yes| WindowStage instance, which is the same as the WindowStage instance in the [onWindowStageCreate](./js-apis-app-ability-uiAbility.md#onwindowstagecreate) API and can be used to load the page for intent execution. It is valid only when **executeMode** is set to **UI_ABILITY_FOREGROUND** (the UIAbility needs to be displayed in the foreground during intent execution).|
32| uiExtensionSession | [UIExtensionContentSession](./js-apis-app-ability-uiExtensionContentSession.md) | No| Yes| UIExtensionContentSession instance, which is the same as the UIExtensionContentSession instance in the [onSessionCreate](./js-apis-app-ability-uiExtensionAbility.md#onsessioncreate) API and can be used to load the page for intent execution. It takes effect only when **executeMode** is set to **UI_EXTENSION_ABILITY** (the UIExtensionAbility needs to be started during intent execution).|
33
34### onExecute
35
36onExecute(): Promise\<insightIntent.IntentResult\<T>>
37
38 When the AI entry triggers intent execution, the system starts the ability bound to this class and triggers this callback. You can implement the required intent operations in this callback. This API uses a promise to return the result.
39
40The following table describes the relationship between the callback and the intent execution mode.
41
42| Intent Execution Mode| API Calling Timing and Sequence|
43| ----------- | -----------------|
44| [UI_ABILITY_FOREGROUND](./js-apis-app-ability-insightIntent.md#executemode)<br>Foreground mode of the UIAbility| - If the UIAbility is cold started, the UIAbility lifecycle callbacks are triggered in the following sequence during intent execution: [onCreate](./js-apis-app-ability-uiAbility.md#oncreate), [onWindowStageCreate](./js-apis-app-ability-uiAbility.md#onwindowstagecreate), onExecute, and [onForeground](./js-apis-app-ability-uiAbility.md#onforeground).<br>- If the UIAbility is hot started in the background, the UIAbility lifecycle callbacks are triggered in the following sequence during intent execution: [onNewWant](./js-apis-app-ability-uiAbility.md#onnewwant), onExecute, and [onForeground](./js-apis-app-ability-uiAbility.md#onforeground).<br>- If the UIAbility is hot started in the foreground, the UIAbility lifecycle callbacks are triggered in the following sequence during intent execution: onExecute.|
45| [UI_ABILITY_BACKGROUND](./js-apis-app-ability-insightIntent.md#executemode)<br>Background mode of the UIAbility| - If the UIAbility is cold started, the UIAbility lifecycle callbacks are triggered in the following sequence during intent execution: [onCreate](./js-apis-app-ability-uiAbility.md#oncreate), onExecute, and [onBackground](./js-apis-app-ability-uiAbility.md#onbackground).<br>- If the UIAbility is hot started, the UIAbility lifecycle callbacks are triggered in the following sequence during intent execution: onExecute.|
46| [UI_EXTENSION_ABILITY](./js-apis-app-ability-insightIntent.md#executemode)<br>UIExtension mode| The UIExtensionAbility lifecycle callbacks are triggered in the following sequence during intent execution: [onCreate](./js-apis-app-ability-uiExtensionAbility.md#oncreate), [onSessionCreate](./js-apis-app-ability-uiExtensionAbility.md#onsessioncreate), onExecute, and [onForeground](./js-apis-app-ability-uiExtensionAbility.md#onforeground).|
47|<!--DelRow-->[SERVICE_EXTENSION_ABILITY](./js-apis-app-ability-insightIntent-sys.md)<br>ServiceExtension mode| The ServiceExtensionAbility lifecycle callbacks are triggered in the following sequence during intent execution: [onCreate](./js-apis-app-ability-serviceExtensionAbility-sys.md#oncreate), [onRequest](./js-apis-app-ability-serviceExtensionAbility-sys.md#onrequest), and onExecute.|
48
49**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
50
51**Atomic service API**: This API can be used in atomic services since API version 20.
52
53**Return value**
54
55| Type| Description|
56|------|-----|
57| Promise\<insightIntent.IntentResult\<T>>| Promise used to return an [insightIntent.IntentResult\<T>](js-apis-app-ability-insightIntent.md#intentresultt20) object, indicating the intent execution result.|
58
59**Example**
60
61```ts
62import { insightIntent, InsightIntentEntry, InsightIntentEntryExecutor } from '@kit.AbilityKit';
63import { hilog } from '@kit.PerformanceAnalysisKit';
64
65const LOG_TAG: string = 'testTag-EntryIntent';
66
67// Use the @InsightIntentEntry decorator to define an intent.
68@InsightIntentEntry({
69  intentName: 'PlayMusic',
70  domain: 'MusicDomain',
71  intentVersion: '1.0.1',
72  displayName: 'Play Music',
73  displayDescription: 'Intent to play music',
74  icon: $r('app.media.app_icon'),
75  llmDescription: 'Supports passing song names to play music',
76  keywords: ['music playback', 'play music', 'PlayMusic'],
77  abilityName: 'EntryAbility',
78  executeMode: [insightIntent.ExecuteMode.UI_ABILITY_FOREGROUND],
79  parameters: {
80    'schema': 'http://json-schema.org/draft-07/schema#',
81    'type': 'object',
82    'title': 'Song Schema',
83    'description': 'A schema for describing songs and their artists',
84    'properties': {
85      'songName': {
86        'type': 'string',
87        'description': 'The name of the song',
88        'minLength': 1
89      }
90    },
91    'required': ['songName']
92  }
93})
94export default class PlayMusicDemo extends InsightIntentEntryExecutor<string> {
95  songName: string = '';
96
97  onExecute(): Promise<insightIntent.IntentResult<string>> {
98    hilog.info(0x0000, LOG_TAG, 'PlayMusicDemo executeMode %{public}s', JSON.stringify(this.executeMode));
99    hilog.info(0x0000, LOG_TAG, '%{public}s', JSON.stringify(this));
100    let storage = new LocalStorage();
101    storage.setOrCreate('songName', this.songName);
102    // Start the PlayMusicPage page based on the executeMode parameter.
103    if (this.executeMode == insightIntent.ExecuteMode.UI_ABILITY_FOREGROUND) {
104      this.windowStage?.loadContent('pages/PlayMusicPage', storage);
105    } else if (this.executeMode == insightIntent.ExecuteMode.UI_EXTENSION_ABILITY) {
106      this.uiExtensionSession?.loadContent('pages/PlayMusicPage', storage);
107    }
108    // Define the intent execution result.
109    let result: insightIntent.IntentResult<string> = {
110      code: 123,
111      result: 'result'
112    }
113    hilog.info(0x0000, LOG_TAG, 'PlayMusicDemo return %{public}s', JSON.stringify(result));
114    // Return the intent execution result in Promise mode.
115    return Promise.reject(result);
116  }
117}
118```
119