• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.enterprise.deviceSettings (Device Settings Management)
2
3The **deviceSettings** module provides APIs for setting enterprise devices, including obtaining the screen-off time of a device.
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> - The APIs of this module can be used only in the stage model.
10>
11> - The APIs provided by this module can be called only by a [device administrator application](enterpriseDeviceManagement-overview.md#basic-concepts) that is [enabled](js-apis-enterprise-adminManager.md#adminmanagerenableadmin).
12
13## Modules to Import
14
15```ts
16import deviceSettings from '@ohos.enterprise.deviceSettings';
17```
18
19## deviceSettings.setScreenOffTime<sup>11+</sup>
20
21setScreenOffTime(admin: Want, time: number): void
22
23Sets the device screen-off time through the specified device administrator application. This API returns the result synchronously. If the operation is successful, **null** is returned. If the operation fails, an exception is thrown.
24
25**Required permissions**: ohos.permission.ENTERPRISE_SET_SCREENOFF_TIME
26
27**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
28
29**System API**: This is a system API.
30
31**Parameters**
32
33| Name     | Type                                      | Mandatory  | Description                      |
34| -------- | ---------------------------------------- | ---- | ------------------------------- |
35| admin    | [Want](js-apis-app-ability-want.md)     | Yes   | Device administrator application.                 |
36| time | number            | Yes   | Screen-off time to set, in milliseconds. You are advised to set this parameter to the device's optional screen-off time.      |
37
38**Error codes**
39
40For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
41
42| ID| Error Message                                                                      |
43| ------- | ---------------------------------------------------------------------------- |
44| 9200001 | the application is not an administrator of the device.                        |
45| 9200002 | the administrator application does not have permission to manage the device. |
46
47**Example**
48
49```ts
50import Want from '@ohos.app.ability.Want';
51let wantTemp: Want = {
52  bundleName: 'com.example.myapplication',
53  abilityName: 'EntryAbility',
54};
55try {
56  deviceSettings.setScreenOffTime(wantTemp, 30000);
57  console.info(`Succeeded in setting screen off time`);
58} catch(err) {
59  console.error(`Failed to set screen off time. Code: ${err.code}, message: ${err.message}`);
60}
61```
62
63## deviceSettings.getScreenOffTime
64
65getScreenOffTime(admin: Want, callback: AsyncCallback&lt;number&gt;): void
66
67Obtains the device screen-off time through the specified device administrator application. This API uses an asynchronous callback to return the result.
68
69**Required permissions**: ohos.permission.ENTERPRISE_GET_SETTINGS
70
71**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
72
73**System API**: This is a system API.
74
75**Parameters**
76
77| Name     | Type                                      | Mandatory  | Description                      |
78| -------- | ---------------------------------------- | ---- | ------------------------------- |
79| admin    | [Want](js-apis-app-ability-want.md)     | Yes   | Device administrator application.                 |
80| callback | AsyncCallback&lt;number&gt;            | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **null** and **data** is the screen-off time in ms. If the operation fails, **err** is an error object.      |
81
82**Error codes**
83
84For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
85
86| ID| Error Message                                                                      |
87| ------- | ---------------------------------------------------------------------------- |
88| 9200001 | the application is not an administrator of the device.                        |
89| 9200002 | the administrator application does not have permission to manage the device. |
90
91**Example**
92
93```ts
94import Want from '@ohos.app.ability.Want';
95let wantTemp: Want = {
96  bundleName: 'com.example.myapplication',
97  abilityName: 'EntryAbility',
98};
99
100deviceSettings.getScreenOffTime(wantTemp, (err, result) => {
101  if (err) {
102    console.error(`Failed to get screen off time. Code: ${err.code}, message: ${err.message}`);
103    return;
104  }
105  console.info(`Succeeded in getting screen off time, result : ${result}`);
106});
107```
108
109## deviceSettings.getScreenOffTime
110
111getScreenOffTime(admin: Want): Promise&lt;number&gt;
112
113Obtains the device screen-off time through the specified device administrator application. This API uses a promise to return the result.
114
115**Required permissions**: ohos.permission.ENTERPRISE_GET_SETTINGS
116
117**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
118
119**System API**: This is a system API.
120
121**Parameters**
122
123| Name  | Type                                 | Mandatory  | Description     |
124| ----- | ----------------------------------- | ---- | ------- |
125| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application.|
126
127**Return value**
128
129| Type                  | Description                     |
130| --------------------- | ------------------------- |
131| Promise&lt;number&gt; | Promise used to return the screen-off time, in ms. |
132
133**Error codes**
134
135For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
136
137| ID| Error Message                                                                    |
138| ------- | ---------------------------------------------------------------------------- |
139| 9200001 | the application is not an administrator of the device.                        |
140| 9200002 | the administrator application does not have permission to manage the device. |
141
142**Example**
143
144```ts
145import Want from '@ohos.app.ability.Want';
146import { BusinessError } from '@ohos.base';
147let wantTemp: Want = {
148  bundleName: 'com.example.myapplication',
149  abilityName: 'EntryAbility',
150};
151
152deviceSettings.getScreenOffTime(wantTemp).then((result) => {
153  console.info(`Succeeded in getting screen off time, result : ${result}`);
154}).catch((err: BusinessError) => {
155  console.error(`Failed to get screen off time. Code: ${err.code}, message: ${err.message}`);
156});
157```
158
159## deviceSettings.installUserCertificate
160
161installUserCertificate(admin: Want, certificate: CertBlob, callback: AsyncCallback&lt;string&gt;): void
162
163Installs a user certificate through the specified device administrator application. This API uses an asynchronous callback to return the result.
164
165**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE
166
167**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
168
169**System API**: This is a system API.
170
171**Parameters**
172
173| Name     | Type                                      | Mandatory  | Description                      |
174| -------- | ---------------------------------------- | ---- | ------------------------------- |
175| admin    | [Want](js-apis-app-ability-want.md)     | Yes   | Device administrator application.                 |
176| certificate    | [CertBlob](#certblob)     | Yes   | Information about the certificate to install.                 |
177| callback | AsyncCallback&lt;string&gt;            | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **null**. Otherwise, **err** is an error object.     |
178
179**Error codes**
180
181For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
182
183| ID| Error Message                                                                      |
184| ------- | ---------------------------------------------------------------------------- |
185| 9200001 | the application is not an administrator of the device.                        |
186| 9200002 | the administrator application does not have permission to manage the device. |
187| 9201001 | manage certificate failed |
188
189**Example**
190
191```ts
192import Want from '@ohos.app.ability.Want';
193import { BusinessError } from '@ohos.base';
194let wantTemp: Want = {
195  bundleName: 'com.example.myapplication',
196  abilityName: 'EntryAbility',
197};
198let certFileArray: Uint8Array = new Uint8Array();
199// The variable context needs to be initialized in MainAbility's onCreate callback function
200// test.cer needs to be placed in the rawfile directory
201getContext().resourceManager.getRawFileContent("test.cer").then((value) => {
202  certFileArray = value;
203  deviceSettings.installUserCertificate(wantTemp, { inData: certFileArray, alias: "cert_alias_xts" }, (err, result) => {
204    if (err) {
205      console.error(`Failed to install user certificate. Code: ${err.code}, message: ${err.message}`);
206    } else {
207      console.info(`Succeeded in installing user certificate, result : ${JSON.stringify(result)}`);
208    }
209  });
210}).catch((error: BusinessError) => {
211  console.error(`Failed to get row file content. message: ${error.message}`);
212  return
213});
214```
215
216## deviceSettings.installUserCertificate
217
218installUserCertificate(admin: Want, certificate: CertBlob): Promise&lt;string&gt;
219
220Installs a user certificate through the specified device administrator application. This API uses a promise to return the result.
221
222**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE
223
224**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
225
226**System API**: This is a system API.
227
228**Parameters**
229
230| Name  | Type                                 | Mandatory  | Description     |
231| ----- | ----------------------------------- | ---- | ------- |
232| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application.|
233| certificate    | [CertBlob](#certblob)     | Yes   | Information about the certificate to install.                 |
234
235**Return value**
236
237| Type                  | Description                     |
238| --------------------- | ------------------------- |
239| Promise&lt;string&gt; | Promise used to return the URI of the installed certificate. This URI can be used to uninstall the certificate.|
240
241**Error codes**
242
243For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
244
245| ID| Error Message                                                                    |
246| ------- | ---------------------------------------------------------------------------- |
247| 9200001 | the application is not an administrator of the device.                        |
248| 9200002 | the administrator application does not have permission to manage the device. |
249| 9201001 | manage certificate failed |
250
251**Example**
252
253```ts
254import Want from '@ohos.app.ability.Want';
255import { BusinessError } from '@ohos.base';
256let wantTemp: Want = {
257  bundleName: 'com.example.myapplication',
258  abilityName: 'EntryAbility',
259};
260let certFileArray: Uint8Array = new Uint8Array();
261// The variable context needs to be initialized in MainAbility's onCreate callback function
262// test.cer needs to be placed in the rawfile directory
263getContext().resourceManager.getRawFileContent("test.cer").then((value) => {
264  certFileArray = value
265  deviceSettings.installUserCertificate(wantTemp, { inData: certFileArray, alias: "cert_alias_xts" })
266    .then((result) => {
267      console.info(`Succeeded in installing user certificate, result : ${JSON.stringify(result)}`);
268    }).catch((err: BusinessError) => {
269    console.error(`Failed to install user certificate. Code: ${err.code}, message: ${err.message}`);
270  })
271}).catch((error: BusinessError) => {
272  console.error(`Failed to get row file content. message: ${error.message}`);
273  return
274});
275```
276
277## CertBlob
278
279Represents the certificate information.
280
281**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
282
283**System API**: This is a system API.
284
285| Name        | Type    | Mandatory| Description                           |
286| ----------- | --------| ----- | ------------------------------- |
287| inData | Uint8Array | Yes| Binary content of the certificate.|
288| alias | string | Yes| Certificate alias.|
289
290## deviceSettings.uninstallUserCertificate
291
292uninstallUserCertificate(admin: Want, certUri: string, callback: AsyncCallback&lt;void&gt;): void
293
294Uninstalls a user certificate through the specified device administrator application. This API uses an asynchronous callback to return the result.
295
296**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE
297
298**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
299
300**System API**: This is a system API.
301
302**Parameters**
303
304| Name     | Type                                      | Mandatory  | Description                      |
305| -------- | ---------------------------------------- | ---- | ------------------------------- |
306| admin    | [Want](js-apis-app-ability-want.md)     | Yes   | Device administrator application.                 |
307| certUri    | string    | Yes   | Certificate URI, which is returned by **installUserCertificate()**.                 |
308| callback | AsyncCallback&lt;void&gt;            | Yes   | Callback invoked to return the result. If the operation is successful, **err** is **null**. Otherwise, **err** is an error object.     |
309
310**Error codes**
311
312For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
313
314| ID| Error Message                                                                      |
315| ------- | ---------------------------------------------------------------------------- |
316| 9200001 | the application is not an administrator of the device.                        |
317| 9200002 | the administrator application does not have permission to manage the device. |
318| 9201001 | manage certificate failed |
319
320**Example**
321
322```ts
323import Want from '@ohos.app.ability.Want';
324let wantTemp: Want = {
325  bundleName: 'com.example.myapplication',
326  abilityName: 'EntryAbility',
327};
328let aliasStr = "certName"
329deviceSettings.uninstallUserCertificate(wantTemp, aliasStr, (err) => {
330  if (err) {
331    console.error(`Failed to uninstall user certificate. Code: ${err.code}, message: ${err.message}`);
332    return;
333  }
334  console.info(`Succeeded in uninstalling user certificate`);
335});
336```
337
338## deviceSettings.uninstallUserCertificate
339
340uninstallUserCertificate(admin: Want, certUri: string): Promise&lt;void&gt;
341
342Uninstalls a user certificate through the specified device administrator application. This API uses a promise to return the result.
343
344**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE
345
346**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
347
348**System API**: This is a system API.
349
350**Parameters**
351
352| Name  | Type                                 | Mandatory  | Description     |
353| ----- | ----------------------------------- | ---- | ------- |
354| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application.|
355| certUri    | string     | Yes   | Certificate URI, which is returned by **installUserCertificate()**.                 |
356
357**Return value**
358
359| Type                  | Description                     |
360| --------------------- | ------------------------- |
361| Promise&lt;void&gt; | Promise that returns no value. An error object will be thrown if the operation fails.|
362
363**Error codes**
364
365For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
366
367| ID| Error Message                                                                    |
368| ------- | ---------------------------------------------------------------------------- |
369| 9200001 | the application is not an administrator of the device.                        |
370| 9200002 | the administrator application does not have permission to manage the device. |
371| 9201001 | manage certificate failed |
372
373**Example**
374
375```ts
376import Want from '@ohos.app.ability.Want';
377import { BusinessError } from '@ohos.base';
378let wantTemp: Want = {
379  bundleName: 'com.example.myapplication',
380  abilityName: 'EntryAbility',
381};
382let aliasStr = "certName"
383deviceSettings.uninstallUserCertificate(wantTemp, aliasStr).then(() => {
384  console.info(`Succeeded in uninstalling user certificate`);
385}).catch((err: BusinessError) => {
386  console.error(`Failed to uninstall user certificate. Code is ${err.code}, message is ${err.message}`);
387});
388```
389
390## deviceSettings.setPowerPolicy<sup>11+</sup>
391
392setPowerPolicy(admin: Want, powerScene: PowerScene, powerPolicy: PowerPolicy): void
393
394Sets the device power policy through the specified device administrator application. This API returns the result synchronously. If the operation is successful, **null** is returned. If the operation fails, an exception is thrown.
395
396**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_SETTINGS
397
398**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
399
400**System API**: This is a system API.
401
402**Parameters**
403
404| Name     | Type                                      | Mandatory  | Description                      |
405| -------- | ---------------------------------------- | ---- | ------------------------------- |
406| admin    | [Want](js-apis-app-ability-want.md)     | Yes   | Device administrator application.                 |
407| powerScene | [PowerScene](#powerscene11) | Yes   | Scenario to which the power policy applies. Currently, only the timeout scenario is supported.      |
408| powerPolicy | [PowerPolicy](#powerpolicy11) | Yes   | Power policy to set.      |
409
410**Error codes**
411
412For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
413
414| ID| Error Message                                                                      |
415| ------- | ---------------------------------------------------------------------------- |
416| 9200001 | the application is not an administrator of the device.                        |
417| 9200002 | the administrator application does not have permission to manage the device. |
418
419**Example**
420
421```ts
422import Want from '@ohos.app.ability.Want';
423let wantTemp: Want = {
424  bundleName: 'com.example.myapplication',
425  abilityName: 'EntryAbility',
426};
427try {
428  let delayTime = 0;
429  let powerScene: deviceSettings.PowerScene = deviceSettings.PowerScene.TIME_OUT;
430  let powerPolicyAction: deviceSettings.PowerPolicyAction = deviceSettings.PowerPolicyAction.AUTO_SUSPEND;
431  let powerPolicy: deviceSettings.PowerPolicy = {powerPolicyAction, delayTime};
432  deviceSettings.setPowerPolicy(wantTemp, powerScene, powerPolicy);
433  console.info(`Succeeded in setting power polilcy`);
434} catch (err) {
435  console.error(`Failed to set power policy. Code: ${err.code}, message: ${err.message}`);
436}
437```
438
439## deviceSettings.getPowerPolicy<sup>11+</sup>
440
441getPowerPolicy(admin: Want, powerScene: PowerScene): PowerPolicy
442
443Obtains the device power policy through the specified device administrator application. This API returns the result synchronously. If the operation is successful, the power policy obtained is returned. If the operation fails, an exception is thrown.
444
445**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_SETTINGS
446
447**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
448
449**System API**: This is a system API.
450
451**Parameters**
452
453| Name     | Type                                      | Mandatory  | Description                      |
454| -------- | ---------------------------------------- | ---- | ------------------------------- |
455| admin    | [Want](js-apis-app-ability-want.md)     | Yes   | Device administrator application.                 |
456| powerScene | [PowerScene](#powerscene11) | Yes   | Scenario to which the power policy applies. Currently, only the timeout scenario is supported.      |
457
458**Return value**
459
460| Type  | Description                                 | Description                      |
461| ----- | ----------------------------------- |------------------------------- |
462| PowerPolicy | [PowerPolicy](#powerpolicy11) |   Power policy obtained.      |
463
464**Error codes**
465
466For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
467
468| ID| Error Message                                                                      |
469| ------- | ---------------------------------------------------------------------------- |
470| 9200001 | the application is not an administrator of the device.                        |
471| 9200002 | the administrator application does not have permission to manage the device. |
472
473**Example**
474
475```ts
476import Want from '@ohos.app.ability.Want';
477let wantTemp: Want = {
478  bundleName: 'com.example.myapplication',
479  abilityName: 'EntryAbility',
480};
481try {
482  let powerScene: deviceSettings.PowerScene = deviceSettings.PowerScene.TIME_OUT;
483  let powerPolicy: deviceSettings.PowerPolicy = deviceSettings.getPowerPolicy(wantTemp, powerScene);
484  console.info(`Succeeded in getting power polilcy ${JSON.stringify(powerPolicy)}`);
485} catch (err) {
486  console.error(`Failed to get power policy. Code: ${err.code}, message: ${err.message}`);
487}
488```
489
490## PowerPolicy<sup>11+</sup>
491
492Represents the power policy.
493
494**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
495
496**System API**: This is a system API.
497
498| Name        | Type    | Mandatory| Description                           |
499| ----------- | --------| ----- | ------------------------------- |
500| powerPolicyAction | [PowerPolicyAction](#powerpolicyaction11) | Yes| Action to apply the power policy.|
501| delayTime | number | Yes| Delay time allowed.|
502
503## PowerScene<sup>11+</sup>
504
505Defines the scenario to which the power policy applies.
506
507**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
508
509**System API**: This is a system API.
510
511| Name| Value| Description|
512| -------- | -------- | -------- |
513| TIME_OUT | 0 | Timeout scenario.|
514
515## PowerPolicyAction<sup>11+</sup>
516
517Enumerates the actions that can be performed to apply the power policy.
518
519**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
520
521**System API**: This is a system API.
522
523| Name| Value| Description|
524| -------- | -------- | -------- |
525| NONE | 0 | No action is performed.|
526| AUTO_SUSPEND | 1 | Automatically enter the sleep mode.|
527| FORCE_SUSPEND | 2 | Forcibly enter the sleep mode.|
528| HIBERNATE | 3 | Enter the hibernation state. Currently, the power subsystem does not support this action.|
529| SHUTDOWN | 4 | Shut down the system.|
530