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 * Register uid policy change listener. 192 * 193 * @permission ohos.permission.CONNECTIVITY_INTERNAL 194 * @systemapi Hide this for inner system use. 195 */ 196 function on(type: "netUidPolicyChange", callback: Callback<{ uid: number, policy: NetUidPolicy }>): void; 197 198 /** 199 * Unregister uid policy change listener. 200 * 201 * @permission ohos.permission.CONNECTIVITY_INTERNAL 202 * @systemapi Hide this for inner system use. 203 */ 204 function off(type: "netUidPolicyChange", callback?: Callback<{ uid: number, policy: NetUidPolicy }>): void; 205 206 /** 207 * Register uid rule change listener. 208 * 209 * @permission ohos.permission.CONNECTIVITY_INTERNAL 210 * @systemapi Hide this for inner system use. 211 */ 212 function on(type: "netUidRuleChange", callback: Callback<{ uid: number, rule: NetUidRule }>): void; 213 214 /** 215 * Unregister uid rule change listener. 216 * 217 * @permission ohos.permission.CONNECTIVITY_INTERNAL 218 * @systemapi Hide this for inner system use. 219 */ 220 function off(type: "netUidRuleChange", callback?: Callback<{ uid: number, rule: NetUidRule }>): void; 221 222 /** 223 * Register metered ifaces change listener. 224 * 225 * @permission ohos.permission.CONNECTIVITY_INTERNAL 226 * @systemapi Hide this for inner system use. 227 */ 228 function on(type: "netMeteredIfacesChange", callback: Callback<Array<string>>): void; 229 230 /** 231 * Unregister metered ifaces change listener. 232 * 233 * @permission ohos.permission.CONNECTIVITY_INTERNAL 234 * @systemapi Hide this for inner system use. 235 */ 236 function off(type: "netMeteredIfacesChange", callback?: Callback<Array<string>>): void; 237 238 /** 239 * Register quota policies change listener. 240 * 241 * @permission ohos.permission.CONNECTIVITY_INTERNAL 242 * @systemapi Hide this for inner system use. 243 */ 244 function on(type: "netQuotaPolicyChange", callback: Callback<Array<NetQuotaPolicy>>): void; 245 246 /** 247 * Unregister quota policies change listener. 248 * 249 * @permission ohos.permission.CONNECTIVITY_INTERNAL 250 * @systemapi Hide this for inner system use. 251 */ 252 function off(type: "netQuotaPolicyChange", callback?: Callback<Array<NetQuotaPolicy>>): void; 253 254 /** 255 * Register network background policy change listener. 256 * 257 * @permission ohos.permission.CONNECTIVITY_INTERNAL 258 * @systemapi Hide this for inner system use. 259 */ 260 function on(type: "netBackgroundPolicyChange", callback: Callback<boolean>): void; 261 262 /** 263 * Unregister network background policy change listener. 264 * 265 * @permission ohos.permission.CONNECTIVITY_INTERNAL 266 * @systemapi Hide this for inner system use. 267 */ 268 function off(type: "netBackgroundPolicyChange", callback?: Callback<boolean>): void; 269 270 /** 271 * Indicate whether the application can use metered networks in background. 272 * 273 * @systemapi Hide this for inner system use. 274 */ 275 export enum NetBackgroundPolicy { 276 /* Default value. */ 277 NET_BACKGROUND_POLICY_NONE = 0, 278 /* Apps can use metered networks on background. */ 279 NET_BACKGROUND_POLICY_ENABLE = 1, 280 /* Apps can't use metered networks on background. */ 281 NET_BACKGROUND_POLICY_DISABLE = 2, 282 /* Only apps in allow list can use metered networks on background. */ 283 NET_BACKGROUND_POLICY_ALLOW_LIST = 3, 284 } 285 286 /** 287 * Policy for net quota, includes usage period, limit and warning actions. 288 * 289 * @systemapi Hide this for inner system use. 290 */ 291 export interface NetQuotaPolicy { 292 /* netType see {@link NetBearType}. */ 293 netType: NetBearType; 294 /* The ID of the target card, valid when netType is BEARER_CELLULAR. */ 295 iccid: string; 296 /* To specify the identity of network, such as different WLAN. */ 297 ident: string; 298 /* The period and the start time for quota policy, defalt: "M1". */ 299 periodDuration: string; 300 /* The warning threshold of traffic, default: DATA_USAGE_UNKNOWN. */ 301 warningBytes: number; 302 /* The limit threshold of traffic, default: DATA_USAGE_UNKNOWN. */ 303 limitBytes: number; 304 /* The updated wall time that last warning remind, default: REMIND_NEVER. */ 305 lastWarningRemind: number; 306 /* The updated wall time that last limit remind, default: REMIND_NEVER. */ 307 lastLimitRemind: number; 308 /* Is meterd network or not. */ 309 metered: boolean; 310 /* The action while the used bytes reach the limit, see {@link LimitAction}. */ 311 limitAction: LimitAction; 312 } 313 314 /** 315 * The action when quota policy hit the limit. 316 * 317 * @systemapi Hide this for inner system use. 318 */ 319 export enum LimitAction { 320 /* Default action, do nothing. */ 321 LIMIT_ACTION_NONE = -1, 322 /* Access is disabled, when quota policy hit the limit. */ 323 LIMIT_ACTION_DISABLE = 0, 324 /* The user is billed automatically, when quota policy hit the limit. */ 325 LIMIT_ACTION_AUTO_BILL = 1, 326 } 327 328 /** 329 * Rules whether an uid can access to a metered or non-metered network. 330 * 331 * @systemapi Hide this for inner system use. 332 */ 333 export enum NetUidRule { 334 /* Default uid rule. */ 335 NET_RULE_NONE = 0, 336 /* Allow traffic on metered networks while app is foreground. */ 337 NET_RULE_ALLOW_METERED_FOREGROUND = 1 << 0, 338 /* Allow traffic on metered network. */ 339 NET_RULE_ALLOW_METERED = 1 << 1, 340 /* Reject traffic on metered network. */ 341 NET_RULE_REJECT_METERED = 1 << 2, 342 /* Allow traffic on all network (metered or non-metered). */ 343 NET_RULE_ALLOW_ALL = 1 << 5, 344 /* Reject traffic on all network. */ 345 NET_RULE_REJECT_ALL = 1 << 6, 346 } 347 348 /** 349 * Specify the remind type, see {@link updateRemindPolicy}. 350 * 351 * @systemapi Hide this for inner system use. 352 */ 353 export enum RemindType { 354 /* Warning remind. */ 355 REMIND_TYPE_WARNING = 1, 356 /* Limit remind. */ 357 REMIND_TYPE_LIMIT = 2, 358 } 359 360 /** 361 * Network policy for uid. 362 * 363 * @systemapi Hide this for inner system use. 364 */ 365 export enum NetUidPolicy { 366 /* Default net policy. */ 367 NET_POLICY_NONE = 0, 368 /* Reject on metered networks when app in background. */ 369 NET_POLICY_ALLOW_METERED_BACKGROUND = 1 << 0, 370 /* Allow on metered networks when app in background. */ 371 NET_POLICY_REJECT_METERED_BACKGROUND = 1 << 1, 372 } 373} 374 375export default policy;