• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * This module provides the Network Response.
18 * @interface NetworkResponse
19 * @syscap SystemCapability.Communication.NetManager.Core
20 * @since 3
21 */
22export interface NetworkResponse {
23  /**
24   * Network type. The values can be 2G, 3G, 4G, WiFi, or none.
25   * @type {string}
26   * @syscap SystemCapability.Communication.NetManager.Core
27   * @since 3
28   */
29  type: string;
30  /**
31   * Whether the billing is based on the data volume.
32   * @type {boolean}
33   * @syscap SystemCapability.Communication.NetManager.Core
34   * @since 3
35   */
36  metered: boolean;
37}
38
39/**
40 * @syscap SystemCapability.Communication.NetManager.Core
41 * @since 3
42 */
43export default class Network {
44  /**
45   * Obtains the network type.
46   * @param { object } options - Options.
47   * @syscap SystemCapability.Communication.NetManager.Core
48   * @since 3
49   */
50  static getType(options?: {
51    /**
52     * Called when the network type is obtained.
53     * @syscap SystemCapability.Communication.NetManager.Core
54     * @since 3
55     */
56    success?: (data: NetworkResponse) => void;
57    /**
58     * Called when the network type fails to be obtained.
59     * @syscap SystemCapability.Communication.NetManager.Core
60     * @since 3
61     */
62    fail?: (data: any, code: number) => void;
63    /**
64     * Called when the execution is completed.
65     * @syscap SystemCapability.Communication.NetManager.Core
66     * @since 3
67     */
68    complete?: () => void;
69  }): void;
70
71  /**
72   * Listens to the network connection state. If this method is called multiple times, the last call takes effect.
73   * @param { object } options - Options.
74   * @syscap SystemCapability.Communication.NetManager.Core
75   * @since 3
76   */
77  static subscribe(options?: {
78    /**
79     * Called when the network connection state changes.
80     * @syscap SystemCapability.Communication.NetManager.Core
81     * @since 3
82     */
83    success?: (data: NetworkResponse) => void;
84    /**
85     * Called when the listening fails.
86     * @syscap SystemCapability.Communication.NetManager.Core
87     * @since 3
88     */
89    fail?: (data: any, code: number) => void;
90  }): void;
91
92  /**
93   * Cancels listening to the network connection state.
94   * @syscap SystemCapability.Communication.NetManager.Core
95   * @since 3
96   */
97  static unsubscribe(): void;
98}
99