1# @ohos.data.unifiedDataChannel (Unified Data Channel) 2 3As a part of the Unified Data Management Framework (UDMF), the **unifiedDataChannel** module provides unified data channels and standard data access interfaces for many-to-many data sharing across applications. It also provides definitions for uniform data types, such as text and image, to streamline data interaction between different applications and minimize the workload of data type adaptation. Although the UDMF does not parse user data, you are advised not to transfer sensitive personal data or privacy data due to low-level security of storage path. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 10 11```ts 12import { unifiedDataChannel } from '@kit.ArkData'; 13``` 14 15## ShareOptions<sup>12+</sup> 16 17Enumerates the options for using **UnifiedData** in a device. 18 19**Atomic service API**: This API can be used in atomic services since API version 12. 20 21**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 22 23| Name | Value| Description | 24|-------------|---|-------------------| 25| IN_APP | 0 | **UnifiedData** can be used only in the same application of a device.| 26| CROSS_APP | 1 | **UnifiedData** can be used across applications of a device.| 27 28## GetDelayData<sup>12+</sup> 29 30type GetDelayData = (type: string) => UnifiedData 31 32A type that defines a function used to obtain a deferred **UnifiedData** object. Currently, it can be used only in the pasteboard application of the same device. 33 34**Atomic service API**: This API can be used in atomic services since API version 12. 35 36**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 37 38**Parameters** 39 40| Name| Type| Mandatory| Description| 41| -------- | -------- | -------- | -------- | 42| type | string | Yes| Identifier of the deferred encapsulation.| 43 44**Return value** 45 46| Type | Description | 47| ---------------------------------------- |-------------------------| 48| [UnifiedData](#unifieddata) | **UnifiedData** object.| 49 50**Example** 51 52```ts 53import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 54 55let getDelayData: unifiedDataChannel.GetDelayData = ((type: string) => { 56 if (type == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 57 let plainTextDetails : Record<string, string> = { 58 'attr1': 'value1', 59 'attr2': 'value2', 60 } 61 let plainText : uniformDataStruct.PlainText = { 62 uniformDataType: 'general.plain-text', 63 textContent : 'This is a plain text example', 64 abstract : 'This is abstract', 65 details : plainTextDetails, 66 } 67 let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 68 let textData = new unifiedDataChannel.UnifiedData(text); 69 return textData; 70 } 71 return new unifiedDataChannel.UnifiedData(); 72}); 73``` 74 75## ValueType<sup>12+</sup> 76 77type ValueType = number | string | boolean | image.PixelMap | Want | ArrayBuffer | object | null | undefined 78 79Enumerates the data field types allowed in a unified data record. 80 81**Atomic service API**: This API can be used in atomic services since API version 12. 82 83**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 84 85| Type| Description| 86| -------- | -------- | 87| number | Number.| 88| string | String.| 89| boolean | Boolean.| 90| image.PixelMap | The value is of the [image.PixelMap](../apis-image-kit/arkts-apis-image-PixelMap.md) type.| 91| Want | [Want](../apis-ability-kit/js-apis-app-ability-want.md).| 92| ArrayBuffer | ArrayBuffer.| 93| object | Object.| 94| null | Null.| 95| undefined | Undefined.| 96 97## UnifiedDataProperties<sup>12+</sup> 98 99Defines the properties of the data records in the unified data object, including the timestamp, tag, pasting range, and additional data. 100 101**Atomic service API**: This API can be used in atomic services since API version 12. 102 103**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 104 105| Name| Type| Read-Only| Optional| Description| 106| -------- | -------- | -------- | -------- | -------- | 107| extras<sup>12+</sup> | Record<string, object> | No| Yes| Object of the dictionary type used to set other properties. The default value is an empty dictionary object.| 108| tag<sup>12+</sup> | string | No| Yes| Customized tag. The default value is an empty string.| 109| timestamp<sup>12+</sup> | Date | Yes| Yes| Timestamp when [UnifiedData](#unifieddata) is generated. The default value is January 1, 1970 (UTC).| 110| shareOptions<sup>12+</sup> | [ShareOptions](#shareoptions12) | No| Yes| Range, in which [UnifiedData](#unifieddata) can be used. The default value is **CROSS_APP**.| 111| getDelayData<sup>12+</sup> | [GetDelayData](#getdelaydata12) | No| Yes| Callback for obtaining the deferred data. Currently, it can be used only in the pasteboard application of the same device. The default value is **undefined**.| 112 113**Example** 114 115```ts 116import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 117 118let properties = new unifiedDataChannel.UnifiedDataProperties(); 119properties.extras = { 120 key: { 121 title: 'MyTitle', 122 content: 'MyContent' 123 } 124}; 125properties.tag = "This is a tag of properties"; 126properties.shareOptions = unifiedDataChannel.ShareOptions.CROSS_APP; 127properties.getDelayData = ((type: string) => { 128 if (type == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 129 let plainTextDetails : Record<string, string> = { 130 'attr1': 'value1', 131 'attr2': 'value2', 132 } 133 let plainText : uniformDataStruct.PlainText = { 134 uniformDataType: 'general.plain-text', 135 textContent : 'This is a plain text example', 136 abstract : 'This is abstract', 137 details : plainTextDetails, 138 } 139 let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 140 let textData = new unifiedDataChannel.UnifiedData(text); 141 return textData; 142 } 143 return new unifiedDataChannel.UnifiedData(); 144}); 145``` 146 147## UnifiedData 148 149Provides APIs for encapsulating a set of data records. 150 151**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 152 153### Properties 154 155| Name| Type| Read-Only| Optional| Description | 156| -------- | -------- | -------- | -------- |-------------------------------------------------------------------------------------------------| 157| properties<sup>12+</sup> | [UnifiedDataProperties](#unifieddataproperties12) | No| No| Properties of all the data records in a unified data object, including the timestamp, tag, application range, and additional data.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 158 159### constructor<sup>12+</sup> 160 161constructor() 162 163A constructor used to create a **UnifiedData** object. 164 165**Atomic service API**: This API can be used in atomic services since API version 12. 166 167**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 168 169**Example** 170 171```ts 172let unifiedData = new unifiedDataChannel.UnifiedData(); 173``` 174 175### constructor 176 177constructor(record: UnifiedRecord) 178 179A constructor used to create a **UnifiedData** object with a data record. 180 181**Atomic service API**: This API can be used in atomic services since API version 11. 182 183**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 184 185**Parameters** 186 187| Name| Type | Mandatory| Description | 188| ------ | ------------------------------- | ---- |-----------------------------------------| 189| record | [UnifiedRecord](#unifiedrecord) | Yes | Data record in the **UnifiedData** object. It is a **UnifiedRecord** object or its child class object.| 190 191**Error codes** 192 193For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 194 195| **ID**| **Error Message** | 196| ------------ | ------------------------------------------- | 197| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 198 199**Example** 200 201```ts 202import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 203let plainText : uniformDataStruct.PlainText = { 204 uniformDataType: 'general.plain-text', 205 textContent : 'This is a plain text example', 206 abstract : 'This is abstract', 207} 208let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 209let unifiedData = new unifiedDataChannel.UnifiedData(text); 210``` 211 212### addRecord 213 214addRecord(record: UnifiedRecord): void 215 216Adds a data record to this **UnifiedRecord** object. 217 218**Atomic service API**: This API can be used in atomic services since API version 11. 219 220**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 221 222**Parameters** 223 224| Name| Type | Mandatory| Description | 225| ------ | ------------------------------- | ---- |---------------------------------------------| 226| record | [UnifiedRecord](#unifiedrecord) | Yes | Data record to add. It is a **UnifiedRecord** child class object.| 227 228**Error codes** 229 230For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 231 232| **ID**| **Error Message** | 233| ------------ | ------------------------------------------- | 234| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 235 236**Example** 237 238```ts 239import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 240let plainText : uniformDataStruct.PlainText = { 241 uniformDataType: 'general.plain-text', 242 textContent : 'This is a plain text example', 243 abstract : 'This is abstract', 244} 245let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 246let unifiedData = new unifiedDataChannel.UnifiedData(text); 247 248let hyperlink : uniformDataStruct.Hyperlink = { 249 uniformDataType:'general.hyperlink', 250 url : 'www.XXX.com', 251 description : 'This is the description of the hyperlink', 252} 253let link = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 254unifiedData.addRecord(link); 255``` 256 257### getRecords 258 259getRecords(): Array\<UnifiedRecord\> 260 261Obtains all data records from this **UnifiedData** object. The data obtained is of the **UnifiedRecord** type. Before using the data, you need to use [getType](#gettype) to obtain the data type and convert the data type to a child class. 262 263**Atomic service API**: This API can be used in atomic services since API version 11. 264 265**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 266 267**Return value** 268 269| Type | Description | 270| ---------------------------------------- |-------------------------| 271| Array\<[UnifiedRecord](#unifiedrecord)\> | Records in the **UnifiedData** object obtained.| 272 273**Example** 274 275```ts 276import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 277 278let plainText : uniformDataStruct.PlainText = { 279 uniformDataType: 'general.plain-text', 280 textContent : 'This is a plain text example', 281 abstract : 'This is abstract', 282} 283let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 284let unifiedData = new unifiedDataChannel.UnifiedData(text); 285 286let hyperlink : uniformDataStruct.Hyperlink = { 287 uniformDataType:'general.hyperlink', 288 url : 'www.XXX.com', 289 description : 'This is the description of the hyperlink', 290} 291let link = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 292unifiedData.addRecord(link); 293 294let records = unifiedData.getRecords(); 295for (let i = 0; i < records.length; i++) { 296 let record = records[i]; 297 let types = record.getTypes(); 298 if (types.includes(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT)) { 299 let plainText = record.getEntry(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) as unifiedDataChannel.PlainText; 300 console.info(`textContent: ${plainText.textContent}`); 301 } else if (types.includes(uniformTypeDescriptor.UniformDataType.HYPERLINK)) { 302 let hyperlink = record.getEntry(uniformTypeDescriptor.UniformDataType.HYPERLINK) as unifiedDataChannel.Hyperlink; 303 console.info(`linkUrl: ${hyperlink.url}`); 304 } 305} 306``` 307 308### hasType<sup>12+</sup> 309 310hasType(type: string): boolean 311 312Checks whether this **UnifiedData** object contains the specified data type, including the data types added by using the [addEntry](#addentry15) function. 313 314For file types, if the type set of **UnifiedData** contains **general.jpeg**, **true** is returned when the **hasType** API is called to check whether the **general.image** type is included, because the **general.jpeg** type belongs to the **general.image** type. 315 316**Atomic service API**: This API can be used in atomic services since API version 12. 317 318**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 319 320| Name| Type | Mandatory| Description | 321| ------ | ------------------------------- | ---- |---------------------------------------------| 322| type | string | Yes | Data type to check. For details, see [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype).| 323 324**Return value** 325 326| Type | Description | 327| ---------------------------------------- |-------------------------| 328| boolean | Returns **true** if the specified data type exists; returns **false** otherwise.| 329 330**Error codes** 331 332For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 333 334| **ID**| **Error Message** | 335| ------------ | ------------------------------------------- | 336| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 337 338**Example** 339 340```ts 341import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 342 343let plainText : uniformDataStruct.PlainText = { 344 uniformDataType: 'general.plain-text', 345 textContent : 'This is a plain text example', 346 abstract : 'This is abstract', 347} 348let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 349let unifiedData = new unifiedDataChannel.UnifiedData(text); 350 351let hyperlink : uniformDataStruct.Hyperlink = { 352 uniformDataType:'general.hyperlink', 353 url : 'www.XXX.com', 354 description : 'This is the description of the hyperlink', 355} 356let link = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 357unifiedData.addRecord(link); 358 359let hasPlainText = unifiedData.hasType(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT); 360let hasLink = unifiedData.hasType(uniformTypeDescriptor.UniformDataType.HYPERLINK); 361``` 362 363### getTypes<sup>12+</sup> 364 365getTypes(): Array\<string\> 366 367Obtains the types of all data records in this **UnifiedData** object. 368 369**Atomic service API**: This API can be used in atomic services since API version 12. 370 371**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 372 373**Return value** 374 375| Type | Description | 376| ---------------------------------------- |-------------------------| 377| Array\<string\> | Array of the [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype) types obtained.| 378 379**Example** 380 381```ts 382import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 383 384let plainText : uniformDataStruct.PlainText = { 385 uniformDataType: 'general.plain-text', 386 textContent : 'This is a plain text example', 387 abstract : 'This is abstract', 388} 389let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 390let unifiedData = new unifiedDataChannel.UnifiedData(text); 391 392let hyperlink : uniformDataStruct.Hyperlink = { 393 uniformDataType:'general.hyperlink', 394 url : 'www.XXX.com', 395 description : 'This is the description of the hyperlink', 396} 397let link = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 398unifiedData.addRecord(link); 399 400let types = unifiedData.getTypes(); 401``` 402 403## Summary 404 405Summarizes the data information of the **unifiedData** object, including the data type and size. 406 407**Atomic service API**: This API can be used in atomic services since API version 11. 408 409**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 410 411| Name| Type| Read-Only| Optional| Description| 412| -------- | -------- | -------- | -------- | -------- | 413| summary | Record<string, number> | No| No| Dictionary type object, where the key indicates the data type (see [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)), and the value indicates the total size (in bytes) of this type of records in the unified data object.| 414| totalSize | number | No| No| Total size of all the records in the **UnifiedData** object, in bytes.| 415 416## UnifiedRecord 417 418An abstract definition of the data content supported by the UDMF. A **UnifiedRecord** object contains one or more data records, for example, a text record, an image record, or an HTML record. Since API version 15, different styles of the same content can be added to a **UnifiedRecord** object. Data users can obtain the corresponding styles as required. 419 420### constructor<sup>12+</sup> 421 422constructor() 423 424A constructor used to create a **UnfiedRecord** object. 425 426**Atomic service API**: This API can be used in atomic services since API version 12. 427 428**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 429 430**Example** 431 432```ts 433let unifiedRecord = new unifiedDataChannel.UnifiedRecord(); 434``` 435 436### constructor<sup>12+</sup> 437 438constructor(type: string, value: ValueType) 439 440A constructor used to create a data record with the specified type and value.<br>If **value** is of the [image.PixelMap](../apis-image-kit/arkts-apis-image-PixelMap.md) type, **type** must be the value of **OPENHARMONY_PIXEL_MAP** in [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype).<br>If **value** is of the [Want](../apis-ability-kit/js-apis-app-ability-want.md) type, **type** must be the value of **OPENHARMONY_WANT** in [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype). 441 442**Atomic service API**: This API can be used in atomic services since API version 12. 443 444**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 445 446**Parameters** 447 448| Name| Type | Mandatory| Description | 449| ------ | ------------------------------- | ---- |-----------------------------------------| 450| type | string | Yes | Type of the data record to create.| 451| value | [ValueType](#valuetype12) | Yes | Value of the data record to create.| 452 453**Error codes** 454 455For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 456 457| **ID**| **Error Message** | 458| ------------ | ------------------------------------------- | 459| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 460 461**Example** 462 463```ts 464import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 465import { image } from '@kit.ImageKit'; 466 467let hyperlink : uniformDataStruct.Hyperlink = { 468 uniformDataType:'general.hyperlink', 469 url : 'www.XXX.com', 470 description : 'This is the description of the hyperlink', 471} 472let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 473 474let plainText : uniformDataStruct.PlainText = { 475 uniformDataType: 'general.plain-text', 476 textContent : 'This is a plain text example', 477 abstract : 'This is abstract', 478} 479let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 480 481let arrayBuffer = new ArrayBuffer(4 * 200 * 200); 482let opt : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 200, width: 200 }, alphaType: 3 }; 483let pixelMap : uniformDataStruct.PixelMap = { 484 uniformDataType : 'openharmony.pixel-map', 485 pixelMap : image.createPixelMapSync(arrayBuffer, opt), 486} 487let pixelMapRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.OPENHARMONY_PIXEL_MAP, pixelMap); 488``` 489 490### getType 491 492getType(): string 493 494Obtains the type of this **UnfiedRecord**. The data obtained by [getRecords](#getrecords) from the **UnifiedData** object is a **UnifiedRecord** object. You need to use this API to obtain the specific type of the record, convert the **UnifiedRecord** object to its child class, and call the child class interfaces. 495 496**Atomic service API**: This API can be used in atomic services since API version 11. 497 498**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 499 500**Return value** 501 502| Type | Description | 503| ------ |------------------------------------------------------| 504| string | Data type obtained. For details, see [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype).| 505 506**Example** 507 508```ts 509import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 510 511let plainText : uniformDataStruct.PlainText = { 512 uniformDataType: 'general.plain-text', 513 textContent : 'This is a plain text example', 514 abstract : 'This is abstract', 515} 516let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 517let unifiedData = new unifiedDataChannel.UnifiedData(text); 518 519let records = unifiedData.getRecords(); 520if (records[0].getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 521 let plainText = records[0] as unifiedDataChannel.PlainText; 522 console.info(`textContent: ${plainText.textContent}`); 523} 524``` 525 526### getValue<sup>12+</sup> 527 528getValue(): ValueType 529 530Obtains the value of this data record. 531 532**Atomic service API**: This API can be used in atomic services since API version 12. 533 534**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 535 536**Return value** 537 538| Type | Description | 539| ------ |------------------------------------------------------| 540| [ValueType](#valuetype12) | Value obtained.| 541 542**Example** 543 544```ts 545import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 546 547let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, 'this is value of text'); 548let value = text.getValue(); 549 550let hyperlinkDetails : Record<string, string> = { 551 'attr1': 'value1', 552 'attr2': 'value2', 553} 554let hyperlink : uniformDataStruct.Hyperlink = { 555 uniformDataType:'general.hyperlink', 556 url : 'www.XXX.com', 557 description : 'This is the description of the hyperlink', 558 details : hyperlinkDetails, 559} 560let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 561let hyperlinkValue = hyperlinkRecord.getValue(); 562``` 563 564### addEntry<sup>15+</sup> 565 566addEntry(type: string, value: ValueType): void 567 568Adds data of a specified data type and content to the current data record. You can use this API to add different data types and contents to the same data. 569 570**Atomic service API**: This API can be used in atomic services since API version 15. 571 572**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 573 574**Parameters** 575 576| Name| Type | Mandatory| Description | 577| ------ | ------------------------------- | ---- |-----------------------------------------| 578| type | string | Yes | Type of the data to add. For details, see [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype).| 579| value | [ValueType](#valuetype12) | Yes | Value of the data to add.| 580 581**Error codes** 582 583For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 584 585| **ID**| **Error Message** | 586| ------------ | ------------------------------------------- | 587| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 588 589**Example** 590 591```ts 592import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 593 594let fileUriDetails : Record<string, string> = { 595 'attr1': 'value1', 596 'attr2': 'value2', 597} 598let fileUri : uniformDataStruct.FileUri = { 599 uniformDataType : 'general.file-uri', 600 oriUri : 'file://data/image/1.png', 601 fileType : 'general.image', 602 details : fileUriDetails, 603} 604let hyperlink : uniformDataStruct.Hyperlink = { 605 uniformDataType:'general.hyperlink', 606 url : 'file://data/image/1.png', 607 description : 'This is the description of the hyperlink', 608} 609 610let unifiedData = new unifiedDataChannel.UnifiedData(); 611let record = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink); 612record.addEntry(uniformTypeDescriptor.UniformDataType.FILE_URI, fileUri); 613unifiedData.addRecord(record); 614``` 615 616### getEntry<sup>15+</sup> 617 618getEntry(type: string): ValueType 619 620Obtains data of the specified type from the data record. 621 622**Atomic service API**: This API can be used in atomic services since API version 15. 623 624**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 625 626**Parameters** 627 628| Name| Type | Mandatory| Description | 629| ------ | ------------------------------- | ---- |-----------------------------------------| 630| type | string | Yes | Type of the data to obtain. For details, see [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype).| 631 632**Return value** 633 634| Type | Description | 635| ------ |------------------------------------------------------| 636| [ValueType](#valuetype12) | Value obtained.| 637 638**Error codes** 639 640For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 641 642| **ID**| **Error Message** | 643| ------------ | ------------------------------------------- | 644| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 645 646**Example** 647 648```ts 649import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 650 651let fileUriDetails : Record<string, string> = { 652 'attr1': 'value1', 653 'attr2': 'value2', 654} 655let fileUri : uniformDataStruct.FileUri = { 656 uniformDataType : 'general.file-uri', 657 oriUri : 'file://data/image/1.png', 658 fileType : 'general.image', 659 details : fileUriDetails, 660} 661let formDetails : Record<string, string> = { 662 'attr1': 'value1', 663 'attr2': 'value2', 664} 665let form : uniformDataStruct.Form = { 666 uniformDataType : 'openharmony.form', 667 formId : 1, 668 formName : 'form', 669 bundleName : 'com.xx.app', 670 abilityName : 'ability', 671 module : 'module', 672 details : formDetails, 673} 674 675let unifiedData = new unifiedDataChannel.UnifiedData(); 676let record = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.OPENHARMONY_FORM, form); 677record.addEntry(uniformTypeDescriptor.UniformDataType.FILE_URI, fileUri); 678unifiedData.addRecord(record); 679 680let records = unifiedData.getRecords(); 681for (let i = 0; i < records.length; i++) { 682 let unifiedDataRecord = records[i] as unifiedDataChannel.UnifiedRecord; 683 let fileUriRead : uniformDataStruct.FileUri = unifiedDataRecord.getEntry(uniformTypeDescriptor.UniformDataType.FILE_URI) as uniformDataStruct.FileUri; 684 if (fileUriRead != undefined) { 685 console.info(`oriUri: ${fileUriRead.oriUri}`); 686 } 687 let formRead = unifiedDataRecord.getEntry(uniformTypeDescriptor.UniformDataType.OPENHARMONY_FORM) as uniformDataStruct.Form; 688 if (formRead != undefined) { 689 console.info(`formName: ${formRead.formName}`); 690 } 691} 692``` 693 694### getEntries<sup>15+</sup> 695 696getEntries(): Record<string, ValueType> 697 698Obtains all the data in the current data record. 699 700**Atomic service API**: This API can be used in atomic services since API version 15. 701 702**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 703 704**Return value** 705 706| Type | Description | 707| ------ |------------------------------------------------------| 708| Record<string, [ValueType](#valuetype12)> | Values and types obtained.| 709 710**Example** 711 712```ts 713import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 714 715let fileUriDetails : Record<string, string> = { 716 'attr1': 'value1', 717 'attr2': 'value2', 718} 719let fileUri : uniformDataStruct.FileUri = { 720 uniformDataType : 'general.file-uri', 721 oriUri : 'file://data/image/1.png', 722 fileType : 'general.image', 723 details : fileUriDetails, 724} 725let formDetails : Record<string, string> = { 726 'attr1': 'value1', 727 'attr2': 'value2', 728} 729let form : uniformDataStruct.Form = { 730 uniformDataType : 'openharmony.form', 731 formId : 1, 732 formName : 'form', 733 bundleName : 'com.xx.app', 734 abilityName : 'ability', 735 module : 'module', 736 details : formDetails, 737} 738 739let unifiedData = new unifiedDataChannel.UnifiedData(); 740let record = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.OPENHARMONY_FORM, form); 741record.addEntry(uniformTypeDescriptor.UniformDataType.FILE_URI, fileUri); 742unifiedData.addRecord(record); 743 744let records = unifiedData.getRecords(); 745for (let i = 0; i < records.length; i++) { 746 let unifiedDataRecord = records[i] as unifiedDataChannel.UnifiedRecord; 747 let entries : Record<string, unifiedDataChannel.ValueType> = unifiedDataRecord.getEntries(); 748 let formRead : uniformDataStruct.Form = entries[uniformTypeDescriptor.UniformDataType.OPENHARMONY_FORM] as uniformDataStruct.Form; 749 if (formRead != undefined) { 750 console.info(`formName: ${formRead.formName}`); 751 } 752 let fileUriRead : uniformDataStruct.FileUri = entries[uniformTypeDescriptor.UniformDataType.FILE_URI] as uniformDataStruct.FileUri; 753 if (fileUriRead != undefined) { 754 console.info(`oriUri: ${fileUriRead.oriUri}`); 755 } 756} 757``` 758 759### getTypes<sup>15+</sup> 760 761getTypes(): Array\<string\> 762 763Obtains all the data types in the data record. This API can be called using the **UnifiedRecord** object to query all data types in the record, including the data types added using the [addEntry](#addentry15) function. 764 765**Atomic service API**: This API can be used in atomic services since API version 15. 766 767**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 768 769**Return value** 770 771| Type | Description | 772| ---------------------------------------- |-------------------------| 773| Array\<string\> | Array of [UniformDataType](js-apis-data-uniformTypeDescriptor.md#uniformdatatype)s obtained.| 774 775**Example** 776 777```ts 778import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 779 780let fileUriDetails : Record<string, string> = { 781 'attr1': 'value1', 782 'attr2': 'value2', 783} 784let fileUri : uniformDataStruct.FileUri = { 785 uniformDataType : 'general.file-uri', 786 oriUri : 'file://data/image/1.png', 787 fileType : 'general.image', 788 details : fileUriDetails, 789} 790let formDetails : Record<string, string> = { 791 'attr1': 'value1', 792 'attr2': 'value2', 793} 794let form : uniformDataStruct.Form = { 795 uniformDataType : 'openharmony.form', 796 formId : 1, 797 formName : 'form', 798 bundleName : 'com.xx.app', 799 abilityName : 'ability', 800 module : 'module', 801 details : formDetails, 802} 803 804let unifiedData = new unifiedDataChannel.UnifiedData(); 805let record = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.OPENHARMONY_FORM, form); 806record.addEntry(uniformTypeDescriptor.UniformDataType.FILE_URI, fileUri); 807unifiedData.addRecord(record); 808 809let records = unifiedData.getRecords(); 810for (let i = 0; i < records.length; i++) { 811 let unifiedDataRecord = records[i] as unifiedDataChannel.UnifiedRecord; 812 let types : Array<string> = unifiedDataRecord.getTypes(); 813 if (types.includes(uniformTypeDescriptor.UniformDataType.OPENHARMONY_FORM)) { 814 console.info(`Types include: ${uniformTypeDescriptor.UniformDataType.OPENHARMONY_FORM}`); 815 } 816} 817``` 818 819## Text 820 821Represents the text data. It is a child class of [UnifiedRecord](#unifiedrecord) and a base class of text data. You are advised to use the child class of **Text**, for example, [PlainText](#plaintext), [Hyperlink](#hyperlink), and [HTML](#html), to describe data. 822 823**Atomic service API**: This API can be used in atomic services since API version 11. 824 825**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 826 827| Name| Type| Read-Only| Optional| Description| 828| -------- | -------- | -------- | -------- | -------- | 829| details | Record<string, string> | No| Yes| A dictionary type object, where both the key and value are of the string type and are used to describe the text content. For example, a data object with the following content can be created to describe a text file:<br>{<br>"title":"Title",<br>"content":"Content"<br>}<br> The default value is an empty dictionary object.| 830 831**Example** 832 833```ts 834let text = new unifiedDataChannel.Text(); 835text.details = { 836 title: 'MyTitle', 837 content: 'This is content', 838}; 839let unifiedData = new unifiedDataChannel.UnifiedData(text); 840``` 841 842## PlainText 843 844Represents the plaintext data. It is a child class of [Text](#text) and is used to describe plaintext data. 845 846**Atomic service API**: This API can be used in atomic services since API version 11. 847 848**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 849 850| Name| Type| Read-Only| Optional| Description| 851| -------- | -------- | -------- | -------- | -------- | 852| textContent | string | No| No| Plaintext content. | 853| abstract | string | No| Yes| Text abstract. This parameter is optional. The default value is an empty string.| 854 855**Example** 856 857```ts 858let text = new unifiedDataChannel.PlainText(); 859text.textContent = 'this is textContent'; 860text.abstract = 'This is abstract'; 861``` 862 863## Hyperlink 864 865Represents hyperlink data. It is a child class of [Text](#text) and is used to describe data of the hyperlink type. 866 867**Atomic service API**: This API can be used in atomic services since API version 11. 868 869**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 870 871| Name| Type| Read-Only| Optional| Description| 872| -------- | -------- | -------- | -------- | -------- | 873| url | string | No| No| URL. | 874| description | string | No| Yes| Description of the linked content. This parameter is optional. The default value is an empty string.| 875 876**Example** 877 878```ts 879let link = new unifiedDataChannel.Hyperlink(); 880link.url = 'www.XXX.com'; 881link.description = 'This is description'; 882``` 883 884## HTML 885 886Represents the HTML data. It is a child class of [Text](#text) and is used to describe HTML data. 887 888**Atomic service API**: This API can be used in atomic services since API version 11. 889 890**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 891 892| Name| Type| Read-Only| Optional| Description| 893| -------- | -------- | -------- | -------- | -------- | 894| htmlContent | string | No| No| Content in HTML format. | 895| plainContent | string | No| Yes| Plaintext without HTML tags. This parameter is optional. The default value is an empty string.| 896 897**Example** 898 899```ts 900let html = new unifiedDataChannel.HTML(); 901html.htmlContent = '<div><p>Title</p></div>'; 902html.plainContent = 'This is plainContent'; 903``` 904 905## File 906 907Represents the file data. It is a child class of [UnifiedRecord](#unifiedrecord) and a base class of the data of the file type. You are advised to use the child class of **File**, for example, [Image](#image), [Video](#video), and [Folder](#folder), to describe data. 908 909**Atomic service API**: This API can be used in atomic services since API version 11. 910 911**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 912 913| Name| Type| Read-Only| Optional| Description| 914| -------- | -------- | -------- | -------- | -------- | 915| details | Record<string, string> | No| Yes| A dictionary type object, where both the key and value are of the string type and are used to describe file information. For example, a data object with the following content can be created to describe a file:<br>{<br>"name":"File name",<br>"type":"File type"<br>}<br> The default value is an empty dictionary object.| 916| uri | string | No| No| URI of the local file or online file. The local file URI can be obtained using the [getUriFromPath](../apis-core-file-kit/js-apis-file-fileuri.md#fileurigeturifrompath) function. | 917 918**Example** 919 920```ts 921import { unifiedDataChannel } from '@kit.ArkData'; 922import { fileUri } from '@kit.CoreFileKit' 923import { UIAbility } from '@kit.AbilityKit'; 924import { window } from '@kit.ArkUI'; 925 926export default class EntryAbility extends UIAbility { 927 onWindowStageCreate(windowStage: window.WindowStage) { 928 let context = this.context; 929 let pathDir = context.filesDir; 930 let file = new unifiedDataChannel.File(); 931 file.details = { 932 name: 'test', 933 type: 'txt', 934 }; 935 let filePath = pathDir + '/test.txt'; 936 file.uri = fileUri.getUriFromPath(filePath); 937 } 938} 939``` 940 941## Image 942 943Represents the image data. It is a child class of [File](#file) and is used to describe images. 944 945**Atomic service API**: This API can be used in atomic services since API version 11. 946 947**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 948 949| Name| Type| Read-Only| Optional| Description| 950| -------- | -------- | -------- | -------- | -------- | 951| imageUri | string | No| No| URI of the local image or online image. The local image URI can be obtained using the [getUriFromPath](../apis-core-file-kit/js-apis-file-fileuri.md#fileurigeturifrompath) function.| 952 953**Example** 954 955```ts 956import { unifiedDataChannel } from '@kit.ArkData'; 957import { fileUri } from '@kit.CoreFileKit' 958import { UIAbility } from '@kit.AbilityKit'; 959import { window } from '@kit.ArkUI'; 960 961export default class EntryAbility extends UIAbility { 962 onWindowStageCreate(windowStage: window.WindowStage) { 963 let context = this.context; 964 let pathDir = context.filesDir; 965 let image = new unifiedDataChannel.Image(); 966 let filePath = pathDir + '/test.jpg'; 967 image.imageUri = fileUri.getUriFromPath(filePath); 968 } 969} 970``` 971 972## Video 973 974Represents video data. It is a child class of [File](#file) and is used to describe a video file. 975 976**Atomic service API**: This API can be used in atomic services since API version 11. 977 978**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 979 980| Name| Type| Read-Only| Optional| Description| 981| -------- | -------- | -------- | -------- | -------- | 982| videoUri | string | No| No| URI of the local video or online video. The local video URI can be obtained using the [getUriFromPath](../apis-core-file-kit/js-apis-file-fileuri.md#fileurigeturifrompath) function.| 983 984**Example** 985 986```ts 987import { unifiedDataChannel } from '@kit.ArkData'; 988import { fileUri } from '@kit.CoreFileKit' 989import { UIAbility } from '@kit.AbilityKit'; 990import { window } from '@kit.ArkUI'; 991 992export default class EntryAbility extends UIAbility { 993 onWindowStageCreate(windowStage: window.WindowStage) { 994 let context = this.context; 995 let pathDir = context.filesDir; 996 let video = new unifiedDataChannel.Video(); 997 let filePath = pathDir + '/test.mp4'; 998 video.videoUri =fileUri.getUriFromPath(filePath); 999 } 1000} 1001``` 1002 1003## Audio 1004 1005Represents audio data. It is a child class of [File](#file) and is used to describe an audio file. 1006 1007**Atomic service API**: This API can be used in atomic services since API version 11. 1008 1009**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1010 1011| Name| Type| Read-Only| Optional| Description| 1012| -------- | -------- | -------- | -------- | -------- | 1013| audioUri | string | No| No| URI of the local audio or online audio. The local audio URI can be obtained using the [getUriFromPath](../apis-core-file-kit/js-apis-file-fileuri.md#fileurigeturifrompath) function.| 1014 1015**Example** 1016 1017```ts 1018import { unifiedDataChannel } from '@kit.ArkData'; 1019import { fileUri } from '@kit.CoreFileKit' 1020import { UIAbility } from '@kit.AbilityKit'; 1021import { window } from '@kit.ArkUI'; 1022 1023export default class EntryAbility extends UIAbility { 1024 onWindowStageCreate(windowStage: window.WindowStage) { 1025 let context = this.context; 1026 let pathDir = context.filesDir; 1027 let audio = new unifiedDataChannel.Audio(); 1028 let filePath = pathDir + '/test.mp3'; 1029 audio.audioUri = fileUri.getUriFromPath(filePath); 1030 } 1031} 1032``` 1033 1034## Folder 1035 1036Represents the folder data. It is a child class of [File](#file) and is used to describe a folder. 1037 1038**Atomic service API**: This API can be used in atomic services since API version 11. 1039 1040**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1041 1042| Name| Type| Read-Only| Optional| Description| 1043| -------- | -------- | -------- | -------- | -------- | 1044| folderUri | string | No| No| URI of the local folder or online folder. The local folder URI can be obtained using the [getUriFromPath](../apis-core-file-kit/js-apis-file-fileuri.md#fileurigeturifrompath) function.| 1045 1046**Example** 1047 1048```ts 1049import { unifiedDataChannel } from '@kit.ArkData'; 1050import { fileUri } from '@kit.CoreFileKit' 1051import { UIAbility } from '@kit.AbilityKit'; 1052import { window } from '@kit.ArkUI'; 1053 1054export default class EntryAbility extends UIAbility { 1055 onWindowStageCreate(windowStage: window.WindowStage) { 1056 let context = this.context; 1057 let pathDir = context.filesDir; 1058 let folder = new unifiedDataChannel.Folder(); 1059 let filePath = pathDir + '/folder'; 1060 folder.folderUri = fileUri.getUriFromPath(filePath); 1061 } 1062} 1063``` 1064 1065## SystemDefinedRecord 1066 1067Represents specific data types defined by OpenHarmony. It is a child class of [UnifiedRecord](#unifiedrecord) and a base class of OpenHarmony-specific data types. You are advised to use the child class of **SystemDefinedRecord**, for example, [SystemDefinedForm](#systemdefinedform), [SystemDefinedAppItem](#systemdefinedappitem), and [SystemDefinedPixelMap](#systemdefinedpixelmap), to describe OpenHarmony-specific data. 1068 1069**Atomic service API**: This API can be used in atomic services since API version 11. 1070 1071**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1072 1073| Name| Type| Read-Only| Optional| Description| 1074| -------- | -------- | -------- | -------- | -------- | 1075| details | Record<string, number \| string \| Uint8Array> | No| Yes| A dictionary type object, where the key is of the string type, and the value can be a number, a string, or a Uint8Array. The default value is an empty dictionary object.| 1076 1077**Example** 1078 1079```ts 1080let sdr = new unifiedDataChannel.SystemDefinedRecord(); 1081let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 1082sdr.details = { 1083 title: 'recordTitle', 1084 version: 1, 1085 content: u8Array, 1086}; 1087let unifiedData = new unifiedDataChannel.UnifiedData(sdr); 1088``` 1089 1090## SystemDefinedForm 1091 1092Represents the service widget data defined by the system. It is a child class of [SystemDefinedRecord](#systemdefinedrecord). 1093 1094**Atomic service API**: This API can be used in atomic services since API version 11. 1095 1096**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1097 1098| Name| Type| Read-Only| Optional| Description| 1099| -------- | -------- | -------- | -------- | -------- | 1100| formId | number | No| No| Service widget ID. | 1101| formName | string | No| No| Widget name. | 1102| bundleName | string | No| No| Name of the bundle to which the widget belongs. | 1103| abilityName | string | No| No| Ability name corresponding to the widget.| 1104| module | string | No| No| Name of the module to which the widget belongs. | 1105 1106**Example** 1107 1108```ts 1109let form = new unifiedDataChannel.SystemDefinedForm(); 1110form.formId = 123456; 1111form.formName = 'MyFormName'; 1112form.bundleName = 'MyBundleName'; 1113form.abilityName = 'MyAbilityName'; 1114form.module = 'MyModule'; 1115let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 1116form.details = { 1117 formKey1: 123, 1118 formKey2: 'formValue', 1119 formKey3: u8Array, 1120}; 1121let unifiedData = new unifiedDataChannel.UnifiedData(form); 1122``` 1123 1124## SystemDefinedAppItem 1125 1126Represents the data of the home screen icon defined by the system. It is a child class of [SystemDefinedRecord](#systemdefinedrecord). 1127 1128**Atomic service API**: This API can be used in atomic services since API version 11. 1129 1130**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1131 1132| Name| Type| Read-Only| Optional| Description| 1133| -------- | -------- | -------- | -------- | -------- | 1134| appId | string | No| No| ID of the application, for which the icon is used. | 1135| appName | string | No| No| Name of the application, for which the icon is used. | 1136| appIconId | string | No| No| Image ID of the icon. | 1137| appLabelId | string | No| No| Label ID corresponding to the icon name. | 1138| bundleName | string | No| No| Bundle name corresponding to the icon.| 1139| abilityName | string | No| No| Application ability name corresponding to the icon.| 1140 1141**Example** 1142 1143```ts 1144let appItem = new unifiedDataChannel.SystemDefinedAppItem(); 1145appItem.appId = 'MyAppId'; 1146appItem.appName = 'MyAppName'; 1147appItem.appIconId = 'MyAppIconId'; 1148appItem.appLabelId = 'MyAppLabelId'; 1149appItem.bundleName = 'MyBundleName'; 1150appItem.abilityName = 'MyAbilityName'; 1151let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 1152appItem.details = { 1153 appItemKey1: 123, 1154 appItemKey2: 'appItemValue', 1155 appItemKey3: u8Array, 1156}; 1157let unifiedData = new unifiedDataChannel.UnifiedData(appItem); 1158``` 1159 1160## SystemDefinedPixelMap 1161 1162Represents the image data type corresponding to [PixelMap](../apis-image-kit/arkts-apis-image-PixelMap.md) defined by the system. It is a child class of [SystemDefinedRecord](#systemdefinedrecord) and holds only binary data of PixelMap. 1163 1164**Atomic service API**: This API can be used in atomic services since API version 11. 1165 1166**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1167 1168| Name| Type| Read-Only| Optional| Description| 1169| -------- | -------- | -------- | -------- | -------- | 1170| rawData | Uint8Array | No| No| Binary data of the **PixelMap** object.| 1171 1172**Example** 1173 1174```ts 1175import { image } from '@kit.ImageKit'; // Module where the PixelMap class is defined. 1176import { unifiedDataChannel, uniformTypeDescriptor } from '@kit.ArkData'; 1177import { BusinessError } from '@kit.BasicServicesKit'; 1178 1179const color = new ArrayBuffer(96); // Create a pixelMap object. 1180let opts: image.InitializationOptions = { 1181 editable: true, pixelFormat: 3, size: { 1182 height: 4, width: 6 1183 } 1184} 1185image.createPixelMap(color, opts, (error, pixelMap) => { 1186 if (error) { 1187 console.error('Failed to create pixelMap.'); 1188 } else { 1189 console.info('Succeeded in creating pixelMap.'); 1190 let arrayBuf = new ArrayBuffer(pixelMap.getPixelBytesNumber()); 1191 pixelMap.readPixelsToBuffer(arrayBuf); 1192 let u8Array = new Uint8Array(arrayBuf); 1193 let sdPixel = new unifiedDataChannel.SystemDefinedPixelMap(); 1194 sdPixel.rawData = u8Array; 1195 let unifiedData = new unifiedDataChannel.UnifiedData(sdPixel); 1196 1197 // Read the record of the pixelMap type from unifiedData. 1198 let records = unifiedData.getRecords(); 1199 for (let i = 0; i < records.length; i++) { 1200 if (records[i].getType() === uniformTypeDescriptor.UniformDataType.OPENHARMONY_PIXEL_MAP) { 1201 let pixelMapRecord = records[i] as unifiedDataChannel.SystemDefinedPixelMap; 1202 let newArrayBuf = pixelMapRecord.rawData.buffer; 1203 pixelMap.writeBufferToPixels(newArrayBuf).then(() => { 1204 console.info('Succeeded in writing data from buffer to a pixelMap'); 1205 }).catch((error: BusinessError) => { 1206 console.error(`Failed to write data from a buffer to a PixelMap. code is ${error.code}, message is ${error.message}`); 1207 }) 1208 } 1209 } 1210 } 1211}) 1212``` 1213 1214## ApplicationDefinedRecord 1215 1216Represents the custom data type for applications only. It is a child class of [UnifiedRecord](#unifiedrecord) and a base class of custom data types of applications. Applications can extend custom data types based on this class. 1217 1218**Atomic service API**: This API can be used in atomic services since API version 11. 1219 1220**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1221 1222| Name| Type| Read-Only| Optional| Description| 1223| -------- | -------- | -------- | -------- | -------- | 1224| applicationDefinedType | string | No| No| Application's custom data type identifier, which must start with **ApplicationDefined**.| 1225| rawData | Uint8Array | No| No| Binary data of the custom data type. | 1226 1227**Example** 1228 1229```ts 1230let record = new unifiedDataChannel.ApplicationDefinedRecord(); 1231let u8Array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 1232record.applicationDefinedType = 'ApplicationDefinedType'; 1233record.rawData = u8Array; 1234let unifiedData = new unifiedDataChannel.UnifiedData(record); 1235``` 1236 1237## Intention 1238 1239Enumerates the data channel types supported by the UDMF. It is used to identify different service scenarios, to which the UDMF data channels apply. 1240 1241**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1242 1243| Name | Value | Description | 1244|----------|-----------|---------| 1245| DATA_HUB | 'DataHub' | Public data channel.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 1246| DRAG<sup>14+</sup> | 'Drag' | Channel in which data can be dragged and dropped.<br>**Model restriction**: This API can be used only in the stage model.<br>**Use scenario**: This API is used to share data across applications in drag-and-drop scenarios.| 1247| SYSTEM_SHARE<sup>20+</sup> | 'SystemShare' | Data channel of the system sharing type.<br>**Model restriction**: This API can be used only in the stage model.<br>**Use scenario**: This API is used to share data across applications in system sharing scenarios.| 1248| PICKER<sup>20+</sup> | 'Picker' | Data channel of the picker type.<br>**Model restriction**: This API can be used only in the stage model.<br>**Use scenario**: This API is used to share data across applications in the scenarios where a picker is used.| 1249| MENU<sup>20+</sup> | 'Menu' | Data channel of the menu type.<br>**Model restriction**: This API can be used only in the stage model.<br>**Use scenario**: This API is used to share data across applications in the shortcut menu.| 1250 1251## Visibility<sup>20+</sup> 1252 1253Enumerates the data visibility levels. 1254 1255**Atomic service API**: This API can be used in atomic services since API version 20. 1256 1257**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1258 1259| Name | Value | Description | 1260| ------------- | ---- |------------------------------| 1261| ALL | 0 | Visible to all applications.<br>**Model restriction**: This API can be used only in the stage model. | 1262| OWN_PROCESS | 1 | Visible only to the data provider.<br>**Model restriction**: This API can be used only in the stage model. | 1263 1264## Options 1265 1266Defines the data operation performed by the UDMF. It includes three optional parameters: **intention**, **key**, and **visibility**. The three parameters can be left unspecified. For details, see the parameter description of the specific API. 1267 1268**Atomic service API**: This API can be used in atomic services since API version 11. 1269 1270**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1271 1272| Name | Type | Mandatory| Description | 1273| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 1274| intention | [Intention](#intention) | No | Type of the data channel related to the data operation. | 1275| key | string | No | Unique identifier of the data object in the UDMF, which can be obtained from the value returned by [insertData](#unifieddatachannelinsertdata).<br>The key consists of **udmf:/**, **intention**, **bundleName**, and **groupId** with a (/) in between, for example, **udmf://DataHub/com.ohos.test/0123456789**.<br>**udmf:/** is fixed, **DataHub** is an enum of **intention**, **com.ohos.test** is the bundle name, and **0123456789** is a group ID randomly generated.| 1276| visibility<sup>20+</sup> | [Visibility](#visibility20) | No | Data visibility level. This parameter is effective only when specified during data writing. If unspecified, the default value **Visibility.ALL** is used. | 1277 1278## FileConflictOptions<sup>15+</sup> 1279 1280Enumerates the options for resolving file copy conflicts. 1281 1282**Atomic service API**: This API can be used in atomic services since API version 15. 1283 1284**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1285 1286| Name | Value | Description | 1287| --------- | ---- |----------------| 1288| OVERWRITE | 0 | Overwrite the file with the same name in the destination directory.| 1289| SKIP | 1 | Skip the file if there is a file with the same name in the destination directory.| 1290 1291## ProgressIndicator<sup>15+</sup> 1292 1293Enumerates the progress indicator options. 1294 1295**Atomic service API**: This API can be used in atomic services since API version 15. 1296 1297**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1298 1299| Name | Value | Description | 1300| ------- | ---- |------------------------------------| 1301| NONE | 0 | Do not use the default progress indicator. | 1302| DEFAULT | 1 | Use the default progress indicator. If data is obtained within 500 ms, the default progress bar is not started.| 1303 1304## ListenerStatus<sup>15+</sup> 1305 1306Enumerates the status codes returned when data is obtained from the UDMF. 1307 1308**Atomic service API**: This API can be used in atomic services since API version 15. 1309 1310**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1311 1312| Name | Value | Description | 1313| ------- |-----|----------------------------------------------| 1314| FINISHED | 0 | The task is completed. | 1315| PROCESSING | 1 | The task is being processed. | 1316| CANCELED | 2 | The task is canceled. | 1317| INNER_ERROR | 200 | An internal error occurs. | 1318| INVALID_PARAMETERS | 201 | [GetDataParams](#getdataparams15) contains invalid parameters.| 1319| DATA_NOT_FOUND | 202 | No data is obtained. | 1320| SYNC_FAILED | 203 | Failed to sync data. | 1321| COPY_FILE_FAILED | 204 | Failed to copy data. | 1322 1323## ProgressInfo<sup>15+</sup> 1324 1325Represents the progress information. 1326 1327**Atomic service API**: This API can be used in atomic services since API version 15. 1328 1329**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1330 1331| Name | Type | Read-Only| Optional| Description | 1332| -------- |-------------------------------------| ---- | ---- |----------------------------------------------------------------| 1333| progress | number | No | No | Progress of the drag task, in percentage. <br>The value is an integer ranging from -1 to 100. The value **-1** indicates a failure to obtain data, and the value **100** indicates data is obtained.| 1334| status | [ListenerStatus](#listenerstatus15) | No | No | Status code of the drag task reported by the system. | 1335 1336## DataProgressListener<sup>15+</sup> 1337 1338type DataProgressListener = (progressInfo: ProgressInfo, data: UnifiedData | null) => void 1339 1340Defines the callback used to return the data retrieval progress information and data obtained. 1341 1342**Atomic service API**: This API can be used in atomic services since API version 15. 1343 1344**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1345 1346**Parameters** 1347 1348| Name | Type | Mandatory | Description | 1349|----------|-------------------------------|-------|--------------| 1350| progressInfo| [ProgressInfo](#progressinfo15) | Yes | Progress information to report.| 1351| data | [UnifiedData](#unifieddata) \| null | Yes | Data obtained when the progress reaches 100. If the progress does not reach 100, **null** is returned.| 1352 1353## GetDataParams<sup>15+</sup> 1354 1355Represents the parameters for obtaining data from UDMF, including the destination directory, option for resolving file conflicts, and progress indicator type. 1356 1357For details, see [Obtaining Data Asynchronously Through Drag-and-Drop](../apis-arkui/arkui-ts/ts-universal-events-drag-drop.md#example-3-obtaining-data-asynchronously-through-drag-and-drop). 1358 1359**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1360 1361**Parameters** 1362 1363| Name | Type | Read-Only| Optional| Description | 1364|----------------------|-------------------------------------------------| ---- | ---- |----------------------------------------------------------------------------------------------------------------------------------------------------| 1365| progressIndicator | [ProgressIndicator](#progressindicator15) | No | No | Progress indicator options. You can use the default progress indicator as required.<br>**Atomic service API**: This API can be used in atomic services since API version 15. | 1366| dataProgressListener | [DataProgressListener](#dataprogresslistener15) | No | No | Callback used to return the data retrieval progress and data obtained.<br>**Atomic service API**: This API can be used in atomic services since API version 15. | 1367| destUri | string | No | Yes | Destination directory for the file copied. If file processing is not supported, leave this parameter unspecified, which is the default value of this parameter. If file processing is supported, pass in an existing directory. If complex file processing policies are involved or multipathing file storage is required, you are advised to leave this parameter unspecified and let the application handle file copying. If this parameter is not specified, the source URI is obtained. If this parameter is specified, the specified destination URI is obtained.<br>**Atomic service API**: This API can be used in atomic services since API version 15.| 1368| fileConflictOptions | [FileConflictOptions](#fileconflictoptions15) | No | Yes | Option for resolving file copy conflicts. The default value is **OVERWRITE**.<br>**Atomic service API**: This API can be used in atomic services since API version 15. | 1369| acceptableInfo<sup>20+</sup> | [DataLoadInfo](#dataloadinfo20) | No | Yes | Capability of the receiver to receive data types and data records. In the lazy loading scenario, the sender can generate and return more appropriate data content based on this information. The default value is empty, indicating that the receiver does not have the data receiving capability.<br>**Atomic service API**: This API can be used in atomic services since API version 20. | 1370 1371## DataLoadInfo<sup>20+</sup> 1372 1373Defines type and quantity of the data to load. 1374 1375- Used by the **data sender** to define the data range that can be provided. This field is mandatory. 1376- Used by the **data receiver** to define the expected data type and quantity. This field is optional. 1377 1378**Atomic service API**: This API can be used in atomic services since API version 20. 1379 1380**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1381 1382**Parameters** 1383 1384| Name | Type | Mandatory| Description | 1385|----------------------|-------------------------------------------------| ---- |----------------------------------------------------------------------------------------------------------------------------------------------------| 1386| types | Set\<string\> | No| Data type set. The default value is an empty set. | 1387| recordCount | number | No| Maximum number of data records, ranging from 0 to 2<sup>32</sup>-1. The default value is **0**. If the value is out of the range, the default value is used. If the value is a floating point number, only the integer part is used. If the value is used for drag and drop, it is displayed as the number of badges with a maximum value of **2<sup>31</sup>-1**. If the value exceeds the maximum, no badge is displayed. In addition, the priority of this API is lower than the **numberBadge** method in [DragPreviewOptions](../apis-arkui/arkui-ts/ts-universal-attributes-drag-drop.md#dragpreviewoptions11). | 1388 1389## DataLoadHandler<sup>20+</sup> 1390 1391type DataLoadHandler = (acceptableInfo?: DataLoadInfo) => UnifiedData | null 1392 1393Defines a processing function for lazy data loading. The data sender can dynamically generate data based on the information passed by the data receiver to implement more flexible and precise data interaction policies. 1394 1395**Atomic service API**: This API can be used in atomic services since API version 20. 1396 1397**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1398 1399**Parameters** 1400 1401| Name | Type | Mandatory | Description | 1402|----------|-------------------------------|-------|--------------| 1403| acceptableInfo | [DataLoadInfo](#dataloadinfo20) | No | Data type and quantity to receive. The default value is empty.| 1404 1405**Return value** 1406 1407| Type | Description | 1408|-----------------------|-----------------------------------| 1409| [UnifiedData](#unifieddata) \| null | Returns **UnifiedData** or **null** when the processing function for lazy data loading is triggered.| 1410 1411## DataLoadParams<sup>20+</sup> 1412 1413Defines the data loading policy for the data sender in the lazy loading scenario. 1414 1415**Atomic service API**: This API can be used in atomic services since API version 20. 1416 1417**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1418 1419**Parameters** 1420 1421| Name | Type | Mandatory| Description | 1422|----------------------|-------------------------------------------------| ---- |----------------------------------------------------------------------------------------------------------------------------------------------------| 1423| loadHandler | [DataLoadHandler](#dataloadhandler20) | Yes| Processing function used for lazy data loading. | 1424| dataLoadInfo | [DataLoadInfo](#dataloadinfo20) | Yes| Data type and quantity that can be generated by the sender. | 1425 1426## unifiedDataChannel.insertData 1427 1428insertData(options: Options, data: UnifiedData, callback: AsyncCallback<string>): void 1429 1430Inserts data to the UDMF public data channel. This API uses an asynchronous callback to return the unique identifier of the data inserted. 1431 1432**Atomic service API**: This API can be used in atomic services since API version 11. 1433 1434**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1435 1436**Parameters** 1437 1438| Name | Type | Mandatory| Description | 1439|----------|----------------------------|----|------------------------------| 1440| options | [Options](#options) | Yes | Configuration for the data insertion operation. The **intention** field is mandatory. If it is not specified, error code 401 will be returned. The settings of other parameters do not affect the use of this API. | 1441| data | [UnifiedData](#unifieddata) | Yes | Data to insert. | 1442| callback | AsyncCallback<string> | Yes | Callback used to return the key (unique identifier) of the data inserted.| 1443 1444**Error codes** 1445 1446For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1447 1448| **ID**| **Error Message** | 1449| ------------ | ------------------------------------------- | 1450| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1451 1452**Example** 1453 1454```ts 1455import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 1456import { BusinessError } from '@kit.BasicServicesKit'; 1457 1458let plainText : uniformDataStruct.PlainText = { 1459 uniformDataType: 'general.plain-text', 1460 textContent : 'This is a plain text example', 1461 abstract : 'This is abstract', 1462} 1463let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 1464let unifiedData = new unifiedDataChannel.UnifiedData(text); 1465 1466let options: unifiedDataChannel.Options = { 1467 intention: unifiedDataChannel.Intention.DATA_HUB 1468} 1469try { 1470 unifiedDataChannel.insertData(options, unifiedData, (err, key) => { 1471 if (err === undefined) { 1472 console.info(`Succeeded in inserting data. key = ${key}`); 1473 } else { 1474 console.error(`Failed to insert data. code is ${err.code}, message is ${err.message} `); 1475 } 1476 }); 1477} catch (e) { 1478 let error: BusinessError = e as BusinessError; 1479 console.error(`Insert data throws an exception. code is ${error.code}, message is ${error.message} `); 1480} 1481``` 1482 1483## unifiedDataChannel.insertData 1484 1485insertData(options: Options, data: UnifiedData): Promise<string> 1486 1487Inserts data to the UDMF public data channel. This API uses a promise to return the unique identifier of the data inserted. 1488 1489**Atomic service API**: This API can be used in atomic services since API version 11. 1490 1491**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1492 1493**Parameters** 1494 1495| Name | Type | Mandatory| Description | 1496|---------|-----------------------------|----|-----------------------| 1497| options | [Options](#options) | Yes | Configuration for the data insertion operation. The **intention** field is mandatory. If it is not specified, error code 401 will be returned. The settings of other parameters do not affect the use of this API.| 1498| data | [UnifiedData](#unifieddata) | Yes | Data to insert. | 1499 1500**Return value** 1501 1502| Type | Description | 1503|-----------------------|-----------------------------------| 1504| Promise<string> | Promise used to return the key of the data inserted.| 1505 1506**Error codes** 1507 1508For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1509 1510| **ID**| **Error Message** | 1511| ------------ | ------------------------------------------- | 1512| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1513 1514**Example** 1515 1516```ts 1517import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 1518import { BusinessError } from '@kit.BasicServicesKit'; 1519 1520let plainText : uniformDataStruct.PlainText = { 1521 uniformDataType: 'general.plain-text', 1522 textContent : 'This is a plain text example', 1523 abstract : 'This is abstract', 1524} 1525let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 1526let unifiedData = new unifiedDataChannel.UnifiedData(text); 1527 1528let options: unifiedDataChannel.Options = { 1529 intention: unifiedDataChannel.Intention.DATA_HUB 1530} 1531try { 1532 unifiedDataChannel.insertData(options, unifiedData).then((key) => { 1533 console.info(`Succeeded in inserting data. key = ${key}`); 1534 }).catch((err: BusinessError) => { 1535 console.error(`Failed to insert data. code is ${err.code}, message is ${err.message} `); 1536 }); 1537} catch (e) { 1538 let error: BusinessError = e as BusinessError; 1539 console.error(`Insert data throws an exception. code is ${error.code}, message is ${error.message} `); 1540} 1541``` 1542 1543## unifiedDataChannel.updateData 1544 1545updateData(options: Options, data: UnifiedData, callback: AsyncCallback<void>): void 1546 1547Updates the data in the UDMF public data channel. This API uses an asynchronous callback to return the result. 1548 1549**Atomic service API**: This API can be used in atomic services since API version 11. 1550 1551**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1552 1553**Parameters** 1554 1555| Name | Type | Mandatory| Description | 1556|----------|-----------------------------|----|-------------------------------------| 1557| options | [Options](#options) | Yes | Configuration for the data update operation. The **key** field is mandatory. If it is not specified, error code 401 will be returned. The settings of other parameters do not affect the use of this API. | 1558| data | [UnifiedData](#unifieddata) | Yes | Data to update. | 1559| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the data is updated successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 1560 1561**Error codes** 1562 1563For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1564 1565| **ID**| **Error Message** | 1566| ------------ | ------------------------------------------- | 1567| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1568 1569**Example** 1570 1571```ts 1572import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 1573import { BusinessError } from '@kit.BasicServicesKit'; 1574 1575let plainText : uniformDataStruct.PlainText = { 1576 uniformDataType: 'general.plain-text', 1577 textContent : 'This is a plain text example', 1578 abstract : 'This is abstract', 1579} 1580let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 1581let unifiedData = new unifiedDataChannel.UnifiedData(text); 1582let options: unifiedDataChannel.Options = { 1583 intention: unifiedDataChannel.Intention.DATA_HUB 1584} 1585try { 1586 unifiedDataChannel.insertData(options, unifiedData).then((key) => { 1587 console.info(`Succeeded in inserting data. key = ${key}`); 1588 let updateOptions: unifiedDataChannel.Options = { 1589 intention: unifiedDataChannel.Intention.DATA_HUB, 1590 key: key 1591 } 1592 let plainTextUpdate : uniformDataStruct.PlainText = { 1593 uniformDataType: 'general.plain-text', 1594 textContent : 'This is plainText textContent for update', 1595 abstract : 'This is abstract for update', 1596 } 1597 let textUpdate = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainTextUpdate); 1598 let unifiedDataUpdate = new unifiedDataChannel.UnifiedData(textUpdate); 1599 try { 1600 unifiedDataChannel.updateData(updateOptions, unifiedDataUpdate, (err) => { 1601 if (err === undefined) { 1602 console.info('Succeeded in updating data.'); 1603 } else { 1604 console.error(`Failed to update data. code is ${err.code}, message is ${err.message} `); 1605 } 1606 }); 1607 } catch (e) { 1608 let error: BusinessError = e as BusinessError; 1609 console.error(`Update data throws an exception. code is ${error.code}, message is ${error.message} `); 1610 } 1611 }).catch((err: BusinessError) => { 1612 console.error(`Failed to insert data. code is ${err.code}, message is ${err.message} `); 1613 }); 1614} catch (e) { 1615 let error: BusinessError = e as BusinessError; 1616 console.error(`Insert data throws an exception. code is ${error.code}, message is ${error.message} `); 1617} 1618``` 1619 1620## unifiedDataChannel.updateData 1621 1622updateData(options: Options, data: UnifiedData): Promise<void> 1623 1624Updates the data in the UDMF public data channel. This API uses a promise to return the result. 1625 1626**Atomic service API**: This API can be used in atomic services since API version 11. 1627 1628**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1629 1630**Parameters** 1631 1632| Name | Type | Mandatory| Description | 1633|---------|-----------------------------|----|-----------------| 1634| options | [Options](#options) | Yes | Configuration for the data update operation. The **key** field is mandatory. If it is not specified, error code 401 will be returned. The settings of other parameters do not affect the use of this API.| 1635| data | [UnifiedData](#unifieddata) | Yes | Data to update. | 1636 1637**Return value** 1638 1639| Type | Description | 1640|---------------------|----------------------------| 1641| Promise<void> | Promise that returns no value.| 1642 1643**Error codes** 1644 1645For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1646 1647| **ID**| **Error Message** | 1648| ------------ | ------------------------------------------- | 1649| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1650 1651**Example** 1652 1653```ts 1654import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 1655import { BusinessError } from '@kit.BasicServicesKit'; 1656 1657let plainText : uniformDataStruct.PlainText = { 1658 uniformDataType: 'general.plain-text', 1659 textContent : 'This is a plain text example', 1660 abstract : 'This is abstract', 1661} 1662let text = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 1663let unifiedData = new unifiedDataChannel.UnifiedData(text); 1664let options: unifiedDataChannel.Options = { 1665 intention: unifiedDataChannel.Intention.DATA_HUB 1666} 1667try { 1668 unifiedDataChannel.insertData(options, unifiedData).then((key) => { 1669 console.info(`Succeeded in inserting data. key = ${key}`); 1670 let updateOptions: unifiedDataChannel.Options = { 1671 intention: unifiedDataChannel.Intention.DATA_HUB, 1672 key: key 1673 } 1674 let plainTextUpdate : uniformDataStruct.PlainText = { 1675 uniformDataType: 'general.plain-text', 1676 textContent : 'This is plainText textContent for update', 1677 abstract : 'This is abstract for update', 1678 } 1679 let textUpdate = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainTextUpdate); 1680 let unifiedDataUpdate = new unifiedDataChannel.UnifiedData(textUpdate); 1681 try { 1682 unifiedDataChannel.updateData(updateOptions, unifiedDataUpdate).then(() => { 1683 console.info('Succeeded in updating data.'); 1684 }).catch((err: BusinessError) => { 1685 console.error(`Failed to update data. code is ${err.code}, message is ${err.message} `); 1686 }); 1687 } catch (e) { 1688 let error: BusinessError = e as BusinessError; 1689 console.error(`Update data throws an exception. code is ${error.code}, message is ${error.message} `); 1690 } 1691 }).catch((err: BusinessError) => { 1692 console.error(`Failed to insert data. code is ${err.code}, message is ${err.message} `); 1693 }); 1694} catch (e) { 1695 let error: BusinessError = e as BusinessError; 1696 console.error(`Insert data throws an exception. code is ${error.code}, message is ${error.message} `); 1697} 1698``` 1699 1700## unifiedDataChannel.queryData 1701 1702queryData(options: Options, callback: AsyncCallback<Array<UnifiedData>>): void 1703 1704Queries data in the UDMF public data channel. This API uses an asynchronous callback to return the result. 1705 1706**Atomic service API**: This API can be used in atomic services since API version 11. 1707 1708**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1709 1710**Parameters** 1711 1712| Name | Type | Mandatory| Description | 1713|----------|---------------------------------------------------------------|----|------------------------------------------------------------------------------------------------------------------------------------------------------------------| 1714| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in. | 1715| callback | AsyncCallback<Array<[UnifiedData](#unifieddata)>> | Yes | Callback used to return the queried data.<br>If only the **key** is specified in **options**, the data corresponding to the key is returned.<br>If only the **intention** is specified in **options**, all data in the **intention** is returned.<br>If both **intention** and **key** are specified, the intersection of the two is returned, which is the result obtained when only **key** is specified. If there is no intersection between the specified **intention** and **key**, an error object is returned.| 1716 1717**Error codes** 1718 1719For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1720 1721| **ID**| **Error Message** | 1722| ------------ | ------------------------------------------- | 1723| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1724 1725**Example** 1726 1727```ts 1728import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 1729import { BusinessError } from '@kit.BasicServicesKit'; 1730 1731let options: unifiedDataChannel.Options = { 1732 intention: unifiedDataChannel.Intention.DATA_HUB 1733}; 1734 1735try { 1736 unifiedDataChannel.queryData(options, (err, data) => { 1737 if (err === undefined) { 1738 console.info(`Succeeded in querying data. size = ${data.length}`); 1739 for (let i = 0; i < data.length; i++) { 1740 let records = data[i].getRecords(); 1741 for (let j = 0; j < records.length; j++) { 1742 if (records[j].getTypes().includes(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT)) { 1743 let text = records[j].getEntry(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) as uniformDataStruct.PlainText; 1744 console.info(`${i + 1}.${text.textContent}`); 1745 } 1746 } 1747 } 1748 } else { 1749 console.error(`Failed to query data. code is ${err.code}, message is ${err.message} `); 1750 } 1751 }); 1752} catch (e) { 1753 let error: BusinessError = e as BusinessError; 1754 console.error(`Query data throws an exception. code is ${error.code}, message is ${error.message} `); 1755} 1756``` 1757 1758## unifiedDataChannel.queryData 1759 1760queryData(options: Options): Promise<Array<UnifiedData>> 1761 1762Queries data in the UDMF public data channel. This API uses a promise to return the result. 1763 1764**Atomic service API**: This API can be used in atomic services since API version 11. 1765 1766**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1767 1768**Parameters** 1769 1770| Name | Type | Mandatory| Description | 1771|---------|---------------------|----|-----------------------------------------------| 1772| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in.| 1773 1774**Return value** 1775 1776| Type | Description | 1777|---------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------| 1778| Promise<Array<[UnifiedData](#unifieddata)>> | Promise used to return the result.<br>If only the **key** is specified in **options**, the data corresponding to the key is returned.<br>If only the **intention** is specified in **options**, all data in the **intention** is returned.<br>If both **intention** and **key** are specified, the intersection of the two is returned, which is the result obtained when only **key** is specified. If there is no intersection between the specified **intention** and **key**, an error object is returned.| 1779 1780**Error codes** 1781 1782For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1783 1784| **ID**| **Error Message** | 1785| ------------ | ------------------------------------------- | 1786| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1787 1788**Example** 1789 1790```ts 1791import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 1792import { BusinessError } from '@kit.BasicServicesKit'; 1793 1794let options: unifiedDataChannel.Options = { 1795 key: 'udmf://DataHub/com.ohos.test/0123456789' 1796}; 1797 1798try { 1799 unifiedDataChannel.queryData(options).then((data) => { 1800 console.info(`Succeeded in querying data. size = ${data.length}`); 1801 for (let i = 0; i < data.length; i++) { 1802 let records = data[i].getRecords(); 1803 for (let j = 0; j < records.length; j++) { 1804 if (records[j].getTypes().includes(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT)) { 1805 let text = records[j].getEntry(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) as uniformDataStruct.PlainText; 1806 console.info(`${i + 1}.${text.textContent}`); 1807 } 1808 } 1809 } 1810 }).catch((err: BusinessError) => { 1811 console.error(`Failed to query data. code is ${err.code}, message is ${err.message} `); 1812 }); 1813} catch (e) { 1814 let error: BusinessError = e as BusinessError; 1815 console.error(`Query data throws an exception. code is ${error.code}, message is ${error.message} `); 1816} 1817``` 1818 1819## unifiedDataChannel.deleteData 1820 1821deleteData(options: Options, callback: AsyncCallback<Array<UnifiedData>>): void 1822 1823Deletes data from the UDMF public data channel. This API uses an asynchronous callback to return the result. 1824 1825**Atomic service API**: This API can be used in atomic services since API version 11. 1826 1827**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1828 1829**Parameters** 1830 1831| Name | Type | Mandatory| Description | 1832|----------|---------------------------------------------------------------|----|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 1833| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in. | 1834| callback | AsyncCallback<Array<[UnifiedData](#unifieddata)>> | Yes | Callback used to return the data deleted.<br>If only the **key** is specified in **options**, the data corresponding to the key deleted is returned.<br>If only the **intention** is specified in **options**, all data in the **intention** deleted is returned.<br>If both **intention** and **key** are specified, the intersection of the two deleted is returned. If there is no intersection between the two, an error object is returned.| 1835 1836**Error codes** 1837 1838For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1839 1840| **ID**| **Error Message** | 1841| ------------ | ------------------------------------------- | 1842| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1843 1844**Example** 1845 1846```ts 1847import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 1848import { BusinessError } from '@kit.BasicServicesKit'; 1849 1850let options: unifiedDataChannel.Options = { 1851 intention: unifiedDataChannel.Intention.DATA_HUB 1852}; 1853 1854try { 1855 unifiedDataChannel.deleteData(options, (err, data) => { 1856 if (err === undefined) { 1857 console.info(`Succeeded in deleting data. size = ${data.length}`); 1858 for (let i = 0; i < data.length; i++) { 1859 let records = data[i].getRecords(); 1860 for (let j = 0; j < records.length; j++) { 1861 if (records[j].getTypes().includes(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT)) { 1862 let text = records[j].getEntry(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) as uniformDataStruct.PlainText; 1863 console.info(`${i + 1}.${text.textContent}`); 1864 } 1865 } 1866 } 1867 } else { 1868 console.error(`Failed to delete data. code is ${err.code}, message is ${err.message} `); 1869 } 1870 }); 1871} catch (e) { 1872 let error: BusinessError = e as BusinessError; 1873 console.error(`Delete data throws an exception. code is ${error.code}, message is ${error.message} `); 1874} 1875``` 1876 1877## unifiedDataChannel.deleteData 1878 1879deleteData(options: Options): Promise<Array<UnifiedData>> 1880 1881Deletes data from the UDMF public data channel. This API uses a promise to return the result. 1882 1883**Atomic service API**: This API can be used in atomic services since API version 11. 1884 1885**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1886 1887**Parameters** 1888 1889| Name | Type | Mandatory| Description | 1890|---------|---------------------|----|--------| 1891| options | [Options](#options) | Yes | Configuration parameters. Both the **key** and **intention** are optional, and the return value varies depending on the parameters passed in.| 1892 1893**Return value** 1894 1895| Type | Description | 1896|---------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| 1897| Promise<Array<[UnifiedData](#unifieddata)>> | Promise used to return the data deleted.<br>If only the **key** is specified in **options**, the data corresponding to the key deleted is returned.<br>If only the **intention** is specified in **options**, all data in the **intention** deleted is returned.<br>If both **intention** and **key** are specified, the intersection of the two deleted is returned. If there is no intersection between the two, an error object is returned.| 1898 1899**Error codes** 1900 1901For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1902 1903| **ID**| **Error Message** | 1904| ------------ | ------------------------------------------- | 1905| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1906 1907**Example** 1908 1909```ts 1910import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 1911import { BusinessError } from '@kit.BasicServicesKit'; 1912 1913let options: unifiedDataChannel.Options = { 1914 key: 'udmf://DataHub/com.ohos.test/0123456789' 1915}; 1916 1917try { 1918 unifiedDataChannel.deleteData(options).then((data) => { 1919 console.info(`Succeeded in deleting data. size = ${data.length}`); 1920 for (let i = 0; i < data.length; i++) { 1921 let records = data[i].getRecords(); 1922 for (let j = 0; j < records.length; j++) { 1923 if (records[j].getTypes().includes(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT)) { 1924 let text = records[j].getEntry(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) as uniformDataStruct.PlainText; 1925 console.info(`${i + 1}.${text.textContent}`); 1926 } 1927 } 1928 } 1929 }).catch((err: BusinessError) => { 1930 console.error(`Failed to delete data. code is ${err.code}, message is ${err.message} `); 1931 }); 1932} catch (e) { 1933 let error: BusinessError = e as BusinessError; 1934 console.error(`Query data throws an exception. code is ${error.code}, message is ${error.message} `); 1935} 1936``` 1937 1938## unifiedDataChannel.setAppShareOptions<sup>14+</sup> 1939 1940setAppShareOptions(intention: Intention, shareOptions: ShareOptions): void 1941 1942Sets the [ShareOptions](#shareoptions12) for the application data. Currently, only the drag-and-drop data channel is supported. 1943 1944**Required permissions**: ohos.permission.MANAGE_UDMF_APP_SHARE_OPTION 1945 1946**Model restriction**: This API can be used only in the stage model. 1947 1948**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1949 1950**Parameters** 1951 1952| Name | Type | Mandatory| Description | 1953|----------|----------------------------|----|------------------------------| 1954| intention | [Intention](#intention) | Yes | Type of the data channel. Currently, only the data channel of the **DRAG** type is supported.| 1955| shareOptions | [ShareOptions](#shareoptions12) | Yes | Usage scope of the [UnifiedData](#unifieddata).| 1956 1957**Error codes** 1958 1959For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [UDMF Error Codes](errorcode-udmf.md). 1960 1961| **ID**| **Error Message** | 1962| ------------ | ------------------------------------------------------------ | 1963| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1964| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1965| 20400001 | Settings already exist. To reconfigure, remove the existing sharing options. | 1966 1967**Example** 1968 1969```ts 1970import { BusinessError } from '@kit.BasicServicesKit'; 1971try { 1972 unifiedDataChannel.setAppShareOptions(unifiedDataChannel.Intention.DRAG, unifiedDataChannel.ShareOptions.IN_APP); 1973 console.info(`[UDMF]setAppShareOptions success. `); 1974}catch (e){ 1975 let error: BusinessError = e as BusinessError; 1976 console.error(`[UDMF]setAppShareOptions throws an exception. code is ${error.code}, message is ${error.message} `); 1977} 1978``` 1979 1980## unifiedDataChannel.removeAppShareOptions<sup>14+</sup> 1981 1982removeAppShareOptions(intention: Intention): void 1983 1984Removes the data control information set by [setAppShareOptions](#unifieddatachannelsetappshareoptions14). 1985 1986**Required permissions**: ohos.permission.MANAGE_UDMF_APP_SHARE_OPTION 1987 1988**Model restriction**: This API can be used only in the stage model. 1989 1990**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 1991 1992**Parameters** 1993 1994| Name | Type | Mandatory| Description | 1995| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 1996| intention | [Intention](#intention) | Yes | Type of the data channel. Currently, only the data channel of the **DRAG** type is supported.| 1997 1998**Error codes** 1999 2000For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2001 2002| **ID**| **Error Message** | 2003| ------------ | ------------------------------------------------------------ | 2004| 201 | Permission verification failed. The application does not have the permission required to call the API. | 2005| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2006 2007**Example** 2008 2009```ts 2010import { BusinessError } from '@kit.BasicServicesKit'; 2011try { 2012 unifiedDataChannel.removeAppShareOptions(unifiedDataChannel.Intention.DRAG); 2013 console.info(`[UDMF]removeAppShareOptions success. `); 2014}catch (e){ 2015 let error: BusinessError = e as BusinessError; 2016 console.error(`[UDMF]removeAppShareOptions throws an exception. code is ${error.code}, message is ${error.message} `); 2017} 2018``` 2019 2020## unifiedDataChannel.convertRecordsToEntries<sup>17+</sup> 2021 2022convertRecordsToEntries(data: UnifiedData): void 2023 2024Converts the provided data into a multi-style data structure, which is useful when the original data uses multiple records to represent different styles of the same data. 2025 2026This API is used only when the following rules are met: 20271. The number of records in data is greater than 1. 20282. The value of **unifiedData.properties.tag** is **records_to_entries_data_format**. 2029 2030 2031 2032**Atomic service API**: This API can be used in atomic services since API version 17. 2033 2034**Model restriction**: This API can be used only in the stage model. 2035 2036**System capability**: SystemCapability.DistributedDataManager.UDMF.Core 2037 2038**Parameters** 2039 2040| Name | Type | Mandatory| Description | 2041| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 2042| data | [UnifiedData](#unifieddata) | Yes | Data to convert. | 2043 2044**Error codes** 2045 2046For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2047 2048| **ID**| **Error Message** | 2049| ------------ | ------------------------------------------------------------ | 2050| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2051 2052**Example** 2053 2054```ts 2055import { unifiedDataChannel } from '@kit.ArkData'; 2056import { uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 2057import { BusinessError } from '@kit.BasicServicesKit'; 2058 2059let details : Record<string, string> = { 2060 'attr1': 'value1', 2061 'attr2': 'value2', 2062} 2063let plainTextObj : uniformDataStruct.PlainText = { 2064 uniformDataType: 'general.plain-text', 2065 textContent : 'The weather is very good today', 2066 abstract : 'The weather is very good today', 2067 details : details, 2068} 2069let htmlObj : uniformDataStruct.HTML = { 2070 uniformDataType :'general.html', 2071 htmlContent : '<div><p>The weather is very good today</p></div>', 2072 plainContent : 'The weather is very good today', 2073 details : details, 2074} 2075let plainText = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainTextObj); 2076let html = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HTML, htmlObj); 2077let unifiedData = new unifiedDataChannel.UnifiedData(plainText); 2078unifiedData.addRecord(html); 2079unifiedData.properties.tag = 'records_to_entries_data_format'; 2080 2081try { 2082 unifiedDataChannel.convertRecordsToEntries(unifiedData); 2083 let records: Array<unifiedDataChannel.UnifiedRecord> = unifiedData.getRecords(); 2084 console.info(`Records size is ${records.length}`); // After conversion, its length must be less than 1 2085 if (records.length == 1) { 2086 let plainTextObjRead: uniformDataStruct.PlainText = records[0].getEntry(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) as uniformDataStruct.PlainText; 2087 console.info(`TextContent is ${plainTextObjRead.textContent}`); 2088 let htmlObjRead: uniformDataStruct.HTML = records[0].getEntry(uniformTypeDescriptor.UniformDataType.HTML) as uniformDataStruct.HTML; 2089 console.info(`HtmlContent is ${htmlObjRead.htmlContent}`); 2090 } 2091} catch (e) { 2092 let error: BusinessError = e as BusinessError; 2093 console.error(`Convert data throws an exception. code is ${error.code}, message is ${error.message} `); 2094} 2095``` 2096