js-apis-config-policy.md 5.9 KB
Newer Older
1 2
# Configuration Policy

G
Gloria 已提交
3
The **configPolicy** module provides APIs for obtaining the custom configuration directory and file path based on the predefined custom configuration level.
4

W
wusongqing 已提交
5 6 7 8
>  **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.
>
G
Gloria 已提交
9
>  The APIs provided by this module are system APIs.
W
wusongqing 已提交
10

11 12
## Modules to Import

W
wusongqing 已提交
13
```js
14 15 16 17 18
import configPolicy from '@ohos.configPolicy';
```

## getOneCfgFile

W
wusongqing 已提交
19
getOneCfgFile(relPath: string, callback: AsyncCallback<string>)
20 21

Obtains the path of a configuration file with the specified name and highest priority. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
22
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.
23 24 25 26

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

**Parameters**
W
wusongqing 已提交
27 28 29 30
| 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.|
31 32

**Example**
W
wusongqing 已提交
33 34
  ```js
  configPolicy.getOneCfgFile('etc/config.xml', (error, value) => {
G
Gloria 已提交
35
      if (error == null) {
W
wusongqing 已提交
36
          console.log("value is " + value);
37
      } else {
W
wusongqing 已提交
38
          console.log("error occurs "+ error);
39 40 41 42 43 44 45 46 47 48 49 50 51 52
      }
  });
  ```


## 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**
W
wusongqing 已提交
53 54 55
| Name    | Type    | Mandatory  | Description   |
| ------- | ------ | ---- | ----- |
| relPath | string | Yes   | Name of the configuration file.|
56 57

**Return value**
W
wusongqing 已提交
58 59 60
| Type                   | Description          |
| --------------------- | ------------ |
| Promise<string> | Promise used to return the path of the configuration file.|
61 62

**Example**
W
wusongqing 已提交
63 64 65
  ```js
  configPolicy.getOneCfgFile('etc/config.xml').then(value => {
      console.log("value is " + value);
66 67 68 69 70 71 72 73
  }).catch(error => {
      console.log("getOneCfgFile promise " + error);
  });
  ```


## getCfgFiles

W
wusongqing 已提交
74
getCfgFiles(relPath: string, callback: AsyncCallback<Array<string>>)
75

G
Gloria 已提交
76 77
Obtains a list of configuration files with the specified name, sorted 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** (in ascending order of priority), then **/system/etc/config.xml, /sys_pod/etc/config.xml** is returned.
78 79 80 81

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

**Parameters**
W
wusongqing 已提交
82 83 84 85
| Name     | Type                                      | Mandatory  | Description           |
| -------- | ---------------------------------------- | ---- | ------------- |
| relPath  | string                                   | Yes   | Name of the configuration file.        |
| callback | AsyncCallback<Array<string>> | Yes   | Callback used to return the file list.|
86 87

**Example**
W
wusongqing 已提交
88 89
  ```js
  configPolicy.getCfgFiles('etc/config.xml', (error, value) => {
G
Gloria 已提交
90
      if (error == null) {
W
wusongqing 已提交
91
          console.log("value is " + value);
92
      } else {
W
wusongqing 已提交
93
          console.log("error occurs "+ error);
94 95 96 97 98 99 100 101 102
      }
  });
  ```


## getCfgFiles

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

G
Gloria 已提交
103
Obtains a list of configuration files with the specified name, sorted in ascending order of priority. This API uses a promise to return the result.
104 105 106 107

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

**Parameters**
W
wusongqing 已提交
108 109 110
| Name    | Type    | Mandatory  | Description   |
| ------- | ------ | ---- | ----- |
| relPath | string | Yes   | Name of the configuration file.|
111 112

**Return value**
W
wusongqing 已提交
113 114 115
| Type                                | Description  |
| ---------------------------------- | ---- |
| Promise<Array<string>> | Promise used to return the file list.|
116 117

**Example**
W
wusongqing 已提交
118 119 120
  ```js
  configPolicy.getCfgFiles('etc/config.xml').then(value => {
      console.log("value is " + value);
121 122 123 124 125 126 127 128
  }).catch(error => {
      console.log("getCfgFiles promise " + error);
  });
  ```


## getCfgDirList

W
wusongqing 已提交
129
getCfgDirList(callback: AsyncCallback<Array<string>>)
130

G
Gloria 已提交
131
Obtains the list of configuration level directories. This API uses an asynchronous callback to return the result.
132 133 134 135

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

**Parameters**
W
wusongqing 已提交
136 137 138
| Name     | Type                                      | Mandatory  | Description               |
| -------- | ---------------------------------------- | ---- | ----------------- |
| callback | AsyncCallback<Array<string>> | Yes   | Callback used to return the configuration level directory list.|
139 140

**Example**
W
wusongqing 已提交
141
  ```js
142
  configPolicy.getCfgDirList((error, value) => {
G
Gloria 已提交
143
      if (error == null) {
W
wusongqing 已提交
144
          console.log("value is " + value);
145
      } else {
W
wusongqing 已提交
146
          console.log("error occurs "+ error);
147 148 149 150 151 152 153 154 155
      }
  });
  ```


## getCfgDirList

getCfgDirList(): Promise<Array<string>>

G
Gloria 已提交
156
Obtains the list of configuration level directories. This API uses a promise to return the result.
157 158 159 160

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

**Return value**
W
wusongqing 已提交
161 162 163
| Type                                | Description      |
| ---------------------------------- | -------- |
| Promise<Array<string>> | Promise used to return the configuration level directory list.|
164 165

**Example**
W
wusongqing 已提交
166
  ```js
167
  configPolicy.getCfgDirList().then(value => {
W
wusongqing 已提交
168
      console.log("value is " + value);
169 170 171 172
  }).catch(error => {
      console.log("getCfgDirList promise " + error);
  });
  ```