• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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
16import {AsyncCallback, Callback} from "./basic";
17import connection from "./@ohos.net.connection";
18
19/**
20 * Provides interfaces to manage network policy rules.
21 *
22 * @since 8
23 * @sysCap SystemCapability.Communication.NetManager.Core
24 */
25declare namespace policy {
26  type NetBearType = connection.NetBearType;
27
28  /**
29   * Set the background policy.
30   *
31   * @param allow Indicates whether the background appications are allowed to access network.
32   * @permission ohos.permission.SET_NETWORK_POLICY
33   * @systemapi Hide this for inner system use.
34   */
35  function setBackgroundPolicy(allow: boolean, callback: AsyncCallback<void>): void ;
36  function setBackgroundPolicy(allow: boolean): Promise<void>;
37
38  /**
39   * Query the background policy.
40   *
41   * @param callback Returns the background policy.
42   *      For details, see {@link BackgroundPolicy#BACKGROUND_POLICY_DISABLE}.
43   * @permission ohos.permission.GET_NETWORK_POLICY
44   * @systemapi Hide this for inner system use.
45   */
46  function getBackgroundPolicy(callback: AsyncCallback<BackgroundPolicy>): void;
47  function getBackgroundPolicy(): Promise<BackgroundPolicy>;
48
49  /**
50   * Set policy for the specified UID.
51   *
52   * @param uid the specified UID of application.
53   * @param policy the policy of the current UID of application.
54   *      For details, see {@link NetUidPolicy}.
55   * @permission ohos.permission.SET_NETWORK_POLICY
56   * @systemapi Hide this for inner system use.
57   */
58  function setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback<void>): void;
59  function setPolicyByUid(uid: number, policy: NetUidPolicy): Promise<void>;
60
61  /**
62   * Query the policy of the specified UID.
63   *
64   * @param uid the specified UID of application.
65   * @param callback Returns the policy of the current UID of application.
66   *      For details, see {@link NetUidPolicy}.
67   * @permission ohos.permission.GET_NETWORK_POLICY
68   * @systemapi Hide this for inner system use.
69   */
70  function getPolicyByUid(uid: number, callback: AsyncCallback<NetUidPolicy>): void;
71  function getPolicyByUid(uid: number): Promise<NetUidPolicy>;
72
73  /**
74   * Query the application UIDs of the specified policy.
75   *
76   * @param policy the policy of the current UID of application.
77   *      For details, see {@link NetUidPolicy}.
78   * @param callback Returns the UIDs of the specified policy.
79   * @permission ohos.permission.GET_NETWORK_POLICY
80   * @systemapi Hide this for inner system use.
81   */
82  function getUidsByPolicy(policy: NetUidPolicy, callback: AsyncCallback<Array<number>>): void;
83  function getUidsByPolicy(policy: NetUidPolicy): Promise<Array<number>>;
84
85  /**
86   * Register and unregister network policy listener.
87   *
88   * @permission ohos.permission.CONNECTIVITY_INTERNAL
89   * @systemapi Hide this for inner system use.
90   */
91  function on(type: 'netUidPolicyChange', callback: Callback<{ uid: number, policy: NetUidPolicy }>): void;
92
93  /**
94   * @systemapi Hide this for inner system use.
95   */
96  function off(type: 'netUidPolicyChange', callback?: Callback<{ uid: number, policy: NetUidPolicy }>): void;
97
98  /**
99   * Get network policies.
100   *
101   * @return See {@link NetPolicyQuotaPolicy}.
102   * @permission ohos.permission.GET_NETWORK_POLICY
103   * @systemapi Hide this for inner system use.
104   */
105  function getNetQuotaPolicies(callback: AsyncCallback<Array<NetPolicyQuotaPolicy>>): void;
106  function getNetQuotaPolicies(): Promise<Array<NetPolicyQuotaPolicy>>;
107
108  /**
109   * Set network policies.
110   *
111   * @param quotaPolicies Indicates {@link NetPolicyQuotaPolicy}.
112   * @permission ohos.permission.SET_NETWORK_POLICY
113   * @systemapi Hide this for inner system use.
114   */
115  function setNetQuotaPolicies(quotaPolicies: Array<NetPolicyQuotaPolicy>, callback: AsyncCallback<void>): void;
116  function setNetQuotaPolicies(quotaPolicies: Array<NetPolicyQuotaPolicy>): Promise<void>;
117
118  /**
119   * Temporarily deactivate the specified network management policy.
120   *
121   * @param simId Indicates the specified sim that is valid when netType is cellular.
122   * @param netType Indicates the {@link NetBearType}.
123   * @permission ohos.permission.SET_NETWORK_POLICY
124   * @systemapi Hide this for inner system use.
125   */
126  function setSnoozePolicy(simId: number, netType: NetBearType, callback: AsyncCallback<void>): void;
127  function setSnoozePolicy(simId: number, netType: NetBearType): Promise<void>;
128
129  /**
130   * Reset the specified network management policy.
131   *
132   * @param simId Indicates the specified sim that is valid when netType is cellular.
133   * @permission ohos.permission.SET_NETWORK_POLICY
134   * @systemapi Hide this for inner system use.
135   */
136  function setFactoryPolicy(simId: number, callback: AsyncCallback<void>): void;
137  function setFactoryPolicy(simId: number): Promise<void>;
138
139  export enum BackgroundPolicy {
140    /**
141     * Indicates that applications can use metered networks.
142     */
143    BACKGROUND_POLICY_DISABLE = 1,
144
145    /**
146     * Indicates that only applications in the allowlist can use metered networks.
147     */
148    BACKGROUND_POLICY_ALLOWLISTED = 2,
149
150    /**
151     * Indicates that applications cannot use metered networks.
152     */
153    BACKGROUND_POLICY_ENABLED = 3
154  }
155
156  /**
157   * @systemapi Hide this for inner system use.
158   */
159  export interface NetPolicyQuotaPolicy {
160    /* netType value range in NetBearType */
161    netType: NetBearType;
162    /* The ID of the target card, valid when netType is BEARER_CELLULAR. */
163    simId: number;
164    /*  Time rubbing, for example:1636598990 */
165    periodStartTime: number;
166    /* Unit: The cycle starts on one day of month, for example: M1 Indicates the 1st of each month. */
167    periodDuration: string;
168    /* Alarm threshold */
169    warningBytes: number;
170    /* Limit threshold */
171    limitBytes: number;
172    /* Time rubbing, for example:1636598990, -1 Indicates the policy need not snooze */
173    lastLimitSnooze?: number;
174    /* @see{MeteringMode} */
175    metered?: MeteringMode;
176  }
177
178  /**
179   * @systemapi Hide this for inner system use.
180   */
181  export enum NetUidPolicy {
182    NET_POLICY_NONE = 0,
183    NET_POLICY_ALLOW_METERED_BACKGROUND = 1 << 0,
184    NET_POLICY_TEMPORARY_ALLOW_METERED = 1 << 1,
185    NET_POLICY_REJECT_METERED_BACKGROUND = 1 << 2,
186    NET_POLICY_ALLOW_METERED = 1 << 3,
187    NET_POLICY_REJECT_METERED = 1 << 4,
188    NET_POLICY_ALLOW_ALL = 1 << 5,
189    NET_POLICY_REJECT_ALL = 1 << 6
190  }
191
192  /**
193   * @systemapi Hide this for inner system use.
194   */
195  export enum MeteringMode {
196    /* non metering */
197    UN_METERED = 0,
198    /* metering */
199    METERED = 1
200  }
201}
202
203export default policy;