Querying Assets (C/C++)

Available APIs

For details about the API, see:

int32_t OH_Asset_Query(const Asset_Attr *query, uint32_t queryCnt, Asset_ResultSet *resultSet)

The following table describes the parameters.

Attribute Name (Asset_Tag) Attribute Content (Asset_Value) Mandatory Description
ASSET_TAG_ALIAS Type: uint8[]
Length: 1-256 bytes
No 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.
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.
ASSET_TAG_DATA_LABEL_CRITICAL_1 Type: uint8[]
Length: 1-512 bytes
No Additional asset data customized by the service with integrity protection.
ASSET_TAG_DATA_LABEL_CRITICAL_2 Type: uint8[]
Length: 1-512 bytes
No Additional asset data customized by the service with integrity protection.
ASSET_TAG_DATA_LABEL_CRITICAL_3 Type: uint8[]
Length: 1-512 bytes
No Additional asset data customized by the service with integrity protection.
ASSET_TAG_DATA_LABEL_CRITICAL_4 Type: uint8[]
Length: 1-512 bytes
No Additional asset data customized by the service with integrity protection.
ASSET_TAG_DATA_LABEL_NORMAL_1 Type: uint8[]
Length: 1-512 bytes
No Additional asset data customized by the service without integrity protection.
ASSET_TAG_DATA_LABEL_NORMAL_2 Type: uint8[]
Length: 1-512 bytes
No Additional asset data customized by the service without integrity protection.
ASSET_TAG_DATA_LABEL_NORMAL_3 Type: uint8[]
Length: 1-512 bytes
No Additional asset data customized by the service without integrity protection.
ASSET_TAG_DATA_LABEL_NORMAL_4 Type: uint8[]
Length: 1-512 bytes
No Additional asset data customized by the service without integrity protection.
ASSET_TAG_RETURN_TYPE Type: uint32_t
Value range: see Asset_ReturnType
No Type of the asset query result to return.
ASSET_TAG_RETURN_LIMIT Type: uint32_t No Maximum number of asset records to return.
ASSET_TAG_RETURN_OFFSET Type: uint32_t
Value range: 1-65536
No Offset of the asset query result.
NOTE: This parameter specifies the starting asset record to return in batch asset query.
ASSET_TAG_RETURN_ORDERED_BY Type: uint32_t
Value: ASSET_TAG_DATA_LABEL_xxx
No How the query results are sorted. Currently, the results can be sorted only by ASSET_TAG_DATA_LABEL.
NOTE: By default, assets are returned in the order in which they are added.

Example

Querying the Plaintext of an Asset

Query the plaintext of asset demo_alias.

  1. Add the dynamic library in the CMake script.

    target_link_libraries(entry PUBLIC libasset_ndk.z.so)
    
  2. Add an asset.

    #include <string.h>
    
    #include "asset/asset_api.h"
    
    void QueryAsset() {
       static const char *ALIAS = "demo_alias";
       Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS };
       Asset_Attr attr[] = {
          { .tag = ASSET_TAG_ALIAS, .value.blob = alias },  // Specify the alias of the asset to query.
          { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }, // Return all asset information, including the attributes and asset plaintext.
       };
    
       Asset_ResultSet resultSet = {0};
       int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet);
       if (ret == ASSET_SUCCESS) {
          // Parse the resultSet.
          for (uint32_t i = 0; i < resultSet.count; i++) {
                // Parse the secret: the data is secret->blob.data, the size is secret->blob.size.
                Asset_Attr *secret = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_SECRET);
          }
       }
       OH_Asset_FreeResultSet(&resultSet);
    }
    

Querying Attributes of an Asset

Query attributes of asset demo_alias.

  1. Add the dynamic library in the CMake script.

    target_link_libraries(entry PUBLIC libasset_ndk.z.so)
    
  2. Add an asset.

    #include <string.h>
    
    #include "asset/asset_api.h"
    
    void QueryAttributes() {
       static const char *ALIAS = "demo_alias";
       Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS };
       Asset_Attr attr[] = {
          { .tag = ASSET_TAG_ALIAS, .value.blob = alias },  // Specify the alias of the asset to query.
          { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES }, // Return only the asset attributes, that is, the result does not include the asset plaintext.
       };
    
       Asset_ResultSet resultSet = {0};
       int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet);
       if (ret == ASSET_SUCCESS) {
          // Parse the result.
          for (uint32_t i = 0; i < resultSet.count; i++) {
          // Parse the data label: the data is label->blob.data, the size is label->blob.size.
                Asset_Attr *label = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_DATA_LABEL_NORMAL_1);
          }
       }
       OH_Asset_FreeResultSet(&resultSet);
    }
    

Querying Attributes of Assets

Query attributes of assets with the tag of demo_label and return a total of 10 records sorted by DATA_LABEL_NORMAL_1 starting from the fifth record that matches the search criteria.

  1. Add the dynamic library in the CMake script.

    target_link_libraries(entry PUBLIC libasset_ndk.z.so)
    
  2. Add an asset.

    #include <string.h>
    
    #include "asset/asset_api.h"
    
    void BatchQuery() {
       static const char *LABEL = "demo_label";
       Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL };
    
       Asset_Attr attr[] = {
          { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES },
          { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label },
          { .tag = ASSET_TAG_RETURN_OFFSET, .value.u32 = 5 },
          { .tag = ASSET_TAG_RETURN_LIMIT, .value.u32 = 10 },
          { .tag = ASSET_TAG_RETURN_ORDERED_BY, .value.u32 = ASSET_TAG_DATA_LABEL_NORMAL_1 },
       };
    
       Asset_ResultSet resultSet = { 0 };
       int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet);
       if (ret == ASSET_SUCCESS) {
          // Parse the result.
          for (uint32_t i = 0; i < resultSet.count; i++) {
                // Parse the data alias: the data is alias->blob.data, the size is alias->blob.size.
                Asset_Attr *alias = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_ALIAS);
          }
       }
       OH_Asset_FreeResultSet(&resultSet);
    }
    

Constraints

  • Batch asset query

    The assets queried are transmitted to the service through an IPC channel. Due to the limitation of the IPC buffer size, the maximum number of assets to be queried at a time cannot exceed 40.