This article aims to deeply explore the technical details of Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices.
Mainly serving as a carrier for technical sharing and exchange, it is inevitable that there may be errors and omissions. We welcome valuable opinions and questions from colleagues to make progress together.
This article is original content. Any form of reprint must indicate the source and original author.
HarmonyOS's IME Kit not only provides basic input method development functions but also introduces a shared sandbox mechanism, opening a door to cross-process data sharing for developers. Behind this door lies infinite possibilities, such as cloud synchronization, personalized settings, and extended functions, bringing more abundant functions and application scenarios to input method applications. This article will take you on a deep exploration of the shared sandbox mechanism of IME Kit, unveil its mystery, and show you how to achieve cross-process data transmission to make your input method application more intelligent and convenient.
Shared sandbox: A bridge connecting different processes
The shared sandbox is a safe data sharing mechanism in HarmonyOS. It is like a bridge connecting different processes and allowing them to exchange data while ensuring data security.
Application scenarios of shared sandbox:
- Cloud synchronization: Imagine that users set personalized settings such as input method themes and dictionaries on their mobile phones. Do they hope to use them on tablets as well? Through the shared sandbox, we can store users' personalized settings in the shared sandbox and synchronize them through cloud services to achieve cross-device data sharing and make users' input experience more consistent.
- Personalized settings: Users like personalized input method experiences, such as different skin themes and key press sounds. Through the shared sandbox, we can store users' personalized settings in the shared sandbox to achieve personalized setting sharing between different input methods, allowing users to customize input methods according to their preferences.
- Extended functions: Input method extension functions, such as voice input and handwriting recognition, often need to exchange data with other processes. For example, voice input needs to transmit voice recognition results to the input method application, and handwriting recognition needs to transmit handwriting recognition results to the input method application. Through the shared sandbox, we can easily achieve data sharing between these functions and provide users with a richer input experience.
Security and permissions: The cornerstone of shared sandbox
Although the shared sandbox mechanism provides a convenient way for data sharing, security and permission management are its cornerstones to ensure the security of user data.
Security guarantees of shared sandbox:
- Sandbox isolation: Each process has its own independent sandbox. Processes cannot directly access each other's sandboxes, just like independent rooms, ensuring data security.
- Permission control: Only processes with corresponding permissions can access the shared sandbox. Just like entering a room requires a key, it ensures the security of data access.
- Data encryption: Data in the shared sandbox can be encrypted for storage, just like locking files in a safe to prevent data leakage.
Configuration of shared sandbox: Building a data sharing bridge
To use the shared sandbox mechanism, some configurations are needed:
1. Configure data-group-ids in module.json5
In the module.json5
configuration file, configure the same data-group-ids
for the input method extension and the main entry of the application so that they can access the same shared sandbox. data-group-ids
can be regarded as the key to the shared sandbox. Processes with the same key can access the same shared sandbox.
Sample code:
"module": {
//...
"extensionAbilities": [
"description": "InputMethodExtDemo",
"icon": "$media:icon",
"name": "InputMethodExtAbility",
"srcEntry": "./ets/InputMethodExtensionAbility/InputMethodService.ts",
"type": "inputMethod",
"exported": true,
"data-group-ids": ["group1"]
],
"abilities": [
"description": "MainAbility",
"icon": "$media:icon",
"name": "MainAbility",
"srcEntry": "./ets/MainAbility/MainAbility",
"type": "page",
"exported": true,
"data-group-ids": ["group1"]
]
}
2. Use getGroupDir method to obtain the shared path
Use the context.getGroupDir()
method to obtain the path of the shared sandbox for data reading and writing operations. Just like finding the room key to enter the room, only after obtaining the shared path can we access the data in the shared sandbox.
Sample code:
let sharedPath = context.getGroupDir("group1");
Cross-process data transmission: The art of data sharing
In the shared sandbox, we can use file operation APIs for data reading and writing to achieve cross-process data transmission.
1. Write data
Use the fs.writeFileSync()
method to write data to a file in the shared sandbox.
Sample code:
// Write data
let filePath = `${sharedPath}/user_data.txt`;
let data = "用户数据";
fs.writeFileSync(filePath, data);
2. Read data
Use the fs.readFileSync()
method to read data from a file in the shared sandbox.
Sample code:
// Read data
let data = fs.readFileSync(filePath);
Summary
The shared sandbox mechanism of IME Kit provides us developers with a safe and convenient data sharing mechanism, bringing more abundant functions and application scenarios to input method applications. This article introduced the overview, application scenarios, security, configuration and implementation methods of the shared sandbox mechanism, as well as cross-process data transmission operations, helping you master the advanced application skills of IME Kit.
Top comments (0)