• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.dialogRequest (dialogRequest)
2
3The **dialogRequest** module provides APIs related to modal dialog box processing, including obtaining the request information (used to bind a modal dialog box) and request callback (used to set the request result).
4A modal dialog box is a system pop-up box that intercepts events (such as mouse, keyboard, and touchscreen events) triggered for the page displayed under it. The page can be operated only after the modal dialog box is destroyed.
5
6> **NOTE**
7>
8>  - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
9>  - The APIs provided by this module are used in ServiceExtensionAbilities. For a ServiceExtensionAbility that implements modal dialog boxes, you can use the APIs to obtain the request information and request callback and return the request result.
10
11## Modules to Import
12
13```ts
14import dialogRequest from '@ohos.app.ability.dialogRequest';
15```
16
17## dialogRequest.getRequestInfo
18
19getRequestInfo(want: Want): RequestInfo
20
21Obtains the request information from Want.
22
23**System capability**: SystemCapability.Ability.AbilityRuntime.Core
24
25**Parameters**
26
27| Name| Type  | Mandatory| Description                       |
28| ---- | ------ | ---- | --------------------------- |
29| want  | [Want](js-apis-app-ability-want.md) | Yes  | Want passed in the request for a modal dialog box.|
30
31**Return value**
32
33| Type  | Description                    |
34| ------ | ------------------------ |
35| [RequestInfo](#requestinfo) | **RequestInfo** object obtained, which is used to bind a modal dialog box.|
36
37**Example**
38
39```ts
40   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
41   import Want from '@ohos.app.ability.Want';
42   import rpc from '@ohos.rpc';
43   import dialogRequest from '@ohos.app.ability.dialogRequest';
44
45    const REQUEST_VALUE = 1;
46
47    class StubTest extends rpc.RemoteObject {
48      constructor(des: string) {
49        super(des);
50      }
51
52      onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption) {
53        if (code === REQUEST_VALUE) {
54          let optFir = data.readInt();
55          let optSec = data.readInt();
56          reply.writeInt(optFir + optSec);
57        }
58        return true;
59      }
60
61      getInterfaceDescriptor() {
62        return "";
63      }
64
65      getCallingPid() {
66        return REQUEST_VALUE;
67      }
68
69      getCallingUid() {
70        return REQUEST_VALUE;
71      }
72
73      attachLocalInterface(localInterface: rpc.IRemoteBroker, descriptor: string) {
74      }
75    }
76
77    let TAG = "getRequestInfoTest";
78
79    export default class ServiceExtAbility extends ServiceExtensionAbility {
80      onCreate(want: Want) {
81        console.info(TAG, `onCreate, want: ${want.abilityName}`);
82      }
83
84      onRequest(want: Want, startId: number) {
85        console.info(TAG, `onRequest, want: ${want.abilityName}`);
86        try {
87          let requestInfo = dialogRequest.getRequestInfo(want);
88        } catch (err) {
89          console.error(`getRequestInfo err= ${JSON.stringify(err)}`);
90        }
91      }
92
93      onConnect(want: Want) {
94        console.info(TAG, `onConnect, want: ${want.abilityName}`);
95        return new StubTest("test");
96      }
97
98      onDisconnect(want: Want) {
99        console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
100      }
101
102      onDestroy() {
103        console.info(TAG, `onDestroy`);
104      }
105    }
106   ```
107
108## dialogRequest.getRequestCallback
109
110getRequestCallback(want: Want): RequestCallback
111
112Obtains the request callback from Want.
113
114**System capability**: SystemCapability.Ability.AbilityRuntime.Core
115
116**Parameters**
117
118| Name| Type  | Mandatory| Description                       |
119| ---- | ------ | ---- | --------------------------- |
120| want  | [Want](js-apis-app-ability-want.md) | Yes  | Want passed in the request for a modal dialog box.|
121
122**Return value**
123
124| Type  | Description                    |
125| ------ | ------------------------ |
126| [RequestCallback](#requestcallback) | **RequestCallback** object obtained, which is used to set the return result.|
127
128**Example**
129
130```ts
131   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
132   import Want from '@ohos.app.ability.Want';
133   import rpc from '@ohos.rpc';
134   import dialogRequest from '@ohos.app.ability.dialogRequest';
135
136   let TAG = "getRequestCallbackTest";
137
138   const REQUEST_VALUE = 1;
139
140    class StubTest extends rpc.RemoteObject {
141      constructor(des: string) {
142        super(des);
143      }
144
145      onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption) {
146        if (code === REQUEST_VALUE) {
147          let optFir = data.readInt();
148          let optSec = data.readInt();
149          reply.writeInt(optFir + optSec);
150        }
151        return true;
152      }
153
154      getInterfaceDescriptor() {
155        return "";
156      }
157
158      getCallingPid() {
159        return REQUEST_VALUE;
160      }
161
162      getCallingUid() {
163        return REQUEST_VALUE;
164      }
165
166      attachLocalInterface(localInterface: rpc.IRemoteBroker, descriptor: string) {
167      }
168    }
169
170   export default class ServiceExtAbility extends ServiceExtensionAbility {
171     onCreate(want: Want) {
172       console.info(TAG, `onCreate, want: ${want.abilityName}`);
173     }
174
175     onRequest(want: Want, startId: number) {
176       console.info(TAG, `onRequest, want: ${want.abilityName}`);
177       try {
178            let requestCallback = dialogRequest.getRequestCallback(want);
179        } catch(err) {
180            console.error(`getRequestInfo err= ${JSON.stringify(err)}`);
181        }
182     }
183
184     onConnect(want: Want) {
185       console.info(TAG, `onConnect, want: ${want.abilityName}`);
186       return new StubTest("test");
187     }
188
189     onDisconnect(want: Want) {
190       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
191     }
192
193     onDestroy() {
194       console.info(TAG, `onDestroy`);
195     }
196   }
197   ```
198
199## WindowRect<sup>10+</sup>
200
201Defines the location attributes of a modal dialog box.
202
203**Model restriction**: This API can be used only in the stage model.
204
205**System capability**: SystemCapability.Ability.AbilityRuntime.Core
206
207| Name| Type  | Mandatory| Description                       |
208| ---- | ------ | ---- | --------------------------- |
209| left  | number | No  | X-coordinate of the upper left corner of the dialog box.|
210| top  | number | No  | Y-coordinate of the upper left corner of the dialog box.|
211| width  | number | No  | Width of the dialog box.|
212| height  | number | No  | Height of the dialog box.|
213
214## RequestInfo
215
216Defines the request information, which is used as an input parameter for binding the modal dialog box.
217
218**Model restriction**: This API can be used only in the stage model.
219
220**System capability**: SystemCapability.Ability.AbilityRuntime.Core
221
222| Name     | Type      | Mandatory  | Description    |
223| ------------ | ------------------| ------ | ---------------------- |
224| windowRect<sup>10+</sup>            | [WindowRect](#windowrect10)    | No  | Location attributes of a modal dialog box.         |
225
226**Example**
227
228```ts
229   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
230   import Want from '@ohos.app.ability.Want';
231   import { BusinessError } from '@ohos.base';
232   import rpc from '@ohos.rpc';
233   import dialogRequest from '@ohos.app.ability.dialogRequest';
234   import window from '@ohos.window';
235
236   let TAG = "RequestInfoTest";
237
238   const REQUEST_VALUE = 1;
239
240    class StubTest extends rpc.RemoteObject {
241      constructor(des: string) {
242        super(des);
243      }
244
245      onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption) {
246        if (code === REQUEST_VALUE) {
247          let optFir = data.readInt();
248          let optSec = data.readInt();
249          reply.writeInt(optFir + optSec);
250        }
251        return true;
252      }
253
254      getInterfaceDescriptor() {
255        return "";
256      }
257
258      getCallingPid() {
259        return REQUEST_VALUE;
260      }
261
262      getCallingUid() {
263        return REQUEST_VALUE;
264      }
265
266      attachLocalInterface(localInterface: rpc.IRemoteBroker, descriptor: string) {
267      }
268    }
269
270   export default class ServiceExtAbility extends ServiceExtensionAbility {
271     onCreate(want: Want) {
272       console.info(TAG, `onCreate, want: ${want.abilityName}`);
273     }
274
275     onRequest(want: Want, startId: number) {
276       console.info(TAG, `onRequest, want: ${want.abilityName}`);
277       let windowClass: window.Window | undefined = undefined;
278       let config: window.Configuration = {name: "dialogWindow", windowType: window.WindowType.TYPE_DIALOG, ctx: this.context};
279       try {
280            let requestInfo = dialogRequest.getRequestInfo(want);
281            window.createWindow(config, (err, data) => {
282              if (err.code) {
283                  console.error('Failed to create the window. Cause: ' + JSON.stringify(err));
284                  return;
285              }
286              windowClass = data;
287              windowClass.bindDialogTarget(requestInfo, () => {
288                console.info('Dialog Window Need Destroy.');
289              }, (err: BusinessError) => {
290                  if (err.code) {
291                      console.error(`Failed to bind dialog target. Cause: ${JSON.stringify(err)}`);
292                      return;
293                  }
294                  console.info('Succeeded in binding dialog target.');
295              });
296            });
297        } catch(err) {
298            console.error(`getRequestInfo err= ${JSON.stringify(err)}`);
299        }
300     }
301
302     onConnect(want: Want) {
303       console.info(TAG, `onConnect, want: ${want.abilityName}`);
304       return new StubTest("test");
305     }
306
307     onDisconnect(want: Want) {
308       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
309     }
310
311     onDestroy() {
312       console.info(TAG, `onDestroy`);
313     }
314   }
315   ```
316
317## ResultCode
318
319Enumerates the result codes of the request for the modal dialog box.
320
321**System capability**: SystemCapability.Ability.AbilityRuntime.Core
322
323| Name     | Value         | Description    |
324| ------------ | ------------------ | ---------------------- |
325| RESULT_OK            | 0          | The request succeeds.         |
326| RESULT_CANCEL        | 1          | The request fails.         |
327
328## RequestResult
329Defines the result of the request for the modal dialog box. It contains **ResultCode** and **ResultWant**.
330
331### Attributes
332
333**Model restriction**: This API can be used only in the stage model.
334
335**System capability**: SystemCapability.Ability.AbilityRuntime.Core
336
337| Name| Type| Readable| Writable| Description|
338| -------- | -------- | -------- | -------- | -------- |
339| result | [ResultCode](#resultcode) | Yes| Yes| Result code of the request.|
340| want<sup>10+</sup> | [ResultWant](js-apis-app-ability-want.md)  | Yes| Yes| Want information, such as the ability name and bundle name.|
341
342## RequestCallback
343
344Provides a callback for setting the modal dialog box request result.
345
346**Model restriction**: This API can be used only in the stage model.
347
348### RequestCallback.setRequestResult
349
350setRequestResult(result: RequestResult): void;
351
352Sets the result of the request for the modal dialog box.
353
354**Model restriction**: This API can be used only in the stage model.
355
356**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
357
358**Parameters**
359
360| Name| Type| Mandatory| Description|
361| -------- | -------- | -------- | -------- |
362| result | [RequestResult](#requestresult) | Yes| Request result to set.|
363
364**Error codes**
365
366| ID| Error Message|
367| ------- | -------------------------------- |
368| 401 | If the input parameter is not valid parameter. |
369
370For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
371
372**Example**
373
374```ts
375   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
376   import Want from '@ohos.app.ability.Want';
377   import rpc from '@ohos.rpc';
378   import dialogRequest from '@ohos.app.ability.dialogRequest';
379
380   let TAG = "setRequestResultTest";
381
382      const REQUEST_VALUE = 1;
383
384    class StubTest extends rpc.RemoteObject {
385      constructor(des: string) {
386        super(des);
387      }
388
389      onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption) {
390        if (code === REQUEST_VALUE) {
391          let optFir = data.readInt();
392          let optSec = data.readInt();
393          reply.writeInt(optFir + optSec);
394        }
395        return true;
396      }
397
398      getInterfaceDescriptor() {
399        return "";
400      }
401
402      getCallingPid() {
403        return REQUEST_VALUE;
404      }
405
406      getCallingUid() {
407        return REQUEST_VALUE;
408      }
409
410      attachLocalInterface(localInterface: rpc.IRemoteBroker, descriptor: string) {
411      }
412    }
413
414   export default class ServiceExtAbility extends ServiceExtensionAbility {
415     onCreate(want: Want) {
416       console.info(TAG, `onCreate, want: ${want.abilityName}`);
417     }
418
419     onRequest(want: Want, startId: number) {
420       console.info(TAG, `onRequest, want: ${want.abilityName}`);
421       try {
422            let requestCallback = dialogRequest.getRequestCallback(want);
423            let myResult: dialogRequest.RequestResult = {
424                result : dialogRequest.ResultCode.RESULT_CANCEL,
425            };
426            requestCallback.setRequestResult(myResult);
427        } catch(err) {
428            console.error(`getRequestInfo err= ${JSON.stringify(err)}`);
429        }
430     }
431
432     onConnect(want: Want) {
433       console.info(TAG, `onConnect, want: ${want.abilityName}`);
434       return new StubTest("test");
435     }
436
437     onDisconnect(want: Want) {
438       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
439     }
440
441     onDestroy() {
442       console.info(TAG, `onDestroy`);
443     }
444   }
445  ```
446