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