You can use OH_Asset_Update to update an asset.
The following table describes the attributes for updating an asset.
In the following table, the attributes ASSET_TAG_ALIAS and those starting with ASSET_TAG_DATA_LABEL are custom asset attributes reserved for services. These attributes are not encrypted. Therefore, do not put sensitive personal data in these attributes.
Attributes in query:
| Attribute Name (Asset_Tag) | Attribute Content (Asset_Value) | Mandatory | Description |
|---|---|---|---|
| ASSET_TAG_ALIAS | Type: uint8[] Length: 1-256 bytes | Yes | Asset alias, which uniquely identifies an asset. |
| ASSET_TAG_ACCESSIBILITY | Type: uint32_t Value range: see Asset_Accessibility. | No | Access control based on the lock screen status. |
| ASSET_TAG_REQUIRE_PASSWORD_SET | Type: bool | No | Whether the asset is accessible only when a lock screen password is set. The value true means to update the asset accessible only when a lock screen password is set. The value false means to update the asset that can be accessed regardless of whether a lock screen password is set. |
| ASSET_TAG_AUTH_TYPE | Type: uint32_t Value range: see Asset_AuthType. | No | Type of user authentication required for accessing the asset. |
| ASSET_TAG_SYNC_TYPE | Type: uint32_t Value range: see Asset_SyncType. | No | Type of sync supported by the asset. |
| ASSET_TAG_IS_PERSISTENT | Type: bool | No | Whether to retain the asset when the application is uninstalled. The value true means to retain the asset even after the application is uninstalled. The value false means the opposite. |
| ASSET_TAG_DATA_LABEL_CRITICAL_1 | Type: uint8[], length: 1-2048 bytes. | Optional | Attached information of the asset. The content is customized by the service and has integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_CRITICAL_2 | Type: uint8[], length: 1-2048 bytes. | Optional | Attached information of the asset. The content is customized by the service and has integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_CRITICAL_3 | Type: uint8[], length: 1-2048 bytes. | Optional | Attached information of the asset. The content is customized by the service and has integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_CRITICAL_4 | Type: uint8[], length: 1-2048 bytes. | Optional | Attached information of the asset. The content is customized by the service and has integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_1 | Type: uint8[], length: 1-2048 bytes. | Optional | Attached information of the asset. The content is customized by the service and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_2 | Type: uint8[], length: 1-2048 bytes. | Optional | Attached information of the asset. The content is customized by the service and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_3 | Type: uint8[], length: 1-2048 bytes. | Optional | Attached information of the asset. The content is customized by the service and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_4 | Type: uint8[], length: 1-2048 bytes. | Optional | Attached information of the asset. The content is customized by the service and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_112+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_212+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_312+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_412+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_REQUIRE_ATTR_ENCRYPTED14+ | Type: bool | No | Whether to update the encrypted data of service customized supplementary information. The value true means to update the encrypted data of service customized supplementary information; the value false means to update the non-encrypted data of service customized supplementary information. Default value: false. |
| ASSET_TAG_GROUP_ID18+ | Type: Uint8[] Length: 7-127 bytes | No | Group to which the asset to be updated belongs. By default, assets that do not belong to any group are updated. |
Attributes in attributesToUpdate:
| Attribute Name (Asset_Tag) | Attribute Content (Asset_Value) | Mandatory | Description |
|---|---|---|---|
| ASSET_TAG_SECRET | Type: uint8[] Length: 1-1024 bytes | No | New asset in plaintext. |
| ASSET_TAG_DATA_LABEL_NORMAL_1 | Type: uint8[], length: 1-2048 bytes. | Optional | Key Asset attached information. The content is customized by business and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_2 | Type: uint8[], length: 1-2048 bytes. | Optional | Key Asset attached information. The content is customized by business and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_3 | Type: uint8[], length: 1-2048 bytes. | Optional | Key Asset attached information. The content is customized by business and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_4 | Type: uint8[], length: 1-2048 bytes. | Optional | Key Asset attached information. The content is customized by business and has no integrity protection. Note: In API version 12 and earlier, the length is 1-512 bytes. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_112+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_212+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_312+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_412+ | Type: uint8[] Length: 1-2048 bytes | No | Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced. |
Before updating an asset, ensure that the asset exists. For details about how to add an asset, see Adding an Asset. Otherwise, the NOT_FOUND error (24000002) is reported.
Update asset demo_alias as follows: change the asset plaintext to demo_pwd_new and additional attribute to demo_label_new.
For details about how to update an asset in a group, see Updating an asset in a Group.
Link the dynamic libraries in the CMake script.
target_link_libraries(entry PUBLIC libasset_ndk.z.so)
Include header files.
#include "napi/native_api.h" #include <string.h> #include "asset/asset_api.h"
Update an asset.
static napi_value UpdateAsset(napi_env env, napi_callback_info info)
{
const char *aliasStr = "demo_alias";
const char *secretStr = "demo_pwd_new";
const char *labelStr = "demo_label_new";
Asset_Blob alias = {(uint32_t)(strlen(aliasStr)), (uint8_t *)aliasStr};
Asset_Blob new_secret = {(uint32_t)(strlen(secretStr)), (uint8_t *)secretStr};
Asset_Blob new_label = {(uint32_t)(strlen(labelStr)), (uint8_t *)labelStr};
Asset_Attr query[] = {{.tag = ASSET_TAG_ALIAS, .value.blob = alias }};
Asset_Attr attributesToUpdate[] = {
{.tag = ASSET_TAG_SECRET, .value.blob = new_secret},
{.tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = new_label},
};
int32_t updateResult = OH_Asset_Update(query, sizeof(query) / sizeof(query[0]), attributesToUpdate,
sizeof(attributesToUpdate) / sizeof(attributesToUpdate[0]));
napi_value ret;
napi_create_int32(env, updateResult, &ret);
return ret;
}