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 9
23 * @syscap SystemCapability.Communication.NetManager.Core
24 */
25declare namespace policy {
26  type NetBearType = connection.NetBearType;
27
28  /**
29   * Control if applications can use data on background.
30   *
31   * @param isAllowed Allow applications to use data on background.
32   * @permission ohos.permission.CONNECTIVITY_INTERNAL
33   * @systemapi Hide this for inner system use.
34   */
35  function setBackgroundAllowed(isAllowed: boolean, callback: AsyncCallback<void>): void;
36  function setBackgroundAllowed(isAllowed: boolean): Promise<void>;
37
38  /**
39   * Get the status if applications can use data on background.
40   *
41   * @param callback Returns that it's allowed or not to use data on background.
42   * @permission ohos.permission.CONNECTIVITY_INTERNAL
43   * @systemapi Hide this for inner system use.
44   */
45  function isBackgroundAllowed(callback: AsyncCallback<boolean>): void;
46  function isBackgroundAllowed(): Promise<boolean>;
47
48  /**
49   * Set the policy for the specified UID.
50   *
51   * @param uid the specified UID of application.
52   * @param policy the policy of the current UID of application.
53   *      For details, see {@link NetUidPolicy}.
54   * @permission ohos.permission.CONNECTIVITY_INTERNAL
55   * @systemapi Hide this for inner system use.
56   */
57  function setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback<void>): void;
58  function setPolicyByUid(uid: number, policy: NetUidPolicy): Promise<void>;
59
60  /**
61   * Query the policy of the specified UID.
62   *
63   * @param uid the specified UID of application.
64   * @param callback Returns the policy of the current UID of application.
65   *      For details, see {@link NetUidPolicy}.
66   * @permission ohos.permission.CONNECTIVITY_INTERNAL
67   * @systemapi Hide this for inner system use.
68   */
69  function getPolicyByUid(uid: number, callback: AsyncCallback<NetUidPolicy>): void;
70  function getPolicyByUid(uid: number): Promise<NetUidPolicy>;
71
72  /**
73   * Query the application UIDs of the specified policy.
74   *
75   * @param policy the policy of the current UID of application.
76   *      For details, see {@link NetUidPolicy}.
77   * @param callback Returns the UIDs of the specified policy.
78   * @permission ohos.permission.CONNECTIVITY_INTERNAL
79   * @systemapi Hide this for inner system use.
80   */
81  function getUidsByPolicy(policy: NetUidPolicy, callback: AsyncCallback<Array<number>>): void;
82  function getUidsByPolicy(policy: NetUidPolicy): Promise<Array<number>>;
83
84  /**
85   * Get network policies.
86   *
87   * @return See {@link NetQuotaPolicy}.
88   * @permission ohos.permission.CONNECTIVITY_INTERNAL
89   * @systemapi Hide this for inner system use.
90   */
91  function getNetQuotaPolicies(callback: AsyncCallback<Array<NetQuotaPolicy>>): void;
92  function getNetQuotaPolicies(): Promise<Array<NetQuotaPolicy>>;
93
94  /**
95   * Set network policies.
96   *
97   * @param quotaPolicies Indicates {@link NetQuotaPolicy}.
98   * @permission ohos.permission.CONNECTIVITY_INTERNAL
99   * @systemapi Hide this for inner system use.
100   */
101  function setNetQuotaPolicies(quotaPolicies: Array<NetQuotaPolicy>, callback: AsyncCallback<void>): void;
102  function setNetQuotaPolicies(quotaPolicies: Array<NetQuotaPolicy>): Promise<void>;
103
104  /**
105   * Reset the specified network management policy.
106   *
107   * @param iccid Indicates the specified sim that is valid when netType is cellular.
108   * @permission ohos.permission.CONNECTIVITY_INTERNAL
109   * @systemapi Hide this for inner system use.
110   */
111  function restoreAllPolicies(iccid: string, callback: AsyncCallback<void>): void;
112  function restoreAllPolicies(iccid: string): Promise<void>;
113
114  /**
115   * Get the status whether the specified uid app can access the metered network or non-metered network.
116   *
117   * @param uid The specified UID of application.
118   * @param isMetered Indicates meterd network or non-metered network.
119   * @permission ohos.permission.CONNECTIVITY_INTERNAL
120   * @systemapi Hide this for inner system use.
121   */
122  function isUidNetAllowed(uid: number, isMetered: boolean, callback: AsyncCallback<boolean>): void;
123  function isUidNetAllowed(uid: number, isMetered: boolean): Promise<boolean>;
124
125  /**
126   * Get the status whether the specified uid app can access the specified iface network.
127   *
128   * @param uid The specified UID of application.
129   * @param iface Iface name.
130   * @permission ohos.permission.CONNECTIVITY_INTERNAL
131   * @systemapi Hide this for inner system use.
132   */
133  function isUidNetAllowed(uid: number, iface: string, callback: AsyncCallback<boolean>): void;
134  function isUidNetAllowed(uid: number, iface: string): Promise<boolean>;
135
136  /**
137   * Set the UID into device idle allow list.
138   *
139   * @param uid The specified UID of application.
140   * @param isAllowed The UID is into allow list or not.
141   * @param callback
142   * @permission ohos.permission.CONNECTIVITY_INTERNAL
143   * @systemapi Hide this for inner system use.
144   */
145  function setDeviceIdleAllowList(uid: number, isAllowed: boolean, callback: AsyncCallback<void>): void;
146  function setDeviceIdleAllowList(uid: number, isAllowed: boolean): Promise<void>;
147
148  /**
149   * Get the allow list of in device idle mode.
150   *
151   * @param callback Returns the list of UIDs
152   * @permission ohos.permission.CONNECTIVITY_INTERNAL
153   * @systemapi Hide this for inner system use.
154   */
155  function getDeviceIdleAllowList(callback: AsyncCallback<Array<number>>): void;
156  function getDeviceIdleAllowList(): Promise<Array<number>>;
157
158  /**
159   * Get the background network restriction policy for the specified uid.
160   *
161   * @param uid The specified UID of application.
162   * @param callback {@link NetBackgroundPolicy}.
163   * @permission ohos.permission.CONNECTIVITY_INTERNAL
164   * @systemapi Hide this for inner system use.
165   */
166  function getBackgroundPolicyByUid(uid: number, callback: AsyncCallback<NetBackgroundPolicy>): void;
167  function getBackgroundPolicyByUid(uid: number): Promise<NetBackgroundPolicy>;
168
169  /**
170   * Reset network policies\rules\quota policies\firewall rules.
171   *
172   * @param iccid Specify the matched iccid of quota policy.
173   * @permission ohos.permission.CONNECTIVITY_INTERNAL
174   * @systemapi Hide this for inner system use.
175   */
176  function resetPolicies(iccid: string, callback: AsyncCallback<void>): void;
177  function resetPolicies(iccid: string): Promise<void>;
178
179  /**
180   * Update the limit or warning remind time of quota policy.
181   *
182   * @param iccid Specify the matched iccid of quota policy when netType is cellular.
183   * @param netType {@link NetBearType}.
184   * @permission ohos.permission.CONNECTIVITY_INTERNAL
185   * @systemapi Hide this for inner system use.
186   */
187  function updateRemindPolicy(netType: NetBearType, iccid: string, remindType: RemindType, callback: AsyncCallback<void>): void;
188  function updateRemindPolicy(netType: NetBearType, iccid: string, remindType: RemindType): Promise<void>;
189
190  /**
191   * Set the policy to access the network of the specified application.
192   *
193   * @permission ohos.permission.MANAGE_NET_STRATEGY
194   * @param { number } uid - The specified UID of application.
195   * @param { NetworkAccessPolicy } policy - The network access policy of application. For details, see {@link NetworkAccessPolicy}.
196   * @param { boolean } [isReconfirmed] - Whether this operation is reconfirmed by user or not. Default false.
197   * @throws { BusinessError } 201 - Permission denied.
198   * @throws { BusinessError } 202 - Non-system applications use system APIs.
199   * @throws { BusinessError } 401 - Parameter error.
200   * @throws { BusinessError } 2100001 - Invalid parameter value.
201   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
202   * @throws { BusinessError } 2100003 - System internal error.
203   * @syscap SystemCapability.Communication.NetManager.Core
204   * @systemapi Hide this for inner system use.
205   * @since 12
206   */
207  function setNetworkAccessPolicy(uid: number, policy: NetworkAccessPolicy, isReconfirmed?: boolean): Promise<void>;
208
209  /**
210   * Query the network access policy of the specified application.
211   *
212   * @permission ohos.permission.MANAGE_NET_STRATEGY
213   * @param { number } uid - The specified UID of application.
214   * @returns { Promise<NetworkAccessPolicy> } Returns the network access policy of the application. For details, see {@link NetworkAccessPolicy}.
215   * @throws { BusinessError } 201 - Permission denied.
216   * @throws { BusinessError } 202 - Non-system applications use system APIs.
217   * @throws { BusinessError } 401 - Parameter error.
218   * @throws { BusinessError } 2100001 - Invalid parameter value.
219   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
220   * @throws { BusinessError } 2100003 - System internal error.
221   * @syscap SystemCapability.Communication.NetManager.Core
222   * @systemapi Hide this for inner system use.
223   * @since 12
224   */
225  function getNetworkAccessPolicy(uid: number): Promise<NetworkAccessPolicy>;
226
227  /**
228   * Query the network access policy of all applications.
229   * @permission ohos.permission.MANAGE_NET_STRATEGY
230   * @returns { Promise<UidNetworkAccessPolicy> } the network access policy of all applications.
231   * @throws { BusinessError } 201 - Permission denied.
232   * @throws { BusinessError } 202 - Non-system applications use system APIs.
233   * @throws { BusinessError } 401 - Parameter error.
234   * @throws { BusinessError } 2100001 - Invalid parameter value.
235   * @throws { BusinessError } 2100002 - Operation failed. Cannot connect to service.
236   * @throws { BusinessError } 2100003 - System internal error.
237   * @syscap SystemCapability.Communication.NetManager.Core
238   * @systemapi Hide this for inner system use.
239   * @since 12
240   */
241  function getNetworkAccessPolicy(): Promise<UidNetworkAccessPolicy>;
242
243  /**
244   * Register uid policy change listener.
245   *
246   * @permission ohos.permission.CONNECTIVITY_INTERNAL
247   * @systemapi Hide this for inner system use.
248   */
249  function on(type: "netUidPolicyChange", callback: Callback<{ uid: number, policy: NetUidPolicy }>): void;
250
251  /**
252   * Unregister uid policy change listener.
253   *
254   * @permission ohos.permission.CONNECTIVITY_INTERNAL
255   * @systemapi Hide this for inner system use.
256   */
257  function off(type: "netUidPolicyChange", callback?: Callback<{ uid: number, policy: NetUidPolicy }>): void;
258
259  /**
260   * Register uid rule change listener.
261   *
262   * @permission ohos.permission.CONNECTIVITY_INTERNAL
263   * @systemapi Hide this for inner system use.
264   */
265  function on(type: "netUidRuleChange", callback: Callback<{ uid: number, rule: NetUidRule }>): void;
266
267  /**
268   * Unregister uid rule change listener.
269   *
270   * @permission ohos.permission.CONNECTIVITY_INTERNAL
271   * @systemapi Hide this for inner system use.
272   */
273  function off(type: "netUidRuleChange", callback?: Callback<{ uid: number, rule: NetUidRule }>): void;
274
275  /**
276   * Register metered ifaces change listener.
277   *
278   * @permission ohos.permission.CONNECTIVITY_INTERNAL
279   * @systemapi Hide this for inner system use.
280   */
281  function on(type: "netMeteredIfacesChange", callback: Callback<Array<string>>): void;
282
283  /**
284   * Unregister metered ifaces change listener.
285   *
286   * @permission ohos.permission.CONNECTIVITY_INTERNAL
287   * @systemapi Hide this for inner system use.
288   */
289  function off(type: "netMeteredIfacesChange", callback?: Callback<Array<string>>): void;
290
291  /**
292   * Register quota policies change listener.
293   *
294   * @permission ohos.permission.CONNECTIVITY_INTERNAL
295   * @systemapi Hide this for inner system use.
296   */
297  function on(type: "netQuotaPolicyChange", callback: Callback<Array<NetQuotaPolicy>>): void;
298
299  /**
300   * Unregister quota policies change listener.
301   *
302   * @permission ohos.permission.CONNECTIVITY_INTERNAL
303   * @systemapi Hide this for inner system use.
304   */
305  function off(type: "netQuotaPolicyChange", callback?: Callback<Array<NetQuotaPolicy>>): void;
306
307  /**
308   * Register network background policy change listener.
309   *
310   * @permission ohos.permission.CONNECTIVITY_INTERNAL
311   * @systemapi Hide this for inner system use.
312   */
313  function on(type: "netBackgroundPolicyChange", callback: Callback<boolean>): void;
314
315  /**
316   * Unregister network background policy change listener.
317   *
318   * @permission ohos.permission.CONNECTIVITY_INTERNAL
319   * @systemapi Hide this for inner system use.
320   */
321  function off(type: "netBackgroundPolicyChange", callback?: Callback<boolean>): void;
322
323  /**
324   * Indicate whether the application can use metered networks in background.
325   *
326   * @systemapi Hide this for inner system use.
327   */
328  export enum NetBackgroundPolicy {
329    /* Default value. */
330    NET_BACKGROUND_POLICY_NONE = 0,
331    /* Apps can use metered networks on background. */
332    NET_BACKGROUND_POLICY_ENABLE = 1,
333    /* Apps can't use metered networks on background. */
334    NET_BACKGROUND_POLICY_DISABLE = 2,
335    /* Only apps in allow list can use metered networks on background. */
336    NET_BACKGROUND_POLICY_ALLOW_LIST = 3,
337  }
338
339  /**
340   * Policy for net quota, includes usage period, limit and warning actions.
341   *
342   * @systemapi Hide this for inner system use.
343   */
344  export interface NetQuotaPolicy {
345    /* netType see {@link NetBearType}. */
346    netType: NetBearType;
347    /* The ID of the target card, valid when netType is BEARER_CELLULAR. */
348    iccid: string;
349    /* To specify the identity of network, such as different WLAN. */
350    ident: string;
351    /* The period and the start time for quota policy, defalt: "M1". */
352    periodDuration: string;
353    /* The warning threshold of traffic, default:  DATA_USAGE_UNKNOWN. */
354    warningBytes: number;
355    /* The limit threshold of traffic, default: DATA_USAGE_UNKNOWN. */
356    limitBytes: number;
357    /* The updated wall time that last warning remind, default: REMIND_NEVER. */
358    lastWarningRemind: number;
359    /* The updated wall time that last limit remind, default: REMIND_NEVER. */
360    lastLimitRemind: number;
361    /* Is meterd network or not. */
362    metered: boolean;
363    /* The action while the used bytes reach the limit, see {@link LimitAction}. */
364    limitAction: LimitAction;
365  }
366
367  /**
368   * The action when quota policy hit the limit.
369   *
370   * @systemapi Hide this for inner system use.
371   */
372  export enum LimitAction {
373    /* Default action, do nothing. */
374    LIMIT_ACTION_NONE = -1,
375    /* Access is disabled, when quota policy hit the limit. */
376    LIMIT_ACTION_DISABLE = 0,
377    /* The user is billed automatically, when quota policy hit the limit. */
378    LIMIT_ACTION_AUTO_BILL = 1,
379  }
380
381  /**
382   * Rules whether an uid can access to a metered or non-metered network.
383   *
384   * @systemapi Hide this for inner system use.
385   */
386  export enum NetUidRule {
387    /* Default uid rule. */
388    NET_RULE_NONE = 0,
389    /* Allow traffic on metered networks while app is foreground. */
390    NET_RULE_ALLOW_METERED_FOREGROUND = 1 << 0,
391    /* Allow traffic on metered network. */
392    NET_RULE_ALLOW_METERED = 1 << 1,
393    /* Reject traffic on metered network. */
394    NET_RULE_REJECT_METERED = 1 << 2,
395    /* Allow traffic on all network (metered or non-metered). */
396    NET_RULE_ALLOW_ALL = 1 << 5,
397    /* Reject traffic on all network. */
398    NET_RULE_REJECT_ALL = 1 << 6,
399  }
400
401  /**
402   * Specify the remind type, see {@link updateRemindPolicy}.
403   *
404   * @systemapi Hide this for inner system use.
405   */
406  export enum RemindType {
407    /* Warning remind. */
408    REMIND_TYPE_WARNING = 1,
409    /* Limit remind. */
410    REMIND_TYPE_LIMIT = 2,
411  }
412
413  /**
414   * Network policy for uid.
415   *
416   * @systemapi Hide this for inner system use.
417   */
418  export enum NetUidPolicy {
419    /* Default net policy. */
420    NET_POLICY_NONE = 0,
421    /* Reject on metered networks when app in background. */
422    NET_POLICY_ALLOW_METERED_BACKGROUND = 1 << 0,
423    /* Allow on metered networks when app in background. */
424    NET_POLICY_REJECT_METERED_BACKGROUND = 1 << 1,
425  }
426
427  /**
428   * Network policies that limit the specified UID of application to access the network.
429   * @interface NetworkAccessPolicy
430   * @syscap SystemCapability.Communication.NetManager.Core
431   * @systemapi Hide this for inner system use.
432   * @since 12
433   */
434  export interface NetworkAccessPolicy {
435    /**
436     * Indicate whether the application can be allowed to access the network by wifi.
437     * @type { boolean }
438     * @syscap SystemCapability.Communication.NetManager.Core
439     * @systemapi Hide this for inner system use.
440     * @since 12
441     */
442    allowWiFi?: boolean;
443    /**
444     * Indicate whether the application can be allowed to access the network by cellular.
445     * @type { boolean }
446     * @syscap SystemCapability.Communication.NetManager.Core
447     * @systemapi Hide this for inner system use.
448     * @since 12
449     */
450    allowCellular?: boolean;
451  }
452
453  /**
454   * Provides the container definition for network access policy key-value pairs.
455   * @interface UidNetworkAccessPolicy
456   * @syscap SystemCapability.Communication.NetManager.Core
457   * @systemapi Hide this for inner system use.
458   * @since 12
459   */
460  export interface UidNetworkAccessPolicy {
461      /**
462       * @type key:value pair. Key indicates the specified UID of an application. For value, see @NetworkAccessPolicy.
463       * @syscap SystemCapability.Communication.NetManager.Core
464       * @systemapi Hide this for inner system use.
465       * @since 12
466       */
467      [uid: number]: NetworkAccessPolicy;
468  }
469}
470
471export default policy;
472