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
This commit is contained in:
Chalard Jean
2020-10-09 13:08:00 +09:00
parent e7d24dad51
commit 1f832406ca
2 changed files with 6 additions and 5 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}