From 738ad911c7e433462d5b8a83e96d60fb4763c6a8 Mon Sep 17 00:00:00 2001 From: markchien Date: Thu, 9 Dec 2021 18:15:45 +0800 Subject: [PATCH] Add updateMeteredNetwork{Allow, Deny}List APIs To deprecated below netd binder interfaces and move the functionality to tethering(connectivity) mainline module: bandwidthAddNaughtyApp bandwidthRemoveNaughtyApp bandwidthAddNiceApp bandwidthRemoveNiceApp Expose updateMeteredNetwork{Allow, Deny}List APIs to support the caller outside the module. Currently the two APIs are still call to INetd binders. Once functionality is moved to mainline module, will switch to use them. Bug: 209935649 Test: m Change-Id: I8df720935748c2587f91a7b760cfd5a93a0fa852 --- framework/api/module-lib-current.txt | 2 + .../src/android/net/ConnectivityManager.java | 44 +++++++++++++++++++ .../src/android/net/IConnectivityManager.aidl | 4 ++ .../android/server/ConnectivityService.java | 30 +++++++++++++ 4 files changed, 80 insertions(+) diff --git a/framework/api/module-lib-current.txt b/framework/api/module-lib-current.txt index 50dd2ad5da..d3a68ae310 100644 --- a/framework/api/module-lib-current.txt +++ b/framework/api/module-lib-current.txt @@ -24,6 +24,8 @@ package android.net { method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_TEST_NETWORKS, android.Manifest.permission.NETWORK_STACK}) public void simulateDataStall(int, long, @NonNull android.net.Network, @NonNull android.os.PersistableBundle); method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void startCaptivePortalApp(@NonNull android.net.Network); method public void systemReady(); + method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void updateMeteredNetworkAllowList(int, boolean); + method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void updateMeteredNetworkDenyList(int, boolean); field public static final String ACTION_CLEAR_DNS_CACHE = "android.net.action.CLEAR_DNS_CACHE"; field public static final String ACTION_PROMPT_LOST_VALIDATION = "android.net.action.PROMPT_LOST_VALIDATION"; field public static final String ACTION_PROMPT_PARTIAL_CONNECTIVITY = "android.net.action.PROMPT_PARTIAL_CONNECTIVITY"; diff --git a/framework/src/android/net/ConnectivityManager.java b/framework/src/android/net/ConnectivityManager.java index c21bcfa18a..dbc7fcf339 100644 --- a/framework/src/android/net/ConnectivityManager.java +++ b/framework/src/android/net/ConnectivityManager.java @@ -5511,4 +5511,48 @@ public class ConnectivityManager { public static Range getIpSecNetIdRange() { return new Range(TUN_INTF_NETID_START, TUN_INTF_NETID_START + TUN_INTF_NETID_RANGE - 1); } + + /** + * Allow target application using metered network. + * + * @param uid uid of target app + * @hide + */ + @SystemApi(client = MODULE_LIBRARIES) + @RequiresPermission(anyOf = { + android.Manifest.permission.NETWORK_SETTINGS, + android.Manifest.permission.NETWORK_STACK, + NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK + }) + public void updateMeteredNetworkAllowList(final int uid, final boolean add) { + try { + mService.updateMeteredNetworkAllowList(uid, add); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } catch (IllegalStateException ie) { + throw ie; + } + } + + /** + * Disallow target application using metered network. + * + * @param uid uid of target app + * @hide + */ + @SystemApi(client = MODULE_LIBRARIES) + @RequiresPermission(anyOf = { + android.Manifest.permission.NETWORK_SETTINGS, + android.Manifest.permission.NETWORK_STACK, + NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK + }) + public void updateMeteredNetworkDenyList(final int uid, final boolean add) { + try { + mService.updateMeteredNetworkDenyList(uid, add); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } catch (IllegalStateException ie) { + throw ie; + } + } } diff --git a/framework/src/android/net/IConnectivityManager.aidl b/framework/src/android/net/IConnectivityManager.aidl index 50ec78120f..96a0cee5cb 100644 --- a/framework/src/android/net/IConnectivityManager.aidl +++ b/framework/src/android/net/IConnectivityManager.aidl @@ -228,4 +228,8 @@ interface IConnectivityManager void unofferNetwork(in INetworkOfferCallback callback); void setTestAllowBadWifiUntil(long timeMs); + + void updateMeteredNetworkAllowList(int uid, boolean add); + + void updateMeteredNetworkDenyList(int uid, boolean add); } diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java index 6227bb25b9..cb9df547df 100644 --- a/service/src/com/android/server/ConnectivityService.java +++ b/service/src/com/android/server/ConnectivityService.java @@ -10566,4 +10566,34 @@ public class ConnectivityService extends IConnectivityManager.Stub return createNetworkRequest(NetworkRequest.Type.REQUEST, netcap); } } + + @Override + public void updateMeteredNetworkAllowList(final int uid, final boolean add) { + enforceNetworkStackOrSettingsPermission(); + + try { + if (add) { + mNetd.bandwidthAddNiceApp(uid); + } else { + mNetd.bandwidthRemoveNiceApp(uid); + } + } catch (RemoteException | ServiceSpecificException e) { + throw new IllegalStateException(e); + } + } + + @Override + public void updateMeteredNetworkDenyList(final int uid, final boolean add) { + enforceNetworkStackOrSettingsPermission(); + + try { + if (add) { + mNetd.bandwidthAddNaughtyApp(uid); + } else { + mNetd.bandwidthRemoveNaughtyApp(uid); + } + } catch (RemoteException | ServiceSpecificException e) { + throw new IllegalStateException(e); + } + } }