HarmonyOS Next Part Three: Analysis of Standardized Data Types - A Detailed Explanation of UTD

xun_wang_6384a403f9817c2

SameX

Posted on November 15, 2024

HarmonyOS Next Part Three: Analysis of Standardized Data Types - A Detailed Explanation of UTD

This article aims to deeply explore the technical details of the Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices. It mainly serves as a carrier for technical sharing and communication. Mistakes and omissions are inevitable. Colleagues are welcome to put forward valuable opinions and questions so that we can make progress together. This article is original content, and any form of reprint must indicate the source and the original author.
This article will conduct an in-depth exploration of the core concept in Huawei HarmonyOS Next - the Standardized Data Type (Uniform Type Descriptor, abbreviated as UTD), and provide a detailed analysis of its design and functions, including aspects such as the ID of the standardized data type, hierarchical affiliation, cross-application and cross-device compatibility. Meanwhile, this article will also introduce how to create custom data types and use them in combination with preset data types to demonstrate the flexibility and practicality of UTD.

Design and Functions of UTD

Definition of UTD

UTD is a core concept in Huawei HarmonyOS Next. It defines a unique identifier and a set of attributes for different types of data, such as the type of affiliation, brief description, file extension, MIME type, etc. The introduction of UTD aims to solve the problem of ambiguous types in the HarmonyOS system, for example, different applications may describe the same type of data in different ways, thereby improving the compatibility between different applications and the efficiency of data interaction.

Advantages of UTD

  • Uniqueness of Types: UTD defines a unique identifier for each data type, such as general.image, general.video, etc., avoiding type ambiguity and confusion.
  • Hierarchical Affiliation: UTD classifies types using a hierarchical structure, making it convenient for developers to understand and use. For example, the image type can be affiliated with the graphic type, and the graphic type can be affiliated with the media type.
  • Cross-Application Compatibility: UTD improves the compatibility between different applications, enabling applications to share and exchange data more easily.
  • Cross-Device Compatibility: UTD supports cross-device data synchronization, allowing applications to effortlessly share data between different devices. ### Analysis of UTD #### ID of UTD The ID of UTD is a unique string used to identify a specific data type. For example, the UTD ID of the image type is general.image, and the UTD ID of the video type is general.video. #### Hierarchical Affiliation of UTD The hierarchical affiliation of UTD refers to which higher-level type a certain data type belongs to. For example, the image type can be affiliated with the graphic type, and the graphic type can be affiliated with the media type. Hierarchical affiliation helps developers understand the relationships between data types and facilitates data management and operations. #### Compatibility of UTD The compatibility of UTD refers to the consistency of the recognition and understanding of data types among different applications and devices. UDMF ensures seamless data exchange and sharing among different applications and devices by defining a unified set of data type standards. ### Creating Custom Data Types Developers can create custom data types according to their own needs and register them in the system so that other applications can reference and use them. Example Code:
// Create a custom data type
let customType = new uniformTypeDescriptor.TypeDescriptor({
  typeId: 'com.example.custom-type',
  belongingToTypes: ['general.object'],
  description: 'Custom data type',
  filenameExtensions: ['.custom'],
  mimeTypes: ['application/custom-type'],
});
// Register the custom data type
uniformTypeDescriptor.registerTypeDescriptor(customType);
// Use the custom data type in other applications
let customTypeObject = uniformTypeDescriptor.getTypeDescriptor('com.example.custom-type');
console.log(customTypeObject.description); // Output: Custom data type
Enter fullscreen mode Exit fullscreen mode

Combined Use of Preset Data Types and Custom Data Types

Developers can combine custom data types with preset data types to meet more complex data management requirements.
Example Code:

// Create a custom data type
let customType = new uniformTypeDescriptor.TypeDescriptor({
  typeId: 'com.example.custom-image',
  belongingToTypes: ['general.image'],
  description: 'Custom image type',
  filenameExtensions: ['.custom-image'],
  mimeTypes: ['application/custom-image'],
});
// Register the custom data type
uniformTypeDescriptor.registerTypeDescriptor(customType);
// Create a custom image data structure
let customImageStruct = new uniformDataStruct.ImageStruct({
  uniformDataType: 'com.example.custom-image',
  width: 1920,
  height: 1080,
  url: 'https://www.example.com/custom-image.jpg',
});
// Use the custom image data structure in other applications
let customImageUTD = uniformTypeDescriptor.getTypeDescriptor('com.example.custom-image');
let customImageStructObject = new uniformDataStruct.ImageStruct({
  uniformDataType: customImageUTD.typeId,
  width: 1280,
  height: 720,
  url: 'https://www.example.com/another-custom-image.jpg',
});
Enter fullscreen mode Exit fullscreen mode

Summary

UTD is a powerful data management tool in Huawei HarmonyOS Next. It realizes the unification and standardization of data interaction between different applications and different devices through standardizing data types and hierarchical affiliation. We can utilize UTD to create custom data types and combine them with preset data types to meet more complex data management requirements.

💖 💪 🙅 🚩
xun_wang_6384a403f9817c2
SameX

Posted on November 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related