From 1f832406ca95fd4e996ab76dc7b45a7cc2ac24d8 Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Fri, 9 Oct 2020 13:08:00 +0900 Subject: [PATCH 1/2] Manage VPN legacy state separately from NetworkInfo This is mainly a cleanup, but is also necessary for the network selection project. This is for network selection ultimately because NetworkSelection needs NetworkAgents to use the newer API introduced in R rather than the legacy internal API. Using that API forbids communicating to ConnectivityService through NetworkInfo, and does not support the FAILED state because there is no usage in connectivity. In VPN, FAILED is used only to communicate a state to Settings and it does this through IConnectivityManager.getLegacyVpnInfo, which already is using an int to communicate this information. Splitting the legacy state from NetworkInfo not only is simpler ultimately because it's the format in which it's consumed, but also will allow removing NetworkInfo completely. Test: FrameworksNetTests NetworkStackTests Bug: 167544279 Change-Id: I8b95e020919e38a5166892221096db6271985574 --- core/java/com/android/internal/net/LegacyVpnInfo.java | 7 +++---- .../core/java/com/android/server/connectivity/Vpn.java | 4 +++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/java/com/android/internal/net/LegacyVpnInfo.java b/core/java/com/android/internal/net/LegacyVpnInfo.java index 4eb7dede77699..43984b59378cb 100644 --- a/core/java/com/android/internal/net/LegacyVpnInfo.java +++ b/core/java/com/android/internal/net/LegacyVpnInfo.java @@ -83,8 +83,8 @@ public class LegacyVpnInfo implements Parcelable { * Return best matching {@link LegacyVpnInfo} state based on given * {@link NetworkInfo}. */ - public static int stateFromNetworkInfo(NetworkInfo info) { - switch (info.getDetailedState()) { + public static int stateFromNetworkInfo(NetworkInfo.DetailedState state) { + switch (state) { case CONNECTING: return STATE_CONNECTING; case CONNECTED: @@ -94,8 +94,7 @@ public class LegacyVpnInfo implements Parcelable { case FAILED: return STATE_FAILED; default: - Log.w(TAG, "Unhandled state " + info.getDetailedState() - + " ; treating as disconnected"); + Log.w(TAG, "Unhandled state " + state + " ; treating as disconnected"); return STATE_DISCONNECTED; } } diff --git a/services/core/java/com/android/server/connectivity/Vpn.java b/services/core/java/com/android/server/connectivity/Vpn.java index e3481a3b3bc7e..5ded77c0e6ea1 100644 --- a/services/core/java/com/android/server/connectivity/Vpn.java +++ b/services/core/java/com/android/server/connectivity/Vpn.java @@ -201,6 +201,7 @@ public class Vpn { private final Context mContext; @VisibleForTesting final Dependencies mDeps; private final NetworkInfo mNetworkInfo; + private int mLegacyState = LegacyVpnInfo.STATE_DISCONNECTED; @VisibleForTesting protected String mPackage; private int mOwnerUID; private boolean mIsPackageTargetingAtLeastQ; @@ -440,6 +441,7 @@ public class Vpn { @VisibleForTesting protected void updateState(DetailedState detailedState, String reason) { if (LOGD) Log.d(TAG, "setting state=" + detailedState + ", reason=" + reason); + mLegacyState = LegacyVpnInfo.stateFromNetworkInfo(detailedState); mNetworkInfo.setDetailedState(detailedState, reason, null); if (mNetworkAgent != null) { mNetworkAgent.sendNetworkInfo(mNetworkInfo); @@ -2265,7 +2267,7 @@ public class Vpn { final LegacyVpnInfo info = new LegacyVpnInfo(); info.key = mConfig.user; - info.state = LegacyVpnInfo.stateFromNetworkInfo(mNetworkInfo); + info.state = mLegacyState; if (mNetworkInfo.isConnected()) { info.intent = mStatusIntent; } From e7f602e398b5b4b73798cdf48bb99d32ab72d849 Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Thu, 29 Oct 2020 11:56:03 +0900 Subject: [PATCH 2/2] Fix comments from aosp/1455975 Test: FrameworksNetTests NetworkStackTests Change-Id: I623e29833c22b5c8ab84a74c7f5732423f00d07c --- services/core/java/com/android/server/connectivity/Vpn.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/connectivity/Vpn.java b/services/core/java/com/android/server/connectivity/Vpn.java index 5ded77c0e6ea1..a55df6077cf50 100644 --- a/services/core/java/com/android/server/connectivity/Vpn.java +++ b/services/core/java/com/android/server/connectivity/Vpn.java @@ -201,7 +201,7 @@ public class Vpn { private final Context mContext; @VisibleForTesting final Dependencies mDeps; private final NetworkInfo mNetworkInfo; - private int mLegacyState = LegacyVpnInfo.STATE_DISCONNECTED; + private int mLegacyState; @VisibleForTesting protected String mPackage; private int mOwnerUID; private boolean mIsPackageTargetingAtLeastQ; @@ -416,6 +416,7 @@ public class Vpn { Log.wtf(TAG, "Problem registering observer", e); } + mLegacyState = LegacyVpnInfo.STATE_DISCONNECTED; mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_VPN, 0 /* subtype */, NETWORKTYPE, "" /* subtypeName */); mNetworkCapabilities = new NetworkCapabilities(); @@ -1245,6 +1246,7 @@ public class Vpn { // behaves the same as when it uses the default network. mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); + mLegacyState = LegacyVpnInfo.STATE_CONNECTING; mNetworkInfo.setDetailedState(DetailedState.CONNECTING, null, null); NetworkAgentConfig networkAgentConfig = new NetworkAgentConfig();