diff --git a/en/application-dev/reference/apis/js-apis-system-battery.md b/en/application-dev/reference/apis/js-apis-system-battery.md new file mode 100644 index 0000000000000000000000000000000000000000..c65bc13c40412db14b9e0f624db9e37a18f1098e --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-battery.md @@ -0,0 +1,55 @@ +# Battery Level + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> - The APIs of this module are no longer maintained since API version 7. It is recommended that you use [`@ohos.batteryInfo`](js-apis-battery-info.md) instead. +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import battery from '@system.battery'; +``` + + +## battery.getStatus + +getStatus(Object): void + +Obtains the current charging state and battery level. + +**System capability**: SystemCapability.PowerManager.BatteryManager.Core + +**Parameter** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| success | Function | No | Called when the check result is obtained | +| fail | Function | No | Called when the check result fails to be obtained | +| complete | Function | No | Called when the execution is complete | + +The following value will be returned when the check result is obtained. + +| Name | Type | Description | +| -------- | -------- | -------- | +| charging | boolean | Whether the battery is being charged | +| level | number | Current battery level, which ranges from 0.00 to 1.00. | + +**Example** + +``` +export default { + getStatus() { + battery.getStatus({ + success: function(data) { + console.log('success get battery level:' + data.level); + }, + fail: function(data, code) { + console.log('fail to get battery level code:' + code + ', data: ' + data); + }, + }); + }, +} +``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-brightness.md b/en/application-dev/reference/apis/js-apis-system-brightness.md new file mode 100644 index 0000000000000000000000000000000000000000..ce21ce2e7c13ac2b22e5ae026259d685b401c5e9 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-brightness.md @@ -0,0 +1,202 @@ +# Screen Brightness + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> - The APIs of this module are no longer maintained since API version 7. It is recommended that you use [`@ohos.brightness`](js-apis-brightness.md) instead. +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import brightness from '@system.brightness'; +``` + + +## brightness.getValue + +getValue(Object): void + +Obtains the current screen brightness. + +**System capability**: SystemCapability.PowerManager.DisplayPowerManager + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| success | Function | No | Called when the execution is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete | + +The following values will be returned when the operation is successful. + +| Name | Type | Description | +| -------- | -------- | -------- | +| value | number | Screen brightness, which ranges from 1 to 255. | + +**Example** + +``` +export default { + getValue() { + brightness.getValue({ + success: function(data){ + console.log('success get brightness value:' + data.value); + }, + fail: function(data, code) { + console.log('get brightness fail, code: ' + code + ', data: ' + data); + }, + }); + }, +} +``` + + +## brightness.setValue + +setValue(Object): void + +Sets the screen brightness. + +**System capability**: SystemCapability.PowerManager.DisplayPowerManager + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| value | number | Yes | Screen brightness. The value is an integer ranging from 1 to 255.
- If the value is less than or equal to **0**, value **1** will be used.
- If the value is greater than **255**, value **255** will be used.
- If the value contains decimals, the integral part of the value will be used. For example, if value **8.1** is set, value **8** will be used. | +| success | Function | No | Called when the execution is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +**Example** + +``` +export default { + setValue() { + brightness.setValue({ + value: 100, + success: function(){ + console.log('handling set brightness success.'); + }, + fail: function(data, code){ + console.log('handling set brightness value fail, code:' + code + ', data: ' + data); + }, + }); + }, +} +``` + + +## brightness.getMode + +getMode(Object): void + +Obtains the screen brightness adjustment mode. + +**System capability**: SystemCapability.PowerManager.DisplayPowerManager + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| success | Function | No | Called when the execution is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete | + +The following values will be returned when the operation is successful. + +| Name | Type | Description | +| -------- | -------- | -------- | +| mode | number | The value can be **0** or **1**.
- **0**: The screen brightness is manually adjusted.
- **1**: The screen brightness is automatically adjusted. | + +**Example** + +``` +export default { + getMode() { + brightness.getMode({ + success: function(data){ + console.log('success get mode:' + data.mode); + }, + fail: function(data, code){ + console.log('handling get mode fail, code:' + code + ', data: ' + data); + }, + }); + }, +} +``` + + +## brightness.setMode + +setMode(Object): void + +Sets the screen brightness adjustment mode. + +**System capability**: SystemCapability.PowerManager.DisplayPowerManager + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| mode | number | Yes | The value can be **0** or **1**.
- **0**: The screen brightness is manually adjusted.
- **1**: The screen brightness is automatically adjusted. | +| success | Function | No | Called when the execution is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +**Example** + +``` +export default { + setMode() { + brightness.setMode({ + mode: 1, + success: function(){ + console.log('handling set mode success.'); + }, + fail: function(data, code){ + console.log('handling set mode fail, code:' + code + ', data: ' + data); + }, + }); + }, +} +``` + + +## brightness.setKeepScreenOn + +setKeepScreenOn(Object): void + +Sets whether to always keep the screen on. Call this API in **onShow()**. + +**System capability**: SystemCapability.PowerManager.DisplayPowerManager + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| keepScreenOn | boolean | Yes | Whether to always keep the screen on | +| success | Function | No | Called when the execution is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +**Example** + +``` +export default { + setKeepScreenOn() { + brightness.setKeepScreenOn({ + keepScreenOn: true, + success: function () { + console.log('handling set keep screen on success.') + }, + fail: function (data, code) { + console.log('handling set keep screen on fail, code:' + code + ', data: ' + data); + }, + }); + }, +} +``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-cipher.md b/en/application-dev/reference/apis/js-apis-system-cipher.md new file mode 100644 index 0000000000000000000000000000000000000000..b0dc3c5b117f5256020aa0cc5073e662c62cb6c9 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-cipher.md @@ -0,0 +1,165 @@ +# Encryption Algorithm + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. +> - This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR. + + +## Modules to Import + + +``` +import cipher from '@system.cipher' +``` + + +## cipher.rsa + +rsa(Object): void + +Encrypts or decrypts data using RSA. + +**System capability**: SystemCapability.Security.Cipher + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| action | string | Yes | Action type. The options are as follows:
1. **encrypt**: Encrypts data.
2. **decrypt**: Decrypts data. | +| text | string | Yes | Text content to be encrypted or decrypted. The text to be encrypted must be a common text and cannot exceed the length calculated based on the formula (keySize/8 - 66). **keySize** indicates the key length. For example, if the key length is 1024 bytes, the text cannot exceed 62 bytes (1024/8 - 66 = 62). The text content to be decrypted must be a binary value encoded using Base64. The default format is used for Base64 encoding. | +| key | string | Yes | Keys encrypted using RSA. During encryption, this parameter is a public key. During decryption, it is a private key. | +| transformation | string | No | RSA algorithm padding. The default value is **RSA/None/OAEPWithSHA256AndMGF1Padding**. | +| success | Function | No | Called when data is encrypted or decrypted successfully. | +| fail | Function | No | Called when data fails to be encrypted or decrypted. | +| complete | Function | No | Called when the execution is complete. | + +**Example** + +``` +export default { + rsa() { + cipher.rsa({ + // Encrypt data. + action: 'encrypt', + // Text content to be encrypted + text: 'hello', + // Base64-encoded public key used for encryption + key: + 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDc7GR2MrfAoefES+wrs1ns2afT\n' + + 'eJXSfIkEHfPXG9fVFjaws1ho4KcZfsxlA0+SXvc83f2SVGCuzULmM2lxxRCtcUN/\n' + + 'h7SoaYEeluhqFimL2AEjfSwINHCLqObJkcjCfoZpE1JCehPiDOJsyT50Auc08h/4\n' + + 'jHQfanyC1nc62LqUCQIDAQAB', + success: function(data) { + console.log('handling success: ${data.text}'); + }, + fail: function(data, code) { + console.log(`### cipher.rsa encrypt fail ### ${code}: ${data}`); + } + }); + cipher.rsa({ + // Decrypt data. + action: 'decrypt', + // The text to be decrypted is a Base64-encoded binary value, and the decrypted text is "hello". + text: + 'CUg3tTxTIdpCfreIxIBdws3uhd5qXLwcrVl3XDnQzZFVHyjVVCDHS16rjopaZ4C5xU2Tc8mSDzt7\n' + + 'gp9vBfSwi7bMtSUvXG18DlncsKJFDkJpS5t0PkpS9YrJXrY80Gpe+ME6+6dN9bjgqMljbitDdBRf\n' + + 'S/ZWNI4Q8Q0suNjNkGU=', + // Base64-encoded public key used for encryption + key: + 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANzsZHYyt8Ch58RL\n' + + '7CuzWezZp9N4ldJ8iQQd89cb19UWNrCzWGjgpxl+zGUDT5Je9zzd/ZJUYK7NQuYz\n' + + 'aXHFEK1xQ3+HtKhpgR6W6GoWKYvYASN9LAg0cIuo5smRyMJ+hmkTUkJ6E+IM4mzJ\n' + + 'PnQC5zTyH/iMdB9qfILWdzrYupQJAgMBAAECgYEAkibhH0DWR13U0gvYJeD08Lfd\n' + + 'Sw1PMHyquEqIcho9Yv7bF3LOXjOg2EEGPx09mvuwXFgP1Kp1e67XPytr6pQQPzK7\n' + + 'XAPcLPx80R/ZjZs8vNFndDOd1HgD3vSVmYQarNzmKi72tOUWMPevsaFXPHo6Xx3X\n' + + '8x0wYb7XuBsQguRctTECQQD7GWX3JUiyo562iVrpTDPOXsrUxmzCrgz2OZildxMd\n' + + 'Pp/PkyDrx7mEXTpk4K/XnQJ3GpJNi2iDSxDuPSAeJ/aPAkEA4Tw4+1Z43S/xH3C3\n' + + 'nfulYBNyB4si6KEUuC0krcC1pDJ21Gd12efKo5VF8SaJI1ZUQOzguV+dqNsB/JUY\n' + + 'OFfX5wJAB1dKv9r7MR3Peg6x9bggm5vx2h6i914XSuuMJupASM6X5X2rrLj+F3yS\n' + + 'RHi9K1SPyeOg+1tkBtKfABgRZFBOyQJAbuTivUSe73AqTKuHjB4ZF0ubqgEkJ9sf\n' + + 'Q2rekzm9dOFvxjZGPQo1qALX09qATMi1ZN376ukby8ZAnSafLSZ64wJBAM2V37go\n' + + 'Sj44HF76ksRow8gecuQm48NCTGAGTicXg8riKog2GC9y8pMNHAezoR9wXJF7kk+k\n' + + 'lz5cHyoMZ9mcd30=', + success: function(data) { + console.log('handling success: ${data.text}'); + }, + fail: function(data, code) { + console.log(`### cipher.rsa decrypt fail ### ${code}: ${data}`); + }, + }); + } +} +``` + + +## cipher.aes + +aes(Object): void + +Encrypts or decrypts data using AES. + +**System capability**: SystemCapability.Security.Cipher + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| action | string | Yes | Action type. The options are as follows:
1. **encrypt**: Encrypts data.
2. **decrypt**: Decrypts data. | +| text | string | Yes | Text content to be encrypted or decrypted. The text to be encrypted must be a common text. The text content to be decrypted must be a binary value encoded using Base64. The default format is used for Base64 encoding. | +| key | string | Yes | Key used for encryption or decryption, which is a character string encrypted using Base64. | +| transformation | string | No | Encryption mode and padding of the AES algorithm. The default value is **AES/CBC/PKCS5Padding**. | +| iv | string | No | Initial vector for AES-based encryption and decryption. The value is a character string encoded using Base64. The default value is the key value. | +| ivOffset | string | No | Offset of the initial vector for AES-based encryption and decryption. The default value is **0**. | +| ivLen | string | No | Length of the initial vector for AES-based encryption and decryption. The default value is **16**. | +| success | Function | No | Called when data is encrypted or decrypted successfully. | +| fail | Function | No | Called when data fails to be encrypted or decrypted. | +| complete | Function | No | Called when the execution is complete. | + +**Example** + +``` +export default { + aes() { + cipher.aes({ + // Encrypt data. + action: 'encrypt', + // Text content to be encrypted + text: 'hello', + // Base64-encoded key used for encryption + key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=', + transformation: 'AES/CBC/PKCS5Padding', + ivOffset: 0, + ivLen: 16, + success: (data) => { + console.log('handling success: ${data.text}'); + }, + fail: (data, code) => { + console.log(`### cipher.aes encrypt fail ### ${code}: ${data}`); + } + }); + cipher.aes({ + // Decrypt data. + action: 'decrypt', + // Text to be decrypted, which is a Base64-encoded binary value + text: 'CUg3tTxTIdpCfreIxIBdws3uhd5qXLwcrVl3XDnQzZFVHyjVVCDHS16rjopaZ4C5xU2Tc8mSDzt7\n' + + 'gp9vBfSwi7bMtSUvXG18DlncsKJFDkJpS5t0PkpS9YrJXrY80Gpe+ME6+6dN9bjgqMljbitDdBRf\n' + + 'S/ZWNI4Q8Q0suNjNkGU=', + // Base64-encoded key used for decryption + key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=', + transformation: 'AES/CBC/PKCS5Padding', + ivOffset: 0, + ivLen: 16, + success: (data) => { + this.dealTxt = data.text; + }, + fail: (data, code) => { + prompt.showToast({ + message: (`### cipher.aes decrypt fail ### code = ${code}: ${data}`) + }) + }, + }); + } +} + +``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-configuration.md b/en/application-dev/reference/apis/js-apis-system-configuration.md new file mode 100644 index 0000000000000000000000000000000000000000..310abffe5779e336ca676161d09172eb39af5bac --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-configuration.md @@ -0,0 +1,45 @@ +# Application Configuration + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> - The APIs of this module are no longer maintained since API version 7. It is recommended that you use [`@ohos.i18n`](js-apis-i18n.md) and [`@ohos.intl`](js-apis-intl.md) instead. +> +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import configuration from '@system.configuration'; +``` + + +## configuration.getLocale + +getLocale(): <LocaleResponse> + +Obtains the current locale of the application, which is the same as the system locale. + +**System capability**: SystemCapability.ArkUI.ArkUI.Lite + +**Return values** +**Table 1** LocaleResponse + +| Name | Type | Description | +| -------- | -------- | -------- | +| language | string | Current language of the application, for example, **zh**. | +| countryOrRegion | string | Country or region, for example, **CN**. | +| dir | string | Text layout direction. Available values are as follows:
- **ltr**: The text direction is from left to right.
- **rtl**: The text direction is from right to left. | +| unicodeSetting5+ | string | Unicode key set determined by the locale.
For example, **{"nu":"arab"}** indicates that the current locale uses Arabic numerals.
If the current locale does not have a specific key set, an empty set is returned. | + +**Example** + +``` +export default { + getLocale() { + const localeInfo = configuration.getLocale(); + console.info(localeInfo.language); + } +} +``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-device.md b/en/application-dev/reference/apis/js-apis-system-device.md new file mode 100644 index 0000000000000000000000000000000000000000..c5b92831958ea79cb7b868fbf549974cdf39a669 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-device.md @@ -0,0 +1,75 @@ +# Device Information + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> - The APIs of this module are no longer maintained since API version 6. It is recommended that you use [`@ohos.deviceInfo`](js-apis-device-info.md) instead. +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import device from '@system.device'; +``` + + +## device.getInfo + +getInfo(Object): void + +Obtains the device information. + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> Do not call **device.getInfo** before the **onShow** event of the home page. + +**System capability**: SystemCapability.Startup.SysInfo + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| success | Function | No | Called when the device information is obtained | +| fail | Function | No | Called when the device information fails to be obtained | +| complete | Function | No | Called when the execution is complete | + +The following values will be returned when the device information is obtained. + +| Name | Type | Description | +| -------- | -------- | -------- | +| brand | string | Brand | +| manufacturer | string | Manufacturer | +| model | string | Model | +| product | string | Product number | +| language4+ | string | System language | +| region4+ | string | System region | +| windowWidth | number | Window width | +| windowHeight | number | Window height | +| screenDensity4+ | number | Screen density | +| screenShape4+ | string | Screen shape. The options are as follows:
- rect: rectangle screen
- circle: circle screen | +| apiVersion4+ | number | API version | +| releaseType4+ | string | Release type. The value includes both the release type and the API version, for example, Beta1.
Available release types are as follows:
- **Canary**: For the same API version, different canary releases are compatible with each other, but not compatible with those of the **beta** and **release** type.
- **Beta**: For the same API version, different beta releases are compatible with each other, but not compatible with those of the **release** type.
- **Release**: Releases of this type are compatible with the latest five API versions. | +| deviceType4+ | string | Device type | + +The following error code will be returned if the device information fails to be obtained. + +| Error Code | Description | +| -------- | -------- | +| 200 | The returned result contains information that cannot be obtained. | + +**Example** + +``` +export default { + getInfo() { + device.getInfo({ + success: function(data) { + console.log('Device information obtained successfully. Device brand:' + data.brand); + }, + fail: function(data, code) { + console.log('Failed to obtain device information. Error code:'+ code + '; Error information: ' + data); + }, + }); + }, +} +``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-fetch.md b/en/application-dev/reference/apis/js-apis-system-fetch.md new file mode 100644 index 0000000000000000000000000000000000000000..e198f5562db784b6ffe70d0bf359529abce6f2d7 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-fetch.md @@ -0,0 +1,105 @@ +# Data Request + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> - The APIs of this module are no longer maintained since API version 6. It is recommended that you use [`@ohos.net.http`](js-apis-http.md) instead. +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import fetch from '@system.fetch'; +``` + + +## fetch.fetch + +fetch(Object): void + +Obtains data through a network. + +**Required permissions:** ohos.permission.INTERNET + +**System capability**: SystemCapability.Communication.NetStack + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| url | string | Yes | Resource URL. | +| data | string \| Object | No | Request parameter, which can be a string or a JSON object. For details, see Relationship between data and Content-Type. | +| header | Object | No | Request header. | +| method | string | No | Request method. The default value is **GET**. The value can be **OPTIONS**, **GET**, **HEAD**, **POST**, **PUT**, **DELETE **or **TRACE**. | +| responseType | string | No | Response type. The return type can be text or JSON. By default, the return type is determined based on **Content-Type** in the header returned by the server. For details, see return values of the success callback. | +| success | Function | No | Called when the network data is obtained successfully. | +| fail | Function | No | Called when the network data fails to be obtained. | +| complete | Function | No | Called when the execution is complete. | + + **Table1** Relationship between data and Content-Type + +| data | Content-Type | Description | +| -------- | -------- | -------- | +| string | Not set | The default value of Content-Type is text/plain, and the value of data is used as the request body. | +| string | Any type | The value of data is used as the request body. | +| Object | Not set | The default value of **Content-Type** is **application/x-www-form-urlencoded**. The **data** value is encoded based on the URL rule and appended in the request body. | +| Object | application/x-www-form-urlencoded | The value of data is encoded based on the URL rule and is used as the request body. | + +The following values will be returned when data is successfully obtained. + +| Parameter | Type | Description | +| -------- | -------- | -------- | +| code | number | Server status code. | +| data | string \| Object | The type of the returned data is determined by **responseType**. For details, see Relationship between responseType and data returned by the success function. | +| headers | Object | All headers in the response from the server. | + + **Table2** Relationship between responseType and data returned by the success function + +| responseType | data | Description | +| -------- | -------- | -------- | +| N/A | string | When the type in the header returned by the server is **text/\***, **application/json**, **application/javascript**, or **application/xml**, the value is the text content. | +| text | string | Text content. | +| json | Object | A JSON object. | + +**Example** + +``` +export default { + data: { + responseData: 'NA', + url: "test_url", + }, + fetch: function () { + var that = this; + fetch.fetch({ + url: that.url, + success: function(response) { + console.info("fetch success"); + that.responseData = JSON.stringify(response); + }, + fail: function() { + console.info("fetch fail"); + } + }); + } +} +``` + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> HTTPS is supported by default. To support HTTP, you need to add **"network"** to the **config.json** file, and set the attribute **"cleartextTraffic"** to **true**. +> +> ``` +> { +> "deviceConfig": { +> "default": { +> "network": { +> "cleartextTraffic": true +> } +> ... +> } +> } +> ... +> } +> ``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-file.md b/en/application-dev/reference/apis/js-apis-system-file.md new file mode 100644 index 0000000000000000000000000000000000000000..f05357dbe766366e8653c51b85544d12317385ce --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-file.md @@ -0,0 +1,601 @@ +# File Storage + + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> - The APIs of this module are no longer maintained since API version 6. It is recommended that you use [`@ohos.fileio`](js-apis-fileio.md) instead. +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import file from '@system.file'; +``` + + +## file.move + +move(Object): void + +Moves a specified file to a given location. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| srcUri | string | Yes | URI of the file to move. | +| dstUri | string | Yes | URI of the location to which the file is to move. | +| success | Function | No | Called when the source file is moved to the specified location successfully. This function returns the URI of the destination location. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Invalid parameter. | +| 300 | I/O error. | +| 301 | File or directory not exist. | + +**Example** + +``` +export default { + move() { + file.move({ + srcUri: 'internal://app/myfiles1', + dstUri: 'internal://app/myfiles2', + success: function(uri) { + console.log('call success callback success'); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.copy + +copy(Object): void + +Copies a file and saves the copy to a specified location. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| srcUri | string | Yes | URI of the file to copy. | +| dstUri | string | Yes | URI of the location to which the copy is to save.
The directory of application resources and URI of the **tmp** type are not supported. | +| success | Function | No | Called when the source file is copied and saved to the specified location successfully. This function returns the URI of the destination location. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Invalid parameter. | +| 300 | I/O error. | +| 301 | File or directory not exist. | + +**Example** + +``` +export default { + copy() { + file.copy({ + srcUri: 'internal://app/file.txt', + dstUri: 'internal://app/file_copy.txt', + success: function(uri) { + console.log('call success callback success'); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.list + +list(Object): void + +Obtains the list of all files in a specified directory. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| uri | string | Yes | URI of the directory. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +Return values of the **success** callback + +| Name | Type | Description | +| -------- | -------- | -------- | +| fileList | Array<FileInfo> | File list. The format of each file is as follows:
{
uri:'file1',
lastModifiedTime:1589965924479,
length:10240,
type: 'file'
} | + +**Table1** FileInfo + +| Name | Type | Description | +| -------- | -------- | -------- | +| uri | string | File URI. | +| lastModifiedTime | number | Timestamp when the file is stored the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT. | +| length | number | File size, in bytes. | +| type | string | File type. Available values are as follows:
- **dir**: directory
- **file**: file | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Invalid parameter. | +| 300 | I/O error. | +| 301 | File or directory not exist. | + +**Example** + +``` +export default { + list() { + file.list({ + uri: 'internal://app/pic', + success: function(data) { + console.log(data.fileList); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.get + +get(Object): void + +Obtains information about a specified local file. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| uri | string | Yes | File URI. | +| recursive | boolean | No | Whether to recursively obtain the file list under a subdirectory. The default value is **false**. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +Return values of the **success** callback + +| Name | Type | Description | +| -------- | -------- | -------- | +| uri | string | File URI. | +| length | number | File size, in bytes. | +| lastModifiedTime | number | Timestamp when the file is stored the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT. | +| type | string | File type. The values are as follows:
- **dir**: directory
- **file**: file | +| subFiles | Array | File list. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Invalid parameter. | +| 300 | I/O error. | +| 301 | File or directory not exist. | + +**Example** + +``` +export default { + get() { + file.get({ + uri: 'internal://app/file', + success: function(data) { + console.log(data.uri); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.delete + +delete(Object): void + +Deletes local files. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| uri | string | Yes | URI of the file to delete, which cannot be an application resource path. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Incorrect parameter. | +| 300 | I/O error. | +| 301 | File or directory not exist. | + +**Example** + +``` +export default { + delete() { + file.delete({ + uri: 'internal://app/my_file', + success: function() { + console.log('call delete success.'); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.writeText + +writeText(Object): void + +Writes text into a specified file. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| uri | string | Yes | URI of a local file. If it does not exist, a file will be created. | +| text | string | Yes | Character string to write into the local file. | +| encoding | string | No | Encoding format. The default format is UTF-8. | +| append | boolean | No | Whether to enable the append mode. The default value is **false**. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Incorrect parameter. | +| 300 | I/O error. | + +**Example** + +``` +export default { + writeText() { + file.writeText({ + uri: 'internal://app/test.txt', + text: 'Text that just for test.', + success: function() { + console.log('call writeText success.'); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.writeArrayBuffer + +writeArrayBuffer(Object): void + +Writes buffer data into a specified file. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| uri | string | Yes | URI of a local file. If it does not exist, a file will be created. | +| buffer | Uint8Array | Yes | Buffer from which the data is derived. | +| position | number | No | Offset to the position where the writing starts. The default value is **0**. | +| append | boolean | No | Whether to enable the append mode. The default value is **false**. If the value is **true**, the **position** parameter will become invalid. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Invalid parameter. | +| 300 | I/O error. | + +**Example** + +``` +export default { + writeArrayBuffer() { + file.writeArrayBuffer({ + uri: 'internal://app/test', + buffer: new Uint8Array(8), // The buffer is of the Uint8Array type. + success: function() { + console.log('call writeArrayBuffer success.'); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.readText + +readText(Object): void + +Reads text from a specified file. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| uri | string | Yes | URI of a local file. | +| encoding | string | No | Encoding format. The default format is UTF-8. | +| position | number | No | Position where the reading starts. The default value is the start position of the file. | +| length | number | No | Length of the text to be read (in bytes). The default value is **4096**. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +Return values of the **success** callback + +| Name | Type | Description | +| -------- | -------- | -------- | +| text | string | Text read from the specified file. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Invalid parameter. | +| 300 | I/O error. | +| 301 | The file or directory does not exist. | +| 302 | The size of text to read exceeds 4096 bytes. | + +**Example** + +``` +export default { + readText() { + file.readText({ + uri: 'internal://app/text.txt', + success: function(data) { + console.log('call readText success: ' + data.text); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.readArrayBuffer + +readArrayBuffer(Object): void + +Reads buffer data from a specified file. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| uri | string | Yes | URI of a local file. | +| position | number | No | Position where the reading starts. The default value is the start position of the file. | +| length | number | No | Length of data to read. If this parameter is not set, the reading proceeds until the end of the file. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +Return values of the **success** callback + +| Name | Type | Description | +| -------- | -------- | -------- | +| buffer | Uint8Array | File content that is read | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Invalid parameter. | +| 300 | I/O error. | +| 301 | File or directory not exist. | + +**Example** + +``` +export default { + readArrayBuffer() { + file.readArrayBuffer({ + uri: 'internal://app/test', + position: 10, + length: 200, + success: function(data) { + console.log('call readArrayBuffer success: ' + data.buffer); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.access + +access(Object): void + +Checks whether a specified file or directory exists. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| uri | string | Yes | URI of the directory or file. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Invalid parameter. | +| 300 | I/O error. | +| 301 | File or directory not exist. | + +**Example** + +``` +export default { + access() { + file.access({ + uri: 'internal://app/test', + success: function() { + console.log('call access success.'); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.mkdir + +mkdir(Object): void + +Creates a specified directory. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| uri | string | Yes | URI of the directory. | +| recursive | boolean | No | Whether to recursively create upper-level directories of the specified directory. The default value is **false**. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Invalid parameter. | +| 300 | I/O error. | + +**Example** + +``` +export default { + mkdir() { + file.mkdir({ + uri: 'internal://app/test_directory', + success: function() { + console.log('call mkdir success.'); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## file.rmdir + +rmdir(Object): void + +Deletes a specified directory. + +**System capability**: SystemCapability.FileManagement.File.FileIO + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| uri | string | Yes | URI of the directory. | +| recursive | boolean | No | Whether to recursively delete subfiles and subdirectories of the specified directory. The default value is **false**. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 202 | Invalid parameter. | +| 300 | I/O error. | +| 301 | File or directory not exist. | + +**Example** + +``` +export default { + rmdir() { + file.rmdir({ + uri: 'internal://app/test_directory', + success: function() { + console.log('call rmdir success.'); + }, + fail: function(data, code) { + console.error('call fail callback fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-location.md b/en/application-dev/reference/apis/js-apis-system-location.md new file mode 100644 index 0000000000000000000000000000000000000000..ab53fb2d188b7b929c591a67c33cec12984e063c --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-location.md @@ -0,0 +1,211 @@ +# Geographic Location + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> - The APIs of this module are no longer maintained since API version 7. It is recommended that you use [`@ohos.geolocation`](js-apis-geolocation.md) instead. +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import geolocation from '@system.geolocation'; +``` + + +## Required Permissions + +ohos.permission.LOCATION + + +## geolocation.getLocation + +getLocation(Object): void + +Obtains the geographic location. + +**System capability**: SystemCapability.Location.Location.Lite + +**Parameters** + +| Parameter | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| timeout | number | No | Timeout duration, in milliseconds. The default value is **30000**.
The timeout duration is necessary in case the request to obtain the geographic location is rejected for the lack of the required permission, weak positioning signal, or incorrect location settings. After the timeout duration expires, the fail function will be called.
The value is a 32-digit positive integer. If the value set is less than or equal to **0**, the default value will be used. | +| coordType | string | No | Coordinate system type. Available types can be obtained by **getSupportedCoordTypes**. The default type is **wgs84**. | +| success | Function | No | Called when the operation is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete | + +The following values will be returned when the operation is successful. + +| Parameter | Type | Description | +| -------- | -------- | -------- | +| longitude | number | Longitude | +| latitude | number | Latitude | +| altitude | number | Altitude | +| accuracy | number | Location accuracy | +| time | number | Time when the location is obtained | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 601 | Failed to obtain the required permission because the user rejected the request. | +| 602 | Permission not declared. | +| 800 | Operation times out due to a poor network condition or unavailable GPS. | +| 801 | System location disabled. | +| 802 | The method is called again while the previous execution result is not returned yet. | + +**Example** + +``` +export default { + getLocation() { + geolocation.getLocation({ + success: function(data) { + console.log('success get location data. latitude:' + data.latitude); + }, + fail: function(data, code) { + console.log('fail to get location. code:' + code + ', data:' + data); + }, + }); + }, +} +``` + + +## geolocation.getLocationType + +getLocationType(Object): void + +Obtains the supported location types. + +**System capability**: SystemCapability.Location.Location.Lite + +**Parameters** + +| Parameter | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| success | Function | No | Called when the execution is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete | + +The following values will be returned when the operation is successful. + +| Parameter | Type | Description | +| -------- | -------- | -------- | +| types | Array<string> | Available location types, ['gps', 'network'] | + +**Example** + +``` +export default { + getLocationType() { + geolocation.getLocationType({ + success: function(data) { + console.log('success get location type:' + data.types[0]); + }, + fail: function(data, code) { + console.log('fail to get location. code:' + code + ', data:' + data); + }, + }); + }, +} +``` + + +## geolocation.subscribe + +subscribe(Object): void + +Listens to the geographical location. If this method is called multiple times, the last call takes effect. + +**System capability**: SystemCapability.Location.Location.Lite + +**Parameters** + +| Parameter | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| coordType | string | No | Coordinate system type. Available types can be obtained by **getSupportedCoordTypes**. The default type is **wgs84**. | +| success | Function | Yes | Called when the geographical location changes | +| fail | Function | No | Called when the listening fails | + +The following values will be returned when the network type is obtained. + +| Parameter | Type | Description | +| -------- | -------- | -------- | +| longitude | number | Longitude | +| latitude | number | Latitude | +| altitude | number | Altitude | +| accuracy | number | Location accuracy | +| time | number | Time when the location is obtained | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 601 | Failed to obtain the required permission because the user rejected the request. | +| 602 | Permission not declared. | +| 801 | System location disabled. | + +**Example** + +``` +export default { + subscribe() { + geolocation.subscribe({ + success: function(data) { + console.log('get location. latitude:' + data.latitude); + }, + fail: function(data, code) { + console.log('fail to get location. code:' + code + ', data:' + data); + }, + }); + }, +} +``` + + +## geolocation.unsubscribe + +unsubscribe(): void + +Cancels listening to the geographical location. + +**System capability**: SystemCapability.Location.Location.Lite + +**Example** + +``` +export default { + unsubscribe() { + geolocation.unsubscribe(); + }, +} +``` + + +## geolocation.getSupportedCoordTypes + +getSupportedCoordTypes(): Array<string> + +Obtains coordinate system types supported by the device. + +**System capability**: SystemCapability.Location.Location.Lite + +**Return Value** + +| Type | Non-Null | Description | +| -------- | -------- | -------- | +| Array<string> | Yes | Coordinate system types, for example, **[wgs84, gcj02]**. | + +**Example** + +``` +export default { + getSupportedCoordTypes() { + var types = geolocation.getSupportedCoordTypes(); + }, +} +``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-network.md b/en/application-dev/reference/apis/js-apis-system-network.md new file mode 100644 index 0000000000000000000000000000000000000000..1bf121889896276a676f177300f341da5fb327fe --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-network.md @@ -0,0 +1,134 @@ +# Network State + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> - The APIs of this module are no longer maintained since API version 7. It is recommended that you use [`@ohos.telephony.observer`](js-apis-observer.md) instead. +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import network from '@system.network'; +``` + + +## Required Permissions + +ohos.permission.GET_WIFI_INFO + +ohos.permission.GET_NETWORK_INFO + + +## network.getType + +getType(Object): void + +Obtains the network type. + +**System capability**: SystemCapability.Communication.NetManager.Core + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| success | Function | No | Called when the execution is successful. | +| fail | Function | No | Called when the operation fails. | +| complete | Function | No | Called when the execution is complete | + +The following value will be returned when the multimedia volume is obtained. + +| Parameter | Type | Description | +| -------- | -------- | -------- | +| metered | boolean | Whether the billing is based on the data volume. | +| type | string | Network type. The value can be **2G**, **3G**, **4G**, **5G**, **WiFi**, or **none**. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 602 | The current permission is not declared. | + +**Example** + +``` +export default { + getType() { + network.getType({ + success: function(data) { + console.log('success get network type:' + data.type); + }, + fail: function(data, code) { + console.log('fail to get network type code:' + code + ', data:' + data); + }, + }); + }, +} +``` + + +## network.subscribe + +subscribe(Object): void + +Listens to the network connection state. If this method is called multiple times, the last call takes effect. + +**System capability**: SystemCapability.Communication.NetManager.Core + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| success | Function | No | Called when the network connection state changes | +| fail | Function | No | Called when the multimedia volume fails to be obtained. | + +The following value will be returned when the multimedia volume is obtained. + +| Parameter | Type | Description | +| -------- | -------- | -------- | +| metered | boolean | Whether the billing is based on the data volume. | +| type | string | Network type. The value can be **2G**, **3G**, **4G**, **5G**, **WiFi**, or **none**. | + +One of the following error codes will be returned if the listening fails. + +| Error Code | Description | +| -------- | -------- | +| 602 | The current permission is not declared. | +| 200 | The subscription fails. | + +**Example** + +``` +export default { + subscribe() { + network.subscribe({ + success: function(data) { + console.log('network type change type:' + data.type); + }, + fail: function(data, code) { + console.log('fail to subscribe network, code:' + code + ', data:' + data); + }, + }); + }, +} +``` + + +## network.unsubscribe + +unsubscribe(): void + +Cancels listening to the network connection state. + +**System capability**: SystemCapability.Communication.NetManager.Core + +**Example** + +``` +export default { + unsubscribe() { + network.unsubscribe(); + }, +} +``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-package.md b/en/application-dev/reference/apis/js-apis-system-package.md new file mode 100644 index 0000000000000000000000000000000000000000..c6453ec28f1dca4749d8f8c91414a609af161466 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-package.md @@ -0,0 +1,60 @@ +# Application Management + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> +> - The APIs of this module are no longer maintained since API version 7. It is recommended that you use [`@ohos.bundle`](js-apis-Bundle.md) instead. +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import pkg from '@system.package'; +``` + + +## package.hasInstalled + +hasInstalled(Object): void + +Checks whether an application exists, or whether a native application has been installed. + +**Required permissions**: ohos.permission.GET_BUNDLE_INFO + +**System capability**: SystemCapability.BundleManager.BundleFramework + +**Parameter** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| bundleName | string | Yes | Application bundle name | +| success | Function | No | Called when the check result is obtained | +| fail | Function | No | Called when the check result fails to be obtained | +| complete | Function | No | Called when the execution is complete | + +The following value will be returned when the check result is obtained. + +| Name | Type | Description | +| -------- | -------- | -------- | +| result | boolean | Whether the application exists, or whether the native application has been installed | + +**Example** + +``` +export default { + hasInstalled() { + pkg.hasInstalled({ + bundleName: 'com.example.bundlename', + success: function(data) { + console.log('package has installed: ' + data); + }, + fail: function(data, code) { + console.log('query package fail, code: ' + code + ', data: ' + data); + }, + }); + }, +} +``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-request.md b/en/application-dev/reference/apis/js-apis-system-request.md new file mode 100644 index 0000000000000000000000000000000000000000..53fa3bf82253930056ccd2caa524f24d85598046 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-request.md @@ -0,0 +1,189 @@ +# Uploading and Downloading + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> - The APIs of this module are no longer maintained since API version 6. It is recommended that you use [`@ohos.request`](js-apis-request.md) instead. +> +> - The initial APIs of this module are supported since API version 4. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import request from '@system.request'; +``` + +## Required Permissions + +ohos.permission.INTERNET. + + +## request.upload + +upload(Object): void + +Uploads files. + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| url | string | Yes | URL of the upload server. | +| header | Object | No | Request header. | +| method | string | No | Request methods available: **POST** and **PUT**. The default value is **POST**. | +| files | Array<File> | Yes | List of files to upload, which is submitted through **multipart/form-data**. | +| data | Array<RequestData> | No | Form data in the request body. | +| success | Function | No | Called when the download task is complete. | +| fail | Function | No | Called when downloading fails or the task does not exist. | +| complete | Function | No | Called when the execution is complete. | + +**Table 1** File + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| filename | string | No | File name in the header when **multipart** is used. | +| name | string | No | Name of a form item when **multipart** is used. The default value is **file**. | +| uri | string | Yes | Local storage path of a file. | +| type | string | No | Type of the file content. By default, the type is obtained based on the suffix of the file name or URI. | + +**Table 2** RequestData + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| name | string | Yes | Name of the form element | +| value | string | Yes | Value of the form element | + +The following values will be returned when the files are successfully uploaded. + +| Name | Type | Description | +| -------- | -------- | -------- | +| code | number | HTTP status code returned by the server. | +| data | string | Content returned by the server. The value type is determined by the type in the returned headers. | +| headers | Object | Headers returned by the server. | + +**Example** + +``` +export default { + upLoad() { + request.upload({ + url: 'http://www.path.com', + files: [ + { + uri: 'internal://cache/path/to/file.txt', + name: 'file', + filename: 'file.txt', + }, + ], + data:[ + { + name: 'name1', + value: 'value', + }, + ], + success: function(data) { + console.log('upload success, code:' + data.code); + }, + fail: function() { + console.log('upload fail'); + }, + }); + } +} +``` + + +## request.download + +download(Object): void + +Downloads files. + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| url | string | Yes | Resource URL. | +| header | Object | No | Request header. | +| description | string | No | Download description. The default value is the file name. | +| filename | string | No | Name of the file to download. The value is obtained from the current request or resource URL by default. | +| success | Function | No | Called when the download task is complete. | +| fail | Function | No | Called when downloading fails or the task does not exist. | +| complete | Function | No | Called when the execution is complete. | + +Return values of the **success** callback + +| Name | Type | Description | +| -------- | -------- | -------- | +| token | string | Download token, which is used to obtain the download status. | + +One of the following error codes will be returned if the operation fails. + +| Error Code | Description | +| -------- | -------- | +| 400 | Download task failed | + +**Example** + +``` +export default { + downLoad() { + request.download({ + url: 'http://www.path.com', + success: function(data) { + console.log('call success callback success: ' + data.token); + }, + fail: function(data, code) { + console.log('handling fail'); + }, + }); + } +} +``` + + +## request.onDownloadComplete + +onDownloadComplete(Object): void + +Listens to download task status. + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| token | string | Yes | Token of the result returned by the download method | +| success | Function | No | Called when the download task is complete. | +| fail | Function | No | Called when downloading fails or the task does not exist. | +| complete | Function | No | Called when the execution is complete. | + +Return values of the **success** callback + +| Name | Type | Description | +| -------- | -------- | -------- | +| uri | string | URI of the download file | + +One of the following error codes will be returned if the listening fails. + +| Error Code | Description | +| -------- | -------- | +| 400 | Download task failed | +| 401 | Download task not exist | + +**Example** + +``` +export default { + onDownloadComplete() { + request.onDownloadComplete({ + token: 'token-index', + success: function(data) { + console.log('download success, uri:' + data.uri); + }, + fail: function(data, code) { + console.log('download fail'); + }, + }); + } +} +``` \ No newline at end of file diff --git a/en/application-dev/reference/apis/js-apis-system-storage.md b/en/application-dev/reference/apis/js-apis-system-storage.md new file mode 100644 index 0000000000000000000000000000000000000000..2440183d41464f6b88907b096cd0185707192bd5 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-system-storage.md @@ -0,0 +1,162 @@ +# Data Storage + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** +> - The APIs of this module are no longer maintained since API version 6. It is recommended that you use [`@ohos.data.storage`](js-apis-data-storage.md) instead. +> +> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. + + +## Modules to Import + + +``` +import storage from '@system.storage'; +``` + + +## storage.get + +get(Object): void + +Reads the stored content. + +**System capability**: SystemCapability.DistributedDataManager.Preferences.Core + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| key | string | Yes | Content index. | +| default | string | No | Default value returned when the **key** does not exist. | +| success | Function | No | Called when the stored content is successfully read. | +| fail | Function | No | Called when the stored content fails to be read. | +| complete | Function | No | Called when the execution is complete. | + +**Example** + +``` +export default { + storageGet() { + storage.get({ + key: 'storage_key', + success: function(data) { + console.log('call storage.get success: ' + data); + }, + fail: function(data, code) { + console.log('call storage.get fail, code: ' + code + ', data: ' + data); + }, + complete: function() { + console.log('call complete'); + }, + }); + } +} +``` + + +## storage.set + +set(Object): void + +Modifies the stored content. + +**System capability**: SystemCapability.DistributedDataManager.Preferences.Core + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| key | string | Yes | Index of the stored content to be modified. | +| value | string | No | Target storage content. The maximum number of characters allowed is 128. | +| success | Function | No | Called when the stored content is modified successfully. | +| fail | Function | No | Called when the stored content fails to be modified. | +| complete | Function | No | Called when the execution is complete. | + +**Example** + +``` +export default { + storageSet() { + storage.set({ + key: 'storage_key', + value: 'storage value', + success: function() { + console.log('call storage.set success.'); + }, + fail: function(data, code) { + console.log('call storage.set fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## storage.clear + +clear(Object): void + +Clears the stored content. + +**System capability**: SystemCapability.DistributedDataManager.Preferences.Core + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| success | Function | No | Called when the stored content is cleared successfully | +| fail | Function | No | Called when the stored content fails to be cleared | +| complete | Function | No | Called when the execution is complete | + +**Example** + +``` +export default { + storageClear() { + storage.clear({ + success: function() { + console.log('call storage.clear success.'); + }, + fail: function(data, code) { + console.log('call storage.clear fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` + + +## storage.delete + +delete(Object): void + +Deletes the stored content. + +**System capability**: SystemCapability.DistributedDataManager.Preferences.Core + +**Parameters** + +| Name | Type | Mandatory | Description | +| -------- | -------- | -------- | -------- | +| key | string | Yes | Content index. | +| success | Function | No | Called when the stored content is deleted successfully. | +| fail | Function | No | Called when the stored content fails to be deleted. | +| complete | Function | No | Called when the execution is complete. | + +**Example** + +``` +export default { + storageDelete() { + storage.delete({ + key: 'Storage1', + success: function() { + console.log('call storage.delete success.'); + }, + fail: function(data, code) { + console.log('call storage.delete fail, code: ' + code + ', data: ' + data); + }, + }); + } +} +``` \ No newline at end of file