• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Managing Assets in a Group (ArkTS)
2
3Before managing assets in a group, ensure that you are familiar with the following operations:
4
5- [Adding an Asset (ArkTS)](asset-js-add.md)
6- [Removing Assets (ArkTS)](asset-js-remove.md)
7- [Updating an Asset (ArkTS)](asset-js-update.md)
8- [Querying Assets (ArkTS)](asset-js-query.md)
9
10## Prerequisites
11
12Set the group ID **demo_group_id** in the **app.json5** file.
13
14```json
15{
16  "app": {
17    // Other configuration items are omitted here.
18    "assetAccessGroups": [
19      "demo_group_id"
20    ]
21  }
22}
23```
24
25## Adding an Asset to a Group
26
27Add an asset to the group, with the password **demo_pwd**, alias **demo_alias**, and additional attribute **demo_label**. The asset can be accessed after the user unlocks the device for the first time.
28
29```typescript
30import { asset } from '@kit.AssetStoreKit';
31import { util } from '@kit.ArkTS';
32import { BusinessError } from '@kit.BasicServicesKit';
33
34function stringToArray(str: string): Uint8Array {
35  let textEncoder = new util.TextEncoder();
36  return textEncoder.encodeInto(str);
37}
38
39let attr: asset.AssetMap = new Map();
40attr.set(asset.Tag.SECRET, stringToArray('demo_pwd'));
41attr.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
42attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED);
43attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label'));
44attr.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
45try {
46  asset.add(attr).then(() => {
47    console.info(`Asset added to the group successfully.`);
48  }).catch((err: BusinessError) => {
49    console.error(`Failed to add Asset to the group. Code is ${err.code}, message is ${err.message}`);
50  })
51} catch (error) {
52  let err = error as BusinessError;
53  console.error(`Failed to add Asset to the group. Code is ${err.code}, message is ${err.message}`);
54}
55```
56
57## Removing an Asset from a Group
58
59Remove asset **demo_alias** from group **demo_group_id**.
60
61```typescript
62import { asset } from '@kit.AssetStoreKit';
63import { util } from '@kit.ArkTS';
64import { BusinessError } from '@kit.BasicServicesKit';
65
66function stringToArray(str: string): Uint8Array {
67  let textEncoder = new util.TextEncoder();
68  return textEncoder.encodeInto(str);
69}
70
71let query: asset.AssetMap = new Map();
72query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the asset alias to remove a single asset. To remove all assets, leave the alias unspecified.
73query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
74try {
75  asset.remove(query).then(() => {
76    console.info(`Asset removed from the group successfully.`);
77  }).catch((err: BusinessError) => {
78    console.error(`Failed to remove Asset from the group. Code is ${err.code}, message is ${err.message}`);
79  });
80} catch (error) {
81  let err = error as BusinessError;
82  console.error(`Failed to remove Asset from the group. Code is ${err.code}, message is ${err.message}`);
83}
84```
85
86## Updating an asset in a Group
87
88Update asset **demo_alias** in group **demo_group_id** as follows: change the asset plaintext to **demo_pwd_new** and the additional attribute to **demo_label_new**.
89
90```typescript
91import { asset } from '@kit.AssetStoreKit';
92import { util } from '@kit.ArkTS';
93import { BusinessError } from '@kit.BasicServicesKit';
94
95function stringToArray(str: string): Uint8Array {
96  let textEncoder = new util.TextEncoder();
97  return textEncoder.encodeInto(str);
98}
99
100let query: asset.AssetMap = new Map();
101query.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
102query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
103let attrsToUpdate: asset.AssetMap = new Map();
104attrsToUpdate.set(asset.Tag.SECRET, stringToArray('demo_pwd_new'));
105attrsToUpdate.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label_new'));
106try {
107  asset.update(query, attrsToUpdate).then(() => {
108    console.info(`Asset in the group updated successfully.`);
109  }).catch((err: BusinessError) => {
110    console.error(`Failed to update Asset in the group. Code is ${err.code}, message is ${err.message}`);
111  });
112} catch (error) {
113  let err = error as BusinessError;
114  console.error(`Failed to update Asset in the group. Code is ${err.code}, message is ${err.message}`);
115}
116```
117
118## Querying the Plaintext of an Asset in a Group
119
120Query the plaintext of asset **demo_alias** in group **demo_group_id**.
121
122```typescript
123import { asset } from '@kit.AssetStoreKit';
124import { util } from '@kit.ArkTS';
125import { BusinessError } from '@kit.BasicServicesKit';
126
127function stringToArray(str: string): Uint8Array {
128  let textEncoder = new util.TextEncoder();
129  return textEncoder.encodeInto(str);
130}
131
132function arrayToString(arr: Uint8Array): string {
133  let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true });
134  let str = textDecoder.decodeToString(arr, { stream: false })
135  return str;
136}
137
138let query: asset.AssetMap = new Map();
139query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the alias of the asset to query.
140query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // Return all asset information, including attributes and asset plaintext.
141query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
142try {
143  asset.query(query).then((res: Array<asset.AssetMap>) => {
144    for (let i = 0; i < res.length; i++) {
145      // parse the secret.
146      let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array;
147      // parse uint8array to string
148      let secretStr: string = arrayToString(secret);
149    }
150  }).catch ((err: BusinessError) => {
151    console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
152  });
153} catch (error) {
154  let err = error as BusinessError;
155  console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
156}
157```
158
159## Querying the Attributes of an Asset in a Group
160
161Query the attributes of asset **demo_alias** in group **demo_group_id**.
162
163```typescript
164import { asset } from '@kit.AssetStoreKit';
165import { util } from '@kit.ArkTS';
166import { BusinessError } from '@kit.BasicServicesKit';
167
168function stringToArray(str: string): Uint8Array {
169  let textEncoder = new util.TextEncoder();
170  return textEncoder.encodeInto(str);
171}
172
173let query: asset.AssetMap = new Map();
174query.set(asset.Tag.ALIAS, stringToArray('demo_alias'));       // Specify the alias of the asset to query.
175query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ATTRIBUTES); // Return only the attributes of the asset, that is, the result does not include the asset plaintext.
176query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
177try {
178  asset.query(query).then((res: Array<asset.AssetMap>) => {
179    for (let i = 0; i < res.length; i++) {
180      // Parse the attribute.
181      let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number;
182    }
183  }).catch ((err: BusinessError) => {
184    console.error(`Failed to query Asset from the group. Code is ${err.code}, message is ${err.message}`);
185  });
186} catch (error) {
187  let err = error as BusinessError;
188  console.error(`Failed to query Asset from the group. Code is ${err.code}, message is ${err.message}`);
189}
190```
191