From e46042b833c96ae18045fe9b96e6584db9447b4e Mon Sep 17 00:00:00 2001 From: markchien Date: Wed, 2 Mar 2022 18:07:35 +0800 Subject: [PATCH] Split updateMeteredNetwork{Allow, Deny}List into add and remove Split the updateXXX methods into an addXXX and removeXXX instead of using a boolean parameter to indicate whether the uid should be added or removed Bug: 218494748 Test: atest FrameworksNetTests Change-Id: I868cf35c8f51b25d8719e618c6c48a5cd642da7f --- framework/api/module-lib-current.txt | 6 +- .../src/android/net/ConnectivityManager.java | 62 ++++++++++++++++--- .../server/NetworkManagementServiceTest.java | 8 +-- 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/framework/api/module-lib-current.txt b/framework/api/module-lib-current.txt index b65c0ce53f..e7421a3798 100644 --- a/framework/api/module-lib-current.txt +++ b/framework/api/module-lib-current.txt @@ -6,6 +6,8 @@ package android.net { } public class ConnectivityManager { + method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void addUidToMeteredNetworkAllowList(int); + method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void addUidToMeteredNetworkDenyList(int); method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void factoryReset(); method @NonNull @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK, android.Manifest.permission.NETWORK_SETTINGS}) public java.util.List getAllNetworkStateSnapshots(); method @Nullable public android.net.ProxyInfo getGlobalProxy(); @@ -14,6 +16,8 @@ package android.net { method @Nullable @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_STACK, android.Manifest.permission.NETWORK_SETTINGS}) public android.net.NetworkCapabilities redactNetworkCapabilitiesForPackage(@NonNull android.net.NetworkCapabilities, int, @NonNull String); method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_SETTINGS}) public void registerDefaultNetworkCallbackForUid(int, @NonNull android.net.ConnectivityManager.NetworkCallback, @NonNull android.os.Handler); method @RequiresPermission(anyOf={android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_SETTINGS}) public void registerSystemDefaultNetworkCallback(@NonNull android.net.ConnectivityManager.NetworkCallback, @NonNull android.os.Handler); + method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void removeUidFromMeteredNetworkAllowList(int); + method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void removeUidFromMeteredNetworkDenyList(int); method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void replaceFirewallChain(int, @NonNull int[]); method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void requestBackgroundNetwork(@NonNull android.net.NetworkRequest, @NonNull android.net.ConnectivityManager.NetworkCallback, @NonNull android.os.Handler); method @Deprecated public boolean requestRouteToHostAddress(int, java.net.InetAddress); @@ -31,8 +35,6 @@ package android.net { method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void swapActiveStatsMap(); 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 updateFirewallRule(int, int, boolean); - 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 e8e1efa09f..5568bdc58c 100644 --- a/framework/src/android/net/ConnectivityManager.java +++ b/framework/src/android/net/ConnectivityManager.java @@ -5706,8 +5706,8 @@ public class ConnectivityManager { } /** - * Sets whether the specified UID is allowed to use data on metered networks even when - * background data is restricted. + * Adds the specified UID to the list of UIds that are allowed to use data on metered networks + * even when background data is restricted. The deny list takes precedence over the allow list. * * @param uid uid of target app * @throws IllegalStateException if updating allow list failed. @@ -5719,17 +5719,40 @@ public class ConnectivityManager { android.Manifest.permission.NETWORK_STACK, NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK }) - public void updateMeteredNetworkAllowList(final int uid, final boolean add) { + public void addUidToMeteredNetworkAllowList(final int uid) { try { - mService.updateMeteredNetworkAllowList(uid, add); + mService.updateMeteredNetworkAllowList(uid, true /* add */); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Sets whether the specified UID is prevented from using background data on metered networks. - * Takes precedence over {@link #updateMeteredNetworkAllowList}. + * Removes the specified UID from the list of UIDs that are allowed to use background data on + * metered networks when background data is restricted. The deny list takes precedence over + * the allow list. + * + * @param uid uid of target app + * @throws IllegalStateException if updating allow list failed. + * @hide + */ + @SystemApi(client = MODULE_LIBRARIES) + @RequiresPermission(anyOf = { + android.Manifest.permission.NETWORK_SETTINGS, + android.Manifest.permission.NETWORK_STACK, + NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK + }) + public void removeUidFromMeteredNetworkAllowList(final int uid) { + try { + mService.updateMeteredNetworkAllowList(uid, false /* remove */); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** + * Adds the specified UID to the list of UIDs that are not allowed to use background data on + * metered networks. Takes precedence over {@link #addUidToMeteredNetworkAllowList}. * * @param uid uid of target app * @throws IllegalStateException if updating deny list failed. @@ -5741,9 +5764,32 @@ public class ConnectivityManager { android.Manifest.permission.NETWORK_STACK, NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK }) - public void updateMeteredNetworkDenyList(final int uid, final boolean add) { + public void addUidToMeteredNetworkDenyList(final int uid) { try { - mService.updateMeteredNetworkDenyList(uid, add); + mService.updateMeteredNetworkDenyList(uid, true /* add */); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** + * Removes the specified UID from the list of UIds that can use use background data on metered + * networks if background data is not restricted. The deny list takes precedence over the + * allow list. + * + * @param uid uid of target app + * @throws IllegalStateException if updating deny list failed. + * @hide + */ + @SystemApi(client = MODULE_LIBRARIES) + @RequiresPermission(anyOf = { + android.Manifest.permission.NETWORK_SETTINGS, + android.Manifest.permission.NETWORK_STACK, + NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK + }) + public void removeUidFromMeteredNetworkDenyList(final int uid) { + try { + mService.updateMeteredNetworkDenyList(uid, false /* remove */); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } diff --git a/tests/unit/java/com/android/server/NetworkManagementServiceTest.java b/tests/unit/java/com/android/server/NetworkManagementServiceTest.java index a3b0e7c8ab..7688a6ba5c 100644 --- a/tests/unit/java/com/android/server/NetworkManagementServiceTest.java +++ b/tests/unit/java/com/android/server/NetworkManagementServiceTest.java @@ -250,7 +250,7 @@ public class NetworkManagementServiceTest { mNMService.setUidOnMeteredNetworkDenylist(TEST_UID, true); assertTrue("Should be true since mobile data usage is restricted", mNMService.isNetworkRestricted(TEST_UID)); - verify(mCm).updateMeteredNetworkDenyList(TEST_UID, true /* enabled */); + verify(mCm).addUidToMeteredNetworkDenyList(TEST_UID); mNMService.setDataSaverModeEnabled(true); verify(mNetdService).bandwidthEnableDataSaver(true); @@ -258,16 +258,16 @@ public class NetworkManagementServiceTest { mNMService.setUidOnMeteredNetworkDenylist(TEST_UID, false); assertTrue("Should be true since data saver is on and the uid is not allowlisted", mNMService.isNetworkRestricted(TEST_UID)); - verify(mCm).updateMeteredNetworkDenyList(TEST_UID, true /* false */); + verify(mCm).removeUidFromMeteredNetworkDenyList(TEST_UID); mNMService.setUidOnMeteredNetworkAllowlist(TEST_UID, true); assertFalse("Should be false since data saver is on and the uid is allowlisted", mNMService.isNetworkRestricted(TEST_UID)); - verify(mCm).updateMeteredNetworkAllowList(TEST_UID, true /* enabled */); + verify(mCm).addUidToMeteredNetworkAllowList(TEST_UID); // remove uid from allowlist and turn datasaver off again mNMService.setUidOnMeteredNetworkAllowlist(TEST_UID, false); - verify(mCm).updateMeteredNetworkAllowList(TEST_UID, false /* enabled */); + verify(mCm).removeUidFromMeteredNetworkAllowList(TEST_UID); mNMService.setDataSaverModeEnabled(false); verify(mNetdService).bandwidthEnableDataSaver(false); assertFalse("Network should not be restricted when data saver is off",