• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.InsightIntentExecutor (Base Class for InsightIntent Call Execution)
2
3The **InsightIntentExecutor** module provides the base class for InsightIntent call execution. Through this base class, you can access the InsightIntent framework on the device side. You need to implement the service logic to respond to InsightIntent calls. To access the InsightIntent framework, you need to declare the InsightIntent name and InsightIntent access mode in the InsightIntent configuration file. The system calls the InsightIntent based on the user interaction and InsightIntent configuration file and triggers the corresponding InsightIntent call execution callback.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import InsightIntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
13```
14
15## Attributes
16
17**Model restriction**: This API can be used only in the stage model.
18
19**System capability**: SystemCapability.Ability.AbilityRuntime.Core
20
21| Name| Type| Readable| Writable| Description|
22| -------- | -------- | -------- | -------- | -------- |
23| context | [InsightIntentContext](js-apis-app-ability-insightIntentContext.md) | Yes| No| InsightIntent call execution context.|
24
25## InsightIntentExecutor.onExecuteInUIAbilityForegroundMode
26
27onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage):
28  insightIntent.ExecuteResult | Promise<insightIntent.ExecuteResult>
29
30Called when the InsightIntent call displays a UIAbility in the foreground. Both synchronous calls and asynchronous calls using Promise are supported.
31
32**Model restriction**: This API can be used only in the stage model.
33
34**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
35
36**Parameters**
37
38| Name| Type| Mandatory| Description|
39| -------- | -------- | -------- | -------- |
40| name | string | Yes| InsightIntent name.|
41| param | Record<string, Object> | Yes| InsightIntent call parameter.|
42| pageLoader | [window.WindowStage](js-apis-window.md#windowstage9) | Yes| Page loader.|
43
44**Return value**
45
46| Type| Description|
47| -------- | -------- |
48| [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) | InsightIntent call execution result.|
49| Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Promise used to return the InsightIntent call execution result.|
50
51**Example**
52
53The code snippet below shows the synchronous call that returns the InsightIntent call result:
54  ```ts
55  import IntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
56  import insightIntent from '@ohos.app.ability.insightIntent';
57  import window from '@ohos.window';
58  import hilog from '@ohos.hilog';
59
60  export default class IntentExecutorImpl extends IntentExecutor {
61    onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage): insightIntent.ExecuteResult {
62      let result: insightIntent.ExecuteResult;
63      if (name !== 'SupportedInsightIntentName') {
64        hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name);
65        result = {
66          // decided by developer
67          code: 404,
68          result: {
69            message: 'Unsupported insight intent.',
70          }
71        };
72        return result;
73      }
74
75      // if developer need load content
76      pageLoader.loadContent('pages/Index', (err, data) => {
77        if (err.code) {
78          hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
79        } else {
80          hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in loading the content');
81        }
82      });
83
84      result = {
85        code: 0,
86        result: {
87          message: 'Execute insight intent succeed.',
88        }
89      };
90      return result;
91    }
92  }
93  ```
94
95The code snippet below shows the promise-based asynchronous call that returns the InsightIntent call result:
96  ```ts
97  import IntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
98  import insightIntent from '@ohos.app.ability.insightIntent';
99  import window from '@ohos.window';
100  import hilog from '@ohos.hilog';
101
102  async function executeInsightIntent(param: Record<string, Object>): Promise<insightIntent.ExecuteResult> {
103    return new Promise((resolve, reject) => {
104      let result: insightIntent.ExecuteResult = {
105        code: 0,
106        result: {
107          message: 'Execute insight intent succeed.',
108        }
109      };
110      resolve(result);
111    })
112  }
113
114  export default class IntentExecutorImpl extends IntentExecutor {
115    async onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage): Promise<insightIntent.ExecuteResult> {
116      let result: insightIntent.ExecuteResult;
117      if (name !== 'SupportedInsightIntentName') {
118        hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name);
119        result = {
120          // decided by developer
121          code: 404,
122          result: {
123            message: 'Unsupported insight intent.',
124          }
125        };
126        return result;
127      }
128
129      result = await executeInsightIntent(param);
130      return result;
131    }
132  }
133  ```
134
135## InsightIntentExecutor.onExecuteInUIAbilityBackgroundMode
136
137onExecuteInUIAbilityBackgroundMode(name: string, param: Record<string, Object>):
138    insightIntent.ExecuteResult | Promise<insightIntent.ExecuteResult>
139
140Called when the InsightIntent call displays a UIAbility in the background. Both synchronous calls and asynchronous calls using Promise are supported.
141
142**Model restriction**: This API can be used only in the stage model.
143
144**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
145
146**Parameters**
147
148| Name| Type| Mandatory| Description|
149| -------- | -------- | -------- | -------- |
150| name | string | Yes| InsightIntent name.|
151| param | Record<string, Object> | Yes| InsightIntent call parameter.|
152
153**Return value**
154
155| Type| Description|
156| -------- | -------- |
157| [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) | InsightIntent call execution result.|
158| Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Promise used to return the InsightIntent call execution result.|
159
160**Example**
161
162The code snippet below shows the synchronous call that returns the InsightIntent call result:
163  ```ts
164  import IntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
165  import insightIntent from '@ohos.app.ability.insightIntent';
166
167  export default class IntentExecutorImpl extends IntentExecutor {
168    onExecuteInUIAbilityBackgroundMode(name: string, param: Record<string, Object>): insightIntent.ExecuteResult {
169      let result: insightIntent.ExecuteResult = {
170        code: 0,
171        result: {
172          message: 'Execute insight intent succeed.',
173        }
174      };
175      return result;
176    }
177  }
178  ```
179
180The code snippet below shows the promise-based asynchronous call that returns the InsightIntent call result:
181  ```ts
182  import IntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
183  import insightIntent from '@ohos.app.ability.insightIntent';
184
185  async function executeInsightIntent(param: Record<string, Object>): Promise<insightIntent.ExecuteResult> {
186    return new Promise((resolve, reject) => {
187      let result: insightIntent.ExecuteResult = {
188        code: 0,
189        result: {
190          message: 'Execute insight intent succeed.',
191        }
192      };
193      resolve(result);
194    })
195  }
196
197  export default class IntentExecutorImpl extends IntentExecutor {
198    async onExecuteInUIAbilityBackgroundMode(name: string, param: Record<string, Object>): Promise<insightIntent.ExecuteResult> {
199      let result: insightIntent.ExecuteResult = await executeInsightIntent(param);
200      return result;
201    }
202  }
203  ```
204
205## InsightIntentExecutor.onExecuteInUIExtensionAbility
206
207onExecuteInUIExtensionAbility(name: string, param: Record<string, Object>, pageLoader: UIExtensionContentSession):
208  insightIntent.ExecuteResult | Promise<insightIntent.ExecuteResult>
209
210Called when the InsightIntent call starts a UIExtensionAbility. Both synchronous calls and asynchronous calls using Promise are supported.
211
212**Model restriction**: This API can be used only in the stage model.
213
214**System capability**: SystemCapability.Ability.AbilityRuntime.Core
215
216**Parameters**
217
218| Name| Type| Mandatory| Description|
219| -------- | -------- | -------- | -------- |
220| name | string | Yes| InsightIntent name.|
221| param | Record<string, Object> | Yes| InsightIntent call parameter.|
222| pageLoader | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| Page loader.|
223
224**Return value**
225
226| Type| Description|
227| -------- | -------- |
228| [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) | InsightIntent call execution result.|
229| Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Promise used to return the InsightIntent call execution result.|
230
231**Example**
232
233The code snippet below shows the synchronous call that returns the InsightIntent call result:
234  ```ts
235  import IntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
236  import insightIntent from '@ohos.app.ability.insightIntent';
237  import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
238  import hilog from '@ohos.hilog';
239
240  export default class IntentExecutorImpl extends IntentExecutor {
241    onExecuteInUIExtensionAbility(name: string, param: Record<string, Object>, pageLoader: UIExtensionContentSession): insightIntent.ExecuteResult {
242      let result: insightIntent.ExecuteResult;
243      if (name !== 'SupportedInsightIntentName') {
244        hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name);
245        result = {
246          // decided by developer
247          code: 404,
248          result: {
249            message: 'Unsupported insight intent.',
250          }
251        };
252        return result;
253      }
254
255      // if developer need load content
256      pageLoader.loadContent('pages/Index');
257
258      result = {
259        code: 0,
260        result: {
261          message: 'Execute insight intent succeed.',
262        }
263      };
264      return result;
265    }
266  }
267  ```
268
269The code snippet below shows the promise-based asynchronous call that returns the InsightIntent call result:
270  ```ts
271  import IntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
272  import insightIntent from '@ohos.app.ability.insightIntent';
273  import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
274  import hilog from '@ohos.hilog';
275
276  async function executeInsightIntent(param: Record<string, Object>): Promise<insightIntent.ExecuteResult> {
277    return new Promise((resolve, reject) => {
278      let result: insightIntent.ExecuteResult = {
279        code: 0,
280        result: {
281          message: 'Execute insight intent succeed.',
282        }
283      };
284      resolve(result);
285    })
286  }
287
288  export default class IntentExecutorImpl extends IntentExecutor {
289    async onExecuteInUIExtensionAbility(name: string, param: Record<string, Object>, pageLoader: UIExtensionContentSession): Promise<insightIntent.ExecuteResult> {
290      let result: insightIntent.ExecuteResult;
291      if (name !== 'SupportedInsightIntentName') {
292        hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name);
293        result = {
294          // decided by developer
295          code: 404,
296          result: {
297            message: 'Unsupported insight intent.',
298          }
299        };
300        return result;
301      }
302
303      result = await executeInsightIntent(param);
304      return result;
305    }
306  }
307  ```
308
309## InsightIntentExecutor.onExecuteInServiceExtensionAbility
310
311onExecuteInServiceExtensionAbility(name: string, param: Record<string, Object>):
312    insightIntent.ExecuteResult | Promise<insightIntent.ExecuteResult>
313
314Called when the InsightIntent call starts a ServiceExtensionAbility. Both synchronous calls and asynchronous calls using Promise are supported.
315
316**Model restriction**: This API can be used only in the stage model.
317
318**System capability**: SystemCapability.Ability.AbilityRuntime.Core
319
320**Parameters**
321
322| Name| Type| Mandatory| Description|
323| -------- | -------- | -------- | -------- |
324| name | string | Yes| InsightIntent name.|
325| param | Record<string, Object> | Yes| InsightIntent call parameter.|
326
327**Return value**
328
329| Type| Description|
330| -------- | -------- |
331| [insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult) | InsightIntent call execution result.|
332| Promise<[insightIntent.ExecuteResult](js-apis-app-ability-insightIntent.md#executeresult)> | Promise used to return the InsightIntent call execution result.|
333
334**Example**
335
336The code snippet below shows the synchronous call that returns the InsightIntent call result:
337  ```ts
338  import IntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
339  import insightIntent from '@ohos.app.ability.insightIntent';
340  import hilog from '@ohos.hilog';
341
342  export default class IntentExecutorImpl extends IntentExecutor {
343    onExecuteInServiceExtensionAbility(name: string, param: Record<string, Object>): insightIntent.ExecuteResult {
344      let result: insightIntent.ExecuteResult;
345      if (name !== 'SupportedInsightIntentName') {
346        hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name);
347        result = {
348          // decided by developer
349          code: 404,
350          result: {
351            message: 'Unsupported insight intent.',
352          }
353        };
354        return result;
355      }
356
357      result = {
358        code: 0,
359        result: {
360          message: 'Execute insight intent succeed.',
361        }
362      };
363      return result;
364    }
365  }
366  ```
367
368The code snippet below shows the promise-based asynchronous call that returns the InsightIntent call result:
369  ```ts
370  import IntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
371  import insightIntent from '@ohos.app.ability.insightIntent';
372  import hilog from '@ohos.hilog';
373
374  async function executeInsightIntent(param: Record<string, Object>): Promise<insightIntent.ExecuteResult> {
375    return new Promise((resolve, reject) => {
376      let result: insightIntent.ExecuteResult = {
377        code: 0,
378        result: {
379          message: 'Execute insight intent succeed.',
380        }
381      };
382      resolve(result);
383    });
384  }
385
386  export default class IntentExecutorImpl extends IntentExecutor {
387    async onExecuteInServiceExtensionAbility(name: string, param: Record<string, Object>): Promise<insightIntent.ExecuteResult> {
388      let result: insightIntent.ExecuteResult;
389      if (name !== 'SupportedInsightIntentName') {
390        hilog.warn(0x0000, 'testTag', 'Unsupported insight intent %{public}s', name);
391        result = {
392          // decided by developer
393          code: 404,
394          result: {
395            message: 'Unsupported insight intent.',
396          }
397        };
398        return result;
399      }
400
401      result = await executeInsightIntent(param);
402      return result;
403    }
404  }
405  ```
406