js-apis-config-policy.md 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
# Configuration Policy

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> - The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - The APIs of this module are system APIs and cannot be called by third-party applications.

The configuration policy provides the capability of obtaining the custom configuration directory and file path based on the predefined custom configuration level.

## Modules to Import

```
import configPolicy from '@ohos.configPolicy';
```

## getOneCfgFile

getOneCfgFile(relPath: string, callback: AsyncCallback<string>): void

Obtains the path of a configuration file with the specified name and highest priority. This API uses an asynchronous callback to return the result.
For example, if the **config.xml** file is stored in **/system/etc/config.xml** and **/sys-pod/etc/config.xml** (in ascending order of priority), then **/sys-pod/etc/config.xml** is returned.

**System capability**: SystemCapability.Customization.ConfigPolicy

**Parameters**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | relPath | string | Yes| Name of the configuration file.|
  | callback | AsyncCallback<string> | Yes| Callback used to return the path of the configuration file.|

**Example**
  ```
  configPolicy.getOneCfgFile('config.xml', (error, value) => {
      if (error == undefined) {
          console.log(value);
      } else {
          console.log(error);
      }
  });
  ```


## getOneCfgFile

getOneCfgFile(relPath: string): Promise<string>

Obtains the path of a configuration file with the specified name and highest priority. This API uses a promise to return the result.

**System capability**: SystemCapability.Customization.ConfigPolicy

**Parameters**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | relPath | string | Yes| Name of the configuration file.|

**Return value**
  | Type| Description|
  | -------- | -------- |
  | Promise<string> | Promise used to return the path of the configuration file.|

**Example**
  ```
  configPolicy.getOneCfgFile('config.xml').then(value => {
      console.log(value);
  }).catch(error => {
      console.log("getOneCfgFile promise " + error);
  });
  ```


## getCfgFiles

getCfgFiles(relPath: string, callback: AsyncCallback<Array<string>>): void

Obtains all configuration files with the specified name and lists them in ascending order of priority. This API uses an asynchronous callback to return the result. For example, if the **config.xml** file is stored in **/system/etc/config.xml**
and **/sys-pod/etc/config.xml**, then **/system/etc/config.xml, /sys-pod/etc/config.xml** is returned.

**System capability**: SystemCapability.Customization.ConfigPolicy

**Parameters**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | relPath | string | Yes| Name of the configuration file.|
  | callback | AsyncCallback<Array<string>> | Yes| Callback used to return the file list.|

**Example**
  ```
  configPolicy.getCfgFiles('config.xml', (error, value) => {
      if (error == undefined) {
          console.log(value);
      } else {
          console.log(error);
      }
  });
  ```


## getCfgFiles

getCfgFiles(relPath: string): Promise<Array<string>>

Obtains all configuration files with the specified name and lists them in ascending order of priority. This API uses a promise to return the result.

**System capability**: SystemCapability.Customization.ConfigPolicy

**Parameters**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | relPath | string | Yes| Name of the configuration file.|

**Return value**
  | Type| Description|
  | -------- | -------- |
  | Promise<Array<string>> | Promise used to return the file list.|

**Example**
  ```
  configPolicy.getCfgFiles('config.xml').then(value => {
      console.log(value);
  }).catch(error => {
      console.log("getCfgFiles promise " + error);
  });
  ```


## getCfgDirList

getCfgDirList(callback: AsyncCallback<Array<string>>): void

Obtains the configuration level directory list. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Customization.ConfigPolicy

**Parameters**
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<Array<string>> | Yes| Callback used to return the configuration level directory list.|

**Example**
  ```
  configPolicy.getCfgDirList((error, value) => {
      if (error == undefined) {
          console.log(value);
      } else {
          console.log(error);
      }
  });
  ```


## getCfgDirList

getCfgDirList(): Promise<Array<string>>

Obtains the configuration level directory list. This API uses a promise to return the result.

**System capability**: SystemCapability.Customization.ConfigPolicy

**Return value**
  | Type| Description|
  | -------- | -------- |
  | Promise<Array<string>> | Promise used to return the configuration level directory list.|

**Example**
  ```
  configPolicy.getCfgDirList().then(value => {
      console.log(value);
  }).catch(error => {
      console.log("getCfgDirList promise " + error);
  });
  ```